Example #1
0
    def get_aperture(self, r, mass, masserr):
        """
        get r,mass,masserr and the mass from background
        within our aperture (rmin,rmax)
        """


        w=where1((r >= self.rmin) & (r <= self.rmax))

        rap=r[w]
        massap=mass[w]
        massaperr=masserr[w]

        # mass from background (standard rho)
        msap = self.mstd(r[w])

        wl = where1(msap < massap)
        wg = where1(msap >= massap)
        if wl.size == w.size or wg.size == w.size:
            raise ValueError("lines do not cross in "
                             "aperture: %s,%s" % (self.rmin,self.rmax))

        if rap[wl].max() > rap[wg].min():
            raise ValueError("mass is not monatonic")

        return rap, massap, massaperr, msap
Example #2
0
File: sg.py Project: esheldon/espy
def read_test_data():
    import esutil as eu
    from . import select
    gal=eu.io.read('~/data/boss/calibObj-000756-3-gal.fits',lower=True)
    star=eu.io.read('~/data/boss/calibObj-000756-3-star.fits',lower=True)

    gflags = get_select_logic(gal,4.0)
    sflags = get_select_logic(star,4.0)

    gw = where1(gflags)
    sw = where1(sflags)

    ntot = gw.size + sw.size
    dt=[('origtype','i4'),
        ('modelflux','f4',5),
        ('modelflux_ivar','f4',5),
        ('psfflux','f4',5),
        ('psfflux_ivar','f4',5)]

    data = numpy.zeros(ntot, dtype=dt)
    data['origtype'][0:gw.size] = 3
    data['origtype'][gw.size:] = 6
    for n in ['modelflux','modelflux_ivar','psfflux','psfflux_ivar']:
        data[n][0:gw.size] = gal[n][gw]
        data[n][gw.size:] = star[n][sw]

    return data
Example #3
0
def add_to_log_plot(plt, x, y, yerr, 
                    color='black', type='filled circle',
                    minval=1.e-5):

    w = where1(y > minval)
    if w.size > 0:
        p = Points(x[w], y[w],type=type,color=color)
        plt.add(p)
    else:
        p=None


    ehigh = y + yerr
    elow  = y - yerr

    
    # only show errors where y+yerr is greater than the min value.
    w=where1(ehigh > minval)
    if w.size > 0:
        elow = where(elow < minval, minval, elow)
        pe = ErrY(x[w], elow[w], ehigh[w],color=color)
        plt.add(pe)
    else:
        pe=None

    odict={}
    odict['p'] = p
    odict['pe'] = p
    #odict['xrange'] = [0.3*x.min(), 2.25*x.max()]
    #odict['yrange'] = [0.3*elow.min(), 2.25*ehigh.max()]
    odict['xrange'] = eu.plotting.get_log_plot_range(x)
    odict['yrange'] = eu.plotting.get_log_plot_range(y,yerr)
    return odict
Example #4
0
    def find_mvir(self, r, mass, masserr):
        """
        Look where the mass curve crosses the mass from the 
        background times delrho

        r in Mpc, mass in Msun

        """
        rap, massap, massaperr, msap = self.get_aperture(r,mass,masserr)

        wl=where1(msap <  massap)
        wg=where1(msap >= massap)

        i1 = rap[wl].argmax()
        i2 = rap[wg].argmin()
        i1 = wl[i1]
        i2 = wg[i2]
        r1 = rap[i1]
        r2 = rap[i2]

        if i2-i1 != 1:
            raise ValueError("indices i1,i2 are not consecutive")

        
        al=(log(massap[i2])-log(massap[i1]))/(log(rap[i2])-log(rap[i1]))
        A=massap[i1]/(rap[i1]**al)

        B=self.mfactor()
        rvir=(A/B)**(1./(3.-al))

        mvir=B*rvir**3
        mvir_err=(massaperr[i1]+massaperr[i2])/2.0
        rvir_err=rvir*(mvir_err/mvir)/3.0

        return rvir, rvir_err, mvir, mvir_err
Example #5
0
    def write_columns(self):

        d = files.input_coldir(self.name, self.version)
        if os.path.exists(d):
            raise ValueError("coldir exists, start fresh: '%s'" % d)
        outcols = Columns(d)
        outcols.create()

        if self.logic is None:
            self.select()

        w=where1(self.mag_and_mask_logic)
        print("\nkeeping those that pass mag and mask logic: %s/%s" % (w.size, self.logic.size))


        colnames = ['photoid',
                    'ra',
                    'dec',
                    'objc_flags',
                    'objc_flags2',
                    'ingood',
                    'instar',
                    'inbadfield']

        for col in colnames:
            print('Creating:',col)
            data = self.cols[col][:]
            data = data[w]
            outcols.write_column(col, data)

            del data


        combcols = ['flags','flags2',
                    'modelflux','modelflux_ivar',
                    'cmodelflux','cmodelflux_ivar',
                    'extinction']

        for ccol in combcols:
            print('Creating:',ccol)
            colnames = [ccol+'_'+f for f in ['u','g','r','i','z'] ]
            data = self.cols.read_columns(colnames, rows=w, verbose=True)

            dts = data[ccol+'_'+f].dtype.descr[0][1]
            dt = [(ccol, dts, 5)]
            data = data.view(dt)

            # don't want it to show up as a .rec
            rawdata = data[ccol]
            outcols.write_column(ccol, rawdata)

            del data


        print('Adding more restrictive flags to "keep"')
        keep = zeros(w.size, dtype='u1')
        wrest = where1( self.logic[w] )
        keep[wrest] = 1

        outcols.write_column('keep', keep)
