/* 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 c, i, j, got, args, jsonlen, istart; int odim1, odim2, blen, pad; int idim1=0, idim2=0, bitpix=0, ncard=0; int verbose=0; size_t totbytes, dlen; char tbuf[SZ_LINE]; char *buf=NULL; char *jsonheader=NULL; char *iname=NULL, *oname=NULL; FILE *ofp=NULL; Optinfo optinfo; #if HAVE_CFITSIO int status = 0; int n; int hdutype; int maxcard, morekeys; int dims[2] = {0, 0}; int block = 1; void *dbuf; double d1, d2, d3, d4; double cens[2] = {0.0, 0.0}; char *s; char *filter=NULL; char *evtlist = DEF_EVTLIST; char card[81]; char s1[BUFLEN], s2[BUFLEN], s3[BUFLEN], s4[BUFLEN]; fitsfile *fptr=NULL, *ofptr=NULL; #elif HAVE_FUNTOOLS char *s=NULL; int dtype; Fun ifun=NULL, tfun=NULL; #endif /* 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, "b:e:f:s:v")) != -1){ switch(c){ case 'b': #if HAVE_CFITSIO block = atoi(optarg); #else fprintf(stderr, "warning: -b switch only for cfitsio (ignoring)\n"); #endif break; case 'e': #if HAVE_CFITSIO evtlist = optarg; #else fprintf(stderr, "warning: -e switch only for cfitsio (ignoring)\n"); #endif break; case 'f': #if HAVE_CFITSIO filter = optarg; #else fprintf(stderr, "warning: -f switch only for cfitsio (ignoring)\n"); #endif break; case 's': #if HAVE_CFITSIO s = strdup(optarg); if( strlen(s) > BUFLEN ) s[BUFLEN-1] = '\0'; if( sscanf(s, "%[0-9.*] @ %[-0-9.*] , %[0-9.*] @ %[-0-9.*]%n", s1, s2, s3, s4, &n) == 4){ dims[0] = atof(s1); cens[0] = atof(s2); dims[1] = atof(s3); cens[1] = atof(s4); } else if(sscanf(s, "%[-0-9.*] : %[-0-9.*] , %[-0-9.*] : %[-0-9.*]%n", s1, s2, s3, s4, &n) == 4){ d1 = atof(s1); d2 = atof(s2); d3 = atof(s3); d4 = atof(s4); dims[0] = d2 - d1 + 1; cens[0] = dims[0] / 2; dims[1] = d4 - d3 + 1; cens[1] = dims[1] / 2; } else { fprintf(stderr, "warning: unknown arg for -s switch (ignoring)\n"); } if( s ) free(s); #else fprintf(stderr, "warning: -s switch only for cfitsio (ignoring)\n"); #endif break; case 'v': verbose++; break; } } /* check for required arguments */ args = argc - optind; if( args < 2 ){ fprintf(stderr, "usage: %s iname oname\n", argv[0]); exit(1); } iname = argv[optind++]; oname = argv[optind++]; /* optional info */ if( !(optinfo = (Optinfo)calloc(sizeof(OptinfoRec), 1)) ){ fprintf(stderr, "ERROR: can't allocate optional info rec\n"); exit(1); } /* open the input FITS file */ #if HAVE_CFITSIO fptr = openFITSFile(iname, evtlist, &hdutype, &status); errchk(status); #elif HAVE_FUNTOOLS if( !(ifun = FunOpen(iname, "r", NULL)) ){ fprintf(stderr, "ERROR could not open input FITS file: %s (%s)\n", iname, strerror(errno)); exit(1); } #endif /* save the input filename in the png file */ optinfo->fitsname = iname; /* open the output PGN file */ if( !strcmp(oname, "-") || !strcmp(oname, "stdout") ){ ofp = stdout; } else if( !(ofp = fopen(oname, "w")) ){ fprintf(stderr, "ERROR: could not create output PNG file: %s (%s)\n", oname, strerror(errno)); exit(1); } #if HAVE_CFITSIO switch(hdutype){ case IMAGE_HDU: // get image array dbuf = getImageToArray(fptr, NULL, NULL, &idim1, &idim2, &bitpix, &status); errchk(status); fits_get_hdrspace(fptr, &maxcard, &morekeys, &status); errchk(status); ofptr = fptr; break; default: ofptr = filterTableToImage(fptr, filter, NULL, dims, cens, block, &status); errchk(status); // get image array dbuf = getImageToArray(ofptr, NULL, NULL, &idim1, &idim2, &bitpix, &status); errchk(status); // get number of keys fits_get_hdrspace(ofptr, &maxcard, &morekeys, &status); errchk(status); break; } #elif HAVE_FUNTOOLS /* copy the input fits header into a FITS image header */ if( !(tfun = (Fun)calloc(1, sizeof(FunRec))) ){ fprintf(stderr, "ERROR: could not create tfun struct\n"); exit(1); } _FunCopy2ImageHeader(ifun, tfun); /* and save for storage in the png file */ optinfo->fitsheader = (char *)tfun->header->cards; /* get image parameters. its safe to do this before calling image get so long as we don't change bitpix before that call */ FunInfoGet(ifun, FUN_SECT_BITPIX, &bitpix, FUN_SECT_DIM1, &idim1, FUN_SECT_DIM2, &idim2, 0); #endif /* convert FITS header into a json string */ snprintf(tbuf, SZ_LINE-1, "{\"js9Protocol\": %s, ", JS9_PROTOCOL); scat(tbuf, &jsonheader); snprintf(tbuf, SZ_LINE-1, "\"js9Endian\": \"%s\", ", JS9_ENDIAN); scat(tbuf, &jsonheader); snprintf(tbuf, SZ_LINE-1, "\"cardstr\": \""); scat(tbuf, &jsonheader); // concat header cards into a single string #if HAVE_CFITSIO while( ++ncard <= maxcard ){ fits_read_record(ofptr, ncard, card, &status); errchk(status); // change " to ' for(i=0; i<80; i++){ if( card[i] == '"' ){ card[i] = '\''; } } snprintf(tbuf, SZ_LINE-1, "%-80s", card); scat(tbuf, &jsonheader); } #elif HAVE_FUNTOOLS while( (s = FunParamGets(tfun, NULL, ++ncard, NULL, &dtype)) ){ for(i=0; i<80; i++){ if( s[i] == '"' ){ s[i] = '\''; } } scat(s, &jsonheader); if( s ) free(s); } #endif // end with the number of cards snprintf(tbuf, SZ_LINE-1, "\", \"ncard\": %d}", ncard); scat(tbuf, &jsonheader); /* we want the image buffer to start on an 8-byte boundary, so make jsonheader + null byte end on one */ pad = 8 - (strlen(jsonheader) % 8) - 1; for(i=0; i<pad; i++){ strcat(jsonheader, " "); } /* get final length of json header */ jsonlen = strlen(jsonheader); /* total length of the header + null + image we are storing */ blen = ABS(bitpix/8); dlen = (size_t)idim1 * (size_t)idim2 * blen; totbytes = jsonlen + 1 + dlen; /* all of this should now fit into the png image */ /* somewhat arbitrarily, we use idim1 for odim1, and adjust odim2 to fit */ odim1 = idim1; odim2 = (int)(((totbytes + odim1 - 1) / odim1) + (COLOR_CHANNELS-1)) / COLOR_CHANNELS; /* allocate buf to hold json header + null byte + RGB image */ if( !(buf=calloc(COLOR_CHANNELS, odim1 * odim2)) ){ fprintf(stderr, "ERROR: can't allocate image buf\n"); exit(1); } /* move the json header into the output buffer */ memmove(buf, jsonheader, jsonlen); /* add a null byte to signify end of json header */ buf[jsonlen] = '\0'; /* offset into image buffer where image starts, past header and null byte */ istart = jsonlen + 1; /* debug output */ if( verbose ){ fprintf(stderr, "idim=%d,%d (bitpix=%d jsonlen=%d istart=%d endian=%s) [%ld] -> odim=%d,%d [%d]\n", idim1, idim2, bitpix, jsonlen, istart, JS9_ENDIAN, totbytes, odim1, odim2, odim1 * odim2 * COLOR_CHANNELS); } #if HAVE_CFITSIO /* move the json header into the output buffer */ memmove(&buf[istart], dbuf, dlen); #elif HAVE_FUNTOOLS /* extract and bin the data section into an image buffer */ if( !FunImageGet(ifun, &buf[istart], NULL) ){ fprintf(stderr, "ERROR: could not FunImageGet: %s\n", iname); exit(1); } #endif /* debugging output to check against javascript input */ if( verbose > 1 ){ fprintf(stderr, "jsonheader: %s\n", jsonheader); for(j=0; j<idim2; j++){ fprintf(stderr, "data #%d: ", j); for(i=0; i<idim1; i++){ switch(bitpix){ case 8: fprintf(stderr, "%d ", *(unsigned char *)(buf + istart + ((j * idim1) + i) * blen)); break; case 16: fprintf(stderr, "%d ", *(short *)(buf + istart + ((j * idim1) + i) * blen)); break; case -16: fprintf(stderr, "%d ", *(unsigned short *)(buf + istart + ((j * idim1) + i) * blen)); break; case 32: fprintf(stderr, "%d ", *(int *)(buf + istart + ((j * idim1) + i) * blen)); break; case -32: fprintf(stderr, "%.3f ", *(float *)(buf + istart + ((j * idim1) + i) * blen)); break; case -64: fprintf(stderr, "%.3f ", *(double *)(buf + istart + ((j * idim1) + i) * blen)); break; } } fprintf(stderr, "\n"); } fprintf(stderr, "\n"); } /* might have to swap to preferred endian for png creation */ if( (!strncmp(JS9_ENDIAN, "l", 1) && is_bigendian()) || (!strncmp(JS9_ENDIAN, "b", 1) && !is_bigendian()) ){ swap_data(&buf[istart], idim1 * idim2, bitpix/8); } /* write the PNG file */ got = writePNG(ofp, buf, odim1, odim2, optinfo); /* free up space */ if( buf ) free(buf); if( optinfo ) free(optinfo); if( jsonheader ) free(jsonheader); /* close files */ #if HAVE_CFITSIO status = 0; if( ofptr && (ofptr != fptr) ) closeFITSFile(ofptr, &status); if( fptr ) closeFITSFile(fptr, &status); if( dbuf ) free(dbuf); #elif HAVE_FUNTOOLS if( ifun ) FunClose(ifun); if( tfun ){ FunClose(tfun); free(tfun); } #endif if( ofp) fclose(ofp); /* return the news */ return got; }
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; }
/* process this command */ static int ProcessCmd(char *cmd, char **args, int node, int tty) { int ip=0; char tbuf[SZ_LINE]; Finfo finfo, tfinfo; #if HAVE_CFITSIO int x0, x1, y0, y1, bin; int xcolnum, ycolnum, hdunum, hdutype; int status=0, status2=0; int dims[2]; double cens[2]; char *cols[2] = {"X", "Y"}; char *ofile=NULL; char *omode=NULL; fitsfile *ifptr, *ofptr, *tfptr; #endif switch(*cmd){ case 'f': if( !strcmp(cmd, "fitsFile") ){ if( args && word(args[0], tbuf, &ip) ){ if( !(tfinfo=FinfoLookup(tbuf)) ){ fprintf(stderr, NOIMAGE, tbuf); 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( !args || !word(args[0], tbuf, &ip) ){ fprintf(stderr, WRONGARGS, cmd, 1, 0); return 1; } /* new image */ if( !(finfo = FinfoNew(tbuf)) ){ fprintf(stderr, NONEW, cmd); 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( !args || !word(args[0], tbuf, &ip) ){ fprintf(stderr, WRONGARGS, cmd, 1, 0); return 1; } /* new image */ if( !(finfo = FinfoNew(tbuf)) ){ fprintf(stderr, NONEW, cmd); 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; } ifptr = openFITSFile(finfo->fitsfile, EXTLIST, &hdutype, &status); if( status ){ fprintf(stderr, "ERROR: can't open FITS file '%s'\n", finfo->fitsfile); return 1; } if( !args || !parseSection(args[0], &x0, &x1, &y0, &y1, &bin) ){ fprintf(stderr, "ERROR: can't parse section for '%s' [%s]\n", finfo->fitsfile, (args && args[1]) ? args[1] : "NONE"); return 1; } if( args[1] ){ omode = args[1]; } else { omode = "native"; } ofile = "stdout"; // create image if ifile is an image or omode is not native if( (hdutype == IMAGE_HDU) || strcmp(omode, "native") ){ 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: if( bin != 1 ){ fprintf(stderr, "ERROR: imsection of an image must use bin 1 for '%s'\n", finfo->fitsfile); return 1; } snprintf(tbuf, SZ_LINE-1, "%d:%d,%d:%d", x0, x1, y0, y1); fits_copy_image_section(ifptr, ofptr, tbuf, &status); break; default: dims[0] = x1 - x0 + 1; dims[1] = y1 - y0 + 1; cens[0] = (x0 + x1) / 2; cens[1] = (y0 + y1) / 2; tfptr = filterTableToImage(ifptr, NULL, cols, dims, cens, bin, &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_copy_image_section(tfptr, ofptr, "*,*", &status); closeFITSFile(tfptr, &status2); break; } if( status ){ fits_get_errstatus(status, tbuf); fprintf(stderr, "ERROR: can't write section FITS file for '%s' [%s]\n", finfo->fitsfile, tbuf); closeFITSFile(ofptr, &status); return 1; } closeFITSFile(ofptr, &status); } else { // extract (native) table snprintf(tbuf, SZ_LINE-1, "x >= %d && x <= %d && y >= %d && y <= %d", x0, x1, y0, y1); // ffselect_table(&ifptr, ofile, tbuf, &status); // copied from cfileio.c/ffselect_table() /* create new empty file to hold copy of the image */ if (ffinit(&ofptr, ofile, &status) > 0) { fits_get_errstatus(status, tbuf); fprintf(stderr, "ERROR: can't init section file for '%s' [%s]\n", finfo->fitsfile, tbuf); return 1; } /* save current HDU number in input file */ fits_get_hdu_num(ifptr, &hdunum); /* copy the primary array */ fits_movabs_hdu(ifptr, 1, NULL, &status); if( fits_copy_hdu(ifptr, ofptr, 0, &status) > 0){ fits_get_errstatus(status, tbuf); fprintf(stderr, "ERROR: can't copy primary for section file '%s' [%s]\n", finfo->fitsfile, tbuf); fits_close_file(ofptr, &status); return 1; } /* back to current hdu */ fits_movabs_hdu(ifptr, hdunum, NULL, &status); /* copy all the header keywords from the input to output file */ if (fits_copy_header(ifptr, ofptr, &status) > 0){ fits_get_errstatus(status, tbuf); fprintf(stderr, "ERROR: can't copy header for section file '%s' [%s]\n", finfo->fitsfile, tbuf); fits_close_file(ofptr, &status); return 1; } /* set number of rows = 0 */ /* warning: start of cfitsio black magic */ fits_modify_key_lng(ofptr, "NAXIS2", 0, NULL, &status); (ofptr->Fptr)->numrows = 0; (ofptr->Fptr)->origrows = 0; /* force the header to be scanned */ if (ffrdef(ofptr, &status) > 0){ fits_get_errstatus(status, tbuf); fprintf(stderr, "ERROR: can't rdef for section file '%s' [%s]\n", finfo->fitsfile, tbuf); fits_close_file(ofptr, &status); return 1; } /* warning: end of cfitsio black magic */ /* select filtered rows and write to output file */ if (fits_select_rows(ifptr, ofptr, tbuf, &status) > 0){ fits_get_errstatus(status, tbuf); fprintf(stderr, "ERROR: can't select rows for section file '%s' [%s]\n", finfo->fitsfile, tbuf); fits_close_file(ofptr, &status); return 1; } /* update params for this section */ if( (fits_get_colnum(ofptr, CASEINSEN, "X", &xcolnum, &status) > 0) || (fits_get_colnum(ofptr, CASEINSEN, "Y", &ycolnum, &status) > 0) ){ fits_get_errstatus(status, tbuf); fprintf(stderr, "ERROR: can't find X,Y cols for section file '%s' [%s]\n", finfo->fitsfile, tbuf); fits_close_file(ofptr, &status); return 1; } /* we can ignore errors here */ status = 0; snprintf(tbuf, SZ_LINE-1, "TALEN%d", xcolnum); fits_modify_key_lng(ofptr, tbuf, x1-x0, NULL, &status); status = 0; snprintf(tbuf, SZ_LINE-1, "TALEN%d", ycolnum); fits_modify_key_lng(ofptr, tbuf, y1-y0, NULL, &status); status = 0; snprintf(tbuf, SZ_LINE-1, "TLMIN%d", xcolnum); fits_modify_key_flt(ofptr, tbuf, x0, 6, NULL, &status); status = 0; snprintf(tbuf, SZ_LINE-1, "TLMAX%d", xcolnum); fits_modify_key_flt(ofptr, tbuf, x1, 6, NULL, &status); status = 0; snprintf(tbuf, SZ_LINE-1, "TLMIN%d", ycolnum); fits_modify_key_flt(ofptr, tbuf, y0, 6, NULL, &status); status = 0; snprintf(tbuf, SZ_LINE-1, "TLMAX%d", ycolnum); fits_modify_key_flt(ofptr, tbuf, y1, 6, NULL, &status); /* close the output file */ status = 0; fits_close_file(ofptr, &status); } closeFITSFile(ifptr, &status); 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; } break; case 's': if( !strcmp(cmd, "setDataPath") ){ if( args && word(args[0], tbuf, &ip) ){ setenv("JS9_DATAPATH", tbuf, 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( !args || !word(args[0], tbuf, &ip) ){ fprintf(stderr, WRONGARGS, cmd, 1, 0); return 1; } /* close this image */ FinfoFree(tbuf); 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; }