def test_make_kurucz_tlusty_spectral_grid(): # download the needed files kurucz_fname = download_rename("kurucz2004.grid.fits") tlusty_fname = download_rename("tlusty.lowres.grid.fits") filter_fname = download_rename("filters.hd5") iso_fname = download_rename("beast_example_phat_iso.csv") # download cached version of spectral grid spec_fname_cache = download_rename("beast_example_phat_spec_grid.hd5") ################ # generate the same spectral grid from the code # read in the cached isochrones oiso = ezIsoch(iso_fname) # define the distance distances = [24.47] distance_unit = units.mag velocity = -300 * units.km / units.s redshift = (velocity / const.c).decompose().value # define the spectral libraries to use osl = stellib.Tlusty(filename=tlusty_fname) + stellib.Kurucz( filename=kurucz_fname) # define the extinction curve to use extLaw = extinction.Gordon16_RvFALaw() filters = [ "HST_WFC3_F275W", "HST_WFC3_F336W", "HST_ACS_WFC_F475W", "HST_ACS_WFC_F814W", "HST_WFC3_F110W", "HST_WFC3_F160W", ] add_spectral_properties_kwargs = dict(filternames=filters) spec_fname = "/tmp/beast_example_phat_spec_grid.hd5" spec_fname, g = make_spectral_grid( "test", oiso, osl=osl, redshift=redshift, distance=distances, distance_unit=distance_unit, spec_fname=spec_fname, filterLib=filter_fname, extLaw=extLaw, add_spectral_properties_kwargs=add_spectral_properties_kwargs, ) # compare the new to the cached version compare_hdf5(spec_fname_cache, spec_fname)
def test_gen_spectral_grid_from_stellib_given_points(): """ Make sure it runs and returns a grid """ osl = stellib.Kurucz() oiso = isochrone.padova2010() chunksize = 10000 # as it is an interator, list does the actual loop list( gen_spectral_grid_from_stellib_given_points(osl, oiso.data, chunksize=chunksize))
def test_stellib_boundaries(): """ Test get_stellib_boundaries function """ import pylab as plt s = stellib.Kurucz() iso = isochrone.padova2010() leftb = get_stellib_boundaries(s, 0.1, 0.3, True) plt.plot(s.Teff, s.logg, ".", color="k") plt.plot(leftb[:, 1], leftb[:, 0], "o-") # leftb = [logg, teff] plt.plot(iso.data["logT"], iso.data["logg"], ",", color="r") data = np.array([iso.data["logg"], iso.data["logT"]]).T aa = points_inside_poly(data, leftb) plt.plot(iso.data["logT"][aa], iso.data["logg"][aa], ",", color="g") plt.xlabel("log(Teff)") plt.ylabel("log(g)") plt.xlim(plt.xlim()[::-1]) plt.ylim(plt.ylim()[::-1])
# Isochrone Model Grid # Current Choices: Padova or MIST # PadovaWeb() -- `modeltype` param for iso sets from ezpadova # (choices: parsec12s_r14, parsec12s, 2010, 2008, 2002) # MISTWeb() -- `rotation` param (choices: vvcrit0.0=default, vvcrit0.4) # # Default: PARSEC+COLIBRI oiso = isochrone.PadovaWeb() # Alternative: PARSEC1.2S -- old grid parameters #oiso = isochrone.PadovaWeb(modeltype='parsec12s', filterPMS=True) # Alternative: MIST -- v1, no rotation #oiso = isochrone.MISTWeb() # Stellar Atmospheres library definition osl = stellib.Tlusty() + stellib.Kurucz() ################ ### Dust extinction grid definition extLaw = extinction.Gordon16_RvFALaw() # A(V): dust column in magnitudes # acceptable avs > 0.0 # example [min, max, step] = [0.0, 10.055, 1.0] avs = [0.0, 10.055, 1.0] av_prior_model = {'name': 'flat'} #av_prior_model = {'name': 'lognormal', # 'max_pos': 2.0, # 'sigma': 1.0, # 'N': 10.}
def main_last_grid(): s = stellib.Kurucz() iso = isochrone.padova2010() gen_spectral_grid_from_kurucz("tmp.fits", s, iso)
def make_spectral_grid( project, oiso, osl=None, bounds={}, verbose=True, spec_fname=None, distance=10, distance_unit=units.pc, redshift=0.0, filterLib=None, add_spectral_properties_kwargs=None, extLaw=None, **kwargs, ): """ The spectral grid is generated using the stellar parameters by interpolation of the isochrones and the generation of spectra into the physical units Parameters ---------- project: str project name oiso: isochrone.Isochrone object set of isochrones to use osl: stellib.Stellib object Spectral library to use (default stellib.Kurucz) distance: float or list of float distances at which models should be shifted, specified as a single number or as [min, max, step] 0 means absolute magnitude. distance_unit: astropy length unit or mag distances will be evenly spaced in this unit therefore, specifying a distance grid in mag units will lead to a log grid redshift: float Redshift to which wavelengths should be shifted Default is 0 (rest frame) spec_fname: str full filename to save the spectral grid into filterLib: str full filename to the filter library hd5 file extLaw: extinction.ExtLaw (default=None) if set, only save the spectrum for the wavelengths over which the extinction law is valid add_spectral_properties_kwargs: dict keyword arguments to call :func:`add_spectral_properties` to add model properties from the spectra into the grid property table Returns ------- fname: str name of saved file g: grid.SpectralGrid object spectral grid to transform """ if spec_fname is None: spec_fname = "%s/%s_spec_grid.hd5" % (project, project) # remove the isochrone points with logL=-9.999 oiso.data = oiso[oiso["logL"] > -9] if not os.path.isfile(spec_fname): osl = osl or stellib.Kurucz() # filter extrapolations of the grid with given sensitivities in # logg and logT if "dlogT" not in bounds: bounds["dlogT"] = 0.1 if "dlogg" not in bounds: bounds["dlogg"] = 0.3 # make the spectral grid if verbose: print("Make spectra") g = creategrid.gen_spectral_grid_from_stellib_given_points( osl, oiso.data, bounds=bounds) # Construct the distances array. Turn single value into # 1-element list if single distance is given. _distance = np.atleast_1d(distance) if len(_distance) == 3: mindist, maxdist, stepdist = _distance distances = np.arange(mindist, maxdist + stepdist, stepdist) elif len(_distance) == 1: distances = np.array(_distance) else: raise ValueError( "distance needs to be (min, max, step) or single number") # calculate the distances in pc if distance_unit == units.mag: distances = np.power(10, distances / 5.0 + 1) * units.pc else: distances = (distances * distance_unit).to(units.pc) print("applying {} distances".format(len(distances))) if verbose: print( "Adding spectral properties:", add_spectral_properties_kwargs is not None, ) if add_spectral_properties_kwargs is not None: nameformat = ( add_spectral_properties_kwargs.pop("nameformat", "{0:s}") + "_nd") # Apply the distances to the stars. Seds already at 10 pc, need # multiplication by the square of the ratio to this distance. # TODO: Applying the distances might have to happen in chunks # for larger grids. def apply_distance_and_spectral_props(g): # distance g = creategrid.apply_distance_grid(g, distances, redshift=redshift) # spectral props if add_spectral_properties_kwargs is not None: g = creategrid.add_spectral_properties( g, nameformat=nameformat, filterLib=filterLib, **add_spectral_properties_kwargs, ) # extinction if extLaw is not None: ext_law_range_A = 1e4 / np.array(extLaw.x_range) valid_lambda = np.where((g.lamb > np.min(ext_law_range_A)) & (g.lamb < np.max(ext_law_range_A)))[0] g.lamb = g.lamb[valid_lambda] g.seds = g.seds[:, valid_lambda] return g # Perform the extensions defined above and Write to disk if hasattr(g, "write"): g = apply_distance_and_spectral_props(g) g.write(spec_fname) else: for gk in g: gk = apply_distance_and_spectral_props(gk) gk.write(spec_fname, append=True) g = SpectralGrid(spec_fname, backend="memory") return (spec_fname, g)