Ejemplo n.º 1
0
const ValVec<htmRange> & 
htmInterface::domainCmd( char *str ) {

  cmd_ = str;
  if(t_)delete t_;
  t_ = new VarStrToken(cmd_);

  cmdCode code = getCode();
  if(code != HTMDOMAIN)
    throw SpatialInterfaceError("htmInterface:domainCmd: missing keyword HTMDOMAIN");
  getDepth();

  int32 nx,nc;
  nx = getInteger();

  SpatialDomain dom;
  for(int32 i = 0 ; i < nx; i++ ) {
    SpatialConvex convex;
    nc = getInteger();
    for(int32 j = 0; j < nc; j++ ) {
      float64 x = getFloat();
      float64 y = getFloat();
      float64 z = getFloat();
      float64 d = getFloat();
      SpatialConstraint c(SpatialVector(x,y,z),d);
      convex.add(c);
    }
    dom.add(convex);
  }

  return domain(dom);
}
Ejemplo n.º 2
0
const ValueVector &
htmInterface::doHull() {

	if(polyCorners_.size() < 3)
		throw SpatialInterfaceError("htmInterface:convexHull: empty hull: points on one line");

	SpatialVector v;

	RangeConvex cvx;

	SpatialDomain dom;

	// The constraint we have for each side is a 0-constraint (great circle)
	// passing through the 2 corners. Since we are in counterclockwise order,
	// the vector product of the two successive corners just gives the correct
	// constraint.
	size_t i, len = polyCorners_.size();
	for(i = 0; i < len; i++) {
		v = polyCorners_[i].c_ ^ polyCorners_[ i == len-1 ? 0 : i + 1].c_;
#ifdef DIAG
		cerr << "doHull:: " << v << " " << i << "," << i+1 << endl;
#endif
		v.normalize();
		SpatialConstraint c(v,0);
		cvx.add(c); // [ed:RangeConvex::add]
	}
	dom.add(cvx);
//%%%%%%%%%%%%%%%%%%%%%%%%%%%
//	dom.convexes_[0].boundingCircle_.write(cout);
//	dom.write(cout);
//%%%%%%%%%%%%%%%%%%%%%%%%%%%
	return domain(dom);
}
Ejemplo n.º 3
0
const ValVec<htmRange> &
htmInterface::doHull() {

  if(polyCorners_.length() < 3)
    throw SpatialInterfaceError("htmInterface:convexHull: empty hull: points on one line");

  SpatialVector v;
  SpatialConvex x;
  SpatialDomain d;

  // The constraint we have for each side is a 0-constraint (great circle)
  // passing through the 2 corners. Since we are in counterclockwise order,
  // the vector product of the two successive corners just gives the correct
  // constraint.
  size_t i, len = polyCorners_.length();
  for(i = 0; i < len; i++) {
    v = polyCorners_[i].c_ ^ polyCorners_[ i == len-1 ? 0 : i + 1].c_;
#ifdef DIAG
    cerr << v << " " << i << "," << i+1 << "\n";
#endif
    v.normalize();
    SpatialConstraint c(v,0);
    x.add(c);
  }
  d.add(x);
  d.intersect(index_, idList_);

  range_.cut(range_.length());
  makeRange();

  return range_;
}
Ejemplo n.º 4
0
/*
 *
 * Intersect ra/dec with HTM triangles within angle cosangle. Fill idlist
 * with the HTM triangle ids
 *
 */
