This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsfitsio.c
1304 lines (1276 loc) · 34.5 KB
/
jsfitsio.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
/*
*
* jsfitsio.c -- extra routines for javascript version
*
*/
/*
Notes:
passing arrays and passing by reference:
http://stackoverflow.com/questions/17883799/how-to-handle-passing-returning-array-pointers-to-emscripten-compiled-code
https://groups.google.com/forum/#!topic/emscripten-discuss/JDaNHIRQ_G4
*/
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <sys/time.h>
#include <math.h>
#include "fitsio.h"
#include "jsfitsio.h"
#include "healpix.h"
#if WITH_IDX
#include "idx.h"
#endif
/*
* http://stackoverflow.com/questions/3599160/unused-parameter-warnings-in-c-code
*/
#ifndef UNUSED
#ifdef __GNUC__
# define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
#else
# define UNUSED(x) UNUSED_ ## x
#endif
#endif
int ffpxsz(int datatype);
/*
*----------------------------------------------------------------------------
*
*
* Private Routines and Data
*
*
*----------------------------------------------------------------------------
*/
// emscripten does not have access to unlimited memory
#if __EMSCRIPTEN__
#define HAVE_MEMORY_LIMIT 1
#else
#define HAVE_MEMORY_LIMIT 0
#endif
// ffhist3 was added to cfitsio v3.39
#if (CFITSIO_MAJOR < 3) || ((CFITSIO_MAJOR == 3) && (CFITSIO_MINOR < 39))
#error ffhist3() required: please upgrade cfitsio to v3.39 or higher
#endif
/* must match what cfitsio expects (i.e., 4 for histogramming) */
#define IDIM 4
#define IFILE "mem://"
#define MFILE "cfitsio_memfile"
#define MAXINT 2147483647
#define MININT -2147483648
#ifndef min
#define min(a,b) (a<=b?a:b)
#endif
#ifndef max
#define max(a,b) (a>=b?a:b)
#endif
#ifndef SZ_LINE
#define SZ_LINE 1024
#endif
#if HAVE_MEMORY_LIMIT
#define DEF_MAX_MEMORY 2000000000
#else
#define DEF_MAX_MEMORY 0
#endif
static long def_max_memory = DEF_MAX_MEMORY;
/*
*
* Routine: keyword
*
* Purpose: look for a keyword=<value> string inside another string,
* remove and return the <value> in another buffer
*
* Returns: len if keyword was found, 0 otherwise
*
*/
static int keyword (char *ibuf, char *key, char *env, char *obuf, int maxlen){
int qlev;
int len = 0;
char *s;
char *t;
char *u;
char *v;
char *ibase = NULL;
char *iptr=NULL;
char quote='\0';
/* if we have no input string, we are done */
if( !ibuf || !*ibuf || !key || !*key ) return 0;
/* start out pessimistically */
ibase = strdup(ibuf);
iptr = ibase;
*obuf = '\0';
/* maxlen generally is 1 more than we can handle */
maxlen--;
/* keep trying */
while( *iptr ){
/* look for key from current position */
if( (s = (char *)strstr(iptr, key)) == NULL ) goto done;
/* if we found a key, we need to make sure ... */
/* it must be preceeded by beginning of string, beginning of bracket,
or by a "," from previous keyword */
if( (s == ibase) || (*(s-1) == ',') || (*(s-1) == '[') ){
/* it can be followed by spaces ... */
t = s + strlen(key);
while( isspace((int)*t) ) t++;
/* but must be followed by an "=" */
if( *t == '=' ){
t++;
/* skip spaces again */
while( isspace((int)*t) ) t++;
/* this is where the actual value part of the string begins */
u = t;
/* this will be where it ends */
v = t;
/* gather up everything to the next "," or end of filter */
if( (*t == '"') || (*t == '\'') || (*t == '(') || (*t == '[') ){
switch(*t){
case '"':
case '\'':
quote = *t;
break;
case '(':
quote = ')';
break;
case '[':
quote = ']';
break;
}
/* bump past opening quote char */
t++; u++; v++;
while( *t && (*t != quote) ){
t++; v++;
}
if( *t == quote ){
t++;
}
}
else{
qlev = 0;
while( *t &&
((qlev != 0) || (*t != ',')) &&
((qlev != 0) || (*t != ']')) ){
if( *t == '[' ){
qlev++;
} else if( *t == ']' ){
qlev--;
}
t++; v++;
}
}
len = min(maxlen, v - u);
strncpy(obuf, u, len);
obuf[len] = '\0';
/* remove keyword=value string from the original buffer */
/* first remove preceding comma, if necessary */
if( (s > ibase) && (*(s-1) == ',') ){
s--;
}
/* but leave 1 comma in place */
else if( *t == ',' ){
t++;
}
/* now overwrite original from where the keyword started */
memmove(s, t, strlen(t)+1);
/* return success */
goto done;
}
}
/* start next search just past this one */
iptr = s+1;
}
/* didn't find anything */
len = 0;
done:
if( ibase ) free(ibase);
/* if not found, check for environment variable, if necessary */
if( !len && env && (s = (char *)getenv(env)) ){
strncpy(obuf, s, maxlen);
len = strlen(obuf);
}
return len;
}
/*
*
* getcolinfo -- read a string containing tlmin, tlmax, binsiz of a column
*
* col[:tlmin[:tmlax[:binsiz]]]
*
* If only one arg is given, it's assumed to be the binsize
*
*
*/
static int getcolinfo(char *s, char *col, int clen,
double *tlmin, double *tlmax, double *binsiz){
int i, got, tlen;
char *v;
char vbuf[SZ_LINE];
double val[3];
/* initialize */
*col = '\0';
/* make sure we have something to do */
if( !s || !*s ){
return 0;
}
/* get column name */
for(v=col, tlen=0; *s && (*s != ':') && tlen < clen; tlen++ ){
*v++ = *s++;
}
*v = '\0';
/* get image dimensions and bin size */
for(i=0, got=0; i<3; i++, got++){
if( *s != ':' ){
break;
}
/* skip past ':' */
s++;
/* fill buffer with next value */
*vbuf = '\0';
for(v=vbuf; *s && (*s != ':') && tlen < SZ_LINE; tlen++){
*v++ = *s++;
}
*v = '\0';
/* convert string to double */
val[i] = atof(vbuf);
}
switch(got){
case 0:
*binsiz = 1;
got = 1;
break;
case 1:
*binsiz = val[0];
break;
case 2:
*tlmin = val[0];
*tlmax = val[1];
break;
case 3:
default:
*tlmin = val[0];
*tlmax = val[1];
*binsiz = val[2];
break;
}
return got;
}
// give a bitpix, return type
int fitstypeof(int bitpix){
switch(bitpix){
case 8:
return TBYTE;
case 16:
return TSHORT;
case -16:
return TUSHORT;
case 32:
return TINT;
case 64:
return TLONGLONG;
case -32:
return TFLOAT;
case -64:
return TDOUBLE;
default:
return TINT;
}
}
// gotoFITSHDU: try to go to a reasonable HDU if the primary is useless
// we look for specified extensions and if not found, go to hdu #2
// this is how xray binary tables are imaged automatically
static fitsfile *gotoFITSHDU(fitsfile *fptr,
char *extlist, int *hdutype, int *status){
int hdunum, naxis, thdutype, gotext=0;
long naxes[IDIM] = {0, 0, 0, 0};
char *ext, *textlist;
// if this is the primary array and it does not contain an image,
// try to move to something more reasonble
fits_get_hdu_num(fptr, &hdunum); *status = 0;
fits_get_img_dim(fptr, &naxis, status); *status = 0;
fits_get_img_size(fptr, min(IDIM,naxis), naxes, status); *status = 0;
if( (hdunum == 1) && ((naxis == 0) || naxes[0] == 0) ){
// look through the extension list
if( extlist ){
gotext = 0;
textlist = (char *)strdup(extlist);
for(ext=(char *)strtok(textlist, " "); ext != NULL;
ext=(char *)strtok(NULL," ")){
fits_movnam_hdu(fptr, ANY_HDU, ext, 0, status);
if( *status == 0 ){
gotext = 1;
break;
} else {
*status = 0;
}
}
free(textlist);
}
if( !gotext ){
// if all else fails, move to extension #2 and hope for the best
fits_movabs_hdu(fptr, 2, &thdutype, status);
}
}
fits_get_hdu_type(fptr, hdutype, status);
return fptr;
}
/*
*----------------------------------------------------------------------------
*
*
* Public Routines and Data
*
*
*----------------------------------------------------------------------------
*/
// openFITSFile: open a FITS file for reading and go to a useful HDU
//
fitsfile *openFITSFile(char *ifile, int iomode, char *extlist,
char *UNUSED(opts), int *hdutype, int *status){
fitsfile *fptr;
// open fits file
fits_open_file(&fptr, ifile, iomode, status);
// bail out if there is an error at this point
if( *status ) return NULL;
return gotoFITSHDU(fptr, extlist, hdutype, status);
}
// openFITSMem: open a FITS memory buffer for reading and go to a useful HDU
fitsfile *openFITSMem(void **buf, size_t *buflen, char *extlist,
char *UNUSED(opts), int *hdutype, int *status){
fitsfile *fptr;
// open fits file
fits_open_memfile(&fptr, MFILE, READWRITE, buf, buflen, 0, NULL, status);
// bail out if there is an error at this point
if( *status ) return NULL;
return gotoFITSHDU(fptr, extlist, hdutype, status);
}
// getHeaderToString: get header as a string
void getHeaderToString(fitsfile *fptr,
char **cardstr, int *ncard, int *status){
fits_convert_hdr2str(fptr, 0, NULL, 0, cardstr, ncard, status);
}
// update/add WCS params
// update/add LTM and LTV header params
// ftp://iraf.noao.edu/iraf/web/projects/fitswcs/specwcs.html
void updateWCS(fitsfile *fptr, fitsfile *ofptr,
int xcen, int ycen, int dim1, int dim2, double bin,
double *amin){
int status;
double x1, y1;
double dval;
char comment[FLEN_CARD];
if( !bin ){
bin = 1;
}
// use amin to calculate CRPIX values, if present
if( amin != NULL ){
x1 = (double)amin[0];
y1 = (double)amin[1];
dval = 0.0; *comment = '\0'; status = 0;
fits_read_key(fptr, TDOUBLE, "CRPIX1", &dval, comment, &status);
if( status == 0 ){
// funtools-style: see funtools/funcopy.c/_FunCopy2ImageHeader
dval = (dval + 1.0 - x1 - 0.5) / bin + 0.5;
// cfitsio-style: see cfitsio/histo.c
// dval = (dval - x1) / bin + 0.5;
// zhtools-style: see zhtools/src/images/imblock
// dval = (dval - x1 - 0.5) / bin + 0.5;
fits_update_key(ofptr, TDOUBLE, "CRPIX1", &dval, comment, &status);
}
dval = 0.0; *comment = '\0'; status = 0;
fits_read_key(fptr, TDOUBLE, "CRPIX2", &dval, comment, &status);
if( status == 0 ){
// funtools-style: see funtools/funcopy.c/_FunCopy2ImageHeader
dval = (dval + 1.0 - y1 - 0.5) / bin + 0.5;
// cfitsio-style: see cfitsio/histo.c
// dval = (dval - y1) / bin + 0.5;
// zhtools-style: see zhtools/src/images/imblock
// dval = (dval - y1 - 0.5) / bin + 0.5;
fits_update_key(ofptr, TDOUBLE, "CRPIX2", &dval, comment, &status);
}
dval = 0.0; *comment = '\0'; status = 0;
fits_read_key(fptr, TDOUBLE, "CDELT1", &dval, comment, &status);
if( status == 0 ){
dval = dval * bin;
fits_update_key(ofptr, TDOUBLE, "CDELT1", &dval, comment, &status);
}
dval = 0.0; *comment = '\0'; status = 0;
fits_read_key(fptr, TDOUBLE, "CDELT2", &dval, comment, &status);
if( status == 0 ){
dval = dval * bin;
fits_update_key(ofptr, TDOUBLE, "CDELT2", &dval, comment, &status);
}
dval = 0.0; *comment = '\0'; status = 0;
fits_read_key(fptr, TDOUBLE, "CD1_1", &dval, comment, &status);
if( status == 0 ){
dval = dval * bin;
fits_update_key(ofptr, TDOUBLE, "CD1_1", &dval, comment, &status);
}
dval = 0.0; *comment = '\0'; status = 0;
fits_read_key(fptr, TDOUBLE, "CD1_2", &dval, comment, &status);
if( status == 0 ){
dval = dval * bin;
fits_update_key(ofptr, TDOUBLE, "CD1_2", &dval, comment, &status);
}
dval = 0.0; *comment = '\0'; status = 0;
fits_read_key(fptr, TDOUBLE, "CD2_1", &dval, comment, &status);
if( status == 0 ){
dval = dval * bin;
fits_update_key(ofptr, TDOUBLE, "CD2_1", &dval, comment, &status);
}
dval = 0.0; *comment = '\0'; status = 0;
fits_read_key(fptr, TDOUBLE, "CD2_2", &dval, comment, &status);
if( status == 0 ){
dval = dval * bin;
fits_update_key(ofptr, TDOUBLE, "CD2_2", &dval, comment, &status);
}
}
// update ltm/ltv values, using center to calculate ltv values
if( dim1 && dim2 ){
x1 = (xcen - (dim1 / 2.0));
y1 = (ycen - (dim2 / 2.0));
dval = 1.0; *comment = '\0'; status = 0;
fits_read_key(fptr, TDOUBLE, "LTM1_1", &dval, comment, &status);
dval = dval / bin; status = 0;
fits_update_key(ofptr, TDOUBLE, "LTM1_1", &dval, comment, &status);
dval = 0.0; *comment = '\0'; status = 0;
fits_read_key(fptr, TDOUBLE, "LTM1_2", &dval, comment, &status);
dval = dval / bin; status = 0;
fits_update_key(ofptr, TDOUBLE, "LTM1_2", &dval, comment, &status);
dval = 0.0; *comment = '\0'; status = 0;
fits_read_key(fptr, TDOUBLE, "LTM2_1", &dval, comment, &status);
dval = dval / bin; status = 0;
fits_update_key(ofptr, TDOUBLE, "LTM2_1", &dval, comment, &status);
dval = 1.0; *comment = '\0'; status = 0;
fits_read_key(fptr, TDOUBLE, "LTM2_2", &dval, comment, &status);
dval = dval / bin; status = 0;
fits_update_key(ofptr, TDOUBLE, "LTM2_2", &dval, comment, &status);
dval = 0.0; *comment = '\0'; status = 0;
fits_read_key(fptr, TDOUBLE, "LTV1", &dval, comment, &status);
dval = (dval - x1 - 0.5) / bin + 0.5; status = 0;
fits_update_key(ofptr, TDOUBLE, "LTV1", &dval, comment, &status);
dval = 0.0; *comment = '\0'; status = 0;
fits_read_key(fptr, TDOUBLE, "LTV2", &dval, comment, &status);
dval = (dval - y1 - 0.5) / bin + 0.5; status = 0;
fits_update_key(ofptr, TDOUBLE, "LTV2", &dval, comment, &status);
}
}
// getImageToArray: extract a sub-section from an image HDU, return array
void *getImageToArray(fitsfile *fptr,
int *dims, double *cens, double bin, int binMode,
char *slice, char *opts,
int *start, int *end, int *bitpix, int *status){
int b, i, j, k, naxis;
int idim1, idim2, idim3, sdim1, sdim2, hidim1, hidim2, hidim3;
int ttype, tsize;
int bsize = 0;
int biter = 1;
int ooff = 0;
int ojoff = 0;
int tstatus = 0;
int doscale = 0;
int dobin = 0;
#if __EMSCRIPTEN__
int doconvert64 = 0;
#endif
void *obuf, *rbuf;
long totim, totpix, totbytes;
long naxes[IDIM], fpixel[IDIM], lpixel[IDIM], myfpixel[IDIM], inc[IDIM];
double bin2;
double xcen, ycen;
double bscale = 1.0;
double bzero = 0.0;
char comment[FLEN_CARD];
char tbuf[SZ_LINE];
char *s, *tslice;
char *bptr = NULL;
int nslice, iaxis0=0, iaxis1=0, iaxis2=0;
int iaxes[3] = {0, 1, 2};
int saxes[IDIM] = {0, 0, 0, 0};
long max_memory = def_max_memory;
unsigned char *crbuf=NULL, *cobuf=NULL;
short *srbuf=NULL, *sobuf=NULL;
unsigned short *usrbuf=NULL, *usobuf=NULL;
int *irbuf=NULL, *iobuf=NULL;
long long *lrbuf=NULL, *lobuf=NULL;
float *frbuf=NULL, *fobuf=NULL;
double *drbuf=NULL, *dobuf=NULL;
// get options
if( keyword(opts, "maxsize", "JSFITSIO_MAXMEMORY", tbuf, SZ_LINE) ){
max_memory = strtol(tbuf, NULL, 10);
}
// seed buffers
for(i=0; i<IDIM; i++){
naxes[i] = 0;
fpixel[i] = 1;
lpixel[i] = 1;
inc[i] = 1;
}
// get image dimensions and type
fits_get_img_dim(fptr, &naxis, status);
fits_get_img_size(fptr, min(IDIM,naxis), naxes, status);
fits_get_img_type(fptr, bitpix, status);
if( naxis < 2 ){
*status = BAD_DIMEN;
return NULL;
}
// get binning parameter
// negative bin => 1/abs(bin)
if( bin == 0 ){
bin = 1.0;
} else if( bin < 0 ){
bin = 1.0 / fabs(bin);
}
// parse slice string into primary axes and slice axes
if( slice && *slice ){
if( !strcmp(slice, "all") ){
tslice = (char *)strdup("*:*:all");
} else {
if( strchr(slice, ':') || strchr(slice, ',') ){
tslice = (char *)strdup(slice);
} else {
snprintf(tbuf, SZ_LINE, "*:*:%s", slice);
tslice = (char *)strdup(tbuf);
}
}
for(s=(char *)strtok(tslice, ":,"), nslice=0, i=0;
(s != NULL) && (nslice < IDIM);
s=(char *)strtok(NULL,":,"), nslice++){
if( !strcmp(s, "*") ){
if( i < 2 ){
iaxes[i++] = nslice;
}
} else {
// all slices (i.e. the whole data cube)?
if( !strcmp(s, "all") ){
saxes[nslice] = -naxes[nslice];
iaxis2 = nslice;
} else {
// specific slice
saxes[nslice] = atoi(s);
if( (saxes[nslice] < 1) || (saxes[nslice] > naxes[nslice]) ){
*status = SEEK_ERROR;
return NULL;
}
}
}
}
free(tslice);
}
// convenience variables for the primary axis indexes
iaxis0 = iaxes[0];
iaxis1 = iaxes[1];
// max dimension of each input axis for integral binning
if( bin >= 1 ){
idim1 = (int)(naxes[iaxis0] / bin) * bin;
idim2 = (int)(naxes[iaxis1] / bin) * bin;
} else {
idim1 = naxes[iaxis0];
idim2 = naxes[iaxis1];
}
// get limits of extracted section taking binning into account
if( dims && dims[0] && dims[1] ){
sdim1 = min(dims[0], idim1);
sdim2 = min(dims[1], idim2);
// read image section
if( cens && cens[0] && cens[1] ){
xcen = cens[0];
ycen = cens[1];
} else {
xcen = idim1/2.0;
ycen = idim2/2.0;
}
// min and max of input section, indexed from 1
fpixel[iaxis0] = (int)(xcen - (sdim1/2.0) + 1);
fpixel[iaxis1] = (int)(ycen - (sdim2/2.0) + 1);
lpixel[iaxis0] = (int)(xcen + (sdim1/2.0));
if( sdim1 % 2 == 1 ){ lpixel[iaxis0] += 1; }
lpixel[iaxis1] = (int)(ycen + (sdim2/2.0));
if( sdim2 % 2 == 1 ){ lpixel[iaxis1] += 1; }
} else {
// read entire input image
fpixel[iaxis0] = 1;
fpixel[iaxis1] = 1;
lpixel[iaxis0] = idim1;
lpixel[iaxis1] = idim2;
}
// stay within image limits
fpixel[iaxis0] = max(fpixel[iaxis0], 1);
fpixel[iaxis0] = min(fpixel[iaxis0], naxes[iaxis0]);
lpixel[iaxis0] = max(lpixel[iaxis0], 1);
lpixel[iaxis0] = min(lpixel[iaxis0], naxes[iaxis0]);
fpixel[iaxis1] = max(fpixel[iaxis1], 1);
fpixel[iaxis1] = min(fpixel[iaxis1], naxes[iaxis1]);
lpixel[iaxis1] = max(lpixel[iaxis1], 1);
lpixel[iaxis1] = min(lpixel[iaxis1], naxes[iaxis1]);
/* get output dimensions, ensuring that bin divides dimensions evenly */
if( fmod(bin,1) == 0.0 ){
for(i=0; i<bin; i++){
idim1 = (lpixel[iaxis0] - fpixel[iaxis0] + 1);
if( (idim1 <= 0 ) || fmod(((float)idim1/(float)bin), 1) == 0.0 ){
break;
}
lpixel[iaxis0] -= 1;
}
for(i=0; i<bin; i++){
idim2 = (lpixel[iaxis1] - fpixel[iaxis1] + 1);
if( (idim2 <= 0 ) || fmod(((float)idim2/(float)bin), 1) == 0.0 ){
break;
}
lpixel[iaxis1] -= 1;
}
} else {
idim1 = (lpixel[iaxis0] - fpixel[iaxis0] + 1);
idim2 = (lpixel[iaxis1] - fpixel[iaxis1] + 1);
}
// for sliced dimensions, set first and last pixel to the specified slice
idim3 = 1;
for(i=0; i<min(IDIM,naxis); i++){
if( saxes[i] ){
if( saxes[i] < 0 ){
// entire slice dimension
fpixel[i] = 1;
lpixel[i] = naxes[i];
idim3 = naxes[i];
} else {
// 1 pixel slice in this dimension
fpixel[i] = saxes[i];
lpixel[i] = saxes[i];
}
// stay within image limits
fpixel[i] = max(fpixel[i], 1);
fpixel[i] = min(fpixel[i], naxes[i]);
lpixel[i] = max(lpixel[i], 1);
lpixel[i] = min(lpixel[i], naxes[i]);
}
}
// save section limits
if( start ){
start[0] = fpixel[iaxis0];
start[1] = fpixel[iaxis1];
if( iaxis2 ){
start[2] = fpixel[iaxis2];
}
}
if( end ){
end[0] = lpixel[iaxis0];
end[1] = lpixel[iaxis1];
if( iaxis2 ){
end[2] = lpixel[iaxis2];
}
}
// total pixels in one slice of the image
totim = idim1 * idim2;
// total pixels in the image
totpix = idim1 * idim2 * idim3;
// make sure we have an image with valid dimensions size
if( totpix <= 1 ){
*status = NEG_AXIS;
return NULL;
}
// are we scaling?
fits_read_key(fptr, TDOUBLE, "BSCALE", &bscale, comment, &tstatus);
if( tstatus != VALUE_UNDEFINED ){
fits_read_key(fptr, TDOUBLE, "BZERO", &bzero, comment, &tstatus);
}
if( (bscale != 1.0) || (bzero != 0.0) ){
doscale = 1;
}
if( bin != 1 ){
if( (binMode == 0) || (binMode == 's') ){
dobin = 's';
} else {
dobin = 'a';
}
}
// in javascript, there is no 64-bit typed array support other than BitInt64,
// which strictly requires bigint values, so we convert to double float
#if __EMSCRIPTEN__
if( *bitpix == 64 ){
*bitpix = -64;
doconvert64 = 1;
}
#endif
// allocate space for the pixel array
// scaled integer data => float
// binned sum integer data => int
// binned avg integer data => float
switch(*bitpix){
case 8:
if( doscale ){
*bitpix = -32;
ttype = TFLOAT;
tsize = sizeof(float);
} else if( dobin == 's' ){
*bitpix = 32;
ttype = TINT;
tsize = sizeof(int);
} else if( dobin == 'a' ){
*bitpix = -32;
ttype = TFLOAT;
tsize = sizeof(float);
} else {
ttype = TBYTE;
tsize = sizeof(char);
}
break;
case 16:
if( doscale ){
*bitpix = -32;
ttype = TFLOAT;
tsize = sizeof(float);
} else if( dobin == 's' ){
*bitpix = 32;
ttype = TINT;
tsize = sizeof(int);
} else if( dobin == 'a' ){
*bitpix = -32;
ttype = TFLOAT;
tsize = sizeof(float);
} else {
ttype = TSHORT;
tsize = sizeof(short);
}
break;
case -16:
if( doscale ){
*bitpix = -32;
ttype = TFLOAT;
tsize = sizeof(float);
} else if( dobin == 's' ){
*bitpix = 32;
ttype = TINT;
tsize = sizeof(int);
} else if( dobin == 'a' ){
*bitpix = -32;
ttype = TFLOAT;
tsize = sizeof(float);
} else {
ttype = TUSHORT;
tsize = sizeof(unsigned short);
}
break;
case 32:
if( doscale ){
*bitpix = -32;
ttype = TFLOAT;
tsize = sizeof(float);
} else if( dobin == 'a' ){
*bitpix = -32;
ttype = TFLOAT;
tsize = sizeof(float);
} else {
ttype = TINT;
tsize = sizeof(int);
}
break;
case 64:
if( doscale ){
*bitpix = -32;
ttype = TFLOAT;
tsize = sizeof(float);
} else if( dobin == 'a' ){
*bitpix = -32;
ttype = TFLOAT;
tsize = sizeof(float);
} else {
ttype = TLONGLONG;
tsize = sizeof(long long);
}
break;
case -32:
ttype = TFLOAT;
tsize = sizeof(float);
break;
case -64:
ttype = TDOUBLE;
tsize = sizeof(double);
break;
default:
return NULL;
}
if( bin == 1 ){
totbytes = totpix * tsize;
// sanity check on memory limits
if( max_memory && totbytes > max_memory ){
*status = MEMORY_ALLOCATION;
return NULL;
}
// allocate memory for the whole image section
if(!(obuf = (void *)calloc(totbytes, sizeof(char)))){
*status = MEMORY_ALLOCATION;
return NULL;
}
/* read the image section */
fits_read_subset(fptr, ttype, fpixel, lpixel, inc, 0, obuf, 0, status);
} else {
// allocate memory for one full row of input data
if(!(rbuf = (void *)calloc(idim1 * tsize, sizeof(char)))){
*status = MEMORY_ALLOCATION;
return NULL;
}
// get total bytes
totbytes = (int)((int)(idim1 / bin) * (int)(idim2 / bin) * idim3 * tsize);
/* allocate memory for the output binned image section */
if( !(obuf = (void *)calloc(totbytes, sizeof(char))) ){
*status = MEMORY_ALLOCATION;
return NULL;
}
switch(*bitpix){
case 8:
crbuf = (unsigned char *)rbuf;
cobuf = (unsigned char *)obuf;
break;
case 16:
srbuf = (short *)rbuf;
sobuf = (short *)obuf;
break;
case -16:
usrbuf = (unsigned short *)rbuf;
usobuf = (unsigned short *)obuf;
break;
case 32:
irbuf = (int *)rbuf;
iobuf = (int *)obuf;
break;
case 64:
lrbuf = (long long *)rbuf;
lobuf = (long long *)obuf;
break;
case -32:
frbuf = (float *)rbuf;
fobuf = (float *)obuf;
break;
case -64:
drbuf = (double *)rbuf;
dobuf = (double *)obuf;
break;
}
/* seed section limits */
for(i=0; i<IDIM; i++){
myfpixel[i] = fpixel[i];
}
// loop limits
if( bin >= 1 ){
hidim1 = (int)(idim1 / bin) * bin;
hidim2 = (int)(idim2 / bin) * bin;
hidim3 = idim3;
} else {
hidim1 = idim1;
hidim2 = idim2;
hidim3 = idim3;
biter = (int)(1.0 / bin);
bsize = (int)(idim1 / bin) * tsize;
}
/* for each input slice */
for(k=0; k<hidim3; k++){
/* read thus slice slice */
myfpixel[2] = fpixel[2] + k;
/* for each input row */
for(j=0; j<hidim2; j++){
/* read next line of the input section */
myfpixel[1] = fpixel[1] + j;
tstatus = 0;
fits_read_pix(fptr, ttype, myfpixel, idim1, NULL, rbuf, NULL, &tstatus);
/* exit on error, perhaps we still have something to show */
if( tstatus ){
break;
}
// offset into output image for this line
ojoff = (int)(k * totim) + (int)(j / bin) * (int)(idim1 / bin);
/* for each pixel in the input line */
for(i=0; i<hidim1; i++){
/* index of output image pixel */
ooff = ojoff + (int)(i / bin);
switch(*bitpix){
case 8:
for(b=0; b<biter; b++){
cobuf[ooff+b] += crbuf[i];
}
break;
case 16:
for(b=0; b<biter; b++){
sobuf[ooff+b] += srbuf[i];
}
break;
case -16:
for(b=0; b<biter; b++){
usobuf[ooff+b] += usrbuf[i];
}
break;
case 32:
for(b=0; b<biter; b++){
iobuf[ooff+b] += irbuf[i];
}
break;
case 64:
for(b=0; b<biter; b++){
lobuf[ooff+b] += lrbuf[i];
}
break;
case -32:
for(b=0; b<biter; b++){
fobuf[ooff+b] += frbuf[i];
}
break;
case -64:
for(b=0; b<biter; b++){
dobuf[ooff+b] += drbuf[i];
}
break;
}
}
if( bin < 1 ){
bptr = obuf + (ojoff * tsize);
for(b=1; b<biter; b++){
memcpy(bptr + b * bsize, bptr, bsize);
}
}
}
free(rbuf);
}
}
// average, if necessary
if( dobin == 'a' ){
if( bin >= 1 ){
bin2 = bin * bin;
} else {
bin2 = 1 / (bin * bin);
}
totpix = totbytes / tsize;
for(i=0; i<totpix; i++){
switch(*bitpix){
case 8:
cobuf[i] /= bin2;
break;
case 16:
sobuf[i] /= bin2;
break;
case -16:
usobuf[i] /= bin2;
break;
case 32:
iobuf[i] /= bin2;
break;
case 64:
lobuf[i] /= bin2;
break;
case -32:
fobuf[i] /= bin2;
break;
case -64:
dobuf[i] /= bin2;
break;
}
}
}
// if we converted bitpix 64 to -64, check if we can convert back to 32
#if __EMSCRIPTEN__
if( doconvert64 ){
// ensure dobuf points to obuf (in case we read the whole section)
dobuf = (double *)obuf;
// are all pixel values are in range of 32 bit ints?
for(i=0; i<totpix; i++){
if( (dobuf[i] > MAXINT) || (dobuf[i] < MININT) ){
// data is out of 32-bit range, leave as double
doconvert64 = 0;
break;
}
}
// if so, we can convert the output to int 32 data