Changeset 3811


Ignore:
Timestamp:
Feb 4, 2019 3:35:43 PM (3 years ago)
Author:
toby
Message:

scriptable: fix reading py2 gpx in py3 & use of multi-set instprm files

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIscriptable.py

    r3809 r3811  
    571571import GSASIIpwd as G2pwd
    572572import GSASIIstrMain as G2strMain
     573import GSASIIstrIO as G2strIO
    573574import GSASIIspc as G2spc
    574575import GSASIIElem as G2elem
     
    620621
    621622      Project = {                 #NB:dict order is not tree order
    622         u'Phases':{'data':None,'fap':{phase dict}},
    623         u'PWDR FAP.XRA Bank 1':{'data':[histogram data list],'Comments':comments,'Limits':limits, etc},
    624         u'Rigid bodies':{'data': {rigid body dict}},
    625         u'Covariance':{'data':{covariance data dict}},
    626         u'Controls':{'data':{controls data dict}},
    627         u'Notebook':{'data':[notebook list]},
    628         u'Restraints':{'data':{restraint data dict}},
    629         u'Constraints':{'data':{constraint data dict}}]
     623        'Phases':{'data':None,'fap':{phase dict}},
     624        'PWDR FAP.XRA Bank 1':{'data':[histogram data list],'Comments':comments,'Limits':limits, etc},
     625        'Rigid bodies':{'data': {rigid body dict}},
     626        'Covariance':{'data':{covariance data dict}},
     627        'Controls':{'data':{controls data dict}},
     628        'Notebook':{'data':[notebook list]},
     629        'Restraints':{'data':{restraint data dict}},
     630        'Constraints':{'data':{constraint data dict}}]
    630631        }
    631632      nameList = [                #NB: reproduces tree order
    632         [u'Notebook',],
    633         [u'Controls',],
    634         [u'Covariance',],
    635         [u'Constraints',],
    636         [u'Restraints',],
    637         [u'Rigid bodies',],
    638         [u'PWDR FAP.XRA Bank 1',
    639              u'Comments',
    640              u'Limits',
    641              u'Background',
    642              u'Instrument Parameters',
    643              u'Sample Parameters',
    644              u'Peak List',
    645              u'Index Peak List',
    646              u'Unit Cells List',
    647              u'Reflection Lists'],
    648         [u'Phases', u'fap']
     633        ['Notebook',],
     634        ['Controls',],
     635        ['Covariance',],
     636        ['Constraints',],
     637        ['Restraints',],
     638        ['Rigid bodies',],
     639        ['PWDR FAP.XRA Bank 1',
     640             'Comments',
     641             'Limits',
     642             'Background',
     643             'Instrument Parameters',
     644             'Sample Parameters',
     645             'Peak List',
     646             'Index Peak List',
     647             'Unit Cells List',
     648             'Reflection Lists'],
     649        ['Phases', 'fap']
    649650        ]
    650651    '''
    651652    # Let IOError be thrown if file does not exist
    652     # if not ospath.exists(ProjFile):
    653     #     print ('\n*** Error attempt to open project file that does not exist:\n   '+
    654     #         str(ProjFile))
    655     #     return
    656     file = open(ProjFile,'rb')
    657     # print('loading from file: {}'.format(ProjFile))
    658     Project = {}
    659     nameList = []
     653    if not ospath.exists(ProjFile):
     654        print ('\n*** Error attempt to open project file that does not exist: \n    {}'.
     655                   format(ProjFile))
     656        raise IOError('GPX file {} does not exist'.format(ProjFile))
    660657    try:
    661         while True:
    662             try:
    663                 data = cPickle.load(file)
    664             except EOFError:
    665                 break
    666             datum = data[0]
    667             Project[datum[0]] = {'data':datum[1]}
    668             nameList.append([datum[0],])
    669             for datus in data[1:]:
    670                 Project[datum[0]][datus[0]] = datus[1]
    671                 nameList[-1].append(datus[0])
    672         file.close()
    673         # print('project load successful')
    674     except:
    675         raise IOError("Error reading file "+str(ProjFile)+". This is not a GSAS-II .gpx file")
    676     finally:
    677         file.close()
     658        Project, nameList = G2strIO.GetFullGPX(ProjFile)
     659    except Exception as msg:
     660        raise IOError(msg)
    678661    return Project,nameList
    679662
     
    10361019
    10371020    if ext.lower() == '.instprm':
    1038         # New GSAS File, load appropriately
    1039         # this likely needs some work as there can now be more than one bank
     1021        # New GSAS File, load appropriate bank
    10401022        with open(instfile) as f:
    10411023            lines = f.readlines()
    1042         bank = reader.powderentry[2] # this may not be the right entry to use
     1024        if bank is None:
     1025            bank = reader.powderentry[2]
    10431026        numbanks = reader.numbanks
    10441027        iparms = G2fil.ReadPowderInstprm(lines, bank, numbanks, reader)
    1045 
    10461028        reader.instfile = instfile
    1047         reader.instmsg = 'GSAS-II file' + instfile
     1029        reader.instmsg = '{} (G2 fmt) bank {}'.format(instfile,bank)
    10481030        return iparms
    10491031    elif ext.lower() not in ('.prm', '.inst', '.ins'):
     
    10751057    reader.powderentry[2] = 1
    10761058    reader.instfile = instfile
    1077     reader.instmsg = instfile + ' bank ' + str(reader.instbank)
     1059    reader.instmsg = '{} bank {}'.format(instfile,reader.instbank)
    10781060    return G2fil.SetPowderInstParms(Iparm, reader)
    10791061
  • trunk/GSASIIstrIO.py

    r3803 r3811  
    4949       return cPickle.load(fp,encoding='latin-1')
    5050
     51def GetFullGPX(GPXfile):
     52    ''' Returns complete contents of GSASII gpx file
     53
     54    :param str GPXfile: full .gpx file name
     55    :returns: Project,nameList, where
     56
     57      * Project (dict) is a representation of gpx file following the GSAS-II tree structure
     58        for each item: key = tree name (e.g. 'Controls','Restraints',etc.), data is dict
     59        data dict = {'data':item data whch may be list, dict or None,'subitems':subdata (if any)}
     60      * nameList (list) has names of main tree entries & subentries used to reconstruct project file
     61    '''
     62    fp = open(GPXfile,'rb')
     63    Project = {}
     64    nameList = []
     65    try:
     66        while True:
     67            try:
     68                data = cPickleLoad(fp)
     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        # print('project load successful')
     78    except Exception as msg:
     79        print('Read Error:',msg)
     80        raise Exception("Error reading file "+str(GPXfile)+". This is not a GSAS-II .gpx file")
     81    finally:
     82        fp.close()
     83    return Project,nameList
     84   
    5185def GetControls(GPXfile):
    5286    ''' Returns dictionary of control items found in GSASII gpx file
Note: See TracChangeset for help on using the changeset viewer.