Debian Codegraph @

cextern/cfitsio/utilities/fpackutil.c

raw
1 /* FPACK utility routines
2 R. Seaman, NOAO & W. Pence, NASA/GSFC
3 */
4
5 #include <time.h>
6 #include <float.h>
7 #include <signal.h>
8 #include <ctype.h>
9
10 /* #include "bzlib.h" only for experimental purposes */
11
12 #if defined(unix) || defined(__unix__) || defined(__unix)
13 #include <sys/time.h>
14 #endif
15
16 #include <math.h>
17 #include "fitsio.h"
18 #include "fpack.h"
19
20 /* these filename buffer are used to delete temporary files */
21 /* in case the program is aborted */
22 char tempfilename[SZ_STR];
23 char tempfilename2[SZ_STR];
24 char tempfilename3[SZ_STR];
25
26 /* nearest integer function */
27 # define NINT(x) ((x >= 0.) ? (int) (x + 0.5) : (int) (x - 0.5))
28 # define NSHRT(x) ((x >= 0.) ? (short) (x + 0.5) : (short) (x - 0.5))
29
30 /* define variables for measuring elapsed time */
31 clock_t scpu, ecpu;
32 long startsec; /* start of elapsed time interval */
33 int startmilli; /* start of elapsed time interval */
34
35 /* CLOCKS_PER_SEC should be defined by most compilers */
36 #if defined(CLOCKS_PER_SEC)
37 #define CLOCKTICKS CLOCKS_PER_SEC
38 #else
39 /* on SUN OS machine, CLOCKS_PER_SEC is not defined, so set its value */
40 #define CLOCKTICKS 1000000
41 #endif
42
43 FILE *outreport;
44
45 /* dimension of central image area to be sampled for test statistics */
46 int XSAMPLE = 4100;
47 int YSAMPLE = 4100;
48
49 int fp_msg (char *msg)
50 {
51 printf ("%s", msg);
52 return(0);
53 }
54 /*--------------------------------------------------------------------------*/
55 int fp_noop (void)
56 {
57 fp_msg ("Input and output files are unchanged.\n");
58 return(0);
59 }
60 /*--------------------------------------------------------------------------*/
61 void fp_abort_output (fitsfile *infptr, fitsfile *outfptr, int stat)
62 {
63 int status = 0, hdunum;
64 char msg[SZ_STR];
65
66 if (infptr)
67 {
68 fits_file_name(infptr, tempfilename, &status);
69 fits_get_hdu_num(infptr, &hdunum);
70
71 fits_close_file (infptr, &status);
72
73 snprintf(msg, SZ_STR,"Error processing file: %s\n", tempfilename);
74 fp_msg (msg);
75 snprintf(msg, SZ_STR," in HDU number %d\n", hdunum);
76 fp_msg (msg);
77 }
78 else
79 {
80 snprintf(msg, SZ_STR,"Error: Unable to process input file\n");
81 fp_msg(msg);
82 }
83 fits_report_error (stderr, stat);
84
85 if (outfptr) {
86 fits_delete_file(outfptr, &status);
87 fp_msg ("Input file is unchanged.\n");
88 }
89
90 exit (stat);
91 }
92 /*--------------------------------------------------------------------------*/
93 int fp_version (void)
94 {
95 float version;
96 char cfitsioversion[40];
97
98 fp_msg (FPACK_VERSION);
99 fits_get_version(&version);
100 snprintf(cfitsioversion, 40," CFITSIO version %5.3f", version);
101 fp_msg(cfitsioversion);
102 fp_msg ("\n");
103 return(0);
104 }
105 /*--------------------------------------------------------------------------*/
106 int fp_access (char *filename)
107 {
108 /* test if a file exists */
109
110 FILE *diskfile;
111
112 diskfile = fopen(filename, "r");
113
114 if (diskfile) {
115 fclose(diskfile);
116 return(0);
117 } else {
118 return(-1);
119 }
120 }
121 /*--------------------------------------------------------------------------*/
122 int fp_tmpnam(char *suffix, char *rootname, char *tmpnam)
123 {
124 /* create temporary file name */
125
126 int maxtry = 30, ii;
127
128 if (strlen(suffix) + strlen(rootname) > SZ_STR-5) {
129 fp_msg ("Error: filename is too long to create temporary file\n"); exit (-1);
130 }
131
132 strcpy (tmpnam, rootname); /* start with rootname */
133 strcat(tmpnam, suffix); /* append the suffix */
134
135 maxtry = SZ_STR - strlen(tmpnam) - 1;
136
137 for (ii = 0; ii < maxtry; ii++) {
138 if (fp_access(tmpnam)) break; /* good, the file does not exist */
139 if (strlen(tmpnam) > SZ_STR-2)
140 {
141 fp_msg ("\nCould not create temporary file name:\n");
142 fp_msg (tmpnam);
143 fp_msg ("\n");
144 exit (-1);
145 }
146 strcat(tmpnam, "x"); /* append an x to the name, and try again */
147 }
148
149 if (ii == maxtry) {
150 fp_msg ("\nCould not create temporary file name:\n");
151 fp_msg (tmpnam);
152 fp_msg ("\n");
153 exit (-1);
154 }
155
156 return(0);
157 }
158 /*--------------------------------------------------------------------------*/
159 int fp_init (fpstate *fpptr)
160 {
161 int ii;
162
163 fpptr->comptype = RICE_1;
164 fpptr->quantize_level = DEF_QLEVEL;
165 fpptr->no_dither = 0;
166 fpptr->dither_method = 1;
167 fpptr->dither_offset = 0;
168 fpptr->int_to_float = 0;
169
170 /* thresholds when using the -i2f flag */
171 fpptr->n3ratio = 2.0; /* minimum ratio of image noise sigma / q */
172 fpptr->n3min = 6.; /* minimum noise sigma. */
173
174 fpptr->scale = DEF_HCOMP_SCALE;
175 fpptr->smooth = DEF_HCOMP_SMOOTH;
176 fpptr->rescale_noise = DEF_RESCALE_NOISE;
177 fpptr->ntile[0] = (long) -1; /* -1 means extent of axis */
178
179 for (ii=1; ii < MAX_COMPRESS_DIM; ii++)
180 fpptr->ntile[ii] = (long) 1;
181
182 fpptr->to_stdout = 0;
183 fpptr->listonly = 0;
184 fpptr->clobber = 0;
185 fpptr->delete_input = 0;
186 fpptr->do_not_prompt = 0;
187 fpptr->do_checksums = 1;
188 fpptr->do_gzip_file = 0;
189 fpptr->do_tables = 0; /* this is intended for testing purposes */
190 fpptr->do_images = 1; /* can be turned off with -tableonly switch */
191 fpptr->test_all = 0;
192 fpptr->verbose = 0;
193
194 fpptr->prefix[0] = 0;
195 fpptr->extname[0] = 0;
196 fpptr->delete_suffix = 0;
197 fpptr->outfile[0] = 0;
198
199 fpptr->firstfile = 1;
200
201 /* magic number for initialization check, boolean for preflight
202 */
203 fpptr->initialized = FP_INIT_MAGIC;
204 fpptr->preflight_checked = 0;
205 return(0);
206 }
207 /*--------------------------------------------------------------------------*/
208 int fp_list (int argc, char *argv[], fpstate fpvar)
209 {
210 fitsfile *infptr;
211 char infits[SZ_STR], msg[SZ_STR];
212 int hdunum, iarg, stat=0;
213 LONGLONG sizell;
214
215 if (fpvar.initialized != FP_INIT_MAGIC) {
216 fp_msg ("Error: internal initialization error\n"); exit (-1);
217 }
218
219 for (iarg=fpvar.firstfile; iarg < argc; iarg++) {
220 strncpy (infits, argv[iarg], SZ_STR-1);
221 infits[SZ_STR-1]=0;
222
223 if (strchr (infits, '[') || strchr (infits, ']')) {
224 fp_msg ("Error: section/extension notation not supported: ");
225 fp_msg (infits); fp_msg ("\n"); exit (-1);
226 }
227
228 if (fp_access (infits) != 0) {
229 fp_msg ("Error: can't find or read input file "); fp_msg (infits);
230 fp_msg ("\n"); fp_noop (); exit (-1);
231 }
232
233 fits_open_file (&infptr, infits, READONLY, &stat);
234 if (stat) { fits_report_error (stderr, stat); exit (stat); }
235
236 /* move to the end of file, to get the total size in bytes */
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);
240
241
242 if (stat) {
243 fp_abort_output(infptr, NULL, stat);
244 }
245
246 snprintf (msg, SZ_STR,"# %s (", infits); fp_msg (msg);
247
248 #if defined(_MSC_VER)
249 /* Microsoft Visual C++ 6.0 uses '%I64d' syntax for 8-byte integers */
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);
253 #else
254 snprintf(msg, SZ_STR,"%ld bytes)\n", sizell); fp_msg (msg);
255 #endif
256 fp_info_hdu (infptr);
257
258 fits_close_file (infptr, &stat);
259 if (stat) { fits_report_error (stderr, stat); exit (stat); }
260 }
261 return(0);
262 }
263 /*--------------------------------------------------------------------------*/
264 int fp_info_hdu (fitsfile *infptr)
265 {
266 long naxes[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
267 char msg[SZ_STR], val[SZ_CARD], com[SZ_CARD];
268 int naxis=0, hdutype, bitpix, hdupos, stat=0, ii;
269 unsigned long datasum, hdusum;
270
271 fits_movabs_hdu (infptr, 1, NULL, &stat);
272 if (stat) {
273 fp_abort_output(infptr, NULL, stat);
274 }
275
276 for (hdupos=1; ! stat; hdupos++) {
277 fits_get_hdu_type (infptr, &hdutype, &stat);
278 if (stat) {
279 fp_abort_output(infptr, NULL, stat);
280 }
281
282 /* fits_get_hdu_type calls unknown extensions "IMAGE_HDU"
283 * so consult XTENSION keyword itself
284 */
285 fits_read_keyword (infptr, "XTENSION", val, com, &stat);
286 if (stat == KEY_NO_EXIST) {
287 /* in primary HDU which by definition is an "image" */
288 stat=0; /* clear for later error handling */
289
290 } else if (stat) {
291 fp_abort_output(infptr, NULL, stat);
292
293 } else if (hdutype == IMAGE_HDU) {
294 /* that is, if XTENSION != "IMAGE" AND != "BINTABLE" */
295 if (strncmp (val+1, "IMAGE", 5) &&
296 strncmp (val+1, "BINTABLE", 5)) {
297
298 /* assign something other than any of these */
299 hdutype = IMAGE_HDU + ASCII_TBL + BINARY_TBL;
300 }
301 }
302
303 fits_get_chksum(infptr, &datasum, &hdusum, &stat);
304
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);
308
309 fits_get_img_param (infptr, 9, &bitpix, &naxis, naxes, &stat);
310
311 snprintf (msg, SZ_STR," BITPIX=%d", bitpix); fp_msg (msg);
312
313 if (naxis == 0) {
314 snprintf (msg, SZ_STR," [no_pixels]"); fp_msg (msg);
315 } else if (naxis == 1) {
316 snprintf (msg, SZ_STR," [%ld]", naxes[1]); fp_msg (msg);
317 } else {
318 snprintf (msg, SZ_STR," [%ld", naxes[0]); fp_msg (msg);
319 for (ii=1; ii < naxis; ii++) {
320 snprintf (msg, SZ_STR,"x%ld", naxes[ii]); fp_msg (msg);
321 }
322 fp_msg ("]");
323 }
324
325 if (fits_is_compressed_image (infptr, &stat)) {
326 fits_read_keyword (infptr, "ZCMPTYPE", val, com, &stat);
327
328 /* allow for quote in keyword value */
329 if (! strncmp (val+1, "RICE_1", 6))
330 fp_msg (" tiled_rice\n");
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))
336 fp_msg (" tiled_plio\n");
337 else if (! strncmp (val+1, "HCOMPRESS_1", 11))
338 fp_msg (" tiled_hcompress\n");
339 else
340 fp_msg (" unknown\n");
341
342 } else
343 fp_msg (" not_tiled\n");
344
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);
348
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);
352
353 } else {
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);
356 snprintf (msg, SZ_STR," %s\n", val); fp_msg (msg);
357 }
358
359 fits_movrel_hdu (infptr, 1, NULL, &stat);
360 }
361 return(0);
362 }
363
364 /*--------------------------------------------------------------------------*/
365 int fp_preflight (int argc, char *argv[], int unpack, fpstate *fpptr)
366 {
367 char infits[SZ_STR], outfits[SZ_STR];
368 int iarg, namelen, nfiles = 0;
369
370 if (fpptr->initialized != FP_INIT_MAGIC) {
371 fp_msg ("Error: internal initialization error\n"); exit (-1);
372 }
373
374 for (iarg=fpptr->firstfile; iarg < argc; iarg++) {
375
376 outfits[0] = '\0';
377
378 if (strlen(argv[iarg]) > SZ_STR - 4) { /* allow for .fz or .gz suffix */
379 fp_msg ("Error: input file name\n "); fp_msg (argv[iarg]);
380 fp_msg ("\n is too long\n"); fp_noop (); exit (-1);
381 }
382
383 strncpy (infits, argv[iarg], SZ_STR);
384 if (infits[0] == '-' && infits[1] != '\0') {
385 /* don't interpret this as intending to read input file from stdin */
386 fp_msg ("Error: invalid input file name\n "); fp_msg (argv[iarg]);
387 fp_msg ("\n"); fp_noop (); exit (-1);
388 }
389
390 if (strchr (infits, '[') || strchr (infits, ']')) {
391 fp_msg ("Error: section/extension notation not supported: ");
392 fp_msg (infits); fp_msg ("\n"); fp_noop (); exit (-1);
393 }
394
395 if (unpack) {
396 /* ********** This section applies to funpack ************ */
397
398 /* check that input file exists */
399 if (infits[0] != '-') { /* if not reading from stdin stream */
400 if (fp_access (infits) != 0) { /* if not, then check if */
401 strcat(infits, ".fz"); /* a .fz version exsits */
402 if (fp_access (infits) != 0) {
403 namelen = strlen(infits);
404 infits[namelen - 3] = '\0'; /* remove the .fz suffix */
405 fp_msg ("Error: can't find or read input file "); fp_msg (infits);
406 fp_msg ("\n"); fp_noop (); exit (-1);
407 }
408 } else { /* make sure a .fz version of the same file doesn't exist */
409 namelen = strlen(infits);
410 strcat(infits, ".fz");
411 if (fp_access (infits) == 0) {
412 infits[namelen] = '\0'; /* remove the .fz suffix */
413 fp_msg ("Error: ambiguous input file name. Which file should be unpacked?:\n ");
414 fp_msg (infits); fp_msg ("\n ");
415 fp_msg (infits); fp_msg (".fz\n");
416 fp_noop (); exit (-1);
417 } else {
418 infits[namelen] = '\0'; /* remove the .fz suffix */
419 }
420 }
421 }
422
423 /* if writing to stdout, then we are all done */
424 if (fpptr->to_stdout) {
425 continue;
426 }
427
428 if (fpptr->outfile[0]) { /* user specified output file name */
429 nfiles++;
430 if (nfiles > 1) {
431 fp_msg ("Error: cannot use same output file name for multiple files:\n ");
432 fp_msg (fpptr->outfile);
433 fp_msg ("\n"); fp_noop (); exit (-1);
434 }
435
436 /* check that output file doesn't exist */
437 if (fp_access (fpptr->outfile) == 0) {
438 fp_msg ("Error: output file already exists:\n ");
439 fp_msg (fpptr->outfile);
440 fp_msg ("\n "); fp_noop (); exit (-1);
441 }
442 continue;
443 }
444
445 /* construct output file name to test */
446 if (fpptr->prefix[0]) {
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);
450 }
451 strcpy(outfits,fpptr->prefix);
452 }
453
454 /* construct output file name */
455 if (infits[0] == '-') {
456 strcpy(outfits, "output.fits");
457 } else {
458 strcat(outfits, infits);
459 }
460
461 /* remove .gz or .bz2 suffix, if present (output is not gzipped) */
462 namelen = strlen(outfits);
463 if (namelen >= 3 && !strcmp(".gz", outfits + namelen - 3) ) {
464 outfits[namelen - 3] = '\0';
465 }
466 else if (namelen >= 4 && !strcmp(".bz2", outfits + namelen - 4)) {
467 outfits[namelen - 4] = '\0';
468 }
469
470 /* check for .fz suffix that is sometimes required */
471 /* and remove it if present */
472 if (infits[0] != '-') { /* if not reading from stdin stream */
473 namelen = strlen(outfits);
474 if (namelen>=3 && !strcmp(".fz", outfits + namelen - 3) ) { /* suffix is present */
475 outfits[namelen - 3] = '\0';
476 } else if (fpptr->delete_suffix) { /* required suffix is missing */
477 fp_msg ("Error: input compressed file "); fp_msg (infits);
478 fp_msg ("\n does not have the default .fz suffix.\n");
479 fp_noop (); exit (-1);
480 }
481 }
482
483 /* if infits != outfits, make sure outfits doesn't already exist */
484 if (strcmp(infits, outfits)) {
485 if (fp_access (outfits) == 0) {
486 fp_msg ("Error: output file already exists:\n "); fp_msg (outfits);
487 fp_msg ("\n "); fp_noop (); exit (-1);
488 }
489 }
490
491 /* if gzipping the output, make sure .gz file doesn't exist */
492 if (fpptr->do_gzip_file) {
493 if (strlen(outfits)+3 > SZ_STR-1)
494 {
495 fp_msg ("Error: output file name too long:\n "); fp_msg (outfits);
496 fp_msg ("\n "); fp_noop (); exit (-1);
497 }
498 strcat(outfits, ".gz");
499 if (fp_access (outfits) == 0) {
500 fp_msg ("Error: output file already exists:\n "); fp_msg (outfits);
501 fp_msg ("\n "); fp_noop (); exit (-1);
502 }
503 namelen = strlen(outfits);
504 outfits[namelen - 3] = '\0'; /* remove the .gz suffix again */
505 }
506 } else {
507 /* ********** This section applies to fpack ************ */
508
509 /* check that input file exists */
510 if (infits[0] != '-') { /* if not reading from stdin stream */
511 if (fp_access (infits) != 0) { /* if not, then check if */
512 if (strlen(infits)+3 > SZ_STR-1)
513 {
514 fp_msg ("Error: input file name too long:\n "); fp_msg (infits);
515 fp_msg ("\n "); fp_noop (); exit (-1);
516 }
517 strcat(infits, ".gz"); /* a gzipped version exsits */
518 if (fp_access (infits) != 0) {
519 namelen = strlen(infits);
520 infits[namelen - 3] = '\0'; /* remove the .gz suffix */
521 fp_msg ("Error: can't find or read input file "); fp_msg (infits);
522 fp_msg ("\n"); fp_noop (); exit (-1);
523 }
524 }
525 }
526
527 /* make sure the file to pack does not already have a .fz suffix */
528 namelen = strlen(infits);
529 if (namelen>=3 && !strcmp(".fz", infits + namelen - 3) ) {
530 fp_msg ("Error: fpack input file already has '.fz' suffix\n" ); fp_msg (infits);
531 fp_msg ("\n"); fp_noop (); exit (-1);
532 }
533
534 /* if writing to stdout, or just testing the files, then we are all done */
535 if (fpptr->to_stdout || fpptr->test_all) {
536 continue;
537 }
538
539 if (fpptr->outfile[0]) { /* user specified output file name */
540 nfiles++;
541 if (nfiles > 1) {
542 fp_msg("Error: cannot use same output file name for multiple files:\n ");
543 fp_msg(fpptr->outfile);
544 fp_msg ("\n"); fp_noop (); exit (-1);
545 }
546
547 /* check that output file doesn't exist */
548 if (fp_access (fpptr->outfile) == 0) {
549 fp_msg ("Error: output file already exists:\n ");
550 fp_msg (fpptr->outfile);
551 fp_msg ("\n "); fp_noop (); exit (-1);
552 }
553 continue;
554 }
555
556 /* construct output file name */
557 if (infits[0] == '-') {
558 strcpy(outfits, "input.fits");
559 } else {
560 strcpy(outfits, infits);
561 }
562
563 /* remove .gz suffix, if present (output is not gzipped) */
564 /* do the same if compression suffix is bz2 */
565 namelen = strlen(outfits);
566 if (namelen >=3 && !strcmp(".gz", outfits + namelen - 3) ) {
567 outfits[namelen - 3] = '\0';
568 }
569 else if (namelen >= 4 && !strcmp(".bz2", outfits + namelen - 4)) {
570 outfits[namelen - 4] = '\0';
571 }
572
573
574 /* remove .imh suffix (IRAF format image), and replace with .fits */
575 namelen = strlen(outfits);
576 if (namelen >=4 && !strcmp(".imh", outfits + namelen - 4) ) {
577 outfits[namelen - 4] = '\0';
578 if (strlen(outfits) == SZ_STR-5)
579 strcat(outfits, ".fit");
580 else
581 strcat(outfits, ".fits");
582 }
583
584 /* If not clobbering the input file, add .fz suffix to output name */
585 if (! fpptr->clobber)
586 {
587 if (strlen(outfits) > SZ_STR-4)
588 {
589 fp_msg ("Error: output file name too long:\n "); fp_msg (outfits);
590 fp_msg ("\n "); fp_noop (); exit (-1);
591 }
592 else
593 strcat(outfits, ".fz");
594 }
595
596 /* if infits != outfits, make sure outfits doesn't already exist */
597 if (strcmp(infits, outfits)) {
598 if (fp_access (outfits) == 0) {
599 fp_msg ("Error: output file already exists:\n "); fp_msg (outfits);
600 fp_msg ("\n "); fp_noop (); exit (-1);
601 }
602 }
603 } /* end of fpack section */
604 }
605
606 fpptr->preflight_checked++;
607 return(0);
608 }
609
610 /*--------------------------------------------------------------------------*/
611 /* must run fp_preflight() before fp_loop()
612 */
613 int fp_loop (int argc, char *argv[], int unpack, fpstate fpvar)
614 {
615 char infits[SZ_STR], outfits[SZ_STR];
616 char temp[SZ_STR], answer[30];
617 char valchar[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.#()+,-_@[]/^{}";
618 int ichar=0, outlen=0, iarg, islossless, namelen, iraf_infile = 0, status = 0, ifail;
619
620 if (fpvar.initialized != FP_INIT_MAGIC) {
621 fp_msg ("Error: internal initialization error\n"); exit (-1);
622 } else if (! fpvar.preflight_checked) {
623 fp_msg ("Error: internal preflight error\n"); exit (-1);
624 }
625
626 if (fpvar.test_all && fpvar.outfile[0]) {
627 outreport = fopen(fpvar.outfile, "w");
628 fprintf(outreport," Filename Extension BITPIX NAXIS1 NAXIS2 Size N_nulls Minval Maxval Mean Sigm Noise1 Noise2 Noise3 Noise5 T_whole T_rowbyrow ");
629 fprintf(outreport,"[Comp_ratio, Pack_cpu, Unpack_cpu, Lossless readtimes] (repeated for Rice, Hcompress, and GZIP)\n");
630 }
631
632
633 tempfilename[0] = '\0';
634 tempfilename2[0] = '\0';
635 tempfilename3[0] = '\0';
636
637 /* set up signal handler to delete temporary file on abort */
638 #ifdef SIGINT
639 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
640 (void) signal(SIGINT, abort_fpack);
641 }
642 #endif
643
644 #ifdef SIGTERM
645 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
646 (void) signal(SIGTERM, abort_fpack);
647 }
648 #endif
649
650 #ifdef SIGHUP
651 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
652 (void) signal(SIGHUP, abort_fpack);
653 }
654 #endif
655
656 for (iarg=fpvar.firstfile; iarg < argc; iarg++) {
657
658 temp[0] = '\0';
659 outfits[0] = '\0';
660 islossless = 1;
661
662 strncpy (infits, argv[iarg], SZ_STR - 1);
663 infits[SZ_STR-1]=0;
664
665 if (unpack) {
666 /* ********** This section applies to funpack ************ */
667
668 /* find input file */
669 if (infits[0] != '-') { /* if not reading from stdin stream */
670 if (fp_access (infits) != 0) { /* if not, then */
671 strcat(infits, ".fz"); /* a .fz version must exsit */
672 /* fp_preflight already checked for enough size to add '.fz' */
673 }
674 }
675
676 if (fpvar.to_stdout) {
677 strcpy(outfits, "-");
678
679 } else if (fpvar.outfile[0]) { /* user specified output file name */
680 strcpy(outfits, fpvar.outfile);
681
682 } else {
683 /* construct output file name */
684 if (fpvar.prefix[0]) {
685 /* fp_preflight already checked this */
686 strcpy(outfits,fpvar.prefix);
687 }
688
689 /* construct output file name */
690 if (infits[0] == '-') {
691 strcpy(outfits, "output.fits");
692 } else {
693 strcat(outfits, infits);
694 }
695
696 /* remove .gz suffix, if present (output is not gzipped) */
697 namelen = strlen(outfits);
698 if (namelen >= 3 && !strcmp(".gz", outfits + namelen - 3) ) {
699 outfits[namelen - 3] = '\0';
700 }
701 else if (namelen >= 4 && !strcmp(".bz2", outfits + namelen - 4)) {
702 outfits[namelen - 4] = '\0';
703 }
704
705 /* check for .fz suffix that is sometimes required */
706 /* and remove it if present */
707 namelen = strlen(outfits);
708 if (namelen >= 3 && !strcmp(".fz", outfits + namelen - 3) ) { /* suffix is present */
709 outfits[namelen - 3] = '\0';
710 }
711 }
712
713 } else {
714 /* ********** This section applies to fpack ************ */
715
716 if (fpvar.to_stdout) {
717 strcpy(outfits, "-");
718 } else if (! fpvar.test_all) {
719
720 if (fpvar.outfile[0]) { /* user specified output file name */
721 strcpy(outfits, fpvar.outfile);
722 }
723 else {
724 /* construct output file name */
725 if (infits[0] == '-') {
726 strcpy(outfits, "input.fits");
727 } else {
728 strcpy(outfits, infits);
729 }
730 /* Remove .gz suffix, if present (output is not gzipped).
731 Do the same for .bz2 */
732 namelen = strlen(outfits);
733 if (namelen >= 3 && !strcmp(".gz", outfits + namelen - 3) ) {
734 outfits[namelen - 3] = '\0';
735 }
736 else if (namelen >= 4 && !strcmp(".bz2", outfits + namelen - 4)) {
737 outfits[namelen - 4] = '\0';
738 }
739
740 /* remove .imh suffix (IRAF format image), and replace with .fits */
741 namelen = strlen(outfits);
742 if (namelen >= 4 && !strcmp(".imh", outfits + namelen - 4) ) {
743 outfits[namelen - 4] = '\0';
744 if (strlen(outfits) == SZ_STR-5)
745 strcat(outfits, ".fit");
746 else
747 strcat(outfits, ".fits");
748 iraf_infile = 1; /* this is an IRAF format input file */
749 /* change the output name to "NAME.fits.fz" */
750 }
751
752 /* If not clobbering the input file, add .fz suffix to output name */
753 if (! fpvar.clobber)
754 strcat(outfits, ".fz");
755 }
756 }
757 }
758
759 strncpy(temp, outfits, SZ_STR-1);
760 temp[SZ_STR-1]=0;
761
762 if (infits[0] != '-') { /* if not reading from stdin stream */
763 if (!strcmp(infits, outfits) ) { /* are input and output names the same? */
764
765 /* clobber the input file with the output file with the same name */
766 if (! fpvar.clobber) {
767 fp_msg ("\nError: must use -F flag to clobber input file.\n");
768 exit (-1);
769 }
770
771 /* create temporary file name in the output directory (same as input directory)*/
772 fp_tmpnam("Tmp1", infits, outfits);
773
774 strcpy(tempfilename, outfits); /* store temp file name, in case of abort */
775 }
776 }
777
778
779 /* *************** now do the real work ********************* */
780
781 if (fpvar.verbose && ! fpvar.to_stdout)
782 printf("%s ", infits);
783
784 if (fpvar.test_all) { /* compare all the algorithms */
785
786 /* create 2 temporary file names, in the CWD */
787 fp_tmpnam("Tmpfile1", "", tempfilename);
788 fp_tmpnam("Tmpfile2", "", tempfilename2);
789
790 fp_test (infits, tempfilename, tempfilename2, fpvar);
791
792 remove(tempfilename);
793 tempfilename[0] = '\0'; /* clear the temp file name */
794 remove(tempfilename2);
795 tempfilename2[0] = '\0';
796 continue;
797
798 } else if (unpack) {
799 if (fpvar.to_stdout) {
800 /* unpack the input file to the stdout stream */
801 fp_unpack (infits, outfits, fpvar);
802 } else {
803 /* unpack to temporary file, so other tasks can't open it until it is renamed */
804
805 /* create temporary file name, in the output directory */
806 fp_tmpnam("Tmp2", outfits, tempfilename2);
807
808 /* unpack the input file to the temporary file */
809 fp_unpack (infits, tempfilename2, fpvar);
810
811 /* rename the temporary file to it's real name */
812 ifail = rename(tempfilename2, outfits);
813 if (ifail) {
814 fp_msg("Failed to rename temporary file name:\n ");
815 fp_msg(tempfilename2);
816 fp_msg(" -> ");
817 fp_msg(outfits);
818 fp_msg("\n");
819 exit (-1);
820 } else {
821 tempfilename2[0] = '\0'; /* clear temporary file name */
822 }
823 }
824 } else {
825 fp_pack (infits, outfits, fpvar, &islossless);
826 }
827
828 if (fpvar.to_stdout) {
829 continue;
830 }
831
832 /* ********** clobber and/or delete files, if needed ************** */
833
834 if (!strcmp(infits, temp) && fpvar.clobber ) {
835
836 if (!islossless && ! fpvar.do_not_prompt) {
837 fp_msg ("\nFile ");
838 fp_msg (infits);
839 fp_msg ("\nwas compressed with a LOSSY method. Overwrite the\n");
840 fp_msg ("original file with the compressed version? (Y/N) ");
841 fgets(answer, 29, stdin);
842 if (answer[0] != 'Y' && answer[0] != 'y') {
843 fp_msg ("\noriginal file NOT overwritten!\n");
844 remove(outfits);
845 continue;
846 }
847 }
848
849 if (iraf_infile) { /* special case of deleting an IRAF format header and pixel file */
850 if (fits_delete_iraf_file(infits, &status)) {
851 fp_msg("\nError deleting IRAF .imh and .pix files.\n");
852 fp_msg(infits); fp_msg ("\n"); exit (-1);
853 }
854 }
855
856 #if defined(unix) || defined(__unix__) || defined(__unix)
857 /* rename clobbers input on Unix platforms */
858 if (rename (outfits, temp) != 0) {
859 fp_msg ("\nError renaming tmp file to ");
860 fp_msg (temp); fp_msg ("\n"); exit (-1);
861 }
862 #else
863 /* rename DOES NOT clobber existing files on Windows platforms */
864 /* so explicitly remove any existing file before renaming the file */
865 remove(temp);
866 if (rename (outfits, temp) != 0) {
867 fp_msg ("\nError renaming tmp file to ");
868 fp_msg (temp); fp_msg ("\n"); exit (-1);
869 }
870 #endif
871
872 tempfilename[0] = '\0'; /* clear temporary file name */
873 strcpy(outfits, temp);
874
875 } else if (fpvar.clobber || fpvar.delete_input) { /* delete the input file */
876 if (!islossless && !fpvar.do_not_prompt) { /* user did not turn off delete prompt */
877 fp_msg ("\nFile ");
878 fp_msg (infits);
879 fp_msg ("\nwas compressed with a LOSSY method. \n");
880 fp_msg ("Delete the original file? (Y/N) ");
881 fgets(answer, 29, stdin);
882 if (answer[0] != 'Y' && answer[0] != 'y') { /* user abort */
883 fp_msg ("\noriginal file NOT deleted!\n");
884 } else {
885 if (iraf_infile) { /* special case of deleting an IRAF format header and pixel file */
886 if (fits_delete_iraf_file(infits, &status)) {
887 fp_msg("\nError deleting IRAF .imh and .pix files.\n");
888 fp_msg(infits); fp_msg ("\n"); exit (-1);
889 }
890 } else if (remove(infits) != 0) { /* normal case of deleting input FITS file */
891 fp_msg ("\nError deleting input file ");
892 fp_msg (infits); fp_msg ("\n"); exit (-1);
893 }
894 }
895 } else { /* user said don't prompt, so just delete the input file */
896 if (iraf_infile) { /* special case of deleting an IRAF format header and pixel file */
897 if (fits_delete_iraf_file(infits, &status)) {
898 fp_msg("\nError deleting IRAF .imh and .pix files.\n");
899 fp_msg(infits); fp_msg ("\n"); exit (-1);
900 }
901 } else if (remove(infits) != 0) { /* normal case of deleting input FITS file */
902 fp_msg ("\nError deleting input file ");
903 fp_msg (infits); fp_msg ("\n"); exit (-1);
904 }
905 }
906 }
907 iraf_infile = 0;
908
909 if (fpvar.do_gzip_file) { /* gzip the output file */
910 strcpy(temp, "gzip -1 ");
911 outlen = strlen(outfits);
912 if (outlen + 8 > SZ_STR-1)
913 {
914 fp_msg("\nError: Output file name is too long.\n");
915 exit(-1);
916 }
917 for (ichar=0; ichar < outlen; ++ichar)
918 {
919 if (!strchr(valchar, outfits[ichar]))
920 {
921 fp_msg("\n Error: Invalid characters in output file name.\n");
922 exit(-1);
923 }
924 }
925 strcat(temp,outfits);
926 system(temp);
927 strcat(outfits, ".gz"); /* only possibible with funpack */
928 }
929
930 if (fpvar.verbose && ! fpvar.to_stdout)
931 printf("-> %s\n", outfits);
932
933 }
934
935 if (fpvar.test_all && fpvar.outfile[0])
936 fclose(outreport);
937 return(0);
938 }
939
940 /*--------------------------------------------------------------------------*/
941 /* fp_pack assumes the output file does not exist (checked by preflight)
942 */
943 int fp_pack (char *infits, char *outfits, fpstate fpvar, int *islossless)
944 {
945 fitsfile *infptr, *outfptr;
946 int stat=0;
947
948 fits_open_file (&infptr, infits, READONLY, &stat);
949 if (stat) { fits_report_error (stderr, stat); exit (stat); }
950
951 fits_create_file (&outfptr, outfits, &stat);
952 if (stat) {
953 fp_abort_output(infptr, NULL, stat);
954 }
955
956
957 if (stat) {
958 fp_abort_output(infptr, outfptr, stat);
959 }
960
961 while (! stat) {
962
963 /* LOOP OVER EACH HDU */
964
965 fits_set_lossy_int (outfptr, fpvar.int_to_float, &stat);
966 fits_set_compression_type (outfptr, fpvar.comptype, &stat);
967 fits_set_tile_dim (outfptr, 6, fpvar.ntile, &stat);
968
969 if (fpvar.no_dither)
970 fits_set_quantize_method(outfptr, -1, &stat);
971 else
972 fits_set_quantize_method(outfptr, fpvar.dither_method, &stat);
973
974 fits_set_quantize_level (outfptr, fpvar.quantize_level, &stat);
975 fits_set_dither_offset(outfptr, fpvar.dither_offset, &stat);
976 fits_set_hcomp_scale (outfptr, fpvar.scale, &stat);
977 fits_set_hcomp_smooth (outfptr, fpvar.smooth, &stat);
978
979 fp_pack_hdu (infptr, outfptr, fpvar, islossless, &stat);
980
981 if (fpvar.do_checksums) {
982 fits_write_chksum (outfptr, &stat);
983 }
984
985 fits_movrel_hdu (infptr, 1, NULL, &stat);
986 }
987
988 if (stat == END_OF_FILE) stat = 0;
989
990 /* set checksum for case of newly created primary HDU */
991
992 if (fpvar.do_checksums) {
993 fits_movabs_hdu (outfptr, 1, NULL, &stat);
994 fits_write_chksum (outfptr, &stat);
995 }
996
997 if (stat) {
998 fp_abort_output(infptr, outfptr, stat);
999 }
1000
1001 fits_close_file (outfptr, &stat);
1002 fits_close_file (infptr, &stat);
1003
1004 return(0);
1005 }
1006
1007 /*--------------------------------------------------------------------------*/
1008 /* fp_unpack assumes the output file does not exist
1009 */
1010 int fp_unpack (char *infits, char *outfits, fpstate fpvar)
1011 {
1012 fitsfile *infptr, *outfptr;
1013 int stat=0, hdutype, extnum, single = 0;
1014 char *loc, *hduloc, hduname[SZ_STR];
1015
1016 fits_open_file (&infptr, infits, READONLY, &stat);
1017 fits_create_file (&outfptr, outfits, &stat);
1018
1019 if (stat) {
1020 fp_abort_output(infptr, outfptr, stat);
1021 }
1022
1023 if (fpvar.extname[0]) { /* unpack a list of HDUs? */
1024
1025 /* move to the first HDU in the list */
1026 hduloc = fpvar.extname;
1027 loc = strchr(hduloc, ','); /* look for 'comma' delimiter between names */
1028
1029 if (loc)
1030 *loc = '\0'; /* terminate the first name in the string */
1031
1032 strcpy(hduname, hduloc); /* copy the first name into temporary string */
1033
1034 if (loc)
1035 hduloc = loc + 1; /* advance to the beginning of the next name, if any */
1036 else {
1037 hduloc += strlen(hduname); /* end of the list */
1038 single = 1; /* only 1 HDU is being unpacked */
1039 }
1040
1041 if (isdigit( (int) hduname[0]) ) {
1042 extnum = strtol(hduname, &loc, 10); /* read the string as an integer */
1043
1044 /* check for junk following the integer */
1045 if (*loc == '\0' ) /* no junk, so move to this HDU number (+1) */
1046 {
1047 fits_movabs_hdu(infptr, extnum + 1, &hdutype, &stat); /* move to HDU number */
1048 if (hdutype != IMAGE_HDU)
1049 stat = NOT_IMAGE;
1050
1051 } else { /* the string is not an integer, so must be the column name */
1052 hdutype = IMAGE_HDU;
1053 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1054 }
1055 }
1056 else
1057 {
1058 /* move to the named image extension */
1059 hdutype = IMAGE_HDU;
1060 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1061 }
1062 }
1063
1064 if (stat) {
1065 fp_msg ("Unable to find and move to extension '");
1066 fp_msg(hduname);
1067 fp_msg("'\n");
1068 fp_abort_output(infptr, outfptr, stat);
1069 }
1070
1071 while (! stat) {
1072
1073 if (single)
1074 stat = -1; /* special status flag to force output primary array */
1075
1076 fp_unpack_hdu (infptr, outfptr, fpvar, &stat);
1077
1078 if (fpvar.do_checksums) {
1079 fits_write_chksum (outfptr, &stat);
1080 }
1081
1082 /* move to the next HDU */
1083 if (fpvar.extname[0]) { /* unpack a list of HDUs? */
1084
1085 if (!(*hduloc)) {
1086 stat = END_OF_FILE; /* we reached the end of the list */
1087 } else {
1088 /* parse the next HDU name and move to it */
1089 loc = strchr(hduloc, ',');
1090
1091 if (loc) /* look for 'comma' delimiter between names */
1092 *loc = '\0'; /* terminate the first name in the string */
1093
1094 strcpy(hduname, hduloc); /* copy the next name into temporary string */
1095
1096 if (loc)
1097 hduloc = loc + 1; /* advance to the beginning of the next name, if any */
1098 else
1099 *hduloc = '\0'; /* end of the list */
1100
1101 if (isdigit( (int) hduname[0]) ) {
1102 extnum = strtol(hduname, &loc, 10); /* read the string as an integer */
1103
1104 /* check for junk following the integer */
1105 if (*loc == '\0' ) /* no junk, so move to this HDU number (+1) */
1106 {
1107 fits_movabs_hdu(infptr, extnum + 1, &hdutype, &stat); /* move to HDU number */
1108 if (hdutype != IMAGE_HDU)
1109 stat = NOT_IMAGE;
1110
1111 } else { /* the string is not an integer, so must be the column name */
1112 hdutype = IMAGE_HDU;
1113 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1114 }
1115
1116 } else {
1117 /* move to the named image extension */
1118 hdutype = IMAGE_HDU;
1119 fits_movnam_hdu(infptr, hdutype, hduname, 0, &stat);
1120 }
1121
1122 if (stat) {
1123 fp_msg ("Unable to find and move to extension '");
1124 fp_msg(hduname);
1125 fp_msg("'\n");
1126 }
1127 }
1128 } else {
1129 /* increment to the next HDU */
1130 fits_movrel_hdu (infptr, 1, NULL, &stat);
1131 }
1132 }
1133
1134 if (stat == END_OF_FILE) stat = 0;
1135
1136 /* set checksum for case of newly created primary HDU
1137 */
1138 if (fpvar.do_checksums) {
1139 fits_movabs_hdu (outfptr, 1, NULL, &stat);
1140 fits_write_chksum (outfptr, &stat);
1141 }
1142
1143
1144 if (stat) {
1145 fp_abort_output(infptr, outfptr, stat);
1146 }
1147
1148 fits_close_file (outfptr, &stat);
1149 fits_close_file (infptr, &stat);
1150
1151 return(0);
1152 }
1153
1154 /*--------------------------------------------------------------------------*/
1155 /* fp_test assumes the output files do not exist
1156 */
1157 int fp_test (char *infits, char *outfits, char *outfits2, fpstate fpvar)
1158 {
1159 fitsfile *inputfptr, *infptr, *outfptr, *outfptr2, *tempfile;
1160
1161 long naxes[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
1162 int stat=0, totpix=0, naxis=0, ii, hdutype, bitpix = 0, extnum = 0, len;
1163 int tstatus = 0, hdunum, rescale_flag, bpix, ncols;
1164 char dtype[8], dimen[100];
1165 double bscale, rescale, noisemin;
1166 long headstart, datastart, dataend;
1167 float origdata = 0., whole_cpu, whole_elapse, row_elapse, row_cpu, xbits;
1168
1169 LONGLONG nrows;
1170 /* structure to hold image statistics (defined in fpack.h) */
1171 imgstats imagestats;
1172
1173 fits_open_file (&inputfptr, infits, READONLY, &stat);
1174 fits_create_file (&outfptr, outfits, &stat);
1175 fits_create_file (&outfptr2, outfits2, &stat);
1176
1177 if (stat) { fits_report_error (stderr, stat); exit (stat); }
1178
1179 while (! stat) {
1180
1181 /* LOOP OVER EACH HDU */
1182 rescale_flag = 0;
1183 fits_get_hdu_type (inputfptr, &hdutype, &stat);
1184
1185 if (hdutype == IMAGE_HDU) {
1186 fits_get_img_param (inputfptr, 9, &bitpix, &naxis, naxes, &stat);
1187 for (totpix=1, ii=0; ii < 9; ii++) totpix *= naxes[ii];
1188 }
1189
1190 if (!fits_is_compressed_image (inputfptr, &stat) && hdutype == IMAGE_HDU &&
1191 naxis != 0 && totpix != 0 && fpvar.do_images) {
1192
1193 /* rescale a scaled integer image to reduce noise? */
1194 if (fpvar.rescale_noise != 0. && bitpix > 0 && bitpix < LONGLONG_IMG) {
1195
1196 tstatus = 0;
1197 fits_read_key(inputfptr, TDOUBLE, "BSCALE", &bscale, 0, &tstatus);
1198
1199 if (tstatus == 0 && bscale != 1.0) { /* image must be scaled */
1200
1201 if (bitpix == LONG_IMG)
1202 fp_i4stat(inputfptr, naxis, naxes, &imagestats, &stat);
1203 else
1204 fp_i2stat(inputfptr, naxis, naxes, &imagestats, &stat);
1205
1206 /* use the minimum of the MAD 2nd, 3rd, and 5th order noise estimates */
1207 noisemin = imagestats.noise3;
1208 if (imagestats.noise2 != 0. && imagestats.noise2 < noisemin) noisemin = imagestats.noise2;
1209 if (imagestats.noise5 != 0. && imagestats.noise5 < noisemin) noisemin = imagestats.noise5;
1210
1211 rescale = noisemin / fpvar.rescale_noise;
1212 if (rescale > 1.0) {
1213
1214 /* all the criteria are met, so create a temporary file that */
1215 /* contains a rescaled version of the image, in CWD */
1216
1217 /* create temporary file name */
1218 fp_tmpnam("Tmpfile3", "", tempfilename3);
1219
1220 fits_create_file(&tempfile, tempfilename3, &stat);
1221
1222 fits_get_hdu_num(inputfptr, &hdunum);
1223 if (hdunum != 1) {
1224
1225 /* the input hdu is an image extension, so create dummy primary */
1226 fits_create_img(tempfile, 8, 0, naxes, &stat);
1227 }
1228
1229 fits_copy_header(inputfptr, tempfile, &stat); /* copy the header */
1230
1231 /* rescale the data, so that it will compress more efficiently */
1232 if (bitpix == LONG_IMG)
1233 fp_i4rescale(inputfptr, naxis, naxes, rescale, tempfile, &stat);
1234 else
1235 fp_i2rescale(inputfptr, naxis, naxes, rescale, tempfile, &stat);
1236
1237 /* scale the BSCALE keyword by the inverse factor */
1238
1239 bscale = bscale * rescale;
1240 fits_update_key(tempfile, TDOUBLE, "BSCALE", &bscale, 0, &stat);
1241
1242 /* rescan the header, to reset the actual scaling parameters */
1243 fits_set_hdustruc(tempfile, &stat);
1244
1245 infptr = tempfile;
1246 rescale_flag = 1;
1247 }
1248 }
1249 }
1250
1251 if (!rescale_flag) /* just compress the input file, without rescaling */
1252 infptr = inputfptr;
1253
1254 /* compute basic statistics about the input image */
1255 if (bitpix == BYTE_IMG) {
1256 bpix = 8;
1257 strcpy(dtype, "8 ");
1258 fp_i2stat(infptr, naxis, naxes, &imagestats, &stat);
1259 } else if (bitpix == SHORT_IMG) {
1260 bpix = 16;
1261 strcpy(dtype, "16 ");
1262 fp_i2stat(infptr, naxis, naxes, &imagestats, &stat);
1263 } else if (bitpix == LONG_IMG) {
1264 bpix = 32;
1265 strcpy(dtype, "32 ");
1266 fp_i4stat(infptr, naxis, naxes, &imagestats, &stat);
1267 } else if (bitpix == LONGLONG_IMG) {
1268 bpix = 64;
1269 strcpy(dtype, "64 ");
1270 } else if (bitpix == FLOAT_IMG) {
1271 bpix = 32;
1272 strcpy(dtype, "-32");
1273 fp_r4stat(infptr, naxis, naxes, &imagestats, &stat);
1274 } else if (bitpix == DOUBLE_IMG) {
1275 bpix = 64;
1276 strcpy(dtype, "-64");
1277 fp_r4stat(infptr, naxis, naxes, &imagestats, &stat);
1278 }
1279
1280 /* use the minimum of the MAD 2nd, 3rd, and 5th order noise estimates */
1281 noisemin = imagestats.noise3;
1282 if (imagestats.noise2 != 0. && imagestats.noise2 < noisemin) noisemin = imagestats.noise2;
1283 if (imagestats.noise5 != 0. && imagestats.noise5 < noisemin) noisemin = imagestats.noise5;
1284
1285 xbits = (float) (log10(noisemin)/.301 + 1.792);
1286
1287 printf("\n File: %s\n", infits);
1288 printf(" Ext BITPIX Dimens. Nulls Min Max Mean Sigma Noise2 Noise3 Noise5 Nbits MaxR\n");
1289
1290 printf(" %3d %s", extnum, dtype);
1291 snprintf(dimen,100," (%ld", naxes[0]);
1292 len =strlen(dimen);
1293 for (ii = 1; ii < naxis; ii++) {
1294 if (len < 99)
1295 snprintf(dimen+len,100-len,",%ld", naxes[ii]);
1296 len =strlen(dimen);
1297 }
1298 if (strlen(dimen)<99)
1299 strcat(dimen, ")");
1300 printf("%-12s",dimen);
1301
1302 fits_get_hduaddr(inputfptr, &headstart, &datastart, &dataend, &stat);
1303 origdata = (float) ((dataend - datastart)/1000000.);
1304
1305 /* get elapsed and cpu times need to read the uncompressed image */
1306 fits_read_image_speed (infptr, &whole_elapse, &whole_cpu,
1307 &row_elapse, &row_cpu, &stat);
1308
1309 printf(" %5d %6.0f %6.0f %8.1f %#8.2g %#7.3g %#7.3g %#7.3g %#5.1f %#6.2f\n",
1310 imagestats.n_nulls, imagestats.minval, imagestats.maxval,
1311 imagestats.mean, imagestats.sigma,
1312 imagestats.noise2, imagestats.noise3, imagestats.noise5, xbits, bpix/xbits);
1313
1314 printf("\n Type Ratio Size (MB) Pk (Sec) UnPk Exact ElpN CPUN Elp1 CPU1\n");
1315
1316 printf(" Native %5.3f %5.3f %5.3f %5.3f\n",
1317 whole_elapse, whole_cpu, row_elapse, row_cpu);
1318
1319 if (fpvar.outfile[0]) {
1320 fprintf(outreport,
1321 " %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",
1322 infits, extnum, bitpix, naxes[0], naxes[1], origdata, imagestats.n_nulls, imagestats.minval,
1323 imagestats.maxval, imagestats.mean, imagestats.sigma,
1324 imagestats.noise1, imagestats.noise2, imagestats.noise3, imagestats.noise5, whole_elapse, whole_cpu, row_elapse, row_cpu);
1325 }
1326
1327 fits_set_lossy_int (outfptr, fpvar.int_to_float, &stat);
1328 if ( (bitpix > 0) && (fpvar.int_to_float != 0) ) {
1329
1330 if ( (noisemin < (fpvar.n3ratio * fpvar.quantize_level) ) ||
1331 (noisemin < fpvar.n3min)) {
1332
1333 /* image contains too little noise to quantize effectively */
1334 fits_set_lossy_int (outfptr, 0, &stat);
1335 fits_get_hdu_num(infptr, &hdunum);
1336
1337 printf(" HDU %d does not meet noise criteria to be quantized, so losslessly compressed.\n", hdunum);
1338 }
1339 }
1340
1341 /* test compression ratio and speed for each algorithm */
1342
1343 if (fpvar.quantize_level != 0) {
1344
1345 fits_set_compression_type (outfptr, RICE_1, &stat);
1346 fits_set_tile_dim (outfptr, 6, fpvar.ntile, &stat);
1347 if (fpvar.no_dither)
1348 fits_set_quantize_method(outfptr, -1, &stat);
1349 else
1350 fits_set_quantize_method(outfptr, fpvar.dither_method, &stat);
1351
1352 fits_set_quantize_level (outfptr, fpvar.quantize_level, &stat);
1353 fits_set_dither_offset(outfptr, fpvar.dither_offset, &stat);
1354 fits_set_hcomp_scale (outfptr, fpvar.scale, &stat);
1355 fits_set_hcomp_smooth (outfptr, fpvar.smooth, &stat);
1356
1357 fp_test_hdu(infptr, outfptr, outfptr2, fpvar, &stat);
1358 }
1359
1360 if (fpvar.quantize_level != 0) {
1361 \
1362 fits_set_compression_type (outfptr, HCOMPRESS_1, &stat);
1363 fits_set_tile_dim (outfptr, 6, fpvar.ntile, &stat);
1364
1365 if (fpvar.no_dither)
1366 fits_set_quantize_method(outfptr, -1, &stat);
1367 else
1368 fits_set_quantize_method(outfptr, fpvar.dither_method, &stat);
1369
1370 fits_set_quantize_level (outfptr, fpvar.quantize_level, &stat);
1371 fits_set_dither_offset(outfptr, fpvar.dither_offset, &stat);
1372 fits_set_hcomp_scale (outfptr, fpvar.scale, &stat);
1373 fits_set_hcomp_smooth (outfptr, fpvar.smooth, &stat);
1374
1375 fp_test_hdu(infptr, outfptr, outfptr2, fpvar, &stat);
1376 }
1377
1378 if (fpvar.comptype == GZIP_2) {
1379 fits_set_compression_type (outfptr, GZIP_2, &stat);
1380 } else {
1381 fits_set_compression_type (outfptr, GZIP_1, &stat);
1382 }
1383
1384 fits_set_tile_dim (outfptr, 6, fpvar.ntile, &stat);
1385
1386 if (fpvar.no_dither)
1387 fits_set_quantize_method(outfptr, -1, &stat);
1388 else
1389 fits_set_quantize_method(outfptr, fpvar.dither_method, &stat);
1390
1391 fits_set_quantize_level (outfptr, fpvar.quantize_level, &stat);
1392 fits_set_dither_offset(outfptr, fpvar.dither_offset, &stat);
1393 fits_set_hcomp_scale (outfptr, fpvar.scale, &stat);
1394 fits_set_hcomp_smooth (outfptr, fpvar.smooth, &stat);
1395
1396 fp_test_hdu(infptr, outfptr, outfptr2, fpvar, &stat);
1397
1398 /*
1399 fits_set_compression_type (outfptr, BZIP2_1, &stat);
1400 fits_set_tile_dim (outfptr, 6, fpvar.ntile, &stat);
1401 fp_test_hdu(infptr, outfptr, outfptr2, fpvar, &stat);
1402 */
1403 /*
1404 fits_set_compression_type (outfptr, PLIO_1, &stat);
1405 fits_set_tile_dim (outfptr, 6, fpvar.ntile, &stat);
1406 fp_test_hdu(infptr, outfptr, outfptr2, fpvar, &stat);
1407 */
1408 /*
1409 if (bitpix == SHORT_IMG || bitpix == LONG_IMG) {
1410 fits_set_compression_type (outfptr, NOCOMPRESS, &stat);
1411 fits_set_tile_dim (outfptr, 6, fpvar.ntile, &stat);
1412 fp_test_hdu(infptr, outfptr, outfptr2, fpvar, &stat);
1413 }
1414 */
1415 if (fpvar.outfile[0])
1416 fprintf(outreport,"\n");
1417
1418 /* delete the temporary file */
1419 if (rescale_flag) {
1420 fits_delete_file (infptr, &stat);
1421 tempfilename3[0] = '\0'; /* clear the temp filename */
1422 }
1423 } else if ( (hdutype == BINARY_TBL) && fpvar.do_tables) {
1424
1425 fits_get_num_rowsll(inputfptr, &nrows, &stat);
1426 fits_get_num_cols(inputfptr, &ncols, &stat);
1427 #if defined(_MSC_VER)
1428 /* Microsoft Visual C++ 6.0 uses '%I64d' syntax for 8-byte integers */
1429 printf("\n File: %s, HDU %d, %d cols X %I64d rows\n", infits, extnum, ncols, nrows);
1430 #elif (USE_LL_SUFFIX == 1)
1431 printf("\n File: %s, HDU %d, %d cols X %lld rows\n", infits, extnum, ncols, nrows);
1432 #else
1433 printf("\n File: %s, HDU %d, %d cols X %ld rows\n", infits, extnum, ncols, nrows);
1434 #endif
1435 fp_test_table(inputfptr, outfptr, outfptr2, fpvar, &stat);
1436
1437 } else {
1438 fits_copy_hdu (inputfptr, outfptr, 0, &stat);
1439 fits_copy_hdu (inputfptr, outfptr2, 0, &stat);
1440 }
1441
1442 fits_movrel_hdu (inputfptr, 1, NULL, &stat);
1443 extnum++;
1444 }
1445
1446
1447 if (stat == END_OF_FILE) stat = 0;
1448
1449 fits_close_file (outfptr2, &stat);
1450 fits_close_file (outfptr, &stat);
1451 fits_close_file (inputfptr, &stat);
1452
1453 if (stat) {
1454 fits_report_error (stderr, stat);
1455 }
1456 return(0);
1457 }
1458 /*--------------------------------------------------------------------------*/
1459 int fp_pack_hdu (fitsfile *infptr, fitsfile *outfptr, fpstate fpvar,
1460 int *islossless, int *status)
1461 {
1462 fitsfile *tempfile;
1463 long naxes[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
1464 int stat=0, totpix=0, naxis=0, ii, hdutype, bitpix;
1465 int tstatus, hdunum;
1466 double bscale, rescale;
1467
1468 char outfits[SZ_STR], fzalgor[FLEN_VALUE];
1469 long headstart, datastart, dataend, datasize;
1470 double noisemin;
1471 /* structure to hold image statistics (defined in fpack.h) */
1472 imgstats imagestats;
1473
1474 if (*status) return(0);
1475
1476 fits_get_hdu_type (infptr, &hdutype, &stat);
1477
1478 if (hdutype == IMAGE_HDU) {
1479 fits_get_img_param (infptr, 9, &bitpix, &naxis, naxes, &stat);
1480 for (totpix=1, ii=0; ii < 9; ii++) totpix *= naxes[ii];
1481 }
1482
1483 /* check directive keyword to see if this HDU should not be compressed */
1484 tstatus = 0;
1485 if (!fits_read_key(infptr, TSTRING, "FZALGOR", fzalgor, NULL, &tstatus) ) {
1486 if (!strcmp(fzalgor, "NONE") || !strcmp(fzalgor, "none") ) {
1487 fits_copy_hdu (infptr, outfptr, 0, &stat);
1488
1489 *status = stat;
1490 return(0);
1491 }
1492 }
1493
1494 /* =============================================================== */
1495 /* This block is only for binary table compression */
1496 if (hdutype == BINARY_TBL && fpvar.do_tables) {
1497
1498 fits_get_hduaddr(infptr, &headstart, &datastart, &dataend, status);
1499 datasize = dataend - datastart;
1500
1501 if (datasize <= 2880) {
1502 /* data is less than 1 FITS block in size, so don't compress */
1503 fits_copy_hdu (infptr, outfptr, 0, &stat);
1504 } else {
1505 fits_compress_table (infptr, outfptr, &stat);
1506 }
1507
1508 *status = stat;
1509 return(0);
1510 }
1511 /* =============================================================== */
1512
1513 /* If this is not a non-null image HDU, just copy it verbatim */
1514 if (fits_is_compressed_image (infptr, &stat) || hdutype != IMAGE_HDU ||
1515 naxis == 0 || totpix == 0 || !fpvar.do_images) {
1516 fits_copy_hdu (infptr, outfptr, 0, &stat);
1517
1518 } else { /* remaining code deals only with IMAGE HDUs */
1519
1520 /* special case: rescale a scaled integer image to reduce noise? */
1521 if (fpvar.rescale_noise != 0. && bitpix > 0 && bitpix < LONGLONG_IMG) {
1522
1523 tstatus = 0;
1524 fits_read_key(infptr, TDOUBLE, "BSCALE", &bscale, 0, &tstatus);
1525 if (tstatus == 0 && bscale != 1.0) { /* image must be scaled */
1526
1527 if (bitpix == LONG_IMG)
1528 fp_i4stat(infptr, naxis, naxes, &imagestats, &stat);
1529 else
1530 fp_i2stat(infptr, naxis, naxes, &imagestats, &stat);
1531
1532 /* use the minimum of the MAD 2nd, 3rd, and 5th order noise estimates */
1533 noisemin = imagestats.noise3;
1534 if (imagestats.noise2 != 0. && imagestats.noise2 < noisemin) noisemin = imagestats.noise2;
1535 if (imagestats.noise5 != 0. && imagestats.noise5 < noisemin) noisemin = imagestats.noise5;
1536
1537 rescale = noisemin / fpvar.rescale_noise;
1538 if (rescale > 1.0) {
1539
1540 /* all the criteria are met, so create a temporary file that */
1541 /* contains a rescaled version of the image, in output directory */
1542
1543 /* create temporary file name */
1544 fits_file_name(outfptr, outfits, &stat); /* get the output file name */
1545 fp_tmpnam("Tmp3", outfits, tempfilename3);
1546
1547 fits_create_file(&tempfile, tempfilename3, &stat);
1548
1549 fits_get_hdu_num(infptr, &hdunum);
1550 if (hdunum != 1) {
1551
1552 /* the input hdu is an image extension, so create dummy primary */
1553 fits_create_img(tempfile, 8, 0, naxes, &stat);
1554 }
1555
1556 fits_copy_header(infptr, tempfile, &stat); /* copy the header */
1557
1558 /* rescale the data, so that it will compress more efficiently */
1559 if (bitpix == LONG_IMG)
1560 fp_i4rescale(infptr, naxis, naxes, rescale, tempfile, &stat);
1561 else
1562 fp_i2rescale(infptr, naxis, naxes, rescale, tempfile, &stat);
1563
1564
1565 /* scale the BSCALE keyword by the inverse factor */
1566
1567 bscale = bscale * rescale;
1568 fits_update_key(tempfile, TDOUBLE, "BSCALE", &bscale, 0, &stat);
1569
1570 /* rescan the header, to reset the actual scaling parameters */
1571 fits_set_hdustruc(tempfile, &stat);
1572
1573 fits_img_compress (tempfile, outfptr, &stat);
1574 fits_delete_file (tempfile, &stat);
1575 tempfilename3[0] = '\0'; /* clear the temp filename */
1576 *islossless = 0; /* used a lossy compression method */
1577
1578 *status = stat;
1579 return(0);
1580 }
1581 }
1582 }
1583
1584 /* if requested to do lossy compression of integer images (by */
1585 /* converting to float), then check if this HDU qualifies */
1586 if ( (bitpix > 0) && (fpvar.int_to_float != 0) ) {
1587
1588 if (bitpix >= LONG_IMG)
1589 fp_i4stat(infptr, naxis, naxes, &imagestats, &stat);
1590 else
1591 fp_i2stat(infptr, naxis, naxes, &imagestats, &stat);
1592
1593 /* rescan the image header to reset scaling values (changed by fp_iNstat) */
1594 ffrhdu(infptr, &hdutype, &stat);
1595
1596 /* use the minimum of the MAD 2nd, 3rd, and 5th order noise estimates */
1597 noisemin = imagestats.noise3;
1598 if (imagestats.noise2 != 0. && imagestats.noise2 < noisemin) noisemin = imagestats.noise2;
1599 if (imagestats.noise5 != 0. && imagestats.noise5 < noisemin) noisemin = imagestats.noise5;
1600
1601 if ( (noisemin < (fpvar.n3ratio * fpvar.quantize_level) ) ||
1602 (imagestats.noise3 < fpvar.n3min)) {
1603
1604 /* image contains too little noise to quantize effectively */
1605 fits_set_lossy_int (outfptr, 0, &stat);
1606
1607 fits_get_hdu_num(infptr, &hdunum);
1608
1609 printf(" HDU %d does not meet noise criteria to be quantized, so losslessly compressed.\n", hdunum);
1610
1611 } else {
1612 /* compressed image is not identical to original */
1613 *islossless = 0;
1614 }
1615 }
1616
1617 /* finally, do the actual image compression */
1618 fits_img_compress (infptr, outfptr, &stat);
1619
1620 if (bitpix < 0 ||
1621 (fpvar.comptype == HCOMPRESS_1 && fpvar.scale != 0.)) {
1622
1623 /* compressed image is not identical to original */
1624 *islossless = 0;
1625 }
1626 }
1627
1628 *status = stat;
1629 return(0);
1630 }
1631
1632 /*--------------------------------------------------------------------------*/
1633 int fp_unpack_hdu (fitsfile *infptr, fitsfile *outfptr, fpstate fpvar, int *status)
1634 {
1635 int hdutype, lval;
1636
1637 if (*status > 0) return(0);
1638
1639 fits_get_hdu_type (infptr, &hdutype, status);
1640
1641 /* =============================================================== */
1642 /* This block is only for beta testing of binary table compression */
1643 if (hdutype == BINARY_TBL) {
1644
1645 fits_read_key(infptr, TLOGICAL, "ZTABLE", &lval, NULL, status);
1646
1647 if (*status == 0 && lval != 0) {
1648 /* uncompress the table */
1649 fits_uncompress_table (infptr, outfptr, status);
1650 } else {
1651 if (*status == KEY_NO_EXIST) /* table is not compressed */
1652 *status = 0;
1653 fits_copy_hdu (infptr, outfptr, 0, status);
1654 }
1655
1656 return(0);
1657 /* =============================================================== */
1658
1659 } else if (fits_is_compressed_image (infptr, status)) {
1660 /* uncompress the compressed image HDU */
1661 fits_img_decompress (infptr, outfptr, status);
1662 } else {
1663 /* not a compressed image HDU, so just copy it to the output */
1664 fits_copy_hdu (infptr, outfptr, 0, status);
1665 }
1666
1667 return(0);
1668 }
1669 /*--------------------------------------------------------------------------*/
1670 int fits_read_image_speed (fitsfile *infptr, float *whole_elapse,
1671 float *whole_cpu, float *row_elapse, float *row_cpu, int *status)
1672 {
1673 unsigned char *carray, cnull = 0;
1674 short *sarray, snull=0;
1675 int bitpix, naxis, anynull, *iarray, inull = 0;
1676 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};
1677 long inc[9]={1,1,1,1,1,1,1,1,1} ;
1678 float *earray, enull = 0, filesize;
1679 double *darray, dnull = 0;
1680
1681 if (*status) return(*status);
1682
1683 fits_get_img_param (infptr, 9, &bitpix, &naxis, naxes, status);
1684
1685 if (naxis != 2)return(*status);
1686
1687 lpixel[0] = naxes[0];
1688 lpixel[1] = naxes[1];
1689
1690 /* filesize in MB */
1691 filesize = (float) (naxes[0] * abs(bitpix) / 8000000. * naxes[1]);
1692
1693 /* measure time required to read the raw image */
1694 fits_set_bscale(infptr, 1.0, 0.0, status);
1695 *whole_elapse = 0.;
1696 *whole_cpu = 0;
1697
1698 if (bitpix == BYTE_IMG) {
1699 carray = calloc(naxes[1]*naxes[0], sizeof(char));
1700
1701 /* remove any cached uncompressed tile
1702 (dangerous to directly modify the structure!) */
1703 /* (infptr->Fptr)->tilerow = 0; */
1704
1705 marktime(status);
1706 fits_read_subset(infptr, TBYTE, fpixel, lpixel, inc, &cnull,
1707 carray, &anynull, status);
1708
1709 /* get elapsped times */
1710 gettime(whole_elapse, whole_cpu, status);
1711
1712 /* now read the image again, row by row */
1713 if (row_elapse) {
1714
1715 /* remove any cached uncompressed tile
1716 (dangerous to directly modify the structure!) */
1717 /* (infptr->Fptr)->tilerow = 0; */
1718
1719 marktime(status);
1720 for (ii = 0; ii < naxes[1]; ii++) {
1721 fpixel[1] = ii+1;
1722 fits_read_pix(infptr, TBYTE, fpixel, naxes[0], &cnull,
1723 carray, &anynull, status);
1724 }
1725 /* get elapsped times */
1726 gettime(row_elapse, row_cpu, status);
1727 }
1728 free(carray);
1729
1730 } else if (bitpix == SHORT_IMG) {
1731 sarray = calloc(naxes[0]*naxes[1], sizeof(short));
1732
1733 marktime(status);
1734 fits_read_subset(infptr, TSHORT, fpixel, lpixel, inc, &snull,
1735 sarray, &anynull, status);
1736
1737 gettime(whole_elapse, whole_cpu, status); /* get elapsped times */
1738
1739 /* now read the image again, row by row */
1740 if (row_elapse) {
1741 marktime(status);
1742 for (ii = 0; ii < naxes[1]; ii++) {
1743
1744 fpixel[1] = ii+1;
1745 fits_read_pix(infptr, TSHORT, fpixel, naxes[0], &snull,
1746 sarray, &anynull, status);
1747 }
1748 /* get elapsped times */
1749 gettime(row_elapse, row_cpu, status);
1750 }
1751
1752 free(sarray);
1753
1754 } else if (bitpix == LONG_IMG) {
1755 iarray = calloc(naxes[0]*naxes[1], sizeof(int));
1756
1757 marktime(status);
1758
1759 fits_read_subset(infptr, TINT, fpixel, lpixel, inc, &inull,
1760 iarray, &anynull, status);
1761
1762 /* get elapsped times */
1763 gettime(whole_elapse, whole_cpu, status);
1764
1765
1766 /* now read the image again, row by row */
1767 if (row_elapse) {
1768 marktime(status);
1769 for (ii = 0; ii < naxes[1]; ii++) {
1770 fpixel[1] = ii+1;
1771 fits_read_pix(infptr, TINT, fpixel, naxes[0], &inull,
1772 iarray, &anynull, status);
1773 }
1774 /* get elapsped times */
1775 gettime(row_elapse, row_cpu, status);
1776 }
1777
1778
1779 free(iarray);
1780
1781 } else if (bitpix == FLOAT_IMG) {
1782 earray = calloc(naxes[1]*naxes[0], sizeof(float));
1783
1784 marktime(status);
1785
1786 fits_read_subset(infptr, TFLOAT, fpixel, lpixel, inc, &enull,
1787 earray, &anynull, status);
1788
1789 /* get elapsped times */
1790 gettime(whole_elapse, whole_cpu, status);
1791
1792 /* now read the image again, row by row */
1793 if (row_elapse) {
1794 marktime(status);
1795 for (ii = 0; ii < naxes[1]; ii++) {
1796 fpixel[1] = ii+1;
1797 fits_read_pix(infptr, TFLOAT, fpixel, naxes[0], &enull,
1798 earray, &anynull, status);
1799 }
1800 /* get elapsped times */
1801 gettime(row_elapse, row_cpu, status);
1802 }
1803
1804 free(earray);
1805
1806 } else if (bitpix == DOUBLE_IMG) {
1807 darray = calloc(naxes[1]*naxes[0], sizeof(double));
1808
1809 marktime(status);
1810
1811 fits_read_subset(infptr, TDOUBLE, fpixel, lpixel, inc, &dnull,
1812 darray, &anynull, status);
1813
1814 /* get elapsped times */
1815 gettime(whole_elapse, whole_cpu, status);
1816
1817 /* now read the image again, row by row */
1818 if (row_elapse) {
1819 marktime(status);
1820 for (ii = 0; ii < naxes[1]; ii++) {
1821 fpixel[1] = ii+1;
1822 fits_read_pix(infptr, TDOUBLE, fpixel, naxes[0], &dnull,
1823 darray, &anynull, status);
1824 }
1825 /* get elapsped times */
1826 gettime(row_elapse, row_cpu, status);
1827 }
1828
1829 free(darray);
1830 }
1831
1832 if (whole_elapse) *whole_elapse = *whole_elapse / filesize;
1833 if (row_elapse) *row_elapse = *row_elapse / filesize;
1834 if (whole_cpu) *whole_cpu = *whole_cpu / filesize;
1835 if (row_cpu) *row_cpu = *row_cpu / filesize;
1836
1837 return(*status);
1838 }
1839 /*--------------------------------------------------------------------------*/
1840 int fp_test_hdu (fitsfile *infptr, fitsfile *outfptr, fitsfile *outfptr2,
1841 fpstate fpvar, int *status)
1842 {
1843 /* This routine is only used for performance testing of image HDUs. */
1844 /* Use fp_test_table for testing binary table HDUs. */
1845
1846 int stat = 0, hdutype, comptype;
1847 char ctype[20], lossless[4];
1848 long headstart, datastart, dataend;
1849 float origdata = 0., compressdata = 0.;
1850 float compratio = 0., packcpu = 0., unpackcpu = 0.;
1851 float elapse, whole_elapse, row_elapse, whole_cpu, row_cpu;
1852 unsigned long datasum1, datasum2, hdusum;
1853
1854 if (*status) return(0);
1855
1856 origdata = 0;
1857 compressdata = 0;
1858 compratio = 0.;
1859 lossless[0] = '\0';
1860
1861 fits_get_compression_type(outfptr, &comptype, &stat);
1862 if (comptype == RICE_1)
1863 strcpy(ctype, "RICE");
1864 else if (comptype == GZIP_1)
1865 strcpy(ctype, "GZIP1");
1866 else if (comptype == GZIP_2)
1867 strcpy(ctype, "GZIP2");/*
1868 else if (comptype == BZIP2_1)
1869 strcpy(ctype, "BZIP2");
1870 */
1871 else if (comptype == PLIO_1)
1872 strcpy(ctype, "PLIO");
1873 else if (comptype == HCOMPRESS_1)
1874 strcpy(ctype, "HCOMP");
1875 else if (comptype == NOCOMPRESS)
1876 strcpy(ctype, "NONE");
1877 else {
1878 fp_msg ("Error: unsupported image compression type ");
1879 *status = DATA_COMPRESSION_ERR;
1880 return(0);
1881 }
1882
1883 /* -------------- COMPRESS the image ------------------ */
1884
1885 marktime(&stat);
1886
1887 fits_img_compress (infptr, outfptr, &stat);
1888
1889 /* get elapsped times */
1890 gettime(&elapse, &packcpu, &stat);
1891
1892 /* get elapsed and cpu times need to read the compressed image */
1893 fits_read_image_speed (outfptr, &whole_elapse, &whole_cpu,
1894 &row_elapse, &row_cpu, &stat);
1895
1896 if (!stat) {
1897
1898 /* -------------- UNCOMPRESS the image ------------------ */
1899
1900 /* remove any cached uncompressed tile
1901 (dangerous to directly modify the structure!) */
1902 /* (outfptr->Fptr)->tilerow = 0; */
1903 marktime(&stat);
1904
1905 fits_img_decompress (outfptr, outfptr2, &stat);
1906
1907 /* get elapsped times */
1908 gettime(&elapse, &unpackcpu, &stat);
1909
1910 /* ----------------------------------------------------- */
1911
1912
1913 /* get sizes of original and compressed images */
1914
1915 fits_get_hduaddr(infptr, &headstart, &datastart, &dataend, &stat);
1916 origdata = (float) ((dataend - datastart)/1000000.);
1917
1918 fits_get_hduaddr(outfptr, &headstart, &datastart, &dataend, &stat);
1919 compressdata = (float) ((dataend - datastart)/1000000.);
1920
1921 if (compressdata != 0)
1922 compratio = (float) origdata / (float) compressdata;
1923
1924 /* is this uncompressed image identical to the original? */
1925
1926 fits_get_chksum(infptr, &datasum1, &hdusum, &stat);
1927 fits_get_chksum(outfptr2, &datasum2, &hdusum, &stat);
1928
1929 if ( datasum1 == datasum2) {
1930 strcpy(lossless, "Yes");
1931 } else {
1932 strcpy(lossless, "No");
1933 }
1934
1935 printf(" %-5s %6.2f %7.2f ->%7.2f %7.2f %7.2f %s %5.3f %5.3f %5.3f %5.3f\n",
1936 ctype, compratio, origdata, compressdata,
1937 packcpu, unpackcpu, lossless, whole_elapse, whole_cpu,
1938 row_elapse, row_cpu);
1939
1940
1941 if (fpvar.outfile[0]) {
1942 fprintf(outreport," %6.3f %5.2f %5.2f %s %7.3f %7.3f %7.3f %7.3f",
1943 compratio, packcpu, unpackcpu, lossless, whole_elapse, whole_cpu,
1944 row_elapse, row_cpu);
1945 }
1946
1947 /* delete the output HDUs to concerve disk space */
1948
1949 fits_delete_hdu(outfptr, &hdutype, &stat);
1950 fits_delete_hdu(outfptr2, &hdutype, &stat);
1951
1952 } else {
1953 printf(" %-5s (unable to compress image)\n", ctype);
1954 }
1955
1956 /* try to recover from any compression errors */
1957 if (stat == DATA_COMPRESSION_ERR) stat = 0;
1958
1959 *status = stat;
1960 return(0);
1961 }
1962 /*--------------------------------------------------------------------------*/
1963 int fp_test_table (fitsfile *infptr, fitsfile *outfptr, fitsfile *outfptr2,
1964 fpstate fpvar, int *status)
1965 {
1966 /* this routine is for performance testing of the table compression methods */
1967
1968 int stat = 0, hdutype, tstatus = 0;
1969 char fzalgor[FLEN_VALUE];
1970 LONGLONG headstart, datastart, dataend;
1971 float elapse, cpu;
1972
1973 if (*status) return(0);
1974
1975 /* check directive keyword to see if this HDU should not be compressed */
1976 if (!fits_read_key(infptr, TSTRING, "FZALGOR", fzalgor, NULL, &tstatus) ) {
1977 if (!strcmp(fzalgor, "NONE") || !strcmp(fzalgor, "none")) {
1978 return(0);
1979 }
1980 }
1981
1982 fits_get_hduaddrll(infptr, &headstart, &datastart, &dataend, status);
1983
1984 /* can't compress small tables with less than 2880 bytes of data */
1985 if (dataend - datastart <= 2880) {
1986 return(0);
1987 }
1988
1989 marktime(&stat);
1990 stat= -999; /* set special flag value */
1991 fits_compress_table (infptr, outfptr, &stat);
1992
1993 /* get elapsped times */
1994 gettime(&elapse, &cpu, &stat);
1995
1996 fits_delete_hdu(outfptr, &hdutype, &stat);
1997
1998 printf("\nElapsed time = %f, cpu = %f\n", elapse, cpu);
1999
2000 fits_report_error (stderr, stat);
2001
2002 return(0);
2003 }
2004 /*--------------------------------------------------------------------------*/
2005 int marktime(int *status)
2006 {
2007 #if defined(unix) || defined(__unix__) || defined(__unix)
2008 struct timeval tv;
2009 /* struct timezone tz; */
2010
2011 /* gettimeofday (&tv, &tz); */
2012 gettimeofday (&tv, NULL);
2013
2014 startsec = tv.tv_sec;
2015 startmilli = tv.tv_usec/1000;
2016
2017 scpu = clock();
2018 #else
2019 /* don't support high timing precision on Windows machines */
2020 startsec = 0;
2021 startmilli = 0;
2022
2023 scpu = clock();
2024 #endif
2025 return( *status );
2026 }
2027 /*--------------------------------------------------------------------------*/
2028 int gettime(float *elapse, float *elapscpu, int *status)
2029 {
2030 #if defined(unix) || defined(__unix__) || defined(__unix)
2031 struct timeval tv;
2032 /* struct timezone tz; */
2033 int stopmilli;
2034 long stopsec;
2035
2036 /* gettimeofday (&tv, &tz); */
2037 gettimeofday (&tv, NULL);
2038 ecpu = clock();
2039
2040 stopmilli = tv.tv_usec/1000;
2041 stopsec = tv.tv_sec;
2042
2043 *elapse = (stopsec - startsec) + (stopmilli - startmilli)/1000.;
2044 *elapscpu = (ecpu - scpu) * 1.0 / CLOCKTICKS;
2045 /*
2046 printf(" (start: %ld + %d), stop: (%ld + %d) elapse: %f\n ",
2047 startsec,startmilli,stopsec, stopmilli, *elapse);
2048 */
2049 #else
2050 /* set the elapsed time the same as the CPU time on Windows machines */
2051 *elapscpu = (float) ((ecpu - scpu) * 1.0 / CLOCKTICKS);
2052 *elapse = *elapscpu;
2053 #endif
2054 return( *status );
2055 }
2056 /*--------------------------------------------------------------------------*/
2057 int fp_i2stat(fitsfile *infptr, int naxis, long *naxes, imgstats *imagestats, int *status)
2058 {
2059 /*
2060 read the central XSAMPLE by YSAMPLE region of pixels in the int*2 image,
2061 and then compute basic statistics: min, max, mean, sigma, mean diff, etc.
2062 */
2063
2064 long fpixel[9] = {1,1,1,1,1,1,1,1,1};
2065 long lpixel[9] = {1,1,1,1,1,1,1,1,1};
2066 long inc[9] = {1,1,1,1,1,1,1,1,1};
2067 long i1, i2, npix, ngood, nx, ny;
2068 short *intarray, minvalue, maxvalue, nullvalue;
2069 int anynul, tstatus, checknull = 1;
2070 double mean, sigma, noise1, noise2, noise3, noise5;
2071
2072 /* select the middle XSAMPLE by YSAMPLE area of the image */
2073 i1 = naxes[0]/2 - (XSAMPLE/2 - 1);
2074 i2 = naxes[0]/2 + (XSAMPLE/2);
2075 if (i1 < 1) i1 = 1;
2076 if (i2 > naxes[0]) i2 = naxes[0];
2077 fpixel[0] = i1;
2078 lpixel[0] = i2;
2079 nx = i2 - i1 +1;
2080
2081 if (naxis > 1) {
2082 i1 = naxes[1]/2 - (YSAMPLE/2 - 1);
2083 i2 = naxes[1]/2 + (YSAMPLE/2);
2084 if (i1 < 1) i1 = 1;
2085 if (i2 > naxes[1]) i2 = naxes[1];
2086 fpixel[1] = i1;
2087 lpixel[1] = i2;
2088 }
2089 ny = i2 - i1 +1;
2090
2091 npix = nx * ny;
2092
2093 /* if there are higher dimensions, read the middle plane of the cube */
2094 if (naxis > 2) {
2095 fpixel[2] = naxes[2]/2 + 1;
2096 lpixel[2] = naxes[2]/2 + 1;
2097 }
2098
2099 intarray = calloc(npix, sizeof(short));
2100 if (!intarray) {
2101 *status = MEMORY_ALLOCATION;
2102 return(*status);
2103 }
2104
2105 /* turn off any scaling of the integer pixel values */
2106 fits_set_bscale(infptr, 1.0, 0.0, status);
2107
2108 fits_read_subset_sht(infptr, 0, naxis, naxes, fpixel, lpixel, inc,
2109 0, intarray, &anynul, status);
2110
2111 /* read the null value keyword (BLANK) if present */
2112 tstatus = 0;
2113 fits_read_key(infptr, TSHORT, "BLANK", &nullvalue, 0, &tstatus);
2114 if (tstatus) {
2115 nullvalue = 0;
2116 checknull = 0;
2117 }
2118
2119 /* compute statistics of the image */
2120
2121 fits_img_stats_short(intarray, nx, ny, checknull, nullvalue,
2122 &ngood, &minvalue, &maxvalue, &mean, &sigma, &noise1, &noise2, &noise3, &noise5, status);
2123
2124 imagestats->n_nulls = npix - ngood;
2125 imagestats->minval = minvalue;
2126 imagestats->maxval = maxvalue;
2127 imagestats->mean = mean;
2128 imagestats->sigma = sigma;
2129 imagestats->noise1 = noise1;
2130 imagestats->noise2 = noise2;
2131 imagestats->noise3 = noise3;
2132 imagestats->noise5 = noise5;
2133
2134 free(intarray);
2135 return(*status);
2136 }
2137 /*--------------------------------------------------------------------------*/
2138 int fp_i4stat(fitsfile *infptr, int naxis, long *naxes, imgstats *imagestats, int *status)
2139 {
2140 /*
2141 read the central XSAMPLE by YSAMPLE region of pixels in the int*2 image,
2142 and then compute basic statistics: min, max, mean, sigma, mean diff, etc.
2143 */
2144
2145 long fpixel[9] = {1,1,1,1,1,1,1,1,1};
2146 long lpixel[9] = {1,1,1,1,1,1,1,1,1};
2147 long inc[9] = {1,1,1,1,1,1,1,1,1};
2148 long i1, i2, npix, ngood, nx, ny;
2149 int *intarray, minvalue, maxvalue, nullvalue;
2150 int anynul, tstatus, checknull = 1;
2151 double mean, sigma, noise1, noise2, noise3, noise5;
2152
2153 /* select the middle XSAMPLE by YSAMPLE area of the image */
2154 i1 = naxes[0]/2 - (XSAMPLE/2 - 1);
2155 i2 = naxes[0]/2 + (XSAMPLE/2);
2156 if (i1 < 1) i1 = 1;
2157 if (i2 > naxes[0]) i2 = naxes[0];
2158 fpixel[0] = i1;
2159 lpixel[0] = i2;
2160 nx = i2 - i1 +1;
2161
2162 if (naxis > 1) {
2163 i1 = naxes[1]/2 - (YSAMPLE/2 - 1);
2164 i2 = naxes[1]/2 + (YSAMPLE/2);
2165 if (i1 < 1) i1 = 1;
2166 if (i2 > naxes[1]) i2 = naxes[1];
2167 fpixel[1] = i1;
2168 lpixel[1] = i2;
2169 }
2170 ny = i2 - i1 +1;
2171
2172 npix = nx * ny;
2173
2174 /* if there are higher dimensions, read the middle plane of the cube */
2175 if (naxis > 2) {
2176 fpixel[2] = naxes[2]/2 + 1;
2177 lpixel[2] = naxes[2]/2 + 1;
2178 }
2179
2180 intarray = calloc(npix, sizeof(int));
2181 if (!intarray) {
2182 *status = MEMORY_ALLOCATION;
2183 return(*status);
2184 }
2185
2186 /* turn off any scaling of the integer pixel values */
2187 fits_set_bscale(infptr, 1.0, 0.0, status);
2188
2189 fits_read_subset_int(infptr, 0, naxis, naxes, fpixel, lpixel, inc,
2190 0, intarray, &anynul, status);
2191
2192 /* read the null value keyword (BLANK) if present */
2193 tstatus = 0;
2194 fits_read_key(infptr, TINT, "BLANK", &nullvalue, 0, &tstatus);
2195 if (tstatus) {
2196 nullvalue = 0;
2197 checknull = 0;
2198 }
2199
2200 /* compute statistics of the image */
2201
2202 fits_img_stats_int(intarray, nx, ny, checknull, nullvalue,
2203 &ngood, &minvalue, &maxvalue, &mean, &sigma, &noise1, &noise2, &noise3, &noise5, status);
2204
2205 imagestats->n_nulls = npix - ngood;
2206 imagestats->minval = minvalue;
2207 imagestats->maxval = maxvalue;
2208 imagestats->mean = mean;
2209 imagestats->sigma = sigma;
2210 imagestats->noise1 = noise1;
2211 imagestats->noise2 = noise2;
2212 imagestats->noise3 = noise3;
2213 imagestats->noise5 = noise5;
2214
2215 free(intarray);
2216 return(*status);
2217 }
2218 /*--------------------------------------------------------------------------*/
2219 int fp_r4stat(fitsfile *infptr, int naxis, long *naxes, imgstats *imagestats, int *status)
2220 {
2221 /*
2222 read the central XSAMPLE by YSAMPLE region of pixels in the int*2 image,
2223 and then compute basic statistics: min, max, mean, sigma, mean diff, etc.
2224 */
2225
2226 long fpixel[9] = {1,1,1,1,1,1,1,1,1};
2227 long lpixel[9] = {1,1,1,1,1,1,1,1,1};
2228 long inc[9] = {1,1,1,1,1,1,1,1,1};
2229 long i1, i2, npix, ngood, nx, ny;
2230 float *array, minvalue, maxvalue, nullvalue = FLOATNULLVALUE;
2231 int anynul,checknull = 1;
2232 double mean, sigma, noise1, noise2, noise3, noise5;
2233
2234 /* select the middle XSAMPLE by YSAMPLE area of the image */
2235 i1 = naxes[0]/2 - (XSAMPLE/2 - 1);
2236 i2 = naxes[0]/2 + (XSAMPLE/2);
2237 if (i1 < 1) i1 = 1;
2238 if (i2 > naxes[0]) i2 = naxes[0];
2239 fpixel[0] = i1;
2240 lpixel[0] = i2;
2241 nx = i2 - i1 +1;
2242
2243 if (naxis > 1) {
2244 i1 = naxes[1]/2 - (YSAMPLE/2 - 1);
2245 i2 = naxes[1]/2 + (YSAMPLE/2);
2246 if (i1 < 1) i1 = 1;
2247 if (i2 > naxes[1]) i2 = naxes[1];
2248 fpixel[1] = i1;
2249 lpixel[1] = i2;
2250 }
2251 ny = i2 - i1 +1;
2252
2253 npix = nx * ny;
2254
2255 /* if there are higher dimensions, read the middle plane of the cube */
2256 if (naxis > 2) {
2257 fpixel[2] = naxes[2]/2 + 1;
2258 lpixel[2] = naxes[2]/2 + 1;
2259 }
2260
2261 array = calloc(npix, sizeof(float));
2262 if (!array) {
2263 *status = MEMORY_ALLOCATION;
2264 return(*status);
2265 }
2266
2267 fits_read_subset_flt(infptr, 0, naxis, naxes, fpixel, lpixel, inc,
2268 nullvalue, array, &anynul, status);
2269
2270 /* are there any null values in the array? */
2271 if (!anynul) {
2272 nullvalue = 0.;
2273 checknull = 0;
2274 }
2275
2276 /* compute statistics of the image */
2277
2278 fits_img_stats_float(array, nx, ny, checknull, nullvalue,
2279 &ngood, &minvalue, &maxvalue, &mean, &sigma, &noise1, &noise2, &noise3, &noise5, status);
2280
2281 imagestats->n_nulls = npix - ngood;
2282 imagestats->minval = minvalue;
2283 imagestats->maxval = maxvalue;
2284 imagestats->mean = mean;
2285 imagestats->sigma = sigma;
2286 imagestats->noise1 = noise1;
2287 imagestats->noise2 = noise2;
2288 imagestats->noise3 = noise3;
2289 imagestats->noise5 = noise5;
2290
2291 free(array);
2292 return(*status);
2293 }
2294 /*--------------------------------------------------------------------------*/
2295 int fp_i2rescale(fitsfile *infptr, int naxis, long *naxes, double rescale,
2296 fitsfile *outfptr, int *status)
2297 {
2298 /*
2299 divide the integer pixel values in the input file by rescale,
2300 and write back out to the output file..
2301 */
2302
2303 long ii, jj, nelem = 1, nx, ny;
2304 short *intarray, nullvalue;
2305 int anynul, tstatus, checknull = 1;
2306
2307 nx = naxes[0];
2308 ny = 1;
2309
2310 for (ii = 1; ii < naxis; ii++) {
2311 ny = ny * naxes[ii];
2312 }
2313
2314 intarray = calloc(nx, sizeof(short));
2315 if (!intarray) {
2316 *status = MEMORY_ALLOCATION;
2317 return(*status);
2318 }
2319
2320 /* read the null value keyword (BLANK) if present */
2321 tstatus = 0;
2322 fits_read_key(infptr, TSHORT, "BLANK", &nullvalue, 0, &tstatus);
2323 if (tstatus) {
2324 checknull = 0;
2325 }
2326
2327 /* turn off any scaling of the integer pixel values */
2328 fits_set_bscale(infptr, 1.0, 0.0, status);
2329 fits_set_bscale(outfptr, 1.0, 0.0, status);
2330
2331 for (ii = 0; ii < ny; ii++) {
2332
2333 fits_read_img_sht(infptr, 1, nelem, nx,
2334 0, intarray, &anynul, status);
2335
2336 if (checknull) {
2337 for (jj = 0; jj < nx; jj++) {
2338 if (intarray[jj] != nullvalue)
2339 intarray[jj] = NSHRT( (intarray[jj] / rescale) );
2340 }
2341 } else {
2342 for (jj = 0; jj < nx; jj++)
2343 intarray[jj] = NSHRT( (intarray[jj] / rescale) );
2344 }
2345
2346 fits_write_img_sht(outfptr, 1, nelem, nx, intarray, status);
2347
2348 nelem += nx;
2349 }
2350
2351 free(intarray);
2352 return(*status);
2353 }
2354 /*--------------------------------------------------------------------------*/
2355 int fp_i4rescale(fitsfile *infptr, int naxis, long *naxes, double rescale,
2356 fitsfile *outfptr, int *status)
2357 {
2358 /*
2359 divide the integer pixel values in the input file by rescale,
2360 and write back out to the output file..
2361 */
2362
2363 long ii, jj, nelem = 1, nx, ny;
2364 int *intarray, nullvalue;
2365 int anynul, tstatus, checknull = 1;
2366
2367 nx = naxes[0];
2368 ny = 1;
2369
2370 for (ii = 1; ii < naxis; ii++) {
2371 ny = ny * naxes[ii];
2372 }
2373
2374 intarray = calloc(nx, sizeof(int));
2375 if (!intarray) {
2376 *status = MEMORY_ALLOCATION;
2377 return(*status);
2378 }
2379
2380 /* read the null value keyword (BLANK) if present */
2381 tstatus = 0;
2382 fits_read_key(infptr, TINT, "BLANK", &nullvalue, 0, &tstatus);
2383 if (tstatus) {
2384 checknull = 0;
2385 }
2386
2387 /* turn off any scaling of the integer pixel values */
2388 fits_set_bscale(infptr, 1.0, 0.0, status);
2389 fits_set_bscale(outfptr, 1.0, 0.0, status);
2390
2391 for (ii = 0; ii < ny; ii++) {
2392
2393 fits_read_img_int(infptr, 1, nelem, nx,
2394 0, intarray, &anynul, status);
2395
2396 if (checknull) {
2397 for (jj = 0; jj < nx; jj++) {
2398 if (intarray[jj] != nullvalue)
2399 intarray[jj] = NINT( (intarray[jj] / rescale) );
2400 }
2401 } else {
2402 for (jj = 0; jj < nx; jj++)
2403 intarray[jj] = NINT( (intarray[jj] / rescale) );
2404 }
2405
2406 fits_write_img_int(outfptr, 1, nelem, nx, intarray, status);
2407
2408 nelem += nx;
2409 }
2410
2411 free(intarray);
2412 return(*status);
2413 }
2414 /* ========================================================================
2415 * Signal and error handler.
2416 */
2417 void abort_fpack(int sig)
2418 {
2419 /* clean up by deleting temporary files */
2420
2421 if (tempfilename[0]) {
2422 remove(tempfilename);
2423 }
2424 if (tempfilename2[0]) {
2425 remove(tempfilename2);
2426 }
2427 if (tempfilename3[0]) {
2428 remove(tempfilename3);
2429 }
2430 exit(-1);
2431 }