コード例 #1
0
 def setUp(self):
     # Create a typical distortion product.
     self.bmatrix = [[0.1, 0.2, 0.3, 0.4], [0.5, 0.6, 0.7, 0.8],
                     [0.8, 0.7, 0.6, 0.5], [0.4, 0.3, 0.2, 0.1]]
     self.amatrix = [[0.1, 0.0, 0.0, 0.0], [0.0, 0.1, 0.0, 0.0],
                     [0.0, 0.0, 0.1, 0.0], [0.0, 0.0, 0.0, 0.1]]
     self.tmatrix = [[0.5, 0.0, 0.0], [0.2, 0.5, 0.2], [0.0, 0.0, 0.5]]
     self.mmatrix = [[0.0, 0.0, 0.1], [0.0, 0.1, 0.0], [0.1, 0.0, 0.0]]
     self.boresight_offsets = [('One', 0.1, 0.2), ('Two', 0.2, 0.3)]
     self.dataproduct = MiriImagingDistortionModel(
         amatrix=self.bmatrix,
         bmatrix=self.amatrix,
         tmatrix=self.tmatrix,
         mmatrix=self.mmatrix,
         dmatrix=self.bmatrix,
         cmatrix=self.amatrix,
         fmatrix=self.bmatrix,
         ematrix=self.amatrix,
         boresight_offsets=self.boresight_offsets)
     # Add some example metadata.
     self.dataproduct.set_target_metadata(0.0, 0.0)
     self.dataproduct.set_instrument_metadata(detector='MIRIFUSHORT',
                                              channel='1',
                                              ccc_pos='CLOSED',
                                              deck_temperature=11.0,
                                              detector_temperature=6.0)
     self.dataproduct.set_exposure_metadata(readpatt='FAST',
                                            nints=1,
                                            ngroups=1,
                                            frame_time=1.0,
                                            integration_time=10.0,
                                            group_time=10.0,
                                            reset_time=0,
                                            frame_resets=3)
     self.testfile = "MiriImagingDistortionModel_test.fits"
コード例 #2
0
    def test_creation(self):
        # Check the successful creation of a data product with all 4 matrices.
        dataproduct1 = MiriImagingDistortionModel(
            bmatrix=self.bmatrix,
            amatrix=self.amatrix,
            tmatrix=self.tmatrix,
            mmatrix=self.mmatrix,
            boresight_offsets=self.boresight_offsets)
        self.assertTrue(np.allclose(dataproduct1.bmatrix, self.bmatrix))
        self.assertTrue(np.allclose(dataproduct1.amatrix, self.amatrix))
        self.assertTrue(np.allclose(dataproduct1.tmatrix, self.tmatrix))
        self.assertTrue(np.allclose(dataproduct1.mmatrix, self.mmatrix))
        descr = str(dataproduct1)
        self.assertIsNotNone(descr)
        del dataproduct1

        # It should be possible to create a model containing A and B matrices
        # only.
        dataproduct2 = MiriImagingDistortionModel(bmatrix=self.bmatrix,
                                                  amatrix=self.amatrix)
        self.assertTrue(np.allclose(dataproduct2.bmatrix, self.bmatrix))
        self.assertTrue(np.allclose(dataproduct2.amatrix, self.amatrix))
        descr = str(dataproduct2)
        self.assertIsNotNone(descr)
        del dataproduct2

        # Attempting to create a data model with arrays of the wrong shape
        # should fail.
        amatrix1D = [0.1, 0.2, 0.3, 0.4]
        bmatrix1D = [0.1, 0.0, 0.0, 0.0]
        self.assertRaises(TypeError,
                          MiriImagingDistortionModel,
                          amatrix=amatrix1D,
                          bmatrix=bmatrix1D)

        # It should be possible to set up an empty data product with
        # a specified shape. Both arrays should be initialised to
        # the same shape.
        emptydp = MiriImagingDistortionModel((4, 4))
        self.assertIsNotNone(emptydp.amatrix)
        self.assertEqual(emptydp.amatrix.shape, (4, 4))
        self.assertIsNotNone(emptydp.bmatrix)
        self.assertEqual(emptydp.bmatrix.shape, (4, 4))
        descr = str(emptydp)
        self.assertIsNotNone(descr)
        del emptydp, descr

        # A null data product can also be created and populated
        # with data later.
        nulldp = MiriImagingDistortionModel()
        descr1 = str(nulldp)
        nulldp.amatrix = np.asarray(self.amatrix)
        self.assertIsNotNone(nulldp.amatrix)
        nulldp.bmatrix = np.asarray(self.bmatrix)
        self.assertIsNotNone(nulldp.bmatrix)
        descr2 = str(nulldp)
        del nulldp, descr1, descr2
