예제 #1
0
def gen_dataset(nsne, under_model):

    #under_model = pop_model()
    z_true, x1_true, c_true, x0_true = under_model.gen_dataset_params(nsne)

    t0_true = np.zeros(nsne)

    if not os.path.exists("testdata_%s" % (under_model.name(nsne))):
        os.mkdir("testdata_%s" % (under_model.name(nsne)))

    # Pretend we observe all SNe with the same cadence and bands
    time = np.arange(-30., 70.)
    band = np.array(25 * ['desg', 'desr', 'desi', 'desz'])
    zp = 25. * np.ones_like(time)
    zpsys = np.array(100 * ['ab'])

    model = sncosmo.Model(source='salt2-extended')

    i = 1
    for z, t0, x0, x1, c in zip(z_true, t0_true, x0_true, x1_true, c_true):
        param_dict = dict(z=z, t0=t0, x0=x0, x1=x1, c=c)
        model.set(**param_dict)
        flux_true = model.bandflux(band, time, zp=zp, zpsys=zpsys)
        fluxerr = 0.1 * np.max(flux_true) * np.ones_like(flux_true)
        flux = normal(flux_true, fluxerr)

        data = Table((time, band, flux, fluxerr, zp, zpsys),
                     names=('time', 'band', 'flux', 'fluxerr', 'zp', 'zpsys'),
                     meta=param_dict)
        sncosmo.write_lc(
            data, 'testdata_%s/sn{0:02d}_%s.dat'.format(i) %
            (under_model.name(nsne), under_model.name(nsne)))
        i += 1
    return
def make_new_files(filename, table, output, sntype):
    with open(filename) as f:
        data = f.read().split("\n")
    filters = data[5].split()
    survey = data[0].split()
    stuffRA = data[6].split()
    stuffDec = data[7].split()
    MWEBV = data[11].split()
    if sntype > 3:
        if sntype >=20 and sntype < 30:
            sntype=2
        if sntype >=30:
            sntype=3
    if sntype==1:
        typestring='SN Type = Ia , MODEL = mlcs2k2.SNchallenge'
    elif sntype==2:
        typestring='SN Type = II , MODEL = SDSS-017564'
    elif sntype==3:
        typestring='SN Type = Ic , MODEL = SDSS-014475'
    else:
        typestring='SOMETHING WENT HORRIBLY WRONG'
    table.meta = {survey[0][:-1]: survey[1], stuffRA[0][:-1]: stuffRA[1], stuffDec[0][:-1]: stuffDec[1],filters[0][:-1]: filters[1],
                 MWEBV[0][:-1]: MWEBV[1], 'SNTYPE': -9, 'SIM_COMMENT': typestring  }
    #table.rename_column('mjd', 'MJD')
    #table.rename_column('filter ', 'FLT')
    #table.rename_column('flux', 'FLUXCAL')
    #table.rename_column('flux_error', 'FLUXCALERR')
    sncosmo.write_lc(table, 'new_mocks/%s'%output,pedantic=True, format = 'snana')
예제 #3
0
파일: metrics.py 프로젝트: rbiswas4/Cadence
    def writeLightCurve(self,
                        fname,
                        format='ascii',
                        nightlyCoadd=False,
                        **kwargs):

        lc = self.SNCosmoLC(nightlyCoadd=nightlyCoadd)
        sncosmo.write_lc(lc, fname, format=format, **kwargs)
예제 #4
0
    def writeLightCurve(self,
                        fname,
                        format='ascii',
                        nightlyCoadd=False,
                        **kwargs):

        lc = self.SNCosmoLC(nightlyCoadd=nightlyCoadd)
        sncosmo.write_lc(lc, fname, format=format, **kwargs)
예제 #5
0
def test_roundtripping():
    for format in ['json', 'ascii', 'salt2']:
        f = NamedTemporaryFile(delete=False)
        f.close()  # close to ensure that we can open it in write_lc()

        # raw=True is for the benefit of salt2 writer that modifies column
        # and header names by default.
        sncosmo.write_lc(lcdata, f.name, format=format, raw=True,
                         pedantic=False)
        data = sncosmo.read_lc(f.name, format=format)

        for key in lcdata.colnames:
            assert np.all(data[key] == lcdata[key])
        for key in lcdata.meta:
            assert data.meta[key] == lcdata.meta[key]

        os.unlink(f.name)
