Exemplo n.º 1
0
def decam_sky(overwrite=False): 
    ''' read in decam sky data 
    '''
    fpickle = os.path.join(UT.dat_dir(), 'decam_sky.p')
    if not os.path.isfile(fpickle) or overwrite: 
        fdecam = fits.open(os.path.join(UT.dat_dir(), 'decalsobs-zpt-dr3-allv2.fits'))
        _decam = fdecam[1].data
        
        keep = ((_decam['AIRMASS'] != 0.0) & 
                (_decam['TRANSP'] > .75) & 
                (_decam['TRANSP'] < 1.3)) 
        
        decam = {} 
        for k in _decam.names: 
            decam[k.lower()] = _decam[k][keep]
        
        # calculate moon altitude and moon separation 
        time = astropy.time.Time(decam['date'], format='jd')
        location = astropy.coordinates.EarthLocation.from_geodetic(
                lat='-30d10m10.78s', lon='-70d48m23.49s', height=2241.4*u.m)

        moon_position = astropy.coordinates.get_moon(time, location)
        moon_ra = moon_position.ra.value
        moon_dec = moon_position.dec.value
        
        moon_position_altaz = moon_position.transform_to(astropy.coordinates.AltAz(obstime=time, location=location))
        decam['moon_alt']   = moon_position_altaz.alt.value

        sun_position = astropy.coordinates.get_sun(time) 
        decam['moon_sun_sep'] = sun_position.separation(moon_position).deg
        pickle.dump(decam, open(fpickle, 'wb'))
    else: 
        decam = pickle.load(open(fpickle, 'rb'))
    return decam 
Exemplo n.º 2
0
def _twilight_coeffs(): 
    ''' save twilight coefficients from Parker
    '''
    import pandas as pd
    f = os.path.join(UT.code_dir(), 'dat', 'sky', 'MoonResults.csv')

    coeffs = pd.DataFrame.from_csv(f)
    coeffs.columns = [
        'wl', 'model', 'data_var', 'unexplained_var',' X2', 'rX2',
        'c0', 'c_am', 'tau', 'tau2', 'c_zodi', 'c_isl', 'sol', 'I',
        't0', 't1', 't2', 't3', 't4', 'm0', 'm1', 'm2', 'm3', 'm4', 'm5', 'm6',
        'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec',
        'c2', 'c3', 'c4', 'c5', 'c6']
    # keep moon models
    twi_coeffs = coeffs[coeffs['model'] == 'twilight']
    coeffs = coeffs[coeffs['model'] == 'moon']
    # order based on wavelengths for convenience
    wave_sort = np.argsort(np.array(coeffs['wl']))

    twi = {} 
    twi['wave'] = np.array(coeffs['wl'])[wave_sort] 
    for k in ['t0', 't1', 't2', 't3', 't4', 'c0']:
        twi[k] = np.array(twi_coeffs[k])[wave_sort]
    
    # save to file 
    ftwi = os.path.join(UT.dat_dir(), 'sky', 'twilight_coeffs.p')
    pickle.dump(twi, open(ftwi, 'wb'))
    return None 
