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

Revision 172, 11.6 KB checked in by marcus, 13 years ago (diff)

r101 | svn | 2009-11-27 11:48:20 +0100 (Fr, 27 Nov 2009) | 1 line

code for renaming gec2a,gec2b to gec2; added some station info

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