Skip to content

Commit

Permalink
Raise error when there is no key in the json file
Browse files Browse the repository at this point in the history
  • Loading branch information
dachengx committed Aug 2, 2023
1 parent c7252e8 commit 462fb5b
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions alea/template_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,16 @@ def _get_json_spectrum(filename):
Args:
filename (str): Name of the JSON file.
Todo:
Define the format of the JSON file clearly.
"""
with open(filename, "r") as f:
contents = json.load(f)
logging.debug(contents["description"])
if "description" in contents:
logging.debug(contents["description"])
if "coordinate_system" not in contents:
raise ValueError("Coordinate system not in JSON file.")
esyst = contents["coordinate_system"][0][1]
ret = interp1d(
np.linspace(*esyst), contents["map"],
Expand All @@ -369,9 +375,11 @@ def build_histogram(self):
histname = self.config["histname"].format(**self.format_named_parameters)
h = template_to_multihist(templatename, histname)

spectrum = self.config["spectrum"]
if "spectrum_name" not in self.config:
raise ValueError("spectrum_name not in config")
spectrum_name = self.config["spectrum_name"]
if isinstance(spectrum, str):
spectrum = self._get_json_spectrum(spectrum.format(**self.format_named_parameters))
spectrum = self._get_json_spectrum(spectrum_name.format(**self.format_named_parameters))

# Perform E-scaling, assume first axis is energy
ecenters = h.bin_centers[0]
Expand Down

0 comments on commit 462fb5b

Please sign in to comment.