Exemple #1
0
 def create_real_first_image(self, path="data/testimg.fits"):
     # Put a real fits image on the first source, first observation
     apcor = ApcorData.from_string("4 15   0.19   0.01")
     hdulist = fits.open(self.get_abs_path(path))
     first_reading = self.model.get_current_workunit().get_sources(
     )[0].get_readings()[0]
     self.first_snapshot = SourceCutout(first_reading, hdulist, apcor)
     self.image_manager.on_singlet_image_loaded(self.first_snapshot)
Exemple #2
0
    def setUp(self):
        test_file = "data/image_reading/realstest2.measure3.reals.astrom"
        astrom_data = AstromParser().parse(self.get_abs_path(test_file))

        self.reading = astrom_data.get_sources()[0].get_reading(0)
        self.original_observed_x = self.reading.x
        self.original_observed_y = self.reading.y

        self.offset_x = 10
        self.offset_y = 15
        coordinate_converter = CoordinateConverter(self.offset_x,
                                                   self.offset_y)

        self.undertest = SourceCutout(self.reading, Mock())

        self.mock_update_ra_dec = Mock()
        self.undertest._update_ra_dec = self.mock_update_ra_dec
Exemple #3
0
    def setUp(self):
        test_file = self.path("realstest2.measure3.reals.astrom")
        astrom_data = AstromParser().parse(test_file)

        self.reading = astrom_data.get_sources()[0].get_reading(0)

        # Load the real image
        hdulist = fits.open(self.path("cutout-1616687p.fits"))

        with open(self.path("1616687p10.apcor")) as fh:
            apcor = ApcorData.from_string(fh.read())

        # NOTE: the test image
        # vos://cadc.nrc.ca~vospace/OSSOS/dbimages/1616687/1616687p.fits
        # has cutout: [11][1449:1199,1429:1179] and is inverted.
        #
        # Therefore the first reading at (772.13, 3334.70) in observed
        # coordinates is at
        # (NAX1 - max cutout x, NAX2 - max cutout y)
        #  = (2112 - 1449, 4644 - 1429) = (663, 3215)
        self.original_pixel_x = 663
        self.original_pixel_y = 3215

        self.original_observed_x = 772.13
        self.original_observed_y = 3334.70

        x_offset = self.original_observed_x - self.original_pixel_x
        y_offset = self.original_observed_y - self.original_pixel_y

        self.undertest = SourceCutout(self.reading, hdulist,
                                      CoordinateConverter(x_offset, y_offset),
                                      apcor=apcor)

        assert_that(self.undertest.observed_x, equal_to(self.original_observed_x))
        assert_that(self.undertest.observed_y, equal_to(self.original_observed_y))

        assert_that(self.undertest.pixel_x, equal_to(self.original_pixel_x))
        assert_that(self.undertest.pixel_y, equal_to(self.original_pixel_y))
Exemple #4
0
    def setUp(self):
        test_file = "data/image_reading/realstest2.measure3.reals.astrom"
        astrom_data = AstromParser().parse(self.get_abs_path(test_file))

        self.reading = astrom_data.get_sources()[0].get_reading(0)
        self.original_observed_x = self.reading.x
        self.original_observed_y = self.reading.y

        self.offset_x = 10
        self.offset_y = 15
        coordinate_converter = CoordinateConverter(self.offset_x, self.offset_y)

        self.undertest = SourceCutout(self.reading, Mock(), coordinate_converter)

        self.mock_update_ra_dec = Mock()
        self.undertest._update_ra_dec = self.mock_update_ra_dec
