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 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)