This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
fpackutil.c
2431 lines (1964 loc) · 76.6 KB
/
fpackutil.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* FPACK utility routines
R. Seaman, NOAO & W. Pence, NASA/GSFC
*/
#include <time.h>
#include <float.h>
#include <signal.h>
#include <ctype.h>
/* #include "bzlib.h" only for experimental purposes */
#if defined(unix) || defined(__unix__) || defined(__unix)
#include <sys/time.h>
#endif
#include <math.h>
#include "fitsio.h"
#include "fpack.h"
/* these filename buffer are used to delete temporary files */
/* in case the program is aborted */
char tempfilename[SZ_STR];
char tempfilename2[SZ_STR];
char tempfilename3[SZ_STR];
/* nearest integer function */
# define NINT(x) ((x >= 0.) ? (int) (x + 0.5) : (int) (x - 0.5))
# define NSHRT(x) ((x >= 0.) ? (short) (x + 0.5) : (short) (x - 0.5))
/* define variables for measuring elapsed time */
clock_t scpu, ecpu;
long startsec; /* start of elapsed time interval */
int startmilli; /* start of elapsed time interval */
/* CLOCKS_PER_SEC should be defined by most compilers */
#if defined(CLOCKS_PER_SEC)
#define CLOCKTICKS CLOCKS_PER_SEC
#else
/* on SUN OS machine, CLOCKS_PER_SEC is not defined, so set its value */
#define CLOCKTICKS 1000000
#endif
FILE *outreport;
/* dimension of central image area to be sampled for test statistics */
int XSAMPLE = 4100;
int YSAMPLE = 4100;
int fp_msg (char *msg)
{
printf ("%s", msg);
return(0);
}
/*--------------------------------------------------------------------------*/
int fp_noop (void)
{
fp_msg ("Input and output files are unchanged.\n");
return(0);
}
/*--------------------------------------------------------------------------*/
void fp_abort_output (fitsfile *infptr, fitsfile *outfptr, int stat)
{
int status = 0, hdunum;
char msg[SZ_STR];
if (infptr)
{
fits_file_name(infptr, tempfilename, &status);
fits_get_hdu_num(infptr, &hdunum);
fits_close_file (infptr, &status);
snprintf(msg, SZ_STR,"Error processing file: %s\n", tempfilename);
fp_msg (msg);
snprintf(msg, SZ_STR," in HDU number %d\n", hdunum);
fp_msg (msg);
}
else
{
snprintf(msg, SZ_STR,"Error: Unable to process input file\n");
fp_msg(msg);
}
fits_report_error (stderr, stat);
if (outfptr) {
fits_delete_file(outfptr, &status);
fp_msg ("Input file is unchanged.\n");
}
exit (stat);
}
/*--------------------------------------------------------------------------*/
int fp_version (void)
{
float version;
char cfitsioversion[40];
fp_msg (FPACK_VERSION);
fits_get_version(&version);
snprintf(cfitsioversion, 40," CFITSIO version %5.3f", version);
fp_msg(cfitsioversion);
fp_msg ("\n");
return(0);
}
/*--------------------------------------------------------------------------*/
int fp_access (char *filename)
{
/* test if a file exists */
FILE *diskfile;
diskfile = fopen(filename, "r");
if (diskfile) {
fclose(diskfile);
return(0);
} else {
return(-1);
}
}
/*--------------------------------------------------------------------------*/
int fp_tmpnam(char *suffix, char *rootname, char *tmpnam)
{
/* create temporary file name */
int maxtry = 30, ii;
if (strlen(suffix) + strlen(rootname) > SZ_STR-5) {
fp_msg ("Error: filename is too long to create temporary file\n"); exit (-1);
}
strcpy (tmpnam, rootname); /* start with rootname */
strcat(tmpnam, suffix); /* append the suffix */
maxtry = SZ_STR - strlen(tmpnam) - 1;
for (ii = 0; ii < maxtry; ii++) {
if (fp_access(tmpnam)) break; /* good, the file does not exist */
if (strlen(tmpnam) > SZ_STR-2)
{
fp_msg ("\nCould not create temporary file name:\n");
fp_msg (tmpnam);
fp_msg ("\n");
exit (-1);
}
strcat(tmpnam, "x"); /* append an x to the name, and try again */
}
if (ii == maxtry) {
fp_msg ("\nCould not create temporary file name:\n");
fp_msg (tmpnam);
fp_msg ("\n");
exit (-1);
}
return(0);
}
/*--------------------------------------------------------------------------*/
int fp_init (fpstate *fpptr)
{
int ii;
fpptr->comptype = RICE_1;
fpptr->quantize_level = DEF_QLEVEL;
fpptr->no_dither = 0;
fpptr->dither_method = 1;
fpptr->dither_offset = 0;
fpptr->int_to_float = 0;
/* thresholds when using the -i2f flag */
fpptr->n3ratio = 2.0; /* minimum ratio of image noise sigma / q */
fpptr->n3min = 6.; /* minimum noise sigma. */
fpptr->scale = DEF_HCOMP_SCALE;
fpptr->smooth = DEF_HCOMP_SMOOTH;
fpptr->rescale_noise = DEF_RESCALE_NOISE;
fpptr->ntile[0] = (long) -1; /* -1 means extent of axis */
for (ii=1; ii < MAX_COMPRESS_DIM; ii++)
fpptr->ntile[ii] = (long) 1;
fpptr->to_stdout = 0;
fpptr->listonly = 0;
fpptr->clobber = 0;
fpptr->delete_input = 0;
fpptr->do_not_prompt = 0;
fpptr->do_checksums = 1;
fpptr->do_gzip_file = 0;
fpptr->do_tables = 0; /* this is intended for testing purposes */
fpptr->do_images = 1; /* can be turned off with -tableonly switch */
fpptr->test_all = 0;
fpptr->verbose = 0;
fpptr->prefix[0] = 0;
fpptr->extname[0] = 0;
fpptr->delete_suffix = 0;
fpptr->outfile[0] = 0;
fpptr->firstfile = 1;
/* magic number for initialization check, boolean for preflight
*/
fpptr->initialized = FP_INIT_MAGIC;
fpptr->preflight_checked = 0;
return(0);
}
/*--------------------------------------------------------------------------*/
int fp_list (int argc, char *argv[], fpstate fpvar)
{
fitsfile *infptr;
char infits[SZ_STR], msg[SZ_STR];
int hdunum, iarg, stat=0;
LONGLONG sizell;
if (fpvar.initialized != FP_INIT_MAGIC) {
fp_msg ("Error: internal initialization error\n"); exit (-1);
}
for (iarg=fpvar.firstfile; iarg < argc; iarg++) {
strncpy (infits, argv[iarg], SZ_STR-1);
infits[SZ_STR-1]=0;
if (strchr (infits, '[') || strchr (infits, ']')) {
fp_msg ("Error: section/extension notation not supported: ");
fp_msg (infits); fp_msg ("\n"); exit (-1);
}
if (fp_access (infits) != 0) {
fp_msg ("Error: can't find or read input file "); fp_msg (infits);
fp_msg ("\n"); fp_noop (); exit (-1);
}
fits_open_file (&infptr, infits, READONLY, &stat);
if (stat) { fits_report_error (stderr, stat); exit (stat); }
/* move to the end of file, to get the total size in bytes */
fits_get_num_hdus (infptr, &hdunum, &stat);
fits_movabs_hdu (infptr, hdunum, NULL, &stat);
fits_get_hduaddrll(infptr, NULL, NULL, &sizell, &stat);
if (stat) {
fp_abort_output(infptr, NULL, stat);
}
snprintf (msg, SZ_STR,"# %s (", infits); fp_msg (msg);
#if defined(_MSC_VER)
/* Microsoft Visual C++ 6.0 uses '%I64d' syntax for 8-byte integers */
snprintf(msg, SZ_STR,"%I64d bytes)\n", sizell); fp_msg (msg);
#elif (USE_LL_SUFFIX == 1)
snprintf(msg, SZ_STR,"%lld bytes)\n", sizell); fp_msg (msg);
#else
snprintf(msg, SZ_STR,"%ld bytes)\n", sizell); fp_msg (msg);
#endif
fp_info_hdu (infptr);
fits_close_file (infptr, &stat);
if (stat) { fits_report_error (stderr, stat); exit (stat); }
}
return(0);
}
/*--------------------------------------------------------------------------*/
int fp_info_hdu (fitsfile *infptr)
{
long naxes[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
char msg[SZ_STR], val[SZ_CARD], com[SZ_CARD];
int naxis=0, hdutype, bitpix, hdupos, stat=0, ii;
unsigned long datasum, hdusum;
fits_movabs_hdu (infptr, 1, NULL, &stat);
if (stat) {
fp_abort_output(infptr, NULL, stat);
}
for (hdupos=1; ! stat; hdupos++) {
fits_get_hdu_type (infptr, &hdutype, &stat);
if (stat) {
fp_abort_output(infptr, NULL, stat);
}
/* fits_get_hdu_type calls unknown extensions "IMAGE_HDU"
* so consult XTENSION keyword itself
*/
fits_read_keyword (infptr, "XTENSION", val, com, &stat);
if (stat == KEY_NO_EXIST) {
/* in primary HDU which by definition is an "image" */
stat=0; /* clear for later error handling */
} else if (stat) {
fp_abort_output(infptr, NULL, stat);
} else if (hdutype == IMAGE_HDU) {
/* that is, if XTENSION != "IMAGE" AND != "BINTABLE" */
if (strncmp (val+1, "IMAGE", 5) &&
strncmp (val+1, "BINTABLE", 5)) {
/* assign something other than any of these */
hdutype = IMAGE_HDU + ASCII_TBL + BINARY_TBL;
}
}
fits_get_chksum(infptr, &datasum, &hdusum, &stat);
if (hdutype == IMAGE_HDU) {
snprintf (msg, SZ_STR," %d IMAGE", hdupos); fp_msg (msg);
snprintf (msg, SZ_STR," SUMS=%lu/%lu", (unsigned long) (~((int) hdusum)), datasum); fp_msg (msg);
fits_get_img_param (infptr, 9, &bitpix, &naxis, naxes, &stat);
snprintf (msg, SZ_STR," BITPIX=%d", bitpix); fp_msg (msg);
if (naxis == 0) {
snprintf (msg, SZ_STR," [no_pixels]"); fp_msg (msg);
} else if (naxis == 1) {
snprintf (msg, SZ_STR," [%ld]", naxes[1]); fp_msg (msg);
} else {
snprintf (msg, SZ_STR," [%ld", naxes[0]); fp_msg (msg);
for (ii=1; ii < naxis; ii++) {
snprintf (msg, SZ_STR,"x%ld", naxes[ii]); fp_msg (msg);
}
fp_msg ("]");
}
if (fits_is_compressed_image (infptr, &stat)) {
fits_read_keyword (infptr, "ZCMPTYPE", val, com, &stat);
/* allow for quote in keyword value */
if (! strncmp (val+1, "RICE_1", 6))
fp_msg (" tiled_rice\n");
else if (! strncmp (val+1, "GZIP_1", 6))
fp_msg (" tiled_gzip_1\n");
else if (! strncmp (val+1, "GZIP_2", 6))
fp_msg (" tiled_gzip_2\n");
else if (! strncmp (val+1, "PLIO_1", 6))
fp_msg (" tiled_plio\n");
else if (! strncmp (val+1, "HCOMPRESS_1", 11))
fp_msg (" tiled_hcompress\n");
else
fp_msg (" unknown\n");
} else
fp_msg (" not_tiled\n");
} else if (hdutype == ASCII_TBL) {
snprintf (msg, SZ_STR," %d ASCII_TBL", hdupos); fp_msg (msg);
snprintf (msg, SZ_STR," SUMS=%lu/%lu\n", (unsigned long) (~((int) hdusum)), datasum); fp_msg (msg);
} else if (hdutype == BINARY_TBL) {
snprintf (msg, SZ_STR," %d BINARY_TBL", hdupos); fp_msg (msg);
snprintf (msg, SZ_STR," SUMS=%lu/%lu\n", (unsigned long) (~((int) hdusum)), datasum); fp_msg (msg);
} else {
snprintf (msg, SZ_STR," %d OTHER", hdupos); fp_msg (msg);
snprintf (msg, SZ_STR," SUMS=%lu/%lu", (unsigned long) (~((int) hdusum)), datasum); fp_msg (msg);
snprintf (msg, SZ_STR," %s\n", val); fp_msg (msg);
}
fits_movrel_hdu (infptr, 1, NULL, &stat);
}
return(0);
}
/*--------------------------------------------------------------------------*/
int fp_preflight (int argc, char *argv[], int unpack, fpstate *fpptr)
{
char infits[SZ_STR], outfits[SZ_STR];
int iarg, namelen, nfiles = 0;
if (fpptr->initialized != FP_INIT_MAGIC) {
fp_msg ("Error: internal initialization error\n"); exit (-1);
}
for (iarg=fpptr->firstfile; iarg < argc; iarg++) {
outfits[0] = '\0';
if (strlen(argv[iarg]) > SZ_STR - 4) { /* allow for .fz or .gz suffix */
fp_msg ("Error: input file name\n "); fp_msg (argv[iarg]);
fp_msg ("\n is too long\n"); fp_noop (); exit (-1);
}
strncpy (infits, argv[iarg], SZ_STR);
if (infits[0] == '-' && infits[1] != '\0') {
/* don't interpret this as intending to read input file from stdin */
fp_msg ("Error: invalid input file name\n "); fp_msg (argv[iarg]);
fp_msg ("\n"); fp_noop (); exit (-1);
}
if (strchr (infits, '[') || strchr (infits, ']')) {
fp_msg ("Error: section/extension notation not supported: ");
fp_msg (infits); fp_msg ("\n"); fp_noop (); exit (-1);
}
if (unpack) {
/* ********** This section applies to funpack ************ */
/* check that input file exists */
if (infits[0] != '-') { /* if not reading from stdin stream */
if (fp_access (infits) != 0) { /* if not, then check if */
strcat(infits, ".fz"); /* a .fz version exsits */
if (fp_access (infits) != 0) {
namelen = strlen(infits);
infits[namelen - 3] = '\0'; /* remove the .fz suffix */
fp_msg ("Error: can't find or read input file "); fp_msg (infits);
fp_msg ("\n"); fp_noop (); exit (-1);
}
} else { /* make sure a .fz version of the same file doesn't exist */
namelen = strlen(infits);
strcat(infits, ".fz");
if (fp_access (infits) == 0) {
infits[namelen] = '\0'; /* remove the .fz suffix */
fp_msg ("Error: ambiguous input file name. Which file should be unpacked?:\n ");
fp_msg (infits); fp_msg ("\n ");
fp_msg (infits); fp_msg (".fz\n");
fp_noop (); exit (-1);
} else {
infits[namelen] = '\0'; /* remove the .fz suffix */
}
}
}
/* if writing to stdout, then we are all done */
if (fpptr->to_stdout) {
continue;
}
if (fpptr->outfile[0]) { /* user specified output file name */
nfiles++;
if (nfiles > 1) {
fp_msg ("Error: cannot use same output file name for multiple files:\n ");
fp_msg (fpptr->outfile);
fp_msg ("\n"); fp_noop (); exit (-1);
}
/* check that output file doesn't exist */
if (fp_access (fpptr->outfile) == 0) {
fp_msg ("Error: output file already exists:\n ");
fp_msg (fpptr->outfile);
fp_msg ("\n "); fp_noop (); exit (-1);
}
continue;
}
/* construct output file name to test */
if (fpptr->prefix[0]) {
if (strlen(fpptr->prefix) + strlen(infits) > SZ_STR - 1) {
fp_msg ("Error: output file name for\n "); fp_msg (infits);
fp_msg ("\n is too long with the prefix\n"); fp_noop (); exit (-1);
}
strcpy(outfits,fpptr->prefix);
}
/* construct output file name */
if (infits[0] == '-') {
strcpy(outfits, "output.fits");
} else {
strcat(outfits, infits);
}
/* remove .gz or .bz2 suffix, if present (output is not gzipped) */
namelen = strlen(outfits);
if (namelen >= 3 && !strcmp(".gz", outfits + namelen - 3) ) {
outfits[namelen - 3] = '\0';
}
else if (namelen >= 4 && !strcmp(".bz2", outfits + namelen - 4)) {
outfits[namelen - 4] = '\0';
}
/* check for .fz suffix that is sometimes required */
/* and remove it if present */
if (infits[0] != '-') { /* if not reading from stdin stream */
namelen = strlen(outfits);
if (namelen>=3 && !strcmp(".fz", outfits + namelen - 3) ) { /* suffix is present */
outfits[namelen - 3] = '\0';
} else if (fpptr->delete_suffix) { /* required suffix is missing */
fp_msg ("Error: input compressed file "); fp_msg (infits);
fp_msg ("\n does not have the default .fz suffix.\n");
fp_noop (); exit (-1);
}
}
/* if infits != outfits, make sure outfits doesn't already exist */
if (strcmp(infits, outfits)) {
if (fp_access (outfits) == 0) {
fp_msg ("Error: output file already exists:\n "); fp_msg (outfits);
fp_msg ("\n "); fp_noop (); exit (-1);
}
}
/* if gzipping the output, make sure .gz file doesn't exist */
if (fpptr->do_gzip_file) {
if (strlen(outfits)+3 > SZ_STR-1)
{
fp_msg ("Error: output file name too long:\n "); fp_msg (outfits);
fp_msg ("\n "); fp_noop (); exit (-1);
}
strcat(outfits, ".gz");
if (fp_access (outfits) == 0) {
fp_msg ("Error: output file already exists:\n "); fp_msg (outfits);
fp_msg ("\n "); fp_noop (); exit (-1);
}
namelen = strlen(outfits);
outfits[namelen - 3] = '\0'; /* remove the .gz suffix again */
}
} else {
/* ********** This section applies to fpack ************ */
/* check that input file exists */
if (infits[0] != '-') { /* if not reading from stdin stream */
if (fp_access (infits) != 0) { /* if not, then check if */
if (strlen(infits)+3 > SZ_STR-1)
{
fp_msg ("Error: input file name too long:\n "); fp_msg (infits);
fp_msg ("\n "); fp_noop (); exit (-1);
}
strcat(infits, ".gz"); /* a gzipped version exsits */
if (fp_access (infits) != 0) {
namelen = strlen(infits);
infits[namelen - 3] = '\0'; /* remove the .gz suffix */
fp_msg ("Error: can't find or read input file "); fp_msg (infits);
fp_msg ("\n"); fp_noop (); exit (-1);
}
}
}
/* make sure the file to pack does not already have a .fz suffix */
namelen = strlen(infits);
if (namelen>=3 && !strcmp(".fz", infits + namelen - 3) ) {
fp_msg ("Error: fpack input file already has '.fz' suffix\n" ); fp_msg (infits);
fp_msg ("\n"); fp_noop (); exit (-1);
}
/* if writing to stdout, or just testing the files, then we are all done */
if (fpptr->to_stdout || fpptr->test_all) {
continue;
}
if (fpptr->outfile[0]) { /* user specified output file name */
nfiles++;
if (nfiles > 1) {
fp_msg("Error: cannot use same output file name for multiple files:\n ");
fp_msg(fpptr->outfile);
fp_msg ("\n"); fp_noop (); exit (-1);
}
/* check that output file doesn't exist */
if (fp_access (fpptr->outfile) == 0) {
fp_msg ("Error: output file already exists:\n ");
fp_msg (fpptr->outfile);
fp_msg ("\n "); fp_noop (); exit (-1);
}
continue;
}
/* construct output file name */
if (infits[0] == '-') {
strcpy(outfits, "input.fits");
} else {
strcpy(outfits, infits);
}
/* remove .gz suffix, if present (output is not gzipped) */
/* do the same if compression suffix is bz2 */
namelen = strlen(outfits);
if (namelen >=3 && !strcmp(".gz", outfits + namelen - 3) ) {
outfits[namelen - 3] = '\0';
}
else if (namelen >= 4 && !strcmp(".bz2", outfits + namelen - 4)) {
outfits[namelen - 4] = '\0';
}
/* remove .imh suffix (IRAF format image), and replace with .fits */
namelen = strlen(outfits);
if (namelen >=4 && !strcmp(".imh", outfits + namelen - 4) ) {
outfits[namelen - 4] = '\0';
if (strlen(outfits) == SZ_STR-5)
strcat(outfits, ".fit");
else
strcat(outfits, ".fits");
}
/* If not clobbering the input file, add .fz suffix to output name */
if (! fpptr->clobber)
{
if (strlen(outfits) > SZ_STR-4)
{
fp_msg ("Error: output file name too long:\n "); fp_msg (outfits);
fp_msg ("\n "); fp_noop (); exit (-1);
}
else
strcat(outfits, ".fz");
}
/* if infits != outfits, make sure outfits doesn't already exist */
if (strcmp(infits, outfits)) {
if (fp_access (outfits) == 0) {
fp_msg ("Error: output file already exists:\n "); fp_msg (outfits);
fp_msg ("\n "); fp_noop (); exit (-1);
}
}
} /* end of fpack section */
}
fpptr->preflight_checked++;
return(0);
}
/*--------------------------------------------------------------------------*/
/* must run fp_preflight() before fp_loop()
*/
int fp_loop (int argc, char *argv[], int unpack, fpstate fpvar)
{
char infits[SZ_STR], outfits[SZ_STR];
char temp[SZ_STR], answer[30];
char valchar[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.#()+,-_@[]/^{}";
int ichar=0, outlen=0, iarg, islossless, namelen, iraf_infile = 0, status = 0, ifail;
if (fpvar.initialized != FP_INIT_MAGIC) {
fp_msg ("Error: internal initialization error\n"); exit (-1);
} else if (! fpvar.preflight_checked) {
fp_msg ("Error: internal preflight error\n"); exit (-1);
}
if (fpvar.test_all && fpvar.outfile[0]) {
outreport = fopen(fpvar.outfile, "w");
fprintf(outreport," Filename Extension BITPIX NAXIS1 NAXIS2 Size N_nulls Minval Maxval Mean Sigm Noise1 Noise2 Noise3 Noise5 T_whole T_rowbyrow ");
fprintf(outreport,"[Comp_ratio, Pack_cpu, Unpack_cpu, Lossless readtimes] (repeated for Rice, Hcompress, and GZIP)\n");
}
tempfilename[0] = '\0';
tempfilename2[0] = '\0';
tempfilename3[0] = '\0';
/* set up signal handler to delete temporary file on abort */
#ifdef SIGINT
if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
(void) signal(SIGINT, abort_fpack);
}
#endif
#ifdef SIGTERM
if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
(void) signal(SIGTERM, abort_fpack);
}
#endif
#ifdef SIGHUP
if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
(void) signal(SIGHUP, abort_fpack);
}
#endif
for (iarg=fpvar.firstfile; iarg < argc; iarg++) {
temp[0] = '\0';
outfits[0] = '\0';
islossless = 1;
strncpy (infits, argv[iarg], SZ_STR - 1);
infits[SZ_STR-1]=0;
if (unpack) {
/* ********** This section applies to funpack ************ */
/* find input file */
if (infits[0] != '-') { /* if not reading from stdin stream */
if (fp_access (infits) != 0) { /* if not, then */
strcat(infits, ".fz"); /* a .fz version must exsit */
/* fp_preflight already checked for enough size to add '.fz' */
}
}
if (fpvar.to_stdout) {
strcpy(outfits, "-");
} else if (fpvar.outfile[0]) { /* user specified output file name */
strcpy(outfits, fpvar.outfile);
} else {
/* construct output file name */
if (fpvar.prefix[0]) {
/* fp_preflight already checked this */
strcpy(outfits,fpvar.prefix);
}
/* construct output file name */
if (infits[0] == '-') {
strcpy(outfits, "output.fits");
} else {
strcat(outfits, infits);
}
/* remove .gz suffix, if present (output is not gzipped) */
namelen = strlen(outfits);
if (namelen >= 3 && !strcmp(".gz", outfits + namelen - 3) ) {
outfits[namelen - 3] = '\0';
}
else if (namelen >= 4 && !strcmp(".bz2", outfits + namelen - 4)) {
outfits[namelen - 4] = '\0';
}
/* check for .fz suffix that is sometimes required */
/* and remove it if present */
namelen = strlen(outfits);
if (namelen >= 3 && !strcmp(".fz", outfits + namelen - 3) ) { /* suffix is present */
outfits[namelen - 3] = '\0';
}
}
} else {
/* ********** This section applies to fpack ************ */
if (fpvar.to_stdout) {
strcpy(outfits, "-");
} else if (! fpvar.test_all) {
if (fpvar.outfile[0]) { /* user specified output file name */
strcpy(outfits, fpvar.outfile);
}
else {
/* construct output file name */
if (infits[0] == '-') {
strcpy(outfits, "input.fits");
} else {
strcpy(outfits, infits);
}
/* Remove .gz suffix, if present (output is not gzipped).
Do the same for .bz2 */
namelen = strlen(outfits);
if (namelen >= 3 && !strcmp(".gz", outfits + namelen - 3) ) {
outfits[namelen - 3] = '\0';
}
else if (namelen >= 4 && !strcmp(".bz2", outfits + namelen - 4)) {
outfits[namelen - 4] = '\0';
}
/* remove .imh suffix (IRAF format image), and replace with .fits */
namelen = strlen(outfits);
if (namelen >= 4 && !strcmp(".imh", outfits + namelen - 4) ) {
outfits[namelen - 4] = '\0';
if (strlen(outfits) == SZ_STR-5)
strcat(outfits, ".fit");
else
strcat(outfits, ".fits");
iraf_infile = 1; /* this is an IRAF format input file */
/* change the output name to "NAME.fits.fz" */
}
/* If not clobbering the input file, add .fz suffix to output name */
if (! fpvar.clobber)
strcat(outfits, ".fz");
}
}
}
strncpy(temp, outfits, SZ_STR-1);
temp[SZ_STR-1]=0;
if (infits[0] != '-') { /* if not reading from stdin stream */
if (!strcmp(infits, outfits) ) { /* are input and output names the same? */
/* clobber the input file with the output file with the same name */
if (! fpvar.clobber) {
fp_msg ("\nError: must use -F flag to clobber input file.\n");
exit (-1);
}
/* create temporary file name in the output directory (same as input directory)*/
fp_tmpnam("Tmp1", infits, outfits);
strcpy(tempfilename, outfits); /* store temp file name, in case of abort */
}
}
/* *************** now do the real work ********************* */
if (fpvar.verbose && ! fpvar.to_stdout)
printf("%s ", infits);
if (fpvar.test_all) { /* compare all the algorithms */
/* create 2 temporary file names, in the CWD */
fp_tmpnam("Tmpfile1", "", tempfilename);
fp_tmpnam("Tmpfile2", "", tempfilename2);
fp_test (infits, tempfilename, tempfilename2, fpvar);
remove(tempfilename);
tempfilename[0] = '\0'; /* clear the temp file name */
remove(tempfilename2);
tempfilename2[0] = '\0';
continue;
} else if (unpack) {
if (fpvar.to_stdout) {
/* unpack the input file to the stdout stream */
fp_unpack (infits, outfits, fpvar);
} else {
/* unpack to temporary file, so other tasks can't open it until it is renamed */
/* create temporary file name, in the output directory */
fp_tmpnam("Tmp2", outfits, tempfilename2);
/* unpack the input file to the temporary file */
fp_unpack (infits, tempfilename2, fpvar);
/* rename the temporary file to it's real name */
ifail = rename(tempfilename2, outfits);
if (ifail) {
fp_msg("Failed to rename temporary file name:\n ");
fp_msg(tempfilename2);
fp_msg(" -> ");
fp_msg(outfits);
fp_msg("\n");
exit (-1);
} else {
tempfilename2[0] = '\0'; /* clear temporary file name */
}
}
} else {
fp_pack (infits, outfits, fpvar, &islossless);
}
if (fpvar.to_stdout) {
continue;
}
/* ********** clobber and/or delete files, if needed ************** */
if (!strcmp(infits, temp) && fpvar.clobber ) {
if (!islossless && ! fpvar.do_not_prompt) {
fp_msg ("\nFile ");
fp_msg (infits);
fp_msg ("\nwas compressed with a LOSSY method. Overwrite the\n");
fp_msg ("original file with the compressed version? (Y/N) ");
fgets(answer, 29, stdin);
if (answer[0] != 'Y' && answer[0] != 'y') {
fp_msg ("\noriginal file NOT overwritten!\n");
remove(outfits);
continue;
}
}
if (iraf_infile) { /* special case of deleting an IRAF format header and pixel file */
if (fits_delete_iraf_file(infits, &status)) {
fp_msg("\nError deleting IRAF .imh and .pix files.\n");
fp_msg(infits); fp_msg ("\n"); exit (-1);
}
}
#if defined(unix) || defined(__unix__) || defined(__unix)
/* rename clobbers input on Unix platforms */
if (rename (outfits, temp) != 0) {
fp_msg ("\nError renaming tmp file to ");
fp_msg (temp); fp_msg ("\n"); exit (-1);
}
#else
/* rename DOES NOT clobber existing files on Windows platforms */
/* so explicitly remove any existing file before renaming the file */
remove(temp);
if (rename (outfits, temp) != 0) {
fp_msg ("\nError renaming tmp file to ");
fp_msg (temp); fp_msg ("\n"); exit (-1);
}
#endif
tempfilename[0] = '\0'; /* clear temporary file name */
strcpy(outfits, temp);
} else if (fpvar.clobber || fpvar.delete_input) { /* delete the input file */
if (!islossless && !fpvar.do_not_prompt) { /* user did not turn off delete prompt */
fp_msg ("\nFile ");
fp_msg (infits);
fp_msg ("\nwas compressed with a LOSSY method. \n");
fp_msg ("Delete the original file? (Y/N) ");
fgets(answer, 29, stdin);
if (answer[0] != 'Y' && answer[0] != 'y') { /* user abort */
fp_msg ("\noriginal file NOT deleted!\n");
} else {
if (iraf_infile) { /* special case of deleting an IRAF format header and pixel file */
if (fits_delete_iraf_file(infits, &status)) {
fp_msg("\nError deleting IRAF .imh and .pix files.\n");
fp_msg(infits); fp_msg ("\n"); exit (-1);
}
} else if (remove(infits) != 0) { /* normal case of deleting input FITS file */
fp_msg ("\nError deleting input file ");
fp_msg (infits); fp_msg ("\n"); exit (-1);
}
}
} else { /* user said don't prompt, so just delete the input file */
if (iraf_infile) { /* special case of deleting an IRAF format header and pixel file */
if (fits_delete_iraf_file(infits, &status)) {
fp_msg("\nError deleting IRAF .imh and .pix files.\n");
fp_msg(infits); fp_msg ("\n"); exit (-1);
}
} else if (remove(infits) != 0) { /* normal case of deleting input FITS file */
fp_msg ("\nError deleting input file ");
fp_msg (infits); fp_msg ("\n"); exit (-1);
}
}
}
iraf_infile = 0;
if (fpvar.do_gzip_file) { /* gzip the output file */
strcpy(temp, "gzip -1 ");
outlen = strlen(outfits);
if (outlen + 8 > SZ_STR-1)
{
fp_msg("\nError: Output file name is too long.\n");
exit(-1);
}
for (ichar=0; ichar < outlen; ++ichar)
{
if (!strchr(valchar, outfits[ichar]))
{
fp_msg("\n Error: Invalid characters in output file name.\n");
exit(-1);
}
}
strcat(temp,outfits);
system(temp);
strcat(outfits, ".gz"); /* only possibible with funpack */
}
if (fpvar.verbose && ! fpvar.to_stdout)
printf("-> %s\n", outfits);
}
if (fpvar.test_all && fpvar.outfile[0])
fclose(outreport);
return(0);
}
/*--------------------------------------------------------------------------*/
/* fp_pack assumes the output file does not exist (checked by preflight)
*/
int fp_pack (char *infits, char *outfits, fpstate fpvar, int *islossless)
{
fitsfile *infptr, *outfptr;
int stat=0;
fits_open_file (&infptr, infits, READONLY, &stat);
if (stat) { fits_report_error (stderr, stat); exit (stat); }
fits_create_file (&outfptr, outfits, &stat);
if (stat) {
fp_abort_output(infptr, NULL, stat);
}
if (stat) {
fp_abort_output(infptr, outfptr, stat);
}
while (! stat) {
/* LOOP OVER EACH HDU */
fits_set_lossy_int (outfptr, fpvar.int_to_float, &stat);
fits_set_compression_type (outfptr, fpvar.comptype, &stat);
fits_set_tile_dim (outfptr, 6, fpvar.ntile, &stat);
if (fpvar.no_dither)
fits_set_quantize_method(outfptr, -1, &stat);
else
fits_set_quantize_method(outfptr, fpvar.dither_method, &stat);
fits_set_quantize_level (outfptr, fpvar.quantize_level, &stat);
fits_set_dither_offset(outfptr, fpvar.dither_offset, &stat);
fits_set_hcomp_scale (outfptr, fpvar.scale, &stat);
fits_set_hcomp_smooth (outfptr, fpvar.smooth, &stat);
fp_pack_hdu (infptr, outfptr, fpvar, islossless, &stat);
if (fpvar.do_checksums) {
fits_write_chksum (outfptr, &stat);
}
fits_movrel_hdu (infptr, 1, NULL, &stat);
}
if (stat == END_OF_FILE) stat = 0;
/* set checksum for case of newly created primary HDU */
if (fpvar.do_checksums) {
fits_movabs_hdu (outfptr, 1, NULL, &stat);
fits_write_chksum (outfptr, &stat);
}
if (stat) {
fp_abort_output(infptr, outfptr, stat);
}