Ejemplo n.º 1
0
    def global_skysub(self, sciimg, sciivar, tilts, std=False, skymask=None, update_crmask=True, maskslits=None, show_fit=False,
                      show=False, show_objs=False):
        """
        Perform global sky subtraction, slit by slit

        Wrapper to skysub.global_skysub

        Parameters
        ----------
        tslits_dict: dict
           Dictionary containing information on the slits traced for this image

        Optional Parameters
        -------------------
        bspline_spaceing: (float):
           Break-point spacing for bspline

        use_skymask: (bool, optional):
           Mask objects using self.skymask if object finding has been run
           (This requires they were found previously, i.e. that find_objects was already run)

        Returns:
            global_sky: (numpy.ndarray) image of the the global sky model
        """


        # Prep
        self.global_sky = np.zeros_like(sciimg)
        if (std and not self.redux_par['global_sky_std']):
            msgs.info('Skipping global sky-subtraction for standard star.')
            return self.global_sky

        if std:
            sigrej = 7.0
            update_crmask = False
        else:
            sigrej = 3.0

        self.sciimg = sciimg
        self.sciivar = sciivar
        self.tilts = tilts
        self.maskslits = self.maskslits if maskslits is None else maskslits
        gdslits = np.where(np.invert(self.maskslits))[0]


        # Mask objects using the skymask? If skymask has been set by objfinding, and masking is requested, then do so
        skymask_now = skymask if (skymask is not None) else np.ones_like(self.sciimg, dtype=bool)
        # Loop on slits
        for slit in gdslits:
            msgs.info("Global sky subtraction for slit: {:d}".format(slit))
            thismask = (self.slitmask == slit)
            inmask = (self.mask == 0) & thismask & skymask_now
            # Find sky
            self.global_sky[thismask] = skysub.global_skysub(self.sciimg, self.sciivar,
                                                             self.tilts, thismask,
                                                             self.tslits_dict['slit_left'][:,slit],
                                                             self.tslits_dict['slit_righ'][:,slit],
                                                             inmask=inmask,
                                                             sigrej=sigrej,
                                                             bsp=self.redux_par['bspline_spacing'],
                                                             no_poly=self.redux_par['no_poly'],
                                                             pos_mask = (not self.ir_redux),
                                                             show_fit=show_fit)
            # Mask if something went wrong
            if np.sum(self.global_sky[thismask]) == 0.:
                self.maskslits[slit] = True

        if update_crmask:
            self.crmask = processimages.ProcessImages.build_crmask(self.sciimg - self.global_sky, self.proc_par,
                                                                   self.det, self.spectrograph, ivar = self.sciivar,
                                                                   binning=self.binning)
            # Rebuild the mask with this new crmask
            self.mask = processimages.ProcessImages.update_mask_cr(self.mask, self.crmask)

        # Step
        self.steps.append(inspect.stack()[0][3])

        if show:
            sobjs_show = None if show_objs else self.sobjs_obj
            # Global skysub is the first step in a new extraction so clear the channels here
            self.show('global', slits=True, sobjs =sobjs_show, clear=False)

        # Return
        return self.global_sky
Ejemplo n.º 2
0
    def global_skysub(self,
                      skymask=None,
                      update_crmask=True,
                      show_fit=False,
                      show=False,
                      show_objs=False):
        """
        Perform global sky subtraction, slit by slit

        Wrapper to skysub.global_skysub

        Args:
            skymask (np.ndarray):
            update_crmask (bool, optional):
            show_fit (bool, optional):
            show (bool, optional):
            show_objs (bool, optional):

        Returns:
            numpy.ndarray: image of the the global sky model

        """
        # Prep
        self.global_sky = np.zeros_like(self.sciImg.image)
        # Parameters for a standard star
        if self.std_redux:
            sigrej = 7.0
            update_crmask = False
            if not self.par['reduce']['skysub']['global_sky_std']:
                msgs.info('Skipping global sky-subtraction for standard star.')
                return self.global_sky
        else:
            sigrej = 3.0

        gdslits = np.where(np.invert(self.reduce_bpm))[0]

        # Mask objects using the skymask? If skymask has been set by objfinding, and masking is requested, then do so
        skymask_now = skymask if (skymask is not None) else np.ones_like(
            self.sciImg.image, dtype=bool)

        # Loop on slits
        for slit_idx in gdslits:
            slit_spat = self.slits.spat_id[slit_idx]
            msgs.info(
                "Global sky subtraction for slit: {:d}".format(slit_spat))
            thismask = self.slitmask == slit_spat
            inmask = (self.sciImg.fullmask == 0) & thismask & skymask_now
            # Find sky
            self.global_sky[thismask] \
                    = skysub.global_skysub(self.sciImg.image, self.sciImg.ivar, self.tilts,
                                           thismask, self.slits_left[:,slit_idx],
                                           self.slits_right[:,slit_idx],
                                           inmask=inmask, sigrej=sigrej,
                                           bsp=self.par['reduce']['skysub']['bspline_spacing'],
                                           no_poly=self.par['reduce']['skysub']['no_poly'],
                                           pos_mask=(not self.ir_redux), show_fit=show_fit)
            # Mask if something went wrong
            if np.sum(self.global_sky[thismask]) == 0.:
                self.reduce_bpm[slit_idx] = True

        if update_crmask:
            # Find CRs with sky subtraction
            self.sciImg.build_crmask(self.par['scienceframe']['process'],
                                     subtract_img=self.global_sky)
            # Update the fullmask
            self.sciImg.update_mask_cr(self.sciImg.crmask)

        # Step
        self.steps.append(inspect.stack()[0][3])

        if show:
            sobjs_show = None if show_objs else self.sobjs_obj
            # Global skysub is the first step in a new extraction so clear the channels here
            self.show('global', slits=True, sobjs=sobjs_show, clear=False)

        # Return
        return self.global_sky
 rcen = tslits_dict['rcen']
 ximg = tslits_dict['ximg']
 edgmask = tslits_dict['edge_mask']
 FWHM = 5.0
 bsp = 0.8
 residual_img = np.zeros_like(sciframe_A)
 specobjs_pos = specobjs.SpecObjs()
 specobjs_neg = specobjs.SpecObjs()
 skymask = (slitpix > 0)
 for islit in range(1, nslits + 1):
     thismask = (slitpix == islit)
     residual_img[thismask] = skysub.global_skysub(diff_AB,
                                                   ivar_AB,
                                                   mstilts,
                                                   thismask,
                                                   lcen[:,islit-1],
                                                   rcen[:,islit-1],
                                                   inmask=((edgmask == False) & (mask_AB == True)),
                                                   bsp=bsp,
                                                   pos_mask=False,
                                                   show_fit=False)
     image = diff_AB - residual_img
     # Extract negative trace
     specobj_slit_neg, skymask_neg, objmask_neg = extract.objfind(-image,
                                                                  #ivar_AB,
                                                                  thismask,
                                                                  lcen[:,islit-1],
                                                                  rcen[:,islit-1],
                                                                  sig_thresh=3.0,
                                                                  inmask=mask_AB,
                                                                  FWHM=FWHM,
                                                                  nperslit=1,