summaryrefslogtreecommitdiffstats
path: root/funtools/filter/idx.l
blob: a542505b4f849fb184f3c8f4540c9904a7d326a4 (plain)
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
%option caseless
%array

%{
#include <math.h>
#include <filter.h>
#include <idx.h>
#include <idx.tab.h>

extern int idx_debug;

static int _valint(char *s, idxvalrec **v);
static int _valfloat(char *s, idxvalrec **v);
static int _valname(char *s, idxvalrec **v);
static int _valreg(char *s, idxvalrec **v);
static int _valfunc(char *s, idxvalrec **v);
%}

SP	[ \t]
DIG	[0-9]
DIG2	[0-9a-fA-F]
/* note that negative numbers are not defined here, but in the grammar */
INT1	{DIG}+L?
INT2	0[xX]{DIG2}+L?
INT	({INT1}|{INT2})
FLOAT1  {DIG}+\.?([eE][-+]?{DIG}+)?
FLOAT2  {DIG}*\.{DIG}+([eE][-+]?{DIG}+)?
XFLOAT  -142857.142857
FLOAT   ({FLOAT1}|{FLOAT2}|{XFLOAT})
NUM	({INT}|{FLOAT})
NAME	[A-Za-z_][0-9A-Za-z~_]*(\[[^\[]*\])?

/* must match regions in filt.l */
REGION	(ev|im)[vn]?(annulus|box|circle|ellipse|line|panda|pie|qtpie|point|polygon|field|bpanda|cpanda|epanda)
%%

{INT}	{return _valint(yytext, &(idxlval.val));}

{FLOAT}	{return _valfloat(yytext, &(idxlval.val));}


evfield"(g",{INT},{INT},{INT},{INT},"(double)"{NAME},"(double)"{NAME}")" {
	return _valreg(yytext, &(idxlval.val));
}

{REGION}"(g",{INT},{INT},{INT},{INT},"(double)"{NAME},"(double)"{NAME}(,[+-]?{FLOAT})*,[+-]?{FLOAT}")" {
	return _valreg(yytext, &(idxlval.val));
}

{NAME}"("[^()]*("("[^()]*")")*")" {
	/* support functions with one level of nested parens */
	return _valfunc(yytext, &(idxlval.val));
}

{NAME}	{return _valname(yytext, &(idxlval.val));}

"||"	{return OR;}
"&&"	{return AND;}
"=="	{return EQ;}
"!="	{return NE;}
"<="	{return LE;}
">="	{return GE;}

{SP}    {;}

<<EOF>>	{
	  yy_delete_buffer( YY_CURRENT_BUFFER );
          yyterminate();
	}

.	{return yytext[0];}

%%

#ifdef YY_USE_PROTOS
static int _valint(char *s, idxvalrec **v)
#else
static int _valint(s, v)
  char *s;
  idxvalrec **v;
#endif
{
  char *t;
  *v = idxvalnew(s);
  (*v)->ival = strtoll(s, &t, 0);
  if( *t )
    idxerror("bad integer value");
  (*v)->type = NUM;
  (*v)->ntype = PARSE_INTEGER;
  (*v)->dval = (double)(*v)->ival;
  return NUM;
}

#ifdef YY_USE_PROTOS
static int _valfloat(char *s, idxvalrec **v)
#else
static int _valfloat(s, v)
  char *s;
  idxvalrec **v;
#endif
{
  char *t;
  *v = idxvalnew(s);
  (*v)->dval = strtod(s, &t);
  if( *t )
    idxerror("bad float value");
  (*v)->type = NUM;
  (*v)->ntype = PARSE_FLOAT;
  return NUM;
}

#ifdef YY_USE_PROTOS
static int _valname(char *s, idxvalrec **v)
#else
static int _valname(s, v)
  char *s;
  idxvalrec **v;
#endif
{
  int i;
  int got;
  int oflag;
  int isize=0;
  char *iname;
  char *colname;
  char tail[SZ_LINE];
  FilterSymbols sp=NULL;
  idxvalrec *vv;

  *v = idxvalnew(s);
  /* lookup the string */
  if( !(sp=FilterSymbolLookup(FilterDefault(), s)) ){
    idxerror("column name not found in data file");
    (*v)->type = INDEF;
    return INDEF;
  }
  colname = idxinfo(IDX_COLNAME);
  /* see what sort of symbol it is */
  switch(sp->type){
  case SYM_COL:
    if( !(iname=idxindexfilename(s, &isize)) ){
      (*v)->type = INDEF;
      return INDEF;
    }
    /* if we already have opened this index, just use the existing handle */
    if( (vv=idxlookupfilename(iname)) ){
      (*v)->igio = vv->igio;
      (*v)->ifits = vv->ifits;
    }
    /* open index for first time and mark iname */
    else{
      oflag = setgerror(0);
      (*v)->igio = ft_fitsheadopen(iname, &((*v)->ifits), tail, SZ_LINE, "r");
      setgerror(oflag);
      if( !((*v)->igio) ){
        idxerror("existing index file can't be opened");
        (*v)->type = INDEF;
        return INDEF;
      }
      else{
        (*v)->iname = xstrdup(iname);
      }
    }
    xfree(iname);
    if( (*v)->ifits ){
      for(got=0, i=0; i<(*v)->ifits->table->tfields; i++){
        if( !strcasecmp(s, (*v)->ifits->table->col[i].name) ){
          (*v)->vtype = (*v)->ifits->table->col[i].type;
          (*v)->voffset = (*v)->ifits->table->col[i].offset;
          (*v)->vn = (*v)->ifits->table->col[i].n;
          got++;
        }
        if( !strcasecmp(colname, (*v)->ifits->table->col[i].name) ){
          (*v)->itype = (*v)->ifits->table->col[i].type;
          (*v)->ioffset = (*v)->ifits->table->col[i].offset;
          (*v)->in = (*v)->ifits->table->col[i].n;
          got++;
        }
      }
      if( got == 2 ){
#ifdef HAVE_SYS_MMAN_H
        if( !(*v)->igio->gz ){
          if(!((*v)->idata = mmap(NULL, isize, PROT_READ, MAP_PRIVATE,
 			          fileno((*v)->igio->fp), 0)) ){
            idxerror("index file can't be mmap'ed");
            (*v)->type = INDEF;
            return INDEF;
          }
	  (*v)->ilen = isize;
  	}
#endif
        (*v)->nrow = ft_naxis((*v)->ifits,2);
        (*v)->type = COL;
        return COL;
      }
      else{
        idxerror("column name and/or index not found in index file");
      }
    }
    else{
      (*v)->type = INDEF;
      return INDEF;
    }
  case SYM_PAR:
    (*v)->ntype=ParseDataType(sp->value, &(*v)->dval, &(*v)->ival);
    (*v)->type = NUM;
    switch((*v)->ntype){
    case PARSE_INTEGER:
    case PARSE_HEXINT:
      (*v)->dval = (double)(*v)->ival;
      return NUM;
    case PARSE_FLOAT:
      return NUM;
    default:
      idxerror("invalid parameter type in index");
      return 0;
    }
  default:
    idxerror("unknown symbol type in index");
    return 0;
  }
}

#ifdef YY_USE_PROTOS
static int _valreglims(idxvalrec *v, char *s)
#else
static int _valreglims(v, s)
  idxvalrec *v;
  char *s;
#endif
{
  int i;
  int ip=0;
  int nd=0, maxd=0;
  double xcen, ycen;
  double dval;
  double *dvals;
  double pts[8];
  char tbuf[SZ_LINE];
  char tbuf2[SZ_LINE];
  double angle;
  double xwidth, yheight;
  double angl;			 /* l: Cartesian angle in radians */
  double half_width, half_height;/* l: radii (1/2 width and height) */
  double cosangl, sinangl;	 /* l: sine, cosine of the Cartesian angle */
  double hw_cos, hw_sin;	 /* l: products of half_width with sin, cos */
  double hh_cos, hh_sin;	 /* l: products of half_height with sin, cos */

  if( !strcmp(v->s, "circle")  || !strcmp(v->s, "ncircle")   ||
      !strcmp(v->s, "annulus") || !strcmp(v->s, "nannulus")  ){
    if( !word(s, tbuf, &ip)||!word(s, tbuf2, &ip) ) return 0;
    xcen = atof(tbuf);
    ycen = atof(tbuf2);
    dval = -1;
    if( *v->s == 'n' ){
      if( !word(s, tbuf, &ip) || !word(s, tbuf, &ip) ) return 0;
      dval = MAX(atof(tbuf),dval);
    }
    else{
      while( word(s, tbuf, &ip) && strcmp(tbuf, "-142857.142857") ){
        dval = MAX(atof(tbuf),dval);
      }
    }
    v->rlo[0] = xcen - dval - 1;
    v->rhi[0] = xcen + dval + 1;
    v->rlo[1] = ycen - dval - 1;
    v->rhi[1] = ycen + dval + 1;
    return 1;
  }
  else if( !strcmp(v->s, "box")     || !strcmp(v->s, "nbox")     ||
           !strcmp(v->s, "ellipse") || !strcmp(v->s, "nellipse") ){
    if( !word(s, tbuf, &ip)||!word(s, tbuf2, &ip) ) return 0;
    xcen = atof(tbuf);
    ycen = atof(tbuf2);
    maxd = SZ_LINE;
    if( !(dvals=(double *)malloc(maxd*sizeof(double))) ) return 0;
    if( *v->s == 'n' ){
      if( !word(s, tbuf, &ip) || !word(s, tbuf, &ip) ) return 0;
      if( !word(s, tbuf, &ip) || !word(s, tbuf2, &ip) ) return 0;
      dvals[nd++] = atof(tbuf);
      dvals[nd++] = atof(tbuf2);
      if( word(s, tbuf, &ip) && word(s, tbuf, &ip) )
        dvals[nd++] = atof(tbuf);
    }
    else{
      while( word(s, tbuf, &ip) && strcmp(tbuf, "-142857.142857") ){
        dvals[nd++] = atof(tbuf);
        if( nd == maxd ){
          maxd += SZ_LINE;
          if( !(dvals=(double *)realloc(dvals, maxd*sizeof(double))) ) return 0;
        }
      }
    }
ellcom:
    if( nd == 2 ){
      angle = 0.0;
      xwidth = dvals[0];
      yheight = dvals[1];
    }
    else{
      angle = dvals[nd-1];
      xwidth = dvals[nd-3];
      yheight = dvals[nd-2];
    }
    if( dvals ) xfree(dvals);

    /* Why is this done in evfilter.c??? Doesn't seem necessary */
    /* angl = angle + 90.0; */
    angl = angle;
    while (angl >= 360.0) angl = angl - 360.0;
    /* convert to radians */
    angl = (angl / 180.0) * M_PI;
    sinangl = sin (angl);
    cosangl = cos (angl);
    /* Why is this done in evfilter.c??? Doesn't seem necessary */
    /* since we rotate by 90.0 degrees to get from astro angle to cartesian, */
    /* we also need to switch the width and height. we do this secretly so */
    /* that the display will turn out right, by doing it in the half terms */
    if( !strcmp(v->s, "box") ){
      /*
      half_width = yheight / 2.0;
      half_height = xwidth / 2.0;
      */
      half_width = xwidth / 2.0;
      half_height = yheight / 2.0;
    }
    else{
      /* 
      half_width = yheight;
      half_height = xwidth;
      */
      half_width = xwidth;
      half_height = yheight;
    }
    hw_cos = half_width * cosangl;
    hw_sin = half_width * sinangl;
    hh_cos = half_height * cosangl;
    hh_sin = half_height * sinangl;

    pts[0] = xcen - hw_cos - hh_sin;
    pts[1] = ycen - hw_sin + hh_cos;
    pts[2] = xcen + hw_cos - hh_sin;
    pts[3] = ycen + hw_sin + hh_cos;
    pts[4] = xcen + hw_cos + hh_sin;
    pts[5] = ycen + hw_sin - hh_cos;
    pts[6] = xcen - hw_cos + hh_sin;
    pts[7] = ycen - hw_sin - hh_cos;

    v->rlo[0] = pts[0];
    v->rhi[0] = pts[0];
    v->rlo[1] = pts[1];
    v->rhi[1] = pts[1];
    for(i=2; i<8; i+=2){
      v->rlo[0] = MIN(pts[i],v->rlo[0]);
      v->rhi[0] = MAX(pts[i],v->rhi[0]);
      v->rlo[1] = MIN(pts[i+1],v->rlo[1]);
      v->rhi[1] = MAX(pts[i+1],v->rhi[1]);
    }
    return 1;
  }
  else if( !strcmp(v->s, "line") ){
    for(i=0; i<4; i++){
      if( word(s, tbuf, &ip) ){
        pts[i] = atof(tbuf);
      }
    }
    v->rlo[0] = MIN(pts[0],pts[2]);
    v->rhi[0] = MAX(pts[0],pts[2]);
    v->rlo[1] = MIN(pts[1],pts[3]);
    v->rhi[1] = MAX(pts[1],pts[3]);
    return 1;
  }
  else if( !strcmp(v->s, "point") || !strcmp(v->s, "polygon") ){
    if( !word(s, tbuf, &ip)||!word(s, tbuf2, &ip) ) return 0;
    xcen = atof(tbuf);
    ycen = atof(tbuf2);
    v->rlo[0] = xcen-1;
    v->rhi[0] = xcen+1;
    v->rlo[1] = ycen-1;
    v->rhi[1] = ycen+1;
    while( word(s, tbuf, &ip) && strcmp(tbuf, "-142857.142857") &&
           word(s, tbuf2, &ip) ){
      dval = atof(tbuf);
      v->rlo[0] = MIN(dval-1,v->rlo[0]);
      v->rhi[0] = MAX(dval+1,v->rhi[0]);
      dval = atof(tbuf2);
      v->rlo[1] = MIN(dval-1,v->rlo[1]);
      v->rhi[1] = MAX(dval+1,v->rhi[1]);
    }
    return 1;
  }
  else if( !strcmp(v->s, "pie") || !strcmp(v->s, "qtpie") ){
    return 0;
  }
  else if( !strcmp(v->s, "panda") || !strcmp(v->s, "cpanda") ){
    maxd = SZ_LINE;
    if( !(dvals=(double *)malloc(maxd*sizeof(double))) ) return 0;
    while( word(s, tbuf, &ip) && strcmp(tbuf, "-142857.142857") ){
      dvals[nd++] = atof(tbuf);
      if( nd == maxd ){
        maxd += SZ_LINE;
        if( !(dvals=(double *)realloc(dvals, maxd*sizeof(double))) ) return 0;
      }
    }
    v->rlo[0] = dvals[0] - dvals[6] - 1;
    v->rhi[0] = dvals[0] + dvals[6] + 1;
    v->rlo[1] = dvals[1] - dvals[6] - 1;
    v->rhi[1] = dvals[1] + dvals[6] + 1;
    if( dvals ) xfree(dvals);
    return 1;
  }
  else if( !strcmp(v->s, "bpanda") || !strcmp(v->s, "epanda") ){
    maxd = 3;
    if( !(dvals=(double *)malloc(maxd*sizeof(double))) ) return 0;
    /* grab: xcen ycen */
    if( !word(s, tbuf, &ip)||!word(s, tbuf2, &ip) ) return 0;
    xcen = atof(tbuf);
    ycen = atof(tbuf2);
    /* skip: ang1 ang2 nang xwlo yhlo */
    for(i=0; i<5; i++){
      if( !word(s, tbuf, &ip) ) return 0;
    }
    /* grab: xwhi yhhi */
    for(i=0; i<2; i++){
      if( !word(s, tbuf, &ip) ) return 0;
      dvals[nd++] = atof(tbuf);
    }
    /* skip: nrad */
    if( !word(s, tbuf, &ip) ) return 0;
    /* grab: ang */
    if( !word(s, tbuf, &ip) ) return 0;
    dvals[nd++] = atof(tbuf);
    /* we can now handle this with box/ellipse code */
    goto ellcom;
  }
  else if( !strcmp(v->s, "field") ){
    return 0;
  }
  else{
    return 0;
  }
}

#ifdef YY_USE_PROTOS
static int _valreg(char *s, idxvalrec **v)
#else
static int _valreg(s, v)
  char *s;
  idxvalrec **v;
#endif
{
  int ip=0;
  char *t;
  char tbuf[SZ_LINE];
  *v = idxvalnew(NULL);
  newdtable("(),");
  while( *s == '(' ) s++;
  if( !word(s, tbuf, &ip) ){
    (*v)->type = INDEF;
    return REG;
  }
  if( strstr(tbuf, "field") ){
    (*v)->type = INDEF;
    return REG;
  }
  t = tbuf+2;
  if( *t == 'v' ) t++;
  (*v)->s = xstrdup(t);
  if( !word(s, tbuf, &ip) || !word(s, tbuf, &ip) || 
      !word(s, tbuf, &ip) || !word(s, tbuf, &ip) ){
    (*v)->type = INDEF;
    return REG;
  }
  /* include/exclude */
  if( !atoi(tbuf) ){
    (*v)->type = INDEF;
    return REG;
  }
  if( !word(s, tbuf, &ip) || !word(s, tbuf, &ip) || !word(s, tbuf, &ip) ){
    (*v)->type = INDEF;
    return REG;
  }
  culc(tbuf);
  _valname(tbuf, &(*v)->rv[0]);
  if( !word(s, tbuf, &ip) || !word(s, tbuf, &ip)  ){
    (*v)->type = INDEF;
    return REG;
  }
  culc(tbuf);
  _valname(tbuf, &(*v)->rv[1]);
  if( !_valreglims(*v, &s[ip]) ){
    (*v)->type = INDEF;
    return REG;
  }
  freedtable();
  (*v)->type = REG;
  return REG;
}

#ifdef YY_USE_PROTOS
static int _valfunc(char *s, idxvalrec **v)
#else
static int _valfunc(s, v)
  char *s;
  idxvalrec **v;
#endif
{
  *v = idxvalnew(s);
  (*v)->type = FUNC;
  return FUNC;
}


#ifdef YY_USE_PROTOS
void
idxstring(char *s)
#else
void idxstring(s)
     char *s;
#endif
{
  idx_scan_string(s);
}

#ifdef YY_USE_PROTOS
int
idxerror(char *msg)
#else
int idxerror(msg)
     char *msg;
#endif
{
  Filter filter;

  YY_FLUSH_BUFFER;
  /* turn indexing off */
  if( (filter=FilterDefault()) ){
    filter->doidx = -1;
  }
  /* output message, if necessary */
  if( idx_debug ){
    fprintf(stderr, "ERROR: %s", msg);
    if( !strcmp(msg, "syntax error") ){
      fprintf(stderr, " (terminating index processing)");
    }
    fprintf(stderr, "\n");    
  }
  yyterminate();
}

#ifdef YY_USE_PROTOS
int yywrap(void)
#else
int yywrap()
#endif
{
  return 1;
}