示例#1
0
def test_process(deimos_flat_files):
    # Instantiate
    traceImage = traceimage.TraceImage('keck_deimos', deimos_flat_files)
    # Run
    assert traceImage.nfiles == 2
    mstrace = traceImage.process(bias_subtract='overscan', trim=True)
    assert isinstance(mstrace, np.ndarray)
    assert traceImage.steps[-1] == 'combine'
    assert traceImage.steps[-2] == 'bias_subtract'
示例#2
0
def test_process(deimos_flat_files):
    keck_deimos = load_spectrograph('keck_deimos')
    # Instantiate
    traceImage = traceimage.TraceImage(keck_deimos, deimos_flat_files)
    # Run
    assert traceImage.nfiles == 2
    traceImage.build_image()
    assert isinstance(traceImage.pypeitImage.image, np.ndarray)
    for key in ['subtract_overscan', 'apply_gain']:
        assert key in traceImage.process_steps
def get_tslits_nires(flat_files,
                     user_settings=par,
                     gingashow=True,
                     tilt_root='tilt_nires'):
    """Precess flat files and get titlts for NIRES
    """

    # Process flat images
    tImage = traceimage.TraceImage(spectrograph,
                                   file_list=flat_files,
                                   par=par['calibrations']['traceframe'])

    tflat = tImage.process(bias_subtract='overscan',
                           trim=False)

    mstrace = tflat.copy()

    # Define pixlocn and bpm
    pixlocn = pixels.gen_pixloc(tImage.stack.shape)
    bpm = spectrograph.bpm(shape=tflat.shape, det=1)

    # Instantiate Trace
    tSlits = traceslits.TraceSlits(mstrace,
                                   pixlocn,
                                   par=par['calibrations']['slits'],
                                   binbpx=bpm)
    tslits_dict = tSlits.run(plate_scale = 0.123)

    if gingashow:
        # Look at what TraceSlits was actually trying to trace
        viewer, ch = ginga.show_image(tSlits.edgearr)
        # Look at the sawtooth convolved image
        viewer, ch = ginga.show_image(tSlits.siglev)

        tmp = tSlits.edgearr * 100.
        tmp[np.where(tmp == 0.)] = 1.
        ginga.show_image(tSlits.mstrace * tmp)
        ginga.show_slits(viewer,
                         ch,
                         tSlits.lcen,
                         tSlits.rcen,
                         slit_ids=np.arange(tSlits.lcen.shape[1]) + 1,
                         pstep=50)

    if tilt_root is not None:
        # Write dict on a json file
        jdict = ltu.jsonify(tslits_dict.copy())
        ltu.savejson(tilt_root + '.json', jdict, overwrite=True, indent=None, easy_to_read=True)
        print("Wrote: {:s}".format(tilt_root + '.json'))

    return tslits_dict
示例#4
0
def test_instantiate(deimos_flat_files):
    # Empty
    traceImage = traceimage.TraceImage('keck_deimos', [])
