@@ -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 )
0 commit comments