Changeset 688


Ignore:
Timestamp:
Jul 13, 2012 4:19:26 PM (11 years ago)
Author:
toby
Message:

use instprm or .prm files as input for histogram import

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASII.py

    r687 r688  
    532532        self.Bind(wx.EVT_MENU, self.OnImportPowder, id=item.GetId())
    533533           
     534    def ReadPowderInstprm(self,instfile):
     535        '''Read a GSAS-II (new) instrument parameter file'''
     536        if os.path.splitext(instfile)[1].lower() != '.instprm': # invalid file
     537            return None           
     538        if not os.path.exists(instfile): # no such file
     539            return None
     540        File = open(instfile,'r')
     541        S = File.readline()
     542        if not S.startswith('#GSAS-II'): # not a valid file
     543            File.close()
     544            return None
     545        newItems = []
     546        newVals = []
     547        while S:
     548            if S[0] == '#':
     549                S = File.readline()
     550                continue
     551            [item,val] = S[:-1].split(':')
     552            newItems.append(item)
     553            try:
     554                newVals.append(float(val))
     555            except ValueError:
     556                newVals.append(val)                       
     557            S = File.readline()               
     558        File.close()
     559        return [tuple(newVals),newVals,len(newVals)*[False,],newItems]
     560       
    534561    def ReadPowderIparm(self,instfile,bank,databanks,rd):
    535562        '''Read a GSAS (old) instrument parameter file'''
     
    557584            # choice from a human here
    558585            choices = []
    559             for i in range(1,1+len(ibanks)):
     586            for i in range(1,1+ibanks):
    560587                choices.append('Bank '+str(i))
    561588            bank = rd.BlockSelector(
    562589                choices, self,
    563590                title='Select an instrument parameter block for '+
    564                 rd.powderentry[0]+' block '+str(bank)+
    565                 '\nCancel: Use default settings',
     591                os.path.split(rd.powderentry[0])[1]+' block '+str(bank)+
     592                '\nOr use Cancel to select from the default parameter sets',
    566593                header='Block Selector')
    567594        if bank is None: return {}
     
    578605    def GetPowderIparm(self,rd, prevIparm, lastIparmfile, lastdatafile):
    579606        '''Open and read an instrument parameter file for a data file
    580         for now, just use old GSAS type files, but someday allow other options.
    581         Then update SetPowderInstParms to work with that input
     607        Returns the list of parameters used in the data tree
    582608        '''
     609        def SetPowderInstParms(Iparm, rd):
     610            '''extracts values from instrument parameters in rd.instdict
     611            or in array Iparm.
     612            Create and return the contents of the instrument parameter tree entry.
     613            '''
     614            DataType = Iparm['INS   HTYPE '].strip()[0:3]  # take 1st 3 chars
     615            # override inst values with values read from data file
     616            if rd.instdict.get('type'):
     617                DataType = rd.instdict.get('type')
     618            wave1 = None
     619            wave2 = 0.0
     620            if rd.instdict.get('wave'):
     621                wl = rd.instdict.get('wave')
     622                wave1 = wl[0]
     623                if len(wl) > 1: wave2 = wl[1]
     624            data = [DataType,]
     625            if 'C' in DataType:
     626                s = Iparm['INS  1 ICONS']
     627                if not wave1:
     628                    wave1 = G2IO.sfloat(s[:10])
     629                    wave2 = G2IO.sfloat(s[10:20])
     630                v = (wave1,wave2,
     631                     G2IO.sfloat(s[20:30]),G2IO.sfloat(s[55:65]),G2IO.sfloat(s[40:50])) #get lam1, lam2, zero, pola & ratio
     632                if not v[1]:
     633                    names = ['Type','Lam','Zero','Polariz.','U','V','W','X','Y','SH/L','Azimuth']
     634                    v = (v[0],v[2],v[4])
     635                    codes = [0,0,0,0]
     636                else:
     637                    names = ['Type','Lam1','Lam2','Zero','I(L2)/I(L1)','Polariz.','U','V','W','X','Y','SH/L','Azimuth']
     638                    codes = [0,0,0,0,0,0]
     639                data.extend(v)
     640                v1 = Iparm['INS  1PRCF1 '].split()                                                 
     641                v = Iparm['INS  1PRCF11'].split()
     642                data.extend([float(v[0]),float(v[1]),float(v[2])])                  #get GU, GV & GW - always here
     643                azm = Iparm.get('INS  1DETAZM')
     644                if azm is None: #not in this Iparm file
     645                    azm = 0.0
     646                else:
     647                    azm = float(azm)
     648                v = Iparm['INS  1PRCF12'].split()
     649                if v1[0] == 3:
     650                    data.extend([float(v[0]),float(v[1]),float(v[2])+float(v[3],azm)])  #get LX, LY, S+H/L & azimuth
     651                else:
     652                    data.extend([0.0,0.0,0.002,azm])                                      #OK defaults if fxn #3 not 1st in iprm file
     653                codes.extend([0,0,0,0,0,0,0])
     654                return [tuple(data),data,codes,names]
     655
    583656        # stuff we might need from the reader
    584657        filename = rd.powderentry[0]
     
    595668            if os.path.exists(instfile):
    596669                #print 'debug: try read',instfile
     670                instParmList = self.ReadPowderInstprm(instfile)
     671                if instParmList is not None:
     672                    rd.instfile = instfile
     673                    rd.instmsg = 'GSAS-II file '+instfile
     674                    return instParmList
    597675                Iparm = self.ReadPowderIparm(instfile,bank,numbanks,rd)
    598676                if Iparm:
     
    600678                    rd.instfile = instfile
    601679                    rd.instmsg = instfile + ' bank ' + str(rd.instbank)
    602                     return Iparm
     680                    return SetPowderInstParms(Iparm,rd)
    603681            else:
    604682                self.ErrorDialog('Open Error',
     
    609687        # with extension .inst or .prm? If so read it
    610688        basename = os.path.splitext(filename)[0]
    611         for ext in '.inst','.prm':
     689        for ext in '.instprm','.prm','.inst','.ins':
    612690            instfile = basename + ext
     691            instParmList = self.ReadPowderInstprm(instfile)
     692            if instParmList is not None:
     693                rd.instfile = instfile
     694                rd.instmsg = 'GSAS-II file '+instfile
     695                return instParmList
    613696            Iparm = self.ReadPowderIparm(instfile,bank,numbanks,rd)
    614697            if Iparm:
     
    616699                rd.instfile = instfile
    617700                rd.instmsg = instfile + ' bank ' + str(rd.instbank)
    618                 return Iparm
     701                return SetPowderInstParms(Iparm,rd)
    619702            else:
    620 #                print 'debug: open/read failed',instfile
     703                #print 'debug: open/read failed',instfile
    621704                pass # fail silently
    622705
     
    624707        # instrument parameter file
    625708        if self.zipfile:
    626             for ext in '.inst','.prm':
     709            for ext in '.instprm','.prm','.inst','.ins':
    627710                instfile = G2IO.ExtractFileFromZip(
    628711                    self.zipfile,
     
    631714                if instfile is not None and instfile != self.zipfile:
    632715                    print 'debug:',instfile,'created from ',self.zipfile
     716                    instParmList = self.ReadPowderInstprm(instfile)
     717                    if instParmList is not None:
     718                        rd.instfile = instfile
     719                        rd.instmsg = 'GSAS-II file '+instfile
     720                        return instParmList
    633721                    Iparm = self.ReadPowderIparm(instfile,bank,numbanks,rd)
    634722                    if Iparm:
    635723                        rd.instfile = instfile
    636724                        rd.instmsg = instfile + ' bank ' + str(rd.instbank)
    637                         return Iparm
     725                        return SetPowderInstParms(Iparm,rd)
    638726                    else:
    639                         print 'debug: open/read for',instfile,'from',self.zipfile,'failed'
     727                        #print 'debug: open/read for',instfile,'from',self.zipfile,'failed'
    640728                        pass # fail silently
    641729
    642730        while True: # loop until we get a file that works or we get a cancel
    643731            instfile = ''
    644             dlg = wx.FileDialog(self,
    645                                 'Choose inst parm file for "'
    646                                 +rd.idstring
    647                                 +'" (Cancel for defaults)',
    648                                 '.', '',
    649                                 'GSAS iparm file (*.prm)|*.prm|All files(*.*)|*.*',
    650                                 wx.OPEN|wx.CHANGE_DIR)
     732            dlg = wx.FileDialog(
     733                self,
     734                'Choose inst. param file for "'
     735                +rd.idstring
     736                +'" (or Cancel for default)',
     737                '.', '',
     738                'GSAS-II iparm file (*.instprm)|*.instprm|'
     739                'GSAS iparm file (*.prm)|*.prm|'
     740                'GSAS iparm file (*.inst)|*.inst|'
     741                'GSAS iparm file (*.ins)|*.ins|'
     742                'All files (*.*)|*.*',
     743                wx.OPEN|wx.CHANGE_DIR)
    651744            if os.path.exists(lastIparmfile):
    652745                dlg.SetFilename(lastIparmfile)
     
    655748            dlg.Destroy()
    656749            if not instfile: break
     750            instParmList = self.ReadPowderInstprm(instfile)
     751            if instParmList is not None:
     752                rd.instfile = instfile
     753                rd.instmsg = 'GSAS-II file '+instfile
     754                return instParmList
    657755            Iparm = self.ReadPowderIparm(instfile,bank,numbanks,rd)
    658756            if Iparm:
     
    660758                rd.instfile = instfile
    661759                rd.instmsg = instfile + ' bank ' + str(rd.instbank)
    662                 return Iparm
     760                return SetPowderInstParms(Iparm,rd)
    663761            else:
    664762                self.ErrorDialog('Read Error',
     
    680778            rd.instfile = ''
    681779            rd.instmsg = 'default: '+rd.defaultIparm_lbl[res]
    682             return rd.defaultIparms[res]
    683 
    684     def SetPowderInstParms(self, Iparm, rd):
    685         '''extracts values from instrument parameter file and creates
    686         the contents of the instrument parameter tree entry
    687         '''
    688         DataType = Iparm['INS   HTYPE '].strip()[0:3]  # take 1st 3 chars
    689         # override inst values with values read from data file
    690         if rd.instdict.get('type'):
    691             DataType = rd.instdict.get('type')
    692         wave1 = None
    693         wave2 = 0.0
    694         if rd.instdict.get('wave'):
    695             wl = rd.instdict.get('wave')
    696             wave1 = wl[0]
    697             if len(wl) > 1: wave2 = wl[1]
    698         data = [DataType,]
    699         if 'C' in DataType:
    700             s = Iparm['INS  1 ICONS']
    701             if not wave1:
    702                 wave1 = G2IO.sfloat(s[:10])
    703                 wave2 = G2IO.sfloat(s[10:20])
    704             v = (wave1,wave2,
    705                  G2IO.sfloat(s[20:30]),G2IO.sfloat(s[55:65]),G2IO.sfloat(s[40:50])) #get lam1, lam2, zero, pola & ratio
    706             if not v[1]:
    707                 names = ['Type','Lam','Zero','Polariz.','U','V','W','X','Y','SH/L','Azimuth']
    708                 v = (v[0],v[2],v[4])
    709                 codes = [0,0,0,0]
    710             else:
    711                 names = ['Type','Lam1','Lam2','Zero','I(L2)/I(L1)','Polariz.','U','V','W','X','Y','SH/L','Azimuth']
    712                 codes = [0,0,0,0,0,0]
    713             data.extend(v)
    714             v1 = Iparm['INS  1PRCF1 '].split()                                                 
    715             v = Iparm['INS  1PRCF11'].split()
    716             data.extend([float(v[0]),float(v[1]),float(v[2])])                  #get GU, GV & GW - always here
    717             azm = Iparm.get('INS  1DETAZM')
    718             if azm is None: #not in this Iparm file
    719                 azm = 0.0
    720             else:
    721                 azm = float(azm)
    722             v = Iparm['INS  1PRCF12'].split()
    723             if v1[0] == 3:
    724                 data.extend([float(v[0]),float(v[1]),float(v[2])+float(v[3],azm)])  #get LX, LY, S+H/L & azimuth
    725             else:
    726                 data.extend([0.0,0.0,0.002,azm])                                      #OK defaults if fxn #3 not 1st in iprm file
    727             codes.extend([0,0,0,0,0,0,0])
    728             return [tuple(data),data,codes,names]
     780            return SetPowderInstParms(rd.defaultIparms[res],rd)
    729781
    730782    def OnImportPowder(self,event):
     
    743795        for rd in rdlist:
    744796            # get instrument parameters for each dataset
    745             Iparm = self.GetPowderIparm(rd, Iparm, lastIparmfile, lastdatafile)
     797            instParmList = self.GetPowderIparm(rd, Iparm, lastIparmfile, lastdatafile)
    746798            lastIparmfile = rd.instfile
    747799            lastdatafile = rd.powderentry[0]
     
    771823            self.PatternTree.SetItemPyData(
    772824                self.PatternTree.AppendItem(Id,text='Instrument Parameters'),
    773                 self.SetPowderInstParms(Iparm,rd))
     825                instParmList)
    774826            self.PatternTree.SetItemPyData(
    775827                self.PatternTree.AppendItem(Id,text='Sample Parameters'),
  • trunk/GSASIIIO.py

    r683 r688  
    10871087        #print 'created',self.__class__
    10881088
    1089     def BlockSelector(self, ChoiceList, ParentFrame=None,itle='Select a block',
    1090         size=None, header='Block Selector'):
     1089    def BlockSelector(self, ChoiceList, ParentFrame=None,
     1090                      title='Select a block',
     1091                      size=None, header='Block Selector'):
    10911092        ''' Provide a wx dialog to select a block if the file contains more
    10921093        than one set of data and one must be selected
  • trunk/GSASIIpwdGUI.py

    r687 r688  
    88# $Id$
    99########### SVN repository information ###################
     10import os.path
    1011import wx
    1112import wx.grid as wg
     
    647648                   
    648649    def OnLoad(event):
     650        '''Loads instrument parameters from a G2 .instprm file
     651        in response to the Instrument Parameters-Operations/Load Profile menu
     652       
     653        Note that similar code is found in ReadPowderInstprm (GSASII.py)
     654        '''
    649655        dlg = wx.FileDialog(G2frame, 'Choose GSAS-II instrument parameters file', '.', '',
    650656            'instrument parameter files (*.instprm)|*.instprm',wx.OPEN|wx.CHANGE_DIR)
     
    676682       
    677683    def OnSave(event):
     684        '''Respond to the Instrument Parameters Operations/Save Profile menu
     685        item: writes current parameters to a .instprm file
     686        '''
    678687        dlg = wx.FileDialog(G2frame, 'Choose GSAS-II instrument parameters file', '.', '',
    679688            'instrument parameter files (*.instprm)|*.instprm',wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT|wx.CHANGE_DIR)
     
    681690            if dlg.ShowModal() == wx.ID_OK:
    682691                filename = dlg.GetPath()
     692                # make sure extension is .instprm
     693                filename = os.path.splitext(filename)[0]+'.instprm'
    683694                File = open(filename,'w')
    684695                File.write("#GSAS-II instrument parameter file; do not add/delete or change order of items!\n")
     
    14001411            if ObjId < 3:
    14011412                Obj.SetValue("%.5f"%(controls[6+ObjId]))
     1413
    14021414            else:
    14031415                Obj.SetValue("%.3f"%(controls[6+ObjId]))
Note: See TracChangeset for help on using the changeset viewer.