Пример #1
0
    def MasterTrace(self, fitsdict, det):
        """
        Generate Master Trace frame for a given detector

        Parameters
        ----------
        fitsdict : dict
          Contains relevant information from fits header files
        det : int
          Index of the detector

        Returns
        -------
        boolean : bool
          Should other ScienceExposure classes be updated?
        """

        # If the master trace is already made, use it
        if self._mstrace[det-1] is not None:
            msgs.info("An identical master trace frame already exists")
            return False
        if self._argflag['reduce']['usetrace'] in ['trace', 'blzflat']:
            if self._argflag['masters']['use']:
                # Attempt to load the Master Frame
                mstrace_name = armasters.master_name(self._argflag['run']['masterdir'],
                                                   'trace', self._argflag['masters']['setup'])
                try:
                    mstrace, head = arload.load_master(mstrace_name, frametype="trace")
                except IOError:
                    msgs.warn("No MasterTrace frame found {:s}".format(mstrace_name))
                else:
                    # Extras
                    lordloc, _ = arload.load_master(mstrace_name, frametype="trace", exten=1)
                    rordloc, _ = arload.load_master(mstrace_name, frametype="trace", exten=2)
                    pixcen, _ = arload.load_master(mstrace_name, frametype="trace", exten=3)
                    pixwid, _ = arload.load_master(mstrace_name, frametype="trace", exten=4)
                    lordpix, _ = arload.load_master(mstrace_name, frametype="trace", exten=5)
                    rordpix, _ = arload.load_master(mstrace_name, frametype="trace", exten=6)
                    self.SetFrame(self._lordloc, lordloc, det)
                    self.SetFrame(self._rordloc, rordloc, det)
                    self.SetFrame(self._pixcen, pixcen.astype(np.int), det)
                    self.SetFrame(self._pixwid, pixwid.astype(np.int), det)
                    self.SetFrame(self._lordpix, lordpix.astype(np.int), det)
                    self.SetFrame(self._rordpix, rordpix.astype(np.int), det)
                    #
                    self._argflag['masters']['loaded'].append('trace'+self._argflag['masters']['setup'])
            if 'trace'+self._argflag['masters']['setup'] not in self._argflag['masters']['loaded']:
                msgs.info("Preparing a master trace frame with {0:s}".format(self._argflag['reduce']['usetrace']))
                ind = self._idx_trace
                # Load the frames for tracing
                frames = arload.load_frames(self, fitsdict, ind, det, frametype='trace', msbias=self._msbias[det-1],
                                            trim=self._argflag['reduce']['trim'], transpose=self._transpose)
                if self._argflag['reduce']['flatmatch'] > 0.0:
                    sframes = arsort.match_frames(frames, self._argflag['reduce']['flatmatch'], msgs, frametype='trace', satlevel=self._spect['det'][det-1]['saturation']*self._spect['det'][det-1]['nonlinear'])
                    subframes = np.zeros((frames.shape[0], frames.shape[1], len(sframes)))
                    numarr = np.array([])
                    for i in xrange(len(sframes)):
                        numarr = np.append(numarr, sframes[i].shape[2])
                        mstrace = arcomb.comb_frames(sframes[i], det, spect=self._spect, frametype='trace', **self._argflag['trace']['comb'])
                        subframes[:,:,i] = mstrace.copy()
                    del sframes
                    # Combine all sub-frames
                    mstrace = arcomb.comb_frames(subframes, det, spect=self._spect, frametype='trace', weights=numarr, **self._argflag['trace']['comb'])
                    del subframes
                else:
                    mstrace = arcomb.comb_frames(frames, det, spect=self._spect, frametype='trace', **self._argflag['trace']['comb'])
                del frames
        elif self._argflag['reduce']['usetrace'] == 'science':
            msgs.error("Tracing with a science frame is not yet implemented")
        else: # It must be the name of a file the user wishes to load
            mstrace_name = self._argflag['run']['masterdir']+'/'+self._argflag['reduce']['usetrace']
            mstrace, head = arload.load_master(mstrace_name, frametype=None)
            debugger.set_trace()  # NEED TO LOAD EXTRAS AS ABOVE
        # Set and then delete the Master Trace frame
        self.SetMasterFrame(mstrace, "trace", det)
        del mstrace
        return True
Пример #2
0
    def MasterFlatField(self, fitsdict, det):
        """
        Generate Master Flat-field frame for a given detector

        Parameters
        ----------
        fitsdict : dict
          Contains relevant information from fits header files
        det : int
          Index of the detector

        Returns
        -------
        boolean : bool
          Should other ScienceExposure classes be updated?
        """

        if self._argflag['reduce']['flatfield']:  # Only do it if the user wants to flat field
        # If the master pixflat is already made, use it
            if self._mspixflat[det-1] is not None:
                msgs.info("An identical master pixflat frame already exists")
                if self._mspixflatnrm[det-1] is None:
                    # Normalize the flat field
                    msgs.info("Normalizing the pixel flat")
                    mspixflatnrm, msblaze = arproc.flatnorm(self, det, self.GetMasterFrame("pixflat", det),
                                                            overpix=0, plotdesc="Blaze function")
                    self.SetFrame(self._msblaze, msblaze, det)
                    self.SetMasterFrame(mspixflatnrm, "normpixflat", det)
                return False
            ###############
            # Generate a master pixel flat frame
            if self._argflag['reduce']['useflat'] in ['pixflat', 'blzflat']:
                if self._argflag['masters']['use']:
                    # Attempt to load the Master Frame
                    msflat_name = armasters.master_name(self._argflag['run']['masterdir'],
                                                    'normpixflat', self._argflag['masters']['setup'])
                    try:
                        mspixflatnrm, head = arload.load_master(msflat_name, frametype="normpixflat")
                    except IOError:
                        msgs.warn("No MasterFlatField frame found {:s}".format(msflat_name))
                    else:
                        self._argflag['masters']['loaded'].append('normpixflat'+self._argflag['masters']['setup'])
                        mspixflat = mspixflatnrm
                if 'normpixflat'+self._argflag['masters']['setup'] not in self._argflag['masters']['loaded']:
                    msgs.info("Preparing a master pixel flat frame with {0:s}".format(self._argflag['reduce']['useflat']))
                    # Get all of the pixel flat frames for this science frame
                    ind = self._idx_flat
                    # Load the frames for tracing
                    frames = arload.load_frames(self, fitsdict, ind, det, frametype='pixel flat',
                                                msbias=self._msbias[det-1], transpose=self._transpose)
                    if self._argflag['reduce']['flatmatch'] > 0.0:
                        sframes = arsort.match_frames(frames, self._argflag['reduce']['flatmatch'],
                                                      frametype='pixel flat', satlevel=self._nonlinear)
                        subframes = np.zeros((frames.shape[0], frames.shape[1], len(sframes)))
                        numarr = np.array([])
                        for i in xrange(len(sframes)):
                            numarr = np.append(numarr, sframes[i].shape[2])
                            mspixflat = arcomb.comb_frames(sframes[i], det, spect=self._spect, frametype='pixel flat',
                                                           **self._argflag['pixflat']['comb'])
                            subframes[:,:,i] = mspixflat.copy()
                        del sframes
                        # Combine all sub-frames
                        mspixflat = arcomb.comb_frames(subframes, det, spect=self._spect, frametype='pixel flat',
                                                       weights=numarr, **self._argflag['pixflat']['comb'])
                        del subframes
                    else:
                        mspixflat = arcomb.comb_frames(frames, det, spect=self._spect, frametype='pixel flat',
                                                       **self._argflag['pixflat']['comb'])
                    del frames
                    # Apply gain (instead of ampsec scale)
                    mspixflat *= arproc.gain_frame(self, det)
                    # Normalize the flat field
                    mspixflatnrm, msblaze = arproc.flatnorm(self, det, mspixflat, overpix=0, plotdesc="Blaze function")
                    self.SetFrame(self._msblaze, msblaze, det)
            else:  # It must be the name of a file the user wishes to load
                mspixflat_name = armasters.user_master_name(self._argflag['run']['masterdir'],
                                                            self._argflag['reduce']['useflat'])
                mspixflatnrm, head = arload.load_master(mspixflat_name, exten=det, frametype=None)
                mspixflat = mspixflatnrm
            # Now that the combined, master flat field frame is loaded...
        else:
            msgs.work("Pixel Flat arrays need to be generated when not flat fielding")
            msgs.bug("Blaze is currently undefined")
            mspixflat = np.ones_like(self._msarc)
            mspixflatnrm = np.ones_like(self._msarc)
        # Set Master Frames
        self.SetMasterFrame(mspixflat, "pixflat", det)
        self.SetMasterFrame(mspixflatnrm, "normpixflat", det)
        return True
Пример #3
0
    def MasterArc(self, fitsdict, det):
        """
        Generate Master Arc frame for a given detector

        Parameters
        ----------
        fitsdict : dict
          Contains relevant information from fits header files
        det : int
          Index of the detector

        Returns
        -------
        boolean : bool
          Should other ScienceExposure classes be updated?
        """

        if self._msarc[det-1] is not None:
            msgs.info("An identical master arc frame already exists")
            return False
        if self._argflag['reduce']['usearc'] in ['arc']:
            # Attempt to load the Master Frame
            if self._argflag['masters']['use']:
                msarc_name = armasters.master_name(self._argflag['run']['masterdir'],
                                                    'arc', self._argflag['masters']['setup'])
                try:
                    msarc, head = arload.load_master(msarc_name, frametype="arc")
                except IOError:
                    msgs.warn("No MasterArc frame found {:s}".format(msarc_name))
                else:
                    self._transpose = head['transp']
                    if self._transpose:  # Need to setup for flipping
                        self._argflag['trace']['disp']['direction'] = 1
                    else:
                        self._argflag['trace']['disp']['direction'] = 0
                    # Append as loaded
                    self._argflag['masters']['loaded'].append('arc'+self._argflag['masters']['setup'])
            if 'arc'+self._argflag['masters']['setup'] not in self._argflag['masters']['loaded']:
                msgs.info("Preparing a master arc frame")
                ind = self._idx_arcs
                # Load the arc frames
                frames = arload.load_frames(self, fitsdict, ind, det, frametype='arc',
                                            msbias=self._msbias[det-1])
                if self._argflag['reduce']['arcmatch'] > 0.0:
                    sframes = arsort.match_frames(frames, self._argflag['reduce']['arcmatch'], msgs, frametype='arc',
                                                  satlevel=self._spect['det']['saturation']*self._spect['det']['nonlinear'])
                    subframes = np.zeros((frames.shape[0], frames.shape[1], len(sframes)))
                    numarr = np.array([])
                    for i in xrange(len(sframes)):
                        numarr = np.append(numarr, sframes[i].shape[2])
                        msarc = arcomb.comb_frames(sframes[i], det, spect=self._spect,
                                                   frametype='arc', **self._argflag['arc']['comb'])
                        # Send the data away to be saved
                        subframes[:,:,i] = msarc.copy()
                    del sframes
                    # Combine all sub-frames
                    msarc = arcomb.comb_frames(subframes, det, spect=self._spect,
                                               frametype='arc', weights=numarr, **self._argflag['arc']['comb'])
                    del subframes
                else:
                    msarc = arcomb.comb_frames(frames, det, spect=self._spect,
                                           frametype='arc', **self._argflag['arc']['comb'])
                del frames
            # # Derive a suitable name for the master arc frame
            # msarc_name = "{0:s}/{1:s}/msarc{2:s}_{3:03d}.fits".format(os.getcwd(),self._argflag['run']['masterdir'],self._spect["det"][det-1]["suffix"],len(self._done_arcs))
            # self._tltprefix = os.path.splitext(os.path.basename(msarc_name))[0]
            # # Send the data away to be saved
            # arsave.save_master(self, msarc, filename=msarc_name, frametype='arc', ind=ind)
            # # Store the files used and the master bias name in case it can be used during the later reduction processes
            # self._done_arcs.append(ind)
            # self._name_arcs.append(msarc_name)
        else:
            msarc_name = self._argflag['run']['masterdir']+'/'+self._argflag['reduce']['usearc']
            msarc, head = arload.load_master(msarc_name, frametype=None)
        # Set and then delete the Master Arc frame
        self.SetMasterFrame(msarc, "arc", det)
        del msarc
        return True