Exemplo n.º 3
0
def _Noll_sky_ESO(): 
    ''' try to reproduce plots in Noll et al. (2012) using the ESO sky model 
    '''
    # Noll+(2012) Figure 1 generated from parameters in Table 1 
    dic = {
            'airmass': 1.0366676717,   # skysim.zodiacal.airmass_zodi(90 - 85.1) (based on alitutde) 
            'moon_sun_sep': 77.9,       # separation of sun and moon 
            'moon_target_sep': 51.3,    # separation of moon and target 
            'moon_alt': 41.3,           # altitude of the moon above the horizon
            'moon_earth_dist': 1.,      # relative distance to the moon  
            'ecl_lon': -124.5,          # heliocentric eclipitic longitude
            'ecl_lat': -31.6,           # heliocentric eclipitic latitude
            'msolflux': 205.5,          # monthly-averaged solar radio flux at 10.7 cm
            'pwv_mode': 'season',       # pwv or season
            'season': 4, 
            'time': 3, 
            'vacair': 'air', 
            'wmin': 300.,
            'wmax': 4200., 
            'wdelta': 5.,
            'observatory': '2640'}
    skyModel = skycalc.SkyModel()
    skyModel.callwith(dic)
    ftmp = os.path.join(UT.dat_dir(), 'sky', '_tmp.fits')
    skyModel.write(ftmp) # the clunkiest way to deal with this ever. 

    f = fits.open(ftmp) 
    fdata = f[1].data 
    wave = fdata['lam']             # wavelength in Ang 
    radiance = fdata['flux']        # photons/s/m^2/microm/arcsec^2 (radiance -- f**k)
    print fdata.names 

    fig = plt.figure()
    sub = fig.add_subplot(111)
    sub.plot(wave, np.log10(radiance), c='k', lw=0.5, label='composite') 
    for k, lbl, clr in zip(['flux_sml', 'flux_ssl', 'flux_zl', 'flux_tie', 'flux_tme', 'flux_ael', 'flux_arc'], 
            ['moon', 'stars', 'zodiacal', 'telescope', 'lower atmos.', 'airglow lines', 'resid. cont.'], ['b', 'C0', 'g', 'r', 'y', 'm', 'cyan']): 
        sub.plot(wave, np.log10(fdata[k]), lw=0.5, c=clr, label=lbl) 
    sub.legend(loc='lower right', frameon=True, fontsize=15) 
    sub.set_xlabel(r'Wavelength [$\mu m$]', fontsize=20) 
    sub.set_xlim(0.2, 4.2) 
    sub.set_ylabel(r'Radiance [dex]', fontsize=20) 
    sub.set_ylim(-1.5, 7.5) 
    fig.savefig(os.path.join(UT.code_dir(), 'figs', '_Nollfig1_sky_ESO.png'), bbox_inches='tight') 

    fig = plt.figure()
    sub = fig.add_subplot(111)
    w0p5 = np.abs(wave - 0.5).argmin() 
    sub.plot(wave, (fdata['flux_sml']/fdata['flux_sml'][w0p5]), lw=0.5, c='b', label='Moon') 
    sub.plot(wave, (fdata['flux_ssl']/fdata['flux_ssl'][w0p5]), lw=0.5, c='g', ls='--', label='stars')  
    sub.plot(wave, (fdata['flux_zl']/fdata['flux_zl'][w0p5]), lw=0.5, c='r', ls='-.', label='zodiacal light') 
    sub.legend(loc='lower right', fontsize=15) 
    sub.set_xlabel(r'Wavelength [$\mu m$]', fontsize=20) 
    sub.set_xlim(0.36, 0.885) 
    sub.set_ylabel(r'rel.radiance', fontsize=20) 
    sub.set_ylim(0.,1.8) 
    fig.savefig(os.path.join(UT.code_dir(), 'figs', '_Nollfig6_sky_ESO.png'), bbox_inches='tight') 
    return None 
Exemplo n.º 4
0
def boss_sky(): 
    ''' read in BOSS sky data -- fluxes and meta-data 

    :notes: 
         BOSS sky fiber treats the sky as a point source and therefore corrects for 
         fiber-loss accordingly. Therefore to do it correctly would involve backing 
         out the uncorrected sky flux, which is complicated.
    '''
    fboss = os.path.join(UT.dat_dir(), 'sky', 'Bright_BOSS_Sky_blue.fits')
    boss = aTable.read(fboss)
    return boss
