Esempio n. 1
0
def writeexfitres(fitresfile,fitresoutfile):
    import os
    from txtobj import txtobj
    import numpy as np

    fitresheader = """# VERSION: PS1_PS1MD
# FITOPT:  NONE
# ---------------------------------------- 
NVAR: 31 
VARNAMES:  CID IDSURVEY TYPE FIELD zHD zHDERR HOST_LOGMASS HOST_LOGMASS_ERR SNRMAX1 SNRMAX2 SNRMAX3 PKMJD PKMJDERR x1 x1ERR c cERR mB mBERR x0 x0ERR COV_x1_c COV_x1_x0 COV_c_x0 NDOF FITCHI2 FITPROB PNN_Ia PTRUE_Ia SIM_TYPE_INDEX SIM_ZCMB
# VERSION_SNANA      = v10_39i 
# VERSION_PHOTOMETRY = PS1_PS1MD 
# TABLE NAME: FITRES 
# 
"""
    fitresvars = ["CID","IDSURVEY","TYPE","FIELD",
                  "zHD","zHDERR","HOST_LOGMASS",
                  "HOST_LOGMASS_ERR","SNRMAX1","SNRMAX2",
                  "SNRMAX3","PKMJD","PKMJDERR","x1","x1ERR",
                  "c","cERR","mB","mBERR","x0","x0ERR","COV_x1_c",
                  "COV_x1_x0","COV_c_x0","NDOF","FITCHI2","FITPROB",
                  "PNN_Ia","PTRUE_Ia","SIM_TYPE_INDEX","SIM_ZCMB"]
    fitresfmt = 'SN: %s %i %i %s %.5f %.5f %.4f %.4f %.4f %.4f %.4f %.3f %.3f %8.5e %8.5e %8.5e %8.5e %.4f %.4f %8.5e %8.5e %8.5e %8.5e %8.5e %i %.4f %.4f %.4f %.4f %i %.5f'

    fr = txtobj(fitresfile,fitresheader=True)
    cols = np.where((fr.x1 > -3) & (fr.x1 < 3) &
                    (fr.c > -0.3) & (fr.c < 0.3) &
                    (fr.x1ERR < 1) & (fr.PKMJDERR < 2*(1+fr.zHD)) &
                    (fr.FITPROB >= 0.001) &
                    (fr.zHD > 0.01) & (fr.zHD < 0.7) &
                    (fr.PNN_Ia >= 0))
    for k in fr.__dict__.keys():
        fr.__dict__[k] = fr.__dict__[k][cols]

    writefitres(fr,range(len(fr.CID)),
                fitresoutfile,fitresheader=fitresheader,
                fitresvars=fitresvars,fitresfmt=fitresfmt)
    return(fitresoutfile)
Esempio n. 2
0
def main(id,outfile='',filters='griz'):

    import pipeclasses
    params = pipeclasses.paramfileclass()
    params.loadfile(os.environ['PIPE_PARAMS'])
    params.loadfile(os.environ['EXTRAPARAMFILE'],addflag=True)

    if not outfile:
        fout = open('%s.supernova'%id,'w')
    else:
        fout = open(outfile,'w')

    nfiles = 0
    nxpix = 0
    for f in filters:
        impath = '%s/%s/%s'%(params.get('WORKSPACE_DIR'),id,f)
        files = glob.glob('%s/*%s'%(impath,params.get('SWARPSUFFIX')))
        nfiles += len(files)
        if not nxpix:
            nxpix = pyfits.getval(files[0],'NAXIS1')
            nypix = pyfits.getval(files[0],'NAXIS2')

    objlist = txtobj(params.get('C2E_EVENTLIST'))
    col = np.where(objlist.PS1id == id)
    ra,dec,peakmjd = objlist.ra[col][0],\
        objlist.dec[col][0],objlist.mjd_disc[col][0]

    header = """SURVEY:    PS1
SNID:      %s
FILTERS:   griz
PIXSIZE:   0.25    # arcsec
NXPIX:     %i
NYPIX:     %i
ZPFLUX:    30.0     # PS1 FLUX zero point
PSF_FORMAT: DAOPHOT
PEAKMJD:  %.1f
WEIGHT_BADPIXEL:  3  99999
RA: %.7f
DECL: %.7f

IMAGE_DIR: 
PSF_UNIT: pixels
PSF_SIZEPARAM: sigma
PSF_MODEL: daophot
PLATESCALE: %s

NVAR: 12 
NOBS: %i
VARNAMES: ID_OBS ID_COADD MJD BAND GAIN FLUX FLUXERR IMAGE_NAME_SEARCH IMAGE_NAME_WEIGHT IMAGE_NAME_PSF IMAGE_NAME_MASK CATALOG_FILE IMAGE_ZPT"""%(id,nxpix,nypix,peakmjd,ra,dec,params.get('SW_PLATESCALE'),nfiles)
    print >> fout, header

    count = 1
    swarpsuffix = params.get('SWARPSUFFIX')
    icmpsuffix = params.get('IDLDAOPHOTCMPSUFFIX')
    
    for f in filters:
        impath = '%s/%s/%s'%(params.get('WORKSPACE_DIR'),id,f)
        files = glob.glob('%s/*%s'%(impath,params.get('IDLDAOPHOTCMPSUFFIX')))
        for fl in files:
            line = 'OBS: %i %i %.1f %s %s -99 -99 '%(count,count,pyfits.getval(fl,'MJD-OBS'),
                                                     f,pyfits.getval(fl,'GAIN'))
            line += '%s %s %s %s %s %s'%(fl.replace('.icmp','.fits'),
                                         fl.replace('.icmp','.noise.fits'),
                                         fl.replace('.icmp','.dao.psf.fits'),
                                         fl.replace('.icmp','.mask.fits'),
                                         fl.replace('.icmp','.absphotcat.wcs'),
                                         pyfits.getval(fl,'ZPTMAG'))
            print >> fout, line

    fout.close()
