summaryrefslogtreecommitdiffstats
path: root/funtools/faq/faq2.html
blob: f5b424b33ac5b70858aa0f649adb4c7696f714e3 (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
<html><head><title>Funtools FAQ: Programming</title></head>

<ol>
<li><a name="faq1"><b>What are the compile/link commands for various platforms?</b></a>
<P>
Assuming, for example, that funtools is installed in /soft/saord and that
gcc is the compiler being used:
<ol>
<li> Linux:
<BLOCKQUOTE><CODE>
<PRE>
gcc -g -I/soft/saord/include -c -o foo.o foo.c
gcc -g foo.o -o foo -L/soft/saord/lib -lfuntools -ldl -lm
</PRE>
</CODE></BLOCKQUOTE>
<li> Apple OS X:
<BLOCKQUOTE><CODE>
<PRE>
gcc -g -no-cpp-precomp -fno-common -I/soft/saord/include -c -o foo.o foo.c
gcc -g foo.o -o foo -L/soft/saord/lib -lfuntools -lm
</PRE>
</CODE></BLOCKQUOTE>
<li> Sun Solaris:
<BLOCKQUOTE><CODE>
<PRE>
gcc -g -no-cpp-precomp -fno-common -I/soft/saord/include -c -o foo.o foo.c
gcc -g foo.o -o foo -L/soft/saord/lib -lfuntools -lsocket -lnsl -ldl -lm
</PRE>
</CODE></BLOCKQUOTE>

</ol>

<li><a name="faq2"><b>What is the simplest possible program?</b></a>
<P>
<BLOCKQUOTE><CODE>
<PRE>
#include &lt;stdlib.h&gt;
#include &lt;funtools.h&gt;

int main(int argc, char **argv)
{
  Fun fun;

  /* sanity check */
  if( argc &lt; 2 ) return 1;
  /* open file for reading */
  if( !(fun=FunOpen(argv[1], "r", NULL)) ){
    fprintf(stderr, "ERROR: can't open funtools file: %s\n", argv[1]);
    return 1;
  }
  /* close file */
  FunClose(fun);
  return 0;
}
</PRE>
</CODE></BLOCKQUOTE>

<li><a name="faq1.1"><b>How do I read and display events?</b></a>
<P>
<BLOCKQUOTE><CODE>
<PRE>#
#include &lt;stdlib.h&gt;
#include &lt;funtools.h&gt;

typedef struct evstruct{
  double x, y;
  int pi, pha;
} *Ev, EvRec;

int main(int argc, char **argv)
{
  int i, got;
  int maxrow=1024;
  Ev ev, ebuf=NULL;
  Fun fun;

  /* sanity check */
  if( argc &lt; 2 ) return 1;
  /* open file for reading */
  if( !(fun=FunOpen(argv[1], "r", NULL)) ){
    fprintf(stderr, "ERROR: can't open funtools file: %s\n", argv[1]);
    return 1;
  }
  /* select columns to read (and data types to convert to) */
  got = FunColumnSelect(fun, sizeof(EvRec), NULL,
			"x",       "D", "r", FUN_OFFSET(Ev, x),
			"y",       "D", "r", FUN_OFFSET(Ev, y),
			"pha",     "J", "r", FUN_OFFSET(Ev, pha),
			"pi",      "J", "r", FUN_OFFSET(Ev, pi),
			NULL);
  /* read and process events */
  while( (ebuf=(void *)FunTableRowGet(fun, NULL, maxrow, NULL, &got)) && got ){
    for(i=0; i&lt;got; i++){
      ev = (Ev)ebuf+i;
      fprintf(stdout, "%.1f %.1f %d %d\n", ev->x, ev->y, ev->pha, ev->pi);
    }
    if( ebuf) free(ebuf);
  }
  /* close file */
  FunClose(fun);
  return 0;
}
</PRE>
</CODE></BLOCKQUOTE>

<li><a name="faq1.2"><b>How do I change the value of a single column in all events?</b></a>
<P>
<BLOCKQUOTE><CODE>
<PRE>
#include &lt;stdlib.h&gt;
#include &lt;funtools.h&gt;

typedef struct evstruct{
  int pi;
} *Ev, EvRec;

int main(int argc, char **argv)
{
  int i, got;
  int maxrow=1024;
  Ev ev, ebuf=NULL;
  Fun fun, ofun;

  /* sanity check */
  if( argc &lt; 3 ) return 1;
  /* open file for reading */
  if( !(fun=FunOpen(argv[1], "r", NULL)) ){
    fprintf(stderr, "ERROR: can't open input funtools file: %s\n", argv[1]);
    return 1;
  }
  /* open output file and inherit header params, columns, etc. from input */
  if( !(ofun=FunOpen(argv[2], "w", fun)) ){
    fprintf(stderr, "ERROR: can't open output funtools file: %s\n", argv[2]);
    return 1;
  }
  /* select columns to read (and data types to convert to) */
  /* use "merge=update" to change value while keeping original data type */
  /* use "merge=replace" to change boh the value and data type */
  got = FunColumnSelect(fun, sizeof(EvRec), "merge=update",
			"pi",     "J", "rw", FUN_OFFSET(Ev, pi),
			NULL);
  /* read and process events */
  while( (ebuf=(void *)FunTableRowGet(fun, NULL, maxrow, NULL, &got)) && got ){
    for(i=0; i&lt;got; i++){
      ev = (Ev)ebuf+i;
      ev->pi = ev->pi + 1;
    }
    /* write rows to output file */
    if( FunTableRowPut(ofun, ebuf, got, 0, NULL) != got ){
      fprintf(stderr, "ERROR: writing to funtools file: %s\n", argv[2]);
      return 1;
    }
    if( ebuf) free(ebuf);
  }
  /* close files */
  FunClose(ofun);
  FunClose(fun);
  return 0;
}
</PRE>
</CODE></BLOCKQUOTE>

<li><a name="faq1.3"><b>How do I process events based on the region each is in?</b></a>
<P>
<BLOCKQUOTE><CODE>
<PRE>
#include &lt;stdlib.h&gt;
#include &lt;funtools.h&gt;

typedef struct evstruct{
  int x, y;
  int pi, pha;
  int region;
} *Ev, EvRec;

int main(int argc, char **argv)
{
  int i, got;
  int maxrow=1024;
  Ev ev, ebuf=NULL;
  Fun fun;

  /* sanity check */
  if( argc &lt; 2 ) return 1;
  /* open file for reading */
  if( !(fun=FunOpen(argv[1], "r", NULL)) ){
    fprintf(stderr, "ERROR: can't open funtools file: %s\n", argv[1]);
    return 1;
  }
  /* select columns to read (and data types to convert to) */
  /* specifying $REGION will retrieve the one-indexed region number */
  /* events passing the filter but not in a region will have value -1 */
  got = FunColumnSelect(fun, sizeof(EvRec), NULL,
			"x",       "J", "r", FUN_OFFSET(Ev, x),
			"y",       "J", "r", FUN_OFFSET(Ev, y),
			"pha",     "J", "r", FUN_OFFSET(Ev, pha),
			"pi",      "J", "r", FUN_OFFSET(Ev, pi),
			"$REGION", "J", "r", FUN_OFFSET(Ev, region),
			NULL);
  /* read and process events */
  while( (ebuf=(void *)FunTableRowGet(fun, NULL, maxrow, NULL, &got)) && got ){
    for(i=0; i&lt;got; i++){
      ev = (Ev)ebuf+i;
      fprintf(stdout, "%4d %4d %3d %3d %4d\n", 
	      ev->x, ev->y, ev->pha, ev->pi, ev->region);
    }
    if( ebuf) free(ebuf);
  }
  /* close file */
  FunClose(fun);
  return 0;
}
</PRE>
</CODE></BLOCKQUOTE>

<li><a name="faq1.4"><b>How do I make a FITS event file from a non-FITS source (and add my own params)?</b></a>
<P>
<BLOCKQUOTE><CODE>
<PRE>
#include &lt;stdlib.h&gt;
#include &lt;funtools.h&gt;

typedef struct evstruct{
  int x, y;
  int pha;
  float pi;
} *Ev, EvRec;

int main(int argc, char **argv)
{
  int i, nev;
  int pmod=16, put=1;
  double pinc=0.1234;
  char xbuf[32], ybuf[32];
  Ev ev;
  Fun ofun;

  /* sanity check */
  if( argc &lt; 3 ) return 1;
  /* open new file for writing */
  if( !(ofun=FunOpen(argv[1], "w", NULL)) ){
    fprintf(stderr, "ERROR: can't open funtools file: %s\n", argv[1]);
    return 1;
  }
  if( (nev = atoi(argv[2])) <=0 ) return 1;
  ev = (Ev)calloc(1, sizeof(EvRec));
  /* The pair of numeric values specify the data value range, used to bin
     x, y into an image. They are permitted but not needed for pi and pha */
  sprintf(xbuf, "J:1:%d", nev);
  sprintf(ybuf, "J:1:%d", nev);
  /* select columns to write */
  FunColumnSelect(ofun, sizeof(EvRec), NULL,
		  "x",   xbuf,      "w", FUN_OFFSET(Ev, x),
		  "y",   ybuf,      "w", FUN_OFFSET(Ev, y),
		  "pha", "J:1:16",  "w", FUN_OFFSET(Ev, pha),
		  "pi",  "E",       "w", FUN_OFFSET(Ev, pi),
		  NULL);
  /* write params to header; generally added before first event is written */
  FunParamPuti(ofun, "PMOD",  0, pmod,  "modulus for pha generation", 1);
  FunParamPutd(ofun, "PINC",  0, pinc,  7, "increment for pi generation", 1);
  /* make up events */
  for(i=1; i<=nev; i++){
    ev->x = i; ev->y = nev-i+1; ev->pha = i % pmod; ev->pi = ev->pha + pinc;
    /* write rows to output file -- this can be done in batches, of course */
    if( FunTableRowPut(ofun, ev, put, 0, NULL) != put ){
      fprintf(stderr, "ERROR: writing to funtools file: %s\n", argv[1]);
      return 1;
    }
  }
  if( ev) free(ev);
  /* close file */
  FunClose(ofun);
  return 0;
}
</PRE>
</CODE></BLOCKQUOTE>

<li><a name="faq2.1"><b>How do I process an image in double float format?</b></a>
<P>
<BLOCKQUOTE><CODE>
<PRE>
#include &lt;stdlib.h&gt;
#include &lt;funtools.h&gt;

int main(int argc, char **argv)
{
  int i, j, dim1, dim2;
  double *buf;
  Fun fun;

  /* sanity check */
  if( argc &lt; 2 ) return 1;
  /* open file for reading */
  if( !(fun=FunOpen(argv[1], "r", NULL)) ){
    fprintf(stderr, "ERROR: can't open funtools file: %s\n", argv[1]);
    return 1;
  }
  /* extract (and bin, if necessary) data into a double prec. image buffer */
  if( !(buf = FunImageGet(fun, NULL, "bitpix=-64")) ){
    fprintf(stderr, "ERROR: can't get image: %s\n", argv[1]);
    return 1;
  }
  /* get image dimensions after FunImageGet, in case an image section
     was specified on the command line, which changes image dimensions */
  FunInfoGet(fun, FUN_SECT_DIM1, &dim1, FUN_SECT_DIM2, &dim2, 0);
  /* loop through image */
  for(i=0; i&lt;dim2; i++){
    for(j=0; j&lt;dim1; j++){
      fprintf(stdout, "%.1f ", buf[i*dim1+j]);
    }
    fprintf(stdout, "\n");
  }
  /* close file */
  FunClose(fun);
  return 0;
}
</PRE>
</CODE></BLOCKQUOTE>

<li><a name="faq2.1"><b>How do I process an image in its native format?</b></a>
<BLOCKQUOTE><CODE>
<PRE>
#include &lt;funtools.h&gt;

int main(int argc, char **argv)
{
  int i, j, bitpix, dim1, dim2;
  double *buf;
  unsigned char *cbuf;
  short *sbuf;
  int *ibuf;
  float *fbuf;
  double *dbuf;
  Fun fun;

  /* sanity check */
  if( argc &lt; 2 ) return 1;
  /* open file for reading */
  if( !(fun=FunOpen(argv[1], "r", NULL)) ){
    fprintf(stderr, "ERROR: can't open funtools file: %s\n", argv[1]);
    return 1;
  }
  /* extract (and bin, if necessary) data into a buffer whose
     data type is not specified and therefore is that of the file */
  if( !(buf = FunImageGet(fun, NULL, NULL)) ){
    fprintf(stderr, "ERROR: can't get image: %s\n", argv[1]);
    return 1;
  }
  /* get image dimensions after FunImageGet, in case an image section
     was specified on the command line, which changes image dimensions */
  FunInfoGet(fun, FUN_SECT_BITPIX, &bitpix, 
	     FUN_SECT_DIM1, &dim1, FUN_SECT_DIM2, &dim2, 0);
  /* set appropriate data type buffer to point to image buffer */
  switch(bitpix){
    case 8:   cbuf = (unsigned char *)buf; break;
    case 16:  sbuf = (short *)buf;         break;
    case 32:  ibuf = (int *)buf;           break;
    case -32: fbuf = (float *)buf;         break;
    case -64: dbuf = (double *)buf;        break;
  }
  /* loop through image */
  for(i=0; i&lt;dim2; i++){
    for(j=0; j&lt;dim1; j++){
      switch(bitpix){
        case 8:   fprintf(stdout, "%4d ",  cbuf[i*dim1+j]);  break;
        case 16:  fprintf(stdout, "%6d ",  sbuf[i*dim1+j]);  break;
        case 32:  fprintf(stdout, "%9d ",  ibuf[i*dim1+j]);  break;
        case -32: fprintf(stdout, "%.2f ", fbuf[i*dim1+j]);  break;
        case -64: fprintf(stdout, "%.6f ", dbuf[i*dim1+j]);  break;
      }
    }
    fprintf(stdout, "\n");
  }
  /* close file */
  FunClose(fun);
  return 0;
}
</PRE>
</CODE></BLOCKQUOTE>

</ol>