示例#5
0
def main(args):

    import time
    import os
    import numpy as np
    from pypeit.spectrographs.util import load_spectrograph
    from pypeit import traceimage, edgetrace, biasframe
    from pypeit.pypeit import PypeIt
    from pypeit.core import parse

    from IPython import embed

    if args.pypeit_file is not None:
        pypeit_file = args.pypeit_file
        if not os.path.isfile(pypeit_file):
            raise FileNotFoundError(
                'File does not exist: {0}'.format(pypeit_file))
        pypeit_file = os.path.abspath(pypeit_file)
        redux_path = os.path.abspath(
            os.path.split(pypeit_file)[0] if args.redux_path is None else args.
            redux_path)

        rdx = PypeIt(pypeit_file, redux_path=redux_path)
        # Save the spectrograph
        spec = rdx.spectrograph
        # Get the calibration group to use
        group = np.unique(
            rdx.fitstbl['calib'])[0] if args.group is None else args.group
        if group not in np.unique(rdx.fitstbl['calib']):
            raise ValueError(
                'Not a valid calibration group: {0}'.format(group))
        # Find the rows in the metadata table with trace frames in the
        # specified calibration group
        tbl_rows = rdx.fitstbl.find_frames('trace',
                                           calib_ID=int(group),
                                           index=True)
        # Master keyword
        master_key_base = '_'.join(
            rdx.fitstbl.master_key(tbl_rows[0]).split('_')[:2])
        # Save the binning
        binning = rdx.fitstbl['binning'][tbl_rows[0]]
        # Save the full file paths
        files = rdx.fitstbl.frame_paths(tbl_rows)
        # Trace image processing parameters
        proc_par = rdx.caliBrate.par['traceframe']
        # Slit tracing parameters
        trace_par = rdx.caliBrate.par['slitedges']

        # Get the bias files, if requested
        bias_rows = rdx.fitstbl.find_frames('bias',
                                            calib_ID=int(group),
                                            index=True)
        bias_files = rdx.fitstbl.frame_paths(bias_rows)
        bias_par = rdx.caliBrate.par['biasframe']

        # Set the QA path
        qa_path = rdx.qa_path
    else:
        spec = load_spectrograph(args.spectrograph)
        master_key_base = 'A_1'
        binning = '1,1' if args.binning is None else args.binning
        if not os.path.isfile(args.trace_file):
            raise FileNotFoundError('File does not exist: {0}'.format(
                args.trace_file))
        files = [os.path.abspath(args.trace_file)]
        redux_path = os.path.abspath(
            os.path.split(files[0])[0] if args.redux_path is None else args.
            redux_path)
        par = spec.default_pypeit_par()
        proc_par = par['calibrations']['traceframe']
        trace_par = par['calibrations']['slitedges']
        bias_files = None
        bias_par = None

        # Set the QA path
        qa_path = os.path.join(os.path.abspath(os.path.split(files[0])[0]),
                               'QA')

    detectors = np.arange(spec.ndet) + 1 if args.detector is None else [
        args.detector
    ]
    master_dir = os.path.join(redux_path, args.master_dir)
    for det in detectors:
        # Master keyword for output file name
        master_key = '{0}_{1}'.format(master_key_base, str(det).zfill(2))

        # Get the bias frame if requested
        if bias_files is None:
            proc_par['process']['bias'] = 'skip'
            msbias = None
        else:
            biasFrame = biasframe.BiasFrame(spec,
                                            files=bias_files,
                                            det=det,
                                            par=bias_par,
                                            master_key=master_key,
                                            master_dir=master_dir)
            msbias = biasFrame.build_image()

        msbpm = spec.bpm(files[0], det)

        # Build the trace image
        traceImage = traceimage.TraceImage(spec,
                                           files=files,
                                           det=det,
                                           par=proc_par,
                                           bias=msbias)
        traceImage.build_image(bias=msbias, bpm=msbpm)

        # Trace the slit edges
        t = time.perf_counter()
        edges = edgetrace.EdgeTraceSet(spec,
                                       trace_par,
                                       master_key=master_key,
                                       master_dir=master_dir,
                                       img=traceImage,
                                       det=det,
                                       bpm=msbpm,
                                       auto=True,
                                       debug=args.debug,
                                       show_stages=args.show,
                                       qa_path=qa_path)
        print('Tracing for detector {0} finished in {1} s.'.format(
            det,
            time.perf_counter() - t))
        edges.save()

    return 0
示例#6
0
    def get_slits(self, redo=False, write_qa=True):
        """
        Load or generate the definition of the slit boundaries.

        Internals that must be available are :attr:`fitstbl`,
        :attr:`calib_ID`, :attr:`det`.

        Args:
            redo (bool): Redo
            write_qa (bool, optional):
              Generate the QA?  Turn off for testing..

        Returns:
            Returns the trace-slits dictionary (also kept internally as
            :attr:`tslits_dict`) and the slit mask array (numpy.ndarray;
            also kept internally as :attr:`maskslits`)

        """
        # Check for existing data
        if not self._chk_objs(['msbpm']):
            self.tslits_dict = None
            return self.tslits_dict

        # Check internals
        self._chk_set(['det', 'calib_ID', 'par'])

        # Prep
        trace_rows = self.fitstbl.find_frames('trace',
                                              calib_ID=self.calib_ID,
                                              index=True)
        self.trace_image_files = self.fitstbl.frame_paths(trace_rows)
        self.master_key_dict['trace'] \
                = self.fitstbl.master_key(trace_rows[0] if len(trace_rows) > 0 else self.frame,
                                          det=self.det)

        # Return already generated data
        if self._cached('trace', self.master_key_dict['trace']) and not redo:
            self.tslits_dict = self.calib_dict[
                self.master_key_dict['trace']]['trace']
            return self.tslits_dict

        # Instantiate
        self.traceSlits = traceslits.TraceSlits(
            self.spectrograph,
            self.par['slits'],
            det=self.det,
            master_key=self.master_key_dict['trace'],
            master_dir=self.master_dir,
            qa_path=self.qa_path,
            reuse_masters=self.reuse_masters,
            msbpm=self.msbpm)

        # Load the MasterFrame (if it exists and is desired)?
        self.tslits_dict, _ = self.traceSlits.load()
        if self.tslits_dict is None:
            # Build the trace image
            self.traceImage = traceimage.TraceImage(
                self.spectrograph,
                files=self.trace_image_files,
                det=self.det,
                par=self.par['traceframe'],
                bias=self.msbias)
            self.traceImage.build_image(bias=self.msbias, bpm=self.msbpm)

            # Compute the plate scale in arcsec which is needed to trim short slits
            binspectral, binspatial = parse.parse_binning(self.binning)
            plate_scale = binspatial * self.spectrograph.detector[
                self.det - 1]['platescale']

            # JFH Why is this stuff on user defined slits here and not
            # in the class?  User-defined slits??
            # TODO: this should be done inside TraceSlits so that the
            # call to run() or whatever has the same format as what the
            # user sees in TraceSlitsPar
            add_user_slits = None if self.par['slits']['add_slits'] is None \
                                else trace_slits.parse_user_slits(self.par['slits']['add_slits'],
                                                                  self.det)
            rm_user_slits = None if self.par['slits']['rm_slits'] is None \
                                else trace_slits.parse_user_slits(self.par['slits']['rm_slits'],
                                                                  self.det, rm=True)
            # Now we go forth
            try:
                self.tslits_dict = self.traceSlits.run(
                    self.traceImage.image,
                    self.binning,
                    add_user_slits=add_user_slits,
                    rm_user_slits=rm_user_slits,
                    plate_scale=plate_scale,
                    show=self.show,
                    write_qa=write_qa)
            except:
                self.traceSlits.save(traceImage=self.traceImage)
                msgs.error(
                    'Crashed out of finding the slits. Have saved the work done to disk '
                    'but it needs fixing.')

            # No slits?
            if self.tslits_dict is None:
                return self.tslits_dict

            # Save to disk
            if self.save_masters:
                self.traceSlits.save(traceImage=self.traceImage)

        # Save, initialize maskslits, and return
        # TODO: We're not caching self.mstrace.  And actually there is
        # no mstrace in Calibrations anymore; only in TraceSlits?
        self._update_cache('trace', 'trace', self.tslits_dict)
        return self.tslits_dict
