diff --git a/GSASII/GSASIIscriptable.py b/GSASII/GSASIIscriptable.py index 227f356d..2e066ee4 100644 --- a/GSASII/GSASIIscriptable.py +++ b/GSASII/GSASIIscriptable.py @@ -588,13 +588,36 @@ def load_pwd_from_reader(reader, instprm, existingnames=[],bank=None): HistName = 'PWDR ' + G2obj.StripUnicode(reader.idstring, '_') HistName = G2obj.MakeUniqueLabel(HistName, existingnames) - try: - Iparm1, Iparm2 = instprm - except ValueError: + # get instrumental parameters from reader... + if instprm is None: + try: + Iparm1, Iparm2 = reader.pwdparms['Instrument Parameters'] + print('Instrument parameters supplied in data file') + except KeyError as err: + Iparm1 = None # signal error rather than raise exception inside an exception handler + if Iparm1 is None: + msg = "The data file does not have any instrument params associated with it and none were provided." + raise Exception(msg) + + # ...or from a file... + elif isinstance(instprm, str): Iparm1, Iparm2 = load_iprms(instprm, reader, bank=bank) G2fil.G2Print('Instrument parameters read:',reader.instmsg) - except TypeError: # instprm is None, get iparms from reader - Iparm1, Iparm2 = reader.pwdparms['Instrument Parameters'] + + # ...or from an input... + elif ( + isinstance(instprm, (list, tuple)) + and len(instprm) == 2 + and all(isinstance(i, dict) for i in instprm) + ): + Iparm1, Iparm2 = instprm + print('Instrument parameters supplied in script') + + # ...else raise an error. + else: + msg = f"load_pwd... Error: Invalid instprm entered ({instprm!r})" + raise Exception(msg) + if 'T' in Iparm1['Type'][0]: if not reader.clockWd and reader.GSAS: @@ -1058,14 +1081,20 @@ def add_powder_histogram(self, datafile, iparams=None, phases=[], if not multiple: pwdrreaders = pwdrreaders[0:1] histlist = [] if URL: - iparmfile = downloadFile(iparams) - else: + instprm = downloadFile(iparams) + elif iparams: try: - iparmfile = os.path.abspath(os.path.expanduser(iparams)) - except: - pass + instprm = os.path.abspath(os.path.expanduser(iparams)) + except TypeError: # iparams is not a file path + if isinstance(iparams, (list, tuple)): + instprm = iparams + else: + raise Exception(f"add_powder_histogram Error: Invalid iparams supplied ({iparams!r})") + else: + instprm = None # will error out unless the reader supplies them + for r in pwdrreaders: - histname, new_names, pwdrdata = load_pwd_from_reader(r, iparmfile, + histname, new_names, pwdrdata = load_pwd_from_reader(r, instprm, [h.name for h in self.histograms()],bank=instbank) if histname in self.data: G2fil.G2Print("Warning - redefining histogram", histname) diff --git a/docs/source/GSASIIscriptable.rst b/docs/source/GSASIIscriptable.rst index b0e8b65f..494da7e1 100644 --- a/docs/source/GSASIIscriptable.rst +++ b/docs/source/GSASIIscriptable.rst @@ -1664,6 +1664,42 @@ and the refinement is repeated. hist.fit_fixed_points() gpx.save() +Specify Instrument Parameters Directly +----------------------------------------- + +Rather than read instrument parameters from a file, it is also possible +to specify them directly in a script. See the documentation on instrument +parameter file contents, :ref:`CWPowder_table` and :ref:`TOFPowder_table` +for more information on the parameters supplied here. + +.. code-block:: python + + import G2script as G2sc + import os + datadir = os.path.expanduser("~/Scratch/peakfit") + PathWrap = lambda fil: os.path.join(datadir,fil) + gpx = G2sc.G2Project(newgpx=PathWrap('pkfit.gpx')) + # specify instrmental parameters dictionaries + inst_params = [ + { + "Type": ["PXC", "PXC", 0], + "Lam": [1.5405, 1.5405, 0], + "Zero": [0.0, 0.0, 0], + "Polariz.": [0.7, 0.7, 0], + "U": [2.0, 2.0, 0], + "V": [-2.0, -2.0, 0], + "W": [5.0, 5.0, 0], + "X": [0.0, 0.0, 0], + "Y": [0.0, 0.0, 0], + "Z": [0.0, 0.0, 0], + "SH/L": [0.002, 0.002, 0], + "Azimuth": [0.0, 0.0, 0], + "Bank": [1, 1, 0], + }, + {}, + ] + hist = gpx.add_powder_histogram(PathWrap('FAP.XRA'), fmthint='GSAS powder', + iparams=inst_params) .. _CommandlineInterface: