def test_fits_without_sci(): schema = { "allOf": [ mschema.load_schema( "http://stsci.edu/schemas/jwst_datamodel/core.schema", resolve_references=True), { "type": "object", "properties": { "coeffs": { 'max_ndim': 1, 'fits_hdu': 'COEFFS', 'datatype': 'float32' } } } ] } hdulist = fits.HDUList() hdulist.append(fits.PrimaryHDU()) hdulist.append( fits.ImageHDU(name='COEFFS', data=np.array([0.0], np.float32))) with JwstDataModel(hdulist, schema=schema) as dm: assert_array_equal(dm.coeffs, [0.0])
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 JwstDataModel() as dm: dm.save(path) with datamodels.open("s3://test-s3-data/test.asdf") as m: assert isinstance(m, JwstDataModel)
def test_default_input_with_new_model(): """Test getting input name with new model""" step = StepWithModel() model = JwstDataModel() step.run(model) assert step._input_filename is None
def make_models(tmp_path): """Create basic models Returns ------- path_just_fits, path_model : (str, str) `path_just_fits` is a FITS file of `JwstDataModel` without the ASDF extension. `path_model` is a FITS file of `JwstDataModel` with the ASDF extension. """ path_just_fits = tmp_path / 'just_fits.fits' path_model = tmp_path / 'model.fits' primary_hdu = fits.PrimaryHDU() primary_hdu.header['EXP_TYPE'] = 'NRC_IMAGE' primary_hdu.header['DATAMODL'] = "JwstDataModel" hduls = fits.HDUList([primary_hdu]) hduls.writeto(path_just_fits) model = JwstDataModel(hduls) model.save(path_model) return {'just_fits': path_just_fits, 'model': path_model}
def test_meta_date_management(tmp_path): model = JwstDataModel({"meta": {"date": "2000-01-01T00:00:00.000"}}) assert model.meta.date == "2000-01-01T00:00:00.000" model.save(str(tmp_path / "test.fits")) assert abs((Time.now() - Time(model.meta.date)).value) < 1.0 model = JwstDataModel() assert abs((Time.now() - Time(model.meta.date)).value) < 1.0
def test_strict_validation_enum(): with JwstDataModel(strict_validation=True) as dm: assert dm.meta.instrument.name is None with pytest.raises(jsonschema.ValidationError): # FOO is not in the allowed enumerated values dm.meta.instrument.name = 'FOO'
def test_strict_validation_date(): with JwstDataModel(strict_validation=True) as dm: time_obj = time.Time(dm.meta.date) assert isinstance(time_obj, time.Time) date_obj = datetime.strptime(dm.meta.date, '%Y-%m-%dT%H:%M:%S.%f') assert isinstance(date_obj, datetime)
def test_strict_validation_type(): with JwstDataModel(strict_validation=True) as dm: with pytest.raises(jsonschema.ValidationError): # Schema requires a float dm.meta.target.ra = "FOO"
def test_data_array(tmp_path): """Test lots of things """ path = str(tmp_path / "data_array.fits") data_array_schema = { "allOf": [ mschema.load_schema( "http://stsci.edu/schemas/jwst_datamodel/core.schema", resolve_references=True), { "type": "object", "properties": { "arr": { 'title': 'An array of data', 'type': 'array', "fits_hdu": ["FOO", "DQ"], "items": { "title": "entry", "type": "object", "properties": { "data": { "fits_hdu": "FOO", "default": 0.0, "max_ndim": 2, "datatype": "float64" }, "dq": { "fits_hdu": "DQ", "default": 1, "datatype": "uint8" }, } } } } } ] } array1 = np.random.rand(5, 5) array2 = np.random.rand(5, 5) array3 = np.random.rand(5, 5) with JwstDataModel(schema=data_array_schema) as x: x.arr.append(x.arr.item()) x.arr[0].data = array1 assert len(x.arr) == 1 x.arr.append(x.arr.item(data=array2)) assert len(x.arr) == 2 x.arr.append({}) assert len(x.arr) == 3 x.arr[2].data = array3 del x.arr[1] assert len(x.arr) == 2 x.save(path) with JwstDataModel(path, schema=data_array_schema) as x: assert len(x.arr) == 2 assert_array_almost_equal(x.arr[0].data, array1) assert_array_almost_equal(x.arr[1].data, array3) del x.arr[0] assert len(x.arr) == 1 x.arr = [] assert len(x.arr) == 0 x.arr.append({'data': np.empty((5, 5))}) assert len(x.arr) == 1 x.arr.extend([ x.arr.item(data=np.empty((5, 5))), x.arr.item(data=np.empty((5, 5)), dq=np.empty((5, 5), dtype=np.uint8)) ]) assert len(x.arr) == 3 del x.arr[1] assert len(x.arr) == 2 x.save(path) from astropy.io import fits with fits.open(path) as hdulist: x = set() for hdu in hdulist: x.add((hdu.header.get('EXTNAME'), hdu.header.get('EXTVER'))) assert x == set([('FOO', 2), ('FOO', 1), ('ASDF', None), ('DQ', 2), (None, None)])
def test_delete(fits_file): with JwstDataModel() as dm: dm.meta.instrument.name = 'NIRCAM' assert dm.meta.instrument.name == 'NIRCAM' del dm.meta.instrument.name assert dm.meta.instrument.name is None
def inputs(request): table = None instrument, exptype = request.param dm = JwstDataModel() dm.meta.instrument.name = instrument dm.meta.exposure.type = exptype if instrument == 'MIRI': if 'LRS' in exptype: dm.meta.subarray.name = 'FULL' table = Table( { 'subarray': ['FULL', 'sub02'], 'nelem_wl': [3, 3], 'nelem_size': [3, 3], 'wavelength': [[1, 2, 3, 0], [1, 2, 3, 0]], 'size': [[1, 2, 3, 0], [1, 2, 3, 0]], 'apcorr': np.full((2, 4, 4), 0.5) } ) if 'MRS' in exptype: table = Table( { 'wavelength': [np.arange(12), ], 'radius': [np.arange(12), ], 'nelem_wl': [10, ], 'apcorr': np.full((1, 12), 0.5) } ) if instrument == 'NIRSPEC': # Too complicated to come up with silly example data; using "dummy" ref file dm.meta.instrument.filter = 'CLEAR' dm.meta.instrument.grating = 'PRISM' if 'FIXEDSLIT' in dm.meta.exposure.type: dm.meta.instrument.fixed_slit = 'S200A1' table = Table.read(NIR_TEST_FILES['FIXEDSLIT/BRIGHTOBJ'], format='fits')[:2] else: table = Table.read(NIR_TEST_FILES['ISU/MSASPEC'], format='fits')[:2] table['APCORR'] = table['APCORR'].reshape((len(table), 3, 2048, 3)) # Reshape test data to expected shape if instrument == 'NIRCAM': dm.meta.instrument.filter = 'F322W2' dm.meta.instrument.pupil = 'GRISMR' table = Table( { 'filter': ['F322W2', 'filter02'], 'pupil': ['GRISMR', 'pupil02'], 'nelem_wl': [3, 3], 'nelem_size': [3, 3], 'wavelength': [[1, 2, 3, 0], [1, 2, 3, 0]], 'size': [[1, 2, 3, 0], [1, 2, 3, 0]], 'apcorr': np.full((2, 4, 4), 0.5) } ) if instrument == 'NIRISS': dm.meta.instrument.filter = 'GR150R' dm.meta.instrument.pupil = 'F090W' table = Table( { 'filter': ['GR150R', 'filter02'], 'pupil': ['F090W', 'pupil02'], 'nelem_wl': [3, 3], 'nelem_size': [3, 3], 'wavelength': [[1, 2, 3, 0], [1, 2, 3, 0]], 'size': [[1, 2, 3, 0], [1, 2, 3, 0]], 'apcorr': np.full((2, 4, 4), 0.5) } ) return dm, fits.table_to_hdu(table).data