예제 #1
0
def survey_overlaps(tiledata):
    """
    Returns True for fibers that have MWS_ANY bit set in 
    DESI_TARGET and other DESI_TARGET bits set.
    """
    is_assigned = fibers_assigned(tiledata)

    # Define as having MWS_ANY and any bits other than MWS_ANY
    is_mws = (tiledata['DESI_TARGET'] & desi_mask.mask('MWS_ANY')) != 0
    is_other = (tiledata['DESI_TARGET'] & ~desi_mask.mask('MWS_ANY')) != 0
    return (is_assigned & is_mws & is_other)
예제 #2
0
    def test_numobs(self):
        t = self.targets

        # - No target bits set is an error
        with self.assertRaises(ValueError):
            calc_numobs(t)

        # - ELGs and QSOs get one/four observations
        t['DESI_TARGET'] = desi_mask.ELG
        self.assertTrue(np.all(calc_numobs(t) == 1))
        t['DESI_TARGET'] = desi_mask.QSO
        self.assertTrue(np.all(calc_numobs(t) == 4))

        # ADM LRG NUMOBS is defined using per-pass target bits
        # ADM the desi_mask.LRG reference tests the default, which
        # ADM should correspond to 2 observations
        t['DESI_TARGET'] = [
            desi_mask.LRG, desi_mask.LRG_1PASS, desi_mask.LRG_2PASS,
            desi_mask.LRG_1PASS, desi_mask.LRG_2PASS
        ]
        nobs = calc_numobs(t)
        self.assertTrue(np.all(nobs == [2, 1, 2, 1, 2]))

        # - test astropy Table
        t = Table(t)
        nobs = calc_numobs(t)
        self.assertTrue(np.all(nobs == [2, 1, 2, 1, 2]))

        # - this is true even if other targeting bits are set
        t['DESI_TARGET'] |= desi_mask.mask('ELG|BGS_ANY')
        nobs = calc_numobs(t)
        self.assertTrue(np.all(nobs == [2, 1, 2, 1, 2]))
예제 #3
0
def print_efficiency_stats(truth, mtl_initial, zcat):
    print('Overall efficiency')
    tmp_init = join(mtl_initial, truth, keys='TARGETID')
    total = join(zcat, tmp_init, keys='TARGETID')

    true_types = ['LRG', 'ELG', 'QSO']
    zcat_types = ['GALAXY', 'GALAXY', 'QSO']

    for true_type, zcat_type in zip(true_types, zcat_types):
        i_initial = ((tmp_init['DESI_TARGET'] & desi_mask.mask(true_type)) != 0) & (tmp_init['TRUESPECTYPE'] == zcat_type)
        i_final = ((total['DESI_TARGET'] & desi_mask.mask(true_type)) != 0) & (total['SPECTYPE'] == zcat_type)
        n_t = 1.0*len(total['TARGETID'][i_final])
        n_i = 1.0*len(tmp_init['TARGETID'][i_initial])
        print("\t {} fraction : {}".format(true_type, n_t/n_i))
    #print("\t TRUE:ZCAT\n\t {}\n".format(Counter(zip(total['DESI_TARGET'], total['TYPE']))))
    return
예제 #4
0
def print_efficiency_stats(truth, mtl_initial, zcat):
    print('Overall efficiency')
    tmp_init = join(mtl_initial, truth, keys='TARGETID')
    total = join(zcat, tmp_init, keys='TARGETID')

    true_types = ['LRG', 'ELG', 'QSO']
    zcat_types = ['GALAXY', 'GALAXY', 'QSO']

    for true_type, zcat_type in zip(true_types, zcat_types):
        i_initial = ((tmp_init['DESI_TARGET'] & desi_mask.mask(true_type)) != 0) & (tmp_init['TRUESPECTYPE'] == zcat_type)
        i_final = ((total['DESI_TARGET'] & desi_mask.mask(true_type)) != 0) & (total['SPECTYPE'] == zcat_type)
        n_t = 1.0*len(total['TARGETID'][i_final])
        n_i = 1.0*len(tmp_init['TARGETID'][i_initial])
        print("\t {} fraction : {}".format(true_type, n_t/n_i))
    #print("\t TRUE:ZCAT\n\t {}\n".format(Counter(zip(total['DESI_TARGET'], total['TYPE']))))
    return
예제 #5
0
def global_eff(targets,
               id_avail,
               zcat,
               target_class='QSO',
               zcat_spectype='QSO',
               z_max=None,
               z_min=None):
    ii_avail = np.in1d(targets['TARGETID'], id_avail)
    targets_avail = targets[ii_avail]

    if z_max is None and z_min is None:
        sub_zcat = zcat.copy()
    elif (z_min is not None) or (z_max is not None):
        if z_max is not None:
            sub_zcat = zcat[zcat['Z'] < z_max]
        if z_min is not None:
            sub_zcat = zcat[zcat['Z'] > z_min]
    else:
        print("Error")
        sub_zcat = None

    # input target consistent with target_class
    is_class = (targets_avail['DESI_TARGET']
                & desi_mask.mask(target_class)) != 0
    targets_avail_class = targets_avail[is_class]
    n_avail = len(targets_avail_class)

    # output in the redshift catalog consistent with truth_spectype
    sub_zcat_class = sub_zcat[sub_zcat['SPECTYPE'] == zcat_spectype]

    # keep the elements in the zcat that correspond to the correct input target class
    id_intersection = np.in1d(sub_zcat_class['TARGETID'],
                              targets_avail_class['TARGETID'])
    sub_zcat_class = sub_zcat_class[id_intersection]
    n_assigned = len(sub_zcat_class)

    nobs = dict()
    for i in range(10):
        nobs[i] = np.count_nonzero(sub_zcat_class['NUMOBS'] == i)
    nobs[0] = (n_avail - n_assigned)

    return {
        'target_class': target_class,
        'zcat_class': zcat_spectype,
        'eff': n_assigned / n_avail,
        'n_avail': n_avail,
        'n_assign': n_assigned,
        'n_obs': nobs
    }
예제 #6
0
def isStdStar(desi_target, bright=None):
    """
    Determines if target(s) are standard stars

    Args:
        desi_target: int or array of DESI_TARGET targeting bit mask(s)

    Optional:
        bright: if True, only bright time standards; if False, only darktime, otherwise both

    Returns bool or array of bool

    TODO: move out of scripts/stdstars.py
    """
    from desitarget.targetmask import desi_mask
    yes = (desi_target & desi_mask.STD_WD) != 0
    if bright is None:
        yes |= (desi_target & desi_mask.mask('STD_WD|STD_FAINT|STD_BRIGHT')) != 0
    elif bright:
        yes |= (desi_target & desi_mask.mask('STD_WD|STD_BRIGHT')) != 0
    else:
        yes |= (desi_target & desi_mask.mask('STD_WD|STD_FAINT')) != 0

    return yes
