-
Notifications
You must be signed in to change notification settings - Fork 1
/
histo.c
3252 lines (2792 loc) · 111 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"
#include "eval_defs.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;
long incr[5];
int wtrecip, wtcolnum;
char *wtexpr;
double weight;
char *rowselector;
char *rowselector_cur;
long repeat;
int startCols[5];
int numIterCols;
iteratorCol *iterCols;
ParseData *parsers;
parseInfo *infos;
} histType;
/*--------------------------------------------------------------------------*/
int ffbinse(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? */
char ***exprs, /* returned with expressions (or 0) */
int *status)
{
/*
Parse the extended 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
bin x(expr), y(expr)=min:max:size, ...
most other reasonable combinations are supported. The (expr) is an
optional expression that will be calculated on the fly instead of
a table column name. The name is still used for the output pixel
array metadata.
If expr == 0, then expressions are forbidden. The caller does not
expect expressions.
If exprs is non-zero, then upon return an array of expressions is
passed back to the caller. Storage may be allocated by this routine,
If *exprs is non-zero upon return, the caller is responsible to
free(*exprs). Upon return, the contains of exprs is,
(*exprs)[0] = expression for column 1 (or 0 if none)
(*exprs)[1] = expression for column 2 (or 0 if none)
(*exprs)[2] = expression for column 3 (or 0 if none)
(*exprs)[3] = expression for column 4 (or 0 if none)
(*exprs)[4] = expression for weighting (or 0 if none)
If the user specifies a column name and not an expression for bin
axis i, then the corresponding (*exprs)[i] will be a null pointer.
To be recognized as an expression, the weighting expression must be
enclosed in parentheses.
Expressions are never allowed using the bin (xcol,ycol) notation.
*/
int ii, slen, defaulttype;
char *ptr, tmpname[FLEN_VALUE], *file_expr = NULL;
double dummy;
char *exprbeg[5] = {0}, *exprend[5] = {0};
int has_exprs = 0;
if (exprs) (*exprs) = 0; /* initialized output */
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 */
/* Note that the extended syntax is not allowed here */
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 */
{
exprbeg[ii] = exprend[ii] = 0;
ffbinre(&ptr, colname[ii], &(exprbeg[ii]), &(exprend[ii]),
&minin[ii], &maxin[ii], &binsizein[ii], minname[ii],
maxname[ii], binname[ii], status);
/* Check for expressions */
if (exprbeg[ii]) has_exprs = 1;
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 */
exprbeg[4] = exprend[4] = 0;
ffbinre(&ptr, wtname, &(exprbeg[4]), &(exprend[4]),
&dummy, &dummy, wt, tmpname,
tmpname, tmpname, status);
if (exprbeg[4]) has_exprs = 1;
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) ||
(defaulttype && exprbeg[4])) {
*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 );
/* If we found expressions, this is where we accumulate them into
something to be returned to the caller. The start and end of
each expression will be found in exprbeg[] and exprend[], with
the 5th entry being the weight expression if any */
if (has_exprs) {
size_t nchars = 0;
char *ptr;
for (ii = 0; ii <= 4; ii++) {
nchars += (exprend[ii] - exprbeg[ii]) + 1; /* null terminator */
}
/* Allocate storage for 5 pointers plus the characters. Caller
is responsible to free(*exprs) which will free both the 5-array
and the character string data. */
ptr = malloc( sizeof(char *) * 5 + nchars * sizeof(char) );
if (!ptr) {
ffpmsg("ffbinse: memory allocation failure");
return(*status = MEMORY_ALLOCATION);
}
(*exprs) = (char **) ptr; /* Pointer array portion */
ptr = (char *) (&((*exprs)[5])); /* String portion starts after the pointer array */
for (ii = 0; ii <= 4; ii++) {
(*exprs)[ii] = ptr;
nchars = (exprend[ii]-exprbeg[ii]);
strncpy(ptr, exprbeg[ii], nchars);
ptr += nchars;
ptr[0] = 0; /* Ensure null terminator */
ptr ++; /* Advance to next string position */
}
}
return(*status);
}
/*--------------------------------------------------------------------------*/
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 non-extended expression, but otherwise the same as ffbinse() */
return ffbinse(binspec,
imagetype, histaxis, colname,
minin, maxin, binsizein,
minname, maxname, binname,
wt, wtname, recip,
0, /* No exprs pointer */
status);
}
/*--------------------------------------------------------------------------*/
int ffbinre(char **ptr,
char *colname,
char **exprbeg, char **exprend,
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.
This is the "extended" binning syntax that allows for an expression
of the form XCOL(expr). The expression must be enclosed in parentheses.
The start and end of the expression are returned in *exprbeg and *exprend.
If exprbeg and exprend are null pointers then the expression is forbidden.
*/
{
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)++;
/* An optional expression of the form XCOL(expr) is allowed here, but only
if exprbeg and exprend are non-null */
if (**ptr == '(' && exprbeg && exprend) {
*exprbeg = *ptr;
if ((*exprend = fits_find_match_delim(*ptr+1,')')) == 0) { /* find ')' */
ffpmsg("bin expression syntax error (ffbinr)");
return(*status=PARSE_SYNTAX_ERR);
}
*ptr = *exprend; /* Advance pointer past delimeter */
}
while (**ptr == ' ') (*ptr)++; /* skip over more possible blanks */
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 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.
This is the non-extended version of the parser which disallows
binning expressions. Only column names are allowed.
*/
{
return ffbinre(ptr, colname, 0, 0,
minin, maxin, binsizein,
minname, maxname, binname,
status);
}
/*--------------------------------------------------------------------------*/
int ffhist2e(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 */
char *colexpr[4], /* I - optionally, expression intead of colum */
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*/
char *wtexpr, /* I - optionally, weight expression */
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;
int numIterCols = 0;
int datatypes[4], wtdatatype = 0;
long *repeat, wtrepeat = 0;
char errmsg[FLEN_ERRMSG];
long vectorRepeat;
if (*status > 0)
return(*status);
if (naxis > 4)
{
ffpmsg("histogram has more than 4 dimensions");
*status = BAD_DIMEN;
goto cleanup;
}
/* 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;
goto cleanup;
}
/* Calculate the binning parameters: */
/* columm numbers, axes length, min values, max values, and binsizes. */
if (fits_calc_binningde(
*fptr, naxis, colname, colexpr,
minin, maxin, binsizein, minname, maxname, binname,
colnum, datatypes, haxes, amin, amax, binsize,
&vectorRepeat, status) > 0)
{
ffpmsg("failed to determine binning parameters");
goto cleanup;
}
/* 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) == 0)
{
/* Data type if keyword was found */
wtdatatype = TDOUBLE;
wtrepeat = 1;
}
else
{
/* 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);
goto cleanup;
}
/* get the datatype of the column */
fits_get_eqcoltype(*fptr, wtcolnum, &wtdatatype,
&wtrepeat, NULL, status);
weight = DOUBLENULLVALUE;
}
}
else if (wtexpr && wtexpr[0]) /* A weighting expression - always TDOUBLE */
{
/* Initialize the parser so that we can determine the datatype
of the returned type as well as the vector dimensions. The
parsers is kept allocated so we can assemble an iterator that
uses it below.
*/
int naxis1;
long int nelem, naxes[MAXDIMS];
ParseData lParse;
ffiprs( *fptr, 0, wtexpr, MAXDIMS, &wtdatatype, &nelem, &naxis1,
naxes, &lParse, status );
ffcprs( &lParse );
if (nelem < 0) nelem = 1; /* If it's a constant expression */
weight = DOUBLENULLVALUE;
wtrepeat = nelem;
wtdatatype = wtdatatype;
}
else
{
weight = (double) weightin;
wtrepeat = vectorRepeat;
wtdatatype = TDOUBLE;
}
/* Make sure weighting column is not an un-binnable data type */
if (wtdatatype < 0 || wtdatatype == TSTRING || wtdatatype == TBIT ||
wtdatatype == TLOGICAL) {
ffpmsg("Invalid datatype for bin weighting factor");
*status = BAD_DATATYPE;
goto cleanup;
}
/* And dimensions of weighting must agree with input column data */
if (wtrepeat != vectorRepeat) {
ffpmsg("Vector dimensions of weighting do not agree with binning columns");
*status = BAD_DIMEN;
goto cleanup;
}
if (weight <= 0. && weight != DOUBLENULLVALUE)
{
ffpmsg("Illegal histogramming weighting factor <= 0.");
*status = URL_PARSE_ERROR;
goto cleanup;
}
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");
goto cleanup;
}
/* create output FITS image HDU */
if (ffcrim(histptr, bitpix, naxis, haxes, status) > 0)
{
ffpmsg("failed to create output histogram FITS image");
goto cleanup;
}
/* 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");
goto cleanup;
}
/* if the table columns have no WCS keywords, then write default keywords */
fits_write_keys_histoe(*fptr, histptr, naxis, colnum, colname, colexpr, 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_histde(*fptr, histptr, datatypes, bitpix, naxis, haxes,
colnum, colexpr, amin, amax, binsize,
weight, wtcolnum, wtexpr, recip,
selectrow, status) > 0)
{
ffpmsg("failed to calculate new histogram values");
goto cleanup;
}
/* finally, close the original file and return ptr to the new image */
ffclos(*fptr, status);
*fptr = histptr;
cleanup:
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)
{
/* Non-extended-syntax version of ffhist2e() */
return ffhist2e(fptr, outfile, imagetype, naxis, colname, 0,
minin, maxin, binsizein,
minname, maxname, binname,
weightin, wtcol, 0, recip, selectrow, 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) )