Example #6
0
def process_image_cat(imfile, catfile, get_meta=False):
    """
    Process the input image and cat files.

    If get_meta=True, a tuple of (array, meta) is returned.  meta is a
    dictionary with 'shiftmax' and 'nsub'
    """
    im, ivar_im, cat = read_image_cat(imfile, catfile)

    col=cat['x_image']-1
    row=cat['y_image']-1
    sky=cat['background']

    colint=col.astype('i4')
    rowint=row.astype('i4')
    ivar = ivar_im[rowint,colint] 
    sigsky = zeros(cat.size) 
    w=where1(ivar > 0)
    wbad=where1(ivar <= 0)

    if w.size > 0:
        sigsky[w] = 1.0/sqrt(ivar[w])

        print >>stderr,'running admom'
        tmp=admom.admom(im, row[w], col[w], sky=sky[w], sigsky=sigsky[w])
        print >>stderr,'copying to array output'
        a,h=as_array(cat, w, wbad, tmp)
        if get_meta:
            out=(a,h)
        else:
            out=a
    else:
        out=None

    return out
Example #7
0
    def check_bounds(self, ra, dec):
        """
        Select all points within the bounds of the sample specified
        in "type"
        """

        logic = numpy.zeros(ra.size, dtype=numpy.bool)

        for type in sorted(bounds):
            stdout.write("Checking '%s'\n" % type)
            b = bounds[type]

            temp_logic = ( (ra >= b['ra'][0])   & (ra <= b['ra'][1]) 
                          & (dec >= b['dec'][0]) & (dec <= b['dec'][1]) )

            # or the logics together
            logic = logic | temp_logic
            w=where1(temp_logic)
            stdout.write("    Found %s\n" % w.size)

            del w

        wtot = where1(logic)
        stdout.write("Kept a total of %s\n" % wtot.size)
        return wtot
    def rotate_multi(self,
                     runs,
                     camcols,
                     fields,
                     filter,
                     e1pix,
                     e2pix,
                     getrot=False,
                     verbose=False):

        e1 = numpy.zeros(runs.size, dtype='f8')
        e2 = e1.copy()
        if getrot:
            angles = e1.copy()

        urun = unique(runs)
        for run in urun:
            if verbose:
                print("  run:", run)
            wrun = where1(runs == run)
            ucamcols = unique(camcols[wrun])
            for camcol in ucamcols:
                if verbose:
                    print("    camcol:", camcol)
                wc = where1(camcols[wrun] == camcol)
                wc = wrun[wc]
                h, rev = eu.stat.histogram(fields[wc], binsize=1, rev=True)
                for i in xrange(h.size):
                    if rev[i] != rev[i + 1]:

                        wf = rev[rev[i]:rev[i + 1]]
                        wf = wc[wf]

                        #uf = unique(fields[wf])
                        #if uf.size != 1:
                        #    raise ValueError("expected unique field")

                        field = fields[wf[0]]
                        #print("      field:",field)

                        res = self.rotate(run,
                                          camcol,
                                          field,
                                          filter,
                                          e1pix[wf],
                                          e2pix[wf],
                                          getrot=getrot,
                                          verbose=verbose)
                        e1[wf] = res[0]
                        e2[wf] = res[1]
                        if getrot:
                            angles[wf] = res[2]

        if getrot:
            return e1, e2, angles
        else:
            return e1, e2
Example #9
0
    def create_objshear_input(self, nrand=None, extra=None):
        """
        To work in chunks, send nrand= and extra=chunknum
        """
        from es_sdsspy.stomp_maps import get_quad_logic

        if nrand is not None:
            if extra is None:
                raise ValueError("If sending nrand, also send extra=")
        else:
            nrand = self['nrand']

        self.load_stomp_maps()

        strict_edgecut = self.get('strict_edgecut',False)

        print("Generating",nrand,"random points "
              "with z in [%0.2f,%0.2f]" % (self['zmin'],self['zmax']))
        n=0

        dt = lensing.files.lcat_dtype()
        output = numpy.zeros(nrand, dtype=dt)
        while n < nrand:
            if n == 0:
                print("  generating",nrand-n," ",end='')
            else:
                print("  re-generating",nrand-n," ",end='')
            print("z",end='')
            z = self.zgen.genrand(nrand-n)
            print(" -> ra,dec ",end='')
            ra,dec = self.tycho_map.GenerateRandomEq(nrand-n)

            if 'rmax_hard' in self:
                print(" -> maskflags_hard (%0.1f)" % self['rmax_hard'], end='')
                maskflags_hard = self.get_maskflags(ra,dec,z,hard=True)
                hard_edge_logic = get_quad_logic(maskflags_hard, strict=True)

            print(" -> maskflags (%0.1f)" % self['rmax'], end='')
            maskflags = self.get_maskflags(ra,dec,z)
            quad_logic      = get_quad_logic(maskflags,strict=strict_edgecut)

            if 'rmax_hard' in self:
                wgood = where1(quad_logic & hard_edge_logic)
            else:
                wgood = where1(quad_logic)

            print(" -> good ones:",wgood.size)
            if wgood.size > 0:
                output['zindex'][n:n+wgood.size] = \
                        numpy.arange(n,n+wgood.size,dtype='i4')
                output['ra'][n:n+wgood.size] = ra[wgood]
                output['dec'][n:n+wgood.size] = dec[wgood]
                output['z'][n:n+wgood.size] = z[wgood]
                output['maskflags'][n:n+wgood.size] = maskflags[wgood]
                n += wgood.size

        lensing.files.lcat_write(sample=self['sample'], data=output, extra=extra)