Esempio n. 3
0
    def mkParamDict(self,zcontrol):
        """Make a dictionary to store all info about parameters"""
        from txtobj import txtobj
        pf = txtobj(self.options.mcmcparamfile)

        if self.options.twogauss:
            pf.use[pf.param == 'popB2mean'] = 1
            pf.use[pf.param == 'popB2std'] = 1
        if self.options.skewedgauss:
            pf.use[pf.param == 'skewB'] = 1
        if len(self.options.fix):
            for fixvar in self.options.fix:
                print('Fixing parameter %s!!'%fixvar)
                pf.fixed[pf.param == fixvar] = 1
        if len(self.options.use):
            for use in self.options.use:
                usevar,useval = use
                useval = int(useval)
                print('use = %i for parameter %s!!'%(useval,usevar))
                pf.use[pf.param == usevar] = useval
        if len(self.options.bounds):
            for bounds in self.options.bounds:
                boundsvar,lbound,ubound = bounds
                lbound,ubound = float(lbound),float(ubound)
                print('%.3f < %s < %.3f !!'%(lbound,boundsvar,ubound))
                pf.lbound[pf.param == boundsvar] = lbound
                pf.ubound[pf.param == boundsvar] = ubound
        if len(self.options.guess):
            for guess in self.options.guess:
                guessvar,guessval = guess
                guessvar = float(guessvar)
                print('initial guess = %.3f for parameter %s!!'%(
                        guessval,guessvar))
                pf.guess[pf.param == guessvar] = guessval
        if len(self.options.prior):
            for prior in self.options.prior:
                priorvar,priormean,priorstd = prior
                priormean,priorstd = float(priormean),float(priorstd)
                print('Prior = %.3f +/- %.3f for parameter %s!!'%(
                        priormean,priorstd,priorvar))
                pf.prior[pf.param == priorvar] = priormean
                pf.sigma[pf.param == priorvar] = sigma
        if len(self.options.bins):
            for bins in self.options.bins:
                binvar,nbins = bins
                nbins = float(nbins)
                print('%i bins for parameter %s!!'%(nbins,binvar))
                pf.bins[pf.param == binvar] = nbins


        self.pardict = {}
        idx = 0
        for par,i in zip(pf.param,xrange(len(pf.param))):
            self.pardict[par] = {'guess':pf.guess[i],'prior_mean':pf.prior[i],
                                 'prior_std':pf.sigma[i],'fixed':pf.fixed[i],
                                 'use':pf.use[i],'addcosmo':pf.addcosmo[i],
                                 'mcstep':self.options.mcrandstep,
                                 'bins':pf.bins[i],'zpoly':pf.zpoly[i],
                                 'bounds':(pf.lbound[i],pf.ubound[i])}
            if not pf.use[i]: self.pardict[par]['idx'] = -1
            else:
                self.pardict[par]['idx'] = idx
                if pf.addcosmo[i]:
                    if pf.bins[i] == 1:
                        self.pardict[par]['guess'] = pf.guess[i] + cosmo.distmod(zcontrol).value
                        self.pardict[par]['prior_mean'] = pf.prior[i] + cosmo.distmod(zcontrol).value
                        self.pardict[par]['idx'] = idx + np.arange(len(zcontrol))
                        self.pardict[par]['bounds'] = (pf.lbound[i] + cosmo.distmod(zcontrol).value,
                                                       pf.ubound[i] + cosmo.distmod(zcontrol).value)
                        if pf.use[i]: idx += len(zcontrol)
                    else:
                        zcontrolCC = np.logspace(np.log10(min(zcontrol)),np.log10(max(zcontrol)),pf.bins[i])
                        self.pardict[par]['guess'] = pf.guess[i] + cosmo.distmod(zcontrolCC).value
                        self.pardict[par]['prior_mean'] = pf.prior[i] + cosmo.distmod(zcontrolCC).value
                        self.pardict[par]['idx'] = idx + np.arange(len(zcontrolCC))
                        self.pardict[par]['bounds'] = (pf.lbound[i] + cosmo.distmod(zcontrol).value,
                                                       pf.ubound[i] + cosmo.distmod(zcontrol).value)

                        if pf.use[i]: idx += len(zcontrolCC)
                elif pf.bins[i]:
                    if pf.bins[i] == 1:
                        self.pardict[par]['guess'] = np.zeros(len(zcontrol)) + pf.guess[i]
                        self.pardict[par]['prior_mean'] = np.zeros(len(zcontrol)) + pf.prior[i]
                        self.pardict[par]['idx'] = idx + np.arange(len(zcontrol))
                        if pf.use[i]: idx += len(zcontrol)
                    else:
                        zcontrolCC = np.logspace(np.log10(min(zcontrol)),np.log10(max(zcontrol)),pf.bins[i])
                        self.pardict[par]['guess'] = np.zeros(len(zcontrolCC)) + pf.guess[i]
                        self.pardict[par]['prior_mean'] = np.zeros(len(zcontrolCC)) + pf.prior[i]
                        self.pardict[par]['idx'] = idx + np.arange(len(zcontrolCC))
                        if pf.use[i]: idx += len(zcontrolCC)
                elif pf.zpoly[i]:
                    self.pardict[par]['guess'] = np.append(pf.guess[i],np.array([0.]*int(pf.zpoly[i])))
                    self.pardict[par]['prior_mean'] = np.append(pf.prior[i],np.array([0.]*int(pf.zpoly[i])))
                    self.pardict[par]['idx'] = idx + np.arange(int(pf.zpoly[i])+1)
                    if pf.use[i]: idx += int(pf.zpoly[i])+1
                elif pf.use[i]: idx += 1

                if pf.fixed[i]:
                    self.pardict[par]['prior_std'] = 1e-5
                    self.pardict[par]['mcstep'] = 0
                    self.pardict[par]['bounds'] = (self.pardict[par]['prior_mean']-1e-5,self.pardict[par]['prior_mean']+1e-5)

        guess = ()
        for k in pf.param:
            if not pf.use[pf.param == k]: continue
            if hasattr(self.pardict[k]['guess'],"__len__"):
                for g in self.pardict[k]['guess']:
                    guess += (g,)
            else:
                guess += (self.pardict[k]['guess'],)

        return(self.pardict,guess)
