Beispiel #1
0
def BOSS_mask(sample):
    ''' read mangle polygon for specified sample 
    '''
    if sample == 'cmass-north':
        boss_poly = pymangle.Mangle(
            os.path.join(os.environ('BOSSSBI_DIR'), 'boss',
                         'mask_DR12v5_CMASS_North.ply'))
    else:
        boss_poly = pymangle.Mangle(
            os.path.join(os.path.dirname(os.path.realpath(__file__)), 'dat',
                         'boss_geometry_2014_05_28.ply'))
    return boss_poly
Beispiel #2
0
def mask(inl, maskl, md='veto', upper=True):
    if upper:
        RA = 'RA'
        DEC = 'DEC'
    else:
        RA = 'ra'
        DEC = 'dec'

    if md == 'foot':
        keep = np.zeros(
            inl.size,
            dtype='bool')  #object is outside of footprint unless it is found
    if md == 'veto':
        keep = np.ones(
            inl.size,
            dtype='bool')  #object is outside of veto mask unless it is found

    for mask in maskl:
        mng = pymangle.Mangle(mask)
        polyid = mng.polyid(inl[RA], inl[DEC])
        if md == 'foot':
            keep[polyid != -1] = True  #keep the object if a polyid is found
        if md == 'veto':
            keep[polyid !=
                 -1] = False  #do not keep the object if a polyid is found
        print(mask + ' done')
    return keep
Beispiel #3
0
def mask(inl,maskl,md='veto',upper = False):
        '''
        input: inl--data, mask--file names of the mask list
        output: list of True/False for whether a point should be masked
        '''
        if upper:
            RA = 'RA'
            DEC = 'DEC'
        else:
            RA = 'ra'
            DEC = 'dec'

        if md == 'foot':
                keep = np.zeros(inl.size,dtype='bool')  #object is outside of footprint unless it is found
        if md == 'veto':
                keep = np.ones(inl.size,dtype='bool')   #object is outside of veto mask unless it is found

        for mask in maskl:
                mng = pymangle.Mangle(mask)
                polyid = mng.polyid(inl[RA],inl[DEC])
                if md == 'foot':
                        keep[polyid!=-1] = True #keep the object if a polyid is found
                if md == 'veto':
                        keep[polyid!=-1] = False #do not keep the object if a polyid is found   
                print(mask+' done')
        return keep
def dr8_imaging_randoms(nrandoms, writetable=False):
    dr8_mask = pymangle.Mangle('footprints/sdss_dr8_footprint_dipomp.ply')

    ra_rand, dec_rand = dr8_mask.genrand(nrandoms)

    ra_rand, dec_rand = remove_bright_star_regions(ra_rand, dec_rand)

    ra_rand, dec_rand = remove_bad_field_regions(ra_rand, dec_rand)

    map4stack = hp.read_map('maps/smoothed_masked_planck.fits',
                            dtype=np.single)
    ls, bs = stacking.equatorial_to_galactic(ra_rand, dec_rand)
    outsidemaskbool = (map4stack[hp.ang2pix(2048, ls, bs, lonlat=True)] !=
                       hp.UNSEEN)
    planckidxs = np.where(outsidemaskbool)

    ra_rand, dec_rand = ra_rand[planckidxs], dec_rand[planckidxs]

    if writetable:
        t = Table()
        t['RA'] = np.array(ra_rand, dtype=np.float)
        t['DEC'] = np.array(dec_rand, dtype=np.float)

        t.write('random_catalogs/dr8rands.fits', format='fits', overwrite=True)

    return ra_rand, dec_rand
Beispiel #5
0
def createBooleanColumn(name, ra, dec, dict_mask=dict_mask):
	#print dict_mask[name]
	mng         = mangle.Mangle(dict_mask[name])
	polyid      = mng.polyid(ra,dec)
	tmp         = (polyid!=-1)
	col = fits.Column(name=name, format="L", array=tmp)
	return col
def construct_random_files(spec_file, geometry_file, output_cat, FACTOR=40):
    print os.path.isfile(spec_file), spec_file
    print os.path.isfile(geometry_file), geometry_file
    hd_s = fits.open(spec_file)[1].data
    Nspec = len(hd_s)
    print "N targeted in chunk = ", Nspec

    mng = mangle.Mangle(geometry_file)
    raR, decR = mng.genrand(Nspec * FACTOR)
    polyid_randoms = mng.polyid(raR, decR)

    ids_with_data = n.array(
        list(set(hd_s['tiling_polyid'][(hd_s['tiling_polyid'] > 0)])))

    mask = n.in1d(polyid_randoms, ids_with_data)
    print len(raR[mask]) * 1. / Nspec
    ra_col = fits.Column(name="RA", format='D', array=raR[mask])
    dec_col = fits.Column(name="DEC", format='D', array=decR[mask])
    polyid_col_r = fits.Column(name="tiling_polyid",
                               format='I',
                               array=polyid_randoms[mask])

    hd_r_columns = fits.ColDefs([ra_col, dec_col, polyid_col_r])
    hdu = fits.BinTableHDU.from_columns(hd_r_columns)

    if os.path.isfile(output_cat):
        os.remove(output_cat)
    hdu.writeto(output_cat)
