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
|
/*
*
* asc2fits foo.fits < foo.ascii
*
* This is an example of generating a binary table from specific ASCII input.
* The more general case is much harder.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <funtools.h>
#define SZ_LINE 1024
#define MAXREC 30
typedef struct EvStruct{
int x, y, pha;
double time;
} *Event, EventRec;
int main(int argc, char **argv)
{
int got, put;
char tbuf[SZ_LINE];
Fun fun;
EventRec events[MAXREC];
Event ev;
/* exit on gio errors */
setgerror(2);
if( argc < 2 ){
fprintf( stderr, "usage: %s oname\n", argv[0]);
exit(1);
}
/* open output file */
if( !(fun = FunOpen(argv[1],"w", NULL)) )
gerror(stderr, "Could not open the output file: %s\n", argv[1]);
/* set up the (hardwired) columns */
FunColumnSelect( fun, sizeof(EventRec), NULL,
"x", "J", "w", FUN_OFFSET(Event, x),
"y", "J", "w", FUN_OFFSET(Event, y),
"pha", "J", "w", FUN_OFFSET(Event, pha),
"time", "D", "w", FUN_OFFSET(Event, time),
NULL);
/* ignore first line, which is the header */
fgets(tbuf, SZ_LINE, stdin);
/* process data lines */
got = 0;
/* get next line */
while( fgets(tbuf, SZ_LINE, stdin) ){
/* ignore comments */
if( (*tbuf == '\n') || (*tbuf == '#') )
continue;
/* point to next buffer to fill */
ev = &events[got];
/* parse data record */
if(sscanf(tbuf, "%d %d %d %lf", &ev->x, &ev->y, &ev->pha, &ev->time) != 4)
break;
/* got another record */
got++;
/* flush this batch of records if necessary */
if( got == MAXREC ){
if( (put=FunTableRowPut(fun, events, got, 0, NULL)) != got ){
gerror(stderr, "expected to write %d rows; only wrote %d\n",
got, put);
}
/* reset record counter */
got = 0;
}
}
/* final flush */
if( got ){
if( (put=FunTableRowPut(fun, events, got, 0, NULL)) != got ){
gerror(stderr, "expected to write %d rows; only wrote %d\n",
got, put);
}
}
FunClose(fun);
return(0);
}
|