Skip to content

Commit

Permalink
Merge pull request #1 from braingram/feature/alternate_asdf_in_fits
Browse files Browse the repository at this point in the history
alternative asdf in fits implementation
  • Loading branch information
eslavich authored Jan 20, 2023
2 parents 6e528cb + 8e38c98 commit 1deeabb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
4 changes: 1 addition & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 15 additions & 21 deletions src/stdatamodels/fits_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<name>[ -~]+),)?(?P<ver>[0-9]+)",
Expand All @@ -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):
Expand Down

0 comments on commit 1deeabb

Please sign in to comment.