Beispiel #7
0
def mask_creator(ra_ctr, dec_ctr, fov_x, fov_y, pixSize, mangle_fn):

    # Define the number of pixels of the output mask (AMICO style)
    nx = int(math.floor(fov_x / pixSize + 1))
    ny = int(math.floor(fov_y / pixSize + 1))

    # Set WCS system
    w = wcs.WCS(naxis=2)
    w.wcs.crpix = [int(nx / 2.0), int(ny / 2.0)]
    w.wcs.crval = [ra_ctr, dec_ctr]
    w.wcs.cdelt = np.array([pixSize, pixSize])
    w.wcs.ctype = ["RA---TAN", "DEC--TAN"]

    # Read the mangle polygons describing regions to be masked using pymangle
    mask = pymangle.Mangle(mangle_fn)

    # Loop over the mask image
    m = np.arange(nx * ny).reshape(ny, nx)
    for i in range(0, nx):
        for j in range(0, ny):
            # Transform to celestial coordinates
            this_ra, this_dec = w.wcs_pix2world(i, j, 0)
            m[j, i] = int(1 - mask.contains(this_ra, this_dec) + 0.5)

    return m, w.to_header()
Beispiel #8
0
 def __init__(self, mask=None, path=None, rng=None, seed=None):
     self.mask = mask
     if path is not None:
         import pymangle
         self.logger.info('Loading geometry file: {}.'.format(path))
         self.mask = pymangle.Mangle(path)
     self.rng = rng
     if rng is None: self.rng = scipy.random.RandomState(seed=seed)
def remove_bad_field_regions(ras, decs):
    bad_field_mask = pymangle.Mangle(
        'footprints/badfield_mask_unphot_seeing_extinction_pixs8_dr12.ply')
    in_mask = bad_field_mask.contains(ras, decs)

    ras = ras[np.logical_not(in_mask)]
    decs = decs[np.logical_not((in_mask))]

    return ras, decs
 def createBooleanColumn(name, ra, dec, dict_mask=dict_mask):
     #print dict_mask[name]
     mng = mangle.Mangle(dict_mask[name])
     polyid, w = mng.polyid_and_weight(ra, dec)
     #print polyid, w
     tmp = (polyid != -1)
     col = fits.Column(name=name, format="L", array=tmp)
     colw = fits.Column(name=name + "_w", format="D", array=w)
     return col, colw
def remove_bright_star_regions(ras, decs):
    bright_star_mask = pymangle.Mangle(
        'footprints/allsky_bright_star_mask_pix.ply')
    in_mask = bright_star_mask.contains(ras, decs)

    ras = ras[np.logical_not(in_mask)]
    decs = decs[np.logical_not((in_mask))]

    return ras, decs
Beispiel #12
0
    def apply_mangle(self, lon=None, lat=None):

        try:
            m = mangle.Mangle(self.filename)
        except:
            raise Exception(f"Unable to open mangle mask file: {self.filename}")

        m_mask = m.contains(lon, lat)

        return m_mask
Beispiel #13
0
def BOSS_mask(sample):
    ''' read mangle polygon for specified sample 
    '''
    if sample == 'lowz-south':
        f_poly = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                              'dat', 'mask_DR12v5_LOWZ_South.ply')
    else:
        raise NotImplementedError
    boss_poly = pymangle.Mangle(f_poly)
    return boss_poly
def read_data(target=None,
              cap=None,
              data_name=None,
              rand_name=None,
              survey_comp=None,
              survey_geometry=None,
              nbar_name=None):

    #-- Read real data
    if data_name is None:
        data_name = f'/mnt/lustre/eboss/DR16_{target}_data/v7/eBOSS_{target}_full_{cap}_v7.dat.fits'
        rand_name = f'/mnt/lustre/eboss/DR16_{target}_data/v7/eBOSS_{target}_full_{cap}_v7.ran.fits'
        nbar_name = f'/mnt/lustre/eboss/DR16_{target}_data/v7/nbar_eBOSS_{target}_{cap}_v7.dat'
        survey_comp = f'/mnt/lustre/eboss/DR16_{target}_data/v7/eBOSS_{target}geometry_v7.fits'
        survey_geometry = '/mnt/lustre/eboss/DR16_geometry/eboss_geometry_eboss0_eboss27'

    print('Reading data from ', data_name)
    data = Table.read(data_name)
    print('Reading randoms from ', rand_name)
    rand = Table.read(rand_name)

    #-- Cut on COMP_BOSS > 0.5 (there's no mocks outside)
    wd = (data['COMP_BOSS'] > 0.5) & (data['sector_SSR'] > 0.5)
    data = data[wd]
    wr = (rand['COMP_BOSS'] > 0.5) & (rand['sector_SSR'] > 0.5)
    rand = rand[wr]

    #-- Removing NaN (just to avoid annoying warning messages)
    w = np.isnan(data['Z'])
    data['Z'][w] = -1

    #-- Read mask files
    print('Reading completeness info from ', survey_comp)
    survey_comp = Table.read(survey_comp)
    sec_comp = unique(survey_comp['SECTOR', 'COMP_BOSS'])
    geo_sector = sec_comp['SECTOR']
    geo_comp = sec_comp['COMP_BOSS']

    print('Reading survey geometry from ', survey_geometry)
    mask_fits = Table.read(survey_geometry + '.fits')
    mask_ply = pymangle.Mangle((survey_geometry + '.ply'))

    mask_dict = {
        'geo_sector': geo_sector,
        'geo_comp': geo_comp,
        'mask_ply': mask_ply,
        'mask_fits': mask_fits
    }

    nbar = read_nbar(nbar_name)

    return data, rand, mask_dict, nbar
