source: SH_SHM/trunk/source/seed_io/sfdline.c @ 303

Revision 303, 10.1 KB checked in by marcus, 13 years ago (diff)

r145 | walther | 2011-01-11 23:18:02 +0100 (Di, 11 Jan 2011) | 12 lines

  • Extended sfdline for output of additional information about network and location code (on by default). Setting the new option "-nonetloc" will reveal the old behaviour. This change should be transparent for any Seismic Handler installations, since this extra information is ignored by default. To access a code using network and location code, simply change for station MOX (network GR and empty location code):

reads . 10-APR-2010_00:00:19 5 mox z bh

to

reads . 10-APR-2010_00:00:19 5 mox.gr.. z bh

Please note: SHM doesn't yet support these schema.

  • Added python version of sfdline (only subset of options supported).
Line 
1
2/* file sfdline.c
3 *      =========
4 *
5 * version 15, 17-Jun-2005
6 *
7 * returns sfd line for specified SEED file.  It is assumed that the SEED
8 * file contains a single channel (read from the first record) and has
9 * SEED records of monotonically increasing times.
10 * K. Stammler, 8-Apr-94
11 *
12 * v5, 23-Nov-94, K. Stammler, included swap option
13 */
14
15
16#include <stdio.h>
17#include <string.h>
18#include BASECNST
19#include BC_SYSBASE
20#include BC_CPAR
21#include BC_TCUSRDEF
22#include BC_UTUSRDEF
23#include BC_ERUSRDEF
24#include "seedcfg.h"
25#include "seed_lib.h"
26
27
28#define TIMECORRUNIT 1000.0
29
30
31int main( int argc, char *argv[] )
32{
33        /* local variables */
34        int      recsize;                   /* SEED record size in bytes */
35        char     recsize_par[cBcLineLth];   /* record size parameter string */
36        STATUS   status;                    /* return status */
37        SeedSbyteT *seedrec;                /* pointer to SEED record */
38        SeedDataHeaderT *seedhdr;           /* pointer to seed data header */
39        char     seedfile[BC_FILELTH+1];    /* name of SEED file */
40        int      read_ret;                  /* fread return values */
41        NTIME    ntime;                     /* numeric time */
42        FILE     *fp;                       /* pointer to SEED file */
43        char     sfd_t_start[BC_TIMELTH+1]; /* start time */
44        char     sfd_t_end[BC_TIMELTH+1];   /* end time */
45        int      sfd_recno;                 /* number of records in file */
46        char     sfd_stream[BC_LINELTH+1];  /* stream string */
47        char     sfd_netloc[5];             /* network and location code string */
48        BOOLEAN  sfd_swap_hdr;              /* swap header necessary */
49        int      i;                         /* counter */
50        long     fsize;                     /* file size */
51        float    dt;                        /* sample distance in sec */
52        char     *csrc, *cdst;              /* moving pointers */
53        char     outfile[BC_FILELTH+1];     /* output file */
54        FILE     *out;                      /* pointer to output file */
55        char     chanstr[BC_SHORTSTRLTH+1]; /* scratch string for channel */
56        int      byteoff;                   /* byte offset at beginning */
57        int      errcnt;                    /* error counter */
58        TSyBoolean include_bad;             /* include bad data files */
59        TSyBoolean use_timecorr;            /* use time correction */
60        TSyBoolean netloc;                  /* display network/location code */
61
62        /* executable code */
63
64        status = BC_NOERROR;
65        recsize = 0;  /* must be set or detected later, otherwise abort */
66
67        pa_init( argc, argv );
68        if  (pa_pnumber() < 1 || pa_pnumber() > 2)  {
69                fprintf( stderr, "Usage: %s <seed-file> [<outfile>]***\n",
70                        pa_progname() );
71                return 1;
72        } /*endif*/
73        strcpy( seedfile, pa_pvalue(1) );
74        if  (pa_pnumber() == 2)  {
75                strcpy( outfile, pa_pvalue(2) );
76                out = fopen( outfile, "a" );
77                if  (out == NULL)  {
78                        fprintf( stderr, "%s: output file %s couldn't be opened ***\n",
79                                pa_progname(), outfile );
80                        return 1;
81                } /*endif*/
82        } else {
83                out = stdout;
84        } /*endif*/
85
86        /* check for record size qualifier */
87        if  (pa_qspecified("-seedrec"))  {
88                strcpy( recsize_par, pa_qvalue("-seedrec") );
89                if  (strcmp(recsize_par,"quickfind") == 0)  {
90                        /* just do nothing, leave recsize zero */
91                } else {
92                        if  (sscanf(recsize_par,"%d",&recsize) != 1)  {
93                                fprintf( stderr, "%s: illegal seedrec qualifier.  Abort.\n",
94                                        pa_progname() );
95                                return 1;
96                        } /*endif*/
97                } /*endif*/
98        } /*endif*/
99
100        /* check for offset specified */
101        byteoff = 0;
102        if  (pa_qspecified("-byteoff"))
103                sscanf( pa_qvalue("-byteoff"), "%d", &byteoff );
104
105        include_bad = FALSE;
106        if  (pa_qspecified("-include_bad"))
107                include_bad = TRUE;
108
109        use_timecorr = FALSE;
110        if  (pa_qspecified("-timecorr"))
111                use_timecorr = TRUE;
112
113        /* output network and location code */
114        netloc = TRUE;
115        if (pa_qspecified("-nonetloc"))
116                netloc = FALSE;
117
118        /* open seed file */
119        fp = fopen( seedfile, "rb" );
120        if  (fp == NULL)  {
121                fprintf( stderr, "*** %s: file %s not found\n", pa_progname(), seedfile );
122                fclose( out );
123                return 1;
124        } /*endif*/
125
126        /* determine record size if not specified */
127        if  (recsize == 0)  {
128                SeedQuickFindReclth( fp, &recsize, &byteoff );
129                if  (recsize <= 0)  {
130                        fclose( fp );
131                        if  (recsize == 0)  {
132                                fprintf( stderr, "%s: no valid SEED file %s\n",
133                                        pa_progname(), seedfile );
134                        } else {
135                                fprintf( stderr, "%s: cannot find record size in %s\n",
136                                        pa_progname(), seedfile );
137                        } /*endif*/
138                        return 1;
139                } /*endif*/
140        } /*endif*/
141
142        /* allocate memory for SEED record */
143        seedrec = (SeedSbyteT *)sy_allocmem( 1, recsize, &status );
144        if  (Severe(&status))  err_writemsg( status, "", TRUE );
145
146        /* skip offset */
147        if  (byteoff > 0)
148                fseek( fp, byteoff, 0 );
149
150        /* read first record */
151        read_ret = (int)fread( (char *)seedrec, recsize, 1, fp );
152        if  (read_ret != 1)  {
153                fprintf( stderr, "*** %s: read error on file %s\n",
154                        pa_progname(), seedfile );
155                fclose( fp );
156                fclose( out );
157                return 1;
158        } /*endif*/
159        seedhdr = (SeedDataHeaderT *)seedrec;
160        sfd_swap_hdr = SeedSwapNecessary( seedhdr );
161        if  (sfd_swap_hdr)  SeedSwapHeader( seedhdr );
162
163        /* find start time */
164        SeedBtimeToNtime( &(seedhdr->starttime), &ntime, &status );
165        if  (Severe(&status))  {
166                fprintf( stderr, "%s: couldn't read start time in file %s\n",
167                        pa_progname(), seedfile );
168                fclose( fp );
169                fclose( out );
170                return 1;
171        } /*endif*/
172        /* check for time correction, K.S. 16-Mar-99 */
173        if  (use_timecorr && (seedhdr->timecorr != 0)
174                && (((int)(seedhdr->activity) & Seed_F_ACT_TIMECORRAPP) == 0))  {
175                tc_nadd( &ntime, (float)(seedhdr->timecorr)/TIMECORRUNIT,
176                        &ntime, &status );
177                if  (Severe(&status))  {
178                        fprintf( stderr, "%s: couldn't apply time correction in file %s\n",
179                                pa_progname(), seedfile );
180                        fclose( fp );
181                        fclose( out );
182                        return 1;
183                } /*endif*/
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        i = 0;
199        /* exit loop after 5 chars maximum, added 17-Jun-2005, K.S. */
200        while  (*csrc > ' ' && *csrc <= 'z' && i < 5)  {
201                *cdst++ = *csrc++;
202                i++;
203        } /*endwhile*/
204        *cdst++ = '-';
205        *cdst = '\0';
206        chanstr[0] = Cap( seedhdr->channel[0] );
207        chanstr[1] = Cap( seedhdr->channel[1] );
208        chanstr[2] = '\0';
209        if  (chanstr[0] <= ' ' || chanstr[0] > 'z')  chanstr[0] = '\0';
210        if  (chanstr[1] <= ' ' || chanstr[1] > 'z')  chanstr[1] = '\0';
211        ut_uncap( chanstr );
212
213        strcat( sfd_stream, chanstr );
214        strcat( sfd_stream, "-" );
215        i = (int)strlen( sfd_stream );
216        sfd_stream[i] = seedhdr->channel[2];
217        if  (sfd_stream[i] <= ' ' || sfd_stream[i] > 'z')  sfd_stream[i] = '\0';
218        if  (sfd_stream[i] == '?' || sfd_stream[i] == '*')  sfd_stream[i] = 'x';
219        sfd_stream[i+1] = '\0';
220        ut_uncap( sfd_stream );
221
222        /* network and location code */
223        sfd_netloc[0] = Cap(seedhdr->network[0]);
224        sfd_netloc[1] = Cap(seedhdr->network[1]);
225        sfd_netloc[2] = Cap(seedhdr->locid[0]);
226        sfd_netloc[3] = Cap(seedhdr->locid[1]);
227        sfd_netloc[4] = '\0';
228
229        /* blanks are masked by dots */
230        for (i=0; i<4; i++)
231            if (sfd_netloc[i] == ' ')
232                sfd_netloc[i] = '.';
233
234        /* get sample distance */
235        if  (!include_bad)  {
236                if  (seedhdr->smprate_fact == 0 || seedhdr->smprate_mult == 0)  {
237                        fprintf( stderr, "%s: ignore bad data file %s\n", argv[0], seedfile );
238                        fclose( fp );
239                        return 1;
240                } /*endif*/
241        } /*endif*/
242        dt = SeedGetSampleDist( seedhdr );
243
244        /* get length of file */
245        fseek( fp, 0, 2 );
246        fsize = ftell( fp ) - byteoff;
247        if  (fsize % recsize != 0)  {
248                fprintf( stderr, "*** %s: illegal size %ld bytes of SEED file %s\n",
249                        pa_progname(), fsize, seedfile );
250                fclose( fp );
251                fclose( out );
252                return 1 ;
253        } /*endif*/
254        sfd_recno = (int)(fsize / (long)recsize);
255
256        /* get last record */
257        fseek( fp,
258                (long)(sfd_recno-1)*(long)recsize + (long)byteoff, 0 );
259        read_ret = (int)fread( (char *)seedrec, recsize, 1, fp );
260        if  (read_ret != 1)  {
261                fprintf( stderr, "*** %s: read error on file %s (end)\n",
262                        pa_progname(), seedfile );
263                fclose( fp );
264                fclose( out );
265                return 1;
266        } /*endif*/
267        seedhdr = (SeedDataHeaderT *)seedrec;
268        if  (SeedSwapNecessary(seedhdr))  SeedSwapHeader(seedhdr);
269        errcnt = 0;
270        SeedBtimeToNtime( &(seedhdr->starttime), &ntime, &status );
271        if  (status == cBcNoError)
272                tc_nadd( &ntime, (float)(seedhdr->no_of_samples)*dt, &ntime, &status );
273        while  (Severe(&status))  {
274                errcnt++;
275                fprintf( stderr,
276                        "%s: unreadable last record, take %d before in file %s\n",
277                        pa_progname(), errcnt, seedfile );
278                status = cBcNoError;
279                /* get previous record */
280                sfd_recno--;
281                fseek( fp,
282                        (long)(sfd_recno-1)*(long)recsize + (long)byteoff,
283                        0 );
284                read_ret = (int)fread( (char *)seedrec, recsize, 1, fp );
285                if  (read_ret != 1)  {
286                        fprintf( stderr, "%s: read error on file %s (end)\n",
287                                pa_progname(), seedfile );
288                        fclose( fp );
289                        fclose( out );
290                        return 1;
291                } /*endif*/
292                seedhdr = (SeedDataHeaderT *)seedrec;
293                if  (SeedSwapNecessary(seedhdr))  SeedSwapHeader(seedhdr);
294                SeedBtimeToNtime( &(seedhdr->starttime), &ntime, &status );
295                if  (status == cBcNoError)
296                        tc_nadd( &ntime, (float)(seedhdr->no_of_samples)*dt, &ntime, &status );
297                if  (Severe(&status) && errcnt > 10)  {
298                        fprintf( stderr,
299                                "%s: couldn't read start time of last record in file %s\n",
300                                pa_progname(), seedfile );
301                        fclose( fp );
302                        fclose( out );
303                        return 1;
304                } /*endif*/
305        } /*endwhile*/
306        tc_n2t( &ntime, sfd_t_end, &status );
307        if  (Severe(&status))  {
308                fprintf( stderr, "%s: couldn't convert end time in file %s\n",
309                        pa_progname(), seedfile );
310                fclose( fp );
311                fclose( out );
312                return 1;
313        } /*endif*/
314
315        fclose( fp );
316
317        fprintf( out, "%c>%s %c>%s %c>%s %c>%s %c>%d %c>%d %c>%d %c>%d",
318                Seed_C_SfdStream, sfd_stream, Seed_C_SfdName, seedfile,
319                Seed_C_SfdTStart, sfd_t_start, Seed_C_SfdTEnd, sfd_t_end,
320                Seed_C_SfdRecno, sfd_recno, Seed_C_SfdSwapH, sfd_swap_hdr,
321                Seed_C_SfdReclth, recsize, Seed_C_SfdOffset, byteoff );
322       
323        if (netloc)
324            fprintf( out, " %c>%s",
325                Seed_C_SfdAddinf, sfd_netloc);
326
327        fprintf(out, "\n");
328
329        if  (out != stdout)  fclose( out );
330
331        return 0;
332
333} /* end of main */
334
335
336
337/*----------------------------------------------------------------------------*/
338
Note: See TracBrowser for help on using the repository browser.