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
/
histo.c
2539 lines (2184 loc) · 84.8 KB
/
histo.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
/* Globally defined histogram parameters */
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include "fitsio2.h"
typedef struct { /* Structure holding all the histogramming information */
union { /* the iterator work functions (ffwritehist, ffcalchist) */
char *b; /* need to do their job... passed via *userPointer. */
short *i;
int *j;
float *r;
double *d;
} hist;
fitsfile *tblptr;
int haxis, hcolnum[4], himagetype;
long haxis1, haxis2, haxis3, haxis4;
double amin1, amin2, amin3, amin4;
double maxbin1, maxbin2, maxbin3, maxbin4;
double binsize1, binsize2, binsize3, binsize4;
int wtrecip, wtcolnum;
double weight;
char *rowselector;
} histType;
/*--------------------------------------------------------------------------*/
int ffbins(char *binspec, /* I - binning specification */
int *imagetype, /* O - image type, TINT or TSHORT */
int *histaxis, /* O - no. of axes in the histogram */
char colname[4][FLEN_VALUE], /* column name for axis */
double *minin, /* minimum value for each axis */
double *maxin, /* maximum value for each axis */
double *binsizein, /* size of bins on each axis */
char minname[4][FLEN_VALUE], /* keyword name for min */
char maxname[4][FLEN_VALUE], /* keyword name for max */
char binname[4][FLEN_VALUE], /* keyword name for binsize */
double *wt, /* weighting factor */
char *wtname, /* keyword or column name for weight */
int *recip, /* the reciprocal of the weight? */
int *status)
{
/*
Parse the input binning specification string, returning the binning
parameters. Supports up to 4 dimensions. The binspec string has
one of these forms:
bin binsize - 2D histogram with binsize on each axis
bin xcol - 1D histogram on column xcol
bin (xcol, ycol) = binsize - 2D histogram with binsize on each axis
bin x=min:max:size, y=min:max:size, z..., t...
bin x=:max, y=::size
bin x=size, y=min::size
most other reasonable combinations are supported.
*/
int ii, slen, defaulttype;
char *ptr, tmpname[FLEN_VALUE], *file_expr = NULL;
double dummy;
if (*status > 0)
return(*status);
/* set the default values */
*histaxis = 2;
*imagetype = TINT;
defaulttype = 1;
*wt = 1.;
*recip = 0;
*wtname = '\0';
/* set default values */
for (ii = 0; ii < 4; ii++)
{
*colname[ii] = '\0';
*minname[ii] = '\0';
*maxname[ii] = '\0';
*binname[ii] = '\0';
minin[ii] = DOUBLENULLVALUE; /* undefined values */
maxin[ii] = DOUBLENULLVALUE;
binsizein[ii] = DOUBLENULLVALUE;
}
ptr = binspec + 3; /* skip over 'bin' */
if (*ptr == 'i' ) /* bini */
{
*imagetype = TSHORT;
defaulttype = 0;
ptr++;
}
else if (*ptr == 'j' ) /* binj; same as default */
{
defaulttype = 0;
ptr ++;
}
else if (*ptr == 'r' ) /* binr */
{
*imagetype = TFLOAT;
defaulttype = 0;
ptr ++;
}
else if (*ptr == 'd' ) /* bind */
{
*imagetype = TDOUBLE;
defaulttype = 0;
ptr ++;
}
else if (*ptr == 'b' ) /* binb */
{
*imagetype = TBYTE;
defaulttype = 0;
ptr ++;
}
if (*ptr == '\0') /* use all defaults for other parameters */
return(*status);
else if (*ptr != ' ') /* must be at least one blank */
{
ffpmsg("binning specification syntax error:");
ffpmsg(binspec);
return(*status = URL_PARSE_ERROR);
}
while (*ptr == ' ') /* skip over blanks */
ptr++;
if (*ptr == '\0') /* no other parameters; use defaults */
return(*status);
/* Check if need to import expression from a file */
if( *ptr=='@' ) {
if( ffimport_file( ptr+1, &file_expr, status ) ) return(*status);
ptr = file_expr;
while (*ptr == ' ')
ptr++; /* skip leading white space... again */
}
if (*ptr == '(' )
{
/* this must be the opening parenthesis around a list of column */
/* names, optionally followed by a '=' and the binning spec. */
for (ii = 0; ii < 4; ii++)
{
ptr++; /* skip over the '(', ',', or ' ') */
while (*ptr == ' ') /* skip over blanks */
ptr++;
slen = strcspn(ptr, " ,)");
strncat(colname[ii], ptr, slen); /* copy 1st column name */
ptr += slen;
while (*ptr == ' ') /* skip over blanks */
ptr++;
if (*ptr == ')' ) /* end of the list of names */
{
*histaxis = ii + 1;
break;
}
}
if (ii == 4) /* too many names in the list , or missing ')' */
{
ffpmsg(
"binning specification has too many column names or is missing closing ')':");
ffpmsg(binspec);
if( file_expr ) free( file_expr );
return(*status = URL_PARSE_ERROR);
}
ptr++; /* skip over the closing parenthesis */
while (*ptr == ' ') /* skip over blanks */
ptr++;
if (*ptr == '\0') {
if( file_expr ) free( file_expr );
return(*status); /* parsed the entire string */
}
else if (*ptr != '=') /* must be an equals sign now*/
{
ffpmsg("illegal binning specification in URL:");
ffpmsg(" an equals sign '=' must follow the column names");
ffpmsg(binspec);
if( file_expr ) free( file_expr );
return(*status = URL_PARSE_ERROR);
}
ptr++; /* skip over the equals sign */
while (*ptr == ' ') /* skip over blanks */
ptr++;
/* get the single range specification for all the columns */
ffbinr(&ptr, tmpname, minin,
maxin, binsizein, minname[0],
maxname[0], binname[0], status);
if (*status > 0)
{
ffpmsg("illegal binning specification in URL:");
ffpmsg(binspec);
if( file_expr ) free( file_expr );
return(*status);
}
for (ii = 1; ii < *histaxis; ii++)
{
minin[ii] = minin[0];
maxin[ii] = maxin[0];
binsizein[ii] = binsizein[0];
strcpy(minname[ii], minname[0]);
strcpy(maxname[ii], maxname[0]);
strcpy(binname[ii], binname[0]);
}
while (*ptr == ' ') /* skip over blanks */
ptr++;
if (*ptr == ';')
goto getweight; /* a weighting factor is specified */
if (*ptr != '\0') /* must have reached end of string */
{
ffpmsg("illegal syntax after binning range specification in URL:");
ffpmsg(binspec);
if( file_expr ) free( file_expr );
return(*status = URL_PARSE_ERROR);
}
return(*status);
} /* end of case with list of column names in ( ) */
/* if we've reached this point, then the binning specification */
/* must be of the form: XCOL = min:max:binsize, YCOL = ... */
/* where the column name followed by '=' are optional. */
/* If the column name is not specified, then use the default name */
for (ii = 0; ii < 4; ii++) /* allow up to 4 histogram dimensions */
{
ffbinr(&ptr, colname[ii], &minin[ii],
&maxin[ii], &binsizein[ii], minname[ii],
maxname[ii], binname[ii], status);
if (*status > 0)
{
ffpmsg("illegal syntax in binning range specification in URL:");
ffpmsg(binspec);
if( file_expr ) free( file_expr );
return(*status);
}
if (*ptr == '\0' || *ptr == ';')
break; /* reached the end of the string */
if (*ptr == ' ')
{
while (*ptr == ' ') /* skip over blanks */
ptr++;
if (*ptr == '\0' || *ptr == ';')
break; /* reached the end of the string */
if (*ptr == ',')
ptr++; /* comma separates the next column specification */
}
else if (*ptr == ',')
{
ptr++; /* comma separates the next column specification */
}
else
{
ffpmsg("illegal characters following binning specification in URL:");
ffpmsg(binspec);
if( file_expr ) free( file_expr );
return(*status = URL_PARSE_ERROR);
}
}
if (ii == 4)
{
/* there are yet more characters in the string */
ffpmsg("illegal binning specification in URL:");
ffpmsg("apparently greater than 4 histogram dimensions");
ffpmsg(binspec);
return(*status = URL_PARSE_ERROR);
}
else
*histaxis = ii + 1;
/* special case: if a single number was entered it should be */
/* interpreted as the binning factor for the default X and Y axes */
if (*histaxis == 1 && *colname[0] == '\0' &&
minin[0] == DOUBLENULLVALUE && maxin[0] == DOUBLENULLVALUE)
{
*histaxis = 2;
binsizein[1] = binsizein[0];
}
getweight:
if (*ptr == ';') /* looks like a weighting factor is given */
{
ptr++;
while (*ptr == ' ') /* skip over blanks */
ptr++;
recip = 0;
if (*ptr == '/')
{
*recip = 1; /* the reciprocal of the weight is entered */
ptr++;
while (*ptr == ' ') /* skip over blanks */
ptr++;
}
/* parse the weight as though it were a binrange. */
/* either a column name or a numerical value will be returned */
ffbinr(&ptr, wtname, &dummy, &dummy, wt, tmpname,
tmpname, tmpname, status);
if (*status > 0)
{
ffpmsg("illegal binning weight specification in URL:");
ffpmsg(binspec);
if( file_expr ) free( file_expr );
return(*status);
}
/* creat a float datatype histogram by default, if weight */
/* factor is not = 1.0 */
if ( (defaulttype && *wt != 1.0) || (defaulttype && *wtname) )
*imagetype = TFLOAT;
}
while (*ptr == ' ') /* skip over blanks */
ptr++;
if (*ptr != '\0') /* should have reached the end of string */
{
ffpmsg("illegal syntax after binning weight specification in URL:");
ffpmsg(binspec);
*status = URL_PARSE_ERROR;
}
if( file_expr ) free( file_expr );
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffbinr(char **ptr,
char *colname,
double *minin,
double *maxin,
double *binsizein,
char *minname,
char *maxname,
char *binname,
int *status)
/*
Parse the input binning range specification string, returning
the column name, histogram min and max values, and bin size.
*/
{
int slen, isanumber=0;
char *token=0;
if (*status > 0)
return(*status);
slen = fits_get_token2(ptr, " ,=:;", &token, &isanumber, status); /* get 1st token */
if ((*status) || (slen == 0 && (**ptr == '\0' || **ptr == ',' || **ptr == ';')) )
return(*status); /* a null range string */
if (!isanumber && **ptr != ':')
{
/* this looks like the column name */
/* Check for case where col name string is empty but '='
is still there (indicating a following specification string).
Musn't enter this block as token would not have been allocated. */
if (token)
{
if (strlen(token) > FLEN_VALUE-1)
{
ffpmsg("column name too long (ffbinr)");
free(token);
return(*status=PARSE_SYNTAX_ERR);
}
if (token[0] == '#' && isdigit((int) token[1]) )
{
/* omit the leading '#' in the column number */
strcpy(colname, token+1);
}
else
strcpy(colname, token);
free(token);
token=0;
}
while (**ptr == ' ') /* skip over blanks */
(*ptr)++;
if (**ptr != '=')
return(*status); /* reached the end */
(*ptr)++; /* skip over the = sign */
while (**ptr == ' ') /* skip over blanks */
(*ptr)++;
/* get specification info */
slen = fits_get_token2(ptr, " ,:;", &token, &isanumber, status);
if (*status)
return(*status);
}
if (**ptr != ':')
{
/* This is the first token, and since it is not followed by
a ':' this must be the binsize token. Or it could be empty. */
if (token)
{
if (!isanumber)
{
if (strlen(token) > FLEN_VALUE-1)
{
ffpmsg("binname too long (ffbinr)");
free(token);
return(*status=PARSE_SYNTAX_ERR);
}
strcpy(binname, token);
}
else
*binsizein = strtod(token, NULL);
free(token);
}
return(*status); /* reached the end */
}
else
{
/* the token contains the min value */
if (slen)
{
if (!isanumber)
{
if (strlen(token) > FLEN_VALUE-1)
{
ffpmsg("minname too long (ffbinr)");
free(token);
return(*status=PARSE_SYNTAX_ERR);
}
strcpy(minname, token);
}
else
*minin = strtod(token, NULL);
free(token);
token=0;
}
}
(*ptr)++; /* skip the colon between the min and max values */
slen = fits_get_token2(ptr, " ,:;", &token, &isanumber, status); /* get token */
if (*status)
return(*status);
/* the token contains the max value */
if (slen)
{
if (!isanumber)
{
if (strlen(token) > FLEN_VALUE-1)
{
ffpmsg("maxname too long (ffbinr)");
free(token);
return(*status=PARSE_SYNTAX_ERR);
}
strcpy(maxname, token);
}
else
*maxin = strtod(token, NULL);
free(token);
token=0;
}
if (**ptr != ':')
{
free(token);
return(*status); /* reached the end; no binsize token */
}
(*ptr)++; /* skip the colon between the max and binsize values */
slen = fits_get_token2(ptr, " ,:;", &token, &isanumber, status); /* get token */
if (*status)
return(*status);
/* the token contains the binsize value */
if (slen)
{
if (!isanumber)
{
if (strlen(token) > FLEN_VALUE-1)
{
ffpmsg("binname too long (ffbinr)");
free(token);
return(*status=PARSE_SYNTAX_ERR);
}
strcpy(binname, token);
}
else
*binsizein = strtod(token, NULL);
free(token);
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffhist2(fitsfile **fptr, /* IO - pointer to table with X and Y cols; */
/* on output, points to histogram image */
char *outfile, /* I - name for the output histogram file */
int imagetype, /* I - datatype for image: TINT, TSHORT, etc */
int naxis, /* I - number of axes in the histogram image */
char colname[4][FLEN_VALUE], /* I - column names */
double *minin, /* I - minimum histogram value, for each axis */
double *maxin, /* I - maximum histogram value, for each axis */
double *binsizein, /* I - bin size along each axis */
char minname[4][FLEN_VALUE], /* I - optional keywords for min */
char maxname[4][FLEN_VALUE], /* I - optional keywords for max */
char binname[4][FLEN_VALUE], /* I - optional keywords for binsize */
double weightin, /* I - binning weighting factor */
char wtcol[FLEN_VALUE], /* I - optional keyword or col for weight*/
int recip, /* I - use reciprocal of the weight? */
char *selectrow, /* I - optional array (length = no. of */
/* rows in the table). If the element is true */
/* then the corresponding row of the table will*/
/* be included in the histogram, otherwise the */
/* row will be skipped. Ingnored if *selectrow*/
/* is equal to NULL. */
int *status)
{
fitsfile *histptr;
int bitpix, colnum[4], wtcolnum;
long haxes[4];
double amin[4], amax[4], binsize[4], weight;
if (*status > 0)
return(*status);
if (naxis > 4)
{
ffpmsg("histogram has more than 4 dimensions");
return(*status = BAD_DIMEN);
}
/* reset position to the correct HDU if necessary */
if ((*fptr)->HDUposition != ((*fptr)->Fptr)->curhdu)
ffmahd(*fptr, ((*fptr)->HDUposition) + 1, NULL, status);
if (imagetype == TBYTE)
bitpix = BYTE_IMG;
else if (imagetype == TSHORT)
bitpix = SHORT_IMG;
else if (imagetype == TINT)
bitpix = LONG_IMG;
else if (imagetype == TFLOAT)
bitpix = FLOAT_IMG;
else if (imagetype == TDOUBLE)
bitpix = DOUBLE_IMG;
else
return(*status = BAD_DATATYPE);
/* Calculate the binning parameters: */
/* columm numbers, axes length, min values, max values, and binsizes. */
if (fits_calc_binningd(
*fptr, naxis, colname, minin, maxin, binsizein, minname, maxname, binname,
colnum, haxes, amin, amax, binsize, status) > 0)
{
ffpmsg("failed to determine binning parameters");
return(*status);
}
/* get the histogramming weighting factor, if any */
if (*wtcol)
{
/* first, look for a keyword with the weight value */
if (ffgky(*fptr, TDOUBLE, wtcol, &weight, NULL, status) )
{
/* not a keyword, so look for column with this name */
*status = 0;
/* get the column number in the table */
if (ffgcno(*fptr, CASEINSEN, wtcol, &wtcolnum, status) > 0)
{
ffpmsg(
"keyword or column for histogram weights doesn't exist: ");
ffpmsg(wtcol);
return(*status);
}
weight = DOUBLENULLVALUE;
}
}
else
weight = (double) weightin;
if (weight <= 0. && weight != DOUBLENULLVALUE)
{
ffpmsg("Illegal histogramming weighting factor <= 0.");
return(*status = URL_PARSE_ERROR);
}
if (recip && weight != DOUBLENULLVALUE)
/* take reciprocal of weight */
weight = (double) (1.0 / weight);
/* size of histogram is now known, so create temp output file */
if (fits_create_file(&histptr, outfile, status) > 0)
{
ffpmsg("failed to create temp output file for histogram");
return(*status);
}
/* create output FITS image HDU */
if (ffcrim(histptr, bitpix, naxis, haxes, status) > 0)
{
ffpmsg("failed to create output histogram FITS image");
return(*status);
}
/* copy header keywords, converting pixel list WCS keywords to image WCS form */
if (fits_copy_pixlist2image(*fptr, histptr, 9, naxis, colnum, status) > 0)
{
ffpmsg("failed to copy pixel list keywords to new histogram header");
return(*status);
}
/* if the table columns have no WCS keywords, then write default keywords */
fits_write_keys_histo(*fptr, histptr, naxis, colnum, status);
/* update the WCS keywords for the ref. pixel location, and pixel size */
fits_rebin_wcsd(histptr, naxis, amin, binsize, status);
/* now compute the output image by binning the column values */
if (fits_make_histd(*fptr, histptr, bitpix, naxis, haxes, colnum, amin, amax,
binsize, weight, wtcolnum, recip, selectrow, status) > 0)
{
ffpmsg("failed to calculate new histogram values");
return(*status);
}
/* finally, close the original file and return ptr to the new image */
ffclos(*fptr, status);
*fptr = histptr;
return(*status);
}
/*--------------------------------------------------------------------------*/
/* ffhist3: same as ffhist2, but does not close the original file */
/* and/or replace the original file pointer */
fitsfile *ffhist3(fitsfile *fptr, /* I - ptr to table with X and Y cols*/
char *outfile, /* I - name for the output histogram file */
int imagetype, /* I - datatype for image: TINT, TSHORT, etc */
int naxis, /* I - number of axes in the histogram image */
char colname[4][FLEN_VALUE], /* I - column names */
double *minin, /* I - minimum histogram value, for each axis */
double *maxin, /* I - maximum histogram value, for each axis */
double *binsizein, /* I - bin size along each axis */
char minname[4][FLEN_VALUE], /* I - optional keywords for min */
char maxname[4][FLEN_VALUE], /* I - optional keywords for max */
char binname[4][FLEN_VALUE], /* I - optional keywords for binsize */
double weightin, /* I - binning weighting factor */
char wtcol[FLEN_VALUE], /* I - optional keyword or col for weight*/
int recip, /* I - use reciprocal of the weight? */
char *selectrow, /* I - optional array (length = no. of */
/* rows in the table). If the element is true */
/* then the corresponding row of the table will*/
/* be included in the histogram, otherwise the */
/* row will be skipped. Ingnored if *selectrow*/
/* is equal to NULL. */
int *status)
{
fitsfile *histptr;
int bitpix, colnum[4], wtcolnum;
long haxes[4];
double amin[4], amax[4], binsize[4], weight;
if (*status > 0)
return(NULL);
if (naxis > 4)
{
ffpmsg("histogram has more than 4 dimensions");
*status = BAD_DIMEN;
return(NULL);
}
/* reset position to the correct HDU if necessary */
if ((fptr)->HDUposition != ((fptr)->Fptr)->curhdu)
ffmahd(fptr, ((fptr)->HDUposition) + 1, NULL, status);
if (imagetype == TBYTE)
bitpix = BYTE_IMG;
else if (imagetype == TSHORT)
bitpix = SHORT_IMG;
else if (imagetype == TINT)
bitpix = LONG_IMG;
else if (imagetype == TFLOAT)
bitpix = FLOAT_IMG;
else if (imagetype == TDOUBLE)
bitpix = DOUBLE_IMG;
else{
*status = BAD_DATATYPE;
return(NULL);
}
/* Calculate the binning parameters: */
/* columm numbers, axes length, min values, max values, and binsizes. */
if (fits_calc_binningd(
fptr, naxis, colname, minin, maxin, binsizein, minname, maxname, binname,
colnum, haxes, amin, amax, binsize, status) > 0)
{
ffpmsg("failed to determine binning parameters");
return(NULL);
}
/* get the histogramming weighting factor, if any */
if (*wtcol)
{
/* first, look for a keyword with the weight value */
if (fits_read_key(fptr, TDOUBLE, wtcol, &weight, NULL, status) )
{
/* not a keyword, so look for column with this name */
*status = 0;
/* get the column number in the table */
if (ffgcno(fptr, CASEINSEN, wtcol, &wtcolnum, status) > 0)
{
ffpmsg(
"keyword or column for histogram weights doesn't exist: ");
ffpmsg(wtcol);
return(NULL);
}
weight = DOUBLENULLVALUE;
}
}
else
weight = (double) weightin;
if (weight <= 0. && weight != DOUBLENULLVALUE)
{
ffpmsg("Illegal histogramming weighting factor <= 0.");
*status = URL_PARSE_ERROR;
return(NULL);
}
if (recip && weight != DOUBLENULLVALUE)
/* take reciprocal of weight */
weight = (double) (1.0 / weight);
/* size of histogram is now known, so create temp output file */
if (fits_create_file(&histptr, outfile, status) > 0)
{
ffpmsg("failed to create temp output file for histogram");
return(NULL);
}
/* create output FITS image HDU */
if (ffcrim(histptr, bitpix, naxis, haxes, status) > 0)
{
ffpmsg("failed to create output histogram FITS image");
return(NULL);
}
/* copy header keywords, converting pixel list WCS keywords to image WCS */
if (fits_copy_pixlist2image(fptr, histptr, 9, naxis, colnum, status) > 0)
{
ffpmsg("failed to copy pixel list keywords to new histogram header");
return(NULL);
}
/* if the table columns have no WCS keywords, then write default keywords */
fits_write_keys_histo(fptr, histptr, naxis, colnum, status);
/* update the WCS keywords for the ref. pixel location, and pixel size */
fits_rebin_wcsd(histptr, naxis, amin, binsize, status);
/* now compute the output image by binning the column values */
if (fits_make_histd(fptr, histptr, bitpix, naxis, haxes, colnum, amin, amax,
binsize, weight, wtcolnum, recip, selectrow, status) > 0)
{
ffpmsg("failed to calculate new histogram values");
return(NULL);
}
return(histptr);
}
/*--------------------------------------------------------------------------*/
int ffhist(fitsfile **fptr, /* IO - pointer to table with X and Y cols; */
/* on output, points to histogram image */
char *outfile, /* I - name for the output histogram file */
int imagetype, /* I - datatype for image: TINT, TSHORT, etc */
int naxis, /* I - number of axes in the histogram image */
char colname[4][FLEN_VALUE], /* I - column names */
double *minin, /* I - minimum histogram value, for each axis */
double *maxin, /* I - maximum histogram value, for each axis */
double *binsizein, /* I - bin size along each axis */
char minname[4][FLEN_VALUE], /* I - optional keywords for min */
char maxname[4][FLEN_VALUE], /* I - optional keywords for max */
char binname[4][FLEN_VALUE], /* I - optional keywords for binsize */
double weightin, /* I - binning weighting factor */
char wtcol[FLEN_VALUE], /* I - optional keyword or col for weight*/
int recip, /* I - use reciprocal of the weight? */
char *selectrow, /* I - optional array (length = no. of */
/* rows in the table). If the element is true */
/* then the corresponding row of the table will*/
/* be included in the histogram, otherwise the */
/* row will be skipped. Ingnored if *selectrow*/
/* is equal to NULL. */
int *status)
{
int ii, datatype, repeat, imin, imax, ibin, bitpix, tstatus, use_datamax = 0;
long haxes[4];
fitsfile *histptr;
char errmsg[FLEN_ERRMSG], keyname[FLEN_KEYWORD], card[FLEN_CARD];
tcolumn *colptr;
iteratorCol imagepars[1];
int n_cols = 1, nkeys;
long offset = 0;
long n_per_loop = -1; /* force whole array to be passed at one time */
histType histData; /* Structure holding histogram info for iterator */
double amin[4], amax[4], binsize[4], maxbin[4];
double datamin = DOUBLENULLVALUE, datamax = DOUBLENULLVALUE;
char svalue[FLEN_VALUE];
double dvalue;
char cpref[4][FLEN_VALUE];
char *cptr;
if (*status > 0)
return(*status);
if (naxis > 4)
{
ffpmsg("histogram has more than 4 dimensions");
return(*status = BAD_DIMEN);
}
/* reset position to the correct HDU if necessary */
if ((*fptr)->HDUposition != ((*fptr)->Fptr)->curhdu)
ffmahd(*fptr, ((*fptr)->HDUposition) + 1, NULL, status);
histData.tblptr = *fptr;
histData.himagetype = imagetype;
histData.haxis = naxis;
histData.rowselector = selectrow;
if (imagetype == TBYTE)
bitpix = BYTE_IMG;
else if (imagetype == TSHORT)
bitpix = SHORT_IMG;
else if (imagetype == TINT)
bitpix = LONG_IMG;
else if (imagetype == TFLOAT)
bitpix = FLOAT_IMG;
else if (imagetype == TDOUBLE)
bitpix = DOUBLE_IMG;
else
return(*status = BAD_DATATYPE);
/* The CPREF keyword, if it exists, gives the preferred columns. */
/* Otherwise, assume "X", "Y", "Z", and "T" */
tstatus = 0;
ffgky(*fptr, TSTRING, "CPREF", cpref[0], NULL, &tstatus);
if (!tstatus)
{
/* Preferred column names are given; separate them */
cptr = cpref[0];
/* the first preferred axis... */
while (*cptr != ',' && *cptr != '\0')
cptr++;
if (*cptr != '\0')
{
*cptr = '\0';
cptr++;
while (*cptr == ' ')
cptr++;
strcpy(cpref[1], cptr);
cptr = cpref[1];
/* the second preferred axis... */
while (*cptr != ',' && *cptr != '\0')
cptr++;
if (*cptr != '\0')
{
*cptr = '\0';
cptr++;
while (*cptr == ' ')
cptr++;
strcpy(cpref[2], cptr);
cptr = cpref[2];
/* the third preferred axis... */
while (*cptr != ',' && *cptr != '\0')
cptr++;
if (*cptr != '\0')
{
*cptr = '\0';
cptr++;
while (*cptr == ' ')
cptr++;
strcpy(cpref[3], cptr);
}
}
}
}
for (ii = 0; ii < naxis; ii++)
{
/* get the min, max, and binsize values from keywords, if specified */
if (*minname[ii])
{
if (ffgky(*fptr, TDOUBLE, minname[ii], &minin[ii], NULL, status) )
{
ffpmsg("error reading histogramming minimum keyword");
ffpmsg(minname[ii]);
return(*status);
}
}
if (*maxname[ii])
{
if (ffgky(*fptr, TDOUBLE, maxname[ii], &maxin[ii], NULL, status) )
{
ffpmsg("error reading histogramming maximum keyword");
ffpmsg(maxname[ii]);
return(*status);
}
}
if (*binname[ii])
{
if (ffgky(*fptr, TDOUBLE, binname[ii], &binsizein[ii], NULL, status) )
{
ffpmsg("error reading histogramming binsize keyword");
ffpmsg(binname[ii]);
return(*status);
}
}
if (binsizein[ii] == 0.)
{
ffpmsg("error: histogram binsize = 0");
return(*status = ZERO_SCALE);
}
if (*colname[ii] == '\0')
{
strcpy(colname[ii], cpref[ii]); /* try using the preferred column */
if (*colname[ii] == '\0')
{
if (ii == 0)
strcpy(colname[ii], "X");
else if (ii == 1)
strcpy(colname[ii], "Y");
else if (ii == 2)
strcpy(colname[ii], "Z");
else if (ii == 3)
strcpy(colname[ii], "T");
}
}
/* get the column number in the table */
if (ffgcno(*fptr, CASEINSEN, colname[ii], histData.hcolnum+ii, status)
> 0)