Example #10
0
File: sg.py Project: esheldon/espy
    def select_goods_gal(self,d):

        wf=where1(d['flags'] == 0)

        plt=eu.plotting.bscatter(d['mag_best'][wf], d['class_star'][wf], 
                                 xrange=[15,30], yrange=[-0.05,1.05], type='dot',
                                 xlabel='mag_best',ylabel='class_star', show=False)

        w=where1( (d['flags'] == 0) & (d['mag_best'] < 25) & (d['class_star'] < 0.8))
        eu.plotting.bscatter(d['mag_best'][w], d['class_star'][w], type='filled circle', color='red',
                             plt=plt)
Example #11
0
File: sg.py Project: esheldon/espy
    def gather_epochs(self, data, epochs_index_current):
        photoid = sdsspy.photoid(data)

        fh, frev=eu.stat.histogram(data['field'], binsize=1, rev=True)

        epochlist = []
        epochs_indices = numpy.zeros(data.size)
        epochs_count = numpy.zeros(data.size)

        epochs_index = epochs_index_current
        for i in xrange(fh.size):
            if frev[i] != frev[i+1]:
                wfield = frev[ frev[i]:frev[i+1] ]
                field = data['field'][wfield[0]]

                epochs = sdsspy.read('photoEpochs', 
                                     data['run'][0], 
                                     data['camcol'][0], 
                                     field=field,
                                     verbose=False, 
                                     lower=True)

                # first extract those that were actually used according
                # to the criteria for selecting the primaries above
                w=where1(epochs['cmodel_used'][:,2] == 1)
                if w.size == 0:
                    raise ValueError("found none that were used!")

                epochs = epochs[w]
                    
                # extract those that match by photoid
                for j in xrange(wfield.size):
                    pid = photoid[wfield[j]]
                    w=where1(epochs['primary_photoid'] == pid)
                    if w.size == 0:
                        raise ValueError("no matches for pid",pid)

                    epochs_indices[wfield[j]] = epochs_index
                    epochs_count[wfield[j]] = w.size
                    epochs_index += w.size

                    epochlist.append(epochs[w])

        if len(epochlist) == 0:
            raise ValueError("Expected to find some epochs!")

        epochs = eu.numpy_util.combine_arrlist(epochlist)

        if (epochs_index-epochs_index_current) != epochs.size:
            raise ValueError("epoch index not incremented right amount:"
                             "%i vs %i" % (epochs_index-epochs_index_current,epochs.size))

        return epochs, epochs_indices, epochs_count
Example #12
0
    def create_objshear_input(self, **keys):
        from es_sdsspy.stomp_maps import get_quad_logic

        lens_split=keys.get('lens_split',None)
        if lens_split is None:
            raise ValueError("send lens_split=")

        nsplit=self['nsplit']
        print("doing lens_split %s: %s/%s" % (lens_split,lens_split+1,nsplit))

        strict_edgecut = self['strict_edgecut']

        n=0

        data=self.read_original()
        ntot=data.size

        npersplit = data.size/nsplit

        start = lens_split*npersplit
        end   = (lens_split+1)*npersplit
        data = data[start:end]
        print('lens_split',lens_split)
        print('    keeping: %d/%d' % (data.size,ntot))

        # trim z for speed was not implemented in rmrand01
        z_logic = self.get_z_logic(data['z'])
        w=where1(z_logic)
        data=data[w]

        maskflags = self.get_maskflags(data['ra'],data['dec'],data['z'])

        quad_logic = get_quad_logic(maskflags, strict=strict_edgecut)

        wgood = where1(quad_logic)

        print("    keeping: %d/%d" %(wgood.size,data.size))

        data      = data[wgood]
        maskflags = maskflags[wgood]

        dt = lensing.files.lcat_dtype()
        output = numpy.zeros(wgood.size, dtype=dt)
        output['zindex'][:]    = data['zindex']
        output['ra'][:]        = data['ra']
        output['dec'][:]       = data['dec']
        output['z'][:]         = data['z']
        output['maskflags'][:] = maskflags

        lensing.files.lcat_write(sample=self['sample'],
                                 data=output,
                                 lens_split=lens_split)
Example #13
0
def tselect(t):
    import sdsspy
    from esutil.numpy_util import where1

    plogic = (t['resolve_status'] & sdsspy.flagval('resolve_status','survey_primary')) != 0
    blended_logic = (t['objc_flags'] & sdsspy.flagval('object1','blended')) == 0
    nodeblend_logic = (t['objc_flags'] & sdsspy.flagval('object1','nodeblend')) != 0

    pw = where1( plogic )
    aw = where1( plogic & (blended_logic | nodeblend_logic) )

    print 'primary:',pw.size
    print 'primary + blend cuts:',aw.size