void LensSource::IntersectHtm(
        double rRa, 
        double rDec, 
        double rCosAngle, 
        vector<uint64> &rIdList)
{

    ValVec<uint64> plist, flist;
    uint64 id;

    // We must intitialize each time because it remembers it's state
    // internally
    SpatialDomain domain;    // initialize empty domain

    domain.setRaDecD(rRa,rDec,rCosAngle); //put in ra,dec,d E.S.S.

    domain.intersect(mSpatialIndex, plist, flist);	  // intersect with list

    rIdList.clear();
    for (int32 i=0; i<flist.length(); i++) {
        id = flist[i];
        if (id >= mPars.min_htm_index && id <= mPars.max_htm_index) {
            rIdList.push_back(id);
        }
    }
    for (int32 i=0; i<plist.length(); i++) 
    {
        id = plist[i];
        if (id >= mPars.min_htm_index && id <= mPars.max_htm_index) 
        {
            rIdList.push_back(id);
        }
    }

}
Ejemplo n.º 5
0
int
test4 (int flag) {

  int nfailed = 0;
  HtmRange hr;
  SpatialIndex  si (3,3);
  SpatialVector sx (0.,0.);
  SpatialVector sz (0.,90.);
  SpatialConstraint sd (sx, .98);
  SpatialConstraint sc (sz, .98);
  RangeConvex cc;
  RangeConvex cd;
  SpatialDomain sdm;

  // create two convexes, along x and z
  cc.add(sc);
  cd.add(sd);
	
  // add them to the domain
  sdm.add(cc);
  sdm.add(cd);

  sdm.intersect(&si, &hr);

  if (flag>=DBG_MORE) sdm.write(cout);
	
  nfailed = 0;
  // showlists("domain 1",flag,full,partial);

  return showtest("Test4 (domain)\t",nfailed,flag);
}
Ejemplo n.º 6
0
void corrobj::intersect(double ra, double dec, float DA, vector<uint64> &idlist)
{

    double d = cos( par.rmax/DA );
    ValVec<uint64> plist, flist;

    // We must intitialize each time because it remembers it's state
    // internally
    SpatialDomain domain;    // initialize empty domain

    domain.setRaDecD(ra,dec,d); //put in ra,dec,d E.S.S.

    domain.intersect(spatialIndex, plist, flist);	  // intersect with list

    //cout << "Number in plist+flist: " << plist.length() + flist.length() << endl;
    //cout << "   Separately: " << plist.length() << " " << flist.length() << endl;
    // Save the result in idlist. This is not a bottleneck
    idlist.resize( flist.length() + plist.length() );

    int idCount=0;
    // ----------- FULL NODES -------------
    for(int i = 0; i < flist.length(); i++)
    {  
        idlist[idCount] = flist(i);
        idCount++;
    }
    // ----------- Partial Nodes ----------
    for(int i = 0; i < plist.length(); i++)
    {  
        idlist[idCount] = plist(i);
        idCount++;
    }


}
Ejemplo n.º 7
0
int
test4 (int flag) {

  int nfailed = 0;
  ValueVectorUint64 full, partial;
  SpatialIndex  si (3,3);
  SpatialVector sx (0.,0.);
  SpatialVector sz (0.,90.);
  SpatialConstraint sd (sx, .98);
  SpatialConstraint sc (sz, .98);
  SpatialConvex cc;
  SpatialConvex cd;
  SpatialDomain sdm;

  // create two convexes, along x and z
  cc.add(sc);
  cd.add(sd);
	
  // add them to the domain
  sdm.add(cc);
  sdm.add(cd);

  sdm.intersect(&si, partial, full);

  if (flag>=DBG_MORE) sdm.write(std::cout);
	
  nfailed += !(partial.size()==24);
  nfailed += !(full.size()==8);
  showlists("domain 1",flag,full,partial);

  return showtest("Test4 (domain)\t",nfailed,flag);
}
Ejemplo n.º 8
0
const ValueVector & 
htmInterface::circleRegion( float64 ra,
			    float64 dec,
			    float64 rad ) {

	SpatialDomain dom;

	RangeConvex cvx;

	float64 d = cos(gPi * rad/10800.0);
	SpatialConstraint c(SpatialVector(ra,dec),d);
	cvx.add(c); // [ed:RangeConvex::add]
	dom.add(cvx);
	return domain(dom);
}
Ejemplo n.º 9
0
const ValVec<htmRange> & 
htmInterface::circleRegion( float64 ra,
			    float64 dec,
			    float64 rad ) {

  SpatialDomain domain;
  SpatialConvex convex;
  float64 d = cos(gPi * rad/10800.0);
  SpatialConstraint c(SpatialVector(ra,dec),d);

  convex.add(c);
  domain.add(convex);
  domain.intersect(index_, idList_);

  range_.cut(range_.length());
  makeRange();

  return range_;
}
Ejemplo n.º 10
0
int main(int argc, char *argv[])
{

    if (argc < 2) 
    {
        cout << "-Syntax: objshear par_file" << endl;
        return(1);
    }

    int debug=0;

    string par_file = argv[1];

    LensSource data(par_file);
    return(0);
    data.TestScat();
    printf("\n");
    data.TestLcat();


    /* Copy out pointers to the data */
    par_struct    *par_struct = data.GetPar();

    lens_struct   *lcat = data.GetLcat();
    source_struct *scat = data.GetScat();
    int32         *revInd = data.GetRev();


    float32 scinv_factor;
    float32 aeta_zero;
    float32 *aeta_lens;
    float32 *aeta_source;
    scinv_struct2d* scinv;
    if (par_struct->sigmacrit_style == 1)
    {
        scinv_factor = data.GetScinvFactor();
        aeta_zero    = data.GetAetaZero();
        aeta_lens   = data.GetAetaLens();
        aeta_source = data.GetAetaSource();
    } else if (par_struct->sigmacrit_style == 3)
    {
        scinv = data.GetScinv();
    }

    float32 H0 = par_struct->h*100.0;
    float32 omega_m = par_struct->omega_m;

    int32 minLeafId = par_struct->min_htm_index;
    int32 maxLeafId = par_struct->max_htm_index;

    int32 step=500;
    int32 bigStep=10000;
    //int32 bigStep=1000;

    float32 zbuffer = par_struct->zbuffer;

    // Number of lenses 
    int32 i;

    int32 nlens = data.GetNlens();

    cout << endl <<
        "Number of lenses: " << nlens << endl;

    cout << endl <<
        "Each dot is " << step << " lenses" << endl;

    lensout_struct lensout;
    data.MakeLensout(lensout, par_struct->nbin);


    FILE *fptr = fopen(par_struct->output_file.c_str(), "w");
    cout << "Writing header for file " << par_struct->output_file << endl;
    data.WriteHeader(fptr);

    FILE *pfptr;
    if (par_struct->dopairs) {
        pfptr = fopen(par_struct->pair_file.c_str(), "w");
        // Write nrows=0 into the header to start with
        cout << "Writing header for pair file " << par_struct->pair_file << endl;
        //write_pair_header(pfptr, 0);
        exit(45);
    }


    try {

        if ( (zbuffer != 0.0) && (par_struct->sigmacrit_style == 2) )
        {
            cout << "Do not support zbuffer for sigmacrit_style==2 yet" << endl;
            exit(1);
        }


        // construct index with given depth and savedepth
        int32 savedepth=2;
        int32 nlens = data.GetNlens();

        htmInterface htm(par_struct->depth,savedepth);  // generate htm interface
        const SpatialIndex &index = htm.index();

        /* keeping track of those found */
        int64 nFoundTot=0, nFoundUse=0, totalPairs=0;
        float32 sigwsum=0, sigsum=0;

        // Loop over all the lenses


        printf("lens = 0/%d   Mean dens. cont. = 0 +/- 0\n", nlens);
        fflush(stdout);

        int32 lensUsed = 0,lensUsedOld=0;
        for (int32 lensIndex=0; lensIndex < nlens; lensIndex++) 
        {

            int32 zindex = lcat[lensIndex].zindex;
            lensout.zindex = zindex;

            int16 bad;
            int16 pixelMaskFlags;

            lensUsed++;

            pixelMaskFlags = lcat[lensIndex].pixelMaskFlags;

            bad = pixelMaskFlags & FLAGS_MASKED;


            // Is the central point masked?  I often check before
            // making the catalog...
            if (!bad) 
            {


                // Declare the domain and the lists
                SpatialDomain domain;    // initialize empty domain
                ValVec<uint64> plist, flist;	// List results

                vector <uint32> idList;
                int32 idCount=0;

                static const float 
                    PI=3.1415927,
                    TWOPI=6.2831853,
                    PIOVER2=1.5707963,
                    THREE_PIOVER2=4.7123890;

                float32 
                    zlens, zSource, zSourceErr, 
                    sig_inv, sig_inv2, 
                    sWeight, osWeight, wts_ssh,
                    inverse_wsum, rmin_act, rmax_act;

                float32 comov_fac2, comov_fac4;

                float64 
                    ra, dec, clambda, ceta;

                float64 d, R, theta, theta2, Rkpc;
                float32 DL;
                float32 adiff_ls, adiff_s;

                int8 radBin;

                float64 
                    xrel, yrel, diffsq, xy, 
                    xpysum=0, xmysum=0, xysum=0, ie1, ie2, ie, mm, maxe=0.2, 
                    RkpcInv2, cos2theta, sin2theta,
                    e1prime, e2prime, e1e1err2, e1e2err, e1e2err2, e2e2err2,
                    etan_err2, shear_err2, ortho_err2, orthoshear_err2,
                    denscont, densconterr2, orthodenscont, orthodensconterr2;

                float32 tw, f_e, f_sn, k0, k1, F;

                int16 bad12, bad23, bad34, bad41;

                int32 i, j, n, iSrc, srcUse;
                int32 nFound;
                int32 leafId, leafBin, nLeafBin;
                float32 logRkpc;

                source_struct *tscat;

                bad12 = pixelMaskFlags & (FLAGS_QUAD1_MASKED+FLAGS_QUAD2_MASKED);
                bad23 = pixelMaskFlags & (FLAGS_QUAD2_MASKED+FLAGS_QUAD3_MASKED);  
                bad34 = pixelMaskFlags & (FLAGS_QUAD3_MASKED+FLAGS_QUAD4_MASKED);
                bad41 = pixelMaskFlags & (FLAGS_QUAD4_MASKED+FLAGS_QUAD1_MASKED);

                // extract some shorthand 
                ra      = lcat[lensIndex].ra;
                dec     = lcat[lensIndex].dec;
                clambda = lcat[lensIndex].clambda;
                ceta    = lcat[lensIndex].ceta;

                zlens = lcat[lensIndex].z;

                d = cos( lcat[lensIndex].angmax*D2R );

                domain.setRaDecD(ra,dec,d); //put in ra,dec,d E.S.S.

                domain.intersect(&index,plist,flist);	  // intersect with list


                nFound = flist.length() + plist.length();
                nFoundTot += nFound;

                // Save the result in idlist. This is not a bottleneck
                idList.resize(nFound);

                // ----------- FULL NODES -------------
                for(i = 0; i < flist.length(); i++)
                {  
                    idList[idCount] = (uint32 )flist(i);
                    idCount++;
                }
                // ----------- Partial Nodes ----------
                for(i = 0; i < plist.length(); i++)
                {  
                    idList[idCount] = (uint32 )plist(i);
                    idCount++;
                }


                // Now loop over leaf ids and get the sources
                for(i=0; i<nFound;i++)
                {


                    leafId = idList[i];

                    // Convert leafid into bin number
                    leafBin = idList[i] - minLeafId;


                    // Check if there are sources in this leafid
                    if ( leafId >= minLeafId  &&  leafId <= maxLeafId)
                    {



                        // Look for sources in this triangle
                        if (revInd[leafBin] != revInd[leafBin+1]) 
                        {


                            nFoundUse+=1;

                            nLeafBin = revInd[leafBin+1] - revInd[leafBin];
                            //cout << "nleafbin = " << nLeafBin << endl;
                            // Loop over sources in this leaf
                            for(iSrc=0;iSrc<nLeafBin;iSrc++)
                            {

                                int pcount=1;
                                srcUse = revInd[ revInd[leafBin]+iSrc ];

                                if (debug) printf("srcUse = %d", srcUse);
                                tscat = &scat[srcUse];

                                // Distance and angle to source
                                // *** This is a major bottleneck ***
                                //gcircSurvey(clambda, ceta, 
                                //	    tscat->clambda, tscat->ceta,
                                //	    R, theta);
                                gcircSurvey2(clambda, ceta, 
                                        tscat->clambda, tscat->ceta,
                                        R, theta);


                                // kpc
                                DL = lcat[lensIndex].DL;
                                Rkpc = R*DL;

                                // Mpc
                                DL = DL/1000.0;

                                // convert to comoving?
                                if (par_struct->comoving) 
                                {
                                    comov_fac2 = 1./pow(1+zlens, 2);
                                    comov_fac4 = 1./pow(1+zlens, 4);
                                    Rkpc = Rkpc*(1+zlens);
                                }



                                // Within our circular radius as well as lower limit
                                // in angular radius?
                                if (Rkpc >= par_struct->rmin && Rkpc <= par_struct->rmax &&
                                        R > MINIMUM_ANGLE)
                                {

                                    // Is this source in one of the quadrants that is 
                                    // useful for this lens?
                                    theta2 = PIOVER2 - theta;
                                    if (objShearTestQuad(bad12,bad23,bad34,bad41,theta2))
                                    {

                                        // What kind of binning?
                                        if (par_struct->logbin)
                                        {
                                            logRkpc = log10(Rkpc);
                                            radBin = (int8) ( (logRkpc-par_struct->logRmin)/par_struct->logBinsize );
                                        }
                                        else 
                                        {
                                            radBin = (int8) ( (Rkpc-par_struct->rmin)/par_struct->binsize );
                                        }



                                        // valid bin number?
                                        if (radBin >= 0 && radBin < par_struct->nbin)
                                        {


                                            ///////////////////////////////////////////////////////////
                                            // How will we calculate the inverse critical density?
                                            ///////////////////////////////////////////////////////////

                                            if (par_struct->sigmacrit_style == 1)
                                            {
                                                // Treating photozs as truth
                                                zSource = tscat->photoz_z;

                                                if ( zSource >= (zlens + zbuffer) )
                                                {
                                                    adiff_ls = aeta_lens[lensIndex] - aeta_source[srcUse];
                                                    adiff_s = aeta_zero - aeta_source[srcUse];
                                                    sig_inv = scinv_factor*DL*adiff_ls/adiff_s;
                                                }
                                                else
                                                    sig_inv = -9999.0;

                                            }
                                            else if (par_struct->sigmacrit_style == 2)
                                            {
                                                // Using mean inverse critical density, integrated over
                                                // deconvolved distribution.  Note: this is not changing
                                                // for each source, but the assignment is placed here
                                                // anyway for clarity
                                                sig_inv = lcat[lensIndex].mean_scinv;

                                            }
                                            else if (par_struct->sigmacrit_style == 3) 
                                            {
                                                zSource = tscat->photoz_z;
                                                sig_inv = sigmaCritInvInterp2d(zlens, zSource, scinv);
                                                //if (sig_inv < 0.0)
                                                //    printf("zlens: %f zsourc: %f sig_inv = %e\n", zlens, zSource, sig_inv);
                                            }


                                            // Valid inverse critical density?
                                            if (sig_inv > 0.0) 
                                            {


                                                sig_inv2 = sig_inv*sig_inv;

                                                // X/Y positions
                                                xrel = Rkpc*cos(theta);
                                                yrel = Rkpc*sin(theta);


                                                // eta is flipped
                                                diffsq = xrel*xrel - yrel*yrel;
                                                xy = xrel*yrel;

                                                RkpcInv2 = 1.0/Rkpc/Rkpc;

                                                cos2theta = diffsq*RkpcInv2;
                                                sin2theta = 2.0*xy*RkpcInv2;

                                                // Tangential/45-degree rotated ellipticities
                                                e1prime = -(tscat->e1*cos2theta + tscat->e2*sin2theta);
                                                e2prime =  (tscat->e1*sin2theta - tscat->e2*cos2theta);


                                                // covariance
                                                e1e2err = tscat->e1e2err;
                                                e1e2err2 = e1e2err*e1e2err;
                                                if (e1e2err < 0) e1e2err2 = -e1e2err2;

                                                e1e1err2 = 
                                                    tscat->e1e1err*tscat->e1e1err;

                                                e2e2err2 = 
                                                    tscat->e2e2err*tscat->e2e2err;


                                                // Errors in tangential/ortho
                                                etan_err2 = 
                                                    e1e1err2*cos2theta*cos2theta + e2e2err2*sin2theta*sin2theta - 
                                                    2.0*cos2theta*sin2theta*e1e2err2; 

                                                shear_err2 = 0.25*(etan_err2 + SHAPENOISE2);

                                                ortho_err2 = 
                                                    e1e1err2*sin2theta*sin2theta + e2e2err2*cos2theta*cos2theta - 
                                                    2.0*cos2theta*sin2theta*e1e2err2; 

                                                orthoshear_err2 = 0.25*(ortho_err2 + SHAPENOISE2);

                                                // density contrast
                                                denscont = e1prime/2.0/sig_inv;
                                                densconterr2 = shear_err2/sig_inv2;

                                                orthodenscont = e2prime/2.0/sig_inv;
                                                orthodensconterr2 = orthoshear_err2/sig_inv2;


                                                if (par_struct->comoving) 
                                                {
                                                    denscont *= comov_fac2;
                                                    densconterr2 *= comov_fac4;

                                                    orthodenscont *= comov_fac2;
                                                    orthodensconterr2 *= comov_fac4;
                                                }

                                                if (densconterr2 > 0.0) {
                                                    sWeight = 1./densconterr2;
                                                    osWeight = 1./orthodensconterr2;
                                                } else {
                                                    sWeight = 0.0;
                                                    osWeight = 0.0;
                                                }

                                               if (sWeight > 0.0)
                                               {
                                                   if (debug)
                                                   {
                                                       cout <<" "<<lensIndex<<" "<<srcUse<<" "
                                                           <<tscat->e1<<" "<<tscat->e2<<" "
                                                           <<sWeight<<" "<<denscont<<" "<<densconterr2<<" "
                                                           <<xrel<<" "<<yrel;
                                                   }
 
                                                    totalPairs +=1;

                                                    // Write the pair info
                                                    if (par_struct->dopairs)
                                                    {
                                                        //write_pairs(pfptr, zindex, srcUse, (float) Rkpc, sWeight);
                                                        exit(45);
                                                    }


                                                    tw = 1./(etan_err2 + SHAPENOISE2);
                                                    f_e = etan_err2*tw;
                                                    f_sn = SHAPENOISE2*tw;

                                                    // coefficients (p 596 Bern02) 
                                                    // there is a k1*e^2/2 in Bern02 because
                                                    // its the total ellipticity he is using

                                                    wts_ssh = sWeight;
                                                    k0 = f_e*SHAPENOISE2;
                                                    k1 = f_sn*f_sn;
                                                    F = 1. - k0 - k1*e1prime*e1prime;

                                                    // keep running totals of positions for
                                                    // ellipticity of source distribution

                                                    xpysum += xrel*xrel + yrel*yrel;
                                                    xmysum += diffsq;
                                                    xysum  += xy;

                                                    ////////////////////////////////////////
                                                    // Fill in the lens structure
                                                    ////////////////////////////////////////

                                                    lensout.tot_pairs += 1;
                                                    lensout.npair[radBin] +=1;

                                                    rmax_act = 
                                                        lensout.rmax_act[radBin];
                                                    rmin_act =
                                                        lensout.rmin_act[radBin];

                                                    if (rmax_act == 0.0) {
                                                        rmax_act=Rkpc;
                                                    } else {
                                                        rmax_act = max(rmax_act, (float32) Rkpc);
                                                    }
                                                    if (rmin_act == 0.0) {
                                                        rmin_act=Rkpc;
                                                    } else {
                                                        rmin_act = min(rmin_act, (float32) Rkpc);
                                                    }

                                                    lensout.rmax_act[radBin] = rmax_act;
                                                    lensout.rmin_act[radBin] = rmin_act;

                                                    // these are initally sums, then converted to
                                                    // means later by dividing by wsum

                                                    lensout.rsum[radBin]     += Rkpc;

                                                    lensout.sigma[radBin]    += sWeight*denscont;
                                                    lensout.orthosig[radBin] += osWeight*orthodenscont;

                                                    lensout.weight        += sWeight;
                                                    lensout.wsum[radBin]  += sWeight;
                                                    lensout.owsum[radBin] += osWeight;
                                                    lensout.wscritinvsum[radBin] += sWeight*sig_inv;

                                                    lensout.sigerrsum[radBin] += 
                                                        sWeight*sWeight*denscont*denscont;
                                                    lensout.orthosigerrsum[radBin] += 
                                                        osWeight*osWeight*orthodenscont*orthodenscont;

                                                    lensout.sshsum   += wts_ssh*F;
                                                    lensout.wsum_ssh += wts_ssh;



                                                } // Non-zero weight?
                                                else if (debug) printf(" bad weight");

                                            } // Good sig_inv?
                                            else if (debug) printf(" bad siginv");

                                        } // good radBin? 
                                        else if (debug) printf(" bad radbin");

                                    } // Good quadrant?
                                    else if (debug) printf(" bad quad");

                                } // radius within circular radius min/max?
                                else if (debug) printf(" outside rmin/rmax: %f", Rkpc);

                                if (debug) printf("\n");
                            } // loop over sources			

                        } // any sources found in this leaf?

                    } // leaf id is in allowed range?

                } // loop over found leaf ids



                /////////////////////////////////////////////////////////
                // Now compute averages if there were any sources for
                // this lens, i.e. weight > 0
                /////////////////////////////////////////////////////////

                if (lensout.weight > 0.0) 
                {

                    ie1 = xmysum/xpysum;
                    ie2 = 2.*xysum/xpysum;
                    ie = sqrt( ie1*ie1 + ie2*ie2 );
                    lensout.ie = ie;

                    mm = 3.0/sqrt(lensout.tot_pairs);

                    for(radBin=0; radBin<par_struct->nbin; radBin++)
                    {

                        if (lensout.wsum[radBin] > 0.0) 
                        {

                            // Keep track for "good" lenses
                            if (ie < max(mm, maxe)) 
                            {
                                inverse_wsum = 1.0/( lensout.wsum[radBin] );

                                float tsigwsum = lensout.wsum[radBin];
                                float tsigsum  = lensout.sigma[radBin];
                                if (!isnan(tsigwsum) && !isnan(tsigsum) && !isnan(inverse_wsum)) {
                                    sigwsum += tsigwsum;
                                    sigsum += tsigsum;
                                    lensout.sigma[radBin] *= inverse_wsum;
                                    lensout.sigmaerr[radBin] = sqrt(inverse_wsum);
                                } else {
                                    lensout.wsum[radBin] = 0.0;
                                    lensout.sigma[radBin] = 0.0;
                                    lensout.sigmaerr[radBin] = 0.0;
                                    printf("/");
                                }
                            }
                        }
                        if (lensout.owsum[radBin] > 0.0) 
                        {
                            inverse_wsum = 1.0/( lensout.owsum[radBin] );

                            lensout.orthosig[radBin] *= inverse_wsum;
                            lensout.orthosigerr[radBin] = sqrt(inverse_wsum);
                        }

                    }

                } // weight > 0.0?

            } // central point unmasked?

            // need to write all since we set NROWS = nlens
            // For large angles, all lenses have weight > 0 but for smaller
            // radii this may write zeroes

            data.WriteLensout(fptr, lensout);

            data.ResetLensout(lensout);

            if ( (lensIndex % step) == 0 && (lensIndex != 0)) 
            {
                printf(".");
                if ( (lensIndex % bigStep) == 0)
                    objShearPrintMeans(lensIndex,nlens,sigsum,sigwsum);
                fflush(stdout);
            }

            /*
               if ( (lensUsed % step) == 0 && (lensUsed != 0) && 
               (lensUsed != lensUsedOld) ) 
               {
               printf(".");
               if ( (lensUsed % bigStep) == 0)
               objShearPrintMeans(lensUsed,nlens,sigsum,sigwsum);
               fflush(stdout);
               lensUsedOld = lensUsed;
               }
               */
        } // loop over lenses 


        if (par_struct->dopairs) {
            rewind(pfptr);
            //write_pair_header(pfptr, totalPairs);
            exit(45);
        }

    } catch (SpatialException &x) {
        printf("%s\n",x.what());
    }



    fclose(fptr);

    if (par_struct->dopairs) 
        fclose(pfptr);

    cout << endl << "Done" << endl;

    return(0);
}
Ejemplo n.º 11
0
void htmmatchc(int argc, IDL_VPTR *argv, char *argk)
{


    // These will point at the input data
    double *ra1, *dec1, *ra2, *dec2, *angleinput;
    IDL_LONG *htmrev2, minLeafId, maxLeafId;
    FILE *fptr=NULL;


    // Will be copies of inputs
    IDL_MEMINT n1, n2, nangle;
    IDL_MEMINT depth;
    IDL_MEMINT maxmatch;

    IDL_LONG64 ntotal=0;

    // Process the keywords
    int nparms = IDL_KWProcessByOffset(argc, argv, argk, kw_pars, 
            (IDL_VPTR *) 0, 1, &kw);


    ///////////////////////////////////////////////////////////
    // Check the arguments
    ///////////////////////////////////////////////////////////

    if (nparms != 11)
    {
        htmMatchErrOut("-Syntax: htmmatchc, ra1, dec1, ra2, dec2, htmrev2, minLeafId, maxLeafId, angle, match1, match2, d12, depth=, maxmatch=, status=\nRA/DEC must be double");
        return;
    }

    IDL_VPTR ra1Vptr  = argv[0];
    IDL_VPTR dec1Vptr = argv[1];
    IDL_VPTR ra2Vptr  = argv[2];
    IDL_VPTR dec2Vptr = argv[3];

    IDL_VPTR htmrev2Vptr = argv[4];
    IDL_VPTR minLeafIdVptr = argv[5];
    IDL_VPTR maxLeafIdVptr = argv[6];
    IDL_VPTR angleVptr = argv[7];

    IDL_VPTR match1Vptr = argv[8];
    IDL_VPTR match2Vptr = argv[9];
    IDL_VPTR dis12Vptr = argv[10];

    // Extract the inputs
    if (!htmMatchGetData(ra1Vptr, dec1Vptr, 
                ra2Vptr, dec2Vptr, 
                htmrev2Vptr, 
                minLeafIdVptr, maxLeafIdVptr,
                angleVptr, 

                &ra1, &dec1,
                &ra2, &dec2,
                &htmrev2, 
                &minLeafId, &maxLeafId,
                &n1, &n2, 
                &angleinput, &nangle,
                &depth, &maxmatch)) 
    {
        return;
    }

    // Defaults
    IDL_StoreScalarZero(match1Vptr, IDL_TYP_LONG);
    match1Vptr->value.l = -1;
    IDL_StoreScalarZero(match2Vptr, IDL_TYP_LONG);
    match2Vptr->value.l = -1;
    IDL_StoreScalarZero(dis12Vptr, IDL_TYP_DOUBLE);
    dis12Vptr->value.d = -1;

    if (kw.file_there)
    {
        char* file = IDL_VarGetString(kw.file);
        fptr = fopen(file, "w");
        if (fptr==NULL) 
        {
            char mess[100];
            sprintf(mess,"Cannot open file %s: %s", file, strerror(errno)); 
            htmMatchErrOut(mess);
            return;
        }
        // zero for starters
        fwrite(&ntotal, sizeof(IDL_LONG64), 1, fptr);

        // The size of the indices is IDL_LONG (3) for now
        IDL_LONG sz = 3;
        fwrite(&sz, sizeof(IDL_LONG), 1, fptr);

        // Size of distance is DOUBLE (5)
        sz = 5;
        fwrite(&sz, sizeof(IDL_LONG), 1, fptr);
    }

    try {

        int savedepth = 2;
        htmInterface htm(depth,savedepth);  // generate htm interface
        const SpatialIndex &index = htm.index();

        double angle=0, d=0;

        vector <int32> m1;
        vector <int32> m2;  
        vector <float64> d12;

        if (nangle == 1) {
            angle = angleinput[0];
            d = cos( angle );
        }


        time_t tleaves=0, tother=0;

        IDL_MEMINT nFound;

        for (IDL_LONG i1=0; i1<n1; i1++) {

            time_t t0 = clock();      

            // Declare the domain and the lists
            SpatialDomain domain;    // initialize empty domain
            ValVec<uint64> plist, flist;	// List results

            vector <uint32> idList;
            int32 idCount=0;

            // Find the triangles around this object

            if (nangle > 1) {
                angle = angleinput[i1];
                d = cos( angle );
            }

            domain.setRaDecD(ra1[i1],dec1[i1],d); //put in ra,dec,d E.S.S.

            domain.intersect(&index,plist,flist);	  // intersect with list


            nFound = flist.length() + plist.length();

            // Save the result in idlist. This is not a bottleneck
            idList.resize(nFound);

            // ----------- FULL NODES -------------
            for(size_t i = 0; i < flist.length(); i++)
            {  
                idList[idCount] = (uint32 )flist(i);
                idCount++;
            }
            // ----------- Partial Nodes ----------
            for(size_t i = 0; i < plist.length(); i++)
            {  
                idList[idCount] = (uint32 )plist(i);
                idCount++;
            }

            tleaves += clock()-t0;

            // Loop over the idList and check objects in those leaf ids

            t0 = clock();

            // these are temporary vectors to hold matches to this point
            std::vector<PAIR_INFO> pair_info;

            IDL_MEMINT nKeep = 0;
            for (IDL_MEMINT j=0; j<nFound; j++) {

                int32 leafId = idList[j];

                // Make sure leaf is in list for ra2,dec2
                if ( leafId >= minLeafId && leafId <= maxLeafId) {

                    int32 leafBin = idList[j] - minLeafId;

                    // Any found in this leaf?
                    if ( htmrev2[leafBin] != htmrev2[leafBin+1] ) {

                        // Now loop over the sources
                        int32 nLeafBin = htmrev2[leafBin+1] - htmrev2[leafBin];

                        for (int32 ileaf=0; ileaf<nLeafBin;ileaf++) {

                            IDL_LONG i2 = htmrev2[ htmrev2[leafBin] + ileaf ];

                            // Returns distance in radians
                            float64 dis = gcirc(ra1[i1], dec1[i1], ra2[i2], dec2[i2]);

                            // Turns out, this pushing is not a bottleneck!
                            // Time is negligible compared to the leaf finding
                            // and the gcirc.
                            if (dis <= angle) {

                                PAIR_INFO pi;
                                pi.i1 = i1;
                                pi.i2 = i2;
                                pi.d12 = dis;
                                pair_info.push_back(pi);

                                /*
                                if (kw.file_there) 
                                {
                                    fwrite(&i1, sizeof(IDL_LONG), 1, fptr);
                                    fwrite(&i2, sizeof(IDL_LONG), 1, fptr);
                                    fwrite(&dis, sizeof(float64), 1, fptr);
                                }
                                else
                                {
                                    m1.push_back(i1);
                                    m2.push_back(i2);
                                    d12.push_back(dis);
                                }
                                nKeep += 1;
                                */
                            } // Within distance 

                        } // loop over objects in leaf 

                    } // any in leaf?

                } // leaf id in list 2?
            } // loop over leaves

            ntotal += nKeep;
            tother += clock()-t0;


            IDL_MEMINT nkeep = pair_info.size();
            if ( nkeep > 0 ) {

                // Sort the result by distance
                std::sort( 
                        pair_info.begin(), 
                        pair_info.end(), 
                        PAIR_INFO_ORDERING());

                if ((maxmatch > 0) ) {
                    // setting maxmatch to zero is same as "keep all matches"
                    if (nkeep > maxmatch) {
                        nkeep=maxmatch;
                    }
                }
                for (IDL_MEMINT ci=0; ci<nkeep; ci++) {
                    if (fptr) {
                        fwrite(&(pair_info[ci].i1), sizeof(int32), 1, fptr);
                        fwrite(&(pair_info[ci].i2), sizeof(int32), 1, fptr);
                        fwrite(&(pair_info[ci].d12), sizeof(double), 1, fptr);
                    } else {
                        m1.push_back(pair_info[ci].i1);
                        m2.push_back(pair_info[ci].i2);
                        d12.push_back(pair_info[ci].d12);
                    }
                    // keep track of the total number actually saved or written
                    ntotal += 1;
                }
            }



        } /* loop over first list */

        IDL_ARRAY_DIM dim;
        IDL_VPTR m1tmp;
        IDL_VPTR m2tmp;
        IDL_VPTR d12tmp;

        // Case where there were no matches
        if (ntotal == 0)
        {
            htmMatchSetStatus(NO_MATCHES);
            if (kw.file_there)
            {
                fclose(fptr);
                return;
            }
        }
        else 
        {

            // We wrote to a file, no results returned
            if (kw.file_there)
            {
                // Write out the number of pairs found and close
                rewind(fptr);
                fwrite(&ntotal, sizeof(IDL_LONG64), 1, fptr);
                fclose(fptr);
                htmMatchSetStatus(SUCCESS);
            }
            else 
            {
                dim[0] = m1.size();
                IDL_LONG *match1 = 
                    (IDL_LONG *) IDL_MakeTempArray(IDL_TYP_LONG, 1, dim, 
                            IDL_ARR_INI_NOP, &m1tmp);
                IDL_LONG *match2 = 
                    (IDL_LONG *) IDL_MakeTempArray(IDL_TYP_LONG, 1, dim, 
                            IDL_ARR_INI_NOP, &m2tmp);
                double *dis12 = 
                    (double *) IDL_MakeTempArray(IDL_TYP_DOUBLE, 1, dim, 
                            IDL_ARR_INI_NOP, &d12tmp);

                for (size_t i=0;i<m1.size();i++) {
                    match1[i] = m1[i];
                    match2[i] = m2[i];
                    dis12[i] = d12[i];
                }

                IDL_VarCopy(m1tmp, match1Vptr);
                IDL_VarCopy(m2tmp, match2Vptr);
                IDL_VarCopy(d12tmp, dis12Vptr);

                htmMatchSetStatus(SUCCESS);
            }
        } // matches found

        return;


    } catch (SpatialException x) {
        char errMessage[100];
        sprintf(errMessage, "%s", x.what());
        htmMatchErrOut(errMessage);
        return;
    }




}
Ejemplo n.º 12
0
int main(int argc, char *argv[])
{

  if (argc < 2)  
    {
      cout << "-Syntax: objshear par_file" << endl;
      return(1);
    }

  char *par_file = argv[1];

  LENS_SOURCE data(par_file);

  /* Copy out pointers to the data */
  lens_struct   *lcat = data.lcat();
  source_pixel_struct *scat = data.scat_pixel();
  source_pixel_struct *tscat;
  int32         *revInd = data.rev();
  PAR_STRUCT    *par_struct = data.par();

  float32 scinv_factor = data.scinv_factor();
  float32 aeta_zero    = data.aeta_zero();
  float32 *aeta_lens   = data.aeta_lens();

  // Need to fill this in
  SCINV_STRUCT scinvStruct;  

  float32 H0 = par_struct->h*100.0;
  float32 omega_m = par_struct->omega_m;

  int32 minLeafId = par_struct->min_htm_index;
  int32 maxLeafId = par_struct->max_htm_index;

  int32 step=500;
  int32 bigStep=10000;

  // Number of lenses
  int32 i;
  int32 nregion=0;
  for (i=0; i<data.nlens();i++)
    if (lcat[i].region == par_struct->region)
      nregion++;
  par_struct->nlens_region = nregion;

  cout << endl <<
    "Number of lenses in this region: " << nregion << endl;
  cout << endl <<
    "Each dot is " << step << " lenses" << endl;

  lensout_struct lensout;
  make_lensout(lensout, par_struct->nbin);

  FILE *fptr = fopen(par_struct->output_file, "w");
  cout << "Writing header for file " << par_struct->output_file << endl;
  write_header(fptr, par_struct);

  try {

    // construct index with given depth and savedepth
    int32 savedepth=2;
    int32 nlens = data.nlens();

    htmInterface htm(par_struct->depth,savedepth);  // generate htm interface
    const SpatialIndex &index = htm.index();

    /* keeping track of those found */
    int64 nFoundTot=0, nFoundUse=0, totalPairs=0, badBin=0;
    float32 sigwsum=0, sigsum=0;

    // Loop over all the lenses

    printf("lens = 0/%d   Mean dens. cont. = 0 +/- 0\n",
	   nregion);
    fflush(stdout);

    int32 lensUsed = 0,lensUsedOld=0;
    for (int32 lensIndex=0; lensIndex < nlens; lensIndex++) 
      {

	lensout.zindex = lcat[lensIndex].zindex;

	if (lcat[lensIndex].region == par_struct->region) 
	  {
	    int16 bad;
	    int16 pixelMaskFlags;
      
	    lensUsed++;

	    pixelMaskFlags = lcat[lensIndex].pixelMaskFlags;

	    bad = pixelMaskFlags & FLAGS_MASKED;


	    // Is the central point masked?  I often check before
	    // making the catalog...
	    if (!bad) 
	      {

		
		// Declare the domain and the lists
		SpatialDomain domain;    // initialize empty domain
		ValVec<uint64> plist, flist;	// List results

		vector <uint32> idList;
		int32 idCount=0;

		static const float 
		  PI=3.1415927,
		  TWOPI=6.2831853,
		  PIOVER2=1.5707963,
		  THREE_PIOVER2=4.7123890;
	    
		float32 
		  zlens, zSource, zSourceErr, 
		  sig_inv, sig_inv2, 
		  sWeight, osWeight, wts_ssh,
		  inverse_wsum, rmin_act, rmax_act;

		float32 comov_fac2, comov_fac4;
		
		float64 
		  ra, dec, clambda, ceta;
		
		float64 d, R, theta, theta2, Rkpc;
		float32 DL;
		float32 adiff_ls, adiff_s;

		int8 radBin;
		
		float64 
		  xrel, yrel, diffsq, xy, 
		  xpysum=0, xmysum=0, xysum=0, ie1, ie2, ie, mm, maxe=0.2, 
		  RkpcInv2, cos2theta, sin2theta,
		  e1prime, e2prime, e1e1err2, e1e2err, e1e2err2, e2e2err2,
		  etan_err2, shear_err2, ortho_err2, orthoshear_err2,
		  denscont, densconterr2, orthodenscont, orthodensconterr2;
		
		float32 tw, f_e, f_sn, k0, k1, F;
		
		int16 bad12, bad23, bad34, bad41;
		
		int32 i, j, n, iSrc, srcUse;
		int32 nFound;
		int32 leafId, leafBin, nLeafBin;
		float32 logRkpc;

		bad12 = pixelMaskFlags & (FLAGS_QUAD1_MASKED+FLAGS_QUAD2_MASKED);
		bad23 = pixelMaskFlags & (FLAGS_QUAD2_MASKED+FLAGS_QUAD3_MASKED);  
		bad34 = pixelMaskFlags & (FLAGS_QUAD3_MASKED+FLAGS_QUAD4_MASKED);
		bad41 = pixelMaskFlags & (FLAGS_QUAD4_MASKED+FLAGS_QUAD1_MASKED);

		// extract some shorthand 
		ra      = lcat[lensIndex].ra;
		dec     = lcat[lensIndex].dec;
		clambda = lcat[lensIndex].clambda;
		ceta    = lcat[lensIndex].ceta;

		zlens = lcat[lensIndex].z;

		d = cos( lcat[lensIndex].angmax*D2R );

		domain.setRaDecD(ra,dec,d); //put in ra,dec,d E.S.S.

		domain.intersect(&index,plist,flist);	  // intersect with list
    
		
		nFound = flist.length() + plist.length();
		nFoundTot += nFound;
    
		// Save the result in idlist. This is not a bottleneck
		idList.resize(nFound);

		// ----------- FULL NODES -------------
		for(i = 0; i < flist.length(); i++)
		  {  
		    idList[idCount] = (uint32 )flist(i);
		    idCount++;
		  }
		// ----------- Partial Nodes ----------
		for(i = 0; i < plist.length(); i++)
		  {  
		    idList[idCount] = (uint32 )plist(i);
		    idCount++;
		  }
		
      
		// Now loop over leaf ids
		for(i=0; i<nFound;i++)
		  {

		    
		    leafId = idList[i];

		    // Convert leafid into bin number
		    leafBin = idList[i] - minLeafId;

		    
		    // Check if there are sources in this leafid
		    if ( leafId >= minLeafId  &&  leafId <= maxLeafId)
		      {


			// Does this triangle have data in or catalog?
			if (revInd[leafBin] != revInd[leafBin+1]) 
			  {

			    
			    nFoundUse+=1;

			    srcUse = revInd[ revInd[leafBin]+iSrc ];

			    tscat = &scat[srcUse];

			    // Distance and angle to source
			    // *** This is a major bottleneck ***
			    //gcircSurvey(clambda, ceta, 
			    //	    tscat->clambda, tscat->ceta,
			    //	    R, theta);
			    gcircSurvey2(clambda, ceta, 
					 tscat->clambda, tscat->ceta,
					 R, theta);
				
			    // kpc
			    DL = lcat[lensIndex].DL;
			    Rkpc = R*DL;
			    
			    // Mpc
			    DL = DL/1000.0;
			    
			    // convert to comoving?
			    if (par_struct->comoving) 
			      {
				comov_fac2 = 1./pow(1+zlens, 2);
				comov_fac4 = 1./pow(1+zlens, 4);
				Rkpc = Rkpc*(1+zlens);
			      }

			    // Within our circular radius?
			    if (Rkpc >= par_struct->rmin && Rkpc <= par_struct->rmax)
			      {
				
				// Is this source in one of the quadrants that is 
				// useful for this lens?
				theta2 = PIOVER2 - theta;
				if (objShearTestQuad(bad12,bad23,bad34,bad41,theta2))
				  {
					
				    // What kind of binning?
				    if (par_struct->logbin)
				      {
					logRkpc = log10(Rkpc);
					radBin = (int8) ( (logRkpc-par_struct->logRmin)/par_struct->logBinsize );
				      }
				    else 
				      {
					radBin = (int8) ( (Rkpc-par_struct->rmin)/par_struct->binsize );
				      }

					
				    // valid bin number?
				    if (radBin >= 0 && radBin < par_struct->nbin)
				      {

					  
					// Mean inverse critical density for this pixel/lens
					sig_inv = 
					  scinv_factor*DL*(aeta_lens[lensIndex]*tscat->aeta1 - tscat->aeta2);
					    
					if (sig_inv > 0.0) 
					  {
					    
					    totalPairs +=1;
					    
					    sig_inv2 = sig_inv*sig_inv;
					    
					    // X/Y positions
					    xrel = Rkpc*cos(theta);
					    yrel = Rkpc*sin(theta);
					    
					    // eta is flipped
					    diffsq = xrel*xrel - yrel*yrel;
					    xy = xrel*yrel;
					    
					    RkpcInv2 = 1.0/Rkpc/Rkpc;
					    
					    cos2theta = diffsq*RkpcInv2;
					    sin2theta = 2.0*xy*RkpcInv2;
				    
					    // Tangential/45-degree rotated ellipticities
					    e1prime = -(tscat->e1*cos2theta + tscat->e2*sin2theta);
					    e2prime =  (tscat->e1*sin2theta - tscat->e2*cos2theta);



					    // covariance
					    e1e2err = tscat->e1e2err;
					    e1e2err2 = e1e2err*e1e2err;
					    if (e1e2err < 0) e1e2err2 = -e1e2err2;
				    
					    e1e1err2 = 
					      tscat->e1e1err*tscat->e1e1err;

					    e2e2err2 = 
					      tscat->e2e2err*tscat->e2e2err;


					    // Errors in tangential/ortho
					    etan_err2 = 
					      e1e1err2*cos2theta*cos2theta + e2e2err2*sin2theta*sin2theta - 
					      2.0*cos2theta*sin2theta*e1e2err2; 

					    shear_err2 = 0.25*(etan_err2 + SHAPENOISE2);

					    ortho_err2 = 
					      e1e1err2*sin2theta*sin2theta + e2e2err2*cos2theta*cos2theta - 
					      2.0*cos2theta*sin2theta*e1e2err2; 
				    
					    orthoshear_err2 = 0.25*(ortho_err2 + SHAPENOISE2);
				    
					    // density contrast
					    denscont = e1prime/2.0/sig_inv;
					    densconterr2 = shear_err2/sig_inv2;
				    
					    orthodenscont = e2prime/2.0/sig_inv;
					    orthodensconterr2 = orthoshear_err2/sig_inv2;

					    if (par_struct->comoving) 
					      {
						denscont *= comov_fac2;
						densconterr2 *= comov_fac4;
						
						orthodenscont *= comov_fac2;
						orthodensconterr2 *= comov_fac4;
					      }

					    sWeight = 1./densconterr2;
					    osWeight = 1./orthodensconterr2;

					    tw = 1./(etan_err2 + SHAPENOISE2);
					    f_e = etan_err2*tw;
					    f_sn = SHAPENOISE2*tw;
				    
					    // coefficients (p 596 Bern02) 
					    // there is a k1*e^2/2 in Bern02 because
					    // its the total ellipticity he is using

					    wts_ssh = sWeight;
					    k0 = f_e*SHAPENOISE2;
					    k1 = f_sn*f_sn;
					    F = 1. - k0 - k1*e1prime*e1prime;
				
					    // keep running totals of positions for
					    // ellipticity of source distribution
				
					    xpysum += xrel*xrel + yrel*yrel;
					    xmysum += diffsq;
					    xysum  += xy;
				
					    ////////////////////////////////////////
					    // Fill in the lens structure
					    ////////////////////////////////////////
					    
					    lensout.tot_pairs += 1;
					    lensout.npair[radBin] +=1;
					    
					    rmax_act = 
					      lensout.rmax_act[radBin];
					    rmin_act =
					      lensout.rmin_act[radBin];
					    
					    if (rmax_act == 0.0) {
					      rmax_act=Rkpc;
					    } else {
					      rmax_act = max(rmax_act, (float32) Rkpc);
					    }
					    if (rmin_act == 0.0) {
					      rmin_act=Rkpc;
					    } else {
					      rmin_act = min(rmin_act, (float32) Rkpc);
					    }
				
					    lensout.rmax_act[radBin] = rmax_act;
					    lensout.rmin_act[radBin] = rmin_act;
					    
					    // these are initally sums, then converted to
					    // means later by dividing by wsum
				
					    lensout.rsum[radBin]     += Rkpc;
					    
					    lensout.sigma[radBin]    += sWeight*denscont;
					    lensout.orthosig[radBin] += osWeight*orthodenscont;

					    lensout.weight        += sWeight;
					    lensout.wsum[radBin]  += sWeight;
					    lensout.owsum[radBin] += osWeight;
				
					    lensout.sigerrsum[radBin] += 
					      sWeight*sWeight*denscont*denscont;
					    lensout.orthosigerrsum[radBin] += 
					      osWeight*osWeight*orthodenscont*orthodenscont;
				
					    lensout.sshsum   += wts_ssh*F;
					    lensout.wsum_ssh += wts_ssh;
					    
					  } // Good sig_inv?
					    
				      } 
				    else // good radBin? 
				      {
					badBin += 1;
				      }
					
				  } // Good quadrant?
				    
			      } // radius within circular radius min/max?

			  } // any sources found in this leaf?

		      } // leaf id is in allowed range?

		  } // loop over found leaf ids


		/////////////////////////////////////////////////////////
		// Now compute averages if there were any sources for
		// this lens, i.e. weight > 0
		/////////////////////////////////////////////////////////

		if (lensout.weight > 0.0) 
		  {
	  
		    ie1 = xmysum/xpysum;
		    ie2 = 2.*xysum/xpysum;
		    ie = sqrt( ie1*ie1 + ie2*ie2 );
		    lensout.ie = ie;

		    mm = 3.0/sqrt(lensout.tot_pairs);

		    for(radBin=0; radBin<par_struct->nbin; radBin++)
		      {

			if (lensout.wsum[radBin] > 0.0) 
			  {

			    // Keep track for "good" lenses
			    if (ie < max(mm, maxe)) 
			      {
				sigwsum += lensout.wsum[radBin];
				sigsum  += lensout.sigma[radBin];
			      }

			    inverse_wsum = 1.0/( lensout.wsum[radBin] );
		  
			    lensout.sigma[radBin] *= inverse_wsum;
			    lensout.sigmaerr[radBin] = sqrt(inverse_wsum);

			  }
			if (lensout.owsum[radBin] > 0.0) 
			  {
			    inverse_wsum = 1.0/( lensout.owsum[radBin] );
		  
			    lensout.orthosig[radBin] *= inverse_wsum;
			    lensout.orthosigerr[radBin] = sqrt(inverse_wsum);
			  }
			
		      }
		    
		  } // weight > 0.0?
			     
	      } // central point unmasked?
	
	    // need to write all in region since we set NROWS = nregion
	    // For large angles, all lenses have weight > 0 but for smaller
	    // radii this may write zeroes
	    write_lensout(fptr, lensout);

	  } // in region?

	reset_lensout(lensout);

	/*
	if ( (lensIndex % step) == 0 && (lensIndex != 0)) 
	  {
	    printf(".");
	    if ( (lensIndex % bigStep) == 0)
	      objShearPrintMeans(lensUsed,nregion,sigsum,sigwsum);
	    fflush(stdout);
	  }
	*/
	if ( (lensUsed % step) == 0 && (lensUsed != 0) && 
	     (lensUsed != lensUsedOld) ) 
	  {
	    printf(".");
	    if ( (lensUsed % bigStep) == 0)
	      objShearPrintMeans(lensUsed,nregion,sigsum,sigwsum);
	    fflush(stdout);
	    lensUsedOld = lensUsed;
	  }

      } // loop over lenses 

  
  
  } catch (SpatialException &x) {
    printf("%s\n",x.what());
  }



  fclose(fptr);

  cout << endl << "Done" << endl;

  return(0);
}