Пример #4
0
    def MasterTrace(self, fitsdict, det):
        """
        Generate Master Trace frame for a given detector

        Parameters
        ----------
        fitsdict : dict
          Contains relevant information from fits header files
        det : int
          Index of the detector

        Returns
        -------
        boolean : bool
          Should other ScienceExposure classes be updated?
        """
        dnum = settings.get_dnum(det)
        # If the master trace is already made, use it
        if self._mstrace[det - 1] is not None:
            msgs.info("An identical master trace frame already exists")
            return False
        if settings.argflag['reduce']['trace']['useframe'] in ['trace']:
            try:
                mstrace = armasters.get_master_frame(self, "trace", det=det)
            except IOError:
                msgs.info("Preparing a master trace frame with {0:s}".format(
                    settings.argflag['reduce']['trace']['useframe']))
                ind = self._idx_trace
                # Load the frames for tracing
                frames = arload.load_frames(
                    fitsdict,
                    ind,
                    det,
                    frametype='trace',
                    msbias=self._msbias[det - 1],
                    trim=settings.argflag['reduce']['trim'])
                if settings.argflag['trace']['combine']['match'] > 0.0:
                    sframes = arsort.match_frames(
                        frames,
                        settings.argflag['trace']['combine']['match'],
                        frametype='trace',
                        satlevel=settings.spect[dnum]['saturation'] *
                        settings.spect['det'][det - 1]['nonlinear'])
                    subframes = np.zeros(
                        (frames.shape[0], frames.shape[1], len(sframes)))
                    numarr = np.array([])
                    for i in range(len(sframes)):
                        numarr = np.append(numarr, sframes[i].shape[2])
                        mstrace = arcomb.comb_frames(sframes[i], det, 'trace')
                        subframes[:, :, i] = mstrace.copy()
                    del sframes
                    # Combine all sub-frames
                    mstrace = arcomb.comb_frames(subframes,
                                                 det,
                                                 'trace',
                                                 weights=numarr)
                    del subframes
                else:
                    mstrace = arcomb.comb_frames(frames, det, 'trace')
                del frames
        else:  # It must be the name of a file the user wishes to load
            mstrace_name = settings.argflag['run']['directory'][
                'master'] + '/' + settings.argflag['reduce']['trace'][
                    'useframe']
            mstrace, _ = arload.load_master(mstrace_name, frametype=None)
            debugger.set_trace(
            )  # NEED TO LOAD EXTRAS AS ABOVE;  SHOULD MODIFY get_master_frame()
        # Set and then delete the Master Trace frame
        self.SetMasterFrame(mstrace, "trace", det)
        del mstrace
        return True
