def plot_postage_stamps(self, sourceId, size=10):
     ra, dec = self.get_catsim_coords((sourceId,))
     ra, dec = ra[0], dec[0]
     l2_objects = self.SNs.find_objects(ra, dec, size/2.)
     phosim_objects = self.SNs.find_phosim_objects(ra, dec, size/2.)
     snra, sndec = self.get_catsim_coords(phosim_objects['sourceId'].tolist())
     fig = plt.figure()
     for i, band in enumerate('ugrizy'):
         expfile = os.path.join(self.repo, 'deepCoadd', band, '0/0,0.fits')
         maker = PostageStampMaker(expfile)
         stamp = maker.create(ra, dec, size)
         hdu = convert_image_to_hdu(stamp)
         title = 'band: %(band)s' % locals()
         subplot = '23%i' % (i + 1)
         fig, axes, im, norm = render_fits_image(hdu, title=title, fig=fig,
                                                 subplot=subplot)
         axis_range = plt.axis()
         axes.scatter(l2_objects['RA'].tolist(),
                      l2_objects['Dec'].tolist(),
                      transform=axes.get_transform('icrs'), color='red',
                      alpha=0.5)
         axes.scatter(snra, sndec,
                      transform=axes.get_transform('icrs'),
                      color='green', alpha=0.5, marker=(5, 2), s=100)
         axes.scatter([ra], [dec],
                      transform=axes.get_transform('icrs'),
                      color='blue', alpha=0.5, marker=(5, 2), s=100)
         plt.axis(axis_range)
     fig.suptitle('sourceId: %(sourceId)i' % locals())
     return fig
 def get_pixel_data(self, objectId, band, size=10, pickle_file=None):
     """
     Use PostageStampMaker to extract the pixel data cutout for the
     specified objectId andn band for each visit.
     """
     if pickle_file is None:
         pickle_file = 'pixel_data_%(objectId)i_%(band)s.pkl' % locals()
     if os.path.isfile(pickle_file):
         with open(pickle_file, 'r') as input_:
             pixel_data = pickle.load(input_)
         return pixel_data, pickle_file
     pixel_data = OrderedDict()
     visits = self.get_visits(band)
     ra, dec = self.get_coords(objectId)
     for visit in visits:
         print("working on visit", visit)
         sys.stdout.flush()
         exp_file = os.path.join(repo, 'deepCoadd', band, '0/0,0tempExp',
                                 'v%(visit)i-f%(band)s.fits' % locals())
         maker = PostageStampMaker(exp_file)
         stamp = maker.create(ra, dec, size)
         pixel_data[visit] = \
             copy.deepcopy(stamp.getMaskedImage().getImage().getArray())
     with open(pickle_file, 'w') as output:
         pickle.dump(pixel_data, output)
     return pixel_data, pickle_file
    def _display_figure(self, l2_service, size, figsize, scaling_factor):
        """
        Display the image and light curve.
        """
        objectId = self.objectId
        band = self.band

        # Create the coadd postage stamp and use as the initial image.
        coadd = PostageStampMaker(os.path.join(repo, 'deepCoadd', band,
                                               '0/0,0.fits'))
        ra, dec = l2_service.get_coords(objectId)

        # Use the coadd to set the image normalization
        stamp = coadd.create(ra, dec, size)
        hdu = convert_image_to_hdu(stamp)
        norm = image_norm(hdu.data*scaling_factor)

        plt.rcParams['figure.figsize'] = figsize
        self.fig = plt.figure()
        self.fig.suptitle('objectId: %(objectId)i\n%(band)s band' % locals())
        self.image = render_fits_image(hdu, norm=norm, fig=self.fig,
                                       subplot=211)[2]
        self.fig.add_subplot(212)
        self.light_curve = l2_service.get_light_curve(objectId, band)
        plt.errorbar(self.light_curve['mjd'], self.light_curve['flux'],
                     yerr=self.light_curve['fluxerr'], fmt='.')
        self.yrange = plt.axis()[2:]
        self.current_point = plt.plot([self.light_curve['mjd'][0]],
                                      [self.light_curve['flux'][0]],
                                      marker='o', color='red')
        self.current_point.extend(plt.plot([self.light_curve['mjd'][0],
                                            self.light_curve['mjd'][0]],
                                           self.yrange, 'k:'))
        plt.xlabel('MJD')
        plt.ylabel('flux (nmgy)')
 def setUp(self):
     """
     Set up each test with a new PostageStampMaker object using the
     test FITS file, coordinates of the image center, and a postage
     stamp size of 10 arcsec.
     """
     self.expfile = os.path.join(lsst.utils.getPackageDir('monitor'),
                                 'tests', 'small_CoaddTempExp.fits.gz')
     self.stamp_maker = PostageStampMaker(self.expfile)
     center_coord = self.stamp_maker.center_coord(self.stamp_maker.exposure)
     self.ra = center_coord.getLongitude().asDegrees()
     self.dec = center_coord.getLatitude().asDegrees()
     self.size = 10
