Beispiel #1
0
    def _get_dpp_list(self, inima, confterm, back):
        """Determine the name of all DPP files"""
        DPP_list = []

        # generate the input list for aXe
        axe_inputs = axeinputs.aXeInput(inima, confterm)

        # go over the list of all inputs
        for an_input in axe_inputs:
            # load the configuration file
            conf = configfile.ConfigFile(
                config_util.getCONF(an_input['config']))

            # get the image extensions
            ext_info = config_util.get_ext_info(
                config_util.getDATA(an_input['grisim']), conf)

            # get the name of all axe files
            axe_names = config_util.get_axe_names(an_input['grisim'], ext_info)

            # if requested,
            # append the background DPP file
            if back:
                DPP_list.append(axe_names['BCK_DPP'])
            else:
                # append the 'normal' DPP name to the list
                DPP_list.append(axe_names['DPP'])

        # return the DPP list
        return DPP_list
Beispiel #2
0
    def _check_dpps(self, back=False):
        # go over all inputs
        for one_input in self.axe_inputs:
            # load the config file and get the extension information
            conf = configfile.ConfigFile(
                config_util.getCONF(one_input['config']))
            ext_info = config_util.get_ext_info(
                config_util.getDATA(one_input['grisim']), conf)

            # derive the aXe names
            axe_names = config_util.get_axe_names(one_input['grisim'],
                                                  ext_info)
            # check the DPP file
            if not os.path.isfile(config_util.getOUTPUT(axe_names['DPP'])):
                # error and out
                err_msg = (
                    f"{self.taskname}: The DPP file: {config_util.getOUTPUT(axe_names['DPP'])}"
                    f" does not exist!")

                raise aXeError(err_msg)

            # check for the background DPP file
            if back and not os.path.isfile(
                    config_util.getOUTPUT(axe_names['BCK_DPP'])):
                # error and out
                err_msg = (
                    f"{self.taskname}: The DPP file: {config_util.getOUTPUT(axe_names['BCK_DPP'])}"
                    f" does not exist!")
                raise aXeError(err_msg)
Beispiel #3
0
    def _make_drzgeocont(self, ext_info):
        # get the aXe names
        axe_names = config_util.get_axe_names(self.grisim, ext_info)

        # for the name of a special contamination OAF
        cont_oaf = config_util.getOUTPUT(axe_names['OAF'].replace(
            '.OAF', '_{0:s}.OAF'.format(int(self.params['drzfwhm'] * 10.0))))

        # run GOL2AF,
        # getting the special OAF as output
        axetasks.gol2af(grism=self.grisim,
                        config=self.config,
                        mfwhm=self.params['drzfwhm'],
                        back=False,
                        orient=self.params['orient'],
                        slitless_geom=self.params['slitless_geom'],
                        exclude=self.params['exclude'],
                        lambda_mark=self.params['lambda_mark'],
                        dmag=self.dmag,
                        out_af=cont_oaf,
                        in_gol=None)

        # run PETCONT,
        # using the special OAF as input
        axetasks.petcont(grism=self.grisim,
                         config=self.config,
                         cont_model=self.params['cont_model'],
                         model_scale=self.params['model_scale'],
                         spec_models=None,
                         object_models=None,
                         inter_type=self.params['inter_type'],
                         lambda_psf=self.params['lambda_psf'],
                         cont_map=True,
                         in_af=cont_oaf)
Beispiel #4
0
    def _check_fluxcubes(self):
        # go over all inputs
        for one_input in self.axe_inputs:

            # load the config file and get the extension information
            conf = configfile.ConfigFile(config_util.getCONF(one_input['config']))
            ext_info = config_util.get_ext_info(config_util.getDATA(one_input['grisim']), conf)

            # derive the aXe names
            axe_names = config_util.get_axe_names(one_input['grisim'], ext_info)

            # check the fluxcube
            if not os.path.isfile(config_util.getDATA(axe_names['FLX'])):
                # error and out
                err_msg = ("{0:s}: The fluxcube file: {1:s} does not exist!"
                           .format(self.taskname,
                                   config_util.getDATA(axe_names['FLX'])))
                raise aXeError(err_msg)
Beispiel #5
0
    def _subtract_wfc3irsky(self, ext_info):
        """Special sky subtraction for WFC3 IR images"""
        # get the axe names
        axe_names = config_util.get_axe_names(self.grisim, ext_info)

        # check for a previous background subtraction
        try:
            fits.getval(self.grisim, 'AXEPRBCK', exten=ext_info['fits_ext'])
            _log.info(
                f"WARNING: Image {self.grisim} seems to be already background "
                "subtracted! Continuing anyways...")
        except KeyError:
            _log.info(
                "Previous subtraction not recorded, proceeding with background subtraction."
            )

        scalebck = axelowlev.aXe_SCALEBCK(
            os.path.split(self.grisim)[-1],
            os.path.split(axe_names['MSK'])[-1],
            os.path.split(self.config_name)[-1],
            os.path.split(self.master_bck)[-1])
        try:
            scalebck.runall()
        except aXeError:
            _log.info("There was a problem with the background subtraction, "
                      "continuing without it")
            return False

        # check whether the background image exists
        bckfilename = config_util.getOUTPUT(axe_names['SGRI'])

        if not os.path.isfile(bckfilename):
            err_msg = ("The background image: {0:s} does NOT exist!".format(
                bckfilename))
            raise aXeError(err_msg)

        fits_image = fits.open(bckfilename, ext=0, mode='readonly')
        sky_frac = fits_image[0].header["FRACFIN"]
        scal_val = fits_image[0].header["SCALVAL"]
        bck_data = fits_image[1].data
        fits_image.close()

        if sky_frac < 0.1:
            _log.info(
                "Low fraction of sky pixels found (<10%) continuing WITHOUT"
                " sky subtraction")
            return False

        # Subtract the scaled background image from the grism image
        grism_file = fits.open(self.grisim, mode='update')
        grism_file[ext_info['ext_version']].data -= bck_data
        grism_header = grism_file[ext_info['fits_ext']].header

        # write some information into the
        # grism image header
        grism_header['AXEPRBCK'] = (
            'Done', 'flag that background subtraction was done')
        grism_header['SKY_IMG'] = (self.master_bck,
                                   'name of the 1st master background image')

        # write some scaling information into the header
        grism_header['F_SKYPIX'] = (float(sky_frac),
                                    'fraction of pixels used for scaling')
        grism_header['SKY_CPS'] = (
            float(scal_val), 'scale used for master sky == sky value [cps]')
        # close the grism image and sacve and the scaled image
        grism_file.close()
        return True
Beispiel #6
0
    def _subtract_nicsky(self, ext_info):
        """Special sky subtraction for NICMOS images"""
        # get the axe names
        axe_names = config_util.get_axe_names(self.grisim, ext_info)

        # check for a previous background subtraction
        fits_img = fits.open(self.grisim, 'readonly')
        fits_head = fits_img[ext_info['fits_ext']].header
        # npix = int(fits_head['NAXIS1']) * int(fits_head['NAXIS1'])

        if 'AXEPRBCK' in fits_head:
            # warn that this is the second time
            _log.info(
                f"WARNING: Image {self.grisim} seems to be already background "
                "subtracted!")

        # close the fits
        fits_img.close()

        # do the special background fitting for NICMOS
        if self.params['backped'] is not None:
            nicback = axelowlev.aXe_NICBACK(self.grisim, self.config_name,
                                            self.master_bck,
                                            self.params['backped'])
        else:
            nicback = axelowlev.aXe_NICBACK(self.grisim, self.config_name,
                                            self.master_bck)
        nicback.runall()
        del nicback

        # check whether the background image exists
        if not os.path.isfile(config_util.getOUTPUT(axe_names['NBCK'])):
            err_msg = ("The background image: {0:s} does NOT exist!".format(
                config_util.getOUTPUT(axe_names['NBCK'])))
            raise aXeError(err_msg)

        # Subtract the scaled background image from the grism image
        # copy the image to the output directory first
        sci_file = fits.open(self.grisim, mode='update')
        bck_file = fits.open(config_util.getOUTPUT(axe_names['NBCK']),
                             'readonly')
        sci_file['SCI', ext_info['ext_version']].data -= bck_file[1].data
        sci_file.close()
        bck_file.close()

        # open the background image
        fits_img = fits.open(config_util.getOUTPUT(axe_names['NBCK']),
                             'readonly')
        fits_head = fits_img['BCK'].header

        # open the grism image and isolate the correct extension header
        grism_img = fits.open(self.grisim, mode='update')
        grism_header = grism_img[ext_info['fits_ext']].header

        if 'SKY_SCAL' in fits_head and 'F_SKYPIX' in fits_head:

            # transfer important keywords
            # to the grism image
            grism_header['SKY_SCAL'] = (float(fits_head['SKY_SCAL']),
                                        'scaling value of background')
            grism_header['F_SKYPIX'] = (float(fits_head['F_SKYPIX']),
                                        'fraction of pixels used for scaling')

        # close the fits again
        fits_img.close()

        # write some keywords
        grism_header['AXEPRBCK'] = (
            'Done', 'flag that background subtraction was done')
        grism_header['SKY_IMG'] = (self.master_bck,
                                   'name of the 1st master background image')
        if self.params['backped'] is not None:
            grism_header['SKY_IMG2'] = (
                self.params['backped'],
                'name of the 2nd master background image')

        # close the image again
        grism_img.close()

        return True
Beispiel #7
0
    def _subtract_sky(self, ext_info, flag=-1.0e10):
        """Perform a classical background subtraction."""

        # Derive the name of all aXe products for a given image
        axe_names = config_util.get_axe_names(self.grisim, ext_info)
        msk_image_sc = axe_names['MSK'] + '[SCI]'

        # check for a previous background subtraction
        with fits.open(self.grisim, mode='update') as grism_file:
            if 'AXEPRBCK' in grism_file[ext_info['fits_ext']].header:
                # warn that this is the second time
                _log.info("WARNING: Image %25s seems to be already background "
                          "subtracted!".format(self.grisim))

            # Compute the ratio of the grism SCI image to the background image
            sci_data = grism_file['SCI', ext_info['ext_version']].data
            sci_header = grism_file['SCI', ext_info['ext_version']].header
            npix = int(sci_header["NAXIS1"]) * int(sci_header["NAXIS2"])

            bck_data = fits.getdata(self.master_bck)
            ratio_data = sci_data / bck_data

            # Flag pixels in the ratio image based on the grism image DQ array
            grism_dq_data = grism_file['DQ', ext_info['ext_version']].data
            ratio_data[grism_dq_data > 0.5] = flag

            # Flag pixels in the ratio image based on the grism image MSK file
            msk_file = fits.open(
                config_util.getOUTPUT(msk_image_sc.split("[")[0]), 'readonly')
            msk_data = msk_file['SCI'].data
            msk_file.close()

            ratio_data[msk_data < -900000] = flag

            # Flag pixels in the background image based on the grism image DQ
            # and MSK file
            bck_data[grism_dq_data > 0.5] = flag
            bck_data[msk_data < -900000] = flag

            # Compute stats for the ratio image
            stats = imagestats.ImageStats(ratio_data[ratio_data > flag],
                                          fields='midpt,stddev,npix',
                                          lower=None,
                                          upper=None,
                                          nclip=3,
                                          lsig=3.0,
                                          usig=3.0,
                                          binwidth=0.01)

            # Compute stats for the background image
            bstats = imagestats.ImageStats(bck_data[bck_data > flag],
                                           fields='midpt,stddev,npix',
                                           lower=None,
                                           upper=None,
                                           nclip=3,
                                           lsig=3.0,
                                           usig=3.0,
                                           binwidth=0.01)

            # Subtract the scaled background from the grism image
            # Reload a clean version of background
            bck_data = fits.getdata(self.master_bck)

            grism_file['SCI',
                       ext_info['ext_version']].data -= bck_data * stats.midpt
            grism_header = grism_file['SCI', ext_info['ext_version']].header

            # write some header iformation
            grism_header['SKY_SCAL'] = (float(
                stats.midpt), 'scaling value for the master background')
            grism_header['SKY_MAST'] = (float(
                bstats.midpt), 'average value of the master background')
            grism_header['SKY_IMG'] = (self.master_bck,
                                       'name of the master background image')
            grism_header['F_SKYPIX'] = (float(stats.npix) / float(npix),
                                        'fraction of pixels used for scaling')
            grism_header['AXEPRBCK'] = (
                'Done', 'flag that background subtraction was done')
        return 0