Catalog make_cat(double *ras, double *decs, double *zs, double *ws, int ngals, np_t *sum_w, np_t *sum_w2) { /* * Makes a CUTE catalogue with the data */ int ii; Catalog cat; cat.np=ngals; cat.red=(double *)my_malloc(cat.np*sizeof(double)); cat.cth=(double *)my_malloc(cat.np*sizeof(double)); cat.phi=(double *)my_malloc(cat.np*sizeof(double)); #ifdef _WITH_WEIGHTS cat.weight=(double *)my_malloc(cat.np*sizeof(double)); #endif //_WITH_WEIGHTS //Read galaxies in mask *sum_w=0; *sum_w2=0; for(ii=0;ii<ngals;ii++) { double zz,cth,phi,weight; zz = zs[ii]; if(zz<0) { fprintf(stderr,"Wrong redshift = %lf %d\n",zz,ii+1); exit(1); } cth = cos(DTORAD*(90-ras[ii])); if((cth>1)||(cth<-1)) { fprintf(stderr,"Wrong cos(theta) = %lf %d\n",cth,ii+1); exit(1); } phi=DTORAD*decs[ii]; phi=wrap_phi(phi); weight = ws[ii]; cat.red[ii]=zz; cat.cth[ii]=cth; cat.phi[ii]=phi; #ifdef _WITH_WEIGHTS cat.weight[ii]=weight; (*sum_w)+=weight; (*sum_w2)+=weight*weight; #else //_WITH_WEIGHTS (*sum_w)++; (*sum_w2)++; #endif //_WITH_WEIGHTS } printf(" Effective n. of particles: %lf\n",(*sum_w)); printf("\n"); return cat; }
Catalog read_catalog(char *fname,np_t *sum_w,np_t *sum_w2) { ////// // Creates catalog from file fname FILE *fd; int ng; int ii; double z_mean=0; Catalog cat; print_info("*** Reading catalog "); #ifdef _VERBOSE print_info("from file %s",fname); #endif print_info("\n"); //Open file and count lines fd=fopen(fname,"r"); if(fd==NULL) error_open_file(fname); if(n_objects==-1) ng=linecount(fd); else ng=n_objects; rewind(fd); print_info(" %d lines in the catalog\n",ng); //Allocate catalog memory cat.np=ng; cat.red=(double *)my_malloc(cat.np*sizeof(double)); cat.cth=(double *)my_malloc(cat.np*sizeof(double)); cat.phi=(double *)my_malloc(cat.np*sizeof(double)); #ifdef _WITH_WEIGHTS cat.weight=(double *)my_malloc(cat.np*sizeof(double)); #endif //_WITH_WEIGHTS rewind(fd); //Read galaxies in mask int i_dat=0; *sum_w=0; *sum_w2=0; for(ii=0;ii<ng;ii++) { double zz,cth,phi,weight; int st=read_line(fd,&zz,&cth,&phi,&weight); if(st) error_read_line(fname,ii+1); z_mean+=zz; if(zz<0) { fprintf(stderr,"Wrong redshift = %lf %d\n",zz,ii+1); exit(1); } if((cth>1)||(cth<-1)) { fprintf(stderr,"Wrong cos(theta) = %lf %d\n",cth,ii+1); exit(1); } phi=wrap_phi(phi); cat.red[i_dat]=zz; cat.cth[i_dat]=cth; cat.phi[i_dat]=phi; #ifdef _WITH_WEIGHTS cat.weight[i_dat]=weight; (*sum_w)+=weight; (*sum_w2)+=weight*weight; #else //_WITH_WEIGHTS (*sum_w)++; (*sum_w2)++; #endif //_WITH_WEIGHTS i_dat++; } fclose(fd); if(i_dat!=ng) { fprintf(stderr,"CUTE: Something went wrong !!\n"); exit(1); } z_mean/=ng; #ifdef _VERBOSE print_info(" The average redshift is %lf\n",z_mean); #endif //_VERBOSE #ifdef _WITH_WEIGHTS print_info(" Effective n. of particles: %lf\n",(*sum_w)); #else //_WITH_WEIGHTS print_info(" Total n. of particles read: %d\n",(*sum_w)); #endif //_WITH_WEIGHTS print_info("\n"); return cat; }
static int read_line(FILE *fi,double *zz,double *cth, double *phi,double *weight) { ////// // Reads source positions and weight in each line double x0,x1,x2; char s0[1024]; int sr; if(fgets(s0,sizeof(s0),fi)==NULL) return 1; #ifdef _WITH_WEIGHTS double x3; sr=sscanf(s0,"%lf %lf %lf %lf",&x0,&x1,&x2,&x3); if(sr!=4) return 1; *weight=x3; #else //_WITH_WEIGHTS sr=sscanf(s0,"%lf %lf %lf",&x0,&x1,&x2); if(sr!=3) return 1; *weight=1; #endif //_WITH_WEIGHTS ////// // Modify here to add other formats // x0, x1, x2 are the first columns // in the data file if(input_format==0) { // z cos(theta) phi if((x1>1)||(x1<-1)) { fprintf(stderr,"CUTE: wrong cos(theta) = %lf \n",x1); return 1; } *zz=x0; *cth=x1; *phi=x2; } else if(input_format==1) { // z dec ra if((x1<-90)||(x1>90)) { fprintf(stderr,"CUTE: wrong declination: %lf \n",x1); return 1; } *zz=x0; *cth=cos(DTORAD*(90-x1)); *phi=DTORAD*x2; } else if(input_format==2) { // ra dec z if((x1<-90)||(x1>90)) { fprintf(stderr,"CUTE: wrong declination: %lf \n",x1); return 1; } *zz=x2; *cth=cos(DTORAD*(90-x1)); *phi=DTORAD*x0; } else if(input_format==3) { // z ra dec if((x2<-90)||(x2>90)) { fprintf(stderr,"CUTE: wrong declination: %lf \n",x1); return 1; } *zz=x0; *cth=cos(DTORAD*(90-x2)); *phi=DTORAD*x1; } else { fprintf(stderr,"CUTE: wrong input format %d \n", input_format); exit(1); } if((*zz)<0) { fprintf(stderr,"Wrong redshift = %lf \n",(*zz)); return 1; } (*phi)=wrap_phi((*phi)); return 0; }