예제 #7
0
def write_initial_truth_file(initial_truth_file):
    import desitarget.mock.mockmaker as mb
    from desitarget.targetmask import desi_mask, bgs_mask, mws_mask
    pixweight_file = "/project/projectdirs/desi/target/catalogs/dr8/0.31.1/pixweight/pixweight-dr8-0.31.1.fits"

    is_lya_qso = assign_lya_qso(initial_mtl_file, pixweight_file)
    
    targets = Table.read(initial_mtl_file)
    colnames = list(targets.dtype.names)
    print(colnames)
    nobj = len(targets)
    truth = mb.empty_truth_table(nobj=nobj)[0]
    print(truth.keys())

    for k in colnames:
        if k in truth.keys():
            print(k)
            truth[k][:] = targets[k][:]

    nothing = '          '
    truth['TEMPLATESUBTYPE'] = np.repeat(nothing, nobj)

    masks = ['MWS_ANY', 'BGS_ANY', 'STD_FAINT', 'STD_BRIGHT','ELG', 'LRG', 'QSO', ]
    dict_truespectype = {'BGS_ANY':'GALAXY', 'ELG':'GALAXY', 'LRG':'GALAXY', 'QSO':'QSO', 
                    'MWS_ANY':'STAR', 'STD_FAINT':'STAR', 'STD_BRIGHT':'STAR'}
    dict_truetemplatetype = {'BGS_ANY':'BGS', 'ELG':'ELG', 'LRG':'LRG', 'QSO':'QSO', 
                        'MWS_ANY':'STAR', 'STD_FAINT':'STAR', 'STD_BRIGHT':'STAR'}
    dict_truez = {'BGS_ANY':0.2, 'ELG':1.5, 'LRG':0.7, 'QSO':2.0, 
                        'MWS_ANY':0.0, 'STD_FAINT':0.0, 'STD_BRIGHT':0.0}

    for m in masks:
        istype = (targets['DESI_TARGET'] & desi_mask.mask(m))!=0
        print(m, np.count_nonzero(istype))
        truth['TRUESPECTYPE'][istype] = np.repeat(dict_truespectype[m], np.count_nonzero(istype))
        truth['TEMPLATETYPE'][istype] = np.repeat(dict_truetemplatetype[m], np.count_nonzero(istype))
        truth['MOCKID'][istype] = targets['TARGETID'][istype]
        truth['TRUEZ'][istype] = dict_truez[m]
        
    truth['TRUEZ'][is_lya_qso] = 3.0

    # Check that all targets have been assigned to a class
    iii = truth['MOCKID']==0
    assert np.count_nonzero(iii)==0
    
    print('writing truth')
    truth.write(initial_truth_file, overwrite=True)
    print('done truth')
