source: trunk/GSASIIscriptable.py @ 2817

Last change on this file since 2817 was 2817, checked in by vondreele, 5 years ago

major revision - move all importers to GSASIIobj & make them independent of wx so they can be used in a scripting environment.
Still to move are PhaseSelector?, and 3 BlockSelector? dialogs

File size: 4.2 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: 2017-04-12 15:12:45 -0500 (Wed, 12 Apr 2017) $
4# $Author: vondreele $
5# $Revision: 2777 $
6# $URL: https://subversion.xray.aps.anl.gov/pyGSAS/trunk/GSASIIscriptable.py $
7# $Id: GSASIIIO.py 2777 2017-04-12 20:12:45Z vondreele $
8########### SVN repository information ###################
9"""
10
11"""
12import os.path as ospath
13import sys
14import cPickle
15import GSASIIpath
16import GSASIIobj as G2obj
17
18def LoadDictFromProjFile(ProjFile):
19    ''''Read a GSAS-II project file and load items to dictionary
20    :param: str ProjFile: GSAS-II project (name.gpx) full file name
21    :returns: dict Project: representation of gpx file following the GSAS-II tree struture
22        for each item: key = tree name (e.g. 'Controls','Restraints',etc.), data is dict
23        data dict = {'data':item data whch may be list, dict or None,'subitems':subdata (if any)}
24    :returns: list nameList: names of main tree entries & subentries used to reconstruct project file
25    Example for fap.gpx:
26    Project = {                 #NB:dict order is not tree order
27        u'Phases':{'data':None,'fap':{phase dict}},
28        u'PWDR FAP.XRA Bank 1':{'data':[histogram data list],'Comments':comments,'Limits':limits, etc},
29        u'Rigid bodies':{'data': {rigid body dict}},
30        u'Covariance':{'data':{covariance data dict}},
31        u'Controls':{'data':{controls data dict}},
32        u'Notebook':{'data':[notebook list]},
33        u'Restraints':{'data':{restraint data dict}},
34        u'Constraints':{'data':{constraint data dict}}]}
35    nameList = [                #NB: reproduces tree order
36        [u'Notebook',],
37        [u'Controls',],
38        [u'Covariance',],
39        [u'Constraints',],
40        [u'Restraints',],
41        [u'Rigid bodies',],
42        [u'PWDR FAP.XRA Bank 1',
43             u'Comments',
44             u'Limits',
45             u'Background',
46             u'Instrument Parameters',
47             u'Sample Parameters',
48             u'Peak List',
49             u'Index Peak List',
50             u'Unit Cells List',
51             u'Reflection Lists'],
52        [u'Phases',
53             u'fap']]
54    '''
55    if not ospath.exists(ProjFile):
56        print ('\n*** Error attempt to open project file that does not exist:\n   '+
57            str(ProjFile))
58        return
59    file = open(ProjFile,'rb')
60    print 'loading from file: ',ProjFile
61    Project = {}
62    nameList = []
63    try:
64        while True:
65            try:
66                data = cPickle.load(file)
67            except EOFError:
68                break
69            datum = data[0]
70            Project[datum[0]] = {'data':datum[1]}
71            nameList.append([datum[0],])
72            for datus in data[1:]:
73                Project[datum[0]][datus[0]] = datus[1]
74                nameList[-1].append(datus[0])
75        file.close()
76        print('project load successful')
77    except:
78        print("Error reading file "+str(ProjFile)+". This is not a GSAS-II .gpx file")
79        return None
80    return Project,nameList
81   
82def SaveDictToProjFile(Project,nameList,ProjFile):
83    'Save a GSAS-II project file from dictionary/nameList created by LoadDictFromProjFile'
84    file = open(ProjFile,'wb')
85    print 'save to file: ',ProjFile
86    for name in nameList:
87        print name
88        data = []
89        item = Project[name[0]]
90        print item
91        data.append([name[0],item['data']])
92        for item2 in name[1:]:
93            data.append([item2,item[item2]])
94        cPickle.dump(data,file,1)
95    file.close()
96    print('project save successful')
97   
98def ImportPowder(filename,reader):
99    print 'importer'
100   
101
102def main():
103    'Needs a doc string'
104    arg = sys.argv
105    print arg
106    if len(arg) > 1:
107        GPXfile = arg[1]
108        if not ospath.exists(GPXfile):
109            print 'ERROR - ',GPXfile," doesn't exist!"
110            exit()
111        Project,nameList = LoadDictFromProjFile(GPXfile)
112        SaveDictToProjFile(Project,nameList,'testout.gpx')
113    else:
114        print 'ERROR - missing filename'
115        exit()
116    print "Done"
117         
118if __name__ == '__main__':
119    main()
Note: See TracBrowser for help on using the repository browser.