15 #if defined(unix) || defined(__unix__) || defined(__unix)
31 # define NINT(x) ((x >= 0.) ? (int) (x + 0.5) : (int) (x - 0.5))
32 # define NSHRT(x) ((x >= 0.) ? (short) (x + 0.5) : (short) (x - 0.5))
40 #if defined(CLOCKS_PER_SEC)
41 #define CLOCKTICKS CLOCKS_PER_SEC
44 #define CLOCKTICKS 1000000
53 #define UNUSED(x) (void)(x)
54 #define fp_tmpnam(suffix, rootname, tmpnam) _fp_tmpnam((char *)suffix, (char *)rootname, (char *)tmpnam)
59 fp_msg (
"Input and output files are unchanged.\n");
65 int status = 0, hdunum;
71 fits_get_hdu_num(infptr, &hdunum);
73 fits_close_file (infptr, &status);
77 snprintf(msg,
SZ_STR,
" in HDU number %d\n", hdunum);
82 snprintf(msg,
SZ_STR,
"Error: Unable to process input file\n");
85 fits_report_error (stderr, stat);
88 fits_delete_file(outfptr, &status);
89 fp_msg (
"Input file is unchanged.\n");
96 char cfitsioversion[40];
99 fits_get_version(&version);
100 snprintf(cfitsioversion, 40,
" CFITSIO version %5.3f", version);
112 diskfile = fopen(filename,
"r");
128 if (strlen(suffix) + strlen(rootname) >
SZ_STR-5) {
129 fp_msg (
"Error: filename is too long to create temporary file\n"); exit (-1);
132 strcpy (tmpnam, rootname);
133 strcat(tmpnam, suffix);
135 maxtry =
SZ_STR - strlen(tmpnam) - 1;
137 for (ii = 0; ii < maxtry; ii++) {
139 if (strlen(tmpnam) >
SZ_STR-2)
141 fp_msg (
"\nCould not create temporary file name:\n");
150 fp_msg (
"\nCould not create temporary file name:\n");
177 fpptr->
ntile[0] = (long) -1;
179 for (ii=1; ii < MAX_COMPRESS_DIM; ii++)
180 fpptr->
ntile[ii] = (
long) 1;
212 int hdunum, iarg, stat=0;
216 fp_msg (
"Error: internal initialization error\n"); exit (-1);
219 for (iarg=fpvar.
firstfile; iarg < argc; iarg++) {
220 strncpy (infits, argv[iarg],
SZ_STR-1);
223 if (strchr (infits,
'[') || strchr (infits,
']')) {
224 fp_msg (
"Error: section/extension notation not supported: ");
229 fp_msg (
"Error: can't find or read input file ");
fp_msg (infits);
233 fits_open_file (&infptr, infits, READONLY, &stat);
234 if (stat) { fits_report_error (stderr, stat); exit (stat); }
237 fits_get_num_hdus (infptr, &hdunum, &stat);
238 fits_movabs_hdu (infptr, hdunum, NULL, &stat);
239 fits_get_hduaddrll(infptr, NULL, NULL, &sizell, &stat);
248 #if defined(_MSC_VER)
250 snprintf(msg,
SZ_STR,
"%I64d bytes)\n", sizell);
fp_msg (msg);
251 #elif (USE_LL_SUFFIX == 1)
252 snprintf(msg,
SZ_STR,
"%lld bytes)\n", sizell);
fp_msg (msg);
254 snprintf(msg,
SZ_STR,
"%ld bytes)\n", sizell);
fp_msg (msg);
258 fits_close_file (infptr, &stat);
259 if (stat) { fits_report_error (stderr, stat); exit (stat); }
266 long naxes[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
268 int naxis=0, hdutype, bitpix, hdupos, stat=0, ii;
269 unsigned long datasum, hdusum;
271 fits_movabs_hdu (infptr, 1, NULL, &stat);
276 for (hdupos=1; ! stat; hdupos++) {
277 fits_get_hdu_type (infptr, &hdutype, &stat);
285 fits_read_keyword (infptr,
"XTENSION", val, com, &stat);
286 if (stat == KEY_NO_EXIST) {
293 }
else if (hdutype == IMAGE_HDU) {
295 if (strncmp (val+1,
"IMAGE", 5) &&
296 strncmp (val+1,
"BINTABLE", 5)) {
299 hdutype = IMAGE_HDU + ASCII_TBL + BINARY_TBL;
303 fits_get_chksum(infptr, &datasum, &hdusum, &stat);
305 if (hdutype == IMAGE_HDU) {
306 snprintf (msg,
SZ_STR,
" %d IMAGE", hdupos);
fp_msg (msg);
307 snprintf (msg,
SZ_STR,
" SUMS=%lu/%lu", (
unsigned long) (~((
int) hdusum)), datasum);
fp_msg (msg);
309 fits_get_img_param (infptr, 9, &bitpix, &naxis, naxes, &stat);
311 snprintf (msg,
SZ_STR,
" BITPIX=%d", bitpix);
fp_msg (msg);
315 }
else if (naxis == 1) {
316 snprintf (msg,
SZ_STR,
" [%ld]", naxes[1]);
fp_msg (msg);
319 for (ii=1; ii < naxis; ii++) {
325 if (fits_is_compressed_image (infptr, &stat)) {
326 fits_read_keyword (infptr,
"ZCMPTYPE", val, com, &stat);
329 if (! strncmp (val+1,
"RICE_1", 6))
331 else if (! strncmp (val+1,
"GZIP_1", 6))
332 fp_msg (
" tiled_gzip_1\n");
333 else if (! strncmp (val+1,
"GZIP_2", 6))
334 fp_msg (
" tiled_gzip_2\n");
335 else if (! strncmp (val+1,
"PLIO_1", 6))
337 else if (! strncmp (val+1,
"HCOMPRESS_1", 11))
338 fp_msg (
" tiled_hcompress\n");
345 }
else if (hdutype == ASCII_TBL) {
346 snprintf (msg,
SZ_STR,
" %d ASCII_TBL", hdupos);
fp_msg (msg);
347 snprintf (msg,
SZ_STR,
" SUMS=%lu/%lu\n", (
unsigned long) (~((
int) hdusum)), datasum);
fp_msg (msg);
349 }
else if (hdutype == BINARY_TBL) {
350 snprintf (msg,
SZ_STR,
" %d BINARY_TBL", hdupos);
fp_msg (msg);
351 snprintf (msg,
SZ_STR,
" SUMS=%lu/%lu\n", (
unsigned long) (~((
int) hdusum)), datasum);
fp_msg (msg);
354 snprintf (msg,
SZ_STR,
" %d OTHER", hdupos);
fp_msg (msg);
355 snprintf (msg,
SZ_STR,
" SUMS=%lu/%lu", (
unsigned long) (~((
int) hdusum)), datasum);
fp_msg (msg);
359 fits_movrel_hdu (infptr, 1, NULL, &stat);
368 int iarg, namelen, nfiles = 0;
371 fp_msg (
"Error: internal initialization error\n"); exit (-1);
374 for (iarg=fpptr->
firstfile; iarg < argc; iarg++) {
378 if (strlen(argv[iarg]) >
SZ_STR - 4) {
379 fp_msg (
"Error: input file name\n ");
fp_msg (argv[iarg]);
383 strncpy (infits, argv[iarg],
SZ_STR-1);
384 if (infits[0] ==
'-' && infits[1] !=
'\0') {
386 fp_msg (
"Error: invalid input file name\n ");
fp_msg (argv[iarg]);
390 if (strchr (infits,
'[') || strchr (infits,
']')) {
391 fp_msg (
"Error: section/extension notation not supported: ");
399 if (infits[0] !=
'-') {
401 strcat(infits,
".fz");
403 namelen = strlen(infits);
404 infits[namelen - 3] =
'\0';
405 fp_msg (
"Error: can't find or read input file ");
fp_msg (infits);
409 namelen = strlen(infits);
410 strcat(infits,
".fz");
412 infits[namelen] =
'\0';
413 fp_msg (
"Error: ambiguous input file name. Which file should be unpacked?:\n ");
418 infits[namelen] =
'\0';
431 fp_msg (
"Error: cannot use same output file name for multiple files:\n ");
438 fp_msg (
"Error: output file already exists:\n ");
447 if (strlen(fpptr->
prefix) + strlen(infits) >
SZ_STR - 1) {
448 fp_msg (
"Error: output file name for\n ");
fp_msg (infits);
449 fp_msg (
"\n is too long with the prefix\n");
fp_noop (); exit (-1);
451 strcat(outfits,fpptr->
prefix);
455 if (infits[0] ==
'-') {
456 strcpy(outfits,
"output.fits");
458 strcpy(outfits, infits);
462 namelen = strlen(outfits);
463 if ( !strcmp(
".gz", outfits + namelen - 3) ) {
464 outfits[namelen - 3] =
'\0';
469 if (infits[0] !=
'-') {
470 namelen = strlen(outfits);
471 if ( !strcmp(
".fz", outfits + namelen - 3) ) {
472 outfits[namelen - 3] =
'\0';
474 fp_msg (
"Error: input compressed file ");
fp_msg (infits);
475 fp_msg (
"\n does not have the default .fz suffix.\n");
481 if (strcmp(infits, outfits)) {
483 fp_msg (
"Error: output file already exists:\n ");
fp_msg (outfits);
490 if (strlen(outfits)+3 >
SZ_STR-1)
492 fp_msg (
"Error: output file name too long:\n ");
fp_msg (outfits);
495 strcat(outfits,
".gz");
497 fp_msg (
"Error: output file already exists:\n ");
fp_msg (outfits);
500 namelen = strlen(outfits);
501 outfits[namelen - 3] =
'\0';
507 if (infits[0] !=
'-') {
509 if (strlen(infits)+3 >
SZ_STR-1)
511 fp_msg (
"Error: input file name too long:\n ");
fp_msg (infits);
514 strcat(infits,
".gz");
516 namelen = strlen(infits);
517 infits[namelen - 3] =
'\0';
518 fp_msg (
"Error: can't find or read input file ");
fp_msg (infits);
525 namelen = strlen(infits);
526 if ( !strcmp(
".fz", infits + namelen - 3) ) {
527 fp_msg (
"Error: fpack input file already has '.fz' suffix\n" );
fp_msg (infits);
537 if (infits[0] ==
'-') {
538 strcpy(outfits,
"input.fits");
540 strcpy(outfits, infits);
544 namelen = strlen(outfits);
545 if ( !strcmp(
".gz", outfits + namelen - 3) ) {
546 outfits[namelen - 3] =
'\0';
550 namelen = strlen(outfits);
551 if ( !strcmp(
".imh", outfits + namelen - 4) ) {
552 outfits[namelen - 4] =
'\0';
553 strcat(outfits,
".fits");
558 strcat(outfits,
".fz");
561 if (strcmp(infits, outfits)) {
563 fp_msg (
"Error: output file already exists:\n ");
fp_msg (outfits);
577 int fp_loop (
int argc,
char *argv[],
int unpack,
char *output_filename,
fpstate fpvar)
580 char temp[
SZ_STR], answer[30];
581 char valchar[]=
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.#()+,-_@[]/^{}";
582 int ichar=0, outlen=0, iarg, islossless, namelen, iraf_infile = 0, status = 0, ifail;
585 fp_msg (
"Error: internal initialization error\n"); exit (-1);
587 fp_msg (
"Error: internal preflight error\n"); exit (-1);
592 fprintf(
outreport,
" Filename Extension BITPIX NAXIS1 NAXIS2 Size N_nulls Minval Maxval Mean Sigm Noise1 Noise2 Noise3 Noise5 T_whole T_rowbyrow ");
593 fprintf(
outreport,
"[Comp_ratio, Pack_cpu, Unpack_cpu, Lossless readtimes] (repeated for Rice, Hcompress, and GZIP)\n");
603 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
609 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
615 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
620 for (iarg=fpvar.
firstfile; iarg < argc; iarg++) {
626 strncpy (infits, argv[iarg],
SZ_STR - 1);
633 if (infits[0] !=
'-') {
635 strcat(infits,
".fz");
640 strcpy(outfits,
"-");
643 strcpy(outfits, fpvar.
outfile);
648 strcat(outfits,fpvar.
prefix);
652 if (infits[0] ==
'-') {
653 strcpy(outfits,
"output.fits");
656 strcpy(outfits, output_filename);
660 namelen = strlen(outfits);
661 if ( !strcmp(
".gz", outfits + namelen - 3) ) {
662 outfits[namelen - 3] =
'\0';
667 namelen = strlen(outfits);
668 if ( !strcmp(
".fz", outfits + namelen - 3) ) {
669 outfits[namelen - 3] =
'\0';
677 strcpy(outfits,
"-");
681 if (infits[0] ==
'-') {
682 strcpy(outfits,
"input.fits");
684 strcpy(outfits, output_filename);
688 namelen = strlen(outfits);
689 if ( !strcmp(
".gz", outfits + namelen - 3) ) {
690 outfits[namelen - 3] =
'\0';
694 namelen = strlen(outfits);
695 if ( !strcmp(
".imh", outfits + namelen - 4) ) {
696 outfits[namelen - 4] =
'\0';
697 strcat(outfits,
".fits");
704 strcat(outfits,
".fz");
708 strncpy(temp, outfits,
SZ_STR-1);
711 if (infits[0] !=
'-') {
712 if (!strcmp(infits, outfits) ) {
716 fp_msg (
"\nError: must use -F flag to clobber input file.\n");
731 printf(
"%s ", infits);
763 fp_msg(
"Failed to rename temporary file name:\n ");
774 fp_pack (infits, outfits, fpvar, &islossless);
783 if (!strcmp(infits, temp) && fpvar.
clobber ) {
788 fp_msg (
"\nwas compressed with a LOSSY method. Overwrite the\n");
789 fp_msg (
"original file with the compressed version? (Y/N) ");
790 if (fgets(answer, 29, stdin) && answer[0] !=
'Y' && answer[0] !=
'y') {
791 fp_msg (
"\noriginal file NOT overwritten!\n");
798 if (fits_delete_iraf_file(infits, &status)) {
799 fp_msg(
"\nError deleting IRAF .imh and .pix files.\n");
804 #if defined(unix) || defined(__unix__) || defined(__unix)
806 if (rename (outfits, temp) != 0) {
807 fp_msg (
"\nError renaming tmp file to ");
814 if (rename (outfits, temp) != 0) {
815 fp_msg (
"\nError renaming tmp file to ");
821 strcpy(outfits, temp);
827 fp_msg (
"\nwas compressed with a LOSSY method. \n");
828 fp_msg (
"Delete the original file? (Y/N) ");
829 if (fgets(answer, 29, stdin) && answer[0] !=
'Y' && answer[0] !=
'y') {
830 fp_msg (
"\noriginal file NOT deleted!\n");
833 if (fits_delete_iraf_file(infits, &status)) {
834 fp_msg(
"\nError deleting IRAF .imh and .pix files.\n");
837 }
else if (remove(infits) != 0) {
838 fp_msg (
"\nError deleting input file ");
844 if (fits_delete_iraf_file(infits, &status)) {
845 fp_msg(
"\nError deleting IRAF .imh and .pix files.\n");
848 }
else if (remove(infits) != 0) {
849 fp_msg (
"\nError deleting input file ");
857 strcpy(temp,
"gzip -1 ");
858 outlen = strlen(outfits);
859 if (outlen + 8 >
SZ_STR-1)
861 fp_msg(
"\nError: Output file name is too long.\n");
864 for (ichar=0; ichar < outlen; ++ichar)
866 if (!strchr(valchar, outfits[ichar]))
868 fp_msg(
"\n Error: Invalid characters in output file name.\n");
872 strcat(temp,outfits);
873 int rc = system(temp);
875 strcat(outfits,
".gz");
879 printf(
"-> %s\n", outfits);
893 fitsfile *infptr, *outfptr;
896 fits_open_file (&infptr, infits, READONLY, &stat);
899 fits_report_error (stderr, stat);
903 fits_create_file (&outfptr, outfits, &stat);
914 fits_set_lossy_int (outfptr, fpvar.
int_to_float, &stat);
915 fits_set_compression_type (outfptr, fpvar.
comptype, &stat);
916 fits_set_tile_dim (outfptr, 6, fpvar.
ntile, &stat);
919 fits_set_quantize_method(outfptr, -1, &stat);
921 fits_set_quantize_method(outfptr, fpvar.
dither_method, &stat);
925 fits_set_hcomp_scale (outfptr, fpvar.
scale, &stat);
926 fits_set_hcomp_smooth (outfptr, fpvar.
smooth, &stat);
928 fp_pack_hdu (infptr, outfptr, fpvar, islossless, &stat);
931 fits_write_chksum (outfptr, &stat);
934 fits_movrel_hdu (infptr, 1, NULL, &stat);
937 if (stat == END_OF_FILE) stat = 0;
942 fits_movabs_hdu (outfptr, 1, NULL, &stat);
943 fits_write_chksum (outfptr, &stat);
950 fits_close_file (outfptr, &stat);
951 fits_close_file (infptr, &stat);
961 fitsfile *infptr, *outfptr;
963 size_t outbufferSize = 2880;
964 void *outbuffer = (
char *)malloc(outbufferSize);
965 void *inbuffer = (
void *)(inputBuffer);
967 fits_open_memfile(&infptr,
"", READONLY, &inbuffer, &inputBufferSize, 2880, NULL, &stat);
971 fits_report_error (stderr, stat);
975 fits_create_memfile(&outfptr, &outbuffer, &outbufferSize, 2880, realloc, &stat);
986 fits_set_lossy_int (outfptr, fpvar.
int_to_float, &stat);
987 fits_set_compression_type (outfptr, fpvar.
comptype, &stat);
988 fits_set_tile_dim (outfptr, 6, fpvar.
ntile, &stat);
991 fits_set_quantize_method(outfptr, -1, &stat);
993 fits_set_quantize_method(outfptr, fpvar.
dither_method, &stat);
997 fits_set_hcomp_scale (outfptr, fpvar.
scale, &stat);
998 fits_set_hcomp_smooth (outfptr, fpvar.
smooth, &stat);
1000 fp_pack_hdu (infptr, outfptr, fpvar, islossless, &stat);
1003 fits_write_chksum (outfptr, &stat);
1006 fits_movrel_hdu (infptr, 1, NULL, &stat);
1009 if (stat == END_OF_FILE) stat = 0;
1014 fits_movabs_hdu (outfptr, 1, NULL, &stat);
1015 fits_write_chksum (outfptr, &stat);
1022 fits_close_file (infptr, &stat);
1031 int fp_pack_data_to_data (
const char *inputBuffer,
size_t inputBufferSize,
unsigned char **outputBuffer,
size_t *outputBufferSize,
1032 fpstate fpvar,
int *islossless)
1034 fitsfile *infptr, *outfptr;
1036 void *inbuffer = (
void *)(inputBuffer);
1038 fits_open_memfile(&infptr,
"", READONLY, &inbuffer, &inputBufferSize, 2880, NULL, &stat);
1041 fits_report_error (stderr, stat);
1045 void *outbuffer = (
void *)(outputBuffer);
1046 fits_create_memfile(&outfptr, outbuffer, outputBufferSize, 2880, realloc, &stat);
1057 fits_set_lossy_int (outfptr, fpvar.
int_to_float, &stat);
1058 fits_set_compression_type (outfptr, fpvar.
comptype, &stat);
1059 fits_set_tile_dim (outfptr, 6, fpvar.
ntile, &stat);
1062 fits_set_quantize_method(outfptr, -1, &stat);
1064 fits_set_quantize_method(outfptr, fpvar.
dither_method, &stat);
1067 fits_set_dither_offset(outfptr, fpvar.
dither_offset, &stat);
1068 fits_set_hcomp_scale (outfptr, fpvar.
scale, &stat);
1069 fits_set_hcomp_smooth (outfptr, fpvar.
smooth, &stat);
1071 fp_pack_hdu (infptr, outfptr, fpvar, islossless, &stat);
1074 fits_write_chksum (outfptr, &stat);
1077 fits_movrel_hdu (infptr, 1, NULL, &stat);
1080 if (stat == END_OF_FILE) stat = 0;
1085 fits_movabs_hdu (outfptr, 1, NULL, &stat);
1086 fits_write_chksum (outfptr, &stat);
1093 fits_close_file (infptr, &stat);
1104 size_t outbufferSize = 2880;
1105 void *outbuffer = (
char *)malloc(outbufferSize);
1107 fits_create_memfile(&outfptr, &outbuffer, &outbufferSize, 2880, realloc, &stat);
1118 fits_set_lossy_int (outfptr, fpvar.
int_to_float, &stat);
1119 fits_set_compression_type (outfptr, fpvar.
comptype, &stat);
1120 fits_set_tile_dim (outfptr, 6, fpvar.
ntile, &stat);
1123 fits_set_quantize_method(outfptr, -1, &stat);
1125 fits_set_quantize_method(outfptr, fpvar.
dither_method, &stat);
1128 fits_set_dither_offset(outfptr, fpvar.
dither_offset, &stat);
1129 fits_set_hcomp_scale (outfptr, fpvar.
scale, &stat);
1130 fits_set_hcomp_smooth (outfptr, fpvar.
smooth, &stat);
1132 fp_pack_hdu (infptr, outfptr, fpvar, islossless, &stat);
1135 fits_write_chksum (outfptr, &stat);
1138 fits_movrel_hdu (infptr, 1, NULL, &stat);
1141 if (stat == END_OF_FILE) stat = 0;
1146 fits_movabs_hdu (outfptr, 1, NULL, &stat);
1147 fits_write_chksum (outfptr, &stat);
1154 fits_close_file (infptr, &stat);
1165 fitsfile *infptr, *outfptr;
1166 int stat=0, hdutype, extnum, single = 0;
1167 char *loc, *hduloc = 0, hduname[
SZ_STR] = { 0 };
1169 fits_open_file (&infptr, infits, READONLY, &stat);
1170 fits_create_file (&outfptr, outfits, &stat);
1180 loc = strchr(hduloc,
',');
1185 strcpy(hduname, hduloc);
1190 hduloc += strlen(hduname);
1194 if (isdigit( (
int) hduname[0]) ) {
1195 extnum = strtol(hduname, &loc, 10);
1200 fits_movabs_hdu(infptr, extnum + 1, &hdutype, &stat);
1201 if (hdutype != IMAGE_HDU)
1205 hdutype = IMAGE_HDU;
1206 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1212 hdutype = IMAGE_HDU;
1213 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1218 fp_msg (
"Unable to find and move to extension '");
1232 fits_write_chksum (outfptr, &stat);
1242 loc = strchr(hduloc,
',');
1247 strcpy(hduname, hduloc);
1254 if (isdigit( (
int) hduname[0]) ) {
1255 extnum = strtol(hduname, &loc, 10);
1260 fits_movabs_hdu(infptr, extnum + 1, &hdutype, &stat);
1261 if (hdutype != IMAGE_HDU)
1265 hdutype = IMAGE_HDU;
1266 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1271 hdutype = IMAGE_HDU;
1272 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1276 fp_msg (
"Unable to find and move to extension '");
1283 fits_movrel_hdu (infptr, 1, NULL, &stat);
1287 if (stat == END_OF_FILE) stat = 0;
1292 fits_movabs_hdu (outfptr, 1, NULL, &stat);
1293 fits_write_chksum (outfptr, &stat);
1301 fits_close_file (outfptr, &stat);
1302 fits_close_file (infptr, &stat);
1312 fitsfile *infptr, *outfptr;
1313 int stat=0, hdutype, extnum, single = 0;
1314 char *loc, *hduloc = 0, hduname[
SZ_STR] = { 0 };
1315 size_t outbufferSize = 2880;
1316 void *outbuffer = (
char *)malloc(outbufferSize);
1318 fits_open_file (&infptr, infits, READONLY, &stat);
1319 fits_create_memfile(&outfptr, &outbuffer, &outbufferSize, 2880, realloc, &stat);
1329 loc = strchr(hduloc,
',');
1334 strcpy(hduname, hduloc);
1339 hduloc += strlen(hduname);
1343 if (isdigit( (
int) hduname[0]) ) {
1344 extnum = strtol(hduname, &loc, 10);
1349 fits_movabs_hdu(infptr, extnum + 1, &hdutype, &stat);
1350 if (hdutype != IMAGE_HDU)
1354 hdutype = IMAGE_HDU;
1355 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1361 hdutype = IMAGE_HDU;
1362 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1367 fp_msg (
"Unable to find and move to extension '");
1381 fits_write_chksum (outfptr, &stat);
1391 loc = strchr(hduloc,
',');
1396 strcpy(hduname, hduloc);
1403 if (isdigit( (
int) hduname[0]) ) {
1404 extnum = strtol(hduname, &loc, 10);
1409 fits_movabs_hdu(infptr, extnum + 1, &hdutype, &stat);
1410 if (hdutype != IMAGE_HDU)
1414 hdutype = IMAGE_HDU;
1415 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1420 hdutype = IMAGE_HDU;
1421 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1425 fp_msg (
"Unable to find and move to extension '");
1432 fits_movrel_hdu (infptr, 1, NULL, &stat);
1436 if (stat == END_OF_FILE) stat = 0;
1441 fits_movabs_hdu (outfptr, 1, NULL, &stat);
1442 fits_write_chksum (outfptr, &stat);
1451 fits_close_file (infptr, &stat);
1462 fitsfile *infptr, *outfptr;
1463 int stat=0, hdutype, extnum, single = 0;
1464 char *loc, *hduloc = 0, hduname[
SZ_STR] = { 0 };
1465 size_t outbufferSize = 2880;
1466 void *outbuffer = (
char *)malloc(outbufferSize);
1467 void *inbuffer = (
void *)(inputBuffer);
1469 fits_open_memfile(&infptr,
"", READONLY, &inbuffer, &inputBufferSize, 2880, NULL, &stat);
1470 fits_create_memfile(&outfptr, &outbuffer, &outbufferSize, 2880, realloc, &stat);
1480 loc = strchr(hduloc,
',');
1485 strcpy(hduname, hduloc);
1490 hduloc += strlen(hduname);
1494 if (isdigit( (
int) hduname[0]) ) {
1495 extnum = strtol(hduname, &loc, 10);
1500 fits_movabs_hdu(infptr, extnum + 1, &hdutype, &stat);
1501 if (hdutype != IMAGE_HDU)
1505 hdutype = IMAGE_HDU;
1506 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1512 hdutype = IMAGE_HDU;
1513 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1518 fp_msg (
"Unable to find and move to extension '");
1532 fits_write_chksum (outfptr, &stat);
1542 loc = strchr(hduloc,
',');
1547 strcpy(hduname, hduloc);
1554 if (isdigit( (
int) hduname[0]) ) {
1555 extnum = strtol(hduname, &loc, 10);
1560 fits_movabs_hdu(infptr, extnum + 1, &hdutype, &stat);
1561 if (hdutype != IMAGE_HDU)
1565 hdutype = IMAGE_HDU;
1566 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1571 hdutype = IMAGE_HDU;
1572 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1576 fp_msg (
"Unable to find and move to extension '");
1583 fits_movrel_hdu (infptr, 1, NULL, &stat);
1587 if (stat == END_OF_FILE) stat = 0;
1592 fits_movabs_hdu (outfptr, 1, NULL, &stat);
1593 fits_write_chksum (outfptr, &stat);
1602 fits_close_file (infptr, &stat);
1613 fitsfile *inputfptr, *infptr, *outfptr, *outfptr2, *tempfile;
1615 long naxes[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
1616 int stat=0, totpix=0, naxis=0, ii, hdutype, bitpix = 0, extnum = 0, len;
1617 int tstatus = 0, hdunum, rescale_flag, bpix = 8, ncols;
1618 char dtype[8], dimen[100];
1619 double bscale, rescale, noisemin;
1620 long headstart, datastart, dataend;
1621 float origdata = 0., whole_cpu, whole_elapse, row_elapse, row_cpu, xbits;
1627 fits_open_file (&inputfptr, infits, READONLY, &stat);
1628 fits_create_file (&outfptr, outfits, &stat);
1629 fits_create_file (&outfptr2, outfits2, &stat);
1631 if (stat) { fits_report_error (stderr, stat); exit (stat); }
1637 fits_get_hdu_type (inputfptr, &hdutype, &stat);
1639 if (hdutype == IMAGE_HDU) {
1640 fits_get_img_param (inputfptr, 9, &bitpix, &naxis, naxes, &stat);
1641 for (totpix=1, ii=0; ii < 9; ii++) totpix *= naxes[ii];
1644 if (!fits_is_compressed_image (inputfptr, &stat) && hdutype == IMAGE_HDU &&
1645 naxis != 0 && totpix != 0 && fpvar.
do_images) {
1648 if (fpvar.
rescale_noise != 0. && bitpix > 0 && bitpix < LONGLONG_IMG) {
1651 fits_read_key(inputfptr, TDOUBLE,
"BSCALE", &bscale, 0, &tstatus);
1653 if (tstatus == 0 && bscale != 1.0) {
1655 if (bitpix == LONG_IMG)
1656 fp_i4stat(inputfptr, naxis, naxes, &imagestats, &stat);
1658 fp_i2stat(inputfptr, naxis, naxes, &imagestats, &stat);
1661 noisemin = imagestats.
noise3;
1662 if (imagestats.
noise2 != 0. && imagestats.
noise2 < noisemin) noisemin = imagestats.
noise2;
1663 if (imagestats.
noise5 != 0. && imagestats.
noise5 < noisemin) noisemin = imagestats.
noise5;
1666 if (rescale > 1.0) {
1676 fits_get_hdu_num(inputfptr, &hdunum);
1680 fits_create_img(tempfile, 8, 0, naxes, &stat);
1683 fits_copy_header(inputfptr, tempfile, &stat);
1686 if (bitpix == LONG_IMG)
1687 fp_i4rescale(inputfptr, naxis, naxes, rescale, tempfile, &stat);
1689 fp_i2rescale(inputfptr, naxis, naxes, rescale, tempfile, &stat);
1693 bscale = bscale * rescale;
1694 fits_update_key(tempfile, TDOUBLE,
"BSCALE", &bscale, 0, &stat);
1697 fits_set_hdustruc(tempfile, &stat);
1709 if (bitpix == BYTE_IMG) {
1711 strncpy(dtype,
"8 ",
sizeof(dtype)/
sizeof(dtype[0]));
1712 fp_i2stat(infptr, naxis, naxes, &imagestats, &stat);
1713 }
else if (bitpix == SHORT_IMG) {
1715 strncpy(dtype,
"16 ",
sizeof(dtype)/
sizeof(dtype[0]));
1716 fp_i2stat(infptr, naxis, naxes, &imagestats, &stat);
1717 }
else if (bitpix == LONG_IMG) {
1719 strncpy(dtype,
"32 ",
sizeof(dtype)/
sizeof(dtype[0]));
1720 fp_i4stat(infptr, naxis, naxes, &imagestats, &stat);
1721 }
else if (bitpix == LONGLONG_IMG) {
1723 strncpy(dtype,
"64 ",
sizeof(dtype)/
sizeof(dtype[0]));
1724 }
else if (bitpix == FLOAT_IMG) {
1726 strncpy(dtype,
"-32",
sizeof(dtype)/
sizeof(dtype[0]));
1727 fp_r4stat(infptr, naxis, naxes, &imagestats, &stat);
1728 }
else if (bitpix == DOUBLE_IMG) {
1730 strncpy(dtype,
"-64",
sizeof(dtype)/
sizeof(dtype[0]));
1731 fp_r4stat(infptr, naxis, naxes, &imagestats, &stat);
1733 else dtype[0] =
'\0';
1736 noisemin = imagestats.
noise3;
1737 if (imagestats.
noise2 != 0. && imagestats.
noise2 < noisemin) noisemin = imagestats.
noise2;
1738 if (imagestats.
noise5 != 0. && imagestats.
noise5 < noisemin) noisemin = imagestats.
noise5;
1740 xbits = (float) (log10(noisemin)/.301 + 1.792);
1742 printf(
"\n File: %s\n", infits);
1743 printf(
" Ext BITPIX Dimens. Nulls Min Max Mean Sigma Noise2 Noise3 Noise5 Nbits MaxR\n");
1745 printf(
" %3d %s", extnum, dtype);
1746 snprintf(dimen,100,
" (%ld", naxes[0]);
1748 for (ii = 1; ii < naxis; ii++) {
1750 snprintf(dimen+len,100-len,
",%ld", naxes[ii]);
1753 if (strlen(dimen)<99)
1755 printf(
"%-12s",dimen);
1757 fits_get_hduaddr(inputfptr, &headstart, &datastart, &dataend, &stat);
1758 origdata = (float) ((dataend - datastart)/1000000.);
1762 &row_elapse, &row_cpu, &stat);
1764 printf(
" %5d %6.0f %6.0f %8.1f %#8.2g %#7.3g %#7.3g %#7.3g %#5.1f %#6.2f\n",
1769 printf(
"\n Type Ratio Size (MB) Pk (Sec) UnPk Exact ElpN CPUN Elp1 CPU1\n");
1771 printf(
" Native %5.3f %5.3f %5.3f %5.3f\n",
1772 whole_elapse, whole_cpu, row_elapse, row_cpu);
1776 " %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",
1777 infits, extnum, bitpix, naxes[0], naxes[1], origdata, imagestats.
n_nulls, imagestats.
minval,
1779 imagestats.
noise1, imagestats.
noise2, imagestats.
noise3, imagestats.
noise5, whole_elapse, whole_cpu, row_elapse, row_cpu);
1782 fits_set_lossy_int (outfptr, fpvar.
int_to_float, &stat);
1786 (noisemin < fpvar.
n3min)) {
1789 fits_set_lossy_int (outfptr, 0, &stat);
1790 fits_get_hdu_num(infptr, &hdunum);
1792 printf(
" HDU %d does not meet noise criteria to be quantized, so losslessly compressed.\n", hdunum);
1800 fits_set_compression_type (outfptr, RICE_1, &stat);
1801 fits_set_tile_dim (outfptr, 6, fpvar.
ntile, &stat);
1803 fits_set_quantize_method(outfptr, -1, &stat);
1805 fits_set_quantize_method(outfptr, fpvar.
dither_method, &stat);
1808 fits_set_dither_offset(outfptr, fpvar.
dither_offset, &stat);
1809 fits_set_hcomp_scale (outfptr, fpvar.
scale, &stat);
1810 fits_set_hcomp_smooth (outfptr, fpvar.
smooth, &stat);
1812 fp_test_hdu(infptr, outfptr, outfptr2, fpvar, &stat);
1817 fits_set_compression_type (outfptr, HCOMPRESS_1, &stat);
1818 fits_set_tile_dim (outfptr, 6, fpvar.
ntile, &stat);
1821 fits_set_quantize_method(outfptr, -1, &stat);
1823 fits_set_quantize_method(outfptr, fpvar.
dither_method, &stat);
1826 fits_set_dither_offset(outfptr, fpvar.
dither_offset, &stat);
1827 fits_set_hcomp_scale (outfptr, fpvar.
scale, &stat);
1828 fits_set_hcomp_smooth (outfptr, fpvar.
smooth, &stat);
1830 fp_test_hdu(infptr, outfptr, outfptr2, fpvar, &stat);
1834 fits_set_compression_type (outfptr, GZIP_2, &stat);
1836 fits_set_compression_type (outfptr, GZIP_1, &stat);
1839 fits_set_tile_dim (outfptr, 6, fpvar.
ntile, &stat);
1842 fits_set_quantize_method(outfptr, -1, &stat);
1844 fits_set_quantize_method(outfptr, fpvar.
dither_method, &stat);
1847 fits_set_dither_offset(outfptr, fpvar.
dither_offset, &stat);
1848 fits_set_hcomp_scale (outfptr, fpvar.
scale, &stat);
1849 fits_set_hcomp_smooth (outfptr, fpvar.
smooth, &stat);
1851 fp_test_hdu(infptr, outfptr, outfptr2, fpvar, &stat);
1875 fits_delete_file (infptr, &stat);
1878 }
else if ( (hdutype == BINARY_TBL) && fpvar.
do_tables) {
1880 fits_get_num_rowsll(inputfptr, &nrows, &stat);
1881 fits_get_num_cols(inputfptr, &ncols, &stat);
1882 #if defined(_MSC_VER)
1884 printf(
"\n File: %s, HDU %d, %d cols X %I64d rows\n", infits, extnum, ncols, nrows);
1885 #elif (USE_LL_SUFFIX == 1)
1886 printf(
"\n File: %s, HDU %d, %d cols X %lld rows\n", infits, extnum, ncols, nrows);
1888 printf(
"\n File: %s, HDU %d, %d cols X %ld rows\n", infits, extnum, ncols, nrows);
1893 fits_copy_hdu (inputfptr, outfptr, 0, &stat);
1894 fits_copy_hdu (inputfptr, outfptr2, 0, &stat);
1897 fits_movrel_hdu (inputfptr, 1, NULL, &stat);
1902 if (stat == END_OF_FILE) stat = 0;
1904 fits_close_file (outfptr2, &stat);
1905 fits_close_file (outfptr, &stat);
1906 fits_close_file (inputfptr, &stat);
1909 fits_report_error (stderr, stat);
1915 int *islossless,
int *status)
1918 long naxes[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
1919 int stat=0, totpix=0, naxis=0, ii, hdutype, bitpix;
1920 int tstatus, hdunum;
1921 double bscale, rescale;
1923 char outfits[
SZ_STR], fzalgor[FLEN_VALUE];
1924 long headstart, datastart, dataend, datasize;
1929 if (*status)
return(0);
1931 fits_get_hdu_type (infptr, &hdutype, &stat);
1933 if (hdutype == IMAGE_HDU) {
1934 fits_get_img_param (infptr, 9, &bitpix, &naxis, naxes, &stat);
1935 for (totpix=1, ii=0; ii < 9; ii++) totpix *= naxes[ii];
1940 if (!fits_read_key(infptr, TSTRING,
"FZALGOR", fzalgor, NULL, &tstatus) ) {
1941 if (!strcmp(fzalgor,
"NONE") || !strcmp(fzalgor,
"none") ) {
1942 fits_copy_hdu (infptr, outfptr, 0, &stat);
1951 if (hdutype == BINARY_TBL && fpvar.
do_tables) {
1953 fits_get_hduaddr(infptr, &headstart, &datastart, &dataend, status);
1954 datasize = dataend - datastart;
1956 if (datasize <= 2880) {
1958 fits_copy_hdu (infptr, outfptr, 0, &stat);
1960 fits_compress_table (infptr, outfptr, &stat);
1969 if (fits_is_compressed_image (infptr, &stat) || hdutype != IMAGE_HDU ||
1970 naxis == 0 || totpix == 0 || !fpvar.
do_images) {
1971 fits_copy_hdu (infptr, outfptr, 0, &stat);
1976 if (fpvar.
rescale_noise != 0. && bitpix > 0 && bitpix < LONGLONG_IMG) {
1979 fits_read_key(infptr, TDOUBLE,
"BSCALE", &bscale, 0, &tstatus);
1980 if (tstatus == 0 && bscale != 1.0) {
1982 if (bitpix == LONG_IMG)
1983 fp_i4stat(infptr, naxis, naxes, &imagestats, &stat);
1985 fp_i2stat(infptr, naxis, naxes, &imagestats, &stat);
1988 noisemin = imagestats.
noise3;
1989 if (imagestats.
noise2 != 0. && imagestats.
noise2 < noisemin) noisemin = imagestats.
noise2;
1990 if (imagestats.
noise5 != 0. && imagestats.
noise5 < noisemin) noisemin = imagestats.
noise5;
1993 if (rescale > 1.0) {
1999 fits_file_name(outfptr, outfits, &stat);
2004 fits_get_hdu_num(infptr, &hdunum);
2008 fits_create_img(tempfile, 8, 0, naxes, &stat);
2011 fits_copy_header(infptr, tempfile, &stat);
2014 if (bitpix == LONG_IMG)
2015 fp_i4rescale(infptr, naxis, naxes, rescale, tempfile, &stat);
2017 fp_i2rescale(infptr, naxis, naxes, rescale, tempfile, &stat);
2022 bscale = bscale * rescale;
2023 fits_update_key(tempfile, TDOUBLE,
"BSCALE", &bscale, 0, &stat);
2026 fits_set_hdustruc(tempfile, &stat);
2028 fits_img_compress (tempfile, outfptr, &stat);
2029 fits_delete_file (tempfile, &stat);
2043 if (bitpix >= LONG_IMG)
2044 fp_i4stat(infptr, naxis, naxes, &imagestats, &stat);
2046 fp_i2stat(infptr, naxis, naxes, &imagestats, &stat);
2049 ffrhdu(infptr, &hdutype, &stat);
2052 noisemin = imagestats.
noise3;
2053 if (imagestats.
noise2 != 0. && imagestats.
noise2 < noisemin) noisemin = imagestats.
noise2;
2054 if (imagestats.
noise5 != 0. && imagestats.
noise5 < noisemin) noisemin = imagestats.
noise5;
2060 fits_set_lossy_int (outfptr, 0, &stat);
2062 fits_get_hdu_num(infptr, &hdunum);
2064 printf(
" HDU %d does not meet noise criteria to be quantized, so losslessly compressed.\n", hdunum);
2073 fits_img_compress (infptr, outfptr, &stat);
2094 if (*status > 0)
return(0);
2096 fits_get_hdu_type (infptr, &hdutype, status);
2100 if (hdutype == BINARY_TBL) {
2102 fits_read_key(infptr, TLOGICAL,
"ZTABLE", &lval, NULL, status);
2104 if (*status == 0 && lval != 0) {
2106 fits_uncompress_table (infptr, outfptr, status);
2108 if (*status == KEY_NO_EXIST)
2110 fits_copy_hdu (infptr, outfptr, 0, status);
2116 }
else if (fits_is_compressed_image (infptr, status)) {
2118 fits_img_decompress (infptr, outfptr, status);
2121 fits_copy_hdu (infptr, outfptr, 0, status);
2128 float *whole_cpu,
float *row_elapse,
float *row_cpu,
int *status)
2130 unsigned char *carray, cnull = 0;
2131 short *sarray, snull=0;
2132 int bitpix, naxis, anynull, *iarray, inull = 0;
2133 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};
2134 long inc[9]={1,1,1,1,1,1,1,1,1} ;
2135 float *earray, enull = 0, filesize;
2136 double *darray, dnull = 0;
2138 if (*status)
return(*status);
2140 fits_get_img_param (infptr, 9, &bitpix, &naxis, naxes, status);
2142 if (naxis != 2)
return(*status);
2144 lpixel[0] = naxes[0];
2145 lpixel[1] = naxes[1];
2148 filesize = (float) (naxes[0] * abs(bitpix) / 8000000. * naxes[1]);
2151 fits_set_bscale(infptr, 1.0, 0.0, status);
2155 if (bitpix == BYTE_IMG) {
2156 carray = calloc(naxes[1]*naxes[0],
sizeof(
char));
2163 fits_read_subset(infptr, TBYTE, fpixel, lpixel, inc, &cnull,
2164 carray, &anynull, status);
2167 gettime(whole_elapse, whole_cpu, status);
2177 for (ii = 0; ii < naxes[1]; ii++) {
2179 fits_read_pix(infptr, TBYTE, fpixel, naxes[0], &cnull,
2180 carray, &anynull, status);
2183 gettime(row_elapse, row_cpu, status);
2187 }
else if (bitpix == SHORT_IMG) {
2188 sarray = calloc(naxes[0]*naxes[1],
sizeof(
short));
2191 fits_read_subset(infptr, TSHORT, fpixel, lpixel, inc, &snull,
2192 sarray, &anynull, status);
2194 gettime(whole_elapse, whole_cpu, status);
2199 for (ii = 0; ii < naxes[1]; ii++) {
2202 fits_read_pix(infptr, TSHORT, fpixel, naxes[0], &snull,
2203 sarray, &anynull, status);
2206 gettime(row_elapse, row_cpu, status);
2211 }
else if (bitpix == LONG_IMG) {
2212 iarray = calloc(naxes[0]*naxes[1],
sizeof(
int));
2216 fits_read_subset(infptr, TINT, fpixel, lpixel, inc, &inull,
2217 iarray, &anynull, status);
2220 gettime(whole_elapse, whole_cpu, status);
2226 for (ii = 0; ii < naxes[1]; ii++) {
2228 fits_read_pix(infptr, TINT, fpixel, naxes[0], &inull,
2229 iarray, &anynull, status);
2232 gettime(row_elapse, row_cpu, status);
2238 }
else if (bitpix == FLOAT_IMG) {
2239 earray = calloc(naxes[1]*naxes[0],
sizeof(
float));
2243 fits_read_subset(infptr, TFLOAT, fpixel, lpixel, inc, &enull,
2244 earray, &anynull, status);
2247 gettime(whole_elapse, whole_cpu, status);
2252 for (ii = 0; ii < naxes[1]; ii++) {
2254 fits_read_pix(infptr, TFLOAT, fpixel, naxes[0], &enull,
2255 earray, &anynull, status);
2258 gettime(row_elapse, row_cpu, status);
2263 }
else if (bitpix == DOUBLE_IMG) {
2264 darray = calloc(naxes[1]*naxes[0],
sizeof(
double));
2268 fits_read_subset(infptr, TDOUBLE, fpixel, lpixel, inc, &dnull,
2269 darray, &anynull, status);
2272 gettime(whole_elapse, whole_cpu, status);
2277 for (ii = 0; ii < naxes[1]; ii++) {
2279 fits_read_pix(infptr, TDOUBLE, fpixel, naxes[0], &dnull,
2280 darray, &anynull, status);
2283 gettime(row_elapse, row_cpu, status);
2289 if (whole_elapse) *whole_elapse = *whole_elapse / filesize;
2290 if (row_elapse) *row_elapse = *row_elapse / filesize;
2291 if (whole_cpu) *whole_cpu = *whole_cpu / filesize;
2292 if (row_cpu) *row_cpu = *row_cpu / filesize;
2297 int fp_test_hdu (fitsfile *infptr, fitsfile *outfptr, fitsfile *outfptr2,
2303 int stat = 0, hdutype, comptype;
2304 char ctype[20], lossless[4];
2305 long headstart, datastart, dataend;
2306 float origdata = 0., compressdata = 0.;
2307 float compratio = 0., packcpu = 0., unpackcpu = 0.;
2308 float elapse, whole_elapse, row_elapse, whole_cpu, row_cpu;
2309 unsigned long datasum1, datasum2, hdusum;
2311 if (*status)
return(0);
2318 fits_get_compression_type(outfptr, &comptype, &stat);
2319 if (comptype == RICE_1)
2320 strcpy(ctype,
"RICE");
2321 else if (comptype == GZIP_1)
2322 strcpy(ctype,
"GZIP1");
2323 else if (comptype == GZIP_2)
2324 strcpy(ctype,
"GZIP2");
2328 else if (comptype == PLIO_1)
2329 strcpy(ctype,
"PLIO");
2330 else if (comptype == HCOMPRESS_1)
2331 strcpy(ctype,
"HCOMP");
2332 else if (comptype == NOCOMPRESS)
2333 strcpy(ctype,
"NONE");
2335 fp_msg (
"Error: unsupported image compression type ");
2336 *status = DATA_COMPRESSION_ERR;
2344 fits_img_compress (infptr, outfptr, &stat);
2347 gettime(&elapse, &packcpu, &stat);
2351 &row_elapse, &row_cpu, &stat);
2362 fits_img_decompress (outfptr, outfptr2, &stat);
2365 gettime(&elapse, &unpackcpu, &stat);
2372 fits_get_hduaddr(infptr, &headstart, &datastart, &dataend, &stat);
2373 origdata = (float) ((dataend - datastart)/1000000.);
2375 fits_get_hduaddr(outfptr, &headstart, &datastart, &dataend, &stat);
2376 compressdata = (float) ((dataend - datastart)/1000000.);
2378 if (compressdata != 0)
2379 compratio = (float) origdata / (
float) compressdata;
2383 fits_get_chksum(infptr, &datasum1, &hdusum, &stat);
2384 fits_get_chksum(outfptr2, &datasum2, &hdusum, &stat);
2386 if ( datasum1 == datasum2) {
2387 strcpy(lossless,
"Yes");
2389 strcpy(lossless,
"No");
2392 printf(
" %-5s %6.2f %7.2f ->%7.2f %7.2f %7.2f %s %5.3f %5.3f %5.3f %5.3f\n",
2393 ctype, compratio, origdata, compressdata,
2394 packcpu, unpackcpu, lossless, whole_elapse, whole_cpu,
2395 row_elapse, row_cpu);
2399 fprintf(
outreport,
" %6.3f %5.2f %5.2f %s %7.3f %7.3f %7.3f %7.3f",
2400 compratio, packcpu, unpackcpu, lossless, whole_elapse, whole_cpu,
2401 row_elapse, row_cpu);
2406 fits_delete_hdu(outfptr, &hdutype, &stat);
2407 fits_delete_hdu(outfptr2, &hdutype, &stat);
2410 printf(
" %-5s (unable to compress image)\n", ctype);
2414 if (stat == DATA_COMPRESSION_ERR) stat = 0;
2427 int stat = 0, hdutype, tstatus = 0;
2428 char fzalgor[FLEN_VALUE];
2429 LONGLONG headstart, datastart, dataend;
2432 if (*status)
return(0);
2435 if (!fits_read_key(infptr, TSTRING,
"FZALGOR", fzalgor, NULL, &tstatus) ) {
2436 if (!strcmp(fzalgor,
"NONE") || !strcmp(fzalgor,
"none")) {
2441 fits_get_hduaddrll(infptr, &headstart, &datastart, &dataend, status);
2444 if (dataend - datastart <= 2880) {
2450 fits_compress_table (infptr, outfptr, &stat);
2453 gettime(&elapse, &cpu, &stat);
2455 fits_delete_hdu(outfptr, &hdutype, &stat);
2457 printf(
"\nElapsed time = %f, cpu = %f\n", elapse, cpu);
2459 fits_report_error (stderr, stat);
2466 #if defined(unix) || defined(__unix__) || defined(__unix)
2471 gettimeofday (&tv, NULL);
2487 int gettime(
float *elapse,
float *elapscpu,
int *status)
2489 #if defined(unix) || defined(__unix__) || defined(__unix)
2496 gettimeofday (&tv, NULL);
2499 stopmilli = tv.tv_usec/1000;
2500 stopsec = tv.tv_sec;
2511 *elapse = *elapscpu;
2523 long fpixel[9] = {1,1,1,1,1,1,1,1,1};
2524 long lpixel[9] = {1,1,1,1,1,1,1,1,1};
2525 long inc[9] = {1,1,1,1,1,1,1,1,1};
2526 long i1, i2, npix, ngood, nx, ny;
2527 short *intarray, minvalue, maxvalue, nullvalue;
2528 int anynul, tstatus, checknull = 1;
2529 double mean, sigma, noise1, noise2, noise3, noise5;
2532 i1 = naxes[0]/2 - (
XSAMPLE/2 - 1);
2533 i2 = naxes[0]/2 + (
XSAMPLE/2);
2535 if (i2 > naxes[0]) i2 = naxes[0];
2541 i1 = naxes[1]/2 - (
YSAMPLE/2 - 1);
2542 i2 = naxes[1]/2 + (
YSAMPLE/2);
2544 if (i2 > naxes[1]) i2 = naxes[1];
2554 fpixel[2] = naxes[2]/2 + 1;
2555 lpixel[2] = naxes[2]/2 + 1;
2558 intarray = calloc(npix,
sizeof(
short));
2560 *status = MEMORY_ALLOCATION;
2565 fits_set_bscale(infptr, 1.0, 0.0, status);
2567 fits_read_subset_sht(infptr, 0, naxis, naxes, fpixel, lpixel, inc,
2568 0, intarray, &anynul, status);
2572 fits_read_key(infptr, TSHORT,
"BLANK", &nullvalue, 0, &tstatus);
2580 fits_img_stats_short(intarray, nx, ny, checknull, nullvalue,
2581 &ngood, &minvalue, &maxvalue, &mean, &sigma, &noise1, &noise2, &noise3, &noise5, status);
2583 imagestats->
n_nulls = npix - ngood;
2584 imagestats->
minval = minvalue;
2585 imagestats->
maxval = maxvalue;
2586 imagestats->
mean = mean;
2587 imagestats->
sigma = sigma;
2588 imagestats->
noise1 = noise1;
2589 imagestats->
noise2 = noise2;
2590 imagestats->
noise3 = noise3;
2591 imagestats->
noise5 = noise5;
2604 long fpixel[9] = {1,1,1,1,1,1,1,1,1};
2605 long lpixel[9] = {1,1,1,1,1,1,1,1,1};
2606 long inc[9] = {1,1,1,1,1,1,1,1,1};
2607 long i1, i2, npix, ngood, nx, ny;
2608 int *intarray, minvalue, maxvalue, nullvalue;
2609 int anynul, tstatus, checknull = 1;
2610 double mean, sigma, noise1, noise2, noise3, noise5;
2613 i1 = naxes[0]/2 - (
XSAMPLE/2 - 1);
2614 i2 = naxes[0]/2 + (
XSAMPLE/2);
2616 if (i2 > naxes[0]) i2 = naxes[0];
2622 i1 = naxes[1]/2 - (
YSAMPLE/2 - 1);
2623 i2 = naxes[1]/2 + (
YSAMPLE/2);
2625 if (i2 > naxes[1]) i2 = naxes[1];
2635 fpixel[2] = naxes[2]/2 + 1;
2636 lpixel[2] = naxes[2]/2 + 1;
2639 intarray = calloc(npix,
sizeof(
int));
2641 *status = MEMORY_ALLOCATION;
2646 fits_set_bscale(infptr, 1.0, 0.0, status);
2648 fits_read_subset_int(infptr, 0, naxis, naxes, fpixel, lpixel, inc,
2649 0, intarray, &anynul, status);
2653 fits_read_key(infptr, TINT,
"BLANK", &nullvalue, 0, &tstatus);
2661 fits_img_stats_int(intarray, nx, ny, checknull, nullvalue,
2662 &ngood, &minvalue, &maxvalue, &mean, &sigma, &noise1, &noise2, &noise3, &noise5, status);
2664 imagestats->
n_nulls = npix - ngood;
2665 imagestats->
minval = minvalue;
2666 imagestats->
maxval = maxvalue;
2667 imagestats->
mean = mean;
2668 imagestats->
sigma = sigma;
2669 imagestats->
noise1 = noise1;
2670 imagestats->
noise2 = noise2;
2671 imagestats->
noise3 = noise3;
2672 imagestats->
noise5 = noise5;
2685 long fpixel[9] = {1,1,1,1,1,1,1,1,1};
2686 long lpixel[9] = {1,1,1,1,1,1,1,1,1};
2687 long inc[9] = {1,1,1,1,1,1,1,1,1};
2688 long i1, i2, npix, ngood, nx, ny;
2689 float *array, minvalue, maxvalue, nullvalue = FLOATNULLVALUE;
2690 int anynul,checknull = 1;
2691 double mean, sigma, noise1, noise2, noise3, noise5;
2694 i1 = naxes[0]/2 - (
XSAMPLE/2 - 1);
2695 i2 = naxes[0]/2 + (
XSAMPLE/2);
2697 if (i2 > naxes[0]) i2 = naxes[0];
2703 i1 = naxes[1]/2 - (
YSAMPLE/2 - 1);
2704 i2 = naxes[1]/2 + (
YSAMPLE/2);
2706 if (i2 > naxes[1]) i2 = naxes[1];
2716 fpixel[2] = naxes[2]/2 + 1;
2717 lpixel[2] = naxes[2]/2 + 1;
2720 array = calloc(npix,
sizeof(
float));
2722 *status = MEMORY_ALLOCATION;
2726 fits_read_subset_flt(infptr, 0, naxis, naxes, fpixel, lpixel, inc,
2727 nullvalue, array, &anynul, status);
2737 fits_img_stats_float(array, nx, ny, checknull, nullvalue,
2738 &ngood, &minvalue, &maxvalue, &mean, &sigma, &noise1, &noise2, &noise3, &noise5, status);
2740 imagestats->
n_nulls = npix - ngood;
2741 imagestats->
minval = minvalue;
2742 imagestats->
maxval = maxvalue;
2743 imagestats->
mean = mean;
2744 imagestats->
sigma = sigma;
2745 imagestats->
noise1 = noise1;
2746 imagestats->
noise2 = noise2;
2747 imagestats->
noise3 = noise3;
2748 imagestats->
noise5 = noise5;
2755 fitsfile *outfptr,
int *status)
2762 long ii, jj, nelem = 1, nx, ny;
2763 short *intarray, nullvalue;
2764 int anynul, tstatus, checknull = 1;
2769 for (ii = 1; ii < naxis; ii++) {
2770 ny = ny * naxes[ii];
2773 intarray = calloc(nx,
sizeof(
short));
2775 *status = MEMORY_ALLOCATION;
2781 fits_read_key(infptr, TSHORT,
"BLANK", &nullvalue, 0, &tstatus);
2787 fits_set_bscale(infptr, 1.0, 0.0, status);
2788 fits_set_bscale(outfptr, 1.0, 0.0, status);
2790 for (ii = 0; ii < ny; ii++) {
2792 fits_read_img_sht(infptr, 1, nelem, nx,
2793 0, intarray, &anynul, status);
2796 for (jj = 0; jj < nx; jj++) {
2797 if (intarray[jj] != nullvalue)
2798 intarray[jj] =
NSHRT( (intarray[jj] / rescale) );
2801 for (jj = 0; jj < nx; jj++)
2802 intarray[jj] =
NSHRT( (intarray[jj] / rescale) );
2805 fits_write_img_sht(outfptr, 1, nelem, nx, intarray, status);
2815 fitsfile *outfptr,
int *status)
2822 long ii, jj, nelem = 1, nx, ny;
2823 int *intarray, nullvalue;
2824 int anynul, tstatus, checknull = 1;
2829 for (ii = 1; ii < naxis; ii++) {
2830 ny = ny * naxes[ii];
2833 intarray = calloc(nx,
sizeof(
int));
2835 *status = MEMORY_ALLOCATION;
2841 fits_read_key(infptr, TINT,
"BLANK", &nullvalue, 0, &tstatus);
2847 fits_set_bscale(infptr, 1.0, 0.0, status);
2848 fits_set_bscale(outfptr, 1.0, 0.0, status);
2850 for (ii = 0; ii < ny; ii++) {
2852 fits_read_img_int(infptr, 1, nelem, nx,
2853 0, intarray, &anynul, status);
2856 for (jj = 0; jj < nx; jj++) {
2857 if (intarray[jj] != nullvalue)
2858 intarray[jj] =
NINT( (intarray[jj] / rescale) );
2861 for (jj = 0; jj < nx; jj++)
2862 intarray[jj] =
NINT( (intarray[jj] / rescale) );
2865 fits_write_img_int(outfptr, 1, nelem, nx, intarray, status);
#define DEF_RESCALE_NOISE
int fp_pack_data_to_data(const char *inputBuffer, size_t inputBufferSize, unsigned char **outputBuffer, size_t *outputBufferSize, fpstate fpvar, int *islossless)
void abort_fpack(int sig)
int fp_loop(int argc, char *argv[], int unpack, char *output_filename, fpstate fpvar)
int fp_preflight(int argc, char *argv[], int unpack, fpstate *fpptr)
int fits_read_image_speed(fitsfile *infptr, float *whole_elapse, float *whole_cpu, float *row_elapse, float *row_cpu, int *status)
int fp_pack_hdu(fitsfile *infptr, fitsfile *outfptr, fpstate fpvar, int *islossless, int *status)
int fp_pack_fits_to_fits(fitsfile *infptr, fitsfile **outfits, fpstate fpvar, int *islossless)
int fp_pack_data_to_fits(const char *inputBuffer, size_t inputBufferSize, fitsfile **outfits, fpstate fpvar, int *islossless)
char tempfilename2[SZ_STR]
int marktime(int *status)
void fp_abort_output(fitsfile *infptr, fitsfile *outfptr, int stat)
char tempfilename3[SZ_STR]
int fp_info_hdu(fitsfile *infptr)
int fp_i2rescale(fitsfile *infptr, int naxis, long *naxes, double rescale, fitsfile *outfptr, int *status)
int fp_unpack_hdu(fitsfile *infptr, fitsfile *outfptr, fpstate fpvar, int *status)
int fp_i4rescale(fitsfile *infptr, int naxis, long *naxes, double rescale, fitsfile *outfptr, int *status)
int fp_test_table(fitsfile *infptr, fitsfile *outfptr, fitsfile *outfptr2, fpstate fpvar, int *status)
int fp_test(char *infits, char *outfits, char *outfits2, fpstate fpvar)
int fp_init(fpstate *fpptr)
int _fp_tmpnam(char *suffix, char *rootname, char *tmpnam)
int fp_i2stat(fitsfile *infptr, int naxis, long *naxes, imgstats *imagestats, int *status)
int fp_unpack_data_to_fits(const char *inputBuffer, size_t inputBufferSize, fitsfile **outfits, fpstate fpvar)
int fp_access(char *filename)
int fp_list(int argc, char *argv[], fpstate fpvar)
int fp_unpack(char *infits, char *outfits, fpstate fpvar)
int fp_i4stat(fitsfile *infptr, int naxis, long *naxes, imgstats *imagestats, int *status)
int fp_test_hdu(fitsfile *infptr, fitsfile *outfptr, fitsfile *outfptr2, fpstate fpvar, int *status)
int fp_pack(char *infits, char *outfits, fpstate fpvar, int *islossless)
char tempfilename[SZ_STR]
int fp_unpack_file_to_fits(char *infits, fitsfile **outfits, fpstate fpvar)
int fp_r4stat(fitsfile *infptr, int naxis, long *naxes, imgstats *imagestats, int *status)
#define fp_tmpnam(suffix, rootname, tmpnam)
int gettime(float *elapse, float *elapscpu, int *status)
long ntile[MAX_COMPRESS_DIM]