예제 #8
0
def merge_results_tile(out_dtype, copy_fba, params):
    """Merge results for one tile.

    This uses target catalog data which has been pre-staged to shared memory.
    This function should not be called directly, but only from the
    merge_results() function.

    Args:
        out_dtype (np.dtype):  The output recarray dtype for the merged target
            HDU.  This is the union of columns chosen from the input catalogs.
        copy_fba (bool):  If True, copy the original raw fiberassign HDUs onto
            the end of the output file.
        params (tuple):  The tile ID and input / output files.  Set by
            multiprocessing call.
    Returns:
        None.

    """
    (tile_id, infile, outfile) = params
    log = Logger.get()

    log.info("Reading raw tile data {}".format(infile))

    tm = Timer()
    tm.start()

    inhead, fiber_data, targets_data, avail_data, gfa_targets = \
        read_assignment_fits_tile((tile_id, infile))

    # tm.stop()
    # tm.report("  read input data {}".format(tile_id))
    # tm.clear()
    # tm.start()

    # Get the list of all targets we are considering for this tile.  This
    # will be either just the assigned targets or all available targets
    # depending on how the user wrote the file.

    tile_tgids = np.copy(targets_data["TARGETID"])
    tile_tgindx = {y: x for x, y in enumerate(tile_tgids)}

    # Extract just these targets from the full set of catalogs

    tile_targets_dtype = np.dtype(out_dtype.fields)
    tile_targets = np.zeros(len(tile_tgids), dtype=tile_targets_dtype)

    # Copy original data

    for field in tile_targets_dtype.names:
        if field in results_targets_columns:
            tile_targets[field] = targets_data[field]

    # tm.stop()
    # tm.report("  index input targets {}".format(tile_id))
    # tm.clear()
    # tm.start()

    # Loop over input target files and copy data.  Note: these are guaranteed
    # to be sorted by TARGETID, since that is done during staging to shared
    # memory.
    targetfiles = list(merge_results_tile_tgbuffers.keys())
    for tf in targetfiles:
        tgview = np.frombuffer(merge_results_tile_tgbuffers[tf],
                               dtype=merge_results_tile_tgdtypes[tf])\
                               .reshape(merge_results_tile_tgshapes[tf])
        # Some columns may not exist in all target files (e.g. PRIORITY),
        # So we select the valid columns for this file and only copy those.
        # The ordering of the targets in the catalog is not guaranteed to be
        # sorted, and the output table is sorted by fiber ID, not target.  So
        # must build an explicit mapping from target catalog rows to output
        # table rows.
        tgids = tgview["TARGETID"]
        inrows = np.where(np.isin(tgids, tile_tgids, assume_unique=True))[0]
        outrows = np.where(np.isin(tile_tgids, tgids, assume_unique=True))[0]
        tfcolsin = list()
        tfcolsout = list()
        for c in tgview.dtype.names:
            nm = c
            if c in merged_fiberassign_swap:
                nm = merged_fiberassign_swap[c]
            if nm in out_dtype.names:
                tfcolsin.append(c)
                tfcolsout.append(nm)

        # tm.stop()
        # tm.report("  indexing {} for {}".format(tf, tile_id))
        # tm.clear()
        # tm.start()

        if len(outrows) > 0:
            for irw, orw in zip(inrows, outrows):
                for c, nm in zip(tfcolsin, tfcolsout):
                    tile_targets[nm][orw] = tgview[c][irw]
        del tgids
        del inrows
        del outrows
        del tgview

        # tm.stop()
        # tm.report("  copy targets from {} for {}".format(tf, tile_id))
        # tm.clear()
        # tm.start()

    skytargetfiles = list(merge_results_tile_skybuffers.keys())
    for tf in skytargetfiles:
        skyview = np.frombuffer(merge_results_tile_skybuffers[tf],
                                dtype=merge_results_tile_skydtypes[tf])\
                               .reshape(merge_results_tile_skyshapes[tf])
        # Some columns may not exist in all target files (e.g. PRIORITY),
        # So we select the valid columns for this file and only copy those.
        # The ordering of the targets in the catalog is not guaranteed to be
        # sorted, and the output table is sorted by fiber ID, not target.  So
        # must build an explicit mapping from target catalog rows to output
        # table rows.
        skyids = skyview["TARGETID"]
        inrows = np.where(np.isin(skyids, tile_tgids, assume_unique=True))[0]
        outrows = np.where(np.isin(tile_tgids, skyids, assume_unique=True))[0]
        tfcolsin = list()
        tfcolsout = list()
        for c in skyview.dtype.names:
            nm = c
            if c in merged_fiberassign_swap:
                nm = merged_fiberassign_swap[c]
            if nm in out_dtype.names:
                tfcolsin.append(c)
                tfcolsout.append(nm)

        if len(outrows) > 0:
            for irw, orw in zip(inrows, outrows):
                for c, nm in zip(tfcolsin, tfcolsout):
                    tile_targets[nm][orw] = skyview[c][irw]
        del skyids
        del inrows
        del outrows
        del skyview

    # Now we have a reduced set of target data including only the targets
    # relevant for this tile, and with data merged from all the files.
    # Next, merge this with the assignment information.

    # Determine the rows of the assignment that are for science and sky
    # monitor positioners.
    science_rows = np.where(fiber_data["DEVICE_TYPE"].astype(str) == "POS")[0]
    sky_rows = np.where(fiber_data["DEVICE_TYPE"].astype(str) == "ETC")[0]

    # Construct output recarray
    outdata = np.zeros(len(fiber_data), dtype=out_dtype)

    # Build mapping from FTARGETS rows (sorted by target) to FASSIGN rows
    # (sorted by fiber).

    # Rows containing assigned fibers
    fassign_valid = np.where(fiber_data["TARGETID"] >= 0)[0]

    # Rows in target tables containing assigned targets These indices are
    # valid for both the original input FTARGETS data and also our per-tile
    # copy of the target catalog data.  These indices are essentially random
    # access into the target table.
    target_rows = np.array(
        [tile_tgindx[x] for x in fiber_data["TARGETID"][fassign_valid]])

    # tm.stop()
    # tm.report("  fiber / target index mapping {}".format(tile_id))
    # tm.clear()
    # tm.start()

    # Copy original data and also determine which of our required columns
    # will come from external catalogs
    external_cols = list()
    for field in out_dtype.names:
        if field in results_assign_columns:
            # Copy assignment and fiber property columns directly.
            outdata[field] = fiber_data[field]
        else:
            if field in results_targets_columns:
                # This is a column we are copying from our raw output.
                if (len(target_rows) > 0):
                    outdata[field][fassign_valid] = \
                        targets_data[field][target_rows]
            else:
                # This required column is coming from external catalogs
                external_cols.append(field)

    # tm.stop()
    # tm.report("  copy raw data to output {}".format(tile_id))
    # tm.clear()
    # tm.start()

    # Now copy external target properties for the remaining columns.
    if (len(target_rows) > 0):
        # # Looping over rows and then columns is faster, likely because there
        # # are so many columns and the data is stored row-major.
        # for irw, orw in zip(target_rows, fassign_valid):
        #     for c in external_cols:
        #         realname = c
        #         if c in merged_fiberassign_swap:
        #             realname = merged_fiberassign_swap[c]
        #         outdata[realname][orw] = tile_targets[c][irw]
        for c in external_cols:
            if c == "OBJTYPE":
                # FIXME:  This column is redundant to doing a simple bitwise
                # operation on the target bit field, and it is specific to
                # the main survey.  Personally I believe it should be
                # removed completely.  -TK
                objtype = np.zeros(len(fassign_valid), dtype="S3")
                if "DESI_TARGET" in out_dtype.names:
                    # This is a main survey file.
                    objtype[:] = "TGT"
                    is_sky = (tile_targets["DESI_TARGET"][target_rows]
                              & desi_mask.SKY) != 0
                    objtype[is_sky] = "SKY"
                    badmask = \
                        desi_mask.mask("BAD_SKY|NO_TARGET|IN_BRIGHT_OBJECT")
                    is_bad = (tile_targets["DESI_TARGET"][target_rows]
                              & badmask) != 0
                    objtype[is_bad] = "BAD"
                else:
                    # This is some other survey
                    objtype = ["NA" for x in range(len(fassign_valid))]
                outdata[c][fassign_valid] = objtype
            else:
                outdata[c][fassign_valid] = tile_targets[c][target_rows]

    # Special check for REF_EPOCH which isn't in MTL yet
    ismoving = (outdata['PMRA'] != 0) | (outdata['PMDEC'] != 0)
    if np.all(outdata['REF_EPOCH'] == 0.0) and np.any(ismoving):
        log.error('REF_EPOCH not set, using 2015.5')
        outdata['REF_EPOCH'][ismoving] = 2015.5

    # tm.stop()
    # tm.report("  copy external data to output {}".format(tile_id))
    # tm.clear()
    # tm.start()

    cols_to_keep = list()
    for c in outdata.dtype.names:
        if len(outdata[c].shape
               ) < 2:  #Don't propagate 2D target columns into FIBERASSIGN HDU
            cols_to_keep.append(c)
    outdata = outdata[cols_to_keep]

    cols_to_keep = list()
    for c in tile_targets.dtype.names:
        if len(tile_targets[c].shape
               ) < 2:  #Don't propagate 2D target columns into TARGETS HDU
            cols_to_keep.append(c)
    tile_targets = tile_targets[cols_to_keep]

    # Create the file
    if os.path.isfile(outfile):
        os.remove(outfile)
    fd = fitsio.FITS(outfile, "rw")

    # Write a heaader-only primary HDU
    fd.write(None, header=inhead, extname="PRIMARY")

    # Write the main FIBERASSIGN HDU- only the data for science positioners
    # Enforce sorting by FIBER
    log.info("Writing new data {}".format(outfile))
    ii = np.argsort(outdata['FIBER'][science_rows])
    fd.write(outdata[science_rows][ii], header=inhead, extname="FIBERASSIGN")

    # Now write out the sky monitor fibers.  We extract the rows and columns
    # from the already-computed recarray.

    skymon_dtype = np.dtype([(x, y) for x, y in merged_skymon_columns.items()])
    skymon = np.zeros(len(sky_rows), dtype=skymon_dtype)
    # Sky monitor fake FIBER column with values 0-19.  The fake FIBER value in
    # the raw data is already based on increasing LOCATION value.
    skymon_fiber = np.arange(len(sky_rows), dtype=np.int32)
    for field in skymon_dtype.names:
        if field == "FIBER":
            skymon["FIBER"] = skymon_fiber
        else:
            skymon[field] = outdata[field][sky_rows]
    fd.write(skymon, header=inhead, extname="SKY_MONITOR")

    # Copy GFA data if it exists
    if gfa_targets is not None:
        fd.write(gfa_targets, header=inhead, extname="GFA_TARGETS")

    # Write the per-tile catalog information also.  Sadly, this HDU is
    # expected to have the original column names.  We swap them back, which
    # is fine since we are going to delete this data after writing anyway.
    backswap = {y: x for x, y in merged_fiberassign_swap.items()}
    curnames = np.copy(tile_targets.dtype.names)
    newnames = list()
    for nm in curnames:
        if nm in backswap:
            newnames.append(backswap[nm])
        else:
            newnames.append(nm)
    tile_targets.dtype.names = newnames
    fd.write(tile_targets, header=inhead, extname="TARGETS")
    del tile_targets

    # Write the "POTENTIAL_ASSIGNMENTS" HDU
    potential_dtype = np.dtype([(x, y)
                                for x, y in merged_potential_columns.items()])
    potential = np.zeros(len(avail_data), dtype=potential_dtype)

    locfiber = {
        x: y
        for x, y in zip(fiber_data["LOCATION"], fiber_data["FIBER"])
    }
    potential["LOCATION"] = avail_data["LOCATION"]
    potential["TARGETID"] = avail_data["TARGETID"]
    potential["FIBER"] = [locfiber[x] for x in avail_data["LOCATION"]]
    fd.write(potential, header=inhead, extname="POTENTIAL_ASSIGNMENTS")

    # Now copy the original HDUs
    if copy_fba:
        fd.write(fiber_data, header=inhead, extname="FASSIGN")
        fd.write(targets_data, header=inhead, extname="FTARGETS")
        fd.write(avail_data, header=inhead, extname="FAVAIL")

    # Close the file
    fd.close()

    # tm.stop()
    # tm.report("  write data to file {}".format(tile_id))
    # tm.clear()
    # tm.start()

    # Try to encourage python to free some memory...
    del fd

    del avail_data
    del targets_data
    del fiber_data
    return
