Changeset 3809


Ignore:
Timestamp:
Feb 3, 2019 10:35:00 AM (5 years ago)
Author:
toby
Message:

update scriptable to handle banks in data/inst files

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIIO.py

    r3806 r3809  
    604604        return
    605605    LastSavedUsing = None
    606     file = open(G2frame.GSASprojectfile,'rb')
     606    filep = open(G2frame.GSASprojectfile,'rb')
    607607    if showProvenance: print ('loading from file: '+G2frame.GSASprojectfile)
    608608    wx.BeginBusyCursor()
     
    611611            try:
    612612                if '2' in platform.python_version_tuple()[0]:
    613                     data = cPickle.load(file)
     613                    data = cPickle.load(filep)
    614614                else:
    615                     data = cPickle.load(file,encoding='latin-1')
     615                    data = cPickle.load(filep,encoding='latin-1')
    616616            except EOFError:
    617617                break
     
    667667                if Data['setDefault']:
    668668                    G2frame.imageDefault = Data               
    669         file.close()
    670669        if LastSavedUsing:
    671670            print('GPX load successful. Last saved with GSAS-II revision '+LastSavedUsing)
     
    683682        msg.ShowModal()
    684683    finally:
     684        filep.close()
    685685        wx.EndBusyCursor()
    686686        G2frame.Status.SetStatusText('Mouse RB drag/drop to reorder',0)
  • trunk/GSASIIscriptable.py

    r3506 r3809  
    957957    pass
    958958
    959 def import_generic(filename, readerlist, fmthint=None):
     959def import_generic(filename, readerlist, fmthint=None, bank=None):
    960960    """Attempt to import a filename, using a list of reader objects.
    961961
     
    979979            # Initialize reader
    980980            rd.selections = []
     981            if bank is None:
     982                rd.selections = []
     983            else:
     984                rd.selections = [bank-1]
    981985            rd.dnames = []
    982986            rd.ReInitialize()
     
    985989            if not rd.ContentsValidator(filename):
    986990                # Report error
    987                 pass
     991                print("Warning: File {} has a validation error, continuing".format(filename))
    988992            if len(rd.selections) > 1:
    989                 # Select data?
    990                 # GSASII.py:543
    991                 raise G2ImportException("Not sure what data to select")
     993                raise G2ImportException("File {} has {} banks. Specify which bank to read with databank param."
     994                                .format(filename,len(rd.selections)))
    992995
    993996            block = 0
     
    10131016                    print("Read warning by", rd.formatName, "reader:",
    10141017                          rd.warnings, file=sys.stderr)
     1018                elif bank is None:
     1019                    print("{} read by Reader {}"
     1020                              .format(filename,rd.formatName))
    10151021                else:
    1016                     print("{} read by Reader {}\n".format(filename,rd.formatName))                   
     1022                    print("{} block # {} read by Reader {}"
     1023                              .format(filename,bank,rd.formatName))
    10171024                return rd_list
    10181025    raise G2ImportException("No reader could read file: " + filename)
    10191026
    10201027
    1021 def load_iprms(instfile, reader):
     1028def load_iprms(instfile, reader, bank=None):
    10221029    """Loads instrument parameters from a file, and edits the
    10231030    given reader.
     
    10301037    if ext.lower() == '.instprm':
    10311038        # New GSAS File, load appropriately
     1039        # this likely needs some work as there can now be more than one bank
    10321040        with open(instfile) as f:
    10331041            lines = f.readlines()
    1034         bank = reader.powderentry[2]
     1042        bank = reader.powderentry[2] # this may not be the right entry to use
    10351043        numbanks = reader.numbanks
    10361044        iparms = G2fil.ReadPowderInstprm(lines, bank, numbanks, reader)
     
    10501058            Iparm[line[:12]] = line[12:-1]
    10511059    ibanks = int(Iparm.get('INS   BANK  ', '1').strip())
    1052     if ibanks == 1:
     1060    if bank is not None:
     1061        # pull out requested bank # bank from the data, and change the bank to 1
     1062        Iparm,IparmC = {},Iparm
     1063        for key in IparmC:
     1064            if 'INS' not in key[:3]: continue   #skip around rubbish lines in some old iparm
     1065            if key[4:6] == "  ":
     1066                Iparm[key] = IparmC[key]
     1067            elif int(key[4:6].strip()) == bank:
     1068                Iparm[key[:4]+' 1'+key[6:]] = IparmC[key]           
     1069        reader.instbank = bank
     1070    elif ibanks == 1:
    10531071        reader.instbank = 1
    1054         reader.powderentry[2] = 1
    1055         reader.instfile = instfile
    1056         reader.instmsg = instfile + ' bank ' + str(reader.instbank)
    1057         return G2fil.SetPowderInstParms(Iparm, reader)
    1058     # TODO handle >1 banks
    1059     raise NotImplementedError("Check GSASIIfiles.py: ReadPowderInstprm")
    1060 
    1061 def load_pwd_from_reader(reader, instprm, existingnames=[]):
     1072    else:
     1073        raise G2ImportException("Instrument parameter file has {} banks, select one with instbank param."
     1074                                    .format(ibanks))
     1075    reader.powderentry[2] = 1
     1076    reader.instfile = instfile
     1077    reader.instmsg = instfile + ' bank ' + str(reader.instbank)
     1078    return G2fil.SetPowderInstParms(Iparm, reader)
     1079
     1080def load_pwd_from_reader(reader, instprm, existingnames=[],bank=None):
    10621081    """Loads powder data from a reader object, and assembles it into a GSASII data tree.
    10631082
     
    10721091        Iparm1, Iparm2 = instprm
    10731092    except ValueError:
    1074         Iparm1, Iparm2 = load_iprms(instprm, reader)
    1075 
     1093        Iparm1, Iparm2 = load_iprms(instprm, reader, bank=bank)
     1094        print('Instrument parameters read:',reader.instmsg)
    10761095    Ymin = np.min(reader.powderdata[1])
    10771096    Ymax = np.max(reader.powderdata[1])
     
    13001319        SaveDictToProjFile(self.data, self.names, self.filename)
    13011320
    1302     def add_powder_histogram(self, datafile, iparams, phases=[], fmthint=None):
     1321    def add_powder_histogram(self, datafile, iparams, phases=[], fmthint=None,
     1322                                 databank=None, instbank=None):
    13031323        """Loads a powder data histogram into the project.
    13041324
     
    13151335          importers consistent with the file extension will be tried
    13161336          (equivalent to "guess format" in menu).
     1337        :param int databank: Specifies a dataset number to read, if file contains
     1338          more than set of data. This should be 1 to read the first bank in
     1339          the file (etc.) regardless of the number on the Bank line, etc.
     1340          Default is None which means there should only be one dataset in the
     1341          file.
     1342        :param int instbank: Specifies an instrument parameter set to read, if
     1343          the instrument parameter file contains more than set of parameters.
     1344          This will match the INS # in an GSAS type file so it will typically
     1345          be 1 to read the first parameter set in the file (etc.)
     1346          Default is None which means there should only be one parameter set
     1347          in the file.
    13171348
    13181349        :returns: A :class:`G2PwdrData` object representing
     
    13221353        datafile = os.path.abspath(os.path.expanduser(datafile))
    13231354        iparams = os.path.abspath(os.path.expanduser(iparams))
    1324         pwdrreaders = import_generic(datafile, PwdrDataReaders,fmthint=fmthint)
     1355        pwdrreaders = import_generic(datafile, PwdrDataReaders,fmthint=fmthint,bank=databank)
    13251356        histname, new_names, pwdrdata = load_pwd_from_reader(
    13261357                                          pwdrreaders[0], iparams,
    1327                                           [h.name for h in self.histograms()])
     1358                                          [h.name for h in self.histograms()],bank=instbank)
    13281359        if histname in self.data:
    13291360            print("Warning - redefining histogram", histname)
Note: See TracChangeset for help on using the changeset viewer.