예제 #6
0
파일: test_io.py 프로젝트: tallamjr/sncosmo
def test_roundtripping():
    for format in ['json', 'ascii', 'salt2']:
        f = NamedTemporaryFile(delete=False)
        f.close()  # close to ensure that we can open it in write_lc()

        # raw=True is for the benefit of salt2 writer that modifies column
        # and header names by default.
        sncosmo.write_lc(lcdata, f.name, format=format, raw=True,
                         pedantic=False)
        data = sncosmo.read_lc(f.name, format=format)

        for key in lcdata.colnames:
            assert np.all(data[key] == lcdata[key])
        for key in lcdata.meta:
            assert data.meta[key] == lcdata.meta[key]

        os.unlink(f.name)
예제 #7
0
#!/usr/bin/env python
import numpy as np
import sncosmo
from astropy.utils import OrderedDict as odict
from astropy.table import Table


model = sncosmo.ObsModel(source='salt2')
model.set(z=0.5, c=0.2, t0=55100., x1=0.5)
model.set_source_peakabsmag(-19.5, 'bessellb', 'ab')

times = np.linspace(55070., 55150., 40)
bands = np.array(10 * ['sdssg', 'sdssr', 'sdssi', 'sdssz'])
zp = 25. * np.ones(40)
zpsys = np.array(40 * ['ab'])

flux = model.bandflux(bands, times, zp=zp, zpsys=zpsys)
fluxerr = (0.05 * np.max(flux)) * np.ones(40, dtype=np.float)
flux += fluxerr * np.random.randn(40)

data = Table(odict([('time', times), ('band', bands), ('flux', flux), 
                    ('fluxerr', fluxerr), ('zp', zp), ('zpsys', zpsys)]),
             meta=dict(zip(model.param_names, model.parameters)))

sncosmo.write_lc(data, 'example_photometric_data.dat')
예제 #8
0
x = Table(relevantdata[0])

print str(x)

print str(len(relevantdata[0]))

# Write out to a file as this is how we will do things in a larger set by looping through, and then read in the file

model.set(**params[0])

# fig_relevant = sncosmo.plot_lc(relevantdata[0], model=model)

#print "Close Window to continute"
#pl.show()
sncosmo.write_lc(Table(relevantdata[0]), fname='lc.dat', format='ascii')

# sncosmo.write_lc(Table(relevantdata[0]), fname='lc.dat.json', format='json')

# sncosmo.write_lc(Table(relevantdata[0]), fname='lc.dat.fits', format='snana') # fits

lc = sncosmo.read_lc('lc.dat', format='ascii')

fmodel = sncosmo.Model(source='salt2-extended')
for z in zvals:
    fmodel.set(z=z)
    res, fitmodel = sncosmo.fit_lc(relevantdata[0], fmodel, ['t0', 'x0', 'x1', 'c']) 

print res

print params[0]
예제 #9
0
def test_write_lc_snana():
    """Just check if the snana writer works without error."""
    f = NamedTemporaryFile(delete=False)
    f.close()  # close to ensure that we can open it in write_lc()
    sncosmo.write_lc(lcdata, f.name, format='snana', pedantic=False)
    os.unlink(f.name)
예제 #10
0
def test_write_lc_salt2():
    """Extra test to see if column renaming works"""
    f = NamedTemporaryFile(delete=False)
    f.close()  # close to ensure that we can open it in write_lc()
    sncosmo.write_lc(lcdata, f.name, format='salt2')
    os.unlink(f.name)
예제 #11
0
파일: clip.py 프로젝트: srodney/snsed
            print(os.path.basename(f), blue + '-' + red)

    else:
        num = 0
        maxBand = None
        maxBand2 = None
        for Band in [
                x for x in np.unique(lc['Band'])
                if x in ['B', 'V', 'R', 'g', 'r']
        ]:
            temp = len(lc[lc['Band'] == Band])
            if temp > num:
                maxBand = Band
                num = temp
        blue = maxBand
        num = 0
        maxBand = None
        maxBand2 = None
        for Band in [x for x in np.unique(lc['Band']) if x in ['J', 'H', 'K']]:
            temp = len(lc[lc['Band'] == Band])
            if temp > num:
                maxBand2 = Band
                num = temp
        red = maxBand2
        if red and blue:
            print(os.path.basename(f), blue + '-' + red)
    if not blue or not red:
        remove.append(f)
    sncosmo.write_lc(lc, f[:-4] + '_clipped.dat')
