void execmap(string prog) { int mappid, mapstat; char histbuf[512]; mappid = fork(); if (mappid == 0) { // if this is child process sprintf(histbuf, "HISTORY=%s", *ask_history()); execl(prog, getargv0(), getparam("in"), getparam("out"), getparam("times"), getparam("require"), getparam("produce"), getparam("passall"), getparam("seed"), histbuf, NULL); error("%s: execl %s failed\n", getargv0(), prog); } while (wait(&mapstat) != mappid) eprintf("[%s: waiting on subprocess %d]\n", getargv0(), mappid); }
/* ----------------------------------------------------------------- | init_io_one : | Initilalize the NEMO engine and some variables. +----------------------------------------------------------------- */ void init_io_one(int * maxbodies, bool * read_one, bool * save_one, bool * set_history, char **history_prog, int MAXIO) { string defv[] = { "none=none","VERSION=1.52",NULL }; string argv[] = { "IO_NEMO",NULL }; int i; string * histo; static bool first=FALSE; initparam(argv,defv); if (first) { first=FALSE; /* initialyze files control arrays */ for (i=0; i< MAXIO; i++) { maxbodies[i] = 0; read_one[i] = FALSE; save_one[i] = FALSE; //set_history[i] = FALSE; } } for (i=0; i< MAXIO; i++) { maxbodies[i] = 0; read_one[i] = FALSE; save_one[i] = FALSE; set_history[i] = FALSE; } /* get command line history */ histo = (char **) ask_history(); *history_prog = (char * ) allocate_pointer(*history_prog, sizeof(char)*(strlen(histo[0])+1)); /* save command line history */ strcpy(*history_prog, histo[0]); /*fprintf(stderr,"history prog : <%s> \n", *history_prog); */ }
void write_fits(string name,imageptr iptr) { FLOAT tmpr,xmin[3],xref[3],dx[3],mapmin,mapmax; /* fitsio FLOAT !!! */ FITS *fitsfile; char *cp, origin[80]; string *hitem, axname[3]; float *buffer, *bp; int i, j, k, axistype, bitpix, keepaxis[3], nx[3], p[3], nx_out[3], ndim=3; double bscale, bzero; if (hasvalue("ndim")) ndim = getiparam("ndim"); nx[0] = Nx(iptr); nx[1] = Ny(iptr); nx[2] = Nz(iptr); if (nx[2] <= 0) nx[2] = 1; xmin[0] = Xmin(iptr)*scale[0]; xmin[1] = Ymin(iptr)*scale[1]; xmin[2] = Zmin(iptr)*scale[2]; dx[0] = Dx(iptr)*scale[0]; dx[1] = Dy(iptr)*scale[1]; dx[2] = Dz(iptr)*scale[2]; xref[0] = Xref(iptr)+1.0; xref[1] = Yref(iptr)+1.0; xref[2] = Zref(iptr)+1.0; axistype = Axis(iptr); axname[0] = (Namex(iptr) ? Namex(iptr) : xyz[0]); axname[1] = (Namey(iptr) ? Namey(iptr) : xyz[1]); axname[2] = (Namez(iptr) ? Namez(iptr) : xyz[2]); mapmin = MapMin(iptr); mapmax = MapMax(iptr); if (Qdummy) for (i=0; i<3; i++) p[i] = i; else { if (Qrefmap) warning("dummy=f and usage of refmap will result in bad headers"); permute(nx,p,3); dprintf(0,"Reordering axes: %d %d %d\n",p[0],p[1],p[2]); } #if 1 for (i=0; i<3; i++) nx_out[i] = nx[p[i]]; /* fix this so CubeValue works */ Nx(iptr) = nx_out[0]; Ny(iptr) = nx_out[1]; Nz(iptr) = nx_out[2]; #else for (i=0; i<3; i++) nx_out[i] = nx[i]; #endif sprintf(origin,"NEMO ccdfits %s",getparam("VERSION")); dprintf(1,"NEMO Image file written to FITS disk file\n"); dprintf(1,"%d %d %d %f %f %f %f %f %f %f %f %f %f %f \n", nx[0],nx[1],nx[2],xmin[0],xmin[1],xmin[2],dx[0],dx[1],dx[2],xref[0],xref[1],xref[2], mapmin,mapmax); dprintf(1,"keepaxis(%d,%d,%d)\n",keepaxis[0],keepaxis[1],keepaxis[2]); fit_setblocksize(2880*getiparam("blocking")); bitpix = getiparam("bitpix"); fit_setbitpix(bitpix); if (bitpix == 16) { /* scale from -2^(bitpix-1) .. 2^(bitpix-1)-1 */ bscale = (mapmax - mapmin) / (2.0*32768.0 - 1.0); bzero = mapmax - bscale*32767.0; fit_setscale(bscale,bzero); } else if (bitpix == 32) { bscale = (mapmax - mapmin) / (2.0*2147483648.0 - 1.0); bzero = mapmax - bscale*2147483647.0; fit_setscale(bscale,bzero); } else if (bitpix == 8) { bscale = (mapmax - mapmin) / (2.0*128.0 - 1.0); bzero = mapmin; fit_setscale(bscale,bzero); } dprintf(1,"bscale,bzero=%g %g\n",bscale,bzero); fitsfile = fitopen(name,"new",ndim,nx_out); if (fitsfile==NULL) error("Could not open fitsfile %s for writing\n",name); if (Qrefmap || Qcrpix) { fitwrhdr(fitsfile,"CRPIX1",ref_crpix[0]); fitwrhdr(fitsfile,"CRPIX2",ref_crpix[1]); if (ndim>2) fitwrhdr(fitsfile,"CRPIX3",ref_crpix[2]); } else { if (axistype==1) { fitwrhdr(fitsfile,"CRPIX1",xref[0]); fitwrhdr(fitsfile,"CRPIX2",xref[1]); if (ndim>2) fitwrhdr(fitsfile,"CRPIX3",xref[2]); } else { fitwrhdr(fitsfile,"CRPIX1",1.0); /* CRPIX = 1 by Nemo definition */ fitwrhdr(fitsfile,"CRPIX2",1.0); if (ndim>2) fitwrhdr(fitsfile,"CRPIX3",1.0); } } if (Qrefmap || Qcrval) { fitwrhdr(fitsfile,"CRVAL1",ref_crval[0]); fitwrhdr(fitsfile,"CRVAL2",ref_crval[1]); if (ndim>2) fitwrhdr(fitsfile,"CRVAL3",ref_crval[2]); } else { fitwrhdr(fitsfile,"CRVAL1",xmin[p[0]]); fitwrhdr(fitsfile,"CRVAL2",xmin[p[1]]); if (ndim>2) fitwrhdr(fitsfile,"CRVAL3",xmin[p[2]]); } if (Qcdmatrix) { fitwrhdr(fitsfile,"CD1_1",dx[p[0]]); fitwrhdr(fitsfile,"CD2_2",dx[p[1]]); if (ndim>2) fitwrhdr(fitsfile,"CD3_3",dx[p[2]]); } else { if (Qrefmap || Qcdelt) { fitwrhdr(fitsfile,"CDELT1",ref_cdelt[0]*scale[0]); fitwrhdr(fitsfile,"CDELT2",ref_cdelt[1]*scale[1]); if (ndim>2) fitwrhdr(fitsfile,"CDELT3",ref_cdelt[2]*scale[2]); } else { fitwrhdr(fitsfile,"CDELT1",dx[p[0]]); fitwrhdr(fitsfile,"CDELT2",dx[p[1]]); if (ndim>2) fitwrhdr(fitsfile,"CDELT3",dx[p[2]]); } } if (Qradecvel) { dprintf(0,"[Axes names written as %s, %s, %s\n", radeve[p[0]],radeve[p[1]],radeve[p[2]]); fitwrhda(fitsfile,"CTYPE1",radeve[p[0]]); fitwrhda(fitsfile,"CTYPE2",radeve[p[1]]); if (ndim>2) fitwrhda(fitsfile,"CTYPE3",radeve[p[2]]); } else { if (Qrefmap) { fitwrhda(fitsfile,"CTYPE1",ref_ctype[0]); fitwrhda(fitsfile,"CTYPE2",ref_ctype[1]); if (ndim>2) fitwrhda(fitsfile,"CTYPE3",ref_ctype[2]); } else { fitwrhda(fitsfile,"CTYPE1",axname[p[0]]); fitwrhda(fitsfile,"CTYPE2",axname[p[1]]); if (ndim>2) fitwrhda(fitsfile,"CTYPE3",axname[p[2]]); } } fitwrhdr(fitsfile,"DATAMIN",mapmin); fitwrhdr(fitsfile,"DATAMAX",mapmax); fitwrhda(fitsfile,"ORIGIN",origin); cp = getenv("USER"); /* AUTHOR */ if (cp) fitwrhda(fitsfile,"AUTHOR",cp); else fitwrhda(fitsfile,"AUTHOR","NEMO"); if (object) /* OBJECT */ fitwrhda(fitsfile,"OBJECT",object); if (comment) /* COMMENT */ stuffit(fitsfile,"COMMENT",comment); if (headline) stuffit(fitsfile,"COMMENT",headline); hitem = ask_history(); /* HISTORY */ fitwra(fitsfile,"HISTORY","NEMO: History in reversed order"); for (i=0, cp=hitem[0]; cp != NULL; i++) { stuffit(fitsfile,"HISTORY",cp); cp = hitem[i+1]; } for(i=0; i<nfill; i++) /* debugging header I/O */ fitwra(fitsfile,"COMMENT","Dummy filler space"); buffer = (float *) allocate(nx[p[0]]*sizeof(float)); for (k=0; k<nx_out[2]; k++) { /* loop over all planes */ fitsetpl(fitsfile,1,&k); for (j=0; j<nx_out[1]; j++) { /* loop over all rows */ for (i=0, bp=buffer; i<nx_out[0]; i++, bp++) *bp = iscale[0] * CubeValue(iptr,i,j,k) + iscale[1]; fitwrite(fitsfile,j,buffer); } } free(buffer); fitclose(fitsfile); }
void nemo_main() { stream instr, outstr; int i, n, naxis1, naxis2, naxis[2], moment; double edges[2]; struct fits_header fh; struct my_table_header r; char *record, *cp, mesg[80]; string *hitem; FITS *map; /* Setup */ instr = stropen(getparam("in"),"r"); /* open input */ moment = getiparam("moment"); if (moment < 1 || moment > 2) error("moment must be 1 or 2"); band = getiparam("band"); if (band < 1 || band > 10) { band = band_id(getdparam("band")); if (band < 1) error("Invalid DIRBE band"); } naxis[0] = nlon = getiparam("nlong"); naxis[1] = nlat = getiparam("nlat"); grid = (entryptr *) allocate(nlat*nlon*sizeof(entryptr)); for (i=0; i<nlat*nlon; i++) grid[i] = NULL; cp = getparam("coord"); switch (*cp) { case 'g': gal_coord = TRUE; break; case 'e': gal_coord = FALSE; break; default: error("Bad coordinate system choosen; try gal or ecl"); } if (nemoinpd(getparam("long"),edges,2) != 2) error("long= needs 2 values"); if (edges[0] <= edges[1]) error("long= needs left edge to be largest"); lonmin = edges[0]; lonmax = edges[1]; dlon = (lonmax-lonmin)/(float)nlon; if (nemoinpd(getparam("lat"),edges,2) != 2) error("lat= needs 2 values"); if (edges[0] >= edges[1]) error("lat= needs right edge to be largest"); latmin = edges[0]; latmax = edges[1]; dlat = (latmax-latmin)/(float)nlat; dprintf(1,"GridSize: %d * %d Pixels: %g * %g\n",nlon,nlat,dlon,dlat); gc_middle = (lonmax < 0.0 && lonmin > 0.0); /* see if to use SYM_ANGLE */ ncell = getiparam("ncell"); sigma2 = 2*sqr(getdparam("sigma")); /* Open output FITS file, and write a small yet descriptive enough header */ map = fitopen(getparam("out"),"new",2,naxis); fitwrhda(map,"CTYPE1", gal_coord ? "GLON" : "ELON"); fitwrhdr(map,"CRPIX1",(float) 1.0); /* should use center */ fitwrhdr(map,"CRVAL1",(float) (lonmin + 0.5 * dlon)); fitwrhdr(map,"CDELT1",(float) dlon); fitwrhda(map,"CTYPE2", gal_coord ? "GLAT" : "ELAT"); fitwrhdr(map,"CRPIX2",(float) 1.0); /* should use center */ fitwrhdr(map,"CRVAL2",(float) (latmin + 0.5 * dlat)); fitwrhdr(map,"CDELT2",(float) dlat); fitwrhda(map,"TELESCOP","COBE"); fitwrhda(map,"INSTRUME","DIRBE"); fitwrhda(map,"ORIGIN","NEMO processing on CDAC data"); fitwrhda(map,"BUNIT","MJy/sr"); sprintf(mesg,"NEMO: %s VERSION=%s",getargv0(),getparam("VERSION")); fitwra(map,"HISTORY", mesg); hitem = ask_history(); fitwra(map,"HISTORY","NEMO: History in reversed order"); for (i=0, cp=hitem[0]; cp != NULL; i++) { fitwral(map,"HISTORY",cp); cp = hitem[i+1]; /* point to next history item */ } /* Open input file, and process all rows */ fts_zero(&fh); /* clean out header */ n = fts_rhead(&fh,instr); /* read primary header */ if (n<0) error("Error reading primary HDU"); fts_sdata(&fh,instr); /* and skip data .. */ fts_zero(&fh); /* clean out header */ n = fts_rhead(&fh,instr); /* read primary header */ if (n<0) error("Error reading second HDU"); naxis1 = fh.naxisn[0]; /* size of one row */ naxis2 = fh.naxisn[1]; /* number of rows */ record = allocate(naxis1); for (i=0; i<naxis2; i++) { /* loop reading rows */ n = fread(record,1,naxis1,instr); if (n != naxis1) error("Early EOF on record %d",i+1); stuffit(&fh,record,&r); } printf("Used %d/%d points in gridding\n",nused,naxis2); /* map the data on a grid */ mapit(map,moment); /* finish off */ fitclose(map); strclose(instr); }