Repository navigation
Expand file tree
/
Copy pathcompressionmodule.c
More file actions
1323 lines (1149 loc) · 44.7 KB
/
Copy pathcompressionmodule.c
File metadata and controls
1323 lines (1149 loc) · 44.7 KB
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
/* "compression module */
/*****************************************************************************/
/* */
/* The compression software is a python module implemented in C that, when */
/* accessed through the astropy module, supports the storage of compressed */
/* images in FITS binary tables. An n-dimensional image is divided into a */
/* rectangular grid of subimages or 'tiles'. Each tile is then compressed */
/* as a continuous block of data, and the resulting compressed byte stream */
/* is stored in a row of a variable length column in a FITS binary table. */
/* The default tiling pattern treates each row of a 2-dimensional image */
/* (or higher dimensional cube) as a tile, such that each tile contains */
/* NAXIS1 pixels. */
/* */
/* This module contains three functions that are callable from python. The */
/* first is compress_hdu. This function takes an */
/* astropy.io.fits.CompImageHDU object containing the uncompressed image */
/* data and returns the compressed data for all tiles into the */
/* .compressed_data attribute of that HDU. */
/* */
/* The second function is decompress_hdu. It takes an */
/* astropy.io.fits.CompImageHDU object that already has compressed data in */
/* its .compressed_data attribute. It returns the decompressed image data */
/* into the HDU's .data attribute. */
/* */
/* Copyright (C) 2013 Association of Universities for Research in Astronomy */
/* (AURA) */
/* */
/* Redistribution and use in source and binary forms, with or without */
/* modification, are permitted provided that the following conditions are */
/* met: */
/* */
/* 1. Redistributions of source code must retain the above copyright */
/* notice, this list of conditions and the following disclaimer. */
/* */
/* 2. Redistributions in binary form must reproduce the above */
/* copyright notice, this list of conditions and the following */
/* disclaimer in the documentation and/or other materials provided */
/* with the distribution. */
/* */
/* 3. The name of AURA and its representatives may not be used to */
/* endorse or promote products derived from this software without */
/* specific prior written permission. */
/* */
/* THIS SOFTWARE IS PROVIDED BY AURA ``AS IS'' AND ANY EXPRESS OR IMPLIED */
/* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
/* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
/* DISCLAIMED. IN NO EVENT SHALL AURA BE LIABLE FOR ANY DIRECT, INDIRECT, */
/* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, */
/* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS */
/* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND */
/* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR */
/* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE */
/* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH */
/* DAMAGE. */
/* */
/* Some of the source code used by this module was copied and modified from */
/* the FITSIO software that was written by William Pence at the High Energy */
/* Astrophysic Science Archive Research Center (HEASARC) at the NASA Goddard */
/* Space Flight Center. That software contained the following copyright and */
/* warranty notices: */
/* */
/* Copyright (Unpublished--all rights reserved under the copyright laws of */
/* the United States), U.S. Government as represented by the Administrator */
/* of the National Aeronautics and Space Administration. No copyright is */
/* claimed in the United States under Title 17, U.S. Code. */
/* */
/* Permission to freely use, copy, modify, and distribute this software */
/* and its documentation without fee is hereby granted, provided that this */
/* copyright notice and disclaimer of warranty appears in all copies. */
/* */
/* DISCLAIMER: */
/* */
/* THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, */
/* EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, */
/* ANY WARRANTY THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY */
/* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR */
/* PURPOSE, AND FREEDOM FROM INFRINGEMENT, AND ANY WARRANTY THAT THE */
/* DOCUMENTATION WILL CONFORM TO THE SOFTWARE, OR ANY WARRANTY THAT THE */
/* SOFTWARE WILL BE ERROR FREE. IN NO EVENT SHALL NASA BE LIABLE FOR ANY */
/* DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, SPECIAL OR */
/* CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, OR IN ANY WAY */
/* CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, */
/* CONTRACT, TORT , OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY */
/* PERSONS OR PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED */
/* FROM, OR AROSE OUT OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR */
/* SERVICES PROVIDED HEREUNDER." */
/* */
/*****************************************************************************/
/* Include the Python C API */
#include <float.h>
#include <limits.h>
#include <math.h>
#include <string.h>
#include <Python.h>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>
#include <fitsio2.h>
#include "compressionmodule.h"
/* These defaults mirror the defaults in astropy.io.fits.hdu.compressed */
#define DEFAULT_COMPRESSION_TYPE "RICE_1"
#define DEFAULT_QUANTIZE_LEVEL 16.0
#define DEFAULT_HCOMP_SCALE 0
#define DEFAULT_HCOMP_SMOOTH 0
#define DEFAULT_BLOCK_SIZE 32
#define DEFAULT_BYTE_PIX 4
/* Flags to pass to get_header_* functions to control error messages. */
typedef enum {
HDR_NOFLAG = 0,
HDR_FAIL_KEY_MISSING = 1 << 0,
HDR_FAIL_VAL_NEGATIVE = 1 << 1,
} HeaderGetFlags;
/* Report any error based on the status returned from cfitsio. */
void process_status_err(int status)
{
PyObject* except_type;
char err_msg[81];
char def_err_msg[81];
err_msg[0] = '\0';
def_err_msg[0] = '\0';
switch (status) {
case MEMORY_ALLOCATION:
except_type = PyExc_MemoryError;
break;
case OVERFLOW_ERR:
except_type = PyExc_OverflowError;
break;
case BAD_COL_NUM:
strcpy(def_err_msg, "bad column number");
except_type = PyExc_ValueError;
break;
case BAD_PIX_NUM:
strcpy(def_err_msg, "bad pixel number");
except_type = PyExc_ValueError;
break;
case NEG_AXIS:
strcpy(def_err_msg, "negative axis number");
except_type = PyExc_ValueError;
break;
case BAD_DATATYPE:
strcpy(def_err_msg, "bad data type");
except_type = PyExc_TypeError;
break;
case NO_COMPRESSED_TILE:
strcpy(def_err_msg, "no compressed or uncompressed data for tile.");
except_type = PyExc_ValueError;
break;
default:
except_type = PyExc_RuntimeError;
break;
}
if (fits_read_errmsg(err_msg)) {
PyErr_SetString(except_type, err_msg);
} else if (*def_err_msg) {
PyErr_SetString(except_type, def_err_msg);
} else {
PyErr_Format(except_type, "unknown error %i.", status);
}
}
void bitpix_to_datatypes(int bitpix, int* datatype, int* npdatatype) {
/* Given a FITS BITPIX value, returns the appropriate CFITSIO type code and
Numpy type code for that BITPIX into datatype and npdatatype
respectively.
*/
switch (bitpix) {
case BYTE_IMG:
*datatype = TBYTE;
*npdatatype = NPY_UINT8;
break;
case SHORT_IMG:
*datatype = TSHORT;
*npdatatype = NPY_INT16;
break;
case LONG_IMG:
*datatype = TINT;
*npdatatype = NPY_INT32;
break;
case LONGLONG_IMG:
*datatype = TLONGLONG;
*npdatatype = NPY_LONGLONG;
break;
case FLOAT_IMG:
*datatype = TFLOAT;
*npdatatype = NPY_FLOAT;
break;
case DOUBLE_IMG:
*datatype = TDOUBLE;
*npdatatype = NPY_DOUBLE;
break;
default:
PyErr_Format(PyExc_ValueError, "Invalid value for BITPIX: %d",
bitpix);
break;
}
return;
}
int compress_type_from_string(char* zcmptype) {
if (0 == strcmp(zcmptype, "RICE_1")) {
return RICE_1;
} else if (0 == strcmp(zcmptype, "GZIP_1")) {
return GZIP_1;
} else if (0 == strcmp(zcmptype, "GZIP_2")) {
return GZIP_2;
} else if (0 == strcmp(zcmptype, "PLIO_1")) {
return PLIO_1;
} else if (0 == strcmp(zcmptype, "HCOMPRESS_1")) {
return HCOMPRESS_1;
}
#ifdef CFITSIO_SUPPORTS_SUBTRACTIVE_DITHER_2
/* CFITSIO adds a compression type alias for RICE_1 compression
as a flag for using subtractive_dither_2 */
else if (0 == strcmp(zcmptype, "RICE_ONE")) {
return RICE_1;
}
#endif
else {
PyErr_Format(PyExc_ValueError, "Unrecognized compression type: %s",
zcmptype);
return -1;
}
}
PyObject *
get_header_value(PyObject* header, const char* key, HeaderGetFlags flags) {
PyObject* hdrkey;
PyObject* hdrval;
hdrkey = PyUnicode_FromString(key);
if (hdrkey == NULL) {
return NULL;
}
hdrval = PyObject_GetItem(header, hdrkey);
Py_DECREF(hdrkey);
if ((flags & HDR_FAIL_KEY_MISSING) == 0) {
/* Normally we have a default so we want to ignore the exception in
any case. But if the flag was given this step must be skipped. */
PyErr_Clear();
}
return hdrval;
}
// TODO: It might be possible to simplify these further by making the
// conversion function (eg. PyString_AsString) an argument to a macro or
// something, but I'm not sure yet how easy it is to generalize the error
// handling
/* The get_header_* functions resemble "Header.get" where "def" is the default
value, "keyword" is a string representing the header-key and the result is
stored in "val".
The function returns 0 on success, 1 if the header didn't have the keyword
and the default was applied and -1 (with an exception set) if an Exception
happened (like a MemoryError or Overflow).
*/
#define GET_HEADER_SUCCESS 0
#define GET_HEADER_DEFAULT_USED 1
#define GET_HEADER_FAILED -1
int get_header_string(PyObject* header, const char* keyword, char* val,
const char* def, HeaderGetFlags flags) {
/* nonnegative doesn't make sense for strings*/
assert(!(flags & HDR_FAIL_VAL_NEGATIVE));
PyObject* keyval = get_header_value(header, keyword, flags);
if (keyval == NULL) {
strncpy(val, def, 72);
return PyErr_Occurred() ? GET_HEADER_FAILED : GET_HEADER_DEFAULT_USED;
}
PyObject* tmp = PyUnicode_AsLatin1String(keyval);
// FITS header values should always be ASCII, but Latin1 is on the
// safe side
Py_DECREF(keyval);
if (tmp == NULL) {
/* could always fail to allocate the memory or such like. */
return GET_HEADER_FAILED;
}
strncpy(val, PyBytes_AsString(tmp), 72);
Py_DECREF(tmp);
return GET_HEADER_SUCCESS;
}
int get_header_long(PyObject* header, const char* keyword, long* val, long def,
HeaderGetFlags flags) {
PyObject* keyval = get_header_value(header, keyword, flags);
if (keyval == NULL) {
*val = def;
return PyErr_Occurred() ? GET_HEADER_FAILED : GET_HEADER_DEFAULT_USED;
}
long tmp = PyLong_AsLong(keyval);
Py_DECREF(keyval);
if (PyErr_Occurred()) {
return GET_HEADER_FAILED;
}
if ((flags & HDR_FAIL_VAL_NEGATIVE) && (tmp < 0)) {
PyErr_Format(PyExc_ValueError, "%s should not be negative.", keyword);
return GET_HEADER_FAILED;
}
*val = tmp;
return GET_HEADER_SUCCESS;
}
int get_header_int(PyObject* header, const char* keyword, int* val, int def,
HeaderGetFlags flags) {
long tmp;
int ret = get_header_long(header, keyword, &tmp, def, flags);
if (ret == GET_HEADER_SUCCESS) {
if (tmp >= INT_MIN && tmp <= INT_MAX) {
*val = (int) tmp;
} else {
PyErr_Format(PyExc_OverflowError, "Cannot convert %ld to C 'int'", tmp);
ret = GET_HEADER_FAILED;
}
}
return ret;
}
int get_header_double(PyObject* header, const char* keyword, double* val,
double def, HeaderGetFlags flags) {
/* nonnegative isn't currently used for doubles/floats. But if needed one
could simply remove the assert again and implement the negative check. */
assert(!(flags & HDR_FAIL_VAL_NEGATIVE));
PyObject* keyval = get_header_value(header, keyword, flags);
if (keyval == NULL) {
*val = def;
return PyErr_Occurred() ? GET_HEADER_FAILED : GET_HEADER_DEFAULT_USED;
}
double tmp = PyFloat_AsDouble(keyval);
Py_DECREF(keyval);
if (PyErr_Occurred()) {
return GET_HEADER_FAILED;
}
*val = tmp;
return GET_HEADER_SUCCESS;
}
int get_header_float(PyObject* header, const char* keyword, float* val,
float def, HeaderGetFlags flags) {
double tmp;
int ret = get_header_double(header, keyword, &tmp, def, flags);
if (ret == GET_HEADER_SUCCESS) {
if (tmp == 0.0 || (fabs(tmp) >= FLT_MIN && fabs(tmp) <= FLT_MAX)) {
*val = (float) tmp;
} else {
PyErr_SetString(PyExc_OverflowError,
"Cannot convert 'double' to 'float'");
ret = GET_HEADER_FAILED;
}
}
return ret;
}
int get_header_longlong(PyObject* header, const char* keyword, long long* val,
long long def, HeaderGetFlags flags) {
PyObject* keyval = get_header_value(header, keyword, flags);
if (keyval == NULL) {
*val = def;
return PyErr_Occurred() ? GET_HEADER_FAILED : GET_HEADER_DEFAULT_USED;
}
long long tmp = PyLong_AsLongLong(keyval);
Py_DECREF(keyval);
if (PyErr_Occurred()) {
return GET_HEADER_FAILED;
}
if ((flags & HDR_FAIL_VAL_NEGATIVE) && (tmp < 0)) {
PyErr_Format(PyExc_ValueError, "%s should not be negative.", keyword);
return GET_HEADER_FAILED;
}
*val = tmp;
return GET_HEADER_SUCCESS;
}
void tcolumns_from_header(fitsfile* fileptr, PyObject* header,
tcolumn** columns) {
// Creates the array of tcolumn structures from the table column keywords
// read from the astropy.io.fits.Header object; caller is responsible for
// freeing the memory allocated for this array
tcolumn* column;
char tkw[9];
int tfields;
char ttype[72];
char tform[72];
int dtcode;
long trepeat;
long twidth;
long long totalwidth;
int status = 0;
int idx;
if (get_header_int(header, "TFIELDS", &tfields, 0, HDR_FAIL_VAL_NEGATIVE) == GET_HEADER_FAILED) {
return;
}
/* To avoid issues in the loop we need to limit the number of TFIELDs to
999. Otherwise we would exceed the maximum length of the keyword name of
8. This could lead to multiple accesses of the same header keyword with
snprintf because we limit it to 8 characters + null-termination. */
if (tfields > 999) {
PyErr_SetString(PyExc_ValueError, "The TFIELDS value exceeds 999.");
return;
}
// This used to use PyMem_New, but don't do that; CFITSIO will later
// free() this object when the file is closed, so just use malloc here
// *columns = column = PyMem_New(tcolumn, (size_t) tfields);
*columns = column = calloc((size_t) tfields, sizeof(tcolumn));
if (column == NULL) {
PyErr_SetString(PyExc_MemoryError,
"Couldn't allocate memory for columns.");
return;
}
for (idx = 1; idx <= tfields; idx++, column++) {
/* set some invalid defaults */
column->ttype[0] = '\0';
column->tbcol = 0;
column->tdatatype = -9999; /* this default used by cfitsio */
column->trepeat = 1;
column->strnull[0] = '\0';
column->tform[0] = '\0';
column->twidth = 0;
snprintf(tkw, 9, "TTYPE%u", idx);
if (get_header_string(header, tkw, ttype, "", HDR_NOFLAG) == GET_HEADER_FAILED) {
return;
}
strncpy(column->ttype, ttype, 69);
column->ttype[69] = '\0';
snprintf(tkw, 9, "TFORM%u", idx);
if (get_header_string(header, tkw, tform, "", HDR_NOFLAG) == GET_HEADER_FAILED) {
return;
}
strncpy(column->tform, tform, 9);
column->tform[9] = '\0';
fits_binary_tform(tform, &dtcode, &trepeat, &twidth, &status);
if (status != 0) {
process_status_err(status);
return;
}
column->tdatatype = dtcode;
column->trepeat = trepeat;
column->twidth = twidth;
snprintf(tkw, 9, "TSCAL%u", idx);
if (get_header_double(header, tkw, &(column->tscale), 1.0, HDR_NOFLAG) == GET_HEADER_FAILED) {
return;
}
snprintf(tkw, 9, "TZERO%u", idx);
if (get_header_double(header, tkw, &(column->tzero), 0.0, HDR_NOFLAG) == GET_HEADER_FAILED) {
return;
}
snprintf(tkw, 9, "TNULL%u", idx);
if (get_header_longlong(header, tkw, &(column->tnull), NULL_UNDEFINED, HDR_NOFLAG) == GET_HEADER_FAILED) {
return;
}
}
fileptr->Fptr->tableptr = *columns;
fileptr->Fptr->tfield = tfields;
// This routine from CFITSIO calculates the byte offset of each column
// and stores it in the column->tbcol field
ffgtbc(fileptr, &totalwidth, &status);
if (status != 0) {
process_status_err(status);
}
return;
}
void configure_compression(fitsfile* fileptr, PyObject* header) {
/* Configure the compression-related elements in the fitsfile struct
using values in the FITS header. */
FITSfile* Fptr;
int tfields;
tcolumn* columns;
char keyword[9];
char zname[72];
int znaxis;
char tmp[72];
float version;
int idx;
Fptr = fileptr->Fptr;
tfields = Fptr->tfield;
columns = Fptr->tableptr;
int tmp_retval;
// Get the ZBITPIX header value; if this is missing we're in trouble
if (get_header_int(header, "ZBITPIX", &(Fptr->zbitpix), 0, HDR_FAIL_KEY_MISSING) != GET_HEADER_SUCCESS) {
return;
}
// By default assume there is no ZBLANK column and check for ZBLANK or
// BLANK in the header
Fptr->cn_zblank = Fptr->cn_zzero = Fptr->cn_zscale = -1;
Fptr->cn_uncompressed = 0;
#ifdef CFITSIO_SUPPORTS_GZIPDATA
Fptr->cn_gzip_data = 0;
#endif
// Check for a ZBLANK, ZZERO, ZSCALE, and
// UNCOMPRESSED_DATA/GZIP_COMPRESSED_DATA columns in the compressed data
// table
for (idx = 0; idx < tfields; idx++) {
if (0 == strncmp(columns[idx].ttype, "UNCOMPRESSED_DATA", 18)) {
Fptr->cn_uncompressed = idx + 1;
#ifdef CFITSIO_SUPPORTS_GZIPDATA
} else if (0 == strncmp(columns[idx].ttype,
"GZIP_COMPRESSED_DATA", 21)) {
Fptr->cn_gzip_data = idx + 1;
#endif
} else if (0 == strncmp(columns[idx].ttype, "ZSCALE", 7)) {
Fptr->cn_zscale = idx + 1;
} else if (0 == strncmp(columns[idx].ttype, "ZZERO", 6)) {
Fptr->cn_zzero = idx + 1;
} else if (0 == strncmp(columns[idx].ttype, "ZBLANK", 7)) {
Fptr->cn_zblank = idx + 1;
}
}
Fptr->zblank = 0;
if (Fptr->cn_zblank < 1) {
// No ZBLANK column--check the ZBLANK and BLANK heard keywords
switch (get_header_int(header, "ZBLANK", &(Fptr->zblank), 0, HDR_NOFLAG)) {
case GET_HEADER_FAILED:
return;
case GET_HEADER_DEFAULT_USED:
// ZBLANK keyword not found
if (get_header_int(header, "BLANK", &(Fptr->zblank), 0, HDR_NOFLAG) == GET_HEADER_FAILED) {
return;
}
break;
default:
break;
}
}
Fptr->zscale = 1.0;
if (Fptr->cn_zscale < 1) {
switch (get_header_double(header, "ZSCALE", &(Fptr->zscale), 1.0, HDR_NOFLAG)) {
case GET_HEADER_FAILED:
return;
case GET_HEADER_DEFAULT_USED:
Fptr->cn_zscale = 0;
break;
default:
break;
}
}
Fptr->cn_bscale = Fptr->zscale;
Fptr->zzero = 0.0;
if (Fptr->cn_zzero < 1) {
switch (get_header_double(header, "ZZERO", &(Fptr->zzero), 0.0, HDR_NOFLAG)) {
case GET_HEADER_FAILED:
return;
case GET_HEADER_DEFAULT_USED:
Fptr->cn_zzero = 0;
break;
default:
break;
}
}
Fptr->cn_bzero = Fptr->zzero;
if (get_header_string(header, "ZCMPTYPE", tmp, DEFAULT_COMPRESSION_TYPE, HDR_NOFLAG) == GET_HEADER_FAILED) {
return;
}
strncpy(Fptr->zcmptype, tmp, 11);
Fptr->zcmptype[strlen(tmp)] = '\0';
Fptr->compress_type = compress_type_from_string(Fptr->zcmptype);
if (PyErr_Occurred()) {
return;
}
if (get_header_int(header, "ZNAXIS", &znaxis, 0, HDR_NOFLAG) == GET_HEADER_FAILED) {
return;
}
Fptr->zndim = znaxis;
if (znaxis > MAX_COMPRESS_DIM) {
// The CFITSIO compression code currently only supports up to 6
// dimensions by default.
znaxis = MAX_COMPRESS_DIM;
}
Fptr->tilerow = NULL;
Fptr->maxtilelen = 1;
for (idx = 1; idx <= znaxis; idx++) {
snprintf(keyword, 9, "ZNAXIS%u", idx);
if (get_header_long(header, keyword, Fptr->znaxis + idx - 1, 0, HDR_NOFLAG) == GET_HEADER_FAILED) {
return;
}
snprintf(keyword, 9, "ZTILE%u", idx);
if (get_header_long(header, keyword, Fptr->tilesize + idx - 1, 0, HDR_NOFLAG) == GET_HEADER_FAILED) {
return;
}
Fptr->maxtilelen *= Fptr->tilesize[idx - 1];
}
// Set some more default compression options
Fptr->rice_blocksize = DEFAULT_BLOCK_SIZE;
Fptr->rice_bytepix = DEFAULT_BYTE_PIX;
Fptr->quantize_level = DEFAULT_QUANTIZE_LEVEL;
Fptr->hcomp_smooth = DEFAULT_HCOMP_SMOOTH;
Fptr->hcomp_scale = DEFAULT_HCOMP_SCALE;
// Now process the ZVALn keywords
idx = 1;
while (1) {
snprintf(keyword, 9, "ZNAME%u", idx);
// Assumes there are no gaps in the ZNAMEn keywords; this same
// assumption was made in the Python code. This could be done slightly
// more flexibly by using a wildcard slice of the header
tmp_retval = get_header_string(header, keyword, zname, "", HDR_NOFLAG);
if (tmp_retval == GET_HEADER_FAILED) {
return;
} else if (tmp_retval == 1) {
break;
}
snprintf(keyword, 9, "ZVAL%u", idx);
if (Fptr->compress_type == RICE_1) {
if (0 == strcmp(zname, "BLOCKSIZE")) {
if (get_header_int(header, keyword, &(Fptr->rice_blocksize),
DEFAULT_BLOCK_SIZE, HDR_NOFLAG) == GET_HEADER_FAILED) {
return;
}
} else if (0 == strcmp(zname, "BYTEPIX")) {
if (get_header_int(header, keyword, &(Fptr->rice_bytepix),
DEFAULT_BYTE_PIX, HDR_NOFLAG) == GET_HEADER_FAILED) {
return;
}
}
} else if (Fptr->compress_type == HCOMPRESS_1) {
if (0 == strcmp(zname, "SMOOTH")) {
if (get_header_int(header, keyword, &(Fptr->hcomp_smooth),
DEFAULT_HCOMP_SMOOTH, HDR_NOFLAG) == GET_HEADER_FAILED) {
return;
}
} else if (0 == strcmp(zname, "SCALE")) {
if (get_header_float(header, keyword, &(Fptr->hcomp_scale),
DEFAULT_HCOMP_SCALE, HDR_NOFLAG) == GET_HEADER_FAILED) {
return;
}
}
}
if (Fptr->zbitpix < 0 && 0 == strcmp(zname, "NOISEBIT")) {
if (get_header_float(header, keyword, &(Fptr->quantize_level),
DEFAULT_QUANTIZE_LEVEL, HDR_NOFLAG) == GET_HEADER_FAILED) {
return;
}
if (Fptr->quantize_level == 0.0) {
/* NOISEBIT == 0 is equivalent to no quantize */
Fptr->quantize_level = NO_QUANTIZE;
}
}
idx++;
}
/* The ZQUANTIZ keyword determines the quantization algorithm; NO_QUANTIZE
implies lossless compression */
tmp_retval = get_header_string(header, "ZQUANTIZ", tmp, "", HDR_NOFLAG);
if (tmp_retval == GET_HEADER_FAILED) {
return;
} else if (tmp_retval == GET_HEADER_SUCCESS) {
/* Ugh; the fact that cfitsio defines its version as a float makes
preprocessor comparison impossible */
fits_get_version(&version);
if ((version >= CFITSIO_LOSSLESS_COMP_SUPPORTED_VERS) &&
(0 == strcmp(tmp, "NONE"))) {
Fptr->quantize_level = NO_QUANTIZE;
} else if (0 == strcmp(tmp, "SUBTRACTIVE_DITHER_1")) {
#ifdef CFITSIO_SUPPORTS_SUBTRACTIVE_DITHER_2
// Added in CFITSIO 3.35, this also changed the name of the
// quantize_dither struct member to quantize_method
Fptr->quantize_method = SUBTRACTIVE_DITHER_1;
} else if (0 == strcmp(tmp, "SUBTRACTIVE_DITHER_2")) {
Fptr->quantize_method = SUBTRACTIVE_DITHER_2;
} else {
Fptr->quantize_method = NO_DITHER;
}
} else {
Fptr->quantize_method = NO_DITHER;
}
if (Fptr->quantize_method != NO_DITHER) {
switch (get_header_int(header, "ZDITHER0", &(Fptr->dither_seed), 0, HDR_NOFLAG)) {
case GET_HEADER_FAILED:
return;
case GET_HEADER_DEFAULT_USED: // ZDITHER0 keyword not found
Fptr->dither_seed = 0;
Fptr->request_dither_seed = 0;
break;
default:
break;
}
}
#else
Fptr->quantize_dither = SUBTRACTIVE_DITHER_1;
} else {
Fptr->quantize_dither = NO_DITHER;
}
} else {
Fptr->quantize_dither = NO_DITHER;
}
if (Fptr->quantize_dither != NO_DITHER) {
switch (get_header_int(header, "ZDITHER0", &(Fptr->dither_offset), 0, HDR_NOFLAG)) {
case GET_HEADER_FAILED:
return;
case GET_HEADER_DEFAULT_USED: // ZDITHER0 keyword no found
/* TODO: Find out if that's actually working and not invalid... */
Fptr->dither_offset = 0;
Fptr->request_dither_offset = 0;
break;
default:
break;
}
}
#endif
Fptr->compressimg = 1;
Fptr->maxelem = imcomp_calc_max_elem(Fptr->compress_type,
Fptr->maxtilelen,
Fptr->zbitpix,
Fptr->rice_blocksize);
Fptr->cn_compressed = 1;
return;
}
void init_output_buffer(PyObject* hdu, void** buf, size_t* bufsize) {
// Determines a good size for the output data buffer and allocates
// memory for it, returning the address and size of the allocated
// memory into **buf and *bufsize respectively.
PyObject* header = NULL;
char keyword[9];
char tmp[72];
int znaxis;
int compress_type;
int zbitpix;
int rice_blocksize = 0;
long long rowlen;
long long nrows;
long maxelem;
long tilelen;
unsigned long maxtilelen = 1;
int idx;
header = PyObject_GetAttrString(hdu, "_header");
if (header == NULL) {
return;
}
if (get_header_int(header, "ZNAXIS", &znaxis, 0,
HDR_FAIL_KEY_MISSING | HDR_FAIL_VAL_NEGATIVE) != GET_HEADER_SUCCESS) {
goto fail;
}
if (znaxis > 999) {
PyErr_SetString(PyExc_ValueError, "ZNAXIS is greater than 999.");
goto fail;
}
for (idx = 1; idx <= znaxis; idx++) {
snprintf(keyword, 9, "ZTILE%u", idx);
if (get_header_long(header, keyword, &tilelen, 1, HDR_NOFLAG) == GET_HEADER_FAILED) {
goto fail;
}
maxtilelen *= tilelen;
}
if (get_header_string(header, "ZCMPTYPE", tmp, DEFAULT_COMPRESSION_TYPE, HDR_NOFLAG) == GET_HEADER_FAILED) {
goto fail;
}
compress_type = compress_type_from_string(tmp);
if (PyErr_Occurred()) {
goto fail;
}
if (compress_type == RICE_1) {
if (get_header_int(header, "ZVAL1", &rice_blocksize, 0, HDR_NOFLAG) == GET_HEADER_FAILED) {
goto fail;
}
}
/* Because we calculate the size of the buffer based on these values they
must not be negative. Otherwise it would wrap around during the casting
to size_t and give huge values. */
if (get_header_longlong(header, "NAXIS1", &rowlen, 0, HDR_FAIL_VAL_NEGATIVE) == GET_HEADER_FAILED) {
goto fail;
}
if (get_header_longlong(header, "NAXIS2", &nrows, 0, HDR_FAIL_VAL_NEGATIVE) == GET_HEADER_FAILED) {
goto fail;
}
// Get the ZBITPIX header value; if this is missing we're in trouble
if (get_header_int(header, "ZBITPIX", &zbitpix, 0, HDR_FAIL_KEY_MISSING) != GET_HEADER_SUCCESS) {
goto fail;
}
maxelem = imcomp_calc_max_elem(compress_type, maxtilelen, zbitpix,
rice_blocksize);
*bufsize = ((size_t) (rowlen * nrows) + (nrows * maxelem));
if (*bufsize < IOBUFLEN) {
// We must have a full FITS block at a minimum
*bufsize = IOBUFLEN;
} else if (*bufsize % IOBUFLEN != 0) {
// Still make sure to pad out to a multiple of 2880 byte blocks
// otherwise CFITSIO can get read errors when it tries to read
// a partial block that goes past the end of the file
*bufsize += ((size_t) (IOBUFLEN - (*bufsize % IOBUFLEN)));
}
*buf = calloc(*bufsize, sizeof(char));
if (*buf == NULL) {
// Checking if calloc failed.
PyErr_SetString(PyExc_MemoryError,
"Failed to allocate memory for output data buffer.");
goto fail;
}
fail:
Py_DECREF(header);
return;
}
void get_hdu_data_base(PyObject* hdu, void** buf, size_t* bufsize) {
// Given a pointer to an HDU object, returns a pointer to the deepest base
// array of that HDU's data array into **buf, and the size of that array
// into *bufsize.
PyArrayObject* data = NULL;
PyArrayObject* base;
PyArrayObject* tmp;
data = (PyArrayObject*) PyObject_GetAttrString(hdu, "compressed_data");
if (data == NULL) {
goto fail;
}
// Walk the array data bases until we find the lowest ndarray base; for
// CompImageHDUs there should always be at least one contiguous byte array
// allocated for the table and its heap
if (!PyObject_TypeCheck(data, &PyArray_Type)) {
PyErr_SetString(PyExc_TypeError,
"CompImageHDU.compressed_data must be a numpy.ndarray");
goto fail;
}
tmp = base = data;
while (PyObject_TypeCheck((PyObject*) tmp, &PyArray_Type)) {
base = tmp;
*bufsize = (size_t) PyArray_NBYTES(base);
tmp = (PyArrayObject*) PyArray_BASE(base);
if (tmp == NULL) {
break;
}
}
*buf = PyArray_DATA(base);
fail:
Py_XDECREF(data);
return;
}
void open_from_hdu(fitsfile** fileptr, void** buf, size_t* bufsize,
PyObject* hdu, tcolumn** columns, int mode) {
PyObject* header = NULL;
FITSfile* Fptr;
int status = 0;
long long rowlen;
long long nrows;
long long heapsize;
long long theap;
header = PyObject_GetAttrString(hdu, "_header");
if (header == NULL) {
goto fail;
}
if (get_header_longlong(header, "NAXIS1", &rowlen, 0, HDR_NOFLAG) == GET_HEADER_FAILED) {
goto fail;
}
if (get_header_longlong(header, "NAXIS2", &nrows, 0, HDR_NOFLAG) == GET_HEADER_FAILED) {
goto fail;
}
// The PCOUNT keyword contains the number of bytes in the table heap
if (get_header_longlong(header, "PCOUNT", &heapsize, 0, HDR_FAIL_VAL_NEGATIVE) == GET_HEADER_FAILED) {
goto fail;
}
// The THEAP keyword gives the offset of the heap from the beginning of
// the HDU data portion; normally this offset is 0 but it can be set
// to something else with THEAP
if (get_header_longlong(header, "THEAP", &theap, 0, HDR_NOFLAG) == GET_HEADER_FAILED) {
goto fail;
}
fits_create_memfile(fileptr, buf, bufsize, 0, realloc, &status);
if (status != 0) {
process_status_err(status);
goto fail;
}
Fptr = (*fileptr)->Fptr;
// Now we have some fun munging some of the elements in the fitsfile struct
Fptr->writemode = mode;
Fptr->open_count = 1;
Fptr->hdutype = BINARY_TBL; /* This is a binary table HDU */
Fptr->lasthdu = 1;
Fptr->headstart[0] = 0;
Fptr->headend = 0;
Fptr->datastart = 0; /* There is no header, data starts at 0 */
Fptr->origrows = Fptr->numrows = nrows;
Fptr->rowlength = rowlen;
if (theap != 0) {
Fptr->heapstart = theap;
} else {
Fptr->heapstart = rowlen * nrows;
}
Fptr->heapsize = heapsize;
// Configure the array of table column structs from the Astropy header
// instead of allowing CFITSIO to try to read from the header
tcolumns_from_header(*fileptr, header, columns);
if (PyErr_Occurred()) {
goto fail;
}
// If any errors occur in this function they'll bubble up from here to
// compression_decompress_hdu
configure_compression(*fileptr, header);
fail:
Py_XDECREF(header);
return;
}
PyObject* compression_compress_hdu(PyObject* self, PyObject* args)
{
PyObject* hdu;
PyObject* retval = NULL;
tcolumn* columns = NULL;
void* outbuf = NULL;
size_t outbufsize;
PyObject* tmp_indata;
PyArrayObject* indata = NULL;