예제 #1
0
    def draw_hpxbin(self, lon, lat, nside=256, **kwargs):
        """
        Create a healpix histogram of the counts.

        Like `hexbin` from matplotlib

        Parameters:
        -----------
        lon : input longitude (deg)
        lat : input latitude (deg)
        nside : heaplix nside resolution
        kwargs : passed to draw_hpxmap and plt.pcolormesh

        Returns:
        --------
        hpxmap, im : healpix map and image
        """
        try:
            pix = hp.ang2pix(nside, lon, lat, lonlat=True)
        except TypeError:
            pix = hp.ang2pix(nside, np.radians(90 - lat), np.radians(lon))

        npix = hp.nside2npix(nside)
        hpxmap = hp.UNSEEN * np.ones(npix)
        idx, cts = np.unique(pix, return_counts=True)
        hpxmap[idx] = cts

        return hpxmap, self.draw_hpxmap(hpxmap, **kwargs)
예제 #2
0
파일: healpix.py 프로젝트: peakz22/skymap
def hpx2xy(hpxmap, pixel=None, nside=None, xsize=800, aspect=1.0,
           lonra=None, latra=None):
    """ Convert a healpix map into x,y pixels and values"""
    check_hpxmap(hpxmap,pixel,nside)

    if lonra is None and latra is None:
        lonra,latra = get_map_range(hpxmap,pixel,nside)
    elif (lonra is None) or (latra is None):
        msg = "Both lonra and latra must be specified"
        raise Exception(msg)

    lon = np.linspace(lonra[0],lonra[1], xsize)
    lat = np.linspace(latra[0],latra[1], int(aspect*xsize))
    lon, lat = np.meshgrid(lon, lat)

    # Calculate the value at the average location for pcolormesh
    # ADW: How does this play with RA = 360 boundary?
    llon = (lon[1:,1:]+lon[:-1,:-1])/2.
    llat = (lat[1:,1:]+lat[:-1,:-1])/2.

    if nside is None:
        if isinstance(hpxmap,np.ma.MaskedArray):
            nside = hp.get_nside(hpxmap.data)
        else:
            nside = hp.get_nside(hpxmap)

    # Old version of healpy
    try:
        pix = hp.ang2pix(nside,llon,llat,lonlat=True)
    except TypeError:
        pix = hp.ang2pix(nside,np.radians(90-llat),np.radians(llon))

    if pixel is None:
        values = masked_array(hpxmap[pix])
    else:
        # Things get fancy here...
        # Match the arrays on the pixel index
        pixel_df = pd.DataFrame({'pix':pixel,'idx':np.arange(len(pixel))})
        # Pandas warns about type comparison.
        # It probably doesn't like `pix.flat`, but it would save space
        #pix_df = pd.DataFrame({'pix':pix.flat},dtype=int)
        pix_df = pd.DataFrame({'pix':pix.ravel()},dtype=int)
        idx = pix_df.merge(pixel_df,on='pix',how='left')['idx'].values
        mask = np.isnan(idx)

        # Index the values by the matched index
        values = np.nan*np.ones(pix.shape,dtype=hpxmap.dtype)
        values[np.where(~mask.reshape(pix.shape))] = hpxmap[idx[~mask].astype(int)]
        values = np.ma.array(values,mask=mask)

    return lon,lat,values
예제 #3
0
def ang2pix(nside, lon, lat, coord='GAL'):
    """
    Input (lon, lat) in degrees instead of (theta, phi) in radians
    """
    theta = np.radians(90. - lat)
    phi = np.radians(lon)
    return hp.ang2pix(nside, theta, phi)
