source: trunk/GSASIIscriptable.py @ 2820

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

PWDR import now scriptable for single bank patterns

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