Example #14
0
def make_nasa_sdss_edge(stomp_map='boss', map_type='good', radmax=10.):
    """
    radmax in degrees
    """
    data=read_nasa_sdss()

    flagname='boss%s_maskflags' % map_type
    edgename='boss%s_edgeok' % map_type
    out = eu.numpy_util.add_fields(data, [(flagname,'i2',len(_radii)),
                                          (edgename,'i2',len(_radii))])
    out[flagname] = -1
    out[edgename] = 0

    m = es_sdsspy.stomp_maps.load(stomp_map,map_type)
    cosmo = cosmology.Cosmo()

    for i,r in enumerate(_radii):
        w=where1(out['z'] > 0.0)

        d = cosmo.Da(0.0, data['z'][w])
        srad = r/d*180.0/numpy.pi

        w2=where1(srad < radmax)
        print '    Found',w2.size,'with search rad <',radmax,'degrees'

        srad=srad[w2]
        w=w[w2]

        print '    search rad range from:',srad.min(),srad.max(),'degrees'
        print '        Checking edges'
        maskflags=m.Contains(out['ra'][w], out['dec'][w],"eq",srad)

        good=es_sdsspy.stomp_maps.quad_check(maskflags, strict=True)

        out[flagname][w,i] = maskflags[:]
        out[edgename][w[good],i] = 1

    rstr = ' '.join( [str(r) for r in _radii] )

    header=[{'name':'map',     'value':stomp_map,        'comment':'stomp map'},
            {'name':'map_type','value':map_type,         'comment':'stomp map type'},
            {'name':'H0',      'value':cosmo.H0(),       'comment':'H0 used for distance calculations'},
            {'name':'flat',    'value':cosmo.flat(),     'comment':'Was a flat universe used?'},
            {'name':'omega_m', 'value':cosmo.omega_m(),  'comment':'omega_m used for distance calculations'},
            {'name':'omega_l', 'value':cosmo.omega_l(),  'comment':'omega_l used for distance calculations'},
            {'name':'omega_k', 'value':cosmo.omega_k(),  'comment':'omega_k used for distance calculations'},
            {'name':'radii',   'value':rstr,             'comment':'radii used for edge checks'}]

    write_nasa_sdss_edge(out, header=header)
Example #15
0
    def create_objshear_input(self, **keys):
        from es_sdsspy.stomp_maps import get_quad_logic

        nsplit=self['nsplit']
        if nsplit != 1:
            raise ValueError("expected nsplit=1 for SDSSVoids")

        data = self.read_original()
        orig_size = data.size
        zindex = numpy.arange(orig_size,dtype='i4')

        zmin = self['zmin']
        zmax = self['zmax']

        good=where1(  (data['z'] > zmin) & (data['z'] < zmax) )
        print("  z cut: %s/%s: %s" % (data.size-good.size,
                                      orig_size,
                                      (data.size-good.size)/float(orig_size)) )
        if good.size == 0:
            stop

        print("Actually trimming the bad z for speed")
        data = data[good]
        zindex = zindex[good]

        #maskflags = self.get_maskflags(data['ra'][0:5], data['dec'][0:5], data['z'][0:5])
        maskflags = self.get_maskflags(data['ra'], data['dec'], data['z'])
        quad_logic = get_quad_logic(maskflags)

        good = where1(quad_logic)
        print("  quad mask cut: %s/%s: %s" % (data.size-good.size,
                                              orig_size,
                                              (data.size-good.size)/float(orig_size)) )

        if good.size == 0:
            stop

        print('creating output array')
        output = make_output_array(good.size)

        print('copying data')
        output['zindex']    = zindex[good]
        output['ra']        = data['ra'][good]
        output['dec']       = data['dec'][good]
        output['z']         = data['z'][good]
        output['maskflags'] = maskflags[good]
        lensing.files.lcat_write(sample=self['sample'], data=output, 
                                 lens_split=0)
Example #16
0
def match_columns(photoid, tags, type='primgal'):
    """

    Take the input photoids and match it to the input photo columns.  Extract
    the requested columns.

    """
    if isinstance(tags, str):
        tags=[tags]
    cols = open_columns(type)
    add_descr = []
    for tag in tags:
        if tag not in cols:
            raise ValueError("Requested tag not in database:",tag)

        print("will extract:",cols[tag].dtype.descr[0])
        add_descr.append(cols[tag].dtype.descr[0])

    print("reading photoid from columns")
    pid = cols['photoid'][:]
    
    print("matching")
    minput, mcols = eu.numpy_util.match(photoid, pid)

    if minput.size != photoid.size:
        raise ValueError("Not all objects matched: %d/%d" % (minput.size, photoid.size))

    print("verifying")
    tpid = cols['photoid'][mcols]
    wbad=where1(tpid != photoid[minput])
    if wbad.size != 0:
        raise ValueError("extracted photoid did not match up")

    struct = cols.read_columns(tags, rows=mcols)
    return struct
Example #17
0
    def create_objshear_input(self, **keys):

        data = self.read_original()

        # keep index into original data
        orig_size = data.size
        zindex = numpy.arange(orig_size,dtype='i4')

        # trim not well understood low ngals stuff
        ngals_logic = self.ngals_logic(data['ngals_r200'])

        # trim z for speed
        z_logic = self.get_z_logic(data['photoz_cts'])

        good = where1(ngals_logic & z_logic)
        print("Finally kept: %d/%d" % (good.size,data.size))

        print('creating output array')
        output = make_output_array(good.size)

        print('copying data')
        output['zindex']    = zindex[good]
        output['ra']        = data['ra'][good]
        output['dec']       = data['dec'][good]
        output['z']         = data['photoz_cts'][good]
        output['maskflags'] = self.get_maskflags(output['ra'],output['dec'],output['z'])
        lensing.files.lcat_write(sample=self['sample'], data=output)
Example #18
0
def select_good_me_bycol(c):
    flags   = c['shear_flags'][:]
    flagsin = c['input_flags'][:]
    flagswt = c['flags_weight'][:]
    w = where1((flags == 0) & (flagsin == 0) & (flagswt==0))

    return w
Example #19
0
    def j3(self, r, **keys):
        """
        This is proportional to the mass enclosed.  If you
        do a fit for B, then you should be able to just multiply
        B*rhocrit0*j3 to get the mass.  rhocrit0 is the rhocrit
        at redshift 0.

        See the m() for that very thing.

        You should use a small radius to get the mass right
        """
        xi = self.xi(r, **keys)

        w=where1(xi <= 0)
        if w.size > 0:
            raise ValueError("all xi must be > 0 for power law "
                             "interpolation")
        lr  = log(r)
        lxi = log(xi)

        al=( lxi-roll(lxi,1) )/( lr-roll(lr,1) )
        al[0]=al[1]

        A=xi/r**(al)

        ex=3.0+al
        Rb=r
        Ra=roll(r,1)
        Ra[0]=0.0
        int0=A*(1.0/ex) *(Rb**ex -Ra**ex)

        j3=4*PI*int0.cumsum()
        return j3
Example #20
0
 def load(self, reload=False):
     if not hasattr(RunList, 'data') or reload:
         rl = read('runList')
         # remove duplicate, bad entry
         w = where1((rl['run'] != 5194) | (rl['rerun'] == '301'))
         rl = rl[w]
         RunList.data = rl
Example #21
0
    def rotate(self, run, camcol, field, filter, e1pix, e2pix,
               getrot=False, verbose=False):
        if not numpy.isscalar(run):
            return self.rotate_multi(run,camcol,field,filter,e1pix,e2pix,
                                     getrot=getrot, verbose=verbose)

        self.load_run(run)

        if verbose:
            print("%06i-%i-%04i" % (run,camcol,field))

        w=where1((self.rotstruct['camcol'] == camcol) & (self.rotstruct['field'] == field) )
        if w.size != 1:
            print("expected single match for %06i-%i-%04i, got %i" % (run,camcol,field,w.size))
            fields = numpy.unique(self.rotstruct['field'])
            print("here are the fields:",fields)
            raise ValueError("stopping")

        filternum = sdsspy.FILTERNUM[filter]
        cos2angle = self.rotstruct['cos2angle'][w,filternum]
        sin2angle = self.rotstruct['sin2angle'][w,filternum]

        # hmm... this seems to be applying -2*angle rotation...
        e1 =  e1pix*cos2angle + e2pix*sin2angle
        e2 = -e1pix*sin2angle + e2pix*cos2angle

        if getrot:
            angle = self.rotstruct['angle'][w,filternum]
            angles = numpy.zeros(e1.size, dtype='f8') + angle[0]
            return e1, e2, angles
        else:
            return e1,e2
Example #22
0
 def readfield(self, type, run, camcol, field, **keys):    
     self.cache_column(type, run, camcol, **keys)
     data = self.data
     w=where1(data['field'] == field)
     if w.size == 0:
         raise ValueError("field not found: %06i-%i-%04i" % (run,camcol,field))
     return data[w]
Example #23
0
 def readobj(self, type, run, camcol, field, id, **keys):    
     self.cache_column(type, run, camcol, **keys)
     data = self.data
     w=where1( (data['field'] == field) & (data['id'] == id) )
     if w.size == 0:
         raise ValueError("object not found: %06i-%i-%04i-%05i" % (run,camcol,field,id))
     return data[w[0]]
Example #24
0
 def load(self, reload=False):
     if not hasattr(RunList, 'data') or reload:
         rl = read('runList')
         # remove duplicate, bad entry
         w=where1( (rl['run'] != 5194) | (rl['rerun'] == '301'))
         rl = rl[w]
         RunList.data = rl
Example #25
0
    def create_objshear_input(self, **keys):
        
        nsplit=self['nsplit']
        if nsplit != 1:
            raise ValueError("expected nsplit=1 for RedMapperDES")

        z_field = self['z_field']

        data = self.read_original()

        # keep index into original data
        orig_size = data.size
        zindex = numpy.arange(orig_size,dtype='i4')

        # trim z for speed
        z_logic = self.get_z_logic(data[z_field])

        good=where1(z_logic)

        # make sure in the tycho window and two adjacent quadrants
        # not hitting edge (or no edge if strict=True)

        print('creating output array')
        output = make_output_array(good.size)

        print('copying data')
        output['zindex']    = zindex[good]
        output['ra']        = data['ra'][good]
        output['dec']       = data['dec'][good]
        output['z']         = data[z_field][good]
        output['maskflags'] = 0 # not currently used
        lensing.files.lcat_write(sample=self['sample'], data=output,
                                 lens_split=0)
