Ejemplo n.º 1
0
def test_proc_diff(nires_sci_files, nires_bg_files):
    """
    Run on near-IR frames
    """
    # Setup
    det = 1
    bpm = np.zeros((2048, 1024))
    pixelflat = np.ones_like(bpm)

    # Sci image
    flatImages = flatfield.FlatImages(pixelflat_norm=pixelflat)
    sciImg = buildimage.buildimage_fromlist(keck_nires,
                                            det,
                                            nires_par['scienceframe'],
                                            nires_sci_files,
                                            bias=None,
                                            bpm=bpm,
                                            flatimages=flatImages)
    # Bg image
    bgImg = buildimage.buildimage_fromlist(keck_nires,
                                           det,
                                           nires_par['scienceframe'],
                                           nires_bg_files,
                                           bias=None,
                                           bpm=bpm,
                                           flatimages=flatImages)
    # Difference
    sciImg = sciImg.sub(bgImg, nires_par['scienceframe']['process'])
    # Test
    assert isinstance(sciImg, pypeitimage.PypeItImage)
Ejemplo n.º 2
0
def test_flatimages():
    tmp = np.ones((1000, 100)) * 10.
    x = np.random.rand(500)
    # Create bspline
    spat_bspline1 = bspline.bspline(x, bkspace=0.01 * (np.max(x) - np.min(x)))
    spat_bspline2 = bspline.bspline(x, bkspace=0.01 * (np.max(x) - np.min(x)))
    instant_dict = dict(procflat=tmp,
                        pixelflat=np.ones_like(tmp),
                        flat_model=None,
                        spat_bsplines=np.asarray(
                            [spat_bspline1, spat_bspline2]),
                        spat_id=np.asarray([100, 200]))

    flatImages = flatfield.FlatImages(**instant_dict)
    assert flatImages.flat_model is None

    # I/O
    outfile = data_path('tst_flatimages.fits')
    flatImages.to_file(outfile, overwrite=True)
    _flatImages = flatfield.FlatImages.from_file(outfile)
    # Test
    for key in instant_dict.keys():
        if key == 'spat_bsplines':
            np.array_equal(flatImages[key][0].breakpoints,
                           _flatImages[key][0].breakpoints)
            continue
        if isinstance(instant_dict[key], np.ndarray):
            assert np.array_equal(flatImages[key], _flatImages[key])
        else:
            assert flatImages[key] == _flatImages[key]

    os.remove(outfile)
Ejemplo n.º 3
0
def test_proc_diff(nires_sci_files, nires_bg_files):
    """
    Run on near-IR frames
    """
    # Setup
    det = 1
    bpm = np.zeros((2048, 1024), dtype=int)
    pixelflat = np.ones(bpm.shape, dtype=float)
    nires_par = keck_nires.default_pypeit_par()

    # Sci image
    flatImages = flatfield.FlatImages(pixelflat_norm=pixelflat)
    nires_par['scienceframe']['process']['use_illumflat'] = False
    nires_par['scienceframe']['process']['use_specillum'] = False
    sciImg = buildimage.buildimage_fromlist(keck_nires,
                                            det,
                                            nires_par['scienceframe'],
                                            nires_sci_files,
                                            bias=None,
                                            bpm=bpm,
                                            flatimages=flatImages)
    # Bg image
    bgImg = buildimage.buildimage_fromlist(keck_nires,
                                           det,
                                           nires_par['scienceframe'],
                                           nires_bg_files,
                                           bias=None,
                                           bpm=bpm,
                                           flatimages=flatImages)
    # Difference
    sciImg = sciImg.sub(bgImg, nires_par['scienceframe']['process'])
    # Test
    assert isinstance(sciImg, pypeitimage.PypeItImage)
Ejemplo n.º 4
0
def test_instantiate_from_one(shane_kast_blue_sci_files):
    """
    Run on a single science frame
    """
    #
    det = 1
    # Load calibrations
    pixelflat = load_kast_blue_masters(pixflat=True)[0]
    bpm = kast_blue.empty_bpm(shane_kast_blue_sci_files[0], det)
    # Process steps -- Set in PypeItPar
    frame_par = kast_par['scienceframe']
    frame_par['process']['use_illumflat'] = False
    frame_par['process']['use_biasimage'] = False
    # Load
    rawImage = rawimage.RawImage(shane_kast_blue_sci_files[0], kast_blue, det)
    flatImages = flatfield.FlatImages(pixelflat_norm=pixelflat)
    pypeItImage = rawImage.process(frame_par['process'], flatimages=flatImages)
Ejemplo n.º 5
0
def test_flatimages():
    tmp = np.ones((1000, 100)) * 10.
    x = np.random.rand(500)
    # Create bspline
    spat_bspline1 = bspline.bspline(x, bkspace=0.01 * (np.max(x) - np.min(x)))
    spat_bspline2 = bspline.bspline(x, bkspace=0.01 * (np.max(x) - np.min(x)))
    instant_dict = dict(
        pixelflat_raw=tmp,
        pixelflat_norm=np.ones_like(tmp),
        pixelflat_model=None,
        pixelflat_spat_bsplines=np.asarray([spat_bspline1, spat_bspline2]),
        pixelflat_spec_illum=None,
        illumflat_raw=tmp,
        illumflat_spat_bsplines=np.asarray([spat_bspline1, spat_bspline2]),
        spat_id=np.asarray([100, 200]),
        PYP_SPEC="specname")

    flatImages = flatfield.FlatImages(**instant_dict)
    assert flatImages.pixelflat_model is None
    assert flatImages.pixelflat_spec_illum is None
    assert flatImages.pixelflat_spat_bsplines is not None

    # I/O
    outfile = data_path('tst_flatimages.fits')
    flatImages.to_master_file(outfile)
    _flatImages = flatfield.FlatImages.from_file(outfile)

    # Test
    for key in instant_dict.keys():
        if key == 'pixelflat_spat_bsplines':
            np.array_equal(flatImages[key][0].breakpoints,
                           _flatImages[key][0].breakpoints)
            continue
        if key == 'illumflat_spat_bsplines':
            np.array_equal(flatImages[key][0].breakpoints,
                           _flatImages[key][0].breakpoints)
            continue
        if isinstance(instant_dict[key], np.ndarray):
            assert np.array_equal(flatImages[key], _flatImages[key])
        else:
            assert flatImages[key] == _flatImages[key]

    os.remove(outfile)
Ejemplo n.º 6
0
def test_from_list(shane_kast_blue_sci_files):
    """
    Run on two frames
    """
    det = 1
    # Load calibrations
    pixelflat = load_kast_blue_masters(pixflat=True)[0]
    bpm = kast_blue.empty_bpm(shane_kast_blue_sci_files[0], det)
    # Do it
    flatImages = flatfield.FlatImages(pixelflat_norm=pixelflat)
    kast_par['scienceframe']['process']['use_illumflat'] = False
    kast_par['scienceframe']['process']['use_biasimage'] = False
    sciImg = buildimage.buildimage_fromlist(kast_blue,
                                            det,
                                            kast_par['scienceframe'],
                                            shane_kast_blue_sci_files,
                                            bpm=bpm,
                                            bias=None,
                                            flatimages=flatImages)
    # Test
    assert isinstance(sciImg, pypeitimage.PypeItImage)
Ejemplo n.º 7
0
    def get_flats(self):
        """
        Load or generate a normalized pixel flat and slit illumination
        flat.

        Requires :attr:`slits`, :attr:`wavetilts`, :attr:`det`,
        :attr:`par`.

        Constructs :attr:`flatimages`.

        """
        # Check for existing data
        if not self._chk_objs(['msarc', 'msbpm', 'slits', 'wv_calib']):
            msgs.warn(
                'Must have the arc, bpm, slits, and wv_calib defined to make flats!  Skipping and may crash down the line'
            )
            self.flatimages = flatfield.FlatImages()
            return

        # Slit and tilt traces are required to flat-field the data
        if not self._chk_objs(['slits', 'wavetilts']):
            # TODO: Why doesn't this fault?
            msgs.warn(
                'Flats were requested, but there are quantities missing necessary to '
                'create flats.  Proceeding without flat fielding....')
            self.flatimages = flatfield.FlatImages()
            return

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

        # Prep
        illum_image_files, self.master_key_dict[
            'flat'] = self._prep_calibrations('illumflat')
        pixflat_image_files, self.master_key_dict[
            'flat'] = self._prep_calibrations('pixelflat')

        masterframe_filename = masterframe.construct_file_name(
            flatfield.FlatImages,
            self.master_key_dict['flat'],
            master_dir=self.master_dir)
        # The following if-elif-else does:
        #   1.  Try to load a MasterFrame (if reuse_masters is True).  If successful, pass it back
        #   2.  Build from scratch
        #   3.  Load any user-supplied images to over-ride any built

        # Load MasterFrame?
        if os.path.isfile(masterframe_filename) and self.reuse_masters:
            flatimages = flatfield.FlatImages.from_file(masterframe_filename)
            flatimages.is_synced(self.slits)
            # Load user defined files
            if self.par['flatfield']['pixelflat_file'] is not None:
                # Load
                msgs.info(
                    'Using user-defined file: {0}'.format('pixelflat_file'))
                with fits.open(self.par['flatfield']['pixelflat_file']) as hdu:
                    nrm_image = flatfield.FlatImages(
                        pixelflat_norm=hdu[self.det].data)
                    flatimages = flatfield.merge(flatimages, nrm_image)
            self.flatimages = flatimages
            # update slits
            self.slits.mask_flats(self.flatimages)
            return self.flatimages

        # Generate the image
        pixelflatImages, illumflatImages = None, None
        # Check if the image files are the same
        pix_is_illum = Counter(illum_image_files) == Counter(
            pixflat_image_files)
        if len(pixflat_image_files) > 0:
            pixel_flat = buildimage.buildimage_fromlist(
                self.spectrograph,
                self.det,
                self.par['pixelflatframe'],
                pixflat_image_files,
                dark=self.msdark,
                bias=self.msbias,
                bpm=self.msbpm)
            # Initialise the pixel flat
            pixelFlatField = flatfield.FlatField(pixel_flat, self.spectrograph,
                                                 self.par['flatfield'],
                                                 self.slits, self.wavetilts,
                                                 self.wv_calib)
            # Generate
            pixelflatImages = pixelFlatField.run(show=self.show)

        # Only build illum_flat if the input files are different from the pixel flat
        if (not pix_is_illum) and len(illum_image_files) > 0:
            illum_flat = buildimage.buildimage_fromlist(
                self.spectrograph,
                self.det,
                self.par['illumflatframe'],
                illum_image_files,
                dark=self.msdark,
                bias=self.msbias,
                bpm=self.msbpm)
            # Initialise the pixel flat
            illumFlatField = flatfield.FlatField(illum_flat,
                                                 self.spectrograph,
                                                 self.par['flatfield'],
                                                 self.slits,
                                                 self.wavetilts,
                                                 self.wv_calib,
                                                 spat_illum_only=True)
            # Generate
            illumflatImages = illumFlatField.run(show=self.show)

        # Merge the illum flat with the pixel flat
        if pixelflatImages is not None:
            # Combine the pixelflat and illumflat parameters into flatimages.
            # This will merge the attributes of pixelflatImages that are not None
            # with the attributes of illflatImages that are not None. Default is
            # to take pixelflatImages.
            flatimages = flatfield.merge(pixelflatImages, illumflatImages)
        else:
            # No pixel flat, but there might be an illumflat. This will mean that
            # the attributes prefixed with 'pixelflat_' will all be None.
            flatimages = illumflatImages

        # Save flat images
        if flatimages is not None:
            flatimages.to_master_file(masterframe_filename)
            # Save slits too, in case they were tweaked
            self.slits.to_master_file()

        # 3) Load user-supplied images
        #  NOTE:  This is the *final* images, not just a stack
        #  And it will over-ride what is generated below (if generated)
        if self.par['flatfield']['pixelflat_file'] is not None:
            # Load
            msgs.info('Using user-defined file: {0}'.format('pixelflat_file'))
            with fits.open(self.par['flatfield']['pixelflat_file']) as hdu:
                flatimages = flatfield.merge(
                    flatimages,
                    flatfield.FlatImages(pixelflat_norm=hdu[self.det].data))

        self.flatimages = flatimages
        # Return
        return self.flatimages
Ejemplo n.º 8
0
    def get_flats(self):
        """
        Load or generate a normalized pixel flat and slit illumination
        flat.

        Requires :attr:`slits`, :attr:`wavetilts`, :attr:`det`,
        :attr:`par`.

        Returns:
            :class:`pypeit.flatfield.FlatImages`:
        """
        # Check for existing data
        if not self._chk_objs(['msarc', 'msbpm', 'slits', 'wv_calib']):
            msgs.warn(
                'Must have the arc, bpm, slits, and wv_calib defined to make flats!  Skipping and may crash down the line'
            )
            self.flatimages = flatfield.FlatImages(None, None, None, None)
            return

        # Slit and tilt traces are required to flat-field the data
        if not self._chk_objs(['slits', 'wavetilts']):
            # TODO: Why doesn't this fault?
            msgs.warn(
                'Flats were requested, but there are quantities missing necessary to '
                'create flats.  Proceeding without flat fielding....')
            self.flatimages = flatfield.FlatImages(None, None, None, None)
            return

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

        # Prep
        illum_image_files, self.master_key_dict[
            'flat'] = self._prep_calibrations('illumflat')
        pixflat_image_files, self.master_key_dict[
            'flat'] = self._prep_calibrations('pixelflat')

        masterframe_filename = masterframe.construct_file_name(
            flatfield.FlatImages,
            self.master_key_dict['flat'],
            master_dir=self.master_dir)
        # The following if-elif-else does:
        #   1.  Try to load a MasterFrame (if reuse_masters is True).  If successful, pass it back
        #   2.  Build from scratch
        #   3.  Load any user-supplied images to over-ride any built

        # Load MasterFrame?
        if os.path.isfile(masterframe_filename) and self.reuse_masters:
            self.flatimages = flatfield.FlatImages.from_file(
                masterframe_filename)
            self.flatimages.is_synced(self.slits)
            self.slits.mask_flats(self.flatimages)
            return self.flatimages

        # TODO -- Allow for separate pixelflat and illumflat images
        # Generate the image
        stacked_flat = None
        if len(illum_image_files) > 0:
            stacked_flat = buildimage.buildimage_fromlist(
                self.spectrograph,
                self.det,
                self.par['illumflatframe'],
                illum_image_files,
                dark=self.msdark,
                bias=self.msbias,
                bpm=self.msbpm)
        if stacked_flat is None and len(pixflat_image_files) > 0:
            stacked_flat = buildimage.buildimage_fromlist(
                self.spectrograph,
                self.det,
                self.par['pixelflatframe'],
                pixflat_image_files,
                dark=self.msdark,
                bias=self.msbias,
                bpm=self.msbpm)
        if stacked_flat is not None:
            # Create pixelflat and illumination flat from illumination flat stack
            flatField = flatfield.FlatField(stacked_flat, self.spectrograph,
                                            self.par['flatfield'], self.slits,
                                            self.wavetilts)
            # Run
            self.flatimages = flatField.run(show=self.show)

            # Save to Masters
            self.flatimages.to_master_file(masterframe_filename)
            # Save slits too, in case they were tweaked
            self.slits.to_master_file()
        else:
            self.flatimages = flatfield.FlatImages(None, None, None, None)

        # 3) Load user-supplied images
        #  NOTE:  This is the *final* images, not just a stack
        #  And it will over-ride what is generated below (if generated)
        if self.par['flatfield']['pixelflat_file'] is not None:
            # - Name is explicitly correct?
            # THIS IS DONE IN PYPEITPAR
            #if os.path.isfile(self.par['flatfield']['pixelflat_file']):
            #    flat_file = self.par['flatfield']['pixelflat_file']
            #else:
            #    msgs.error('Could not find user-defined flatfield file: {0}'.format(
            #        self.par['flatfield']['pixelflat_file']))
            # Load
            msgs.info('Using user-defined file: {0}'.format('pixelflat_file'))
            with fits.open(self.par['flatfield']['pixelflat_file']) as hdu:
                self.flatimages.pixelflat = hdu[self.det].data

        # Return
        return self.flatimages