From d0945e71ea5f653ab1dab606194bfa8167e6975e Mon Sep 17 00:00:00 2001 From: alphasentaurii Date: Wed, 1 Nov 2023 14:06:20 -0400 Subject: [PATCH] added getval and setval methods to AsdfFile class --- crds/io/asdf.py | 34 ++++++++++++++++++++++++++++++++++ crds/io/factory.py | 3 ++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/crds/io/asdf.py b/crds/io/asdf.py index bbc162b00..3ec14d015 100644 --- a/crds/io/asdf.py +++ b/crds/io/asdf.py @@ -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): """ diff --git a/crds/io/factory.py b/crds/io/factory.py index e7a3bed2a..aa98b9ed2 100644 --- a/crds/io/factory.py +++ b/crds/io/factory.py @@ -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()