예제 #9
0
def fa_plot(axarr, i, j, tids, fao_fibs, fao_tiles, tiles, targets, ttypes=['QSO'], notin=True, s=7):
    '''
    Plot the distribution of assigned and unassigned targets for a list of tiles. 

    Input:

    tids      --  Tile IDs to plot.
    num       --  Index of Tile to plot in tids.
    fao_fibs  --  Loaded fiberassign fiber-* files. 
    fao_tiles --  Loaded fiberassign tile-* files. 
    tiles     --  (Associated) Nominal tile info. 
    targets   --  instance of the targets class.
    ttypes    --  Target types to plot ['ELG', 'LRG', 'QSO', 'BGS']
    notin     --  Add markers for unassigned (more busy).
    '''

    colordict    = {'ELG': 'b', 'LRG': 'r', 'QSO': 'orange', 'BGS': 'y'}        
    tile_radius  = 1.65

    if isinstance(axarr, list) | isinstance(axarr, np.ndarray):
      ax         = axarr[i,j]

    else:
      ax         = axarr

    ##  Plot boundaries of all in tids. 
    for count, tid in enumerate(tids):
        tileloc  = np.where(tiles['TILEID'].quantity == tid)[0][0]
        ra, dec  = tiles['RA'][tileloc], tiles['DEC'][tileloc]
        c        = plt.Circle((ra, dec), tile_radius, facecolor='None', edgecolor='k')
        ax.add_artist(c)

    ##  Plot for main tile.
    tid          = tids[i]

    fao_fib      = fao_fibs[i]
    fao_tile     = fao_tiles[i]

    tileloc      = np.where(tiles['TILEID'].quantity == tid)[0][0]
    prog         = tiles['PROGRAM'][tileloc]

    ##  Centre plot on first tile in tids. 
    ctileloc     = np.where(tiles['TILEID'].quantity == tids[0])[0][0]

    tmp          = fao_tile['TARGETID']
    
    not_assigned = (~np.in1d(tmp, fao_fib['TARGETID']))
    assigned     = ( np.in1d(tmp, fao_fib['TARGETID']))

    dsorted      = np.argsort(targets['TARGETID'])
        
    ypos         = np.searchsorted(targets['TARGETID'][dsorted], tmp)
    inds         = np.zeros_like(ypos)

    inds[tmp <= np.max(targets['TARGETID'])] = dsorted[ypos[tmp <= np.max(targets['TARGETID'])]]

    for ttype in ttypes:
        ##  Enforce BGS on bright.  MWS?
        if prog == 'BRIGHT':
            ttype = 'BGS'

        istype    = (targets[inds]['DESI_TARGET'] & desi_mask.mask(ttype) != 0)

        ax.scatter(targets[inds]['RA'][assigned & istype & (tmp <= np.max(targets['TARGETID']))],
                   targets[inds]['DEC'][assigned & istype & (tmp <= np.max(targets['TARGETID']))],
                   s=s, edgecolor="none", c=colordict[ttype], zorder=10, label=ttype)
        
        if notin:
            ax.scatter(targets[inds]['RA'][not_assigned & istype & (tmp <= np.max(targets['TARGETID']))],
                       targets[inds]['DEC'][not_assigned & istype & (tmp <= np.max(targets['TARGETID']))],
                       s=s, edgecolor="none", c=colordict[ttype], marker="x", zorder=10, label='Unassigned', alpha=0.6)
        
        ax.set_xlim(tiles['RA'][ctileloc]  - tile_radius, tiles['RA'][ctileloc]  + tile_radius)
        ax.set_ylim(tiles['DEC'][ctileloc] - tile_radius, tiles['DEC'][ctileloc] + tile_radius)
        
        ax.legend(frameon=False, title='TILE %d:  %s' % (tid, prog), loc=1)

        ax.set_aspect('equal', 'datalim')
        ax.invert_xaxis()
예제 #10
0
def make_global_DR8_truth(global_DR8_mtl_file,
                          output_path='./',
                          program='dark'):
    import desitarget.mock.mockmaker as mb
    from desitarget.targetmask import desi_mask, bgs_mask, mws_mask

    os.makedirs(output_path, exist_ok=True)
    global_DR8_truth_file = os.path.join(
        output_path, 'global_DR8_truth_{}.fits'.format(program))
    if os.path.exists(global_DR8_truth_file):
        print("File {} already exists".format(global_DR8_truth_file))
        return global_DR8_truth_file

    print('Started reading file {}'.format(global_DR8_mtl_file))
    targets = Table.read(global_DR8_mtl_file)
    print('Finished reading file {}'.format(global_DR8_mtl_file))

    # Find what targets will be associated to lya targets
    is_lya_qso = random_assign_lya_qso(targets)

    # Initialized truth Table
    colnames = list(targets.dtype.names)
    print(colnames)
    nobj = len(targets)
    truth = mb.empty_truth_table(nobj=nobj)[0]
    print(truth.keys())

    for k in colnames:
        if k in truth.keys():
            print(k)
            truth[k][:] = targets[k][:]

    nothing = '          '
    truth['TEMPLATESUBTYPE'] = np.repeat(nothing, nobj)

    masks = [
        'MWS_ANY',
        'BGS_ANY',
        'STD_FAINT',
        'STD_BRIGHT',
        'ELG',
        'LRG',
        'QSO',
    ]
    dict_truespectype = {
        'BGS_ANY': 'GALAXY',
        'ELG': 'GALAXY',
        'LRG': 'GALAXY',
        'QSO': 'QSO',
        'MWS_ANY': 'STAR',
        'STD_FAINT': 'STAR',
        'STD_BRIGHT': 'STAR'
    }
    dict_truetemplatetype = {
        'BGS_ANY': 'BGS',
        'ELG': 'ELG',
        'LRG': 'LRG',
        'QSO': 'QSO',
        'MWS_ANY': 'STAR',
        'STD_FAINT': 'STAR',
        'STD_BRIGHT': 'STAR'
    }
    dict_truez = {
        'BGS_ANY': 0.2,
        'ELG': 1.5,
        'LRG': 0.7,
        'QSO': 2.0,
        'MWS_ANY': 0.0,
        'STD_FAINT': 0.0,
        'STD_BRIGHT': 0.0
    }

    for m in masks:
        istype = (targets['DESI_TARGET'] & desi_mask.mask(m)) != 0
        print(m, np.count_nonzero(istype))
        truth['TRUESPECTYPE'][istype] = np.repeat(dict_truespectype[m],
                                                  np.count_nonzero(istype))
        truth['TEMPLATETYPE'][istype] = np.repeat(dict_truetemplatetype[m],
                                                  np.count_nonzero(istype))
        truth['MOCKID'][istype] = targets['TARGETID'][istype]
        truth['TRUEZ'][istype] = dict_truez[m]

    truth['TRUEZ'][is_lya_qso] = 3.0
    truth['RA'] = targets['RA']
    truth['DEC'] = targets['DEC']

    # Check that all targets have been assigned to a class
    iii = truth['MOCKID'] == 0
    assert np.count_nonzero(iii) == 0

    del targets

    print('Started writing to file {}'.format(global_DR8_truth_file))
    truth.write(global_DR8_truth_file, overwrite=True)
    print('Finished writing to file {}'.format(global_DR8_truth_file))

    del truth
    return global_DR8_truth_file
