/* process this command */ static int ProcessCmd(char *cmd, char **args, int narg, int node, int tty) { char tbuf[SZ_LINE]; Finfo finfo, tfinfo; #if HAVE_CFITSIO int xlims[2], ylims[2], bin, got, hdutype, hdunum, ncard; int status=0, tstatus=0; int dims[2]; double cens[2]; char extname[FLEN_CARD]; char *cols[2] = {"X", "Y"}; char *ofile="stdout"; char *section=NULL; char *filter=NULL; char *slice=NULL; char *cardstr=NULL; void *tcens=NULL; fitsfile *ifptr, *ofptr, *tfptr; #endif switch(*cmd){ case 'f': if( !strcmp(cmd, "fitsFile") ){ if( narg ){ if( !(tfinfo=FinfoLookup(args[0])) ){ fprintf(stderr, NOIMAGE, args[0]); return 1; } } else if( !(tfinfo=FinfoGetCurrent()) ){ fprintf(stderr, NOFINFO, cmd); return 1; } if( tfinfo->fitsfile ){ if( node ) fprintf(stdout, "fitsFile\r"); fprintf(stdout, "%s %s\n", tfinfo->fname, tfinfo->fitsfile); fflush(stdout); } return 0; } break; break; case 'i': if( !strcmp(cmd, "image") ){ if( !narg ){ fprintf(stderr, WRONGARGS, cmd, 1, 0); return 1; } /* new image */ if( !(finfo = FinfoNew(args[0])) ){ return 1; } /* make it current */ FinfoSetCurrent(finfo); if( node ) fprintf(stdout, "image\r"); /* return the FITS file name, if possible */ fprintf(stdout, "%s %s\n", finfo->fname, finfo->fitsfile ? finfo->fitsfile : "?"); fflush(stdout); return 0; } else if( !strcmp(cmd, "image_") ){ if( !narg ){ fprintf(stderr, WRONGARGS, cmd, 1, 0); return 1; } /* new image */ if( !(finfo = FinfoNew(args[0])) ){ return 1; } /* make it current */ FinfoSetCurrent(finfo); /* no output! */ return 0; } else if( !strcmp(cmd, "imsection") ){ #if HAVE_CFITSIO if( !(finfo=FinfoGetCurrent()) ){ fprintf(stderr, NOFINFO, cmd); return 1; } if( narg < 2 ){ fprintf(stderr, WRONGARGS2, cmd, 2); return 1; } ifptr = openFITSFile(finfo->fitsfile, READONLY, EXTLIST, &hdutype, &status); if( status ){ fprintf(stderr, "ERROR: can't open FITS file '%s'\n", finfo->fitsfile); return 1; } /* process args */ ofile = args[0]; section = args[1]; if( narg >= 3 && args[2] ){ filter = args[2]; } if( narg >= 4 && args[3] ){ slice = args[3]; } if( !section || !(got = parseSection(ifptr, hdutype, section, xlims, ylims, dims, cens, &bin)) ){ fprintf(stderr, "ERROR: can't parse section for '%s' [%s]\n", finfo->fitsfile, (args && args[0]) ? args[0] : "NONE"); return 1; } /* output image */ fits_create_file(&ofptr, ofile, &status); if( status ){ fits_get_errstatus(status, tbuf); fprintf(stderr, "ERROR: can't open output FITS file to section '%s' [%s]\n", finfo->fitsfile, tbuf); return 1; } switch(hdutype){ case IMAGE_HDU: /* image: let cfitsio make a section */ if( copyImageSection(ifptr, ofptr, dims, cens, bin, slice, &status) ){ fits_get_errstatus(status, tbuf); fprintf(stderr, "ERROR: can't copy image section for '%s' [%s]\n", finfo->fitsfile, tbuf); return 1; } break; default: /* table: let jsfitsio create an image section by binning the table */ tfptr = filterTableToImage(ifptr, filter, cols, dims, cens, 1, &status); if( status ){ fits_get_errstatus(status, tbuf); fprintf(stderr, "ERROR: can't create image from table for '%s' [%s]\n", finfo->fitsfile, tbuf); return 1; } fits_read_key(tfptr, TSTRING, "CTYPE1", tbuf, NULL, &status); if( status == 0 ){ if( strstr(tbuf, "--HPX") || strstr(tbuf, "--hpx") ){ tcens = cens; } } status = 0; /* copy section to new image */ if( copyImageSection(tfptr, ofptr, dims, tcens, bin, NULL, &status) ){ fits_get_errstatus(status, tbuf); fprintf(stderr, "ERROR: can't copy image section for '%s' [%s]\n", finfo->fitsfile, tbuf); return 1; } tstatus = 0; closeFITSFile(tfptr, &tstatus); break; } if( status ){ fits_get_errstatus(status, tbuf); fprintf(stderr, "ERROR: can't create section FITS file for '%s' [%s]\n", finfo->fitsfile, tbuf); closeFITSFile(ofptr, &status); return 1; } // return a json object with info about original data fprintf(stdout, "{\"file\":\"%s\"", finfo->fitsfile); fprintf(stdout, ",\"type\":%d", hdutype); ffghdn(ifptr, &hdunum); fprintf(stdout, ",\"extnum\":%d", hdunum-1); tstatus=0; ffgky(ifptr, TSTRING, "EXTNAME", extname, NULL, &tstatus); if( !tstatus ){ fprintf(stdout, ",\"extname\":\"%s\"", extname); } fprintf(stdout, ",\"hdus\":"); _listhdu(finfo->fitsfile, NULL); tstatus=0; getHeaderToString(ifptr, &cardstr, &ncard, &tstatus); if( cardstr ){ fprintf(stdout, ",\"ncard\":%d",ncard); fprintf(stdout, ",\"cardstr\":\"%s\"",cardstr); free(cardstr); } fprintf(stdout, "}\n"); fflush(stdout); tstatus=0; closeFITSFile(ifptr, &tstatus); tstatus=0; closeFITSFile(ofptr, &tstatus); return 0; #else fprintf(stderr, "ERROR: for section support, build js9helper with cfitsio\n"); return 1; #endif } else if( !strcmp(cmd, "info") ){ if( tty ){ if( !(finfo=FinfoGetCurrent()) ){ fprintf(stderr, NOFINFO, cmd); return 1; } /* make sure we have a wcs */ fprintf(stdout, "fname:\t%s\n", finfo->fname); fprintf(stdout, "fits:\t%s\n", finfo->fitsfile?finfo->fitsfile:"N/A"); fflush(stdout); } return 0; } break; case 'l': /* list all images */ if( !strcmp(cmd, "list") ){ FinfoList(stdout); return 0; #if HAVE_CFITSIO } else if( !strcmp(cmd, "listhdus") ){ if( !(finfo=FinfoGetCurrent()) ){ fprintf(stderr, NOFINFO, cmd); return 1; } _listhdu(finfo->fitsfile, NULL); fflush(stdout); return 0; #endif } break; case 's': if( !strcmp(cmd, "setDataPath") ){ if( narg ){ setenv("JS9_DATAPATH", args[0], 1); if( node ) fprintf(stdout, "setDataPath\r"); fprintf(stdout, "%s\n", getenv("JS9_DATAPATH")); fflush(stdout); } else { fprintf(stderr, WRONGARGS, cmd, 1, 0); return 1; } return 0; } break; case 'u': if( !strcmp(cmd, "unimage") ){ if( !narg ){ fprintf(stderr, WRONGARGS, cmd, 1, 0); return 1; } /* close this image */ FinfoFree(args[0]); return 0; } break; case '#': case '\0': return 0; default: break; } /* if we reached here, we did not recognize the command */ fprintf(stderr, "ERROR: unknown command '%s'\n", cmd); /* return the news */ return 2; }
int main(int argc, char **argv){ int i, c, args, hdutype, ncard; int status=0; int mode=1; int x0=1, x1=0, y0=1, y1=0, block=1; long naxes[2]; char *cards=NULL; char regstr[SZ_LINE]; fitsfile *fptr=NULL, *nfptr=NULL; /* we want the args in the same order in which they arrived, and gnu getopt sometimes changes things without this */ putenv("POSIXLY_CORRECT=true"); /* process switch arguments */ while ((c = getopt(argc, argv, "hlms:")) != -1){ switch(c){ case 'h': regdispUsage(argv[0]); break; case 'l': mode = 0; break; case 'm': mode = 1; break; case 's': if( sscanf(optarg, "%d %d %d %d %d", &x0, &x1, &y0, &y1, &block) != 5 ){ xerror(stderr, "for sections: -s 'x0 x1 y0 y1 block'\n"); } break; } } args = argc - optind; if( args <= 0 ){ regdispUsage(argv[0]); } fptr = openFITSFile(argv[optind+0], READONLY, EXTLIST, &hdutype, &status); regcntsErrchk(status); if( (x1 == 0) || (y1 == 0) ){ switch(hdutype){ case IMAGE_HDU: break; default: nfptr = filterTableToImage(fptr, NULL, NULL, NULL, NULL, 1, &status); regcntsErrchk(status); closeFITSFile(fptr, &status); regcntsErrchk(status); fptr = nfptr; break; } fits_get_img_size(fptr, 2, naxes, &status); regcntsErrchk(status); x1 = naxes[0]; y1 = naxes[1]; } getHeaderToString(fptr, &cards, &ncard, &status); regcntsErrchk(status); if( args > 1 ){ for(i=1; i<args; i++){ doreg(cards, argv[optind+i], x0, x1, y0, y1, block, mode); } } else { while( fgets(regstr, SZ_LINE, stdin) ){ doreg(cards, regstr, x0, x1, y0, y1, block, mode); } } closeFITSFile(fptr, &status); regcntsErrchk(status); xfree(cards); return 0; }