示例#7
0
    def get_slits(self, redo=False, write_qa=True):
        """
        Load or generate the definition of the slit boundaries.

        Internals that must be available are :attr:`fitstbl`,
        :attr:`calib_ID`, :attr:`det`.

        Args:
            redo (bool): Redo
            write_qa (bool, optional):
              Generate the QA?  Turn off for testing..

        Returns:
            Returns the trace-slits dictionary (also kept internally as
            :attr:`tslits_dict`) and the slit mask array (numpy.ndarray;
            also kept internally as :attr:`maskslits`)

        """
        # Check for existing data
        if not self._chk_objs(['msbpm']):
            self.tslits_dict = None
            return self.tslits_dict

        # Check internals
        self._chk_set(['det', 'calib_ID', 'par'])

        # Prep
        trace_rows = self.fitstbl.find_frames('trace',
                                              calib_ID=self.calib_ID,
                                              index=True)
        self.trace_image_files = self.fitstbl.frame_paths(trace_rows)
        self.master_key_dict['trace'] \
                = self.fitstbl.master_key(trace_rows[0] if len(trace_rows) > 0 else self.frame,
                                          det=self.det)

        # Return already generated data
        if self._cached('trace', self.master_key_dict['trace']) and not redo:
            self.tslits_dict = self.calib_dict[
                self.master_key_dict['trace']]['trace']
            return self.tslits_dict

        # Instantiate
        self.edges = edgetrace.EdgeTraceSet(
            self.spectrograph,
            self.par['slitedges'],
            master_key=self.master_key_dict['trace'],
            master_dir=self.master_dir,
            qa_path=self.qa_path if write_qa else None)

        if self.reuse_masters and self.edges.exists():
            self.edges.load()
            self.tslits_dict = self.edges.convert_to_tslits_dict()
        else:
            # Build the trace image
            self.traceImage = traceimage.TraceImage(
                self.spectrograph,
                files=self.trace_image_files,
                det=self.det,
                par=self.par['traceframe'],
                bias=self.msbias)
            self.traceImage.build_image(bias=self.msbias, bpm=self.msbpm)

            try:
                self.edges.auto_trace(
                    self.traceImage,
                    bpm=self.msbpm,
                    det=self.det,
                    save=self.save_masters)  #, debug=True, show_stages=True)
            except:
                self.edges.save()
                msgs.error(
                    'Crashed out of finding the slits. Have saved the work done to disk '
                    'but it needs fixing.')
                return None

            # Show the result if requested
            if self.show:
                self.edges.show(thin=10, in_ginga=True)

            # TODO: Stop-gap until we can get rid of tslits_dict
            self.tslits_dict = self.edges.convert_to_tslits_dict()

        # Save, initialize maskslits, and return
        self._update_cache('trace', 'trace', self.tslits_dict)
        return self.tslits_dict