コード例 #3
0
    def test_fitsio(self):
        # Suppress metadata warnings
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")

            # Check that the data product can be written to a FITS
            # file and read back again without changing the data.
            self.dataproduct.save(self.testfile, overwrite=True)
            with MiriImagingDistortionModel(self.testfile) as readback:
                self.assertEqual(self.dataproduct.meta.reftype,
                                 readback.meta.reftype)
                self.assertEqual(self.dataproduct.meta.model_type,
                                 readback.meta.model_type)
                self.assertTrue(
                    np.allclose(self.dataproduct.amatrix, readback.amatrix))
                self.assertTrue(
                    np.allclose(self.dataproduct.bmatrix, readback.bmatrix))
                del readback
コード例 #4
0
class TestMiriImagingDistortionModel(unittest.TestCase):

    # Test the MiriImagingDistortionModel class.

    def setUp(self):
        # Create a typical distortion product.
        self.bmatrix = [[0.1, 0.2, 0.3, 0.4], [0.5, 0.6, 0.7, 0.8],
                        [0.8, 0.7, 0.6, 0.5], [0.4, 0.3, 0.2, 0.1]]
        self.amatrix = [[0.1, 0.0, 0.0, 0.0], [0.0, 0.1, 0.0, 0.0],
                        [0.0, 0.0, 0.1, 0.0], [0.0, 0.0, 0.0, 0.1]]
        self.tmatrix = [[0.5, 0.0, 0.0], [0.2, 0.5, 0.2], [0.0, 0.0, 0.5]]
        self.mmatrix = [[0.0, 0.0, 0.1], [0.0, 0.1, 0.0], [0.1, 0.0, 0.0]]
        self.boresight_offsets = [('One', 0.1, 0.2), ('Two', 0.2, 0.3)]
        self.dataproduct = MiriImagingDistortionModel(
            amatrix=self.bmatrix,
            bmatrix=self.amatrix,
            tmatrix=self.tmatrix,
            mmatrix=self.mmatrix,
            dmatrix=self.bmatrix,
            cmatrix=self.amatrix,
            fmatrix=self.bmatrix,
            ematrix=self.amatrix,
            boresight_offsets=self.boresight_offsets)
        # Add some example metadata.
        self.dataproduct.set_target_metadata(0.0, 0.0)
        self.dataproduct.set_instrument_metadata(detector='MIRIFUSHORT',
                                                 channel='1',
                                                 ccc_pos='CLOSED',
                                                 deck_temperature=11.0,
                                                 detector_temperature=6.0)
        self.dataproduct.set_exposure_metadata(readpatt='FAST',
                                               nints=1,
                                               ngroups=1,
                                               frame_time=1.0,
                                               integration_time=10.0,
                                               group_time=10.0,
                                               reset_time=0,
                                               frame_resets=3)
        self.testfile = "MiriImagingDistortionModel_test.fits"

    def tearDown(self):
        # Tidy up
        del self.dataproduct
        # TBD
        # Remove temporary file, if able to.
        if os.path.isfile(self.testfile):
            try:
                os.remove(self.testfile)
            except Exception as e:
                strg = "Could not remove temporary file, " + self.testfile + \
                    "\n   " + str(e)
                warnings.warn(strg)

    def test_referencefile(self):
        # Check that the data product contains the standard
        # reference file metadata.
        type1 = self.dataproduct.meta.model_type
        type2 = self.dataproduct.meta.reftype
        self.assertIsNotNone(type1)
        self.assertIsNotNone(type2)
        pedigree = self.dataproduct.meta.pedigree
        self.assertIsNotNone(pedigree)

    def test_creation(self):
        # Check the successful creation of a data product with all 4 matrices.
        dataproduct1 = MiriImagingDistortionModel(
            bmatrix=self.bmatrix,
            amatrix=self.amatrix,
            tmatrix=self.tmatrix,
            mmatrix=self.mmatrix,
            boresight_offsets=self.boresight_offsets)
        self.assertTrue(np.allclose(dataproduct1.bmatrix, self.bmatrix))
        self.assertTrue(np.allclose(dataproduct1.amatrix, self.amatrix))
        self.assertTrue(np.allclose(dataproduct1.tmatrix, self.tmatrix))
        self.assertTrue(np.allclose(dataproduct1.mmatrix, self.mmatrix))
        descr = str(dataproduct1)
        self.assertIsNotNone(descr)
        del dataproduct1

        # It should be possible to create a model containing A and B matrices
        # only.
        dataproduct2 = MiriImagingDistortionModel(bmatrix=self.bmatrix,
                                                  amatrix=self.amatrix)
        self.assertTrue(np.allclose(dataproduct2.bmatrix, self.bmatrix))
        self.assertTrue(np.allclose(dataproduct2.amatrix, self.amatrix))
        descr = str(dataproduct2)
        self.assertIsNotNone(descr)
        del dataproduct2

        # Attempting to create a data model with arrays of the wrong shape
        # should fail.
        amatrix1D = [0.1, 0.2, 0.3, 0.4]
        bmatrix1D = [0.1, 0.0, 0.0, 0.0]
        self.assertRaises(TypeError,
                          MiriImagingDistortionModel,
                          amatrix=amatrix1D,
                          bmatrix=bmatrix1D)

        # It should be possible to set up an empty data product with
        # a specified shape. Both arrays should be initialised to
        # the same shape.
        emptydp = MiriImagingDistortionModel((4, 4))
        self.assertIsNotNone(emptydp.amatrix)
        self.assertEqual(emptydp.amatrix.shape, (4, 4))
        self.assertIsNotNone(emptydp.bmatrix)
        self.assertEqual(emptydp.bmatrix.shape, (4, 4))
        descr = str(emptydp)
        self.assertIsNotNone(descr)
        del emptydp, descr

        # A null data product can also be created and populated
        # with data later.
        nulldp = MiriImagingDistortionModel()
        descr1 = str(nulldp)
        nulldp.amatrix = np.asarray(self.amatrix)
        self.assertIsNotNone(nulldp.amatrix)
        nulldp.bmatrix = np.asarray(self.bmatrix)
        self.assertIsNotNone(nulldp.bmatrix)
        descr2 = str(nulldp)
        del nulldp, descr1, descr2

    def test_copy(self):
        # Test that a copy can be made of the data product.
        datacopy = self.dataproduct.copy()
        self.assertIsNotNone(datacopy)
        self.assertTrue(np.allclose(self.dataproduct.amatrix,
                                    datacopy.amatrix))
        self.assertTrue(np.allclose(self.dataproduct.bmatrix,
                                    datacopy.bmatrix))
        del datacopy

    def test_fitsio(self):
        # Suppress metadata warnings
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")

            # Check that the data product can be written to a FITS
            # file and read back again without changing the data.
            self.dataproduct.save(self.testfile, overwrite=True)
            with MiriImagingDistortionModel(self.testfile) as readback:
                self.assertEqual(self.dataproduct.meta.reftype,
                                 readback.meta.reftype)
                self.assertEqual(self.dataproduct.meta.model_type,
                                 readback.meta.model_type)
                self.assertTrue(
                    np.allclose(self.dataproduct.amatrix, readback.amatrix))
                self.assertTrue(
                    np.allclose(self.dataproduct.bmatrix, readback.bmatrix))
                del readback

    def test_description(self):
        # Test that the querying and description functions work.
        # For the test to pass these need to run without error
        # and generate non-null strings.
        descr = str(self.dataproduct)
        self.assertIsNotNone(descr)
        del descr
        descr = repr(self.dataproduct)
        self.assertIsNotNone(descr)
        del descr
        descr = self.dataproduct.stats()
        self.assertIsNotNone(descr)
        del descr
コード例 #5
0
        print(help_text)
        time.sleep(1) # Ensure help text appears before error messages.
        parser.error("Not enough arguments provided")
        sys.exit(1)

    verb = options.verb
    makeplot = options.makeplot
    overwrite = options.overwrite

    # Read the given file.
    if verb:
        print("Reading %s" % inputfile)
    header, inputdata,  = load_distortion_fits( inputfile )
    
    # Convert the distortion data into a MIRI Distortion CDP
    with MiriImagingDistortionModel( init=inputfile ) as oldproduct:
        
        # Make a copy of the data product just created.
        distortproduct = oldproduct.copy()
        
        # Copy over distortion data
        distortproduct.cmatrix = inputdata[0]
        distortproduct.rmatrix = inputdata[1]
        
        # default modifications for CDP-1 specifications
        distortproduct.meta.filename_original = os.path.basename(inputfile)
        distortproduct.meta.filename = os.path.basename(outputfile)

        if verb:
            print(distortproduct)
        if makeplot: