Example #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");
}
Example #2
0
int cmd_fddebug(char *param)
{
	switch(onoffStr(param)) {
  	default: {
  		FILE *f;
  		char *p;

  		if((p = strdup(trimcl(param))) == 0) {
  			error_out_of_memory();
  			return 1;
  		}

  		if(stricmp(param, "stderr") == 0) f = stderr;
  		else if(stricmp(param, "stdout") == 0) f = stdout;
  		else if((f = fopen(param, "at")) == 0) {
  			error_open_file(param);
  			return 2;
  		}
		if(dbg_logfile != stderr && dbg_logfile != stdout)
			fclose(dbg_logfile);
		dbg_logfile = f;
		free(dbg_logname);
		dbg_logname = p;
		/* FALL THROUGH */
	}
  	case OO_On:		fddebug = 1;	break;
	case OO_Null:	case OO_Empty:
		displayString(TEXT_MSG_FDDEBUG_STATE, fddebug ? D_ON : D_OFF);
		displayString(TEXT_MSG_FDDEBUG_TARGET
		 , dbg_logname? dbg_logname: "stdout");
		break;
  	case OO_Off:	fddebug = 0;	break;
	}
  return 0;
}
Example #3
0
void write_grid(double *grid,char *fn)
{
  //////
  // Writes grid into file fn.
  // Only used for debugging
  FILE *fr;
  lint ii;
  double agrid=l_box/n_grid;
  fr=fopen(fn,"w");
  if(fr==NULL) error_open_file(fn);
  for(ii=0;ii<n_grid;ii++) {
    lint jj;
    double x=(ii+0.5)*agrid;
    for(jj=0;jj<n_grid;jj++) {
      lint kk;
      double y=(jj+0.5)*agrid;
      for(kk=0;kk<n_grid;kk++) {
	double z=(kk+0.5)*agrid;
	lint index=kk+n_grid*(jj+n_grid*ii);
	fprintf(fr,"%lf %lf %lf %lf\n",x,y,z,grid[index]);
      }
    }
  }
  fclose(fr);
}
Example #4
0
void write_cat(Catalog cat,char *fn)
{
  //////
  // Writes catalog into file fn.
  // Only used for debugging
  FILE *fr;
  lint ii;
  fr=fopen(fn,"w");
  if(fr==NULL) error_open_file(fn);
  for(ii=0;ii<cat.np;ii++) {
    fprintf(fr,"%lf %lf %lf\n",cat.pos[3*ii],
	    cat.pos[3*ii+1],cat.pos[3*ii+2]);
  }
  fclose(fr);
}
Example #5
0
File: io_fg.c Project: 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);
}
Example #6
0
/*
 * Grab the filename of COMMAND.COM
 *
 *  If warn != 0, warnings can be issued; otherwise this functions
 *  is silent.
 */
void grabComFilename(int warn, char far *fnam)
{
  char *buf;
  size_t len;

  assert(fnam);

  /* Copy the filename into the local heap */
  len = _fstrlen(fnam);
  if(len >= INT_MAX || len < 1) {
    /* no filename specified */
    if(warn)
      error_syntax(NULL);
    return;
  }

  if((buf = malloc(len + 1)) == NULL) {
    if(warn) error_out_of_memory();
    return ;
  }
  _fmemcpy((char far*)buf, fnam, len);
  buf[len] = '\0';

    if (buf[1] != ':' || buf[2] != '\\')
    { char *p;

        /* expand the string for the user */
      p = dfnexpand(buf, NULL);
      free(buf);
      if((buf = p) == NULL) {
		  if(warn) error_out_of_memory();
		  return;
      }
      if(warn)
          error_init_fully_qualified(buf);
    }

    if(dfnstat(buf) & DFN_DIRECTORY) {
      /* The user specified a directory, try if we can find the
        COMMAND.COM with the standard name in there */
      char *p;

      if((p = realloc(buf, len + sizeof(COM_NAME) + 1)) == NULL) {
        if(warn) error_out_of_memory();
        free(buf);
        return;
      }
      buf = p;
      strcpy(&buf[len], "\\" COM_NAME);
    }

    if(!(dfnstat(buf) & DFN_FILE)) {
      /* not found */
      if(warn) error_open_file(buf);
      free(buf);
      return;
    }

  free(ComPath);    /* Save the found file */
  ComPath = buf;
}
Example #7
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");
}
Example #8
0
int grabComFilename(const int warn, const char far * const fnam)
{
    char *buf;
    size_t len;
    int rc;

    dprintf( ("[INIT: grabComFilename(%s)]\n", fnam) );
    if(!fnam)
        return 4;

    /* Copy the filename into the local heap */
    len = _fstrlen(fnam);
    if(len >= INT_MAX || len < 1) {
        /* no filename specified */
        if(warn)
            error_syntax(0);
        return 4;
    }

    if((buf = malloc(len + 1)) == 0) {
        if(warn) error_out_of_memory();
        return 4;
    }
    _fmemcpy((char far*)buf, fnam, len);
    buf[len] = '\0';

    if (buf[1] != ':' || buf[2] != '\\')
    {   char *p;

        /* expand the string for the user */
        p = abspath(buf, warn);
        free(buf);
        if((buf = p) == 0)
            return 4;
        if(warn)
            error_init_fully_qualified(buf);

        len = strlen(buf);
    }

    while(buf[len - 1] == '\\')
        --len;
    buf[len] = 0;

    if(dfnstat(buf) & DFN_DIRECTORY) {
        /* The user specified a directory, try if we can find the
          COMMAND.COM with the standard name in there */
        char *p;

        if((p = realloc(buf, len + sizeof(COM_NAME) + 1)) == 0) {
            if(warn) error_out_of_memory();
            free(buf);
            return 4;
        }
        buf = p;
        strcpy(&buf[len], "\\" COM_NAME);
    }


    if(0 != (rc = validResFile(buf))) {
        if(warn) switch(rc) {
            default:
#ifdef NDEBUG
                assert(0);
#endif
            case 1:
                error_open_file(buf);
                break;
            case 2:
                error_fcom_is_device(buf);
                break;
            case 3:
                error_fcom_invalid(buf);
                break;
            }

        free(buf);
        return rc;
    }

    free(ComPath);    /* Save the found file */
    ComPath = buf;
    dprintf(("[INIT: new resource file name: %s]\n", ComPath));

    isSwapFile = 0;
    buf = dfnfilename(ComPath);
    assert(buf);
    if((buf = strchr(buf, '.')) != 0
            && stricmp(buf, ".swp") == 0) {
        dprintf(("[INIT: VSpawn file found: %s]\n", ComPath));
        memcpy(++buf, "COM", 3);
        isSwapFile = buf - ComPath;
    }

    return 0;
}
Example #9
0
int copy(char *dst, char *pattern, struct CopySource *src
  , int openMode)
{ char mode[3], *p;
  struct ffblk ff;
  struct CopySource *h;
  char *rDest, *rSrc;
  FILE *fin, *fout;
  int rc, asc;
  char *buf;
  size_t len;

  assert(dst);
  assert(pattern);
  assert(src);

  if(FINDFIRST(pattern, &ff, FA_RDONLY | FA_ARCH) != 0) {
    error_sfile_not_found(pattern);
    return 0;
  }

  mode[2] = '\0';

  do {
    if((rDest = fillFnam(dst, ff.ff_name)) == 0)
      return 0;
    h = src;
    do {  /* to prevent to open a source file for writing, e.g.
          for COPY *.c *.?    */
      if((rSrc = fillFnam(h->fnam, ff.ff_name)) == 0) {
        free(rDest);
        return 0;
      }
      rc = samefile(rDest, rSrc);
      free(rSrc);
      if(rc < 0) {
        error_out_of_memory();
        free(rDest);
        return 0;
      } else if(rc) {
        error_selfcopy(rDest);
        free(rDest);
        return 0;
      }
    } while((h = h->app) != 0);

    if(interactive_command		/* Suppress prompt if in batch file */
     && openMode != 'a' && !optY && (fout = fopen(rDest, "rb")) != 0) {
    	int destIsDevice = isadev(fileno(fout));

      fclose(fout);
      if(!destIsDevice) {	/* Devices do always exist */
      	switch(userprompt(PROMPT_OVERWRITE_FILE, rDest)) {
		default:	/* Error */
		case 4:	/* Quit */
			  free(rDest);
			  return 0;
		case 3:	/* All */
			optY = 1;
		case 1: /* Yes */
			break;
		case 2:	/* No */
			free(rDest);
			continue;
		}
	  }
    }
    if(cbreak) {
      free(rDest);
      return 0;
    }
    mode[0] = openMode;
    mode[1] = 'b';
    if((fout = fdevopen(rDest, mode)) == 0) {
      error_open_file(rDest);
      free(rDest);
      return 0;
    }
    mode[0] = 'r';
    h = src;
    do {
      if((rSrc = fillFnam(h->fnam, ff.ff_name)) == 0) {
        fclose(fout);
        free(rDest);
        return 0;
      }
      mode[1] = (asc = h->flags & ASCII) != 0? 't': 'b';
    reOpenIn:
      if((fin = fdevopen(rSrc, mode)) == 0) {
        error_open_file(rSrc);
        fclose(fout);
        free(rSrc);
        free(rDest);
        return 0;
      }
      if(isadev(fileno(fin)) && mode[1] != 't'
       && (h->flags & BINARY) == 0) {
        /* character devices are opened in textmode
          by default */
        fclose(fin);
        mode[1] = 't';
        goto reOpenIn;
      }

      dispCopy(rSrc, rDest, openMode == 'a' || h != src);
      if(cbreak) {
        fclose(fin);
        fclose(fout);
        free(rSrc);
        free(rDest);
        return 0;
      }

      /* Now copy the file */
      rc = 1;
      if(mode[1] != 't') {    /* binary file */
        if(Fcopy(fout, fin) != 0) {
          if(ferror(fin)) {
            error_read_file(rSrc);
          } else if(ferror(fout)) {
            error_write_file(rDest);
          } else error_copy();
          rc = 0;
        }
      } else {      /* text file, manually transform '\n' */
        if(Fmaxbuf((byte**)&buf, &len) == 0) {
          if(len > INT_MAX)
            len = INT_MAX;
          while(fgets(buf, len, fin)) {
            p = strchr(buf, '\0');
            if(*--p == '\n') {
              *p = '\0';
              fputs(buf, fout);
              putc('\r', fout);
              putc('\n', fout);
            } else
              fputs(buf, fout);
          }
          free(buf);
        } else {
          error_out_of_memory();
          rc = 0;
        }
      }
      if(rc)
        if(ferror(fin)) {
          error_read_file(rSrc);
          rc = 0;
        } else if(ferror(fout)) {
          error_write_file(rDest);
          rc = 0;
        }
      if(cbreak)
        rc = 0;
      fclose(fin);
      free(rSrc);
      if(!rc) {
        fclose(fout);
        free(rDest);
        return 0;
      }
    } while((h = h->app) != 0);
    if(asc) {   /* append the ^Z as we copied in ASCII mode */
      putc(0x1a, fout);
    }
    rc = ferror(fout);
    fclose(fout);
    if(rc) {
      error_write_file(rDest);
      free(rDest);
      return 0;
    }
    free(rDest);
  } while(FINDNEXT(&ff) == 0);

  return 1;
}
Example #10
0
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;
}
Example #11
0
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;
}
Example #12
0
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");
}
Example #13
0
void write_CF_cuda(char *fname,unsigned long long *DD,
		   unsigned long long *DR,unsigned long long *RR,
		   int nD,int nR)
{
  //////
  // Writes correlation function to file fname
  FILE *fo;
  int ii;

  print_info("*** Writing output file ");
#ifdef _VERBOSE
  print_info("%s ",fname);
#endif
  print_info("\n");

  fo=fopen(fname,"w");
  if(fo==NULL) {
    char oname[64]="output_CUTE.dat";
    fprintf(stderr,"Error opening output file %s",fname);
    fprintf(stderr," using ./output_CUTE.dat");
    fo=fopen(oname,"w");
    if(fo==NULL) error_open_file(oname);
  }

  if(corr_type==1) {
    for(ii=0;ii<NB_HISTO_1D;ii++) {
      double th;
      double corr,ercorr;
      make_CF((histo_t)(DD[ii]),(histo_t)(DR[ii]),
	      (histo_t)(RR[ii]),(np_t)(nD),(np_t)(nD),
	      (np_t)(nR),(np_t)(nR),&corr,&ercorr);
      if(logbin)
	th=pow(10,((ii+0.5)-NB_HISTO_1D)/n_logint+log_th_max)/DTORAD;
      else
      th=(ii+0.5)/(NB_HISTO_1D*i_theta_max*DTORAD);

      fprintf(fo,"%lE %lE %lE %llu %llu %llu\n",
	      th,corr,ercorr,DD[ii],DR[ii],RR[ii]);
    }
  }
  else if(corr_type==2) {
    for(ii=0;ii<NB_HISTO_1D;ii++) {
      double rr;
      double corr,ercorr;
      make_CF((histo_t)(DD[ii]),(histo_t)(DR[ii]),
	      (histo_t)(RR[ii]),(np_t)(nD),(np_t)(nD),
	      (np_t)(nR),(np_t)(nR),&corr,&ercorr);
      if(logbin)
	rr=pow(10,((ii+0.5)-NB_HISTO_1D)/n_logint+log_r_max);
      else
	rr=(ii+0.5)/(NB_HISTO_1D*i_r_max);

      fprintf(fo,"%lE %lE %lE %llu %llu %llu\n",
	      rr,corr,ercorr,DD[ii],DR[ii],RR[ii]);
    }
  }
  else if(corr_type==3) {
    for(ii=0;ii<NB_HISTO_2D;ii++) {
      int jj;
      double rt=(ii+0.5)/(NB_HISTO_2D*i_rt_max);
      for(jj=0;jj<NB_HISTO_2D;jj++) {
	double corr,ercorr;
	double rl=(jj+0.5)/(NB_HISTO_2D*i_rl_max);
	int ind=jj+NB_HISTO_2D*ii;
	make_CF((histo_t)(DD[ind]),(histo_t)(DR[ind]),
		(histo_t)(RR[ind]),(np_t)(nD),(np_t)(nD),
		(np_t)(nR),(np_t)(nR),&corr,&ercorr);
	fprintf(fo,"%lE %lE %lE %lE %llu %llu %llu\n",
		rl,rt,corr,ercorr,DD[ind],DR[ind],RR[ind]);
      }
    }
  }
  else if(corr_type==4) {
    for(ii=0;ii<NB_HISTO_2D;ii++) {
      int jj;
      double mu=(ii+0.5)/(NB_HISTO_2D);
      for(jj=0;jj<NB_HISTO_2D;jj++) {
	double corr,ercorr;
	double rr;
	if(logbin)
	  rr=pow(10,((jj+0.5)-NB_HISTO_2D)/n_logint+log_r_max);
	else
	  rr=(jj+0.5)/(NB_HISTO_2D*i_r_max);

	int ind=jj+NB_HISTO_2D*ii;
	make_CF((histo_t)(DD[ind]),(histo_t)(DR[ind]),
		(histo_t)(RR[ind]),(np_t)(nD),(np_t)(nD),
		(np_t)(nR),(np_t)(nR),&corr,&ercorr);
	fprintf(fo,"%lE %lE %lE %lE %llu %llu %llu\n",
		mu,rr,corr,ercorr,DD[ind],DR[ind],RR[ind]);
      }
    }
  }
  fclose(fo);
  
  print_info("\n");
}
Example #14
0
void write_CF(char *fname,
	      histo_t *DD,histo_t *DR,histo_t *RR,
	      np_t sum_wd,np_t sum_wd2,
	      np_t sum_wr,np_t sum_wr2)
{
  //////
  // Writes correlation function to file fname
#ifdef _HAVE_MPI
  int n_bins_all=0;

  if(corr_type==0)
    n_bins_all=nb_dz;
  else if(corr_type==1)
    n_bins_all=nb_theta;
  else if(corr_type==2)
    n_bins_all=nb_r;
  else if(corr_type==3)
    n_bins_all=nb_rt*nb_rl;
  else if(corr_type==4)
    n_bins_all=nb_r*nb_mu;
  else if(corr_type==5)
    n_bins_all=nb_red*nb_dz*nb_theta;
  else if(corr_type==6)
    n_bins_all=nb_theta*((nb_red*(nb_red+1))/2);

  if(NodeThis==0)
    MPI_Reduce(MPI_IN_PLACE,DD,n_bins_all,HISTO_T_MPI,MPI_SUM,0,MPI_COMM_WORLD);
  else
    MPI_Reduce(DD,NULL,n_bins_all,HISTO_T_MPI,MPI_SUM,0,MPI_COMM_WORLD);
  if(NodeThis==0)
    MPI_Reduce(MPI_IN_PLACE,DR,n_bins_all,HISTO_T_MPI,MPI_SUM,0,MPI_COMM_WORLD);
  else
    MPI_Reduce(DR,NULL,n_bins_all,HISTO_T_MPI,MPI_SUM,0,MPI_COMM_WORLD);
  if(NodeThis==0)
    MPI_Reduce(MPI_IN_PLACE,RR,n_bins_all,HISTO_T_MPI,MPI_SUM,0,MPI_COMM_WORLD);
  else
    MPI_Reduce(RR,NULL,n_bins_all,HISTO_T_MPI,MPI_SUM,0,MPI_COMM_WORLD);
#endif //_HAVE_MPI

  if(NodeThis==0) {
    FILE *fo;
    int ii;
    
    print_info("*** Writing output file ");
#ifdef _VERBOSE
    print_info("%s ",fname);
#endif
    print_info("\n");

    fo=fopen(fname,"w");
    if(fo==NULL) {
      char oname[64]="output_CUTE.dat";
      fprintf(stderr,"Error opening output file %s",fname);
      fprintf(stderr," using ./output_CUTE.dat");
      fo=fopen(oname,"w");
      if(fo==NULL) error_open_file(oname);
    }
    
    if(corr_type==0) {
      for(ii=0;ii<nb_dz;ii++) {
	double dz;
	double corr,ercorr;
	make_CF(DD[ii],DR[ii],RR[ii],sum_wd,sum_wd2,
		sum_wr,sum_wr2,&corr,&ercorr);
	dz=(ii+0.5)/(nb_dz*i_dz_max);
	fprintf(fo,"%lE %lE %lE ",dz,corr,ercorr);
#ifdef _WITH_WEIGHTS
	fprintf(fo,"%lE %lE %lE\n",DD[ii],DR[ii],RR[ii]);
#else //_WITH_WEIGHTS
	fprintf(fo,"%llu %llu %llu\n",DD[ii],DR[ii],RR[ii]);
#endif //_WITH_WEIGHTS
      }
    }
    else if(corr_type==1) {
      for(ii=0;ii<nb_theta;ii++) {
	double th;
	double corr,ercorr;
	make_CF(DD[ii],DR[ii],RR[ii],sum_wd,sum_wd2,
		sum_wr,sum_wr2,&corr,&ercorr);
	if(logbin)
	  th=pow(10,((ii+0.5)-nb_theta)/n_logint+log_th_max)/DTORAD;
	else
	  th=(ii+0.5)/(nb_theta*i_theta_max*DTORAD);
	
	fprintf(fo,"%lE %lE %lE ",th,corr,ercorr);
#ifdef _WITH_WEIGHTS
	fprintf(fo,"%lE %lE %lE\n",DD[ii],DR[ii],RR[ii]);
#else //_WITH_WEIGHTS
	fprintf(fo,"%llu %llu %llu\n",DD[ii],DR[ii],RR[ii]);
#endif //_WITH_WEIGHTS
      }
    }
    else if(corr_type==2) {
      for(ii=0;ii<nb_r;ii++) {
	double rr;
	double corr,ercorr;
	make_CF(DD[ii],DR[ii],RR[ii],sum_wd,sum_wd2,
		sum_wr,sum_wr2,&corr,&ercorr);
	if(logbin)
	  rr=pow(10,((ii+0.5)-nb_r)/n_logint+log_r_max);
	else
	  rr=(ii+0.5)/(nb_r*i_r_max);
	
	fprintf(fo,"%lE %lE %lE ",rr,corr,ercorr);
#ifdef _WITH_WEIGHTS
	fprintf(fo,"%lE %lE %lE\n",DD[ii],DR[ii],RR[ii]);
#else //_WITH_WEIGHTS
	fprintf(fo,"%llu %llu %llu\n",DD[ii],DR[ii],RR[ii]);
#endif //_WITH_WEIGHTS
      }
    }
    else if(corr_type==3) {
      for(ii=0;ii<nb_rt;ii++) {
	int jj;
	double rt=(ii+0.5)/(nb_rt*i_rt_max);
	for(jj=0;jj<nb_rl;jj++) {
	  double corr,ercorr;
	  double rl=(jj+0.5)/(nb_rl*i_rl_max);
	  int ind=jj+nb_rl*ii;
	  make_CF(DD[ind],DR[ind],RR[ind],sum_wd,sum_wd2,
		  sum_wr,sum_wr2,&corr,&ercorr);
	  fprintf(fo,"%lE %lE %lE %lE ",rl,rt,corr,ercorr);
#ifdef _WITH_WEIGHTS
	  fprintf(fo,"%lE %lE %lE\n",DD[ind],DR[ind],RR[ind]);
#else //_WITH_WEIGHTS
	  fprintf(fo,"%llu %llu %llu\n",DD[ind],DR[ind],RR[ind]);
#endif //_WITH_WEIGHTS
	}
      }
    }
    else if(corr_type==4) {
      for(ii=0;ii<nb_r;ii++) {
	int jj;
	double rr;
	if(logbin)
	  rr=pow(10,((ii+0.5)-nb_r)/n_logint+log_r_max);
	else
	  rr=(ii+0.5)/(nb_r*i_r_max);
	
	for(jj=0;jj<nb_mu;jj++) {
	  double corr,ercorr;
	  double mu=(jj+0.5)/(nb_mu);
	  int ind=jj+nb_mu*ii;
	  make_CF(DD[ind],DR[ind],RR[ind],sum_wd,sum_wd2,
		  sum_wr,sum_wr2,&corr,&ercorr);
	  fprintf(fo,"%lE %lE %lE %lE ",mu,rr,corr,ercorr);
#ifdef _WITH_WEIGHTS
	  fprintf(fo,"%lE %lE %lE\n",DD[ind],DR[ind],RR[ind]);
#else //_WITH_WEIGHTS
	  fprintf(fo,"%llu %llu %llu\n",DD[ind],DR[ind],RR[ind]);
#endif //_WITH_WEIGHTS
	}
      }
    }
    else if(corr_type==5) {
      for(ii=0;ii<nb_red;ii++) {
	int jj;
	double z_mean=red_0+(ii+0.5)/(i_red_interval*nb_red);
	for(jj=0;jj<nb_dz;jj++) {
	  int kk;
	  double dz=(jj+0.5)/(nb_dz*i_dz_max);
	  for(kk=0;kk<nb_theta;kk++) {
	    double theta;
	    int index=kk+nb_theta*(jj+nb_dz*ii);
	    double corr,ercorr;
	    if(logbin)
	      theta=pow(10,((kk+0.5)-nb_theta)/n_logint+log_th_max)/DTORAD;
	    else
	      theta=(kk+0.5)/(nb_theta*i_theta_max*DTORAD);
	    make_CF(DD[index],DR[index],RR[index],sum_wd,sum_wd2,
		    sum_wr,sum_wr2,&corr,&ercorr);
	    fprintf(fo,"%lE %lE %lE %lE %lE ",z_mean,dz,theta,corr,ercorr);
#ifdef _WITH_WEIGHTS
	    fprintf(fo,"%lE %lE %lE\n",DD[index],DR[index],RR[index]);
#else //_WITH_WEIGHTS
	    fprintf(fo,"%llu %llu %llu\n",DD[index],DR[index],RR[index]);
#endif //_WITH_WEIGHTS
	  }
	}
      }
    }
    else if(corr_type==6) {
      for(ii=0;ii<nb_red;ii++) {
	int jj;
	double z1=red_0+(ii+0.5)/(i_red_interval*nb_red);
	for(jj=ii;jj<nb_red;jj++) {
	  int kk;
	  double z2=red_0+(jj+0.5)/(i_red_interval*nb_red);
	  for(kk=0;kk<nb_theta;kk++) {
	    double theta;
	    //	  int index=kk+nb_theta*(jj+nb_red*ii);
	    int index=kk+nb_theta*((ii*(2*nb_red-ii-1))/2+jj);
	    double corr,ercorr;
	    if(logbin)
	      theta=pow(10,((kk+0.5)-nb_theta)/n_logint+log_th_max)/DTORAD;
	    else
	      theta=(kk+0.5)/(nb_theta*i_theta_max*DTORAD);
	    make_CF(DD[index],DR[index],RR[index],sum_wd,sum_wd2,
		    sum_wr,sum_wr2,&corr,&ercorr);
	    fprintf(fo,"%lE %lE %lE %lE %lE ",z1,z2,theta,corr,ercorr);
#ifdef _WITH_WEIGHTS
	    fprintf(fo,"%lE %lE %lE\n",DD[index],DR[index],RR[index]);
#else //_WITH_WEIGHTS
	    fprintf(fo,"%llu %llu %llu\n",DD[index],DR[index],RR[index]);
#endif //_WITH_WEIGHTS
	  }
	}
      }
    }
    fclose(fo);
    
    printf("\n");
  }
}
Example #15
0
File: io_fg.c Project: 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;
}