Exemple #5
0
class SourceSnapshotIntegrationTest(FileReadingTestCase):
    def path(self, relative):
        return self.get_abs_path("data/image_reading/%s" % relative)

    def setUp(self):
        test_file = self.path("realstest2.measure3.reals.astrom")
        astrom_data = AstromParser().parse(test_file)

        self.reading = astrom_data.get_sources()[0].get_reading(0)

        # Load the real image
        hdulist = fits.open(self.path("cutout-1616687p.fits"))

        with open(self.path("1616687p10.apcor")) as fh:
            apcor = ApcorData.from_string(fh.read())

        # NOTE: the test image
        # vos://cadc.nrc.ca~vospace/OSSOS/dbimages/1616687/1616687p.fits
        # has cutout: [11][1449:1199,1429:1179] and is inverted.
        #
        # Therefore the first reading at (772.13, 3334.70) in observed
        # coordinates is at
        # (NAX1 - max cutout x, NAX2 - max cutout y)
        #  = (2112 - 1449, 4644 - 1429) = (663, 3215)
        self.original_pixel_x = 663
        self.original_pixel_y = 3215

        self.original_observed_x = 772.13
        self.original_observed_y = 3334.70

        x_offset = self.original_observed_x - self.original_pixel_x
        y_offset = self.original_observed_y - self.original_pixel_y

        self.undertest = SourceCutout(self.reading, hdulist,
                                      CoordinateConverter(x_offset, y_offset),
                                      apcor=apcor)

        assert_that(self.undertest.observed_x, equal_to(self.original_observed_x))
        assert_that(self.undertest.observed_y, equal_to(self.original_observed_y))

        assert_that(self.undertest.pixel_x, equal_to(self.original_pixel_x))
        assert_that(self.undertest.pixel_y, equal_to(self.original_pixel_y))

    @patch("ossos.wcs.xy2sky")
    def test_update_ra_dec(self, mock_xy2sky):
        new_ra = 2000.0
        new_dec = -11.0
        mock_xy2sky.return_value = (new_ra, new_dec)

        diff_x = 5.5
        diff_y = 4.5
        new_pixel_x = self.original_pixel_x + diff_x
        new_pixel_y = self.original_pixel_y + diff_y

        self.undertest.update_pixel_location((new_pixel_x, new_pixel_y))

        assert_that(self.undertest.ra, equal_to(new_ra))
        assert_that(self.undertest.dec, equal_to(new_dec))

        # Check it was called with the correct values:
        #   x, y, crpix1, crpix2, crval1, crval2, cd, pv, nord

        # These values from FITS header:

        crpix1 = 6.779623933420000E+03
        crpix2 = -3.343209710100000E+03

        crval1 = 211.828420000
        crval2 = -11.8333100000

        # CD1_1, CD1_2, CD2_1, CD2_2
        cd = [[-5.156837193510000E-05, -3.588985558218000E-07],
              [-1.674871346376000E-07, 5.178515755263000E-05]]

        # PV1_0 - PV1_10, PV2_0 - PV2_10
        pv = [[-1.909806626877E-03, 1.00815723310, 1.983393486250E-03,
               0.00000000000, 4.623172227977E-05, -1.020423075276E-05,
               2.258600565396E-05, -2.354656733463E-02, -1.671912720931E-04,
               -2.339051960031E-02, -2.903290518437E-05],
              [-7.294883106769E-04, 1.00392029973, 1.983331686156E-03,
               0.00000000000, 3.022667872225E-05, 4.509364811534E-05,
               -5.385195875663E-06, -2.333221106141E-02, -1.789051786060E-04,
               -2.348803123711E-02, -2.465552723903E-05]]

        # NORDFIT
        nord = 3

        # NOTE: the x and y passed in must be the updated OBSERVED coordinates
        mock_xy2sky.assert_called_once_with(
            new_pixel_x, new_pixel_y,
            crpix1, crpix2, crval1, crval2,
            cd, pv, nord)

    @patch("ossos.daophot.phot_mag")
    def test_get_observed_magnitude(self, mock_phot_mag):
        self.undertest.get_observed_magnitude()

        # NOTE: the x and y passed in must be the PIXEL coordinates
        mock_phot_mag.assert_called_once_with(
            ANY,
            self.original_pixel_x,
            self.original_pixel_y,
            aperture=5.0,
            sky=21.0,
            swidth=5.0,
            apcor=0.23,
            maxcount=30000.0)
