Skip to content

Commit

Permalink
Fix DatasetDict's setitem to allow empty md in value.
Browse files Browse the repository at this point in the history
Sometimes a dataset/projectable doesn't have any info attached to it, eg
because the dataset is synthetic. In these cases, setitem would crash.
This is now fixed, and if a string is provided as a key in setitem it is
used as a name if no better name is already there.

Signed-off-by: Martin Raspaud <martin.raspaud@smhi.se>
  • Loading branch information
mraspaud committed Dec 14, 2015
1 parent e00d8ed commit b2059cc
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions mpop/readers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,23 @@ def __setitem__(self, key, value):
"""
d = value.info if isinstance(value, Projectable) else value
if not isinstance(key, DatasetID):
old_key = key
key = self.get_key(key)
if key is None:
if (d.get("name") == "undefined") and isinstance(old_key, (str, six.text_type)):
new_name = old_key
else:
new_name = d.get("name")
# this is a new key and it's not a full DatasetID tuple
key = DatasetID(
name=d["name"],
resolution=d["resolution"],
wavelength=d["wavelength_range"],
polarization=d["polarization"],
calibration=d["calibration"],
name=new_name,
resolution=d.get("resolution"),
wavelength=d.get("wavelength_range"),
polarization=d.get("polarization"),
calibration=d.get("calibration"),
)
if key.name is None and key.wavelength is None:
raise ValueError("One of 'name' or 'wavelength_range' info values should be set.")

# update the 'value' with the information contained in the key
d["name"] = key.name
Expand All @@ -175,7 +182,8 @@ def __setitem__(self, key, value):
d["calibration"] = key.calibration
d["polarization"] = key.polarization
# you can't change the wavelength of a dataset, that doesn't make sense
assert(d["wavelength_range"] == key.wavelength)
if "wavelength_range" in d and d["wavelength_range"] != key.wavelength:
raise TypeError("Can't change the wavelength of a dataset")

return super(DatasetDict, self).__setitem__(key, value)

Expand Down

0 comments on commit b2059cc

Please sign in to comment.