예제 #11
0
    def setUpClass(cls):
        cls.ntiles = 4
        tiles = desimodel.io.load_tiles()
        cls.tileids = tiles['TILEID'][0:cls.ntiles]
        cls.tilefiles = ['tile-{:05d}.fits'.format(i) for i in cls.tileids]
        cls.tilefiles_multiobs = [
            'multitile-{:05d}.fits'.format(i) for i in cls.tileids
        ]

        cls.nspec = n = 1000
        targets = Table()
        targets['TARGETID'] = np.random.randint(0, 2**60, size=n)
        targets['DESI_TARGET'] = 2**np.random.randint(0, 3, size=n)
        targets['BGS_TARGET'] = np.zeros(n, dtype=int)
        targets['MWS_TARGET'] = np.zeros(n, dtype=int)
        isLRG = (targets['DESI_TARGET'] & desi_mask.LRG) != 0
        isELG = (targets['DESI_TARGET'] & desi_mask.ELG) != 0
        isQSO = (targets['DESI_TARGET'] & desi_mask.QSO) != 0
        cls.targets = targets

        #- Make a few of them BGS and MWS
        iibright = np.random.choice(np.arange(n), size=6, replace=False)
        isBGS = iibright[0:3]
        isMWS = iibright[3:6]
        targets['DESI_TARGET'][isBGS] = desi_mask.BGS_ANY
        targets['BGS_TARGET'][isBGS] = bgs_mask.BGS_BRIGHT
        targets['DESI_TARGET'][isMWS] = desi_mask.MWS_ANY
        targets['MWS_TARGET'][isMWS] = mws_mask.MWS_MAIN

        #- Add some fake photometry; no attempt to get colors right
        flux = np.zeros((n, 6))  #- ugrizY; DESI has grz
        flux[isLRG, 1] = np.random.uniform(0, 1.0, np.count_nonzero(isLRG))
        flux[isLRG, 2] = np.random.uniform(0, 5.0, np.count_nonzero(isLRG))
        flux[isLRG, 4] = np.random.uniform(0, 5.0, np.count_nonzero(isLRG))
        flux[isELG, 1] = np.random.uniform(0, 4.0, np.count_nonzero(isELG))
        flux[isELG, 2] = np.random.uniform(0, 4.0, np.count_nonzero(isELG))
        flux[isELG, 4] = np.random.uniform(0, 10.0, np.count_nonzero(isELG))
        flux[isQSO, 1] = np.random.uniform(0, 4.0, np.count_nonzero(isQSO))
        flux[isQSO, 2] = np.random.uniform(0, 4.0, np.count_nonzero(isQSO))
        flux[isQSO, 4] = np.random.uniform(0, 6.0, np.count_nonzero(isQSO))
        flux[isBGS, 1] = np.random.uniform(10, 600, np.count_nonzero(isBGS))
        flux[isBGS, 2] = np.random.uniform(15, 1000, np.count_nonzero(isBGS))
        flux[isBGS, 4] = np.random.uniform(10, 1400, np.count_nonzero(isBGS))
        flux[isMWS, 1] = np.random.uniform(10, 150, np.count_nonzero(isMWS))
        flux[isMWS, 2] = np.random.uniform(15, 350, np.count_nonzero(isMWS))
        flux[isMWS, 4] = np.random.uniform(10, 1500, np.count_nonzero(isMWS))
        targets['DECAM_FLUX'] = flux

        truth = Table()
        truth['TARGETID'] = targets['TARGETID']
        truth['TRUEZ'] = np.random.uniform(0, 1.5, size=n)
        truth['TRUESPECTYPE'] = np.zeros(n, dtype=(str, 10))
        truth['GMAG'] = np.random.uniform(18.0, 24.0, size=n)
        ii = (targets['DESI_TARGET'] & desi_mask.mask('LRG|ELG|BGS_ANY')) != 0
        truth['TRUESPECTYPE'][ii] = 'GALAXY'
        ii = (targets['DESI_TARGET'] == desi_mask.QSO)
        truth['TRUESPECTYPE'][ii] = 'QSO'
        starmask = desi_mask.mask('MWS_ANY|STD_FSTAR|STD_WD|STD_BRIGHT')
        ii = (targets['DESI_TARGET'] & starmask) != 0
        truth['TRUESPECTYPE'][ii] = 'STAR'

        #- Add some fake [OII] fluxes for the ELGs; include some that will fail
        isELG = (targets['DESI_TARGET'] & desi_mask.ELG) != 0
        nELG = np.count_nonzero(isELG)
        truth['OIIFLUX'] = np.zeros(n, dtype=float)
        truth['OIIFLUX'][isELG] = np.random.normal(2e-17, 2e-17,
                                                   size=nELG).clip(0)

        cls.truth = truth

        fiberassign = truth['TARGETID', ]
        fiberassign['RA'] = np.random.uniform(0, 5, size=n)
        fiberassign['DEC'] = np.random.uniform(0, 5, size=n)
        fiberassign.meta['EXTNAME'] = 'FIBER_ASSIGNMENTS'
        nx = cls.nspec // cls.ntiles
        cls.targets_in_tile = dict()
        for i, filename in enumerate(cls.tilefiles):
            subset = fiberassign[i * nx:(i + 1) * nx]
            subset.write(filename)
            cls.targets_in_tile[cls.tileids[i]] = subset['TARGETID']
            hdulist = fits.open(filename, mode='update')
            hdr = hdulist[1].header
            hdr.set('TILEID', cls.tileids[i])
            hdulist.close()

        #- Also create a test of tile files that have multiple observations
        nx = cls.nspec // cls.ntiles
        for i, filename in enumerate(cls.tilefiles_multiobs):
            subset = fiberassign[0:(i + 1) * nx]
            subset.write(filename)
            hdulist = fits.open(filename, mode='update')
            hdr = hdulist[1].header
            hdr.set('TILEID', cls.tileids[i])
            hdulist.close()
예제 #12
0
    dict_truespectype = {
        'BGS_ANY': 'GALAXY',
        'ELG': 'GALAXY',
        'LRG': 'GALAXY',
        'QSO': 'QSO',
        'STD': 'STAR'
    }
    dict_truetemplatetype = {
        'BGS_ANY': 'BGS',
        'ELG': 'ELG',
        'LRG': 'LRG',
        'QSO': 'QSO',
        'STD': 'STAR'
    }
    masks = {
        'BGS_ANY': desi_mask.mask('BGS_ANY'),
        'ELG': desi_mask.mask('ELG'),
        'LRG': desi_mask.mask('LRG'),
        'QSO': desi_mask.mask('QSO'),
        'STD': std_mask
    }

    for m in masks.keys():
        istype = (targets['DESI_TARGET'] & masks[m]) != 0
        print(np.count_nonzero(istype))
        truth['TRUESPECTYPE'][istype] = np.repeat(dict_truespectype[m],
                                                  np.count_nonzero(istype))
        truth['TEMPLATETYPE'][istype] = np.repeat(dict_truetemplatetype[m],
                                                  np.count_nonzero(istype))
        truth['MOCKID'][istype] = targets['TARGETID'][istype]
