示例#1
0
void read_red_dist(void)
{
  //////
  // Reads redshift distribution and creates
  // array for interpolation
  FILE *fdist;
  double *zarr,*dndz_dist_arr;
  int n_z_dist;
  int ii;

  fdist=fopen(fnamedNdz,"r");
  if(fdist==NULL) error_open_file(fnamedNdz);
  print_info("*** Reading redshift selection function ");
#ifdef _VERBOSE
  print_info("from file %s",fnamedNdz);
#endif //_VERBOSE
  print_info("\n");

  n_z_dist=linecount(fdist);
  rewind(fdist);
  
  dndz_dist_arr=(double *)my_malloc(n_z_dist*sizeof(double));
  zarr=(double *)my_malloc(n_z_dist*sizeof(double));

  for(ii=0;ii<n_z_dist;ii++) {
    int sr=fscanf(fdist,"%lf %lf",&(zarr[ii]),&(dndz_dist_arr[ii]));
    if(sr!=2) error_read_line(fnamedNdz,ii+1);
    if(dndz_dist_arr[ii]>=dist_max)
      dist_max=dndz_dist_arr[ii];
  }
  fclose(fdist);
  dist_max*=1.1;

  dndz_set=1;

#ifdef _DEBUG
  char dfname[64]="debug_dndz.dat";
  fdist=fopen(dfname,"w");
  if(fdist==NULL) error_open_file(dfname);
  for(ii=0;ii<n_z_dist;ii++)
    fprintf(fdist,"%lf %lf \n",zarr[ii],dndz_dist_arr[ii]);
  fclose(fdist);
#endif //_DEBUG

  redshift_0=zarr[0];
  redshift_f=zarr[n_z_dist-1];

  cute_intacc_dndz=gsl_interp_accel_alloc();
  cute_spline_dndz=gsl_spline_alloc(gsl_interp_cspline,n_z_dist);
  gsl_spline_init(cute_spline_dndz,zarr,dndz_dist_arr,n_z_dist);

  free(zarr);
  free(dndz_dist_arr);
  
  print_info("\n");
}
示例#2
0
文件: io_fg.c 项目: slosar/CRIME
void read_nutable(char *fname, ParamsForGet *pars)
{
  int ii,nnu;
  pars->nutable=(double **)my_malloc(3*sizeof(double *));
  FILE *fin=fopen(fname,"r");
  if(fin==NULL) error_open_file(fname);
  nnu=linecount(fin);
  rewind(fin);

  pars->n_nu=nnu;
  /* We'll just allocate one extra slot in case we need if for
     Haslam freq */
  for(ii=0;ii<3;ii++)
    pars->nutable[ii]=(double *)my_malloc((nnu+1)*sizeof(double)); 

  // only add haslam freq if doing galaxy
  pars->haslam_nu_added=pars->do_galaxy;
  for(ii=0;ii<nnu;ii++) {
    int inu;
    double nu0,nuf,dum;
    int stat=fscanf(fin,"%d %lf %lf %lf %lf\n",
		    &inu,&nu0,&nuf,&dum,&dum);
    if(stat!=5) error_read_line(fname,ii+1);
    pars->nutable[0][ii]=0.5*(nu0+nuf);
    pars->nutable[1][ii]=nu0;
    pars->nutable[2][ii]=nuf;
    if((NU_HASLAM<=nuf)&&(NU_HASLAM>nu0)) pars->haslam_nu_added=0;
  }
  if (pars->haslam_nu_added) {
    pars->n_nu++;
    pars->nutable[0][ii]=NU_HASLAM;
    pars->nutable[1][ii]=NU_HASLAM-0.5; /* dummy freq differences */
    pars->nutable[2][ii]=NU_HASLAM+0.5;
  }
  
  fclose(fin);
}
示例#3
0
void read_mask(void)
{
  //////
  // Reads mask file and loads mask info
  FILE *fmask;
  int ii;
  
  print_info("*** Reading mask ");
#ifdef _VERBOSE
  print_info("from file %s\n",fnameMask);
#endif //_VERBOSE
  fmask=fopen(fnameMask,"r");
  if(fmask==NULL) {
    fprintf(stderr,"\n");
    error_open_file(fnameMask);
  }
  n_mask_regions=linecount(fmask);
#ifdef _VERBOSE
  print_info("  There are %d mask regions\n",n_mask_regions);
#endif //_VERBOSE
  rewind(fmask);
  
  mask=my_malloc(sizeof(MaskRegion)*n_mask_regions);
#ifdef _VERBOSE
  print_info("  Mask is: \n");
  print_info("  (z0,zf), (cth0,cthf), (phi0,phif)\n");
#endif //_VERBOSE
  for(ii=0;ii<n_mask_regions;ii++) {
    int sr;
    sr=fscanf(fmask,"%lf %lf %lf %lf %lf %lf",
	      &((mask[ii]).z0),&((mask[ii]).zf),
	      &((mask[ii]).cth0),&((mask[ii]).cthf),
	      &((mask[ii]).phi0),&((mask[ii]).phif));
    if(sr!=6) error_read_line(fnameMask,ii);
    if((fabs(mask[ii].cth0)>1)||(fabs(mask[ii].cthf)>1)||
       (fabs(mask[ii].phi0)>2*M_PI)||(fabs(mask[ii].phif)>2*M_PI)||
       (mask[ii].z0<0)||(mask[ii].zf<0)||
       (mask[ii].z0>RED_COSMO_MAX)||(mask[ii].zf>RED_COSMO_MAX)) {
      fprintf(stderr,"CUTE: Wrong mask region: %lf %lf %lf %lf %lf %lf \n",
	      mask[ii].z0,mask[ii].zf,mask[ii].cth0,
	      mask[ii].cthf,mask[ii].phi0,mask[ii].phif);
    }
#ifdef _VERBOSE
    print_info("  (%.3lf,%.3lf), (%.3lf,%.3lf), (%.3lf,%.3lf)\n",
	   (mask[ii]).z0,(mask[ii]).zf,
	   (mask[ii]).cth0,(mask[ii]).cthf,
	   (mask[ii]).phi0,(mask[ii]).phif);
#endif //_VERBOSE
  }
  fclose(fmask);

  //Determine mask boundaries
  red_min_mask=(mask[0]).z0;
  red_max_mask=(mask[0]).zf;
  cth_min_mask=(mask[0]).cth0;
  cth_max_mask=(mask[0]).cthf;
  phi_min_mask=(mask[0]).phi0;
  phi_max_mask=(mask[0]).phif;
  for(ii=0;ii<n_mask_regions;ii++) {
    if((mask[ii]).z0<=red_min_mask)
      red_min_mask=(mask[ii]).z0;
    if((mask[ii]).zf>=red_max_mask)
      red_max_mask=(mask[ii]).zf;
    if((mask[ii]).cth0<=cth_min_mask)
      cth_min_mask=(mask[ii]).cth0;
    if((mask[ii]).cthf>=cth_max_mask)
	cth_max_mask=(mask[ii]).cthf;
    if((mask[ii]).phi0<=phi_min_mask)
      phi_min_mask=(mask[ii]).phi0;
    if((mask[ii]).phif>=phi_max_mask)
      phi_max_mask=(mask[ii]).phif;
  }
#ifdef _VERBOSE
  print_info("  Mask absolute limits: \n");
  print_info("  (%.3lf,%.3lf), (%.3lf,%.3lf), (%.3lf,%.3lf)\n",
	 red_min_mask,red_max_mask,cth_min_mask,cth_max_mask,
	 phi_min_mask,phi_max_mask);
#endif //_VERBOSE

  //increase boundaries by 0.2% for safety
  red_min_mask-=0.001*(red_max_mask-red_min_mask);
  cth_min_mask-=0.001*(cth_max_mask-cth_min_mask);
  phi_min_mask-=0.001*(phi_max_mask-phi_min_mask);
  red_max_mask+=0.001*(red_max_mask-red_min_mask);
  cth_max_mask+=0.001*(cth_max_mask-cth_min_mask);
  phi_max_mask+=0.001*(phi_max_mask-phi_min_mask);

  mask_set=1;
  print_info("\n");
}
示例#4
0
文件: io.c 项目: flaviasobreira/CUTE
Catalog_f read_catalog_f(char *fname,int *np)
{
  //////
  // Creates catalog from file fname
  FILE *fd;
  int ng;
  int ii;
  double z_mean=0;
  Catalog_f 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;
  *np=ng;
  rewind(fd);

  //Allocate catalog memory
  cat.np=ng;
  cat.pos=(float *)my_malloc(3*cat.np*sizeof(float));

  rewind(fd);
  //Read galaxies in mask
  int i_dat=0;
  for(ii=0;ii<ng;ii++) {
    double zz,cth,phi,rr,sth,dum_weight;
    int st=read_line(fd,&zz,&cth,&phi,&dum_weight);
    if(st) error_read_line(fname,ii+1);
    z_mean+=zz;
    
    sth=sqrt(1-cth*cth);
    if(corr_type!=1)
      rr=z2r(zz);
    else
      rr=1;

    cat.pos[3*i_dat]=(float)(rr*sth*cos(phi));
    cat.pos[3*i_dat+1]=(float)(rr*sth*sin(phi));
    cat.pos[3*i_dat+2]=(float)(rr*cth);
    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

  print_info("\n");
  return cat;
}
示例#5
0
文件: io.c 项目: flaviasobreira/CUTE
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;
}
示例#6
0
文件: io.c 项目: flaviasobreira/CUTE
void read_run_params(char *fname)
{
  //////
  // Reads and checks the parameter file
  FILE *fi;
  int n_lin,ii;
  char estim[64]="none";
  Binner binner;
  binner.dim1_max=-1;
  binner.dim2_max=-1;
  binner.dim3_min=-1;
  binner.dim3_max=-1;
  binner.dim1_nbin=-1;
  binner.dim1_nbin=-1;
  binner.dim1_nbin=-1;
  binner.logbin=-1;
  binner.n_logint=-1;

  print_info("*** Reading run parameters \n");

  //Read parameters from file
  fi=fopen(fname,"r");
  if(fi==NULL) error_open_file(fname);
  n_lin=linecount(fi);
  rewind(fi);
  for(ii=0;ii<n_lin;ii++) {
    char s0[512],s1[64],s2[256];
    if(fgets(s0,sizeof(s0),fi)==NULL)
      error_read_line(fname,ii+1);
    if((s0[0]=='#')||(s0[0]=='\n')) continue;
    int sr=sscanf(s0,"%s %s",s1,s2);
    if(sr!=2)
      error_read_line(fname,ii+1);

    if(!strcmp(s1,"data_filename="))
      sprintf(fnameData,"%s",s2);
    else if(!strcmp(s1,"random_filename="))
      sprintf(fnameRandom,"%s",s2);
    else if(!strcmp(s1,"num_lines=")) {
      if(!strcmp(s2,"all"))
	n_objects=-1;
      else
	n_objects=atoi(s2);
    }
    else if(!strcmp(s1,"input_format="))
      input_format=atoi(s2);
    else if(!strcmp(s1,"output_filename="))
      sprintf(fnameOut,"%s",s2);
    else if(!strcmp(s1,"mask_filename="))
      sprintf(fnameMask,"%s",s2);
    else if(!strcmp(s1,"z_dist_filename="))
      sprintf(fnamedNdz,"%s",s2);
    else if(!strcmp(s1,"corr_estimator=")) {
      sprintf(estim,"%s",s2);
      if(!strcmp(estim,"PH"))
	estimator=0;
      else if(!strcmp(estim,"DP"))
	estimator=1;
      else if(!strcmp(estim,"HAM"))
	estimator=2;
      else if(!strcmp(estim,"LS"))
	estimator=3;
      else if(!strcmp(estim,"HEW"))
	estimator=4;
      else {
	fprintf(stderr,"CUTE: Unknown estimator %s, using 'LS'\n",estim);
	estimator=3;
      }
    }
    else if(!strcmp(s1,"corr_type=")) {
      if(!strcmp(s2,"radial")) corr_type=0;
      else if(!strcmp(s2,"angular")) corr_type=1;
      else if(!strcmp(s2,"monopole")) corr_type=2;
      else if(!strcmp(s2,"3D_ps")) corr_type=3;
      else if(!strcmp(s2,"3D_rm")) corr_type=4;
      else if(!strcmp(s2,"full")) corr_type=5;
      else if(!strcmp(s2,"angular_cross")) corr_type=6;
      else {
	fprintf(stderr,"CUTE: wrong corr type %s.",s2);
	fprintf(stderr," Possible types are \"radial\", \"angular\", \"full\",");
	fprintf(stderr," \"monopole\", \"3D_ps\" and \"3D_rm\".\n");
      }
    }
    else if(!strcmp(s1,"np_rand_fact="))
      fact_n_rand=atoi(s2);
    else if(!strcmp(s1,"omega_M="))
      omega_M=atof(s2);
    else if(!strcmp(s1,"omega_L="))
      omega_L=atof(s2);
    else if(!strcmp(s1,"w="))
      weos=atof(s2);
    else if(!strcmp(s1,"radial_aperture="))
      aperture_los=atof(s2)*DTORAD;
    else if(!strcmp(s1,"dim1_max="))
      binner.dim1_max=atof(s2);
    else if(!strcmp(s1,"dim2_max="))
      binner.dim2_max=atof(s2);
    else if(!strcmp(s1,"dim3_max="))
      binner.dim3_max=atof(s2);
    else if(!strcmp(s1,"dim3_min="))
      binner.dim3_min=atof(s2);
    else if(!strcmp(s1,"dim1_nbin="))
      binner.dim1_nbin=atoi(s2);
    else if(!strcmp(s1,"dim2_nbin="))
      binner.dim2_nbin=atoi(s2);
    else if(!strcmp(s1,"dim3_nbin="))
      binner.dim3_nbin=atoi(s2);
    else if(!strcmp(s1,"log_bin="))
      binner.logbin=atoi(s2);
    else if(!strcmp(s1,"n_logint="))
      binner.n_logint=atoi(s2);
    else if(!strcmp(s1,"use_pm="))
      use_pm=atoi(s2);
    else if(!strcmp(s1,"n_pix_sph=")) {
      n_side_cth=atoi(s2);
      n_side_phi=2*n_side_cth;
    }
    else
      fprintf(stderr,"CUTE: Unknown parameter %s\n",s1);
  }
  fclose(fi);

  process_binner(binner);

  check_params();

#ifdef _VERBOSE
  print_info("  Using estimator: %s\n",estim);
  if(gen_ran) {
    print_info("  The random catalog will be generated ");
    if(fact_n_rand==1)
      print_info("with as many particles as in the data \n");
    else
      print_info("with %d times more particles than the data \n",fact_n_rand);
  }
#endif //_VERBOSE

  print_info("\n");
}
示例#7
0
文件: io_fg.c 项目: slosar/CRIME
ParamsForGet *read_input_params_ForGet(char *fname)
{
  FILE *fi;
  int nlin,ii;
  int seed_signed=-1;
  ParamsForGet *pars=set_default_params_ForGet();
  
  printf("*** Reading run parameters\n");
  sprintf(pars->fname_input,"%s",fname);

  fi=fopen(fname,"r");
  if(fi==NULL) error_open_file(fname);
  nlin=linecount(fi);
  rewind(fi);
  for(ii=0;ii<nlin;ii++) {
    char s0[512],s1[64],s2[256];
    if(fgets(s0,sizeof(s0),fi)==NULL)
      error_read_line(fname,ii+1);
    if((s0[0]=='#')||(s0[0]=='\n')) continue;
    int sr=sscanf(s0,"%s %s",s1,s2);
    if(sr!=2)
      error_read_line(fname,ii+1);

    if(!strcmp(s1,"fname_nutable=")) {
      sprintf(pars->fname_nutable,"%s",s2);
      printf("  fname_nutable = %s\n",pars->fname_nutable);
    }
    else if(!strcmp(s1,"fname_haslam=")) {
      sprintf(pars->fname_haslam,"%s",s2);
      printf("  fname_haslam = %s\n",pars->fname_haslam);
    }
    else if(!strcmp(s1,"fname_specin=")) {
      sprintf(pars->fname_specin,"%s",s2);
      printf("  fname_specin = %s\n",pars->fname_specin);
    }
    else if(!strcmp(s1,"prefix_out=")) {
      sprintf(pars->prefix_out,"%s",s2);
      printf("  prefix_out = %s\n",pars->prefix_out);
    }
    else if(!strcmp(s1,"lmin=")) {
      pars->lmin=atoi(s2);
      printf("  lmin = %d\n",pars->lmin);
    }
    else if(!strcmp(s1,"lmax=")) {
      pars->lmax=atoi(s2);
      printf("  lmax = %d\n",pars->lmax);
    }
    else if(!strcmp(s1,"nside=")) {
      pars->nside=atoi(s2);
      printf("  nside = %d\n",pars->nside);
    }
    else if(!strcmp(s1,"seed="))
      seed_signed=atoi(s2);
    else if(!strcmp(s1,"cl_model="))
      sprintf(pars->cl_model,"%s",s2);
    else if(!strcmp(s1,"amp="))
      pars->amp=atof(s2);
    else if(!strcmp(s1,"beta="))
      pars->beta=atof(s2);
    else if(!strcmp(s1,"alpha="))
      pars->alpha=atof(s2);
    else if(!strcmp(s1,"xi="))
      pars->xi=atof(s2);
    else if(!strcmp(s1,"nu_ref="))
      pars->nu_ref=atof(s2);
    else if(!strcmp(s1,"lref="))
      pars->lref=atoi(s2);
    else if(!strcmp(s1,"do_polarization="))
      pars->do_polarization=atoi(s2);
    else if(!strcmp(s1,"xi_polarization="))
      pars->xi_polarization=atof(s2);
    else if(!strcmp(s1,"beta_polarization="))
      pars->beta_polarization=atof(s2);
    else if(!strcmp(s1,"fname_faraday=")) {
      sprintf(pars->fname_faraday,"%s",s2);
      printf("  fname_faraday = %s\n",pars->fname_faraday);
    }
    else {
      fprintf(stderr,"CRIME: unknown param %s in line %d\n",
	      s1,ii+1);
    }
  }

  set_cl_model(pars);

  if(seed_signed<=0) pars->seed=time(NULL);
  else pars->seed=(unsigned int)seed_signed;
  printf("  seed = %u\n",pars->seed);
  
  read_nutable(pars->fname_nutable, pars);
  if(pars->nside<=0) {
    fprintf(stderr,"CRIME: Wrong nside = %d\n",pars->nside);
    exit(1);
  }

  printf("\n");
  return pars;
}