Exemplo n.º 5
0
def sky_ESO(airmass, moon_sun_sep, moonalt, moonsep, observatory='2400'): 
    ''' calculate sky brightness using ESO sky calc 

    :param airmass: 
        airmass ranging from [1., 3.]

    :param moon_sun_sep:
        Separation in deg of Sun and Moon as seen from Earth

    :param moonalt: 
        Moon Altitude over Horizon in deg

    :param moonsep: 
        Moon-Target Separation in deg

    :references: 
    - https://www.eso.org/observing/etc/bin/gen/form?INS.MODE=swspectr+INS.NAME=SKYCALC
    - https://www.eso.org/observing/etc/doc/skycalc/helpskycalccli.html
    '''
    airmassp = 1./(1./airmass + 0.025*np.exp(-11./airmass)) # correct sec(z) airmass to Rozenberg (1966) airmass
    dic = {'airmass': round(airmass,5), 
            'incl_moon': 'Y', 
            'moon_sun_sep': moon_sun_sep, 
            'moon_target_sep': moonsep, 
            'moon_alt': moonalt, 
            'wmin': 355.,
            'wmax': 985., 
            'observatory': observatory}
    skyModel = skycalc.SkyModel()
    skyModel.callwith(dic)
    ftmp = os.path.join(UT.dat_dir(), 'sky', '_tmp.fits')
    skyModel.write(ftmp) # the clunkiest way to deal with this ever. 

    f = fits.open(ftmp) 
    fdata = f[1].data 
    wave = fdata['lam'] * 1e4       # wavelength in Ang 
    radiance = fdata['flux']        # photons/s/m^2/microm/arcsec^2 (radiance -- f**k)
    radiance *= 1e-8                # photons/s/cm^2/Ang/arcsec^2 
    # photons/s/cm^2/Ang/arcsec^2 * h * c / lambda 
    Isky = 1.99 * 1e-8 * radiance / wave * 1e17 # 10^-17 erg/s/cm^2/Ang/arcsec^2
    return wave, Isky
Exemplo n.º 6
0
def cI_twi(alpha, delta, airmass):
    ''' twilight contribution

    :param alpha: 

    :param delta: 

    :param airmass: 

    :return twi: 

    '''
    ftwi = os.path.join(UT.dat_dir(), 'sky', 'twilight_coeffs.p')
    twi_coeffs = pickle.load(open(ftwi, 'rb'))
    twi = (
        twi_coeffs['t0'] * np.abs(alpha) +      # CT2
        twi_coeffs['t1'] * np.abs(alpha)**2 +   # CT1
        twi_coeffs['t2'] * np.abs(delta)**2 +   # CT3
        twi_coeffs['t3'] * np.abs(delta)        # CT4
    ) * np.exp(-twi_coeffs['t4'] * airmass) + twi_coeffs['c0']
    return twi_coeffs['wave'], np.array(twi)
