def test_add_wcs_default(data_file, tmp_path): """Handle when no pointing exists and the default is used.""" expected_name = 'add_wcs_default.fits' try: stp.add_wcs(data_file, siaf_path=siaf_path, tolerance=0, allow_default=True) except ValueError: pass # This is what we want for the test. except Exception as e: pytest.skip('Live ENGDB service is not accessible.' '\nException={}'.format(e)) # Tests with datamodels.Level1bModel(data_file) as model: # Save for post-test comparison and update model.save(tmp_path / expected_name) with datamodels.open(DATA_PATH / expected_name) as expected: for meta in METAS_EQUALITY: assert model[meta] == expected[meta], f'{meta} has changed' for meta in METAS_ISCLOSE: assert np.isclose(model[meta], expected[meta]), f'{meta} has changed' assert word_precision_check(model.meta.wcsinfo.s_region, expected.meta.wcsinfo.s_region)
def test_add_wcs_method_full_siafdb(eng_db_ngas, data_file, tmp_path): """Test using the database and a specified siaf db""" expected_name = 'add_wcs_method_full_siafdb.fits' # Calculate stp.add_wcs(data_file, siaf_path=siaf_path, method=stp.Methods.OPS_TR_202111, engdb_url='http://localhost') # Test with datamodels.Level1bModel(data_file) as model: # Save for post-test comparison and update model.save(tmp_path / expected_name) with datamodels.open(DATA_PATH / expected_name) as expected: for meta in METAS_EQUALITY: if isinstance(model[meta], str): assert model[meta] == expected[meta] else: assert np.isclose(model[meta], expected[meta], atol=1e-13) for meta in METAS_ISCLOSE: assert np.isclose(model[meta], expected[meta]) assert word_precision_check(model.meta.wcsinfo.s_region, expected.meta.wcsinfo.s_region)
def test_add_wcs_default_nosiaf(data_file_nosiaf, caplog): """Handle when no pointing exists and the default is used and no SIAF specified.""" with pytest.raises(ValueError): stp.add_wcs(data_file_nosiaf, siaf_path=siaf_db, tolerance=0, allow_default=True)
def test_allow_any_file(file_case, allow_any_file): """Test various files against whether they should be allowed or not Parameters ---------- file_case : (Path-like, allow) File to test and whether it should always be allowed. If not `allow`, the file should be usable only when `allow_any_file`. allow_any_file : bool Value of `allow_any_file` to try """ path, allow = file_case with warnings.catch_warnings(): warnings.filterwarnings("ignore", "model_type not found") if not allow and not allow_any_file: with pytest.raises(TypeError): stp.add_wcs(path, siaf_path=siaf_path, allow_any_file=allow_any_file, dry_run=True) else: # Expected error when trying to actually add the wcs. # The provided files do not have sufficient info to do the calculations. with pytest.raises(AttributeError): stp.add_wcs(path, siaf_path=siaf_path, allow_any_file=allow_any_file, dry_run=True)
def test_miri_setpointing(_jail, rtdata, fitsdiff_default_kwargs): """ Regression test of the set_telescope_pointing script on a level-1b MIRI image. """ # Get SIAF PRD database file siaf_path = rtdata.get_data("common/prd.db") # Get the input level-1b file rtdata.get_data("miri/image/jw80600010001_02101_00001_mirimage_uncal.fits") # The add_wcs function overwrites its input, so output = input rtdata.output = rtdata.input # Call the WCS routine, using the ENGDB_Service # Note that there aren't any quaternion mnemonics in the ENGDB for the time # range of this exposure, so we set "allow_default" to tell add_wcs to use # default values for the pointing-related keywords. Keyword values retrieved # from the SIAF will be good. add_wcs(rtdata.input, allow_default=True, siaf_path=siaf_path) # Compare the results rtdata.get_truth( "truth/test_miri_setpointing/jw80600010001_02101_00001_mirimage_uncal.fits" ) fitsdiff_default_kwargs['rtol'] = 1e-6 diff = FITSDiff(rtdata.output, rtdata.truth, **fitsdiff_default_kwargs) assert diff.identical, diff.report()
def test_miri_setpointing(_bigdata): """ Regression test of the set_telescope_pointing script on a level-1b MIRI file. """ # Copy original version of file to test file, which will get overwritten by test copyfile( _bigdata + '/miri/test_pointing/jw80600010001_02101_00001_mirimage_uncal_orig.fits', 'jw80600010001_02101_00001_mirimage_uncal.fits') add_wcs('jw80600010001_02101_00001_mirimage_uncal.fits') h = fits.open('jw80600010001_02101_00001_mirimage_uncal.fits') href = fits.open( _bigdata + '/miri/test_pointing/jw80600010001_02101_00001_mirimage_uncal_ref.fits' ) newh = fits.HDUList([h['primary'], h['sci'], h['refout'], h['group']]) newhref = fits.HDUList( [href['primary'], href['sci'], href['refout'], href['group']]) result = fits.diff.FITSDiff( newh, newhref, ignore_keywords=['DATE', 'CAL_VER', 'CAL_VCS', 'CRDS_VER', 'CRDS_CTX'], rtol=0.000001) assert result.identical, result.report()
def test_add_wcs_method_gscmd(eng_db_ngas, data_file, tmp_path): """Test using the database and the original, pre-JSOCINT-555 algorithms""" expected_name = 'add_wcs_method_gscmd.fits' # Calculate stp.add_wcs(data_file, siaf_path=siaf_path, method=stp.Methods.GSCMD_J3PAGS, engdb_url='http://localhost') # Tests with datamodels.Level1bModel(data_file) as model: # Save for post-test comparison and update model.save(tmp_path / expected_name) with datamodels.open(DATA_PATH / expected_name) as expected: for meta in METAS_EQUALITY: if isinstance(model[meta], str): assert model[meta] == expected[meta], f'{meta} has changed' else: assert np.isclose(model[meta], expected[meta], atol=1e-13), f'{meta} has changed' for meta in METAS_ISCLOSE: assert np.isclose(model[meta], expected[meta]), f'{meta} has changed' assert word_precision_check(model.meta.wcsinfo.s_region, expected.meta.wcsinfo.s_region)
def test_add_wcs_method_full_nosiafdb(eng_db_ngas, data_file, tmp_path): """Test using the database""" # Only run if `pysiaf` is installed. pytest.importorskip('pysiaf') expected_name = 'add_wcs_method_full_nosiafdb.fits' # Calculate stp.add_wcs(data_file, method=stp.Methods.OPS_TR_202111, engdb_url='http://localhost') # Tests with datamodels.Level1bModel(data_file) as model: # Save for post-test comparison and update model.save(tmp_path / expected_name) with datamodels.open(DATA_PATH / expected_name) as expected: for meta in METAS_EQUALITY: assert model[meta] == expected[meta] for meta in METAS_ISCLOSE: assert np.isclose(model[meta], expected[meta]) assert word_precision_check(model.meta.wcsinfo.s_region, expected.meta.wcsinfo.s_region)
def test_nircam_setpointing_tsimg(_jail, rtdata, fitsdiff_default_kwargs): """ Regression test of the set_telescope_pointing script on a level-1b NIRCam TSO imaging file. """ # Get SIAF PRD database file siaf_path = rtdata.get_data("common/prd.db") rtdata.get_data( "nircam/tsimg/jw00312006001_02102_00001-seg001_nrcb1_uncal.fits") # The add_wcs function overwrites its input, so output = input rtdata.output = rtdata.input # Call the WCS routine, using the ENGDB_Service try: add_wcs(rtdata.input, siaf_path=siaf_path) except ValueError: pytest.skip('Engineering Database not available.') rtdata.get_truth( "truth/test_nircam_setpointing/jw00312006001_02102_00001-seg001_nrcb1_uncal.fits" ) fitsdiff_default_kwargs['rtol'] = 1e-6 diff = FITSDiff(rtdata.output, rtdata.truth, **fitsdiff_default_kwargs) assert diff.identical, diff.report()
def test_miri_setpointing(self): """ Regression test of the set_telescope_pointing script on a level-1b MIRI file. """ # Copy original version of file to test file, which will get overwritten by test input_file = self.get_data(self.test_dir, 'jw80600010001_02101_00001_mirimage_uncal_orig.fits', docopy=True # always produce local copy ) add_wcs(input_file) outputs = [(input_file, 'jw80600010001_02101_00001_mirimage_uncal_ref.fits')] self.compare_outputs(outputs)
def test_miri_setpointing(self): """ Regression test of the set_telescope_pointing script on a level-1b MIRI file. """ # Copy original version of file to test file, which will get overwritten by test input_file = self.get_data(self.test_dir, 'jw80600010001_02101_00001_mirimage_uncal_orig.fits', docopy=True # always produce local copy ) add_wcs(input_file, allow_default=True) outputs = [(input_file, 'jw80600010001_02101_00001_mirimage_uncal_ref.fits')] self.compare_outputs(outputs)
def test_miri_setpointing(self): """ Regression test of the set_telescope_pointing script on a level-1b MIRI file. """ # Copy original version of file to test file, which will get overwritten by test input_file = self.get_data(self.test_dir, 'jw80600010001_02101_00001_mirimage_uncal_orig.fits') # Get SIAF PRD database file siaf_prd_loc = ['jwst-pipeline', self.env, 'common', 'prd.db'] siaf_path = get_bigdata(*siaf_prd_loc) add_wcs(input_file, allow_default=True, siaf_path=siaf_path) outputs = [(input_file, 'jw80600010001_02101_00001_mirimage_uncal_ref.fits')] self.compare_outputs(outputs)
def test_add_wcs_fsmcorr_v1(data_file): """Test with default value using FSM original correction""" try: stp.add_wcs(data_file, fsmcorr_version='v1', siaf_path=siaf_db, tolerance=0, allow_default=True) except ValueError: pass # This is what we want for the test. except Exception as e: pytest.skip('Live ENGDB service is not accessible.' '\nException={}'.format(e)) with datamodels.Level1bModel(data_file) as model: assert model.meta.pointing.ra_v1 == TARG_RA assert model.meta.pointing.dec_v1 == TARG_DEC assert model.meta.pointing.pa_v3 == 0. assert model.meta.wcsinfo.wcsaxes == 2 assert model.meta.wcsinfo.crpix1 == 693.5 assert model.meta.wcsinfo.crpix2 == 512.5 assert model.meta.wcsinfo.crval1 == TARG_RA assert model.meta.wcsinfo.crval2 == TARG_DEC assert model.meta.wcsinfo.ctype1 == "RA---TAN" assert model.meta.wcsinfo.ctype2 == "DEC--TAN" assert model.meta.wcsinfo.cunit1 == 'deg' assert model.meta.wcsinfo.cunit2 == 'deg' assert np.isclose(model.meta.wcsinfo.cdelt1, 3.0555555e-5) assert np.isclose(model.meta.wcsinfo.cdelt2, 3.0555555e-5) assert np.isclose(model.meta.wcsinfo.pc1_1, -0.7558009243361943) assert np.isclose(model.meta.wcsinfo.pc1_2, 0.654801468211972) assert np.isclose(model.meta.wcsinfo.pc2_1, 0.654801468211972) assert np.isclose(model.meta.wcsinfo.pc2_2, 0.7558009243361943) assert model.meta.wcsinfo.v2_ref == 200.0 assert model.meta.wcsinfo.v3_ref == -350.0 assert model.meta.wcsinfo.vparity == -1 assert model.meta.wcsinfo.v3yangle == 42.0 assert model.meta.wcsinfo.ra_ref == TARG_RA assert model.meta.wcsinfo.dec_ref == TARG_DEC assert np.isclose(model.meta.wcsinfo.roll_ref, 358.9045979379) assert word_precision_check(model.meta.wcsinfo.s_region, ('POLYGON ICRS' ' 345.11054995209815 -87.02586884935684' ' 344.6537904121288 -87.00498014679253' ' 345.04569816117015 -86.98138111042982' ' 345.50498899320183 -87.00187988107017'))
def test_miri_setpointing(self): """ Regression test of the set_telescope_pointing script on a level-1b MIRI file. """ # Copy original version of file to test file, which will get overwritten by test input_file = self.get_data( self.test_dir, 'jw80600010001_02101_00001_mirimage_uncal_orig.fits') # Get SIAF PRD database file siaf_prd_loc = ['jwst-pipeline', self.env, 'common', 'prd.db'] siaf_path = get_bigdata(*siaf_prd_loc) add_wcs(input_file, allow_default=True, siaf_path=siaf_path) outputs = [(input_file, 'jw80600010001_02101_00001_mirimage_uncal_ref.fits')] self.compare_outputs(outputs)
def test_nircam_setpointing(_jail, rtdata, fitsdiff_default_kwargs): """ Regression test of the set_telescope_pointing script on a level-1b NIRCam file. """ # Get SIAF PRD database file siaf_path = rtdata.get_data("common/prd.db") rtdata.get_data( "nircam/tsgrism/jw00721012001_03103_00001-seg001_nrcalong_uncal.fits") # The add_wcs function overwrites its input rtdata.output = rtdata.input # Call the WCS routine, using the ENGDB_Service add_wcs(rtdata.input, siaf_path=siaf_path) rtdata.get_truth( "truth/test_nircam_setpointing/jw00721012001_03103_00001-seg001_nrcalong_uncal.fits" ) fitsdiff_default_kwargs['rtol'] = 1e-6 diff = FITSDiff(rtdata.output, rtdata.truth, **fitsdiff_default_kwargs) assert diff.identical, diff.report()
def test_add_wcs_with_db(eng_db_ngas, data_file, tmp_path): """Test using the database""" expected_name = 'add_wcs_with_db.fits' stp.add_wcs(data_file, siaf_path=siaf_path, engdb_url='http://localhost') # Tests with datamodels.Level1bModel(data_file) as model: # Save for post-test comparison and update model.save(tmp_path / expected_name) with datamodels.open(DATA_PATH / expected_name) as expected: for meta in METAS_EQUALITY: assert model[meta] == expected[meta] for meta in METAS_ISCLOSE: assert np.isclose(model[meta], expected[meta]) assert word_precision_check(model.meta.wcsinfo.s_region, expected.meta.wcsinfo.s_region)
def test_add_wcs_with_db_fsmcorr_v1(eng_db_ngas, data_file): """Test using the database with original FSM correction""" stp.add_wcs(data_file, fsmcorr_version='v1', siaf_path=siaf_db, j2fgs_transpose=False) with datamodels.Level1bModel(data_file) as model: assert np.isclose(model.meta.pointing.ra_v1, 348.9278669) assert np.isclose(model.meta.pointing.dec_v1, -38.749239) assert np.isclose(model.meta.pointing.pa_v3, 50.1767077) assert model.meta.wcsinfo.wcsaxes == 2 assert model.meta.wcsinfo.crpix1 == 693.5 assert model.meta.wcsinfo.crpix2 == 512.5 assert np.isclose(model.meta.wcsinfo.crval1, 348.8776709) assert np.isclose(model.meta.wcsinfo.crval2, -38.854159) assert model.meta.wcsinfo.ctype1 == "RA---TAN" assert model.meta.wcsinfo.ctype2 == "DEC--TAN" assert model.meta.wcsinfo.cunit1 == 'deg' assert model.meta.wcsinfo.cunit2 == 'deg' assert np.isclose(model.meta.wcsinfo.cdelt1, 3.0555555e-5) assert np.isclose(model.meta.wcsinfo.cdelt2, 3.0555555e-5) assert np.isclose(model.meta.wcsinfo.pc1_1, 0.03853303979862607) assert np.isclose(model.meta.wcsinfo.pc1_2, 0.9992573266400789) assert np.isclose(model.meta.wcsinfo.pc2_1, 0.9992573266400789) assert np.isclose(model.meta.wcsinfo.pc2_2, -0.03853303979862607) assert model.meta.wcsinfo.v2_ref == 200.0 assert model.meta.wcsinfo.v3_ref == -350.0 assert model.meta.wcsinfo.vparity == -1 assert model.meta.wcsinfo.v3yangle == 42.0 assert np.isclose(model.meta.wcsinfo.ra_ref, 348.8776709) assert np.isclose(model.meta.wcsinfo.dec_ref, -38.854159) assert np.isclose(model.meta.wcsinfo.roll_ref, 50.20832726650) assert word_precision_check( model.meta.wcsinfo.s_region, ('POLYGON ICRS' ' 348.8563379013152 -38.874810886750495' ' 348.85810582665334 -38.84318773861823' ' 348.8982592685148 -38.84439628911871' ' 348.89688051688233 -38.876020020321164'))
def test_miri_setpointing(_jail, rtdata, engdb, fitsdiff_default_kwargs): """ Regression test of the set_telescope_pointing script on a level-1b MIRI image. """ # Get SIAF PRD database file siaf_path = rtdata.get_data("common/prd.db") # Get the input level-1b file rtdata.get_data("miri/image/jw80600010001_02101_00001_mirimage_uncal.fits") # The add_wcs function overwrites its input, so output = input rtdata.output = rtdata.input # Call the WCS routine, using the ENGDB_Service add_wcs(rtdata.input, allow_default=True, engdb_url='http://localhost', siaf_path=siaf_path) # Compare the results rtdata.get_truth("truth/test_miri_setpointing/jw80600010001_02101_00001_mirimage_uncal.fits") fitsdiff_default_kwargs['rtol'] = 1e-6 diff = FITSDiff(rtdata.output, rtdata.truth, **fitsdiff_default_kwargs) assert diff.identical, diff.report()
def test_add_wcs_with_mast(data_file_fromsim, fgsid, tmp_path): """Test using the database""" expected_name = f'add_wcs_with_mast_fgs{fgsid}.fits' # See if access to MAST is available. try: engdb_mast.EngdbMast(base_url=engdb_mast.MAST_BASE_URL) except RuntimeError as exception: pytest.skip( f'Live MAST Engineering Service not available: {exception}') # Execute the operation. try: stp.add_wcs(data_file_fromsim, siaf_path=siaf_path, engdb_url=engdb_mast.MAST_BASE_URL, fgsid=fgsid) except ValueError as exception: pytest.xfail( f'No telemetry exists. Update test to use existing telemetry. Exception: {exception}' ) # Tests with datamodels.Level1bModel(data_file_fromsim) as model: # Save for post-test comparison and update model.save(tmp_path / expected_name) with datamodels.open(DATA_PATH / expected_name) as expected: for meta in METAS_EQUALITY: assert model[meta] == expected[meta] for meta in METAS_ISCLOSE: assert np.isclose(model[meta], expected[meta]) assert word_precision_check(model.meta.wcsinfo.s_region, expected.meta.wcsinfo.s_region)
RA_V1 DEC_V1 PA_V3 CRVAL1 CRVAL2 PC1_1 PC1_2 PC2_1 PC2_2 It does not currently place the new keywords in any particular location in the header other than what is required by the standard. ''' import logging import sys from jwst.lib.set_telescope_pointing import add_wcs logger = logging.getLogger('jwst') handler = logging.StreamHandler() handler.setLevel(logging.DEBUG) logger.addHandler(handler) logger.setLevel(logging.INFO) if __name__ == '__main__': if len(sys.argv) <= 1: raise ValueError('missing filename argument(s)') for filename in sys.argv[1:]: logger.info('Setting pointing for {}'.format(filename)) add_wcs(filename)
parser.add_argument( '--engdb_url', type=str, default=None, help=('URL of the engineering database.' ' If not specified, the environmental variable "ENG_BASE_URL" is used.' ' Otherwise, a hardwired default is used.') ) parser.add_argument( '--transpose_j2fgs', action='store_false', help='Transpose the J2FGS matrix' ) args = parser.parse_args() for filename in args.exposure: logger.info( '\n------' 'Setting pointing for {}'.format(filename) ) try: add_wcs( filename, siaf_path=args.siaf, engdb_url=args.engdb_url, tolerance=args.tolerance, allow_default=args.allow_default, dry_run=args.dry_run, j2fgs_transpose=args.transpose_j2fgs ) except ValueError as exception: logger.info('Cannot determine pointing information: ' + str(exception))
override_transforms = args.override_transforms if override_transforms: override_transforms = stp.Transforms.from_asdf(override_transforms) # Calculate WCS for all inputs. for filename in args.exposure: logger.info('\n------' 'Setting pointing for {}'.format(filename)) # Create path for saving the transforms. transform_path = None if args.save_transforms: path = Path(filename) transform_path = path.with_name(f'{path.stem}_transforms.asdf') try: stp.add_wcs(filename, siaf_path=args.siaf, engdb_url=args.engdb_url, fgsid=args.fgsid, tolerance=args.tolerance, allow_default=args.allow_default, dry_run=args.dry_run, method=args.method, j2fgs_transpose=args.transpose_j2fgs, save_transforms=transform_path, override_transforms=override_transforms) except ValueError as exception: logger.info('Cannot determine pointing information', exc_info=exception)
def test_strict_pointing(data_file, eng_db_jw703): """Test failure on strict pointing""" with pytest.raises(ValueError): stp.add_wcs(data_file, siaf_path=siaf_db, tolerance=0)
help= 'SIAF PRD sqlite database file. If not specified, default is to use `$XML_DATA/prd.db`' ) parser.add_argument( '--engdb_url', type=str, default=None, help= ('URL of the engineering database.' ' If not specified, the environmental variable "ENG_BASE_URL" is used.' ' Otherwise, a hardwired default is used.')) parser.add_argument('--transpose_j2fgs', action='store_false', help='Transpose the J2FGS matrix') args = parser.parse_args() for filename in args.exposure: logger.info('\n------' 'Setting pointing for {}'.format(filename)) try: add_wcs(filename, siaf_path=args.siaf, engdb_url=args.engdb_url, tolerance=args.tolerance, allow_default=args.allow_default, dry_run=args.dry_run, j2fgs_transpose=args.transpose_j2fgs) except ValueError as exception: logger.info('Cannot determine pointing information: ' + str(exception))
DEC_V1 PA_V3 CRVAL1 CRVAL2 PC1_1 PC1_2 PC2_1 PC2_2 It does not currently place the new keywords in any particular location in the header other than what is required by the standard. ''' import logging import sys from jwst.lib.set_telescope_pointing import add_wcs logger = logging.getLogger('jwst') handler = logging.StreamHandler() handler.setLevel(logging.DEBUG) logger.addHandler(handler) logger.setLevel(logging.INFO) if __name__ == '__main__': if len(sys.argv) <= 1: raise ValueError('missing filename argument(s)') for filename in sys.argv[1:]: logger.info('Setting pointing for {}'.format(filename)) add_wcs(filename)