예제 #13
0
    def setUpClass(cls):
        cls.ntiles = 4
        tiles = desimodel.io.load_tiles()
        cls.tileids = tiles['TILEID'][0:cls.ntiles]
        cls.tilefiles = ['tile-{:05d}.fits'.format(i) for i in cls.tileids]
        cls.tilefiles_multiobs = ['multitile-{:05d}.fits'.format(i) for i in cls.tileids]

        cls.nspec = n = 1000
        targets = Table()
        targets['TARGETID'] = np.random.randint(0,2**60, size=n)
        targets['DESI_TARGET'] = 2**np.random.randint(0,3,size=n)
        targets['BGS_TARGET'] = np.zeros(n, dtype=int)
        targets['MWS_TARGET'] = np.zeros(n, dtype=int)
        isLRG = (targets['DESI_TARGET'] & desi_mask.LRG) != 0
        isELG = (targets['DESI_TARGET'] & desi_mask.ELG) != 0
        isQSO = (targets['DESI_TARGET'] & desi_mask.QSO) != 0
        cls.targets = targets

        #- Make a few of them BGS and MWS
        iibright = np.random.choice(np.arange(n), size=6, replace=False)
        isBGS = iibright[0:3]
        isMWS = iibright[3:6]
        targets['DESI_TARGET'][isBGS] = desi_mask.BGS_ANY
        targets['BGS_TARGET'][isBGS] = bgs_mask.BGS_BRIGHT
        targets['DESI_TARGET'][isMWS] = desi_mask.MWS_ANY
        try:
            #- desitarget >= 0.25.0
            targets['MWS_TARGET'][isMWS] = mws_mask.MWS_BROAD
        except AttributeError:
            #- desitarget <= 0.24.0
            targets['MWS_TARGET'][isMWS] = mws_mask.MWS_MAIN

        #- Add some fake photometry; no attempt to get colors right
        flux = np.zeros((n, 6))  #- ugrizY; DESI has grz
        flux[isLRG, 1] = np.random.uniform(0, 1.0, np.count_nonzero(isLRG))
        flux[isLRG, 2] = np.random.uniform(0, 5.0, np.count_nonzero(isLRG))
        flux[isLRG, 4] = np.random.uniform(0, 5.0, np.count_nonzero(isLRG))
        flux[isELG, 1] = np.random.uniform(0, 4.0, np.count_nonzero(isELG))
        flux[isELG, 2] = np.random.uniform(0, 4.0, np.count_nonzero(isELG))
        flux[isELG, 4] = np.random.uniform(0, 10.0, np.count_nonzero(isELG))
        flux[isQSO, 1] = np.random.uniform(0, 4.0, np.count_nonzero(isQSO))
        flux[isQSO, 2] = np.random.uniform(0, 4.0, np.count_nonzero(isQSO))
        flux[isQSO, 4] = np.random.uniform(0, 6.0, np.count_nonzero(isQSO))
        # isBGS and isMWS are arrays of indices, not arrays of booleans
        flux[isBGS, 1] = np.random.uniform(10, 600, isBGS.size)
        flux[isBGS, 2] = np.random.uniform(15, 1000, isBGS.size)
        flux[isBGS, 4] = np.random.uniform(10, 1400, isBGS.size)
        flux[isMWS, 1] = np.random.uniform(10, 150, isMWS.size)
        flux[isMWS, 2] = np.random.uniform(15, 350, isMWS.size)
        flux[isMWS, 4] = np.random.uniform(10, 1500, isMWS.size)
        targets['DECAM_FLUX'] = flux

        truth = Table()
        truth['TARGETID'] = targets['TARGETID']
        truth['TRUEZ'] = np.random.uniform(0, 1.5, size=n)
        truth['TRUESPECTYPE'] = np.zeros(n, dtype=(str, 10))
        truth['GMAG'] = np.random.uniform(18.0, 24.0, size=n)
        ii = (targets['DESI_TARGET'] & desi_mask.mask('LRG|ELG|BGS_ANY')) != 0
        truth['TRUESPECTYPE'][ii] = 'GALAXY'
        ii = (targets['DESI_TARGET'] == desi_mask.QSO)
        truth['TRUESPECTYPE'][ii] = 'QSO'
        starmask = desi_mask.mask('MWS_ANY|STD_FAINT|STD_WD|STD_BRIGHT')
        ii = (targets['DESI_TARGET'] & starmask) != 0
        truth['TRUESPECTYPE'][ii] = 'STAR'

        #- Add some fake [OII] fluxes for the ELGs; include some that will fail
        isELG = (targets['DESI_TARGET'] & desi_mask.ELG) != 0
        nELG = np.count_nonzero(isELG)
        truth['OIIFLUX'] = np.zeros(n, dtype=float)
        truth['OIIFLUX'][isELG] = np.random.normal(2e-17, 2e-17, size=nELG).clip(0)

        cls.truth = truth

        fiberassign = truth['TARGETID',]
        fiberassign['RA'] = np.random.uniform(0,5, size=n)
        fiberassign['DEC'] = np.random.uniform(0,5, size=n)
        fiberassign.meta['EXTNAME'] = 'FIBERASSIGN'
        nx = cls.nspec // cls.ntiles
        cls.targets_in_tile = dict()
        for i, filename in enumerate(cls.tilefiles):
            subset = fiberassign[i*nx:(i+1)*nx]
            subset.write(filename)
            cls.targets_in_tile[cls.tileids[i]] = subset['TARGETID']
            hdulist = fits.open(filename, mode='update')
            hdr = hdulist[1].header
            hdr.set('TILEID', cls.tileids[i])
            hdulist.close()

        #- Also create a test of tile files that have multiple observations
        nx = cls.nspec // cls.ntiles
        for i, filename in enumerate(cls.tilefiles_multiobs):
            subset = fiberassign[0:(i+1)*nx]
            subset.write(filename)
            hdulist = fits.open(filename, mode='update')
            hdr = hdulist[1].header
            hdr.set('TILEID', cls.tileids[i])
            hdulist.close()
예제 #14
0
  
    
if __name__ == '__main__':
 compute        = True

 ext            = '_chi2'  ##  '_r_z'

 dchi2          = [-2.5, -2.0, -1.5, -1.0, -0.5, 0.0]
 snrs           = [3., 4., 6., 9., 12., 20.]

 if compute:
  targets       = glob.glob('/project/projectdirs/desi/target/catalogs/dr8/0.32.0/targets/main/resolve/dark/*.fits')

  ##  elgs      = Table(fits.open(targets[0])[1].data)
  elgs          = Table([fits.open(targets[0])[1].data[x] for x in cols], names=cols)
  elgs          = elgs[(elgs['DESI_TARGET']    & desi_mask.mask('ELG')) != 0]

  ##  S/N should be prior to extinction correction. 
  elgs['GSNR']  = np.sqrt(elgs['FLUX_IVAR_G']) * elgs['FLUX_G'] ##  elgs['MW_TRANSMISSION_G']
  elgs['RSNR']  = np.sqrt(elgs['FLUX_IVAR_R']) * elgs['FLUX_R'] ##  elgs['MW_TRANSMISSION_R']
  elgs['ZSNR']  = np.sqrt(elgs['FLUX_IVAR_Z']) * elgs['FLUX_Z'] ##  elgs['MW_TRANSMISSION_Z']

  ##
  elgs['RZSNR'] = np.sqrt(elgs['RSNR'] ** 2. + elgs['ZSNR'] ** 2.)
  
  elgs['SNR']   = np.sqrt(elgs['GSNR'] ** 2. + elgs['RSNR'] ** 2. + elgs['ZSNR'] ** 2.)

  elgs['BFIT']     = np.array([np.array(x).max()               for x in elgs['DCHISQ']])
  elgs['BFIT2']    = np.array([np.sort(np.array(x))[:-1].max() for x in elgs['DCHISQ']])

  elgs['CHI2DIFF'] = np.clip(np.log10(elgs['BFIT'] - elgs['BFIT2']), a_min=-6., a_max=None)