Exemplo n.º 7
0
def _sky_ESOvsKSvband(): 
    '''
    '''
    # get default ESO moon scatterlight brightness 
    dic = {
            'airmass': 1.0366676717,   # skysim.zodiacal.airmass_zodi(90 - 85.1) (based on alitutde) 
            'moon_sun_sep': 77.9,       # separation of sun and moon 
            'moon_target_sep': 51.3,    # separation of moon and target 
            'moon_alt': 41.3,           # altitude of the moon above the horizon
            'moon_earth_dist': 1.,      # relative distance to the moon  
            'ecl_lon': -124.5,          # heliocentric eclipitic longitude
            'ecl_lat': -31.6,           # heliocentric eclipitic latitude
            'msolflux': 205.5,          # monthly-averaged solar radio flux at 10.7 cm
            'pwv_mode': 'season',       # pwv or season
            'season': 4, 
            'time': 3, 
            'vacair': 'air', 
            'wmin': 300.,
            'wmax': 4200., 
            'wdelta': 5.,
            'observatory': '2640'}
    
    skyModel = skycalc.SkyModel()
    skyModel.callwith(dic)
    ftmp = os.path.join(UT.dat_dir(), 'sky', '_tmp.fits')
    skyModel.write(ftmp) # the clunkiest way to deal with this ever. 

    f = fits.open(ftmp) 
    fdata = f[1].data 
    wave_eso = fdata['lam']         # wavelength in Ang 
    radiance = fdata['flux_sml']    # photons/s/m^2/microm/arcsec^2 (radiance -- f**k)
    radiance *= 1e-8                # photons/s/cm^2/Ang/arcsec^2 
    Im_eso = 1.99 * 1e-8 * radiance / (wave_eso * 1e4) * 1e17 # 10^-17 erg/s/cm^2/Ang/arcsec^2
    
    # get moon brightness where some moon spectra is caled by KS V-band 
    specsim_sky = Sky.specsim_initialize('desi')
    specsim_wave = specsim_sky._wavelength # Ang
    specsim_sky.airmass = 1.0366676717 
    specsim_sky.moon.moon_phase = 77.9/180. #np.arccos(2.*moonill - 1)/np.pi
    specsim_sky.moon.moon_zenith = (90. - 41.3) * u.deg
    specsim_sky.moon.separation_angle = 51.3 * u.deg
    
    # updated KS coefficients 
    specsim_sky.moon.KS_CR = 10**5.70 
    specsim_sky.moon.KS_CM0 = 7.15
    specsim_sky.moon.KS_CM1 = 40.
    
    Im_ks = specsim_sky.moon.surface_brightness.value
    
    rho = specsim_sky.moon.separation_angle.to(u.deg).value
    fR = 10**5.36*(1.06 + np.cos(np.radians(rho))**2)
    fM = 10 ** (6.15 -  rho/ 40.)
    fRp = 10**5.70*(1.06 + np.cos(np.radians(rho))**2)
    fMp = 10 ** (7.15 -  rho/ 40.)
    fRn = 10**5.66*(1.06 + np.cos(np.radians(rho))**2)
    fMn = 10 ** (5.54 -  rho/ 178.)
    tRS = np.interp(specsim_wave/1e4, wave_eso, fdata['trans_rs']) 
    tMS = np.interp(specsim_wave/1e4, wave_eso, fdata['trans_ms']) 
    tall = np.interp(specsim_wave/1e4, wave_eso, fdata['trans']) 
    Xo = (1 - 0.96 * np.sin(specsim_sky.moon.obs_zenith)**2)**(-0.5)
    Xm = (1 - 0.96 * np.sin(specsim_sky.moon.moon_zenith)**2)**(-0.5) 
    tkso = 10 ** (-0.4 * (specsim_sky.moon.vband_extinction * Xo))
    tksm = 10 ** (-0.4 * (specsim_sky.moon.vband_extinction * Xm))
    fcorr = ((fRp*(1.-tRS**Xo) + fMp*(1.-tMS**Xo))*tall**Xm)/((fRp+fMp) * (1-tkso) * tksm)
    f_eso_ks = ((fRp*(1.-tRS**Xo) + fMp*(1.-tMS**Xo))*tall**Xm)/((fR+fM) * (1-tkso) * tksm)
    f_nks_ks = (fRn + fMn)/(fR + fM) 
    f_peso_ks = (fRp + fMp)/(fR + fM) 
    
    fig = plt.figure(figsize=(10,5))
    sub = fig.add_subplot(111)
    sub.plot(wave_eso, Im_eso, c='k', label='ESO refit KS')
    sub.plot(specsim_wave/1e4, fcorr*Im_ks * 1e17, c='C1', label='V-band scale ESO coeff.') 
    sub.legend(loc='upper right', fontsize=20) 
    sub.set_xlabel(r'Wavelength [$\mu m$]', fontsize=20) 
    sub.set_xlim(0.3, 1.) 
    sub.set_ylabel(r'Moon Brightness', fontsize=20) 
    fig.savefig(os.path.join(UT.code_dir(), 'figs', '_sky_ESOvsKSvband.png'), bbox_inches='tight') 

    fig = plt.figure(figsize=(10,5))
    sub = fig.add_subplot(111)
    sub.plot(specsim_wave, f_eso_ks, c='k', label='ESO')
    sub.plot(specsim_wave, np.repeat(f_nks_ks, len(specsim_wave)), c='C0', label='refit KS')
    sub.plot(specsim_wave, np.repeat(f_peso_ks, len(specsim_wave)), c='C1', label='pseudo ESO')
    sub.legend(loc='upper right', fontsize=20) 
    sub.set_xlabel(r'Wavelength [$A$]', fontsize=20) 
    sub.set_xlim(3.4e3, 9.8e3) 
    sub.set_ylabel(r'(moon model)/(KS moon)', fontsize=20) 
    fig.savefig(os.path.join(UT.code_dir(), 'figs', '_sky_ESOoverKS.png'), bbox_inches='tight') 

    fig = plt.figure(figsize=(10,5))
    sub = fig.add_subplot(111)
    sub.plot(wave_eso, fdata['trans_rs'], c='k', label='Rayleigh')
    sub.plot(wave_eso, fdata['trans_ms'], c='C1', label='Mie') 
    sub.plot(wave_eso, np.repeat(10**(-0.4*specsim_sky.moon.vband_extinction*dic['airmass']), len(wave_eso)))
    sub.legend(loc='upper right', fontsize=20) 
    sub.set_xlabel(r'Wavelength [$\mu m$]', fontsize=20) 
    sub.set_xlim(0.3, 1.) 
    sub.set_ylabel(r'transmission', fontsize=20) 
    fig.savefig(os.path.join(UT.code_dir(), 'figs', '_sky_ESOvsKSvband.trans.png'), bbox_inches='tight') 
    return None 
