Skip to content

Commit

Permalink
added getval and setval methods to AsdfFile class
Browse files Browse the repository at this point in the history
  • Loading branch information
alphasentaurii committed Nov 1, 2023
1 parent 38b3b6a commit d0945e7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
34 changes: 34 additions & 0 deletions crds/io/asdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,40 @@ def get_history(self, handle):
if histall:
history = "\n".join(histall)
return history

def getval(self, key, **keys):
return super(AsdfFile, self).getval(key, **keys)

def setval(self, key, value, verbose=False):
"""ASDF version of setval.
Parameters
----------
key : string
Full tree path of the key separated by periods, e.g. "roman.meta.aperture.name"
value : string, astropy.Time, int, float
value to set
"""
import asdf
af = asdf.open(self.filepath)
keys = key.split(".")
tree = None
stop = len(keys) - 1
original = self.getval(key.upper())
for i, k in enumerate(keys):
if i == 0:
tree = af[k]
elif i < stop:
tree = tree[k]
else:
tree[k] = value
af.write_to(self.filename)

new = self.getval(key.upper())
if verbose:
log.info(f"KEY: {key}")
log.info(f"ORIGINAL VALUE: {original}")
log.info(f"NEW VALUE: {new}")

def get_asdf_standard_version(self):
"""
Expand Down
3 changes: 2 additions & 1 deletion crds/io/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def get_observatory(filepath, original_name=None):
with asdf.open(filepath) as handle:
observatory = handle["meta"]["telescope"]
except KeyError:
pass
if "roman" in handle.keys():
observatory = "roman"
elif original_name.endswith((".yaml", ".json", ".text", ".txt")):
return "jwst"
return observatory.lower()
Expand Down

0 comments on commit d0945e7

Please # to comment.