def __init__(self, datadir=None, z_solar=0.019): # Change this to appropriate path if datadir is None: self.datadir = '' # self.datadir='/work1/sharma/Projects/kepler/data/dnu_grid6/' else: self.datadir = datadir # set solar reference values # self.radius= 6.958e8 # self.mass=1.99e30 # sun logg=np.log10(100.0*6.67259e-11*1.989e30/(6.958e8*6.958e8)) self.logg_solar = 4.43796037457 # cgs unit self.teff_solar = 5777.0 # kelvin self.numax_solar = 3090.0 # micro Hz 3090+-30 # cannot change this self.dnu_solar = 135.1 # micro Hz 135.1 self.z_solar = z_solar # solar metallicity value data1 = ebf.read(self.datadir + 'grid_interp1.ebf', '/data') data2 = ebf.read(self.datadir + 'grid_interp2.ebf', '/data') self.igrid1 = _IGrid(data1, ['evstate', 'logz', 'mass', 'logg_teff']) self.igrid2 = _IGrid(data2, ['evstate', 'logz', 'mass_nu', 'logg_teff'])
def read(file1, schema=None, catalog=None, key=None, quant=None): if file1.endswith('.ebf'): if ebf.containsKey(file1, '/data'): data1 = ebf.read(file1, '/data') data1 = npstruct2dict(data1) else: data1 = ebf.read(file1, '/') elif file1.endswith('.fit') or file1.endswith('.fits'): data1 = fitsread(file1) else: data1 = asctab.read(file1) if catalog != None: schema = '/work1/sharma/dbm/schemas/cdef_' + catalog + '.txt' if schema != None: l = asctab.read(schema) if 'alias' in l.keys(): l['ukey'] = l['alias'] s = set(data1.keys()) data2 = {} for i in range(len(l['key'])): if l['key'][i] in s: data2[l['ukey'][i]] = data1[l['key'][i]] data1 = data2 if key != None: if quant != None: return filter_dict(data1, cross_match(data1['key'], quant)) else: raise RuntimeError('FOr cross match, quant should not be None') else: return data1
def galaxia(): # Galaxia simulation data data = ebf.read("data/galaxia/galaxy_A.ebf", "/") glon = data["glon"] glon[glon > 180.] = glon[glon > 180.] - 360 glat = data["glat"] # Compute the mean metallicity Fe_H = data['feh'] # From Sandage 2006: # http://iopscience.iop.org/1538-3881/131/3/1750/pdf/204918.web.pdf B_minus_V_blue_edge = lambda Fe_H: 0.35 + 0.159*Fe_H + 0.05*Fe_H**2 # Carretta & Gratton 1997 (or http://arxiv.org/pdf/astro-ph/0507464v2.pdf) true_M_V = lambda Fe_H: 0.23*Fe_H + 0.93 B_V = data['ubv_b'] - data['ubv_v'] M_V = data['ubv_v'] # Instability strip is ~0.25 - blue edge FBE = np.mean(B_minus_V_blue_edge(Fe_H)) idx = (B_V < 0.6) & (B_V > FBE) & \ (M_V < true_M_V(Fe_H.max())) & (M_V > true_M_V(Fe_H.min())) & \ (glon > -10.) & (glon < 10) & \ (glat > 35) & (glat < 55) d = np.sqrt(data["px"]**2 + data["py"]**2 + data["pz"]**2) mu = 5.*np.log10(d) - 5 m_V = mu + M_V n_galaxia, bins = np.histogram(d[idx], bins=25, density=False) n_galaxia = n_galaxia.astype(float) / 40. # sq. deg. -> # per sq. deg.
def read_args(self,filename,varnames): """ Read arguments from an ebf file Can be used in set_args() """ for name in varnames: self.args[name]=ebf.read(filename,'/'+name)
def galaxia(): # Galaxia simulation data data = ebf.read("data/galaxia/galaxy_A.ebf", "/") glon = data["glon"] glon[glon > 180.] = glon[glon > 180.] - 360 glat = data["glat"] # Compute the mean metallicity Fe_H = data['feh'] # From Sandage 2006: # http://iopscience.iop.org/1538-3881/131/3/1750/pdf/204918.web.pdf B_minus_V_blue_edge = lambda Fe_H: 0.35 + 0.159 * Fe_H + 0.05 * Fe_H**2 # Carretta & Gratton 1997 (or http://arxiv.org/pdf/astro-ph/0507464v2.pdf) true_M_V = lambda Fe_H: 0.23 * Fe_H + 0.93 B_V = data['ubv_b'] - data['ubv_v'] M_V = data['ubv_v'] # Instability strip is ~0.25 - blue edge FBE = np.mean(B_minus_V_blue_edge(Fe_H)) idx = (B_V < 0.6) & (B_V > FBE) & \ (M_V < true_M_V(Fe_H.max())) & (M_V > true_M_V(Fe_H.min())) & \ (glon > -10.) & (glon < 10) & \ (glat > 35) & (glat < 55) d = np.sqrt(data["px"]**2 + data["py"]**2 + data["pz"]**2) mu = 5. * np.log10(d) - 5 m_V = mu + M_V n_galaxia, bins = np.histogram(d[idx], bins=25, density=False) n_galaxia = n_galaxia.astype(float) / 40. # sq. deg. -> # per sq. deg.
def main(filename, out_path=None): filename = os.path.abspath(filename) if out_path is None: out_path = os.path.dirname(filename) else: out_path = os.path.abspath(out_path) basename = os.path.splitext(os.path.basename(filename))[0] hdf5_filename = os.path.join(out_path, "{}.h5".format(basename)) d = ebf.read(filename) with h5py.File(hdf5_filename, "w") as h5f: grp = h5f["/"] for k in d.keys(): data = d[k] h5_dset = grp.create_dataset(k, (len(data),), dtype=data.dtype) h5_dset[:] = data h5_dset.attrs['unit'] = ebf.unit(filename, "/"+k)
def process_galaxia(name=None): import ebf import astropy.table as atpy import numpy as np import string if name == None: name = '/Users/hendel/modules/Galaxia/GalaxiaData/Examples/test_gap_mock.ebf' namebase = string.split(name, '.')[0] F = ebf.read(name) dm = 5 * np.log10(F['rad'] * 1e3) - 5 u, g, r, i, z = [F['sdss_%s' % _] + dm for _ in ['u', 'g', 'r', 'i', 'z']] tab = atpy.Table() #for filt in ['u','g','r','i','z']: # tab.add_column(atpy.Column(eval(filt),filt)) tab.add_column(atpy.Column(eval('g'), 'g')) tab.add_column(atpy.Column(eval('r'), 'r')) tab.add_column(atpy.Column(eval('g') - eval('r'), 'g-r')) tab.write(namebase + '.fits', overwrite=True)
def read(self,outname): if ebf.containsKey(outname,'/names0'): x=ebf.read(outname,'/names0') self.names0=[str(temp) for temp in x] self.chain=ebf.read(outname,'/chain/') else: self.names0=[] if ebf.containsKey(outname,'/names1'): x=ebf.read(outname,'/names1') self.names1=[str(temp) for temp in x] self.mu=ebf.read(outname,'/mu/') self.sigma=ebf.read(outname,'/sigma/') else: self.names1=[] self.descr=ast.literal_eval(ebf.read(outname,'/descr')[0])
def run(self): self.print_constraints() model = ebf.read(os.path.join(DATADIR, 'mesa.ebf')) # prelims to manipulate some model variables (to be automated soon ...) model['rho'] = np.log10(model['rho']) # next line turns off Dnu scaling relation corrections model['fdnu'][:] = 1. model['avs'] = np.zeros(len(model['teff'])) model['dis'] = np.zeros(len(model['teff'])) # Instantiate model x = grid.classify_grid.obsdata() self.addspec(x) self.addjhk(x) self.addgriz(x) self.addplx(x) self.paras = grid.classify_grid.classify(input=x, model=model, dustmodel=0, doplot=0, useav=0)
''' I changed lines 34, 86, 138, 149 in mist/models.py to force vcrit=0. ''' # method 1 tracks = get_ichrone('mist', tracks=True) mass, age, feh = (1.03, 9.27, -0.11) a = tracks.generate(mass, age, feh, return_dict=True, accurate=True) # # method 2 iso = get_ichrone('mist') # read in Galaxia stars rootpath = '' synp = ebf.read(rootpath + "sample/kepler_galaxia_mrtd5.ebf") masses, ages, fehs = synp["smass"], synp["log_age"], synp["feh"] Nstar = masses.shape[0] keys = [ 'nu_max', 'radius', 'logg', 'Teff', 'delta_nu', 'phase', 'Mbol', 'feh', 'logTeff', 'initial_mass', 'density', 'mass', 'logL', 'age' ] #list(a.keys()) res = np.zeros(Nstar, dtype=np.dtype([(keys[i], np.float64) for i in range(len(keys))])) for i in range(Nstar): print(i, "/", Nstar) tmass, tage, tfeh = (float(masses[i]), float(ages[i]), float(fehs[i]) ) #+np.log10(0.019/0.0142) try:
def ebfread(file1, keys): # keys1=[path+key for key in keys] data = {} for key in keys: data[key.rsplit('/', 1)[1]] = ebf.read(file1, key) return data
def gen_bg_counts_interp( dms=np.linspace(14, 20, 6), magerror_mods=[1.], surveys=['SDSS', 'CFHT', 'LSST', 'LSST10', 'CASTOR', 'WFIRST'], bands=['gr', 'gr', 'gr', 'gr', 'ug', 'zh'], minerr=0.003, maxerr=0.1, thresh=2, isodict=None, errormodels=None, bg_name=None, verbose=False): from scipy.interpolate import RegularGridInterpolator import ebf bgdict = {} if bg_name == None: bg_name = '/Users/hendel/modules/Galaxia/GalaxiaData/Examples/test_gap_mock.ebf' F = ebf.read(bg_name) center = (np.median(F['glat']), np.median(F['glon'])) sel = np.sqrt((F['glat'] - center[0])**2 + (F['glon'] - center[1])**2) < (1 / np.sqrt(np.pi)) if isodict == None: isodict = load_iso_interps(remake=False) if errormodels == None: errormodels = load_errormodels() for i, survey in enumerate(surveys): tbands = [ string.lower(survey + '_' + bands[i][0]), string.lower(survey + '_' + bands[i][1]) ] apmags = get_mag(F['smass'][sel], F['age'][sel], F['feh'][sel], rs=F['rad'][sel], bands=tbands, interpdict=isodict) maglim = getMagLimit(bands[i][0], survey=survey) maglims = np.linspace(maglim - 5, maglim, 10) #turn this up in production #data = np.zeros((len(dms), len(magerror_mods), len(maglims))) data = np.zeros((len(dms), len(maglims))) for ii in np.arange(len(dms)): for jj in np.arange(len(magerror_mods)): for kk in np.arange(len(maglims)): #data[ii,jj,kk] = bg_count_func(apmags, dms[ii], magerror_mods[jj], maglims[kk], survey=survey, band = bands[i], isodict=isodict, minerr=minerr, maxerr=maxerr, thresh=thresh) data[ii, kk] = bg_count_func(apmags, dms[ii], magerror_mods[jj], maglims[kk], survey=survey, band=bands[i], isodict=isodict, minerr=minerr, maxerr=maxerr, thresh=thresh) #if verbose==True: print survey, 'dm ', dms[ii], 'magerror ', magerror_mods[jj], 'maglim ', maglims[kk], 'n bg ', data[ii,jj,kk] if verbose == True: print survey, 'dm ', dms[ii], 'maglim ', maglims[ kk], 'n bg ', data[ii, kk] #bgdict[survey] = RegularGridInterpolator((np.array(dms), np.array(magerror_mods), np.array(maglims)), data) bgdict[survey] = RegularGridInterpolator( (np.array(dms), np.array(maglims)), data) return bgdict
import numpy as np import matplotlib.pyplot as plt from classify_grid import * import os, ebf from astropy.io import ascii import time if __name__ == '__main__': homedir = os.path.expanduser('~/') model = ebf.read(homedir + 'science/models/MIST/mesa.ebf') model['rho'] = np.log10(model['rho']) # do this to turn off scaling relation corrections #model['fdnu'][:]=1. model['avs'] = np.zeros(len(model['teff'])) model['dis'] = np.zeros(len(model['teff'])) #dustmodel = mwdust.Combined15()m #data=ascii.read('../../apokasc/tgas_apokasc_obs.txt') #data=ascii.read('../../apokasc/tgas_dwarfs_obs.txt') data = ascii.read('apokasc/tgas_combined_obs.txt') #f = open('../../apokasc/tgas_combined_derived_grid.txt', 'w') #f = open('../../apokasc/tgas_combined_derived_grid_seismo_dnucor.txt', 'w') #f = open('../../apokasc/tgas_combined_derived_grid_plx.txt', 'w') f = open('temp.dat', 'w') f.write(
dat_satid, dat_px, dat_py, dat_pz, dat_mass = np.loadtxt('/Users/Python-2.7/Documents/Johnstons_Group/halo_dat_files/halo02.dat', usecols=[0, 1, 2, 3, 13], unpack=True) unq_satid = np.unique(dat_satid) tot_mass_by_satid = np.zeros((2, unq_satid.size)) for i,satid in enumerate(unq_satid): tot_mass = dat_mass[dat_satid==satid].sum() tot_mass_by_satid[:,i] = [satid, tot_mass] satellites = [] for si in np.unique(dat_satid): indices = np.where(dat_satid==si) satellites.append(zip(dat_px[indices], dat_py[indices], dat_pz[indices])) import ebf ebf_data = ebf.read('/Users/Python-2.7/Documents/Johnstons_Group/halo02_msto_subsample.ebf') ebf_px = np.array(ebf_data['px']-8) ebf_py = np.array(ebf_data['py']) ebf_pz = np.array(ebf_data['pz']-0.015) ebf_satid=np.unique(ebf_data['satid']) assert len(ebf_satid)==len(satellites) ebf_satellites = [] for si in np.unique(ebf_satid): indices = np.where(ebf_data['satid']==si) ebf_satellites.append(zip(ebf_px[indices], ebf_py[indices], ebf_pz[indices])) assert len(ebf_satellites)==len(satellites)
dat_satid, dat_px, dat_py, dat_pz, dat_mass = np.loadtxt('/root/Documents/halo02.dat', usecols=[0, 1, 2, 3, 13], unpack=True) unq_satid = np.unique(dat_satid) tot_mass_by_satid = np.zeros((2, unq_satid.size)) for i,satid in enumerate(unq_satid): tot_mass = dat_mass[dat_satid==satid].sum() tot_mass_by_satid[:,i] = [satid, tot_mass] satellites = [] for si in np.unique(dat_satid): indices = np.where(dat_satid==si) satellites.append(zip(dat_px[indices], dat_py[indices], dat_pz[indices])) import ebf ebf_data = ebf.read('/root/Documents/halo02_msto_subsample.ebf') ebf_px = np.array(ebf_data['px']-8) ebf_py = np.array(ebf_data['py']) ebf_pz = np.array(ebf_data['pz']-0.015) ebf_satid=np.unique(ebf_data['satid']) assert len(ebf_satid)==len(satellites) ebf_satellites = [] for si in np.unique(ebf_satid): indices = np.where(ebf_data['satid']==si) ebf_satellites.append(zip(ebf_px[indices], ebf_py[indices], ebf_pz[indices])) assert len(ebf_satellites)==len(satellites)
import sys import astropy.table as table import ebf t = table.Table() f = ebf.read(sys.argv[1]) glat, glon, rad, smass, age, feh = [ f[_] for _ in ['glat', 'glon', 'rad', 'smass', 'age', 'feh'] ] for col in ['glat', 'glon', 'rad', 'smass', 'age', 'feh']: t.add_column(table.Column(eval(col), col)) t.write(sys.argv[1].split('.ebf')[0] + '.fits', overwrite=True)
import corner import sys import os import scipy import scipy.spatial import scipy.interpolate import scipy.optimize # sys.path.append("/Users/yaguang/Onedrive/Work/nike/code/") # from fast_histogram import histogram2d ## read in data # synthetic stars import ebf synp = ebf.read("sample/kepler_galaxia_mrtd5.ebf") Nstar = synp["alpha"].shape[0] factor = int(Nstar / 16000) idx = np.arange(0, int(Nstar / factor)) * factor for key in synp.keys(): synp[key] = synp[key][idx] idx = (synp["evstate"] == 2) & (np.isfinite(synp["numax"])) xpdv, ypdv, ypdvt = synp["numax"][ idx], synp["numax"][idx]**0.75 / synp["dnu"][idx], synp["dnu"][idx] Npdv = xpdv.shape[0] mass = synp["mact"][idx] g, teff = 10.0**synp["log_grav"][idx], 10.0**synp["log_teff"][idx] # observation stars yu = ascii.read("sample/yu+2018.csv") idx = (yu["Phase"] == 2)
oistars = np.interp(ms, iso['M_ini'], iso['DES-i']) + (5 * np.log10(impact_rs * 1e3) - 5) gstars = ogstars[(ogstars > 14.6) & (ogstars < 28.) & (orstars < 28.) & (oistars < 28.)] rstars = orstars[(ogstars > 14.6) & (ogstars < 28.) & (orstars < 28.) & (oistars < 28.)] istars = oistars[(ogstars > 14.6) & (ogstars < 28.) & (orstars < 28.) & (oistars < 28.)] impactdata_cut = impactdata[(ogstars > 14.6) & (ogstars < 28.) & (orstars < 28.) & (oistars < 28.)] #smoothdata_cut = smoothdata[(ogstars>14.6)&(ogstars<28.)&(orstars<28.)&(oistars<28.)] bg_name = '/Users/hendel/modules/Galaxia/GalaxiaData/Examples/test_gap_mock_big.ebf' import ebf F = ebf.read(bg_name) dm = 5 * np.log10(F['rad'] * 1e3) - 5 bg_u, bg_g, bg_r, bg_i, bg_z = [ F['sdss_%s' % _] + dm for _ in ['u', 'g', 'r', 'i', 'z'] ] bg_b, bg_l = [F['glat'], F['glon']] bg_og = bg_g bg_u = bg_u[bg_og > 14.6] bg_g = bg_g[bg_og > 14.6] bg_r = bg_r[bg_og > 14.6] bg_i = bg_i[bg_og > 14.6] bg_z = bg_z[bg_og > 14.6] bg_l = bg_l[bg_og > 14.6] bg_b = bg_b[bg_og > 14.6] bgc = SkyCoord(bg_l * u.deg, bg_b * u.deg, frame='galactic')
from skysurvey.new_config import SYS_CFG_FNAME from libc.stdlib cimport rand from libc.math cimport cos from libc.math cimport sin from libc.math cimport M_PI cimport numpy as np sys_config_fh = os.path.join(os.path.dirname( os.path.realpath(skysurvey.__file__)), SYS_CFG_FNAME) SysConfig = ConfigParser.ConfigParser() SysConfig.read(sys_config_fh) config_fh = SysConfig.get('skysurvey_global_settings', 'config_fh') Config = ConfigParser.ConfigParser() Config.read(config_fh) ebf_fh = os.path.join(Config.get('PATH', 'data_dir'), 'satprop.ebf') SATPROP = ebf.read(ebf_fh) # np.import_array() if 'NUMBER_OF_PROCESSORS' in list(os.environ.keys()): NUM_PROCESSORS = int(os.environ['NUMBER_OF_PROCESSORS']) else: NUM_PROCESSORS = 1 @cython.boundscheck(False) @cython.wraparound(False) def rotation_matrix( np.ndarray[np.float64_t, ndim=1, mode='c'] ax, np.float64_t th): ''' Description : used to rotate x,y,z arrays. usually called by <rotate>. Parameters
import ebf import astropy.table as atpy import numpy as np import string for name in ['30_deg_mock.ebf', '60_deg_mock.ebf', '90_deg_mock.ebf']: namebase = string.split(name, '.')[0] F = ebf.read(name) dm = 5 * np.log10(F['rad'] * 1e3) - 5 u, g, r, i, z = [F['sdss_%s' % _] + dm for _ in ['u', 'g', 'r', 'i', 'z']] tab = atpy.Table() for filt in ['u', 'g', 'r', 'i', 'z']: tab.add_column(atpy.Column(eval(filt), filt)) tab.write(namebase + '.fits', overwrite=True)
import numpy as np from numpy import pi import ebf dat_satid, dat_px, dat_py, dat_pz, dat_mass = np.loadtxt('/Users/clean/Documents/halo_dat_files//halo02.dat', usecols=[0, 1, 2, 3, 13], unpack=True) ebf_data = ebf.read('/Users/clean/Documents/halo02_msto_subsample.ebf') ebf_px = np.array((ebf_data['px']-8)) #*np.cos(45 * pi / 180)) ebf_py = np.array((ebf_data['py'])) #*np.sin(45 * pi / 180)) ebf_pz = np.array(ebf_data['pz']-0.015) ebf_satid = np.unique(ebf_data['satid']) ebf_lum = np.array(ebf_data['lum']) ebf_mact = np.array(ebf_data['mact']) satellites = [] for si in np.unique(dat_satid): indices = np.where(dat_satid==si) satellites.append(zip(dat_px[indices], dat_py[indices], dat_pz[indices], dat_mass[indices])) assert len(ebf_satid)==len(satellites) ebf_satellites = [] for si in np.unique(ebf_satid): indices = np.where(ebf_data['satid']==si)
axesnow[1].set_xlabel('l') axesnow[1].set_ylabel('b') fignow.savefig('{0}_{1:03d}.pdf'.format(fignameEachpre, pixel_index), rasterized=True) fig.savefig(fignameAll) plt.close(fig) plt.close(fignow) datafile = 'sdssgalaxy_1percent.ebf' #datafile = 'sdssHalo.ebf' #datafile = '../GalaxiaData/sdssgalaxy_10percent.ebf' #datafile = '../GalaxiaData/sdssgalaxy_1percent.ebf' #datafile = '../GalaxiaData/sdssHalo.ebf' data = ebf.read(datafile, '/') c = coord.Galactic(u=data['px'] * u.kpc, v=data['py'] * u.kpc, w=data['pz'] * u.kpc, U=data['vx'] * u.km / u.s, V=data['vy'] * u.km / u.s, W=data['vz'] * u.km / u.s, representation=coord.CartesianRepresentation, differential_cls=coord.CartesianDifferential) c.set_representation_cls(coord.SphericalRepresentation, s=coord.SphericalCosLatDifferential) #for visualizing all data on the sky nside = 128 hpixMap = hu.HealPix("ring", nside)
import numpy.ma as ma from mpl_toolkits.mplot3d import Axes3D dat_satid, dat_px, dat_py, dat_pz, dat_mass = np.loadtxt( '/root/Documents/halo02.dat', usecols=[0, 1, 2, 3, 13], unpack=True) unq_satid = np.unique(dat_satid) tot_mass_by_satid = np.zeros((2, unq_satid.size)) for i, satid in enumerate(unq_satid): tot_mass = dat_mass[dat_satid == satid].sum() tot_mass_by_satid[:, i] = [satid, tot_mass] satellites = [] for si in np.unique(dat_satid): indices = np.where(dat_satid == si) satellites.append(zip(dat_px[indices], dat_py[indices], dat_pz[indices])) import ebf ebf_data = ebf.read('/root/Documents/halo02_msto_subsample.ebf') ebf_px = np.array(ebf_data['px'] - 8) ebf_py = np.array(ebf_data['py']) ebf_pz = np.array(ebf_data['pz'] - 0.015) ebf_satid = np.unique(ebf_data['satid']) assert len(ebf_satid) == len(satellites) ebf_satellites = [] for si in np.unique(ebf_satid): indices = np.where(ebf_data['satid'] == si) ebf_satellites.append( zip(ebf_px[indices], ebf_py[indices], ebf_pz[indices])) assert len(ebf_satellites) == len(satellites) import sys from operator import itemgetter xop = itemgetter(0)
import ebf import astropy.table as atpy import numpy as np F = ebf.read('stream_gap_sim.ebf') dm = 5*np.log10(F['rad']*1e3)-5 g,r,i = [F['sdss_%s'%_]+dm for _ in ['g','r','i']] tab = atpy.Table() for filt in ['g','r']: tab.add_column(atpy.Column(eval(filt),filt)) tab.write('stream_gap_mock.fits',overwrite=True)