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 | """ |
---|
12 | import os.path as ospath |
---|
13 | import sys |
---|
14 | import cPickle |
---|
15 | import imp |
---|
16 | import copy |
---|
17 | import GSASIIpath |
---|
18 | import GSASIIobj as G2obj |
---|
19 | |
---|
20 | def LoadDictFromProjFile(ProjFile): |
---|
21 | ''''Read a GSAS-II project file and load items to dictionary |
---|
22 | :param: str ProjFile: GSAS-II project (name.gpx) full file name |
---|
23 | :returns: dict Project: representation of gpx file following the GSAS-II tree struture |
---|
24 | for each item: key = tree name (e.g. 'Controls','Restraints',etc.), data is dict |
---|
25 | data dict = {'data':item data whch may be list, dict or None,'subitems':subdata (if any)} |
---|
26 | :returns: list nameList: names of main tree entries & subentries used to reconstruct project file |
---|
27 | Example for fap.gpx: |
---|
28 | Project = { #NB:dict order is not tree order |
---|
29 | u'Phases':{'data':None,'fap':{phase dict}}, |
---|
30 | u'PWDR FAP.XRA Bank 1':{'data':[histogram data list],'Comments':comments,'Limits':limits, etc}, |
---|
31 | u'Rigid bodies':{'data': {rigid body dict}}, |
---|
32 | u'Covariance':{'data':{covariance data dict}}, |
---|
33 | u'Controls':{'data':{controls data dict}}, |
---|
34 | u'Notebook':{'data':[notebook list]}, |
---|
35 | u'Restraints':{'data':{restraint data dict}}, |
---|
36 | u'Constraints':{'data':{constraint data dict}}]} |
---|
37 | nameList = [ #NB: reproduces tree order |
---|
38 | [u'Notebook',], |
---|
39 | [u'Controls',], |
---|
40 | [u'Covariance',], |
---|
41 | [u'Constraints',], |
---|
42 | [u'Restraints',], |
---|
43 | [u'Rigid bodies',], |
---|
44 | [u'PWDR FAP.XRA Bank 1', |
---|
45 | u'Comments', |
---|
46 | u'Limits', |
---|
47 | u'Background', |
---|
48 | u'Instrument Parameters', |
---|
49 | u'Sample Parameters', |
---|
50 | u'Peak List', |
---|
51 | u'Index Peak List', |
---|
52 | u'Unit Cells List', |
---|
53 | u'Reflection Lists'], |
---|
54 | [u'Phases', |
---|
55 | u'fap']] |
---|
56 | ''' |
---|
57 | if not ospath.exists(ProjFile): |
---|
58 | print ('\n*** Error attempt to open project file that does not exist:\n '+ |
---|
59 | str(ProjFile)) |
---|
60 | return |
---|
61 | file = open(ProjFile,'rb') |
---|
62 | print 'loading from file: ',ProjFile |
---|
63 | Project = {} |
---|
64 | nameList = [] |
---|
65 | try: |
---|
66 | while True: |
---|
67 | try: |
---|
68 | data = cPickle.load(file) |
---|
69 | except EOFError: |
---|
70 | break |
---|
71 | datum = data[0] |
---|
72 | Project[datum[0]] = {'data':datum[1]} |
---|
73 | nameList.append([datum[0],]) |
---|
74 | for datus in data[1:]: |
---|
75 | Project[datum[0]][datus[0]] = datus[1] |
---|
76 | nameList[-1].append(datus[0]) |
---|
77 | file.close() |
---|
78 | print('project load successful') |
---|
79 | except: |
---|
80 | print("Error reading file "+str(ProjFile)+". This is not a GSAS-II .gpx file") |
---|
81 | return None |
---|
82 | return Project,nameList |
---|
83 | |
---|
84 | def SaveDictToProjFile(Project,nameList,ProjFile): |
---|
85 | 'Save a GSAS-II project file from dictionary/nameList created by LoadDictFromProjFile' |
---|
86 | file = open(ProjFile,'wb') |
---|
87 | print 'save to file: ',ProjFile |
---|
88 | for name in nameList: |
---|
89 | print name |
---|
90 | data = [] |
---|
91 | item = Project[name[0]] |
---|
92 | print item |
---|
93 | data.append([name[0],item['data']]) |
---|
94 | for item2 in name[1:]: |
---|
95 | data.append([item2,item[item2]]) |
---|
96 | cPickle.dump(data,file,1) |
---|
97 | file.close() |
---|
98 | print('project save successful') |
---|
99 | |
---|
100 | def ImportPowder(reader,filename): |
---|
101 | readerlist = ['G2pwd_fxye','G2pwd_xye','G2pwd_BrukerRAW','G2pwd_csv','G2pwd_FP', |
---|
102 | 'G2pwd_Panalytical','G2pwd_rigaku'] |
---|
103 | if reader not in readerlist: |
---|
104 | print '**** ERROR: unrecognized reader ',reader |
---|
105 | return None |
---|
106 | rdfile,rdpath,descr = imp.find_module(reader) |
---|
107 | rdclass = imp.load_module(reader,rdfile,rdpath,descr) |
---|
108 | rd = rdclass.GSAS_ReaderClass() |
---|
109 | fl = open(filename,'rb') |
---|
110 | rdlist = [] |
---|
111 | if rd.ContentsValidator(fl): |
---|
112 | fl.seek(0) |
---|
113 | repeat = True |
---|
114 | rdbuffer = {} # create temporary storage for file reader |
---|
115 | block = 0 |
---|
116 | while repeat: # loop if the reader asks for another pass on the file |
---|
117 | block += 1 |
---|
118 | repeat = False |
---|
119 | rd.objname = ospath.basename(filename) |
---|
120 | flag = rd.Reader(filename,fl,None,buffer=rdbuffer,blocknum=block,) |
---|
121 | if flag: |
---|
122 | rdlist.append(copy.deepcopy(rd)) # save the result before it is written over |
---|
123 | if rd.repeat: |
---|
124 | repeat = True |
---|
125 | return rdlist |
---|
126 | print rd.errors |
---|
127 | return None |
---|
128 | |
---|
129 | |
---|
130 | def main(): |
---|
131 | 'Needs a doc string' |
---|
132 | arg = sys.argv |
---|
133 | print arg |
---|
134 | if len(arg) > 1: |
---|
135 | GPXfile = arg[1] |
---|
136 | if not ospath.exists(GPXfile): |
---|
137 | print 'ERROR - ',GPXfile," doesn't exist!" |
---|
138 | exit() |
---|
139 | Project,nameList = LoadDictFromProjFile(GPXfile) |
---|
140 | SaveDictToProjFile(Project,nameList,'testout.gpx') |
---|
141 | else: |
---|
142 | print 'ERROR - missing filename' |
---|
143 | exit() |
---|
144 | print "Done" |
---|
145 | |
---|
146 | if __name__ == '__main__': |
---|
147 | main() |
---|