Skip to content

Commit

Permalink
Add converter and schema for basic astropy.wcs.WCS objects
Browse files Browse the repository at this point in the history
  • Loading branch information
ViciousEagle03 committed Jul 24, 2024
1 parent dab5b4d commit d7bbee8
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 1 deletion.
3 changes: 2 additions & 1 deletion asdf_astropy/converters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"FitsConverter",
"AsdfFitsConverter",
"AstropyFitsConverter",
"FitsWCSConverter",
"ColumnConverter",
"AstropyTableConverter",
"AsdfTableConverter",
Expand Down Expand Up @@ -51,7 +52,7 @@
SkyCoordConverter,
SpectralCoordConverter,
)
from .fits import AsdfFitsConverter, AstropyFitsConverter, FitsConverter
from .fits import AsdfFitsConverter, AstropyFitsConverter, FitsConverter, FitsWCSConverter
from .table import AsdfTableConverter, AstropyTableConverter, ColumnConverter, NdarrayMixinConverter
from .time import TimeConverter, TimeDeltaConverter
from .transform import (
Expand Down
2 changes: 2 additions & 0 deletions asdf_astropy/converters/fits/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"FitsConverter",
"AsdfFitsConverter",
"AstropyFitsConverter",
"FitsWCSConverter",
]

from .fits import AsdfFitsConverter, AstropyFitsConverter, FitsConverter
from .fitswcs import FitsWCSConverter
29 changes: 29 additions & 0 deletions asdf_astropy/converters/fits/fitswcs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from asdf.extension import Converter


class FitsWCSConverter(Converter):
"""
Converter for serializing and deserializing `astropy.wcs.WCS` objects.
This converter currently supports the serialization of simple WCS objects
by preserving the `wcs.to_header()` data. It does not support complex WCS objects
such as tabular or distortion WCSes.
Future work:
- Until the support for tabular and distortion WCSes is added, throw error for such WCSes when passed through in the converter
- Implement mechanisms to detect tabular and distortion WCSes and support their serialization
"""
tags = ("tag:astropy.org:astropy/fits/fitswcs-*",)
types = ("astropy.wcs.wcs.WCS", )

def from_yaml_tree(self, node, tag, ctx):
from astropy.wcs import WCS

header = node["header"]
wcs = WCS(header)
return wcs

def to_yaml_tree(self, wcs, tag, ctx):
node = {}
node["header"] = dict(wcs.to_header())
return node
41 changes: 41 additions & 0 deletions asdf_astropy/converters/fits/tests/test_fitswcs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import asdf
import pytest
from astropy.wcs import WCS


def create_wcs():
header = {
'CTYPE1': 'TIME',
'CUNIT1': 'min',
'CDELT1': 0.4,
'CRPIX1': 0,
'CRVAL1': 0,
'CTYPE2': 'WAVE',
'CUNIT2': 'Angstrom',
'CDELT2': 0.2,
'CRPIX2': 0,
'CRVAL2': 0,
'CTYPE3': 'HPLT-TAN',
'CUNIT3': 'arcsec',
'CDELT3': 20,
'CRPIX3': 0,
'CRVAL3': 0,
'CTYPE4': 'HPLN-TAN',
'CUNIT4': 'arcsec',
'CDELT4': 5,
'CRPIX4': 5,
'CRVAL4': 0,
}
return WCS(header)

@pytest.mark.parametrize("wcs", [create_wcs()])
def test_wcs_serialization(wcs, tmp_path):
file_path = tmp_path / "test_wcs.asdf"
with asdf.AsdfFile() as af:
af["wcs"] = wcs
af.write_to(file_path)

with asdf.open(file_path) as af:
loaded_wcs = af["wcs"]

assert wcs.to_header() == loaded_wcs.to_header()
2 changes: 2 additions & 0 deletions asdf_astropy/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .converters.coordinates.sky_coord import SkyCoordConverter
from .converters.coordinates.spectral_coord import SpectralCoordConverter
from .converters.fits.fits import AsdfFitsConverter, AstropyFitsConverter
from .converters.fits.fitswcs import FitsWCSConverter
from .converters.table.table import AsdfTableConverter, AstropyTableConverter, ColumnConverter, NdarrayMixinConverter
from .converters.time.time import TimeConverter
from .converters.time.time_delta import TimeDeltaConverter
Expand Down Expand Up @@ -482,6 +483,7 @@
AstropyTableConverter(),
AstropyFitsConverter(),
NdarrayMixinConverter(),
FitsWCSConverter(),
]

_COORDINATES_MANIFEST_URIS = [
Expand Down
5 changes: 5 additions & 0 deletions asdf_astropy/resources/manifests/astropy-1.0.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ tags:
be accessible in its proper form in the ASDF file.
Only image and binary table extensions are supported.
- tag_uri: tag:astropy.org:astropy/fits/fitswcs-1.0.0
schema_uri: http://astropy.org/schemas/astropy/fits/fitswcs-1.0.0
title: Represents an astropy.wcs.WCS object
description: |-
Supports serialization of the simple wcs objects header
- tag_uri: tag:astropy.org:astropy/table/table-1.0.0
schema_uri: http://astropy.org/schemas/astropy/table/table-1.0.0
title: A table.
Expand Down
17 changes: 17 additions & 0 deletions asdf_astropy/resources/schemas/fits/fitswcs-1.0.0.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
%YAML 1.1
---
$schema: "http://stsci.edu/schemas/yaml-schema/draft-01"
id: "http://astropy.org/schemas/astropy/fits/fitswcs-1.0.0"

title:
Represents the fits object

description:
Represents the fits object

allOf:
- tag: "tag:astropy.org:astropy/fits/fitswcs-1.0.0"
- type: object
properties:
header:
type: object

0 comments on commit d7bbee8

Please sign in to comment.