1 | |
---|
2 | /* file seedquickdump.c |
---|
3 | * =============== |
---|
4 | * |
---|
5 | * $Revision: 32 $, $Date: 2008-03-30 19:54:58 +0200 (So, 30 MÀr 2008) $ |
---|
6 | * |
---|
7 | * Dumps out Headerinfo of SEED files. |
---|
8 | * K. Stammler, 10-Aug-98 |
---|
9 | * |
---|
10 | */ |
---|
11 | |
---|
12 | |
---|
13 | #include <stdio.h> |
---|
14 | #include <string.h> |
---|
15 | #include BASECNST |
---|
16 | #include BC_SYSBASE |
---|
17 | #include BC_CPAR |
---|
18 | #include BC_TCUSRDEF |
---|
19 | #include BC_UTUSRDEF |
---|
20 | #include BC_ERUSRDEF |
---|
21 | #include "seedcfg.h" |
---|
22 | #include "seed_lib.h" |
---|
23 | |
---|
24 | |
---|
25 | typedef struct { |
---|
26 | WORD block_id; /* blockette ID = 1000 */ |
---|
27 | WORD next_block; /* next blockette */ |
---|
28 | UBYTE algorithm; /* compression algorithm */ |
---|
29 | UBYTE dunno1; /* ... */ |
---|
30 | UBYTE reclthpow; /* power of record length */ |
---|
31 | UBYTE dunno2; /* ... */ |
---|
32 | } SeedDataOnlyBlocketteT; |
---|
33 | |
---|
34 | typedef struct { |
---|
35 | WORD block_id; /* blockette ID = 1001 */ |
---|
36 | WORD next_block; /* next blockette */ |
---|
37 | UBYTE timequal; /* time quality */ |
---|
38 | UBYTE dunno1; /* ...*/ |
---|
39 | } SeedDataExtBlocketteT; |
---|
40 | |
---|
41 | |
---|
42 | |
---|
43 | int main( int argc, char *argv[] ) |
---|
44 | { |
---|
45 | /* local variables */ |
---|
46 | STATUS status; /* return status */ |
---|
47 | int recsize; /* SEED record size in bytes */ |
---|
48 | char recsize_par[cBcLineLth]; /* record size parameter string */ |
---|
49 | SeedSbyteT *seedrec; /* pointer to SEED record */ |
---|
50 | SeedDataHeaderT *seedhdr; /* pointer to seed data header */ |
---|
51 | char seedfile[BC_FILELTH+1]; /* name of SEED file */ |
---|
52 | int read_ret; /* fread return values */ |
---|
53 | NTIME ntime; /* numeric time */ |
---|
54 | FILE *fp; /* pointer to SEED file */ |
---|
55 | char sfd_t_start[BC_TIMELTH+1]; /* start time */ |
---|
56 | char sfd_t_end[BC_TIMELTH+1]; /* end time */ |
---|
57 | int sfd_recno; /* number of records in file */ |
---|
58 | char sfd_stream[BC_LINELTH+1]; /* stream string */ |
---|
59 | BOOLEAN sfd_swap_hdr; /* swap header necessary */ |
---|
60 | int i; /* counter */ |
---|
61 | long fsize; /* file size */ |
---|
62 | float dt; /* sample distance in sec */ |
---|
63 | char *csrc, *cdst; /* moving pointers */ |
---|
64 | char outfile[BC_FILELTH+1]; /* output file */ |
---|
65 | FILE *out; /* pointer to output file */ |
---|
66 | char chanstr[BC_SHORTSTRLTH+1]; /* scratch string for channel */ |
---|
67 | int byteoff; /* byte offset at beginning */ |
---|
68 | int errcnt; /* error counter */ |
---|
69 | int reccnt; /* record counter */ |
---|
70 | char seq[cBcLineLth+1]; /* sequence number string */ |
---|
71 | SeedDataOnlyBlocketteT *donly; /* data only blockette */ |
---|
72 | SeedDataExtBlocketteT *dext; /* data extension blockette */ |
---|
73 | UWORD tmp; /* scratch */ |
---|
74 | int time_quality; /* time quality factor */ |
---|
75 | char lasttime[cBcTimeLth+1]; /* time of last record */ |
---|
76 | float gaplth; /* length of gap in s */ |
---|
77 | unsigned long int usign; /* first sample values */ |
---|
78 | BOOLEAN print_locid; /* print location ID */ |
---|
79 | char showinfo[cBcLineLth+1]; /* show info qualifier */ |
---|
80 | BOOLEAN b1000found; /* blockette 1000 found */ |
---|
81 | int gap_count=0; /* gap counter */ |
---|
82 | float gap_length=0.0; /* total gap length in s */ |
---|
83 | int jitter_count=0; /* counts very small 'gaps' (<dt/2) */ |
---|
84 | BOOLEAN write_newline; /* write a new line after record */ |
---|
85 | |
---|
86 | /* executable code */ |
---|
87 | |
---|
88 | status = cBcNoError; |
---|
89 | print_locid = FALSE; |
---|
90 | |
---|
91 | pa_init( argc, argv ); |
---|
92 | if (pa_pnumber() < 1 || pa_pnumber() > 2) { |
---|
93 | fprintf( stderr, "Usage: %s <seed-file> \n", |
---|
94 | pa_progname() ); |
---|
95 | return 1; |
---|
96 | } /*endif*/ |
---|
97 | strcpy( seedfile, pa_pvalue(1) ); |
---|
98 | out = stdout; |
---|
99 | |
---|
100 | /* check for record size qualifier */ |
---|
101 | recsize = 0; |
---|
102 | if (pa_qspecified("-seedrec")) { |
---|
103 | strcpy( recsize_par, pa_qvalue("-seedrec") ); |
---|
104 | if (strcmp(recsize_par,"quickfind") == 0) { |
---|
105 | /* just do nothing, leave recsize zero */ |
---|
106 | } else { |
---|
107 | if (sscanf(recsize_par,"%d",&recsize) != 1) { |
---|
108 | fprintf( stderr, "%s: illegal seedrec qualifier. Abort.\n", |
---|
109 | pa_progname() ); |
---|
110 | return 1; |
---|
111 | } /*endif*/ |
---|
112 | } /*endif*/ |
---|
113 | } /*endif*/ |
---|
114 | |
---|
115 | if (pa_qspecified("-locid")) print_locid = TRUE; |
---|
116 | |
---|
117 | *showinfo = '\0'; |
---|
118 | if (pa_qvalue("-show") != NULL) { |
---|
119 | strcpy( showinfo, pa_qvalue("-show") ); |
---|
120 | } /*endif*/ |
---|
121 | |
---|
122 | /* allocate memory for SEED record */ |
---|
123 | seedrec = (SeedSbyteT *)sy_allocmem( 1, Seed_C_MAX_RECLTH, &status ); |
---|
124 | if (Severe(&status)) err_writemsg( status, "", TRUE ); |
---|
125 | |
---|
126 | fp = fopen( seedfile, "rb" ); |
---|
127 | if (fp == NULL) { |
---|
128 | fprintf( stderr, "*** %s: file %s not found\n", pa_progname(), seedfile ); |
---|
129 | fclose( out ); |
---|
130 | return 1; |
---|
131 | } /*endif*/ |
---|
132 | |
---|
133 | /* determine record size if not specified */ |
---|
134 | if (recsize == 0) { |
---|
135 | SeedQuickFindReclth( fp, &recsize, &byteoff ); |
---|
136 | if (recsize <= 0) { |
---|
137 | fclose( fp ); |
---|
138 | if (recsize == 0) { |
---|
139 | fprintf( stderr, "%s: no valid SEED file %s\n", |
---|
140 | pa_progname(), seedfile ); |
---|
141 | } else { |
---|
142 | fprintf( stderr, "%s: cannot find record size in %s\n", |
---|
143 | pa_progname(), seedfile ); |
---|
144 | } /*endif*/ |
---|
145 | return 1; |
---|
146 | } /*endif*/ |
---|
147 | } /*endif*/ |
---|
148 | |
---|
149 | reccnt = 0; |
---|
150 | gaplth = 0.0; |
---|
151 | *lasttime = '\0'; |
---|
152 | |
---|
153 | /* read records */ |
---|
154 | FOREVER { |
---|
155 | |
---|
156 | reccnt++; |
---|
157 | |
---|
158 | /* read next record */ |
---|
159 | read_ret = (int)fread( (char *)seedrec, recsize, 1, fp ); |
---|
160 | if (read_ret != 1) break; |
---|
161 | |
---|
162 | /* check for swapping */ |
---|
163 | seedhdr = (SeedDataHeaderT *)seedrec; |
---|
164 | sfd_swap_hdr = SeedSwapNecessary( seedhdr ); |
---|
165 | if (sfd_swap_hdr) SeedSwapHeader( seedhdr ); |
---|
166 | |
---|
167 | strncpy( seq, seedhdr->seqno, 6 ); |
---|
168 | seq[6] = '\0'; |
---|
169 | if (seedhdr->indicator != 'D' && seedhdr->indicator != 'R' |
---|
170 | && seedhdr->indicator != 'Q') { |
---|
171 | if (*showinfo == '\0') |
---|
172 | printf( "%06d %6s %c\n", reccnt, seq, seedhdr->indicator ); |
---|
173 | continue; |
---|
174 | } /*endif*/ |
---|
175 | |
---|
176 | /* find start time */ |
---|
177 | SeedBtimeToNtime( &(seedhdr->starttime), &ntime, &status ); |
---|
178 | if (Severe(&status)) { |
---|
179 | fprintf( stderr, "%s: couldn't read start time in file %s\n", |
---|
180 | pa_progname(), seedfile ); |
---|
181 | fclose( fp ); |
---|
182 | fclose( out ); |
---|
183 | return 1; |
---|
184 | } /*endif*/ |
---|
185 | tc_n2t( &ntime, sfd_t_start, &status ); |
---|
186 | if (Severe(&status)) { |
---|
187 | fprintf( stderr, "%s: couldn't convert start time in file %s\n", |
---|
188 | pa_progname(), seedfile ); |
---|
189 | fclose( fp ); |
---|
190 | fclose( out ); |
---|
191 | return 1; |
---|
192 | } /*endif*/ |
---|
193 | |
---|
194 | /* channel name */ |
---|
195 | csrc = seedhdr->statcode; |
---|
196 | cdst = sfd_stream; |
---|
197 | *cdst = '\0'; |
---|
198 | while (*csrc > ' ' && *csrc <= 'z') *cdst++ = *csrc++; |
---|
199 | *cdst++ = '-'; |
---|
200 | *cdst = '\0'; |
---|
201 | chanstr[0] = Cap( seedhdr->channel[0] ); |
---|
202 | chanstr[1] = Cap( seedhdr->channel[1] ); |
---|
203 | chanstr[2] = '\0'; |
---|
204 | if (chanstr[0] <= ' ' || chanstr[0] > 'z') chanstr[0] = '\0'; |
---|
205 | if (chanstr[1] <= ' ' || chanstr[1] > 'z') chanstr[1] = '\0'; |
---|
206 | ut_uncap( chanstr ); |
---|
207 | |
---|
208 | strcat( sfd_stream, chanstr ); |
---|
209 | strcat( sfd_stream, "-" ); |
---|
210 | i = (int)strlen( sfd_stream ); |
---|
211 | sfd_stream[i] = seedhdr->channel[2]; |
---|
212 | if (chanstr[i] <= ' ' || chanstr[i] > 'z') chanstr[i] = '\0'; |
---|
213 | sfd_stream[i+1] = '\0'; |
---|
214 | ut_uncap( sfd_stream ); |
---|
215 | |
---|
216 | /* get sample distance */ |
---|
217 | if (seedhdr->smprate_fact == 0 || seedhdr->smprate_mult == 0) { |
---|
218 | dt = 0.0; |
---|
219 | } else { |
---|
220 | dt = SeedGetSampleDist( seedhdr ); |
---|
221 | } /*endif*/ |
---|
222 | |
---|
223 | tc_tadd( sfd_t_start, (float)(seedhdr->no_of_samples)*dt, sfd_t_end, |
---|
224 | &status ); |
---|
225 | if (Severe(&status)) { |
---|
226 | fprintf( stderr, "%s: couldn't compute end time in file\n", |
---|
227 | pa_progname(), seedfile ); |
---|
228 | fclose( fp ); |
---|
229 | fclose( out ); |
---|
230 | return 1; |
---|
231 | } /*endif*/ |
---|
232 | if (*lasttime != '\0') |
---|
233 | gaplth = tc_tdiff( sfd_t_start, lasttime, &status ); |
---|
234 | |
---|
235 | b1000found = FALSE; |
---|
236 | time_quality = -1; |
---|
237 | if (seedhdr->first != 0) { |
---|
238 | donly = (SeedDataOnlyBlocketteT *)(seedrec+(seedhdr->first)); |
---|
239 | #ifdef XXX |
---|
240 | /* this is done in SeedSwapHeader now */ |
---|
241 | if (sfd_swap_hdr) { |
---|
242 | tmp = (UWORD)(donly->block_id); |
---|
243 | donly->block_id = (tmp & 0xff) * 0x100; |
---|
244 | donly->block_id += (UWORD)(tmp & 0xff00) / (UWORD)0x100; |
---|
245 | tmp = (UWORD)(donly->next_block); |
---|
246 | donly->next_block = (tmp & 0xff) * 0x100; |
---|
247 | donly->next_block += (UWORD)(tmp & 0xff00) / (UWORD)0x100; |
---|
248 | } /*endif*/ |
---|
249 | #endif |
---|
250 | b1000found = (donly->block_id == 1000); |
---|
251 | if (b1000found && donly->next_block > 0) { |
---|
252 | dext = (SeedDataExtBlocketteT *)(seedrec+(donly->next_block)); |
---|
253 | #ifdef XXX |
---|
254 | /* this is done in SeedSwapHeader now */ |
---|
255 | if (sfd_swap_hdr) { |
---|
256 | tmp = (UWORD)(dext->block_id); |
---|
257 | dext->block_id = (tmp & 0xff) * 0x100; |
---|
258 | dext->block_id += (UWORD)(tmp & 0xff00) / (UWORD)0x100; |
---|
259 | } /*endif*/ |
---|
260 | #endif |
---|
261 | if (dext->block_id == 1001) { |
---|
262 | time_quality = dext->timequal; |
---|
263 | } /*endif*/ |
---|
264 | if (!b1000found && dext->block_id == 1000) b1000found = TRUE; |
---|
265 | } /*endif*/ |
---|
266 | } /*endif*/ |
---|
267 | |
---|
268 | usign = *((unsigned long int *)(seedrec+68)); |
---|
269 | /* |
---|
270 | tmp = usign & 0x0000ffff; |
---|
271 | usign >>= 16; |
---|
272 | usign += (unsigned long)tmp * 65536; |
---|
273 | */ |
---|
274 | |
---|
275 | write_newline = TRUE; |
---|
276 | if (*showinfo == '\0') { |
---|
277 | printf( "%06d %6s %c %s %s %5d %7.5f %d ", reccnt, seq, seedhdr->indicator, |
---|
278 | sfd_stream, sfd_t_start, seedhdr->no_of_samples, dt, time_quality ); |
---|
279 | |
---|
280 | if (gaplth == 0.0) { |
---|
281 | printf( "cont" ); |
---|
282 | } else { |
---|
283 | printf( "%g", gaplth ); |
---|
284 | if (Abs(gaplth) > (dt/2.0)) { |
---|
285 | gap_length += Abs(gaplth); |
---|
286 | gap_count++; |
---|
287 | } else { |
---|
288 | jitter_count++; |
---|
289 | } /*endif*/ |
---|
290 | } /*endif*/ |
---|
291 | |
---|
292 | if (seedhdr->timecorr != 0) printf( " (%d)", seedhdr->timecorr ); |
---|
293 | |
---|
294 | if (print_locid) printf( " %c%c", seedhdr->locid[0], seedhdr->locid[1] ); |
---|
295 | |
---|
296 | } else { |
---|
297 | |
---|
298 | if (gaplth != 0.0) { |
---|
299 | if (Abs(gaplth) > (dt/2.0)) { |
---|
300 | gap_length += Abs(gaplth); |
---|
301 | gap_count++; |
---|
302 | } else { |
---|
303 | jitter_count++; |
---|
304 | } /*endif*/ |
---|
305 | } /*endif*/ |
---|
306 | |
---|
307 | if (strcmp(showinfo,"netcode") == 0) { |
---|
308 | printf( "%c%c ", seedhdr->network[0], seedhdr->network[1] ); |
---|
309 | } else if (strcmp(showinfo,"timecorr") == 0) { |
---|
310 | printf( "%d ", seedhdr->timecorr ); |
---|
311 | } else if (strcmp(showinfo,"locid") == 0) { |
---|
312 | printf( "%c%c ", seedhdr->locid[0], seedhdr->locid[1] ); |
---|
313 | } else if (strcmp(showinfo,"activity") == 0) { |
---|
314 | printf( "0x%x ", (int)(seedhdr->activity) ); |
---|
315 | } else if (strcmp(showinfo,"b1000") == 0) { |
---|
316 | printf( "%d ", b1000found ); |
---|
317 | } else { |
---|
318 | write_newline = FALSE; |
---|
319 | } /*endif*/ |
---|
320 | |
---|
321 | } /*endif*/ |
---|
322 | |
---|
323 | if (write_newline) printf( "\n" ); |
---|
324 | |
---|
325 | strcpy( lasttime, sfd_t_end ); |
---|
326 | |
---|
327 | } /*endfor*/ |
---|
328 | |
---|
329 | if (strcmp(showinfo,"gaps") == 0) { |
---|
330 | printf( "gap_count %d gap_length %f jitter_count %d\n", |
---|
331 | gap_count, gap_length, jitter_count ); |
---|
332 | } /*endif*/ |
---|
333 | |
---|
334 | fclose( fp ); |
---|
335 | return 0; |
---|
336 | |
---|
337 | #ifdef XXX |
---|
338 | |
---|
339 | |
---|
340 | /* get length of file */ |
---|
341 | fseek( fp, 0, 2 ); |
---|
342 | fsize = ftell( fp ) - byteoff; |
---|
343 | if (fsize % recsize != 0) { |
---|
344 | fprintf( stderr, "*** %s: illegal size %ld bytes of SEED file %s\n", |
---|
345 | pa_progname(), fsize, seedfile ); |
---|
346 | fclose( fp ); |
---|
347 | fclose( out ); |
---|
348 | return 1 ; |
---|
349 | } /*endif*/ |
---|
350 | sfd_recno = (int)(fsize / (long)recsize); |
---|
351 | |
---|
352 | /* get last record */ |
---|
353 | fseek( fp, |
---|
354 | (long)(sfd_recno-1)*(long)recsize + (long)byteoff, 0 ); |
---|
355 | read_ret = (int)fread( (char *)seedrec, recsize, 1, fp ); |
---|
356 | if (read_ret != 1) { |
---|
357 | fprintf( stderr, "*** %s: read error on file %s (end)\n", |
---|
358 | pa_progname(), seedfile ); |
---|
359 | fclose( fp ); |
---|
360 | fclose( out ); |
---|
361 | return 1; |
---|
362 | } /*endif*/ |
---|
363 | seedhdr = (SeedDataHeaderT *)seedrec; |
---|
364 | if (SeedSwapNecessary(seedhdr)) SeedSwapHeader(seedhdr); |
---|
365 | errcnt = 0; |
---|
366 | SeedBtimeToNtime( &(seedhdr->starttime), &ntime, &status ); |
---|
367 | while (Severe(&status)) { |
---|
368 | errcnt++; |
---|
369 | fprintf( stderr, |
---|
370 | "%s: unreadable last record, take %d before in file %s\n", |
---|
371 | pa_progname(), errcnt, seedfile ); |
---|
372 | status = BC_NOERROR; |
---|
373 | /* get previous record */ |
---|
374 | sfd_recno--; |
---|
375 | fseek( fp, |
---|
376 | (long)(sfd_recno-1)*(long)recsize + (long)byteoff, |
---|
377 | 0 ); |
---|
378 | read_ret = (int)fread( (char *)seedrec, recsize, 1, fp); |
---|
379 | if (read_ret != 1) { |
---|
380 | fprintf( stderr, "%s: read error on file %s (end)\n", |
---|
381 | pa_progname(), seedfile ); |
---|
382 | fclose( fp ); |
---|
383 | fclose( out ); |
---|
384 | return 1; |
---|
385 | } /*endif*/ |
---|
386 | seedhdr = (SeedDataHeaderT *)seedrec; |
---|
387 | if (SeedSwapNecessary(seedhdr)) SeedSwapHeader(seedhdr); |
---|
388 | SeedBtimeToNtime( &(seedhdr->starttime), &ntime, &status ); |
---|
389 | if (Severe(&status) && errcnt > 10) { |
---|
390 | fprintf( stderr, |
---|
391 | "%s: couldn't read start time of last record in file %s\n", |
---|
392 | pa_progname(), seedfile ); |
---|
393 | fclose( fp ); |
---|
394 | fclose( out ); |
---|
395 | return 1; |
---|
396 | } /*endif*/ |
---|
397 | } /*endwhile*/ |
---|
398 | tc_nadd( &ntime, (float)(seedhdr->no_of_samples)*dt, &ntime, &status ); |
---|
399 | if (Severe(&status)) { |
---|
400 | fprintf( stderr, "%s: couldn't compute end time in file\n", |
---|
401 | pa_progname(), seedfile ); |
---|
402 | fclose( fp ); |
---|
403 | fclose( out ); |
---|
404 | return 1; |
---|
405 | } /*endif*/ |
---|
406 | tc_n2t( &ntime, sfd_t_end, &status ); |
---|
407 | if (Severe(&status)) { |
---|
408 | fprintf( stderr, "%s: couldn't convert end time in file %s\n", |
---|
409 | pa_progname(), seedfile ); |
---|
410 | fclose( fp ); |
---|
411 | fclose( out ); |
---|
412 | return 1; |
---|
413 | } /*endif*/ |
---|
414 | |
---|
415 | fclose( fp ); |
---|
416 | |
---|
417 | fprintf( out, "%c>%s %c>%s %c>%s %c>%s %c>%d %c>%d %c>%d %c>%d\n", |
---|
418 | Seed_C_SfdStream, sfd_stream, Seed_C_SfdName, seedfile, |
---|
419 | Seed_C_SfdTStart, sfd_t_start, Seed_C_SfdTEnd, sfd_t_end, |
---|
420 | Seed_C_SfdRecno, sfd_recno, Seed_C_SfdSwapH, sfd_swap_hdr, |
---|
421 | Seed_C_SfdReclth, recsize, Seed_C_SfdOffset, byteoff ); |
---|
422 | |
---|
423 | if (out != stdout) fclose( out ); |
---|
424 | |
---|
425 | return 0; |
---|
426 | |
---|
427 | #endif |
---|
428 | |
---|
429 | } /* end of main */ |
---|