/* FPACK utility routines * R. Seaman, NOAO */ #include "fitsio.h" #include "fpack.h" fp_msg (char *msg) { printf ("%s", msg); } fp_version () { fp_msg (FPACK_VERSION); fp_msg ("\n"); } fp_noop () { fp_msg ("input and output files are unchanged\n"); } fp_init (fpstate *fpptr) { int ii; fpptr->comptype = RICE_1; fpptr->noise = DEF_NOISE; fpptr->scale = DEF_HCOMP_SCALE; fpptr->smooth = DEF_HCOMP_SMOOTH; fpptr->ntile[0] = (long) 0; /* 0 means extent of axis */ for (ii=1; ii < MAX_COMPRESS_DIM; ii++) fpptr->ntile[ii] = (long) 1; fpptr->verbose = 0; fpptr->to_stdout = 0; fpptr->listonly = 0; fpptr->clobber = 0; fpptr->do_checksums = 1; fpptr->prefix[0] = (char) NULL; fpptr->firstfile = 1; /* magic number for initialization check, boolean for preflight */ fpptr->initialized = FP_INIT_MAGIC; fpptr->preflight_checked = 0; } fp_list (int argc, char *argv[], fpstate fpvar) { fitsfile *infptr; char infits[SZ_STR]; int hdunum, iarg, stat=0; if (fpvar.initialized != FP_INIT_MAGIC) { fp_msg ("internal initialization error\n"); exit (-1); } for (iarg=fpvar.firstfile; iarg < argc; iarg++) { strncpy (infits, argv[iarg], SZ_STR); if (strchr (infits, '[') || strchr (infits, ']')) { fp_msg ("section/extension notation not supported: "); fp_msg (infits); fp_msg ("\n"); exit (-1); } if (access (infits, R_OK) != 0) { fp_msg ("can't read input file "); fp_msg (infits); fp_msg ("\n"); exit (-1); } fits_open_file (&infptr, infits, READONLY, &stat); if (stat) { fits_report_error (stderr, stat); exit (stat); } fp_info (infits); /* if (fpvar.verbose) { */ fits_get_num_hdus (infptr, &hdunum, &stat); if (stat) { fits_report_error (stderr, stat); exit (stat); } fp_info_hdu (infptr); /* } */ fits_close_file (infptr, &stat); if (stat) { fits_report_error (stderr, stat); exit (stat); } } } fp_info (char *infits) { struct stat sbuf; char msg[SZ_STR]; int mtime, size, uid, gid, nlink; unsigned mode; if (stat (infits, &sbuf) != 0) { fp_msg ("can't stat "); fp_msg (infits); fp_msg ("\n"); } else { size = (int) sbuf.st_size; mtime = (int) sbuf.st_mtime; uid = (int) sbuf.st_uid; gid = (int) sbuf.st_gid; mode = (unsigned) sbuf.st_mode; nlink = (int) sbuf.st_nlink; sprintf (msg, "%s: %d bytes\n", infits, size); fp_msg (msg); } } fp_info_hdu (fitsfile *infptr) { long naxes[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1}; char msg[SZ_STR], val[SZ_CARD], com[SZ_CARD]; int naxis=0, hdutype, bitpix, hdupos, stat=0, ii; fits_movabs_hdu (infptr, 1, NULL, &stat); if (stat) { fits_report_error (stderr, stat); exit (stat); } for (hdupos=1; ! stat; hdupos++) { fits_get_hdu_type (infptr, &hdutype, &stat); /* fits_read_keyword (infptr, FILE_KEY, val, com, &stat); */ if (stat) { fits_report_error (stderr, stat); exit (stat); } if (hdutype == IMAGE_HDU) { sprintf (msg, " %d IMAGE", hdupos); fp_msg (msg); /* sprintf (msg, " %d IMAGE %s", hdupos, val); fp_msg (msg); */ fits_get_img_param (infptr, 9, &bitpix, &naxis, naxes, &stat); if (naxis == 0) { sprintf (msg, " [no pixels]"); fp_msg (msg); } else if (naxis == 1) { sprintf (msg, " [%d]", naxes[1]); fp_msg (msg); } else { sprintf (msg, " [%d", naxes[0]); fp_msg (msg); for (ii=1; ii < naxis; ii++) { sprintf (msg, "x%d", naxes[ii]); fp_msg (msg); } fp_msg ("]"); } if (fits_is_compressed_image (infptr, &stat)) fp_msg (" (compressed)\n"); else fp_msg ("\n"); } else if (hdutype == ASCII_TBL) { sprintf (msg, " %d ASCII TABLE\n", hdupos); fp_msg (msg); } else if (hdutype == BINARY_TBL) { sprintf (msg, " %d BINARY TABLE\n", hdupos); fp_msg (msg); } else { sprintf (msg, " %d UNKNOWN EXTENSION\n", hdupos); fp_msg (msg); } fits_movrel_hdu (infptr, 1, NULL, &stat); } } fp_preflight (int argc, char *argv[], fpstate *fpptr) { char infits[SZ_STR], outfits[SZ_STR]; int iarg; if (fpptr->initialized != FP_INIT_MAGIC) { fp_msg ("internal initialization error\n"); exit (-1); } for (iarg=fpptr->firstfile; iarg < argc; iarg++) { strncpy (infits, argv[iarg], SZ_STR); if (strchr (infits, '[') || strchr (infits, ']')) { fp_msg ("section/extension notation not supported: "); fp_msg (infits); fp_msg ("\n"); fp_noop (); exit (-1); } if (access (infits, R_OK) != 0) { fp_msg ("can't read input file "); fp_msg (infits); fp_msg ("\n"); fp_noop (); exit (-1); } /* CHECK FITS or other image info */ if (fpptr->prefix[0]) { sprintf (outfits, "%s%s", fpptr->prefix, argv[iarg]); if (! fpptr->clobber && access (outfits, F_OK) == 0) { fp_msg ("output file "); fp_msg (outfits); fp_msg (" already exists\n"); fp_noop (); exit (-1); } } else { /* in-place and to_stdout behavior checked in fp_get_param() */ } /* other CHECKS */ } fpptr->preflight_checked++; } /* must run fp_preflight() before fp_loop() */ fp_loop (int argc, char *argv[], int unpack, fpstate fpvar) { char infits[SZ_STR], outfits[SZ_STR]; int iarg; if (fpvar.initialized != FP_INIT_MAGIC) { fp_msg ("internal initialization error\n"); exit (-1); } else if (! fpvar.preflight_checked) { fp_msg ("internal preflight error\n"); exit (-1); } for (iarg=fpvar.firstfile; iarg < argc; iarg++) { strncpy (infits, argv[iarg], SZ_STR); if (fpvar.to_stdout) { sprintf (outfits, "-"); } else if (fpvar.prefix[0]) { sprintf (outfits, "%s%s", fpvar.prefix, argv[iarg]); if (access (outfits, F_OK) == 0) { if (fpvar.clobber) { if (unlink (outfits) != 0) { fp_msg ("error clobbering "); fp_msg (outfits); fp_msg ("\n"); exit (-1); } } else { /* checked in fp_preflight(), shouldn't get here */ fp_msg ("internal clobber error\n"); exit (-1); } } } else { strcpy (outfits, "fptmp.XXXXXX"); mktemp (outfits); if (access (outfits, F_OK) == 0) { /* unlikely name collision, try again (once) */ strcpy (outfits, "fptmp.XXXXXX"); mktemp (outfits); if (access (outfits, F_OK) == 0) { fp_msg ("temporary file "); fp_msg (outfits); fp_msg (" already exists\n"); exit (-1); } } } if (unpack) fp_unpack (infits, outfits, fpvar); else fp_pack (infits, outfits, fpvar); /* rename clobbers input, may be unix/shell version dependent */ if (! fpvar.to_stdout && ! fpvar.prefix[0]) { if (rename (outfits, infits) != 0) { fp_msg ("error renaming tmp file to "); fp_msg (infits); fp_msg ("\n"); exit (-1); } } } } /* fp_pack assumes the output file does not exist */ fp_pack (infits, outfits, fpvar) char *infits; char *outfits; fpstate fpvar; { fitsfile *infptr, *outfptr; int stat=0; fits_open_file (&infptr, infits, READONLY, &stat); fits_create_file (&outfptr, outfits, &stat); if (stat) { fits_report_error (stderr, stat); exit (stat); } fits_set_compression_type (outfptr, fpvar.comptype, &stat); fits_set_noise_bits (outfptr, fpvar.noise, &stat); fits_set_hcomp_scale (outfptr, fpvar.scale, &stat); fits_set_hcomp_smooth (outfptr, fpvar.smooth, &stat); fits_set_tile_dim (outfptr, 6, fpvar.ntile, &stat); if (stat) { fits_report_error (stderr, stat); exit (stat); } /* fits_get_hdu_num (infptr, &hdupos); */ while (! stat) { fp_pack_hdu (infptr, outfptr, &stat); fits_movrel_hdu (infptr, 1, NULL, &stat); } if (stat == END_OF_FILE) stat = 0; /* set checksum for case of newly created primary HDU */ if (fpvar.do_checksums) { fits_movabs_hdu (outfptr, 1, NULL, &stat); fits_write_chksum (outfptr, &stat); } fits_close_file (outfptr, &stat); fits_close_file (infptr, &stat); if (stat) { fits_report_error (stderr, stat); exit (stat); } } /* fp_unpack assumes the output file does not exist */ fp_unpack (infits, outfits, fpvar) char *infits; char *outfits; fpstate fpvar; { fitsfile *infptr, *outfptr; int stat=0; fits_open_file (&infptr, infits, READONLY, &stat); fits_create_file (&outfptr, outfits, &stat); if (stat) { fits_report_error (stderr, stat); exit (stat); } /* fits_get_hdu_num (infptr, &hdupos); */ while (! stat) { fp_unpack_hdu (infptr, outfptr, &stat); fits_movrel_hdu (infptr, 1, NULL, &stat); } if (stat == END_OF_FILE) stat = 0; /* set checksum for case of newly created primary HDU */ if (fpvar.do_checksums) { fits_movabs_hdu (outfptr, 1, NULL, &stat); fits_write_chksum (outfptr, &stat); } fits_close_file (outfptr, &stat); fits_close_file (infptr, &stat); if (stat) { fits_report_error (stderr, stat); exit (stat); } } fp_pack_hdu (infptr, outfptr, status) fitsfile *infptr; fitsfile *outfptr; int *status; { long naxes[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1}; int stat=0, totpix=0, naxis=0, ii, hdutype, bitpix; if (*status) return; fits_get_hdu_type (infptr, &hdutype, &stat); if (hdutype == IMAGE_HDU) { fits_get_img_param (infptr, 9, &bitpix, &naxis, naxes, &stat); for (totpix=1, ii=0; ii < 9; ii++) totpix *= naxes[ii]; } if (fits_is_compressed_image (infptr, &stat) || hdutype != IMAGE_HDU || naxis == 0 || totpix == 0) { fits_copy_hdu (infptr, outfptr, 0, &stat); } else fits_img_compress (infptr, outfptr, &stat); fits_write_chksum (outfptr, &stat); *status = stat; } fp_unpack_hdu (infptr, outfptr, status) fitsfile *infptr; fitsfile *outfptr; int *status; { int stat=0; if (*status) return; if (fits_is_compressed_image (infptr, &stat)) fits_img_decompress (infptr, outfptr, &stat); else fits_copy_hdu (infptr, outfptr, 0, &stat); fits_write_chksum (outfptr, &stat); *status = stat; }