1 | |
---|
2 | /* file gcf2ascii.c |
---|
3 | * =========== |
---|
4 | * |
---|
5 | * version 1, 19-Oct-2003 |
---|
6 | * |
---|
7 | * Converts GCF data to ASCII |
---|
8 | * |
---|
9 | * K. Stammler, 19-Oct-2003 |
---|
10 | */ |
---|
11 | |
---|
12 | #include <stdio.h> |
---|
13 | #include <string.h> |
---|
14 | #include "basecnst.h" |
---|
15 | #include "sysbase.h" |
---|
16 | #include "tcusrdef.h" |
---|
17 | #include "gcflib.h" |
---|
18 | |
---|
19 | |
---|
20 | |
---|
21 | int main( int argc, char *argv[] ) |
---|
22 | { |
---|
23 | /* local variables */ |
---|
24 | char gcffile[cBcFileLth+1]; /* input GCF file */ |
---|
25 | FILE *gcf; /* pointer to GCF file */ |
---|
26 | GcfRawHeaderT rhdr; /* raw header */ |
---|
27 | GcfHeaderT hdr; /* decoded header */ |
---|
28 | int i; /* counter */ |
---|
29 | char str[cBcLineLth+1]; /* text string */ |
---|
30 | char recend[cBcTimeLth+1]; /* end time of block */ |
---|
31 | char lasttime[cBcTimeLth+1]; /* last block end time */ |
---|
32 | float gaplth; /* length of data gap */ |
---|
33 | GcfLongT last_stream_id; /* last stream ID */ |
---|
34 | TSyStatus status; /* return status */ |
---|
35 | int trccnt; /* trace counter */ |
---|
36 | int trclth; /* lengthof output trace */ |
---|
37 | GcfLongT *datablock; /* memory for data block */ |
---|
38 | int *blocksmp; /* memory for samples of a block */ |
---|
39 | int numsmp; /* number of samples in a block */ |
---|
40 | int crcnt; /* carriage return counter */ |
---|
41 | |
---|
42 | /* executable code */ |
---|
43 | |
---|
44 | if (argc != 2) { |
---|
45 | fprintf( stderr, "Usage: %s <inpfile>\n", argv[0] ); |
---|
46 | return 1; |
---|
47 | } /*endif*/ |
---|
48 | |
---|
49 | /* get parameters */ |
---|
50 | strcpy ( gcffile, argv[1] ); |
---|
51 | |
---|
52 | status = cBcNoError; |
---|
53 | |
---|
54 | GcfSetTapcode( 2, "HH", &status ); |
---|
55 | if (SySevere(&status)) err_writemsg( status, "setting tapcode", TRUE ); |
---|
56 | |
---|
57 | gcf = fopen( gcffile, "r" ); |
---|
58 | if (gcf == NULL) { |
---|
59 | fprintf( stderr, "%s: cannot open input file %s\n", argv[0], gcffile ); |
---|
60 | return 1; |
---|
61 | } /*endif*/ |
---|
62 | |
---|
63 | datablock = GcfAllocBlockMem( &status ); |
---|
64 | if (SySevere(&status)) err_writemsg( status, "error reading header", TRUE ); |
---|
65 | blocksmp = GcfAllocBlockSamples( &status ); |
---|
66 | if (SySevere(&status)) err_writemsg( status, "error reading header", TRUE ); |
---|
67 | |
---|
68 | *lasttime = '\0'; |
---|
69 | gaplth = 1.0; |
---|
70 | last_stream_id = 0; |
---|
71 | trccnt = 0; |
---|
72 | trclth = 0; |
---|
73 | crcnt = 1; |
---|
74 | |
---|
75 | FOREVER { |
---|
76 | |
---|
77 | GcfReadRawHeader( gcf, &rhdr, &status ); |
---|
78 | if (status == GcfERR_EOF_FOUND) { |
---|
79 | status = cBcNoError; |
---|
80 | break; |
---|
81 | } /*endif*/ |
---|
82 | if (SySevere(&status)) err_writemsg( status, "error reading header", TRUE ); |
---|
83 | |
---|
84 | GcfDecodeHeader( &rhdr, &hdr, &status ); |
---|
85 | if (SySevere(&status)) |
---|
86 | err_writemsg( status, "error reading header", TRUE ); |
---|
87 | |
---|
88 | /* check for data gap */ |
---|
89 | tc_tadd( hdr.blktime, (float)(hdr.numrec*hdr.cmpcode/hdr.smprate), |
---|
90 | recend, &status ); |
---|
91 | if (SySevere(&status)) |
---|
92 | err_writemsg( status, "error computing end time", TRUE ); |
---|
93 | if (*lasttime != '\0') |
---|
94 | gaplth = tc_tdiff( hdr.blktime, lasttime, &status ); |
---|
95 | |
---|
96 | /* check for multiplexed data (i.e. other station,channel) */ |
---|
97 | if (last_stream_id == 0) last_stream_id = rhdr.stream_id; |
---|
98 | |
---|
99 | if (gaplth > 0.0 || (last_stream_id != rhdr.stream_id)) { |
---|
100 | if (trccnt > 0) |
---|
101 | printf( "\n@@LTH%04d@@ %d\n\n", trccnt, trclth ); |
---|
102 | trclth = 0; |
---|
103 | crcnt = 1; |
---|
104 | trccnt++; |
---|
105 | printf( "STATION: %s\n", hdr.station ); |
---|
106 | printf( "COMP: %c\n", hdr.comp ); |
---|
107 | printf( "START: %s\n", hdr.blktime ); |
---|
108 | printf( "LENGTH: @@LTH%04d@@\n", trccnt ); |
---|
109 | printf( "DELTA: %g\n", 1.0/(float)hdr.smprate ); |
---|
110 | printf( "\n" ); |
---|
111 | } /*endif*/ |
---|
112 | |
---|
113 | GcfReadDataBlock( gcf, &rhdr, datablock, &status ); |
---|
114 | if (SySevere(&status)) |
---|
115 | err_writemsg( status, "error reading data block", TRUE ); |
---|
116 | |
---|
117 | GcfDecodeBlock( &rhdr, datablock, blocksmp, &numsmp, &status ); |
---|
118 | if (SySevere(&status)) |
---|
119 | err_writemsg( status, "error decoding data block", TRUE ); |
---|
120 | trclth += numsmp; |
---|
121 | |
---|
122 | /* write out samples */ |
---|
123 | for (i=0; i<numsmp; i++) { |
---|
124 | printf( " %d", blocksmp[i] ); |
---|
125 | if (crcnt++ % 10 == 0) printf( "\n" ); |
---|
126 | } /*endif*/ |
---|
127 | |
---|
128 | strcpy( lasttime, recend ); |
---|
129 | last_stream_id = rhdr.stream_id; |
---|
130 | |
---|
131 | } /*endwhile*/ |
---|
132 | |
---|
133 | if (trccnt > 0) |
---|
134 | printf( "\n@@LTH%04d@@ %d\n\n", trccnt, trclth ); |
---|
135 | |
---|
136 | fclose( gcf ); |
---|
137 | GcfFreeMem( datablock ); |
---|
138 | GcfFreeMem( blocksmp ); |
---|
139 | |
---|
140 | return 0; |
---|
141 | |
---|
142 | } /* end of main */ |
---|