Example #1
0
void nemo_main(void)
{
    char line[80];
    stream instr = stropen(getparam("in"),"r");
    int mom = getiparam("moment");
    int maxsize = getiparam("maxsize");
    real x;
    Moment m;
    bool Qminmax = getbparam("minmax");
    bool Qmedian = getbparam("median");

    ini_moment(&m,ABS(mom),maxsize);
    while (fgets(line,80,instr) != NULL) {
      x = atof(line);
      accum_moment(&m,x,1.0);
      if (maxsize > 0) {
	debug_moment(1,&m);
	printf("%d ",n_moment(&m));
	if (Qminmax)
	  printf("%g %g\n",min_moment(&m), max_moment(&m));
	else if (Qmedian)
	  printf("%g\n",median_moment(&m));
	else
	  printf("%g\n",show_moment(&m,mom));
      }
    }
    if (maxsize == 0) {
      printf("%d ",n_moment(&m));
      if (Qminmax)
        printf("%g %g\n",min_moment(&m), max_moment(&m));
      else if (Qmedian)
	printf("%g\n",median_moment(&m));
      else
        printf("%g\n",show_moment(&m,mom));
    }
}
Example #2
0
nemo_main()
{
    stream instr, tabstr;
    real   tsnap, ekin, etot, dr, r, rv, v, vr, vt, aux;
    real   varmin[MAXOPT], varmax[MAXOPT];
    real   var0[MAXOPT], var1[MAXOPT], var2[MAXOPT];
    Moment var[MAXOPT];
    string headline=NULL, options, times, mnmxmode;
    Body *btab = NULL, *bp, *bq;
    bool   Qmin, Qmax, Qmean, Qsig, Qtime, scanopt();
    int i, n, nbody, bits, nsep, isep, nopt, ParticlesBit;
    char fmt[20],*pfmt;
    string *burststring(), *opt;
    rproc btrtrans(), fopt[MAXOPT], faux;

    ParticlesBit = (MassBit | PhaseSpaceBit | PotentialBit | AccelerationBit |
            AuxBit | KeyBit);
    instr = stropen(getparam("in"), "r");	/* open input file */
    mnmxmode= getparam("mode");

    opt = burststring(getparam("var"),", ");
    nopt = 0;					/* count options */
    while (opt[nopt]) {				/* scan through options */
        fopt[nopt] = btrtrans(opt[nopt]);
        nopt++;
        if (nopt==MAXOPT) {
            dprintf(0,"\n\nMaximum number of var's = %d exhausted\n",MAXOPT);
            break;
        }
    }
    dprintf(0,"var: \n");
    for (i=0; i<nopt; i++)
        dprintf(0,"%s ",opt[i]);
    dprintf(0,"\n");
    dprintf(0,"mode: %s\n",mnmxmode);

    Qmin = scanopt(mnmxmode,"min");
    Qmax = scanopt(mnmxmode,"max");
    Qmean = scanopt(mnmxmode,"mean");
    Qsig = scanopt(mnmxmode,"sigma");
    Qtime = scanopt(mnmxmode,"time");
    if (!Qmin && !Qmax && !Qmean && !Qsig && !Qtime) 
        error("No mode selected");
#if 0
    pfmt = getparam("tab");
    if (pfmt!=NULL && *pfmt!=NULL) {
	dprintf(0,"Saving table in %s\n",pfmt);
        tabstr = stropen(pfmt,"w");
    } else
#endif
        tabstr = stdout;

    times = getparam("times");
    pfmt = getparam("format");
    strcpy (fmt,pfmt);
    if (strchr(fmt,' ')==NULL && strchr(fmt,',')==NULL)
        strcat (fmt," ");       /* append blank if user did not specify sep */

    get_history(instr);                 /* read history */

    for(;;) {                /* repeating until first or all times are read */
	get_history(instr);
        if (!get_tag_ok(instr, SnapShotTag))
            break;                                  /* done with work */
        get_snap(instr, &btab, &nbody, &tsnap, &bits);
        if (!streq(times,"all") && !within(tsnap,times,0.0001))
            continue;                   /* skip work on this snapshot */
        if ( (bits & ParticlesBit) == 0)
            continue;                   /* skip work, only diagnostics here */

            for (bp = btab, i=0; bp < btab+nbody; bp++, i++) {
                for (n=0; n<nopt; n++) {
                    aux = fopt[n](bp,tsnap,i);
                    if (i==0) ini_moment(&var[n],2,0);
                    accum_moment(&var[n], aux, 1.0);
                }
            }
            if (Qtime)
                fprintf(tabstr,fmt,tsnap);
            if (Qmin) {
                for (n=0; n<nopt; n++)
                    fprintf(tabstr,fmt,min_moment(&var[n]));
            }
            if (Qmax) {
                for (n=0; n<nopt; n++)
                    fprintf(tabstr,fmt,max_moment(&var[n]));
            }
            if (Qmean) {
                for (n=0; n<nopt; n++)
                    fprintf(tabstr,fmt,mean_moment(&var[n]));
            }
            if (Qsig) {
                for (n=0; n<nopt; n++)
                    fprintf(tabstr,fmt,sigma_moment(&var[n]));
            }
            fprintf(tabstr,"\n");
    
    }
    strclose(instr);
}
Example #3
0
nemo_main()
{
    int  i, j, k;
    real x, xmin, xmax, mean, sigma, skew, kurt, median, bad, w, *data;
    real sum, sov;
    Moment m;
    bool Qmin, Qmax, Qbad, Qw, Qmedian, Qmmcount = getbparam("mmcount");
    real nu, nppb = getdparam("nppb");
    int npar = getiparam("npar");
    int ngood = 0;
    int min_count, max_count;

    instr = stropen (getparam("in"), "r");
    read_image (instr,&iptr);
    strclose(instr);

    if (hasvalue("win")) {
      instr = stropen (getparam("win"), "r");
      read_image (instr,&wptr);
      strclose(instr);
      if (Nx(iptr) != Nx(wptr)) error("X sizes of in/win don't match");
      if (Ny(iptr) != Ny(wptr)) error("X sizes of in/win don't match");
      if (Nz(iptr) != Nz(wptr)) error("X sizes of in/win don't match");
      Qw = TRUE;
    } else
      Qw = FALSE;

    nx = Nx(iptr);	
    ny = Ny(iptr);
    nz = Nz(iptr);
    Qmin = hasvalue("min");
    if (Qmin) xmin = getdparam("min");
    Qmax = hasvalue("max");
    if (Qmax) xmax = getdparam("max");
    Qbad = hasvalue("bad");
    if (Qbad) bad = getdparam("bad");
    Qmedian = getbparam("median");
    if (Qmedian)
      data = (real *) allocate(nx*ny*nz*sizeof(real));

    sov = Dx(iptr)*Dy(iptr)*Dz(iptr);   /* voxel volume; TODO: should we do 2D vs. 3D ? */
    
    ini_moment(&m,4,0);
    for (i=0; i<nx; i++) {
      for (j=0; j<ny; j++) {
        for (k=0; k<nz; k++) {
            x =  CubeValue(iptr,i,j,k);
            if (Qmin && x<xmin) continue;
            if (Qmax && x>xmax) continue;
            if (Qbad && x==bad) continue;
	    w = Qw ? CubeValue(wptr,i,j,k) : 1.0;
            accum_moment(&m,x,w);
	    if (Qmedian) data[ngood++] = x;
        }
      }
    }
    if (npar > 0) {
      nu = n_moment(&m)/nppb - npar;
      if (nu < 1) error("%g: No degrees of freedom",nu);
      printf("chi2= %g\n", show_moment(&m,2)/nu/nppb);
      printf("df= %g\n", nu);
    } else {
      nsize = nx * ny * nz;
    
      mean = mean_moment(&m);
      sigma = sigma_moment(&m);
      skew = skewness_moment(&m);
      kurt = kurtosis_moment(&m);
      sum = show_moment(&m,1);
      
      printf ("Min=%f  Max=%f\n",min_moment(&m), max_moment(&m));
      printf ("Number of points      : %d\n",n_moment(&m));
      printf ("Mean and dispersion   : %f %f\n",mean,sigma);
      printf ("Skewness and kurtosis : %f %f\n",skew,kurt);
      printf ("Sum and Sum*Dx*Dy*Dz  : %f %f\n",sum, sum*sov);
      if (Qmedian)
	printf ("Median                : %f\n",get_median(ngood,data));

      if (Qmmcount) {
	min_count = max_count = 0;
	xmin = min_moment(&m);
	xmax = max_moment(&m);
	for (i=0; i<nx; i++) {
	  for (j=0; j<ny; j++) {
	    for (k=0; k<nz; k++) {
	      x =  CubeValue(iptr,i,j,k);
	      if (x==xmin) min_count++;
	      if (x==xmax) max_count++;
	    }
	  }
	} /* i */
	printf("Min_Max_count         : %d %d\n",min_count,max_count);
      }
      printf ("%d/%d out-of-range points discarded\n",nsize-n_moment(&m), nsize);
    }
}
Example #4
0
nemo_main()
{
    int  i, j, k, ki;
    real x, y, z, xmin, xmax, mean, sigma, skew, kurt,  bad, w, *data;
    real dmin, dmax;
    real sum, sov, q1, q2, q3;
    Moment m;
    bool Qmin, Qmax, Qbad, Qw, Qmedian, Qrobust, Qtorben, Qmmcount = getbparam("mmcount");
    bool Qx, Qy, Qz, Qone, Qall, Qign = getbparam("ignore");
    bool Qhalf = getbparam("half");
    bool Qmaxpos = getbparam("maxpos");
    real nu, nppb = getdparam("nppb");
    int npar = getiparam("npar");
    int ngood;
    int ndat = 0;
    int nplanes;
    int min_count, max_count;
    int maxmom = getiparam("maxmom");
    int maxpos[2];
    char slabel[32];

    instr = stropen (getparam("in"), "r");
    read_image (instr,&iptr);
    strclose(instr);
    nx = Nx(iptr);	
    ny = Ny(iptr);
    nz = Nz(iptr);
    dprintf(1,"# data order debug:  %f %f\n",Frame(iptr)[0], Frame(iptr)[1]);
    if (hasvalue("tab")) tabstr = stropen(getparam("tab"),"w");

    planes = (int *) allocate((nz+1)*sizeof(int));
    nplanes = nemoinpi(getparam("planes"),planes,nz+1);
    Qall = (planes[0]==-1);
    if (planes[0]==0) {
      Qone = FALSE;
      nplanes = nz;
      for (k=0; k<nz; k++)
	planes[k] = k;
    } else if (!Qall) {
      Qone = (!Qall && nplanes==1);
      for (k=0; k<nplanes; k++) {
	if (planes[k]<1 || planes[k]>nz) error("%d is an illegal plane [1..%d]",planes[k],nz);
	planes[k]--;
      }
    }
 
    if (hasvalue("win")) {
      instr = stropen (getparam("win"), "r");
      read_image (instr,&wptr);
      strclose(instr);
      if (Nx(iptr) != Nx(wptr)) error("X sizes of in=/win= don't match");
      if (Ny(iptr) != Ny(wptr)) error("Y sizes of in=/win= don't match");
      if (Nz(iptr) != Nz(wptr)) error("Z sizes of in=/win= don't match");
      Qw = TRUE;
    } else
      Qw = FALSE;

    Qmin = hasvalue("min");
    if (Qmin) xmin = getdparam("min");
    Qmax = hasvalue("max");
    if (Qmax) xmax = getdparam("max");
    Qbad = hasvalue("bad");
    if (Qbad) bad = getdparam("bad");
    Qmedian = getbparam("median");
    Qrobust = getbparam("robust");
    Qtorben = getbparam("torben");
    if (Qtorben) Qmedian = TRUE;
    if (Qmedian || Qrobust || Qtorben) {
      ndat = nx*ny*nz;
      data = (real *) allocate(ndat*sizeof(real));
    }

    sov = 1.0;       /* volume of a pixel/voxel */
    Qx = Qign && Nx(iptr)==1;
    Qy = Qign && Ny(iptr)==1;
    Qz = Qign && Nz(iptr)==1;
    sov *= Qx ? 1.0 : Dx(iptr);
    sov *= Qy ? 1.0 : Dy(iptr);
    sov *= Qz ? 1.0 : Dz(iptr);
    strcpy(slabel,"*");
    if (!Qx) strcat(slabel,"Dx*");
    if (!Qy) strcat(slabel,"Dy*");
    if (!Qz) strcat(slabel,"Dz*");
    
    if (maxmom < 0) {
      warning("No work done, maxmom<0");
      stop(0);
    }

    if (Qall) {                 /* treat cube as one data block */

      ini_moment(&m,maxmom,ndat);
      ngood = 0;
      for (k=0; k<nz; k++) {
	for (j=0; j<ny; j++) {
	  for (i=0; i<nx; i++) {
            x =  CubeValue(iptr,i,j,k);
	    if (Qhalf && x>=0.0) continue;
            if (Qmin  && x<xmin) continue;
            if (Qmax  && x>xmax) continue;
            if (Qbad  && x==bad) continue;
	    w = Qw ? CubeValue(wptr,i,j,k) : 1.0;
            accum_moment(&m,x,w);
	    if (Qhalf && x<0) accum_moment(&m,-x,w);
	    if (Qmedian) data[ngood++] = x;
	    if (tabstr) fprintf(tabstr,"%g\n",x);
	  }
	}
      }
      
      if (npar > 0) {
	nu = n_moment(&m)/nppb - npar;
	if (nu < 1) error("%g: No degrees of freedom",nu);
	printf("chi2= %g\n", show_moment(&m,2)/nu/nppb);
	printf("df= %g\n", nu);
      } else {
	nsize = nx * ny * nz;
	sum = mean = sigma = skew = kurt = 0;
	if (maxmom > 0) {
	  mean = mean_moment(&m);
	  sum = show_moment(&m,1);
	}
	if (maxmom > 1)
	  sigma = sigma_moment(&m);
	if (maxmom > 2)
	  skew = skewness_moment(&m);
	if (maxmom > 3)
	  kurt = kurtosis_moment(&m);
	
	printf ("Number of points      : %d\n",n_moment(&m));
	printf ("Min and Max           : %f %f\n",min_moment(&m), max_moment(&m));
	printf ("Mean and dispersion   : %f %f\n",mean,sigma);
	printf ("Skewness and kurtosis : %f %f\n",skew,kurt);
	printf ("Sum and Sum*%s        : %f %f\n",slabel, sum, sum*sov);
	if (Qmedian) {
	  if (Qtorben) {
	    printf ("Median Torben         : %f (%d)\n",median_torben(ngood,data,min_moment(&m),max_moment(&m)),ngood);
	  } else {
	    printf ("Median                : %f\n",get_median(ngood,data));
	    q2 = median(ngood,data);
	    q1 = median_q1(ngood,data);
	    q3 = median_q3(ngood,data);
	    printf ("Q1,Q2,Q3              : %f %f %f\n",q1,q2,q3);
	  }
#if 1
	  if (ndat>0)
	    printf ("MedianL               : %f\n",median_moment(&m));
#endif
	}
	if (Qrobust) {
	  compute_robust_moment(&m);
	  printf ("Mean Robust           : %f\n",mean_robust_moment(&m));
	  printf ("Sigma Robust          : %f\n",sigma_robust_moment(&m));
	  printf ("Median Robust         : %f\n",median_robust_moment(&m));
	}

	if (Qmmcount) {
	  min_count = max_count = 0;
	  xmin = min_moment(&m);
	  xmax = max_moment(&m);
	  for (i=0; i<nx; i++) {
	    for (j=0; j<ny; j++) {
	      for (k=0; k<nz; k++) {
		x =  CubeValue(iptr,i,j,k);
		if (x==xmin) min_count++;
		if (x==xmax) max_count++;
	      }
	    }
	  } /* i */
	  printf("Min_Max_count         : %d %d\n",min_count,max_count);
	}
	printf ("%d/%d out-of-range points discarded\n",nsize-n_moment(&m), nsize);
      }

    } else {             /* treat each plane seperately */

      /* tabular output, one line per (selected) plane */

      printf("# iz z min  max  N  mean sigma skew kurt sum sumsov ");
      if (Qmedian) printf(" [med med]");
      if (Qrobust) printf(" robust[N mean sig med]");
      if (Qmaxpos) printf(" maxposx maxposy");
      printf("\n");

      ini_moment(&m,maxmom,ndat);
      for (ki=0; ki<nplanes; ki++) {
	reset_moment(&m);
	k = planes[ki];
	z = Zmin(iptr) + k*Dz(iptr);
	ngood = 0;
	for (j=0; j<ny; j++) {
	  for (i=0; i<nx; i++) {
            x =  CubeValue(iptr,i,j,k);
            if (Qmin && x<xmin) continue;
            if (Qmax && x>xmax) continue;
            if (Qbad && x==bad) continue;
	    if (Qmaxpos) {
	      if (i==0 && j==0) { dmax = x; maxpos[0] = 0; maxpos[1] = 0;}
	      else if (x>dmax) {  dmax = x; maxpos[0] = i; maxpos[1] = j;}
	    }
	    w = Qw ? CubeValue(wptr,i,j,k) : 1.0;
            accum_moment(&m,x,w);
	    if (Qmedian) data[ngood++] = x;
	  }
	}

	nsize = nx * ny * nz;
	sum = mean = sigma = skew = kurt = 0;
	if (maxmom > 0) {
	  mean = mean_moment(&m);
	  sum = show_moment(&m,1);
	}
	if (maxmom > 1)
	  sigma = sigma_moment(&m);
	if (maxmom > 2)
	  skew = skewness_moment(&m);
	if (maxmom > 3)
	  kurt = kurtosis_moment(&m);
	if (n_moment(&m) == 0) {
	  printf("# %d no data\n",k+1);
	  continue;
	}
	printf("%d %f  %f %f %d  %f %f %f %f  %f %f",
	       k+1, z, min_moment(&m), max_moment(&m), n_moment(&m),
	       mean,sigma,skew,kurt,sum,sum*sov);
	if (Qmedian) {
	  printf ("   %f",get_median(ngood,data));
	  if (ndat>0) printf (" %f",median_moment(&m));
	}
	if (Qrobust) {
	  compute_robust_moment(&m);
	  printf ("   %d %f %f %f",n_robust_moment(&m), mean_robust_moment(&m),
		  sigma_robust_moment(&m), median_robust_moment(&m));
	}
	if (Qmaxpos) {
	  printf("   %d %d",maxpos[0],maxpos[1]);
	}
#if 0	  
	if (Qmmcount) {
	  min_count = max_count = 0;
	  xmin = min_moment(&m);
	  xmax = max_moment(&m);
	  for (i=0; i<nx; i++) {
	    for (j=0; j<ny; j++) {
	      x =  CubeValue(iptr,i,j,k);
	      if (x==xmin) min_count++;
	      if (x==xmax) max_count++;
	    }
	  } /* i,j */
	  printf(" %d %d",min_count,max_count);
	}
	printf ("%d/%d out-of-range points discarded\n",nsize-n_moment(&m), nsize);
#endif
	printf("\n");
      } /* ki */
    }
}