class PostageStampTestCase(unittest.TestCase):
    "TestCase class for PostageStampMaker module."
    def setUp(self):
        """
        Set up each test with a new PostageStampMaker object using the
        test FITS file, coordinates of the image center, and a postage
        stamp size of 10 arcsec.
        """
        self.expfile = os.path.join(lsst.utils.getPackageDir('monitor'),
                                    'tests', 'small_CoaddTempExp.fits.gz')
        self.stamp_maker = PostageStampMaker(self.expfile)
        center_coord = self.stamp_maker.center_coord(self.stamp_maker.exposure)
        self.ra = center_coord.getLongitude().asDegrees()
        self.dec = center_coord.getLatitude().asDegrees()
        self.size = 10

    def tearDown(self):
        "Clean up"
        del self.stamp_maker

    def test_bbox_generation(self):
        "Test the make bounding box function."
        bbox = self.stamp_maker.make_bbox(self.ra, self.dec, self.size)
        self.assertEqual((bbox.getWidth(), bbox.getHeight()), (30, 30))

    def test_stamp_size(self):
        "Test that the postage stamp has the expected size."
        my_exposure = self.stamp_maker.create(self.ra, self.dec, self.size)
        my_imarr = my_exposure.getMaskedImage().getImage().getArray()
        self.assertEqual(my_imarr.shape, (30, 30))

    def test_stamp_central_pixel_value(self):
        """
        Test that the center pixel of both the postage stamp and original
        image have the same value.
        """
        my_exposure = self.stamp_maker.create(self.ra, self.dec, self.size)
        my_imarr = my_exposure.getMaskedImage().getImage().getArray()
        ref_exposure = self.stamp_maker.exposure
        ref_imarr = ref_exposure.getMaskedImage().getImage().getArray()
        self.assertEqual(my_imarr[int(my_imarr.shape[0]/2)][int(my_imarr.shape[1]/2)],
                         ref_imarr[int(ref_imarr.shape[0]/2)][int(ref_imarr.shape[1]/2)])

    def test_stamp_centers_match(self):
        """
        Test that coordinates of the centers of the postage stamp and
        the original image are the same.
        """
        my_exposure = self.stamp_maker.create(self.ra, self.dec, self.size)
        self.assertEqual(self.stamp_maker.center_coord(my_exposure),
                         self.stamp_maker.center_coord(self.stamp_maker.exposure))

    def test_create_array_of_stamps(self):
        """
        Test that creates a sequence of stamps from the Exposure object
        given a sequence of (ra, dec, size) tuples and tests that the
        centers of the stamps are at the expected locations.
        """
        pix_scale = self.stamp_maker.exposure.getWcs().pixelScale().asDegrees()
        npix = 10
        stamp_specs = [(self.ra, self.dec - npix*pix_scale, self.size),
                       (self.ra, self.dec, self.size),
                       (self.ra, self.dec + npix*pix_scale, self.size)]
        my_stamps = self.stamp_maker.create_stamps(stamp_specs)
        self.assertEqual(len(my_stamps), len(stamp_specs))
        for i, stamp in enumerate(my_stamps):
            coord = self.stamp_maker.center_coord(stamp)
            self.assertAlmostEqual(coord.getLongitude().asDegrees(),
                                   stamp_specs[i][0])
            self.assertAlmostEqual(coord.getLatitude().asDegrees(),
                                   stamp_specs[i][1])

    def test_create_sequence_function(self):
        """
        For a given (ra, dec, size), test the create_postage_stamps
        function which returns a sequence of stamps for that sky
        region, extracted from a sequence of input Exposure FITS
        files.
        """
        fits_files = [self.expfile]*3
        my_stamps = create_postage_stamps(self.ra, self.dec, self.size,
                                          fits_files)
        self.assertEqual(len(my_stamps), len(fits_files))
        for stamp in my_stamps:
            self.assertTrue(isinstance(stamp, afwImage.ExposureF))

    def test_hdu_from_exposure(self):
        """
        Test the conversion of the image from Exposure object directly
        to an HDU.
        """
        my_stamp = self.stamp_maker.create(self.ra, self.dec, self.size)
        my_hdu = convert_image_to_hdu(my_stamp)

        # Write the Exposure to a FITS file to test the image data and
        # WCS keywords in the in-memory HDU against the versions
        # written by the Stack.
        stamp_file = 'temp_stamp.fits'
        my_stamp.writeFits(stamp_file)
        stack_hdu = fits.open(stamp_file)[1]

        for value1, value2 in zip(my_hdu.data.flatten(),
                                  stack_hdu.data.flatten()):
            self.assertEqual(value1, value2)
        for keyword in my_hdu.header.keys():
            self.assertEqual(my_hdu.header[keyword], stack_hdu.header[keyword])
        os.remove(stamp_file)