Beispiel #15
0
def random_veto(sample='lowz-south'):
    ''' construct random catalog of RA, Dec with veto mask applied
    '''
    fvetos = [
        'badfield_mask_postprocess_pixs8.ply',
        'badfield_mask_unphot_seeing_extinction_pixs8_dr12.ply',
        'allsky_bright_star_mask_pix.ply', 'bright_object_mask_rykoff_pix.ply',
        'centerpost_mask_dr12.ply', 'collision_priority_mask_dr12.ply'
    ]

    veto_dir = os.path.join(os.environ['QUIJOTE_DIR'], 'chang', 'simbig')
    for i, fveto in enumerate(fvetos):
        if i == 0:
            if sample == 'lowz-south':
                frand = h5py.File(
                    os.path.join(os.environ['QUIJOTE_DIR'], 'chang',
                                 'random_DR12v5_LOWZ_South.hdf5'), 'r')
            elif sample == 'cmass-south':
                frand = h5py.File(
                    os.path.join(os.environ['QUIJOTE_DIR'], 'chang',
                                 'random_DR12v5_CMASS_South.hdf5'), 'r')
        else:
            if sample == 'lowz-south':
                frand = h5py.File(
                    os.path.join(os.environ['QUIJOTE_DIR'], 'chang',
                                 'random_DR12v5_LOWZ_South.veto.hdf5'), 'r')
            elif sample == 'cmass-south':
                frand = h5py.File(
                    os.path.join(os.environ['QUIJOTE_DIR'], 'chang',
                                 'random_DR12v5_CMASS_South.veto.hdf5'), 'r')
        rand_ra = frand['ra'][...]
        rand_dec = frand['dec'][...]
        frand.close()

        veto = pymangle.Mangle(os.path.join(veto_dir, fveto))
        w_veto = veto.weight(rand_ra, rand_dec)
        in_veto = (w_veto > 0.)

        # save points outside of veto mask to hdf5 file
        if sample == 'lowz-south':
            f = h5py.File(
                os.path.join(os.environ['QUIJOTE_DIR'], 'chang',
                             'random_DR12v5_LOWZ_South.veto.hdf5'), 'w')
        elif sample == 'cmass-south':
            f = h5py.File(
                os.path.join(os.environ['QUIJOTE_DIR'], 'chang',
                             'random_DR12v5_CMASS_South.veto.hdf5'), 'w')
        f.create_dataset('ra', data=rand_ra[~in_veto])
        f.create_dataset('dec', data=rand_dec[~in_veto])
        f.close()
    return None
def eBOSS_randoms(nrandoms):
    eBOSS_mask = pymangle.Mangle(
        'footprints/eBOSS_QSOandLRG_fullfootprintgeometry_noveto.ply')

    ra_rand, dec_rand = eBOSS_mask.genrand(nrandoms)

    centerpost_mask = pymangle.Mangle(
        'footprints/centerpost_mask_eboss_DR16_new.ply')
    in_centerpost_mask = centerpost_mask.contains(ra_rand, dec_rand)

    # remove randoms inside centerpost mask
    ra_rand = ra_rand[np.logical_not(in_centerpost_mask)]
    dec_rand = dec_rand[np.logical_not(in_centerpost_mask)]

    ra_rand, dec_rand = remove_bright_star_regions(ra_rand, dec_rand)

    t = Table()
    t['RA'] = np.array(ra_rand, dtype=np.float)
    t['DEC'] = np.array(dec_rand, dtype=np.float)

    t.write('random_catalogs/ebossrands.fits', format='fits', overwrite=True)

    return ra_rand, dec_rand
def BOSS_randoms(nrandoms, writetable=False):
    BOSS_mask = pymangle.Mangle('footprints/sdss_boss_dipomp.ply')
    ra_rand, dec_rand = BOSS_mask.genrand(nrandoms)

    if writetable:
        t = Table()
        t['RA'] = np.array(ra_rand, dtype=np.float)
        t['DEC'] = np.array(dec_rand, dtype=np.float)

        t.write('random_catalogs/bossrands.fits',
                format='fits',
                overwrite=True)

    return ra_rand, dec_rand
Beispiel #18
0
def prep_indes(nside=256, fname=None):
    if fname == None:
        fname = os.getenv(
            'CSCRATCH'
        ) + '/BGS/SV-ASSIGN/des/des.ply'  ##  '/global/cscratch1/sd/raichoor/desits/des.ply'

    ##
    nside = np.int(nside)
    npix = hp.nside2npix(nside)

    # checking hp pixels
    mng = pymangle.Mangle(fname)

    theta, phi = hp.pix2ang(nside, np.arange(0, npix, 1), nest=False)
    hpra, hpdec = 180. / np.pi * phi, 90. - 180. / np.pi * theta

    allhp = mng.polyid(hpra, hpdec)

    hpindes = (allhp != -1).astype(int)

    # pixels with all neighbours in des.
    hpindes_secure = np.array([
        i for i in range(npix)
        if hpindes[i] + hpindes[hp.get_all_neighbours(nside, i)].sum() == 9
    ])

    # pixels with all neighbours outside des.
    hpoutdes_secure = np.array([
        i for i in range(npix)
        if hpindes[i] + hpindes[hp.get_all_neighbours(nside, i)].sum() == 0
    ])

    # hpind to be checked
    tmp = np.ones(npix, dtype=bool)
    tmp[hpindes_secure] = False
    tmp[hpoutdes_secure] = False

    hp_tbc = np.array(range(npix))[tmp]

    np.savetxt(os.getenv('CSCRATCH') + '/BGS/SV-ASSIGN/des/hpindes_secure.txt',
               hpindes_secure,
               fmt='%d')
    np.savetxt(os.getenv('CSCRATCH') + '/BGS/SV-ASSIGN/des/hp_tbc.txt',
               hp_tbc,
               fmt='%d')
Beispiel #19
0
def BOSS_veto(ra, dec):
    ''' given RA and Dec, find the objects that fall within one of the veto 
    masks of BOSS. At the moment it checks through the veto masks one by one.  
    '''
    in_veto = np.zeros(len(ra)).astype(bool)
    fvetos = [
        'badfield_mask_postprocess_pixs8.ply',
        'badfield_mask_unphot_seeing_extinction_pixs8_dr12.ply',
        'allsky_bright_star_mask_pix.ply', 'bright_object_mask_rykoff_pix.ply',
        'centerpost_mask_dr12.ply', 'collision_priority_mask_dr12.ply'
    ]
    for fveto in fvetos:
        veto = pymangle.Mangle(
            os.path.join(os.path.dirname(os.path.realpath(__file__)), 'dat',
                         fveto))
        w_veto = veto.weight(ra, dec)
        in_veto = in_veto | (w_veto > 0.)
    return in_veto
Beispiel #20
0
def wise_mask(ra, dec, mangle_files=wise_mangles):
    """
    Takes a list of RA and DEC and returns the indices which are permissible
    
    Uses equatorial coordinates, converts them to galactic, does the thing, and
    returns the indices.
    
    Dependencies: numpy, astropy.coordinates.SkyCoord, mangle
    
    performance: medium (probably max)
    
    @params
        ra_list, dec_list
            The coordinate pairs to be compared to the footprint
        mangle_files = wise_mangles
            The iterable of full filepaths to the .ply files to be used for
            masking
        galactic = True
        
    @returns indices
            The indices of ra and dec which are not masked 
    """
    coordinates = SkyCoord(ra=np.array(ra, dtype=float) * u.degree,
                           dec=np.array(dec, dtype=float) * u.degree,
                           frame='icrs')
    coordinates = coordinates.transform_to("galactic")
    ga_ra_list = coordinates.l.deg
    ga_dec_list = coordinates.b.deg
    m = []
    for path in mangle_files:
        m.append(pymangle.Mangle(path))
    ins = []
    for j in range(0, len(m)):
        ins.append(m[j].contains(ga_ra_list, ga_dec_list))
    ins_true = ins[0]
    for j in range(1, len(m)):
        ins_true = np.logical_or(ins_true, ins[j])
    return np.logical_not(ins_true)
Beispiel #21
0
def hp_from_mangle(weight_ply_files,
                   nside=None,
                   veto_ply_files=None,
                   hp_coords='equ',
                   verbose=False,
                   coords=None,
                   return_coords=False):
    """
    Rasterize a set of mangle ply files to a healpix map.

    Parameters
    ----------

    weight_ply_files : list of strings
        List of filenames of mangle ply files from which weights for the output mask will be
        extracted. The weights from different files will be summed into the same output map,
        so if a pixel is masked in one file, it need not be masked in the final map if a
        different file contains a non-zero weight for it.

    nside : int, optional
        Healpix map nside parameter. If coords is provided, this is optional.

    veto_ply_files : list of strings, optional
        List of filenames of mangle ply files containing regions that need to be masked
        or vetoed. A region only needs to exist in at least one of the provided files
        in order to be masked.

    hp_coords : string, optional
        The coordinate system of the output healpix map. Use 'fk5','j2000','equatorial' or 'equ'
        for Equatorial, which is the default. Use 'galactic' or 'gal' for Galactic.

    verbose : bool, optinal
        Whether to print more information. Defaults to False.

    coords : optional, (2,npix) array
        Pre-calculated ra,dec values corresponding to each pixel in the output healpix map.
        If not provided, nside has to be provided and the coords will be calculated 
        based on the coordinate system hp_coords.

    return_coords : bool
        Whether to return the ra,dec values corresponding to each pixel in the output healpix map.
        Defaults to False.

    Returns
    -------

    output : 1d numpy array
        The resulting healpix map.

    coords : optional, (2,npix) array
        The ra,dec values corresponding to each pixel in the output healpix map.
        Only returned if return_coords is True.

    """

    import pymangle

    if coords is None:
        npix = hp.nside2npix(nside)
        pixs = np.arange(npix, dtype=np.int)
        if verbose: print("Converting healpix pixels to coordinates.")
        ra, dec = hp.pix2ang(nside, pixs, lonlat=True)

        eq_coords = ['fk5', 'j2000', 'equatorial', 'equ']
        gal_coords = ['galactic', 'gal']

        if hp_coords in gal_coords:
            if verbose: print("Transforming coords...")
            from astropy.coordinates import SkyCoord
            import astropy.units as u

            gc = SkyCoord(ra * u.degree, dec * u.degree, frame='galactic')
            equ = gc.transform_to('fk5')
            ra = equ.ra.deg
            dec = equ.dec.deg
    else:
        ra, dec = coords

    output = 0
    for filename in weight_ply_files:
        if verbose: print(f"Reading weight file {filename}...")
        m = pymangle.Mangle(filename)
        output = output + m.weight(ra, dec)

    if veto_ply_files is None: veto_ply_files = []
    for veto in veto_ply_files:
        if verbose: print(f"Reading veto file {veto}...")
        m = pymangle.Mangle(veto)
        bad = m.contains(ra, dec)
        output[bad] = 0

    if return_coords:
        return output, np.asarray((ra, dec))
    else:
        return output
        cm_ratio = (cm_nrand / cm_ndata)
        return eb_ratio, cm_ratio

    eb_ratio, cm_ratio = get_ratios(eb_data, eb_rand, cm_data, cm_rand)
    print(f'  ratios before: {eb_ratio:.2f} {cm_ratio:.2f}')
    np.random.seed(imock)
    if eb_ratio > cm_ratio:
        w = np.random.rand(len(eb_rand)) < cm_ratio / eb_ratio
        eb_rand = eb_rand[w]
    else:
        w = np.random.rand(len(cm_rand)) < eb_ratio / cm_ratio
        cm_rand = cm_rand[w]
    eb_ratio, cm_ratio = get_ratios(eb_data, eb_rand, cm_data, cm_rand)
    print(f'  ratios after: {eb_ratio:.2f} {cm_ratio:.2f}')

    m = pymangle.Mangle(mply)
    geo_sector = Table.read(mfits, format='fits', hdu=1)['SECTOR']
    try:
        mask = Table.read(eboss_comp, format='fits',
                          hdu=1)['SECTOR', 'COMP_BOSS', 'sector_SSR']
    except:
        mask = Table.read(eboss_comp, format='fits',
                          hdu=1)['SECTOR', 'COMP_BOSS', 'SPEC_SUCCESS_RATE']
        mask.rename_column('SPEC_SUCCESS_RATE', 'sector_SSR')
    mask = unique(mask)

    for cm, eb in zip([cm_data, cm_rand], [eb_data, eb_rand]):

        print('> Getting eBOSS sectors for CMASS data')
        ids = m.polyid(cm['RA'], cm['DEC'])
        sectors = geo_sector[ids] * (ids != -1) - 1 * (ids == -1)
Beispiel #23
0
    6199270,6371066,6547876,6551972,6645991,6711673,6735965,6744444,6744445, \
    6748540,6752636,6769023,6773119,6781133])

conversion = constants.degree
theta_rad = catalog['DEC'] * conversion
phi_rad = catalog['RA'] * conversion
theta_rad = constants.pi / 2. - theta_rad
phi_rad %= 2. * constants.pi
pix = healpy.ang2pix(nside, theta_rad, phi_rad, nest=nest)
mask = in1d(pix, mask_pixel)

mskbit += mask * 2**8

# adding 2**9 for centerpost masking
mask_centerpost = '{}/ELG_centerpost.ply'.format(mdir)
mask_mng = pymangle.Mangle(mask_centerpost)
mask = mask_mng.polyid(catalog['RA'], catalog['DEC']) != -1
mskbit += mask * 2**9

# adding 2**10 for tdss_fes masking
mask_tdss = '{}/ELG_TDSSFES_62arcsec.pix.snap.balk.ply'.format(mdir)
mask_mng = pymangle.Mangle(mask_tdss)
mask = mask_mng.polyid(catalog['RA'], catalog['DEC']) != -1
mskbit += mask * 2**10

