void snapstack(bodyptr btab, bodyptr bt1, int nb1, bodyptr bt2, int nb2, string *tags) { vector deltar, deltav; bodyptr bp; int i; setvect(deltar, burststring(getparam("deltar"), ", ")); setvect(deltav, burststring(getparam("deltav"), ", ")); for (i = 0; i < nb1; i++) { bp = NthBody(btab, i); memcpy(bp, NthBody(bt1, i), SizeofBody); if (set_member(tags, PosTag)) { ADDMULVS(Pos(bp), deltar, 0.5); } if (set_member(tags, VelTag)) { ADDMULVS(Vel(bp), deltav, 0.5); } } for (i = 0; i < nb2; i++) { bp = NthBody(btab, i + nb1); memcpy(bp, NthBody(bt2, i), SizeofBody); if (set_member(tags, PosTag)) { ADDMULVS(Pos(bp), deltar, -0.5); } if (set_member(tags, VelTag)) { ADDMULVS(Vel(bp), deltav, -0.5); } } }
int main(int argc, string argv[]) { stream istr, ostr; string times, *vecs, *produce, iotags[MaxBodyFields]; bool expand; snapshot snap = { NULL, 0, 0.0 }; initparam(argv, defv); istr = stropen(getparam("in"), "r"); get_history(istr); ostr = stropen(getparam("out"), "w"); put_history(ostr); times = getparam("times"); vecs = burststring(getparam("vectors"), ", "); expand = streq(getparam("produce"), "*"); if (! expand) { produce = burststring(getparam("produce"), ", "); layout_body(produce, Precision, NDIM); } while (get_snapshot_timed(istr, snap, iotags, expand, times)) { eprintf("[%s: rotating time %f]\n", getprog(), snap.time); snaprotate(&snap, iotags, vecs, getparam("order"), getbparam("invert"), getdparam("thetax"), getdparam("thetay"), getdparam("thetaz")); put_snapshot(ostr, snap, iotags); skip_history(istr); } strclose(ostr); return (0); }
int main(int argc, string argv[]) { string *datafields, *bodyfields, times, ifmt, rfmt; stream istr; bodyptr btab = NULL; int nbody; real tnow; string intags[MaxBodyFields]; initparam(argv, defv); istr = stropen(getparam("in"), "r"); get_history(istr); datafields = burststring(getparam("fields"), ", "); if (set_member(datafields, TimeTag)) bodyfields = set_diff(datafields, set_cons(TimeTag, NULL)); else bodyfields = datafields; layout_body(bodyfields, Precision, NDIM); times = getparam("times"); ifmt = getparam("ifmt"); rfmt = getparam("rfmt"); print_header(datafields, getparam("hfmt"), getparam("keyhead"), burststring(getparam("auxhead"), ", ")); while (get_snap_t(istr, &btab, &nbody, &tnow, intags, FALSE, times)) { if (! set_subset(intags, bodyfields)) error("%s: one or more required fields not found\n", getargv0()); print_data(btab, nbody, tnow, datafields, ifmt, rfmt); skip_history(istr); } return (0); }
int nemoinpx( char *expr, double *a, int na) { int nret, ncomp; string *vals, *comp; vals = burststring(expr,","); for (nret=0; vals[nret] != NULL; nret++) { if (nret>=na) return -23; comp = burststring(vals[nret],":"); ncomp = xstrlen(comp,sizeof(string))-1; if (ncomp < 1 || ncomp > 3) return -13; a[nret] = atof(comp[0]); if (ncomp==1) continue; a[nret] += atof(comp[1])/60.0; if (ncomp==2) continue; a[nret] += atof(comp[2])/3600.0; freestrings(comp); } freestrings(vals); return nret; }
/* * return the full path to a filter based on a short name * or, if the shortname is the full path, return itself */ string filtername(string shortname) { int nsp; static char fullname[256]; char line[MAX_LINELEN]; string *sp, fpath; stream fstr; if (nemo_file_size(shortname) > 0) return shortname; fpath = getenv("NEMODAT"); if (fpath == 0) error("NEMODAT does not exist"); sprintf(fullname,"%s/filter/Fnames",fpath); dprintf(1,"Alias table %s\n",fullname); fstr = stropen(fullname,"r"); while (fgets(line,MAX_LINELEN,fstr)) { if (line[0] == '#') continue; sp = burststring(line," \n"); nsp = xstrlen(sp,sizeof(sp))-1; if (nsp > 1) { if (streq(shortname,sp[0])) { sprintf(fullname,"%s/filter/%s",fpath,sp[1]); dprintf(1,"Matching %s\n",fullname); freestrings(sp); return fullname; } } freestrings(sp); } return shortname; }
local void convert(stream instr, stream outstr) { char line[MAX_LINELEN]; /* input linelength */ int i, nlines, noutv; string *outv; /* pointer to vector of strings to write */ char *cp, *seps=", \t"; /* column separators */ nlines=0; /* count lines read so far */ for (;;) { if (get_line(instr, line) < 0) /* EOF */ return; dprintf(3,"LINE: (%s)\n",line); if (iscomment(line)) continue; nlines++; tab2space(line); /* work around a Gipsy (?) problem */ outv = burststring(line,seps); noutv = xstrlen(outv,sizeof(string)) - 1; if (noutv < maxcol) error("Too few columns in input file (%d < %d)",noutv,maxcol); if (Qall) nkeep = noutv; for (i=0; i<nkeep; i++) { if (keep[i] == 0) fprintf(outstr,"%d",nlines); else fprintf(outstr,"%s",outv[keep[i]-1]); if (i < nkeep-1) fprintf(outstr,"%c",colsep); } if (colsep != 'n') fprintf(outstr,"\n"); /* end of line */ } }
int nemoinpl( char *expr, long *a, int na) { int nret; string *vals, *burststring(); vals = burststring(expr,", "); for (nret=0; vals[nret] != NULL; nret++) { if (nret>=na) { return(-23); } a[nret] = atoi(vals[nret]); } return(nret); }
void nemo_main() { int n; stream parstr, outstr; string molfile = getparam("mol"); string outfile = getparam("out"); string outdir = getparam("outdir"); string exefile = "radex"; string datfile = "mol.dat"; string parfile = "input.txt"; string logfile = "radex.txt"; char dname[256], cmd[256]; real fmin = getdparam("fmin"); real fmax = getdparam("fmax"); real tkin = getdparam("tkin"); int npartner; string *partner; real density[MAXPARTNER]; real tbg = getdparam("tbg"); real cdmol = getdparam("cdmol"); real deltav = getdparam("deltav"); partner = burststring(getparam("partner"),","); npartner = xstrlen(partner,sizeof(string))-1; if (npartner < 1 || npartner>MAXPARTNER) error("too few/many partners"); n = nemoinpr(getparam("density"),density,MAXPARTNER); if (n != npartner) error("need matching numbers for partner= and density="); run_mkdir(outdir); sprintf(dname,"%s/%s",outdir,parfile); parstr = stropen(dname,"w"); fprintf(parstr,"%s\n",molfile); fprintf(parstr,"%s\n",outfile); fprintf(parstr,"%g %g\n",fmin,fmax); fprintf(parstr,"%g\n",tkin); fprintf(parstr,"%d\n",npartner); for (n=0; n<npartner; n++) { fprintf(parstr,"%s\n",partner[n]); fprintf(parstr,"%g\n",density[n]); } fprintf(parstr,"%g\n",tbg); fprintf(parstr,"%g\n",cdmol); fprintf(parstr,"%g\n",deltav); fprintf(parstr,"0\n"); strclose(parstr); run_cd(outdir); sprintf(cmd,"%s < %s > %s ", exefile, parfile, logfile); run_sh(cmd); }
nemo_main() { string *wp = burststring(getparam("saveparam"),", "); string *sp; for (sp=wp; *sp; sp++) { dprintf(0,"Saving %s\n",*sp); } }
int string_index(string options, string s) { int i=0; string *sa = burststring(options,","); while (sa[i]) { if (streq(sa[i],s)) { freestrings(sa); return i; } i++; } freestrings(sa); return -1; }
nemo_main() { imageptr iptr=NULL, optr=NULL; string *filename; stream instr, outstr; int i, j, i0, j0, nfiles; int nx, ny, nx1, ny1, ix, iy, n; nx = hasvalue("nx") ? getiparam("nx") : 0; ny = hasvalue("ny") ? getiparam("ny") : 0; filename = burststring(getparam("in")," ,\n"); nfiles = xstrlen(filename,sizeof(string))-1; if (nx==0 && ny==0) error("Need at least one of nx= or ny="); else if (nx==0) { nx = nfiles/ny; dprintf(0,"nx=%d ny=%d nfiles=%d\n",nx,ny,nfiles); if (nfiles % ny) error("nfiles=%d/ny=%d does not divide evenly",nfiles,ny); } else if (ny==0) { ny = nfiles/nx; dprintf(0,"nx=%d ny=%d nfiles=%d\n",nx,ny,nfiles); if (nfiles % nx) error("nfiles=%d/nx=%d does not divide evenly",nfiles,nx); } for (iy=0, n=0; iy<ny; iy++) { for (ix=0; ix<nx; ix++, n++) { instr = stropen (filename[n],"r"); read_image (instr,&iptr); /* read image */ strclose(instr); /* close image file */ if (n==0) { nx1 = Nx(iptr); ny1 = Ny(iptr); create_image(&optr,nx*nx1,ny*ny1); } i0 = ix*nx1; j0 = iy*ny1; for (j=0; j<ny1; j++) for (i=0; i<nx1; i++) MapValue(optr,i+i0,j+j0) = MapValue(iptr,i,j); i++; } } outstr = stropen(getparam("out"),"w"); write_image(outstr,optr); strclose(outstr); }
int nemoinpf( char *expr, float *a, int na) { int nret; string *vals; vals = burststring(expr,", "); for (nret=0; vals[nret] != NULL; nret++) { if (nret>=na) { return(-23); } a[nret] = atof(vals[nret]); } return(nret); }
int nemoinpb( char *expr, bool *a, int na) { int nret; string *vals; vals = burststring(expr,", "); for (nret=0; vals[nret] != NULL; nret++) { if (nret>=na) { return(-23); } a[nret] = (*vals[nret]=='t' || *vals[nret]=='T') ? TRUE : FALSE; } return(nret); }
int init_xrandom(string init) { #ifdef HAVE_GSL char my_gsl_type[64], my_gsl_seed[64], *cp; /* need two strings, bug in putenv? */ string *is; int nis, iseed; is = burststring(init,", "); /* parse init as "[seed[,name]]" */ nis = xstrlen(is,sizeof(string))-1; if (nis > 0) { /* seed is first, but optional */ iseed = natoi(is[0]); if (iseed > 0 || streq(is[0],"+0")) { sprintf(my_gsl_seed,"%s=%s",env_seed,is[0]); } else { iseed = set_xrandom(iseed); sprintf(my_gsl_seed,"%s=%u",env_seed,iseed); } putenv(my_gsl_seed); dprintf(1,"putenv: %s\n",my_gsl_seed); if (nis > 1) { /* name is second, also optional */ sprintf(my_gsl_type,"%s=%s",env_type,is[1]); putenv(my_gsl_type); dprintf(1,"putenv: %s\n",my_gsl_type); } } gsl_rng_env_setup(); /* initialize the rng (name/seed) setup */ my_T = gsl_rng_default; my_r = gsl_rng_alloc(my_T); dprintf(1,"GSL generator type: %s\n",gsl_rng_name(my_r)); dprintf(1,"GSL seed = %u\n",gsl_rng_default_seed); dprintf(1,"GSL first value = %u\n",gsl_rng_get(my_r)); return (int) gsl_rng_default_seed; #else return set_xrandom(init? natoi(init) : 0); /* 18/06/2008: allow for init=0 WD */ #endif }
void force_calc(void) { real *pp, *ap; double cpubase; string *rminxstr; int i; bodyptr bp; tol = getdparam("tol"); eps = getdparam("eps"); rsize = getdparam("rsize"); rminxstr = burststring(getparam("rmin"), ", "); if (xstrlen(rminxstr, sizeof(string)) < NDIM) { SETVS(rmin, - rsize / 2.0); } else for (i = 0; i < NDIM; i++) rmin[i] = atof(rminxstr[i]); dprintf(0,"initial rsize: %8f rmin: %8f %8f %8f\n", rsize, rmin[0], rmin[1], rmin[2]); fcells = getdparam("fcells"); phidata = pp = (real *) allocate(ntest * sizeof(real)); accdata = ap = (real *) allocate(ntest * NDIM * sizeof(real)); cpubase = cputime(); maketree(massdata, nmass); cputree = cputime() - cpubase; dprintf(0," final rsize: %8f rmin: %8f %8f %8f\n", rsize, rmin[0], rmin[1], rmin[2]); cpubase = cputime(); n2btot = nbctot = 0; for (bp = testdata; bp < testdata+ntest; bp++) { hackgrav(bp); *pp++ = Phi(bp); SETV(ap, Acc(bp)); ap += NDIM; n2btot += n2bterm; nbctot += nbcterm; } cpufcal = cputime() - cpubase; }
int main(int argc, string argv[]) { stream istr, ostr; string times, *produce, itags[MaxBodyFields], atags[MaxBodyFields]; bodyptr btab = NULL; int nbody, ntag, i; real tsnap; bool expand; initparam(argv, defv); istr = stropen(getparam("in"), "r"); get_history(istr); times = getparam("times"); expand = streq(getparam("produce"), "*"); if (! expand) { produce = burststring(getparam("produce"), ", "); layout_body(produce, Precision, NDIM); } atags[ntag = 0] = NULL; while (get_snap_t(istr, &btab, &nbody, &tsnap, itags, expand, times)) { for (i = 0; itags[i] != NULL; i++) if (! set_member(atags, itags[i])) { atags[ntag++] = itags[i]; atags[ntag] = NULL; } get_history(istr); } if (! expand) { for (i = 0; produce[i] != 0; i++) if (! set_member(atags, produce[i])) error("%s: field %s missing\n", getargv0(), produce[i]); } else produce = atags; ostr = stropen(getparam("out"), "w"); put_history(ostr); put_snap(ostr, &btab, &nbody, &tsnap, produce); strclose(ostr); return (0); }
void nemo_main() { stream istr; char msg[128]; int i, ndim; string *fn; setparams(); compfuncs(); /* btrrtans snapplot-like interface */ plinit("***", 0.0, 20.0, 0.0, 20.0); xaxis( 2.0, 2.0, 16.0, xrange, -7, xtrans, xlabel); xaxis( 2.0, 18.0, 16.0, xrange, -7, xtrans, NULL); yaxis( 2.0, 2.0, 16.0, yrange, -7, ytrans, ylabel); yaxis(18.0, 2.0, 16.0, yrange, -7, ytrans, NULL); sprintf(msg, "File: %s", input); pltext(msg, 2.0, 18.4, 0.32, 0.0); optr = NULL; ndim = NDIM; allocate_orbit(&optr,ndim,maxsteps); /* allocate a large orbit */ fn = burststring(input," ,"); for (i=0; fn[i]; i++) { /* loop over all files */ istr = stropen(fn[i], "r"); /* open orbit file */ while (read_orbit(istr,&optr)) { /* while an orbit found....*/ dprintf(0,"Read orbit with %d phase-points\n",Nsteps(optr)); plot_path(optr,trange[0],trange[1],nplot); Nsteps(optr) = maxsteps; /* reset for next orbit */ } strclose(istr); } plstop(); }
int main(int argc, string argv[]) { stream istr, ostr; string itags1[MaxBodyFields], itags2[MaxBodyFields]; string *produce, *otags; bodyptr btab1 = NULL, btab2 = NULL, btab; int nbody1, nbody2, nbody; real tsnap; initparam(argv, defv); if (! streq(getparam("produce"), "*")) { produce = burststring(getparam("produce"), ", "); layout_body(produce, Precision, NDIM); } else produce = NULL; istr = stropen(getparam("in1"), "r"); get_history(istr); if (! get_snap(istr, &btab1, &nbody1, &tsnap, itags1, produce == NULL)) error("%s: no data in 1st input file\n"); istr = stropen(getparam("in2"), "r"); get_history(istr); if (! get_snap(istr, &btab2, &nbody2, &tsnap, itags2, FALSE)) error("%s: no data in 2nd input file\n"); otags = set_inter(itags1, itags2); if (produce != NULL) checktags(otags, produce); eprintf("[%s: nbody = %d + %d]\n", getargv0(), nbody1, nbody2); nbody = nbody1 + nbody2; btab = (bodyptr) allocate(SizeofBody * nbody); snapstack(btab, btab1, nbody1, btab2, nbody2, otags); ostr = stropen(getparam("out"), "w"); put_history(ostr); put_snap(ostr, &btab, &nbody, &tsnap, otags); strclose(ostr); return (0); }
convert(stream instr, stream outstr) { char line[MAX_LINELEN]; /* input linelength */ double dval; int nval, i, nlines, sign, decimalval,nhms,ndms; string *outv, *hms, *dms; /* pointer to vector of strings to write */ string seps=", \t"; /* column separators */ real dd, mm, ss; nlines=0; /* count lines read so far */ for(;;) { /* loop over all lines in file(s) */ if (get_line(instr, line) < 0) return 0; dprintf(3,"LINE: (%s)\n",line); if(line[0] == '#') continue; /* don't use comment lines */ nlines++; outv = burststring(line,seps); i=0; while (outv[i]) { if (colmode[i+1] == 0) { /* no special mode: just copy */ if (fromhms[i+1]) { hms = burststring(outv[i],sep); nhms = xstrlen(hms,sizeof(string))-1; dval = atof(hms[0])*3600; if (nhms > 1) dval += atof(hms[1])*60; if (nhms > 2) dval += atof(hms[2]); if (nhms > 3) error("HMS string %s too many %s",outv[i],sep); fprintf(outstr,fmt,dval/3600.0); fputs(" ",outstr); freestrings(hms); } else if (fromdms[i+1]) { dms = burststring(outv[i],sep); ndms = xstrlen(dms,sizeof(string))-1; dval = atof(dms[0])*3600; if (ndms > 1) dval += atof(dms[1])*60; if (ndms > 2) dval += atof(dms[2]); if (ndms > 3) error("DMS string %s too many %s",outv[i],sep); fprintf(outstr,fmt,dval*15/3600.0); fputs(" ",outstr); freestrings(dms); } else { fputs(outv[i],outstr); fputs(" ",outstr); } } else { if (nemoinpd(outv[i],&dval,1)<0) error("syntax error decoding %s",outv[i]); if (colmode[i+1] < 0) { sign = SGN(dval); dval = ABS(dval); /* do we allow < 0 ??? */ decimalval = (int)floor(dval); fprintf(outstr,"%d",decimalval*sign); fprintf(outstr,"%s",sep); dval -= decimalval; dval *= ABS(colmode[i+1]); } sign = SGN(dval); dval = ABS(dval); dd = floor(dval); dval = (dval-dd)*60.0; mm = floor(dval); ss = (dval-mm)*60.0; fprintf(outstr,"%02d",(int)dd*sign); fprintf(outstr,"%s",sep); fprintf(outstr,"%02d",(int)mm); fprintf(outstr,"%s",sep); fprintf(outstr,"%06.3f ",ss); } i++; } fputs("\n",outstr); freestrings(outv); } /* for(;;) */ }
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); }
void nemo_main(void){ int i,numwritten=0,nummissed=0; int numimages=0; stream fp_in,fp,fp2; string *sp,logic; int count,n; int edge_dist; /* exclude sources this many pixels from the edge */ string inFile; /* name of input file */ char line[MAX_LINELEN]; /* a line of text from the inFile */ int racol,deccol,maxcol; /* column numbers for ra/dec in file */ double ra,dec; /* the values of ra/dec for a given line */ int nentries; /* number of valid entries, used for progress tracking */ inFile = getparam("in"); edge_dist = getiparam("edge"); logic = getparam("logic"); sp = burststring(getparam("col"),", \n"); n = xstrlen(sp,sizeof(string)) - 1; if (n != 2) error("You must specify ra,dec columns for col keyword!"); racol = natoi(sp[0]) - 1; deccol = natoi(sp[1]) - 1; freestrings(sp); if (racol >= deccol) maxcol = racol; else maxcol = deccol; sp = burststring(getparam("fits"),", \n"); n = xstrlen(sp,sizeof(string))-1; for (i=0; i<n; i++){ ini_mask(sp[i],i); numimages++; } freestrings(sp); sp = burststring(getparam("out"),", \n"); n = xstrlen(sp,sizeof(string))-1; if (n == 1){ fp = stropen(sp[0],"w"); fp2 = stropen("/dev/null","w!"); } else if (n == 2){ fp = stropen(sp[0],"w"); fp2 = stropen(sp[1],"w"); }else error("You must give one or two output files!\n"); freestrings(sp); nentries = nemo_file_lines(inFile,0); progress_init("overlap percent complete:",nentries); nentries = 0; fp_in = stropen(inFile,"r"); while (fgets(line,MAX_LINELEN,fp_in)){ progress_update(nentries); if (line[0] != '#'){ nentries += 1; sp = burststring(line," \t\n"); n = xstrlen(sp,sizeof(string)) - 2; /* zero based */ if (n < maxcol) error("Insufficient columns on line!"); ra = natof(sp[racol]); dec = natof(sp[deccol]); freestrings(sp); count = 0; for (i=0; i< numimages; i++) count+= is_edge(ra,dec,edge_dist,i); if(streq(logic,"or") && (count<numimages)){ fprintf(fp,line); numwritten++; } else if(streq(logic,"and") && (count==0) && (numimages > 0)){ fprintf(fp,line); numwritten++; } else { fprintf(fp2,line); nummissed++; } } } strclose(fp_in); strclose(fp); strclose(fp2); progress_finish(); printf("Sources outside regions: %d\n",nummissed); printf("Sources inside regions: %d\n",numwritten); printf("Number of images: %d\n",numimages); }
void nemo_main(void) { stream instr, outstr, nullstr; string item, *items; string *tags, *sels, *cvt; int i, nsel, select[MAXSEL], cntrd, cntwr; bool all; instr = stropen(getparam("in"), "r"); nullstr = stropen("/dev/null","w+"); /* kludge: force write */ item = getparam("item"); sels = burststring(getparam("select"),", "); nsel = xstrlen(sels, sizeof(string)) - 1; for (i=0; i<nsel; i++) { /* note: array should be sorted */ select[i] = atoi(sels[i]); dprintf(1,"#selected #%d\n",select[i]); if (select[i]<1) error("%d: bad number for select=, must be > 0",select[i]); if (i>0 && (select[i]<select[i-1])) error("%d: bad number for select=, array must be sorted",select[i]); } cvt = burststring(getparam("convert"),", "); if (*item == 0) { all = TRUE; items = (string *) allocate(sizeof(string)); /* PPAP */ items[0] = NULL; } else { all = FALSE; items = burststring(item,", "); if (items==NULL) error("error parsing item=%s\n",item); dprintf(1,"selected: "); for (i=0; items[i]!=NULL; i++) dprintf(1," %s",items[i]); dprintf(1,"\n\n"); } outstr = stropen(getparam("out"), "w"); cntrd = 0; /* keep track of items read */ cntwr = 0; /* and written */ while ((tags = list_tags(instr)) != NULL) { if (check(items, *tags)) { cntrd++; dprintf(1," %d: %s",cntrd,*tags); if (nsel==0 || (nsel>0 && select[cntwr] == cntrd)) { copy_item_cvt(outstr, instr, *tags, cvt); cntwr++; dprintf(1," copied\n"); } else { copy_item(nullstr, instr, *tags); dprintf(1," skipped\n"); } } else { dprintf(1," *: %s",*tags); copy_item(nullstr, instr, *tags); dprintf(1," skipped\n"); } free((char *)*tags); free((char *)tags); if (nsel>0 && cntwr >= nsel) /* early bailout ? */ break; } dprintf(0,"On top-level: %d items read, %d items written\n",cntrd,cntwr); strclose(instr); strclose(outstr); }
int main(int argc, string argv[]) { string *produce, snap_tags[MaxBodyFields], frame_tags[MaxBodyFields]; snapshot *snaplist = NULL, *ssp, frame = { NULL, 0, 0.0, NULL }; stream instr, outstr; int n, nbody, i; bodyptr btab, bp, sp; bool roff, voff; initparam(argv, defv); produce = burststring(getparam("produce"), ", "); layout_body(produce, Precision, NDIM); instr = stropen(getparam("in"), "r"); get_history(instr); snaplist = ssp = (snapshot *) allocate(sizeof(snapshot)); n = nbody = 0; while (get_snapshot(instr, *ssp, snap_tags, FALSE)) { if (! set_equal(snap_tags, produce)) error("%s: input file lacks required items\n", getprog()); n++; nbody += ssp->nbody; get_history(instr); ssp->link = (snapshot *) allocate(sizeof(snapshot)); ssp = ssp->link; } strclose(instr); eprintf("[%s: read %d frames, %d bodies]\n", getprog(), n, nbody); if (! strnull(getparam("frame"))) { instr = stropen(getparam("frame"), "r"); get_history(instr); if (! get_snapshot(instr, frame, frame_tags, FALSE)) error("%s: no snapshot in frame file\n", getprog()); strclose(instr); if (frame.nbody < n) error("%s: need at least %d points in frame\n", getprog(), n); } roff = (frame.nbody > 0) && set_member(frame_tags, PosTag); voff = (frame.nbody > 0) && set_member(frame_tags, VelTag); bp = btab = (bodyptr) allocate(nbody * SizeofBody); ssp = snaplist; for (i = 0; i < n; i++) { sp = ssp->bodies; while (sp < NthBody(ssp->bodies, ssp->nbody)) { memmove((void *) bp, (void *) sp, (size_t) SizeofBody); if (roff) { ADDV(Pos(bp), Pos(bp), Pos(NthBody(frame.bodies, i))); } if (voff) { ADDV(Vel(bp), Vel(bp), Vel(NthBody(frame.bodies, i))); } bp = NextBody(bp); sp = NextBody(sp); } ssp = ssp->link; } outstr = stropen(getparam("out"), "w"); put_history(outstr); put_snap(outstr, &btab, &nbody, &frame.time, produce); strclose(outstr); return (0); }
int main(int argc, string argv[]) { stream istr, ostr, gstr; real tnow, tgrav, eps2, tstop, dtime, tout, Mhqm, ahqm, bhqm, tol; real decrit, epot0, demin, demax, derms, de2avg, enow, denow; int nbody, ngrav; bodyptr btab = NULL, gtab = NULL, bp; string bdtags[MaxBodyFields], grtags[MaxBodyFields], *optags; gsprof *gravgsp = NULL; bool decrit_inc = FALSE; initparam(argv, defv); new_field(&EinitPBF, RealType, EinitTag); // define initial energy field layout_body(bodytags, Precision, NDIM); // layout necessary fields istr = stropen(getparam("in"), "r"); get_history(istr); if (! get_snap(istr, &btab, &nbody, &tnow, bdtags, TRUE)) error("%s: can't read input snapshot\n", getprog()); if (! (set_member(bdtags, PosTag) && set_member(bdtags, VelTag))) error("%s: required data missing from input snapshot\n", getprog()); #if defined(NBDGRAV) gstr = stropen(getparam("grav"), "r"); get_history(gstr); if (! get_snap(gstr, >ab, &ngrav, &tgrav, grtags, FALSE)) error("%s: can't read gravity snapshot\n", getprog()); if (! (set_member(grtags, MassTag) && set_member(grtags, PosTag))) error("%s: required data missing from gravity snapshot\n", getprog()); eps2 = rsqr(getdparam("eps")); #elif defined(GSPGRAV) gstr = stropen(getparam("grav"), "r"); get_history(gstr); gravgsp = get_gsprof(gstr); // read GSP for grav. field #elif defined(HQMGRAV) Mhqm = getdparam("M"); ahqm = getdparam("a"); bhqm = getdparam("b"); tol = getdparam("tol"); #endif ostr = stropen(getparam("out"), "w"); put_history(ostr); tstop = getdparam("tstop"); dtime = getdparam("dtime"); decrit = getdparam("decrit"); optags = burststring(getparam("outputs"), ","); #if defined(NBDGRAV) sumforces(btab, nbody, gtab, ngrav, eps2); // prime the pump... #elif defined(GSPGRAV) gspforces(btab, nbody, gravgsp); #elif defined(HQMGRAV) hqmforces(btab, nbody, Mhqm, ahqm, bhqm, tol); #endif epot0 = 0.0; // use as energy scale for (bp = btab; bp < NthBody(btab, nbody); bp = NextBody(bp)) { epot0 += Phi(bp) / nbody; // compute avg. potential Einit(bp) = Phi(bp) + dotvp(Vel(bp), Vel(bp)) / 2; } eprintf("[%s: initial average potential = %g]\n", getprog(), epot0); put_snap(ostr, &btab, &nbody, &tnow, optags); fflush(NULL); tout = tnow + getdparam("dtout"); demin = demax = derms = 0.0; // track maximum errors while (tnow < tstop) { // enter main loop for (bp = btab; bp < NthBody(btab, nbody); bp = NextBody(bp)) { ADDMULVS(Vel(bp), Acc(bp), 0.5 * dtime); // step velocities by dt/2 ADDMULVS(Pos(bp), Vel(bp), dtime); // step positions by dt } tnow = tnow + dtime; // step time to new value #if defined(NBDGRAV) if (! getbparam("frozen")) if (! get_snap(gstr, >ab, &ngrav, &tgrav, grtags, TRUE)) error("%s: can't read gravity snapshot\n", getprog()); sumforces(btab, nbody, gtab, ngrav, eps2); // get new accelerations #elif defined(GSPGRAV) gspforces(btab, nbody, gravgsp); #elif defined(HQMGRAV) hqmforces(btab, nbody, Mhqm, ahqm, bhqm, tol); #endif de2avg = 0.0; for (bp = btab; bp < NthBody(btab, nbody); bp = NextBody(bp)) { ADDMULVS(Vel(bp), Acc(bp), 0.5 * dtime); // step velocities by dt/2 enow = 0.5 * dotvp(Vel(bp), Vel(bp)) + Phi(bp); denow = (enow - Einit(bp)) / ABS(epot0); // compute rel. energy change demin = MIN(demin, denow); demax = MAX(demax, denow); de2avg += rsqr(denow) / nbody; } derms = MAX(derms, rsqrt(de2avg)); if (demin < -decrit || demax > decrit) { eprintf("[%s: warning: energy error exceeds %.4e at time = %-12.8f\n" " min,max,rms = %.6g,%.6g,%.6g threshold now %.4e]\n", getprog(), decrit, tnow, demin, demax, rsqrt(de2avg), decrit * rsqrt(2.0)); decrit = decrit * rsqrt(2.0); decrit_inc = TRUE; } if (tout <= tnow) { put_snap(ostr, &btab, &nbody, &tnow, optags); tout = tout + getdparam("dtout"); } fflush(NULL); } eprintf(decrit_inc ? "[%s: WARNING: energy error: min,max,rms = %.6g,%.6g,%.6g]\n" : "[%s: energy error: min,max,rms = %.6g,%.6g,%.6g]\n", getprog(), demin, demax, derms); return (0); }
void nemo_main() { int i,j,nentries,ncol,nsp; FILE *fp; /* File pointer for output file */ string outfile; /* name of output file */ string *sp; /* used to read the command line argument "col" */ string wcs_type; /* WCS projection type (can be XXX or -XXX) */ char type[5]; /* the real WCS for worldpos() ; needs to be '-XXX' */ int nx,ny; /* size of map in pixels for x,y directions */ double index,rzero; /* parameters for powerlaw */ double crval1,crval2; /* center RA and DEC */ double cdelt1,cdelt2; /* delta RA and delta DEC for pixels */ double crpix1,crpix2; /* reference pixel */ double xpix, ypix; /* current x and y pixel position in map */ double ra,dec; /* ra,dec of current pixel position in map */ double flux; /* computed flux for each pixel */ double radius; /* radius, in pixels */ /* Read command line */ index = getdparam("index"); rzero = getdparam("rzero"); outfile = getparam("out"); wcs_type = getparam("type"); if (strlen(wcs_type)==3) { /* make sure type is '-XXX' */ type[0] = '-'; strcpy(&type[1],wcs_type); } else if (strlen(wcs_type)==4) { if (*wcs_type == '-') strcpy(type,wcs_type); else error("type=%s probably not legal, should be -XXX",wcs_type); } else error("type=%s must be a valid -XXX or XXX WCS descriptor",wcs_type); sp = burststring(getparam("crval")," ,"); nsp = xstrlen(sp,sizeof(string))-1; if (nsp != 2) error("You must specify two values for the crval keyword!"); crval1 = natof(sp[0]); crval2 = natof(sp[1]); freestrings(sp); sp = burststring(getparam("naxis")," ,"); nsp = xstrlen(sp,sizeof(string))-1; if (nsp != 2) error("You must specify two values for the naxis keyword!"); nx = natoi(sp[0]); ny = natoi(sp[1]); freestrings(sp); crpix1 = nx/2.0+1; crpix2 = ny/2.0+1; cdelt2 = getdparam("cdelt")/3600.; cdelt1 = -cdelt2; /* Test to make sure we can write to output file */ fp = stropen(outfile,"w"); strclose(fp); fprintf(stderr,"nx,ny = %d,%d\n",nx,ny); fprintf(stderr,"crval1,crval2=%lf,%lf\n",crval1,crval2); fprintf(stderr,"crpix1,crpix2=%lf,%lf\n",crpix1,crpix2); fprintf(stderr,"wcs: %s\n",type); xpix=1.0; ypix=1.0; worldpos(xpix,ypix,crval1,crval2,crpix1,crpix2,cdelt1,cdelt2,0.0,type,&ra,&dec); dprintf(1,"LL corner: %g %g\n",ra,dec); xpix=nx; worldpos(xpix,ypix,crval1,crval2,crpix1,crpix2,cdelt1,cdelt2,0.0,type,&ra,&dec); dprintf(1,"LR corner: %g %g\n",ra,dec); ypix=ny; worldpos(xpix,ypix,crval1,crval2,crpix1,crpix2,cdelt1,cdelt2,0.0,type,&ra,&dec); dprintf(1,"UR corner: %g %g\n",ra,dec); xpix=1.0; worldpos(xpix,ypix,crval1,crval2,crpix1,crpix2,cdelt1,cdelt2,0.0,type,&ra,&dec); dprintf(1,"UL corner: %g %g\n",ra,dec); /* Write header for TABFITS to file */ fp = stropen(getparam("out"),"w!"); fprintf(fp,"# NAXIS1 = %d\n",nx); fprintf(fp,"# NAXIS2 = %d\n",ny); fprintf(fp,"# NAXIS3 = 1\n"); fprintf(fp,"# CDELT1 = %lf\n",cdelt1); fprintf(fp,"# CDELT2 = %lf\n",cdelt2); fprintf(fp,"# CRVAL1 = %lf\n",crval1); fprintf(fp,"# CRVAL2 = %lf\n",crval2); fprintf(fp,"# CRPIX1 = %f\n",crpix1); fprintf(fp,"# CRPIX2 = %f\n",crpix2); fprintf(fp,"# CTYPE1 = 'RA--%s'\n",type); fprintf(fp,"# CTYPE2 = 'DEC-%s'\n",type); fprintf(fp,"# COMMENT index=%g / powerlaw index\n",index); fprintf(fp,"# COMMENT rzero=%g / rzero parameter\n",rzero); fprintf(fp,"# COMMENT out=%s / output table\n",outfile); fprintf(fp,"# COMMENT crval=%f,%f / Center WCS coordinates\n",crval1,crval2); fprintf(fp,"# COMMENT naxis=%d,%d / Image size in pixels\n",nx,ny); fprintf(fp,"# COMMENT cdelt=%f / pixel size in arcseconds\n",cdelt2*3600); fprintf(fp,"# COMMENT type=%s / Projection type\n",type); fprintf(fp,"# COMMENT VERSION=%s / Version number\n",getparam("VERSION")); for (i=0; i< ny; i++){ ypix = i + 1.0; for (j=0; j< nx; j++){ xpix = j + 1.0; worldpos(xpix,ypix,crval1,crval2,crpix1,crpix2,cdelt1,cdelt2,0.0, type,&ra,&dec); radius = sqrt((xpix-crpix1)*(xpix-crpix1) + (ypix-crpix2)*(ypix-crpix2)); flux = 1.0/(1.0 + pow(radius/rzero,index)); /* if (radius <= 4) flux = 1.0; else flux = 1.0/(1.0 + pow(radius-4,index)); */ fprintf(fp,"%lf %lf %4.2e\n",ra,dec,flux); } } strclose(fp); }
nemo_main() { stream istr; float fval; double dval; int i, k, l, n, n2, iw, i1, i2, lineno, ival; string *sp, *idf0, *pars; char *cp, *cp1, line[MAX_LINELEN]; bool Qline = getbparam("lineno"); bool Qshow_idf; bool Qtype = getbparam("checktype"); bool Qreport = getbparam("report"); int argc, nidf, nidf2, nw, nopen, nrow; IDF *idf; string *argv, *av, *w; cstring *csp, *csn; if (hasvalue("idf")) { n = nemo_file_lines(getparam("idf"),0); dprintf(1,"%s : nlines=%d\n",getparam("idf"),n); idf0 = (string *) allocate((n+1)*sizeof(string)); istr = stropen(getparam("idf"),"r"); for (i=0; i<n; i++) { if (fgets(line,MAX_LINELEN,istr) == NULL) error("short file"); strip_newline(line); idf0[i] = strdup(line); } idf0[n] = 0; strclose(istr); } else { warning("Testing IDF"); idf0 = testidf; } /* report, and pre-parse and count the true IDF's */ dprintf(1,"report IDF\n"); Qshow_idf = !hasvalue("out"); nidf = 0; for (sp = idf0, lineno=0; *sp; sp++) { if (*sp[0] == '#') continue; lineno++; if (Qshow_idf) { if (Qline) printf("%d: %s\n",lineno,*sp); else printf("%s\n",*sp); } w = burststring(*sp," \t"); nw = xstrlen(w,sizeof(string))-1; for (i=0; i<nw; i++) { if (*w[i] == '#') break; cp = strchr(w[i],':'); if (cp==NULL) error("Missing : on %s",w[i]); nidf++; } } dprintf(0,"Found %d IDF_parameters in %d lines in idf file\n",nidf,lineno); /* now fully parse IDF */ idf = (IDF *) allocate(nidf*sizeof(IDF)); nidf2 = 0; nopen = 0; nrow = 0; for (sp = idf0; *sp; sp++) { if (*sp[0] == '#') continue; nrow++; if (nopen) error("Cannot handle any parameters after an open ended array"); w = burststring(*sp," \t"); nw = xstrlen(w,sizeof(string))-1; for (i=0; i<nw; i++) { if (*w[i] == '#') break; cp = strchr(w[i],':'); if (cp==NULL) error("Missing : on %s",w[i]); *cp++ = 0; /* now w[i] points to type; cp to keyword, possibly with a [] */ idf[nidf2].type = strdup(w[i]); idf[nidf2].key = strdup(cp); idf[nidf2].row = nrow; idf[nidf2].col = i+1; cp1 = strchr(cp,'['); if (cp1) { *cp1++ = 0; idf[nidf2].key = strdup(cp); if (*cp1 == ']') { if (nopen) error("Cannot handle more than one open ended array"); idf[nidf2].nvals = 0; nopen++; } else error("fixed dimensioned arrays not allowed yet : %s",cp); } else { idf[nidf2].key = strdup(cp); idf[nidf2].nvals = -1; } nidf2++; } } if (nidf2 != nidf) error("nidf2=%d != nidf=%d\n",nidf2,nidf); /* report the full IDF */ if (Q) { for (i=0; i<nidf; i++) { dprintf(1,"###: [%d,%d] %s %s\n", idf[i].row, idf[i].col, idf[i].type, idf[i].key); } } if (hasvalue("par")) { pars = line_open_file(getparam("par")); n2 = xstrlen(pars,sizeof(string))-1; dprintf(0,"Found %d lines in par file\n",n2); if (Q) if (n2 != lineno) warning("par file not same as idf"); for (l=0, i=0; l<n2; l++) { /* loop over all lines : l counts lines, i counts idf's */ /* idf[i] is the current IDF */ /* pars[l] is the current line */ /* idf[i].row should match the current line */ /* a comment spans (by definition) the whole line */ if (i<nidf && streq(idf[i].type,IDF_COMMENT)) { idf[i].out = strdup(pars[l]); idf[i].cout.val = strdup(pars[l]); idf[i].cout.nxt = 0; i++; continue; } /* for now any types are not comments, and so treated same way */ w = burststring(pars[l]," \t"); nw = xstrlen(w,sizeof(string))-1; #if 1 /* just do it, no checking */ for (iw=0; iw<nw; iw++) { if (*w[iw] == '#') break; if (i < nidf) { idf[i].out = strdup(w[iw]); idf[i].cout.val = strdup(w[iw]); idf[i].cout.nxt = 0; } else { if (nopen==0) error("Too many values, and no open ended array"); csn = (cstring *) allocate(sizeof(cstring)); csn->val = strdup(w[iw]); csn->nxt = 0; csp = &idf[nidf-1].cout; while (csp->nxt) csp = csp->nxt; csp->nxt = csn; } i++; } if (Q) { if (i==nidf) { warning("end of idf %d %d",l,n2); if (l<n2-1) warning("not exhausting lines in par file"); } } #else /* messy double checking */ for (i1=i; i<nidf && idf[i1].row==idf[i].row; i1++) { dprintf(0,"i=%d i1=%d row=%d row1=%d\n",i,i1,idf[i].row,idf[i1].row); } n2 = i1-i; if (nw != n2) error("nw=%d != n2=%d i=%d l=%d (%s)",nw,n2,i,l,pars[l]); for (i1=i, iw=0; i<nidf && idf[i1].row==idf[i].row; i1++, iw++) { idf[i1].out = strdup(w[iw]); } #endif } if (Q) { for (i=0; i<nidf; i++) { printf("###: [%d,%d] %-3s %-10s = %s\n", idf[i].row, idf[i].col, idf[i].type, idf[i].key, idf[i].out); } } } /* report the command line tail after the -- */ dprintf(1,"getargv\n"); argv = getargv(&argc); dprintf(1," argc=%d\n",argc); if (argc>0) { argv++; /* skip the -- */ if (Qtype) warning("Type checking not implemented yet"); for (av = argv; *av; av++) { if (Q) printf("arg: %s\n",*av); cp = strchr(*av,'='); if (cp) { *cp++ = 0; for (i=0; i<nidf; i++) { if (streq(*av,idf[i].key)) { dprintf(1,"Patching key=%s with val=%s\n",*av,cp); if (idf[i].nvals < 0) { idf[i].out = strdup(cp); idf[i].cout.val = strdup(cp); idf[i].cout.nxt = 0; } else { w = burststring(cp," ,"); nw = xstrlen(w,sizeof(string))-1; idf[i].out = strdup(w[0]); idf[i].cout.val = strdup(cp); idf[i].cout.nxt = 0; csn = &idf[i].cout; for (iw=0; iw<nw; iw++) { csn->val = strdup(w[iw]); if (iw<nw-1) { csn->nxt = (cstring *) allocate(sizeof(cstring)); csn = csn->nxt; } else csn->nxt = 0; } } break; } } if (i==nidf) error("%d: key=%s not registered in IDF",i,*av); } else { error("cannot handle arugments yet that are not key=val"); } } } if (Q) { for (i=0; i<nidf; i++) { printf(">>>: [%d,%d] %-3s %-10s = %s\n", idf[i].row, idf[i].col, idf[i].type, idf[i].key, idf[i].out); } } if (hasvalue("out")) { istr = stropen(getparam("out"),"w"); for (i=0; i<nidf; i++) { if (i>0 && idf[i].row != idf[i-1].row) fprintf(istr,"\n"); if (idf[i].col > 1) fprintf(istr," "); if (streq(idf[i].type,"qs") && idf[i].out[0] != '\'') fprintf(istr,"'%s'",idf[i].out); else fprintf(istr,"%s",idf[i].out); } fprintf(istr,"\n"); if (nopen) { csp = idf[nidf-1].cout.nxt; while (csp) { fprintf(istr,"%s\n",csp->val); csp = csp->nxt; } } strclose(istr); } if (Qreport) { for (i=0; i<nidf; i++) { printf("%s=%s\n",idf[i].key,idf[i].out); } } }
convert(stream instr, stream outstr) { char line[MAX_LINELEN]; /* input linelength */ int nlines, nwords, nskip; long long nt0, nt; real dt; struct tm tm, tm0; string *bp; if (time0) { tm0.tm_sec= tm0.tm_min = tm0.tm_hour = 0; strptime(time0,format1,&tm0); strftime(line,MAX_LINELEN,"%s",&tm0); nt0 = atoll(line); dprintf(0,"Using time0=%lld sec since 1970.0 using %s on %s \n", nt0,format1,time0); } nlines=0; /* count lines read so far */ nskip=0; for(;;) { /* loop over all lines in file(s) */ if (get_line(instr, line) < 0) return 0; dprintf(3,"LINE: (%s)\n",line); if(line[0] == '#') continue; /* don't use comment lines */ nlines++; tm.tm_sec= tm.tm_min = tm.tm_hour = 0; if (dcol>0) { bp = burststring(line,", "); /* split line in words */ nwords = xstrlen(bp,sizeof(string))-1; if (nwords<dcol) { nskip++; continue; } strcpy(line,bp[dcol-1]); /* only pick out 1 column now */ freestrings(bp); } strptime(line,format1,&tm); if (need_time0) { dprintf(0,"First line: %s\n",line); strftime(line,MAX_LINELEN,"%s",&tm); time0 = strdup(line); nt0 = atoll(time0); dprintf(0,"First line uses: time0=%lld sec since 1970.0\n",nt0); need_time0 = FALSE; } strftime(line,MAX_LINELEN,format2,&tm); if (time0) { nt = atoll(line); if (use_scale) { dt = (nt-nt0)/scale; sprintf(line,"%g",dt); } else sprintf(line,"%lld",nt-nt0); } fputs(line,outstr); fputs("\n",outstr); } /* for(;;) */ dprintf(1,"Processed %d lines\n",nlines); if (nskip) warning("%d/%d lines skipped because dcol too large",nskip,nlines); }
void nemo_main() { string *sp; /* string pointer for output of burststring */ int nsp; /* number of tokens in sp after using burststring */ double magj,magh,magks; /* JHKs magnitudes */ double sigmaj,sigmah,sigmaks; /* JHKs uncertainties in magnitudes */ double sum_jh,sum_jh2; /* Sums for J-H weighted averages */ double sum_hks,sum_hks2; /* Sums for H-Ks weighted averages */ double w_jh,w_hks,w_jhhks; /* sums of weights for J-H,H-Ks, and covariance */ double w_jh2,w_hks2,w_jhhks2; /* sums of square of weights for J-H,H-Ks, and cov. */ double sum_jhhks; /* for covariance matrix */ double sum_jhalt,sum_hksalt; /* for covariance matrix */ double tmp1,tmp2,tmp3; /* temp variables */ double avg_jh,avg_hks; /* average values of J-H,H-Ks */ double std_jh,std_hks; /* Std. deviations of J-H,H-Ks*/ double cov; /* covariance of J-H and H-Ks */ double no_jh,no_hks,no_jhhks; /* unweighted sums */ double no_jh2,no_hks2; /* unweighted sums */ int num; /* number of sources read */ stream fp; /* File pointer */ char line[MAX_LINELEN]; /* Character string for reading from file */ /* Initialize variables */ sum_jh = 0; sum_jh2 = 0; sum_hks = 0; sum_hks2 = 0; sum_jhhks = 0; sum_jhalt = 0; sum_hksalt= 0; w_jh = 0; w_hks = 0; w_jhhks = 0; w_jh2 = 0; w_hks2 = 0; w_jhhks2 = 0; num = 0; no_jh = 0; no_hks = 0; no_jhhks = 0; no_jh2 = 0; no_hks2 = 0; fp = stropen(getparam("in"),"r"); while (fgets(line,MAX_LINELEN,fp)){ if (line[0] != '#'){ /* lines starting with # are comments */ sp = burststring(line," \t\n"); nsp = xstrlen(sp,sizeof(string))-1; if (nsp < 8) warning("Skipping line: %s",line); magj = natof(sp[2]); sigmaj = natof(sp[3]); magh = natof(sp[4]); sigmah = natof(sp[5]); magks = natof(sp[6]); sigmaks = natof(sp[7]); num += 1; tmp1 = 1.0/(sigmaj*sigmaj + sigmah*sigmah); tmp2 = 1.0/(sigmah*sigmah + sigmaks*sigmaks); tmp3 = sqrt(tmp1*tmp2); sum_jh += tmp1*(magj - magh); sum_jh2 += tmp1*(magj - magh)*(magj - magh); sum_hks += tmp2*(magh - magks); sum_hks2 += tmp2*(magh - magks)*(magh - magks); sum_jhhks += tmp3*(magj - magh)*(magh - magks); sum_jhalt += tmp3*(magj - magh); sum_hksalt += tmp3*(magh - magks); no_jh += (magj - magh); no_jh2 += (magj - magh)*(magj - magh); no_hks += (magh - magks); no_hks2 += (magh - magks)*(magh - magks); no_jhhks += (magj - magh)*(magh - magks); w_jh += tmp1; w_hks += tmp2; w_jhhks += tmp3; w_jh2 += tmp1*tmp1; w_hks2 += tmp2*tmp2; w_jhhks2 += tmp3*tmp3; freestrings(sp); } } strclose(fp); avg_jh = sum_jh/w_jh; avg_hks = sum_hks/w_hks; std_jh = sqrt(w_jh/(w_jh*w_jh - w_jh2)*(sum_jh2 - avg_jh*avg_jh*w_jh)); std_hks = sqrt(w_hks/(w_hks*w_hks - w_hks2)*(sum_hks2 - avg_hks*avg_hks*w_hks)); cov = sum_jhhks - avg_hks*sum_jhalt - avg_jh*sum_hksalt + avg_jh*avg_hks*w_jhhks; cov = w_jhhks/(w_jhhks*w_jhhks - w_jhhks2)*cov; fprintf(stdout,"Weighted:\n"); fprintf(stdout,"Average J-H = %5.3e +- %5.3e\n",avg_jh,std_jh); fprintf(stdout,"Average H-Ks = %5.3e +- %5.3e\n",avg_hks,std_hks); fprintf(stdout,"Covariance = %5.3e\n",cov); avg_jh = no_jh/num; avg_hks = no_hks/num; std_jh = sqrt((no_jh2 - avg_jh*avg_jh*num)/(num - 1)); std_hks = sqrt((no_hks2 - avg_hks*avg_hks*num)/(num - 1)); no_jhhks = (no_jhhks- avg_jh*avg_hks*num)/(num - 1); fprintf(stdout,"Unweighted:\n"); fprintf(stdout,"Average J-H = %5.3e +- %5.3e\n",avg_jh,std_jh); fprintf(stdout,"Average H-Ks = %5.3e +- %5.3e\n",avg_hks,std_hks); fprintf(stdout,"Covariance = %5.3e\n",no_jhhks); }
void nemo_main() { stream instr, tabstr = stdout; real tsnap, dr, aux; string times; Body *btab = NULL, *bp, *bq; bool Qhead, Qfirst = TRUE; int i, n, nbody, bits, nsep, isep, nopt, ParticlesBit; char fmt[20],*pfmt; string *opt; rproc_body fopt[MAXOPT]; #if defined(SINGLEPREC) warning("SINGLEPREC mode: data will not be in full precision"); #endif ParticlesBit = (MassBit | PhaseSpaceBit | PotentialBit | AccelerationBit | AuxBit | KeyBit | DensBit | EpsBit); instr = stropen(getparam("in"), "r"); /* open input file */ Qhead = getbparam("header"); opt = burststring(getparam("options"),", "); 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 options = %d exhausted\n",MAXOPT); break; } } if (Qhead) { fprintf(tabstr,"# "); for (i=0; i<nopt; i++) fprintf(tabstr,"%s ",opt[i]); fprintf(tabstr,"\n"); } 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 */ #if 0 get_snap(instr, &btab, &nbody, &tsnap, &bits); if (!streq(times,"all") && !within(tsnap,times,0.0001)) continue; /* skip work on this snapshot */ #else get_snap_by_t(instr, &btab, &nbody, &tsnap, &bits, times); #endif if ( (bits & ParticlesBit) == 0) continue; /* skip work, only diagnostics here */ if (Qfirst) /* add blank line between snapshots */ Qfirst = FALSE; else fprintf(tabstr," \n"); fprintf(tabstr,"%d\n",nbody); fprintf(tabstr,fmt,tsnap); fprintf(tabstr,"\n"); for (bp = btab, i=0; bp < btab+nbody; bp++, i++) { for (n=0; n<nopt; n++) { aux = fopt[n](bp,tsnap,i); fprintf(tabstr,fmt,aux); } fprintf(tabstr,"\n"); } } strclose(instr); }
int parse(FILE *fp, FILE *tp, FILE *sp, int maxw, Widget *w) { char *cp, *vp, line[MAXLINELEN]; char t[64], tframe[64], tkey[64], tval[64], tvar[64], tbut[64]; string *wp, *ip; int i, nip, nw, len, cdeflen, pdeflen; cdeflen = strlen(keycdef); pdeflen = strlen(keypdef); nw = 0; fprintf(sp,"# This file is temporary; for merging into main tkrun script\n"); while (fgets(line,MAXLINELEN,fp) != NULL) { len = strlen(line); if (line[len-1]=='\n') line[len-1]='\0'; if (strncmp(line,keycdef,cdeflen)==0) { /* if line starts with "#<" */ cp = strchr(line,'-'); if (cp) { if (nw==0) continue; if (w[nw-1].opt) fprintf(stderr,"Warning: overwriting %s for %s\n", w[nw-1].opt, w[nw-1].key); w[nw-1].opt = (char *) strdup(cp); } continue; } if ( (cp=strstr(line,keypdef)) ) { fprintf(tp,"%s <#\n",cp); wp = burststring(cp," \t"); if (wp[0]==0 || wp[1]==0) continue; if (strcmp(wp[1],"ENTRY")==0) { vp = strchr(wp[2],'='); if (vp==NULL) { fprintf(tp,"### Warning: missing '=' in keyword %s\n",wp[1]); continue; } *vp++ = 0; w[nw].type = W_ENTRY; w[nw].key = (char *) strdup(wp[2]); w[nw].val = (char *) strdup(vp); w[nw].par = 0; w[nw].opt = 0; w[nw].help = 0; w[nw].list = 1; sprintf(t, ".keys.%s", w[nw].key); sprintf(tframe, ".keys.%s.frame", w[nw].key); sprintf(tkey, ".keys.%s.key", w[nw].key); sprintf(tval, ".keys.%s.frame.val", w[nw].key); sprintf(tvar, "var_%s", w[nw].key); fprintf(tp,"frame %s -borderwidth {2} -relief {raised}\n",t); fprintf(tp,"pack %s\n",t); fprintf(tp,"frame %s\n",tframe); fprintf(tp,"global %s\n",tvar); if (*w[nw].val) fprintf(tp,"set %s %s\n",tvar,w[nw].val); fprintf(tp,"entry %s -textvariable %s -relief {sunken}\n", tval,tvar); fprintf(tp,"pack append %s %s {top frame center expand fill}\n", tframe,tval); fprintf(tp,"label %s -text %s:\n",tkey,w[nw].key); fprintf(tp,"bind %s <1> \"my_help 0 $%s\"\n",tkey,tvar); fprintf(tp,"bind %s <2> \"my_help 1 $%s\"\n",tkey,tvar); fprintf(tp,"bind %s <3> \"set %s \\\"\\\"; focus %s\"\n",tkey,tvar,tval); fprintf(tp,"pack append %s %s %s %s %s\n", t,tkey, "{left frame center filly}", tframe,"{top frame center fillx}"); fprintf(tp,"pack append .keys %s %s\n", t, "{top frame center fill}"); nw++; } else if (strcmp(wp[1],"IFILE")==0 || strcmp(wp[1],"OFILE")==0) { vp = strchr(wp[2],'='); if (vp==NULL) { fprintf(tp,"### Warning: missing '=' in keyword %s\n",wp[1]); continue; } *vp++ = 0; w[nw].type = W_IFILE; w[nw].key = (char *) strdup(wp[2]); w[nw].val = (char *) strdup(vp); w[nw].par = 0; w[nw].opt = 0; w[nw].help = 0; w[nw].list = 0; sprintf(t, ".keys.%s", w[nw].key); sprintf(tframe, ".keys.%s.frame", w[nw].key); sprintf(tkey, ".keys.%s.key", w[nw].key); sprintf(tval, ".keys.%s.frame.val", w[nw].key); sprintf(tbut, ".keys.%s.frame.but", w[nw].key); sprintf(tvar, "var_%s", w[nw].key); fprintf(tp,"frame %s -borderwidth {2} -relief {raised}\n",t); fprintf(tp,"pack %s\n",t); fprintf(tp,"frame %s\n",tframe); fprintf(tp,"global %s\n",tvar); if (*w[nw].val) fprintf(tp,"set %s %s\n",tvar,w[nw].val); fprintf(tp,"entry %s -textvariable %s -relief {sunken}\n", tval,tvar); fprintf(tp,"button %s -text Browse... -command new_%s\n", tbut,tvar); fprintf(tp,"pack append %s %s %s %s %s\n", tframe, tbut, "{left frame center filly}", tval, "{top frame center expand fill}"); fprintf(tp,"label %s -text %s:\n",tkey,w[nw].key); fprintf(tp,"bind %s <1> \"my_help 0 $%s\"\n",tkey,tvar); fprintf(tp,"bind %s <2> \"my_help 1 $%s\"\n",tkey,tvar); fprintf(tp,"bind %s <3> \"set %s \\\"\\\"; focus %s\"\n",tkey,tvar,tval); fprintf(tp,"pack append %s %s %s %s %s\n", t,tkey, "{left frame center filly}", tframe,"{top frame center fillx}"); fprintf(tp,"pack append .keys %s %s\n", t, "{top frame center fill}"); /* write new_ */ fprintf(sp,"# For: %s\n",line); fprintf(sp,"proc new_%s {} {\n",tvar); fprintf(sp," global %s\n",tvar); if (strcmp(wp[1],"IFILE")==0) fprintf(sp," set %s [tk_getOpenFile -parent %s]\n", tvar,tval); else if (strcmp(wp[1],"OFILE")==0) fprintf(sp," set %s [tk_getSaveFile -parent %s]\n", tvar,tval); fprintf(sp," focus %s\n",tval); fprintf(sp,"}\n"); nw++; } else if (strcmp(wp[1],"SCALE")==0) { vp = strchr(wp[2],'='); if (vp==NULL) { fprintf(tp,"### Warning: missing '=' in keyword %s\n",wp[1]); continue; } *vp++ = 0; w[nw].type = W_SCALE; w[nw].key = (char *) strdup(wp[2]); w[nw].val = (char *) strdup(vp); w[nw].par = (char *) strdup(wp[3]); w[nw].opt = 0; w[nw].help = 0; w[nw].list = 0; sprintf(t, ".keys.%s", w[nw].key); sprintf(tframe, ".keys.%s.frame", w[nw].key); sprintf(tkey, ".keys.%s.key", w[nw].key); sprintf(tval, ".keys.%s.frame.val", w[nw].key); sprintf(tvar, "var_%s", w[nw].key); if (*w[nw].val) fprintf(tp,"set %s %s\n",tvar,w[nw].val); fprintf(tp,"frame %s -borderwidth {2} -relief {raised}\n",t); fprintf(tp,"pack %s\n",t); fprintf(tp,"frame %s\n",tframe); fprintf(tp,"global %s\n",tvar); fprintf(tp,"scale %s -variable %s -relief {sunken}",tval,tvar); ip = burststring(wp[3],":,"); if (ip[0]) { fprintf(tp," -from %s",ip[0]); if (ip[1]) { fprintf(tp," -to %s",ip[1]); if (ip[2]) fprintf(tp," -resolution %s",ip[2]); } } fprintf(tp," -length 10c -orient horizontal\n"); fprintf(tp,"pack append %s %s {top frame center expand fill}\n", tframe,tval); fprintf(tp,"label %s -text %s:\n",tkey,w[nw].key); fprintf(tp,"bind %s <1> \"my_help 0 $%s\"\n",tkey,tvar); fprintf(tp,"bind %s <2> \"my_help 1 $%s\"\n",tkey,tvar); fprintf(tp,"bind %s <3> \"set %s %s; focus %s\"\n",tkey,tvar,ip[0],tval); fprintf(tp,"pack append %s %s %s %s %s\n", t,tkey, "{left frame center filly}", tframe,"{top frame center fillx}"); fprintf(tp,"pack append .keys %s %s\n", t, "{top frame center fill}"); nw++; } else if (strcmp(wp[1],"RADIO")==0) { vp = strchr(wp[2],'='); if (vp==NULL) { fprintf(tp,"### Warning: missing '=' in keyword %s\n",wp[1]); continue; } *vp++ = 0; w[nw].type = W_RADIO; w[nw].key = (char *) strdup(wp[2]); w[nw].val = (char *) strdup(vp); w[nw].par = (char *) strdup(wp[3]); w[nw].opt = 0; w[nw].help = 0; w[nw].list = 0; sprintf(t, ".keys.%s", w[nw].key); sprintf(tframe, ".keys.%s.frame", w[nw].key); sprintf(tkey, ".keys.%s.key", w[nw].key); sprintf(tval, ".keys.%s.frame.val", w[nw].key); sprintf(tvar, "var_%s", w[nw].key); fprintf(tp,"frame %s -borderwidth {2} -relief {raised}\n",t); fprintf(tp,"pack %s\n",t); fprintf(tp,"frame %s\n",tframe); fprintf(tp,"global %s\n",tvar); if (*w[nw].val) fprintf(tp,"set %s %s\n",tvar,w[nw].val); ip = burststring(wp[3],","); for (i=0; ip[i] != 0; i++) { fprintf(tp,"radiobutton %s.%s -variable %s -text %s -value %s -anchor w \n", tframe,ip[i],tvar,ip[i],ip[i]); } nip = i; fprintf(tp,"pack append %s ",tframe); for (i=0; ip[i] != 0; i++) { fprintf(tp," %s.%s %s", tframe,ip[i], (i < nip-1 ? "{left frame center filly}" : "{top frame center fillx}")); } fprintf(tp,"\n"); fprintf(tp,"label %s -text %s:\n",tkey,w[nw].key); fprintf(tp,"bind %s <1> \"my_help 0 $%s\"\n",tkey,tvar); fprintf(tp,"bind %s <2> \"my_help 1 $%s\"\n",tkey,tvar); fprintf(tp,"# is bug in next line fixed now?\n"); fprintf(tp,"bind %s <3> \"set %s \\\"%s\\\"\"\n",tkey,tvar,ip[0]); fprintf(tp,"pack append %s %s %s %s %s\n", t,tkey, "{left frame center filly}", tframe,"{top frame center fillx}"); fprintf(tp,"pack append .keys %s %s\n", t, "{top frame center fill}"); nw++; } else if (strcmp(wp[1],"CHECK")==0) { /* tricky one; the 'load' from file needs special handling */ vp = strchr(wp[2],'='); if (vp==NULL) { fprintf(tp,"### Warning: missing '=' in keyword %s\n",wp[1]); continue; } *vp++ = 0; w[nw].type = W_CHECK; w[nw].key = (char *) strdup(wp[2]); w[nw].val = (char *) strdup(vp); w[nw].par = (char *) strdup(wp[3]); w[nw].opt = 0; w[nw].help = 0; w[nw].list = 0; sprintf(t, ".keys.%s", w[nw].key); sprintf(tframe, ".keys.%s.frame", w[nw].key); sprintf(tkey, ".keys.%s.key", w[nw].key); sprintf(tval, ".keys.%s.frame.val", w[nw].key); sprintf(tvar, "var_%s", w[nw].key); fprintf(tp,"frame %s -borderwidth {2} -relief {raised}\n",t); fprintf(tp,"pack %s\n",t); fprintf(tp,"frame %s\n",tframe); fprintf(tp,"global %s\n",tvar); fprintf(tp,"set %s %s\n",tvar,w[nw].val); if (*w[nw].val) { fprintf(tp,"foreach i [split %s \",\"] {\n",w[nw].val); fprintf(tp," set %s_$i 1\n",tvar); fprintf(tp,"}\n"); } ip = burststring(wp[3],","); for (i=0; ip[i] != 0; i++) { fprintf(tp,"checkbutton %s.%s -variable %s_%s -text %s -anchor w -command new_%s \n", tframe,ip[i],tvar,ip[i],ip[i],tvar); fprintf(tp,"set arr_%s(%s_%s) %s\n",tvar,tvar,ip[i],ip[i]); fprintf(tp,"global %s_%s\n",tvar,ip[i]); } fprintf(tp,"global arr_%s\n",tvar); nip = i; /* write new_ */ fprintf(sp,"# For: %s\n",line); fprintf(sp,"proc new_%s {} {\n",tvar); fprintf(sp," global arr_%s %s\n",tvar,tvar); fprintf(sp," set %s \"\"\n",tvar); for (i=0; ip[i] != 0; i++) { fprintf(sp," global %s_%s\n",tvar,ip[i]); fprintf(sp," if {$%s_%s} { lappend %s $arr_%s(%s_%s)}\n", tvar,ip[i], tvar, tvar, tvar, ip[i]); } fprintf(sp," set %s [join $%s \",\"]\n",tvar,tvar); fprintf(sp,"}\n"); fprintf(sp,"# For: %s\n",line); fprintf(sp,"proc load_%s {} {\n",tvar); fprintf(sp," global arr_%s %s\n",tvar,tvar); for (i=0; ip[i] != 0; i++) { fprintf(sp," global %s_%s\n",tvar,ip[i]); } fprintf(sp," foreach i [split \"%s\" \",\"] {\n",wp[3]); fprintf(sp," set %s_$i 0\n",tvar); fprintf(sp," }\n"); fprintf(sp," foreach i [split $%s \",\"] {\n",tvar); fprintf(sp," set %s_$i 1\n",tvar); fprintf(sp," }\n"); fprintf(sp,"}\n"); fprintf(tp,"pack append %s ",tframe); for (i=0; ip[i] != 0; i++) { fprintf(tp," %s.%s %s", tframe,ip[i], (i<nip-1 ? "{left frame center filly}" : "{top frame center fillx}")); } fprintf(tp,"\n"); fprintf(tp,"label %s -text %s:\n",tkey,w[nw].key); fprintf(tp,"bind %s <1> \"my_help 0 $%s\"\n",tkey,tvar); fprintf(tp,"bind %s <2> \"my_help 1 $%s\"\n",tkey,tvar); fprintf(tp,"bind %s <3> \"",tkey); for (i=0; ip[i] != 0; i++) fprintf(tp,"set %s_%s 0;",tvar,ip[i]); fprintf(tp,"new_%s\"\n",tvar); fprintf(tp,"pack append %s %s %s %s %s\n", t,tkey, "{left frame center filly}", tframe,"{top frame center fillx}"); fprintf(tp,"pack append .keys %s %s\n", t, "{top frame center fill}"); nw++; } else { fprintf(tp,"### Warning: unknown tag %s\n",wp[1]); fprintf(stderr,"### Warning: skipping unknown tag %s\n",wp[1]); continue; } } /* if keypdef */ } /* while fgets() */ return nw; }