sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) import synthobs from synthobs.sed import models import flare import flare.filters import matplotlib.pyplot as plt "This is an more efficient way of calculating the broadband luminosities of galaxies. It produces the same answer as generate_SED within ~0.05 dex" model = models.define_model('BPASSv2.2.1.binary/ModSalpeter_300', path_to_SPS_grid=flare.FLARE_dir + '/data/SPS/nebular/3.0/') # DEFINE SED GRID - # model = models.define_model('BPASSv2.2.1.binary/ModSalpeter_300', path_to_SPS_grid = flare.FLARE_dir + '/data/SPS/nebular/2.0/Z_refQ_wdust/') # DEFINE SED GRID - model.dust_ISM = ('simple', {'slope': -1.0}) model.dust_BC = ('simple', {'slope': -1.0}) # --- read in test data test = synthobs.test_data() # --- read in some test data # --- create rest-frame luminosities filters = [ 'FAKE.TH.' + f for f in ['FUV', 'NUV', 'V'] ] # --- define the filters. FAKE.FAKE are just top-hat filters using for extracting rest-frame quantities.
cosmo = flare.default_cosmo() z = 8. test = synthobs.test_data() # --- read in some test data # --- calculate V-band (550nm) optical depth for each star particle A = 5.2 test.tauVs = (10**A) * test.MetSurfaceDensities model = models.define_model('BPASSv2.2.1.binary/ModSalpeter_300') # DEFINE SED GRID - model.dust = {'slope': -1.0} # define dust curve do_test = False if do_test: width_arcsec = 3. f = 'Webb.NIRCam.F150W'
x=reg, u=tag, filt=filters[0])) # exists = os.path.isfile("data/flares_sizes_highresproject" # "_{}_{}_{}_{}_{}.hdf5".format(reg, tag, Type, # orientation, # filters[0].split(".")[-1])) # if not exists: # run = True # else: # run = False run = True model = models.define_model( F'BPASSv2.2.1.binary/Chabrier_300') # DEFINE SED GRID - model.dust_ISM = ('simple', {'slope': -1.}) # Define dust curve for ISM model.dust_BC = ('simple', { 'slope': -1. }) # Define dust curve for birth cloud component # --- create rest-frame luminosities F = flare.filters.add_filters(filters, new_lam=model.lam) model.create_Lnu_grid( F) # --- create new L grid for each filter. In units of erg/s/Hz if run: # Define radii radii_fracs = (0.2, 0.5, 0.8)
def lum(sim, kappa, tag, BC_fac, inp='FLARES', IMF='Chabrier_300', LF=True, filters=['FAKE.TH.FUV'], Type='Total', log10t_BC=7., extinction='default', data_folder='data', aperture='30'): S_mass, S_Z, S_age, S_los, S_len, begin, end, S_ap, DTM = get_data( sim, tag, inp, data_folder, aperture) if np.isscalar(filters): Lums = np.zeros(len(begin), dtype=np.float64) else: Lums = np.zeros((len(begin), len(filters)), dtype=np.float64) model = models.define_model( F'BPASSv2.2.1.binary/{IMF}') # DEFINE SED GRID - if extinction == 'default': model.dust_ISM = ('simple', {'slope': -1.}) #Define dust curve for ISM model.dust_BC = ('simple', { 'slope': -1. }) #Define dust curve for birth cloud component elif extinction == 'Calzetti': model.dust_ISM = ('Starburst_Calzetti2000', {''}) model.dust_BC = ('Starburst_Calzetti2000', {''}) elif extinction == 'SMC': model.dust_ISM = ('SMC_Pei92', {''}) model.dust_BC = ('SMC_Pei92', {''}) elif extinction == 'MW': model.dust_ISM = ('MW_Pei92', {''}) model.dust_BC = ('MW_Pei92', {''}) elif extinction == 'N18': model.dust_ISM = ('MW_N18', {''}) model.dust_BC = ('MW_N18', {''}) else: ValueError("Extinction type not recognised") z = float(tag[5:].replace('p', '.')) # --- create rest-frame luminosities F = flare.filters.add_filters(filters, new_lam=model.lam) model.create_Lnu_grid( F) # --- create new L grid for each filter. In units of erg/s/Hz for jj in range(len(begin)): Masses = S_mass[begin[jj]:end[jj]][S_ap[begin[jj]:end[jj]]] Ages = S_age[begin[jj]:end[jj]][S_ap[begin[jj]:end[jj]]] Metallicities = S_Z[begin[jj]:end[jj]][S_ap[begin[jj]:end[jj]]] MetSurfaceDensities = S_los[begin[jj]:end[jj]][S_ap[begin[jj]:end[jj]]] MetSurfaceDensities = DTM[jj] * MetSurfaceDensities if Type == 'Total': tauVs_ISM = kappa * MetSurfaceDensities # --- calculate V-band (550nm) optical depth for each star particle tauVs_BC = BC_fac * (Metallicities / 0.01) fesc = 0.0 elif Type == 'Pure-stellar': tauVs_ISM = np.zeros(len(Masses)) tauVs_BC = np.zeros(len(Masses)) fesc = 1.0 elif Type == 'Intrinsic': tauVs_ISM = np.zeros(len(Masses)) tauVs_BC = np.zeros(len(Masses)) fesc = 0.0 elif Type == 'Only-BC': tauVs_ISM = np.zeros(len(Masses)) tauVs_BC = BC_fac * (Metallicities / 0.01) fesc = 0.0 else: ValueError(F"Undefined Type {Type}") Lnu = models.generate_Lnu( model=model, F=F, Masses=Masses, Ages=Ages, Metallicities=Metallicities, tauVs_ISM=tauVs_ISM, tauVs_BC=tauVs_BC, fesc=fesc, log10t_BC=log10t_BC ) # --- calculate rest-frame Luminosity. In units of erg/s/Hz Lums[jj] = list(Lnu.values()) return Lums
def get_SED(sim, kappa, tag, BC_fac, inp='FLARES', IMF='Chabrier_300', log10t_BC=7., extinction='default', data_folder='data', aperture='30'): S_mass, S_Z, S_age, S_los, S_len, begin, end, S_ap, DTM = get_data( sim, tag, inp, data_folder, aperture) model = models.define_model( F'BPASSv2.2.1.binary/{IMF}') # DEFINE SED GRID - # --- calculate intrinsic quantities if extinction == 'default': model.dust_ISM = ('simple', {'slope': -1.}) #Define dust curve for ISM model.dust_BC = ('simple', { 'slope': -1. }) #Define dust curve for birth cloud component elif extinction == 'Calzetti': model.dust_ISM = ('Starburst_Calzetti2000', {''}) model.dust_BC = ('Starburst_Calzetti2000', {''}) elif extinction == 'SMC': model.dust_ISM = ('SMC_Pei92', {''}) model.dust_BC = ('SMC_Pei92', {''}) elif extinction == 'MW': model.dust_ISM = ('MW_Pei92', {''}) model.dust_BC = ('MW_Pei92', {''}) elif extinction == 'N18': model.dust_ISM = ('MW_N18', {''}) model.dust_BC = ('MW_N18', {''}) else: ValueError("Extinction type not recognised") for jj in range(len(begin)): Masses = S_mass[begin[jj]:end[jj]][S_ap[begin[jj]:end[jj]]] Ages = S_age[begin[jj]:end[jj]][S_ap[begin[jj]:end[jj]]] Metallicities = S_Z[begin[jj]:end[jj]][S_ap[begin[jj]:end[jj]]] MetSurfaceDensities = S_los[begin[jj]:end[jj]][S_ap[begin[jj]:end[jj]]] MetSurfaceDensities = DTM[jj] * MetSurfaceDensities tauVs_ISM = kappa * MetSurfaceDensities # --- calculate V-band (550nm) optical depth for each star particle tauVs_BC = BC_fac * (Metallicities / 0.01) o = models.generate_SED(model, Masses, Ages, Metallicities, tauVs_ISM=tauVs_ISM, tauVs_BC=tauVs_BC, fesc=0.0) if jj == 0: lam = np.zeros((len(begin), len(o.lam)), dtype=np.float64) #in Angstrom stellar = np.zeros((len(begin), len(o.lam)), dtype=np.float64) intrinsic = np.zeros((len(begin), len(o.lam)), dtype=np.float64) no_ism = np.zeros((len(begin), len(o.lam)), dtype=np.float64) total = np.zeros((len(begin), len(o.lam)), dtype=np.float64) lam[jj] = o.lam stellar[jj] = o.stellar.lnu intrinsic[jj] = o.intrinsic.lnu no_ism[jj] = o.BC.lnu total[jj] = o.total.lnu return lam, stellar, intrinsic, no_ism, total
def flux(sim, kappa, tag, BC_fac, inp='FLARES', IMF='Chabrier_300', filters=flare.filters.NIRCam_W, Type='Total', log10t_BC=7., extinction='default', orientation="sim"): kinp = np.load('/cosma/home/dp004/dc-rope1/cosma7/FLARES/' 'flares/los_extinction/kernel_sph-anarchy.npz', allow_pickle=True) lkernel = kinp['kernel'] header = kinp['header'] kbins = header.item()['bins'] S_mass, S_Z, S_age, S_los, G_Z, S_len, \ G_len, G_sml, S_sml, G_mass, S_coords, G_coords, \ S_vels, G_vels, cops, \ begin, end, gbegin, gend = get_data(sim, tag, inp) Fnus = {f: np.zeros(len(S_mass), dtype=np.float64) for f in filters} model = models.define_model( F'BPASSv2.2.1.binary/{IMF}') # DEFINE SED GRID - if extinction == 'default': model.dust_ISM = ( 'simple', {'slope': -1.}) # Define dust curve for ISM model.dust_BC = ('simple', { 'slope': -1.}) # Define dust curve for birth cloud component elif extinction == 'Calzetti': model.dust_ISM = ('Starburst_Calzetti2000', {''}) model.dust_BC = ('Starburst_Calzetti2000', {''}) elif extinction == 'SMC': model.dust_ISM = ('SMC_Pei92', {''}) model.dust_BC = ('SMC_Pei92', {''}) elif extinction == 'MW': model.dust_ISM = ('MW_Pei92', {''}) model.dust_BC = ('MW_Pei92', {''}) elif extinction == 'N18': model.dust_ISM = ('MW_N18', {''}) model.dust_BC = ('MW_N18', {''}) else: ValueError("Extinction type not recognised") z = float(tag[5:].replace('p', '.')) F = flare.filters.add_filters(filters, new_lam=model.lam * (1. + z)) cosmo = flare.default_cosmo() # --- create new Fnu grid for each filter. In units of nJy/M_sol model.create_Fnu_grid(F, z, cosmo) for jj in range(len(begin)): # Extract values for this galaxy Masses = S_mass[begin[jj]: end[jj]] Ages = S_age[begin[jj]: end[jj]] Metallicities = S_Z[begin[jj]: end[jj]] gasMetallicities = G_Z[begin[jj]: end[jj]] gasSML = G_sml[begin[jj]: end[jj]] gasMasses = G_mass[begin[jj]: end[jj]] if orientation == "sim": starCoords = S_coords[:, begin[jj]: end[jj]].T - cops[:, jj] S_coords[:, begin[jj]: end[jj]] = starCoords.T MetSurfaceDensities = S_los[begin[jj]:end[jj]] elif orientation == "face-on": starCoords = S_coords[:, begin[jj]: end[jj]].T - cops[:, jj] gasCoords = G_coords[:, begin[jj]: end[jj]].T - cops[:, jj] gasVels = G_vels[:, begin[jj]: end[jj]].T # Get angular momentum vector ang_vec = util.ang_mom_vector(gasMasses, gasCoords, gasVels) # Rotate positions starCoords = util.get_rotated_coords(ang_vec, starCoords) gasCoords = util.get_rotated_coords(ang_vec, gasCoords) S_coords[:, begin[jj]: end[jj]] = starCoords.T MetSurfaceDensities = util.get_Z_LOS(starCoords, gasCoords, gasMasses, gasMetallicities, gasSML, (0, 1, 2), lkernel, kbins) elif orientation == "side-on": starCoords = S_coords[:, begin[jj]: end[jj]].T - cops[:, jj] gasCoords = G_coords[:, begin[jj]: end[jj]].T - cops[:, jj] gasVels = G_vels[:, begin[jj]: end[jj]].T # Get angular momentum vector ang_vec = util.ang_mom_vector(gasMasses, gasCoords, gasVels) # Rotate positions starCoords = util.get_rotated_coords(ang_vec, starCoords) gasCoords = util.get_rotated_coords(ang_vec, gasCoords) S_coords[:, begin[jj]: end[jj]] = starCoords.T MetSurfaceDensities = util.get_Z_LOS(starCoords, gasCoords, gasMasses, gasMetallicities, gasSML, (2, 0, 1), lkernel, kbins) else: MetSurfaceDensities = None print(orientation, "is not an recognised orientation. " "Accepted types are 'sim', 'face-on', or 'side-on'") Mage = np.nansum(Masses * Ages) / np.nansum(Masses) Z = np.nanmean(gasMetallicities) MetSurfaceDensities = DTM_fit(Z, Mage) * MetSurfaceDensities if Type == 'Total': # --- calculate V-band (550nm) optical depth for each star particle tauVs_ISM = kappa * MetSurfaceDensities tauVs_BC = BC_fac * (Metallicities / 0.01) fesc = 0.0 elif Type == 'Pure-stellar': tauVs_ISM = np.zeros(len(Masses)) tauVs_BC = np.zeros(len(Masses)) fesc = 1.0 elif Type == 'Intrinsic': tauVs_ISM = np.zeros(len(Masses)) tauVs_BC = np.zeros(len(Masses)) fesc = 0.0 elif Type == 'Only-BC': tauVs_ISM = np.zeros(len(Masses)) tauVs_BC = BC_fac * (Metallicities / 0.01) fesc = 0.0 else: tauVs_ISM = None tauVs_BC = None fesc = None ValueError(F"Undefined Type {Type}") # --- calculate rest-frame Luminosity. In units of erg/s/Hz for f in filters: # --- calculate rest-frame flux of each object in nJy Fnu = models.generate_Fnu_array(model, Masses, Ages, Metallicities, tauVs_ISM, tauVs_BC, F, f, fesc=fesc, log10t_BC=log10t_BC) Fnus[f][begin[jj]: end[jj]] = Fnu Fnus["coords"] = S_coords Fnus["smls"] = S_sml Fnus["begin"] = begin Fnus["end"] = end return Fnus
def lum(sim, kappa, tag, BC_fac, inp='FLARES', IMF='Chabrier_300', LF=True, filters=('FAKE.TH.FUV',), Type='Total', log10t_BC=7., extinction='default', orientation="sim", masslim=None, nlim=None): kinp = np.load('/cosma7/data/dp004/dc-payy1/my_files/' 'los/kernel_sph-anarchy.npz', allow_pickle=True) lkernel = kinp['kernel'] header = kinp['header'] kbins = header.item()['bins'] S_mass_ini, S_Z, S_age, S_los, G_Z, S_len, \ G_len, G_sml, S_sml, G_mass, S_coords, G_coords, \ S_vels, G_vels, S_mass, cops, S_bool, G_bool, \ begin, end, gbegin, gend, grpid, subgrpid = get_data(sim, tag, inp) print("Got data, there are ", len(begin), "galaxies") Lums = {f: np.zeros(len(S_mass), dtype=np.float64) for f in filters} for f in filters: Lums[f + "tauVs_ISM"] = np.zeros(len(S_mass), dtype=np.float64) Lums[f + "tauVs_BC"] = np.zeros(len(S_mass), dtype=np.float64) Lums[f + "log10t_BC"] = np.zeros(len(begin), dtype=np.float64) model = models.define_model( F'BPASSv2.2.1.binary/{IMF}') # DEFINE SED GRID - if extinction == 'default': model.dust_ISM = ( 'simple', {'slope': -1.}) # Define dust curve for ISM model.dust_BC = ('simple', { 'slope': -1.}) # Define dust curve for birth cloud component elif extinction == 'Calzetti': model.dust_ISM = ('Starburst_Calzetti2000', {''}) model.dust_BC = ('Starburst_Calzetti2000', {''}) elif extinction == 'SMC': model.dust_ISM = ('SMC_Pei92', {''}) model.dust_BC = ('SMC_Pei92', {''}) elif extinction == 'MW': model.dust_ISM = ('MW_Pei92', {''}) model.dust_BC = ('MW_Pei92', {''}) elif extinction == 'N18': model.dust_ISM = ('MW_N18', {''}) model.dust_BC = ('MW_N18', {''}) else: ValueError("Extinction type not recognised") z = float(tag[5:].replace('p', '.')) # Convert coordinates to physical S_coords = S_coords / (1 + z) G_coords = G_coords / (1 + z) cops = cops / (1 + z) # --- create rest-frame luminosities F = flare.filters.add_filters(filters, new_lam=model.lam) model.create_Lnu_grid( F) # --- create new L grid for each filter. In units of erg/s/Hz for jj in range(len(begin)): # Extract values for this galaxy sbool = S_bool[begin[jj]: end[jj]] gbool = G_bool[gbegin[jj]: gend[jj]] Masses = S_mass_ini[begin[jj]: end[jj]][sbool] Ages = S_age[begin[jj]: end[jj]][sbool] Metallicities = S_Z[begin[jj]: end[jj]][sbool] gasMetallicities = G_Z[gbegin[jj]: gend[jj]][gbool] gasSML = G_sml[gbegin[jj]: gend[jj]][gbool] gasMasses = G_mass[gbegin[jj]: gend[jj]][gbool] if masslim != None: if np.sum(Masses) < masslim: for f in filters: Lums[f][begin[jj]: end[jj]] = np.nan continue if nlim != None: if (end[jj] - begin[jj]) < nlim: for f in filters: Lums[f][begin[jj]: end[jj]] = np.nan continue if orientation == "sim": starCoords = S_coords[:, begin[jj]: end[jj]].T[sbool] gasCoords = G_coords[:, gbegin[jj]: gend[jj]].T[gbool] MetSurfaceDensities = util.get_Z_LOS(starCoords, gasCoords, gasMasses, gasMetallicities, gasSML, (0, 1, 2), lkernel, kbins) S_coords[:, begin[jj]: end[jj]][:, sbool] = (starCoords - cops[:, jj]).T G_coords[:, gbegin[jj]: gend[jj]][:, gbool] = (gasCoords - cops[:, jj]).T elif orientation == "face-on": starCoords = S_coords[:, begin[jj]: end[jj]][:, sbool].T - cops[:, jj] gasCoords = G_coords[:, gbegin[jj]: gend[jj]][:, gbool].T - cops[:, jj] gasVels = G_vels[gbegin[jj]: gend[jj], :][gbool] # Get angular momentum vector ang_vec = util.ang_mom_vector(gasMasses, gasCoords, gasVels) # Rotate positions starCoords = util.get_rotated_coords(ang_vec, starCoords) gasCoords = util.get_rotated_coords(ang_vec, gasCoords) S_coords[:, begin[jj]: end[jj]][:, sbool] = starCoords.T MetSurfaceDensities = util.get_Z_LOS(starCoords, gasCoords, gasMasses, gasMetallicities, gasSML, (0, 1, 2), lkernel, kbins) elif orientation == "side-on": starCoords = S_coords[:, begin[jj]: end[jj]][:, sbool].T - cops[:, jj] gasCoords = G_coords[:, gbegin[jj]: gend[jj]][:, gbool].T - cops[:, jj] gasVels = G_vels[gbegin[jj]: gend[jj], :][gbool] # Get angular momentum vector ang_vec = util.ang_mom_vector(gasMasses, gasCoords, gasVels) # Rotate positions starCoords = util.get_rotated_coords(ang_vec, starCoords) gasCoords = util.get_rotated_coords(ang_vec, gasCoords) S_coords[:, begin[jj]: end[jj]][:, sbool] = starCoords.T MetSurfaceDensities = util.get_Z_LOS(starCoords, gasCoords, gasMasses, gasMetallicities, gasSML, (2, 0, 1), lkernel, kbins) else: MetSurfaceDensities = None print(orientation, "is not an recognised orientation. " "Accepted types are 'sim', 'face-on', or 'side-on'") Mage = np.nansum(Masses * Ages) / np.nansum(Masses) Z = np.nanmean(gasMetallicities) MetSurfaceDensities = DTM_fit(Z, Mage) * MetSurfaceDensities if Type == 'Total': # --- calculate V-band (550nm) optical depth for each star particle tauVs_ISM = kappa * MetSurfaceDensities tauVs_BC = BC_fac * (Metallicities / 0.01) fesc = 0.0 elif Type == 'Pure-stellar': tauVs_ISM = np.zeros(len(Masses)) tauVs_BC = np.zeros(len(Masses)) fesc = 1.0 elif Type == 'Intrinsic': tauVs_ISM = np.zeros(len(Masses)) tauVs_BC = np.zeros(len(Masses)) fesc = 0.0 elif Type == 'Only-BC': tauVs_ISM = np.zeros(len(Masses)) tauVs_BC = BC_fac * (Metallicities / 0.01) fesc = 0.0 else: tauVs_ISM = None tauVs_BC = None fesc = None ValueError(F"Undefined Type {Type}") # --- calculate rest-frame Luminosity. In units of erg/s/Hz for f in filters: Lnu = models.generate_Lnu_array(model=model, Masses=Masses, Ages=Ages, Metallicities=Metallicities, tauVs_ISM=tauVs_ISM, tauVs_BC=tauVs_BC, F=F, f=f, fesc=fesc, log10t_BC=log10t_BC) Lums[f][begin[jj]: end[jj]][sbool] = Lnu Lums[f + "tauVs_ISM"][begin[jj]: end[jj]][sbool] = tauVs_ISM Lums[f + "tauVs_BC"][begin[jj]: end[jj]][sbool] = tauVs_BC Lums[f + "log10t_BC"][jj] = log10t_BC Lums[f][begin[jj]: end[jj]][~sbool] = np.nan Lums[f + "tauVs_ISM"][begin[jj]: end[jj]][~sbool] = np.nan Lums[f + "tauVs_BC"][begin[jj]: end[jj]][~sbool] = np.nan Lums["coords"] = S_coords Lums["gcoords"] = G_coords Lums["grpid"] = grpid Lums["subgrpid"] = subgrpid Lums["smls"] = S_sml Lums["masses"] = S_mass Lums["gmasses"] = G_mass Lums["S_age"] = S_age Lums["S_Z"] = S_Z Lums["G_Z"] = G_Z Lums["nstar"] = S_len Lums["begin"] = begin Lums["end"] = end Lums["gbegin"] = gbegin Lums["gend"] = gend Lums["S_bool"] = S_bool Lums["G_bool"] = G_bool return Lums # , S_len + G_len