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 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)