summaryrefslogtreecommitdiffstats
path: root/funtools/filter/tfilt.c
blob: 0eba0b1c182454176f5a9a00687d53415911a546 (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
/*
 *	Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
 */

/*
 *
 * tfilt.c -- test program for filtering tables
 *
 */

#if HAVE_CONFIG_H
#include <conf.h>
#endif

#include <stdio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <sys/types.h>
#include <prsetup.h>
#include <filter.h>
#include <idx.h>
#include <word.h>

extern char *optarg;
extern int optind;

char bincols[SZ_LINE];
int debug=0;

#define NEVENTS	10240

/* from Harbison&Steele by way of GNU cinfigure ...
   returns 1 for bigendian, 0 for littleendian */
#ifdef ANSI_FUNC
int
is_bigendian(void)
#else
int is_bigendian()
#endif
{
  union
  {
    long l;
    char c[sizeof (long)];
  } u;
  u.l = 1;
  return(u.c[sizeof (long) - 1] == 1);
}

/*
 *
 * ListEvents -- list events in a binary table
 *
 */
#ifdef ANSI_FUNC
static int
ListEvents(GIO gio, FILE *ofd, FITSHead header, char *s, int iformat,
	   char *mode)
#else
static int ListEvents(gio, ofd, header, s, iformat, mode)
     GIO gio;			/* input FITS file handle */
     FILE *ofd;			/* output file handle */
     FITSHead header;		/* fitsy header */
     char *s;			/* filter for events */
     int iformat;		/* input format of data: 0=native, 1=fits */
     char *mode			/* filteropen mode */
#endif
{
  int i, j;			/* loop counters */
  int get;			/* number of events to read */
  int got;			/* number of events read */
  int left;			/* number of events left to read */
  int total;			/* total number of events in file */
  int convert;			/* whether we have to convert to native */
  int nev;			/* max number of events to read at once */
  int evsize;			/* size of an event record */
  int ototal;			/* output total events */
  int *vbuf;			/* valid event flags */
  char *ebuf;			/* event buffer */
  char *eptr;			/* current pointer into ebuf */
  char tbuf[SZ_LINE];		/* ever-present temp buf */
  unsigned char bval;		/* byte value */
  short sval;			/* short value */
  unsigned short usval;		/* short value */
  int ival;			/* int value */
  long long lval;		/* 64-bit int value */
  unsigned int uival;		/* unsigned int value */
  float fval;			/* float value */
  double dval;			/* double value */
  int dofilt=0;			/* true if we can filter */
  Filter filter=NULL;

  /* we have to convert to native if the data is not the
     same as the big-endian-ness of the machine */
  convert = (iformat != is_bigendian());
  /* create the mode string */
  if( convert )
    strcpy(tbuf, "convert=true");
  else
    strcpy(tbuf, "convert=false");
  strcat(tbuf, bincols);
  if( debug )
    strcat(tbuf, ",debug=2");

  /* now add the input mode */
  if( mode && *mode ){
    strcat(tbuf, ",");
    strcat(tbuf, mode);
  }

  /* init some convenience variables */
  evsize = header->basic->naxis[0];
  total = header->basic->naxis[1];
  ototal = 0;

  /* open the filter */
  if( (filter = FilterOpen(header, s, tbuf)) == NULL ){
    fprintf(stderr, "ERROR: tfilt could not open filter: %s\n",
	    s ? s : "no filter");
    return(0);
  }
  else if( filter == NOFILTER ){
    fprintf(stderr, "no valid filter specified\n");
    return(0);
  }

  /* allocate space for a pile of event records */
  for(nev=NEVENTS; nev>0; nev /=2){
    if( ((ebuf = (char *)xmalloc(evsize*nev))!= NULL) &&
        ((vbuf = (int *)xmalloc(sizeof(int)*nev))!= NULL) )
      break;
  }

  /* print out header */
  for(j=0; j<header->table->tfields; j++){
    switch(header->table->col[j].type){
    case 'B':
    case 'L':
      fprintf(ofd, "%6s", header->table->col[j].name);
      break;
    case 'I':
    case 'U':
      fprintf(ofd, "%6s", header->table->col[j].name);
      break;
    case 'J':
    case 'V':
      fprintf(ofd, "%11s", header->table->col[j].name);
      break;
    case 'K':
      fprintf(ofd, "%20s", header->table->col[j].name);
      break;
    case 'E':
      fprintf(ofd, "%11s", header->table->col[j].name);
      break;
    case 'D':
      fprintf(ofd, "%17s", header->table->col[j].name);
      break;
    default:
      break;
    }
  }
  fprintf(ofd, "\n");

  for(j=0; j<header->table->tfields; j++){
    switch(header->table->col[j].type){
    case 'B':
    case 'L':
      fprintf(ofd, " -----");
      break;
    case 'I':
    case 'U':
      fprintf(ofd, " -----");
      break;
    case 'J':
    case 'V':
      fprintf(ofd, " ----------");
      break;
    case 'K':
      fprintf(ofd, " -------------------");
      break;
    case 'E':
      fprintf(ofd, " ----------");
      break;
    case 'D':
      fprintf(ofd, " ----------------");
      break;
    default:
      break;
    }
  }
  fprintf(ofd, "\n");

  /* set the total number of events or 'read til eof' */
  left = total;

  /* read all event records */
  while( left != 0 ){
    /* figure out how many to read this time */
    get = MIN(nev, left);
    /* read in a pile of events */
    got=gread(gio, ebuf, evsize, get);
    /* check for eof */
    if( got != get ){
      fprintf(stderr, "Warning: events ends at %d (expected %d)\n",
		total-left, total);
      /* we must be done */
      left = 0;
    }
    else{
      left -= got;
    }
    /* filter the events through the co-process */
    if( filter ){
      dofilt = FilterEvents(filter, ebuf, evsize, got, vbuf);
    }
    /* process each event in the pile */
    for(eptr=ebuf, i=0; i<got; i++, eptr += evsize){
      /* if its not a valid event, skip it */
      if( dofilt && (vbuf[i] == 0) )
	continue;
      /* else increment the total events */
      ototal++;
      /* process this event */
      for(j=0; j<header->table->tfields; j++){
	switch(header->table->col[j].type){
	case 'B':
	case 'L':
	  ColumnLoad(eptr+header->table->col[j].offset,
		    header->table->col[j].size, 1, convert, (char *)&bval);
	  fprintf(ofd, "%6d", (int)bval);
	  break;
	case 'I':
	  ColumnLoad(eptr+header->table->col[j].offset,
		    header->table->col[j].size, 1, convert, (char *)&sval);
	  fprintf(ofd, "%6d", (int)sval);
	  break;
	case 'U':
	  ColumnLoad(eptr+header->table->col[j].offset,
		    header->table->col[j].size, 1, convert, (char *)&usval);
	  fprintf(ofd, "%6d", (int)usval);
	  break;
	case 'J':
	  ColumnLoad(eptr+header->table->col[j].offset,
		    header->table->col[j].size, 1, convert, (char *)&ival);
	  fprintf(ofd, "%11d", ival);
	  break;
	case 'V':
	  ColumnLoad(eptr+header->table->col[j].offset,
		    header->table->col[j].size, 1, convert, (char *)&uival);
	  fprintf(ofd, "%11d", uival);
	  break;
	case 'K':
	  ColumnLoad(eptr+header->table->col[j].offset,
		    header->table->col[j].size, 1, convert, (char *)&lval);
	  fprintf(ofd, "%20lld", lval);
	  break;
	case 'E':
	  ColumnLoad(eptr+header->table->col[j].offset,
		    header->table->col[j].size, 1, convert, (char *)&fval);
	  fprintf(ofd, "%11.2f", fval);
	  break;
	case 'D':
	  ColumnLoad(eptr+header->table->col[j].offset,
		    header->table->col[j].size, 1, convert, (char *)&dval);
	  fprintf(ofd, "%17.4f", dval);
	  break;
	default:
	  break;
	}
      }
      fprintf(ofd, "\n");
    }
  }

  /* free up allocated space */
  if( ebuf )
    xfree(ebuf);
  if( vbuf )
    xfree(vbuf);

  /* done with the filter process */
  if( filter )
    FilterClose(filter);

  /* final tally */
  fprintf(ofd, "\nTotal events: %d\n", ototal);

  /* that's good news */
  return(ototal);
}

#ifdef ANSI_FUNC
int 
main (int argc, char **argv)
#else
main(argc, argv)
     int argc;
     char **argv;
#endif
{
  int c;
  int type;
  int x, y, n;
  int nmask;
  int x0, x1, y0, y1, block;
  int listev = 0;
  int dispim = 0;
  int dispfilt = 1;
  int doidx = 0;
  char *s;
  char *iname;			/* input file name ptr */
  char fname[SZ_LINE];		/* input file name */
  char tail[SZ_LINE];		/* tail of file name */
  char tbuf[SZ_LINE];		/* ever-present temp buffer */
  char mode[SZ_LINE];		/* mode for FilterOpen */
  char evsect[SZ_LINE];		/* event section for image filters */
  long save_pos;		/* save position in FITS file */
  GIO gio;			/* input FITS file handle */
  FITSHead header;		/* global fitsy header struct */
  Filter filter;
  FilterMask masks;

  /* we want the args in the same order in which they arrived, and
     gnu getopt sometimes changes things without this */
  putenv("POSIXLY_CORRECT=true");

  /* assume events */
  type = 'e';

  /* process switch arguments */
  while ((c = getopt(argc, argv, "d:efilxz")) != -1){
    switch(c){
    case 'd':
      dispim = 1;
      strcpy(evsect, optarg);
      if(sscanf(optarg, "%d %d %d %d %d", &x0, &x1, &y0, &y1, &block)!=5){
	fprintf(stderr, "-d requires x0, x1, y0, y1, block\n");
	exit(1);
      }
      break;
    case 'e':
      type = 'e';
      break;
    case 'f':
      dispfilt = 1;
      break;
    case 'i':
      type = 'i';
      break;
    case 'l':
      listev = 1;
      break;
    case 'x':
      doidx = 1;
      break;
    case 'z':
      putenv("FILTER_PAINT=true");
      break;
    }
  }

  if( type == 'e' ){
    strcpy(mode, "type=events");
    if( dispim ){
      snprintf(tbuf, SZ_LINE, ",evsect=\"%s\"", evsect);
      strcat(mode, tbuf);
    }
  }
  else{
    strcpy(mode, "type=image");
  }


  /* get file name or use a default */
  if( optind >= argc ){
    iname = "../funtest/test.ev[EVENTS]";
  }
  else{
    strncpy(fname, argv[optind], SZ_LINE);
    if( !strchr(fname, '[') )
      strncat(fname, "[EVENTS]", SZ_LINE);
    iname = fname;
    optind++;
  }

  /* if we do not list or display, we are in debug mode */
  if( !listev && !dispim && !dispfilt ){
    debug = 2;
  }

  /* open the binary table extension */
  if( (gio=ft_fitsheadopen(iname, &header, tail, SZ_LINE, "r")) == NULL ){
    fprintf(stderr, "ERROR: can't read FITS binary table: %s\n", iname);
    exit(1);
  }
  if( header == NULL ){
    fprintf(stderr, "ERROR: can't access FITS data: %s\n", iname);
    exit(1);
  }

  if( keyword(tail, "bincols", tbuf, SZ_LINE) ){
    snprintf(bincols, SZ_LINE, ",bincols=(%s)", tbuf);
    strcat(mode, bincols);
  }
  else{
    strcpy(bincols, ",bincols=(x,y)");
    strcat(mode, bincols);
  }
  if( debug )
    strcat(mode, ",debug=2");
  strcat(mode, ",lexonly=1");
  if( (s=getenv("FILTER_PAINT")) ){
    if( istrue(s) )
      strcat(mode, ",paint=true");
    else if( isfalse(s) )
      strcat(mode, ",paint=false");
  }

  while( 1 ){
    *tbuf = '\0';
    if( feof(stdin) )
      break;
    fgets(tbuf, SZ_LINE, stdin);
    if( *tbuf == 'q' )
      exit(0);
    if( *tbuf ){
      if( listev ){
	/* list out events */
	save_pos = gtell(gio);
	ListEvents(gio, stdout, header, tbuf, 1, mode);
	gseek(gio, save_pos, SEEK_SET);
      }
      if( dispim ){
	/* display image */
	if( *tbuf && (*tbuf != '\n') ){
	  fprintf(stdout, "input string: %s", tbuf);
	  filter = FilterOpen(header, tbuf, mode);
	  if( !filter )
	    fprintf(stderr, "Error in FilterOpen()\n");
	  else if( filter == NOFILTER )
	    fprintf(stderr, "No valid filter specified\n");
	  else{
	    nmask = FilterImage(filter, x0, x1, y0, y1, block, &masks, NULL);
	    if( nmask ){
	      /* loop through mask segments */
	      y = 1;
	      for(n=0; n<nmask; n++){
		/* process rows before the y row of this mask segment */
		for(; y<masks[n].y; y++){
		  for(x=1; x<=(x1-x0+block)/block; x++)
		    fprintf(stdout, "*");
		  fprintf(stdout, "\n");
		}
		/* process each mask segment having the same y value */
		x = 1;
		while(1){
		  /* process columns before the start column */
		  for(; x<masks[n].xstart; x++)
		    fprintf(stderr, ".");
		  /* process columns in the mask */
		  for(; x<=masks[n].xstop; x++)
		    fprintf(stderr, "%d", masks[n].region);
		  /* if we have another mask segment with the same y,
		     we keep going */
		  if( ((n+1)<nmask) && (masks[n+1].y==y) )
		    n++;
		  else
		    break;
		}
		/* process the rest of the columns for this y row */
		for(; x<=(x1-x0+block)/block; x++)
		  fprintf(stderr, ".");
		/* processed all mask segs in this y */
		fprintf(stderr, "\n");
		/* bump to next y row */
		y++;
	      }
	      /* process y rows after last row containing a mask segment */
	      for(y=masks[nmask-1].y+1; y<=(y1-y0+block)/block; y++){
		for(x=1; x<=(x1-x0+block)/block; x++)
		  fprintf(stdout, "*");
		fprintf(stdout, "\n");
	      }
	    }
	    if( masks )
	      xfree(masks);
	    FilterClose(filter);
	  }
	}
      }
      else{
	if( *tbuf && (*tbuf != '\n') ){
	  fprintf(stdout, "input string: %s", tbuf);
	  filter = FilterOpen(header, tbuf, mode);
	  if( !filter )
	    fprintf(stderr, "Error in FilterOpen()\n");
	  else if( filter == NOFILTER )
	    fprintf(stderr, "No valid filter specified\n");
	  if( doidx ){
	    if( (s = xstrdup(_FilterString()) ) ){
	      fprintf(stdout, "index input: %s\n", s);
	      idxinitparser(s);
	      idxparse();
	      idxendparser();
	      xfree(s);
	    }
	  }
	  if( dispfilt ){
	    fprintf(stdout, "filter: %s\n", _FilterString());
	  }
	  FilterClose(filter);
	}
      }
    }
  }
  return(0);
}