예제 #4
0
def test_offzenith_vis():
    # Construct a shell with a single point source a known position off from zenith.
    #   Similar to test_vis_calc, but set the pointing center 5deg off from the zenith and adjust analytic calculation

    freqs = [1.0e8]
    Nfreqs = 1
    fov = 60
    ant1_enu = np.array([10.0, 0, 0])
    ant2_enu = np.array([0.0, 140.6, 0])

    bl = observatory.Baseline(ant1_enu, ant2_enu)

    # Set pointing center to ra/dec = 0/0
    center = [0, 0]

    # Make a shell and place a 1 Jy/pix point source at ra/dec of 5/0
    Nside = 128
    Npix = Nside**2 * 12
    shell = np.zeros((Npix, Nfreqs))
    pix_area = 4 * np.pi / float(Npix)
    ind = hp.ang2pix(Nside, 5, 0.0, lonlat=True)
    shell[ind] = 1  # Jy/pix
    shell[ind] *= utils.jy2Tsr(freqs[0], bm=pix_area)  # K

    obs = observatory.Observatory(0, 0, array=[bl], freqs=freqs)
    obs.pointing_centers = [center]
    obs.times_jd = np.array([1])
    obs.set_fov(fov)
    obs.set_beam("uniform")

    sky = sky_model.SkyModel(Nside=Nside, freqs=np.array(freqs), data=shell)

    vis_calc, times, bls = obs.make_visibilities(sky)

    ra_deg, dec_deg = hp.pix2ang(Nside, ind, lonlat=True)

    src_az = np.radians(90.0)
    src_za = np.radians(ra_deg)

    src_l = np.sin(src_az) * np.sin(src_za)
    src_m = np.cos(src_az) * np.sin(src_za)
    src_n = np.cos(src_za)
    u, v, w = bl.get_uvw(freqs[0])

    vis_analytic = (1) * np.exp(2j * np.pi *
                                (u * src_l + v * src_m + w * src_n))

    print(vis_analytic)
    print(vis_calc)
    print(vis_calc[0, 0, 0] - vis_analytic)
    vis_calc = vis_calc[0, 0, 0]
    assert np.isclose(vis_calc.real, vis_analytic.real)
    assert np.isclose(vis_calc.imag, vis_analytic.imag)
예제 #5
0
def test_az_za():
    """
    Check the calculated azimuth and zenith angle of a point exactly 5 deg east on the sphere (az = 90d, za = 5d)
    """
    Nside = 128
    obs = observatory.Observatory(latitude, longitude, fov=20, nside=Nside)
    center = [0, 0]
    lon, lat = [5, 0]
    ind0 = hp.ang2pix(Nside, lon, lat, lonlat=True)
    lon, lat = hp.pix2ang(Nside, ind0, lonlat=True)
    za, az, pix = obs.calc_azza(center, return_inds=True)
    ind = np.where(pix == ind0)
    # lon = longitude of the source, which is set to 5deg off zenith (hence, zenith angle)
    assert np.isclose(np.degrees(za[ind]), lon)
    assert np.isclose(np.degrees(az[ind]), 90.0)
dec = f2[1].data['dec'][ids_active]
zzr = f2[1].data['redshift_R'][ids_active]
zzs = f2[1].data['redshift_S'][ids_active]
dL_cm = f2[1].data['dL'][ids_active]
galactic_NH = f2[1].data['nH'][ids_active]
galactic_ebv = f2[1].data['ebv'][ids_active]
g_lat = f2[1].data['g_lat'][ids_active]
g_lon = f2[1].data['g_lon'][ids_active]
ecl_lat = f2[1].data['ecl_lat'][ids_active]
ecl_lon = f2[1].data['ecl_lon'][ids_active]
N_galaxies = len(zzr)
f2.close()
print('coordinate file opened', time.time() - t0)

pix_ids = healpy.ang2pix(512,
                         n.pi / 2. - g_lat * n.pi / 180.,
                         g_lon * n.pi / 180.,
                         nest=True)
flux_lim_data = fits.open(path_2_flux_limits)  # [1].data
#flux_limit_eRASS3, flux_limit_eRASS8, flux_limit_SNR3
flux_limit = flux_lim_data[1].data['flux_limit_eRASS8']
print('flux limit file opened', time.time() - t0)

