Пример #1
0
def f_add_aper_erm(input_path,
                   input_list,
                   sel_apertures,
                   dither_idx=0,
                   verbose=False):
    '''
        Utility function to add the apertures (e.g. slit, mos) of a component (e.g. background)
        :param input_path:
        :param list input_list: 2-dimensional list of filenames with indices [aperture][dither]
        :param sel_apertures:
        :param dither_idx:
        :param verbose:
        :return:
        '''
    #this function is only applicable to background
    # and flatfield components, which depend on apertures
    #add all aperture components to the erm file
    #TODO: introduce logic to have always a full input component list and select by string, by checking the components in the global list
    for aperture_idx in range(len(sel_apertures)):
        filename = input_list[dither_idx][aperture_idx]
        if aperture_idx == 0:
            comp_erm = c_electronratemap.ElectronRateMap()
            comp_erm.m_read_from_fits(os.path.join(input_path, filename))
            if verbose:
                print('Standard component: {:s}'.format(
                    sel_apertures[aperture_idx]))
        else:
            work_erm = c_electronratemap.ElectronRateMap()
            work_erm.m_read_from_fits(os.path.join(input_path, filename))
            comp_erm.m_add(work_erm)
            if verbose:
                print('Adding :{:s}'.format(
                    input_list[dither_idx][aperture_idx]))

    return comp_erm
Пример #2
0
multiaccum.m_info()

# =======================================================================
# Generating the instances of the reference files (nrspydet.references)
# =======================================================================
print("# Generating the instances of the reference files (gain & IPC).")
gain = fpa106_toolbox.f_generate_gain('FULL-FRAME')
ctm = fpa106_toolbox.f_generate_ctm()

print("# Generating the count-rate maps.")
# Looping over dither pointings (all unity-maps)
count = 0
for i in range(len(mos_erm)):
    t_map = mos_erm[i]
    print('# Processing ' + t_map)
    t_erm = c_electronratemap.ElectronRateMap()
    t_erm.m_read_from_fits(os.path.join(input_path, t_map))

    s_erm = c_electronratemap.ElectronRateMap()
    s_erm.m_read_from_fits(os.path.join(input_path, fxs_erm[i]))

    # Adding slit and mos flats together
    t_erm.m_add(s_erm)
    # -------------------------------------------------------------------
    # Generating the exposure folders and the associated exposures - noiseless!
    # -------------------------------------------------------------------

    #Noiseless Unity - ** OLD ISP format **
    unity_ctms = erm_to_sci.f_standard(t_erm,
                                       multiaccum,
                                       noise_category='noiseless',
Пример #3
0
        print('ERROR - {}'.format(error.args))
        raise ValueError

b_ctms = None
for dither_index in range(4):
    # -------------------------------------------------------------------
    # Name of the various files
    # -------------------------------------------------------------------
    g_ifs = list_galaxy[dither_index]
    b_ifs = list_background[dither_index][0]
    b_mos = list_background[dither_index][1]
    b_slit = list_background[dither_index][2]
    # -------------------------------------------------------------------
    # Background ERM
    # -------------------------------------------------------------------
    b_erm = c_electronratemap.ElectronRateMap()
    b_erm.m_read_from_fits(os.path.join(input_path, b_ifs))
    # b_erm.m_display(491, display=False, filename=os.path.join(output_path, 'bsifs.pdf'))
    work = c_electronratemap.ElectronRateMap()
    work.m_read_from_fits(os.path.join(input_path, b_mos))
    # work.m_display(491, display=False, filename=os.path.join(output_path, 'bsmos.pdf'))
    b_erm.m_add(work, overwrite=False)
    # b_erm.m_display(491, display=False, filename=os.path.join(output_path, 'bsifsmos.pdf'))
    work = c_electronratemap.ElectronRateMap()
    work.m_read_from_fits(os.path.join(input_path, b_slit))
    # work.m_display(491, display=False, filename=os.path.join(output_path, 'bslit.pdf'))
    b_erm.m_add(work)
    # b_erm.m_display(491, display=False, filename=os.path.join(output_path, 'bsifsmosslit.pdf'))
    # -------------------------------------------------------------------
    # Galaxy ERM
    # -------------------------------------------------------------------
Пример #4
0
def f_crm(input_path,
          input_list,
          FWA,
          GWA,
          sampling_grid,
          components,
          sel_apertures,
          nexp,
          multiaccum,
          references,
          output_path,
          base_nid,
          pipeline_id,
          jlab_id,
          base_obs_id,
          datetime_start,
          datetime_end,
          datetime_file,
          daily_folder,
          save_background=True):
    '''
        Basic (for now NIPS centric and commissioning specific (nexp~=1)) function to take all basic ingredients and
        perform
        - transform to count-rate-maps
        - deal with dither, nods and (potentially) exposures
        - create the archive structure, count-rate filenames and NID identifier
        - generate background exposure (save_background=True/False is hardcoded)
        - Include realistic dark flags
        - Populate GWA information (current workaround IPS>4.0 should take care of this)
        - Write the .fits files
        
        Note: Currently NIPS-centric functions are embedded in code #TODO: extract into functions
        
        :param input_path:
        :param input_list:
        :param FWA:
        :param GWA:
        :param sampling_grid:
        :param components:
        :param sel_apertures:
        :param nexp:
        :param multiaccum:
        :param references:
        :param output_path:
        :param base_nid:
        :param pipeline_id:
        :param jlab_id:
        :param base_obs_id:
        :param datetime_start:
        :param datetime_end:
        :param datetime_file:
        :param daily_folder:
        :param save_background:
        :return:
        '''

    #TODO: Idea, maybe promote this function upward to f_post_processing body, as it needs yet more parameters
    # =======================================================================
    # NIPS Archive structure: daily folder generation
    # =======================================================================
    work = os.path.join(output_path, daily_folder)
    try:
        os.makedirs(work)
    except Exception as error:
        if (error.args[0] != 17):
            print(
                'ERROR - Encountered an error when trying to create the daily folder:'
            )
            print('ERROR - {:s}'.format(work))
            print('ERROR - {}'.format(error.args))
            raise ValueError

    # =======================================================================
    # Generating the count-rate maps
    # =======================================================================
    print("# Generating the count-rate maps.")

    dither_pts = sampling_grid['dither_pts']
    nod_pts = sampling_grid['nod_pts']

    target_list = input_list['target_erm']
    if 'background' in components: bckg_list = input_list['bckg_erm']

    for dither_idx in range(dither_pts):
        if 'background' in components:
            # -------------------------------------------------------------------
            # Background ERM: we need to have a background to add, even if it is the same
            # -------------------------------------------------------------------
            b_erm = f_add_aper_erm(input_path,
                                   bckg_list,
                                   sel_apertures,
                                   dither_idx=dither_idx,
                                   verbose=True)

        for nod_idx in range(nod_pts):

            o_name = target_list[dither_idx][nod_idx]
            # -------------------------------------------------------------------
            # Target/object ERM
            # -------------------------------------------------------------------
            o_erm = c_electronratemap.ElectronRateMap()
            o_erm.m_read_from_fits(os.path.join(input_path, o_name))

            if 'background' in components:
                # -------------------------------------------------------------------
                # Obj ERM + Background ERM
                # -------------------------------------------------------------------
                o_erm.m_add(b_erm)
                # -------------------------------------------------------------------
                # Save first background ERM (mimmic off-source exposure for master background in IFS)
                # -------------------------------------------------------------------
                #TODO: boolean parameter save_background deeply buried in this function. Should be promoted to config file
                if save_background and (dither_idx == 0) and (nod_idx == 0):
                    b_ctms = erm_to_sci.f_standard(
                        b_erm,
                        multiaccum,
                        noise_category='extended',
                        noise_model=references.noise_model,
                        scale=1.0,
                        nexp=nexp,
                        planes=None,
                        bias=None,
                        use_superbias=False,
                        dark=references.dark,
                        dark_sub=True,
                        readout=references.readout,
                        gain=references.gain,
                        ctm=references.ctm,
                        verbose=True,
                        seed=-1,
                        force_trimming=False,
                        old=True)
                # -------------------------------------------------------------------
                # Generating archive folders
                # -------------------------------------------------------------------
                nid, obs_id, folder_name, filenames = f_output_archive_structure(
                    output_path,
                    dither_idx,
                    nod_idx,
                    base_nid,
                    pipeline_id,
                    jlab_id,
                    base_obs_id,
                    datetime_start,
                    datetime_end,
                    datetime_file,
                    daily_folder,
                    is_background=True)

                # -------------------------------------------------------------------
                # Write out to archive folders
                # -------------------------------------------------------------------
                f_write_to_fits(b_ctms, output_path, daily_folder, folder_name,
                                filenames, obs_id)

            # -------------------------------------------------------------------
            # Note: Just for NIPS2.0 and commissioning; otherwise introduce the
            # following for loop and change the f_standard parameter to nexp=1
            # GG: Generating the exposure folders and the associated exposures - 4 exposures for pointing
            # -------------------------------------------------------------------
            #for iexp in range(nexp):
            # GG: Note nexp in this function is only used to scale noise -> nexp=1

            o_ctms = erm_to_sci.f_standard(o_erm,
                                           multiaccum,
                                           noise_category='extended',
                                           noise_model=references.noise_model,
                                           scale=1.0,
                                           nexp=nexp,
                                           planes=None,
                                           bias=None,
                                           use_superbias=False,
                                           dark=references.dark,
                                           dark_sub=True,
                                           readout=references.readout,
                                           gain=references.gain,
                                           ctm=references.ctm,
                                           verbose=True,
                                           seed=-1,
                                           force_trimming=False,
                                           old=True)
            # -------------------------------------------------------------------
            # Generating archive folders
            # -------------------------------------------------------------------
            nid, obs_id, folder_name, filenames = f_output_archive_structure(
                output_path,
                dither_idx,
                nod_idx,
                base_nid,
                pipeline_id,
                jlab_id,
                base_obs_id,
                datetime_start,
                datetime_end,
                datetime_file,
                daily_folder,
                is_background=False)

            #TODO: Extract all below code into a NIPS specific function and include switch parameter in config file to branch off
            # -------------------------------------------------------------------
            # Populate header keywords: (specific to NIPS2.0)
            # - dark flags
            # - GWA information
            # -------------------------------------------------------------------
            flag_arrays = []

            # Reading in quality flags from one of our darks
            hdu = fits.open(_dark491fname)
            flag_arrays.append(hdu[3].data)
            print('# Reading in FLAG array 1')

            hdu = fits.open(_dark492fname)
            flag_arrays.append(hdu[3].data)
            print('# Reading in FLAG array 2')
            hdu.close()

            # Before writing out the cont-rate maps (new format for NIPS2.0) we need to update the GWA keywords because
            # the version of the IPS with which they were created did not populated the GWA keyword with the correct value
            # Therefore opening the model gtp file:

            #TODO: insert a try catch to skip this if IPS already populates it (not sure on which attribute)
            filename = _model_path + _model_name + '/Description/disperser_' + GWA + '_TiltY.gtp'
            poly = tiltpoly.TiltPoly()
            poly.m_readFromFile(filename)
            GWA_XTIL = poly.zeroreadings[0]  # dispersion direction

            filename = _model_path + _model_name + '/Description/disperser_' + GWA + '_TiltX.gtp'
            poly = tiltpoly.TiltPoly()
            poly.m_readFromFile(filename)
            GWA_YTIL = poly.zeroreadings[0]  # spatial direction

            mode_gg = 'unknown'
            slit_gg = 'unknown'
            aperture_gg = 'unknown'
            sca_ids = ['NRS1', 'NRS2']
            read_out = 'NRSRAPID'
            #TODO: Hardcoding warning: create a function of (readout_mode, nf) that spits out the cooked readout
            for o_idx in range(1, len(filenames) + 1):
                o_ctms[o_idx].fits_header['GWA_XTIL'] = GWA_XTIL
                o_ctms[o_idx].fits_header['GWA_YTIL'] = GWA_YTIL
                o_ctms[o_idx].m_set_keywords(nid, 'IPS', sca_ids[o_idx - 1],
                                             mode_gg, slit_gg, aperture_gg,
                                             FWA, GWA, read_out, False)
                #o_ctms[o_idx].quality = flag_arrays[o_idx-1]

            # -------------------------------------------------------------------
            # Write out to archive folders
            # We keep the old count-rate for NIPS 2.7 compatibility and plotting
            # -------------------------------------------------------------------
            f_write_to_fits(o_ctms, output_path, daily_folder, folder_name,
                            filenames, obs_id)

    return None
Пример #5
0
        raise ValueError

print("# Generating the count-rate maps.")
b_ctms = None

# Looping over dither pointings
for idither in range(3):

    # Reading in background maps
    b_mos = bkg_mos_erm[idither]
    b_slit = bkg_slit_erm[idither]

    # -------------------------------------------------------------------
    # Background ERM
    # -------------------------------------------------------------------
    b_erm = c_electronratemap.ElectronRateMap()
    b_erm.m_read_from_fits(os.path.join(input_path, b_mos))
    b_erm1 = c_electronratemap.ElectronRateMap()
    b_erm1.m_read_from_fits(os.path.join(input_path, b_slit))
    b_erm.m_add(b_erm1)

    # Looping over nodding positions
    for inod in range(3):
        # -------------------------------------------------------------------
        # Name of the various files
        # -------------------------------------------------------------------
        g_mos = target_erm[idither][inod]

        # Reading in targets e-/rate maps
        g_erm = c_electronratemap.ElectronRateMap()
        g_erm.m_read_from_fits(os.path.join(input_path, g_mos))