summaryrefslogtreecommitdiffstats
path: root/funtools/funsky.c
blob: 9c2b483859fd4c39eade5b46f700b810f83bea1e (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
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
/*
 *	Copyright (c) 2005 Smithsonian Astrophysical Observatory
 */

#include <math.h>
#include <funtoolsP.h>
#include <strtod.h>

extern char *optarg;
extern int optind;

#define RA_DEFAULT        "RA"
#define DEC_DEFAULT       "DEC"
#define RA_DEFAULT_UNITS  "d"
#define DEC_DEFAULT_UNITS "d"

#define X_DEFAULT          "X"
#define Y_DEFAULT          "Y"
#define X_DEFAULT_UNITS    "h"
#define Y_DEFAULT_UNITS    "d"

#define COL1_DEFAULT       "COL1"
#define COL2_DEFAULT       "COL2"

#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 )

typedef struct evstruct{
  double val1, val2;
} *Ev, EvRec;

typedef struct List {
  Fun fun;
  char type[2];
  char *colname[2];
} List;

#ifdef ANSI_FUNC
static int
ColInfo(List *list, char *colstr[], int flag)
#else
static int
ColInfo(list, colstr, flag)
     List *list;
     char *colstr[];
     int flag;
#endif
{
  int i;
  int ip;
  char tbuf[SZ_LINE];

  newdtable(":");
  for(i=0; i<2; i++){
    ip = 0;
    /* column name */
    if( !*colstr[i] || (*colstr[i] == ':') || !word(colstr[i], tbuf, &ip) ){
      switch(i){
      case 0:
	if(FunColumnLookup(list->fun, COL1_DEFAULT, 0, 
			   NULL, NULL, NULL, NULL, NULL, NULL))
	  strcpy(tbuf, COL1_DEFAULT);
	else{
	  if( flag ){
	    strcpy(tbuf, RA_DEFAULT);
	  }
	  else{
	    strcpy(tbuf, X_DEFAULT);
	  }
	}
	break;
      case 1:
	if(FunColumnLookup(list->fun, COL2_DEFAULT, 0, 
			   NULL, NULL, NULL, NULL, NULL, NULL))
	  strcpy(tbuf, COL2_DEFAULT);
	else{
	  if( flag ){
	    strcpy(tbuf, DEC_DEFAULT);
	  }
	  else{
	    strcpy(tbuf, Y_DEFAULT);
	  }
	}
	break;
      }
    }
    list->colname[i] = xstrdup(tbuf);

    /* column units */
    if( !word(colstr[i], tbuf, &ip) ){
      switch(i){
      case 0:
	strcpy(tbuf, RA_DEFAULT_UNITS);
	break;
      case 1:
	strcpy(tbuf, DEC_DEFAULT_UNITS);
	break;
      }
    }
    list->type[i] = *tbuf;
  }
  freedtable();
  return 1;
}

#ifdef ANSI_FUNC
static void
CloseList(List *list)
#else
static void 
CloseList(list)
     List *list;
#endif
{
  int i;
  /* sanity check */
  if( !list ) return;
  if( list->fun ) FunClose(list->fun);
  /* free up memory */
  for(i=0; i<2; i++){
    if( list->colname[i] ) xfree(list->colname[i]);
  }
  xfree(list);
}

#ifdef ANSI_FUNC
static List *
OpenList(char *lname, char *pos1str, char *pos2str, int flag)
#else
static List *
OpenList(lname, pos1str, pos2str, flag)
     char *lname;
     char *pos1str;
     char *pos2str;
     int flag;
#endif
{
  char *colstr[2];
  List *list;

  /* allocate list struct */
  if( !(list = (List *)xcalloc(sizeof(List), 1)) ){
    gerror(stderr, "can't allocate list record\n");
    return NULL;
  }
  /* open list file, if necessary */
  if( !(list->fun = FunOpen(lname, "r", NULL)) ){
    CloseList(list);
    gerror(stderr, "can't FunOpen list file: %s\n", lname);
    return NULL;
  }
  /* get info on the specified columns */
  colstr[0] = pos1str;
  colstr[1] = pos2str;
  if( !ColInfo(list, colstr, flag) ){
    CloseList(list);
    gerror(stderr, "can't get column info for list: %s %s\n",
	   colstr[0], colstr[1]);
    return NULL;
  }
  /* set up to read the specified columns */
  FunColumnSelect(list->fun, sizeof(EvRec), NULL,
		  list->colname[0],  "D", "r", FUN_OFFSET(Ev, val1),
		  list->colname[1],  "D", "r", FUN_OFFSET(Ev, val2),
		  NULL);
  return list;
}

#ifdef ANSI_FUNC
static int 
NextFromList(List *list, double *dval1, double *dval2, int flag)
#else
static int
NextFromList(list, dval1, dval2, flag)
     List *list;
     double *dval1;
     double *dval2;
     int flag;
#endif
{
  int got;
  EvRec evrec;

  /* sanity check */
  if( !list ) return 0;

  /* read next line from table */
  if( !FunTableRowGet(list->fun, &evrec, 1, NULL, &got) || !got)
    return 0;

  /* non-zero flag means we have sky coords and need degrees */
  if( flag ){
    /* perform unit conversion -- wcslib requires degrees */
    switch( list->type[0] ) {
    case 'h':
      *dval1 = h2d(evrec.val1);
      break;
    case 'r':
      *dval1 = r2d(evrec.val1);
      break;
    case 'd':
      *dval1 = evrec.val1;
      break;
    }
    switch( list->type[1] ) {
    case 'h':
      *dval2 = h2d(evrec.val2);
      break;
    case 'r':
      *dval2 = r2d(evrec.val2);
      break;
    case 'd':
      *dval2 = evrec.val2;
      break;
    }
  }
  /* image coords -- leave alone */
  else{
    *dval1 = evrec.val1;
    *dval2 = evrec.val2;
  }
  /* got a row */
  return 1;
}

#ifdef ANSI_FUNC
static void
usage(char *fname)
#else
static void
usage(fname)
     char *fname;
#endif
{
  fprintf(stderr, "usage:\n");
  fprintf(stderr, "%s iname[ext]\t\t\t# RA,Dec (deg) or image pix from stdin\n", fname);
  fprintf(stderr, "%s iname[ext] [lname]\t\t# RA,Dec (deg) or image pix from list\n", fname);
  fprintf(stderr, "%s iname[ext] [col1] [col2]\t# named cols:units from stdin\n", fname);
  fprintf(stderr, "%s iname[ext] [lname] [col1] [col2] # named cols:units from list\n", fname);
  fprintf(stderr, "\n");
  fprintf(stderr, "optional switches:\n");
  fprintf(stderr, "-d\t# always use integer tlmin conversion (as ds9 does)\n");
  fprintf(stderr, "-r\t# convert x,y to RA,Dec (default: convert RA,Dec to x,y)\n");
  fprintf(stderr, "-v\t# display input values also (default: display output only)\n");
  fprintf(stderr, "-o\t# include offset from the nominal target position (in arcsec)\n");
  fprintf(stderr, "-T\t# output display in rdb format (w/header,tab delimiters)\n");
  fprintf(stderr, "\n");
  fprintf(stderr, "The coord. system is taken from the header (might differ from ds9).\n");
  fprintf(stderr, "By default, input RA and Dec are converted into x and y (use the\n");
  fprintf(stderr, "-r to convert from x,y to RA,Dec.) Output x,y values are physical\n");
  fprintf(stderr, "coords for event data, image coords for image data.\n");
  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 type;
  int offscl;
  int args;
  int got;
  int ioff=0;
  int verbose=0;
  int offaxis=0;
  int dods9=0;
  int dotab=0;
  int sky2im=1;
  int tltyp[2];
  double dval1=0.0, dval2=0.0, dval3=0.0, dval4=0.0;
  double ra_target=0.0, dec_target=0.0, x_target=0.0, y_target=0.0;
  double off_axis=0.0;
  double tlmin[2];
  double binsiz[2];
  double zero=0.0;
  char sp=' ';
  char iname[SZ_LINE];
  char lname[SZ_LINE];
  char pos1str[SZ_LINE];
  char pos2str[SZ_LINE];
  char *s0, *s1;
  Fun fun=NULL;
  List *list=NULL;
  void *wcs=NULL;

  /* exit on gio errors */
  if( !getenv("GERROR")  )
    setgerror(2);

  /* 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, "drtoTv")) != -1){
    switch(c){
    case 'd':
      dods9=1;
      break;
    case 'r':
      sky2im=0;
      break;
    case 'o':
      offaxis=1;
      break;
    case 'v':
      verbose=1;
      break;
    case 't':
      dotab = 1;
      break;
    case 'T':
      sp = '\t';
      dotab = 1;
      break;
    default:
      break;
    }
  }

  /* check for required arguments */
  args = argc - optind;
  switch(args){
  case 1:
    /* first argument is input file name containing WCS */
    strncpy(iname, argv[optind+ioff++], SZ_LINE-1);
    /* read positions from stdin */
    strncpy(lname, "stdin", SZ_LINE-1);
    /* using default columns and units */
    *pos1str = '\0';
    *pos2str = '\0';
    break;
  case 2:
    /* first argument is input file name containing WCS */
    strncpy(iname, argv[optind+ioff++], SZ_LINE-1);
    /* read positions from list file */
    strncpy(lname,  argv[optind+ioff++], SZ_LINE-1);
    /* using default columns and units */
    *pos1str = '\0';
    *pos2str = '\0';
    break;
  case 3:
    /* first argument is input file name containing WCS */
    strncpy(iname, argv[optind+ioff++], SZ_LINE-1);
    /* read positions from stdin */
    strncpy(lname, "stdin", SZ_LINE-1);
    /* using specified columns and units */
    strncpy(pos1str,  argv[optind+ioff++], SZ_LINE-1);
    strncpy(pos2str, argv[optind+ioff++], SZ_LINE-1);
    break;
  case 4:
    /* first argument is input file name containing WCS */
    strncpy(iname, argv[optind+ioff++], SZ_LINE-1);
    /* read positions from list file */
    strncpy(lname,  argv[optind+ioff++], SZ_LINE-1);
    /* using specified columns and units */
    strncpy(pos1str,  argv[optind+ioff++], SZ_LINE-1);
    strncpy(pos2str, argv[optind+ioff++], SZ_LINE-1);
    break;
  default:
    usage(argv[0]);
    break;
  }

  /* open input file */
  if( !(fun = FunOpen(iname, "r", NULL)) ){
    gerror(stderr, "can't FunOpen input file (or find extension): %s\n", iname);
  }

  /* if offaxis is set, read the aimpoint information from the header */
  /* This will only work for Chandra Style files */
  if( (offaxis) && (sky2im) ) {
    ra_target = FunParamGetd(fun, "RA_TARG ", 1, -1.0, &got);
    dec_target = FunParamGetd(fun, "DEC_TARG", 1, -2.0, &got);
    if( (ra_target == -1.0) || (dec_target == -2.0) ) {
      gerror(stderr,  "no aim point information in header: %s\n", iname);
    }
  }


  /* make sure we have wcs info */
  FunInfoGet(fun, FUN_WCS, &wcs, FUN_TYPE, &type, 0);
  if( !iswcs(wcs) ){
    gerror(stderr, "could not load WCS information from header: %s\n", iname);
  }

  /* open list to read positions from somewhere (list or stdin) */
  if( !(list=OpenList(lname, pos1str, pos2str, sky2im)) ){
    gerror(stderr, "can't FunOpen list file: %s\n", lname);
  }
  
  /* for tables, we need some extra column info */
  switch(type){
  case FUN_TABLE:
  case FUN_EVENTS:
    s0 = fun->header->table->col[fun->bin[0]].name;
    s1 = fun->header->table->col[fun->bin[1]].name;
    if( s0 ){
      FunColumnLookup(fun,  s0, 0, NULL, &tltyp[0], NULL, NULL, NULL, NULL);
      FunColumnLookup2(fun, s0, 0, &tlmin[0], NULL, &binsiz[0], NULL, NULL);
    }
    else{
      tlmin[0] = 0;
      binsiz[0] = 0;
      tltyp[0] = 'I';
    }
    if( s1 ){
      FunColumnLookup(fun,  s1, 0, NULL, &tltyp[1], NULL, NULL, NULL, NULL);
      FunColumnLookup2(fun, s1, 0, &tlmin[1], NULL, &binsiz[1], NULL, NULL);
    }
    else{
      tlmin[1] = 0;
      binsiz[1] = 0;
      tltyp[1] = 'I';
    }
    break;
  default:
    break;
  }

  /* ds9-style processing requires special treatment */
  if( dods9 ){
    tltyp[0] = 'J';
    tltyp[1] = 'J';
  }

  /* output header if necessary */
  if( dotab ){
    if( verbose ){
      fprintf(stdout, "# IFILE = %s\n", iname);
    }
    if( (sky2im) && (!offaxis) ){
      if( verbose ){
	for(i=0; i<2; i++){
	  fprintf(stdout, "# ICOL%d = %s\n", i+1, list->colname[i]);
	}
	for(i=0; i<2; i++){
	  fprintf(stdout, "# IUNITS%d = %c\n", i+1, list->type[i]);
	}
	fprintf(stdout, "# OCOL1 = X\n");
	fprintf(stdout, "# OCOL2 = Y\n");
	fprintf(stdout, "\n");
	fprintf(stdout, "%12.12s%c%12.12s%c", 
		list->colname[0], sp, list->colname[1], sp);
      }
      fprintf(stdout, "%12.12s%c%12.12s\n", "X",sp,"Y");
      if( verbose ) fprintf(stdout, "------------%c------------%c", sp, sp);
      fprintf(stdout, "------------%c------------\n", sp);
    } else if( (sky2im) && (offaxis) ){
      if( verbose ){
	for(i=0; i<2; i++){
	  fprintf(stdout, "# ICOL%d = %s\n", i+1, list->colname[i]);
	}
	for(i=0; i<2; i++){
	  fprintf(stdout, "# IUNITS%d = %c\n", i+1, list->type[i]);
	}
	fprintf(stdout, "# OCOL1 = X\n");
	fprintf(stdout, "# OCOL2 = Y\n");
	fprintf(stdout, "# OFF_AXIS = OFF_AXIS\n");
	fprintf(stdout, "\n");
	fprintf(stdout, "%12.12s%c%12.12s%c", 
		list->colname[0], sp, list->colname[1], sp);
      }
      fprintf(stdout, "%12.12s%c%12.12s%c%12.12s\n", "X",sp,"Y",sp,"OFF_AXIS");
      if( verbose ) fprintf(stdout, "------------%c------------%c", sp, sp);
      fprintf(stdout, "------------%c------------%c------------\n", sp, sp);
    }
    else{
      if( verbose ){
	for(i=0; i<2; i++){
	  fprintf(stdout, "# ICOL%d = %s\n", i+1, list->colname[i]);
	}
	fprintf(stdout, "# OCOL1 = RA\n");
	fprintf(stdout, "# OCOL2 = DEC\n");
	for(i=0; i<2; i++){
	  fprintf(stdout, "# OUNITS%d = %c\n", i+1, list->type[i]);
	}
	fprintf(stdout, "\n");
	if( verbose ) fprintf(stdout, "%12.12s%c%12.12s%c", 
			      list->colname[0], sp, list->colname[1], sp);
      }
      fprintf(stdout, "          RA%c         DEC\n", sp);
      if( verbose ) fprintf(stdout, "------------%c------------%c", sp, sp);
      fprintf(stdout, "------------%c------------\n", sp);
    }
    fflush(stdout);
  }

  /* if offaxis requested (and sky2img) */
  if( (sky2im) && (offaxis) ){
    /* convert the aim point to image pixels */
    wcs2pix(wcs, ra_target, dec_target, &dval3, &dval4, &offscl);
    if( verbose) {
      fprintf(stdout, "%12.6f%c%12.6f%c", ra_target, sp, dec_target, sp);
    }
    switch(type){
    case FUN_IMAGE:
    case FUN_ARRAY:
      x_target = dval3;
      y_target = dval4;
      break;
    case FUN_TABLE:
    case FUN_EVENTS:
      x_target = tli2p(dval3, tlmin[0], binsiz[0], tltyp[0]);
      y_target = tli2p(dval4, tlmin[1], binsiz[1], tltyp[1]);
      break;
    default:
      gerror(stderr, "unknown FITS data type\n");
      break;
    }
    if( verbose) {
      fprintf(stdout, "%12.2f%c%12.2f%c%10.3f\n", 
	      x_target, sp, y_target, sp, zero);
    }
    fflush(stdout);
  }

  /* get position values, convert, and output */
  while( NextFromList(list, &dval1, &dval2, sky2im) ){
    /* perform the required conversion */
    if( sky2im ){
      /* convert sky coordinates to image pixels */
      wcs2pix(wcs, dval1, dval2, &dval3, &dval4, &offscl);
      if( verbose ) fprintf(stdout, "%12.6f%c%12.6f%c", dval1, sp, dval2, sp);
      switch(type){
      case FUN_IMAGE:
      case FUN_ARRAY:
	fprintf(stdout, "%12.2f%c%12.2f%c", dval3, sp, dval4, sp);
	break;
      case FUN_TABLE:
      case FUN_EVENTS:
	dval3 = tli2p(dval3, tlmin[0], binsiz[0], tltyp[0]);
	dval4 = tli2p(dval4, tlmin[1], binsiz[1], tltyp[1]);
	fprintf(stdout, "%12.2f%c%12.2f%c", dval3, sp, dval4, sp);
	break;
      default:
	gerror(stderr, "unknown FITS data type\n");
	break;
      }
      if( offaxis ) {
	off_axis = 0.5*sqrt( (x_target - dval3)*(x_target - dval3) +
                             (y_target - dval4)*(y_target - dval4) );
	fprintf(stdout, "%10.3f\n", off_axis);
      } else {
	fprintf(stdout, "\n");
      }

      fflush(stdout);
    } else {
      switch(type){
      case FUN_IMAGE:
      case FUN_ARRAY:
	break;
      case FUN_TABLE:
      case FUN_EVENTS:
	dval1 = tlp2i(dval1, tlmin[0], binsiz[0], tltyp[0]);
	dval2 = tlp2i(dval2, tlmin[1], binsiz[1], tltyp[1]);
	break;
      default:
	gerror(stderr, "unknown FITS data type\n");
	break;
      }
      /* convert image pixels to sky coordinates */
      pix2wcs(wcs, (double)dval1, (double)dval2, &dval3, &dval4);
      if( list ){
	/* units */
	switch( list->type[0] ) {
	case 'h':
	  dval3 = d2h(dval3);
	  break;
	case 'r':
	  dval3 = d2r(dval3);
	  break;
	case 'd':
	  break;
	}
	/* units */
	switch( list->type[1] ) {
	case 'h':
	  dval4 = d2h(dval4);
	  break;
	case 'r':
	  dval4 = d2r(dval4);
	  break;
	case 'd':
	  break;
	}
      }
      if( verbose ) fprintf(stdout, "%12.2f%c%12.2f%c", dval1, sp, dval2, sp);
      fprintf(stdout, "%12.6f%c%12.6f\n", dval3, sp, dval4);
      fflush(stdout);
    }
  }

  /* close up shop */
  FunClose(fun);
  CloseList(list);
  return(0);
}