Esempio n. 4
0
    def main(self,inputfile):
        from txtobj import txtobj
        import os


        inp = txtobj(inputfile)
        
        if not len(inp.PA):
            import exceptions
            raise exceptions.RuntimeError('Warning : no data in input file!!')            

        # open the output file
        if os.path.exists(self.options.outputfile) and not self.options.clobber:
            print('Warning : files %s exists!!  Not clobbering'%self.options.outputfile)

        # run the MCMC
        zcontrol = np.logspace(np.log10(self.options.zrange[0]),np.log10(self.options.zrange[1]),num=self.options.nzbins)

        pardict,guess = self.mkParamDict(zcontrol)

        cov,samples = self.mcmc(inp,zcontrol,guess)

        root,ext = os.path.splitext(self.options.outputfile)
        fout = open('%s.covmat'%root,'w')
        print >> fout, '%i'%len(cov)
        shape = np.shape(cov)[0]
        for i in range(shape):
            outline = ''
            for j in range(shape):
                outline += '%8.5e '%cov[j,i]
                if i != j:
                    print >> fout, '%8.5e'%cov[j,i]#outline
                else:
                    print >> fout, '%8.5e'%0 #outline
        fout.close()

        coverr = lambda samp: np.sqrt(np.sum((samp-np.mean(samp))*(samp-np.mean(samp)))/len(samp))

        outlinevars = ['popAmean','popAstd','popBmean','popBstd','popB2mean','popB2std','skewB',
                       'scaleA','shift','lstep','salt2alpha','salt2beta','salt2beta_1']
        outlinefmt = " ".join(["%.4f"]*(1+len(outlinevars)*2))
        fout = open(self.options.outputfile,'w')
        headerline = "# zCMB "
        for o in outlinevars: headerline += "%s %s_err "%(o,o)
        print >> fout, headerline[:-1]
        if self.options.mcrandseed: print >> fout, '# MC Sample used random seed: %i'%self.options.mcrandseed
        fout.close()
        if self.options.verbose:
            print("zCMB " + " ".join(outlinevars))

        for z,i in zip(zcontrol,range(len(zcontrol))):
            outvars = (z,)
            for v in outlinevars:
                if self.pardict[v]["use"]:
                    idx = self.pardict[v]["idx"]
                    if self.pardict[v]['zpoly'] or (i >= self.pardict[v]['bins'] and self.pardict[v]['bins'] and self.pardict[v]['bins'] != 1): 
                        idx = self.pardict[v]["idx"][0]
                    if hasattr(idx,"__len__"):
                        mean,err = np.mean(samples[:,idx[i]]),np.std(samples[:,idx[i]])
                    else:
                        mean,err = np.mean(samples[:,idx]),np.std(samples[:,idx])
                    outvars += (mean,err,)
                else: outvars += (-99,-99,)
            fout = open(self.options.outputfile,'a')
            print >> fout, outlinefmt%outvars
            fout.close()
                
            if self.options.verbose:
                print(outlinefmt%outvars)
