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

Revision 16, 9.6 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 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
31
32int main( int argc, char *argv[] )
33{
34        /* local variables */
35        int      recsize;                   /* SEED record size in bytes */
36        char     recsize_par[cBcLineLth];   /* record size parameter string */
37        STATUS   status;                    /* return status */
38        SeedSbyteT *seedrec;                /* pointer to SEED record */
39        SeedDataHeaderT *seedhdr;           /* pointer to seed data header */
40        char     seedfile[BC_FILELTH+1];    /* name of SEED file */
41        int      read_ret;                  /* fread return values */
42        NTIME    ntime;                     /* numeric time */
43        FILE     *fp;                       /* pointer to SEED file */
44        char     sfd_t_start[BC_TIMELTH+1]; /* start time */
45        char     sfd_t_end[BC_TIMELTH+1];   /* end time */
46        int      sfd_recno;                 /* number of records in file */
47        char     sfd_stream[BC_LINELTH+1];  /* stream 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
61        /* executable code */
62
63        status = BC_NOERROR;
64        recsize = 0;  /* must be set or detected later, otherwise abort */
65
66        pa_init( argc, argv );
67        if  (pa_pnumber() < 1 || pa_pnumber() > 2)  {
68                fprintf( stderr, "Usage: %s <seed-file> [<outfile>]***\n",
69                        pa_progname() );
70                return 1;
71        } /*endif*/
72        strcpy( seedfile, pa_pvalue(1) );
73        if  (pa_pnumber() == 2)  {
74                strcpy( outfile, pa_pvalue(2) );
75                out = fopen( outfile, "a" );
76                if  (out == NULL)  {
77                        fprintf( stderr, "%s: output file %s couldn't be opened ***\n",
78                                pa_progname(), outfile );
79                        return 1;
80                } /*endif*/
81        } else {
82                out = stdout;
83        } /*endif*/
84
85        /* check for record size qualifier */
86        if  (pa_qspecified("-seedrec"))  {
87                strcpy( recsize_par, pa_qvalue("-seedrec") );
88                if  (strcmp(recsize_par,"quickfind") == 0)  {
89                        /* just do nothing, leave recsize zero */
90                } else {
91                        if  (sscanf(recsize_par,"%d",&recsize) != 1)  {
92                                fprintf( stderr, "%s: illegal seedrec qualifier.  Abort.\n",
93                                        pa_progname() );
94                                return 1;
95                        } /*endif*/
96                } /*endif*/
97        } /*endif*/
98
99        /* check for offset specified */
100        byteoff = 0;
101        if  (pa_qspecified("-byteoff"))
102                sscanf( pa_qvalue("-byteoff"), "%d", &byteoff );
103
104        include_bad = FALSE;
105        if  (pa_qspecified("-include_bad"))
106                include_bad = TRUE;
107
108        use_timecorr = FALSE;
109        if  (pa_qspecified("-timecorr"))
110                use_timecorr = TRUE;
111
112        /* open seed file */
113        fp = fopen( seedfile, "rb" );
114        if  (fp == NULL)  {
115                fprintf( stderr, "*** %s: file %s not found\n", pa_progname(), seedfile );
116                fclose( out );
117                return 1;
118        } /*endif*/
119
120        /* determine record size if not specified */
121        if  (recsize == 0)  {
122                SeedQuickFindReclth( fp, &recsize, &byteoff );
123                if  (recsize <= 0)  {
124                        fclose( fp );
125                        if  (recsize == 0)  {
126                                fprintf( stderr, "%s: no valid SEED file %s\n",
127                                        pa_progname(), seedfile );
128                        } else {
129                                fprintf( stderr, "%s: cannot find record size in %s\n",
130                                        pa_progname(), seedfile );
131                        } /*endif*/
132                        return 1;
133                } /*endif*/
134        } /*endif*/
135
136        /* allocate memory for SEED record */
137        seedrec = (SeedSbyteT *)sy_allocmem( 1, recsize, &status );
138        if  (Severe(&status))  err_writemsg( status, "", TRUE );
139
140        /* skip offset */
141        if  (byteoff > 0)
142                fseek( fp, byteoff, 0 );
143
144        /* read first record */
145        read_ret = (int)fread( (char *)seedrec, recsize, 1, fp );
146        if  (read_ret != 1)  {
147                fprintf( stderr, "*** %s: read error on file %s\n",
148                        pa_progname(), seedfile );
149                fclose( fp );
150                fclose( out );
151                return 1;
152        } /*endif*/
153        seedhdr = (SeedDataHeaderT *)seedrec;
154        sfd_swap_hdr = SeedSwapNecessary( seedhdr );
155        if  (sfd_swap_hdr)  SeedSwapHeader( seedhdr );
156
157        /* find start time */
158        SeedBtimeToNtime( &(seedhdr->starttime), &ntime, &status );
159        if  (Severe(&status))  {
160                fprintf( stderr, "%s: couldn't read start time in file %s\n",
161                        pa_progname(), seedfile );
162                fclose( fp );
163                fclose( out );
164                return 1;
165        } /*endif*/
166        /* check for time correction, K.S. 16-Mar-99 */
167        if  (use_timecorr && (seedhdr->timecorr != 0)
168                && (((int)(seedhdr->activity) & Seed_F_ACT_TIMECORRAPP) == 0))  {
169                tc_nadd( &ntime, (float)(seedhdr->timecorr)/TIMECORRUNIT,
170                        &ntime, &status );
171                if  (Severe(&status))  {
172                        fprintf( stderr, "%s: couldn't apply time correction in file %s\n",
173                                pa_progname(), seedfile );
174                        fclose( fp );
175                        fclose( out );
176                        return 1;
177                } /*endif*/
178        } /*endif*/
179        tc_n2t( &ntime, sfd_t_start, &status );
180        if  (Severe(&status))  {
181                fprintf( stderr, "%s: couldn't convert start time in file %s\n",
182                        pa_progname(), seedfile );
183                fclose( fp );
184                fclose( out );
185                return 1;
186        } /*endif*/
187
188        /* channel name */
189        csrc = seedhdr->statcode;
190        cdst = sfd_stream;
191        *cdst = '\0';
192        i = 0;
193        /* exit loop after 5 chars maximum, added 17-Jun-2005, K.S. */
194        while  (*csrc > ' ' && *csrc <= 'z' && i < 5)  {
195                *cdst++ = *csrc++;
196                i++;
197        } /*endwhile*/
198        *cdst++ = '-';
199        *cdst = '\0';
200        chanstr[0] = Cap( seedhdr->channel[0] );
201        chanstr[1] = Cap( seedhdr->channel[1] );
202        chanstr[2] = '\0';
203        if  (chanstr[0] <= ' ' || chanstr[0] > 'z')  chanstr[0] = '\0';
204        if  (chanstr[1] <= ' ' || chanstr[1] > 'z')  chanstr[1] = '\0';
205        ut_uncap( chanstr );
206#ifdef XXX
207        if  (strcmp(chanstr,"bh") == 0)  {         strcat( sfd_stream, "vbb-" );
208        } else if  (strcmp(chanstr,"hh") == 0)  {  strcat( sfd_stream, "vsp-" );
209        } else if  (strcmp(chanstr,"lh") == 0)  {  strcat( sfd_stream, "lp-" );
210        } else {
211                strcat( sfd_stream, chanstr );
212                strcat( sfd_stream, "-" );
213        } /*endif*/
214#endif
215        strcat( sfd_stream, chanstr );
216        strcat( sfd_stream, "-" );
217        i = (int)strlen( sfd_stream );
218        sfd_stream[i] = seedhdr->channel[2];
219        if  (sfd_stream[i] <= ' ' || sfd_stream[i] > 'z')  sfd_stream[i] = '\0';
220        if  (sfd_stream[i] == '?' || sfd_stream[i] == '*')  sfd_stream[i] = 'x';
221        sfd_stream[i+1] = '\0';
222        ut_uncap( sfd_stream );
223
224        /* get sample distance */
225        if  (!include_bad)  {
226                if  (seedhdr->smprate_fact == 0 || seedhdr->smprate_mult == 0)  {
227                        fprintf( stderr, "%s: ignore bad data file %s\n", argv[0], seedfile );
228                        fclose( fp );
229                        return 1;
230                } /*endif*/
231        } /*endif*/
232        dt = SeedGetSampleDist( seedhdr );
233
234        /* get length of file */
235        fseek( fp, 0, 2 );
236        fsize = ftell( fp ) - byteoff;
237        if  (fsize % recsize != 0)  {
238                fprintf( stderr, "*** %s: illegal size %ld bytes of SEED file %s\n",
239                        pa_progname(), fsize, seedfile );
240                fclose( fp );
241                fclose( out );
242                return 1 ;
243        } /*endif*/
244        sfd_recno = (int)(fsize / (long)recsize);
245
246        /* get last record */
247        fseek( fp,
248                (long)(sfd_recno-1)*(long)recsize + (long)byteoff, 0 );
249        read_ret = (int)fread( (char *)seedrec, recsize, 1, fp );
250        if  (read_ret != 1)  {
251                fprintf( stderr, "*** %s: read error on file %s (end)\n",
252                        pa_progname(), seedfile );
253                fclose( fp );
254                fclose( out );
255                return 1;
256        } /*endif*/
257        seedhdr = (SeedDataHeaderT *)seedrec;
258        if  (SeedSwapNecessary(seedhdr))  SeedSwapHeader(seedhdr);
259        errcnt = 0;
260        SeedBtimeToNtime( &(seedhdr->starttime), &ntime, &status );
261        if  (status == cBcNoError)
262                tc_nadd( &ntime, (float)(seedhdr->no_of_samples)*dt, &ntime, &status );
263        while  (Severe(&status))  {
264                errcnt++;
265                fprintf( stderr,
266                        "%s: unreadable last record, take %d before in file %s\n",
267                        pa_progname(), errcnt, seedfile );
268                status = cBcNoError;
269                /* get previous record */
270                sfd_recno--;
271                fseek( fp,
272                        (long)(sfd_recno-1)*(long)recsize + (long)byteoff,
273                        0 );
274                read_ret = (int)fread( (char *)seedrec, recsize, 1, fp );
275                if  (read_ret != 1)  {
276                        fprintf( stderr, "%s: read error on file %s (end)\n",
277                                pa_progname(), seedfile );
278                        fclose( fp );
279                        fclose( out );
280                        return 1;
281                } /*endif*/
282                seedhdr = (SeedDataHeaderT *)seedrec;
283                if  (SeedSwapNecessary(seedhdr))  SeedSwapHeader(seedhdr);
284                SeedBtimeToNtime( &(seedhdr->starttime), &ntime, &status );
285                if  (status == cBcNoError)
286                        tc_nadd( &ntime, (float)(seedhdr->no_of_samples)*dt, &ntime, &status );
287                if  (Severe(&status) && errcnt > 10)  {
288                        fprintf( stderr,
289                                "%s: couldn't read start time of last record in file %s\n",
290                                pa_progname(), seedfile );
291                        fclose( fp );
292                        fclose( out );
293                        return 1;
294                } /*endif*/
295        } /*endwhile*/
296        tc_n2t( &ntime, sfd_t_end, &status );
297        if  (Severe(&status))  {
298                fprintf( stderr, "%s: couldn't convert end time in file %s\n",
299                        pa_progname(), seedfile );
300                fclose( fp );
301                fclose( out );
302                return 1;
303        } /*endif*/
304
305        fclose( fp );
306
307        fprintf( out, "%c>%s %c>%s %c>%s %c>%s %c>%d %c>%d %c>%d %c>%d\n",
308                Seed_C_SfdStream, sfd_stream, Seed_C_SfdName, seedfile,
309                Seed_C_SfdTStart, sfd_t_start, Seed_C_SfdTEnd, sfd_t_end,
310                Seed_C_SfdRecno, sfd_recno, Seed_C_SfdSwapH, sfd_swap_hdr,
311                Seed_C_SfdReclth, recsize, Seed_C_SfdOffset, byteoff );
312
313        if  (out != stdout)  fclose( out );
314
315        return 0;
316
317} /* end of main */
318
319
320
321/*----------------------------------------------------------------------------*/
322
Note: See TracBrowser for help on using the repository browser.