# adding 2**11 for bad photometric calibration
mask_badexp = '{}/ebosselg_badphot.26Aug2019.ply'.format(mdir)
mask_mng = pymangle.Mangle(mask_badexp)
mask = mask_mng.polyid(catalog['RA'], catalog['DEC']) != -1
mskbit += mask * 2**11
Beispiel #24
0
def read_data(target=None, cap=None, data_name=None, rand_name=None, 
    survey_comp=None, survey_geometry=None, nbar_name=None):

    #-- Read real data 
    if data_name is None:
        data_name =   f'/mnt/lustre/eboss/DR16_{target}_data/v7/eBOSS_{target}_full_{cap}_v7.dat.fits'
        rand_name =   f'/mnt/lustre/eboss/DR16_{target}_data/v7/eBOSS_{target}_full_{cap}_v7.ran.fits'
        nbar_name =   f'/mnt/lustre/eboss/DR16_{target}_data/v7/nbar_eBOSS_{target}_{cap}_v7.dat'
        survey_comp = f'/mnt/lustre/eboss/DR16_{target}_data/v7/eBOSS_{target}geometry_v7.fits'
        survey_geometry = '/mnt/lustre/eboss/DR16_geometry/eboss_geometry_eboss0_eboss27'
    
        
    print('Reading data from ', data_name)
    data = Table.read(data_name)
    print('Reading randoms from ', rand_name)
    rand = Table.read(rand_name)

    #-- Cut on COMP_BOSS > 0.5 (there's no mocks outside)
    wd =  (data['COMP_BOSS'] >0.5) 
    print(f' Cutting data COMP_BOSS > 0.5: {np.sum(wd)}/{wd.size}')
    wd &= (data['sector_SSR']>0.5)
    print(f' Cutting data sector_SSR > 0.5: {np.sum(wd)}/{wd.size}')
    data = data[wd]
    wr =  (rand['COMP_BOSS'] >0.5) 
    print(f' Cutting rand COMP_BOSS > 0.5: {np.sum(wr)}/{wr.size}')
    wr &= (rand['sector_SSR']>0.5)
    print(f' Cutting rand sector_SSR > 0.5: {np.sum(wr)}/{wr.size}')
    rand = rand[wr]

    #-- Removing NaN (just to avoid annoying warning messages) 
    w = np.isnan(data['Z'])
    data['Z'][w] = -1

    #-- Read mask files
    print('Reading completeness info from ', survey_comp)
    survey_comp = Table.read(survey_comp) 
    sec_comp = unique(survey_comp['SECTOR', 'COMP_BOSS', 'SECTORAREA'])

    print('Reading survey geometry from ', survey_geometry)
    mask_fits = Table.read(survey_geometry+'.fits')
    mask_ply = pymangle.Mangle((survey_geometry+'.ply'))

    mask_fits['AREA'] = mask_ply.areas*1.
    usect = np.unique(mask_fits['SECTOR'])
    for sec in usect:
        w = mask_fits['SECTOR'] == sec
        area = np.sum(mask_fits['AREA'][w])
        mask_fits['SECTORAREA'][w] = area*1.

    for i in range(len(sec_comp)):
        sec = sec_comp['SECTOR'][i]
        w = mask_fits['SECTOR'] == sec
        sec_comp['SECTORAREA'][i] = mask_fits['SECTORAREA'][w][0]


    mask_dict = {'sector': sec_comp['SECTOR'], 
                 'comp_boss': sec_comp['COMP_BOSS'],
                 'sector_area': sec_comp['SECTORAREA'],
                 'mask_ply': mask_ply,
                 'mask_fits': mask_fits}

    nbar = read_nbar(nbar_name)
        
    return data, rand, mask_dict, nbar
Beispiel #25
0
def eBOSS_ELG_mask(ra, dec, mskdir, mskbit=None):
    '''
  Assign extra masks to eBOSS ELGs.

  Parameters
  ----------
  ra: array_like
        Right Ascension of the catalogue.
  dec: array_like
        Declination of the catalogue.
  mskdir: str
        Directory containing the following mask files:
        * ELG_centerpost.ply
        * ELG_TDSSFES_62arcsec.pix.snap.balk.ply
        * ebosselg_badphot.26Aug2019.ply
        These files are available at:
        https://data.sdss.org/sas/dr16/eboss/lss/catalogs/DR16/ELGmasks
  mskbit: array_like, optional
        Maskbit codes assigned by the `brickmask` program.
        See https://github.com/cheng-zhao/brickmask

  Returns
  -------
  mask: array
        Maskbit codes with extra masks.

  References
  ----------
  * https://doi.org/10.1093/mnras/staa3336 (https://arxiv.org/abs/2007.09007)
  * https://doi.org/10.1093/mnras/stab510  (https://arxiv.org/abs/2007.08997)
  '''
    # Validate inputs
    ra = np.atleast_1d(ra)
    dec = np.atleast_1d(dec)
    if mskbit is None:
        mask = np.zeros_like(ra, dtype='int16')
    else:
        mask = copy.copy(np.atleast_1d(mskbit)).astype('int16')

    if len(ra) != len(dec) or len(ra) != len(mask):
        raise ValueError(
            'ra, dec, and mskbit (if set) must have the same length')

    # Add 2**8 for discrepancy between mskbit and anymask
    mask_pixel = [2981667,3464728,3514005,3645255,4546075,4685432,5867869, \
          5933353,6031493,6072514,6080368,6092477,6301369,6408277,6834661, \
          2907700,3583785,3587880,4067035,4669088,6007074,6186688,6190785, \
          6199270,6371066,6547876,6551972,6645991,6711673,6735965,6744444, \
          6744445,6748540,6752636,6769023,6773119,6781133]
    theta = np.radians(90 - dec)
    phi = np.radians(360 - ra)
    pix = hp.pixelfunc.ang2pix(1024, theta, phi, nest=False, lonlat=True)
    bit = in1d(pix, mask_pixel)
    mask += bit * 2**8

    # Add bit codes for polygon masks
    plys = ['ELG_centerpost.ply', 'ELG_TDSSFES_62arcsec.pix.snap.balk.ply', \
           'ebosselg_badphot.26Aug2019.ply']
    ply_codes = [9, 10, 11]  # Bit code for the polygon masks

    for ply, code in zip(plys, ply_codes):
        iply = '{}/{}'.format(mskdir, ply)
        m = pymangle.Mangle(iply)
        bit = m.polyid(ra, dec) != -1
        mask += bit * 2**code

    return mask