Esempio n. 5
0
def fit(
        fileroot='/export/scratch0/ps1sn1/data/v10.0/GPC1v3/eventsv1/workspace/PSc560121/g/PSc560121.md01s043.g.ut090831e.1917665_14.sw',
        xpos=None, ypos=None, radius=10, pdf_pages=None, ra=None, dec=None, title='', returnstamps = False, maskfile=None, mysky=None,mysig=None):
    # xpos = xpos +1
    # ypos = ypos +1
    # from matplotlib.backends.backend_pdf import PdfPages
    #pdf_pages = PdfPages('daophot_resid.pdf')
    dofcmp = False
    good = False

    im = pyfits.getdata('%s.fits' % fileroot)
    mask = pyfits.getdata(maskfile)
    impsf = pyfits.getdata('%s.dao.psf.fits' % fileroot)
    fullpsf, hpsf = rdpsf.rdpsf('%s.dao.psf.fits' % fileroot)
    imhdr = pyfits.getheader('%s.fits' % fileroot)

    if dofcmp:
        p = pyfits.open('%s.fcmp' % fileroot)
        p.verify("fix")


        if os.path.exists('test.fcmp'):
            os.remove('test.fcmp')
        p.writeto('test.fcmp', output_verify='fix')
        # fcmp = p[0].header
        # print p[1]

        fcmp = txtobj('test.fcmp', cmpheader=True)
        # print fcmp.__dict__['class']
        # print fcmp['class']
        # raw_input()


        w = wcs.WCS('%s.fits' % fileroot)
        #results2 = w.wcs_world2pix(np.array([[ra, dec]]), 0)
        # xpos,ypos =results2[0][0], results2[0][1]


        psfsize = np.shape(impsf)[0]

        fcmp.Xpos = fcmp.Xpos[1:].astype(float)
        fcmp.Ypos = fcmp.Ypos[1:].astype(float)
        fcmp.__dict__['class'] = fcmp.__dict__['class'][1:].astype(float)

        fcmp.flux = fcmp.flux[1:].astype(float)
        fcmp.dflux = fcmp.dflux[1:].astype(float)
        # for x,y,flux,fluxerr in zip(fcmp.Xpos,fcmp.Ypos,
        #                            fcmp.flux,fcmp.dflux):


        # print fcmp.Xpos-xpos
        # print fcmp.Ypos-ypos
        # raw_input()
        #print fcmp.__dict__['class']
        #print fcmp.Xpos
        #print xpos
        #raw_input()
        ww = (abs(fcmp.Xpos - xpos) < 1.) & (abs(fcmp.Ypos - ypos) < 1.)
        thisclass = fcmp.__dict__['class'][ww]
        #print 'THIS CLASS IS', thisclass
        #print 'all classes', fcmp.__dict__['class']
        # flux = fcmp.flux
        # fluxerr = fcmp.dflux

        if len(thisclass) == 1:
            if thisclass[0] == 1:
                good = True

    fluxerr = 100.
    chisq = 1.
    dms = 1.

    x = xpos
    y = ypos

    ny, nx = np.shape(im)
    psfy, psfx = np.shape(impsf)
    ixlo, iylo = int(x - radius), int(y - radius)
    if ixlo < 0: ixlo = 0
    if iylo < 0: iylo = 0
    ixhi = int(x + radius) + 1
    iyhi = int(y + radius) + 1
    if ixhi > (nx - 1): ixhi = nx - 1
    if iyhi > (ny - 1): iyhi = ny - 1
    ixx = ixhi - ixlo + 1
    iyy = iyhi - iylo + 1
    dx = np.arange(ixx) + ixlo - x
    dy = np.arange(iyy) + iylo - y
    psf1d = impsf.reshape(np.shape(impsf)[0] ** 2.)
    gauss = [hpsf['GAUSS1'], hpsf['GAUSS2'], hpsf['GAUSS3'],
             hpsf['GAUSS4'], hpsf['GAUSS5']]
    dx = dx.reshape(1, len(dx))
    dy = dy.reshape(len(dy), 1)
    dx = rebin.rebin(dx, [np.shape(dx)[1], np.shape(dx)[1]])
    dy = rebin.rebin(dy, [len(dy), len(dy)])
    try:
        model = dao_value.dao_value(dx, dy, gauss,
                                    impsf,  # psf1d=psf1d,
                                    deriv=False)  # ,ps1d=False)
    except:
        return 1, 1, 0, 0, False, 0, 0, 0


    subim = im[iylo - 1:iyhi, ixlo - 1:ixhi]
    #print 'modelshape', model.shape, 'imshape', subim.shape
    #raw_input()
    submask = mask[iylo - 1:iyhi, ixlo - 1:ixhi]
    submask[submask != 0] = 9
    submask[submask == 0 ] = 1
    submask[submask == 9 ] = 0
    # scaledpsf = model+impsf[psfy/2+1-radius:psfy/2+1+radius+1,
    #                        psfx/2+1-radius:psfx/2+1+radius+1]
    # print model.shape
    # print flux.shape
    # print hpsf['PSFMAG']
    # print imhdr['SKYADU']
    chisqvec = []
    fluxvec = []
    substamp = model.shape[0]
    fitrad = np.zeros([substamp, substamp])
    radius = 4
    for x in np.arange(substamp):
        for y in np.arange(substamp):
            if np.sqrt((substamp / 2. - x) ** 2 + (substamp / 2. - y) ** 2) < radius:
                fitrad[int(x), int(y)] = 1.
    '''
    for flux in range(1,500000,200):
        scaledpsf = model*flux/10**(-0.4*(hpsf['PSFMAG']-25)) + imhdr['SKYADU']
        chisq = np.sum(fitrad*(subim-scaledpsf)**2/imhdr['SKYSIG']**2)
        chisqvec.append(chisq)
        fluxvec.append(flux)

    chisqvec = np.array(chisqvec)
    fluxvec = np.array(fluxvec)
    flux = fluxvec[np.argmin(chisqvec)]
    scaledpsf = model*flux/10**(-0.4*(hpsf['PSFMAG']-25)) + imhdr['SKYADU']

    #resid(param,psf,im,sigma,fitrad,sky,psfmag)

    #print model, subim, imhdr['SKYSIG']
    '''
    # fluxls, cov = opti.leastsq(resid, 100000,
    #                            args=(model, subim, imhdr['SKYSIG'], fitrad, imhdr['SKYADU'], hpsf['PSFMAG']),full_output=False)

    fluxls, cov = opti.leastsq(resid, 100000,args=(model, subim, mysig, fitrad, mysky, hpsf['PSFMAG']),full_output=False)

    #print cov.shape
    #print fluxls, cov
    #raw_input('covshape')
    # print 'flux fit comparo',flux,fluxls,
    scaledpsf = model*fluxls/10**(-0.4*(hpsf['PSFMAG']-25)) + imhdr['SKYADU']



    dontplot = False
    if not pdf_pages is None:
        if not dontplot:
            print 'plottingggg'
            fig = plt.figure()
            plt.clf()
            axim = plt.subplot(131)
            axpsf = plt.subplot(132)
            axdiff = plt.subplot(133)
            for ax,title in zip([axim,axpsf,axdiff],['image','model','difference']):
                ax.set_title(title)
                axim.imshow(subim,
                cmap='gray',interpolation='nearest')
                axpsf.imshow(model,cmap='gray',interpolation='nearest')
                axdiff.imshow(subim-scaledpsf,cmap='gray',interpolation='nearest')
                #plt.colorbar()
                axim = plt.subplot(131)
                axpsf = plt.subplot(132)
                axdiff = plt.subplot(133)
            #for ax,title in zip([axim,axpsf,axdiff],['image','model','difference']):
                if good:
                    ax.set_title(title + 'GOOD')
                else:
                    ax.set_title(title + 'BADD')
                #ax.set_title(title)
            axim.imshow(subim,cmap='gray',interpolation='nearest')
            axpsf.imshow(scaledpsf,cmap='gray',interpolation='nearest')
            ax = axdiff.imshow(subim-scaledpsf,cmap='gray',interpolation='nearest')
            cbar = fig.colorbar(ax)
            #plt.imshow((subim-scaledpsf)/imhdr['SKYSIG'],cmap='gray',interpolation='nearest')
            #plt.colorbar()
            if good:
                plt.title(title + 'GOOD' )
            else:
                plt.title(title + 'BADD' )
            pdf_pages.savefig(fig)

            #pdf_pages.close()
    #plt.savefig('')




    if returnstamps:
        rpsf = model
        good = True#we know this wont be in the starcat file so set to good is true
        #print 'fluxls', fluxls
        #print 'maxpsf', np.max(rpsf)
        return fluxls,fluxerr,chisq,dms,good,subim, rpsf, imhdr['SKYSIG'], fitrad, imhdr['SKYADU'], hpsf['PSFMAG'], submask
    #print fluxls
    #print np.max(model)
    #raw_input('fluxls')
    sstamp = simstamp(fluxls,model, subim, imhdr['SKYSIG'], fitrad, imhdr['SKYADU'], hpsf['PSFMAG'])

    return fluxls, fluxerr, chisq, dms, good, subim, sstamp, model