Example #26
0
    def get_primary_indices(self, objs, run_primary=False):
        if run_primary:
            primary = self.flags.val('resolve_status','run_primary')
        else:
            primary = self.flags.val('resolve_status','survey_primary')

        w=where1((objs['resolve_status'] & primary) != 0)
        return w
Example #27
0
def doplot(region=None, file=None):
    import biggles
    data = read_nasa_sdss_edge()
    
    lam,eta=eu.coords.eq2sdss(data['ra'],data['dec'])

    w0=where1( data['bossgood_edgeok'][:,0] == 1)
    w1=where1( data['bossgood_edgeok'][:,1] == 1)

    xlabel=r'$\lambda$'
    ylabel=r'$\eta$'
    plt=eu.plotting.bscatter(lam,eta, type='dot', xlabel=xlabel, ylabel=ylabel, show=False)
    plt=eu.plotting.bscatter(lam[w0],eta[w0], type='dot', color='red', show=False,plt=plt)
    plt=eu.plotting.bscatter(lam[w1],eta[w1], type='dot', color='blue', show=False,plt=plt)

    plt=es_sdsspy.stomp_maps.plot_boss_geometry(plt=plt, region=region, show=False)

    fake=eu.plotting.fake_filled_circles(['all','r=%0.2f' % _radii[0], 'r=%0.2f' % _radii[1]],
                                         colors=['black','red','blue'])

    # bug in biggles, ignores valign='bottom' for PlotKey
    valign='top'

    if region is None:
        k_yval=0.6
        l_yval=0.6
    elif region == 'sgc':
        k_yval=0.2
        l_yval=0.05
        valign='bottom'
    else:
        k_yval=0.95
        l_yval=0.95

    key=biggles.PlotKey(0.95,k_yval,fake,halign='right',valign=valign)
    plt.add(key)

    if region is not None:
        nt=biggles.PlotLabel(0.05,l_yval,region.upper(),halign='left',valign=valign)
        plt.add(nt)

    if file is None:
        plt.show()
    else:
        print 'Writing plot file:',file
        plt.write_eps(file)
Example #28
0
    def rotate_multi(self, runs, camcols, fields, filter, e1pix, e2pix, 
                     getrot=False, verbose=False):

        e1=numpy.zeros(runs.size, dtype='f8')
        e2=e1.copy()
        if getrot:
            angles = e1.copy()

        urun = unique(runs)
        for run in urun:
            if verbose:
                print("  run:",run)
            wrun=where1(runs == run)
            ucamcols = unique(camcols[wrun])
            for camcol in ucamcols:
                if verbose:
                    print("    camcol:",camcol)
                wc=where1( camcols[wrun] == camcol)
                wc=wrun[wc]
                h,rev=eu.stat.histogram(fields[wc], binsize=1, rev=True)
                for i in xrange(h.size):
                    if rev[i] != rev[i+1]:

                        wf = rev[rev[i]:rev[i+1]]
                        wf = wc[wf]

                        #uf = unique(fields[wf])
                        #if uf.size != 1:
                        #    raise ValueError("expected unique field")

                        field = fields[wf[0]]
                        #print("      field:",field)

                        res=self.rotate(run, camcol, field, filter, e1pix[wf], e2pix[wf], 
                                        getrot=getrot, verbose=verbose)
                        e1[wf] = res[0]
                        e2[wf] = res[1]
                        if getrot:
                            angles[wf] = res[2]

        if getrot:
            return e1,e2,angles
        else:
            return e1,e2
Example #29
0
    def create_objshear_input(self, lens_split=None):
        """
        To work in chunks, send nrand= and extra=chunknum
        """
        from es_sdsspy.stomp_maps import get_quad_logic

        nsplit=self['nsplit']
        if lens_split is None:
            raise ValueError("send lens_split=")
        print("doing lens_split %s: %s/%s" % (lens_split,lens_split+1,nsplit))

        self.load_stomp_maps()

        strict_edgecut = self['strict_edgecut']

        n=0

        data=self.read_raw()
        zindex = numpy.arange(data.size,dtype='i4')

        # do in chunks so we can see the progress
        npersplit = data.size/nsplit
        nleft = data.size % nsplit

        data = data[lens_split*npersplit:(lens_split+1)*npersplit]
        zindex = zindex[lens_split*npersplit:(lens_split+1)*npersplit]
        #data = data[0:100]
        #zindex = zindex[0:100]

        print("Generating z in [%0.2f,%0.2f]" % (self['zmin'],self['zmax']))
        z = self.zgen.genrand(data.size)


        print(" -> maskflags, max radius: %0.1f" % self['rmax'])
        maskflags = self.get_maskflags(data['ra'],data['dec'],z)

        quad_logic = get_quad_logic(maskflags, strict=strict_edgecut)

        wgood = where1(quad_logic)

        print(" -> good ones:",wgood.size)

        data      = data[wgood]
        zindex    = zindex[wgood]
        z         = z[wgood]
        maskflags = maskflags[wgood]

        dt = lensing.files.lcat_dtype()
        output = numpy.zeros(wgood.size, dtype=dt)
        output['zindex'][:]    = zindex
        output['ra'][:]        = data['ra']
        output['dec'][:]       = data['dec']
        output['z'][:]         = z
        output['maskflags'][:] = maskflags

        lensing.files.lcat_write(sample=self['sample'], data=output, lens_split=lens_split)
