1 | |
---|
2 | /* file seed512to4096.c |
---|
3 | * =============== |
---|
4 | * |
---|
5 | * version 5, 2-Feb-2007 |
---|
6 | * |
---|
7 | * Reformats 512 byte records to 4096 byte records by reading 8 512-records |
---|
8 | * and writing one 4096-record. Some space in the output record remains |
---|
9 | * unused. |
---|
10 | * K. Stammler, 17-Sep-98 |
---|
11 | * |
---|
12 | * v 5, K.S. changed MAXSAMPLE from 5000 to 1000 |
---|
13 | */ |
---|
14 | |
---|
15 | |
---|
16 | #include <stdio.h> |
---|
17 | #include <string.h> |
---|
18 | #include "basecnst.h" |
---|
19 | #ifdef BC_INC_STDLIB |
---|
20 | #include BC_INC_STDLIB |
---|
21 | #endif |
---|
22 | #include "sysbase.h" |
---|
23 | #include "erusrdef.h" |
---|
24 | #include "tcusrdef.h" |
---|
25 | #include "cpar.h" |
---|
26 | #include "seedcfg.h" |
---|
27 | #include "seed_lib.h" |
---|
28 | |
---|
29 | |
---|
30 | #define INPRECSIZE 512 |
---|
31 | /* input record length */ |
---|
32 | #define OUTRECSIZE 4096 |
---|
33 | /* output record length */ |
---|
34 | #define MERGEREC 8 |
---|
35 | /* this number of 512 records is merged to one 4096 */ |
---|
36 | #define MAXSAMPLE 10000 |
---|
37 | /* array size */ |
---|
38 | #define MAXSMPPERREC 3772 |
---|
39 | /* maximum number of samples in one 4096 block */ |
---|
40 | |
---|
41 | |
---|
42 | |
---|
43 | /* global variables */ |
---|
44 | static SeedSbyteT cv_seedrec[INPRECSIZE]; /* SEED input record */ |
---|
45 | static SeedSbyteT cv_wrk[OUTRECSIZE*2]; /* SEED workspace */ |
---|
46 | static INT32 cv_smp[MAXSAMPLE]; /* decoded samples */ |
---|
47 | |
---|
48 | |
---|
49 | int main( int argc, char *argv[] ) |
---|
50 | { |
---|
51 | /* local variables */ |
---|
52 | SeedDataHeaderT hdrproto; /* header prototype */ |
---|
53 | TSyStatus status; /* status value */ |
---|
54 | char fname[cBcFileLth+1]; /* name of input file */ |
---|
55 | FILE *fp; /* pointer to input file */ |
---|
56 | int read_ret; /* read return value */ |
---|
57 | int mergecnt; /* merge counter */ |
---|
58 | float dt; /* sample distance in sec */ |
---|
59 | float tdiff; /* time difference */ |
---|
60 | NTIME lasttime; /* end time of previous record */ |
---|
61 | NTIME bt, et; /* begin and end time of record */ |
---|
62 | NTIME wt; /* start time of sample array */ |
---|
63 | TSyBoolean gap_found; /* gap found */ |
---|
64 | TSyBoolean swap; /* swap data */ |
---|
65 | INT32 smpnum; /* current number of samples */ |
---|
66 | INT32 new_smpnum; /* number of samples in current rec. */ |
---|
67 | INT32 prev_smp; /* previos sample */ |
---|
68 | int av_timequal; /* average time quality over 4096 rec */ |
---|
69 | int cur_timequal; /* time qulaity of current 512 rec */ |
---|
70 | int tq_cnt; /* time quality counter */ |
---|
71 | UBYTE activity, ioflags, quality; /* data flags in header */ |
---|
72 | TSyBoolean flushbuf; /* flush buffer */ |
---|
73 | |
---|
74 | /* executable code */ |
---|
75 | |
---|
76 | status = cBcNoError; |
---|
77 | |
---|
78 | /* initialize seed routines */ |
---|
79 | SeedLibInitialize( &status ); |
---|
80 | if (Severe(&status)) err_writemsg( status, "", TRUE ); |
---|
81 | SeedSetOutputReclth( OUTRECSIZE ); |
---|
82 | |
---|
83 | /* get parameters */ |
---|
84 | pa_init( argc, argv ); |
---|
85 | if (pa_pnumber() != 1) { |
---|
86 | fprintf( stderr, "Usage: %s <file512>\n", pa_progname() ); |
---|
87 | fprintf( stderr, " qualifiers:\n" ); |
---|
88 | fprintf( stderr, " -swap swap data\n" ); |
---|
89 | fprintf( stderr, " -noswap don't swap data\n" ); |
---|
90 | return 1; |
---|
91 | } /*endif*/ |
---|
92 | strcpy( fname, pa_pvalue(1) ); |
---|
93 | swap = TRUE; |
---|
94 | if (pa_qspecified("-swap")) swap = TRUE; |
---|
95 | if (pa_qspecified("-noswap")) swap = FALSE; |
---|
96 | |
---|
97 | /* open input file */ |
---|
98 | fp = fopen( fname, "r" ); |
---|
99 | if (fp == NULL) { |
---|
100 | fprintf( stderr, "%s: input file %s not found\n", pa_progname(), fname ); |
---|
101 | return 1; |
---|
102 | } /*endif*/ |
---|
103 | |
---|
104 | mergecnt = 0; |
---|
105 | lasttime.month = 0; |
---|
106 | gap_found = FALSE; |
---|
107 | smpnum = 0; |
---|
108 | prev_smp = 0; |
---|
109 | av_timequal = cur_timequal = tq_cnt = 0; |
---|
110 | activity = ioflags = quality = (UBYTE)0; |
---|
111 | |
---|
112 | FOREVER { |
---|
113 | |
---|
114 | /* read next record */ |
---|
115 | read_ret = fread( cv_seedrec, 1, INPRECSIZE, fp ); |
---|
116 | if (read_ret == 0) break; |
---|
117 | if (read_ret < INPRECSIZE) { |
---|
118 | fprintf( stderr, "%s: read error on %s, abort.\n", pa_progname(), |
---|
119 | fname ); |
---|
120 | break; |
---|
121 | } /*endif*/ |
---|
122 | # ifdef cBc_OS_LINUX |
---|
123 | SeedSwapHeader( cv_seedrec ); |
---|
124 | # endif |
---|
125 | /* use only data records */ |
---|
126 | if (cv_seedrec[6] != 'D') continue; |
---|
127 | SeedStoreReclth( (SeedDataHeaderT *)cv_seedrec, INPRECSIZE ); |
---|
128 | SeedRecordTime( cv_seedrec, &bt, &et, &status ); |
---|
129 | if (SySevere(&status)) { |
---|
130 | fprintf( stderr, "%s: decode error on %s\n", pa_progname(), fname ); |
---|
131 | status = cBcNoError; |
---|
132 | continue; |
---|
133 | } /*endif*/ |
---|
134 | |
---|
135 | /* check for gaps */ |
---|
136 | if (lasttime.month == 0) { |
---|
137 | dt = SeedGetSampleDist( (SeedDataHeaderT *)cv_seedrec ); |
---|
138 | hdrproto = *(SeedDataHeaderT *)cv_seedrec ; |
---|
139 | SeedSetHeaderPrototype( &hdrproto ); |
---|
140 | wt = bt; |
---|
141 | } else { |
---|
142 | tdiff = tc_ndiff( &bt, &lasttime, &status ); |
---|
143 | gap_found = (Abs(tdiff) > dt/2.0); |
---|
144 | } /*endif*/ |
---|
145 | lasttime = et; |
---|
146 | |
---|
147 | /* store data flags */ |
---|
148 | activity |= ((SeedDataHeaderT *)cv_seedrec)->activity; |
---|
149 | ioflags |= ((SeedDataHeaderT *)cv_seedrec)->ioflags; |
---|
150 | quality |= ((SeedDataHeaderT *)cv_seedrec)->quality; |
---|
151 | |
---|
152 | /* increment record read counter */ |
---|
153 | mergecnt++; |
---|
154 | if (mergecnt == 1) { |
---|
155 | wt = bt; |
---|
156 | if (smpnum > 0) tc_nadd( &wt, -smpnum*dt, &wt, &status ); |
---|
157 | if (SySevere(&status)) { |
---|
158 | fprintf( stderr, "%s: error computing times. Abort.\n", |
---|
159 | pa_progname() ); |
---|
160 | exit( 1 ); |
---|
161 | } /*endif*/ |
---|
162 | } /*endif*/ |
---|
163 | |
---|
164 | if (gap_found) { |
---|
165 | /* set time quality if found */ |
---|
166 | if (tq_cnt > 0) { |
---|
167 | av_timequal = Nint( (float)av_timequal/(float)tq_cnt ); |
---|
168 | SeedSetNextTimequal( av_timequal ); |
---|
169 | av_timequal = tq_cnt = 0; |
---|
170 | } /*endif*/ |
---|
171 | hdrproto.activity = activity; |
---|
172 | hdrproto.ioflags = ioflags; |
---|
173 | hdrproto.quality = quality; |
---|
174 | SeedSetHeaderPrototype( &hdrproto ); |
---|
175 | /* flush output, then decode new record */ |
---|
176 | SeedEncodeSteim1( fileno(stdout), cv_smp, &prev_smp, &smpnum, &wt, |
---|
177 | cv_wrk, 2, TRUE, &status ); |
---|
178 | if (SySevere(&status)) { |
---|
179 | fprintf( stderr, "%s: error writing SEED output. Abort.\n", |
---|
180 | pa_progname() ); |
---|
181 | exit( 1 ); |
---|
182 | } /*endif*/ |
---|
183 | activity = ioflags = quality = (UBYTE)0; |
---|
184 | /* decode record */ |
---|
185 | SeedDecodeSteim1( cv_seedrec, swap, MAXSAMPLE, cv_smp, &smpnum ); |
---|
186 | mergecnt = 1; |
---|
187 | wt = bt; |
---|
188 | prev_smp = 0; |
---|
189 | /* get time quality */ |
---|
190 | cur_timequal = SeedGetTimequal( (SeedDataHeaderT *)cv_seedrec ); |
---|
191 | if (cur_timequal >= 0) { |
---|
192 | av_timequal += cur_timequal; |
---|
193 | tq_cnt++; |
---|
194 | } /*endif*/ |
---|
195 | } else { |
---|
196 | /* decode new record, if 8 input records -> write output record */ |
---|
197 | SeedDecodeSteim1( cv_seedrec, swap, MAXSAMPLE-smpnum, |
---|
198 | cv_smp+smpnum, &new_smpnum ); |
---|
199 | smpnum += new_smpnum; |
---|
200 | /* get time quality */ |
---|
201 | cur_timequal = SeedGetTimequal( (SeedDataHeaderT *)cv_seedrec ); |
---|
202 | if (cur_timequal >= 0) { |
---|
203 | av_timequal += cur_timequal; |
---|
204 | tq_cnt++; |
---|
205 | } /*endif*/ |
---|
206 | if (mergecnt == MERGEREC || smpnum >= MAXSMPPERREC) { |
---|
207 | /* set time quality if found */ |
---|
208 | if (tq_cnt > 0) { |
---|
209 | av_timequal = Nint( (float)av_timequal/(float)tq_cnt ); |
---|
210 | SeedSetNextTimequal( av_timequal ); |
---|
211 | av_timequal = tq_cnt = 0; |
---|
212 | } /*endif*/ |
---|
213 | hdrproto.activity = activity; |
---|
214 | hdrproto.ioflags = ioflags; |
---|
215 | hdrproto.quality = quality; |
---|
216 | hdrproto.timecorr = ((SeedDataHeaderT *)cv_seedrec)->timecorr; |
---|
217 | SeedSetHeaderPrototype( &hdrproto ); |
---|
218 | flushbuf = (smpnum <= MAXSMPPERREC); |
---|
219 | /* write output record */ |
---|
220 | SeedEncodeSteim1( fileno(stdout), cv_smp, &prev_smp, &smpnum, &wt, |
---|
221 | cv_wrk, 2, flushbuf, &status ); |
---|
222 | if (SySevere(&status)) { |
---|
223 | fprintf( stderr, "%s: error writing SEED output. Abort.\n", |
---|
224 | pa_progname() ); |
---|
225 | exit( 1 ); |
---|
226 | } /*endif*/ |
---|
227 | /* reset merge and sample counter */ |
---|
228 | mergecnt = 0; |
---|
229 | if (flushbuf) smpnum = 0; |
---|
230 | activity = ioflags = quality = (UBYTE)0; |
---|
231 | } /*endif*/ |
---|
232 | } /*endif*/ |
---|
233 | |
---|
234 | } /*endfor*/ |
---|
235 | |
---|
236 | /* flush output */ |
---|
237 | if (smpnum > 0) { |
---|
238 | /* set time quality if found */ |
---|
239 | if (tq_cnt > 0) { |
---|
240 | av_timequal = Nint( (float)av_timequal/(float)tq_cnt ); |
---|
241 | SeedSetNextTimequal( av_timequal ); |
---|
242 | av_timequal = tq_cnt = 0; |
---|
243 | } /*endif*/ |
---|
244 | SeedEncodeSteim1( fileno(stdout), cv_smp, &prev_smp, &smpnum, &wt, |
---|
245 | cv_wrk, 2, TRUE, &status ); |
---|
246 | if (SySevere(&status)) { |
---|
247 | fprintf( stderr, "%s: error writing SEED output. Abort.\n", |
---|
248 | pa_progname() ); |
---|
249 | exit( 1 ); |
---|
250 | } /*endif*/ |
---|
251 | } /*endif*/ |
---|
252 | |
---|
253 | fclose( fp ); |
---|
254 | return 0; |
---|
255 | |
---|
256 | } /* end of main */ |
---|