int64_t read_forests(const char *filename, int64_t **f, int64_t **t) { char buffer[MAXBUFSIZE]; const char comment = '#'; /* By passing the comment character, getnumlines will return the actual number of lines, ignoring the first header line. */ const int64_t ntrees = getnumlines(filename, comment); *f = my_malloc(sizeof(int64_t), ntrees); *t = my_malloc(sizeof(int64_t), ntrees); int64_t *forests = *f; int64_t *tree_roots = *t; int64_t ntrees_found = 0; FILE *fp = my_fopen(filename, "r"); while(fgets(buffer, MAXBUFSIZE, fp) != NULL) { if(buffer[0] == comment) { continue; } else { const int nitems_expected = 2; XASSERT(ntrees_found < ntrees, "ntrees=%"PRId64" should be less than ntrees_found=%"PRId64"\n", ntrees, ntrees_found); int nitems = sscanf(buffer, "%"SCNd64" %"SCNd64, &(tree_roots[ntrees_found]), &(forests[ntrees_found])); XASSERT(nitems == nitems_expected, "Expected to parse %d long integers but found `%s' in the buffer. nitems = %d \n", nitems_expected, buffer, nitems); ntrees_found++; } } XASSERT(ntrees == ntrees_found, "ntrees=%"PRId64" should be equal to ntrees_found=%"PRId64"\n", ntrees, ntrees_found); fclose(fp); return ntrees_found; }
int main(int argc,char **argv) { int Ngroups,*Ng=NULL,nmin; double volume; int i,j ; /*---Arguments-------------------------*/ int Nmax; //For file i/o const int MAXBUFSIZE=10000; char *fname=NULL; char buffer[MAXBUFSIZE]; const char comment='#'; const int nitems = 1; int nread; FILE *fp = NULL; /*-------------------------------------*/ int N,*indx,Nlast,*Ngrps ; double *logngrp1,*logngrp2 ; double X1,X2,Y1,Y2,Z1,Z2 ; double inv_volume,half_log10_inv_volume; int first_index_with_nmin=0; if(argc != 4) { fprintf(stderr,"ERROR: Usage - `%s' <total volume> <GalaxyFile> <nmin>",argv[0]); exit(EXIT_FAILURE); } volume=atof(argv[1]); fname=argv[2]; nmin =atoi(argv[3]); Ngroups = getnumlines(fname,comment); Ng = my_malloc(sizeof(*Ng),Ngroups); fp = my_fopen(fname,"r"); i=0; while(1) { if(fgets(buffer, MAXBUFSIZE,fp)!=NULL) { if(buffer[0] !=comment) { nread = sscanf(buffer,"%*s %*d %*lf %*lf %*lf %d ",&Ng[i]); assert(nread == nitems); i++; } } else break; } assert(i==Ngroups); fclose(fp); /*---Read-Arguments---------------------------------------------------*/ assert(volume > 0.0); inv_volume=1.0/volume; half_log10_inv_volume = log10(0.5*inv_volume); /*---Sort-groups-by-N-------------------------------------------------*/ SGLIB_ARRAY_SINGLE_HEAP_SORT(int, Ng, Ngroups, SGLIB_NUMERIC_COMPARATOR); /*---Compute-multiplicity-function------------------------------------*/ Nmax = Ng[Ngroups-1]; indx = my_calloc(sizeof(*indx),Nmax); Ngrps = my_calloc(sizeof(*Ngrps),Nmax); logngrp1 = my_malloc(sizeof(*logngrp1),Nmax); logngrp2 = my_malloc(sizeof(*logngrp2),Nmax); i=0 ; while(Ng[i] < nmin) i++; assert(i < Ngroups); first_index_with_nmin=i; for(N=nmin;N<=Nmax;N++) { if(i >= Ngroups) { fprintf(stderr,"breaking\n"); break; } if(N==Ng[i]) { logngrp1[N-1] = log10((double)(Ngroups-i)*inv_volume); while(i < Ngroups && Ng[i]==N) { Ngrps[N-1]++ ; i++ ; } if(N==Ng[Ngroups-1]) { logngrp2[N-1] = half_log10_inv_volume; } else { assert(i < Ngroups); logngrp2[N-1] = log10((double)(Ngroups-i)*inv_volume); } } else if(N<Ng[i]) { indx[N-1] = 1 ; } else { indx[N-1] = 2 ; } } /*---Interpolate-and-extrapolate-------------------------------------*/ Nlast=first_index_with_nmin; for(N=nmin;N<=Nmax;N++) { if(indx[N-1]==0) { Nlast = N ; } else if(indx[N-1]==1) { X1 = log10(Nlast) ; Y1 = logngrp1[Nlast-1] ; Z1 = logngrp2[Nlast-1] ; j=N ; while(j < Nmax && indx[j-1]==1) { j++ ; } assert(j <= Nmax); assert(j >= 1); X2 = log10(j) ; Y2 = logngrp1[j-1] ; Z2 = logngrp2[j-1] ; logngrp1[N-1] = Y1 + (log10(N)-X1)*(Y2-Y1)/(X2-X1) ; logngrp2[N-1] = Z1 + (log10(N)-X1)*(Z2-Z1)/(X2-X1) ; } else { X1 = log10(Nlast-1) ; Y1 = logngrp1[Nlast-2] ; Z1 = logngrp2[Nlast-2] ; X2 = log10(Nlast) ; Y2 = logngrp1[Nlast-1] ; Z2 = logngrp2[Nlast-1] ; logngrp1[N-1] = Y1 + (log10(N)-X1)*(Y2-Y1)/(X2-X1) ; logngrp2[N-1] = Z1 + (log10(N)-X1)*(Z2-Z1)/(X2-X1) ; } } /*---Print-output-----------------------------------------------------*/ for(i=nmin-1;i<Nmax;i++) { fprintf(stdout,"%3d %5d %7.4f %7.4f %1d\n",i+1,Ngrps[i],logngrp1[i],logngrp2[i],indx[i]); } free(indx);free(Ngrps); free(logngrp1); free(logngrp2); free(Ng); exit(EXIT_SUCCESS); }