/
fitsio_pywrap.c
3895 lines (3152 loc) · 109 KB
/
fitsio_pywrap.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
/*
* fitsio_pywrap.c
*
* This is a CPython wrapper for the cfitsio library.
Copyright (C) 2011 Erin Sheldon, BNL. erin dot sheldon at gmail dot com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string.h>
#include <Python.h>
#include "fitsio.h"
#include "fitsio2.h"
//#include "fitsio_pywrap_lists.h"
#include <numpy/arrayobject.h>
// this is not defined anywhere in cfitsio except in
// the fits file structure
#define CFITSIO_MAX_ARRAY_DIMS 99
// not sure where this is defined in numpy...
#define NUMPY_MAX_DIMS 32
struct PyFITSObject {
PyObject_HEAD
fitsfile* fits;
};
static void
set_ioerr_string_from_status(int status) {
char status_str[FLEN_STATUS], errmsg[FLEN_ERRMSG];
char message[1024];
int nleft=1024;
if (status) {
fits_get_errstatus(status, status_str); /* get the error description */
sprintf(message, "FITSIO status = %d: %s\n", status, status_str);
nleft -= strlen(status_str)+1;
while ( nleft > 0 && fits_read_errmsg(errmsg) ) { /* get error stack messages */
strncat(message, errmsg, nleft-1);
nleft -= strlen(errmsg)+1;
if (nleft >= 2) {
strncat(message, "\n", nleft-1);
}
nleft-=2;
}
PyErr_SetString(PyExc_IOError, message);
}
return;
}
/*
string list helper functions
*/
struct stringlist {
size_t size;
char** data;
};
static struct stringlist* stringlist_new(void) {
struct stringlist* slist=NULL;
slist = malloc(sizeof(struct stringlist));
slist->size = 0;
slist->data=NULL;
return slist;
}
// push a copy of the string onto the string list
static void stringlist_push(struct stringlist* slist, const char* str) {
size_t newsize=0;
size_t i=0;
newsize = slist->size+1;
slist->data = realloc(slist->data, sizeof(char*)*newsize);
slist->size += 1;
i = slist->size-1;
slist->data[i] = strdup(str);
}
static void stringlist_push_size(struct stringlist* slist, size_t slen) {
size_t newsize=0;
size_t i=0;
newsize = slist->size+1;
slist->data = realloc(slist->data, sizeof(char*)*newsize);
slist->size += 1;
i = slist->size-1;
slist->data[i] = calloc(slen+1,sizeof(char));
//slist->data[i] = malloc(sizeof(char)*(slen+1));
//memset(slist->data[i], 0, slen+1);
}
static struct stringlist* stringlist_delete(struct stringlist* slist) {
if (slist != NULL) {
size_t i=0;
if (slist->data != NULL) {
for (i=0; i < slist->size; i++) {
free(slist->data[i]);
}
}
free(slist->data);
free(slist);
}
return NULL;
}
/*
static void stringlist_print(struct stringlist* slist) {
size_t i=0;
if (slist == NULL) {
return;
}
for (i=0; i<slist->size; i++) {
printf(" slist[%ld]: %s\n", i, slist->data[i]);
}
}
*/
static int stringlist_addfrom_listobj(struct stringlist* slist,
PyObject* listObj,
const char* listname) {
size_t size=0, i=0;
if (!PyList_Check(listObj)) {
PyErr_Format(PyExc_ValueError, "Expected a list for %s.", listname);
return 1;
}
size = PyList_Size(listObj);
for (i=0; i<size; i++) {
PyObject* tmp = PyList_GetItem(listObj, i);
const char* tmpstr;
if (!PyString_Check(tmp)) {
PyErr_Format(PyExc_ValueError,
"Expected only strings in %s list.", listname);
return 1;
}
tmpstr = (const char*) PyString_AsString(tmp);
stringlist_push(slist, tmpstr);
}
return 0;
}
static
void add_double_to_dict(PyObject* dict, const char* key, double value) {
PyObject* tobj=NULL;
tobj=PyFloat_FromDouble(value);
PyDict_SetItemString(dict, key, tobj);
Py_XDECREF(tobj);
}
static
void add_long_to_dict(PyObject* dict, const char* key, long value) {
PyObject* tobj=NULL;
tobj=PyLong_FromLong(value);
PyDict_SetItemString(dict, key, tobj);
Py_XDECREF(tobj);
}
static
void add_long_long_to_dict(PyObject* dict, const char* key, long long value) {
PyObject* tobj=NULL;
tobj=PyLong_FromLongLong(value);
PyDict_SetItemString(dict, key, tobj);
Py_XDECREF(tobj);
}
static
void add_string_to_dict(PyObject* dict, const char* key, const char* str) {
PyObject* tobj=NULL;
tobj=PyString_FromString(str);
PyDict_SetItemString(dict, key, tobj);
Py_XDECREF(tobj);
}
static
void add_none_to_dict(PyObject* dict, const char* key) {
PyDict_SetItemString(dict, key, Py_None);
}
/*
static
void append_long_to_list(PyObject* list, long value) {
PyObject* tobj=NULL;
tobj=PyLong_FromLong(value);
PyList_Append(list, tobj);
Py_XDECREF(tobj);
}
*/
static
void append_long_long_to_list(PyObject* list, long long value) {
PyObject* tobj=NULL;
tobj=PyLong_FromLongLong(value);
PyList_Append(list, tobj);
Py_XDECREF(tobj);
}
/*
static
void append_string_to_list(PyObject* list, const char* str) {
PyObject* tobj=NULL;
tobj=PyString_FromString(str);
PyList_Append(list, tobj);
Py_XDECREF(tobj);
}
*/
static int
PyFITSObject_init(struct PyFITSObject* self, PyObject *args, PyObject *kwds)
{
char* filename;
int mode;
int status=0;
int create=0;
if (!PyArg_ParseTuple(args, (char*)"sii", &filename, &mode, &create)) {
return -1;
}
if (create) {
// create and open
if (fits_create_file(&self->fits, filename, &status)) {
set_ioerr_string_from_status(status);
return -1;
}
} else {
if (fits_open_file(&self->fits, filename, mode, &status)) {
set_ioerr_string_from_status(status);
return -1;
}
}
return 0;
}
static PyObject *
PyFITSObject_repr(struct PyFITSObject* self) {
if (self->fits != NULL) {
int status=0;
char filename[FLEN_FILENAME];
char repr[2056];
if (fits_file_name(self->fits, filename, &status)) {
set_ioerr_string_from_status(status);
return NULL;
}
sprintf(repr, "fits file: %s", filename);
return PyString_FromString(repr);
} else {
return PyString_FromString("");
}
}
static PyObject *
PyFITSObject_filename(struct PyFITSObject* self) {
if (self->fits != NULL) {
int status=0;
char filename[FLEN_FILENAME];
PyObject* fnameObj=NULL;
if (fits_file_name(self->fits, filename, &status)) {
set_ioerr_string_from_status(status);
return NULL;
}
fnameObj = PyString_FromString(filename);
return fnameObj;
} else {
PyErr_SetString(PyExc_ValueError, "file is not open, cannot determine name");
return NULL;
}
}
static PyObject *
PyFITSObject_close(struct PyFITSObject* self)
{
int status=0;
if (fits_close_file(self->fits, &status)) {
self->fits=NULL;
/*
set_ioerr_string_from_status(status);
return NULL;
*/
}
self->fits=NULL;
Py_RETURN_NONE;
}
static void
PyFITSObject_dealloc(struct PyFITSObject* self)
{
int status=0;
fits_close_file(self->fits, &status);
#if PY_MAJOR_VERSION >= 3
// introduced in python 2.6
Py_TYPE(self)->tp_free((PyObject*)self);
#else
// old way, removed in python 3
self->ob_type->tp_free((PyObject*)self);
#endif
}
// if input is NULL or None, return NULL
// if maxlen is 0, the full string is copied, else
// it is maxlen+1 for the following null
/*
static char* copy_py_string(PyObject* obj, int maxlen, int* status) {
char* buffer=NULL;
int len=0;
if (obj != NULL && obj != Py_None) {
char* tmp;
if (!PyString_Check(obj)) {
PyErr_SetString(PyExc_ValueError, "Expected a string for extension name");
*status=99;
return NULL;
}
tmp = PyString_AsString(obj);
if (maxlen > 0) {
len = maxlen;
} else {
len = strlen(tmp);
}
buffer = malloc(sizeof(char)*(len+1));
strncpy(buffer, tmp, len);
}
return buffer;
}
*/
// this will need to be updated for array string columns.
// I'm using a tcolumn* here, could cause problems
static long get_groupsize(tcolumn* colptr) {
long gsize=0;
if (colptr->tdatatype == TSTRING) {
//gsize = colptr->twidth;
gsize = colptr->trepeat;
} else {
gsize = colptr->twidth*colptr->trepeat;
}
return gsize;
}
static npy_int64* get_int64_from_array(PyObject* arr, npy_intp* ncols) {
npy_int64* colnums;
int npy_type=0, check=0;
if (!PyArray_Check(arr)) {
PyErr_SetString(PyExc_TypeError, "int64 array must be an array.");
return NULL;
}
npy_type = PyArray_TYPE(arr);
// on some platforms, creating an 'i8' array gives it a longlong
// dtype. Just make sure it is 8 bytes
check=
(npy_type == NPY_INT64)
|
(npy_type==NPY_LONGLONG && sizeof(npy_longlong)==sizeof(npy_int64));
if (!check) {
PyErr_Format(PyExc_TypeError,
"array must be an int64 array (%d), got %d.",
NPY_INT64,npy_type);
return NULL;
}
if (!PyArray_ISCONTIGUOUS(arr)) {
PyErr_SetString(PyExc_TypeError, "int64 array must be a contiguous.");
return NULL;
}
colnums = PyArray_DATA(arr);
*ncols = PyArray_SIZE(arr);
return colnums;
}
// move hdu by name and possibly version, return the hdu number
static PyObject *
PyFITSObject_movnam_hdu(struct PyFITSObject* self, PyObject* args) {
int status=0;
int hdutype=ANY_HDU; // means we don't care if its image or table
char* extname=NULL;
int extver=0; // zero means it is ignored
int hdunum=0;
if (self->fits == NULL) {
PyErr_SetString(PyExc_ValueError, "fits file is NULL");
return NULL;
}
if (!PyArg_ParseTuple(args, (char*)"isi", &hdutype, &extname, &extver)) {
return NULL;
}
if (fits_movnam_hdu(self->fits, hdutype, extname, extver, &status)) {
set_ioerr_string_from_status(status);
return NULL;
}
fits_get_hdu_num(self->fits, &hdunum);
return PyLong_FromLong((long)hdunum);
}
static PyObject *
PyFITSObject_movabs_hdu(struct PyFITSObject* self, PyObject* args) {
int hdunum=0, hdutype=0;
int status=0;
PyObject* hdutypeObj=NULL;
if (self->fits == NULL) {
PyErr_SetString(PyExc_ValueError, "fits file is NULL");
return NULL;
}
if (!PyArg_ParseTuple(args, (char*)"i", &hdunum)) {
return NULL;
}
if (fits_movabs_hdu(self->fits, hdunum, &hdutype, &status)) {
set_ioerr_string_from_status(status);
return NULL;
}
hdutypeObj = PyLong_FromLong((long)hdutype);
return hdutypeObj;
}
// get info for the specified HDU
static PyObject *
PyFITSObject_get_hdu_info(struct PyFITSObject* self, PyObject* args) {
int hdunum=0, hdutype=0, ext=0;
int status=0, tstatus=0, is_compressed=0;
PyObject* dict=NULL;
char extname[FLEN_VALUE];
char hduname[FLEN_VALUE];
int extver=0, hduver=0;
if (self->fits == NULL) {
PyErr_SetString(PyExc_ValueError, "fits file is NULL");
return NULL;
}
if (!PyArg_ParseTuple(args, (char*)"i", &hdunum)) {
return NULL;
}
if (fits_movabs_hdu(self->fits, hdunum, &hdutype, &status)) {
set_ioerr_string_from_status(status);
return NULL;
}
dict = PyDict_New();
ext=hdunum-1;
add_long_to_dict(dict, "hdunum", (long)hdunum);
add_long_to_dict(dict, "extnum", (long)ext);
add_long_to_dict(dict, "hdutype", (long)hdutype);
tstatus=0;
if (fits_read_key(self->fits, TSTRING, "EXTNAME", extname, NULL, &tstatus)==0) {
add_string_to_dict(dict, "extname", extname);
} else {
add_string_to_dict(dict, "extname", "");
}
tstatus=0;
if (fits_read_key(self->fits, TSTRING, "HDUNAME", hduname, NULL, &tstatus)==0) {
add_string_to_dict(dict, "hduname", hduname);
} else {
add_string_to_dict(dict, "hduname", "");
}
tstatus=0;
if (fits_read_key(self->fits, TINT, "EXTVER", &extver, NULL, &tstatus)==0) {
add_long_to_dict(dict, "extver", (long)extver);
} else {
add_long_to_dict(dict, "extver", (long)0);
}
tstatus=0;
if (fits_read_key(self->fits, TINT, "HDUVER", &hduver, NULL, &tstatus)==0) {
add_long_to_dict(dict, "hduver", (long)hduver);
} else {
add_long_to_dict(dict, "hduver", (long)0);
}
tstatus=0;
is_compressed=fits_is_compressed_image(self->fits, &tstatus);
add_long_to_dict(dict, "is_compressed_image", (long)is_compressed);
int ndims=0;
int maxdim=CFITSIO_MAX_ARRAY_DIMS;
LONGLONG dims[CFITSIO_MAX_ARRAY_DIMS];
if (hdutype == IMAGE_HDU) {
// move this into it's own func
int tstatus=0;
int bitpix=0;
int bitpix_equiv=0;
char comptype[20];
PyObject* dimsObj=PyList_New(0);
int i=0;
//if (fits_read_imghdrll(self->fits, maxdim, simple_p, &bitpix, &ndims,
// dims, pcount_p, gcount_p, extend_p, &status)) {
if (fits_get_img_paramll(self->fits, maxdim, &bitpix, &ndims, dims, &tstatus)) {
add_string_to_dict(dict,"error","could not determine image parameters");
} else {
add_long_to_dict(dict,"ndims",(long)ndims);
add_long_to_dict(dict,"img_type",(long)bitpix);
fits_get_img_equivtype(self->fits, &bitpix_equiv, &status);
add_long_to_dict(dict,"img_equiv_type",(long)bitpix_equiv);
tstatus=0;
if (fits_read_key(self->fits, TSTRING, "ZCMPTYPE",
comptype, NULL, &tstatus)==0) {
add_string_to_dict(dict,"comptype",comptype);
} else {
add_none_to_dict(dict,"comptype");
}
for (i=0; i<ndims; i++) {
append_long_long_to_list(dimsObj, (long long)dims[i]);
}
PyDict_SetItemString(dict, "dims", dimsObj);
Py_XDECREF(dimsObj);
}
} else if (hdutype == BINARY_TBL) {
int tstatus=0;
LONGLONG nrows=0;
int ncols=0;
PyObject* colinfo = PyList_New(0);
int i=0,j=0;
fits_get_num_rowsll(self->fits, &nrows, &tstatus);
fits_get_num_cols(self->fits, &ncols, &tstatus);
add_long_long_to_dict(dict,"nrows",(long long)nrows);
add_long_to_dict(dict,"ncols",(long)ncols);
{
PyObject* d = NULL;
tcolumn* col=NULL;
struct stringlist* names=NULL;
struct stringlist* tforms=NULL;
names=stringlist_new();
tforms=stringlist_new();
for (i=0; i<ncols; i++) {
stringlist_push_size(names, 70);
stringlist_push_size(tforms, 70);
}
// just get the names: no other way to do it!
fits_read_btblhdrll(self->fits, ncols, NULL, NULL,
names->data, tforms->data,
NULL, NULL, NULL, &tstatus);
for (i=0; i<ncols; i++) {
d = PyDict_New();
int type=0;
LONGLONG repeat=0;
LONGLONG width=0;
add_string_to_dict(d,"name",names->data[i]);
add_string_to_dict(d,"tform",tforms->data[i]);
fits_get_coltypell(self->fits, i+1, &type, &repeat, &width, &tstatus);
add_long_to_dict(d,"type",(long)type);
add_long_long_to_dict(d,"repeat",(long long)repeat);
add_long_long_to_dict(d,"width",(long long)width);
fits_get_eqcoltypell(self->fits,i+1,&type,&repeat,&width, &tstatus);
add_long_to_dict(d,"eqtype",(long)type);
tstatus=0;
if (fits_read_tdimll(self->fits, i+1, maxdim, &ndims, dims,
&tstatus)) {
add_none_to_dict(d,"tdim");
} else {
PyObject* dimsObj=PyList_New(0);
for (j=0; j<ndims; j++) {
append_long_long_to_list(dimsObj, (long long)dims[j]);
}
PyDict_SetItemString(d, "tdim", dimsObj);
Py_XDECREF(dimsObj);
}
// using the struct, could cause problems
// actually, we can use ffgcprll to get this info, but will
// be redundant with some others above
col = &self->fits->Fptr->tableptr[i];
add_double_to_dict(d,"tscale",col->tscale);
add_double_to_dict(d,"tzero",col->tzero);
PyList_Append(colinfo, d);
Py_XDECREF(d);
}
names=stringlist_delete(names);
tforms=stringlist_delete(tforms);
PyDict_SetItemString(dict, "colinfo", colinfo);
Py_XDECREF(colinfo);
}
} else {
int tstatus=0;
LONGLONG nrows=0;
int ncols=0;
PyObject* colinfo = PyList_New(0);
int i=0,j=0;
fits_get_num_rowsll(self->fits, &nrows, &tstatus);
fits_get_num_cols(self->fits, &ncols, &tstatus);
add_long_long_to_dict(dict,"nrows",(long long)nrows);
add_long_to_dict(dict,"ncols",(long)ncols);
{
tcolumn* col=NULL;
struct stringlist* names=NULL;
struct stringlist* tforms=NULL;
names=stringlist_new();
tforms=stringlist_new();
for (i=0; i<ncols; i++) {
stringlist_push_size(names, 70);
stringlist_push_size(tforms, 70);
}
// just get the names: no other way to do it!
// rowlen nrows
fits_read_atblhdrll(self->fits, ncols, NULL, NULL,
// tfields tbcol units
NULL, names->data, NULL, tforms->data, NULL,
// extname
NULL, &tstatus);
for (i=0; i<ncols; i++) {
PyObject* d = PyDict_New();
int type=0;
LONGLONG repeat=0;
LONGLONG width=0;
add_string_to_dict(d,"name",names->data[i]);
add_string_to_dict(d,"tform",tforms->data[i]);
fits_get_coltypell(self->fits, i+1, &type, &repeat, &width, &tstatus);
add_long_to_dict(d,"type",(long)type);
add_long_long_to_dict(d,"repeat",(long long)repeat);
add_long_long_to_dict(d,"width",(long long)width);
fits_get_eqcoltypell(self->fits, i+1, &type, &repeat, &width, &tstatus);
add_long_to_dict(d,"eqtype",(long)type);
tstatus=0;
if (fits_read_tdimll(self->fits, i+1, maxdim, &ndims, dims,
&tstatus)) {
add_none_to_dict(dict,"tdim");
} else {
PyObject* dimsObj=PyList_New(0);
for (j=0; j<ndims; j++) {
append_long_long_to_list(dimsObj, (long long)dims[j]);
}
PyDict_SetItemString(d, "tdim", dimsObj);
Py_XDECREF(dimsObj);
}
// using the struct, could cause problems
// actually, we can use ffgcprll to get this info, but will
// be redundant with some others above
col = &self->fits->Fptr->tableptr[i];
add_double_to_dict(d,"tscale",col->tscale);
add_double_to_dict(d,"tzero",col->tzero);
PyList_Append(colinfo, d);
Py_XDECREF(d);
}
names=stringlist_delete(names);
tforms=stringlist_delete(tforms);
PyDict_SetItemString(dict, "colinfo", colinfo);
Py_XDECREF(colinfo);
}
}
return dict;
}
// this is the parameter that goes in the type for fits_write_col
static int
npy_to_fits_table_type(int npy_dtype) {
char mess[255];
switch (npy_dtype) {
case NPY_BOOL:
return TLOGICAL;
case NPY_UINT8:
return TBYTE;
case NPY_INT8:
return TSBYTE;
case NPY_UINT16:
return TUSHORT;
case NPY_INT16:
return TSHORT;
case NPY_UINT32:
if (sizeof(unsigned int) == sizeof(npy_uint32)) {
return TUINT;
} else if (sizeof(unsigned long) == sizeof(npy_uint32)) {
return TULONG;
} else {
PyErr_SetString(PyExc_TypeError, "could not determine 4 byte unsigned integer type");
return -9999;
}
case NPY_INT32:
if (sizeof(int) == sizeof(npy_int32)) {
return TINT;
} else if (sizeof(long) == sizeof(npy_int32)) {
return TLONG;
} else {
PyErr_SetString(PyExc_TypeError, "could not determine 4 byte integer type");
return -9999;
}
case NPY_INT64:
if (sizeof(long long) == sizeof(npy_int64)) {
return TLONGLONG;
} else if (sizeof(long) == sizeof(npy_int64)) {
return TLONG;
} else if (sizeof(int) == sizeof(npy_int64)) {
return TINT;
} else {
PyErr_SetString(PyExc_TypeError, "could not determine 8 byte integer type");
return -9999;
}
case NPY_FLOAT32:
return TFLOAT;
case NPY_FLOAT64:
return TDOUBLE;
case NPY_COMPLEX64:
return TCOMPLEX;
case NPY_COMPLEX128:
return TDBLCOMPLEX;
case NPY_STRING:
return TSTRING;
case NPY_UINT64:
PyErr_SetString(PyExc_TypeError, "Unsigned 8 byte integer images are not supported by the FITS standard");
return -9999;
default:
sprintf(mess,"Unsupported numpy table datatype %d", npy_dtype);
PyErr_SetString(PyExc_TypeError, mess);
return -9999;
}
return 0;
}
static int
npy_to_fits_image_types(int npy_dtype, int *fits_img_type, int *fits_datatype) {
char mess[255];
switch (npy_dtype) {
case NPY_UINT8:
*fits_img_type = BYTE_IMG;
*fits_datatype = TBYTE;
break;
case NPY_INT8:
*fits_img_type = SBYTE_IMG;
*fits_datatype = TSBYTE;
break;
case NPY_UINT16:
*fits_img_type = USHORT_IMG;
*fits_datatype = TUSHORT;
break;
case NPY_INT16:
*fits_img_type = SHORT_IMG;
*fits_datatype = TSHORT;
break;
case NPY_UINT32:
//*fits_img_type = ULONG_IMG;
if (sizeof(unsigned short) == sizeof(npy_uint32)) {
*fits_img_type = USHORT_IMG;
*fits_datatype = TUSHORT;
} else if (sizeof(unsigned int) == sizeof(npy_uint32)) {
// there is no UINT_IMG, so use ULONG_IMG
*fits_img_type = ULONG_IMG;
*fits_datatype = TUINT;
} else if (sizeof(unsigned long) == sizeof(npy_uint32)) {
*fits_img_type = ULONG_IMG;
*fits_datatype = TULONG;
} else {
PyErr_SetString(PyExc_TypeError, "could not determine 4 byte unsigned integer type");
*fits_datatype = -9999;
return 1;
}
break;
case NPY_INT32:
//*fits_img_type = LONG_IMG;
if (sizeof(unsigned short) == sizeof(npy_uint32)) {
*fits_img_type = SHORT_IMG;
*fits_datatype = TINT;
} else if (sizeof(int) == sizeof(npy_int32)) {
// there is no UINT_IMG, so use ULONG_IMG
*fits_img_type = LONG_IMG;
*fits_datatype = TINT;
} else if (sizeof(long) == sizeof(npy_int32)) {
*fits_img_type = LONG_IMG;
*fits_datatype = TLONG;
} else {
PyErr_SetString(PyExc_TypeError, "could not determine 4 byte integer type");
*fits_datatype = -9999;
return 1;
}
break;
case NPY_INT64:
//*fits_img_type = LONGLONG_IMG;
if (sizeof(int) == sizeof(npy_int64)) {
// there is no UINT_IMG, so use ULONG_IMG
*fits_img_type = LONG_IMG;
*fits_datatype = TINT;
} else if (sizeof(long) == sizeof(npy_int64)) {
*fits_img_type = LONG_IMG;
*fits_datatype = TLONG;
} else if (sizeof(long long) == sizeof(npy_int64)) {
*fits_img_type = LONGLONG_IMG;
*fits_datatype = TLONGLONG;
} else {
PyErr_SetString(PyExc_TypeError, "could not determine 8 byte integer type");
*fits_datatype = -9999;
return 1;
}
break;
case NPY_FLOAT32:
*fits_img_type = FLOAT_IMG;
*fits_datatype = TFLOAT;
break;
case NPY_FLOAT64:
*fits_img_type = DOUBLE_IMG;
*fits_datatype = TDOUBLE;
break;
case NPY_UINT64:
PyErr_SetString(PyExc_TypeError, "Unsigned 8 byte integer images are not supported by the FITS standard");
*fits_datatype = -9999;
return 1;
break;
default:
sprintf(mess,"Unsupported numpy image datatype %d", npy_dtype);
PyErr_SetString(PyExc_TypeError, mess);
*fits_datatype = -9999;
return 1;
break;
}
return 0;
}
/*
* this is really only for reading variable length columns since we should be
* able to just read the bytes for normal columns
*/
static int fits_to_npy_table_type(int fits_dtype, int* isvariable) {
if (fits_dtype < 0) {
*isvariable=1;
} else {
*isvariable=0;
}
switch (abs(fits_dtype)) {
case TBIT:
return NPY_INT8;
case TLOGICAL: // literal T or F stored as char
return NPY_INT8;
case TBYTE:
return NPY_UINT8;
case TSBYTE:
return NPY_INT8;
case TUSHORT:
if (sizeof(unsigned short) == sizeof(npy_uint16)) {
return NPY_UINT16;
} else if (sizeof(unsigned short) == sizeof(npy_uint8)) {
return NPY_UINT8;
} else {
PyErr_SetString(PyExc_TypeError, "could not determine numpy type for fits TUSHORT");
return -9999;
}
case TSHORT:
if (sizeof(short) == sizeof(npy_int16)) {
return NPY_INT16;
} else if (sizeof(short) == sizeof(npy_int8)) {
return NPY_INT8;
} else {
PyErr_SetString(PyExc_TypeError, "could not determine numpy type for fits TSHORT");
return -9999;
}
case TUINT:
if (sizeof(unsigned int) == sizeof(npy_uint32)) {
return NPY_UINT32;
} else if (sizeof(unsigned int) == sizeof(npy_uint64)) {
return NPY_UINT64;
} else if (sizeof(unsigned int) == sizeof(npy_uint16)) {
return NPY_UINT16;
} else {
PyErr_SetString(PyExc_TypeError, "could not determine numpy type for fits TUINT");
return -9999;
}
case TINT:
if (sizeof(int) == sizeof(npy_int32)) {
return NPY_INT32;
} else if (sizeof(int) == sizeof(npy_int64)) {
return NPY_INT64;
} else if (sizeof(int) == sizeof(npy_int16)) {
return NPY_INT16;
} else {
PyErr_SetString(PyExc_TypeError, "could not determine numpy type for fits TINT");
return -9999;
}
case TULONG:
if (sizeof(unsigned long) == sizeof(npy_uint32)) {
return NPY_UINT32;
} else if (sizeof(unsigned long) == sizeof(npy_uint64)) {
return NPY_UINT64;
} else if (sizeof(unsigned long) == sizeof(npy_uint16)) {
return NPY_UINT16;
} else {
PyErr_SetString(PyExc_TypeError, "could not determine numpy type for fits TULONG");
return -9999;
}
case TLONG:
if (sizeof(unsigned long) == sizeof(npy_int32)) {
return NPY_INT32;
} else if (sizeof(unsigned long) == sizeof(npy_int64)) {
return NPY_INT64;
} else if (sizeof(long) == sizeof(npy_int16)) {
return NPY_INT16;