class PostageStampTestCase(unittest.TestCase):
    "TestCase class for PostageStampMaker module."
    def setUp(self):
        """
        Set up each test with a new PostageStampMaker object using the
        test FITS file, coordinates of the image center, and a postage
        stamp size of 10 arcsec.
        """
        self.expfile = os.path.join(os.environ['MONITOR_DIR'],
                                    'tests', 'small_CoaddTempExp.fits.gz')
        self.stamp_maker = PostageStampMaker(self.expfile)
        center_coord = self.stamp_maker.center_coord(self.stamp_maker.exposure)
        self.ra = center_coord.getLongitude().asDegrees()
        self.dec = center_coord.getLatitude().asDegrees()
        self.size = 10

    def test_bbox_generation(self):
        "Test the make bounding box function."
        bbox = self.stamp_maker.makeBBox(self.ra, self.dec, self.size)
        self.assertEqual((bbox.getWidth(), bbox.getHeight()), (30, 30))

    def test_stamp_size(self):
        "Test that the postage stamp has the expected size."
        my_exposure = self.stamp_maker.create(self.ra, self.dec, self.size)
        my_imarr = my_exposure.getMaskedImage().getImage().getArray()
        self.assertEqual(my_imarr.shape, (30, 30))

    def test_stamp_central_pixel_value(self):
        """
        Test that the center pixel of both the postage stamp and original
        image have the same value.
        """
        my_exposure = self.stamp_maker.create(self.ra, self.dec, self.size)
        my_imarr = my_exposure.getMaskedImage().getImage().getArray()
        ref_exposure = self.stamp_maker.exposure
        ref_imarr = ref_exposure.getMaskedImage().getImage().getArray()
        self.assertEqual(my_imarr[int(my_imarr.shape[0]/2)][int(my_imarr.shape[1]/2)],
                         ref_imarr[int(ref_imarr.shape[0]/2)][int(ref_imarr.shape[1]/2)])

    def test_stamp_centers_match(self):
        """
        Test that coordinates of the centers of the postage stamp and
        the original image are the same.
        """
        my_exposure = self.stamp_maker.create(self.ra, self.dec, self.size)
        self.assertEqual(self.stamp_maker.center_coord(my_exposure),
                         self.stamp_maker.center_coord(self.stamp_maker.exposure))

    def test_create_array_of_stamps(self):
        """
        Test that creates a sequence of stamps from the Exposure object
        given a sequence of (ra, dec, size) tuples and tests that the
        centers of the stamps are at the expected locations.
        """
        pix_scale = self.stamp_maker.exposure.getWcs().pixelScale().asDegrees()
        npix = 10
        stamp_specs = [(self.ra, self.dec - npix*pix_scale, self.size),
                       (self.ra, self.dec, self.size),
                       (self.ra, self.dec + npix*pix_scale, self.size)]
        my_stamps = self.stamp_maker.create_stamps(stamp_specs)
        self.assertEqual(len(my_stamps), len(stamp_specs))
        for i, stamp in enumerate(my_stamps):
            coord = self.stamp_maker.center_coord(stamp)
            self.assertAlmostEqual(coord.getLongitude().asDegrees(),
                                   stamp_specs[i][0])
            self.assertAlmostEqual(coord.getLatitude().asDegrees(),
                                   stamp_specs[i][1])

    def test_create_sequence_function(self):
        """
        For a given (ra, dec, size), test the create_postage_stamps
        function which returns a sequence of stamps for that sky
        region, extracted from a sequence of input Exposure FITS
        files.
        """
        fits_files = [self.expfile]*3
        my_stamps = create_postage_stamps(self.ra, self.dec, self.size,
                                          fits_files)
        self.assertEqual(len(my_stamps), len(fits_files))
        for stamp in my_stamps:
            self.assertTrue(isinstance(stamp, afwImage.ExposureF))