Exemplo n.º 8
0
def Obvs_HODLHD_test(theta,
                     halo,
                     mneut=0.0,
                     nreal=1,
                     nzbin=4,
                     seed=1,
                     obvs='plk',
                     Nmesh=360):
    ''' Build the observables `obvs` for {theta_test} given halo catalog
    '''
    folder = ''.join([
        UT.dat_dir(), 'lhd/',
        str(mneut), 'eV_',
        str(nreal), '_z',
        str(nzbin), '_',
        str(samples), 'samples/', 'HOD', method, '_seed',
        str(seed_hod), '_',
        str(i_p), '/'
    ])

    # F(theta) --- i.e. the galaxy catalog generated
    # from the halo catalog
    p_hod = {
        'logMmin': theta[0],
        'sigma_logM': theta[1],
        'logM0': theta[2],
        'logM1': theta[3],
        'alpha': theta[4]
    }
    g = FM.Galaxies(halos, p_hod, seed=seed)
    g['RSDPosition'] = FM.RSD(g, LOS=[0, 0, 1])  # impose RSD

    # measure P(k) from F(theta) in real and z space
    for rsd in [False, True]:
        if obvs == 'plk':  # power spectrum multipole
            plk = FM.Observables(gals, observable='plk', rsd=rsd, Nmesh=Nmesh)

            # write to file
            if rsd: str_rsd = '.zspace'
            else: str_rsd = '.rspace'
            fname = ''.join([
                folder, 'pk.theta_test.menut',
                str(mneut), '.nreal',
                str(nreal), '.nzbin',
                str(nzbin), str_rsd, '.',
                str(Nmesh), '.nbkt.dat'
            ])

            # save to file
            f = open(fname, 'w')
            f.write("### header ### \n")
            f.write("# shotnoise %f \n" % plk['shotnoise'])
            f.write("# columns : k , P0, P2, P4 \n")
            f.write('### header ### \n')

            for ik in range(len(plk['k'])):
                f.write("%f \t %f \t %f \t %f" %
                        (plk['k'][ik], plk['p0k'][ik], plk['p2k'][ik],
                         plk['p4k'][ik]))
                f.write("\n")
            f.close()
            obvs = plk
        else:
            raise NotImplementedError('only Plk implemented')
    return None