Esempio n. 1
0
    def run(self, debug=False, show=False, maskslits=None):
        """
        Generate normalized pixel and illumination flats

        Code flow::
           1.  Generate the pixelflat image (if necessary)
           2.  Prepare b-spline knot spacing
           3.  Loop on slits/orders
               a. Calculate the slit profile
               b. Normalize
               c. Save

        Args:
            debug (:obj:`bool`, optional):
                Run in debug mode.
            show (:obj:`bool`, optional):
                Show the results in the ginga viewer.
            maskslits (np.ndarray, optional):
               Array specifying whether a slit is good.
               True = bad

        Returns:
            `numpy.ndarray`_: Two arrays are returned, the normalized
            pixel flat data and the slit illumination correction data.
        """
        # Mask
        if maskslits is None:
            maskslits = np.zeros(self.nslits, dtype=bool)

        # Build the pixel flat (as needed)
        self.build_pixflat()

        # Prep tck (sets self.ntckx, self.ntcky)
        #self._prep_tck()

        # Setup
        self.mspixelflat = np.ones_like(self.rawflatimg.image)
        self.msillumflat = np.ones_like(self.rawflatimg.image)
        self.flat_model = np.zeros_like(self.rawflatimg.image)
        self.slitmask = pixels.tslits2mask(self.tslits_dict)

        final_tilts = np.zeros_like(self.rawflatimg.image)

        # If we are tweaking slits allocate the new aray to hold tweaked slit boundaries
        if self.flatpar['tweak_slits']:
            self.tslits_dict['slit_left_tweak'] = np.zeros_like(
                self.tslits_dict['slit_left'])
            self.tslits_dict['slit_righ_tweak'] = np.zeros_like(
                self.tslits_dict['slit_righ'])

        # Loop on slits
        for slit in range(self.nslits):
            # Is this a good slit??
            if maskslits[slit]:
                msgs.info('Skipping bad slit: {}'.format(slit))
                continue
            #
            msgs.info('Computing flat field image for slit: {:d}/{:d}'.format(
                slit, self.nslits - 1))
            if self.msbpm is not None:
                inmask = np.invert(self.msbpm)
            else:
                inmask = np.ones_like(self.rawflatimg.image, dtype=bool)

            # Fit flats for a single slit
            this_tilts_dict = {
                'tilts': self.tilts_dict['tilts'],
                'coeffs': self.tilts_dict['coeffs'][:, :, slit].copy(),
                'slitcen': self.tilts_dict['slitcen'][:, slit].copy(),
                'func2d': self.tilts_dict['func2d']
            }
            nonlinear_counts = self.spectrograph.nonlinear_counts(det=self.det)

            pixelflat, illumflat, flat_model, tilts_out, thismask_out, slit_left_out, \
                    slit_righ_out \
                            = flat.fit_flat(self.rawflatimg.image, this_tilts_dict, self.tslits_dict,
                                           slit, inmask=inmask, nonlinear_counts=nonlinear_counts,
                                           spec_samp_fine=self.flatpar['spec_samp_fine'],
                                           spec_samp_coarse=self.flatpar['spec_samp_coarse'],
                                           spat_samp=self.flatpar['spat_samp'],
                                           tweak_slits=self.flatpar['tweak_slits'],
                                           tweak_slits_thresh=self.flatpar['tweak_slits_thresh'],
                                           tweak_slits_maxfrac=self.flatpar['tweak_slits_maxfrac'],
                                           debug=debug)

            self.mspixelflat[thismask_out] = pixelflat[thismask_out]
            self.msillumflat[thismask_out] = illumflat[thismask_out]
            self.flat_model[thismask_out] = flat_model[thismask_out]

            # Did we tweak slit boundaries? If so, update the tslits_dict and the tilts_dict
            if self.flatpar['tweak_slits']:
                self.tslits_dict['slit_left'][:, slit] = slit_left_out
                self.tslits_dict['slit_righ'][:, slit] = slit_righ_out
                self.tslits_dict['slit_left_tweak'][:, slit] = slit_left_out
                self.tslits_dict['slit_righ_tweak'][:, slit] = slit_righ_out
                final_tilts[thismask_out] = tilts_out[thismask_out]

        # If we tweaked the slits update the tilts_dict
        if self.flatpar['tweak_slits']:
            self.tilts_dict['tilts'] = final_tilts

        if show:
            # Global skysub is the first step in a new extraction so clear the channels here
            self.show(slits=True, wcs_match=True)

        # If illumination flat fielding is turned off, set the illumflat to be None.
        if not self.flatpar['illumflatten']:
            msgs.warn(
                'No illumination flat will be applied to your data (illumflatten=False).'
            )
            self.msillumflat = None

        # Return
        return self.mspixelflat, self.msillumflat