예제 #15
0
    if k in truth.keys():
        print(k)
        truth[k][:] = targets[k][:]

nothing = '          '
truth['TEMPLATESUBTYPE'] = np.repeat(nothing, nobj)


masks = ['BGS_ANY', 'ELG', 'LRG', 'QSO', 'STD_FSTAR', 'STD_BRIGHT']
dict_truespectype = {'BGS_ANY':'GALAXY', 'ELG':'GALAXY', 'LRG':'GALAXY', 'QSO':'QSO', 
                    'STD_FSTAR':'STAR', 'STD_BRIGHT':'STAR'}
dict_truetemplatetype = {'BGS_ANY':'BGS', 'ELG':'ELG', 'LRG':'LRG', 'QSO':'QSO', 
                        'STD_FSTAR':'STAR', 'STD_BRIGHT':'STAR'}

for m in masks:
    istype = (targets['DESI_TARGET'] & desi_mask.mask(m))!=0
    print(m, np.count_nonzero(istype))
    truth['TRUESPECTYPE'][istype] = np.repeat(dict_truespectype[m], np.count_nonzero(istype))
    truth['TEMPLATETYPE'][istype] = np.repeat(dict_truetemplatetype[m], np.count_nonzero(istype))
    truth['MOCKID'][istype] = targets['TARGETID'][istype]

n_unassigned = np.count_nonzero(truth['MOCKID']==0)
print('unassigned', n_unassigned)

mtl = desitarget.mtl.make_mtl(targets)
mtl.meta['EXTNAME'] = 'MTL'

isstd = (targets['DESI_TARGET'] & desi_mask.mask('STD_BRIGHT'))!=0
isstd |= (targets['DESI_TARGET'] & desi_mask.mask('STD_FSTAR'))!=0
print('standards', np.count_nonzero(isstd))
standards = mtl[isstd]
예제 #16
0
from astropy.table import Table
from desitarget.targetmask import desi_mask, bgs_mask, mws_mask
import numpy as np
import os

main_path = "/global/cscratch1/sd/forero/DR5FiberAssign/"
main_path = "/Users/forero/github/SandboxDESI/decals/"
truth = Table.read(os.path.join(main_path, 'targets', 'truth.fits'))
mtl = Table.read(os.path.join(main_path, 'targets', 'mtl.fits'))
zcat = Table.read(os.path.join(main_path, 'zcat', 'zcat.fits'))


isbgs = (mtl['DESI_TARGET'] & desi_mask.mask('BGS_ANY')) != 0
islrg = (mtl['DESI_TARGET'] & desi_mask.mask('LRG')) != 0

n_in = np.count_nonzero(isbgs|islrg)
print('total number of points in {}'.format(n_in))

print('initializing small truth')
small_truth = mtl[['TARGETID','RA','DEC']][isbgs|islrg]
small_truth['TEMPLATETYPE'] = truth['TEMPLATETYPE'][isbgs|islrg]
small_truth['NUMOBS'] = np.zeros(n_in, dtype=int)

print('making small zcat')
ii = np.in1d(zcat['TARGETID'], small_truth['TARGETID'])
small_zcat = zcat['TARGETID', 'NUMOBS'][ii]

print('checking integrity')
ii = np.in1d(small_truth['TARGETID'], small_zcat['TARGETID'])
if not (small_truth['TARGETID'][ii] == small_zcat['TARGETID']).all():
    print('We have problems with the IDs')
예제 #17
0
from astropy.table import Table
from desitarget.targetmask import desi_mask, bgs_mask, mws_mask
import numpy as np
mtl = Table.read('mtl.fits')
zcat = Table.read("zcat.fits")
print("Efficiencies N_in N_out N_in/N_out")
for objtype in ['BGS_ANY', 'ELG', 'LRG', 'QSO', 'STD_FSTAR', 'STD_BRIGHT']:
    ii = (mtl['DESI_TARGET'] & desi_mask.mask(objtype)) != 0
    jj = np.in1d(mtl['TARGETID'][ii], zcat['TARGETID'])
    n_in = np.count_nonzero(ii)
    n_out = np.count_nonzero(jj)
    print('{:10} {} {} {:.2f}'.format(objtype, n_in, n_out, n_out/n_in))
예제 #18
0
        if k in truth.keys():
            print(k)
            truth[k][:] = targetdata[k][:]

    nothing = '          '
    truth['TEMPLATESUBTYPE'] = np.repeat(nothing, nobj)

    masks = ['BGS_ANY', 'ELG', 'LRG', 'QSO', 'STD_FSTAR', 'STD_BRIGHT']
    dict_truespectype = {'BGS_ANY':'GALAXY', 'ELG':'GALAXY', 'LRG':'GALAXY', 'QSO':'QSO', 
                    'STD_FSTAR':'STAR', 'STD_BRIGHT':'STAR', 'STD':'STAR'}
    dict_truetemplatetype = {'BGS_ANY':'BGS', 'ELG':'ELG', 'LRG':'LRG', 'QSO':'QSO', 
                        'STD_FSTAR':'STAR', 'STD_BRIGHT':'STAR', 'STD':'STAR'}

    for m in masks:
        if m in desi_mask.names():
            istype = (targetdata['DESI_TARGET'] & desi_mask.mask(m))!=0
            print(m, np.count_nonzero(istype))
            truth['TRUESPECTYPE'][istype] = np.repeat(dict_truespectype[m], np.count_nonzero(istype))
            truth['TEMPLATETYPE'][istype] = np.repeat(dict_truetemplatetype[m], np.count_nonzero(istype))
            truth['MOCKID'][istype] = targetdata['TARGETID'][istype]
        
    print('writing truth')
    truth.write(truthfile, overwrite=True)
    print('done truth')
    
# Running quicksurvey
cmd = "quicksurvey -T {}".format(datadir)
cmd += " -f fiberassign "
cmd += " -E /global/project/projectdirs/desi/datachallenge/surveysim2017/baseline_1m/exposures.fits"
cmd += " -D fiberassign_dates_baseline_1m.txt"
print(cmd)
예제 #19
0
def parse_assign(optlist=None):
    """Parse assignment options.

    This parses either sys.argv or a list of strings passed in.  If passing
    an option list, you can create that more easily using the
    :func:`option_list` function.

    Args:
        optlist (list, optional): Optional list of arguments to parse instead
            of using sys.argv.

    Returns:
        (namespace):  an ArgumentParser namespace.

    """
    log = Logger.get()
    parser = argparse.ArgumentParser()

    parser.add_argument("--targets",
                        type=str,
                        required=True,
                        nargs="+",
                        help="Input file with targets of any type.  This "
                        "argument can be specified multiple times (for "
                        "example if standards / skies / science targets are "
                        "in different files).  By default, the "
                        "'--mask_column' (default DESI_TARGET)"
                        "column and bitfield values defined in desitarget "
                        "are used to determine the type of each target.  "
                        "Each filename may be optionally followed by comma "
                        "and then one of the strings 'science', 'standard', "
                        "'sky' or 'safe' to force all targets in that file "
                        "to be treated as a fixed target type.")

    parser.add_argument("--sky",
                        type=str,
                        required=False,
                        nargs="+",
                        help="Input file with sky or 'bad sky' targets.  "
                        "This option exists in order to treat main-survey"
                        " sky target files as valid for other survey types."
                        "  If you are running a main survey assignment, you"
                        " can just pass the sky file to the --targets list.")

    parser.add_argument("--gfafile",
                        type=str,
                        required=False,
                        default=None,
                        help="Optional GFA targets FITS file")

    parser.add_argument("--footprint",
                        type=str,
                        required=False,
                        default=None,
                        help="Optional FITS file defining the footprint.  If"
                        " not specified, the default footprint from desimodel"
                        " is used.")

    parser.add_argument("--tiles",
                        type=str,
                        required=False,
                        default=None,
                        help="Optional text file containing a subset of the"
                        " tile IDs to use in the footprint, one ID per line."
                        " Default uses all tiles in the footprint.")

    parser.add_argument("--positioners",
                        type=str,
                        required=False,
                        default=None,
                        help="Optional FITS file describing the fiber "
                        "positioner locations.  Default uses the file from "
                        "desimodel.")

    parser.add_argument("--status",
                        type=str,
                        required=False,
                        default=None,
                        help="Optional fiber status file in astropy ECSV "
                        "format.  Default treats all fibers as good.")

    parser.add_argument("--rundate",
                        type=str,
                        required=False,
                        default=None,
                        help="Optional date to simulate for this run of "
                        "fiber assignment, used with the fiber status file "
                        "to determine which fibers currently have problems.  "
                        "Default uses the current date.  Format is "
                        "YYYY-MM-DDTHH:mm:ss in UTC time.")

    parser.add_argument("--dir",
                        type=str,
                        required=False,
                        default=None,
                        help="Output directory.")

    parser.add_argument("--prefix",
                        type=str,
                        required=False,
                        default="fiberassign_",
                        help="Prefix of each file (before the <tile>.fits).")

    parser.add_argument("--split",
                        required=False,
                        default=False,
                        action="store_true",
                        help="Split output into tile prefix directories.")

    parser.add_argument("--standards_per_petal",
                        type=int,
                        required=False,
                        default=10,
                        help="Required number of standards per"
                        " petal")

    parser.add_argument("--sky_per_petal",
                        type=int,
                        required=False,
                        default=40,
                        help="Required number of sky targets per"
                        " petal")

    parser.add_argument("--write_all_targets",
                        required=False,
                        default=False,
                        action="store_true",
                        help="When writing target properties, write data "
                        "for all available targets, not just those which are "
                        "assigned.  This is convenient, but increases the "
                        "write time and the file size.")

    parser.add_argument("--overwrite",
                        required=False,
                        default=False,
                        action="store_true",
                        help="Overwrite any pre-existing output files")

    parser.add_argument("--mask_column",
                        required=False,
                        default=None,
                        help="FITS column to use for applying target "
                        "masks")

    parser.add_argument("--sciencemask",
                        required=False,
                        default=None,
                        help="Default DESI_TARGET mask to use for science "
                        "targets")

    parser.add_argument("--stdmask",
                        required=False,
                        default=None,
                        help="Default DESI_TARGET mask to use for stdstar "
                        "targets")

    parser.add_argument("--skymask",
                        required=False,
                        default=None,
                        help="Default DESI_TARGET mask to use for sky targets")

    parser.add_argument("--safemask",
                        required=False,
                        default=None,
                        help="Default DESI_TARGET mask to use for safe "
                        "backup targets")

    parser.add_argument("--excludemask",
                        required=False,
                        default=None,
                        help="Default DESI_TARGET mask to exclude from "
                        "any assignments")

    parser.add_argument("--by_tile",
                        required=False,
                        default=False,
                        action="store_true",
                        help="Do assignment one tile at a time.  This disables"
                        " redistribution.")

    args = None
    if optlist is None:
        args = parser.parse_args()
    else:
        args = parser.parse_args(optlist)

    if args.sky is None:
        args.sky = list()

    # FIXME:  The code below is only valid for the main survey.  However,
    # determining the survey type from the first target file requires
    # reading it, which is expensive.  Do we really need to support strings
    # like this?

    # Allow sciencemask, stdmask, etc. to be int or string
    if (args.sciencemask is not None) and isinstance(args.sciencemask, str):
        args.sciencemask = desi_mask.mask(args.sciencemask.replace(",", "|"))

    if (args.stdmask is not None) and isinstance(args.stdmask, str):
        args.stdmask = desi_mask.mask(args.stdmask.replace(",", "|"))

    if (args.skymask is not None) and isinstance(args.skymask, str):
        args.skymask = desi_mask.mask(args.skymask.replace(",", "|"))

    if (args.safemask is not None) and isinstance(args.safemask, str):
        args.safemask = desi_mask.mask(args.safemask.replace(",", "|"))

    if (args.excludemask is not None) and isinstance(args.excludemask, str):
        args.excludemask = desi_mask.mask(args.excludemask.replace(",", "|"))

    # Set output directory
    if args.dir is None:
        if args.rundate is None:
            raise RuntimeError(
                "You must specify the output directory or the rundate")
        args.dir = "out_fiberassign_{}".format(args.rundate)

    return args
예제 #20
0
        'LRG': 'GALAXY',
        'QSO': 'QSO',
        'STD_FSTAR': 'STAR',
        'STD_BRIGHT': 'STAR'
    }
    dict_truetemplatetype = {
        'BGS_ANY': 'BGS',
        'ELG': 'ELG',
        'LRG': 'LRG',
        'QSO': 'QSO',
        'STD_FSTAR': 'STAR',
        'STD_BRIGHT': 'STAR'
    }

    for m in masks:
        istype = (targets['DESI_TARGET'] & desi_mask.mask(m)) != 0
        print(m, np.count_nonzero(istype))
        truth['TRUESPECTYPE'][istype] = np.repeat(dict_truespectype[m],
                                                  np.count_nonzero(istype))
        truth['TEMPLATETYPE'][istype] = np.repeat(dict_truetemplatetype[m],
                                                  np.count_nonzero(istype))
        truth['MOCKID'][istype] = targets['TARGETID'][istype]

    del targets
    print('writing truth')
    truth.write(truthfile, overwrite=True)
    print('done truth')

#generate sky data
from desitarget.mock.sky import random_sky
from desitarget.targetmask import desi_mask, bgs_mask, mws_mask, obsmask, obsconditions
예제 #21
0
        print('Unable to retrieve {}.'.format(fname))
        continue

    ##
    dat = Table(
        _fits[1].data
    )  ##  ['FIBER', 'DESI_TARGET', 'BGS_TARGET', 'SV1_DESI_TARGET', 'SV1_BGS_TARGET']

    ##  print(dat.columns)

    if applyto == 'assign':
        ##  Remove skies.
        isin = np.ones(len(dat), dtype=bool)

        for x in ds_dtypes:
            isin = isin & ((dat['DESI_TARGET'] & desi_mask.mask(x)) == 0)

        skies = dat[~isin]
        dat = dat[isin]

        ##
        dat.sort('FIBER')

    ##
    ttypes = ['faint', 'bright', 'faint_ext', 'lowq', 'fibmag']

    dat['GFLUX'] = dat['FLUX_G'] / dat['MW_TRANSMISSION_G']
    dat['RFLUX'] = dat['FLUX_R'] / dat['MW_TRANSMISSION_R']
    dat['ZFLUX'] = dat['FLUX_Z'] / dat['MW_TRANSMISSION_Z']

    dat['RFFLUX'] = dat['FIBERFLUX_R'] / dat['MW_TRANSMISSION_R']