Example #30
0
File: sg.py Project: esheldon/espy
    def load_primary_avg_gri(self, indices=None):
        cols = self.open_columns('primary82')

        cmodel_mean_g = cols['cmodelflux_mean_g'][:]
        cmodel_mean_ivar_g = cols['cmodelflux_mean_ivar_g'][:]

        psf_mean_g = cols['psfflux_mean_g'][:]
        psf_mean_ivar_g = cols['psfflux_mean_ivar_g'][:]

        cmodel_mean_r = cols['cmodelflux_mean_r'][:]
        cmodel_mean_ivar_r = cols['cmodelflux_mean_ivar_r'][:]

        psf_mean_r = cols['psfflux_mean_r'][:]
        psf_mean_ivar_r = cols['psfflux_mean_ivar_r'][:]

        cmodel_mean_i = cols['cmodelflux_mean_i'][:]
        cmodel_mean_ivar_i = cols['cmodelflux_mean_ivar_i'][:]

        psf_mean_i = cols['psfflux_mean_i'][:]
        psf_mean_ivar_i = cols['psfflux_mean_ivar_i'][:]

        cmodelflux, cmodelflux_ivar = avg_gri(cmodel_mean_g, cmodel_mean_ivar_g,
                                            cmodel_mean_r, cmodel_mean_ivar_r,
                                            cmodel_mean_i, cmodel_mean_ivar_i)
        psfflux, psfflux_ivar = avg_gri(psf_mean_g, psf_mean_ivar_g,
                                        psf_mean_r, psf_mean_ivar_r,
                                        psf_mean_i, psf_mean_ivar_i)

        cbad=where1(numpy.isnan(cmodelflux))
        if cbad.size != 0:
            raise ValueError("nan found in cmodelflux")
        pbad=where1(numpy.isnan(psfflux))
        if pbad.size != 0:
            raise ValueError("nan found in psfflux")



        if indices is not None:
            cmodelflux=cmodelflux[indices]
            cmodelflux_ivar=cmodelflux_ivar[indices]
            psfflux=psfflux[indices]
            psfflux_ivar=psfflux_ivar[indices]
        return cmodelflux, cmodelflux_ivar, psfflux, psfflux_ivar
Example #31
0
    def ngals_logic(self, ngals):
        print("Cutting ngals >= %d" % self['ngals_r200_min'])
        logic = ngals >= self['ngals_r200_min']

        w=where1(logic)
        print("    Keeping %d/%d" % (w.size,ngals.size))
        if w.size == 0:
            raise ValueError("No objects passed z cut")

        return logic
Example #32
0
    def get_z_logic(self, z):
        print("Cutting z to [%f, %f]" % (self['zmin'],self['zmax']))
        logic = (z > self['zmin']) & (z < self['zmax']) 

        w=where1(logic)
        print("    Keeping %d/%d" % (w.size,z.size))
        if w.size == 0:
            raise ValueError("No objects passed z cut")

        return logic