Exemple #6
0
class SourceSnapshotTest(FileReadingTestCase):
    def setUp(self):
        test_file = "data/image_reading/realstest2.measure3.reals.astrom"
        astrom_data = AstromParser().parse(self.get_abs_path(test_file))

        self.reading = astrom_data.get_sources()[0].get_reading(0)
        self.original_observed_x = self.reading.x
        self.original_observed_y = self.reading.y

        self.offset_x = 10
        self.offset_y = 15
        coordinate_converter = CoordinateConverter(self.offset_x,
                                                   self.offset_y)

        self.undertest = SourceCutout(self.reading, Mock())

        self.mock_update_ra_dec = Mock()
        self.undertest._update_ra_dec = self.mock_update_ra_dec

    def assert_update_ra_dec_call_count_equals(self, expected_calls):
        assert_that(self.mock_update_ra_dec.call_count,
                    equal_to(expected_calls))

    def test_xy_coordinate_systems(self):
        assert_that(self.undertest.observed_x,
                    equal_to(self.original_observed_x))
        assert_that(self.undertest.observed_y,
                    equal_to(self.original_observed_y))

        expected_pixel_x = self.original_observed_x - self.offset_x
        expected_pixel_y = self.original_observed_y - self.offset_y

        assert_that(self.undertest.pixel_x, equal_to(expected_pixel_x))
        assert_that(self.undertest.pixel_y, equal_to(expected_pixel_y))

        assert_that(
            self.undertest.observed_source_point,
            equal_to((self.original_observed_x, self.original_observed_y)))
        assert_that(self.undertest.pixel_source_point,
                    equal_to((expected_pixel_x, expected_pixel_y)))

    def test_xy_adjusted(self):
        assert_that(self.undertest.observed_x,
                    equal_to(self.original_observed_x))
        assert_that(self.undertest.observed_y,
                    equal_to(self.original_observed_y))

        assert_that(self.undertest.is_adjusted(), equal_to(False))

        diff_x = 2.5
        diff_y = 3.5

        new_pixel_x = self.undertest.pixel_x + diff_x
        new_pixel_y = self.undertest.pixel_y + diff_y

        self.undertest.update_pixel_location((new_pixel_x, new_pixel_y))

        assert_that(self.undertest.pixel_x, equal_to(new_pixel_x))
        assert_that(self.undertest.pixel_y, equal_to(new_pixel_y))

        assert_that(self.undertest.observed_x,
                    equal_to(self.original_observed_x + diff_x))
        assert_that(self.undertest.observed_y,
                    equal_to(self.original_observed_y + diff_y))

        assert_that(self.undertest.is_adjusted(), equal_to(True))

    def test_update_xy_updates_ra_dec(self):
        self.assert_update_ra_dec_call_count_equals(0)

        self.undertest.update_pixel_location((665.0, 3215.0))

        _ = self.undertest.dec

        self.assert_update_ra_dec_call_count_equals(1)

    def test_ra_dec_updates_lazilly(self):
        self.assert_update_ra_dec_call_count_equals(0)

        self.undertest.update_pixel_location((665.0, 3215.0))

        self.assert_update_ra_dec_call_count_equals(0)

        self.undertest.update_pixel_location((665.0, 3218.0))

        self.assert_update_ra_dec_call_count_equals(0)

        _ = self.undertest.ra

        self.assert_update_ra_dec_call_count_equals(1)

        _ = self.undertest.dec

        self.assert_update_ra_dec_call_count_equals(1)

        self.undertest.update_pixel_location((667.2, 3216.6))

        self.assert_update_ra_dec_call_count_equals(1)

        _ = self.undertest.dec
        _ = self.undertest.ra

        self.assert_update_ra_dec_call_count_equals(2)

    def test_original_xy(self):
        assert_that(self.undertest.observed_x,
                    equal_to(self.original_observed_x))
        assert_that(self.undertest.observed_y,
                    equal_to(self.original_observed_y))

        assert_that(self.undertest.original_observed_x,
                    equal_to(self.original_observed_x))
        assert_that(self.undertest.original_observed_y,
                    equal_to(self.original_observed_y))

        diff_x = 2.5
        diff_y = 3.5

        new_pixel_x = self.undertest.pixel_x + diff_x
        new_pixel_y = self.undertest.pixel_y + diff_y

        self.undertest.update_pixel_location((new_pixel_x, new_pixel_y))

        assert_that(self.undertest.observed_x,
                    equal_to(self.original_observed_x + diff_x))
        assert_that(self.undertest.observed_y,
                    equal_to(self.original_observed_y + diff_y))

        assert_that(self.undertest.original_observed_x,
                    equal_to(self.original_observed_x))
        assert_that(self.undertest.original_observed_y,
                    equal_to(self.original_observed_y))

    def test_reset_location(self):
        original_pixel_x, original_pixel_y = self.undertest.pixel_source_point

        diff_x = 2.5
        diff_y = 3.5
        new_pixel_x = self.undertest.pixel_x + diff_x
        new_pixel_y = self.undertest.pixel_y + diff_y

        self.undertest.update_pixel_location((new_pixel_x, new_pixel_y))

        assert_that(self.undertest.observed_x,
                    equal_to(self.original_observed_x + diff_x))
        assert_that(self.undertest.observed_y,
                    equal_to(self.original_observed_y + diff_y))

        assert_that(self.undertest.pixel_x, equal_to(new_pixel_x))
        assert_that(self.undertest.pixel_y, equal_to(new_pixel_y))

        self.undertest.reset_source_location()

        assert_that(self.undertest.observed_x,
                    equal_to(self.original_observed_x))
        assert_that(self.undertest.observed_y,
                    equal_to(self.original_observed_y))

        assert_that(self.undertest.pixel_x, equal_to(original_pixel_x))
        assert_that(self.undertest.pixel_y, equal_to(original_pixel_y))

    def test_reset_location_is_adjusted_false(self):
        assert_that(self.undertest.is_adjusted(), equal_to(False))

        self.undertest.update_pixel_location((100, 100))

        assert_that(self.undertest.is_adjusted(), equal_to(True))

        self.undertest.reset_source_location()

        assert_that(self.undertest.is_adjusted(), equal_to(False))

    def test_reset_location_makes_data_stale(self):
        self.assert_update_ra_dec_call_count_equals(0)

        self.undertest.update_pixel_location((665.0, 3215.0))

        _ = self.undertest.ra

        self.assert_update_ra_dec_call_count_equals(1)

        self.undertest.reset_source_location()

        self.assert_update_ra_dec_call_count_equals(1)

        _ = self.undertest.ra

        self.assert_update_ra_dec_call_count_equals(2)
