source: trunk/GSASIIscriptable.py @ 2816

Last change on this file since 2816 was 2816, checked in by vondreele, 6 years ago

add new module GSASIIscriptable.py which has routines for reading/writing project file to dictionary

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