summaryrefslogtreecommitdiffstats
path: root/funtools/funtest/mkev.c
blob: 5cb6722865a83b31a586c04b61b2880adc90f496 (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
#if HAVE_CONFIG_H
#include <conf.h>
#endif

#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <math.h>
#include <prsetup.h>
#include <macro.h>
#include <swap.h>
#include <xalloc.h>

#ifndef UNUSED
#ifdef __GNUC__
#  define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
#else
#  define UNUSED(x) UNUSED_ ## x
#endif
#endif

extern char *optarg;
extern int optind;

#ifndef ABS
#define ABS(x)		((x)<0?(-x):(x))
#endif

#define EVSIZE 26

#define FITS_HEADER "\
SIMPLE  =                    T / FITS STANDARD                                  \
BITPIX  =                    8 / Binary data                                    \
NAXIS   =                    0 / No image array present                         \
EXTEND  =                    T / There may be standard extensions               \
END                                                                             \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
                                                                                \
XTENSION= 'BINTABLE'            /  FITS 3D BINARY TABLE                         \
BITPIX  =                    8 / Binary data                                    \
NAXIS   =                    2 / Table is a matrix                              \
NAXIS1  =                 $EVSIZE / Width of table in bytes                        \
NAXIS2  =           $TEV / Number of entries in table                     \
PCOUNT  =                    0 / Random parameter count                         \
GCOUNT  =                    1 / Group count                                    \
TFIELDS =                    7 / Number of fields in row                        \
EXTNAME = 'EVENTS            ' / Table name                                     \
EXTVER  =                    1 / Version number of table                        \
TFORM1  = '1I                ' / Data type for field                            \
TTYPE1  = 'X                 ' / Label for field                                \
TSCAL1  = 10.0                 / Label for field                                \
TZERO1  = 1.0                  / Label for field                                \
TUNIT1  = '        '            /  Physical units for field                     \
TFORM2  = '1I                ' / Data type for field                            \
TTYPE2  = 'Y                 ' / Label for field                                \
TSCAL2  = 20.0                 / Label for field                                \
TZERO2  = 2.0                  / Label for field                                \
TUNIT2  = '        '            /  Physical units for field                     \
TFORM3  = '1I                ' / Data type for field                            \
TTYPE3  = 'PHA               ' / Label for field                                \
TUNIT3  = '        '            /  Physical units for field                     \
TFORM4  = '1J                ' / Data type for field                            \
TTYPE4  = 'PI                ' / Label for field                                \
TUNIT4  = '        '            /  Physical units for field                     \
TFORM5  = '1D                ' / Data type for field                            \
TTYPE5  = 'TIME              ' / Label for field                                \
TUNIT5  = '        '            /  Physical units for field                     \
TFORM6  = '1E                ' / Data type for field                            \
TTYPE6  = 'DX                ' / Label for field                                \
TUNIT6  = '        '            /  Physical units for field                     \
TFORM7  = '1E                ' / Data type for field                            \
TTYPE7  = 'DY                ' / Label for field                                \
TUNIT7  = '        '            /  Physical units for field                     \
RADECSYS= 'FK5     '            /  WCS for this file (e.g. Fk4)                 \
EQUINOX =           2.000000E3  /  equinox (epoch) for WCS                      \
TCTYP1  = 'RA---TAN'          / axis type (e.g. RA---TAN)                       \
TCTYP2  = 'DEC--TAN'          / axis type (e.g. RA---TAN)                       \
TCRVL1  = 9.000000000000000E1   / sky coord (deg.)                              \
TCRVL2  = 6.000000000000000E1   / sky coord (deg.)                              \
TCDLT1  = -1.00000000000000E-1   / degrees per pixel                            \
TCDLT2  = 1.000000000000002E-1   / degrees per pixel                            \
TCRPX1  = 5.000000000000000E0   / pixel of tangent plane direc.                 \
TCRPX2  = 5.000000000000000E0   / pixel of tangent plane direc.                 \
TCROT2  = 0.000000000000000E0   / rotation angle (degrees)                      \
TLMIN1  =           $IXLO / Min. axis value                                \
TLMAX1  =           $IXHI / Max. axis value                                \
TLMIN2  =           $IYLO / Min. axis value                                \
TLMAX2  =           $IYHI / Max. axis value                                \
TLMIN6  =           $XLO / Min. axis value                                \
TLMAX6  =           $XHI / Max. axis value                                \
TLMIN7  =           $YLO / Min. axis value                                \
TLMAX7  =           $XHI / Max. axis value                                \
END                                                                                                                                                                                                                                                                                                                                                                                                             \
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                "

/* these are used in the macro expansion and must be global */
static double xlo, xhi, ylo, yhi;
static int tev;
static char macrobuf[SZ_LINE];

#ifdef ANSI_FUNC
static char *
MacroCB(char *name, void *UNUSED(client_data))
#else
static char *MacroCB(name, client_data)
	char *name;
	void *client_data;
#endif
{
  if( !strcmp(name, "TEV") ){
    sprintf(macrobuf, "%10d", tev);
    return macrobuf;
  }
  else if( !strcmp(name, "XLO") ){
    sprintf(macrobuf, "%10.2f", xlo);
    return macrobuf;
  }
  else if( !strcmp(name, "XHI") ){
    sprintf(macrobuf, "%10.2f", xhi);
    return macrobuf;
  }
  else if( !strcmp(name, "IXLO") ){
    if( xlo > 0 )
      sprintf(macrobuf, "%10d", (int)xlo);
    else
      sprintf(macrobuf, "%10d", (int)ceil(xlo));
    return macrobuf;
  }
  else if( !strcmp(name, "IXHI") ){
    if( xhi > 0 )
      sprintf(macrobuf, "%10d", (int)xhi);
    else
      sprintf(macrobuf, "%10d", (int)ceil(xhi));
    return macrobuf;
  }
  else if( !strcmp(name, "DIM1") ){
    sprintf(macrobuf, "%6d", (int)(xhi-xlo));
    return macrobuf;
  }
  else if( !strcmp(name, "YLO") ){
    sprintf(macrobuf, "%10.2f", ylo);
    return macrobuf;
  }
  else if( !strcmp(name, "YHI") ){
    sprintf(macrobuf, "%10.2f", yhi);
    return macrobuf;
  }
  else if( !strcmp(name, "IYLO") ){
    if( ylo > 0 )
      sprintf(macrobuf, "%10d", (int)ylo);
    else
      sprintf(macrobuf, "%10d", (int)ceil(ylo));
    return macrobuf;
  }
  else if( !strcmp(name, "IYHI") ){
    if( yhi > 0 )
      sprintf(macrobuf, "%10d", (int)yhi);
    else
      sprintf(macrobuf, "%10d", (int)ceil(yhi));
    return macrobuf;
  }
  else if( !strcmp(name, "DIM2") ){
    sprintf(macrobuf, "%6d", (int)(yhi-ylo));
    return macrobuf;
  }
  else if( !strcmp(name, "EVSIZE") ){
    sprintf(macrobuf, "%4d", EVSIZE);
    return macrobuf;
  }
  else{
    return NULL;
  }
}

#ifdef ANSI_FUNC
int 
main (int argc, char **argv)
#else
main(argc, argv)
     int argc;
     char **argv;
#endif
{
  int c;
  int convert;
  int nev;
  int ii, kk;
  int pad;
  int pi;
  int args;
  int doraw=0;
  char *padbuf;
  short x, y;
  short pha;
  double i, j;
  double time;
  float dx, dy;
  char *s, *t;

  /* process switch arguments */
  while ((c = getopt(argc, argv, "r")) != -1){
    switch(c){
    case 'r':
      doraw = 1;
      break;
    }
  }

  /* check for required arguments */
  args = argc - optind;
  /* process arguments */
  if( args != 4 ){
    fprintf(stderr, "usage: %s xlo xhi ylo yhi\n", argv[0]);
    exit(1);
  }
  else{
    xlo = atof(argv[optind]);
    xhi = atof(argv[optind+1]);
    ylo = atof(argv[optind+2]);
    yhi = atof(argv[optind+3]);
  }

  /* get initial header string */
  s = xstrdup(FITS_HEADER);
  /* see if we have to convert to IEEE  format */
  convert = !is_bigendian();

  /* determine how many events we will have */
  for(tev=0,j=ylo; j<=yhi; j++){
    for(i=xlo; i<=xhi; i++){
      if( i != j )
	nev = 1;
      else
	nev = ABS(i);
      tev += nev;
    }
  }

  /* expand header to fill in number of events and dimensions */
  t = ExpandMacro(s, NULL, NULL, 0, MacroCB, NULL);

  /* write header, if necessary */
  if( !doraw ){
    ii = strlen(t);
    if( (ii % 2880) != 0 ){
      fprintf(stderr, "internal error: bad size (%d) for header\n", ii);
    }
    write(1, t, ii);
  }

  /* write events */
  for(tev=0,j=ylo; j<=yhi; j++){
    for(i=xlo; i<=xhi; i++){
      if( i != j )
	nev = 1;
      else
	nev = ABS(i);
      tev += nev;
      for(kk=0; kk<nev; kk++){
	x = i;
	y = j;
	pha = i;
	pi = j;
	time = (double)tev + (double)(i+j+kk)/100.0;
	dx = i;
	dy = j;
	if( convert ){
	  swap_short(&x, 1);
	  swap_short(&y, 1);
	  swap_short(&pha, 1);
	  swap_int(&pi, 1);
	  swap_double(&time, 1);
	  swap_float(&dx, 1);
	  swap_float(&dy, 1);
	}
	write(1, &x, sizeof(short));
	write(1, &y, sizeof(short));
	write(1, &pha, sizeof(short));
	write(1, &pi, sizeof(int));
	write(1, &time, sizeof(double));
	write(1, &dx, sizeof(float));
	write(1, &dy, sizeof(float));
      }
    }
  }

  /* write padding, if necessary */
  if( !doraw ){
    pad = 2880 - ((tev*EVSIZE)%2880);
    if( pad == 2880 ) pad = 0;
    if( pad ){
      padbuf = (char *)calloc(pad, sizeof(char));
      write(1, padbuf, pad);
    }
  }

  return(0);
}