Exemplo n.º 1
0
    def main(args):

        import os

        from pypeit.core.gui.skysub_regions import SkySubGUI
        from pypeit.core import flexure
        from pypeit.scripts import utils
        from pypeit import masterframe
        from pypeit.images import buildimage

        # Generate a utilities class
        info = utils.Utilities(None, pypeit_file=args.file, det=args.det)

        # Interactively select a science frame
        sciIdx = info.select_science_frame(standard=args.standard)

        # Load the spectrograph and parset
        info.load_spectrograph_parset(sciIdx)

        # Get the master key and directory
        mdir, mkey = info.get_master_dirkey()

        # Load the image data
        frame = info.load_frame(sciIdx)

        # Load the slits information
        slits = utils.get_slits(mkey, mdir)
        spat_flexure = None
        if args.flexure:
            spat_flexure = flexure.spat_flexure_shift(frame, slits)

        # Derive an appropriate output filename
        file_base = info.get_basename(sciIdx)
        prefix = os.path.splitext(file_base)
        if prefix[1] == ".gz":
            outname = os.path.splitext(prefix[0])[0]
        else:
            outname = prefix[0]
        ext = buildimage.SkyRegions.master_file_format
        regfile = masterframe.construct_file_name(buildimage.SkyRegions, master_key=mkey,
                                                  master_dir=mdir)
        regfile = regfile.replace(".{0:s}".format(ext), "_{0:s}.{1:s}".format(outname, ext))
        #outname = "{0:s}/MasterSkyRegions_{1:s}_{2:s}.fits.gz".format(mdir, mkey, outname)

        # Finally, initialise the GUI
        skyreg = SkySubGUI.initialize(args.det, frame, slits, info.spectrograph.pypeline,
                                      info.spectrograph.name, outname=regfile,
                                      overwrite=args.overwrite, runtime=False, printout=True,
                                      initial=args.initial, flexure=spat_flexure)

        # Get the results
        skyreg.get_result()
Exemplo n.º 2
0
def main(args):

    # Generate a utilities class
    info = utils.Utilities(args.file, args.det)

    # Interactively select a science frame
    sciIdx = info.select_science_frame()

    # Load the spectrograph and parset
    info.load_spectrograph_parset(sciIdx)

    # Get the master key and directory
    mdir, mkey = info.get_master_dirkey()

    # Load the image data
    frame = info.load_frame(sciIdx)

    # Load the slits information
    slits = utils.get_slits(mkey, mdir)
    spat_flexure = None
    if args.flexure:
        spat_flexure = flexure.spat_flexure_shift(frame, slits)

    # Derive an appropriate output filename
    file_base = info.get_basename(sciIdx)
    prefix = os.path.splitext(file_base)
    if prefix[1] == ".gz":
        outname = os.path.splitext(prefix[0])[0]
    else:
        outname = prefix[0]
    outname = "{0:s}/MasterSkyRegions_{1:s}_{2:s}.fits.gz".format(
        mdir, mkey, outname)

    # Finally, initialise the GUI
    skyreg = SkySubGUI.initialize(args.det,
                                  frame,
                                  slits,
                                  info.spectrograph.pypeline,
                                  outname=outname,
                                  overwrite=args.overwrite,
                                  runtime=False,
                                  printout=True,
                                  initial=args.initial,
                                  flexure=spat_flexure)

    # Get the results
    skyreg.get_result()
Exemplo n.º 3
0
    def process(self,
                par,
                bpm=bpm,
                flatimages=None,
                bias=None,
                slits=None,
                debug=False,
                dark=None):
        """
        Process the image

        Note:  The processing step order is currently 'frozen' as is.
          We may choose to allow optional ordering

        Here are the allowed steps, in the order they will be applied:
            subtract_overscan -- Analyze the overscan region and subtract from the image
            trim -- Trim the image down to the data (i.e. remove the overscan)
            orient -- Orient the image in the PypeIt orientation (spec, spat) with blue to red going down to up
            subtract_bias -- Subtract a bias image
            apply_gain -- Convert to counts, amp by amp
            flatten -- Divide by the pixel flat and (if provided) the illumination flat
            extras -- Generate the RN2 and IVAR images
            crmask -- Generate a CR mask

        Args:
            par (:class:`pypeit.par.pypeitpar.ProcessImagesPar`):
                Parameters that dictate the processing of the images.  See
                :class:`pypeit.par.pypeitpar.ProcessImagesPar` for the
                defaults.
            bpm (`numpy.ndarray`_, optional):
            flatimages (:class:`pypeit.flatfield.FlatImages`):
            bias (`numpy.ndarray`_, optional):
                Bias image
            slits (:class:`pypeit.slittrace.SlitTraceSet`, optional):
                Used to calculate spatial flexure between the image and the slits

        Returns:
            :class:`pypeit.images.pypeitimage.PypeItImage`:

        """
        self.par = par
        self._bpm = bpm

        # Get started
        # Standard order
        #   -- May need to allow for other order some day..
        if par['use_pattern']:  # Note, this step *must* be done before use_overscan
            self.subtract_pattern()
        if par['use_overscan']:
            self.subtract_overscan()
        if par['trim']:
            self.trim()
        if par['orient']:
            self.orient()
        if par['use_biasimage']:  # Bias frame, if it exists, is *not* trimmed nor oriented
            self.subtract_bias(bias)
        if par['use_darkimage']:  # Dark frame, if it exists, is TODO:: check: trimmed, oriented (and oscan/bias subtracted?)
            self.subtract_dark(dark)
        if par['apply_gain']:
            self.apply_gain()

        # This needs to come after trim, orient
        # Calculate flexure -- May not be used, but always calculated when slits are provided
        if slits is not None and self.par['spat_flexure_correct']:
            self.spat_flexure_shift = flexure.spat_flexure_shift(
                self.image, slits)

        # Generate the illumination flat, as needed
        illum_flat = None
        if self.par['use_illumflat']:
            if flatimages is None:
                msgs.error(
                    "Cannot illumflatten, no such image generated. Add one or more illumflat images to your PypeIt file!!"
                )
            if slits is None:
                msgs.error("Need to provide slits to create illumination flat")
            illum_flat = flatimages.fit2illumflat(
                slits, flexure_shift=self.spat_flexure_shift)
            if debug:
                left, right = slits.select_edges(
                    flexure=self.spat_flexure_shift)
                viewer, ch = display.show_image(illum_flat,
                                                chname='illum_flat')
                display.show_slits(viewer, ch, left, right)  # , slits.id)
                #
                orig_image = self.image.copy()
                viewer, ch = display.show_image(orig_image,
                                                chname='orig_image')
                display.show_slits(viewer, ch, left, right)  # , slits.id)

        # Apply the relative spectral illumination
        spec_illum = 1.0
        if self.par['use_specillum']:
            if flatimages is None or flatimages.get_spec_illum() is None:
                msgs.error(
                    "Spectral illumination correction desired but not generated/provided."
                )
            else:
                spec_illum = flatimages.get_spec_illum().copy()

        # Flat field -- We cannot do illumination flat without a pixel flat (yet)
        if self.par['use_pixelflat'] or self.par['use_illumflat']:
            if flatimages is None or flatimages.get_pixelflat() is None:
                msgs.error("Flat fielding desired but not generated/provided.")
            else:
                self.flatten(flatimages.get_pixelflat() * spec_illum,
                             illum_flat=illum_flat,
                             bpm=self.bpm)

        # Fresh BPM
        bpm = self.spectrograph.bpm(self.filename,
                                    self.det,
                                    shape=self.image.shape)

        # Extras
        self.build_rn2img()
        self.build_ivar()

        # Generate a PypeItImage
        pypeitImage = pypeitimage.PypeItImage(
            self.image,
            ivar=self.ivar,
            rn2img=self.rn2img,
            bpm=bpm,
            detector=self.detector,
            spat_flexure=self.spat_flexure_shift,
            PYP_SPEC=self.spectrograph.spectrograph)
        pypeitImage.rawheadlist = self.headarr
        pypeitImage.process_steps = [
            key for key in self.steps.keys() if self.steps[key]
        ]

        # Mask(s)
        if par['mask_cr']:
            pypeitImage.build_crmask(self.par)
        #
        nonlinear_counts = self.spectrograph.nonlinear_counts(
            self.detector, apply_gain=self.par['apply_gain'])
        # Build
        pypeitImage.build_mask(saturation=nonlinear_counts)

        # Return
        return pypeitImage