Esempio n. 6
0
    def mcsamp(self,fitresfile,mciter,lowzfile,nsne):
        import os
        from txtobj import txtobj
        import numpy as np

        fitresheader = """# VERSION: PS1_PS1MD
# FITOPT:  NONE
# ---------------------------------------- 
NVAR: 31 
VARNAMES:  CID IDSURVEY TYPE FIELD zHD zHDERR HOST_LOGMASS HOST_LOGMASS_ERR SNRMAX1 SNRMAX2 SNRMAX3 PKMJD PKMJDERR x1 x1ERR c cERR mB mBERR x0 x0ERR COV_x1_c COV_x1_x0 COV_c_x0 NDOF FITCHI2 FITPROB PBAYES_Ia PGAL_Ia PFITPROB_Ia PNN_Ia PTRUE_Ia PHALF_Ia SIM_TYPE_INDEX SIM_ZCMB
# VERSION_SNANA      = v10_39i 
# VERSION_PHOTOMETRY = PS1_PS1MD 
# TABLE NAME: FITRES 
# 
"""
        fitresvars = ["CID","IDSURVEY","TYPE","FIELD",
                      "zHD","zHDERR","HOST_LOGMASS",
                      "HOST_LOGMASS_ERR","SNRMAX1","SNRMAX2",
                      "SNRMAX3","PKMJD","PKMJDERR","x1","x1ERR",
                      "c","cERR","mB","mBERR","x0","x0ERR","COV_x1_c",
                      "COV_x1_x0","COV_c_x0","NDOF","FITCHI2","FITPROB",
                      "PBAYES_Ia","PGAL_Ia","PFITPROB_Ia","PNN_Ia",
                      "PTRUE_Ia","PHALF_Ia","SIM_TYPE_INDEX","SIM_ZCMB"]
        fitresfmt = 'SN: %s %i %i %s %.5f %.5f %.4f %.4f %.4f %.4f %.4f %.3f %.3f %8.5e %8.5e %8.5e %8.5e %.4f %.4f %8.5e %8.5e %8.5e %8.5e %8.5e %i %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %i %.5f'

        name,ext = os.path.splitext(fitresfile)
        outname,outext = os.path.splitext(self.options.outputfile)
        fitresoutfile = '%s_%s_mc%i%s'%(name,outname.split('/')[-1],mciter,ext)

        fr = txtobj(fitresfile,fitresheader=True)
        if not fr.__dict__.has_key('PTRUE_Ia'): fr.PTRUE_Ia = np.array([-99]*len(fr.CID))
        if not fr.__dict__.has_key('SIM_TYPE_INDEX'): fr.SIM_TYPE_INDEX = np.array([-99]*len(fr.CID))
        if not fr.__dict__.has_key('SIM_ZCMB'): fr.SIM_ZCMB = np.array([-99]*len(fr.CID))
        if lowzfile:
            frlowz = txtobj(lowzfile,fitresheader=True)    
            if not frlowz.__dict__.has_key('PTRUE_Ia'): frlowz.PTRUE_Ia = np.array([-99]*len(fr.CID))
            if not frlowz.__dict__.has_key('SIM_TYPE_INDEX'): frlowz.SIM_TYPE_INDEX = np.array([-99]*len(fr.CID))
            if not frlowz.__dict__.has_key('SIM_ZCMB'): frlowz.SIM_ZCMB = np.array([-99]*len(fr.CID))

        # Light curve cuts
        if self.options.x1cellipse:
            # I'm just going to assume cmax = abs(cmin) and same for x1
            cols = np.where((fr.x1**2./self.options.x1range[0]**2. + fr.c**2./self.options.crange[0]**2. < 1) &
                            (fr.x1ERR < self.options.x1errmax) & (fr.PKMJDERR < self.options.pkmjderrmax*(1+fr.zHD)) &
                            (fr.FITPROB >= self.options.fitprobmin) &
                            (fr.zHD > self.options.zrange[0]) & (fr.zHD < self.options.zrange[1]) &
                            (fr.__dict__[self.options.piacol] >= 0))
        else:
            cols = np.where((fr.x1 > self.options.x1range[0]) & (fr.x1 < self.options.x1range[1]) &
                            (fr.c > self.options.crange[0]) & (fr.c < self.options.crange[1]) &
                            (fr.x1ERR < self.options.x1errmax) & (fr.PKMJDERR < self.options.pkmjderrmax*(1+fr.zHD)) &
                            (fr.FITPROB >= self.options.fitprobmin) &
                            (fr.zHD > self.options.zrange[0]) & (fr.zHD < self.options.zrange[1]) &
                            (fr.__dict__[self.options.piacol] >= 0))
        for k in fr.__dict__.keys():
            fr.__dict__[k] = fr.__dict__[k][cols]


        import random
        if self.options.mcrandseed: random.seed(self.options.mcrandseed)
        try:
            cols = random.sample(range(len(fr.CID)),nsne)
            writefitres(fr,cols,
                        fitresoutfile,fitresheader=fitresheader,
                        fitresvars=fitresvars,fitresfmt=fitresfmt)
        except ValueError:
            print('Warning : crashed because not enough SNe!  Making only a low-z file...')
            if lowzfile:
                writefitres(frlowz,random.sample(range(len(frlowz.CID)),self.options.nmclowz),
                            fitresoutfile,append=False,fitresheader=fitresheader,
                            fitresvars=fitresvars,fitresfmt=fitresfmt)
            return(fitresoutfile)
        if lowzfile:
            try:
                writefitres(frlowz,random.sample(range(len(frlowz.CID)),self.options.nmclowz),
                            fitresoutfile,append=True,fitresheader=fitresheader,
                            fitresvars=fitresvars,fitresfmt=fitresfmt)
            except:
                frlowz.PHALF_Ia = np.ones(len(frlowz.CID))
                writefitres(frlowz,range(len(frlowz.CID)),
                            fitresoutfile,append=True,fitresheader=fitresheader,
                            fitresvars=fitresvars,fitresfmt=fitresfmt)


        return(fitresoutfile)
Esempio n. 7
0
    def main(self,fitres,mkcuts=True):
        from txtobj import txtobj
        from astropy.cosmology import Planck13 as cosmo

        fr = txtobj(fitres,fitresheader=True)
        if self.options.zrange[0] < np.min(fr.zHD): self.options.zrange[0] = np.min(fr.zHD)
        if self.options.zrange[1] > np.max(fr.zHD): self.options.zrange[1] = np.max(fr.zHD)
        
        from dobeams import salt2mu
        fr.MU,fr.MUERR = salt2mu(x1=fr.x1,x1err=fr.x1ERR,c=fr.c,cerr=fr.cERR,mb=fr.mB,mberr=fr.mBERR,
                                 cov_x1_c=fr.COV_x1_c,cov_x1_x0=fr.COV_x1_x0,cov_c_x0=fr.COV_c_x0,
                                 alpha=self.options.salt2alpha,
                                 beta=self.options.salt2beta,
                                 x0=fr.x0,sigint=self.options.sigint,z=fr.zHD)

        fr = self.mkfitrescuts(fr,mkcuts=mkcuts)

        root = os.path.splitext(fitres)[0]
        
        # Prior SN Ia probabilities
        P_Ia = np.zeros(len(fr.CID))
        for i in range(len(fr.CID)):
            P_Ia[i] = fr.__dict__[self.options.piacol][i]
            if self.options.specconfcol:
                if fr.__dict__[self.options.specconfcol][i] == 1:
                    P_Ia[i] = 1

        from dobeams import BEAMS
        import ConfigParser, sys
        sys.argv = ['./doBEAMS.py']
        beam = BEAMS()
        parser = beam.add_options()
        options,  args = parser.parse_args(args=None,values=None)
        options.paramfile = self.options.paramfile

        if options.paramfile:
            config = ConfigParser.ConfigParser()
            config.read(options.paramfile)
        else: config=None
        parser = beam.add_options(config=config)
        options,  args = parser.parse_args()

        beam.options = options
        # clumsy - send some options to the code
        beam.options.twogauss = self.options.twogauss
        beam.options.skewedgauss = self.options.skewedgauss
        beam.options.nthreads = self.options.nthreads
        beam.options.nwalkers = self.options.nwalkers
        beam.options.nsteps = self.options.nsteps
        beam.options.mcmcparamfile = self.options.mcmcparamfile
        beam.options.fix = self.options.fix
        beam.options.bounds = self.options.bounds
        beam.options.guess = self.options.guess
        beam.options.prior = self.options.prior
        beam.options.bins = self.options.bins
        beam.options.use = self.options.use
        beam.options.minmethod = self.options.minmethod
        beam.options.forceminsuccess = self.options.forceminsuccess
        beam.options.miniter = self.options.miniter
        beam.options.ninit = self.options.ninit
        beam.options.ntemps = self.options.ntemps
        beam.options.debug = self.options.debug
        beam.options.mcrandseed = self.options.mcrandseed

        options.inputfile = '%s.input'%root
        if self.options.masscorr:
            beam.options.plcol = 'PL'
            import scipy.stats
            cols = np.where(fr.HOST_LOGMASS > 0)
            for k in fr.__dict__.keys():
                fr.__dict__[k] = fr.__dict__[k][cols]
            fr.PL = np.zeros(len(fr.CID))
            for i in range(len(fr.CID)):
                if fr.HOST_LOGMASS_ERR[i] == 0: fr.HOST_LOGMASS_ERR[i] = 1e-5
                fr.PL[i] = scipy.stats.norm.cdf(10,fr.HOST_LOGMASS[i],fr.HOST_LOGMASS_ERR[i])
            P_Ia = P_Ia[cols]            

        if self.options.masscorrfixed: beam.options.lstepfixed = True

        beam.options.zmin = self.options.zrange[0]
        beam.options.zmax = self.options.zrange[1]
        beam.options.nzbins = self.options.nzbins

        # make the BEAMS input file
        fout = open('%s.input'%root,'w')
        fr.PA = fr.__dict__[self.options.piacol]
        if not self.options.masscorr: fr.PL = np.zeros(len(fr.PA))
        writefitres(fr,range(len(fr.PA)),'%s.input'%root,
                    fitresheader=fitresheaderbeams,
                    fitresfmt=fitresfmtbeams,
                    fitresvars=fitresvarsbeams)

        beam.options.append = False
        beam.options.clobber = self.options.clobber
        beam.options.outputfile = self.options.outputfile
        beam.main(options.inputfile)
        bms = txtobj(self.options.outputfile)
        name,ext = os.path.splitext(self.options.outputfile)
        self.writeBinCorrFitres('%s.fitres'%name,bms,fr=fr)
        return