def fourmost_get_survbit_indiv(ra,dec,bname):
	#
	# Galactic l,b
	c       = SkyCoord(ra=ra, dec=dec, frame='fk5')
	l,b     = c.galactic.l.value,c.galactic.b.value
	lon,lat = c.barycentrictrueecliptic.lon.degree,c.barycentrictrueecliptic.lat.degree
	# 4most/s8 , s8elg
	if (bname in ['s8elg','s8']):
		iss8,iss8elg = fourmost_get_s8foot(ra,dec)
		if (bname=='s8elg'):
			keep = iss8elg
		else: # s8
			keep = iss8
	# desi: no ply file for the moment...
	elif (bname=='desi'):
		# desi sgc
		polyra = np.array([0 ,-25,-35,-50,-54,-45,10,10,  60, 70, 70,53,42,42,38,0])
		polydec= np.array([33,33, 25,  8,  -8, -15,-15,-20,-20,-15,0, 0, 10,20,33,33])
		sgcpoly = Path(np.concatenate(
							(polyra. reshape((len(polyra),1)),
							 polydec.reshape((len(polyra),1))),
							axis=1))
		# desi ngc
		polyra = np.array([275,107,115,130,230,230,230,255,265,275])
		polydec= np.array([33, 33, 12, -10,-10, -2, -2, -2,  13, 33])
		ngcpoly = Path(np.concatenate(
							(polyra. reshape((len(polyra),1)),
							 polydec.reshape((len(polyra),1))),
							axis=1))
		#
		tmpradec         = np.transpose(np.array([ra,dec]))
		tmp              = (ra>300)
		tmpradec[tmp,0] -= 360.
		keep = np.zeros(len(ra),dtype=bool)
		for poly in [sgcpoly,ngcpoly]:
			keep[poly.contains_points(tmpradec)] = True
	elif (bname=='erosita'):
		# johan email 30/07/2018 14:12
		keep = (abs(b)>15) & (l>180)
	elif (bname=='waveswide'):
		# https://wavesurvey.org/project/survey-design/
		keep = (((ra>155) & (ra<240) & (dec>-5) & (dec<5))
				|
				(((ra>330) | (ra<50)) & (dec>-36) & (dec<-26)))
	elif (bname=='euclid'):
		keep = (np.abs(b)>=30) & (np.abs(lat)>5.)
	else:
		if (bname[:3]=='vhs'):
			## -70<dec<0
			mng    = pymangle.Mangle(MASKDIR+'/vhsdec.ply')
			polyid = mng.polyid(ra,dec)
			keepdec= (polyid!=-1)
			# |b|>bmin
			mng    = pymangle.Mangle(MASKDIR+'/'+bname[3:6]+'.ply')
			polyid = mng.polyid(l,b)
			keepb  = (polyid!=-1)
			##
			keep   = (keepdec) & (keepb)
		else:
			mng    = pymangle.Mangle(MASKDIR+'/'+bname+'.ply')
			polyid = mng.polyid(ra,dec)
			keep   = (polyid!=-1)
		if (bname=='vhsb20clean'):
			## Jext<0.1 and low nstar selection [both VHS and DES]
			ra60    = (ra>55)  & (ra<65)  & (dec>-5)  & (dec<0)
			ra70    = (ra>67)  & (ra<72)  & (dec>-16) & (dec<-13)
			ra100   = (ra>50)  & (ra<180) & (dec>-20) & (dec<0)   & (b>-23) & (b<0)
			ra120   = (ra>100) & (ra<180)                         & (b>-15) & (b<0)
			ra230   = (ra>228) & (ra<270) & (dec>-40) & (dec<-20) & (b>0) 
			ra250   = (ra>235) & (ra<270) & (dec>-20) & (dec<0)   & (b>0)
			ra300   = (ra>180) & (ra<360) & (dec>-70) & (dec<0)   & (b>-25) & (b<0)
			LMC     = (ra>70)  & (ra<90)  & (dec>-70) & (dec<-65)
			keep    = ((keep) & 
						(~ra60)  & (~ra70)  & 
						(~ra100) & (~ra120) & 
						(~ra230) & (~ra250) & (~ra300) &
						(~LMC))
	print( bname, len(ra[keep]))
	return keep