#print(remove)
예제 #12
0
def test_write_lc_salt2():
    """Extra test to see if column renaming works"""
    f = NamedTemporaryFile(delete=False)
    f.close()  # close to ensure that we can open it in write_lc()
    sncosmo.write_lc(lcdata, f.name, format='salt2')
    os.unlink(f.name)
예제 #13
0
import os
import numpy as np
import sncosmo

lcs = sncosmo.read_lc('../lc.standardsystem.sesn_allphot.dat')
sne = np.unique(lcs['Name'])
for s in sne:
    sncosmo.write_lc(lcs[lcs['Name'] == s], 'lc_' + s + '.dat')
예제 #14
0
def simulate_simlib(simlibfile, snmodelsource, outfile="LC/simulatedlc.dat", restrict=10):
    """
    Simulate SN based on the simlibfile using SNCosmo SALT models


    Parameters
    ----------
    simlibfile :

    snmodelsource:

    outfile:

    Returns
    -------

    """
    from astropy.units import Unit
    from astropy.coordinates import SkyCoord

    # read the simlibfile into obstables
    meta, obstables = sncosmo.read_snana_simlib(simlibfilename)

    # set the SNCosmo model source
    dustmaproot = os.getenv("SIMS_DUSTMAPS_DIR")
    map_dir = os.path.join(dustmaproot, "DustMaps")
    dust = sncosmo.CCM89Dust()
    model = Model(
        source=snmodelsource, effects=[dust, dust], effect_frames=["rest", "obs"], effect_names=["host", "mw"]
    )

    maxSNperField = restrict
    # Different fields in SIMLIB are indexed by libids
    libids = obstables.keys()
    lcs = []
    for libid in libids:

        # Get the obstable corresponding to each field
        obstable = obstables[libid]
        manipulateObsTable(obstable)
        # Need Area from PixSize
        ra = obstable.meta["RA"]
        dec = obstable.meta["DECL"]
        area = obstable.meta["PIXSIZE"]
        maxmjd = obstable["time"].max()
        minmjd = obstable["time"].min()
        rangemjd = maxmjd - minmjd
        skycoords = SkyCoord(ra, dec, unit="deg")
        t_mwebv = sncosmo.get_ebv_from_map(skycoords, mapdir=map_dir, interpolate=False)
        model.set(mwebv=t_mwebv)
        params = []
        # col = Table.Column(obstable['SEARCH'].size*['ab'], name='zpsys')
        # obstable['FLT'].name =  'band'
        # obstable['MJD'].name =  'time'
        # obstable['ZPTAVG'].name =  'zp'
        # obstable['CCD_GAIN'].name =  'gain'
        # obstable['SKYSIG'].name =  'skynoise'
        # obstable.add_column(col)
        redshifts = list(sncosmo.zdist(0.0, 1.2, ratefunc=cosmoRate, time=rangemjd, area=area))
        print "num SN generated ", len(redshifts)
        for i, z in enumerate(redshifts):
            mabs = normal(-19.3, 0.3)
            model.set(z=z)
            model.set_source_peakabsmag(mabs, "bessellb", "ab")
            x0 = model.get("x0")
            # RB: really should not be min, max but done like in catalogs
            p = {"z": z, "t0": uniform(minmjd, maxmjd), "x0": x0, "x1": normal(0.0, 1.0), "c": normal(0.0, 0.1)}
            params.append(p)
            if maxSNperField is not None:
                if i == maxSNperField:
                    break
        print "realizing SN"
        lcslib = sncosmo.realize_lcs(obstable, model, params, trim_observations=True)
        lcs.append(lcslib)
        # alllcsintables = vstack(lcslib)
    # print alllcsintables[0]
    # print alllcsintables[MJD].size
    # write light curves to disk
    for i, field in enumerate(lcs):
        for snid, lc in enumerate(field):
            sncosmo.write_lc(lc, fname=outfile + "_" + str(i) + "_" + str(snid) + ".dat", format="ascii")
    return lcs
예제 #15
0
#!/usr/bin/env python
import numpy as np
import sncosmo
from astropy.utils import OrderedDict as odict
from astropy.table import Table

model = sncosmo.Model(source='salt2')
model.set(z=0.5, c=0.2, t0=55100., x1=0.5)
model.set_source_peakabsmag(-19.5, 'bessellb', 'ab')

