-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_f.c
2839 lines (2517 loc) · 96.5 KB
/
eval_f.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
/************************************************************************/
/* */
/* CFITSIO Lexical Parser */
/* */
/* This file is one of 3 files containing code which parses an */
/* arithmetic expression and evaluates it in the context of an input */
/* FITS file table extension. The CFITSIO lexical parser is divided */
/* into the following 3 parts/files: the CFITSIO "front-end", */
/* eval_f.c, contains the interface between the user/CFITSIO and the */
/* real core of the parser; the FLEX interpreter, eval_l.c, takes the */
/* input string and parses it into tokens and identifies the FITS */
/* information required to evaluate the expression (ie, keywords and */
/* columns); and, the BISON grammar and evaluation routines, eval_y.c, */
/* receives the FLEX output and determines and performs the actual */
/* operations. The files eval_l.c and eval_y.c are produced from */
/* running flex and bison on the files eval.l and eval.y, respectively. */
/* (flex and bison are available from any GNU archive: see www.gnu.org) */
/* */
/* The grammar rules, rather than evaluating the expression in situ, */
/* builds a tree, or Nodal, structure mapping out the order of */
/* operations and expression dependencies. This "compilation" process */
/* allows for much faster processing of multiple rows. This technique */
/* was developed by Uwe Lammers of the XMM Science Analysis System, */
/* although the CFITSIO implementation is entirely code original. */
/* */
/* */
/* Modification History: */
/* */
/* Kent Blackburn c1992 Original parser code developed for the */
/* FTOOLS software package, in particular, */
/* the fselect task. */
/* Kent Blackburn c1995 BIT column support added */
/* Peter D Wilson Feb 1998 Vector column support added */
/* Peter D Wilson May 1998 Ported to CFITSIO library. User */
/* interface routines written, in essence */
/* making fselect, fcalc, and maketime */
/* capabilities available to all tools */
/* via single function calls. */
/* Peter D Wilson Jun 1998 Major rewrite of parser core, so as to */
/* create a run-time evaluation tree, */
/* inspired by the work of Uwe Lammers, */
/* resulting in a speed increase of */
/* 10-100 times. */
/* Peter D Wilson Jul 1998 gtifilter(a,b,c,d) function added */
/* Peter D Wilson Aug 1998 regfilter(a,b,c,d) function added */
/* Peter D Wilson Jul 1999 Make parser fitsfile-independent, */
/* allowing a purely vector-based usage */
/* Peter D Wilson Aug 1999 Add row-offset capability */
/* Peter D Wilson Sep 1999 Add row-range capability to ffcalc_rng */
/* */
/************************************************************************/
#include <limits.h>
#include <ctype.h>
#include "eval_defs.h"
#include "region.h"
typedef struct {
int datatype; /* Data type to cast parse results into for user */
void *dataPtr; /* Pointer to array of results, NULL if to use iterCol */
void *nullPtr; /* Pointer to nulval, use zero if NULL */
long maxRows; /* Max No. of rows to process, -1=all, 0=1 iteration */
int anyNull; /* Flag indicating at least 1 undef value encountered */
} parseInfo;
/* Internal routines needed to allow the evaluator to operate on FITS data */
static void Setup_DataArrays( int nCols, iteratorCol *cols,
long fRow, long nRows );
static int find_column( char *colName, void *itslval );
static int find_keywd ( char *key, void *itslval );
static int allocateCol( int nCol, int *status );
static int load_column( int varNum, long fRow, long nRows,
void *data, char *undef );
static int DEBUG_PIXFILTER;
#define FREE(x) { if (x) free(x); else printf("invalid free(" #x ") at %s:%d\n", __FILE__, __LINE__); }
/*---------------------------------------------------------------------------*/
int fffrow( fitsfile *fptr, /* I - Input FITS file */
char *expr, /* I - Boolean expression */
long firstrow, /* I - First row of table to eval */
long nrows, /* I - Number of rows to evaluate */
long *n_good_rows, /* O - Number of rows eval to True */
char *row_status, /* O - Array of boolean results */
int *status ) /* O - Error status */
/* */
/* Evaluate a boolean expression using the indicated rows, returning an */
/* array of flags indicating which rows evaluated to TRUE/FALSE */
/*---------------------------------------------------------------------------*/
{
parseInfo Info;
int naxis, constant;
long nelem, naxes[MAXDIMS], elem;
char result;
if( *status ) return( *status );
FFLOCK;
if( ffiprs( fptr, 0, expr, MAXDIMS, &Info.datatype, &nelem, &naxis,
naxes, status ) ) {
ffcprs();
FFUNLOCK;
return( *status );
}
if( nelem<0 ) {
constant = 1;
nelem = -nelem;
} else
constant = 0;
if( Info.datatype!=TLOGICAL || nelem!=1 ) {
ffcprs();
ffpmsg("Expression does not evaluate to a logical scalar.");
FFUNLOCK;
return( *status = PARSE_BAD_TYPE );
}
if( constant ) { /* No need to call parser... have result from ffiprs */
result = gParse.Nodes[gParse.resultNode].value.data.log;
*n_good_rows = nrows;
for( elem=0; elem<nrows; elem++ )
row_status[elem] = result;
} else {
firstrow = (firstrow>1 ? firstrow : 1);
Info.dataPtr = row_status;
Info.nullPtr = NULL;
Info.maxRows = nrows;
if( ffiter( gParse.nCols, gParse.colData, firstrow-1, 0,
parse_data, (void*)&Info, status ) == -1 )
*status = 0; /* -1 indicates exitted without error before end... OK */
if( *status ) {
/***********************/
/* Error... Do nothing */
/***********************/
} else {
/***********************************/
/* Count number of good rows found */
/***********************************/
*n_good_rows = 0L;
for( elem=0; elem<Info.maxRows; elem++ ) {
if( row_status[elem]==1 ) ++*n_good_rows;
}
}
}
ffcprs();
FFUNLOCK;
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffsrow( fitsfile *infptr, /* I - Input FITS file */
fitsfile *outfptr, /* I - Output FITS file */
char *expr, /* I - Boolean expression */
int *status ) /* O - Error status */
/* */
/* Evaluate an expression on all rows of a table. If the input and output */
/* files are not the same, copy the TRUE rows to the output file. If the */
/* files are the same, delete the FALSE rows (preserve the TRUE rows). */
/* Can copy rows between extensions of the same file, *BUT* if output */
/* extension is before the input extension, the second extension *MUST* be */
/* opened using ffreopen, so that CFITSIO can handle changing file lengths. */
/*--------------------------------------------------------------------------*/
{
parseInfo Info;
int naxis, constant;
long nelem, rdlen, naxes[MAXDIMS], maxrows, nbuff, nGood, inloc, outloc;
LONGLONG ntodo, inbyteloc, outbyteloc, hsize;
long freespace;
unsigned char *buffer, result;
struct {
LONGLONG rowLength, numRows, heapSize;
LONGLONG dataStart, heapStart;
} inExt, outExt;
if( *status ) return( *status );
FFLOCK;
if( ffiprs( infptr, 0, expr, MAXDIMS, &Info.datatype, &nelem, &naxis,
naxes, status ) ) {
ffcprs();
FFUNLOCK;
return( *status );
}
if( nelem<0 ) {
constant = 1;
nelem = -nelem;
} else
constant = 0;
/**********************************************************************/
/* Make sure expression evaluates to the right type... logical scalar */
/**********************************************************************/
if( Info.datatype!=TLOGICAL || nelem!=1 ) {
ffcprs();
ffpmsg("Expression does not evaluate to a logical scalar.");
FFUNLOCK;
return( *status = PARSE_BAD_TYPE );
}
/***********************************************************/
/* Extract various table information from each extension */
/***********************************************************/
if( infptr->HDUposition != (infptr->Fptr)->curhdu )
ffmahd( infptr, (infptr->HDUposition) + 1, NULL, status );
if( *status ) {
ffcprs();
FFUNLOCK;
return( *status );
}
inExt.rowLength = (long) (infptr->Fptr)->rowlength;
inExt.numRows = (infptr->Fptr)->numrows;
inExt.heapSize = (infptr->Fptr)->heapsize;
if( inExt.numRows == 0 ) { /* Nothing to copy */
ffcprs();
FFUNLOCK;
return( *status );
}
if( outfptr->HDUposition != (outfptr->Fptr)->curhdu )
ffmahd( outfptr, (outfptr->HDUposition) + 1, NULL, status );
if( (outfptr->Fptr)->datastart < 0 )
ffrdef( outfptr, status );
if( *status ) {
ffcprs();
FFUNLOCK;
return( *status );
}
outExt.rowLength = (long) (outfptr->Fptr)->rowlength;
outExt.numRows = (outfptr->Fptr)->numrows;
if( !outExt.numRows )
(outfptr->Fptr)->heapsize = 0L;
outExt.heapSize = (outfptr->Fptr)->heapsize;
if( inExt.rowLength != outExt.rowLength ) {
ffpmsg("Output table has different row length from input");
ffcprs();
FFUNLOCK;
return( *status = PARSE_BAD_OUTPUT );
}
/***********************************/
/* Fill out Info data for parser */
/***********************************/
Info.dataPtr = (char *)malloc( (size_t) ((inExt.numRows + 1) * sizeof(char)) );
Info.nullPtr = NULL;
Info.maxRows = (long) inExt.numRows;
if( !Info.dataPtr ) {
ffpmsg("Unable to allocate memory for row selection");
ffcprs();
FFUNLOCK;
return( *status = MEMORY_ALLOCATION );
}
/* make sure array is zero terminated */
((char*)Info.dataPtr)[inExt.numRows] = 0;
if( constant ) { /* Set all rows to the same value from constant result */
result = gParse.Nodes[gParse.resultNode].value.data.log;
for( ntodo = 0; ntodo<inExt.numRows; ntodo++ )
((char*)Info.dataPtr)[ntodo] = result;
nGood = (long) (result ? inExt.numRows : 0);
} else {
ffiter( gParse.nCols, gParse.colData, 0L, 0L,
parse_data, (void*)&Info, status );
nGood = 0;
for( ntodo = 0; ntodo<inExt.numRows; ntodo++ )
if( ((char*)Info.dataPtr)[ntodo] ) nGood++;
}
if( *status ) {
/* Error... Do nothing */
} else {
rdlen = (long) inExt.rowLength;
buffer = (unsigned char *)malloc(maxvalue(500000,rdlen) * sizeof(char) );
if( buffer==NULL ) {
ffcprs();
FFUNLOCK;
return( *status=MEMORY_ALLOCATION );
}
maxrows = maxvalue( (500000L/rdlen), 1);
nbuff = 0;
inloc = 1;
if( infptr==outfptr ) { /* Skip initial good rows if input==output file */
while( ((char*)Info.dataPtr)[inloc-1] ) inloc++;
outloc = inloc;
} else {
outloc = (long) (outExt.numRows + 1);
if (outloc > 1)
ffirow( outfptr, outExt.numRows, nGood, status );
}
do {
if( ((char*)Info.dataPtr)[inloc-1] ) {
ffgtbb( infptr, inloc, 1L, rdlen, buffer+rdlen*nbuff, status );
nbuff++;
if( nbuff==maxrows ) {
ffptbb( outfptr, outloc, 1L, rdlen*nbuff, buffer, status );
outloc += nbuff;
nbuff = 0;
}
}
inloc++;
} while( !*status && inloc<=inExt.numRows );
if( nbuff ) {
ffptbb( outfptr, outloc, 1L, rdlen*nbuff, buffer, status );
outloc += nbuff;
}
if( infptr==outfptr ) {
if( outloc<=inExt.numRows )
ffdrow( infptr, outloc, inExt.numRows-outloc+1, status );
} else if( inExt.heapSize && nGood ) {
/* Copy heap, if it exists and at least one row copied */
/********************************************************/
/* Get location information from the output extension */
/********************************************************/
if( outfptr->HDUposition != (outfptr->Fptr)->curhdu )
ffmahd( outfptr, (outfptr->HDUposition) + 1, NULL, status );
outExt.dataStart = (outfptr->Fptr)->datastart;
outExt.heapStart = (outfptr->Fptr)->heapstart;
/*************************************************/
/* Insert more space into outfptr if necessary */
/*************************************************/
hsize = outExt.heapStart + outExt.heapSize;
freespace = (long) (( ( (hsize + 2879) / 2880) * 2880) - hsize);
ntodo = inExt.heapSize;
if ( (freespace - ntodo) < 0) { /* not enough existing space? */
ntodo = (ntodo - freespace + 2879) / 2880; /* number of blocks */
ffiblk(outfptr, (long) ntodo, 1, status); /* insert the blocks */
}
ffukyj( outfptr, "PCOUNT", inExt.heapSize+outExt.heapSize,
NULL, status );
/*******************************************************/
/* Get location information from the input extension */
/*******************************************************/
if( infptr->HDUposition != (infptr->Fptr)->curhdu )
ffmahd( infptr, (infptr->HDUposition) + 1, NULL, status );
inExt.dataStart = (infptr->Fptr)->datastart;
inExt.heapStart = (infptr->Fptr)->heapstart;
/**********************************/
/* Finally copy heap to outfptr */
/**********************************/
ntodo = inExt.heapSize;
inbyteloc = inExt.heapStart + inExt.dataStart;
outbyteloc = outExt.heapStart + outExt.dataStart + outExt.heapSize;
while ( ntodo && !*status ) {
rdlen = (long) minvalue(ntodo,500000);
ffmbyt( infptr, inbyteloc, REPORT_EOF, status );
ffgbyt( infptr, rdlen, buffer, status );
ffmbyt( outfptr, outbyteloc, IGNORE_EOF, status );
ffpbyt( outfptr, rdlen, buffer, status );
inbyteloc += rdlen;
outbyteloc += rdlen;
ntodo -= rdlen;
}
/***********************************************************/
/* But must update DES if data is being appended to a */
/* pre-existing heap space. Edit each new entry in file */
/***********************************************************/
if( outExt.heapSize ) {
LONGLONG repeat, offset, j;
int i;
for( i=1; i<=(outfptr->Fptr)->tfield; i++ ) {
if( (outfptr->Fptr)->tableptr[i-1].tdatatype<0 ) {
for( j=outExt.numRows+1; j<=outExt.numRows+nGood; j++ ) {
ffgdesll( outfptr, i, j, &repeat, &offset, status );
offset += outExt.heapSize;
ffpdes( outfptr, i, j, repeat, offset, status );
}
}
}
}
} /* End of HEAP copy */
FREE(buffer);
}
FREE(Info.dataPtr);
ffcprs();
ffcmph(outfptr, status); /* compress heap, deleting any orphaned data */
FFUNLOCK;
return(*status);
}
/*---------------------------------------------------------------------------*/
int ffcrow( fitsfile *fptr, /* I - Input FITS file */
int datatype, /* I - Datatype to return results as */
char *expr, /* I - Arithmetic expression */
long firstrow, /* I - First row to evaluate */
long nelements, /* I - Number of elements to return */
void *nulval, /* I - Ptr to value to use as UNDEF */
void *array, /* O - Array of results */
int *anynul, /* O - Were any UNDEFs encountered? */
int *status ) /* O - Error status */
/* */
/* Calculate an expression for the indicated rows of a table, returning */
/* the results, cast as datatype (TSHORT, TDOUBLE, etc), in array. If */
/* nulval==NULL, UNDEFs will be zeroed out. For vector results, the number */
/* of elements returned may be less than nelements if nelements is not an */
/* even multiple of the result dimension. Call fftexp to obtain the */
/* dimensions of the results. */
/*---------------------------------------------------------------------------*/
{
parseInfo Info;
int naxis;
long nelem1, naxes[MAXDIMS];
if( *status ) return( *status );
FFLOCK;
if( ffiprs( fptr, 0, expr, MAXDIMS, &Info.datatype, &nelem1, &naxis,
naxes, status ) ) {
ffcprs();
FFUNLOCK;
return( *status );
}
if( nelem1<0 ) nelem1 = - nelem1;
if( nelements<nelem1 ) {
ffcprs();
ffpmsg("Array not large enough to hold at least one row of data.");
FFUNLOCK;
return( *status = PARSE_LRG_VECTOR );
}
firstrow = (firstrow>1 ? firstrow : 1);
if( datatype ) Info.datatype = datatype;
Info.dataPtr = array;
Info.nullPtr = nulval;
Info.maxRows = nelements / nelem1;
if( ffiter( gParse.nCols, gParse.colData, firstrow-1, 0,
parse_data, (void*)&Info, status ) == -1 )
*status=0; /* -1 indicates exitted without error before end... OK */
*anynul = Info.anyNull;
ffcprs();
FFUNLOCK;
return( *status );
}
/*--------------------------------------------------------------------------*/
int ffcalc( fitsfile *infptr, /* I - Input FITS file */
char *expr, /* I - Arithmetic expression */
fitsfile *outfptr, /* I - Output fits file */
char *parName, /* I - Name of output parameter */
char *parInfo, /* I - Extra information on parameter */
int *status ) /* O - Error status */
/* */
/* Evaluate an expression for all rows of a table. Call ffcalc_rng with */
/* a row range of 1-MAX. */
{
long start=1, end=LONG_MAX;
return ffcalc_rng( infptr, expr, outfptr, parName, parInfo,
1, &start, &end, status );
}
/*--------------------------------------------------------------------------*/
int ffcalc_rng( fitsfile *infptr, /* I - Input FITS file */
char *expr, /* I - Arithmetic expression */
fitsfile *outfptr, /* I - Output fits file */
char *parName, /* I - Name of output parameter */
char *parInfo, /* I - Extra information on parameter */
int nRngs, /* I - Row range info */
long *start, /* I - Row range info */
long *end, /* I - Row range info */
int *status ) /* O - Error status */
/* */
/* Evaluate an expression using the data in the input FITS file and place */
/* the results into either a column or keyword in the output fits file, */
/* depending on the value of parName (keywords normally prefixed with '#') */
/* and whether the expression evaluates to a constant or a table column. */
/* The logic is as follows: */
/* (1) If a column exists with name, parName, put results there. */
/* (2) If parName starts with '#', as in #NAXIS, put result there, */
/* with parInfo used as the comment. If expression does not evaluate */
/* to a constant, flag an error. */
/* (3) If a keyword exists with name, parName, and expression is a */
/* constant, put result there, using parInfo as the new comment. */
/* (4) Else, create a new column with name parName and TFORM parInfo. */
/* If parInfo is NULL, use a default data type for the column. */
/*--------------------------------------------------------------------------*/
{
parseInfo Info;
int naxis, constant, typecode, newNullKwd=0;
long nelem, naxes[MAXDIMS], repeat, width;
int col_cnt, colNo;
Node *result;
char card[81], tform[16], nullKwd[9], tdimKwd[9];
if( *status ) return( *status );
FFLOCK;
if( ffiprs( infptr, 0, expr, MAXDIMS, &Info.datatype, &nelem, &naxis,
naxes, status ) ) {
ffcprs();
FFUNLOCK;
return( *status );
}
if( nelem<0 ) {
constant = 1;
nelem = -nelem;
} else
constant = 0;
/* Case (1): If column exists put it there */
colNo = 0;
if( ffgcno( outfptr, CASEINSEN, parName, &colNo, status )==COL_NOT_FOUND ) {
/* Output column doesn't exist. Test for keyword. */
/* Case (2): Does parName indicate result should be put into keyword */
*status = 0;
if( parName[0]=='#' ) {
if( ! constant ) {
ffcprs();
ffpmsg( "Cannot put tabular result into keyword (ffcalc)" );
FFUNLOCK;
return( *status = PARSE_BAD_TYPE );
}
parName++; /* Advance past '#' */
if ( (fits_strcasecmp(parName,"HISTORY") == 0 || fits_strcasecmp(parName,"COMMENT") == 0) &&
Info.datatype != TSTRING ) {
ffcprs();
ffpmsg( "HISTORY and COMMENT values must be strings (ffcalc)" );
FFUNLOCK;
return( *status = PARSE_BAD_TYPE );
}
} else if( constant ) {
/* Case (3): Does a keyword named parName already exist */
if( ffgcrd( outfptr, parName, card, status )==KEY_NO_EXIST ) {
colNo = -1;
} else if( *status ) {
ffcprs();
FFUNLOCK;
return( *status );
}
} else
colNo = -1;
if( colNo<0 ) {
/* Case (4): Create new column */
*status = 0;
ffgncl( outfptr, &colNo, status );
colNo++;
if( parInfo==NULL || *parInfo=='\0' ) {
/* Figure out best default column type */
if( gParse.hdutype==BINARY_TBL ) {
snprintf(tform,15,"%ld",nelem);
switch( Info.datatype ) {
case TLOGICAL: strcat(tform,"L"); break;
case TLONG: strcat(tform,"J"); break;
case TDOUBLE: strcat(tform,"D"); break;
case TSTRING: strcat(tform,"A"); break;
case TBIT: strcat(tform,"X"); break;
case TLONGLONG: strcat(tform,"K"); break;
}
} else {
switch( Info.datatype ) {
case TLOGICAL:
ffcprs();
ffpmsg("Cannot create LOGICAL column in ASCII table");
FFUNLOCK;
return( *status = NOT_BTABLE );
case TLONG: strcpy(tform,"I11"); break;
case TDOUBLE: strcpy(tform,"D23.15"); break;
case TSTRING:
case TBIT: snprintf(tform,16,"A%ld",nelem); break;
}
}
parInfo = tform;
} else if( !(isdigit((int) *parInfo)) && gParse.hdutype==BINARY_TBL ) {
if( Info.datatype==TBIT && *parInfo=='B' )
nelem = (nelem+7)/8;
snprintf(tform,16,"%ld%s",nelem,parInfo);
parInfo = tform;
}
fficol( outfptr, colNo, parName, parInfo, status );
if( naxis>1 )
ffptdm( outfptr, colNo, naxis, naxes, status );
/* Setup TNULLn keyword in case NULLs are encountered */
ffkeyn("TNULL", colNo, nullKwd, status);
if( ffgcrd( outfptr, nullKwd, card, status )==KEY_NO_EXIST ) {
*status = 0;
if( gParse.hdutype==BINARY_TBL ) {
LONGLONG nullVal=0;
fits_binary_tform( parInfo, &typecode, &repeat, &width, status );
if( typecode==TBYTE )
nullVal = UCHAR_MAX;
else if( typecode==TSHORT )
nullVal = SHRT_MIN;
else if( typecode==TINT )
nullVal = INT_MIN;
else if( typecode==TLONG )
nullVal = LONG_MIN;
else if( typecode==TLONGLONG )
nullVal = LONGLONG_MIN;
if( nullVal ) {
ffpkyj( outfptr, nullKwd, nullVal, "Null value", status );
fits_set_btblnull( outfptr, colNo, nullVal, status );
newNullKwd = 1;
}
} else if( gParse.hdutype==ASCII_TBL ) {
ffpkys( outfptr, nullKwd, "NULL", "Null value string", status );
fits_set_atblnull( outfptr, colNo, "NULL", status );
newNullKwd = 1;
}
}
}
} else if( *status ) {
ffcprs();
FFUNLOCK;
return( *status );
} else {
/********************************************************/
/* Check if a TDIM keyword should be written/updated. */
/********************************************************/
ffkeyn("TDIM", colNo, tdimKwd, status);
ffgcrd( outfptr, tdimKwd, card, status );
if( *status==0 ) {
/* TDIM exists, so update it with result's dimension */
ffptdm( outfptr, colNo, naxis, naxes, status );
} else if( *status==KEY_NO_EXIST ) {
/* TDIM does not exist, so clear error stack and */
/* write a TDIM only if result is multi-dimensional */
*status = 0;
ffcmsg();
if( naxis>1 )
ffptdm( outfptr, colNo, naxis, naxes, status );
}
if( *status ) {
/* Either some other error happened in ffgcrd */
/* or one happened in ffptdm */
ffcprs();
FFUNLOCK;
return( *status );
}
}
if( colNo>0 ) {
/* Output column exists (now)... put results into it */
int anyNull = 0;
int nPerLp, i;
long totaln;
ffgkyj(infptr, "NAXIS2", &totaln, 0, status);
/*************************************/
/* Create new iterator Output Column */
/*************************************/
col_cnt = gParse.nCols;
if( allocateCol( col_cnt, status ) ) {
ffcprs();
FFUNLOCK;
return( *status );
}
fits_iter_set_by_num( gParse.colData+col_cnt, outfptr,
colNo, 0, OutputCol );
gParse.nCols++;
for( i=0; i<nRngs; i++ ) {
Info.dataPtr = NULL;
Info.maxRows = end[i]-start[i]+1;
/*
If there is only 1 range, and it includes all the rows,
and there are 10 or more rows, then set nPerLp = 0 so
that the iterator function will dynamically choose the
most efficient number of rows to process in each loop.
Otherwise, set nPerLp to the number of rows in this range.
*/
if( (Info.maxRows >= 10) && (nRngs == 1) &&
(start[0] == 1) && (end[0] == totaln))
nPerLp = 0;
else
nPerLp = Info.maxRows;
if( ffiter( gParse.nCols, gParse.colData, start[i]-1,
nPerLp, parse_data, (void*)&Info, status ) == -1 )
*status = 0;
else if( *status ) {
ffcprs();
FFUNLOCK;
return( *status );
}
if( Info.anyNull ) anyNull = 1;
}
if( newNullKwd && !anyNull ) {
ffdkey( outfptr, nullKwd, status );
}
} else {
/* Put constant result into keyword */
result = gParse.Nodes + gParse.resultNode;
switch( Info.datatype ) {
case TDOUBLE:
ffukyd( outfptr, parName, result->value.data.dbl, 15,
parInfo, status );
break;
case TLONG:
ffukyj( outfptr, parName, result->value.data.lng, parInfo, status );
break;
case TLOGICAL:
ffukyl( outfptr, parName, result->value.data.log, parInfo, status );
break;
case TBIT:
case TSTRING:
if (fits_strcasecmp(parName,"HISTORY") == 0) {
ffphis( outfptr, result->value.data.str, status);
} else if (fits_strcasecmp(parName,"COMMENT") == 0) {
ffpcom( outfptr, result->value.data.str, status);
} else {
ffukys( outfptr, parName, result->value.data.str, parInfo, status );
}
break;
}
}
ffcprs();
FFUNLOCK;
return( *status );
}
/*--------------------------------------------------------------------------*/
int fftexp( fitsfile *fptr, /* I - Input FITS file */
char *expr, /* I - Arithmetic expression */
int maxdim, /* I - Max Dimension of naxes */
int *datatype, /* O - Data type of result */
long *nelem, /* O - Vector length of result */
int *naxis, /* O - # of dimensions of result */
long *naxes, /* O - Size of each dimension */
int *status ) /* O - Error status */
/* */
/* Evaluate the given expression and return information on the result. */
/*--------------------------------------------------------------------------*/
{
FFLOCK;
ffiprs( fptr, 0, expr, maxdim, datatype, nelem, naxis, naxes, status );
ffcprs();
FFUNLOCK;
return( *status );
}
/*--------------------------------------------------------------------------*/
int ffiprs( fitsfile *fptr, /* I - Input FITS file */
int compressed, /* I - Is FITS file hkunexpanded? */
char *expr, /* I - Arithmetic expression */
int maxdim, /* I - Max Dimension of naxes */
int *datatype, /* O - Data type of result */
long *nelem, /* O - Vector length of result */
int *naxis, /* O - # of dimensions of result */
long *naxes, /* O - Size of each dimension */
int *status ) /* O - Error status */
/* */
/* Initialize the parser and determine what type of result the expression */
/* produces. */
/*--------------------------------------------------------------------------*/
{
Node *result;
int i,lexpr, tstatus = 0;
int xaxis, bitpix;
long xaxes[9];
static iteratorCol dmyCol;
if( *status ) return( *status );
/* make sure all internal structures for this HDU are current */
if ( ffrdef(fptr, status) ) return(*status);
/* Initialize the Parser structure */
gParse.def_fptr = fptr;
gParse.compressed = compressed;
gParse.nCols = 0;
gParse.colData = NULL;
gParse.varData = NULL;
gParse.getData = find_column;
gParse.loadData = load_column;
gParse.Nodes = NULL;
gParse.nNodesAlloc= 0;
gParse.nNodes = 0;
gParse.hdutype = 0;
gParse.status = 0;
fits_get_hdu_type(fptr, &gParse.hdutype, status );
if (gParse.hdutype == IMAGE_HDU) {
fits_get_img_param(fptr, 9, &bitpix, &xaxis, xaxes, status);
if (*status) {
ffpmsg("ffiprs: unable to get image dimensions");
return( *status );
}
gParse.totalRows = xaxis > 0 ? 1 : 0;
for (i = 0; i < xaxis; ++i)
gParse.totalRows *= xaxes[i];
if (DEBUG_PIXFILTER)
printf("naxis=%d, gParse.totalRows=%ld\n", xaxis, gParse.totalRows);
}
else if( ffgkyj(fptr, "NAXIS2", &gParse.totalRows, 0, &tstatus) )
{
/* this might be a 1D or null image with no NAXIS2 keyword */
gParse.totalRows = 0;
}
/* Copy expression into parser... read from file if necessary */
if( expr[0]=='@' ) {
if( ffimport_file( expr+1, &gParse.expr, status ) ) return( *status );
lexpr = strlen(gParse.expr);
} else {
lexpr = strlen(expr);
gParse.expr = (char*)malloc( (2+lexpr)*sizeof(char));
strcpy(gParse.expr,expr);
}
strcat(gParse.expr + lexpr,"\n");
gParse.index = 0;
gParse.is_eobuf = 0;
/* Parse the expression, building the Nodes and determing */
/* which columns are needed and what data type is returned */
ffrestart(NULL);
if( ffparse() ) {
return( *status = PARSE_SYNTAX_ERR );
}
/* Check results */
*status = gParse.status;
if( *status ) return(*status);
if( !gParse.nNodes ) {
ffpmsg("Blank expression");
return( *status = PARSE_SYNTAX_ERR );
}
if( !gParse.nCols ) {
dmyCol.fptr = fptr; /* This allows iterator to know value of */
gParse.colData = &dmyCol; /* fptr when no columns are referenced */
}
result = gParse.Nodes + gParse.resultNode;
*naxis = result->value.naxis;
*nelem = result->value.nelem;
for( i=0; i<*naxis && i<maxdim; i++ )
naxes[i] = result->value.naxes[i];
switch( result->type ) {
case BOOLEAN:
*datatype = TLOGICAL;
break;
case LONG:
*datatype = TLONG;
break;
case DOUBLE:
*datatype = TDOUBLE;
break;
case BITSTR:
*datatype = TBIT;
break;
case STRING:
*datatype = TSTRING;
break;
default:
*datatype = 0;
ffpmsg("Bad return data type");
*status = gParse.status = PARSE_BAD_TYPE;
break;
}
gParse.datatype = *datatype;
FREE(gParse.expr);
if( result->operation==CONST_OP ) *nelem = - *nelem;
return(*status);
}
/*--------------------------------------------------------------------------*/
void ffcprs( void ) /* No parameters */
/* */
/* Clear the parser, making it ready to accept a new expression. */
/*--------------------------------------------------------------------------*/
{
int col, node, i;
if( gParse.nCols > 0 ) {
FREE( gParse.colData );
for( col=0; col<gParse.nCols; col++ ) {
if( gParse.varData[col].undef == NULL ) continue;
if( gParse.varData[col].type == BITSTR )
FREE( ((char**)gParse.varData[col].data)[0] );
free( gParse.varData[col].undef );
}
FREE( gParse.varData );
gParse.nCols = 0;
}
if( gParse.nNodes > 0 ) {
node = gParse.nNodes;
while( node-- ) {
if( gParse.Nodes[node].operation==gtifilt_fct ) {
i = gParse.Nodes[node].SubNodes[0];
if (gParse.Nodes[ i ].value.data.ptr)
FREE( gParse.Nodes[ i ].value.data.ptr );
}
else if( gParse.Nodes[node].operation==regfilt_fct ) {
i = gParse.Nodes[node].SubNodes[0];
fits_free_region( (SAORegion *)gParse.Nodes[ i ].value.data.ptr );
}
}
gParse.nNodes = 0;
}
if( gParse.Nodes ) free( gParse.Nodes );
gParse.Nodes = NULL;
gParse.hdutype = ANY_HDU;
gParse.pixFilter = 0;
}
/*---------------------------------------------------------------------------*/
int parse_data( long totalrows, /* I - Total rows to be processed */
long offset, /* I - Number of rows skipped at start*/
long firstrow, /* I - First row of this iteration */
long nrows, /* I - Number of rows in this iter */
int nCols, /* I - Number of columns in use */
iteratorCol *colData, /* IO- Column information/data */
void *userPtr ) /* I - Data handling instructions */
/* */
/* Iterator work function which calls the parser and copies the results */
/* into either an OutputCol or a data pointer supplied in the userPtr */
/* structure. */
/*---------------------------------------------------------------------------*/
{
int status, constant=0, anyNullThisTime=0;
long jj, kk, idx, remain, ntodo;
Node *result;