def test_mask_write(self): """Test that masks are written to file correctly. """ # ADM some meaningless magnitude limits and mask epochs. ml, me = 62.3, 2062.3 # ADM a keyword dictionary to write to the output file header. extra = {'BLAT': 'blat', 'FOO': 'foo'} # ADM test writing without HEALPixel-split. _, mxdir = io.write_masks(self.maskdir, self.allmx, maglim=ml, maskepoch=me, extra=extra) # ADM test writing with HEALPixel-split. _, mxdir = io.write_masks(self.maskdir, self.allmx, maglim=ml, maskepoch=me, extra=extra, nside=self.nside) # ADM construct the output directory and file name. mxd = io.find_target_files(self.maskdir, flavor="masks", maglim=ml, epoch=me) mxfn = io.find_target_files(self.maskdir, flavor="masks", maglim=ml, epoch=me, hp=self.pixnum[0]) # ADM check the output directory is as expected. self.assertEqual(mxdir, mxd) # ADM check all of the files were made in the correct place. fns = glob(os.path.join(mxdir, "masks-hp*fits")) self.assertEqual(len(fns), len(self.pixnum)+1) # ADM check the extra kwargs were written to the header. for key in extra: hdr = fitsio.read_header(mxfn, "MASKS") self.assertEqual(hdr[key].rstrip(), extra[key])
def targets2truthfiles(targets, basedir, nside=64, obscon=None): ''' Return list of mock truth files that contain these targets Args: targets: table with TARGETID column, e.g. from fiber assignment basedir: base directory under which files are found Options: nside (int): healpix nside for file directory grouping obscon (str): (observing conditions) None/dark/bright extra dir level Returns (truthfiles, targetids): truthfiles: list of truth filenames targetids: list of lists of targetids in each truthfile i.e. targetids[i] is the list of targetids from targets['TARGETID'] that are in truthfiles[i] ''' import healpy #import desitarget.mock.io as mockio from desitarget.io import find_target_files assert nside >= 2 #- TODO: what should be done with assignments without targets? targets = targets[targets['TARGETID'] != -1] theta = np.radians(90 - targets['TARGET_DEC']) phi = np.radians(targets['TARGET_RA']) pixels = healpy.ang2pix(nside, theta, phi, nest=True) truthfiles = list() targetids = list() for ipix in sorted(np.unique(pixels)): filename = find_target_files(basedir, flavor='truth', obscon=obscon, hp=ipix, nside=nside, mock=True) truthfiles.append(filename) ii = (pixels == ipix) targetids.append(np.asarray(targets['TARGETID'][ii])) return truthfiles, targetids