From 5483120f6003180c340e2eca8beebf3b6b8ba5b1 Mon Sep 17 00:00:00 2001 From: Brett Date: Tue, 17 Jan 2023 16:26:28 -0500 Subject: [PATCH 1/2] alternative asdf in fits implementation exploits the lazy loading of NDArrayType objects to implement asdf in fits. After the yaml is parsed and tree is loaded, the tree is modified and each NDArrayType is replaced with the corresponding array from the hdulist. --- src/stdatamodels/fits_support.py | 36 +++++++++++++------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/src/stdatamodels/fits_support.py b/src/stdatamodels/fits_support.py index 793af130..c67d8aaa 100644 --- a/src/stdatamodels/fits_support.py +++ b/src/stdatamodels/fits_support.py @@ -733,29 +733,26 @@ def from_fits_asdf(hdulist, ) generic_file = generic_io.get_file(io.BytesIO(asdf_extension.data), mode="rw") - return asdf.open( + af = asdf.open( generic_file, ignore_version_mismatch=ignore_version_mismatch, ignore_unrecognized_tag=ignore_unrecognized_tag, ignore_missing_extensions=ignore_missing_extensions, - _tagged_tree_transform=partial(_convert_fits_arrays, hdulist) ) + # map hdulist to blocks here + _map_hdulist_to_arrays(hdulist, af) + return af -def _convert_fits_arrays(hdulist, tree): - """ - ASDF-in-FITS has ndarray-1.0.0 tags with special source values - that represent links to HDU arrays. The asdf library won't know - what to do with those, so we need to supply code that will - replace them with the HDU data objects. - """ +def _map_hdulist_to_arrays(hdulist, af): def callback(node, json_id): - if (hasattr(node, "_tag") - and node._tag == _NDARRAY_TAG - and isinstance(node.get("source"), str) - and node["source"].startswith(_FITS_SOURCE_PREFIX)): - - source = node["source"] + if ( + isinstance(node, NDArrayType) and + isinstance(node._source, str) and + node._source.startswith(_FITS_SOURCE_PREFIX) + ): + # read the array data from the hdulist + source = node._source parts = re.match( # All printable ASCII characters are allowed in EXTNAME "((?P[ -~]+),)?(?P[0-9]+)", @@ -767,13 +764,10 @@ def callback(node, json_id): pair = (parts.group("name"), ver) else: pair = ver - return hdulist[pair].data - else: - raise ValueError(f"Can not parse FITS block source '{source}'") - + data = hdulist[pair].data + return data return node - - return treeutil.walk_and_modify(tree, callback) + af.tree = treeutil.walk_and_modify(af.tree, callback) def from_fits_hdu(hdu, schema, cast_arrays=True): From 8e38c9843865a815db2536f446b9135fc9dab905 Mon Sep 17 00:00:00 2001 From: Brett Date: Wed, 18 Jan 2023 09:51:28 -0500 Subject: [PATCH 2/2] remstoring asdf requirement --- setup.cfg | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/setup.cfg b/setup.cfg index 30aebdb7..12fdf55c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -22,9 +22,7 @@ setup_requires = setuptools_scm install_requires = jsonschema>=3.0.2 - # TODO: Restore this to an asdf release version before - # merging this PR: - asdf @ git+https://github.com/eslavich/asdf.git@eslavich-add-tree-mapper + asdf>=2.14.1 asdf-astropy>=0.3.0 psutil>=5.7.2 numpy>=1.16