def make_rotation_test_data(run=4335, camcol=3, field=100):
    c = lensing.regauss.open_columns('04')
    # note logic returns indices for Columns columns!
    w = c['run'] == run
    if w.size == 0:
        raise ValueError("no objects found for run %d" % run)

    flags = c['corrflags_rg_r'][w]
    camcols = c['camcol'][w]
    fields = c['field'][w]
    w2 = where1((camcols == camcol) & (flags == 0) & (fields == field))
    #w2=where1( camcols == camcol )
    if w2.size == 0:
        raise ValueError("not objects in camcol %s with flags==0" % camcol)
    w = w[w2]

    data = c.read_columns(
        ['ra', 'dec', 'run', 'camcol', 'field', 'e1_rg_r', 'e2_rg_r'], rows=w)

    output = numpy.zeros(data.size,
                         dtype=[('ra', 'f8'), ('dec', 'f8'), ('g1eq', 'f8'),
                                ('g2eq', 'f8'), ('clambda', 'f8'),
                                ('ceta', 'f8'), ('g1survey', 'f8'),
                                ('g2survey', 'f8')])

    output['ra'] = data['ra']
    output['dec'] = data['dec']

    lam, eta = eu.coords.eq2sdss(data['ra'], data['dec'])
    output['clambda'] = lam
    output['ceta'] = eta

    eq_rotator = SDSSRotator('eq')
    survey_rotator = SDSSRotator('survey')

    g1eq_alt, g2eq_alt = eq_rotator.rotate(data['run'],
                                           data['camcol'],
                                           data['field'],
                                           2,
                                           data['e1_rg_r'],
                                           data['e2_rg_r'],
                                           verbose=True)
    g1eq_alt /= 2
    g2eq_alt /= 2

    output_file = os.path.expanduser('~/tmp/test-rot/test-rot.rec')
    print("writing output file:", output_file)
    fobj = open(output_file, 'w')
    num = numpy.array([output.size], dtype='i8')
    num.tofile(fobj)
    #robj = eu.recfile.Recfile(output_file, 'w', delim=' ')
    robj = eu.recfile.Recfile(fobj, 'r+')
    robj.write(output)
    robj.close()
    def rotate(self,
               run,
               camcol,
               field,
               filter,
               e1pix,
               e2pix,
               getrot=False,
               verbose=False):
        if not numpy.isscalar(run):
            return self.rotate_multi(run,
                                     camcol,
                                     field,
                                     filter,
                                     e1pix,
                                     e2pix,
                                     getrot=getrot,
                                     verbose=verbose)

        self.load_run(run)

        if verbose:
            print("%06i-%i-%04i" % (run, camcol, field))

        w = where1((self.rotstruct['camcol'] == camcol)
                   & (self.rotstruct['field'] == field))
        if w.size != 1:
            print("expected single match for %06i-%i-%04i, got %i" %
                  (run, camcol, field, w.size))
            fields = numpy.unique(self.rotstruct['field'])
            print("here are the fields:", fields)
            raise ValueError("stopping")

        filternum = sdsspy.FILTERNUM[filter]
        cos2angle = self.rotstruct['cos2angle'][w, filternum]
        sin2angle = self.rotstruct['sin2angle'][w, filternum]

        # hmm... this seems to be applying -2*angle rotation...
        e1 = e1pix * cos2angle + e2pix * sin2angle
        e2 = -e1pix * sin2angle + e2pix * cos2angle

        if getrot:
            angle = self.rotstruct['angle'][w, filternum]
            angles = numpy.zeros(e1.size, dtype='f8') + angle[0]
            return e1, e2, angles
        else:
            return e1, e2
    def compare(self, run):
        import biggles
        import pcolors

        pdata, mdata = self.load_data(run)
        if len(pdata) == 0:
            print("no princeton data found")
            return
        if len(mdata) == 0:
            print("no my data found")
            return

        tab = biggles.Table(2, 2)

        pcos_plt = biggles.FramedPlot()
        psin_plt = biggles.FramedPlot()
        mcos_plt = biggles.FramedPlot()
        msin_plt = biggles.FramedPlot()

        pcos_plots = []
        psin_plots = []
        mcos_plots = []
        msin_plots = []

        colors = pcolors.rainbow(6, 'hex')

        for camcol in xrange(1, 6 + 1):
            # first princeton
            wp = where1(pdata['camcol'] == camcol)

            bcos = eu.stat.Binner(pdata['field'][wp],
                                  cos(2 * pdata['phi_offset'][wp]))
            bcos.dohist(binsize=1.0)
            bcos.calc_stats()

            wgood_cos = where1(bcos['hist'] > 0)
            pcos = biggles.Curve(bcos['xmean'][wgood_cos],
                                 bcos['ymean'][wgood_cos],
                                 color=colors[camcol - 1])
            pcos.label = 'princ camcol %s' % camcol

            pcos_plt.add(pcos)
            pcos_plots.append(pcos)

            bsin = eu.stat.Binner(pdata['field'][wp],
                                  sin(2 * pdata['phi_offset'][wp]))
            bsin.dohist(binsize=1.0)
            bsin.calc_stats()

            wgood_sin = where1(bsin['hist'] > 0)
            psin = biggles.Curve(bsin['xmean'][wgood_sin],
                                 bsin['ymean'][wgood_sin],
                                 color=colors[camcol - 1])
            psin.label = 'princ camcol %s' % camcol

            psin_plt.add(psin)
            psin_plots.append(psin)

            # now mine
            wm = where1(mdata['camcol'] == camcol)
            mpcos = biggles.Curve(mdata['field'][wm],
                                  cos(2 * mdata['angle'][wm, 2]),
                                  color=colors[camcol - 1])
            mpcos.label = 'mine camcol %s' % camcol
            mcos_plt.add(mpcos)
            mcos_plots.append(mpcos)

            wm = where1(mdata['camcol'] == camcol)
            mpsin = biggles.Curve(mdata['field'][wm],
                                  sin(2 * mdata['angle'][wm, 2]),
                                  color=colors[camcol - 1])
            mpsin.label = 'mine camcol %s' % camcol
            msin_plt.add(mpsin)
            msin_plots.append(mpsin)

        # princeton stuff
        pcos_key = biggles.PlotKey(0.1, 0.9, pcos_plots)
        pcos_plt.add(pcos_key)

        pcos_plt.xlabel = 'Field'
        pcos_plt.title = 'Run: %s' % run
        pcos_plt.ylabel = 'cos(2*angle)'

        psin_key = biggles.PlotKey(0.1, 0.9, psin_plots)
        psin_plt.add(psin_key)

        psin_plt.xlabel = 'Field'
        psin_plt.title = 'Run: %s' % run
        psin_plt.ylabel = 'sin(2*angle)'

        tab[0, 0] = pcos_plt
        tab[0, 1] = psin_plt

        # my stuff
        mcos_key = biggles.PlotKey(0.1, 0.9, mcos_plots)
        mcos_plt.add(mcos_key)

        mcos_plt.xlabel = 'Field'
        mcos_plt.title = 'Run: %s' % run
        mcos_plt.ylabel = 'cos(2*angle)'

        msin_key = biggles.PlotKey(0.1, 0.9, msin_plots)
        msin_plt.add(msin_key)

        msin_plt.xlabel = 'Field'
        msin_plt.title = 'Run: %s' % run
        msin_plt.ylabel = 'sin(2*angle)'

        tab[1, 0] = mcos_plt
        tab[1, 1] = msin_plt

        tab.show()