Skip to content

add_powder_histogram does not accept instrument parameters as dicts #262

@ezatterin

Description

@ezatterin

Consider the following example:

import GSASII as gs
from GSASII import GSASIIscriptable as gsc

# initialise new project
gpx = gsc.G2Project(newgpx="test.gpx")

# load powder diffraction readers
readers = gs.GSASIIfiles.LoadImportRoutines("pwd")

# 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],
    },
    {},
]

# load the powder histogram
hist = gpx.add_powder_histogram(datafile="some_data_file.xye", iparams=inst_params)

the last line fails with:

File ~/venvs/py3.12/lib/python3.12/site-packages/GSASII/GSASIIscriptable.py:1068, in G2Project.add_powder_histogram(self, datafile, iparams, phases, fmthint, databank, instbank, multiple, URL)
   1066         pass
   1067 for r in pwdrreaders:
-> 1068     histname, new_names, pwdrdata = load_pwd_from_reader(r, iparmfile,
   1069                                   [h.name for h in self.histograms()],bank=instbank)
   1070     if histname in self.data:
   1071         G2fil.G2Print("Warning - redefining histogram", histname)

UnboundLocalError: cannot access local variable 'iparmfile' where it is not associated with a value

This is because the variable iparmfile is never set unless the iparams keyword argument of GSASIIscriptable.G2Project.add_powder_histogram is equal to a string pointing to a file, see lines 1062-1066:

  if URL:
      iparmfile = downloadFile(iparams)
  else:
      try:
          iparmfile = os.path.abspath(os.path.expanduser(iparams))
      except:
          pass

this is despite it is stated in the docstring of the function that

:param str iparams: A filename for an instrument parameters file,
            or a pair of instrument parameter dicts from :func:`load_iprms`.
            This may be omitted for readers that provide the instrument
            parameters in the file. (Only a few importers do this.)

I will make a PR soon.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions