def create(self):
        """MAIN FUNCTION"""

        # Loop over the yaml files and create
        # a direct seed image for each
        imseeds = []
        for pfile in self.paramfiles:
            print('Running catalog_seed_image for {}'.format(pfile))
            cat = catalog_seed_image.Catalog_seed(offline=self.offline)
            cat.paramfile = pfile
            cat.make_seed()
            imseeds.append(cat.seed_file)
            # If Mirage is going to produce an hdf5 file of spectra,
            # then we only need a single direct seed image.
            if self.create_continuum_seds:
                break

        # Create hdf5 file with spectra of all sources if requested.
        if self.create_continuum_seds:
            self.SED_file = spectra_from_catalog.make_all_spectra(
                self.catalog_files,
                input_spectra=self.SED_dict,
                input_spectra_file=self.SED_file,
                extrapolate_SED=self.extrapolate_SED,
                output_filename=self.final_SED_file,
                normalizing_mag_column=self.SED_normalizing_catalog_column)

        # Location of the configuration files needed for dispersion
        loc = os.path.join(
            self.datadir, "{}/GRISM_{}/".format(self.instrument,
                                                self.instrument.upper()))

        # Determine the name of the background file to use, as well as the
        # orders to disperse.
        if self.instrument == 'nircam':
            dmode = 'mod{}_{}'.format(self.module, self.dispersion_direction)
            background_file = ("{}_{}_back.fits".format(
                self.crossing_filter, dmode))
        elif self.instrument == 'niriss':
            dmode = 'GR150{}'.format(self.dispersion_direction)
            background_file = "{}_{}_medium_background.fits".format(
                self.crossing_filter.lower(), dmode.lower())
            print('Background file is {}'.format(background_file))

        # Default to extracting all orders
        orders = None

        # Create dispersed seed image from the direct images
        disp_seed = Grism_seed(imseeds,
                               self.crossing_filter,
                               dmode,
                               config_path=loc,
                               instrument=self.instrument.upper(),
                               extrapolate_SED=self.extrapolate_SED,
                               SED_file=self.SED_file,
                               SBE_save=self.source_stamps_file)
        disp_seed.observation(orders=orders)
        disp_seed.disperse(orders=orders)
        disp_seed.finalize(Back=background_file)

        # Get gain map
        gainfile = cat.params['Reffiles']['gain']
        gain, gainheader = self.read_gain_file(gainfile)

        # Disperser output is always full frame. Remove the signal from
        # the refrence pixels now since we know exactly where they are
        disp_seed.final[0:4, :] = 0.
        disp_seed.final[2044:, :] = 0.
        disp_seed.final[:, 0:4] = 0.
        disp_seed.final[:, 2044:] = 0.

        # Crop to the requested subarray if necessary
        if cat.params['Readout']['array_name'] not in self.fullframe_apertures:
            print("Subarray bounds: {}".format(cat.subarray_bounds))
            print("Dispersed seed image size: {}".format(
                disp_seed.final.shape))
            disp_seed.final = self.crop_to_subarray(disp_seed.final,
                                                    cat.subarray_bounds)
            gain = self.crop_to_subarray(gain, cat.subarray_bounds)

            # Segmentation map will be centered in a frame that is larger
            # than full frame by a factor of sqrt(2), so crop appropriately
            print("Need to make this work for subarrays...")
            segy, segx = cat.seed_segmap.shape
            dx = int((segx - 2048) / 2)
            dy = int((segy - 2048) / 2)
            segbounds = [
                cat.subarray_bounds[0] + dx, cat.subarray_bounds[1] + dy,
                cat.subarray_bounds[2] + dx, cat.subarray_bounds[3] + dy
            ]
            cat.seed_segmap = self.crop_to_subarray(cat.seed_segmap, segbounds)

        # Save the dispersed seed image if requested
        # Save in units of e/s, under the idea that this should be a
        # "perfect" noiseless view of the scene that does not depend on
        # detector effects, such as gain.
        if self.save_dispersed_seed:
            self.save_dispersed_seed_image(disp_seed.final)

        # Convert seed image to ADU/sec to be consistent
        # with other simulator outputs
        disp_seed.final /= gain

        # Update seed image header to reflect the
        # division by the gain
        cat.seedinfo['units'] = 'ADU/sec'

        # Prepare dark current exposure if
        # needed.
        if self.override_dark is None:
            d = dark_prep.DarkPrep(offline=self.offline)
            d.paramfile = self.wfss_yaml
            d.prepare()
            obslindark = d.prepDark
        else:
            self.read_dark_product()
            obslindark = self.darkPrep

        # Using the first of the imaging seed image yaml
        # files as a base, adjust to create the yaml file
        # for the creation of the final dispersed
        # integration

        # I think we won't need this anymore assuming that
        # one of the input yaml files is for wfss mode
        #y = yaml_update.YamlUpdate()
        #y.file = self.paramfiles[0]
        #if self.instrument == 'nircam':
        #    y.filter = self.crossing_filter
        #    y.pupil = 'GRISM' + self.direction
        #elif self.instrument == 'niriss':
        #    y.filter = 'GR150' + self.direction
        #    y.pupil = self.crossing_filter
        #y.outname = ("wfss_dispersed_{}_{}.yaml"
        #             .format(dmode, self.crossing_filter))
        #y.run()

        # Combine into final observation
        obs = obs_generator.Observation(offline=self.offline)
        obs.linDark = obslindark
        obs.seed = disp_seed.final
        obs.segmap = cat.seed_segmap
        obs.seedheader = cat.seedinfo
        #obs.paramfile = y.outname
        obs.paramfile = self.wfss_yaml
        obs.create()
Beispiel #2
0
    def create(self):
        # Make sure inputs are correct
        self.check_inputs()

        # Loop over the yaml files and create
        # a direct seed image for each
        imseeds = []
        # Create imaging seed images
        for pfile in self.paramfiles:
            cat = catalog_seed_image.Catalog_seed()
            cat.paramfile = pfile
            cat.make_seed()
            imseeds.append(cat.seed_file)

        # Create dispersed seed image from
        # the direct images
        dmode = 'mod{}_{}'.format(self.module,self.direction)
        loc = os.path.join(self.datadir,"nircam/GRISM_NIRCAM/")
        background_file = ("{}_{}_back.fits"
                           .format(self.crossing_filter,dmode))
        disp_seed = Grism_seed(imseeds, self.crossing_filter,
                               dmode, config_path=loc,
                               extrapolate_SED=self.extrapolate_SED)
        disp_seed.observation()
        disp_seed.finalize(Back = background_file)

        # Get gain map
        gainfile = cat.params['Reffiles']['gain']
        gain, gainheader = self.read_gain_file(gainfile)

        # Disperser output is always full frame. Crop to the
        # requested subarray if necessary
        if cat.params['Readout']['array_name'] not in self.fullframe_apertures:
            print("Subarray bounds: {}".format(cat.subarray_bounds))
            print("Dispersed seed image size: {}".format(disp_seed.final.shape))
            disp_seed.final = self.crop_to_subarray(disp_seed.final, cat.subarray_bounds)
            gain = self.crop_to_subarray(gain, cat.subarray_bounds)
            # Segmentation map will be centered in a frame that is larger
            # than full frame by a factor of sqrt(2), so crop appropriately
            segy, segx = cat.seed_segmap.shape
            dx = int((segx - 2048) / 2)
            dy = int((segy - 2048) / 2)
            segbounds = [cat.subarray_bounds[0] + dx, cat.subarray_bounds[1] + dy,
                         cat.subarray_bounds[2] + dx, cat.subarray_bounds[3] + dy]
            cat.seed_segmap = self.crop_to_subarray(cat.seed_segmap, segbounds)

        # Convert seed image to ADU/sec to be consistent
        # with other simulator outputs
        disp_seed.final /= gain

        # Update seed image header to reflect the
        # division by the gain
        cat.seedinfo['units'] = 'ADU/sec'

        # Save the dispersed seed image
        if self.save_dispersed_seed:
            hh00 = fits.PrimaryHDU()
            hh11 = fits.ImageHDU(disp_seed.final)
            hhll = fits.HDUList([hh00,hh11])
            hhll[0].header['units'] = 'ADU/sec'
            if self.disp_seed_filename is None:
                pdir, pf = os.path.split(self.paramfiles[0])
                dname = 'dispersed_seed_image_for_' + pf + '.fits'
                self.disp_seed_filename = os.path.join(pdir, dname)
            hhll.writeto(self.disp_seed_filename, overwrite=True)
            print(("Dispersed seed image saved to {}"
                   .format(self.disp_seed_filename)))

        # Prepare dark current exposure if
        # needed.
        if self.override_dark is None:
            d = dark_prep.DarkPrep()
            d.paramfile = self.paramfiles[0]
            d.prepare()
            obslindark = d.prepDark
        else:
            self.read_dark_product(self.override_dark)
            obslindark = self.prepDark

        # Using the first of the imaging seed image yaml
        # files as a base, adjust to create the yaml file
        # for the creation of the final dispersed
        # integration
        y = yaml_update.YamlUpdate()
        y.file = self.paramfiles[0]
        y.filter = self.crossing_filter
        y.pupil = 'GRISM' + self.direction
        y.outname = ("wfss_dispersed_{}_{}.yaml"
                     .format(dmode,self.crossing_filter))
        y.run()

        # Combine into final observation
        obs = obs_generator.Observation()
        obs.linDark = obslindark
        obs.seed = disp_seed.final
        obs.segmap = cat.seed_segmap
        obs.seedheader = cat.seedinfo
        obs.paramfile = y.outname
        obs.create()