Exemple #7
0
    def download_cutout(self, reading, focus=None, needs_apcor=False):
        """
        Downloads a cutout of the FITS image for a given source reading.

        Args:
          source_reading: ossos.astrom.SourceReading
            The reading which will be the focus of the downloaded image.
          focus: tuple(int, int)
            The x, y coordinates that should be the focus of the downloaded
            image.  These coordinates should be in terms of the
            source_reading parameter's coordinate system.
            Default value is None, in which case the source reading's x, y
            position is used as the focus.
          needs_apcor: bool
            If True, the apcor file with data needed for photometry
            calculations is downloaded in addition to the image.
            Defaults to False.

        Returns:
          cutout: ossos.downloads.data.SourceCutout
        """
        if focus is None:
            focus = reading.source_point

        assert isinstance(reading, SourceReading)
        dx = dy = 2 * max(reading.dra, reading.ddec)
        dx = max(reading.dx, dx)
        dy = max(reading.dy, dy)

        (NAXIS1, NAXIS2) = reading.get_original_image_size()

        cutout_str, converter = self.cutout_calculator.build_cutout_str(
            reading.get_extension(),
            focus, (NAXIS1, NAXIS2),
            dx=dx,
            dy=dy,
            inverted=reading.is_inverted)

        image_uri = reading.get_image_uri()
        cutout = re.findall(r'(\d+)', cutout_str)
        y2 = int(cutout[-1])
        y1 = int(cutout[-2])
        logger.debug("Calculated cutout: %s for %s" % (cutout_str, image_uri))

        hdulist = storage.get_image(expnum=reading.get_exposure_number(),
                                    ccd=reading.get_ccd_num(),
                                    cutout=cutout_str,
                                    version=reading.get_observation().ftype,
                                    prefix=reading.get_observation().fk,
                                    return_file=False)

        #hdulist = self.download_hdulist(image_uri, view="cutout",
        #                                cutout=cutout_str)
        # modify the DATASEC to account for possible flip/flop and changes in dimensions of the image.
        DATASEC = hdulist[0].header.get('DATASEC', None)
        if DATASEC is not None:
            datasec = re.findall(r'(\d+)', DATASEC)
            if y2 < y1:
                x2 = int(NAXIS1) - int(datasec[0]) + 1
                x1 = int(NAXIS1) - int(datasec[1]) + 1
                y2 = int(NAXIS2) - int(datasec[2]) + 1
                y1 = int(NAXIS2) - int(datasec[3]) + 1
                logger.debug(
                    "Flip/Flopped DATASEC from {} to [{}:{}:{}:{}]".format(
                        DATASEC, x1, x2, y1, y2))
                datasec = (x1, x2, y1, y2)
            (x1, y1) = converter.convert((int(datasec[0]), int(datasec[2])))
            x1 = max(1, x1)
            y1 = max(1, y1)
            (x2, y2) = converter.convert((int(datasec[1]), int(datasec[3])))
            x2 = min(x2, int(hdulist[0].header['NAXIS1']))
            y2 = min(y2, int(hdulist[0].header['NAXIS2']))
            datasec = "[{}:{},{}:{}]".format(x1, x2, y1, y2)
            logger.debug("Trimmed and offset DATASEC from {} to {}".format(
                DATASEC, datasec))

            hdulist[0].header['DATASEC'] = datasec

        apcor = None
        if needs_apcor:
            try:
                apcor = self.download_apcor(reading.get_apcor_uri())
            except Exception as e:
                logger.error(str(e))
                apcor = None
        zmag = None
        try:
            zmag = self.download_zmag(reading.get_zmag_uri())
        except Exception as e:
            logger.error(str(e))
            pass

        return SourceCutout(reading, hdulist, converter, apcor, zmag=zmag)