Пример #5
0
    def MasterFlatField(self, fitsdict, det):
        """
        Generate Master Flat-field frame for a given detector

        Parameters
        ----------
        fitsdict : dict
          Contains relevant information from fits header files
        det : int
          Index of the detector

        Returns
        -------
        boolean : bool
          Should other ScienceExposure classes be updated?
        """
        from pypit import arqa

        if settings.argflag['reduce']['flatfield'][
                'perform']:  # Only do it if the user wants to flat field
            # If the master pixelflat is already made, use it
            if self._mspixelflat[det - 1] is not None:
                msgs.info("An identical master pixelflat frame already exists")
                if self._mspixelflatnrm[det - 1] is None:
                    # Normalize the flat field
                    msgs.info("Normalizing the pixel flat")
                    slit_profiles, mstracenrm, msblaze, flat_ext1d, extrap_slit = \
                        arproc.slit_profile(self, self.GetMasterFrame("pixelflat", det),
                                            det, ntcky=settings.argflag['reduce']['flatfield']['params'][0])
                    # If some slit profiles/blaze functions need to be extrapolated, do that now
                    if np.sum(extrap_slit) != 0.0:
                        slit_profiles, mstracenrm, msblaze = arproc.slit_profile_pca(
                            self, self.GetMasterFrame("pixelflat", det), det,
                            msblaze, extrap_slit, slit_profiles)
                    mspixelflatnrm = mstracenrm.copy()
                    winpp = np.where(slit_profiles != 0.0)
                    mspixelflatnrm[winpp] /= slit_profiles[winpp]
                    self.SetMasterFrame(mspixelflatnrm, "normpixelflat", det)
                    armasters.save_masters(self, det, mftype='normpixelflat')
                    if np.array_equal(self._idx_flat, self._idx_trace):
                        # The flat field frame is also being used to trace the slit edges and determine the slit
                        # profile. Avoid recalculating the slit profile and blaze function and save them here.
                        self.SetFrame(self._msblaze, msblaze, det)
                        self.SetFrame(self._slitprof, slit_profiles, det)
                        armasters.save_masters(self, det, mftype='slitprof')
                        if settings.argflag["reduce"]["slitprofile"][
                                "perform"]:
                            msgs.info("Preparing QA of each slit profile")
                            arqa.slit_profile(self,
                                              mstracenrm,
                                              slit_profiles,
                                              self._lordloc[det - 1],
                                              self._rordloc[det - 1],
                                              self._slitpix[det - 1],
                                              desc="Slit profile")
                        msgs.info("Saving blaze function QA")
                        arqa.plot_orderfits(self,
                                            msblaze,
                                            flat_ext1d,
                                            desc="Blaze function")
                return False
            ###############
            # Generate/load a master pixel flat frame
            if settings.argflag['reduce']['flatfield']['useframe'] in [
                    'pixelflat', 'trace'
            ]:
                try:
                    mspixelflatnrm = armasters.get_master_frame(
                        self, "normpixelflat")
                except IOError:
                    msgs.info("Preparing a master pixel flat frame with {0:s}".
                              format(settings.argflag['reduce']['flatfield']
                                     ['useframe']))
                    # Get all of the pixel flat frames for this science frame
                    ind = self._idx_flat
                    # Load the frames for tracing
                    frames = arload.load_frames(fitsdict,
                                                ind,
                                                det,
                                                frametype='pixel flat',
                                                msbias=self._msbias[det - 1])
                    if settings.argflag['pixelflat']['combine']['match'] > 0.0:
                        sframes = arsort.match_frames(
                            frames,
                            settings.argflag['pixelflat']['combine']['match'],
                            frametype='pixel flat',
                            satlevel=self._nonlinear)
                        subframes = np.zeros(
                            (frames.shape[0], frames.shape[1], len(sframes)))
                        numarr = np.array([])
                        for i in range(len(sframes)):
                            numarr = np.append(numarr, sframes[i].shape[2])
                            mspixelflat = arcomb.comb_frames(
                                sframes[i],
                                det,
                                'pixelflat',
                                printtype='pixel flat')
                            subframes[:, :, i] = mspixelflat.copy()
                        del sframes
                        # Combine all sub-frames
                        mspixelflat = arcomb.comb_frames(
                            subframes,
                            det,
                            'pixelflat',
                            weights=numarr,
                            printtype='pixel flat')
                        del subframes
                    else:
                        mspixelflat = arcomb.comb_frames(
                            frames, det, 'pixelflat', printtype='pixel flat')
                    del frames
                    # Apply gain (instead of ampsec scale)
                    mspixelflat *= arproc.gain_frame(self, det)
                    # Normalize the flat field
                    msgs.info("Normalizing the pixel flat")
                    slit_profiles, mstracenrm, msblaze, flat_ext1d, extrap_slit = \
                        arproc.slit_profile(self, mspixelflat, det, ntcky=settings.argflag['reduce']['flatfield']['params'][0])
                    # If some slit profiles/blaze functions need to be extrapolated, do that now
                    if np.sum(extrap_slit) != 0.0:
                        slit_profiles, mstracenrm, msblaze = arproc.slit_profile_pca(
                            self, mspixelflat, det, msblaze, extrap_slit,
                            slit_profiles)
                    mspixelflatnrm = mstracenrm.copy()
                    winpp = np.where(slit_profiles != 0.0)
                    mspixelflatnrm[winpp] /= slit_profiles[winpp]
                    if np.array_equal(self._idx_flat, self._idx_trace):
                        # The flat field frame is also being used to trace the slit edges and determine the slit
                        # profile. Avoid recalculating the slit profile and blaze function and save them here.
                        self.SetFrame(self._msblaze, msblaze, det)
                        self.SetFrame(self._slitprof, slit_profiles, det)
                        armasters.save_masters(self, det, mftype='slitprof')
                        if settings.argflag["reduce"]["slitprofile"][
                                "perform"]:
                            msgs.info("Preparing QA of each slit profile")
                            arqa.slit_profile(self,
                                              mstracenrm,
                                              slit_profiles,
                                              self._lordloc[det - 1],
                                              self._rordloc[det - 1],
                                              self._slitpix[det - 1],
                                              desc="Slit profile")
                        msgs.info("Saving blaze function QA")
                        arqa.plot_orderfits(self,
                                            msblaze,
                                            flat_ext1d,
                                            desc="Blaze function")
                else:
                    mspixelflat = mspixelflatnrm
            else:  # It must be the name of a file the user wishes to load
                mspixelflat_name = armasters.user_master_name(
                    settings.argflag['run']['directory']['master'],
                    settings.argflag['reduce']['flatfield']['useframe'])
                mspixelflatnrm, head = arload.load_master(mspixelflat_name,
                                                          exten=det,
                                                          frametype=None)
                mspixelflat = mspixelflatnrm
            # Now that the combined, master flat field frame is loaded...
        else:
            msgs.work(
                "Pixel Flat arrays need to be generated when not flat fielding"
            )
            msgs.bug("Blaze is currently undefined")
            mspixelflat = np.ones_like(self._msarc)
            mspixelflatnrm = np.ones_like(self._msarc)
        # Set Master Frames
        self.SetMasterFrame(mspixelflat, "pixelflat", det)
        self.SetMasterFrame(mspixelflatnrm, "normpixelflat", det)
        armasters.save_masters(self, det, mftype='normpixelflat')
        return True
