573int fp_loop (
int argc,
char *argv[],
int unpack,
char *output_filename,
fpstate fpvar)
576 char temp[
SZ_STR], answer[30];
577 char valchar[]=
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.#()+,-_@[]/^{}";
578 int ichar=0, outlen=0, iarg, islossless, namelen, iraf_infile = 0, status = 0, ifail;
581 fp_msg (
"Error: internal initialization error\n"); exit (-1);
583 fp_msg (
"Error: internal preflight error\n"); exit (-1);
588 fprintf(
outreport,
" Filename Extension BITPIX NAXIS1 NAXIS2 Size N_nulls Minval Maxval Mean Sigm Noise1 Noise2 Noise3 Noise5 T_whole T_rowbyrow ");
589 fprintf(
outreport,
"[Comp_ratio, Pack_cpu, Unpack_cpu, Lossless readtimes] (repeated for Rice, Hcompress, and GZIP)\n");
599 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
605 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
611 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
616 for (iarg=fpvar.
firstfile; iarg < argc; iarg++) {
622 strncpy (infits, argv[iarg],
SZ_STR - 1);
629 if (infits[0] !=
'-') {
631 strcat(infits,
".fz");
636 strcpy(outfits,
"-");
639 strcpy(outfits, fpvar.
outfile);
644 strcat(outfits,fpvar.
prefix);
648 if (infits[0] ==
'-') {
649 strcpy(outfits,
"output.fits");
652 strcpy(outfits, output_filename);
656 namelen = strlen(outfits);
657 if ( !strcmp(
".gz", outfits + namelen - 3) ) {
658 outfits[namelen - 3] =
'\0';
663 namelen = strlen(outfits);
664 if ( !strcmp(
".fz", outfits + namelen - 3) ) {
665 outfits[namelen - 3] =
'\0';
673 strcpy(outfits,
"-");
677 if (infits[0] ==
'-') {
678 strcpy(outfits,
"input.fits");
680 strcpy(outfits, output_filename);
684 namelen = strlen(outfits);
685 if ( !strcmp(
".gz", outfits + namelen - 3) ) {
686 outfits[namelen - 3] =
'\0';
690 namelen = strlen(outfits);
691 if ( !strcmp(
".imh", outfits + namelen - 4) ) {
692 outfits[namelen - 4] =
'\0';
693 strcat(outfits,
".fits");
700 strcat(outfits,
".fz");
704 strncpy(temp, outfits,
SZ_STR-1);
707 if (infits[0] !=
'-') {
708 if (!strcmp(infits, outfits) ) {
712 fp_msg (
"\nError: must use -F flag to clobber input file.\n");
727 printf(
"%s ", infits);
759 fp_msg(
"Failed to rename temporary file name:\n ");
770 fp_pack (infits, outfits, fpvar, &islossless);
779 if (!strcmp(infits, temp) && fpvar.
clobber ) {
784 fp_msg (
"\nwas compressed with a LOSSY method. Overwrite the\n");
785 fp_msg (
"original file with the compressed version? (Y/N) ");
786 if (fgets(answer, 29, stdin) && answer[0] !=
'Y' && answer[0] !=
'y') {
787 fp_msg (
"\noriginal file NOT overwritten!\n");
794 if (fits_delete_iraf_file(infits, &status)) {
795 fp_msg(
"\nError deleting IRAF .imh and .pix files.\n");
800#if defined(unix) || defined(__unix__) || defined(__unix)
802 if (rename (outfits, temp) != 0) {
803 fp_msg (
"\nError renaming tmp file to ");
810 if (rename (outfits, temp) != 0) {
811 fp_msg (
"\nError renaming tmp file to ");
817 strcpy(outfits, temp);
823 fp_msg (
"\nwas compressed with a LOSSY method. \n");
824 fp_msg (
"Delete the original file? (Y/N) ");
825 if (fgets(answer, 29, stdin) && answer[0] !=
'Y' && answer[0] !=
'y') {
826 fp_msg (
"\noriginal file NOT deleted!\n");
829 if (fits_delete_iraf_file(infits, &status)) {
830 fp_msg(
"\nError deleting IRAF .imh and .pix files.\n");
833 }
else if (remove(infits) != 0) {
834 fp_msg (
"\nError deleting input file ");
840 if (fits_delete_iraf_file(infits, &status)) {
841 fp_msg(
"\nError deleting IRAF .imh and .pix files.\n");
844 }
else if (remove(infits) != 0) {
845 fp_msg (
"\nError deleting input file ");
853 strcpy(temp,
"gzip -1 ");
854 outlen = strlen(outfits);
855 if (outlen + 8 >
SZ_STR-1)
857 fp_msg(
"\nError: Output file name is too long.\n");
860 for (ichar=0; ichar < outlen; ++ichar)
862 if (!strchr(valchar, outfits[ichar]))
864 fp_msg(
"\n Error: Invalid characters in output file name.\n");
868 strcat(temp,outfits);
869 int rc = system(temp);
871 strcat(outfits,
".gz");
875 printf(
"-> %s\n", outfits);
889 fitsfile *infptr, *outfptr;
892 fits_open_file (&infptr, infits, READONLY, &stat);
895 fits_report_error (stderr, stat);
899 fits_create_file (&outfptr, outfits, &stat);
910 fits_set_lossy_int (outfptr, fpvar.
int_to_float, &stat);
911 fits_set_compression_type (outfptr, fpvar.
comptype, &stat);
912 fits_set_tile_dim (outfptr, 6, fpvar.
ntile, &stat);
915 fits_set_quantize_method(outfptr, -1, &stat);
917 fits_set_quantize_method(outfptr, fpvar.
dither_method, &stat);
921 fits_set_hcomp_scale (outfptr, fpvar.
scale, &stat);
922 fits_set_hcomp_smooth (outfptr, fpvar.
smooth, &stat);
924 fp_pack_hdu (infptr, outfptr, fpvar, islossless, &stat);
927 fits_write_chksum (outfptr, &stat);
930 fits_movrel_hdu (infptr, 1, NULL, &stat);
933 if (stat == END_OF_FILE) stat = 0;
938 fits_movabs_hdu (outfptr, 1, NULL, &stat);
939 fits_write_chksum (outfptr, &stat);
946 fits_close_file (outfptr, &stat);
947 fits_close_file (infptr, &stat);
957 fitsfile *infptr, *outfptr;
959 size_t outbufferSize = 2880;
960 void *outbuffer = (
char *)malloc(outbufferSize);
961 void *inbuffer = (
void *)(inputBuffer);
963 fits_open_memfile(&infptr,
"", READONLY, &inbuffer, &inputBufferSize, 2880, NULL, &stat);
967 fits_report_error (stderr, stat);
971 fits_create_memfile(&outfptr, &outbuffer, &outbufferSize, 2880, realloc, &stat);
982 fits_set_lossy_int (outfptr, fpvar.
int_to_float, &stat);
983 fits_set_compression_type (outfptr, fpvar.
comptype, &stat);
984 fits_set_tile_dim (outfptr, 6, fpvar.
ntile, &stat);
987 fits_set_quantize_method(outfptr, -1, &stat);
989 fits_set_quantize_method(outfptr, fpvar.
dither_method, &stat);
993 fits_set_hcomp_scale (outfptr, fpvar.
scale, &stat);
994 fits_set_hcomp_smooth (outfptr, fpvar.
smooth, &stat);
996 fp_pack_hdu (infptr, outfptr, fpvar, islossless, &stat);
999 fits_write_chksum (outfptr, &stat);
1002 fits_movrel_hdu (infptr, 1, NULL, &stat);
1005 if (stat == END_OF_FILE) stat = 0;
1010 fits_movabs_hdu (outfptr, 1, NULL, &stat);
1011 fits_write_chksum (outfptr, &stat);
1018 fits_close_file (infptr, &stat);
1027int fp_pack_data_to_data (
const char *inputBuffer,
size_t inputBufferSize,
unsigned char **outputBuffer,
size_t *outputBufferSize,
1028 fpstate fpvar,
int *islossless)
1030 fitsfile *infptr, *outfptr;
1032 void *inbuffer = (
void *)(inputBuffer);
1034 fits_open_memfile(&infptr,
"", READONLY, &inbuffer, &inputBufferSize, 2880, NULL, &stat);
1037 fits_report_error (stderr, stat);
1041 void *outbuffer = (
void *)(outputBuffer);
1042 fits_create_memfile(&outfptr, outbuffer, outputBufferSize, 2880, realloc, &stat);
1053 fits_set_lossy_int (outfptr, fpvar.
int_to_float, &stat);
1054 fits_set_compression_type (outfptr, fpvar.
comptype, &stat);
1055 fits_set_tile_dim (outfptr, 6, fpvar.
ntile, &stat);
1058 fits_set_quantize_method(outfptr, -1, &stat);
1060 fits_set_quantize_method(outfptr, fpvar.
dither_method, &stat);
1063 fits_set_dither_offset(outfptr, fpvar.
dither_offset, &stat);
1064 fits_set_hcomp_scale (outfptr, fpvar.
scale, &stat);
1065 fits_set_hcomp_smooth (outfptr, fpvar.
smooth, &stat);
1067 fp_pack_hdu (infptr, outfptr, fpvar, islossless, &stat);
1070 fits_write_chksum (outfptr, &stat);
1073 fits_movrel_hdu (infptr, 1, NULL, &stat);
1076 if (stat == END_OF_FILE) stat = 0;
1081 fits_movabs_hdu (outfptr, 1, NULL, &stat);
1082 fits_write_chksum (outfptr, &stat);
1089 fits_close_file (infptr, &stat);
1161 fitsfile *infptr, *outfptr;
1162 int stat=0, hdutype, extnum, single = 0;
1163 char *loc, *hduloc = 0, hduname[
SZ_STR] = { 0 };
1165 fits_open_file (&infptr, infits, READONLY, &stat);
1166 fits_create_file (&outfptr, outfits, &stat);
1176 loc = strchr(hduloc,
',');
1181 strcpy(hduname, hduloc);
1186 hduloc += strlen(hduname);
1190 if (isdigit( (
int) hduname[0]) ) {
1191 extnum = strtol(hduname, &loc, 10);
1196 fits_movabs_hdu(infptr, extnum + 1, &hdutype, &stat);
1197 if (hdutype != IMAGE_HDU)
1201 hdutype = IMAGE_HDU;
1202 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1208 hdutype = IMAGE_HDU;
1209 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1214 fp_msg (
"Unable to find and move to extension '");
1228 fits_write_chksum (outfptr, &stat);
1238 loc = strchr(hduloc,
',');
1243 strcpy(hduname, hduloc);
1250 if (isdigit( (
int) hduname[0]) ) {
1251 extnum = strtol(hduname, &loc, 10);
1256 fits_movabs_hdu(infptr, extnum + 1, &hdutype, &stat);
1257 if (hdutype != IMAGE_HDU)
1261 hdutype = IMAGE_HDU;
1262 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1267 hdutype = IMAGE_HDU;
1268 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1272 fp_msg (
"Unable to find and move to extension '");
1279 fits_movrel_hdu (infptr, 1, NULL, &stat);
1283 if (stat == END_OF_FILE) stat = 0;
1288 fits_movabs_hdu (outfptr, 1, NULL, &stat);
1289 fits_write_chksum (outfptr, &stat);
1297 fits_close_file (outfptr, &stat);
1298 fits_close_file (infptr, &stat);
1308 fitsfile *infptr, *outfptr;
1309 int stat=0, hdutype, extnum, single = 0;
1310 char *loc, *hduloc = 0, hduname[
SZ_STR] = { 0 };
1311 size_t outbufferSize = 2880;
1312 void *outbuffer = (
char *)malloc(outbufferSize);
1314 fits_open_file (&infptr, infits, READONLY, &stat);
1315 fits_create_memfile(&outfptr, &outbuffer, &outbufferSize, 2880, realloc, &stat);
1325 loc = strchr(hduloc,
',');
1330 strcpy(hduname, hduloc);
1335 hduloc += strlen(hduname);
1339 if (isdigit( (
int) hduname[0]) ) {
1340 extnum = strtol(hduname, &loc, 10);
1345 fits_movabs_hdu(infptr, extnum + 1, &hdutype, &stat);
1346 if (hdutype != IMAGE_HDU)
1350 hdutype = IMAGE_HDU;
1351 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1357 hdutype = IMAGE_HDU;
1358 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1363 fp_msg (
"Unable to find and move to extension '");
1377 fits_write_chksum (outfptr, &stat);
1387 loc = strchr(hduloc,
',');
1392 strcpy(hduname, hduloc);
1399 if (isdigit( (
int) hduname[0]) ) {
1400 extnum = strtol(hduname, &loc, 10);
1405 fits_movabs_hdu(infptr, extnum + 1, &hdutype, &stat);
1406 if (hdutype != IMAGE_HDU)
1410 hdutype = IMAGE_HDU;
1411 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1416 hdutype = IMAGE_HDU;
1417 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1421 fp_msg (
"Unable to find and move to extension '");
1428 fits_movrel_hdu (infptr, 1, NULL, &stat);
1432 if (stat == END_OF_FILE) stat = 0;
1437 fits_movabs_hdu (outfptr, 1, NULL, &stat);
1438 fits_write_chksum (outfptr, &stat);
1447 fits_close_file (infptr, &stat);
1458 fitsfile *infptr, *outfptr;
1459 int stat=0, hdutype, extnum, single = 0;
1460 char *loc, *hduloc = 0, hduname[
SZ_STR] = { 0 };
1461 size_t outbufferSize = 2880;
1462 void *outbuffer = (
char *)malloc(outbufferSize);
1463 void *inbuffer = (
void *)(inputBuffer);
1465 fits_open_memfile(&infptr,
"", READONLY, &inbuffer, &inputBufferSize, 2880, NULL, &stat);
1466 fits_create_memfile(&outfptr, &outbuffer, &outbufferSize, 2880, realloc, &stat);
1476 loc = strchr(hduloc,
',');
1481 strcpy(hduname, hduloc);
1486 hduloc += strlen(hduname);
1490 if (isdigit( (
int) hduname[0]) ) {
1491 extnum = strtol(hduname, &loc, 10);
1496 fits_movabs_hdu(infptr, extnum + 1, &hdutype, &stat);
1497 if (hdutype != IMAGE_HDU)
1501 hdutype = IMAGE_HDU;
1502 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1508 hdutype = IMAGE_HDU;
1509 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1514 fp_msg (
"Unable to find and move to extension '");
1528 fits_write_chksum (outfptr, &stat);
1538 loc = strchr(hduloc,
',');
1543 strcpy(hduname, hduloc);
1550 if (isdigit( (
int) hduname[0]) ) {
1551 extnum = strtol(hduname, &loc, 10);
1556 fits_movabs_hdu(infptr, extnum + 1, &hdutype, &stat);
1557 if (hdutype != IMAGE_HDU)
1561 hdutype = IMAGE_HDU;
1562 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1567 hdutype = IMAGE_HDU;
1568 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1572 fp_msg (
"Unable to find and move to extension '");
1579 fits_movrel_hdu (infptr, 1, NULL, &stat);
1583 if (stat == END_OF_FILE) stat = 0;
1588 fits_movabs_hdu (outfptr, 1, NULL, &stat);
1589 fits_write_chksum (outfptr, &stat);
1598 fits_close_file (infptr, &stat);
1609 fitsfile *inputfptr, *infptr, *outfptr, *outfptr2, *tempfile;
1611 long naxes[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
1612 int stat=0, totpix=0, naxis=0, ii, hdutype, bitpix = 0, extnum = 0, len;
1613 int tstatus = 0, hdunum, rescale_flag, bpix = 8, ncols;
1614 char dtype[8], dimen[100];
1615 double bscale, rescale, noisemin;
1616 long headstart, datastart, dataend;
1617 float origdata = 0., whole_cpu, whole_elapse, row_elapse, row_cpu, xbits;
1623 fits_open_file (&inputfptr, infits, READONLY, &stat);
1624 fits_create_file (&outfptr, outfits, &stat);
1625 fits_create_file (&outfptr2, outfits2, &stat);
1627 if (stat) { fits_report_error (stderr, stat); exit (stat); }
1633 fits_get_hdu_type (inputfptr, &hdutype, &stat);
1635 if (hdutype == IMAGE_HDU) {
1636 fits_get_img_param (inputfptr, 9, &bitpix, &naxis, naxes, &stat);
1637 for (totpix=1, ii=0; ii < 9; ii++) totpix *= naxes[ii];
1640 if (!fits_is_compressed_image (inputfptr, &stat) && hdutype == IMAGE_HDU &&
1641 naxis != 0 && totpix != 0 && fpvar.
do_images) {
1644 if (fpvar.
rescale_noise != 0. && bitpix > 0 && bitpix < LONGLONG_IMG) {
1647 fits_read_key(inputfptr, TDOUBLE,
"BSCALE", &bscale, 0, &tstatus);
1649 if (tstatus == 0 && bscale != 1.0) {
1651 if (bitpix == LONG_IMG)
1652 fp_i4stat(inputfptr, naxis, naxes, &imagestats, &stat);
1654 fp_i2stat(inputfptr, naxis, naxes, &imagestats, &stat);
1657 noisemin = imagestats.
noise3;
1658 if (imagestats.
noise2 != 0. && imagestats.
noise2 < noisemin) noisemin = imagestats.
noise2;
1659 if (imagestats.
noise5 != 0. && imagestats.
noise5 < noisemin) noisemin = imagestats.
noise5;
1662 if (rescale > 1.0) {
1672 fits_get_hdu_num(inputfptr, &hdunum);
1676 fits_create_img(tempfile, 8, 0, naxes, &stat);
1679 fits_copy_header(inputfptr, tempfile, &stat);
1682 if (bitpix == LONG_IMG)
1683 fp_i4rescale(inputfptr, naxis, naxes, rescale, tempfile, &stat);
1685 fp_i2rescale(inputfptr, naxis, naxes, rescale, tempfile, &stat);
1689 bscale = bscale * rescale;
1690 fits_update_key(tempfile, TDOUBLE,
"BSCALE", &bscale, 0, &stat);
1693 fits_set_hdustruc(tempfile, &stat);
1705 if (bitpix == BYTE_IMG) {
1707 strncpy(dtype,
"8 ",
sizeof(dtype)/
sizeof(dtype[0]));
1708 fp_i2stat(infptr, naxis, naxes, &imagestats, &stat);
1709 }
else if (bitpix == SHORT_IMG) {
1711 strncpy(dtype,
"16 ",
sizeof(dtype)/
sizeof(dtype[0]));
1712 fp_i2stat(infptr, naxis, naxes, &imagestats, &stat);
1713 }
else if (bitpix == LONG_IMG) {
1715 strncpy(dtype,
"32 ",
sizeof(dtype)/
sizeof(dtype[0]));
1716 fp_i4stat(infptr, naxis, naxes, &imagestats, &stat);
1717 }
else if (bitpix == LONGLONG_IMG) {
1719 strncpy(dtype,
"64 ",
sizeof(dtype)/
sizeof(dtype[0]));
1720 }
else if (bitpix == FLOAT_IMG) {
1722 strncpy(dtype,
"-32",
sizeof(dtype)/
sizeof(dtype[0]));
1723 fp_r4stat(infptr, naxis, naxes, &imagestats, &stat);
1724 }
else if (bitpix == DOUBLE_IMG) {
1726 strncpy(dtype,
"-64",
sizeof(dtype)/
sizeof(dtype[0]));
1727 fp_r4stat(infptr, naxis, naxes, &imagestats, &stat);
1729 else dtype[0] =
'\0';
1732 noisemin = imagestats.
noise3;
1733 if (imagestats.
noise2 != 0. && imagestats.
noise2 < noisemin) noisemin = imagestats.
noise2;
1734 if (imagestats.
noise5 != 0. && imagestats.
noise5 < noisemin) noisemin = imagestats.
noise5;
1736 xbits = (float) (log10(noisemin)/.301 + 1.792);
1738 printf(
"\n File: %s\n", infits);
1739 printf(
" Ext BITPIX Dimens. Nulls Min Max Mean Sigma Noise2 Noise3 Noise5 Nbits MaxR\n");
1741 printf(
" %3d %s", extnum, dtype);
1742 snprintf(dimen,100,
" (%ld", naxes[0]);
1744 for (ii = 1; ii < naxis; ii++) {
1746 snprintf(dimen+len,100-len,
",%ld", naxes[ii]);
1749 if (strlen(dimen)<99)
1751 printf(
"%-12s",dimen);
1753 fits_get_hduaddr(inputfptr, &headstart, &datastart, &dataend, &stat);
1754 origdata = (float) ((dataend - datastart)/1000000.);
1758 &row_elapse, &row_cpu, &stat);
1760 printf(
" %5d %6.0f %6.0f %8.1f %#8.2g %#7.3g %#7.3g %#7.3g %#5.1f %#6.2f\n",
1765 printf(
"\n Type Ratio Size (MB) Pk (Sec) UnPk Exact ElpN CPUN Elp1 CPU1\n");
1767 printf(
" Native %5.3f %5.3f %5.3f %5.3f\n",
1768 whole_elapse, whole_cpu, row_elapse, row_cpu);
1772 " %s %d %d %ld %ld %#10.4g %d %#10.4g %#10.4g %#10.4g %#10.4g %#10.4g %#10.4g %#10.4g %#10.4g %#10.4g %#10.4g %#10.4g %#10.4g",
1773 infits, extnum, bitpix, naxes[0], naxes[1], origdata, imagestats.
n_nulls, imagestats.
minval,
1775 imagestats.
noise1, imagestats.
noise2, imagestats.
noise3, imagestats.
noise5, whole_elapse, whole_cpu, row_elapse, row_cpu);
1778 fits_set_lossy_int (outfptr, fpvar.
int_to_float, &stat);
1782 (noisemin < fpvar.
n3min)) {
1785 fits_set_lossy_int (outfptr, 0, &stat);
1786 fits_get_hdu_num(infptr, &hdunum);
1788 printf(
" HDU %d does not meet noise criteria to be quantized, so losslessly compressed.\n", hdunum);
1796 fits_set_compression_type (outfptr, RICE_1, &stat);
1797 fits_set_tile_dim (outfptr, 6, fpvar.
ntile, &stat);
1799 fits_set_quantize_method(outfptr, -1, &stat);
1801 fits_set_quantize_method(outfptr, fpvar.
dither_method, &stat);
1804 fits_set_dither_offset(outfptr, fpvar.
dither_offset, &stat);
1805 fits_set_hcomp_scale (outfptr, fpvar.
scale, &stat);
1806 fits_set_hcomp_smooth (outfptr, fpvar.
smooth, &stat);
1808 fp_test_hdu(infptr, outfptr, outfptr2, fpvar, &stat);
1813 fits_set_compression_type (outfptr, HCOMPRESS_1, &stat);
1814 fits_set_tile_dim (outfptr, 6, fpvar.
ntile, &stat);
1817 fits_set_quantize_method(outfptr, -1, &stat);
1819 fits_set_quantize_method(outfptr, fpvar.
dither_method, &stat);
1822 fits_set_dither_offset(outfptr, fpvar.
dither_offset, &stat);
1823 fits_set_hcomp_scale (outfptr, fpvar.
scale, &stat);
1824 fits_set_hcomp_smooth (outfptr, fpvar.
smooth, &stat);
1826 fp_test_hdu(infptr, outfptr, outfptr2, fpvar, &stat);
1830 fits_set_compression_type (outfptr, GZIP_2, &stat);
1832 fits_set_compression_type (outfptr, GZIP_1, &stat);
1835 fits_set_tile_dim (outfptr, 6, fpvar.
ntile, &stat);
1838 fits_set_quantize_method(outfptr, -1, &stat);
1840 fits_set_quantize_method(outfptr, fpvar.
dither_method, &stat);
1843 fits_set_dither_offset(outfptr, fpvar.
dither_offset, &stat);
1844 fits_set_hcomp_scale (outfptr, fpvar.
scale, &stat);
1845 fits_set_hcomp_smooth (outfptr, fpvar.
smooth, &stat);
1847 fp_test_hdu(infptr, outfptr, outfptr2, fpvar, &stat);
1871 fits_delete_file (infptr, &stat);
1874 }
else if ( (hdutype == BINARY_TBL) && fpvar.
do_tables) {
1876 fits_get_num_rowsll(inputfptr, &nrows, &stat);
1877 fits_get_num_cols(inputfptr, &ncols, &stat);
1881 fits_copy_hdu (inputfptr, outfptr, 0, &stat);
1882 fits_copy_hdu (inputfptr, outfptr2, 0, &stat);
1885 fits_movrel_hdu (inputfptr, 1, NULL, &stat);
1890 if (stat == END_OF_FILE) stat = 0;
1892 fits_close_file (outfptr2, &stat);
1893 fits_close_file (outfptr, &stat);
1894 fits_close_file (inputfptr, &stat);
1897 fits_report_error (stderr, stat);
1903 int *islossless,
int *status)
1906 long naxes[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
1907 int stat=0, totpix=0, naxis=0, ii, hdutype, bitpix;
1908 int tstatus, hdunum;
1909 double bscale, rescale;
1911 char outfits[
SZ_STR], fzalgor[FLEN_VALUE];
1912 long headstart, datastart, dataend, datasize;
1917 if (*status)
return(0);
1919 fits_get_hdu_type (infptr, &hdutype, &stat);
1921 if (hdutype == IMAGE_HDU) {
1922 fits_get_img_param (infptr, 9, &bitpix, &naxis, naxes, &stat);
1923 for (totpix=1, ii=0; ii < 9; ii++) totpix *= naxes[ii];
1928 if (!fits_read_key(infptr, TSTRING,
"FZALGOR", fzalgor, NULL, &tstatus) ) {
1929 if (!strcmp(fzalgor,
"NONE") || !strcmp(fzalgor,
"none") ) {
1930 fits_copy_hdu (infptr, outfptr, 0, &stat);
1939 if (hdutype == BINARY_TBL && fpvar.
do_tables) {
1941 fits_get_hduaddr(infptr, &headstart, &datastart, &dataend, status);
1942 datasize = dataend - datastart;
1944 if (datasize <= 2880) {
1946 fits_copy_hdu (infptr, outfptr, 0, &stat);
1948 fits_compress_table (infptr, outfptr, &stat);
1957 if (fits_is_compressed_image (infptr, &stat) || hdutype != IMAGE_HDU ||
1958 naxis == 0 || totpix == 0 || !fpvar.
do_images) {
1959 fits_copy_hdu (infptr, outfptr, 0, &stat);
1964 if (fpvar.
rescale_noise != 0. && bitpix > 0 && bitpix < LONGLONG_IMG) {
1967 fits_read_key(infptr, TDOUBLE,
"BSCALE", &bscale, 0, &tstatus);
1968 if (tstatus == 0 && bscale != 1.0) {
1970 if (bitpix == LONG_IMG)
1971 fp_i4stat(infptr, naxis, naxes, &imagestats, &stat);
1973 fp_i2stat(infptr, naxis, naxes, &imagestats, &stat);
1976 noisemin = imagestats.
noise3;
1977 if (imagestats.
noise2 != 0. && imagestats.
noise2 < noisemin) noisemin = imagestats.
noise2;
1978 if (imagestats.
noise5 != 0. && imagestats.
noise5 < noisemin) noisemin = imagestats.
noise5;
1981 if (rescale > 1.0) {
1987 fits_file_name(outfptr, outfits, &stat);
1992 fits_get_hdu_num(infptr, &hdunum);
1996 fits_create_img(tempfile, 8, 0, naxes, &stat);
1999 fits_copy_header(infptr, tempfile, &stat);
2002 if (bitpix == LONG_IMG)
2003 fp_i4rescale(infptr, naxis, naxes, rescale, tempfile, &stat);
2005 fp_i2rescale(infptr, naxis, naxes, rescale, tempfile, &stat);
2010 bscale = bscale * rescale;
2011 fits_update_key(tempfile, TDOUBLE,
"BSCALE", &bscale, 0, &stat);
2014 fits_set_hdustruc(tempfile, &stat);
2016 fits_img_compress (tempfile, outfptr, &stat);
2017 fits_delete_file (tempfile, &stat);
2031 if (bitpix >= LONG_IMG)
2032 fp_i4stat(infptr, naxis, naxes, &imagestats, &stat);
2034 fp_i2stat(infptr, naxis, naxes, &imagestats, &stat);
2037 ffrhdu(infptr, &hdutype, &stat);
2040 noisemin = imagestats.
noise3;
2041 if (imagestats.
noise2 != 0. && imagestats.
noise2 < noisemin) noisemin = imagestats.
noise2;
2042 if (imagestats.
noise5 != 0. && imagestats.
noise5 < noisemin) noisemin = imagestats.
noise5;
2048 fits_set_lossy_int (outfptr, 0, &stat);
2050 fits_get_hdu_num(infptr, &hdunum);
2052 printf(
" HDU %d does not meet noise criteria to be quantized, so losslessly compressed.\n", hdunum);
2061 fits_img_compress (infptr, outfptr, &stat);
2116 float *whole_cpu,
float *row_elapse,
float *row_cpu,
int *status)
2118 unsigned char *carray, cnull = 0;
2119 short *sarray, snull=0;
2120 int bitpix, naxis, anynull, *iarray, inull = 0;
2121 long ii, naxes[9], fpixel[9]={1,1,1,1,1,1,1,1,1}, lpixel[9]={1,1,1,1,1,1,1,1,1};
2122 long inc[9]={1,1,1,1,1,1,1,1,1} ;
2123 float *earray, enull = 0, filesize;
2124 double *darray, dnull = 0;
2126 if (*status)
return(*status);
2128 fits_get_img_param (infptr, 9, &bitpix, &naxis, naxes, status);
2130 if (naxis != 2)
return(*status);
2132 lpixel[0] = naxes[0];
2133 lpixel[1] = naxes[1];
2136 filesize = (float) (naxes[0] * abs(bitpix) / 8000000. * naxes[1]);
2139 fits_set_bscale(infptr, 1.0, 0.0, status);
2143 if (bitpix == BYTE_IMG) {
2144 carray = calloc(naxes[1]*naxes[0],
sizeof(
char));
2151 fits_read_subset(infptr, TBYTE, fpixel, lpixel, inc, &cnull,
2152 carray, &anynull, status);
2155 gettime(whole_elapse, whole_cpu, status);
2165 for (ii = 0; ii < naxes[1]; ii++) {
2167 fits_read_pix(infptr, TBYTE, fpixel, naxes[0], &cnull,
2168 carray, &anynull, status);
2171 gettime(row_elapse, row_cpu, status);
2175 }
else if (bitpix == SHORT_IMG) {
2176 sarray = calloc(naxes[0]*naxes[1],
sizeof(
short));
2179 fits_read_subset(infptr, TSHORT, fpixel, lpixel, inc, &snull,
2180 sarray, &anynull, status);
2182 gettime(whole_elapse, whole_cpu, status);
2187 for (ii = 0; ii < naxes[1]; ii++) {
2190 fits_read_pix(infptr, TSHORT, fpixel, naxes[0], &snull,
2191 sarray, &anynull, status);
2194 gettime(row_elapse, row_cpu, status);
2199 }
else if (bitpix == LONG_IMG) {
2200 iarray = calloc(naxes[0]*naxes[1],
sizeof(
int));
2204 fits_read_subset(infptr, TINT, fpixel, lpixel, inc, &inull,
2205 iarray, &anynull, status);
2208 gettime(whole_elapse, whole_cpu, status);
2214 for (ii = 0; ii < naxes[1]; ii++) {
2216 fits_read_pix(infptr, TINT, fpixel, naxes[0], &inull,
2217 iarray, &anynull, status);
2220 gettime(row_elapse, row_cpu, status);
2226 }
else if (bitpix == FLOAT_IMG) {
2227 earray = calloc(naxes[1]*naxes[0],
sizeof(
float));
2231 fits_read_subset(infptr, TFLOAT, fpixel, lpixel, inc, &enull,
2232 earray, &anynull, status);
2235 gettime(whole_elapse, whole_cpu, status);
2240 for (ii = 0; ii < naxes[1]; ii++) {
2242 fits_read_pix(infptr, TFLOAT, fpixel, naxes[0], &enull,
2243 earray, &anynull, status);
2246 gettime(row_elapse, row_cpu, status);
2251 }
else if (bitpix == DOUBLE_IMG) {
2252 darray = calloc(naxes[1]*naxes[0],
sizeof(
double));
2256 fits_read_subset(infptr, TDOUBLE, fpixel, lpixel, inc, &dnull,
2257 darray, &anynull, status);
2260 gettime(whole_elapse, whole_cpu, status);
2265 for (ii = 0; ii < naxes[1]; ii++) {
2267 fits_read_pix(infptr, TDOUBLE, fpixel, naxes[0], &dnull,
2268 darray, &anynull, status);
2271 gettime(row_elapse, row_cpu, status);
2277 if (whole_elapse) *whole_elapse = *whole_elapse / filesize;
2278 if (row_elapse) *row_elapse = *row_elapse / filesize;
2279 if (whole_cpu) *whole_cpu = *whole_cpu / filesize;
2280 if (row_cpu) *row_cpu = *row_cpu / filesize;
2285int fp_test_hdu (fitsfile *infptr, fitsfile *outfptr, fitsfile *outfptr2,
2291 int stat = 0, hdutype, comptype;
2292 char ctype[20], lossless[4];
2293 long headstart, datastart, dataend;
2294 float origdata = 0., compressdata = 0.;
2295 float compratio = 0., packcpu = 0., unpackcpu = 0.;
2296 float elapse, whole_elapse, row_elapse, whole_cpu, row_cpu;
2297 unsigned long datasum1, datasum2, hdusum;
2299 if (*status)
return(0);
2306 fits_get_compression_type(outfptr, &comptype, &stat);
2307 if (comptype == RICE_1)
2308 strcpy(ctype,
"RICE");
2309 else if (comptype == GZIP_1)
2310 strcpy(ctype,
"GZIP1");
2311 else if (comptype == GZIP_2)
2312 strcpy(ctype,
"GZIP2");
2316 else if (comptype == PLIO_1)
2317 strcpy(ctype,
"PLIO");
2318 else if (comptype == HCOMPRESS_1)
2319 strcpy(ctype,
"HCOMP");
2320 else if (comptype == NOCOMPRESS)
2321 strcpy(ctype,
"NONE");
2323 fp_msg (
"Error: unsupported image compression type ");
2324 *status = DATA_COMPRESSION_ERR;
2332 fits_img_compress (infptr, outfptr, &stat);
2335 gettime(&elapse, &packcpu, &stat);
2339 &row_elapse, &row_cpu, &stat);
2350 fits_img_decompress (outfptr, outfptr2, &stat);
2353 gettime(&elapse, &unpackcpu, &stat);
2360 fits_get_hduaddr(infptr, &headstart, &datastart, &dataend, &stat);
2361 origdata = (float) ((dataend - datastart)/1000000.);
2363 fits_get_hduaddr(outfptr, &headstart, &datastart, &dataend, &stat);
2364 compressdata = (float) ((dataend - datastart)/1000000.);
2366 if (compressdata != 0)
2367 compratio = (float) origdata / (
float) compressdata;
2371 fits_get_chksum(infptr, &datasum1, &hdusum, &stat);
2372 fits_get_chksum(outfptr2, &datasum2, &hdusum, &stat);
2374 if ( datasum1 == datasum2) {
2375 strcpy(lossless,
"Yes");
2377 strcpy(lossless,
"No");
2380 printf(
" %-5s %6.2f %7.2f ->%7.2f %7.2f %7.2f %s %5.3f %5.3f %5.3f %5.3f\n",
2381 ctype, compratio, origdata, compressdata,
2382 packcpu, unpackcpu, lossless, whole_elapse, whole_cpu,
2383 row_elapse, row_cpu);
2387 fprintf(
outreport,
" %6.3f %5.2f %5.2f %s %7.3f %7.3f %7.3f %7.3f",
2388 compratio, packcpu, unpackcpu, lossless, whole_elapse, whole_cpu,
2389 row_elapse, row_cpu);
2394 fits_delete_hdu(outfptr, &hdutype, &stat);
2395 fits_delete_hdu(outfptr2, &hdutype, &stat);
2398 printf(
" %-5s (unable to compress image)\n", ctype);
2402 if (stat == DATA_COMPRESSION_ERR) stat = 0;
2511 long fpixel[9] = {1,1,1,1,1,1,1,1,1};
2512 long lpixel[9] = {1,1,1,1,1,1,1,1,1};
2513 long inc[9] = {1,1,1,1,1,1,1,1,1};
2514 long i1, i2, npix, ngood, nx, ny;
2515 short *intarray, minvalue, maxvalue, nullvalue;
2516 int anynul, tstatus, checknull = 1;
2517 double mean, sigma, noise1, noise2, noise3, noise5;
2520 i1 = naxes[0]/2 - (
XSAMPLE/2 - 1);
2521 i2 = naxes[0]/2 + (
XSAMPLE/2);
2523 if (i2 > naxes[0]) i2 = naxes[0];
2529 i1 = naxes[1]/2 - (
YSAMPLE/2 - 1);
2530 i2 = naxes[1]/2 + (
YSAMPLE/2);
2532 if (i2 > naxes[1]) i2 = naxes[1];
2542 fpixel[2] = naxes[2]/2 + 1;
2543 lpixel[2] = naxes[2]/2 + 1;
2546 intarray = calloc(npix,
sizeof(
short));
2548 *status = MEMORY_ALLOCATION;
2553 fits_set_bscale(infptr, 1.0, 0.0, status);
2555 fits_read_subset_sht(infptr, 0, naxis, naxes, fpixel, lpixel, inc,
2556 0, intarray, &anynul, status);
2560 fits_read_key(infptr, TSHORT,
"BLANK", &nullvalue, 0, &tstatus);
2568 fits_img_stats_short(intarray, nx, ny, checknull, nullvalue,
2569 &ngood, &minvalue, &maxvalue, &mean, &sigma, &noise1, &noise2, &noise3, &noise5, status);
2571 imagestats->
n_nulls = npix - ngood;
2572 imagestats->
minval = minvalue;
2573 imagestats->
maxval = maxvalue;
2574 imagestats->
mean = mean;
2575 imagestats->
sigma = sigma;
2576 imagestats->
noise1 = noise1;
2577 imagestats->
noise2 = noise2;
2578 imagestats->
noise3 = noise3;
2579 imagestats->
noise5 = noise5;
2592 long fpixel[9] = {1,1,1,1,1,1,1,1,1};
2593 long lpixel[9] = {1,1,1,1,1,1,1,1,1};
2594 long inc[9] = {1,1,1,1,1,1,1,1,1};
2595 long i1, i2, npix, ngood, nx, ny;
2596 int *intarray, minvalue, maxvalue, nullvalue;
2597 int anynul, tstatus, checknull = 1;
2598 double mean, sigma, noise1, noise2, noise3, noise5;
2601 i1 = naxes[0]/2 - (
XSAMPLE/2 - 1);
2602 i2 = naxes[0]/2 + (
XSAMPLE/2);
2604 if (i2 > naxes[0]) i2 = naxes[0];
2610 i1 = naxes[1]/2 - (
YSAMPLE/2 - 1);
2611 i2 = naxes[1]/2 + (
YSAMPLE/2);
2613 if (i2 > naxes[1]) i2 = naxes[1];
2623 fpixel[2] = naxes[2]/2 + 1;
2624 lpixel[2] = naxes[2]/2 + 1;
2627 intarray = calloc(npix,
sizeof(
int));
2629 *status = MEMORY_ALLOCATION;
2634 fits_set_bscale(infptr, 1.0, 0.0, status);
2636 fits_read_subset_int(infptr, 0, naxis, naxes, fpixel, lpixel, inc,
2637 0, intarray, &anynul, status);
2641 fits_read_key(infptr, TINT,
"BLANK", &nullvalue, 0, &tstatus);
2649 fits_img_stats_int(intarray, nx, ny, checknull, nullvalue,
2650 &ngood, &minvalue, &maxvalue, &mean, &sigma, &noise1, &noise2, &noise3, &noise5, status);
2652 imagestats->
n_nulls = npix - ngood;
2653 imagestats->
minval = minvalue;
2654 imagestats->
maxval = maxvalue;
2655 imagestats->
mean = mean;
2656 imagestats->
sigma = sigma;
2657 imagestats->
noise1 = noise1;
2658 imagestats->
noise2 = noise2;
2659 imagestats->
noise3 = noise3;
2660 imagestats->
noise5 = noise5;
2673 long fpixel[9] = {1,1,1,1,1,1,1,1,1};
2674 long lpixel[9] = {1,1,1,1,1,1,1,1,1};
2675 long inc[9] = {1,1,1,1,1,1,1,1,1};
2676 long i1, i2, npix, ngood, nx, ny;
2677 float *array, minvalue, maxvalue, nullvalue = FLOATNULLVALUE;
2678 int anynul,checknull = 1;
2679 double mean, sigma, noise1, noise2, noise3, noise5;
2682 i1 = naxes[0]/2 - (
XSAMPLE/2 - 1);
2683 i2 = naxes[0]/2 + (
XSAMPLE/2);
2685 if (i2 > naxes[0]) i2 = naxes[0];
2691 i1 = naxes[1]/2 - (
YSAMPLE/2 - 1);
2692 i2 = naxes[1]/2 + (
YSAMPLE/2);
2694 if (i2 > naxes[1]) i2 = naxes[1];
2704 fpixel[2] = naxes[2]/2 + 1;
2705 lpixel[2] = naxes[2]/2 + 1;
2708 array = calloc(npix,
sizeof(
float));
2710 *status = MEMORY_ALLOCATION;
2714 fits_read_subset_flt(infptr, 0, naxis, naxes, fpixel, lpixel, inc,
2715 nullvalue, array, &anynul, status);
2725 fits_img_stats_float(array, nx, ny, checknull, nullvalue,
2726 &ngood, &minvalue, &maxvalue, &mean, &sigma, &noise1, &noise2, &noise3, &noise5, status);
2728 imagestats->
n_nulls = npix - ngood;
2729 imagestats->
minval = minvalue;
2730 imagestats->
maxval = maxvalue;
2731 imagestats->
mean = mean;
2732 imagestats->
sigma = sigma;
2733 imagestats->
noise1 = noise1;
2734 imagestats->
noise2 = noise2;
2735 imagestats->
noise3 = noise3;
2736 imagestats->
noise5 = noise5;