/************************************************************************/ /* Populate_SimpleSEED: Populates simplified response from a rdseed file. /************************************************************************/ /* Author: Stephane Zuzlewski UC Berkeley Seismological Laboratory stephane@seismo.berkeley.edu Purpose: Populates stages 1, 2 & 0 from a rdseed output style file (s option) into the IR schema. Modification History: Date Ver Who What --------------------------------------------------------------------- 2002/11/25 1.0 SMZ Initial coding (Stages 1 & 0). 2002/12/11 1.0 SMZ Added digitizer stage (2). /************************************************************************/ #ifndef lint static char sccsid[] = "%W% %G% %U%"; #endif #include #include #include #define CONNECT_STRING "user/passwd@db" #define NOMOREROWS 1403 #define VERSION "1.0" #define info stdout #define CALDATE "/usr/local/bin/caldate -f \"%Y/%m/%d %H:%M:%S\"" typedef struct { char net[9]; char sta[7]; double lat; double lon; double elev; char staname[51]; int net_id; int word_32; int word_16; char ondate[20]; char offdate[20]; } Station_Data; typedef struct { char net[8]; char sta[7]; char seedchan[4]; char location[3]; int inid; char remark[31]; int unit_signal; int unit_calib; double lat; double lon; double elev; double edepth; double azimuth; double dip; int format_id; int record_length; double samprate; double clock_drift; char flags[28]; char ondate[20]; char offdate[20]; } Channel_Data; typedef struct { char name[255]; int family; int nb_keys; char key[255][255]; } Format_Data; typedef struct { double r_value; double r_error; double i_value; double i_error; } PZ; typedef struct { int unit_in; int unit_out; double AO; double AF; int nb_poles; int nb_zeros; PZ poles[255]; PZ zeros[255]; } PZ_Data; typedef struct { double sensitivity; double frequency; } Gain_Data; typedef struct { int unit_in; int unit_out; } DC; typedef struct { double samprate; int factor; int offset; double delay; double correction; } DM; char *syntax[] = { "%s version " VERSION " -- Populates simplified response from a rdseed file.", "%s [-h] [-v] [-p] ", " where:", " -h Help - prints syntax message.", " -v Verbose mode.", " -p Population mode.", NULL }; /************************************************************************/ EXEC SQL INCLUDE sqlca.h; EXEC SQL BEGIN DECLARE SECTION; char user_pwd[80]; int id; int itmp; char stmp[255]; char stmp2[255]; short ind; char net[9]; char sta[7]; double lat; double lon; double elev; char staname[51]; int net_id; int word_32; int word_16; char ondate[20]; char offdate[20]; char seedchan[4]; char location[3]; int inid; char remark[31]; int unit_signal; int unit_calib; double edepth; double azimuth; double dip; int format_id; int record_length; double samprate; double clock_drift; char flags[28]; int pz_key; int unit_in; int unit_out; double AO; double AF; double r_value; double r_error; double i_value; double i_error; double sensitivity; double frequency; int dm_key; int factor; int offset; double delay; double correction; EXEC SQL END DECLARE SECTION; EXEC SQL DECLARE sql_stmt STATEMENT; /************************************************************************/ /* External variables and symbols. */ /************************************************************************/ char *cmdname; /* program name from command line. */ long SQLCODE; void sql_error(); /* Handles unrecoverable errors */ /************************************************************************/ /* print_syntax: */ /* Print the syntax description of program. */ /************************************************************************/ int print_syntax (char *cmd, /* program name. */ char *syntax[], /* syntax array. */ FILE *fp) /* FILE ptr for output. */ { int i; for (i=0; syntax[i] != NULL; i++) { fprintf (fp, syntax[i], cmd); fprintf (fp, "\n"); } return (0); } /************************************************************************/ /* main: main program. /************************************************************************/ main (int argc, char *argv[]) { int FlagV = 0; /* Verbose Flag */ int FlagP = 0; /* Population Flag */ FILE *frdseed; /* RDSEED File Descriptor */ char nrdseed[255]; /* RDSEED File Name */ char line[256]; /* Current Line */ char header[32]; /* Blockette Header */ Station_Data sdata; /* Station Information */ Channel_Data cdata; /* Channel Information */ Format_Data fdata; /* Format Information */ PZ_Data pzdata[16]; /* Poles & Zeros Information */ Gain_Data gdata[64]; /* Gain Information */ DC dcdata[64]; /* Coefficients Information */ DM dmdata[64]; /* Decimation Information */ int Stage_Nb = 0; /* Current Stage Number */ FILE *cmd_ptr; /* Command Line File Descriptor */ char cmd_line[256]; /* Command Line */ int i; /* Loop Variable */ /* Variables needed for getopt. */ extern char *optarg; extern int optind, opterr; int c; char *p; cmdname = ((p = strrchr(*argv,'/')) != NULL) ? ++p : *argv; /* Parse command line options. */ while ( (c = getopt(argc,argv,"hvp")) != -1) switch (c) { case '?': case 'h': print_syntax(cmdname,syntax,info); exit(0); break; case 'v': FlagV = 1; break; case 'p': FlagP = 1; break; default: fprintf (info, "Unknown option: -%c\n", c); exit(1); } /* Skip over all options and their arguments. */ argv = &(argv[optind]); argc -= optind; if (argc != 1) { print_syntax(cmdname,syntax,info); exit(0); } else strcpy (nrdseed, argv[0]); /* Connect to ORACLE. */ EXEC SQL WHENEVER SQLERROR DO sql_error(); strcpy (user_pwd, CONNECT_STRING); EXEC SQL CONNECT :user_pwd; /* Opening RDSEED file */ if (FlagV) printf ("\n Processing file %s ...\n", nrdseed); if ((frdseed = fopen (nrdseed, "rt")) == NULL) { printf ("\n Error: couldn't open file %s.\n", nrdseed); exit (-1); } /* Reading RDSEED file */ fgets (line, 255, frdseed); while (!feof (frdseed)) { sscanf (line, "%s", header); /* Blockette 50 - Station Information */ if (!strcmp (header, "B050F03")) { sscanf (line, "%*s %*s %*s %s", &sdata.sta); if (FlagV) printf ("\n\n%s\t%s", header, sdata.sta); } else if (!strcmp (header, "B050F04")) { sscanf (line, "%*s %*s %lf", &sdata.lat); if (FlagV) printf ("\n%s\t%f", header, sdata.lat); } else if (!strcmp (header, "B050F05")) { sscanf (line, "%*s %*s %lf", &sdata.lon); if (FlagV) printf ("\n%s\t%f", header, sdata.lon); } else if (!strcmp (header, "B050F06")) { sscanf (line, "%*s %*s %lf", &sdata.elev); if (FlagV) printf ("\n%s\t%f", header, sdata.elev); } else if (!strcmp (header, "B050F09")) { char tmp[32]; sscanf (line, "%*s %*s %s", tmp); strcpy (sdata.staname, strstr (line, tmp)); sdata.staname[strlen (sdata.staname) - 1] = '\0'; if (FlagV) printf ("\n%s\t%s", header, sdata.staname); } else if (!strcmp (header, "B050F10")) { char tmp[32]; char owner[255]; sscanf (line, "%*s %*s %*s %*s %*s %s", tmp); strcpy (owner, strstr (line, tmp)); owner[strlen (owner) - 1] = '\0'; if (FlagV) printf ("\n%s\t%s", header, owner); if (FlagP) { id = (-1); strcpy (stmp, owner); EXEC SQL SELECT id INTO :id:ind FROM D_Abbreviation WHERE description = :stmp; if ((id == (-1)) || (ind < 0)) { EXEC SQL SELECT abbseq.NEXTVAL INTO :id FROM DUAL; EXEC SQL INSERT INTO D_Abbreviation (id, description) VALUES (:id, :stmp); sdata.net_id = id; } else { sdata.net_id = id; } if (FlagV) printf ("\n%s\tnet_id = %d", "DB", sdata.net_id); } } else if (!strcmp (header, "B050F11")) { sscanf (line, "%*s %*s %*s %*s %d", &sdata.word_32); if (FlagV) printf ("\n%s\t%d", header, sdata.word_32); } else if (!strcmp (header, "B050F12")) { sscanf (line, "%*s %*s %*s %*s %d", &sdata.word_16); if (FlagV) printf ("\n%s\t%d", header, sdata.word_16); } else if (!strcmp (header, "B050F13")) { char tmp[20]; sscanf (line, "%*s %*s %*s %s", tmp); if (!strcmp (tmp, "(null)")) strcpy (sdata.ondate, "3000/01/01 00:00:00"); else { sprintf (cmd_line , "%s %s", CALDATE, tmp); if ((cmd_ptr = popen (cmd_line,"r")) == NULL) exit (-1); if ((fgets (sdata.ondate, 255, cmd_ptr)) == NULL) exit (-1); pclose (cmd_ptr); sdata.ondate[strlen (sdata.ondate) - 1] = '\0'; } if (FlagV) printf ("\n%s\t%s", header, sdata.ondate); } else if (!strcmp (header, "B050F14")) { char tmp[20]; sscanf (line, "%*s %*s %*s %s", tmp); if (!strcmp (tmp, "(null)")) strcpy (sdata.offdate, "3000/01/01 00:00:00"); else { sprintf (cmd_line , "%s %s", CALDATE, tmp); if ((cmd_ptr = popen (cmd_line,"r")) == NULL) exit (-1); if ((fgets (sdata.offdate, 255, cmd_ptr)) == NULL) exit (-1); pclose (cmd_ptr); sdata.offdate[strlen (sdata.offdate) - 1] = '\0'; } if (FlagV) printf ("\n%s\t%s", header, sdata.offdate); } else if (!strcmp (header, "B050F16")) { sscanf (line, "%*s %*s %*s %s", &sdata.net); if (FlagV) printf ("\n%s\t%s", header, sdata.net); } /* Blockette 52 - Channel Information */ else if (!strcmp (header, "B052F04")) { sscanf (line, "%*s %*s %s", &cdata.seedchan); if (FlagV) printf ("\n\n%s\t%s", header, cdata.seedchan); } else if (!strcmp (header, "B052F03")) { strcpy (cdata.location, ""); sscanf (line, "%*s %*s %s", &cdata.location); if (strlen (cdata.location) == 0) strcpy (cdata.location, " "); if (FlagV) printf ("\n%s\t%s", header, cdata.location); } else if (!strcmp (header, "B052F06")) { char tmp[32]; char inst_type[255]; sscanf (line, "%*s %*s %*s %*s %s", tmp); strcpy (inst_type, strstr (line, tmp)); inst_type[strlen (inst_type) - 1] = '\0'; if (FlagV) printf ("\n%s\t%s", header, inst_type); if (FlagP) { id = (-1); strcpy (stmp, inst_type); EXEC SQL SELECT id INTO :id:ind FROM D_Abbreviation WHERE description = :stmp; if ((id == (-1)) || (ind < 0)) { EXEC SQL SELECT abbseq.NEXTVAL INTO :id FROM DUAL; EXEC SQL INSERT INTO D_Abbreviation (id, description) VALUES (:id, :stmp); cdata.inid = id; } else { cdata.inid = id; } if (FlagV) printf ("\n%s\tinid = %d", "DB", cdata.inid); } } else if (!strcmp (header, "B052F07")) { char tmp[32]; sscanf (line, "%*s %*s %s", tmp); strcpy (cdata.remark, strstr (line, tmp)); cdata.remark[strlen (cdata.remark) - 1] = '\0'; if (FlagV) printf ("\n%s\t%s", header, cdata.remark); } else if (!strcmp (header, "B052F08")) { char sig_name[127]; char sig_desc[127]; sscanf (line, "%*s %*s %*s %*s %*s %s", sig_name); strcpy (sig_desc, strstr (line, sig_name) + strlen (sig_name) + 3); sig_desc[strlen (sig_desc) - 1] = '\0'; if (FlagV) printf ("\n%s\t%s [%s]", header, sig_name, sig_desc); if (FlagP) { id = (-1); strcpy (stmp, sig_name); EXEC SQL SELECT id INTO :id:ind FROM D_Unit WHERE name = :stmp; if ((id == (-1)) || (ind < 0)) { strcpy (stmp2, sig_desc); EXEC SQL SELECT uniseq.NEXTVAL INTO :id FROM DUAL; EXEC SQL INSERT INTO D_Unit (id, name, description) VALUES (:id, :stmp, :stmp2); cdata.unit_signal = id; } else { cdata.unit_signal = id; } if (FlagV) printf ("\n%s\tunit_signal = %d", "DB", cdata.unit_signal); } } else if (!strcmp (header, "B052F09")) { char cal_name[127]; char cal_desc[127]; sscanf (line, "%*s %*s %*s %*s %*s %s", cal_name); strcpy (cal_desc, strstr (line, cal_name) + strlen (cal_name) + 3); cal_desc[strlen (cal_desc) - 1] = '\0'; if (FlagV) printf ("\n%s\t%s [%s]", header, cal_name, cal_desc); if (FlagP) { id = (-1); strcpy (stmp, cal_name); EXEC SQL SELECT id INTO :id:ind FROM D_Unit WHERE name = :stmp; if ((id == (-1)) || (ind < 0)) { strcpy (stmp2, cal_desc); EXEC SQL SELECT uniseq.NEXTVAL INTO :id FROM DUAL; EXEC SQL INSERT INTO D_Unit (id, name, description) VALUES (:id, :stmp, :stmp2); cdata.unit_calib = id; } else { cdata.unit_calib = id; } if (FlagV) printf ("\n%s\tunit_calib = %d", "DB", cdata.unit_calib); } } else if (!strcmp (header, "B052F10")) { sscanf (line, "%*s %*s %lf", &cdata.lat); if (FlagV) printf ("\n%s\t%f", header, cdata.lat); } else if (!strcmp (header, "B052F11")) { sscanf (line, "%*s %*s %lf", &cdata.lon); if (FlagV) printf ("\n%s\t%f", header, cdata.lon); } else if (!strcmp (header, "B052F12")) { sscanf (line, "%*s %*s %lf", &cdata.elev); if (FlagV) printf ("\n%s\t%f", header, cdata.elev); } else if (!strcmp (header, "B052F13")) { sscanf (line, "%*s %*s %*s %lf", &cdata.edepth); if (FlagV) printf ("\n%s\t%f", header, cdata.edepth); } else if (!strcmp (header, "B052F14")) { sscanf (line, "%*s %*s %lf", &cdata.azimuth); if (FlagV) printf ("\n%s\t%f", header, cdata.azimuth); } else if (!strcmp (header, "B052F15")) { sscanf (line, "%*s %*s %lf", &cdata.dip); if (FlagV) printf ("\n%s\t%f", header, cdata.dip); } else if (!strcmp (header, "B030F03")) { char tmp[32]; sscanf (line, "%*s %*s %*s %s", tmp); strcpy (fdata.name, strstr (line, tmp)); fdata.name[strlen (fdata.name) - 1] = '\0'; if (FlagV) printf ("\n%s\t%s", header, fdata.name); } else if (!strcmp (header, "B030F05")) { sscanf (line, "%*s %*s %*s %d", &fdata.family); if (FlagV) printf ("\n%s\t%d", header, fdata.family); } else if (!strcmp (header, "B030F06")) { int i; char tmp[32]; sscanf (line, "%*s %*s %*s %*s %d", &fdata.nb_keys); if (FlagV) printf ("\n%s\t%d", header, fdata.nb_keys); for (i=0;i