예제 #1
0
def make_lib(R=1000, wmin=1e3, wmax=1e4, velocity=True,
             dirname='./', name='ckc14_new', **extras):
    """Make a new downsampled CKC library, with the desired resolution
    in the desired wavelength range.  This makes both binary files
    (one for each metallicity) and a wavelength file, suitable for use
    in FSPS.  It also makes an hdf5 file.

    This is deprecated in favor of make_lib_byz below
    """
    downsample = utils.read_and_downsample_spectra
    
    if not os.path.exists(dirname):
        os.makedirs(dirname)
    outwave, outres = utils.construct_outwave(R, wmin, wmax,
                                            velocity=velocity)
    wave = np.concatenate(outwave)
    spectra = downsample(outwave, outres, velocity=velocity,
                         write_binary=True,
                         binout=dirname+'{0}_z{{0}}.spectra.bin'.format(name))

    with open(dirname+'{0}.lambda'.format(name), 'w') as f:
        for w in np.concatenate(outwave):
            f.write('{0:15.4f}\n'.format(w))
    params = utils.spec_params(expanded=True)
    with h5py.File(dirname + '{0}.h5'.format(name), 'x') as f:
        dspec = f.create_dataset("spectra", data=spectra)
        dwave = f.create_dataset("wavelengths", data=wave)
        dpar =  f.create_dataset("parameters", data=params)
예제 #2
0
def make_lib_flatfull(R=1000, wmin=1e3, wmax=1e4, velocity=True,
                      h5name='data/h5/ckc14_fsps.flat.h5',
                      outfile='ckc14_new.flat.h5', **extras):
    """Make a new downsampled CKC library, with the desired resolution
    in the desired wavelength range.  This makes an hdf5 file with the
    downsampled spectra.

    :param R:
        Desired resolution in the interval (wmin, wmax) *not including
        the native CKC resolution*, in terms of
        lambda/sigma_lambda. Ouside this interval the resolution will
        be 100.

    :param h5name:
        Full path to the *flat* version of the HDF file containing the
        full CKC grid.

    :param outfile:
        Full path to the output h5 filename.  Note that this will be a
        *flat* spectral file, suitable for use with StarBasis()
    """

    h5fullflat = h5name
    from utils import downsample_onespec as downsample
    # Get the output wavelength grid as segments
    outwave, outres = utils.construct_outwave(R, wmin, wmax,
                                            velocity=velocity)
    wave = np.concatenate(outwave)
    with h5py.File(h5fullflat, "r") as full:
        # Full wavelength vector and number of spectra
        fwave = np.array(full["wavelengths"])
        nspec = full["parameters"].shape[0]
        with h5py.File(outfile, "w") as newf:
            # store the output wavelength vector
            newf.create_dataset("wavelengths", data=wave)
            # copy the spectral parameters over
            full.copy("parameters", newf)
            # create an array to hold the new spectra.
            news = newf.create_dataset("spectra", data=np.ones([nspec, len(wave)]) * 1e-33)
            # loop over the old spectra
            for i in xrange(nspec):
                s = np.array(full["spectra"][i, :])
                # monitor progress
                if np.mod(i , np.int(nspec/10)) == 0:
                    print("doing {0} of {1} spectra".format(i, nspec))
                # don't convolve empty spectra
                if s.max() < 1e-32:
                    continue
                # Actually do the convolution
                lores = downsample(fwave, s, outwave, outres,
                                   velocity=velocity)
                news[i, :] = np.concatenate(lores)
                # flush to disk so you can read the file and monitor
                # progress in another python instance, and if
                # something dies you don't totally lose the data
                if np.mod(i , np.int(nspec/10)) == 0:
                    newf.flush()
예제 #3
0
def make_lib_byz(R=1000, wmin=1e3, wmax=1e4, velocity=True,
             dirname='./', name='ckc14_new', **extras):

    """Make a new downsampled CKC library, with the desired resolution
    in the desired wavelength range.  This makes both binary files
    (one for each metallicity) and a wavelength file, suitable for use
    in FSPS.  It also makes an hdf5 file with the downsampled spectra.
    """
    downsample = utils.read_and_downsample_onez

    # set up output names, directories, and files
    if not os.path.exists(dirname):
        os.makedirs(dirname)
    binout = dirname+'{0}_z{{0}}.spectra.bin'.format(name)
    wout = dirname+'{0}.lambda'.format(name)
    with h5py.File(dirname + '{0}.h5'.format(name), 'x') as f:
        spgr = f.create_group("spectra")

        # Get the output wavelength grid, write to hdf5 and a file
        outwave, outres = utils.construct_outwave(R, wmin, wmax,
                                                velocity=velocity)
        wave = np.concatenate(outwave)
        dwave = f.create_dataset("wavelengths", data=wave)
        with open(wout, 'w') as wf:
            for w in np.concatenate(outwave):
                wf.write('{0:15.4f}\n'.format(w))

        # Parameters
        params = utils.spec_params(expanded=True)
        dpar =  f.create_dataset("parameters", data=params)
        zlegend, logg, logt = utils.spec_params()

        # Get the spectra for each z binary file
        zlist = ['{0:06.4f}'.format(z) for z in zlegend]
        for z in zlist:
            print('doing z={}'.format(z))
            zspec = downsample(z, outwave, outres, binout=binout,
                               write_binary=True, velocity=velocity)
            zd = spgr.create_dataset('z{0}'.format(z), data=zspec)
            f.flush()