示例#1
0
文件: 38.C 项目: JackDrogon/Study
int main()
{
	FILE *fpt1,*fpt2;
	char fname[120];/*存贮文件名*/
	int fill1,fill2;/*分别记录两个文件当前行读入并输出的字符数*/

//	clrscr();
	printf("Enter file 1 name.\n");
	scanf("%s",fname);
	fpt1=fopen(fname,"r");/*打开文件1*/
	if(fpt1==NULL)
	{	printf("Can't open file %s.\n",fname);
		exit(1);
	}
	printf("Enter file 2 name.\n");
	scanf("%s",fname);
	fpt2=fopen(fname,"r");/*打开文件2*/
	if(fpt2==NULL)
	{	printf("Can't open file %s.\n",fname);
		fclose(fpt1);
		exit(2);
	}

	while(!feof(fpt1)||!feof(fpt2))/*在有文件还未结束时循环*/
	{
		fill1=fill2=0;
		if(!feof(fpt1)) fill1=readline(fpt1);/*在文件未结束时读文件*/
		printf("%*c",TXTWIDTH-fill1+TXTGAP,'');
		if(!feof(fpt2)) fill2=readline(fpt2);/*在文件未结束时读文件*/
		printf("%*c%2d\n",TXTWIDTH-fill2+4,'',fill1+fill2);
		linecount();/*调用行计数函数*/
	}
	puts("\n Press any key to quit...");
	getch();
}
示例#2
0
文件: inits.c 项目: Hao-HUST/NBIS
void mkoas_init(char *prsfile, SGMNT_PRS *sgmnt_prs, ENHNC_PRS *enhnc_prs,
          int *rors_slit_range_thresh, float *r92a_discard_thresh,
          RGAR_PRS *rgar_prs, int *ascii_oas, int *update_freq,
          int *clobber_oas_file, FILE **fp_proc_images_list, int *n_oas,
          char *oas_file)
{
  char *datadir, proc_images_list[200], str[400];

  /* Reads first the oas (orientation arrays) parms file, then the
  mkoas additional-parms default file, then the required user parms
  file.  Checks that every parm was set. */
  memset(&mkoas_setflags, 0, sizeof(mkoas_setflags));

  datadir = get_datadir();
  sprintf(str, "%s/parms/oas.prs", datadir);

  mkoas_readparms(str, sgmnt_prs, enhnc_prs, rors_slit_range_thresh,
    r92a_discard_thresh, rgar_prs, ascii_oas, update_freq,
    clobber_oas_file, proc_images_list, oas_file);

  sprintf(str, "%s/parms/mkoas.prs", datadir);

  mkoas_readparms(str, sgmnt_prs, enhnc_prs, rors_slit_range_thresh,
    r92a_discard_thresh, rgar_prs, ascii_oas, update_freq,
    clobber_oas_file, proc_images_list, oas_file);
  mkoas_readparms(prsfile, sgmnt_prs, enhnc_prs,
    rors_slit_range_thresh,
    r92a_discard_thresh, rgar_prs, ascii_oas, update_freq,
    clobber_oas_file, proc_images_list, oas_file);
  mkoas_check_parms_allset();
  strcpy(str, tilde_filename(proc_images_list, 0));
  *n_oas = linecount(str);
  *fp_proc_images_list = fopen_ch(str, "rb");
}
示例#3
0
文件: userdb.c 项目: HarryR/sanos
static int read_group() {
  char *p;
  int n, m;
  int grouptab_len;
  int commas;
  char **grmem;

  // Read user group file into memory
  group = read_file("/etc/group", &group_len);
  if (!group) return -1;

  // Calculate the number of entries in user group file
  n = linecount(group);

  // The total number of group members entries is at most the number of commas
  // plus the number of lines
  commas = 0;
  p = group;
  while (*p) if (*p++ == ',') commas++;

  // Allocate memory for user group table
  grouptab_len = n * sizeof(struct group) + (commas + n * 2) * sizeof(char *);
  grouptab = vmalloc(NULL, grouptab_len, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE, 'UDB');
  if (!grouptab) return -1;
  grmem = (char **) (grouptab + n * sizeof(struct group));

  // Build user group table entries
  p = group;
  n = 0;
  m = 0;
  while (*p) {
    char *next = nextline(p);

    grouptab[n].gr_name = p;
    p = skip(p, ':');
    grouptab[n].gr_passwd = p;
    p = skip(p, ':');
    grouptab[n].gr_gid = atoi(p);
    p = skip(p, ':');
    grouptab[n].gr_mem = &grmem[m];
    while (*p) {
      grmem[m++] = p;
      p = skip(p, ',');
    }
    grmem[m++] = NULL;

    p = next;
    n++;
  }

  // Protect group memory
  if (vmprotect(group, passwd_len, PAGE_READONLY) < 0) return -1;
  if (vmprotect(grouptab, grouptab_len, PAGE_READONLY) < 0) return -1;
  group_cnt = n;

  return 0;
}
示例#4
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");
}
示例#5
0
文件: userdb.c 项目: HarryR/sanos
static int read_passwd() {
  char *p;
  int n;
  int passwdtab_len;

  // Read password file into memory
  passwd = read_file("/etc/passwd", &passwd_len);
  if (!passwd) return -1;

  // Calculate the number of entries in password file
  n = linecount(passwd);

  // Allocate memory for password table
  passwdtab_len = n * sizeof(struct passwd);
  passwdtab = vmalloc(NULL, passwdtab_len, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE, 'UDB');
  if (!passwdtab) return -1;

  // Build password table entries
  p = passwd;
  n = 0;
  while (*p) {
    char *next = nextline(p);

    passwdtab[n].pw_name = p;
    p = skip(p, ':');
    passwdtab[n].pw_passwd = p;
    p = skip(p, ':');
    passwdtab[n].pw_uid = atoi(p);
    p = skip(p, ':');
    passwdtab[n].pw_gid = atoi(p);
    p = skip(p, ':');
    passwdtab[n].pw_gecos = p;
    p = skip(p, ':');
    passwdtab[n].pw_dir = p;
    p = skip(p, ':');
    passwdtab[n].pw_shell = p;

    p = next;
    n++;
  }

  // Protect password memory
  if (vmprotect(passwd, passwd_len, PAGE_READONLY) < 0) return -1;
  if (vmprotect(passwdtab, passwdtab_len, PAGE_READONLY) < 0) return -1;
  passwd_cnt = n;

  return 0;
}
int main(int argc,char **argv){
    int n;          /*  number of chars in given line */
    int count = 0;  /*  number of lines */
    int sum = 0;    /*  total number of characters */
    float avg;      /*  average number of chars per line */

    printf("Enter the text below\n");
    /*  read a line of text and update the cumulative counters */
    while ( (n = linecount()) > 0){
        sum += n;
        ++count;
    }

    avg = (float) sum/count;
    printf("\n Average number of characters per line: %5.2f \n", avg);
    return 0;
}
std::vector<SLogEntry> Connector::getLogEntries(void)
{
    std::string d=getResponse("GET LOGPOINTS","");
    int lc=linecount(d);
    std::vector<SLogEntry> ret;
    for(int i=0; i<lc; ++i)
    {
        std::string l=getline(i, d);
        if(l.empty())continue;
        SLogEntry le;
        le.logid=atoi(getuntil("-", l).c_str() );
        std::string lt=getafter("-", l);
        le.logtime=wxString::FromUTF8(lt.c_str());
        ret.push_back(le);
    }
    return ret;
}
std::vector<SBackupDir> Connector::getSharedPaths(void)
{
    std::vector<SBackupDir> ret;
    std::string d=getResponse("1GET BACKUP DIRS","");
    int lc=linecount(d);
    for(int i=0; i<lc; i+=2)
    {
        SBackupDir bd;
        bd.id=atoi(getline(i, d).c_str() );
        std::string path=getline(i+1, d);
        bd.path=wxString::FromUTF8(path.c_str() );
        if(path.find("|")!=std::string::npos)
        {
            bd.path=ConvertToUnicode(getafter("|", path));
            bd.name=ConvertToUnicode(getuntil("|", path));
        }
        ret.push_back( bd );
    }
    return ret;
}
示例#9
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);
}
示例#10
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");
}
示例#11
0
void
setptr(register FILE *ibuf)
{
	int n, newline = 1, blankline = 1;
	int StartNewMsg = TRUE;
	int ToldUser = FALSE;
	long clen = -1L;
	int hdr = 0;
	int cflg = 0;			/* found Content-length in header */
	register char *cp;
	register int l;
	register long s;
	off_t offset;
	char linebuf[LINESIZE];
	int inhead, newmail, Odot;
	short flag;

	if (!space) {
		msgCount = 0;
		offset = 0;
		space = 32;
		newmail = 0;
		message =
		    (struct message *)calloc(space, sizeof (struct message));
		if (message == NULL) {
			fprintf(stderr, gettext(
			    "calloc: insufficient memory for %d messages\n"),
			    space);
			exit(1);
			/* NOTREACHED */
		}
		dot = message;
	} else {
		newmail = 1;
		offset = fsize(otf);
	}
	s = 0L;
	l = 0;
	/*
	 * Set default flags.  When reading from
	 * a folder, assume the message has been
	 * previously read.
	 */
	if (edit)
		flag = MUSED|MREAD;
	else
		flag = MUSED|MNEW;

	inhead = 0;
	while ((n = getln(linebuf, sizeof (linebuf), ibuf)) > 0) {
		if (!newline) {
			goto putout;
		}
	top:
		hdr = inhead && (headerp(linebuf) ||
		    (linebuf[0] == ' ' || linebuf[0] == '\t'));
		if (!hdr && cflg) {	/* nonheader, Content-length seen */
			if (clen > 0 && clen < n) {	/* read too much */
				/*
				 * NB: this only can happen if there is a
				 * small content that is NOT \n terminated
				 * and has no leading blank line, i.e., never.
				 */
				if (fwrite(linebuf, 1, (int)clen, otf) !=
					clen) {
					fclose(ibuf);
					fflush(otf);
				} else {
					l += linecount(linebuf, clen);
				}
				offset += clen;
				s += clen;
				n -= (int)clen;
				/* shift line to the left, copy null as well */
				memcpy(linebuf, linebuf+clen, n+1);
				cflg = 0;
				message[msgCount-1].m_clen = clen + 1;
				blankline = 1;
				StartNewMsg = TRUE;
				goto top;
			}
			/* here, clen == 0 or clen >= n */
			if (n == 1 && linebuf[0] == '\n') {
				/* leading empty line */
				clen++;		/* cheat */
				inhead = 0;
			}
			offset += clen;
			s += (long)clen;
			message[msgCount-1].m_clen = clen;
			for (;;) {
				if (fwrite(linebuf, 1, n, otf) != n) {
					fclose(ibuf);
					fflush(otf);
				} else {
					l += linecount(linebuf, n);
				}
				clen -= n;
				if (clen <= 0) {
					break;
				}
				n = clen < sizeof (linebuf) ?
				    (int)clen : (int)sizeof (linebuf);
				if ((n = fread(linebuf, 1, n, ibuf)) <= 0) {
					fprintf(stderr, gettext(
			"%s:\tYour mailfile was found to be corrupted.\n"),
					    progname);
					fprintf(stderr, gettext(
					    "\t(Unexpected end-of-file).\n"));
					fprintf(stderr, gettext(
					"\tMessage #%d may be truncated.\n\n"),
					    msgCount);
					offset -= clen;
					s -= clen;
					clen = 0; /* stop the loop */
				}
			}
			/* All done, go to top for next message */
			cflg = 0;
			blankline = 1;
			StartNewMsg = TRUE;
			continue;
		}

		/* Look for a From line that starts a new message */
		if (blankline && linebuf[0] == 'F' && is_headline(linebuf)) {
			if (msgCount > 0 && !newmail) {
				message[msgCount-1].m_size = s;
				message[msgCount-1].m_lines = l;
				message[msgCount-1].m_flag = flag;
			}
			if (msgCount >= space) {
				/*
				 * Limit the speed at which the
				 * allocated space grows.
				 */
				if (space < 512)
					space = space*2;
				else
					space += 512;
				errno = 0;
				Odot = dot - &(message[0]);
				message = (struct message *)
				    realloc(message,
					space*(sizeof (struct message)));
				if (message == NULL) {
					perror("realloc failed");
					fprintf(stderr, gettext(
			"realloc: insufficient memory for %d messages\n"),
					    space);
					exit(1);
				}
				dot = &message[Odot];
			}
			message[msgCount].m_offset = offset;
			message[msgCount].m_text = TRUE;
			message[msgCount].m_clen = 0;
			newmail = 0;
			msgCount++;
			if (edit)
				flag = MUSED|MREAD;
			else
				flag = MUSED|MNEW;
			inhead = 1;
			s = 0L;
			l = 0;
			StartNewMsg = FALSE;
			ToldUser = FALSE;
			goto putout;
		}

		/* if didn't get a header line, we're no longer in the header */
		if (!hdr)
			inhead = 0;
		if (!inhead)
			goto putout;

		/*
		 * Look for Status: line.  Do quick check for second character,
		 * many headers start with "S" but few have "t" as second char.
		 */
		if ((linebuf[1] == 't' || linebuf[1] == 'T') &&
		    ishfield(linebuf, "status")) {
			cp = hcontents(linebuf);
			flag = MUSED|MNEW;
			if (strchr(cp, 'R'))
				flag |= MREAD;
			if (strchr(cp, 'O'))
				flag &= ~MNEW;
		}
		/*
		 * Look for Content-Length and Content-Type headers.  Like
		 * above, do a quick check for the "-", which is rare.
		 */
		if (linebuf[7] == '-') {
			if (ishfield(linebuf, "content-length")) {
				if (!cflg) {
					clen = atol(hcontents(linebuf));
					cflg = clen >= 0;
				}
			} else if (ishfield(linebuf, "content-type")) {
				char word[LINESIZE];
				char *cp2;

				cp = hcontents(linebuf);
				cp2 = word;
				while (!isspace(*cp))
					*cp2++ = *cp++;
				*cp2 = '\0';
				if (icequal(word, "binary"))
					message[msgCount-1].m_text = FALSE;
			}
		}
putout:
		offset += n;
		s += (long)n;
		if (fwrite(linebuf, 1, n, otf) != n) {
			fclose(ibuf);
			fflush(otf);
		} else {
			l++;
		}
		if (ferror(otf)) {
			perror("/tmp");
			exit(1);
		}
		if (msgCount == 0) {
			fclose(ibuf);
			fflush(otf);
		}
		if (linebuf[n-1] == '\n') {
			blankline = newline && n == 1;
			newline = 1;
			if (n == 1) {
				/* Blank line. Skip StartNewMsg check below */
				continue;
			}
		} else {
			newline = 0;
		}
		if (StartNewMsg && !ToldUser) {
			fprintf(stderr, gettext(
			    "%s:\tYour mailfile was found to be corrupted\n"),
			    progname);
			fprintf(stderr,
			    gettext("\t(Content-length mismatch).\n"));
			fprintf(stderr, gettext(
			    "\tMessage #%d may be truncated,\n"), msgCount);
			fprintf(stderr, gettext(
			    "\twith another message concatenated to it.\n\n"));
			ToldUser = TRUE;
		}
	}

	if (n == 0) {
		fflush(otf);
		if (fferror(otf)) {
			perror("/tmp");
			exit(1);
		}
		if (msgCount) {
			message[msgCount-1].m_size = s;
			message[msgCount-1].m_lines = l;
			message[msgCount-1].m_flag = flag;
		}
		fclose(ibuf);
	}
}
示例#12
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;
}
示例#13
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;
}
示例#14
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");
}
示例#15
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;
}