1 | |
---|
2 | /* file split_seed.c |
---|
3 | * ============ |
---|
4 | * |
---|
5 | * version 3, 12-Apr-96 |
---|
6 | * |
---|
7 | * splits SEED Files to Mini-SEED files, one file per stream |
---|
8 | * K. Stammler, 22-Dec-94 |
---|
9 | */ |
---|
10 | |
---|
11 | |
---|
12 | #include <stdio.h> |
---|
13 | #include <string.h> |
---|
14 | #include BASECNST |
---|
15 | #include BC_SYSBASE |
---|
16 | #include BC_CPAR |
---|
17 | #include BC_ERUSRDEF |
---|
18 | #include "seedcfg.h" |
---|
19 | #include "seed_lib.h" |
---|
20 | |
---|
21 | |
---|
22 | |
---|
23 | int main( int argc, char *argv[] ) |
---|
24 | { |
---|
25 | /* local variables */ |
---|
26 | STATUS status; /* return status */ |
---|
27 | int recsize; /* SEED record size in bytes */ |
---|
28 | char recsize_par[cBcLineLth]; /* record size parameter string */ |
---|
29 | int byteoff; /* byte offset at beginning */ |
---|
30 | SeedSbyteT *seedrec; /* pointer to SEED record */ |
---|
31 | SeedDataHeaderT *seedhdr; /* pointer to seed data header */ |
---|
32 | char inpfile[BC_FILELTH+1]; /* name of input file */ |
---|
33 | char outfile[BC_FILELTH+1]; /* name of output file */ |
---|
34 | FILE *inp, *out; /* file pointers */ |
---|
35 | int fsize; /* size of file in bytes */ |
---|
36 | int numrecs; /* number of SEED records */ |
---|
37 | int i, j; /* counter */ |
---|
38 | int read_ret; /* return value of fread */ |
---|
39 | int write_ret; /* return value of fwrite */ |
---|
40 | int reccnt; /* record counter */ |
---|
41 | int filecnt; /* file counter */ |
---|
42 | char last_station[BC_LINELTH+1];/* last station name */ |
---|
43 | char last_chan[BC_LINELTH+1]; /* last channel */ |
---|
44 | char curr_station[BC_LINELTH+1];/* current station */ |
---|
45 | char curr_chan[BC_LINELTH+1]; /* current channel */ |
---|
46 | char ctmp; /* scratch */ |
---|
47 | int maxfile; /* maximum number of files */ |
---|
48 | int startnum; /* start number number of file */ |
---|
49 | |
---|
50 | /* executable code */ |
---|
51 | |
---|
52 | status = BC_NOERROR; |
---|
53 | |
---|
54 | pa_init( argc, argv ); |
---|
55 | if (pa_pnumber() != 1) { |
---|
56 | fprintf( stderr, "*** Usage: %s <inpfile> ***\n", argv[0] ); |
---|
57 | fprintf( stderr, |
---|
58 | "qualifiers: -maxfile=<max> maximum file number\n" ); |
---|
59 | fprintf( stderr, " -start=<start> start number of file\n" ); |
---|
60 | return 1; |
---|
61 | } /*endif*/ |
---|
62 | strcpy( inpfile, pa_pvalue(1) ); |
---|
63 | maxfile = 0; |
---|
64 | if (pa_qspecified("-maxfile")) |
---|
65 | sscanf( pa_qvalue("-maxfile"), "%d", &maxfile ); |
---|
66 | startnum = 1; |
---|
67 | if (pa_qspecified("-start")) |
---|
68 | sscanf( pa_qvalue("-start"), "%d", &startnum ); |
---|
69 | startnum--; |
---|
70 | |
---|
71 | /* check for record size qualifier */ |
---|
72 | recsize = 0; /* must be set or detected later, otherwise abort */ |
---|
73 | byteoff = 0; |
---|
74 | if (pa_qspecified("-seedrec")) { |
---|
75 | strcpy( recsize_par, pa_qvalue("-seedrec") ); |
---|
76 | if (strcmp(recsize_par,"quickfind") == 0) { |
---|
77 | /* just do nothing, leave recsize zero */ |
---|
78 | } else { |
---|
79 | if (sscanf(recsize_par,"%d",&recsize) != 1) { |
---|
80 | fprintf( stderr, "%s: illegal seedrec qualifier. Abort.\n", |
---|
81 | pa_progname() ); |
---|
82 | return 1; |
---|
83 | } /*endif*/ |
---|
84 | } /*endif*/ |
---|
85 | } /*endif*/ |
---|
86 | |
---|
87 | /* get other parameters */ |
---|
88 | SeedLibInitialize( &status ); |
---|
89 | if (Severe(&status)) err_writemsg( status, "", TRUE ); |
---|
90 | |
---|
91 | /* allocate memory for SEED record */ |
---|
92 | seedrec = (SeedSbyteT *)sy_allocmem( 1, Seed_C_MAX_RECLTH, &status ); |
---|
93 | if (Severe(&status)) err_writemsg( status, "", TRUE ); |
---|
94 | seedhdr = (SeedDataHeaderT *)seedrec; |
---|
95 | |
---|
96 | /* open SEED file */ |
---|
97 | inp = fopen( inpfile, "rb" ); |
---|
98 | if (inp == NULL) { |
---|
99 | fprintf( stderr, "*** %s: file %s not found\n", argv[0], inpfile ); |
---|
100 | return 1; |
---|
101 | } /*endif*/ |
---|
102 | |
---|
103 | /* determine record size if not specified */ |
---|
104 | if (recsize == 0) { |
---|
105 | SeedQuickFindReclth( inp, &recsize, &byteoff ); |
---|
106 | if (recsize <= 0) { |
---|
107 | fclose( inp ); |
---|
108 | if (recsize == 0) { |
---|
109 | fprintf( stderr, "%s: no valid SEED file %s\n", |
---|
110 | pa_progname(), inpfile ); |
---|
111 | } else { |
---|
112 | fprintf( stderr, "%s: cannot find record size in %s\n", |
---|
113 | pa_progname(), inpfile ); |
---|
114 | } /*endif*/ |
---|
115 | return 1; |
---|
116 | } /*endif*/ |
---|
117 | } /*endif*/ |
---|
118 | |
---|
119 | /* get length of file */ |
---|
120 | fseek( inp, 0, 2 ); |
---|
121 | fsize = ftell( inp ); |
---|
122 | if (fsize % recsize != 0) { |
---|
123 | fprintf( stderr, "*** %s: illegal size %ld bytes of SEED file %s\n", |
---|
124 | argv[0], fsize, inpfile ); |
---|
125 | fclose( inp ); |
---|
126 | return 1; |
---|
127 | } /*endif*/ |
---|
128 | numrecs = (int)(fsize / (long)recsize); |
---|
129 | fseek( inp, 0, 0 ); |
---|
130 | |
---|
131 | *last_station = *last_chan = '\0'; |
---|
132 | filecnt = startnum; |
---|
133 | reccnt = 0; |
---|
134 | out = NULL; |
---|
135 | |
---|
136 | /* copy records and swap */ |
---|
137 | for (i=0; i<numrecs; i++) { |
---|
138 | read_ret = fread( (char *)seedrec, recsize, 1, inp ); |
---|
139 | if (read_ret != 1) { |
---|
140 | fprintf( stderr, "*** %s: read error on file %s\n", |
---|
141 | argv[0], inpfile ); |
---|
142 | fclose( inp ); |
---|
143 | if (out != NULL) fclose( out ); |
---|
144 | return 1; |
---|
145 | } /*endif*/ |
---|
146 | if (strchr(Seed_C_DATCHARS,seedrec[6]) == NULL) continue; |
---|
147 | strncpy( curr_station, seedhdr->statcode, 5 ); |
---|
148 | curr_station[5] = '\0'; |
---|
149 | if (curr_station[4] <= ' ') curr_station[4] = '\0'; |
---|
150 | if (curr_station[3] <= ' ') curr_station[3] = '\0'; |
---|
151 | strncpy( curr_chan, seedhdr->channel, 3 ); |
---|
152 | curr_chan[3] = '\0'; |
---|
153 | if (curr_chan[2] <= ' ') curr_chan[2] = '\0'; |
---|
154 | if (curr_chan[1] <= ' ') curr_chan[1] = '\0'; |
---|
155 | if (out == NULL || strcmp(curr_chan,last_chan) != 0 |
---|
156 | || strcmp(curr_station,last_station) != 0) { |
---|
157 | if (out != NULL) fclose( out ); |
---|
158 | sprintf( outfile, "miniseed_%03d", ++filecnt ); |
---|
159 | if (maxfile > 0 && filecnt > maxfile) { |
---|
160 | fclose( inp ); |
---|
161 | return 0; |
---|
162 | } /*endif*/ |
---|
163 | out = fopen( outfile, "wb" ); |
---|
164 | if (out == NULL) { |
---|
165 | fprintf( stderr, "*** %s: file %s not opened\n", argv[0], outfile ); |
---|
166 | fclose( inp ); |
---|
167 | return 1; |
---|
168 | } /*endif*/ |
---|
169 | printf( "write file %s with %s %s\n", |
---|
170 | outfile, curr_station, curr_chan ); |
---|
171 | reccnt = 0; |
---|
172 | strcpy( last_station, curr_station ); |
---|
173 | strcpy( last_chan, curr_chan ); |
---|
174 | } /*endif*/ |
---|
175 | ctmp = seedrec[6]; |
---|
176 | sprintf( (char *)seedrec, "%06d", ++reccnt ); |
---|
177 | seedrec[6] = 'D'; /*ctmp;*/ |
---|
178 | write_ret = fwrite( (char *)seedrec, recsize, 1, out ); |
---|
179 | if (write_ret != 1) { |
---|
180 | fprintf( stderr, "*** %s: write error on file %s\n", |
---|
181 | argv[0], outfile ); |
---|
182 | fclose( inp ); |
---|
183 | fclose( out ); |
---|
184 | return 1; |
---|
185 | } /*endif*/ |
---|
186 | } /*endif*/ |
---|
187 | |
---|
188 | fclose( inp ); |
---|
189 | if (out != NULL) fclose( out ); |
---|
190 | |
---|
191 | return 0; |
---|
192 | |
---|
193 | } /* end of main */ |
---|