33import pytest
44import zipfile
55import io
6+ import importlib .util
67
78import asdf
89from astropy import coordinates as coord
1415from astropy .io import fits
1516from gwcs import wcs , coordinate_frames
1617from PIL import Image
17- from stdatamodels import asdf_in_fits
1818
1919from astrocut .asdf_cutout import ASDFCutout , asdf_cut , get_center_pixel
2020from astrocut .exceptions import DataWarning , InvalidInputError , InvalidQueryError
@@ -196,9 +196,11 @@ def check_asdf_metadata(af, original_file, cutout_data):
196196 assert hdul [0 ].header ['ORIG_FLE' ] == test_images [i ].as_posix ()
197197 assert Path (fits_file ).stat ().st_size < Path (test_images [i ]).stat ().st_size
198198
199- # Check ASDF extension contents
200- with asdf_in_fits .open (fits_file ) as af :
201- check_asdf_metadata (af , test_images [i ], cutout .cutouts [i ].data )
199+ # Check ASDF extension contents (stdatamodels optional)
200+ if importlib .util .find_spec ('stdatamodels' ) is not None :
201+ from stdatamodels import asdf_in_fits
202+ with asdf_in_fits .open (fits_file ) as af :
203+ check_asdf_metadata (af , test_images [i ], cutout .cutouts [i ].data )
202204
203205
204206@pytest .mark .parametrize ('output_format' , ['.asdf' , '.fits' ])
@@ -224,7 +226,7 @@ def test_asdf_cutout_write_to_zip(tmpdir, test_images, center_coord, cutout_size
224226 else :
225227 with fits .open (io .BytesIO (data )) as hdul :
226228 assert isinstance (hdul , fits .HDUList )
227- assert len (hdul ) == 2 # primary + embedded ASDF extension
229+ assert len (hdul ) == 2 if importlib . util . find_spec ( 'stdatamodels' ) is not None else 1
228230 assert hdul [0 ].data .shape == (cutout_size , cutout_size )
229231
230232
@@ -254,13 +256,16 @@ def check_lite_metadata(af):
254256 # Write cutouts to HDUList objects in lite mode
255257 cutout = ASDFCutout (test_images , center_coord , cutout_size , lite = True )
256258 for hdul in cutout .fits_cutouts :
257- assert len (hdul ) == 2 # primary HDU + embedded ASDF extension
259+ has_stdatamodels = importlib .util .find_spec ('stdatamodels' ) is not None
260+ assert len (hdul ) == 2 if has_stdatamodels else 1 # primary HDU + embedded ASDF extension
258261 assert hdul [0 ].name == 'PRIMARY'
259- assert hdul [1 ].name == 'ASDF'
260262
261- # Check ASDF extension contents
262- with asdf_in_fits .open (hdul ) as af :
263- check_lite_metadata (af )
263+ # Check ASDF extension contents (stdatamodels optional)
264+ if has_stdatamodels :
265+ assert hdul [1 ].name == 'ASDF'
266+ from stdatamodels import asdf_in_fits
267+ with asdf_in_fits .open (hdul ) as af :
268+ check_lite_metadata (af )
264269
265270
266271def test_asdf_cutout_partial (test_images , center_coord , cutout_size ):
0 commit comments