times = np.linspace(55070., 55150., 40)
bands = np.array(10 * ['sdssg', 'sdssr', 'sdssi', 'sdssz'])
zp = 25. * np.ones(40)
zpsys = np.array(40 * ['ab'])

flux = model.bandflux(bands, times, zp=zp, zpsys=zpsys)
fluxerr = (0.05 * np.max(flux)) * np.ones(40, dtype=np.float)
flux += fluxerr * np.random.randn(40)

data = Table(odict([('time', times), ('band', bands), ('flux', flux),
                    ('fluxerr', fluxerr), ('zp', zp), ('zpsys', zpsys)]),
             meta=dict(zip(model.param_names, model.parameters)))

sncosmo.write_lc(data, 'example_photometric_data.dat')
예제 #16
0
def test_write_lc_snana():
    """Just check if the snana writer works without error."""
    f = NamedTemporaryFile(delete=False)
    f.close()  # close to ensure that we can open it in write_lc()
    sncosmo.write_lc(lcdata, f.name, format='snana', pedantic=False)
    os.unlink(f.name)
예제 #17
0
파일: mklc.py 프로젝트: bhayden53/SALT2X
def do_stuff(ctr):
    p = pb.ProgressBar(maxval=740, widgets = [pb.Percentage(),pb.Bar(),pb.ETA()]).start()
    pbctr = 0
    for i,l in enumerate(lc):
        p.update(pbctr)
        pbctr += 1

        restcut = (3000,7000)
        data = sncosmo.read_lc(l, format='salt2')
        try:
            z = data.meta['Redshift']
        except:
            pass
        try:
            z = data.meta['Z_CMB']
        except:
            pass
        try:
            survey = data.meta['SURVEY']
        except:
            pass
        try:
            survey = data.meta['SET']
        except:
            pass
        nickname = data.meta['SN']
        try:
            nickname = str(int(nickname))
        except:
            pass
        mwebv = data.meta['MWEBV']
        dust = sncosmo.CCM89Dust()
        data = astropy.table.Table(data, masked=True)
        #rename columns so that my fitter can handle things
        data.rename_column('Filter', 'tmp')
        data.rename_column('Date', 'time')
        data.rename_column('Flux', 'flux')
        data.rename_column('Fluxerr', 'fluxerr')
        data.rename_column('MagSys', 'zpsys')
        data.rename_column('ZP', 'zp')

        if survey == 'SNLS':
            sn_nickname = l.split('/')[-1].split('.')[0].split('-')[-1]
            band = []
            for j, bp in enumerate(data['tmp']):
                band.append( '%s-%s' %(sn_nickname, bp) )
            band = astropy.table.Column(band, name='band')
            data.add_column(band)
            data.remove_column('tmp')
        else:
            data.rename_column('tmp', 'band')

        # deal with swope filters
        mask = (data['band'] == 'SWOPE2::V')
        nswopev = len(mask.nonzero()[0])
        if nswopev > 0:
            band = []
            for j, bp in enumerate(data['band']):
                if (bp == 'SWOPE2::V'):
                    if (data['time'][j] < 53749):
                        band.append('swope2::v_lc3014')
                    elif (data['time'][j] < 53760):
                        band.append('swope2::v_lc3009')
                    else:
                        band.append('swope2::v_lc9844')
                else:
                    band.append(bp)
            data.remove_column('band')
            band = astropy.table.Column(band, name='band')
            data.add_column(band)

            ind = np.where( (data['band'] == 'SWOPE2::V') & (data['time']>53749.) & ((data['time']<=53760.)) )
            data['band'][ind] = 'swope2::v_lc3009'
            ind = np.where( (data['band'] == 'SWOPE2::V') & (data['time']>53760.) )
            data['band'][ind] = 'swope2::v_lc9844'
            # print ind

        #deal with filter coverage
        #also deal with STANDARD filter zeropoints
        unique_bands = np.unique(data['band'])
        fit_bands = []
        nofit_bands = []
        # print unique_bands
        tmperr = np.copy(data['fluxerr'])
        for ub in unique_bands:
            # print ub
            bp = sncosmo.get_bandpass(ub)
            rest = bp.wave_eff / (1.0+z)
            if (rest >= restcut[0]) & (rest <= restcut[1]):
                fit_bands.append(ub)
            else:
                nofit_bands.append(ub)
            if 'STANDARD' in ub:
                ind = np.where(data['band'] == ub)
                data['zp'][ind] = data['zp'][ind] - float(standard_zps[ub])
                errcor = 10**(-0.4*standard_zps[ub])
                data['fluxerr'][ind] *= errcor
            if '4SHOOTER2' in ub:
                ind = np.where(data['band'] == ub)
                data['zp'][ind] = data['zp'][ind] - float(FourShooter_zps[ub])
                errcor = 10**(-0.4*FourShooter_zps[ub])
                data['fluxerr'][ind] *= errcor
            if 'KEPLERCAM' in ub:
                ind = np.where(data['band'] == ub)
                data['zp'][ind] = data['zp'][ind] - float(keplercam_zps[ub])
                errcor = 10**(-0.4*keplercam_zps[ub])
                data['fluxerr'][ind] *= errcor
                # print ub
                # print data['zp'][ind]
            if 'swope' in ub.lower():
                ind = np.where(data['band'] == ub)
                data['zp'][ind] = data['zp'][ind] - float(swope_zps[ub])
                errcor = 10**(-0.4*swope_zps[ub])
                data['fluxerr'][ind] *= errcor
            if 'sdss' in ub.lower():
                ind = np.where(data['band'] == ub)
                data['zp'][ind] = data['zp'][ind] - float(sdss_zps[ub])
                errcor = 10**(-0.4*sdss_zps[ub])
                data['fluxerr'][ind] *= errcor

        for nfb in nofit_bands:
            mask = data['band'] == nfb
            for c in data.colnames:
                data[c].mask = (data[c].mask | mask)

        mwebv = data.meta['MWEBV']

        mask = data['band'].mask.nonzero()[0]
        data.remove_rows(mask)

        ind = np.where(lcfits['SN'] == nickname)
        t0 = lcfits['DayMax'][ind][0]

        x1r, x1f, c = np.random.multivariate_normal(mean,cov,size=1)[0]
        mu = cosmo.distmod(z).value
        absmag = MB - ar*x1r - af*x1f + beta*c + np.random.normal(scale=sigint, size=1)[0]
        mB = mu + absmag

        source = Salt2XSource(version='2.4', modeldir=modeldir)
        model  = sncosmo.Model(source=source, effects=[dust], effect_names=['mw'], effect_frames=['obs'])

        model.set(z=z, x1=x1f, s=x1r, c=c, t0=t0, mwebv=mwebv)
        model.set_source_peakabsmag(absmag, 'bessellb', 'ab')
        flux = model.bandflux(data['band'], data['time'], zp=data['zp'], zpsys=data['zpsys'])
        whocares, saltcov = model.bandfluxcov(data['band'], data['time'], data['zp'],data['zpsys'])

        # handle model cov blowups
        saltcov = np.copy(saltcov)
        diag = np.copy(saltcov.diagonal())
        model_snr = whocares/np.sqrt(diag)

        ind = np.where((np.abs(model_snr) < 1) & ~np.isnan(model_snr))

        diag[ind] = diag[ind] * np.abs(model_snr[ind])**2
        np.fill_diagonal(saltcov, diag)


        diagerr = np.diag(data['fluxerr']**2)
        fullcov = saltcov + diagerr
        try:
            np.linalg.cholesky(fullcov)
        except:
            print 'Cholesky failed... exiting'
            sys.exit()
            
        noise = np.random.multivariate_normal(np.zeros(len(diagerr)), fullcov, size=1)[0]

        #lower zp, lower flux
        data['flux'] = flux + noise

        data.meta['x1r'] = x1r
        data.meta['x1f'] = x1f
        data.meta['c'] = c
        data.meta['alpha_r'] = ar
        data.meta['alpha_f'] = af
        data.meta['beta'] = beta
        data.meta['MB'] = MB
        data.meta['mB'] = mu+MB
        data.meta['DayMax'] = t0
        data.meta['cosmology'] = 'Planck15'

        data.rename_column('band', 'Filter')
        data.rename_column('time', 'Date')
        data.rename_column('flux', 'Flux')
        data.rename_column('fluxerr', 'Fluxerr')
        data.rename_column('zpsys', 'MagSys')
        data.rename_column('zp', 'ZP')

        if survey == 'SNLS':
            for row in data:
                tmp = row['Filter']
                ind = tmp.find('MEGACAM')
                row['Filter'] = row['Filter'][ind:]

        sncosmo.write_lc(data,'./cadence_sim/lc/%s_%s.list' %(nickname, ctr), format='salt2')
    p.finish()