Exemple #8
0
class SourceSnapshotTest(FileReadingTestCase):
    def setUp(self):
        test_file = "data/image_reading/realstest2.measure3.reals.astrom"
        astrom_data = AstromParser().parse(self.get_abs_path(test_file))

        self.reading = astrom_data.get_sources()[0].get_reading(0)
        self.original_observed_x = self.reading.x
        self.original_observed_y = self.reading.y

        self.offset_x = 10
        self.offset_y = 15
        coordinate_converter = CoordinateConverter(self.offset_x, self.offset_y)

        self.undertest = SourceCutout(self.reading, Mock(), coordinate_converter)

        self.mock_update_ra_dec = Mock()
        self.undertest._update_ra_dec = self.mock_update_ra_dec

    def assert_update_ra_dec_call_count_equals(self, expected_calls):
        assert_that(self.mock_update_ra_dec.call_count,
                    equal_to(expected_calls))

    def test_xy_coordinate_systems(self):
        assert_that(self.undertest.observed_x,
                    equal_to(self.original_observed_x))
        assert_that(self.undertest.observed_y,
                    equal_to(self.original_observed_y))

        expected_pixel_x = self.original_observed_x - self.offset_x
        expected_pixel_y = self.original_observed_y - self.offset_y

        assert_that(self.undertest.pixel_x, equal_to(expected_pixel_x))
        assert_that(self.undertest.pixel_y, equal_to(expected_pixel_y))

        assert_that(self.undertest.observed_source_point,
                    equal_to((self.original_observed_x, self.original_observed_y)))
        assert_that(self.undertest.pixel_source_point,
                    equal_to((expected_pixel_x, expected_pixel_y)))

    def test_xy_adjusted(self):
        assert_that(self.undertest.observed_x,
                    equal_to(self.original_observed_x))
        assert_that(self.undertest.observed_y,
                    equal_to(self.original_observed_y))

        assert_that(self.undertest.is_adjusted(), equal_to(False))

        diff_x = 2.5
        diff_y = 3.5

        new_pixel_x = self.undertest.pixel_x + diff_x
        new_pixel_y = self.undertest.pixel_y + diff_y

        self.undertest.update_pixel_location((new_pixel_x, new_pixel_y))

        assert_that(self.undertest.pixel_x, equal_to(new_pixel_x))
        assert_that(self.undertest.pixel_y, equal_to(new_pixel_y))

        assert_that(self.undertest.observed_x,
                    equal_to(self.original_observed_x + diff_x))
        assert_that(self.undertest.observed_y,
                    equal_to(self.original_observed_y + diff_y))

        assert_that(self.undertest.is_adjusted(), equal_to(True))

    def test_update_xy_updates_ra_dec(self):
        self.assert_update_ra_dec_call_count_equals(0)

        self.undertest.update_pixel_location((665.0, 3215.0))

        _ = self.undertest.dec

        self.assert_update_ra_dec_call_count_equals(1)

    def test_ra_dec_updates_lazilly(self):
        self.assert_update_ra_dec_call_count_equals(0)

        self.undertest.update_pixel_location((665.0, 3215.0))

        self.assert_update_ra_dec_call_count_equals(0)

        self.undertest.update_pixel_location((665.0, 3218.0))

        self.assert_update_ra_dec_call_count_equals(0)

        _ = self.undertest.ra

        self.assert_update_ra_dec_call_count_equals(1)

        _ = self.undertest.dec

        self.assert_update_ra_dec_call_count_equals(1)

        self.undertest.update_pixel_location((667.2, 3216.6))

        self.assert_update_ra_dec_call_count_equals(1)

        _ = self.undertest.dec
        _ = self.undertest.ra

        self.assert_update_ra_dec_call_count_equals(2)

    def test_original_xy(self):
        assert_that(self.undertest.observed_x, equal_to(self.original_observed_x))
        assert_that(self.undertest.observed_y, equal_to(self.original_observed_y))

        assert_that(self.undertest.original_observed_x,
                    equal_to(self.original_observed_x))
        assert_that(self.undertest.original_observed_y,
                    equal_to(self.original_observed_y))

        diff_x = 2.5
        diff_y = 3.5

        new_pixel_x = self.undertest.pixel_x + diff_x
        new_pixel_y = self.undertest.pixel_y + diff_y

        self.undertest.update_pixel_location((new_pixel_x, new_pixel_y))

        assert_that(self.undertest.observed_x,
                    equal_to(self.original_observed_x + diff_x))
        assert_that(self.undertest.observed_y,
                    equal_to(self.original_observed_y + diff_y))

        assert_that(self.undertest.original_observed_x,
                    equal_to(self.original_observed_x))
        assert_that(self.undertest.original_observed_y,
                    equal_to(self.original_observed_y))

    def test_reset_location(self):
        original_pixel_x, original_pixel_y = self.undertest.pixel_source_point

        diff_x = 2.5
        diff_y = 3.5
        new_pixel_x = self.undertest.pixel_x + diff_x
        new_pixel_y = self.undertest.pixel_y + diff_y

        self.undertest.update_pixel_location((new_pixel_x, new_pixel_y))

        assert_that(self.undertest.observed_x,
                    equal_to(self.original_observed_x + diff_x))
        assert_that(self.undertest.observed_y,
                    equal_to(self.original_observed_y + diff_y))

        assert_that(self.undertest.pixel_x, equal_to(new_pixel_x))
        assert_that(self.undertest.pixel_y, equal_to(new_pixel_y))

        self.undertest.reset_source_location()

        assert_that(self.undertest.observed_x,
                    equal_to(self.original_observed_x))
        assert_that(self.undertest.observed_y,
                    equal_to(self.original_observed_y))

        assert_that(self.undertest.pixel_x, equal_to(original_pixel_x))
        assert_that(self.undertest.pixel_y, equal_to(original_pixel_y))

    def test_reset_location_is_adjusted_false(self):
        assert_that(self.undertest.is_adjusted(), equal_to(False))

        self.undertest.update_pixel_location((100, 100))

        assert_that(self.undertest.is_adjusted(), equal_to(True))

        self.undertest.reset_source_location()

        assert_that(self.undertest.is_adjusted(), equal_to(False))

    def test_reset_location_makes_data_stale(self):
        self.assert_update_ra_dec_call_count_equals(0)

        self.undertest.update_pixel_location((665.0, 3215.0))

        _ = self.undertest.ra

        self.assert_update_ra_dec_call_count_equals(1)

        self.undertest.reset_source_location()

        self.assert_update_ra_dec_call_count_equals(1)

        _ = self.undertest.ra

        self.assert_update_ra_dec_call_count_equals(2)