-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d27051
commit 66904fc
Showing
8 changed files
with
77 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,33 @@ | ||
import warnings | ||
|
||
import asdf | ||
import pytest | ||
from astropy.wcs import WCS | ||
from astropy.io import fits | ||
from astropy.utils.data import download_file | ||
from astropy.wcs import WCS, FITSFixedWarning | ||
|
||
|
||
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) | ||
urls = [ | ||
"http://data.astropy.org/tutorials/FITS-cubes/reduced_TAN_C14.fits", | ||
"http://data.astropy.org/tutorials/FITS-images/HorseHead.fits", | ||
] | ||
|
||
with warnings.catch_warnings(): | ||
warnings.simplefilter("ignore", FITSFixedWarning) | ||
return [WCS(fits.open(download_file(url, cache=True))[0].header) for url in urls] | ||
|
||
|
||
@pytest.mark.parametrize("wcs", [create_wcs()]) | ||
@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: | ||
with asdf.AsdfFile() as af, warnings.catch_warnings(): | ||
warnings.simplefilter("ignore", FITSFixedWarning) | ||
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() | ||
with warnings.catch_warnings(): | ||
warnings.simplefilter("ignore", FITSFixedWarning) | ||
with asdf.open(file_path) as af: | ||
loaded_wcs = af["wcs"] | ||
assert wcs.to_header() == loaded_wcs.to_header() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,34 @@ | ||
import ast | ||
|
||
from asdf.extension import Converter | ||
|
||
|
||
class SlicedWCSConverter(Converter): | ||
tags = ("tag:astropy.org:astropy/slicedwcs/slicedwcs-*",) | ||
types = ("astropy.wcs.wcsapi.wrappers.sliced_wcs.SlicedLowLevelWCS",) | ||
|
||
def parse_slice_string(self, slice_str): | ||
if slice_str.isdigit(): | ||
return int(ast.literal_eval(slice_str)) | ||
slice_str = slice_str[len("slice(") : -1] | ||
parts = slice_str.split(",") | ||
start = ast.literal_eval(parts[0].strip()) | ||
stop = ast.literal_eval(parts[1].strip()) | ||
step = ast.literal_eval(parts[2].strip()) | ||
return slice(start, stop, step) | ||
|
||
def from_yaml_tree(self, node, tag, ctx): | ||
from astropy.wcs.wcsapi.wrappers.sliced_wcs import SlicedLowLevelWCS | ||
|
||
wcs = node["wcs"] | ||
slice_array = node["slices_array"] | ||
slice_array = [self.parse_slice_string(s) for s in slice_array] | ||
|
||
slice_array = [] | ||
slice_array = [ | ||
s if isinstance(s, int) else slice(s["start"], s["stop"], s["step"]) for s in node["slices_array"] | ||
] | ||
return SlicedLowLevelWCS(wcs, slice_array) | ||
|
||
def to_yaml_tree(self, sl, tag, ctx): | ||
node = {} | ||
node["wcs"] = sl._wcs | ||
node["slices_array"] = [str(item) for item in sl._slices_array] | ||
node["slices_array"] = [] | ||
|
||
for s in sl._slices_array: | ||
if isinstance(s, slice): | ||
node["slices_array"].append( | ||
{ | ||
"start": s.start if s.start is not None else None, | ||
"stop": s.stop if s.stop is not None else None, | ||
"step": s.step if s.step is not None else None, | ||
}, | ||
) | ||
else: | ||
node["slices_array"].append(s) | ||
return node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters