def test_zcat(self): mtl = make_mtl(self.targets, self.zcat, trim=False) mtl.sort(keys='TARGETID') self.assertTrue(np.all(mtl['PRIORITY'] == self.post_prio)) self.assertTrue(np.all(mtl['NUMOBS_MORE'] == [0, 1, 0, 3, 1])) #- change one target to a SAFE (BADSKY) target and confirm priority=0 not 1 self.targets['DESI_TARGET'][0] = Mx.BADSKY mtl = make_mtl(self.targets, self.zcat, trim=False) mtl.sort(keys='TARGETID') self.assertEqual(mtl['PRIORITY'][0], 0)
def test_zcat(self): """Test priorities, numobs and obsconditions are set correctly after zcat. """ # ADM loop through once for SV and once for the main survey. for prefix in ["", "SV1_"]: t = self.reset_targets(prefix) mtl = make_mtl(t, self.zcat, trim=False) mtl.sort(keys='TARGETID') self.assertTrue(np.all(mtl['PRIORITY'] == self.post_prio)) self.assertTrue(np.all(mtl['NUMOBS_MORE'] == [0, 1, 0, 3, 1])) # - change one target to a SAFE (BADSKY) target and confirm priority=0 not 1 t[prefix+'DESI_TARGET'][0] = Mx.BAD_SKY mtl = make_mtl(t, self.zcat, trim=False) mtl.sort(keys='TARGETID') self.assertEqual(mtl['PRIORITY'][0], 0)
def test_numobs(self): mtl = make_mtl(self.targets) self.assertTrue(np.all(mtl['NUMOBS_MORE'] == [1, 2, 4])) self.assertTrue(np.all(mtl['PRIORITY'] == self.priorities)) iselg = (self.types == 'ELG') self.assertTrue(np.all(mtl['GRAYLAYER'][iselg] != 0)) self.assertTrue(np.all(mtl['GRAYLAYER'][~iselg] == 0))
def test_mtl_io(self): mtl = make_mtl(self.targets, self.zcat, trim=True) testfile = 'test-aszqweladfqwezceas.fits' mtl.write(testfile, overwrite=True) x = mtl.read(testfile) os.remove(testfile) if x.masked: self.assertTrue(np.all(mtl['NUMOBS_MORE'].mask == x['NUMOBS_MORE'].mask))
def test_numobs(self): """Test priorities, numobs and obsconditions are set correctly with no zcat. """ # ADM loop through once for SV and once for the main survey. mtl = make_mtl(self.targets, "GRAY|DARK") mtl.sort(keys='TARGETID') self.assertTrue(np.all(mtl['NUMOBS_MORE'] == [4, 4, 4])) self.assertTrue(np.all(mtl['PRIORITY'] == self.priorities))
def test_zcat(self): """Test priorities, numobs and obsconditions are set correctly after zcat. """ # ADM loop through once for SV and once for the main survey. mtl = make_mtl(self.targets, "DARK|GRAY", zcat=self.zcat, trim=False) mtl.sort(keys='TARGETID') self.assertTrue(np.all(mtl['PRIORITY'] == self.post_prio)) self.assertTrue(np.all(mtl['NUMOBS_MORE'] == self.post_numobs_more))
def test_numobs(self): mtl = make_mtl(self.targets) mtl.sort(keys='TARGETID') self.assertTrue(np.all(mtl['NUMOBS_MORE'] == [1, 2, 4, 4, 1])) self.assertTrue(np.all(mtl['PRIORITY'] == self.priorities)) #- Check that ELGs can be observed in gray conditions but not others iselg = (self.types == 'ELG') self.assertTrue(np.all((mtl['OBSCONDITIONS'][iselg] & obsconditions.GRAY) != 0)) self.assertTrue(np.all((mtl['OBSCONDITIONS'][~iselg] & obsconditions.GRAY) == 0))
def test_cmx_priorities(self): """Test that priority calculation can handle commissioning files. """ t = self.targets.copy() z = self.zcat # ADM restructure the table to look like a commissioning table. t.rename_column('DESI_TARGET', 'CMX_TARGET') t.remove_column('BGS_TARGET') t.remove_column('MWS_TARGET') # - No targeting bits set is priority=0 self.assertTrue(np.all(calc_priority(t, z, "GRAY|DARK") == 0)) # ADM retrieve the cmx_mask. colnames, masks, _ = main_cmx_or_sv(t) cmx_mask = masks[0] # ADM test handling of unobserved SV0_BGS and SV0_MWS for name, obscon in [("SV0_BGS", "BRIGHT"), ("SV0_MWS", "POOR")]: t['CMX_TARGET'] = cmx_mask[name] self.assertTrue( np.all( calc_priority(t, z, obscon) == cmx_mask[name].priorities['UNOBS'])) # ADM done is Done, regardless of ZWARN. for name, obscon in [("SV0_BGS", "BRIGHT"), ("SV0_MWS", "POOR")]: t['CMX_TARGET'] = cmx_mask[name] t["PRIORITY_INIT"], t["NUMOBS_INIT"] = initial_priority_numobs(t) # APC: Use NUMOBS_INIT here to avoid hardcoding NOBS corresponding to "done". numobs_done = t['NUMOBS_INIT'][0] z['NUMOBS'] = [0, numobs_done, numobs_done] z['ZWARN'] = [1, 1, 0] p = make_mtl(t, obscon, zcat=z)["PRIORITY"] self.assertEqual(p[0], cmx_mask[name].priorities['UNOBS']) self.assertEqual(p[1], cmx_mask[name].priorities['DONE']) self.assertEqual(p[2], cmx_mask[name].priorities['DONE']) # BGS ZGOOD targets always have lower priority than MWS targets that # are not DONE. lowest_bgs_priority_zgood = cmx_mask['SV0_BGS'].priorities[ 'MORE_ZGOOD'] lowest_mws_priority_unobs = cmx_mask['SV0_MWS'].priorities['UNOBS'] lowest_mws_priority_zwarn = cmx_mask['SV0_MWS'].priorities[ 'MORE_ZWARN'] lowest_mws_priority_zgood = cmx_mask['SV0_MWS'].priorities[ 'MORE_ZGOOD'] lowest_mws_priority = min(lowest_mws_priority_unobs, lowest_mws_priority_zwarn, lowest_mws_priority_zgood) self.assertLess(lowest_bgs_priority_zgood, lowest_mws_priority)
def test_mtl(self): """Test output from MTL has the correct column names. """ # ADM loop through once each for the main survey, commissioning and SV. # t = self.reset_targets(prefix) mtl = make_mtl(self.targets, "GRAY|DARK", trimcols=True) mtldm = switch_main_cmx_or_sv(mtldatamodel, mtl) refnames = sorted(mtldm.dtype.names) mtlnames = sorted(mtl.dtype.names) self.assertEqual(refnames, mtlnames)
def test_mtl(self): """Test output from MTL has the correct column names. """ # ADM loop through once each for the main survey, commissioning and SV. for prefix in ["", "CMX_", "SV1_"]: t = self.reset_targets(prefix) mtl = make_mtl(t) goodkeys = sorted(set(t.dtype.names) | set(['NUMOBS_MORE', 'PRIORITY', 'OBSCONDITIONS'])) mtlkeys = sorted(mtl.dtype.names) self.assertEqual(mtlkeys, goodkeys)
def test_zcat(self): """Test priorities, numobs and obsconditions are set correctly after zcat. """ # ADM loop through once for SV and once for the main survey. for prefix in ["", "SV1_"]: t = self.reset_targets(prefix) mtl = make_mtl(t, "DARK|GRAY", zcat=self.zcat, trim=False) mtl.sort(keys='TARGETID') pp = self.post_prio.copy() nom = [0, 0, 0, 3, 1] # ADM in SV, all quasars get all observations. if prefix == "SV1_": pp[2], nom[2] = pp[3], nom[3] self.assertTrue(np.all(mtl['PRIORITY'] == pp)) self.assertTrue(np.all(mtl['NUMOBS_MORE'] == nom)) # - change one target to a SAFE (BADSKY) target and confirm priority=0 not 1 t[prefix + 'DESI_TARGET'][0] = Mx.BAD_SKY mtl = make_mtl(t, "DARK|GRAY", zcat=self.zcat, trim=False) mtl.sort(keys='TARGETID') self.assertEqual(mtl['PRIORITY'][0], 0)
def test_mtl_io(self): """Test MTL correctly handles masked NUMOBS quantities. """ # ADM loop through once for SV and once for the main survey. for prefix in ["", "SV1_"]: t = self.reset_targets(prefix) mtl = make_mtl(t, self.zcat, trim=True) testfile = 'test-aszqweladfqwezceas.fits' mtl.write(testfile, overwrite=True) x = mtl.read(testfile) os.remove(testfile) if x.masked: self.assertTrue(np.all(mtl['NUMOBS_MORE'].mask == x['NUMOBS_MORE'].mask))
def test_numobs(self): """Test priorities, numobs and obsconditions are set correctly with no zcat. """ # ADM loop through once for SV and once for the main survey. for prefix in ["", "SV1_"]: t = self.reset_targets(prefix) mtl = make_mtl(t) mtl.sort(keys='TARGETID') self.assertTrue(np.all(mtl['NUMOBS_MORE'] == [1, 2, 4, 4, 1])) self.assertTrue(np.all(mtl['PRIORITY'] == self.priorities)) # - Check that ELGs can be observed in gray conditions but not others iselg = (self.types == 'ELG') self.assertTrue(np.all((mtl['OBSCONDITIONS'][iselg] & obsconditions.GRAY) != 0)) self.assertTrue(np.all((mtl['OBSCONDITIONS'][~iselg] & obsconditions.GRAY) == 0))
def test_endless_bgs(self): """Test BGS targets always get another observation in bright time. """ # ADM create a set of BGS FAINT/BGS_BRIGHT targets # ADM (perhaps) also another target class. bgstargets = self.targets.copy() bgstargets["DESI_TARGET"] = Mx["BGS_ANY"] bgstargets[ "BGS_TARGET"] = bgs_mask["BGS_FAINT"] | bgs_mask["BGS_BRIGHT"] # ADM set their initial conditions for the bright-time survey. pinit, ninit = initial_priority_numobs(bgstargets, obscon="BRIGHT") bgstargets["PRIORITY_INIT"] = pinit bgstargets["NUMOBS_INIT"] = ninit # ADM create a copy of the zcat. bgszcat = self.zcat.copy() # ADM run through MTL. mtl = make_mtl(bgstargets, obscon="BRIGHT", zcat=bgszcat) # ADM all BGS targets should always have NUMOBS_MORE=1. self.assertTrue(np.all(bgszcat["NUMOBS_MORE"] == 1))
def test_merged_qso(self): """Test QSO tracers that are also other target types get 1 observation. """ # ADM there are other tests of this kind in test_multiple_mtl.py. # ADM create a set of targets that are QSOs and # ADM (perhaps) also another target class. qtargets = self.targets.copy() qtargets["DESI_TARGET"] |= Mx["QSO"] # ADM give them all a "tracer" redshift (below a LyA QSO). qzcat = self.zcat.copy() qzcat["Z"] = 0.5 # ADM set their initial conditions to be that of a QSO. pinit, ninit = initial_priority_numobs(qtargets, obscon="DARK|GRAY") qtargets["PRIORITY_INIT"] = pinit qtargets["NUMOBS_INIT"] = ninit # ADM run through MTL. mtl = make_mtl(qtargets, obscon="DARK|GRAY", zcat=qzcat) # ADM all confirmed tracer quasars should have NUMOBS_MORE=0. self.assertTrue(np.all(qzcat["NUMOBS_MORE"] == 0))
def test_zcat(self): mtl = make_mtl(self.targets, self.zcat, trim=False) mtl.sort(keys='TARGETID') self.assertTrue(np.all(mtl['NUMOBS_MORE'] == [0, 1, 0, 3, 1])) self.assertTrue(np.all(mtl['PRIORITY'] == self.post_prio))
def test_mtl(self): mtl = make_mtl(self.targets) goodkeys = sorted(set(self.targets.dtype.names) | set(['NUMOBS_MORE', 'PRIORITY', 'OBSCONDITIONS'])) mtlkeys = sorted(mtl.dtype.names) self.assertEqual(mtlkeys, goodkeys)
def test_priorities(self): """Test that priorities are set correctly for both the main survey and SV. """ # ADM loop through once for SV and once for the main survey. for prefix in ["", "SV1_"]: t = self.targets.copy() z = self.zcat.copy() main_names = ['DESI_TARGET', 'BGS_TARGET', 'MWS_TARGET'] for name in main_names: t.rename_column(name, prefix + name) # ADM retrieve the mask and column names for this survey flavor. colnames, masks, _ = main_cmx_or_sv(t) desi_target, bgs_target, mws_target = colnames desi_mask, bgs_mask, mws_mask = masks # - No targeting bits set is priority=0 self.assertTrue(np.all(calc_priority(t, z) == 0)) # - test QSO > (LRG_1PASS | LRG_2PASS) > ELG t[desi_target] = desi_mask.ELG self.assertTrue( np.all( calc_priority(t, z) == desi_mask.ELG.priorities['UNOBS'])) t[desi_target] |= desi_mask.LRG_1PASS self.assertTrue( np.all( calc_priority(t, z) == desi_mask.LRG.priorities['UNOBS'])) t[desi_target] |= desi_mask.LRG_2PASS self.assertTrue( np.all( calc_priority(t, z) == desi_mask.LRG.priorities['UNOBS'])) t[desi_target] |= desi_mask.QSO self.assertTrue( np.all( calc_priority(t, z) == desi_mask.QSO.priorities['UNOBS'])) # - different states -> different priorities # - Done is Done, regardless of ZWARN. t[desi_target] = desi_mask.ELG t["PRIORITY_INIT"], t["NUMOBS_INIT"] = initial_priority_numobs(t) z['NUMOBS'] = [0, 1, 1] z['ZWARN'] = [1, 1, 0] p = make_mtl(t, z)["PRIORITY"] self.assertEqual(p[0], desi_mask.ELG.priorities['UNOBS']) self.assertEqual(p[1], desi_mask.ELG.priorities['DONE']) self.assertEqual(p[2], desi_mask.ELG.priorities['DONE']) # - BGS FAINT targets are never DONE, only MORE_ZGOOD. t[desi_target] = desi_mask.BGS_ANY t[bgs_target] = bgs_mask.BGS_FAINT t["PRIORITY_INIT"], t["NUMOBS_INIT"] = initial_priority_numobs(t) z['NUMOBS'] = [0, 1, 1] z['ZWARN'] = [1, 1, 0] p = make_mtl(t, z)["PRIORITY"] self.assertEqual(p[0], bgs_mask.BGS_FAINT.priorities['UNOBS']) self.assertEqual(p[1], bgs_mask.BGS_FAINT.priorities['MORE_ZWARN']) self.assertEqual(p[2], bgs_mask.BGS_FAINT.priorities['MORE_ZGOOD']) # BGS_FAINT: {UNOBS: 2000, MORE_ZWARN: 2000, MORE_ZGOOD: 1000, DONE: 2, OBS: 1, DONOTOBSERVE: 0} # - BGS BRIGHT targets are never DONE, only MORE_ZGOOD. t[desi_target] = desi_mask.BGS_ANY t[bgs_target] = bgs_mask.BGS_BRIGHT t["PRIORITY_INIT"], t["NUMOBS_INIT"] = initial_priority_numobs(t) z['NUMOBS'] = [0, 1, 1] z['ZWARN'] = [1, 1, 0] p = make_mtl(t, z)["PRIORITY"] self.assertEqual(p[0], bgs_mask.BGS_BRIGHT.priorities['UNOBS']) self.assertEqual(p[1], bgs_mask.BGS_BRIGHT.priorities['MORE_ZWARN']) self.assertEqual(p[2], bgs_mask.BGS_BRIGHT.priorities['MORE_ZGOOD']) # BGS_BRIGHT: {UNOBS: 2100, MORE_ZWARN: 2100, MORE_ZGOOD: 1000, DONE: 2, OBS: 1, DONOTOBSERVE: 0} # BGS targets are NEVER done even after 100 observations t[desi_target] = desi_mask.BGS_ANY t[bgs_target] = bgs_mask.BGS_BRIGHT t["PRIORITY_INIT"], t["NUMOBS_INIT"] = initial_priority_numobs(t) z['NUMOBS'] = [0, 100, 100] z['ZWARN'] = [1, 1, 0] p = calc_priority(t, z) self.assertEqual(p[0], bgs_mask.BGS_BRIGHT.priorities['UNOBS']) self.assertEqual(p[1], bgs_mask.BGS_BRIGHT.priorities['MORE_ZWARN']) self.assertEqual(p[2], bgs_mask.BGS_BRIGHT.priorities['MORE_ZGOOD']) # BGS ZGOOD targets always have lower priority than MWS targets that # are not DONE. # ADM first discard N/S informational bits from bitmask as these # ADM should never trump the other bits. bgs_names = [ name for name in bgs_mask.names() if 'NORTH' not in name and 'SOUTH' not in name ] mws_names = [ name for name in mws_mask.names() if 'NORTH' not in name and 'SOUTH' not in name ] lowest_bgs_priority_zgood = min( [bgs_mask[n].priorities['MORE_ZGOOD'] for n in bgs_names]) lowest_mws_priority_unobs = min( [mws_mask[n].priorities['UNOBS'] for n in mws_names]) lowest_mws_priority_zwarn = min( [mws_mask[n].priorities['MORE_ZWARN'] for n in mws_names]) lowest_mws_priority_zgood = min( [mws_mask[n].priorities['MORE_ZGOOD'] for n in mws_names]) lowest_mws_priority = min(lowest_mws_priority_unobs, lowest_mws_priority_zwarn, lowest_mws_priority_zgood) self.assertLess(lowest_bgs_priority_zgood, lowest_mws_priority)
''' Original version written by Anand Raichoor ''' ''' test ''' # need to run before: # source /global/cfs/cdirs/desi/software/desi_environment.sh master import os import numpy as np import fitsio import astropy.io.fits as fits import raichoorlib import sys from desimodel.focalplane.geometry import get_tile_radius_deg from desitarget.io import read_targets_in_tiles from desitarget.mtl import make_mtl import multiprocessing from argparse import ArgumentParser # reading arguments parser = ArgumentParser(allow_abbrev=True) parser.add_argument('--dr', help='legacypipe dr (e.g. dr8)', type=str, default=None, metavar='DR') parser.add_argument('--dtver', help='desitarget version (e.g. 0.39.0)', type=str, default=None,
def test_priorities(self): """Test that priorities are set correctly for both the main survey and SV. """ # ADM loop through once for SV and once for the main survey. for prefix in ["", "SV1_"]: t = self.targets.copy() z = self.zcat.copy() main_names = ['DESI_TARGET', 'BGS_TARGET', 'MWS_TARGET'] for name in main_names: t.rename_column(name, prefix + name) # ADM retrieve the mask and column names for this survey flavor. colnames, masks, _ = main_cmx_or_sv(t) desi_target, bgs_target, mws_target = colnames desi_mask, bgs_mask, mws_mask = masks # - No targeting bits set is priority=0 self.assertTrue(np.all(calc_priority(t, z, "BRIGHT") == 0)) # ADM test QSO > LRG > ELG for main survey and SV. t[desi_target] = desi_mask.ELG self.assertTrue( np.all( calc_priority(t, z, "GRAY|DARK") == desi_mask.ELG.priorities['UNOBS'])) t[desi_target] |= desi_mask.LRG self.assertTrue( np.all( calc_priority(t, z, "GRAY|DARK") == desi_mask.LRG.priorities['UNOBS'])) t[desi_target] |= desi_mask.QSO self.assertTrue( np.all( calc_priority(t, z, "GRAY|DARK") == desi_mask.QSO.priorities['UNOBS'])) # - different states -> different priorities # - Done is Done, regardless of ZWARN. t[desi_target] = desi_mask.ELG t["PRIORITY_INIT"], t["NUMOBS_INIT"] = initial_priority_numobs(t) z['NUMOBS'] = [0, 1, 1] z['ZWARN'] = [1, 1, 0] p = make_mtl(t, "GRAY|DARK", zcat=z)["PRIORITY"] self.assertEqual(p[0], desi_mask.ELG.priorities['UNOBS']) self.assertEqual(p[1], desi_mask.ELG.priorities['DONE']) self.assertEqual(p[2], desi_mask.ELG.priorities['DONE']) # ADM In BRIGHT conditions BGS FAINT targets are # ADM never DONE, only MORE_ZGOOD. t[desi_target] = desi_mask.BGS_ANY t[bgs_target] = bgs_mask.BGS_FAINT t["PRIORITY_INIT"], t["NUMOBS_INIT"] = initial_priority_numobs(t) z['NUMOBS'] = [0, 1, 1] z['ZWARN'] = [1, 1, 0] p = make_mtl(t, "BRIGHT", zcat=z)["PRIORITY"] self.assertEqual(p[0], bgs_mask.BGS_FAINT.priorities['UNOBS']) self.assertEqual(p[1], bgs_mask.BGS_FAINT.priorities['MORE_ZWARN']) self.assertEqual(p[2], bgs_mask.BGS_FAINT.priorities['MORE_ZGOOD']) # BGS_FAINT: {UNOBS: 2000, MORE_ZWARN: 2000, MORE_ZGOOD: 1000, DONE: 2, OBS: 1, DONOTOBSERVE: 0} # ADM but in DARK conditions, BGS_FAINT should behave as # ADM for other target classes. z = self.zcat.copy() z['NUMOBS'] = [0, 1, 1] z['ZWARN'] = [1, 1, 0] p = make_mtl(t, "DARK|GRAY", zcat=z)["PRIORITY"] self.assertEqual(p[0], bgs_mask.BGS_FAINT.priorities['UNOBS']) self.assertEqual(p[1], bgs_mask.BGS_FAINT.priorities['DONE']) self.assertEqual(p[2], bgs_mask.BGS_FAINT.priorities['DONE']) # ADM In BRIGHT conditions BGS BRIGHT targets are # ADM never DONE, only MORE_ZGOOD. t[desi_target] = desi_mask.BGS_ANY t[bgs_target] = bgs_mask.BGS_BRIGHT t["PRIORITY_INIT"], t["NUMOBS_INIT"] = initial_priority_numobs(t) z['NUMOBS'] = [0, 1, 1] z['ZWARN'] = [1, 1, 0] p = make_mtl(t, "BRIGHT", zcat=z)["PRIORITY"] self.assertEqual(p[0], bgs_mask.BGS_BRIGHT.priorities['UNOBS']) self.assertEqual(p[1], bgs_mask.BGS_BRIGHT.priorities['MORE_ZWARN']) self.assertEqual(p[2], bgs_mask.BGS_BRIGHT.priorities['MORE_ZGOOD']) # BGS_BRIGHT: {UNOBS: 2100, MORE_ZWARN: 2100, MORE_ZGOOD: 1000, DONE: 2, OBS: 1, DONOTOBSERVE: 0} # ADM In BRIGHT conditions BGS targets are # ADM NEVER done even after 100 observations t[desi_target] = desi_mask.BGS_ANY t[bgs_target] = bgs_mask.BGS_BRIGHT t["PRIORITY_INIT"], t["NUMOBS_INIT"] = initial_priority_numobs(t) z['NUMOBS'] = [0, 100, 100] z['ZWARN'] = [1, 1, 0] p = calc_priority(t, z, "BRIGHT") self.assertEqual(p[0], bgs_mask.BGS_BRIGHT.priorities['UNOBS']) self.assertEqual(p[1], bgs_mask.BGS_BRIGHT.priorities['MORE_ZWARN']) self.assertEqual(p[2], bgs_mask.BGS_BRIGHT.priorities['MORE_ZGOOD']) # BGS ZGOOD targets always have lower priority than MWS targets that # are not DONE. Exempting the MWS "BACKUP" targets. # ADM first discard N/S informational bits from bitmask as these # ADM should never trump the other bits. bgs_names = [ name for name in bgs_mask.names() if 'NORTH' not in name and 'SOUTH' not in name ] mws_names = [ name for name in mws_mask.names() if 'NORTH' not in name and 'SOUTH' not in name and 'BACKUP' not in name ] lowest_mws_priority_unobs = [ mws_mask[n].priorities['UNOBS'] for n in mws_names ] lowest_bgs_priority_zgood = np.min( [bgs_mask[n].priorities['MORE_ZGOOD'] for n in bgs_names]) # ADM MORE_ZGOOD and MORE_ZWARN are only meaningful if a # ADM target class requests more than 1 observation (except # ADM for BGS, which has a numobs=infinity exception) lowest_mws_priority_zwarn = [ mws_mask[n].priorities['MORE_ZWARN'] for n in mws_names if mws_mask[n].numobs > 1 ] lowest_mws_priority_zgood = [ mws_mask[n].priorities['MORE_ZGOOD'] for n in mws_names if mws_mask[n].numobs > 1 ] lowest_mws_priority = np.min( np.concatenate([ lowest_mws_priority_unobs, lowest_mws_priority_zwarn, lowest_mws_priority_zgood ])) self.assertLess(lowest_bgs_priority_zgood, lowest_mws_priority)
def test_mtl(self): mtl = make_mtl(self.targets) goodkeys = set(self.targets.dtype.names) | set(['NUMOBS_MORE', 'PRIORITY', 'GRAYLAYER']) self.assertTrue(set(mtl.dtype.names) == goodkeys, \ 'colname mismatch: {} vs. {}'.format( \ mtl.dtype.names, goodkeys))
from argparse import ArgumentParser ap = ArgumentParser() ap.add_argument("-s", "--src", help="Tractor/sweeps file or root directory with tractor/sweeps files") ap.add_argument("-d", "--dest", help="Output target selection file") ns = ap.parse_args() iter_truth = dtio.iter_files(ns.src, 'truth') iter_target = dtio.iter_files(ns.src, 'targets') alltruth = [] alltargets = [] for truth_file, target_file in zip(iter_truth, iter_target): alltruth.append(Table.read(truth_file)) alltargets.append(Table.read(target_file)) print(truth_file, target_file) targets = vstack(alltargets) truth = vstack(alltruth) mtl = mtl.make_mtl(targets) out_targets = os.path.join(ns.dest,'targets.fits') out_truth = os.path.join(ns.dest,'truth.fits') out_mtl = os.path.join(ns.dest,'mtl.fits') targets.write(out_targets, overwrite=True) truth.write(out_truth, overwrite=True) mtl.write(out_mtl, overwrite=True)
def mkmtl( obscon="DARK|GRAY", target_ra_min=0, target_ra_max=360, target_dec_min=-90, target_dec_max=90, outdir='/global/cscratch1/sd/ajross/fiberassigntest/fiducialtargets/temp/', target_sample='/project/projectdirs/desi/users/ajross/dr8tar/target_science_sample.fits' ): ''' initially copied from https://github.com/desihub/tutorials/blob/master/FiberAssignAlgorithms_Part2.ipynb ''' science_file = outdir + 'mtl_science.fits' std_file = outdir + 'mtl_std.fits' # Load the raw science / standard target sample and prune columns keep_columns = [ 'TARGETID', 'RA', 'DEC', 'RA_IVAR', 'DEC_IVAR', 'PMRA', 'PMDEC', 'PMRA_IVAR', 'PMDEC_IVAR', 'DESI_TARGET', 'BGS_TARGET', 'MWS_TARGET', 'SUBPRIORITY', 'BRICKNAME', 'BRICKID', 'BRICK_OBJID', 'PRIORITY_INIT', 'NUMOBS_INIT' ] fd = fitsio.FITS(target_sample) fdata = fd[1].read(columns=keep_columns) inside = np.where( np.logical_and( np.logical_and((fdata["RA"] > target_ra_min), (fdata["RA"] < target_ra_max)), np.logical_and((fdata["DEC"] > target_dec_min), (fdata["DEC"] < target_dec_max))))[0] targets_raw = fdata[inside] # Get the default target masks for this target file (filesurvey, filecol, def_sciencemask, def_stdmask, def_skymask, def_suppskymask, def_safemask, def_excludemask) = default_target_masks(targets_raw) print( "Detected targets for survey '{}', using bitfield column '{}'".format( filesurvey, filecol)) # Force our science and std masks to a more restrictive set. Only keep ELG, LRG and QSO targets. # Cut any targets with multiple of those set. # science_mask = 0 # science_mask |= desi_mask["LRG"].mask # science_mask |= desi_mask["ELG"].mask # science_mask |= desi_mask["QSO"].mask # # std_mask = 0 # std_mask |= desi_mask["STD_FAINT"].mask # std_mask |= desi_mask["STD_WD"].mask # std_mask |= desi_mask["STD_BRIGHT"].mask elg_rows = np.where( np.logical_and( np.logical_and( np.logical_and( np.bitwise_and(targets_raw["DESI_TARGET"], desi_mask["ELG"].mask), np.logical_not( np.bitwise_and(targets_raw["DESI_TARGET"], std_mask))), np.logical_not( np.bitwise_and(targets_raw["DESI_TARGET"], desi_mask["QSO"].mask))), np.logical_not( np.bitwise_and(targets_raw["DESI_TARGET"], desi_mask["LRG"].mask))))[0] qso_rows = np.where( np.logical_and( np.logical_and( np.logical_and( np.bitwise_and(targets_raw["DESI_TARGET"], desi_mask["QSO"].mask), np.logical_not( np.bitwise_and(targets_raw["DESI_TARGET"], std_mask))), np.logical_not( np.bitwise_and(targets_raw["DESI_TARGET"], desi_mask["ELG"].mask))), np.logical_not( np.bitwise_and(targets_raw["DESI_TARGET"], desi_mask["LRG"].mask))))[0] lrg_rows = np.where( np.logical_and( np.logical_and( np.logical_and( np.bitwise_and(targets_raw["DESI_TARGET"], desi_mask["LRG"].mask), np.logical_not( np.bitwise_and(targets_raw["DESI_TARGET"], std_mask))), np.logical_not( np.bitwise_and(targets_raw["DESI_TARGET"], desi_mask["QSO"].mask))), np.logical_not( np.bitwise_and(targets_raw["DESI_TARGET"], desi_mask["ELG"].mask))))[0] n_elg = len(elg_rows) n_qso = len(qso_rows) n_lrg = len(lrg_rows) science_rows = np.concatenate([elg_rows, qso_rows, lrg_rows]) std_rows = np.where( np.logical_and( np.bitwise_and(targets_raw["DESI_TARGET"], std_mask), np.logical_not( np.bitwise_and(targets_raw["DESI_TARGET"], science_mask))))[0] print("Using {} science and {} standards from input catalog".format( len(science_rows), len(std_rows))) # Split out the science and standard targets, although this is actually not necessary for passing # to fiberassign. science_targets = np.array(targets_raw[science_rows]) std_targets = np.array(targets_raw[std_rows]) # Close the input fits file so it doesn't take up extra memory del targets_raw fd.close() del fd # We have concatenated the 3 target types in the new table, so now the rows are # different: elg_rows = np.arange(n_elg, dtype=np.int64) qso_rows = np.arange(n_qso, dtype=np.int64) + n_elg lrg_rows = np.arange(n_lrg, dtype=np.int64) + n_elg + n_qso # Make the MTLs science_mtl = make_mtl(science_targets, "DARK|GRAY").as_array() if len(science_mtl) != len(science_targets): print("WARNING: science MTL has {} rows, input has {}".format( len(science_mtl), len(science_targets))) std_mtl = make_mtl(std_targets, "DARK|GRAY").as_array() if len(std_mtl) != len(std_targets): print("WARNING: standards MTL has {} rows, input has {}".format( len(std_mtl), len(std_targets))) # Delete the large intermediate arrays del science_targets del std_targets # Write MTLs if os.path.isfile(science_file): os.remove(science_file) with fitsio.FITS(science_file, "rw") as fd: fd.write(science_mtl) if os.path.isfile(std_file): os.remove(std_file) with fitsio.FITS(std_file, "rw") as fd: fd.write(std_mtl) print("{} science targets".format(len(science_mtl))) print(" {} ELG targets".format(len(elg_rows))) print(" {} QSO targets".format(len(qso_rows))) print(" {} LRG targets".format(len(lrg_rows))) print("{} std targets".format(len(std_mtl))) # We'll be loading later science MTLs as we go through the survey, so delete that now. # the standards are constant so we'll keep those in memory. del science_mtl