Пример #6
0
    def MasterArc(self, fitsdict, det):
        """
        Generate Master Arc frame for a given detector

        Parameters
        ----------
        fitsdict : dict
          Contains relevant information from fits header files
        det : int
          Index of the detector

        Returns
        -------
        boolean : bool
          Should other ScienceExposure classes be updated?
        """
        dnum = settings.get_dnum(det)

        if self._msarc[det - 1] is not None:
            msgs.info("A master arc frame already exists for this frame")
            return False
        if settings.argflag['arc']['useframe'] in ['arc']:
            # Master Frame
            try:
                msarc = armasters.get_master_frame(self, "arc")
            except IOError:
                msgs.info("Preparing a master arc frame")
                ind = self._idx_arcs
                # Load the arc frames
                frames = arload.load_frames(fitsdict,
                                            ind,
                                            det,
                                            frametype='arc',
                                            msbias=self._msbias[det - 1])
                if settings.argflag['arc']['combine']['match'] > 0.0:
                    sframes = arsort.match_frames(
                        frames,
                        settings.argflag['arc']['combine']['match'],
                        frametype='arc',
                        satlevel=settings.spect[dnum]['saturation'] *
                        settings.spect[dnum]['nonlinear'])
                    subframes = np.zeros(
                        (frames.shape[0], frames.shape[1], len(sframes)))
                    numarr = np.array([])
                    for i in range(len(sframes)):
                        numarr = np.append(numarr, sframes[i].shape[2])
                        msarc = arcomb.comb_frames(sframes[i], det, 'arc')
                        # Send the data away to be saved
                        subframes[:, :, i] = msarc.copy()
                    del sframes
                    # Combine all sub-frames
                    msarc = arcomb.comb_frames(subframes,
                                               det,
                                               'arc',
                                               weights=numarr)
                    del subframes
                else:
                    msarc = arcomb.comb_frames(frames, det, 'arc')
                del frames
        else:  # Use input frame name located in MasterFrame directory
            msarc_name = settings.argflag['run']['directory'][
                'master'] + '/' + settings.argflag['arc']['useframe']
            msarc, _ = arload.load_master(msarc_name, frametype=None)

        # Set and then delete the Master Arc frame
        self.SetMasterFrame(msarc, "arc", det)
        armasters.save_masters(self, det, mftype='arc')
        del msarc
        return True