source: SH_SHM/trunk/source/seed_io/sfd2db.c @ 310

Revision 310, 12.4 KB checked in by marcus, 13 years ago (diff)

r152 | walther | 2011-01-26 12:13:52 +0100 (Mi, 26 Jan 2011) | 2 lines

  • one-digit network codes are now supported (like G = GeoScope?).
  • minor clean-up
Line 
1
2/* file sfd2db.c
3 *      ========
4 *
5 * $Revision: 152 $, $Date: 2011-01-26 12:13:52 +0100 (Mi, 26 Jan 2011) $
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
25int 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        char     network[3];                /* network name */
40        char     location[3];               /* location code */
41        int      i;                         /* counter */
42        char     rpath[cBcFileLth+1];       /* relative path name */
43        int      pathid;                    /* path ID */
44        char     abspath[cBcFileLth+1];     /* path to sfdfile */
45        char     sfdbreq[cBcLineLth+1];     /* request command to sfdb database */
46        char     sfdbreqscriptfile[cBcLineLth+1];       /* request command to sfdb database for executing statements in a scriptfile*/
47        char     *env;                      /* pointer to environment */
48        char     shellcmd[cBcLongStrLth+1]; /* shell command */
49        char     tmpfile[cBcFileLth+1];     /* temporary file */
50        int      pathcnt;                   /* path counter */
51        int      sfcnt;                     /* seed file counter */
52        int      dataformat;                /* data format ID */
53        char     ignstr[cBcShortStrLth];    /* ignore string */
54        char     gostr[cBcShortStrLth];     /* go string */
55        TSyBoolean pipe_io;                 /* input/output on stdin/stdout */
56        TSyBoolean pathid_set;              /* path ID set on command line */
57        TSyBoolean update;                  /* produce also update code */
58        TSyBoolean noinsert;                /* produce no insert code */
59        TSyBoolean noignore;                /* produce no ignore keyword code */
60        TSyBoolean invhdr;                  /* invert header info in sfd entry */
61        TSyBoolean baynet;                  /* convert SH to BH for Bayernnetz */
62        TSyBoolean modgec2;                 /* modify gec2a and gec2b to gec2 */
63        TSyBoolean go;                      /* end tmpfile with insert statements with \g */
64
65
66        /* executable code */
67
68        status = cBcNoError;
69        pa_init( argc, argv );
70
71        if  (pa_pnumber() < 2 || pa_pnumber() > 3)  {
72                fprintf( stderr, "Usage: %s <inpfile> <priority> [<pathid>]\n", argv[0] );
73                return 1;
74        } /*endif*/
75
76        update = pa_qspecified( "-u" );
77        noinsert = pa_qspecified( "-noins" );
78        noignore = pa_qspecified( "-noign" );
79        invhdr = pa_qspecified( "-invhdr" );
80        baynet = pa_qspecified( "-baynet" );
81        go = pa_qspecified( "-go" );
82        modgec2 = 1;
83
84        if  (noignore)  {
85                *ignstr = '\0';
86        } else {
87                strcpy( ignstr, "ignore " );
88        } /*endif*/
89
90        if  (go)  {
91                strcpy( gostr, "\\g" );
92        } else {
93                *gostr = '\0';
94        } /*endif*/
95
96        env = (char *)getenv( "SFDBREQ" );
97        if  (env == NULL)  {
98                strcpy( sfdbreq, "mysql sfdb -B -e" );
99        } else {
100                if  (strlen(env) > cBcLineLth)  {
101                        fprintf( stderr, "%s: SFDBREQ too long.  Abort.\n", argv[0] );
102                        return 1;
103                } /*endif*/
104                strcpy( sfdbreq, env );
105        } /*endif*/
106
107        env = (char *)getenv( "SFDBREQSCRIPTFILE" );
108        if  (env == NULL)  {
109                strcpy( sfdbreqscriptfile, "mysql" );
110        } else {
111                if  (strlen(env) > cBcLineLth)  {
112                        fprintf( stderr, "%s: SFDBREQSCRIPTFILE too long.  Abort.\n", argv[0] );
113                        return 1;
114                } /*endif*/
115                strcpy( sfdbreqscriptfile, env );
116        } /*endif*/
117
118
119        /* temporary file */
120        i = 1;
121        FOREVER  {
122                sprintf( tmpfile, "/tmp/sfd2db_%d.000", i++ );
123                fp = fopen( tmpfile, "r" );
124                if  (fp == NULL)  break;
125                fclose( fp );
126        } /*endfor*/
127        /* create empty file to reserve name */
128        fp = fopen( tmpfile, "w" );
129        fclose( fp );
130
131        /* get parameters */
132        if  (strlen(pa_pvalue(1)) > cBcFileLth)  {
133                fprintf( stderr, "%s: name too long.  Abort\n", argv[0] );
134                return 1;
135        } /*endif*/
136        strcpy( fname, pa_pvalue(1) );
137        if  (sscanf(pa_pvalue(2),"%d",&priority) != 1)  {
138                fprintf( stderr, "%s: error reading priority.  Abort.\n", argv[0] );
139                return 1;
140        } /*endif*/
141        pathid = -1;
142        pathid_set = FALSE;
143        if  (pa_pnumber() > 2)  {
144                if  (sscanf(pa_pvalue(3),"%d",&pathid) != 1)  {
145                        fprintf( stderr, "%s: error reading pathid.  Abort.\n", argv[0] );
146                        return 1;
147                } /*endif*/
148                pathid_set = TRUE;
149        } /*endif*/
150
151        /* find last "/" */
152        i = strlen( fname );
153        while  (fname[i] != '/' && i > 0)
154                i--;
155        if  (fname[i] == '/')  {
156                strncpy( abspath, fname, i );
157                abspath[i] = '\0';
158        } else {
159                *abspath = '\0';
160        } /*endif*/
161
162        pathcnt = sfcnt = 0;
163
164        if  (pathid == -1)  {
165                /* get ID number for absolute path */
166                /* (1) check whether path is already inserted */
167                if  (strlen(sfdbreq)+strlen(abspath)+(2*strlen(tmpfile))+50 > cBcLongStrLth)  {
168                        fprintf( stderr, "%s: string overflow on shell command.  Abort.\n", argv[0] );
169                        return 1;
170                } /*endif*/
171                sprintf( shellcmd,
172                        "\\rm %s; %s \"select id from pathtab where rootpath = \'%s\'\" >%s",
173                        tmpfile, sfdbreq, abspath, tmpfile );
174                /*printf( "--> executing: %s\n", shellcmd );*/
175                system( shellcmd );
176                /* (2) read output file of sql command */
177                fp = fopen( tmpfile, "r" );
178                if  (fp == NULL)  {
179                        fprintf( stderr, "%s: open error on tmpfile %s.  This cannot happen.\n",
180                                argv[0], tmpfile );
181                        return 1;
182                } /*endif*/
183                /* read off header */
184                fgets( line, cBcLineLth, fp );
185                /* read result */
186                if  (fgets( line, cBcLineLth, fp ) == NULL)  {
187                        pathid = -1;
188                } else {
189                        if  (sscanf(line,"%d",&pathid) != 1)
190                                pathid = -1;
191                } /*endif*/
192                fclose( fp );
193                /* (3) if not already inserted get highest id number and increment it */
194                if  (pathid == -1)  {
195                        /* make new path entry */
196                        sprintf( shellcmd,
197                                "\\rm %s; %s \"select max(id) as id from pathtab;\" >%s",
198                                tmpfile, sfdbreq, tmpfile );
199                        /*printf( "--> executing %s\n", shellcmd );*/
200                        system( shellcmd );
201                        /* (4) read output file of sql command */
202                        fp = fopen( tmpfile, "r" );
203                        if  (fp == NULL)  {
204                                fprintf( stderr, "%s: open error on tmpfile %s.  This cannot happen.\n",
205                                        argv[0], tmpfile );
206                                return 1;
207                        } /*endif*/
208                        /* read off header */
209                        fgets( line, cBcLineLth, fp );
210                        /* read result */
211                        if  (fgets( line, cBcLineLth, fp ) == NULL)  {
212                                pathid = 1;
213                        } else {
214                                if  (sscanf(line,"%d",&pathid) == 1)  {
215                                        pathid++;
216                                } else {
217                                        pathid = 1;
218                                } /*endif*/
219                        } /*endif*/
220                        fclose( fp );
221                        sprintf( shellcmd, "%s \"insert into pathtab values ( %d, \'%s\' )\"",
222                                sfdbreq, pathid, abspath );
223                        /*printf( "--> executing %s\n", shellcmd );*/
224                        system( shellcmd );
225                        pathcnt++;
226                } /*endif*/
227        } /*endif*/
228        /*printf( "--> pathid: %d\n", pathid );*/
229
230        /* open input file */
231        if  (strcmp(fname,"TT") == 0 || strcmp(fname,"tt") == 0)  {
232                pipe_io = TRUE;
233                fp = stdin;
234        } else {
235                pipe_io = FALSE;
236                fp = fopen( fname, "r" );
237                if  (fp == NULL)  {
238                        fprintf( stderr, "%s: input file %s not found.  Abort.\n",
239                                argv[0], fname );
240                        return 1;
241                } /*endif*/
242        } /*endif*/
243
244        /* open output file */
245        if  (pipe_io)  {
246                out = stdout;
247        } else {
248                out = fopen( tmpfile, "w" );
249                if  (out == NULL)  {
250                        fprintf( stderr, "%s: error opening scratch file %s for output. Cannot happen.\n",
251                                argv[0], tmpfile );
252                        return 1;
253                } /*endif*/
254        } /*endif*/
255
256        dataformat = FORMAT_MSEED;
257
258        /* read through input file */
259        while  (fgets(line,cBcLineLth,fp) != NULL)  {
260
261                /* read and parse next line */
262                status = cBcNoError;
263                SeedParseSfdLine( line, &descr, &status );
264                if  (SySevere(&status))  {
265                        fprintf( stderr, "%s: error decoding line %s\n", argv[0], line );
266                        continue;
267                } /*endif*/
268                if  (invhdr)  descr.swap_hdr = !descr.swap_hdr;
269                if  (baynet)  {
270                        if  (strncmp(descr.stream,"rjob-sh",7) == 0)  descr.stream[5] = 'b';
271                        if  (strncmp(descr.stream,"manz-sh",7) == 0)  descr.stream[5] = 'b';
272                        if  (strncmp(descr.stream,"rotz-sh",7) == 0)  descr.stream[5] = 'b';
273                } /*endif*/
274                if  (modgec2)  {
275                        char tmpstr[cBcLineLth+1];
276                        if  (strncmp(descr.stream,"gec2a",5) == 0
277                                || strncmp(descr.stream,"gec2b",5) == 0)  {
278                                strcpy( tmpstr, descr.stream+5 );
279                                strcpy( descr.stream+4, tmpstr );
280                        } /*endif*/
281                } /*endif*/
282
283                /* parse stream string */
284                if  (strlen(descr.stream) > cBcShortStrLth)  {
285                        fprintf( stderr, "%s: stream too long: %s\n", argv[0], line );
286                        continue;
287                } /*endif*/
288                for  (i=0; descr.stream[i] != '\0'; i++)
289                        if  (descr.stream[i] == '-')  descr.stream[i] = ' ';
290                i = sscanf( descr.stream, "%s %s %c", station, chan, &comp );
291                if  (i < 3)  comp = ' ';
292                if  (i < 2)  strcpy( chan , "  " );
293                if  (i < 1)  strcpy( station , "   " );
294
295                /* parse path */
296                if  (strlen(descr.name) > cBcFileLth)  {
297                        fprintf( stderr, "%s: file path too long: %s\n", argv[0], line );
298                        continue;
299                } /*endif*/
300                if  (strncmp(descr.name,"$ROOT/",6) == 0)  {
301                        strcpy( rpath, descr.name+6 );
302                } else {
303                        strcpy( rpath, descr.name );
304                        if  (!pathid_set)  pathid = 0;
305                } /*endif*/
306
307                /* convert times to integers */
308                tc_t2n( descr.t_start, &stime, &status );
309                if  (SySevere(&status))  {
310                        fprintf( stderr, "%s: error decoding start time %s\n", argv[0], line );
311                        continue;
312                } /*endif*/
313                tc_t2n( descr.t_end, &etime, &status );
314                if  (SySevere(&status))  {
315                        fprintf( stderr, "%s: error decoding end time %s\n", argv[0], line );
316                        continue;
317                } /*endif*/
318
319                /* check for additional information (network and location code) */
320                if (descr.addinf[0] != '\0') {
321                    strncpy(network, descr.addinf, 2);
322                    network[2] = '\0';
323
324                    /* one digit network codes are common */
325                    if (network[1] == '.')
326                        network[1] = '\0';
327
328                    /* no network set at all. */
329                    if (!strncmp(network, "..", 2))
330                        network[0] = '\0';
331
332                    for (i=2; i<4; i++)
333                        location[i-2] = descr.addinf[i];
334                        location[2] = '\0';
335
336                    /* The SEED manual states for the "Location identifier":
337                     * Left justify and pad with spaces. Especially for file
338                     * system this is quite weird, so we support 2 digits only.
339                     * (for now).
340                     */
341                    if (!strncmp(location, "..", 2))
342                        location[0] = '\0';
343                } else {
344                    network[0] = '\0';
345                    location[0] = '\0';
346                }
347
348                /* write command line */
349                if  (!noinsert)
350                        fprintf( out,
351                                "insert %sinto sftab ( station, chan, comp, pathid, relpath, sdate, stime, edate, etime, recnum, hswap, recsize, offset, dataflags, priority, dataformat, network, location ) 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, \'%s\', \'%s\');\n",
352                                ignstr, station, chan, comp, pathid, rpath, stime.year, stime.month, stime.day,
353                                stime.hour, stime.min, stime.sec, stime.ms, etime.year, etime.month,
354                                etime.day, etime.hour, etime.min, etime.sec, etime.ms, descr.recno,
355                                descr.swap_hdr, descr.reclth, descr.byteoff, descr.dataflags, priority,
356                                dataformat, network, location );
357                if  (update)
358                        fprintf( out,
359                                "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",
360                                ignstr, stime.year, stime.month, stime.day, stime.hour, stime.min,
361                                stime.sec, stime.ms, etime.year, etime.month, etime.day, etime.hour,
362                                etime.min, etime.sec, etime.ms, descr.recno, descr.dataflags,
363                                priority, dataformat, descr.swap_hdr, pathid, rpath );
364                sfcnt++;
365
366        } /*endwhile*/
367
368        if  (!pipe_io)  {
369
370                fprintf( out, gostr);
371                fclose( fp );
372                fclose( out );
373
374                /*system( shellcmd );*/
375                sprintf( shellcmd, "%s sfdb < %s", sfdbreqscriptfile, tmpfile );
376                /*printf( "--> executing %s\n", shellcmd );*/
377                system( shellcmd );
378        } /*endif*/
379
380        sy_fdelete( tmpfile );
381
382        if  (!pipe_io)
383                printf( "%d path(s) and %d file(s) inserted\n", pathcnt, sfcnt );
384
385        return 0;
386
387} /* end of main */
Note: See TracBrowser for help on using the repository browser.