# selection function here :
# ( FX_soft_attenuated > 10**(flux_limit[pix_ids]-2) ) & ( SDSS_r_AB_attenuated < 26.5 )
sf1 = (FX_soft_attenuated > 0)
print('selection function applied', time.time() - t0)

HEALPIX_8 = healpy.ang2pix(8,
                           n.pi / 2. - dec * n.pi / 180.,
                           ra * n.pi / 180.,
                           nest=True)
예제 #7
0
                                                    unpack=True)

#att = attenuation( n.transpose([zzs, n.log10(data['galactic_NH']), KT_OUT]))

LX_obsF = LX_out / k_correction_2d
CLU_FX_soft_1 = LX_obsF / (4 * n.pi * dL_cm**2.)

itp_attenuation = interp1d(attenuate_X_logNH, attenuate_Y_frac_obs)
att = itp_attenuation(n.log10(galactic_NH))
CLU_FX_soft = CLU_FX_soft_1 * att

# Flux limit for point sources
# use the flux limit for SNR3 point sources to have all possible clusters, in particular at high redshift
# the a second flux limit will be applied with image simulations
pix_ids = healpy.ang2pix(512,
                         n.pi / 2. - g_lat * n.pi / 180.,
                         g_lon * n.pi / 180.,
                         nest=True)
flux_lim_data = fits.open(path_2_flux_limits)
#flux_limit_eRASS3, flux_limit_eRASS8, flux_limit_SNR3
flux_limit = flux_lim_data[1].data['flux_limit_eRASS8'][pix_ids]
# HPX 8
#HEALPIX_8 = healpy.ang2pix(8, n.pi/2. - dec*n.pi/180. , ra*n.pi/180. , nest=True)

print('flux limit file opened', time.time() - t0)
#if is_writing_images=="yes" and pixel_size_image>=18:
#detected = (CLU_FX_soft > 10**(flux_limit))
#elif is_writing_images=="yes" and pixel_size_image<18 :
#detected = (CLU_FX_soft > 10**(-15)) & (HEALPIX_8==0)
#else :
#detected = (CLU_FX_soft > 10**(-15)) # create way too many points
예제 #8
0
import matplotlib
matplotlib.use('Agg')
matplotlib.rcParams.update({'font.size': 14})
import matplotlib.pyplot as p

cluster_file = os.path.join(os.environ[env], env + '_eRO_CLU.fit')
clu = Table.read(cluster_file)

event_file = "/data40s/erosim/eRASS/eRASS8_cluster_MD40/simulated_photons_ccdA.fits"
evt_all = fits.open(event_file)[1].data

simput_dir = os.path.join(os.environ[env], 'cat_CLU_SIMPUT')

HEALPIX_8 = healpy.ang2pix(8,
                           n.pi / 2. - clu['DEC'] * n.pi / 180.,
                           clu['RA'] * n.pi / 180.,
                           nest=True)
hpx_val = 000
in_simput = clu[(HEALPIX_8 == hpx_val)]
simput_file = os.path.join(simput_dir, str(hpx_val).zfill(6) + '.fit')
spt = Table.read(simput_file)

event_files = n.array(
    glob.glob(
        os.path.join('/data40s/erosim/eRASS/eRASS8_cluster_MD40',
                     str(hpx_val).zfill(3), 'erass_ccd?_evt.fits')))


def get_events(src_id):
    #src_id = simput_entry['SRC_ID']
    all_ev, ras, decs = [], [], []
