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

Revision 333, 13.0 KB checked in by marcus, 13 years ago (diff)

r172 | klaus | 2011-02-18 11:35:16 +0100 (Fr, 18 Feb 2011) | 1 line

modified sfd2db for renaming of han0,han3 eh channels

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