source: SH_SHM/trunk/source/seed_io/seedquickdump.c @ 16

Revision 16, 12.5 KB checked in by marcus, 15 years ago (diff)

r1 | svn | 2007-12-13 11:10:29 +0100 (Do, 13 Dez 2007) | 2 lines

Initial import

Line 
1
2/* file seedquickdump.c
3 *      ===============
4 *
5 * version 9, 9-Feb-2007
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
25typedef 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
34typedef 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
43int 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,"activity") == 0)  {
312                                printf( "0x%x ", (int)(seedhdr->activity) );
313                        } else if  (strcmp(showinfo,"b1000") == 0)  {
314                                printf( "%d ", b1000found );
315                        } else {
316                                write_newline = FALSE;
317                        } /*endif*/
318
319                } /*endif*/
320
321                if  (write_newline)  printf( "\n" );
322
323                strcpy( lasttime, sfd_t_end );
324
325        } /*endfor*/
326
327        if  (strcmp(showinfo,"gaps") == 0)  {
328                printf( "gap_count %d gap_length %f jitter_count %d\n",
329                        gap_count, gap_length, jitter_count );
330        } /*endif*/
331
332        fclose( fp );
333        return 0;
334
335#ifdef XXX
336
337
338        /* get length of file */
339        fseek( fp, 0, 2 );
340        fsize = ftell( fp ) - byteoff;
341        if  (fsize % recsize != 0)  {
342                fprintf( stderr, "*** %s: illegal size %ld bytes of SEED file %s\n",
343                        pa_progname(), fsize, seedfile );
344                fclose( fp );
345                fclose( out );
346                return 1 ;
347        } /*endif*/
348        sfd_recno = (int)(fsize / (long)recsize);
349
350        /* get last record */
351        fseek( fp,
352                (long)(sfd_recno-1)*(long)recsize + (long)byteoff, 0 );
353        read_ret = (int)fread( (char *)seedrec, recsize, 1, fp );
354        if  (read_ret != 1)  {
355                fprintf( stderr, "*** %s: read error on file %s (end)\n",
356                        pa_progname(), seedfile );
357                fclose( fp );
358                fclose( out );
359                return 1;
360        } /*endif*/
361        seedhdr = (SeedDataHeaderT *)seedrec;
362        if  (SeedSwapNecessary(seedhdr))  SeedSwapHeader(seedhdr);
363        errcnt = 0;
364        SeedBtimeToNtime( &(seedhdr->starttime), &ntime, &status );
365        while  (Severe(&status))  {
366                errcnt++;
367                fprintf( stderr,
368                        "%s: unreadable last record, take %d before in file %s\n",
369                        pa_progname(), errcnt, seedfile );
370                status = BC_NOERROR;
371                /* get previous record */
372                sfd_recno--;
373                fseek( fp,
374                        (long)(sfd_recno-1)*(long)recsize + (long)byteoff,
375                        0 );
376                read_ret = (int)fread( (char *)seedrec, recsize, 1, fp);
377                if  (read_ret != 1)  {
378                        fprintf( stderr, "%s: read error on file %s (end)\n",
379                                pa_progname(), seedfile );
380                        fclose( fp );
381                        fclose( out );
382                        return 1;
383                } /*endif*/
384                seedhdr = (SeedDataHeaderT *)seedrec;
385                if  (SeedSwapNecessary(seedhdr))  SeedSwapHeader(seedhdr);
386                SeedBtimeToNtime( &(seedhdr->starttime), &ntime, &status );
387                if  (Severe(&status) && errcnt > 10)  {
388                        fprintf( stderr,
389                                "%s: couldn't read start time of last record in file %s\n",
390                                pa_progname(), seedfile );
391                        fclose( fp );
392                        fclose( out );
393                        return 1;
394                } /*endif*/
395        } /*endwhile*/
396        tc_nadd( &ntime, (float)(seedhdr->no_of_samples)*dt, &ntime, &status );
397        if  (Severe(&status))  {
398                fprintf( stderr, "%s: couldn't compute end time in file\n",
399                        pa_progname(), seedfile );
400                fclose( fp );
401                fclose( out );
402                return 1;
403        } /*endif*/
404        tc_n2t( &ntime, sfd_t_end, &status );
405        if  (Severe(&status))  {
406                fprintf( stderr, "%s: couldn't convert end time in file %s\n",
407                        pa_progname(), seedfile );
408                fclose( fp );
409                fclose( out );
410                return 1;
411        } /*endif*/
412
413        fclose( fp );
414
415        fprintf( out, "%c>%s %c>%s %c>%s %c>%s %c>%d %c>%d %c>%d %c>%d\n",
416                Seed_C_SfdStream, sfd_stream, Seed_C_SfdName, seedfile,
417                Seed_C_SfdTStart, sfd_t_start, Seed_C_SfdTEnd, sfd_t_end,
418                Seed_C_SfdRecno, sfd_recno, Seed_C_SfdSwapH, sfd_swap_hdr,
419                Seed_C_SfdReclth, recsize, Seed_C_SfdOffset, byteoff );
420
421        if  (out != stdout)  fclose( out );
422
423        return 0;
424
425#endif
426
427} /* end of main */
Note: See TracBrowser for help on using the repository browser.