Esempio n. 2
0
    def run(self, debug=False, show=False):
        """
        Main driver to generate normalized flat field and illumination flats

        Code flow --
           1.  Generate the pixelflat image (if necessary)
           2.  Prepare b-spline knot spacing
           3.  Loop on slits/orders
               a. Calculate the slit profile
               b. Normalize
               c. Save

        Args:
            debug (bool, optional):
            show (bool, optional):

        Returns:
            ndarray, ndarray: self.mspixelflatnrm and self.slit_profiles

        """

        # Build the pixel flat (as needed)
        if self.rawflatimg is None:
            self.rawflatimg = self.build_pixflat()

        # Prep tck (sets self.ntckx, self.ntcky)
        #self._prep_tck()

        # Setup
        self.mspixelflat = np.ones_like(self.rawflatimg)
        self.msillumflat = np.ones_like(self.rawflatimg)
        self.flat_model = np.zeros_like(self.rawflatimg)
        self.slitmask = pixels.tslits2mask(self.tslits_dict)

        final_tilts = np.zeros_like(self.rawflatimg)

        # If we are tweaking slits allocate the new aray to hold tweaked slit boundaries
        if self.flatpar['tweak_slits']:
            self.tslits_dict['slit_left_tweak'] = np.zeros_like(
                self.tslits_dict['slit_left'])
            self.tslits_dict['slit_righ_tweak'] = np.zeros_like(
                self.tslits_dict['slit_righ'])

        # Loop on slits
        for slit in range(self.nslits):
            msgs.info('Computing flat field image for slit: {:d}/{:d}'.format(
                slit, self.nslits - 1))
            if self.msbpm is not None:
                inmask = np.invert(self.msbpm)
            else:
                inmask = np.ones_like(self.rawflatimg, dtype=bool)

            # Fit flats for a single slit
            this_tilts_dict = {
                'tilts': self.tilts_dict['tilts'],
                'coeffs': self.tilts_dict['coeffs'][:, :, slit].copy(),
                'slitcen': self.tilts_dict['slitcen'][:, slit].copy(),
                'func2d': self.tilts_dict['func2d']
            }
            nonlinear_counts = self.spectrograph.detector[self.det - 1]['nonlinear']*\
                               self.spectrograph.detector[self.det - 1]['saturation']
            pixelflat, illumflat, flat_model, tilts_out, thismask_out, slit_left_out, slit_righ_out = \
                flat.fit_flat(self.rawflatimg, this_tilts_dict, self.tslits_dict, slit,
                              inmask=inmask,
                              nonlinear_counts=nonlinear_counts,
                              spec_samp_fine=self.flatpar['spec_samp_fine'], spec_samp_coarse=self.flatpar['spec_samp_coarse'],
                              spat_samp=self.flatpar['spat_samp'], tweak_slits=self.flatpar['tweak_slits'],
                              tweak_slits_thresh=self.flatpar['tweak_slits_thresh'],
                              tweak_slits_maxfrac=self.flatpar['tweak_slits_maxfrac'],debug=debug)
            self.mspixelflat[thismask_out] = pixelflat[thismask_out]
            self.msillumflat[thismask_out] = illumflat[thismask_out]
            self.flat_model[thismask_out] = flat_model[thismask_out]
            # Did we tweak slit boundaries? If so, update the tslits_dict and the tilts_dict
            if self.flatpar['tweak_slits']:
                self.tslits_dict['slit_left'][:, slit] = slit_left_out
                self.tslits_dict['slit_righ'][:, slit] = slit_righ_out
                self.tslits_dict['slit_left_tweak'][:, slit] = slit_left_out
                self.tslits_dict['slit_righ_tweak'][:, slit] = slit_righ_out
                final_tilts[thismask_out] = tilts_out[thismask_out]

        # If we tweaked the slits update the tilts_dict
        if self.flatpar['tweak_slits']:
            self.tilts_dict['tilts'] = final_tilts

        if show:
            # Global skysub is the first step in a new extraction so clear the channels here
            self.show(slits=True, wcs_match=True)

        # If illumination flat fielding is turned off, set the illumflat to be None.
        if not self.flatpar['illumflatten']:
            msgs.warn(
                'You have set illumflatten=False. No illumination flat will be applied to your data.'
            )
            self.msillumflat = None

        # Return
        return self.mspixelflat, self.msillumflat