예제 #9
0
        'AGN_DEEP': 100,
        'AGN_IR': 99,
        'QSO': 99,
        'LyA': 100
    }

    # reassigns templates correctly
    z_all = n.hstack((0., n.arange(0.3, 3., 0.2), 3.5, 4.5, 6.))
    zmins = z_all[:-1]
    zmaxs = z_all[1:]

    hd_all = fits.open(path_2_eRO_all_catalog)
    N_agn_all = len(hd_all[1].data['ra'])

    pix_ids = healpy.ang2pix(512,
                             n.pi / 2. - hd_all[1].data['g_lat'] * n.pi / 180.,
                             hd_all[1].data['g_lon'] * n.pi / 180.,
                             nest=True)
    FX_LIM_value_cen = 10**(FX_LIM[pix_ids] - 0.1)
    # =============================
    # =============================
    # eROSITA SAMPLE
    # apply flux limit erosita
    # =============================
    # =============================
    # X-ray
    detected_all = (hd_all[1].data['AGN_FX_soft'] >
                    FX_LIM_value_cen) & (hd_all[1].data['g_lon'] > 180)
    print(len((detected_all).nonzero()[0]) * 768)
    # optical cut
    opt_all = (hd_all[1].data['AGN_SDSS_r_magnitude'] < 23.5)
    # overall erosita selections
print('agn file opened', time.time() - t0)

f0 = fits.open(path_2_light_cone)[1].data[ids_active]
print('halo file opened', time.time() - t0)

f1 = fits.open(path_2_galaxy_file)[1].data[ids_active]
print('galaxy file opened', time.time() - t0)

f2 = fits.open(path_2_coordinate_file)[1].data[ids_active]
ra = f2['ra']
dec = f2['dec']
print('coordinate file opened', time.time() - t0)

# eROSITA flux limit
pix_ids = healpy.ang2pix(512,
                         n.pi / 2. - f2['g_lat'] * n.pi / 180.,
                         f2['g_lon'] * n.pi / 180.,
                         nest=True)
flux_lim_data = fits.open(path_2_flux_limits)  # [1].data
#flux_limit_eRASS3, flux_limit_eRASS8, flux_limit_SNR3
flux_limit = flux_lim_data[1].data['flux_limit_eRASS8']
print('flux limit file opened', time.time() - t0)

# selection function here :
# ( FX_soft_attenuated > 10**(flux_limit[pix_ids]-2) ) & ( SDSS_r_AB_attenuated < 26.5 )
sf1 = (f3['FX_soft_attenuated'] > 0)
print('selection function applied', time.time() - t0)

HEALPIX_8 = healpy.ang2pix(8,
                           n.pi / 2. - dec * n.pi / 180.,
                           ra * n.pi / 180.,
                           nest=True)
예제 #11
0
depth_sdss_i = os.path.join(
    sdss_depth_dir, 'sdss_dr8_nodered_nside2048_i_model_10sigma.fits.gz')
depth_sdss_r = os.path.join(
    sdss_depth_dir, 'sdss_dr8_nodered_nside2048_r_model_10sigma.fits.gz')
depth_sdss_z = os.path.join(
    sdss_depth_dir, 'sdss_dr8_nodered_nside2048_z_model_10sigma.fits.gz')
#
NSIDE = 2048
sdss_g_depth = hp.fitsfunc.read_map(depth_sdss_g)
sdss_r_depth = hp.fitsfunc.read_map(depth_sdss_r)
sdss_i_depth = hp.fitsfunc.read_map(depth_sdss_i)
sdss_z_depth = hp.fitsfunc.read_map(depth_sdss_z)
# retrieves the depths
pixels = healpy.ang2pix(NSIDE,
                        hdu_clu['RA'],
                        hdu_clu['DEC'],
                        nest=False,
                        lonlat=True)
depth_10_sigma_g = sdss_g_depth[pixels]
depth_10_sigma_r = sdss_r_depth[pixels]
depth_10_sigma_i = sdss_i_depth[pixels]
depth_10_sigma_z = sdss_z_depth[pixels]
# computes the magnitudes
# K correction

#kcorr_value_g = kk.calc_kcor('g', zr_CLU, 'g - r', gr_all)
kcorr_value_r = kk.calc_kcor('r', zr_CLU, 'g - r', gr_all)
#kcorr_value_i = kk.calc_kcor('i', zr_CLU, 'g - i', gr_all+ri_all)
#kcorr_value_z = kk.calc_kcor('z', zr_CLU, 'g - z', gr_all+ri_all+iz_all)
#
sdss_r_temp = hdu_clu['mag_abs_r'] + DM2 + kcorr_value_r