def construct_summary_files_data(spec_file,
                                 phot_file,
                                 geometry_file,
                                 output_phot_cat,
                                 rmax=0.5 / 3600.):
    # spectroscopy
    print os.path.isfile(spec_file), spec_file
    print os.path.isfile(phot_file), phot_file
    print os.path.isfile(geometry_file), geometry_file
    tiling_mask = mangle.Mangle(geometry_file)

    hd_p = fits.open(phot_file)[1].data
    hd_s_i = fits.open(spec_file)[1].data
    hd_s = hd_s_i[hd_s_i['ZWARNING'] == 0]

    treeP = t.cKDTree(n.transpose([hd_p['ra'], hd_p['dec']]))
    treeS = t.cKDTree(n.transpose([hd_s['plug_ra'], hd_s['plug_dec']]))

    Ntarg = len(hd_p)
    Nspec = len(hd_s)

    print "N targeted in chunk = ", Ntarg

    # Adding columns to the complete photometry file
    polyid_val_p = tiling_mask.polyid(hd_p['ra'], hd_p['dec'])
    polyid_val_s = tiling_mask.polyid(hd_s['plug_ra'], hd_s['plug_dec'])

    polyid_col_p = fits.Column(name="tiling_polyid",
                               format='I',
                               array=polyid_val_p)
    polyid_col_s = fits.Column(name="tiling_polyid",
                               format='I',
                               array=polyid_val_s)

    #number of observations in 0.5 arcsec
    # 0.5 arcsecond
    nobs_val_p = n.zeros_like(hd_p['ra'])
    nobs_val_s = n.zeros_like(hd_s['plug_ra'])
    id_ps = treeP.query_ball_tree(treeS, rmax)
    nobs_val_p = n.array([len(el) for el in id_ps])

    id_ss = treeS.query_ball_tree(treeS, rmax)
    nobs_val_s = n.array([len(el) for el in id_ss])

    nobs_col_p = fits.Column(name="nobs", format='I', array=nobs_val_p)
    nobs_col_s = fits.Column(name="nobs", format='I', array=nobs_val_s)

    # assign redshift and redshift error to photometric catalog
    z_val = n.ones_like(hd_p['ra']) * -1.
    zerr_val = n.ones_like(hd_p['ra']) * -1.
    print "merges redshift measurements with photometry"
    for ii, el in enumerate(id_ps):
        if len(el) == 0:
            z_val[ii] = -9.
            zerr_val[ii] = -9.
        elif len(el) == 1:
            z_val[ii] = hd_s['Z'][el[0]]
            zerr_val[ii] = hd_s['Z_ERR'][el[0]]
            print z_val[ii], zerr_val[ii]
        elif len(el) >= 2:
            # gets the first guess
            z_0 = hd_s['Z'][el[0]]
            zerr_0 = hd_s['Z_ERR'][el[0]]
            # gets the second guess
            z_1 = hd_s['Z'][el[1]]
            zerr_1 = hd_s['Z_ERR'][el[1]]
            print "observed many times:", len(el), z_0, zerr_0, z_1, zerr_1
            # measurments agree
            if abs(z_0 - z_1) < 0.05:
                z_val[ii] = z_0
                zerr_val[ii] = zerr_0
            else:
                id_zq = n.argmin([zerr_0, zerr_1])
                z_val[ii] = [z_0, z_1][id_zq]
                zerr_val[ii] = [zerr_0, zerr_1][id_zq]

    z_col_p = fits.Column(name="Z", format='D', array=z_val)
    zerr_col_p = fits.Column(name="Z_ERR", format='D', array=zerr_val)

    hd_p_columns = hd_p.columns + nobs_col_p + polyid_col_p + z_col_p + zerr_col_p
    hdu = fits.BinTableHDU.from_columns(hd_p_columns)
    if os.path.isfile(output_phot_cat):
        os.remove(output_phot_cat)

    hdu.writeto(output_phot_cat)
imageinPixscale = float(imageinHDR['HIERARCH OAJ INS PIXSCALE'])
imageinPSF = settings['psf_image']
imageinWeight = settings['weight_map_file']
dataSeg = fits.open(settings['segmentation_map_file'])[0].data
dataSex = settings['sex_file']
modelsFolder = settings['models_path']
if modelsFolder[-1] == "/": modelsFolder = modelsFolder[:-1]
magZeroPoint = float(settings['mag_zero_point'])
MODELS_PER_MAG = int(settings['models_per_mag'])
iterations = int(settings['iter'])
modelsType = settings['model_type']  # right now, 'STAR' (from psf)
# or 'GALAXY' chef galaxies
####
# I read the mask to avoid bad regions. By default it reads a full sky mask.
maskfile = settings['mask']
mng = pymangle.Mangle(maskfile)
####

modelsSexTable = []
modelsInsertedTable = []
imageoutData = imageinData.copy()
modelInsertedID = 0

imageinNy = N.shape(imageinData)[0]
imageinNx = N.shape(imageinData)[1]
"""
Models handling.
NOTE: This version is for CHEFS galaxy models models.
"""
i = 0
if (modelsType == 'GALAXY'):
def generate_random_points_from_mangle(manglename, npoints):
    manglemask = pymangle.Mangle('mangle/%s_footprint.ply' % manglename)
    ra_rand, dec_rand = manglemask.genrand(npoints)
    return ra_rand, dec_rand
#alternative catalogs
path_2_target_AGN_RASS_eboss = os.path.join(
    target_dir, "SPIDERS_RASS_AGN_targets_for_EBOSS_TDSS_SPIDERS.fits.gz")
path_2_target_AGN_RASS_sequels = os.path.join(
    target_dir, "SPIDERS_RASS_AGN_targets_for_SEQUELS.fits.gz")
path_2_target_AGN_XMMSL_eboss = os.path.join(
    target_dir, "SPIDERS_XMMSL_AGN_targets_for_EBOSS_TDSS_SPIDERS.fits.gz")

path_2_specObjAll = os.path.join(os.environ['SDSS_DATA_DIR'], "dr14",
                                 "specObj-BOSS-dr14.fits")

# loads the DR14 mask
#path_2_mask_fits = os.path.join(os.environ['SDSS_DATA_DIR'], 'LSS', 'mask-lrg-N-eboss_v1.8_IRt.fits')
path_2_mask_ply = os.path.join(os.environ['SDSS_DATA_DIR'], 'LSS', 'LRG',
                               'v1.8', 'mask-lrg-N-eboss_v1.8_IRt.ply')
mng = mangle.Mangle(path_2_mask_ply)


def construct_summary_files_data(spec_file,
                                 phot_file,
                                 geometry_file,
                                 output_phot_cat,
                                 rmax=0.5 / 3600.):
    # spectroscopy
    print os.path.isfile(spec_file), spec_file
    print os.path.isfile(phot_file), phot_file
    print os.path.isfile(geometry_file), geometry_file
    tiling_mask = mangle.Mangle(geometry_file)

    hd_p = fits.open(phot_file)[1].data
    hd_s_i = fits.open(spec_file)[1].data