Esempio n. 3
0
flat_model = np.zeros_like(flatimg)

debug = True
# Loop on slits
for slit in gdslits:
    msgs.info("Computing flat field image for slit: {:d}".format(slit + 1))
    slit_left = tslits_dict['lcen'][:, slit]
    slit_righ = tslits_dict['rcen'][:, slit]
    thismask = (tslits_dict['slitpix'] == slit + 1)
    inmask = None  # in the future set this to the bpm
    sys.exit(-1)
    pixelflat[thismask], illumflat[thismask], flat_model[
        thismask] = flat.fit_flat(flatimg,
                                  mstilts,
                                  thismask,
                                  slit_left,
                                  slit_righ,
                                  inmask=inmask,
                                  debug=debug)

ginga.show_image(pixelflat,
                 cuts=(0.9, 1.1),
                 chname='pixeflat',
                 wcs_match=True,
                 clear=True)
ginga.show_image(illumflat,
                 cuts=(0.9, 1.1),
                 chname='illumflat',
                 wcs_match=True)
ginga.show_image(flatimg, chname='flat', wcs_match=True)
ginga.show_image(flat_model, chname='flat_model', wcs_match=True)
Esempio n. 4
0
pixelflat = np.ones_like(flatimg)
illumflat = np.ones_like(flatimg)
flat_model = np.zeros_like(flatimg)

debug = True
# Loop on slits
for slit in gdslits:
    msgs.info("Computing flat field image for slit: {:d}".format(slit + 1))
    slit_left = tslits_dict['lcen'][:, slit]
    slit_righ = tslits_dict['rcen'][:, slit]
    thismask = (tslits_dict['slitpix'] == slit + 1)
    inmask = None # in the future set this to the bpm
    sys.exit(-1)
    pixelflat[thismask], illumflat[thismask], flat_model[thismask] = flat.fit_flat(flatimg, mstilts, thismask,
                                                                                   slit_left, slit_righ,inmask=inmask,
                                                                                   debug = debug)


ginga.show_image(pixelflat,cuts = (0.9,1.1),chname='pixeflat', wcs_match=True, clear=True)
ginga.show_image(illumflat,cuts = (0.9,1.1), chname='illumflat', wcs_match=True)
ginga.show_image(flatimg,chname='flat', wcs_match=True)
ginga.show_image(flat_model,chname='flat_model',wcs_match = True)

'''
    bkspace = 1.0/nsamp # This is the spatial sampling interval in units of fractional slit width

    fit_spat = thismask & inmask
    isrt_spat = np.argsort(ximg[fit_spat])
    ximg_fit = ximg[fit_spat][isrt_spat]
    norm_spec_fit = norm_spec[fit_spat][isrt_spat]