1 | |
---|
2 | /* file sfd2db.c |
---|
3 | * ======== |
---|
4 | * |
---|
5 | * $Revision: 26 $, $Date: 2008-02-11 16:00:39 +0100 (Mo, 11 Feb 2008) $ |
---|
6 | * |
---|
7 | * reads lines from sfdfile and creates commands to insert into sfdb database |
---|
8 | * K. Stammler, 4-Nov-2006 |
---|
9 | */ |
---|
10 | |
---|
11 | |
---|
12 | #include <stdio.h> |
---|
13 | #include <string.h> |
---|
14 | #include <stdlib.h> |
---|
15 | #include "basecnst.h" |
---|
16 | #include "sysbase.h" |
---|
17 | #include "cpar.h" |
---|
18 | #include "tcusrdef.h" |
---|
19 | #include "seedcfg.h" |
---|
20 | #include "seed_lib.h" |
---|
21 | |
---|
22 | #define FORMAT_MSEED 1 |
---|
23 | |
---|
24 | |
---|
25 | int main( int argc, char *argv[] ) |
---|
26 | { |
---|
27 | /* local variables */ |
---|
28 | int priority; /* priority of entry */ |
---|
29 | char fname[cBcFileLth+1]; /* name of input file */ |
---|
30 | FILE *fp; /* pointer to input file */ |
---|
31 | FILE *out; /* pointer to output file */ |
---|
32 | char line[cBcLineLth+1]; /* current line of file */ |
---|
33 | SeedFileDescrT descr; /* seed file descriptor */ |
---|
34 | TSyStatus status; /* return status */ |
---|
35 | NTIME stime, etime; /* start time and end time */ |
---|
36 | char station[cBcShortStrLth+1]; /* station name */ |
---|
37 | char chan[cBcShortStrLth+1]; /* channel name */ |
---|
38 | char comp; /* component name */ |
---|
39 | int i; /* counter */ |
---|
40 | char rpath[cBcFileLth+1]; /* relative path name */ |
---|
41 | int pathid; /* path ID */ |
---|
42 | char abspath[cBcFileLth+1]; /* path to sfdfile */ |
---|
43 | char sfdbreq[cBcLineLth+1]; /* request command to sfdb database */ |
---|
44 | char sfdbreqscriptfile[cBcLineLth+1]; /* request command to sfdb database for executing statements in a scriptfile*/ |
---|
45 | char *env; /* pointer to environment */ |
---|
46 | char shellcmd[cBcLongStrLth+1]; /* shell command */ |
---|
47 | char tmpfile[cBcFileLth+1]; /* temporary file */ |
---|
48 | int pathcnt; /* path counter */ |
---|
49 | int sfcnt; /* seed file counter */ |
---|
50 | int dataformat; /* data format ID */ |
---|
51 | char ignstr[cBcShortStrLth]; /* ignore string */ |
---|
52 | char gostr[cBcShortStrLth]; /* go string */ |
---|
53 | TSyBoolean pipe_io; /* input/output on stdin/stdout */ |
---|
54 | TSyBoolean pathid_set; /* path ID set on command line */ |
---|
55 | TSyBoolean update; /* produce also update code */ |
---|
56 | TSyBoolean noinsert; /* produce no insert code */ |
---|
57 | TSyBoolean noignore; /* produce no ignore keyword code */ |
---|
58 | TSyBoolean invhdr; /* invert header info in sfd entry */ |
---|
59 | TSyBoolean baynet; /* convert SH to BH for Bayernnetz */ |
---|
60 | TSyBoolean go; /* end tmpfile with insert statements with \g */ |
---|
61 | |
---|
62 | |
---|
63 | /* executable code */ |
---|
64 | |
---|
65 | status = cBcNoError; |
---|
66 | pa_init( argc, argv ); |
---|
67 | |
---|
68 | if (pa_pnumber() < 2 || pa_pnumber() > 3) { |
---|
69 | fprintf( stderr, "Usage: %s <inpfile> <priority> [<pathid>]\n", argv[0] ); |
---|
70 | return 1; |
---|
71 | } /*endif*/ |
---|
72 | |
---|
73 | update = pa_qspecified( "-u" ); |
---|
74 | noinsert = pa_qspecified( "-noins" ); |
---|
75 | noignore = pa_qspecified( "-noign" ); |
---|
76 | invhdr = pa_qspecified( "-invhdr" ); |
---|
77 | baynet = pa_qspecified( "-baynet" ); |
---|
78 | go = pa_qspecified( "-go" ); |
---|
79 | |
---|
80 | if (noignore) { |
---|
81 | *ignstr = '\0'; |
---|
82 | } else { |
---|
83 | strcpy( ignstr, "ignore " ); |
---|
84 | } /*endif*/ |
---|
85 | |
---|
86 | if (go) { |
---|
87 | strcpy( gostr, "\\g" ); |
---|
88 | } else { |
---|
89 | *gostr = '\0'; |
---|
90 | } /*endif*/ |
---|
91 | |
---|
92 | env = (char *)getenv( "SFDBREQ" ); |
---|
93 | if (env == NULL) { |
---|
94 | strcpy( sfdbreq, "mysql sfdb -B -e" ); |
---|
95 | } else { |
---|
96 | if (strlen(env) > cBcLineLth) { |
---|
97 | fprintf( stderr, "%s: SFDBREQ too long. Abort.\n", argv[0] ); |
---|
98 | return 1; |
---|
99 | } /*endif*/ |
---|
100 | strcpy( sfdbreq, env ); |
---|
101 | } /*endif*/ |
---|
102 | |
---|
103 | env = (char *)getenv( "SFDBREQSCRIPTFILE" ); |
---|
104 | if (env == NULL) { |
---|
105 | strcpy( sfdbreqscriptfile, "mysql" ); |
---|
106 | } else { |
---|
107 | if (strlen(env) > cBcLineLth) { |
---|
108 | fprintf( stderr, "%s: SFDBREQSCRIPTFILE too long. Abort.\n", argv[0] ); |
---|
109 | return 1; |
---|
110 | } /*endif*/ |
---|
111 | strcpy( sfdbreqscriptfile, env ); |
---|
112 | } /*endif*/ |
---|
113 | |
---|
114 | |
---|
115 | /* temporary file */ |
---|
116 | i = 1; |
---|
117 | FOREVER { |
---|
118 | sprintf( tmpfile, "/tmp/sfd2db_%d.000", i++ ); |
---|
119 | fp = fopen( tmpfile, "r" ); |
---|
120 | if (fp == NULL) break; |
---|
121 | fclose( fp ); |
---|
122 | } /*endfor*/ |
---|
123 | /* create empty file to reserve name */ |
---|
124 | fp = fopen( tmpfile, "w" ); |
---|
125 | fclose( fp ); |
---|
126 | |
---|
127 | /* get parameters */ |
---|
128 | if (strlen(pa_pvalue(1)) > cBcFileLth) { |
---|
129 | fprintf( stderr, "%s: name too long. Abort\n", argv[0] ); |
---|
130 | return 1; |
---|
131 | } /*endif*/ |
---|
132 | strcpy( fname, pa_pvalue(1) ); |
---|
133 | if (sscanf(pa_pvalue(2),"%d",&priority) != 1) { |
---|
134 | fprintf( stderr, "%s: error reading priority. Abort.\n", argv[0] ); |
---|
135 | return 1; |
---|
136 | } /*endif*/ |
---|
137 | pathid = -1; |
---|
138 | pathid_set = FALSE; |
---|
139 | if (pa_pnumber() > 2) { |
---|
140 | if (sscanf(pa_pvalue(3),"%d",&pathid) != 1) { |
---|
141 | fprintf( stderr, "%s: error reading pathid. Abort.\n", argv[0] ); |
---|
142 | return 1; |
---|
143 | } /*endif*/ |
---|
144 | pathid_set = TRUE; |
---|
145 | } /*endif*/ |
---|
146 | |
---|
147 | /* find last "/" */ |
---|
148 | i = strlen( fname ); |
---|
149 | while (fname[i] != '/' && i > 0) |
---|
150 | i--; |
---|
151 | if (fname[i] == '/') { |
---|
152 | strncpy( abspath, fname, i ); |
---|
153 | abspath[i] = '\0'; |
---|
154 | } else { |
---|
155 | *abspath = '\0'; |
---|
156 | } /*endif*/ |
---|
157 | |
---|
158 | pathcnt = sfcnt = 0; |
---|
159 | |
---|
160 | if (pathid == -1) { |
---|
161 | /* get ID number for absolute path */ |
---|
162 | /* (1) check whether path is already inserted */ |
---|
163 | if (strlen(sfdbreq)+strlen(abspath)+(2*strlen(tmpfile))+50 > cBcLongStrLth) { |
---|
164 | fprintf( stderr, "%s: string overflow on shell command. Abort.\n", argv[0] ); |
---|
165 | return 1; |
---|
166 | } /*endif*/ |
---|
167 | sprintf( shellcmd, |
---|
168 | "\\rm %s; %s \"select id from pathtab where rootpath = \'%s\'\" >%s", |
---|
169 | tmpfile, sfdbreq, abspath, tmpfile ); |
---|
170 | /*printf( "--> executing: %s\n", shellcmd );*/ |
---|
171 | system( shellcmd ); |
---|
172 | /* (2) read output file of sql command */ |
---|
173 | fp = fopen( tmpfile, "r" ); |
---|
174 | if (fp == NULL) { |
---|
175 | fprintf( stderr, "%s: open error on tmpfile %s. This cannot happen.\n", |
---|
176 | argv[0], tmpfile ); |
---|
177 | return 1; |
---|
178 | } /*endif*/ |
---|
179 | /* read off header */ |
---|
180 | fgets( line, cBcLineLth, fp ); |
---|
181 | /* read result */ |
---|
182 | if (fgets( line, cBcLineLth, fp ) == NULL) { |
---|
183 | pathid = -1; |
---|
184 | } else { |
---|
185 | if (sscanf(line,"%d",&pathid) != 1) |
---|
186 | pathid = -1; |
---|
187 | } /*endif*/ |
---|
188 | fclose( fp ); |
---|
189 | /* (3) if not already inserted get highest id number and increment it */ |
---|
190 | if (pathid == -1) { |
---|
191 | /* make new path entry */ |
---|
192 | sprintf( shellcmd, |
---|
193 | "\\rm %s; %s \"select max(id) as id from pathtab;\" >%s", |
---|
194 | tmpfile, sfdbreq, tmpfile ); |
---|
195 | /*printf( "--> executing %s\n", shellcmd );*/ |
---|
196 | system( shellcmd ); |
---|
197 | /* (4) read output file of sql command */ |
---|
198 | fp = fopen( tmpfile, "r" ); |
---|
199 | if (fp == NULL) { |
---|
200 | fprintf( stderr, "%s: open error on tmpfile %s. This cannot happen.\n", |
---|
201 | argv[0], tmpfile ); |
---|
202 | return 1; |
---|
203 | } /*endif*/ |
---|
204 | /* read off header */ |
---|
205 | fgets( line, cBcLineLth, fp ); |
---|
206 | /* read result */ |
---|
207 | if (fgets( line, cBcLineLth, fp ) == NULL) { |
---|
208 | pathid = 1; |
---|
209 | } else { |
---|
210 | if (sscanf(line,"%d",&pathid) == 1) { |
---|
211 | pathid++; |
---|
212 | } else { |
---|
213 | pathid = 1; |
---|
214 | } /*endif*/ |
---|
215 | } /*endif*/ |
---|
216 | fclose( fp ); |
---|
217 | sprintf( shellcmd, "%s \"insert into pathtab values ( %d, \'%s\' )\"", |
---|
218 | sfdbreq, pathid, abspath ); |
---|
219 | /*printf( "--> executing %s\n", shellcmd );*/ |
---|
220 | system( shellcmd ); |
---|
221 | pathcnt++; |
---|
222 | } /*endif*/ |
---|
223 | } /*endif*/ |
---|
224 | /*printf( "--> pathid: %d\n", pathid );*/ |
---|
225 | |
---|
226 | /* open input file */ |
---|
227 | if (strcmp(fname,"TT") == 0 || strcmp(fname,"tt") == 0) { |
---|
228 | pipe_io = TRUE; |
---|
229 | fp = stdin; |
---|
230 | } else { |
---|
231 | pipe_io = FALSE; |
---|
232 | fp = fopen( fname, "r" ); |
---|
233 | if (fp == NULL) { |
---|
234 | fprintf( stderr, "%s: input file %s not found. Abort.\n", |
---|
235 | argv[0], fname ); |
---|
236 | return 1; |
---|
237 | } /*endif*/ |
---|
238 | } /*endif*/ |
---|
239 | |
---|
240 | /* open output file */ |
---|
241 | if (pipe_io) { |
---|
242 | out = stdout; |
---|
243 | } else { |
---|
244 | out = fopen( tmpfile, "w" ); |
---|
245 | if (out == NULL) { |
---|
246 | fprintf( stderr, "%s: error opening scratch file %s for output. Cannot happen.\n", |
---|
247 | argv[0], tmpfile ); |
---|
248 | return 1; |
---|
249 | } /*endif*/ |
---|
250 | } /*endif*/ |
---|
251 | |
---|
252 | dataformat = FORMAT_MSEED; |
---|
253 | |
---|
254 | /* read through input file */ |
---|
255 | while (fgets(line,cBcLineLth,fp) != NULL) { |
---|
256 | |
---|
257 | /* read and parse next line */ |
---|
258 | status = cBcNoError; |
---|
259 | SeedParseSfdLine( line, &descr, &status ); |
---|
260 | if (SySevere(&status)) { |
---|
261 | fprintf( stderr, "%s: error decoding line %s\n", argv[0], line ); |
---|
262 | continue; |
---|
263 | } /*endif*/ |
---|
264 | if (invhdr) descr.swap_hdr = !descr.swap_hdr; |
---|
265 | if (baynet) { |
---|
266 | if (strncmp(descr.stream,"rjob-sh",7) == 0) descr.stream[5] = 'b'; |
---|
267 | if (strncmp(descr.stream,"manz-sh",7) == 0) descr.stream[5] = 'b'; |
---|
268 | if (strncmp(descr.stream,"rotz-sh",7) == 0) descr.stream[5] = 'b'; |
---|
269 | } /*endif*/ |
---|
270 | |
---|
271 | /* parse stream string */ |
---|
272 | if (strlen(descr.stream) > cBcShortStrLth) { |
---|
273 | fprintf( stderr, "%s: stream too long: %s\n", argv[0], line ); |
---|
274 | continue; |
---|
275 | } /*endif*/ |
---|
276 | for (i=0; descr.stream[i] != '\0'; i++) |
---|
277 | if (descr.stream[i] == '-') descr.stream[i] = ' '; |
---|
278 | i = sscanf( descr.stream, "%s %s %c", station, chan, &comp ); |
---|
279 | if (i < 3) comp = ' '; |
---|
280 | if (i < 2) strcpy( chan , " " ); |
---|
281 | if (i < 1) strcpy( station , " " ); |
---|
282 | |
---|
283 | /* parse path */ |
---|
284 | if (strlen(descr.name) > cBcFileLth) { |
---|
285 | fprintf( stderr, "%s: file path too long: %s\n", argv[0], line ); |
---|
286 | continue; |
---|
287 | } /*endif*/ |
---|
288 | if (strncmp(descr.name,"$ROOT/",6) == 0) { |
---|
289 | strcpy( rpath, descr.name+6 ); |
---|
290 | } else { |
---|
291 | strcpy( rpath, descr.name ); |
---|
292 | if (!pathid_set) pathid = 0; |
---|
293 | } /*endif*/ |
---|
294 | |
---|
295 | /* convert times to integers */ |
---|
296 | tc_t2n( descr.t_start, &stime, &status ); |
---|
297 | if (SySevere(&status)) { |
---|
298 | fprintf( stderr, "%s: error decoding start time %s\n", argv[0], line ); |
---|
299 | continue; |
---|
300 | } /*endif*/ |
---|
301 | tc_t2n( descr.t_end, &etime, &status ); |
---|
302 | if (SySevere(&status)) { |
---|
303 | fprintf( stderr, "%s: error decoding end time %s\n", argv[0], line ); |
---|
304 | continue; |
---|
305 | } /*endif*/ |
---|
306 | |
---|
307 | /* write command line */ |
---|
308 | if (!noinsert) |
---|
309 | fprintf( out, |
---|
310 | "insert %sinto sftab ( station, chan, comp, pathid, relpath, sdate, stime, edate, etime, recnum, hswap, recsize, offset, dataflags, priority, dataformat ) values ( \'%s\', \'%s\', \'%c\', %d, \'%s\', %4d%02d%02d, %2d%02d%02d.%03d, %4d%02d%02d, %2d%02d%02d.%03d, %d, %d, %d, %d, %d, %d, %d);\n", |
---|
311 | ignstr, station, chan, comp, pathid, rpath, stime.year, stime.month, stime.day, |
---|
312 | stime.hour, stime.min, stime.sec, stime.ms, etime.year, etime.month, |
---|
313 | etime.day, etime.hour, etime.min, etime.sec, etime.ms, descr.recno, |
---|
314 | descr.swap_hdr, descr.reclth, descr.byteoff, descr.dataflags, priority, |
---|
315 | dataformat ); |
---|
316 | if (update) |
---|
317 | fprintf( out, |
---|
318 | "update %ssftab set sdate=%4d%02d%02d, stime=%2d%02d%02d.%03d, edate=%4d%02d%02d, etime=%2d%02d%02d.%03d, recnum=%d, dataflags=%d, priority=%d, dataformat=%d, hswap=%d where pathid=%d and relpath=\'%s\';\n", |
---|
319 | ignstr, stime.year, stime.month, stime.day, stime.hour, stime.min, |
---|
320 | stime.sec, stime.ms, etime.year, etime.month, etime.day, etime.hour, |
---|
321 | etime.min, etime.sec, etime.ms, descr.recno, descr.dataflags, |
---|
322 | priority, dataformat, descr.swap_hdr, pathid, rpath ); |
---|
323 | sfcnt++; |
---|
324 | |
---|
325 | /* old lines for mysql |
---|
326 | "insert %sinto sftab values ( \"%s\", \"%s\", \"%c\", \"%d\", \"%s\", \"%4d%02d%02d\", \"%2d%02d%02d.%03d\", \"%4d%02d%02d\", \"%2d%02d%02d.%03d\", \"%d\", \"%d\", \"%d\", \"%d\", \"%d\", \"%d\", \"%d\", NULL );\n", |
---|
327 | "update %ssftab set sdate=\"%4d%02d%02d\", stime=\"%2d%02d%02d.%03d\", edate=\"%4d%02d%02d\", etime=\"%2d%02d%02d.%03d\", recnum=\"%d\", dataflags=\"%d\", priority=\"%d\", dataformat=\"%d\", hswap=\"%d\" where pathid=\"%d\" and relpath=\"%s\";\n", |
---|
328 | */ |
---|
329 | |
---|
330 | } /*endwhile*/ |
---|
331 | |
---|
332 | if (!pipe_io) { |
---|
333 | |
---|
334 | fprintf( out, gostr); |
---|
335 | fclose( fp ); |
---|
336 | fclose( out ); |
---|
337 | |
---|
338 | /*sprintf( shellcmd, "cat %s", tmpfile );*/ |
---|
339 | /*system( shellcmd );*/ |
---|
340 | sprintf( shellcmd, "%s sfdb < %s", sfdbreqscriptfile, tmpfile ); |
---|
341 | /*printf( "--> executing %s\n", shellcmd );*/ |
---|
342 | system( shellcmd ); |
---|
343 | } /*endif*/ |
---|
344 | |
---|
345 | sy_fdelete( tmpfile ); |
---|
346 | |
---|
347 | if (!pipe_io) |
---|
348 | printf( "%d path(s) and %d file(s) inserted\n", pathcnt, sfcnt ); |
---|
349 | |
---|
350 | return 0; |
---|
351 | |
---|
352 | } /* end of main */ |
---|