This repository has been archived by the owner on Nov 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfuncone.c
1473 lines (1371 loc) · 35.6 KB
/
funcone.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
/*
* Copyright (c) 2005 Smithsonian Astrophysical Observatory
*/
/* code shamelessly ripped off from John Roll's search program in starbase
(http://cfa-www.harvard.edu/~john/starbase/starbase.html) */
#include <math.h>
/* some experimental black magic in this code requires ... */
#include <funtoolsP.h>
#include <filter.h>
#include <swap.h>
#include <word.h>
#include <xalloc.h>
#define MAXROW 8192
static int maxrow=MAXROW;
extern char *optarg;
extern int optind;
#define RA_DEFAULT "RA:h"
#define DEC_DEFAULT "DEC:d"
#define RA_DEFAULT_UNITS "h"
#define DEC_DEFAULT_UNITS "d"
#define RAD_DEFAULT_UNITS "d"
#define RA_NAME "RA_CEN"
#define DEC_NAME "DEC_CEN"
#define RAD_NAME "RAD_CEN"
#define KEY_NAME "CONE_KEY"
#define RA_FLAG 1
#define DEC_FLAG 2
#define RAD_FLAG 4
#define ALL_FLAG (RA_FLAG|DEC_FLAG|RAD_FLAG)
#define X__PI 3.14159265358979323846
#define X_2PI ( 2 * X__PI )
#define X_R2D (X_2PI / 360.0)
#define X_R2H (X_2PI / 24.0)
#define X_H2D (360.0 / 24.0)
#define r2h(r) ( (r) / X_R2H )
#define h2r(d) ( (d) * X_R2H )
#define r2d(r) ( (r) / X_R2D )
#define d2r(d) ( (d) * X_R2D )
#define h2d(r) ( (r) * X_H2D )
#define d2h(d) ( (d) / X_H2D )
#define Abs(xx) (((xx) >= 0) ? (xx) : -(xx) )
#define Max(x, y) (((x) > (y)) ? (x) : (y))
#define MIN_COLS 6
#define ALL_DATA 1
#define ALL_LIST 2
#define ALL_FILT 4
#define ALL_ANY (ALL_DATA|ALL_LIST)
typedef struct evstruct{
double val0, val1, val2;
double ra, dec, rad;
int key;
char *row;
} *Ev, EvRec;
typedef struct Value {
char *colname;
double val;
double lo;
double hi;
int dtype;
int offset;
int n;
} Value;
typedef struct Csys {
char type[3];
Value value[3];
} Csys;
typedef struct List {
Fun fun;
Csys *csys;
int nline;
int litflag;
char *lits[3];
char *row;
int rowsize;
int ncol;
} List;
#ifdef ANSI_FUNC
static void
slaDcs2c(double a, double b, double v[3])
#else
static void slaDcs2c(a, b, v)
double a;
double b;
double v[3];
#endif
{
double cosb;
cosb = cos ( b );
v[0] = cos ( a ) * cosb;
v[1] = sin ( a ) * cosb;
v[2] = sin ( b );
}
#ifdef ANSI_FUNC
static double
slaDsep(double a1, double b1, double a2, double b2)
#else
static double slaDsep(a1, b1, a2, b2)
double a1;
double b1;
double a2;
double b2;
#endif
{
int i;
double d, v1[3], v2[3], s2, c2;
slaDcs2c ( a1, b1, v1 );
slaDcs2c ( a2, b2, v2 );
s2 = 0.0;
for ( i = 0; i < 3; i++ ) {
d = v1[i] - v2[i];
s2 += d * d;
}
s2 /= 4.0;
c2 = 1.0 - s2;
return 2.0 * atan2 ( sqrt ( s2 ), sqrt ( Max ( 0.0, c2 )));
}
#ifdef ANSI_FUNC
static int
ConeFilter(Csys *csys, double val0, double val1)
#else
static int ConeFilter(csys, val0, val1)
Csys *csys;
double val0;
double val1;
#endif
{
double r1=0.0, r2=0.0, d1=0.0, d2=0.0;
double radius=0.0;
switch( csys->type[0] ) {
case 'h':
r1 = h2r(csys->value[0].val);
r2 = h2r(val0);
break;
case 'd':
r1 = d2r(csys->value[0].val);
r2 = d2r(val0);
break;
case 'r':
r1 = csys->value[0].val;
r2 = val0;
break;
}
switch( csys->type[1] ) {
case 'h':
d1 = h2r(csys->value[1].val);
d2 = h2r(val1);
break;
case 'd':
d1 = d2r(csys->value[1].val);
d2 = d2r(val1);
break;
case 'r':
d1 = csys->value[1].val;
d2 = val1;
break;
}
switch( csys->type[2] ) {
case 'h':
radius = h2r(csys->value[2].val);
break;
case 'd':
radius = d2r(csys->value[2].val);
break;
case 'r':
radius = csys->value[2].val;
break;
case '\'':
radius = d2r(csys->value[2].val/60.0);
break;
case '"':
radius = d2r(csys->value[2].val/3600.0);
break;
}
/* check angular separation */
if ( slaDsep(r1, d1, r2, d2) <= radius )
return 1;
else
return 0;
}
#ifdef ANSI_FUNC
static void
radecbox(double ra, double dec, double width,
double *r1, double *r2, double *d1, double *d2)
#else
static void radecbox(ra, dec, width, r1, r2, d1, d2)
double ra;
double dec;
double width;
double *r1, *r2, *d1, *d2;
#endif
{
double cosdec;
*d1 = dec - width / 2.0;
if ( *d1 <= -90.0 ) {
*d1 = -90.0;
*d2 = dec + width / 2.0;
*r1 = 0.0;
*r2 = 24.0;
} else {
*d2 = dec + width / 2.0;
if ( *d2 >= 90.0 ) {
*d1 = dec - width / 2.0;
*d2 = 90.0;
*r1 = 0.0;
*r2 = 24.0;
} else {
if ( dec > 0.0 ) cosdec = Abs(cos(d2r(*d1)));
else cosdec = Abs(cos(d2r(*d2)));
*r1 = ra - d2h(width) / 2 / cosdec;
*r2 = ra + d2h(width) / 2 / cosdec;
if ( *r1 < 0.0 ) *r1 += 24;
if ( *r2 > 24.0 ) *r2 -= 24;
}
}
}
#ifdef ANSI_FUNC
static void
ConeLimits(Csys *csys, char *rastr, char *decstr, char *radstr)
#else
static void ConeLimits(csys, rastr, decstr, radstr)
Csys *csys;
char *rastr;
char *decstr;
char *radstr;
#endif
{
int xtype=0;
char *p=NULL;
double dval=0.0;
double ra=0.0, dec=0.0, radius=0.0;
double r0=0.0, r1=0.0, d0=0.0, d1=0.0;
/* Input units conversion. */
/* process input ra */
dval = SAOstrtod(rastr, &p);
if( rastr == p )
gerror(stderr, "invalid RA value: '%s' (columns go with -l list)\n",
rastr);
xtype = *p;
if( (xtype != 'h') && (xtype != 'd') && (xtype != 'r') ) {
if( SAOdtype == 'd' )
xtype = 'd';
else
xtype = 'h';
}
switch( xtype ) {
case 'h':
ra = dval;
break;
case 'd':
ra = d2h(dval);
break;
case 'r':
ra = r2h(dval);
break;
}
/* process input dec */
dval = SAOstrtod(decstr, &p);
if( decstr == p )
gerror(stderr, "invalid Dec value: '%s' (columns go with -l list)\n",
decstr);
xtype = *p;
if( (xtype != 'h') && (xtype != 'd') && (xtype != 'r') ) {
if( SAOdtype == 'h' )
xtype = 'h';
else
xtype = 'd';
}
switch( xtype ) {
case 'h':
dec = h2d(dval);
break;
case 'd':
dec = dval;
break;
case 'r':
dec = r2d(dval);
break;
}
/* process input radius */
dval = SAOstrtod(radstr, &p);
if( radstr == p )
gerror(stderr, "invalid radius value: '%s'(columns go with -l list)\n",
radstr);
xtype = *p;
if( (xtype != 'h') && (xtype != 'd') && (xtype != 'r') &&
(xtype != '\'') && (xtype != '"') ){
if( SAOdtype == 'h' )
xtype = 'h';
else
xtype = 'd';
}
switch( xtype ) {
case 'h':
radius = h2d(dval);
break;
case 'd':
radius = dval;
break;
case 'r':
radius = r2d(dval);
break;
case '\'':
radius = dval/60.0;
break;
case '"':
radius = dval/3600.0;
break;
default:
radius = dval;
break;
}
/* calculate box limits, units in degrees */
radecbox(ra, dec, radius*2, &r0, &r1, &d0, &d1);
/* Output units conversion */
switch( csys->type[0] ) {
case 'h':
csys->value[0].val = ra;
csys->value[0].lo = r0;
csys->value[0].hi = r1;
break;
case 'd':
csys->value[0].val = h2d(ra);
csys->value[0].lo = h2d(r0);
csys->value[0].hi = h2d(r1);
break;
case 'r':
csys->value[0].val = h2r(ra);
csys->value[0].lo = h2r(r0);
csys->value[0].hi = h2r(r1);
break;
}
switch( csys->type[1] ) {
case 'h':
csys->value[1].val = d2h(dec);
csys->value[1].lo = d2h(d0);
csys->value[1].hi = d2h(d1);
break;
case 'd':
csys->value[1].val = dec;
csys->value[1].lo = d0;
csys->value[1].hi = d1;
break;
case 'r':
csys->value[1].val = d2r(dec);
csys->value[1].lo = d2r(d0);
csys->value[1].hi = d2r(d1);
break;
}
csys->type[2] = xtype;
csys->value[2].val = dval;
#ifdef FOO
/* force separation to be in radians, as needed by angular sep code */
csys->type[2] = 'r';
csys->value[2].val = d2r(radius);
#endif
}
#ifdef ANSI_FUNC
static char *
ColumnName(Fun fun, char *def)
#else
static char *ColumnName(fun, def)
Fun fun;
char *def;
#endif
{
int i;
char tbuf[SZ_LINE];
/* sanity check */
if( !def || !*def ) return NULL;
/* see if default is available */
if( !FunColumnLookup(fun, def, 0, NULL, NULL, NULL, NULL, NULL, NULL) ){
return xstrdup(def);
}
/* add numeric until a column name is available */
else{
i = 2;
while( 1 ){
snprintf(tbuf, SZ_LINE-1, "%s_%d", def, i);
if( !FunColumnLookup(fun, tbuf, 0, NULL, NULL, NULL, NULL, NULL, NULL) ){
return xstrdup(tbuf);
}
}
}
}
#ifdef ANSI_FUNC
static int
ColumnInfo(Csys *csys, char *colstr[], int flag)
#else
static int ColumnInfo(csys, colstr, flag)
Csys *csys;
char *colstr[];
int flag;
#endif
{
int i;
int ip;
int got=0;
char tbuf[SZ_LINE];
char tbuf2[SZ_LINE];
newdtable(":");
for(i=0; i<3; i++){
/* flag tells which of the three possible inputs to check */
if( ((1<<i)&flag) == 0 ) continue;
ip = 0;
/* name of column */
if( !word(colstr[i], tbuf, &ip) ){
gerror(stderr, "invalid column #%d specification\n", i+1);
goto done;
}
/* units */
if( !word(colstr[i], tbuf2, &ip) ){
switch(i){
case 0:
strcpy(tbuf2, RA_DEFAULT_UNITS);
break;
case 1:
strcpy(tbuf2, DEC_DEFAULT_UNITS);
break;
case 2:
strcpy(tbuf2, RAD_DEFAULT_UNITS);
break;
default:
gerror(stderr, "internal funcone error: bad iter %d\n", i);
break;
}
}
else{
/* quotes get handled as delimiters, so look for them here */
if( i == 2 ){
if( !*tbuf2 && (lastdelim() == '"' ) ){
strcpy(tbuf2, "\"");
}
else if( !*tbuf2 && (lastdelim() == '\'' ) ){
strcpy(tbuf2, "'");
}
}
}
/* column info */
csys->value[i].colname = xstrdup(tbuf);
switch(*tbuf2){
case 'h':
csys->type[i] = 'h';
break;
case 'd':
csys->type[i] = 'd';
break;
case 'r':
csys->type[i] = 'r';
break;
default:
/* additional units for radius values */
if( i == 2 ){
switch(*tbuf2){
case '\'':
csys->type[i] = '\'';
break;
case '"':
csys->type[i] = '"';
break;
default:
gerror(stderr,
"invalid units specification '%s' for %s\n", tbuf2, tbuf);
goto done;
}
}
else{
gerror(stderr,
"invalid units specification '%s' for %s\n", tbuf2, tbuf);
goto done;
}
}
}
got = 1;
done:
freedtable();
return got;
}
#ifdef ANSI_FUNC
static double
DConvert (char *buf, int type, int offset, int n)
#else
static double
DConvert(buf, type, offset, n)
char *buf;
int type;
int offset;
int n;
#endif
{
int ival;
double dval;
switch(type){
case 'X':
if( n == 16 )
dval = (double)*(unsigned short *)(buf+offset);
else if( n == 32 )
dval = (double)*(unsigned int *)(buf+offset);
else
dval = (double)*(unsigned char *)(buf+offset);
break;
case 'B':
dval = (double)*(unsigned char *)(buf+offset);
break;
case 'I':
dval = (double)*(short *)(buf+offset);
break;
case 'U':
dval = (double)*(unsigned short *)(buf+offset);
break;
case 'J':
dval = (double)*(int *)(buf+offset);
break;
case 'K':
#if HAVE_LONG_LONG == 0
gerror(stderr,
"64-bit data support not built (long long not available)\n");
#endif
dval = (double)*(longlong *)(buf+offset);
break;
case 'V':
dval = (double)*(unsigned int *)(buf+offset);
break;
case 'E':
dval = (double)*(float *)(buf+offset);
break;
case 'D':
dval = *(double *)(buf+offset);
break;
case 'L':
ival = (int)*(unsigned char *)(buf+offset);
if( !ival || (ival == 'F') || (ival == 'f') )
dval = 0.0;
else
dval = 1.0;
break;
default:
dval = 0.0;
break;
}
return dval;
}
#ifdef ANSI_FUNC
static void
CloseList(List *list)
#else
static void CloseList(List *list)
char *s;
#endif
{
int i;
/* sanity check */
if( !list ) return;
if( list->fun ) FunClose(list->fun);
/* free up memory */
if( list->row ){
xfree(list->row);
list->row=NULL;
}
for(i=0; i<3; i++){
if( list->lits[i] ) xfree(list->lits[i]);
}
if( list->csys ){
for(i=0; i<3; i++){
if( list->csys->value[i].colname ) xfree(list->csys->value[i].colname);
}
xfree(list->csys);
}
xfree(list);
}
#ifdef ANSI_FUNC
static List *
OpenList(char *lname, char *rastr, char *decstr, char *radstr)
#else
static List *OpenList(lname, rastr, decstr, radstr)
char *lname;
char *rastr;
char *decstr;
char *radstr;
#endif
{
char *colstr[3];
char tbuf[SZ_LINE];
char *p;
int flag=0;
List *list;
/* allocate list struct */
if( !(list = (List *)xcalloc(sizeof(List), 1)) ){
gerror(stderr, "can't allocate list record\n");
return NULL;
}
if( !(list->csys = (Csys *)xcalloc(sizeof(Csys), 1)) ){
CloseList(list);
gerror(stderr, "can't allocate list coordinate system record\n");
return NULL;
}
/* open list file */
if( !(list->fun = FunOpen(lname, "r", NULL)) ){
CloseList(list);
gerror(stderr, "can't FunOpen list file: %s\n", lname);
return NULL;
}
/* look for literal numeric values (columns can't start with a number) */
(void)strtod(rastr, &p);
if( p != rastr ){
list->lits[0] = xstrdup(rastr);
list->litflag |= RA_FLAG;
}
else{
flag |= RA_FLAG;
colstr[0] = rastr;
}
(void)strtod(decstr, &p);
if( p != decstr ){
list->lits[1] = xstrdup(decstr);
list->litflag |= DEC_FLAG;
}
else{
flag |= DEC_FLAG;
colstr[1] = decstr;
}
(void)strtod(radstr, &p);
if( p != radstr ){
list->lits[2] = xstrdup(radstr);
list->litflag |= RAD_FLAG;
}
else{
flag |= RAD_FLAG;
colstr[2] = radstr;
}
/* look for column info as needed */
if( flag ){
if( ColumnInfo(list->csys, colstr, flag) ){
if( flag & RA_FLAG ){
if( !FunColumnLookup(list->fun, list->csys->value[0].colname, 0,
NULL,
&(list->csys->value[0].dtype),
NULL,
&(list->csys->value[0].offset),
&(list->csys->value[0].n),
NULL) ){
strncpy(tbuf, list->csys->value[0].colname, SZ_LINE-1);
CloseList(list);
gerror(stderr, "can't get column info for: %s\n", tbuf);
return NULL;
}
}
if( flag & DEC_FLAG ){
if( !FunColumnLookup(list->fun, list->csys->value[1].colname, 0,
NULL,
&(list->csys->value[1].dtype),
NULL,
&(list->csys->value[1].offset),
&(list->csys->value[1].n),
NULL) ){
strncpy(tbuf, list->csys->value[0].colname, SZ_LINE-1);
CloseList(list);
gerror(stderr, "can't get column info for: %s\n", tbuf);
return NULL;
}
}
if( flag & RAD_FLAG ){
if( !FunColumnLookup(list->fun, list->csys->value[2].colname, 0,
NULL,
&(list->csys->value[2].dtype),
NULL,
&(list->csys->value[2].offset),
&(list->csys->value[2].n),
NULL) ){
strncpy(tbuf, list->csys->value[0].colname, SZ_LINE-1);
CloseList(list);
gerror(stderr, "can't get column info for: %s\n", tbuf);
return NULL;
}
}
}
else{
CloseList(list);
gerror(stderr, "can't get column info for list: %s %s\n",
colstr[0], colstr[1]);
return NULL;
}
}
return list;
}
#ifdef ANSI_FUNC
static int
NextList(List *list, char *rastr, char *decstr, char *radstr, int maxlen)
#else
static int NextList(list, rastr, decstr, radstr, maxlen)
List *list;
char *rastr;
char *decstr;
char *radstr;
int maxlen;
#endif
{
int got;
/* sanity check */
if( !list ) return 0;
/* clean up from previous */
if( list->row ){
xfree(list->row);
list->row=NULL;
}
/* if we have all literal values, end after one line */
if( (list->litflag == ALL_FLAG) && (list->nline) ) return 0;
/* read next record and exit on EOF */
if( !(list->row=FunTableRowGet(list->fun, NULL, 1, NULL, &got)) || !got)
return 0;
/* ra */
if( list->lits[0] )
strncpy(rastr, list->lits[0], maxlen-1);
else
snprintf(rastr, SZ_LINE-1, "%15.10f%c",
DConvert(list->row,
list->csys->value[0].dtype,
list->csys->value[0].offset,
list->csys->value[0].n),
list->csys->type[0]);
/* dec */
if( list->lits[1] )
strncpy(decstr, list->lits[1], maxlen-1);
else
snprintf(decstr, SZ_LINE-1, "%15.10f%c",
DConvert(list->row,
list->csys->value[1].dtype,
list->csys->value[1].offset,
list->csys->value[1].n),
list->csys->type[1]);
/* radius */
if( list->lits[2] )
strncpy(radstr, list->lits[2], maxlen-1);
else
snprintf(radstr, SZ_LINE-1, "%15.10f%c",
DConvert(list->row,
list->csys->value[2].dtype,
list->csys->value[2].offset,
list->csys->value[2].n),
list->csys->type[2]);
/* got another line */
list->nline++;
/* return line number */
return list->nline;
}
#ifdef ANSI_FUNC
static int FilterSpec(Csys *csys, char *filter, int maxlen)
#else
static int FilterSpec(csys, filter, maxlen)
Csys *csys;
char *filter;
int maxlen;
#endif
{
int i, j;
char tbuf[SZ_LINE];
/* sanity check */
if( !csys ) return 0;
/* this is the indexed filter */
if( csys->value[0].lo < csys->value[0].hi ){
snprintf(filter, maxlen-1, "%s=%15.10f:%15.10f&&",
csys->value[0].colname, csys->value[0].lo, csys->value[0].hi);
}
else{
snprintf(filter, maxlen-1, "(%s>=%15.10f||%s<=%15.10f)&&",
csys->value[0].colname, csys->value[0].lo,
csys->value[0].colname, csys->value[0].hi);
}
if( csys->value[1].lo < csys->value[1].hi ){
snprintf(tbuf, SZ_LINE-1, "%s=%15.10f:%15.10f",
csys->value[1].colname, csys->value[1].lo, csys->value[1].hi);
}
else{
snprintf(tbuf, SZ_LINE-1, "(%s>=%15.10f||%s<=%15.10f)",
csys->value[1].colname, csys->value[1].lo,
csys->value[1].colname, csys->value[1].hi);
}
strncat(filter, tbuf, maxlen-1);
/* remove spaces (avoid old bug in filter range lists) */
for(i=0, j=0; filter[i]; i++){
if( filter[i] != ' ' ){
filter[j++] = filter[i];
}
}
filter[j] = '\0';
return 1;
}
#ifdef ANSI_FUNC
static void
usage (char *pname)
#else
static void usage(pname)
char *pname;
#endif
{
fprintf(stderr,
"usage: %s <switches> iname oname ra[hdr] dec[hdr] radius[dr\"'] [columns [listcols]]\n",
pname);
fprintf(stderr, "\n");
fprintf(stderr, " iname: input FITS file name\n");
fprintf(stderr, " oname: output FITS file name\n");
fprintf(stderr, " ra: RA center position, optional units (def: hours)\n");
fprintf(stderr, " dec: Dec center position, optional units (def: degrees)\n");
fprintf(stderr, " radius: Search radius, optional units (def: degrees)\n");
fprintf(stderr, " columns: optional columns to output\n");
fprintf(stderr, "\n");
fprintf(stderr, "optional switches:\n");
fprintf(stderr, "\n");
fprintf(stderr, " -d deccol:[hdr] # Dec column name, units (def: DEC:d)\n");
fprintf(stderr, " -j # join columns from list file\n");
fprintf(stderr, " -J # join columns from list file, output all rows\n");
fprintf(stderr, " -l listfile # read centers and radii from a list\n");
fprintf(stderr, " -L listfile # read centers and radii from a list, output list rows\n");
fprintf(stderr, " -n # don't use cone limits as a filter\n");
fprintf(stderr, " -r racol:[hdr] # RA column name, units (def: RA:h)\n");
fprintf(stderr, " -x # append RA_CEN, DEC_CEN, RAD_CEN, KEY cols\n");
fprintf(stderr, " -X # append RA_CEN, DEC_CEN, RAD_CEN, KEY cols, output all rows\n");
fprintf(stderr, "\n");
fprintf(stderr, "The default units for input center and radius are:\n");
fprintf(stderr, " ra:hours, dec:degrees, radius: degrees.\n");
fprintf(stderr, "Sexagesimal notation is supported.\n");
fprintf(stderr, "\n");
fprintf(stderr, "If a list is specified, then the ra, dec, and radius\n");
fprintf(stderr, "can be a list column name or numeric value. Col name\n");
fprintf(stderr, "specifiers support the :hdr suffix.\n");
fprintf(stderr, "\n");
fprintf(stderr, "The -n switch can speed up processing of small tables\n");
fprintf(stderr, "by skipping the overhead associated with filtering on\n");
fprintf(stderr, "the cone limits.\n");
fprintf(stderr, "\n");
fprintf(stderr, "examples:\n");
fprintf(stderr, "\n");
fprintf(stderr, "# defaults: RA col in hours, DEC col in degrees\n");
fprintf(stderr, "# RA pos in hours, DEC pos and radius in degrees\n");
fprintf(stderr, "%s in.fits out.fits 23.45 34.56 0.01\n",
pname);
fprintf(stderr, "# defaults, but with RA position in degrees:\n");
fprintf(stderr, "%s in.fits out.fits 23.45d 34.56 0.01\n",
pname);
fprintf(stderr, "# user specified columns in degrees\n");
fprintf(stderr, "# RA pos in hms, DEC pos in dms, radius in arcmin:\n");
fprintf(stderr, "%s -r myRa:d -d myDec in.fits out.fits 12:30:15.5 30:12 15'\n",
pname);
fprintf(stderr, "# take positions from rax and decx columns in a FITS table\n");
fprintf(stderr, "# and join columns from list to output\n");
fprintf(stderr, "%s -j -l lst.fits in.fits out.fits 'rax:d' 'decx:d' '0.003:d'\n",
pname);
fprintf(stderr, "\n(version: %s)\n", FUN_VERSION);
exit(1);
}
#ifdef ANSI_FUNC
int
main (int argc, char **argv)
#else
int
main(argc, argv)
int argc;
char **argv;
#endif
{
int c;
int i;
int got;
int total;
int rawsize;
int havefilt=0;
int debug=0;
int args=0;
int ioff=0;
int dolist=0;
int dolimfilt=1;
int dojoin=0;
int doall=0;
int iter=0;
int irow=0;
int ncol=0;
int nrow=0;
int *rbuf=NULL;
int *offsets=NULL;
int ltype, lmode, loffset, ln, lwidth;
size_t tsize=0;
char *s;
char *rawbuf;
char *adbuf=NULL;
char *albuf=NULL;
char *iname=NULL;
char *oname=NULL;
char *lname=NULL;
char *xmode=NULL;
char *xmode2=NULL;
char *raname=NULL;
char *decname=NULL;
char *radname=NULL;
char *keyname=NULL;
char *actcols=NULL;
char *lcol=NULL;
char *params="merge=update";
char *colstr[2];
char filter[SZ_LINE];
char rastr[SZ_LINE];
char decstr[SZ_LINE];
char radstr[SZ_LINE];
char mode[SZ_LINE];
char tbuf[SZ_LINE];
char **names=NULL;
char **types=NULL;
char **modes=NULL;
Fun fun, ofun;
Csys *csys=NULL;
List *list=NULL;
Filter filt=NULL;
FITSHead header=NULL;
Ev ev=NULL, ebuf=NULL;
/* exit on gio errors */
if( !getenv("GERROR") )
setgerror(2);
/* initialize names of columns */
colstr[0]=RA_DEFAULT;
colstr[1]=DEC_DEFAULT;
/* we want the args in the same order in which they arrived, and
gnu getopt sometimes changes things without this */
putenv("POSIXLY_CORRECT=true");
/* process switch arguments */
while ((c = getopt(argc, argv, "Dd:jJl:L:nr:xX")) != -1){
switch(c){
case 'D':
debug = 1;
break;
case 'd':
colstr[1] = optarg;
break;
case 'J':
doall |= ALL_DATA;
/* drop through */
case 'j':
dojoin = 1;
break;