Skip to content

Commit 14742e2

Browse files
ezatterinbriantoby
andauthored
fix: handle instrument param input correctly (#263)
* fix: handle instrument param input correctly * tweak add_powder_histogram error reporting; document use of script-coded instrument parameters --------- Co-authored-by: BHT <[email protected]>
1 parent 97101c8 commit 14742e2

File tree

2 files changed

+76
-11
lines changed

2 files changed

+76
-11
lines changed

GSASII/GSASIIscriptable.py

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -588,13 +588,36 @@ def load_pwd_from_reader(reader, instprm, existingnames=[],bank=None):
588588
HistName = 'PWDR ' + G2obj.StripUnicode(reader.idstring, '_')
589589
HistName = G2obj.MakeUniqueLabel(HistName, existingnames)
590590

591-
try:
592-
Iparm1, Iparm2 = instprm
593-
except ValueError:
591+
# get instrumental parameters from reader...
592+
if instprm is None:
593+
try:
594+
Iparm1, Iparm2 = reader.pwdparms['Instrument Parameters']
595+
print('Instrument parameters supplied in data file')
596+
except KeyError as err:
597+
Iparm1 = None # signal error rather than raise exception inside an exception handler
598+
if Iparm1 is None:
599+
msg = "The data file does not have any instrument params associated with it and none were provided."
600+
raise Exception(msg)
601+
602+
# ...or from a file...
603+
elif isinstance(instprm, str):
594604
Iparm1, Iparm2 = load_iprms(instprm, reader, bank=bank)
595605
G2fil.G2Print('Instrument parameters read:',reader.instmsg)
596-
except TypeError: # instprm is None, get iparms from reader
597-
Iparm1, Iparm2 = reader.pwdparms['Instrument Parameters']
606+
607+
# ...or from an input...
608+
elif (
609+
isinstance(instprm, (list, tuple))
610+
and len(instprm) == 2
611+
and all(isinstance(i, dict) for i in instprm)
612+
):
613+
Iparm1, Iparm2 = instprm
614+
print('Instrument parameters supplied in script')
615+
616+
# ...else raise an error.
617+
else:
618+
msg = f"load_pwd... Error: Invalid instprm entered ({instprm!r})"
619+
raise Exception(msg)
620+
598621

599622
if 'T' in Iparm1['Type'][0]:
600623
if not reader.clockWd and reader.GSAS:
@@ -1058,14 +1081,20 @@ def add_powder_histogram(self, datafile, iparams=None, phases=[],
10581081
if not multiple: pwdrreaders = pwdrreaders[0:1]
10591082
histlist = []
10601083
if URL:
1061-
iparmfile = downloadFile(iparams)
1062-
else:
1084+
instprm = downloadFile(iparams)
1085+
elif iparams:
10631086
try:
1064-
iparmfile = os.path.abspath(os.path.expanduser(iparams))
1065-
except:
1066-
pass
1087+
instprm = os.path.abspath(os.path.expanduser(iparams))
1088+
except TypeError: # iparams is not a file path
1089+
if isinstance(iparams, (list, tuple)):
1090+
instprm = iparams
1091+
else:
1092+
raise Exception(f"add_powder_histogram Error: Invalid iparams supplied ({iparams!r})")
1093+
else:
1094+
instprm = None # will error out unless the reader supplies them
1095+
10671096
for r in pwdrreaders:
1068-
histname, new_names, pwdrdata = load_pwd_from_reader(r, iparmfile,
1097+
histname, new_names, pwdrdata = load_pwd_from_reader(r, instprm,
10691098
[h.name for h in self.histograms()],bank=instbank)
10701099
if histname in self.data:
10711100
G2fil.G2Print("Warning - redefining histogram", histname)

docs/source/GSASIIscriptable.rst

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,6 +1664,42 @@ and the refinement is repeated.
16641664
hist.fit_fixed_points()
16651665
gpx.save()
16661666
1667+
Specify Instrument Parameters Directly
1668+
-----------------------------------------
1669+
1670+
Rather than read instrument parameters from a file, it is also possible
1671+
to specify them directly in a script. See the documentation on instrument
1672+
parameter file contents, :ref:`CWPowder_table` and :ref:`TOFPowder_table`
1673+
for more information on the parameters supplied here.
1674+
1675+
.. code-block:: python
1676+
1677+
import G2script as G2sc
1678+
import os
1679+
datadir = os.path.expanduser("~/Scratch/peakfit")
1680+
PathWrap = lambda fil: os.path.join(datadir,fil)
1681+
gpx = G2sc.G2Project(newgpx=PathWrap('pkfit.gpx'))
1682+
# specify instrmental parameters dictionaries
1683+
inst_params = [
1684+
{
1685+
"Type": ["PXC", "PXC", 0],
1686+
"Lam": [1.5405, 1.5405, 0],
1687+
"Zero": [0.0, 0.0, 0],
1688+
"Polariz.": [0.7, 0.7, 0],
1689+
"U": [2.0, 2.0, 0],
1690+
"V": [-2.0, -2.0, 0],
1691+
"W": [5.0, 5.0, 0],
1692+
"X": [0.0, 0.0, 0],
1693+
"Y": [0.0, 0.0, 0],
1694+
"Z": [0.0, 0.0, 0],
1695+
"SH/L": [0.002, 0.002, 0],
1696+
"Azimuth": [0.0, 0.0, 0],
1697+
"Bank": [1, 1, 0],
1698+
},
1699+
{},
1700+
]
1701+
hist = gpx.add_powder_histogram(PathWrap('FAP.XRA'), fmthint='GSAS powder',
1702+
iparams=inst_params)
16671703
16681704
.. _CommandlineInterface:
16691705

0 commit comments

Comments
 (0)