def test_open(): with stdatamodels.open(): pass with stdatamodels.open((50, 50)): pass with pytest.warns(stdatamodels.util.NoTypeWarning): with stdatamodels.open(FITS_FILE) as dm: assert isinstance(dm, QuadModel)
def test_from_hdulist(): from astropy.io import fits warnings.simplefilter("ignore") with fits.open(FITS_FILE, memmap=False) as hdulist: with stdatamodels.open(hdulist) as dm: dm.data assert not hdulist.fileinfo(0)['file'].closed
def test_open_fits(): """Test opening a model from a FITS file""" with warnings.catch_warnings(): warnings.filterwarnings("ignore", "model_type not found") fits_file = t_path('test.fits') with stdatamodels.open(fits_file) as model: assert isinstance(model, DataModel)
def test_open_asdf_s3(s3_root_dir): """Test opening a model from an ASDF file on S3""" path = str(s3_root_dir.join("test.asdf")) with DataModel() as dm: dm.save(path) with stdatamodels.open("s3://test-s3-data/test.asdf") as m: assert isinstance(m, DataModel)
def test_open_from_pathlib(): """Test opening a PurePath object""" path = Path(t_path('test.fits')) assert isinstance(path, PurePath) with warnings.catch_warnings(): warnings.filterwarnings("ignore", "model_type not found") with stdatamodels.open(path) as model: assert isinstance(model, DataModel)
def test_open_hdulist(): hdulist = fits.HDUList() data = np.empty((50, 50), dtype=np.float32) primary = fits.PrimaryHDU() hdulist.append(primary) science = fits.ImageHDU(data=data, name='SCI') hdulist.append(science) with stdatamodels.open(hdulist) as model: assert type(model) == ImageModel
def test_open_association(): """Test for opening an association""" asn_file = t_path('association.json') with warnings.catch_warnings(): warnings.filterwarnings("ignore", "model_type not found") with stdatamodels.open(asn_file) as c: assert isinstance(c, ModelContainer) for model in c: assert model.meta.asn.table_name == "association.json" assert model.meta.asn.pool_name == "pool"
def test_open_cube(tmpdir): """Open 3D data as CubeModel""" path = str(tmpdir.join("ramp.fits")) shape = (2, 3, 4) with fits.HDUList(fits.PrimaryHDU()) as hdulist: hdulist.append(fits.ImageHDU(data=np.zeros(shape), name="SCI", ver=1)) hdulist.writeto(path) with warnings.catch_warnings(): warnings.filterwarnings("ignore", "model_type not found") with stdatamodels.open(path) as model: assert isinstance(model, CubeModel)
def test_open_warning(): with warnings.catch_warnings(record=True) as warners: # Cause all warnings to always be triggered. warnings.simplefilter("always") with stdatamodels.open(FITS_FILE) as model: pass class_name = model.__class__.__name__ j = None for i, w in enumerate(warners): if class_name in str(w.message): j = i assert j is not None
def test_open_asdf_readonly(tmpdir): tmpfile = str(tmpdir.join('readonly.asdf')) with DistortionModel() as model: model.meta.telescope = 'JWST' model.meta.instrument.name = 'NIRCAM' model.meta.instrument.detector = 'NRCA4' model.meta.instrument.channel = 'SHORT' model.save(tmpfile) os.chmod(tmpfile, 0o440) assert os.access(tmpfile, os.W_OK) == False with stdatamodels.open(tmpfile) as model: assert model.meta.telescope == 'JWST'
def test_open_fits_readonly(tmpdir): """Test opening a FITS-format datamodel that is read-only on disk""" tmpfile = str(tmpdir.join('readonly.fits')) data = np.arange(100, dtype=np.float).reshape(10, 10) with ImageModel(data=data) as model: model.meta.telescope = 'JWST' model.meta.instrument.name = 'NIRCAM' model.meta.instrument.detector = 'NRCA4' model.meta.instrument.channel = 'SHORT' model.save(tmpfile) os.chmod(tmpfile, 0o440) assert os.access(tmpfile, os.W_OK) == False with stdatamodels.open(tmpfile) as model: assert model.meta.telescope == 'JWST'
def test_open_reffiles(tmpdir, model_class, shape): """Try opening files with a REFTYPE keyword and different data/dq shapes""" path = str(tmpdir.join("reffile.fits")) with fits.HDUList(fits.PrimaryHDU()) as hdulist: hdulist["PRIMARY"].header.append(("REFTYPE", "foo")) if shape is not None: hdulist.append( fits.ImageHDU(data=np.zeros(shape), name="SCI", ver=1)) hdulist.append( fits.ImageHDU(data=np.zeros(shape, dtype=np.uint), name="DQ", ver=1)) hdulist.writeto(path) with warnings.catch_warnings(): warnings.filterwarnings("ignore", "model_type not found") with stdatamodels.open(path) as model: assert isinstance(model, model_class)
def test_open_illegal(): with pytest.raises(ValueError): init = 5 stdatamodels.open(init)
def test_open_image(): with warnings.catch_warnings(): warnings.filterwarnings("ignore", "model_type not found") image_name = t_path('jwst_image.fits') with stdatamodels.open(image_name) as model: assert type(model) == ImageModel
def test_open_shape(): init = (200, 200) with stdatamodels.open(init) as model: assert type(model) == ImageModel
def test_container_open_asn_with_sourcecat(): path = t_path("association_w_cat.json") with stdatamodels.open(path, asn_exptypes="science") as c: for model in c: assert model.meta.asn.table_name == "association_w_cat.json"
def test_init_with_array(): array = np.empty((50, 50)) with stdatamodels.open(array) as dm: assert dm.data.shape == (50, 50) assert isinstance(dm, ImageModel)