source: PP/branches/leap-second-dev-branch/dbselect/dbselect.pc @ 4026

Revision 4026, 109.6 KB checked in by stephane, 4 years ago (diff)

Added new -F format, FPFIT for fault plane solutions.

Line 
1int debug = 0;
2/************************************************************************/
3/*  dbselect: Parametric Information Search Engine.                     */
4/************************************************************************/
5/*
6    Author:
7        Stephane Zuzlewski
8        UC Berkeley Seismological Laboratory
9        stephane@seismo.berkeley.edu
10
11    Purpose: Parametric Information Query Engine
12
13    Modification History:
14    Date        Ver     Who     What
15    ---------------------------------------------------------------------
16    1998/??/??  1.0     SMZ     Initial coding.
17    2003/09/22  2.0     SMZ     Use of getopt for command line options.
18    2003/09/24  2.1     SMZ     Added new options for location line and
19                                system selection.
20    2003/10/06  2.2     SMZ     Header information now optional;
21                                Use qlib for time parsing;
22                                HTML format option removed.
23    2003/10/27  2.3     SMZ     Added new format 'ncedc'.
24                                Added new option for reviewed flag.
25    2003/11/03  2.4     SMZ     Fixed RMS bug.
26    2005/12/08  2.5     SMZ     Fixed CNSS quality bug.
27    2006/06/19  2.6     SMZ     Added Hypoinverse output format.
28    2006/07/31  2.7     SMZ     Md & Misc. Fixes. Code Optimization.
29                                CNSS format option removed.
30    2006/08/16  2.8     SMZ     More hypoinverse fixes. Added
31                                Amplitudes information.
32    2006/10/11  2.9     SMZ     Now supports multiple phases of same
33                                type and SNCL.
34    2006/11/14  2.10    SMZ     Fixes to phase/coda weights. Use of new
35                                hypoinverse fields velocity model id
36                                & domain.
37    2006/11/29  2.11    SMZ     Database connection string can now be
38                                specified in an environment variable.
39    2006/12/18  2.12    SMZ     Hypoinverse column 118 should be 'D'.
40    2007/01/09  2.13    SMZ     Hypoinverse column 50 should be '0' for
41                                P phase lines.
42    2007/02/21  2.14    SMZ     Returns also events with no magnitude.
43    2007/02/26  2.15    SMZ     Corrected bug with RMS.
44    2007/02/28  2.16    SMZ     Hypoinverse column 109 is taken from
45                                field Arrival.subsource.
46                                Hypoinverse columns 105 & 108 are taken
47                                from field Netmag.uncertainty.
48    2007/03/01  2.17    SMZ     P & S information are now on the same
49                                hypoinverse line.
50    2007/04/12  2.18    SMZ     Fixed bug with preferred magnitude.
51                                Ml is now stored in Max S amp. mag.
52    2007/05/02  2.19    SMZ     Fixed bug with database connection.
53                                Outputs quarry blast in hypo summary 81.
54    2007/05/14  2.20    SMZ     Added option -V to specify a list of
55                                event identifiers.
56    2007/05/29  2.21    SMZ     Added new format Hypoinverse summary.
57                                Outputs all event types in hypo summary
58                                field 81.
59    2007/07/02  2.22    SMZ     Added new format Hypoinverse without
60                                shadow cards.
61    2007/08/14  2.23    SMZ     Magnitude type Mh can also be preferred.
62    2007/08/24  2.24    SMZ     Added support for Mg LBL magnitudes.
63    2007/09/07  2.25    SMZ     Added support for Ma magnitudes.
64    2007/09/20  2.26    SMZ     Added leading zero for seconds.
65    2007/09/24  2.27    SMZ     Codas & amplitudes retrieval optimization.
66    2007/10/22  2.28    SMZ     Updated ncedc output format.
67    2008/01/02  2.29    SMZ     Changed option '-V' to '-E'.
68                                Changed option '-E' to '-C'.
69                                Default format is now 'ncedc'.
70                                Default rflag is now 'A,F,H'.
71    2008/01/16  2.30    SMZ     Added new option -c for connection.
72                                Env var DB_CONNECT replaced by
73                                DB_FILE.
74    2008/01/28  2.31    SMZ     Implemented magnitudes & amplitudes
75                                suggestions.
76    2008/02/27  2.32    SMZ     Fixed issue with stand-alone amplitudes
77                                and bug with S pick time.
78    2008/03/05  2.33    SMZ     Amplitudes associated with the preferred
79                                origin are now being displayed.
80    2008/06/16  2.34    SMZ     Program slow when specifying multiple
81                                event Id's; fixed problem.
82    2008/06/25  2.35    SMZ     Limited number of events to 100 for
83                                option '-E'.
84    2008/07/21  2.36    DSN     Fix polygon option.
85    2008/08/11  2.37    SMZ     Latitude & longitude can now be specified
86                                in D:M:S.
87                                If multiple P phases, the one with the
88                                highest weight is chosen.
89    2008/09/12  2.38    SMZ     Corrected bug with large input dates.
90                                Uses new truetime API's (getString and
91                                putString).
92    2008/10/30  2.39    SMZ     Replaced QWHERE with WHERES package.
93    2008/12/01  2.40    SMZ     Added new option '-U' for subsource.
94    2008/12/14  2.41    PAF     Added new -F format, UW2 for A-card output.
95                                PAF = Paul Friberg
96    2009/01/26  2.42    SMZ     Outputs event version & rflag into
97                                columns 163 & 164. Better error reporting.
98    2009/04/23  2.43    SMZ     Added new -F format, FPFIT for fault plane
99                                solutions.
100                                                                        */
101/************************************************************************/
102
103#ifndef lint
104static char sccsid[] = "%W% %G% %U%";
105#endif
106
107#include <stdio.h>
108#include <string.h>
109#include <strings.h>
110#include <stdlib.h>
111#include <math.h>
112#include <ctype.h>
113#include <oraca.h>
114
115#include <qlib2.h>
116
117EXEC ORACLE OPTION (ORACA=YES);
118
119#define         CONNECT_FILE   "/usr/local/bin/connect.properties"
120#define         NOMOREROWS      1403
121#define         VERSION         "2.43"
122#define         info            stdout
123   
124char *syntax[] = {
125"%s version " VERSION " -- Parametric Information Query Engine.",
126"%s [-h] [-c file] [-F format] [-Q] [-I] [-f date -t date | -s interval] [-E list]",
127"\t [-l min,max] [-L min,max] [-d min,max] [-S system] [-A list] [-U list] [-e list] [-R list]",
128"\t [-n min,max] [-g min,max] [-i min,max] [-r min,max] [-T min,max] [-H min,max] [-D min,max]",
129"\t [-q min,max] [-m min,max] [-M list] [-O min,max] [-C lat,lon,min,max] [-P points]",
130"    where:",
131"       -h              Prints out a summary of the available options.",
132"       -c file         Specifies connection file. Overrides DB_FILE variable.",
133"       -F format       Specifies output format.",
134"                               ncedc   --> NCEDC readable text format.",
135"                               summary --> Summary readable text format.",
136"                               detail  --> Detailled readable text format.",
137"                               hypo    --> Hypoinverse format.",
138"                               hyposha --> Hypoinverse format (including shadow cards).",
139"                               hyposum --> Hypoinverse format (Summary cards only).",
140"                               fpfit   --> Fault plane solutions in Fpfit format.",
141"                               uw2     --> University of Washington format (A cards only).",
142"       -Q              Displays location information (qwhere line, only with ncedc, summary & detail formats).",
143"       -I              Suppress headers (only with ncedc, summary & detail formats).",
144"       -f date         From date - ignore data before this date.",
145"       -t date         To date - ignore data from this date on.",
146"                       Date can be in formats:",
147"                               yyyy/mm/dd/hh:mm:ss.ffff",
148"                               yyyy/mm/dd.hh:mm:ss.ffff",
149"                               yyyy/mm/dd,hh:mm:ss.ffff",
150"                               yyyy.ddd,hh:mm:ss.ffff",
151"                               yyyy,ddd,hh:mm:ss.ffff",
152"                       You may leave off any trailing 0 components of the time.",
153"       -s interval     Span interval.  Alternate way of specifying end time.",
154"                       Interval can be an integer followed immediately by",
155"                       S, M, H, d, m or y for seconds, minutes, hours, days, months or years.",
156"       -E evid,...     Comma-delimited list of event identifiers.",
157"       -l min,max      Latitude interval (d[:m[:s]]).",
158"       -L min,max      Longitude interval (d[:m[:s]]) N.B. West is negative.",
159"       -d min,max      Depth interval.",
160"       -S system       Specifies event system.",
161"                               master --> Master system.",
162"                               slave  --> Backup system.",
163"       -A auth,...     Comma-delimited list of authors (NC,BK,...).",
164"       -U subsrc,...   Comma-delimited list of subsources (RT1,RT2,Jiggle,...).",
165"       -e type,...     Comma-delimited list of event types (ex,le,lp,nt,qb,re,sn,st,ts,uk).",
166"       -R review,...   Comma-delimited list of review types:",
167"                               A: Automatic solution.",
168"                               F: Finalized solution.",
169"                               H: Human-reviewed solution.",
170"       -n min,max      Number of observations interval.",
171"       -g min,max      Gap interval.",
172"       -i min,max      Distance to nearest station interval.",
173"       -r min,max      RMS interval.",
174"       -T min,max      Time error interval.",
175"       -H min,max      Horizontal error interval.",
176"       -D min,max      Depth error interval.",
177"       -q min,max      Quality interval.",
178"       -m min,max      Magnitude interval.",
179"       -M mtype,...    Comma-delimited list of magnitude types (a,d,dl,h,l,w).",
180"       -O min,max      Number of magnitude observations interval.",
181"       -C lat,lon,min,max      Events contained between min & max kilometers (annulus)",
182"                               from location specified by lat & lon.",
183"                               Example: -C 37.877,-122.235,0.,10.",
184"       -P Xo,Yo,....,Xn,Yn     Events contained in polygonal area defined by the",
185"                               points (Xo,Yo) ... (Xn,Yn).",
186"                               Example: -P 30.,-122.,40.,-122.,35.,-140.",
187"",
188" Environment variable DB_FILE --> Overrides default (/usr/local/bin/connect.properties) database connection file.",
189NULL };
190
191/************************************************************************/
192
193EXEC SQL INCLUDE sqlca.h;
194
195typedef struct {
196                char    sta[5];                 /* Station code */
197                char    net[3];                 /* Network code */
198                char    seedchan[4];            /* SEED channel name */
199                char    type;                   /* Phase type (P | S) */
200                char    time[11];               /* Phase time */
201                char    seconds[6];             /* Phase seconds */
202                char    hypo_line[256];         /* Hypoinverse line */
203                char    hypo_shadow[256];       /* Hypoinverse shadow line */
204               } Phase;
205
206typedef struct {
207                int     arid;                   /* Arrival Identifier */
208                int     other_arid;             /* Arrival Identifier */
209                int     coid;                   /* Coda Identifier */
210                int     ampid;                  /* Amplitude identifier */
211                char    sta[5];                 /* Station code */
212                char    net[3];                 /* Network code */
213                char    seedchan[4];            /* SEED channel name */
214                char    location[3];            /* SEED location code */
215                char    iphase;                 /* Phase type (P | S) */
216               } PhaseL;
217
218typedef struct {
219                char    hypo_line[256];         /* Hypoinverse line */
220                char    hypo_shadow[256];       /* Hypoinverse shadow line */
221               } Amplitude;
222
223int             nb_phase = 0;
224Phase           phase[1024];
225int             total_phase = 0;
226PhaseL          PhaseList[8192];
227int             nb_amplitude = 0;
228Amplitude       amplitude[1024];
229
230typedef struct {
231                float   x;
232                float   y;
233               } Point;
234
235typedef Point Polygon[16];
236
237
238typedef struct _listhead {
239         char **name;
240         int n;
241        } LISTHEAD;
242
243
244EXEC SQL BEGIN DECLARE SECTION;
245
246char    user_pwd[310];
247char    db_user[100];
248char    db_password[100];
249char    db_name[100];
250
251char    qlocation[256];
252short   qlocation_ind;
253char    lddate[24];
254short   lddate_ind;
255char    eventype[8];
256short   eventype_ind;
257int     evid;
258int     orid;
259int     magid;
260short   magid_ind;
261int     eversion;
262short   eversion_ind;
263char    orflag;
264short   orflag_ind;
265char    ndatetime[33];
266char    npdatetime[33];
267char    nsdatetime[33];
268double  tdatetime;
269short   tdatetime_ind;
270char    ctime_min[32];
271char    ctime_max[32];
272double  time_min;
273double  time_max;
274float   lat;
275short   lat_ind;
276float   lon;
277short   lon_ind;
278float   depth;
279short   depth_ind;
280char    auth[16];
281short   auth_ind;
282char    subsource[9];
283short   subsource_ind;
284float   gap;
285short   gap_ind;
286float   distance;
287short   distance_ind;
288int     ndef;
289short   ndef_ind;
290int     nbs;
291short   nbs_ind;
292int     nbfm;
293short   nbfm_ind;
294int     totalarr;
295short   totalarr_ind;
296float   sdobs;
297short   sdobs_ind;
298float   stime;
299short   stime_ind;
300float   erhor;
301short   erhor_ind;
302float   sdepth;
303short   sdepth_ind;
304float   quality;
305short   quality_ind;
306float   magnitude;
307short   magnitude_ind;
308char    mag_type[7];
309short   mag_type_ind;
310char    magauth[16];
311short   magauth_ind;
312float   maguncertainty;
313short   maguncertainty_ind;
314float   maggap;
315short   maggap_ind;
316float   magdistance;
317short   magdistance_ind;
318int     nsta;
319short   nsta_ind;
320char    locevid[33];
321short   locevid_ind;
322float   delta;
323float   delta_lat;
324float   delta_lon;
325float   delta_max;
326float   lat_min, lat_max;
327float   lon_min, lon_max;
328float   dn, ds;
329Polygon poly;
330int     nb_points;
331int     crossings;
332char    vmodelid[3];
333short   vmodelid_ind;
334char    cmodelid[3];
335short   cmodelid_ind;
336
337double  azilarge;
338short   azilarge_ind;
339double  diplarge;
340short   diplarge_ind;
341double  maglarge;
342short   maglarge_ind;
343double  aziinter;
344short   aziinter_ind;
345double  dipinter;
346short   dipinter_ind;
347double  maginter;
348short   maginter_ind;
349double  azismall;
350short   azismall_ind;
351double  dipsmall;
352short   dipsmall_ind;
353double  magsmall;
354short   magsmall_ind;
355
356int     arid;
357double  arrival_datetime;
358short   arrival_datetime_ind;
359double  p_arrival_datetime;
360double  s_arrival_datetime;
361char    arrival_sta[7];
362short   arrival_sta_ind;
363char    arrival_net[8];
364short   arrival_net_ind;
365char    arrival_auth[16];
366short   arrival_auth_ind;
367char    arrival_subsource[9];
368short   arrival_subsource_ind;
369char    arrival_seedchan[4];
370short   arrival_seedchan_ind;
371char    arrival_channel[4];
372short   arrival_channel_ind;
373char    arrival_channelsrc[9];
374short   arrival_channelsrc_ind;
375char    arrival_location[3];
376short   arrival_location_ind;
377char    arrival_iphase[9];
378short   arrival_iphase_ind;
379char    arrival_qual;
380short   arrival_qual_ind;
381char    arrival_fm[3];
382short   arrival_fm_ind;
383float   arrival_ema;
384short   arrival_ema_ind;
385float   arrival_quality;
386short   arrival_quality_ind;
387float   arrival_deltim;
388short   arrival_deltim_ind;
389
390float   assocaro_delta;
391short   assocaro_delta_ind;
392float   assocaro_seaz;
393short   assocaro_seaz_ind;
394float   assocaro_timeres;
395short   assocaro_timeres_ind;
396float   assocaro_importance;
397short   assocaro_importance_ind;
398float   assocaro_in_wgt;
399short   assocaro_in_wgt_ind;
400float   assocaro_wgt;
401short   assocaro_wgt_ind;
402float   assocaro_ema;
403short   assocaro_ema_ind;
404float   assocaro_sdelay;
405short   assocaro_sdelay_ind;
406
407int     ampid;
408double  amp_datetime;
409short   amp_datetime_ind;
410char    amp_sta[7];
411short   amp_sta_ind;
412char    amp_net[8];
413short   amp_net_ind;
414char    amp_auth[16];
415short   amp_auth_ind;
416char    amp_seedchan[4];
417short   amp_seedchan_ind;
418char    amp_channel[4];
419short   amp_channel_ind;
420char    amp_channelsrc[9];
421short   amp_channelsrc_ind;
422char    amp_location[3];
423short   amp_location_ind;
424float   amp_amplitude;
425short   amp_amplitude_ind;
426char    amp_amptype[9];
427short   amp_amptype_ind;
428char    amp_units[5];
429short   amp_units_ind;
430char    amp_ampmeas;
431short   amp_ampmeas_ind;
432float   amp_per;
433short   amp_per_ind;
434float   amp_quality;
435short   amp_quality_ind;
436float   amp_eramp;
437short   amp_eramp_ind;
438
439float   assocamo_delta;
440short   assocamo_delta_ind;
441float   assocamo_seaz;
442short   assocamo_seaz_ind;
443
444float   assocamm_mag;
445short   assocamm_mag_ind;
446float   assocamm_magres;
447short   assocamm_magres_ind;
448float   assocamm_in_wgt;
449short   assocamm_in_wgt_ind;
450float   assocamm_weight;
451short   assocamm_weight_ind;
452
453int     coid;
454char    cod_sta[7];
455short   cod_sta_ind;
456char    cod_net[8];
457short   cod_net_ind;
458char    cod_auth[16];
459short   cod_auth_ind;
460char    cod_seedchan[4];
461short   cod_seedchan_ind;
462char    cod_channel[4];
463short   cod_channel_ind;
464char    cod_channelsrc[9];
465short   cod_channelsrc_ind;
466char    cod_location[3];
467short   cod_location_ind;
468char    cod_codatype[4];
469short   cod_codatype_ind;
470float   cod_afix;
471short   cod_afix_ind;
472float   cod_afree;
473short   cod_afree_ind;
474float   cod_qfix;
475short   cod_qfix_ind;
476float   cod_qfree;
477short   cod_qfree_ind;
478float   cod_tau;
479short   cod_tau_ind;
480int     cod_nsample;
481short   cod_nsample_ind;
482float   cod_rms;
483short   cod_rms_ind;
484char    cod_durtype[4];
485short   cod_durtype_ind;
486char    cod_iphase[9];
487short   cod_iphase_ind;
488float   cod_eramp;
489short   cod_eramp_ind;
490char    cod_units[5];
491short   cod_units_ind;
492int     cod_time1;
493short   cod_time1_ind;
494int     cod_time2;
495short   cod_time2_ind;
496int     cod_time3;
497short   cod_time3_ind;
498int     cod_time4;
499short   cod_time4_ind;
500int     cod_time5;
501short   cod_time5_ind;
502int     cod_time6;
503short   cod_time6_ind;
504int     cod_amp1;
505short   cod_amp1_ind;
506int     cod_amp2;
507short   cod_amp2_ind;
508int     cod_amp3;
509short   cod_amp3_ind;
510int     cod_amp4;
511short   cod_amp4_ind;
512int     cod_amp5;
513short   cod_amp5_ind;
514int     cod_amp6;
515short   cod_amp6_ind;
516float   cod_quality;
517short   cod_quality_ind;
518
519int     assoccom_magid;
520short   assoccom_magid_ind;
521float   assoccom_mag;
522short   assoccom_mag_ind;
523float   assoccom_in_wgt;
524short   assoccom_in_wgt_ind;
525float   assoccom_weight;
526short   assoccom_weight_ind;
527
528int     mecid;
529int     mec_strike1;
530short   mec_strike1_ind;
531int     mec_dip1;
532short   mec_dip1_ind;
533int     mec_rake1;
534short   mec_rake1_ind;
535float   mec_unstrike1;
536short   mec_unstrike1_ind;
537float   mec_undip1;
538short   mec_undip1_ind;
539float   mec_unrake1;
540short   mec_unrake1_ind;
541int     mec_nsta;
542short   mec_nsta_ind;
543int     mec_pvr;
544short   mec_pvr_ind;
545
546int     pref_ml_magid;
547int     pref_ma_magid;
548
549char    SQL_Command[2048];
550char    minmax_Command[2048];
551char    inpoly_Command[2048];
552
553EXEC SQL END DECLARE SECTION;
554
555EXEC SQL DECLARE sql_stmt    STATEMENT;
556EXEC SQL DECLARE minmax_stmt STATEMENT;
557EXEC SQL DECLARE inpoly_stmt STATEMENT;
558
559/************************************************************************/
560/*  External variables and symbols.                                     */
561/************************************************************************/
562char *cmdname;          /* program name from command line.      */
563
564char ordt[18];          /* Origin Date & Time */
565
566long    SQLCODE;
567
568void    sql_error();    /* Handles unrecoverable errors */
569
570LISTHEAD t_arg;
571LISTHEAD l_arg;
572LISTHEAD L_arg;
573LISTHEAD d_arg;
574LISTHEAD s_arg;
575LISTHEAD ss_arg;
576LISTHEAD e_arg;
577LISTHEAD R_arg;
578LISTHEAD n_arg;
579LISTHEAD g_arg;
580LISTHEAD i_arg;
581LISTHEAD r_arg;
582LISTHEAD T_arg;
583LISTHEAD H_arg;
584LISTHEAD D_arg;
585LISTHEAD q_arg;
586LISTHEAD m_arg;
587LISTHEAD M_arg;
588LISTHEAD O_arg;
589LISTHEAD E_arg;
590LISTHEAD P_arg;
591LISTHEAD V_arg;
592
593LISTHEAD add_to_list (LISTHEAD l, char *clist)
594{
595    char *str = strdup(clist);
596    char *p = str;
597    char *pc;
598    char *e;
599    while (p != NULL && *p != 0) {
600        e = strchr(p,',');
601        if (e != NULL) *e = '\0';
602        if (l.name == NULL) l.name = (char **)malloc((l.n+1) * sizeof(char **));
603        else l.name = (char **)realloc(l.name, (l.n+1) * sizeof(char **));
604        l.name[l.n] = strdup(p);
605        for (pc=l.name[l.n]; *pc; pc++) {
606            /* 1.  Replace "-" with blank (for location code). */
607            /*if (*pc == '-') *pc = ' ';*/
608            /* 2.  Replace "*" with "%" for unix->sql 0-n char wildcard. */
609            if (*pc == '*') *pc = '%';
610            /* 3.  Replace "?" with "_" for unix->sql 1 char wildcard. */
611            if (*pc == '?') *pc = '_';
612        }
613        l.n++;
614        p = (e!=NULL) ? e+1 : NULL;
615    }
616    free(str);
617
618    /*:: Debug
619    {int i;for (i=0;i<l.n;i++) printf ("[%s]", l.name[i]);printf ("\n");}
620    ::*/
621
622    return(l);
623}
624
625/************************************************************************/
626/*  Remove_Blank:                                                       */
627/*                                                                      */
628/*      Removes trailing blanks in a string.                            */
629/*      Returns new string.                                             */
630/************************************************************************/
631char*   Remove_Blank (s)
632char    *s;
633{
634 int    i = 0;
635 char   *p = s + strlen (s);
636 while (--p >= s)
637        if (*p == ' ')
638                *p = '\0';
639        else    break;
640 
641 return (s);
642}
643
644/************************************************************************/
645/*  Output_NCEDC:   Outputs NCEDC readable text.                        */
646/************************************************************************/
647void    Output_NCEDC(char       *qlocation)
648{
649 float  nb_sec;
650 char   cnb_sec[16];
651 char   ncedc_magtype[5];
652 char   ncedc_source[5];
653 
654
655 EXEC SQL WHENEVER SQLERROR DO sql_error();
656
657 /* Converting date and time */
658 nb_sec = tdatetime - floor (tdatetime);
659 sprintf (cnb_sec, "%6.4f", nb_sec);
660 strcpy (cnb_sec, cnb_sec+1);
661 
662 tdatetime = floor (tdatetime);
663 EXEC SQL SELECT TRUETIME.GETSTRING (:tdatetime) INTO :ndatetime FROM DUAL;
664
665 /* Converting magnitude type */
666 if (!strcmp (mag_type, "a"))
667        strcpy (ncedc_magtype, "Mx");
668 else if (!strcmp (mag_type, "d"))
669        strcpy (ncedc_magtype, "Md");
670 else if (!strcmp (mag_type, "dl"))
671        strcpy (ncedc_magtype, "Mdl");
672 else if (!strcmp (mag_type, "h"))
673        strcpy (ncedc_magtype, "Mh");
674 else if (!strcmp (mag_type, "l"))
675        strcpy (ncedc_magtype, "ML");
676 else if (!strcmp (mag_type, "w"))
677        strcpy (ncedc_magtype, "Mw");
678 else   strcpy (ncedc_magtype, "Unk");
679
680 /* Converting source */
681 if (!strcmp (auth, "NC"))
682        strcpy (ncedc_source, "NCSN");
683 else   strcpy (ncedc_source, auth);
684
685 printf ("%.19s%.3s%9.4f %9.4f %6.2f %5.2f %4s %4d%4.0f%5.0f%5.2f %4s %10d %s\n",
686         ndatetime, cnb_sec, lat, lon, depth, magnitude, ncedc_magtype, ndef, gap, distance,
687         sdobs, ncedc_source, evid, qlocation);
688}
689/************************************************************************/
690/*  Output_UW2_Acard:   Outputs UW2 Acard text.                         */
691/************************************************************************/
692void    Output_UW2_Acard(char   *qlocation)
693{
694 int    lat_degrees,lon_degrees;
695 int lat_minutes_100,lon_minutes_100;
696 float  nb_sec;
697 char   cnb_sec[16];
698 char   ncedc_magtype[5];
699 char   ncedc_source[5];
700 char   lat_char, lon_char;
701 char   uw2_time[20];
702
703 EXEC SQL WHENEVER SQLERROR DO sql_error();
704 
705 lat_char ='N';
706 lon_char ='E';
707 /* Converting date and time */
708 nb_sec = tdatetime - floor (tdatetime);
709 sprintf (cnb_sec, "%6.4f", nb_sec);
710 strcpy (cnb_sec, cnb_sec+1);
711
712 if (lon < 0.0) {lon *= -1.0;lon_char='W';}
713 if (lat < 0.0) {lat *= -1.0;lat_char='S';}
714 /* convert lat and lon to deg min */
715 lat_degrees = (int) floor (lat);
716 lon_degrees = (int) floor (lon);
717
718 lat_minutes_100 = (int) floor((lat - lat_degrees)*6000.);
719 lon_minutes_100 = (int) floor((lon - lon_degrees)*6000.);
720 
721 tdatetime = floor (tdatetime);
722 EXEC SQL SELECT TRUETIME.GETSTRING (:tdatetime) INTO :ndatetime FROM DUAL;
723 /* format into UW2 time format */
724 /* printf("ndatetime=%s\n", ndatetime); */
725 strncpy(&uw2_time[0], &ndatetime[0], 4);
726 strncpy(&uw2_time[4], &ndatetime[5], 2);
727 strncpy(&uw2_time[6], &ndatetime[8], 2);
728 strncpy(&uw2_time[8], &ndatetime[11], 2);
729 strncpy(&uw2_time[10], &ndatetime[14], 2);
730 uw2_time[12]=' ';
731 strncpy(&uw2_time[13], &ndatetime[17], 2);
732 strncpy(&uw2_time[15], cnb_sec, 3);
733 uw2_time[18]='\0';
734
735
736 printf ("A %.19s %2d%c%04d %3d%c%04d %5.2f %4.1f %2d/0%02d %3.0f%3.0f%5.2f  0.0?? ??\n",
737         uw2_time, lat_degrees, lat_char, lat_minutes_100,lon_degrees,lon_char,
738         lon_minutes_100, depth, magnitude, ndef, ndef, gap, distance,
739         sdobs);
740}
741
742/************************************************************************/
743/*  Output_Summary:   Outputs summary readable text.                    */
744/************************************************************************/
745void    Output_Summary(char     *qlocation)
746{
747 float  nb_sec;
748 char   cnb_sec[16];
749
750 
751 EXEC SQL WHENEVER SQLERROR DO sql_error();
752
753 /* Converting date and time */
754 nb_sec = tdatetime - floor (tdatetime);
755 sprintf (cnb_sec, "%6.4f", nb_sec);
756 strcpy (cnb_sec, cnb_sec+1);
757 
758 tdatetime = floor (tdatetime);
759 EXEC SQL SELECT TRUETIME.GETSTRING (:tdatetime) INTO :ndatetime FROM DUAL;
760         
761 printf ("%.19s%s %9.4f %9.4f %-6.2f %5.2f %-3s %03d %-6.2f %-6.2f %-5.2f %-4s %-8s %8d %-10s %-2s %s\n",
762         ndatetime, cnb_sec, lat, lon, depth, magnitude, mag_type, nsta, gap, distance,
763         sdobs, auth, subsource, evid, locevid, eventype, qlocation);
764}
765
766/************************************************************************/
767/*  Output_Detail:   Outputs detailled readable text.                   */
768/************************************************************************/
769void    Output_Detail(char      *qlocation)
770{
771 float  nb_sec;
772 char   cnb_sec[16];
773 
774
775 EXEC SQL WHENEVER SQLERROR DO sql_error();
776
777 /* Converting date and time */
778 nb_sec = tdatetime - floor (tdatetime);
779 sprintf (cnb_sec, "%6.4f", nb_sec);
780 strcpy (cnb_sec, cnb_sec+1);
781 
782 tdatetime = floor (tdatetime);
783 EXEC SQL SELECT TRUETIME.GETSTRING (:tdatetime) INTO :ndatetime FROM DUAL;
784         
785 printf ("%.19s%s %9.4f %9.4f %-6.2f %-4s %-8s %-6.2f %-6.2f %-4d %-5.2f %-4.2f %-5.2f %-6.2f %-5.2f %-5.2f %-3s %03d %8d %-10s %-2s %s\n",
786         ndatetime, cnb_sec, lat, lon, depth, auth, subsource, gap, distance, ndef, stime, sdobs,
787         erhor, sdepth, quality, magnitude, mag_type, nsta, evid, locevid, eventype, qlocation);
788}
789
790/************************************************************************/
791/*  Output_Fpfit:   Outputs Fault plane solutions in Fpfit format.      */
792/************************************************************************/
793void    Output_Fpfit(int evid2, int orid2)
794{
795 float  nb_sec;
796 char   cnb_sec[16];
797
798 char   mechypo[142];           /* Mechanism header format */
799
800 char   c_date[15];             /* Event date */
801 char   h_date[15];
802 char   c_sec[8];               /* Seconds */
803 float  sec;
804 char   h_sec[7];
805 char   c_lat[10];              /* Latitude */
806 int    latd;
807 char   h_latd[4];
808 float  latm;
809 char   h_latm[6];
810 char   nlat;
811 char   c_lon[11];              /* Longitude */
812 int    lond;
813 char   h_lond[5];
814 float  lonm;
815 char   h_lonm[6];
816 char   nlon;
817 char   c_depth[9];             /* Depth */
818 float  ndepth;
819 char   h_depth[8];
820 char   c_ndef[5];              /* Number of observations */
821 int    nndef;
822 char   h_ndef[4];
823 char   c_gap[4];               /* Gap */
824 int    ngap;
825 char   h_gap[5];
826 char   c_dist[11];             /* Distance to nearest station */
827 int    ndist;
828 char   h_dist[6];
829 char   c_rms[8];               /* RMS */
830 float  nrms;
831 char   h_rms[6];
832 char   c_horer[8];             /* Horizontal error */
833 float  nhorer;
834 char   h_horer[6];
835 char   c_verer[8];             /* Vertical error */
836 float  nverer;
837 char   h_verer[6];
838 char   c_evid[13];             /* Event identifier */
839 char   c_magtype[3];           /* Magnitude type */
840 char   h_magtype;
841 char   c_mag[6];               /* Magnitude */
842 float  nmag;
843 char   h_mag[6];
844
845 int    z;
846 int    nb_f_mech = 0;
847 int    f_mech[12];
848 char   h_strike1[4];
849 char   h_dip1[3];
850 char   h_rake1[5];
851 char   h_unstrike1[3];
852 char   h_undip1[3];
853 char   h_unrake1[3];
854 char   h_pvr[5];
855 char   h_nsta[4];
856
857
858 EXEC SQL WHENEVER SQLERROR DO sql_error();
859
860 /* Converting date and time */
861 nb_sec = tdatetime - floor (tdatetime);
862 sprintf (cnb_sec, "%6.4f", nb_sec);
863 strcpy (cnb_sec, cnb_sec+1);
864 tdatetime = floor (tdatetime);
865 EXEC SQL SELECT TRUETIME.GETSTRING (:tdatetime) INTO :ndatetime FROM DUAL;
866
867 evid = evid2;
868 orid = orid2;
869
870 /* Checking for FM solutions */
871 EXEC SQL DECLARE cursor_fm CURSOR FOR
872        SELECT mecid
873        FROM Mec
874        WHERE oridin = :orid
875        AND mecalgo = 'FPFIT';
876
877 EXEC SQL OPEN cursor_fm;
878
879 EXEC SQL FETCH cursor_fm INTO :mecid;
880
881 while (sqlca.sqlcode != NOMOREROWS)
882        {
883         f_mech[nb_f_mech++] = mecid;
884         EXEC SQL FETCH cursor_fm INTO :mecid;
885        }
886
887 EXEC SQL CLOSE cursor_fm;
888
889 if (nb_f_mech > 0)
890        {
891         /* Initializing the mechanism header format */
892         memset (mechypo, 32, 142);
893         mechypo[142] = '\0';
894                 
895         /* Reading location information */
896         sprintf (c_date, "%.4s%.2s%.2s %.2s%.2s", ndatetime, ndatetime+5,
897                  ndatetime+8, ndatetime+11, ndatetime+14);
898         Remove_Blank(c_date);
899         sprintf (c_sec, "%.2s%.7s", ndatetime+17, cnb_sec);
900         Remove_Blank(c_sec);
901         sprintf (c_lat, "%9.5f", lat);
902         Remove_Blank(c_lat);
903         sprintf (c_lon, "%10.5f", lon);
904         Remove_Blank(c_lon);
905         sprintf (c_depth, "%8.4f", depth);
906         Remove_Blank(c_depth);
907         sprintf (c_gap, "%3d", (int)gap);
908         Remove_Blank(c_gap);
909         sprintf (c_dist, "%10.4f", distance);
910         Remove_Blank(c_dist);
911         sprintf (c_rms, "%7.4f", sdobs);
912         Remove_Blank(c_rms);
913         sprintf (c_ndef, "%3d", ndef);
914         Remove_Blank(c_ndef);
915         sprintf (c_horer, "%7.4f", erhor);
916         Remove_Blank(c_horer);
917         sprintf (c_verer, "%7.4f", sdepth);
918         Remove_Blank(c_verer);
919         sprintf (c_evid, "%10d", evid);
920         Remove_Blank(c_evid);
921
922         /* Changing types */
923         if (strlen (c_date) == 0)
924                strcpy (h_date, "             ");
925         else strcpy (h_date, c_date);
926               
927         if (strlen (c_sec) == 0)
928                strcpy (h_sec, "      ");
929         else
930                {
931                 sec = atof (c_sec);
932                 sprintf (h_sec, "%6.2f", sec);
933                }
934
935         if (strlen (c_lat) == 0)
936                {
937                 strcpy (h_latd, "   ");
938                 strcpy (h_latm, "     ");
939                 nlat = ' ';
940                }
941         else
942                {
943                 latm = atof (c_lat);
944                 latd = (int)latm;
945
946                 if (latd >= 0)
947                        nlat = ' ';
948                 else   nlat = 'S';
949
950                 latd = abs (latd);
951                 latm = fabs (latm);
952
953                 sprintf (h_latd, "%3.0f", (float)latd);
954
955                 latm -= (float)latd;
956                 latm *= 60.;
957                 sprintf (h_latm, "%5.2f", latm);
958                }
959
960         if (strlen (c_lon) == 0)
961                {
962                 strcpy (h_lond, "    ");
963                 strcpy (h_lonm, "     ");
964                 nlon = ' ';
965                }
966         else
967                {
968                 lonm = atof (c_lon);
969                 lond = (int)lonm;
970
971                 if (lond >= 0)
972                        nlon = 'E';
973                 else   nlon = ' ';
974
975                 lond = abs (lond);
976                 lonm = fabs (lonm);
977
978                 sprintf (h_lond, "%4.0f", (float)lond);
979
980                 lonm -= (float)lond;
981                 lonm *= 60.;
982                 sprintf (h_lonm, "%5.2f", lonm);
983                }
984
985         if (strlen (c_depth) == 0)
986                strcpy (h_depth, "       ");
987         else
988                {
989                 ndepth = atof (c_depth);
990                 sprintf (h_depth, "%7.2f", ndepth);
991                }
992
993         if (strlen (c_ndef) == 0)
994                strcpy (h_ndef, "   ");
995         else
996                {
997                 nndef = atoi (c_ndef);
998                 sprintf (h_ndef, "%3d", nndef);
999                }
1000
1001         if (strlen (c_gap) == 0)
1002                strcpy (h_gap, "     ");
1003         else
1004                {
1005                 ngap = atoi (c_gap);
1006                 sprintf (h_gap, "%4d", ngap);
1007                }
1008
1009         if (strlen (c_dist) == 0)
1010                strcpy (h_dist, "     ");
1011         else
1012                {
1013                 ndist = atoi (c_dist);
1014                 sprintf (h_dist, "%5.1f", (float)ndist);
1015                }
1016
1017         if (strlen (c_rms) == 0)
1018                strcpy (h_rms, "     ");
1019         else
1020                {
1021                 nrms = atof (c_rms);
1022                 sprintf (h_rms, "%5.2f", nrms);
1023                }
1024
1025         if (strlen (c_horer) == 0)
1026                strcpy (h_horer, "     ");
1027         else
1028                {
1029                 nhorer = atof (c_horer);
1030                 sprintf (h_horer, "%5.1f", nhorer);
1031                }
1032
1033         if (strlen (c_verer) == 0)
1034                strcpy (h_verer, "     ");
1035         else
1036                {
1037                 nverer = atof (c_verer);
1038                 sprintf (h_verer, "%5.1f", nverer);
1039                }
1040
1041         /* Building mechanism line */
1042         memcpy (mechypo+0 , h_date, 13);
1043         memcpy (mechypo+13, h_sec, 6);
1044         memcpy (mechypo+19, h_latd, 3);
1045         memcpy (mechypo+22, &nlat, 1);
1046         memcpy (mechypo+23, h_latm, 5);
1047         memcpy (mechypo+28, h_lond, 4);
1048         memcpy (mechypo+32, &nlon, 1);
1049         memcpy (mechypo+33, h_lonm, 5);
1050         memcpy (mechypo+38, h_depth, 7);
1051         memcpy (mechypo+52, h_ndef, 3);
1052         memcpy (mechypo+55, h_gap, 4);
1053         memcpy (mechypo+59, h_dist, 5);
1054         memcpy (mechypo+64, h_rms, 5); 
1055         memcpy (mechypo+69, h_horer, 5);
1056         memcpy (mechypo+74, h_verer, 5);
1057         memcpy (mechypo+131, c_evid, 10);
1058
1059
1060         /* Reading preferred magnitude information */
1061         sprintf (c_mag, "%5.2f", magnitude);
1062         sprintf (c_magtype, "%.2s", mag_type);
1063
1064         /* Changing types */
1065         if (strlen (c_mag) == 0)
1066                strcpy (h_mag, "     ");
1067         else
1068                {
1069                 nmag = atof (c_mag);
1070                 sprintf (h_mag, "%5.2f", nmag);
1071                }
1072 
1073         /* Magnitude type */
1074         switch (c_magtype[0])
1075                {
1076                 case 'a':  h_magtype = 'X'; break;
1077                 case 'l':  h_magtype = 'L'; break;
1078                 case 'B':  h_magtype = 'B'; break;
1079                 case 'm':  h_magtype = 'B'; break;
1080                 case 'w':  h_magtype = 'W'; break;
1081                 case 'd':  if (c_magtype[1] == 'l')
1082                                h_magtype = 'G';
1083                            else h_magtype = 'D';
1084                            break;
1085                 case 'h':  h_magtype = 'H'; break;
1086                 default: h_magtype = ' ';
1087                }
1088
1089         /* Building mechanism line */
1090         memcpy (mechypo+81, &h_magtype, 1);
1091         memcpy (mechypo+47, h_mag, 5);
1092
1093         /* Loop over mechanisms */
1094         for (z=0;z < nb_f_mech; z++)
1095                {
1096                 mecid = f_mech[z];
1097
1098                 /* Retrieving Mec information */
1099                 mec_strike1 = mec_dip1 = mec_rake1 = 0;
1100                 mec_unstrike1 = mec_undip1 = mec_unrake1 = 0.;
1101                 mec_nsta = mec_pvr = 0;
1102
1103                 EXEC SQL SELECT strike1, dip1, rake1, unstrike1, undip1, unrake1, nsta, pvr
1104                          INTO :mec_strike1:mec_strike1_ind, :mec_dip1:mec_dip1_ind, :mec_rake1:mec_rake1_ind,
1105                               :mec_unstrike1:mec_unstrike1_ind, :mec_undip1:mec_undip1_ind, :mec_unrake1:mec_unrake1_ind,
1106                               :mec_nsta:mec_nsta_ind, :mec_pvr:mec_pvr_ind
1107                          FROM Mec
1108                          WHERE mecid = :mecid;
1109
1110                 /* Changing types */
1111                 if (mec_strike1_ind == (-1))
1112                        strcpy (h_strike1, "   ");
1113                 else   sprintf (h_strike1, "%3d", mec_strike1+90);
1114
1115                 if (mec_dip1_ind == (-1))
1116                        strcpy (h_dip1, "  ");
1117                 else   sprintf (h_dip1, "%2d", mec_dip1);
1118
1119                 if (mec_rake1_ind == (-1))
1120                        strcpy (h_rake1, "    ");
1121                 else   sprintf (h_rake1, "%4d", mec_rake1);
1122
1123                 if (mec_unstrike1_ind == (-1))
1124                        strcpy (h_unstrike1, "  ");
1125                 else   sprintf (h_unstrike1, "%2.0f", mec_unstrike1);
1126
1127                 if (mec_undip1_ind == (-1))
1128                        strcpy (h_undip1, "  ");
1129                 else   sprintf (h_undip1, "%2.0f", mec_undip1);
1130
1131                 if (mec_unrake1_ind == (-1))
1132                        strcpy (h_unrake1, "  ");
1133                 else   sprintf (h_unrake1, "%2.0f", mec_unrake1);
1134
1135                 if (mec_pvr_ind == (-1))
1136                        strcpy (h_pvr, "    ");
1137                 else   sprintf (h_pvr, "%4.2f", (float)((100 - mec_pvr)/100.));
1138
1139                 if (mec_nsta_ind == (-1))
1140                        strcpy (h_nsta, "   ");
1141                 else   sprintf (h_nsta, "%3d", mec_nsta);
1142
1143
1144                 /* Building mechanism line */
1145                 memcpy (mechypo+83,  h_strike1, 3);
1146                 memcpy (mechypo+87,  h_dip1, 2);
1147                 memcpy (mechypo+89,  h_rake1, 4);
1148                 memcpy (mechypo+95,  h_pvr, 4);
1149                 memcpy (mechypo+100, h_nsta, 3);
1150                 memcpy (mechypo+121, h_unstrike1, 2);
1151                 memcpy (mechypo+124, h_undip1, 2);
1152                 memcpy (mechypo+127, h_unrake1, 2);
1153
1154                 /* Multiple solutions? */
1155                 if ((z > 0) && (z == (nb_f_mech-1)))
1156                        memcpy (mechypo+130, "*", 1);
1157
1158
1159                 printf ("%s\n", mechypo);
1160                }
1161        }
1162}
1163
1164/************************************************************************/
1165/*  Output_Loc_Hypo:   Outputs location in Hypoinverse format.          */
1166/************************************************************************/
1167void    Output_Loc_Hypo(int evid2, int orid2, int assoccom_magid, int pref_ma_magid, int flag_shadow)
1168{
1169 float  nb_sec;
1170 char   cnb_sec[16];
1171
1172 char   summary[169];           /* Summary header format */
1173 char   sum_shadow[81];         /* Summary shadow format */
1174
1175 char   c_date[15];             /* Event date */
1176 char   h_date[15];
1177 char   c_sec[8];               /* Seconds */
1178 float  sec;
1179 char   h_sec[7];
1180 char   c_lat[10];              /* Latitude */
1181 int    latd;
1182 char   h_latd[4];
1183 float  latm;
1184 char   h_latm[5];
1185 char   nlat;
1186 char   c_lon[11];              /* Longitude */
1187 int    lond;
1188 char   h_lond[5];
1189 float  lonm;
1190 char   h_lonm[5];
1191 char   nlon;
1192 char   c_depth[9];             /* Depth */
1193 float  ndepth;
1194 char   h_depth[6];
1195 char   c_nbobs[5];             /* Number of observations */
1196 int    nnbobs;
1197 char   h_nbobs[4];
1198 char   c_nbs[5];               /* Number of S phases */
1199 int    nnbs;
1200 char   h_nbs[4];
1201 char   c_nbfm[5];              /* Number of first motions */
1202 int    nnbfm;
1203 char   h_nbfm[4];
1204 char   c_totalarr[6];          /* Number of arrivals */
1205 int    ntotalarr;
1206 char   h_totalarr[4];
1207 char   c_gap[4];               /* Gap */
1208 int    ngap;
1209 char   h_gap[4];
1210 char   c_dist[11];             /* Distance to nearest station */
1211 int    ndist;
1212 char   h_dist[4];
1213 char   c_rms[8];               /* RMS */
1214 float  nrms;
1215 char   h_rms[5];
1216 char   c_horer[8];             /* Horizontal error */
1217 float  nhorer;
1218 char   h_horer[5];
1219 char   c_verer[8];             /* Vertical error */
1220 float  nverer;
1221 char   h_verer[5];
1222 char   c_evid[13];             /* Event identifier */
1223 char   c_magtype[3];           /* Magnitude type */
1224 char   h_magtype;
1225 char   c_mag[6];               /* Magnitude */
1226 float  nmag;
1227 char   h_mag[4];
1228 char   c_mad[6];               /* Magnitude uncertainty */
1229 float  nmad;
1230 char   h_mad[4];
1231 char   c_nsta[6];              /* Number of stations for magnitude */
1232 float  nnsta;
1233 char   h_nsta[4];
1234 char   h_nsta2[5];
1235 char   c_azilarge[6];          /* Azimuth of largest error */
1236 float  nazilarge;
1237 char   h_azilarge[4];
1238 char   c_diplarge[5];          /* Dip of largest error */
1239 float  ndiplarge;
1240 char   h_diplarge[3];
1241 char   c_maglarge[8];          /* Size of largest error */
1242 float  nmaglarge;
1243 char   h_maglarge[5];
1244 char   c_aziinter[6];          /* Azimuth of intermediate error */
1245 float  naziinter;
1246 char   h_aziinter[4];
1247 char   c_dipinter[5];          /* Dip of intermediate error */
1248 float  ndipinter;
1249 char   h_dipinter[3];
1250 char   c_maginter[8];          /* Size of intermediate error */
1251 float  nmaginter;
1252 char   h_maginter[5];
1253 char   c_azismall[6];          /* Azimuth of smallest error */
1254 float  nazismall;
1255 char   h_azismall[4];
1256 char   c_dipsmall[5];          /* Dip of smallest error */
1257 float  ndipsmall;
1258 char   h_dipsmall[3];
1259 char   c_magsmall[8];          /* Size of smallest error */
1260 float  nmagsmall;
1261 char   h_magsmall[5];
1262 char   c_vmodelid[3];          /* Velocity model id */
1263 char   c_cmodelid[3];          /* Velocity model domain */
1264 char   c_eventype[3];          /* Event Type */
1265 char   c_version[2];           /* Event Version */
1266 char   c_rflag;                /* Origin Reviewed Flag */
1267
1268
1269 EXEC SQL WHENEVER SQLERROR DO sql_error();
1270
1271 /* Converting date and time */
1272 nb_sec = tdatetime - floor (tdatetime);
1273 sprintf (cnb_sec, "%6.4f", nb_sec);
1274 strcpy (cnb_sec, cnb_sec+1);
1275 tdatetime = floor (tdatetime);
1276 EXEC SQL SELECT TRUETIME.GETSTRING (:tdatetime) INTO :ndatetime FROM DUAL;
1277
1278 /* Retrieving Origin_Error parameters */
1279 evid = evid2;
1280 orid = orid2;
1281 
1282 azilarge = 0.;
1283 diplarge = 0.;
1284 maglarge = 0.;
1285 aziinter = 0.;
1286 dipinter = 0.;
1287 maginter = 0.;
1288 azismall = 0.;
1289 dipsmall = 0.;
1290 magsmall = 0.;
1291 
1292 EXEC SQL SELECT azilarge, diplarge, maglarge,
1293                 aziinter, dipinter, maginter,
1294                 azismall, dipsmall, magsmall
1295        INTO     :azilarge:azilarge_ind, :diplarge:diplarge_ind, :maglarge:maglarge_ind,
1296                 :aziinter:aziinter_ind, :dipinter:dipinter_ind, :maginter:maginter_ind,
1297                 :azismall:azismall_ind, :dipsmall:dipsmall_ind, :magsmall:magsmall_ind
1298        FROM    Origin_Error
1299        WHERE   orid = :orid;
1300 
1301 /* Initializing the summary header format */
1302 memset (summary, 32, 168);
1303 summary[168] = '\0';
1304                 
1305 /* Reading location information */
1306 sprintf (c_date, "%.4s%.2s%.2s%.2s%.2s", ndatetime, ndatetime+5,
1307          ndatetime+8, ndatetime+11, ndatetime+14);
1308 Remove_Blank(c_date);
1309 sprintf (c_sec, "%.2s%.7s", ndatetime+17, cnb_sec);
1310 Remove_Blank(c_sec);
1311 sprintf (c_lat, "%9.5f", lat);
1312 Remove_Blank(c_lat);
1313 sprintf (c_lon, "%10.5f", lon);
1314 Remove_Blank(c_lon);
1315 sprintf (c_depth, "%8.4f", depth);
1316 Remove_Blank(c_depth);
1317 sprintf (c_nbobs, "%4d", ndef);
1318 Remove_Blank(c_nbobs);
1319 sprintf (c_gap, "%3d", (int)gap);
1320 Remove_Blank(c_gap);
1321 sprintf (c_dist, "%10.4f", distance);
1322 Remove_Blank(c_dist);
1323 sprintf (c_rms, "%7.4f", sdobs);
1324 Remove_Blank(c_rms);
1325 sprintf (c_nbs, "%3d", nbs);
1326 Remove_Blank(c_nbs);
1327 sprintf (c_nbfm, "%3d", nbfm);
1328 Remove_Blank(c_nbfm);
1329 sprintf (c_totalarr, "%3d", totalarr);
1330 Remove_Blank(c_totalarr);
1331 sprintf (c_horer, "%7.4f", erhor);
1332 Remove_Blank(c_horer);
1333 sprintf (c_verer, "%7.4f", sdepth);
1334 Remove_Blank(c_verer);
1335 sprintf (c_evid, "%10d", evid);
1336 Remove_Blank(c_evid);
1337 sprintf (c_azilarge, "%5.2f", azilarge);
1338 Remove_Blank(c_azilarge);
1339 sprintf (c_diplarge, "%4.2f", diplarge);
1340 Remove_Blank(c_diplarge);
1341 sprintf (c_maglarge, "%7.5f", maglarge);
1342 Remove_Blank(c_maglarge);
1343 sprintf (c_aziinter, "%5.2f", aziinter);
1344 Remove_Blank(c_aziinter);
1345 sprintf (c_dipinter, "%4.2f", dipinter);
1346 Remove_Blank(c_dipinter);
1347 sprintf (c_maginter, "%7.5f", maginter);
1348 Remove_Blank(c_maginter);
1349 sprintf (c_azismall, "%5.2f", azismall);
1350 Remove_Blank(c_azismall);
1351 sprintf (c_dipsmall, "%4.2f", dipsmall);
1352 Remove_Blank(c_dipsmall);
1353 sprintf (c_magsmall, "%7.5f", magsmall);
1354 Remove_Blank(c_magsmall);
1355 sprintf (c_vmodelid, "%.2s", vmodelid);
1356 Remove_Blank(c_vmodelid);
1357 sprintf (c_cmodelid, "%.2s", cmodelid);
1358 Remove_Blank(c_cmodelid);
1359 if (eversion >= 10)
1360        eversion = 9;
1361 sprintf (c_version, "%1d", eversion);
1362 Remove_Blank(c_version);
1363 c_rflag = orflag;
1364
1365 Remove_Blank(eventype);
1366 if (!strcmp (eventype, "qb"))
1367        strcpy (c_eventype, "Q ");
1368 else if (!strcmp (eventype, "ex"))
1369        strcpy (c_eventype, "R ");
1370 else if (!strcmp (eventype, "nt"))
1371        strcpy (c_eventype, "N ");
1372 else if (!strcmp (eventype, "lp"))
1373        strcpy (c_eventype, "L ");
1374 else   strcpy (c_eventype, "  ");
1375
1376 /* Changing types */
1377 if (strlen (c_date) == 0)
1378        strcpy (h_date, "            ");
1379 else strcpy (h_date, c_date);
1380               
1381 if (strlen (c_sec) == 0)
1382        strcpy (h_sec, "    ");
1383 else
1384        {
1385         sec = atof (c_sec);
1386         sprintf (h_sec, "%04.0f", sec*100.);
1387        }
1388
1389 if (strlen (c_lat) == 0)
1390        {
1391         strcpy (h_latd, "   ");
1392         strcpy (h_latm, "    ");
1393         nlat = ' ';
1394        }
1395 else
1396        {
1397         latm = atof (c_lat);
1398         latd = (int)latm;
1399
1400         if (latd >= 0)
1401                nlat = ' ';
1402         else   nlat = 'S';
1403
1404         latd = abs (latd);
1405         latm = fabs (latm);
1406
1407         sprintf (h_latd, "%2.0f", (float)latd);
1408
1409         latm -= (float)latd;
1410         latm *= 60.;
1411         sprintf (h_latm, "%4.0f", latm*100.);
1412        }
1413
1414 if (strlen (c_lon) == 0)
1415        {
1416         strcpy (h_lond, "    ");
1417         strcpy (h_lonm, "    ");
1418         nlon = ' ';
1419        }
1420 else
1421        {
1422         lonm = atof (c_lon);
1423         lond = (int)lonm;
1424
1425         if (lond >= 0)
1426                nlon = 'E';
1427         else   nlon = ' ';
1428
1429         lond = abs (lond);
1430         lonm = fabs (lonm);
1431
1432         sprintf (h_lond, "%3.0f", (float)lond);
1433
1434         lonm -= (float)lond;
1435         lonm *= 60.;
1436         sprintf (h_lonm, "%4.0f", lonm*100.);
1437        }
1438
1439 if (strlen (c_depth) == 0)
1440        strcpy (h_depth, "     ");
1441 else
1442        {
1443         ndepth = atof (c_depth);
1444         sprintf (h_depth, "%5.0f", ndepth*100.);
1445        }
1446
1447 if (strlen (c_nbobs) == 0)
1448        strcpy (h_nbobs, "   ");
1449 else
1450        {
1451         nnbobs = atoi (c_nbobs);
1452         sprintf (h_nbobs, "%3d", nnbobs);
1453        }
1454
1455 if (strlen (c_gap) == 0)
1456        strcpy (h_gap, "   ");
1457 else
1458        {
1459         ngap = atoi (c_gap);
1460         sprintf (h_gap, "%3d", ngap);
1461        }
1462
1463 if (strlen (c_dist) == 0)
1464        strcpy (h_dist, "   ");
1465 else
1466        {
1467         ndist = atoi (c_dist);
1468         sprintf (h_dist, "%3.0f", (float)ndist);
1469        }
1470
1471 if (strlen (c_rms) == 0)
1472        strcpy (h_rms, "    ");
1473 else
1474        {
1475         nrms = atof (c_rms);
1476         sprintf (h_rms, "%4.0f", nrms*100.);
1477        }
1478
1479 if (strlen (c_nbs) == 0)
1480        strcpy (h_nbs, "   ");
1481 else
1482        {
1483         nnbs = atoi (c_nbs);
1484         sprintf (h_nbs, "%3d", nnbs);
1485        }
1486
1487 if (strlen (c_nbfm) == 0)
1488        strcpy (h_nbfm, "   ");
1489 else
1490        {
1491         nnbfm = atoi (c_nbfm);
1492         sprintf (h_nbfm, "%3d", nnbfm);
1493        }
1494
1495 if (strlen (c_totalarr) == 0)
1496        strcpy (h_totalarr, "   ");
1497 else
1498        {
1499         ntotalarr = atoi (c_totalarr);
1500         sprintf (h_totalarr, "%3d", ntotalarr);
1501        }
1502
1503 if (strlen (c_horer) == 0)
1504        strcpy (h_horer, "    ");
1505 else
1506        {
1507         nhorer = atof (c_horer);
1508         sprintf (h_horer, "%4.0f", nhorer*100.);
1509        }
1510
1511 if (strlen (c_verer) == 0)
1512        strcpy (h_verer, "    ");
1513 else
1514        {
1515         nverer = atof (c_verer);
1516         sprintf (h_verer, "%4.0f", nverer*100.);
1517        }
1518
1519 if (strlen (c_maglarge) == 0)
1520        strcpy (h_maglarge, "    ");
1521 else
1522        {
1523         nmaglarge = atof (c_maglarge);
1524         sprintf (h_maglarge, "%4.0f", nmaglarge*100.);
1525        }
1526
1527 if (strlen (c_maginter) == 0)
1528        strcpy (h_maginter, "    ");
1529 else
1530        {
1531         nmaginter = atof (c_maginter);
1532         sprintf (h_maginter, "%4.0f", nmaginter*100.);
1533        }
1534
1535 if (strlen (c_magsmall) == 0)
1536        strcpy (h_magsmall, "    ");
1537 else
1538        {
1539         nmagsmall = atof (c_magsmall);
1540         sprintf (h_magsmall, "%4.0f", nmagsmall*100.);
1541        }
1542
1543 if (strlen (c_azilarge) == 0)
1544        strcpy (h_azilarge, "   ");
1545 else
1546        {
1547         nazilarge = atoi (c_azilarge);
1548         sprintf (h_azilarge, "%3.0f", azilarge);
1549        }
1550       
1551 if (strlen (c_aziinter) == 0)
1552        strcpy (h_aziinter, "   ");
1553 else
1554        {
1555         naziinter = atoi (c_aziinter);
1556         sprintf (h_aziinter, "%3.0f", aziinter);
1557        }
1558
1559 if (strlen (c_diplarge) == 0)
1560        strcpy (h_diplarge, "  ");
1561 else
1562        {
1563         ndiplarge = atoi (c_diplarge);
1564         sprintf (h_diplarge, "%2.0f", diplarge);
1565        }
1566
1567 if (strlen (c_dipinter) == 0)
1568        strcpy (h_dipinter, "  ");
1569 else
1570        {
1571         ndipinter = atoi (c_dipinter);
1572         sprintf (h_dipinter, "%2.0f", dipinter);
1573        }
1574
1575 if (strlen (c_vmodelid) == 0)
1576        strcpy (c_vmodelid, "  ");
1577
1578 if (strlen (c_cmodelid) == 0)
1579        strcpy (c_cmodelid, "  ");
1580
1581 /* Building hypoinverse summary line */
1582 memcpy (summary+0 , h_date, 12);
1583 memcpy (summary+12, h_sec, 4);
1584 memcpy (summary+16, h_latd, 2);
1585 memcpy (summary+18, &nlat, 1);
1586 memcpy (summary+19, h_latm, 4);
1587 memcpy (summary+23, h_lond, 3);
1588 memcpy (summary+26, &nlon, 1);
1589 memcpy (summary+27, h_lonm, 4);
1590 memcpy (summary+31, h_depth, 5);
1591 memcpy (summary+39, h_nbobs, 3);
1592 memcpy (summary+42, h_gap, 3);
1593 memcpy (summary+45, h_dist, 3);
1594 memcpy (summary+48, h_rms, 4);
1595 memcpy (summary+52, h_azilarge, 3);
1596 memcpy (summary+55, h_diplarge, 2);
1597 memcpy (summary+57, h_maglarge, 4);
1598 memcpy (summary+61, h_aziinter, 3);
1599 memcpy (summary+64, h_dipinter, 2);
1600 memcpy (summary+66, h_maginter, 4);
1601 memcpy (summary+76, h_magsmall, 4);
1602 memcpy (summary+80, c_eventype, 2);
1603 memcpy (summary+82, h_nbs, 3);
1604 memcpy (summary+85, h_horer, 4);
1605 memcpy (summary+89, h_verer, 4);
1606 memcpy (summary+93, h_nbfm, 3);
1607 memcpy (summary+118, h_totalarr, 3);
1608 memcpy (summary+136, c_evid, 10);
1609 memcpy (summary+162, c_version, 1);
1610 memcpy (summary+163, &c_rflag, 1);
1611 memcpy (summary+164, c_cmodelid, 2);
1612 memcpy (summary+166, c_vmodelid, 2);
1613
1614 /* For Hypoinverse workaround (standalone amplitudes) */
1615 memcpy (ordt, h_date, 12);
1616 ordt[12] = ' ';
1617 memcpy (ordt+13, h_sec, 4);
1618         
1619 /* Reading preferred magnitude information */
1620 sprintf (c_mag, "%5.2f", magnitude);
1621 sprintf (c_magtype, "%.2s", mag_type);
1622 sprintf (c_nsta, "%3.1f", (float)nsta);
1623
1624 /* Changing types */
1625 if (strlen (c_mag) == 0)
1626        strcpy (h_mag, "   ");
1627 else
1628        {
1629         nmag = atof (c_mag);
1630         sprintf (h_mag, "%3.0f", nmag*100.);
1631        }
1632 
1633 if (strlen (c_nsta) == 0)
1634         strcpy (h_nsta2, "    ");
1635 else
1636        {
1637         nnsta = atof (c_nsta);
1638         sprintf (h_nsta2, "%4.0f", nnsta*10.);
1639        }
1640
1641 /* Magnitude type */
1642 switch (c_magtype[0])
1643        {
1644         case 'a':  h_magtype = 'X'; break;
1645         case 'l':  h_magtype = 'L'; break;
1646         case 'B':  h_magtype = 'B'; break;
1647         case 'm':  h_magtype = 'B'; break;
1648         case 'w':  h_magtype = 'W'; break;
1649         case 'd':  if (c_magtype[1] == 'l')
1650                        h_magtype = 'G';
1651                    else h_magtype = 'D';
1652                    break;
1653         case 'h':  h_magtype = 'H'; break;
1654         default: h_magtype = ' ';
1655        }
1656
1657 /* Building hypoinverse summary line */
1658 
1659 /* External Magnitude */
1660 if (strlen (c_nsta) == 0)
1661        strcpy (h_nsta, "   ");
1662 else
1663        {
1664         if (nnsta > 99)
1665                sprintf (h_nsta, "999");
1666         else   sprintf (h_nsta, "%3.0f", nnsta*10.);
1667        }
1668
1669 memcpy (summary+122, &h_magtype, 1);
1670 memcpy (summary+123, h_mag, 3);
1671 memcpy (summary+126, h_nsta, 3);
1672 
1673 /* Preferred Magnitude */
1674 memcpy (summary+146, &h_magtype, 1);
1675 memcpy (summary+147, h_mag, 3);
1676 memcpy (summary+150, h_nsta2, 4);
1677
1678 /* Reading Max S Amplitude magnitude information */
1679 if (pref_ma_magid != 0)
1680        {
1681         magnitude      = 0.;
1682         nsta           = 0.;
1683         maguncertainty = 0.;
1684
1685         EXEC SQL SELECT magnitude, nsta, uncertainty
1686                  INTO    :magnitude:magnitude_ind, :nsta:nsta_ind,
1687                          :maguncertainty:maguncertainty_ind
1688                  FROM    Netmag
1689                  WHERE   magid = :pref_ma_magid;
1690
1691         sprintf (c_mag, "%5.2f", magnitude);
1692         sprintf (c_nsta, "%3.1f", (float)nsta);
1693         sprintf (c_mad, "%3.2f", maguncertainty);
1694
1695         /* Changing types */
1696         if (strlen (c_mag) == 0)
1697                {
1698                 strcpy (h_mag, "   ");
1699                 h_magtype = ' ';
1700                }
1701         else
1702                {
1703                 nmag = atof (c_mag);
1704                 sprintf (h_mag, "%3.0f", nmag*100.);
1705                 h_magtype = 'X';
1706                }
1707
1708         if (strlen (c_mad) == 0)
1709                strcpy (h_mad, "   ");
1710         else
1711                {
1712                 nmad = atof (c_mad);
1713                 sprintf (h_mad, "%3.0f", nmad*100.);
1714                }
1715
1716         if (strlen (c_nsta) == 0)
1717                {
1718                 strcpy (h_nsta2, "    ");
1719                }
1720         else
1721                {
1722                 nnsta = atof (c_nsta);
1723                 sprintf (h_nsta2, "%4.0f", nnsta*10.);
1724                }
1725
1726         /* Building hypoinverse summary line */
1727         memcpy (summary+36, h_mag, 3);
1728         memcpy (summary+96, h_nsta2, 4);
1729         memcpy (summary+104, h_mad, 3);
1730         memcpy (summary+121, &h_magtype, 1);
1731        }
1732
1733 /* Reading Coda magnitude information */
1734 if (assoccom_magid != 0)
1735        {
1736         magnitude      = 0.;
1737         nsta           = 0.;
1738         maguncertainty = 0.;
1739         
1740         EXEC SQL SELECT magnitude, nsta, uncertainty
1741                INTO    :magnitude:magnitude_ind, :nsta:nsta_ind,
1742                        :maguncertainty:maguncertainty_ind
1743                FROM    Netmag
1744                WHERE   magid = :assoccom_magid;
1745       
1746         sprintf (c_mag, "%5.2f", magnitude);
1747         sprintf (c_nsta, "%3.1f", (float)nsta);
1748         sprintf (c_mad, "%3.2f", maguncertainty);
1749
1750         /* Changing types */
1751         if (strlen (c_mag) == 0)
1752                {
1753                 strcpy (h_mag, "   ");
1754                 h_magtype = ' ';
1755                }
1756         else
1757                {
1758                 nmag = atof (c_mag);
1759                 sprintf (h_mag, "%3.0f", nmag*100.);
1760                 h_magtype = 'D';
1761                }
1762
1763         if (strlen (c_mad) == 0)
1764                strcpy (h_mad, "   ");
1765         else
1766                {
1767                 nmad = atof (c_mad);
1768                 sprintf (h_mad, "%3.0f", nmad*100.);
1769                }
1770 
1771         if (strlen (c_nsta) == 0)
1772                {
1773                 strcpy (h_nsta2, "    ");
1774                }
1775         else
1776                {
1777                 nnsta = atof (c_nsta);
1778                 sprintf (h_nsta2, "%4.0f", nnsta*10.);
1779                }
1780
1781         /* Building hypoinverse summary line */
1782         memcpy (summary+70, h_mag, 3);
1783         memcpy (summary+100, h_nsta2, 4);
1784         memcpy (summary+107, h_mad, 3);
1785         memcpy (summary+117, &h_magtype, 1);
1786        }
1787
1788 /* Building hypoinverse summary shadow */
1789 memset (sum_shadow, 32, 80);
1790 sum_shadow[80] = '\0';
1791 sum_shadow[0] = '$';
1792 sum_shadow[1] = '1';
1793
1794 memcpy (sum_shadow+2, h_date, 12);
1795 sprintf (h_sec, "%6.0f", sec*1000.);
1796 memcpy (sum_shadow+14, h_sec, 6);
1797       
1798 printf ("%s", summary);
1799
1800 if (flag_shadow == 1)
1801        printf ("\n%s", sum_shadow);
1802}
1803
1804/*********************************************************************************/
1805/*  Output_Pic_Hypo:   Outputs phases, codas & amplitudes in Hypoinverse format. */
1806/*********************************************************************************/
1807void    Output_Pic_Hypo(int evid2, int orid2, int assoccom_magid, int pref_ml_magid, int flag_shadow)
1808{
1809 float  nb_sec;
1810 char   cnb_sec[16];
1811
1812 int    Flag_Phase = 0;         /* Flag for phase */
1813 int    Flag_S = 0;
1814 int    z = 0, k = 0;
1815 
1816 char   station[121];           /* Station archive format */
1817 char   sta_shadow[96];         /* Station shadow format */
1818
1819 char   c_date[15];             /* Phase time */
1820 char   h_date[15];
1821 char   c_sec[8];               /* Seconds */
1822 float  sec;
1823 char   h_sec[7];
1824 char   c_staname[7];           /* Station name */
1825 char   h_staname[6];
1826 char   c_netcode[3];           /* Network code */
1827 char   h_netcode[3];
1828 char   c_location[3];          /* Location code */
1829 char   h_location[3];
1830 char   c_source[5];            /* Source Id */
1831 char   h_source[5];
1832 char   c_seed[4];              /* SEED stream */
1833 char   h_seed[4];
1834 char   c_onset;                /* Phase onset */
1835 char   h_onset[3];
1836 char   motion;                 /* First motion */
1837 int    wgt;                    /* Weight code */
1838 char   h_wgt;
1839 char   c_wgt[8];
1840 char   ptype;                  /* Phase type */
1841 char   ptype2;                 /* Phase type */
1842 char   c_ampmeasure;           /* Amplitude measure */
1843 char   c_amp[8];               /* Amplitude */
1844 float  amp;
1845 char   h_amp[8];
1846 char   c_epi[11];              /* Epicentral distance */
1847 float  epi;
1848 char   h_epi[5];
1849 char   c_period[8];            /* Period */
1850 float  period;
1851 char   h_period[4];
1852 char   c_azi[4];               /* Azimuth */
1853 int    azi;
1854 char   h_azi[4];
1855 char   c_angle[4];             /* Emergence angle */
1856 int    angle;
1857 char   h_angle[4];
1858 char   c_ttres[8];             /* Travel time residual */
1859 float  ttres;
1860 char   h_ttres[5];
1861 char   c_uwt[8];               /* Weight actually used */
1862 float  uwt;
1863 char   h_uwt[4];
1864 char   c_importance[8];        /* Importance of observation */
1865 float  importance;
1866 char   h_importance[5];
1867 char   c_sdelay[8];            /* Arrival delay time */
1868 float  sdelay;
1869 char   h_sdelay[5];
1870 int    assoc_wgt;              /* Weight code */
1871 char   h_assoc_wgt;
1872 char   c_assoc_wgt[8];
1873 char   c_assoc_mag[8];         /* Coda magnitude */
1874 float  assoc_mag;
1875 char   h_assoc_mag[4];
1876 char   c_cod_tau[8];           /* Coda duration */
1877 int    ncod_tau;
1878 char   h_cod_tau[5];
1879 char   h_cod_afix[6];          /* AFIX */
1880 char   h_cod_qfix[6];          /* QFIX */
1881 char   h_cod_afree[6];         /* AFREE */
1882 char   h_cod_qfree[6];         /* QFREE */
1883 char   h_cod_rms[6];           /* RMS */
1884 char   c_cod_time1[11];        /* Time1 */
1885 int    ncod_time1;
1886 char   h_cod_time1[4];
1887 char   c_cod_amp1[11];         /* Amp1 */
1888 int    ncod_amp1;
1889 char   h_cod_amp1[5];
1890 char   c_cod_time2[11];        /* Time2 */
1891 int    ncod_time2;
1892 char   h_cod_time2[4];
1893 char   c_cod_amp2[11];         /* Amp2 */
1894 int    ncod_amp2;
1895 char   h_cod_amp2[5];
1896 char   c_cod_time3[11];        /* Time3 */
1897 int    ncod_time3;
1898 char   h_cod_time3[4];
1899 char   c_cod_amp3[11];         /* Amp3 */
1900 int    ncod_amp3;
1901 char   h_cod_amp3[5];
1902 char   c_cod_time4[11];        /* Time4 */
1903 int    ncod_time4;
1904 char   h_cod_time4[4];
1905 char   c_cod_amp4[11];         /* Amp4 */
1906 int    ncod_amp4;
1907 char   h_cod_amp4[5];
1908 char   c_cod_time5[11];        /* Time5 */
1909 int    ncod_time5;
1910 char   h_cod_time5[4];
1911 char   c_cod_amp5[11];         /* Amp5 */
1912 int    ncod_amp5;
1913 char   h_cod_amp5[5];
1914 char   c_cod_time6[11];        /* Time6 */
1915 int    ncod_time6;
1916 char   h_cod_time6[4];
1917 char   c_cod_amp6[11];         /* Amp6 */
1918 int    ncod_amp6;
1919 char   h_cod_amp6[5];
1920 char   c_cod_nsample[7];       /* Number of windows */
1921 int    ncod_nsample;
1922 char   h_cod_nsample[4];
1923 char   c_assocam_wgt[5];       /* Magnitude weight */
1924 int    assocam_wgt;
1925 char   h_assocam_wgt;
1926 char   c_assocam_mag[6];       /* Station magnitude */
1927 float  assocam_mag;
1928 char   h_assocam_mag[4];
1929 char   c_amp_per[11];          /* Amplitude period */
1930 float  ampli_per;
1931 char   h_amp_per[4];
1932 char   c_amp_amplitude[8];     /* Amplitude value */
1933 float  ampli_amplitude;
1934 char   h_amp_amplitude[8];
1935 char   h_amp_ampmeas[3];       /* Amplitude measure */
1936
1937
1938 EXEC SQL WHENEVER SQLERROR DO sql_error();
1939
1940 evid = evid2;
1941 orid = orid2;
1942 
1943 nb_phase = 0;
1944 total_phase = 0;
1945
1946   /********************************/
1947  /* Retrieving Phase Information */
1948 /********************************/
1949 EXEC SQL DECLARE cursor_pich CURSOR FOR
1950 SELECT a.arid, a.net, a.sta, a.seedchan, a.location, a.iphase, a.quality
1951 FROM   Arrival a, AssocArO o
1952 WHERE  o.orid = :orid AND o.arid = a.arid
1953 ORDER BY a.net, a.sta, a.seedchan, a.location, a.iphase, (1 - a.quality);
1954
1955 EXEC SQL OPEN cursor_pich;
1956
1957 EXEC SQL FETCH cursor_pich INTO :arid,
1958                                 :arrival_net:arrival_net_ind,
1959                                 :arrival_sta:arrival_sta_ind,
1960                                 :arrival_seedchan:arrival_seedchan_ind,
1961                                 :arrival_location:arrival_location_ind,
1962                                 :arrival_iphase:arrival_iphase_ind,
1963                                 :arrival_quality:arrival_quality_ind;
1964
1965 while (sqlca.sqlcode != NOMOREROWS)
1966        {       
1967         Remove_Blank (arrival_net);
1968         Remove_Blank (arrival_sta);
1969         Remove_Blank (arrival_seedchan);
1970         Remove_Blank (arrival_location);
1971         if (arrival_location_ind == (-1))
1972                strcpy (arrival_location, "--");
1973         else
1974                {
1975                 if ((!strcmp (arrival_location, "")) || (!strcmp (arrival_location, "  ")))
1976                        strcpy (arrival_location, "--");
1977                }
1978         Remove_Blank (arrival_iphase);
1979         
1980         Flag_S = 0;
1981         
1982         /* Is there already a phase for the same SNCL? */
1983         for (k=0; k < total_phase; k++)
1984                {
1985                 if (
1986                        !strcmp (PhaseList[k].net, arrival_net) &&
1987                        !strcmp (PhaseList[k].sta, arrival_sta) &&
1988                        !strcmp (PhaseList[k].seedchan, arrival_seedchan) &&
1989                        !strcmp (PhaseList[k].location, arrival_location)
1990                    )
1991                            {
1992                             if (arrival_iphase[0] == 'S')
1993                                PhaseList[k].other_arid = arid;
1994                               
1995                             Flag_S = 1;
1996                             break;
1997                            }
1998                 }
1999                 
2000         if (Flag_S == 0)
2001                {
2002                 PhaseList[total_phase].arid = arid;
2003                 PhaseList[total_phase].other_arid = 0;
2004                 PhaseList[total_phase].coid = 0;
2005                 PhaseList[total_phase].ampid = 0;
2006                 strcpy (PhaseList[total_phase].net, arrival_net);
2007                 strcpy (PhaseList[total_phase].sta, arrival_sta);
2008                 strcpy (PhaseList[total_phase].seedchan, arrival_seedchan);
2009                 strcpy (PhaseList[total_phase].location, arrival_location);
2010                 PhaseList[total_phase].iphase = arrival_iphase[0];
2011
2012                 total_phase++;
2013                }
2014         
2015         EXEC SQL FETCH cursor_pich INTO :arid,
2016                                 :arrival_net:arrival_net_ind,
2017                                 :arrival_sta:arrival_sta_ind,
2018                                 :arrival_seedchan:arrival_seedchan_ind,
2019                                 :arrival_location:arrival_location_ind,
2020                                 :arrival_iphase:arrival_iphase_ind,
2021                                 :arrival_quality:arrival_quality_ind;
2022        }
2023
2024 EXEC SQL CLOSE cursor_pich;
2025 
2026   /*******************************/
2027  /* Retrieving Coda Information */
2028 /*******************************/
2029 EXEC SQL DECLARE cursor_coch CURSOR FOR
2030 SELECT coid, net, sta, seedchan, location
2031 FROM   Coda
2032 WHERE  coid IN (SELECT coid
2033                 FROM AssocCoM
2034                 WHERE magid = :assoccom_magid);
2035                                 
2036 EXEC SQL OPEN cursor_coch;
2037
2038 EXEC SQL FETCH cursor_coch INTO :coid,
2039                                 :arrival_net:arrival_net_ind,
2040                                 :arrival_sta:arrival_sta_ind,
2041                                 :arrival_seedchan:arrival_seedchan_ind,
2042                                 :arrival_location:arrival_location_ind;
2043
2044 while (sqlca.sqlcode != NOMOREROWS)
2045        {       
2046         Remove_Blank (arrival_net);
2047         Remove_Blank (arrival_sta);
2048         Remove_Blank (arrival_seedchan);
2049         Remove_Blank (arrival_location);
2050         if (arrival_location_ind == (-1))
2051                strcpy (arrival_location, "--");
2052         else
2053                {
2054                 if ((!strcmp (arrival_location, "")) || (!strcmp (arrival_location, "  ")))
2055                        strcpy (arrival_location, "--");
2056                }
2057                 
2058         /* Is there already a phase for the same SNCL? */
2059         for (k=0; k < total_phase; k++)
2060                {
2061                 if (
2062                        !strcmp (PhaseList[k].net, arrival_net) &&
2063                        !strcmp (PhaseList[k].sta, arrival_sta) &&
2064                        !strcmp (PhaseList[k].seedchan, arrival_seedchan) &&
2065                        !strcmp (PhaseList[k].location, arrival_location)
2066                    )
2067                            {
2068                             PhaseList[k].coid = coid;
2069                             break;
2070                            }
2071                 }
2072         
2073         EXEC SQL FETCH cursor_coch INTO :coid,
2074                                         :arrival_net:arrival_net_ind,
2075                                         :arrival_sta:arrival_sta_ind,
2076                                         :arrival_seedchan:arrival_seedchan_ind,
2077                                         :arrival_location:arrival_location_ind;
2078        }
2079
2080 EXEC SQL CLOSE cursor_coch;
2081
2082   /************************************/
2083  /* Retrieving Amplitude Information */
2084 /************************************/
2085 EXEC SQL DECLARE cursor_amch CURSOR FOR
2086 SELECT ampid, net, sta, seedchan, location
2087 FROM   Amp
2088 WHERE  ampid IN (SELECT ampid
2089                  FROM AssocAmO
2090                  WHERE orid = :orid)
2091 AND    amptype = 'WAS';
2092                                 
2093 EXEC SQL OPEN cursor_amch;
2094
2095 EXEC SQL FETCH cursor_amch INTO :ampid,
2096                                 :arrival_net:arrival_net_ind,
2097                                 :arrival_sta:arrival_sta_ind,
2098                                 :arrival_seedchan:arrival_seedchan_ind,
2099                                 :arrival_location:arrival_location_ind;
2100
2101 while (sqlca.sqlcode != NOMOREROWS)
2102        {       
2103         Remove_Blank (arrival_net);
2104         Remove_Blank (arrival_sta);
2105         Remove_Blank (arrival_seedchan);
2106         Remove_Blank (arrival_location);
2107         if (arrival_location_ind == (-1))
2108                strcpy (arrival_location, "--");
2109         else
2110                {
2111                 if ((!strcmp (arrival_location, "")) || (!strcmp (arrival_location, "  ")))
2112                        strcpy (arrival_location, "--");
2113                }
2114                 
2115         /* Is there already a phase for the same SNCL? */
2116         Flag_S = 0;
2117         for (k=0; k < total_phase; k++)
2118                {
2119                 if (
2120                        !strcmp (PhaseList[k].net, arrival_net) &&
2121                        !strcmp (PhaseList[k].sta, arrival_sta) &&
2122                        !strcmp (PhaseList[k].seedchan, arrival_seedchan) &&
2123                        !strcmp (PhaseList[k].location, arrival_location)
2124                    )
2125                            {
2126                             Flag_S = 1;
2127                             PhaseList[k].ampid = ampid;
2128                             break;
2129                            }
2130                 }
2131
2132         if (Flag_S == 0)
2133                {
2134                 PhaseList[total_phase].arid = 0;
2135                 PhaseList[total_phase].other_arid = 0;
2136                 PhaseList[total_phase].coid = 0;
2137                 PhaseList[total_phase].ampid = ampid;
2138                 strcpy (PhaseList[total_phase].net, arrival_net);
2139                 strcpy (PhaseList[total_phase].sta, arrival_sta);
2140                 strcpy (PhaseList[total_phase].seedchan, arrival_seedchan);
2141                 strcpy (PhaseList[total_phase].location, arrival_location);
2142                 PhaseList[total_phase].iphase = ' ';
2143
2144                 total_phase++;
2145                }
2146         
2147         EXEC SQL FETCH cursor_amch INTO :ampid,
2148                                         :arrival_net:arrival_net_ind,
2149                                         :arrival_sta:arrival_sta_ind,
2150                                         :arrival_seedchan:arrival_seedchan_ind,
2151                                         :arrival_location:arrival_location_ind;
2152        }
2153
2154 EXEC SQL CLOSE cursor_amch;
2155
2156
2157   /********************************/
2158  /* Generating phase information */
2159 /********************************/
2160 for (k=0; k < total_phase; k++)
2161        {
2162         /*printf ("\n[%d][%s][%s][%s][%s][%c][%d]",
2163                 PhaseList[k].arid,
2164                 PhaseList[k].net, PhaseList[k].sta,
2165                 PhaseList[k].seedchan, PhaseList[k].location,
2166                 PhaseList[k].iphase, PhaseList[k].other_arid);*/
2167                 
2168         arid = PhaseList[k].arid;
2169         
2170         arrival_datetime    = 0.;
2171         arrival_ema         = 0.;
2172         arrival_quality     = 0.;
2173         arrival_deltim      = 0.;
2174         assocaro_delta      = 0.;
2175         assocaro_seaz       = 0.;
2176         assocaro_timeres    = 0.;
2177         assocaro_importance = 0.;
2178         assocaro_in_wgt     = 0.;
2179         assocaro_wgt        = 0.;
2180         assocaro_ema        = 0.;
2181         assocaro_sdelay     = 0.;
2182
2183         strcpy (arrival_iphase, "  ");
2184         
2185         EXEC SQL SELECT        datetime, sta, net, auth, subsource, seedchan, channel, channelsrc,
2186                                location, iphase, qual, fm, ema, quality, deltim
2187         INTO                   :arrival_datetime:arrival_datetime_ind, :arrival_sta:arrival_sta_ind,
2188                                :arrival_net:arrival_net_ind, :arrival_auth:arrival_auth_ind,
2189                                :arrival_subsource:arrival_subsource_ind,
2190                                :arrival_seedchan:arrival_seedchan_ind,
2191                                :arrival_channel:arrival_channel_ind,
2192                                :arrival_channelsrc:arrival_channelsrc_ind,
2193                                :arrival_location:arrival_location_ind,
2194                                :arrival_iphase:arrival_iphase_ind,
2195                                :arrival_qual:arrival_qual_ind, :arrival_fm:arrival_fm_ind,
2196                                :arrival_ema:arrival_ema_ind, :arrival_quality:arrival_quality_ind,
2197                                :arrival_deltim:arrival_deltim_ind
2198         FROM                   arrival
2199         WHERE                  arid = :arid;
2200
2201         EXEC SQL SELECT        delta, seaz, timeres, importance, in_wgt, wgt, ema, sdelay
2202         INTO                   :assocaro_delta:assocaro_delta_ind,
2203                                :assocaro_seaz:assocaro_seaz_ind,
2204                                :assocaro_timeres:assocaro_timeres_ind,
2205                                :assocaro_importance:assocaro_importance_ind,
2206                                :assocaro_in_wgt:assocaro_in_wgt_ind,
2207                                :assocaro_wgt:assocaro_wgt_ind,
2208                                :assocaro_ema:assocaro_ema_ind,
2209                                :assocaro_sdelay:assocaro_sdelay_ind
2210         FROM                   assocaro
2211         WHERE                  arid = :arid AND orid = :orid;
2212
2213         /* Converting date and time */
2214         p_arrival_datetime = arrival_datetime;
2215
2216         nb_sec = arrival_datetime - floor (arrival_datetime);
2217         sprintf (cnb_sec, "%6.4f", nb_sec);
2218         strcpy (cnb_sec, cnb_sec+1);
2219
2220         arrival_datetime = floor (arrival_datetime);
2221         EXEC SQL SELECT TRUETIME.GETSTRING (:arrival_datetime) INTO :ndatetime FROM DUAL;
2222
2223         Remove_Blank(arrival_location);
2224         if (arrival_location_ind == (-1))
2225                strcpy (arrival_location, "--");
2226         else
2227                {
2228                 if ((!strcmp (arrival_location, "")) || (!strcmp (arrival_location, "  ")))
2229                        strcpy (arrival_location, "--");
2230                }
2231
2232         /* Initializing the station archive format */
2233         memset (station, 32, 120);
2234         station[120] = '\0';
2235 
2236         /* Reading station information */
2237         sprintf (c_date, "%.4s%.2s%.2s%.2s%.2s", ndatetime, ndatetime+5,
2238                                ndatetime+8, ndatetime+11, ndatetime+14);
2239         Remove_Blank(c_date);
2240         sprintf (c_sec, "%.2s%.7s", ndatetime+17, cnb_sec);
2241         Remove_Blank(c_sec);
2242         sprintf (c_staname, "%.5s", arrival_sta);
2243         Remove_Blank(c_staname);
2244         sprintf (c_netcode, "%.2s", arrival_net);
2245         Remove_Blank(c_netcode);
2246         sprintf (c_source, "%.3s", arrival_subsource);
2247         Remove_Blank(c_source);
2248         ptype = arrival_iphase[0];
2249         ptype2 = arrival_iphase[1];
2250         sprintf (c_seed, "%.3s", arrival_seedchan);
2251         Remove_Blank(c_seed);
2252         sprintf (c_location, "%.2s", arrival_location);
2253         Remove_Blank(c_location);
2254         c_onset = arrival_qual;
2255         motion = arrival_fm[0];
2256
2257         sprintf (c_wgt, "%7.4f", (4 - (arrival_quality*4)) + ((1 - assocaro_in_wgt)*5));
2258         Remove_Blank(c_wgt);
2259         wgt = atoi (c_wgt);
2260         sprintf (c_wgt, "%d", wgt);
2261         h_wgt = c_wgt[0];
2262
2263         sprintf (c_uwt, "%7.4f", assocaro_wgt);
2264         Remove_Blank(c_uwt);
2265
2266         sprintf (c_epi, "%10.4f", assocaro_delta);
2267         Remove_Blank(c_epi);
2268         sprintf (c_azi, "%3d", (int)assocaro_seaz);
2269         Remove_Blank(c_azi);
2270         sprintf (c_angle, "%3d", (int)assocaro_ema);
2271         Remove_Blank(c_angle);
2272         sprintf (c_ttres, "%7.4f", assocaro_timeres);
2273         Remove_Blank(c_ttres);
2274         sprintf (c_importance, "%7.4f", assocaro_importance);
2275         Remove_Blank(c_importance);
2276         sprintf (c_sdelay, "%7.4f", assocaro_sdelay);
2277         Remove_Blank(c_sdelay);
2278
2279         /* Changing types */
2280         h_source[0] = c_source[0];
2281         h_source[1] = '\0';
2282
2283         if (motion == 'c')
2284                motion = 'U';
2285         else if (motion == 'd')
2286                motion = 'D';
2287         else   motion = ' ';
2288         
2289         if (strlen (c_date) == 0)
2290                strcpy (h_date, "            ");
2291         else strcpy (h_date, c_date);
2292
2293         if (strlen (c_sec) == 0)
2294                strcpy (h_sec, "     ");
2295         else
2296                {
2297                 sec = atof (c_sec);
2298                 sprintf (h_sec, "%5.0f", sec*100.);
2299                }
2300
2301         if (strlen (c_epi) == 0)
2302                strcpy (h_epi, "    ");
2303         else
2304                {
2305                 epi = atof (c_epi);
2306                 sprintf (h_epi, "%4.0f", epi*10.);
2307                }
2308
2309         if (strlen (c_azi) == 0)
2310                strcpy (h_azi, "   ");
2311         else
2312                {
2313                 azi = atoi (c_azi);
2314                 sprintf (h_azi, "%3d", azi);
2315                }
2316
2317         if (strlen (c_angle) == 0)
2318                strcpy (h_angle, "   ");
2319         else
2320                {
2321                 angle = atoi (c_angle);
2322                 sprintf (h_angle, "%3d", angle);
2323                }
2324
2325         if (strlen (c_ttres) == 0)
2326                strcpy (h_ttres, "    ");
2327         else
2328                {
2329                 ttres = atof (c_ttres);
2330                 sprintf (h_ttres, "%4.0f", ttres*100.);
2331                }
2332
2333         if (strlen (c_uwt) == 0)
2334                strcpy (h_uwt, "   ");
2335         else
2336                {
2337                 uwt = atof (c_uwt);
2338                 sprintf (h_uwt, "%3.0f", uwt*100.);
2339                }
2340
2341         if (strlen (c_importance) == 0)
2342                strcpy (h_importance, "    ");
2343         else
2344                {
2345                 importance = atof (c_importance);
2346                 sprintf (h_importance, "%4.0f", importance*1000.);
2347                }
2348
2349         if (strlen (c_sdelay) == 0)
2350                strcpy (h_sdelay, "    ");
2351         else
2352                {
2353                 sdelay = atof (c_sdelay);
2354                 sprintf (h_sdelay, "%4.0f", sdelay*100.);
2355                }
2356
2357         if (strlen (c_staname) == 0)
2358                strcpy (h_staname, "     ");
2359         else sprintf (h_staname, "%-5s", c_staname);
2360
2361         if (strlen (c_netcode) == 0)
2362                strcpy (h_netcode, "  ");
2363         else sprintf (h_netcode, "%.2s", c_netcode);
2364
2365         if (strlen (c_seed) == 0)
2366                strcpy (h_seed, "   ");
2367         else sprintf (h_seed, "%.3s", c_seed);
2368
2369         if (strlen (c_location) == 0)
2370                strcpy (h_location, "  ");
2371         else sprintf (h_location, "%.2s", c_location);
2372
2373         if (c_onset == ' ')
2374                strcpy (h_onset, "  ");
2375         else sprintf (h_onset, "%c ", toupper (c_onset));
2376
2377         /* Test for (P) and (S) */
2378         if (ptype == '(')
2379                 ptype = ptype2;
2380
2381         /* Building hypoinverse station archive */
2382         if ((ptype == 'p') || (ptype == 'P') || (ptype == 's') || (ptype == 'S'))
2383                {
2384                 memcpy (station+17, h_date, 12);
2385
2386                 if ((ptype == 'p') || (ptype == 'P'))
2387                        {
2388                         memcpy (station+29, h_sec, 5);
2389                         memcpy (station+34, h_ttres, 4);
2390                         memcpy (station+100, h_importance, 4);
2391                         memcpy (station+66, h_sdelay, 4);
2392                         memcpy (station+13, h_onset, 2);
2393                         memcpy (station+15, &motion, 1);
2394                         memcpy (station+16, &h_wgt, 1);
2395                         memcpy (station+38, h_uwt, 3);
2396                         memcpy (station+14, "P", 1);
2397                         memcpy (station+49, "0", 1);
2398                        }
2399                 else   {
2400                         memcpy (station+41, h_sec, 5);
2401                         memcpy (station+50, h_ttres, 4);
2402                         memcpy (station+104, h_importance, 4);
2403                         memcpy (station+70, h_sdelay, 4);
2404                         memcpy (station+46, h_onset, 2);
2405                         memcpy (station+49, &h_wgt, 1);
2406                         memcpy (station+63, h_uwt, 3);
2407                         memcpy (station+47, "S", 1);
2408                         memcpy (station+16, "4", 1);
2409                        }
2410
2411                 memcpy (station+74, h_epi, 4);
2412                 memcpy (station+78, h_angle, 3);
2413                 memcpy (station+91, h_azi, 3);
2414
2415                 memcpy (station+0 , h_staname, 5);
2416                 memcpy (station+5, h_netcode, 2);
2417                 memcpy (station+9, h_seed, 3);
2418                 memcpy (station+108, h_source, 1);
2419                 memcpy (station+111, h_location, 2);
2420
2421                 /* Building hypoinverse station shadow */
2422                 memset (sta_shadow, 32, 95);
2423                 sta_shadow[95] = '\0';
2424                 sta_shadow[0] = '$';
2425
2426                   /*******************************/
2427                  /* Generating coda information */
2428                 /*******************************/
2429                 if (PhaseList[k].coid != 0)
2430                        {
2431                         coid = PhaseList[k].coid;
2432                         
2433                         /* Retrieving Coda information */
2434                         cod_tau     = 0;
2435                         cod_afix    = 0;
2436                         cod_qfix    = 0;
2437                         cod_afree   = 0;
2438                         cod_qfree   = 0;
2439                         cod_rms     = 0;
2440                         cod_time1   = 0;
2441                         cod_amp1    = 0;
2442                         cod_time2   = 0;
2443                         cod_amp2    = 0;
2444                         cod_time3   = 0;
2445                         cod_amp3    = 0;
2446                         cod_time4   = 0;
2447                         cod_amp4    = 0;
2448                         cod_time5   = 0;
2449                         cod_amp5    = 0;
2450                         cod_time6   = 0;
2451                         cod_amp6    = 0;
2452                         cod_nsample = 0.;
2453                         cod_quality = 0.;
2454
2455                         EXEC SQL SELECT tau, afix, qfix, afree, qfree, rms, time1, amp1, time2, amp2, time3, amp3, time4, amp4, time5, amp5, time6, amp6, nsample, quality
2456                         INTO    :cod_tau:cod_tau_ind, :cod_afix:cod_afix_ind, :cod_qfix:cod_qfix_ind, :cod_afree:cod_afree_ind, :cod_qfree:cod_qfree_ind, :cod_rms:cod_rms_ind, :cod_time1:cod_time1_ind, :cod_amp1:cod_amp1_ind, :cod_time2:cod_time2_ind, :cod_amp2:cod_amp2_ind, :cod_time3:cod_time3_ind, :cod_amp3:cod_amp3_ind, :cod_time4:cod_time4_ind, :cod_amp4:cod_amp4_ind, :cod_time5:cod_time5_ind, :cod_amp5:cod_amp5_ind, :cod_time6:cod_time6_ind, :cod_amp6:cod_amp6_ind, :cod_nsample:cod_nsample_ind, :cod_quality:cod_quality_ind
2457                         FROM    Coda
2458                         WHERE   coid = :coid;
2459
2460                         /* Retrieving AssocCoM information */
2461                         assoccom_mag    = 0.;
2462                         assoccom_in_wgt = 0.;
2463
2464                         EXEC SQL SELECT mag, in_wgt
2465                         INTO   :assoccom_mag:assoccom_mag_ind, :assoccom_in_wgt:assoccom_in_wgt_ind
2466                         FROM   AssocCoM
2467                         WHERE  coid = :coid AND magid = :assoccom_magid;
2468
2469/*printf ("\ncoid=%d - %f - %f - %f - %f - %f - %f - %d - %d - %d - %d - %d - %d - %d - %d - %d - %d - %d - %d", coid, cod_tau, cod_afix, cod_qfix, cod_afree, cod_qfree, cod_rms, cod_time1, cod_amp1, cod_time2, cod_amp2, cod_time3, cod_amp3, cod_time4, cod_amp4, cod_time5, cod_amp5, cod_time6, cod_amp6);*/
2470/*printf ("\nsta = %s - coid=%d(%d) - %f - %f", arrival_sta, coid, assoccom_magid, assoccom_mag, assoccom_in_wgt);*/
2471
2472                         sprintf (c_assoc_wgt, "%7.4f", (4 - (cod_quality*4)) + ((1 - assoccom_in_wgt)*5));
2473                         Remove_Blank(c_assoc_wgt);
2474                         assoc_wgt = atoi (c_assoc_wgt);
2475                         sprintf (c_assoc_wgt, "%d", assoc_wgt);
2476                         h_assoc_wgt = c_assoc_wgt[0];
2477
2478                         sprintf (c_assoc_mag, "%7.4f", assoccom_mag);
2479                         Remove_Blank(c_assoc_mag);
2480                         sprintf (c_cod_tau, "%4d", (int)cod_tau);
2481                         Remove_Blank(c_cod_tau);
2482
2483                         sprintf (h_cod_afix, "%5.2f", cod_afix);
2484                         Remove_Blank(h_cod_afix);
2485                         sprintf (h_cod_qfix, "%5.2f", cod_qfix);
2486                         Remove_Blank(h_cod_qfix);
2487                         sprintf (h_cod_afree, "%5.2f", cod_afree);
2488                         Remove_Blank(h_cod_afree);
2489                         sprintf (h_cod_qfree, "%5.2f", cod_qfree);
2490                         Remove_Blank(h_cod_qfree);
2491                         sprintf (h_cod_rms, "%5.2f", cod_rms);
2492                         Remove_Blank(h_cod_rms);
2493                         sprintf (c_cod_time1, "%3d", cod_time1);
2494                         Remove_Blank(c_cod_time1);
2495                         sprintf (c_cod_amp1, "%4d", cod_amp1);
2496                         Remove_Blank(c_cod_amp1);
2497                         sprintf (c_cod_time2, "%3d", cod_time2);
2498                         Remove_Blank(c_cod_time2);
2499                         sprintf (c_cod_amp2, "%4d", cod_amp2);
2500                         Remove_Blank(c_cod_amp2);
2501                         sprintf (c_cod_time3, "%3d", cod_time3);
2502                         Remove_Blank(c_cod_time3);
2503                         sprintf (c_cod_amp3, "%4d", cod_amp3);
2504                         Remove_Blank(c_cod_amp3);
2505                         sprintf (c_cod_time4, "%3d", cod_time4);
2506                         Remove_Blank(c_cod_time4);
2507                         sprintf (c_cod_amp4, "%4d", cod_amp4);
2508                         Remove_Blank(c_cod_amp4);
2509                         sprintf (c_cod_time5, "%3d", cod_time5);
2510                         Remove_Blank(c_cod_time5);
2511                         sprintf (c_cod_amp5, "%4d", cod_amp5);
2512                         Remove_Blank(c_cod_amp5);
2513                         sprintf (c_cod_time6, "%3d", cod_time6);
2514                         Remove_Blank(c_cod_time6);
2515                         sprintf (c_cod_amp6, "%4d", cod_amp6);
2516                         Remove_Blank(c_cod_amp6);
2517                         sprintf (c_cod_nsample, "%3d", cod_nsample);
2518                         Remove_Blank(c_cod_nsample);
2519
2520                         /* Changing types */
2521                         if (strlen (c_assoc_mag) == 0)
2522                                strcpy (h_assoc_mag, "   ");
2523                         else
2524                                {
2525                                 assoc_mag = atof (c_assoc_mag);
2526                                 sprintf (h_assoc_mag, "%3.0f", assoc_mag*100.);
2527                                }
2528
2529                         if (strlen (c_cod_tau) == 0)
2530                                strcpy (h_cod_tau, "    ");
2531                         else
2532                                {
2533                                 ncod_tau = atoi (c_cod_tau);
2534                                 sprintf (h_cod_tau, "%4d", ncod_tau);
2535                                }
2536
2537                         if (strlen (c_cod_time1) == 0)
2538                                strcpy (h_cod_time1, "   ");
2539                         else
2540                                {
2541                                 ncod_time1 = atoi (c_cod_time1);
2542                                 sprintf (h_cod_time1, "%3d", ncod_time1);
2543                                }
2544
2545                         if (strlen (c_cod_amp1) == 0)
2546                                strcpy (h_cod_amp1, "    ");
2547                         else
2548                                {
2549                                 ncod_amp1 = atoi (c_cod_amp1);
2550                                 sprintf (h_cod_amp1, "%4d", ncod_amp1);
2551                                }
2552
2553                         if (strlen (c_cod_time2) == 0)
2554                                strcpy (h_cod_time2, "   ");
2555                         else
2556                                {
2557                                 ncod_time2 = atoi (c_cod_time2);
2558                                 sprintf (h_cod_time2, "%3d", ncod_time2);
2559                                }
2560
2561                         if (strlen (c_cod_amp2) == 0)
2562                                strcpy (h_cod_amp2, "    ");
2563                         else
2564                                {
2565                                 ncod_amp2 = atoi (c_cod_amp2);
2566                                 sprintf (h_cod_amp2, "%4d", ncod_amp2);
2567                                }
2568
2569                         if (strlen (c_cod_time3) == 0)
2570                                strcpy (h_cod_time3, "   ");
2571                         else
2572                                {
2573                                 ncod_time3 = atoi (c_cod_time3);
2574                                 sprintf (h_cod_time3, "%3d", ncod_time3);
2575                                }
2576
2577                         if (strlen (c_cod_amp3) == 0)
2578                                strcpy (h_cod_amp3, "    ");
2579                         else
2580                                {
2581                                 ncod_amp3 = atoi (c_cod_amp3);
2582                                 sprintf (h_cod_amp3, "%4d", ncod_amp3);
2583                                }
2584
2585                         if (strlen (c_cod_time4) == 0)
2586                                strcpy (h_cod_time4, "   ");
2587                         else
2588                                {
2589                                 ncod_time4 = atoi (c_cod_time4);
2590                                 sprintf (h_cod_time4, "%3d", ncod_time4);
2591                                }
2592
2593                         if (strlen (c_cod_amp4) == 0)
2594                                strcpy (h_cod_amp4, "    ");
2595                         else
2596                                {
2597                                 ncod_amp4 = atoi (c_cod_amp4);
2598                                 sprintf (h_cod_amp4, "%4d", ncod_amp4);
2599                                }
2600
2601                         if (strlen (c_cod_time5) == 0)
2602                                strcpy (h_cod_time5, "   ");
2603                         else
2604                                {
2605                                 ncod_time5 = atoi (c_cod_time5);
2606                                 sprintf (h_cod_time5, "%3d", ncod_time5);
2607                                }
2608
2609                         if (strlen (c_cod_amp5) == 0)
2610                                strcpy (h_cod_amp5, "    ");
2611                         else
2612                                {
2613                                 ncod_amp5 = atoi (c_cod_amp5);
2614                                 sprintf (h_cod_amp5, "%4d", ncod_amp5);
2615                                }
2616
2617                         if (strlen (c_cod_time6) == 0)
2618                                strcpy (h_cod_time6, "   ");
2619                         else
2620                                {
2621                                 ncod_time6 = atoi (c_cod_time6);
2622                                 sprintf (h_cod_time6, "%3d", ncod_time6);
2623                                }
2624
2625                         if (strlen (c_cod_amp6) == 0)
2626                                strcpy (h_cod_amp6, "    ");
2627                         else
2628                                {
2629                                 ncod_amp6 = atoi (c_cod_amp6);
2630                                 sprintf (h_cod_amp6, "%4d", ncod_amp6);
2631                                }
2632                               
2633                         if (strlen (c_cod_nsample) == 0)
2634                                strcpy (h_cod_nsample, "   ");
2635                         else
2636                                {
2637                                 ncod_nsample = atoi (c_cod_nsample);
2638                                 sprintf (h_cod_nsample, "%3d", ncod_nsample);
2639                                }
2640
2641                         /* Building hypoinverse station archive */
2642                         memcpy (station+82, &h_assoc_wgt, 1);
2643                         memcpy (station+87, h_cod_tau, 4);
2644                         memcpy (station+94, h_assoc_mag, 3);
2645                         
2646                         if (strcmp (h_assoc_mag, "   "))
2647                                memcpy (station+109, "D", 1);
2648
2649                         /* Building hypoinverse station shadow archive */
2650                         memcpy (sta_shadow+2, h_cod_nsample, 3);
2651                         memcpy (sta_shadow+5, h_cod_afix, 5);
2652                         memcpy (sta_shadow+10, h_cod_qfix, 5);
2653                         memcpy (sta_shadow+15, h_cod_afree, 5);
2654                         memcpy (sta_shadow+20, h_cod_qfree, 5);
2655                         memcpy (sta_shadow+25, h_cod_rms, 5);
2656                         memcpy (sta_shadow+50, h_cod_time1, 3);
2657                         memcpy (sta_shadow+53, h_cod_amp1, 4);
2658                         memcpy (sta_shadow+57, h_cod_time2, 3);
2659                         memcpy (sta_shadow+60, h_cod_amp2, 4);
2660                         memcpy (sta_shadow+64, h_cod_time3, 3);
2661                         memcpy (sta_shadow+67, h_cod_amp3, 4);
2662                         memcpy (sta_shadow+71, h_cod_time4, 3);
2663                         memcpy (sta_shadow+74, h_cod_amp4, 4);
2664                         memcpy (sta_shadow+78, h_cod_time5, 3);
2665                         memcpy (sta_shadow+81, h_cod_amp5, 4);
2666                         memcpy (sta_shadow+85, h_cod_time6, 3);
2667                         memcpy (sta_shadow+88, h_cod_amp6, 4);
2668                        }
2669
2670                   /************************************/
2671                  /* Retrieving amplitude information */
2672                 /************************************/
2673                 if (PhaseList[k].ampid != 0)
2674                        {
2675                         ampid = PhaseList[k].ampid;
2676
2677                         amp_amplitude   = 0.;
2678                         amp_per         = 0.;
2679                         assocamm_mag    = 0.;
2680                         assocamm_in_wgt = 0.;
2681                         
2682                         EXEC SQL SELECT        amplitude, ampmeas, per
2683                         INTO                   :amp_amplitude:amp_amplitude_ind,
2684                                                :amp_ampmeas:amp_ampmeas_ind, :amp_per:amp_per_ind
2685                         FROM                   amp
2686                         WHERE                  ampid = :ampid;
2687
2688                         EXEC SQL SELECT        mag, in_wgt
2689                         INTO                   :assocamm_mag:assocamm_mag_ind,
2690                                                :assocamm_in_wgt:assocamm_in_wgt_ind
2691                         FROM                   assocamm
2692                         WHERE                  ampid = :ampid AND
2693                                                magid = :pref_ml_magid;
2694
2695                         /* Changing types */
2696                         sprintf (c_assocam_wgt, "%4.3f", (4 - (assocamm_in_wgt*4)));
2697                         Remove_Blank(c_assocam_wgt);
2698                         assocam_wgt = atoi (c_assocam_wgt);
2699                         sprintf (c_assocam_wgt, "%d", assocam_wgt);
2700                         h_assocam_wgt = c_assocam_wgt[0];
2701
2702                         sprintf (c_assocam_mag, "%5.2f", assocamm_mag);
2703                         Remove_Blank(c_assocam_mag);
2704                         if (strlen (c_assocam_mag) == 0)
2705                                strcpy (h_assocam_mag, "   ");
2706                         else
2707                                {
2708                                 assocam_mag = atof (c_assocam_mag);
2709                                 sprintf (h_assocam_mag, "%3.0f", assocam_mag*100.);
2710                                }
2711
2712                         sprintf (c_amp_per, "%10.4f", amp_per);
2713                         Remove_Blank(c_amp_per);
2714                         if (strlen (c_amp_per) == 0)
2715                                strcpy (h_amp_per, "   ");
2716                         else
2717                                {
2718                                 ampli_per = atof (c_amp_per);
2719                                 sprintf (h_amp_per, "%3.0f", ampli_per*100.);
2720                                }
2721
2722                         sprintf (c_amp_amplitude, "%7.2f", amp_amplitude);
2723                         Remove_Blank(c_amp_amplitude);
2724                         if (strlen (c_amp_amplitude) == 0)
2725                                strcpy (h_amp_amplitude, "       ");
2726                         else
2727                                {
2728                                 ampli_amplitude = atof (c_amp_amplitude);
2729                                 sprintf (h_amp_amplitude, "%7.0f", ampli_amplitude*100.);
2730                                }
2731
2732                         sprintf (h_amp_ampmeas, " %c", amp_ampmeas);
2733
2734
2735                         /* Building hypoinverse station archive */
2736                         memcpy (station+81, &h_assocam_wgt, 1);
2737                         memcpy (station+97, h_assocam_mag, 3);
2738                         memcpy (station+83, h_amp_per, 3);
2739                         memcpy (station+54, h_amp_amplitude, 7);
2740                         memcpy (station+61, h_amp_ampmeas, 2);
2741                        }
2742
2743                   /**********************************/
2744                  /* Generating S Phase information */
2745                 /**********************************/
2746                 if (PhaseList[k].other_arid != 0)
2747                        {
2748                         arid = PhaseList[k].other_arid;
2749                         
2750                         arrival_datetime    = 0.;
2751                         assocaro_timeres    = 0.;
2752                         assocaro_importance = 0.;
2753                         arrival_quality     = 0.;
2754                         assocaro_in_wgt     = 0.;
2755                         assocaro_wgt        = 0.;
2756                         assocaro_sdelay     = 0.;
2757         
2758                         EXEC SQL SELECT        datetime, qual, quality
2759                         INTO                   :arrival_datetime:arrival_datetime_ind,
2760                                                :arrival_qual:arrival_qual_ind,
2761                                                :arrival_quality:arrival_quality_ind
2762                         FROM                   arrival
2763                         WHERE                  arid = :arid;
2764
2765                         EXEC SQL SELECT        timeres, importance, in_wgt, wgt, sdelay
2766                         INTO                   :assocaro_timeres:assocaro_timeres_ind,
2767                                                :assocaro_importance:assocaro_importance_ind,
2768                                                :assocaro_in_wgt:assocaro_in_wgt_ind,
2769                                                :assocaro_wgt:assocaro_wgt_ind,
2770                                                :assocaro_sdelay:assocaro_sdelay_ind
2771                         FROM                   assocaro
2772                         WHERE                  arid = :arid AND orid = :orid;
2773         
2774                         /* Is S phase on same minute than P phase? */
2775                         p_arrival_datetime = floor (p_arrival_datetime);
2776                         EXEC SQL SELECT TRUETIME.GETSTRING (:p_arrival_datetime) INTO :npdatetime FROM DUAL;
2777
2778                         s_arrival_datetime = floor (arrival_datetime);
2779                         EXEC SQL SELECT TRUETIME.GETSTRING (:s_arrival_datetime) INTO :nsdatetime FROM DUAL;
2780
2781                         memcpy (npdatetime+17, "00", 2);
2782                         memcpy (nsdatetime+17, "00", 2);
2783                         npdatetime[19] = '\0';
2784                         nsdatetime[19] = '\0';
2785
2786                         EXEC SQL SELECT TRUETIME.PUTSTRING (:npdatetime) INTO :p_arrival_datetime FROM DUAL;
2787                         EXEC SQL SELECT TRUETIME.PUTSTRING (:nsdatetime) INTO :s_arrival_datetime FROM DUAL;
2788
2789                         /* Converting values */
2790                         nb_sec = arrival_datetime - floor (arrival_datetime);
2791                         sprintf (cnb_sec, "%6.4f", nb_sec);
2792                         strcpy (cnb_sec, cnb_sec+1);
2793
2794                         arrival_datetime = floor (arrival_datetime);
2795                         EXEC SQL SELECT TRUETIME.GETSTRING (:arrival_datetime) INTO :ndatetime FROM DUAL;
2796
2797                         sprintf (c_sec, "%.2s%.7s", ndatetime+17, cnb_sec);
2798                         Remove_Blank(c_sec);
2799         
2800                         sprintf (c_ttres, "%7.4f", assocaro_timeres);
2801                         Remove_Blank(c_ttres);
2802         
2803                         sprintf (c_importance, "%7.4f", assocaro_importance);
2804                         Remove_Blank(c_importance);
2805         
2806                         sprintf (c_sdelay, "%7.4f", assocaro_sdelay);
2807                         Remove_Blank(c_sdelay);
2808         
2809                         c_onset = arrival_qual;
2810         
2811                         sprintf (c_wgt, "%7.4f", (4 - (arrival_quality*4)) + ((1 - assocaro_in_wgt)*5));
2812                         Remove_Blank(c_wgt);
2813                         wgt = atoi (c_wgt);
2814                         sprintf (c_wgt, "%d", wgt);
2815                         h_wgt = c_wgt[0];
2816
2817                         sprintf (c_uwt, "%7.4f", assocaro_wgt);
2818                         Remove_Blank(c_uwt);
2819                         
2820                         /* Changing types */
2821                         if (strlen (c_sec) == 0)
2822                                strcpy (h_sec, "     ");
2823                         else
2824                                {
2825                                 sec = atof (c_sec) + (s_arrival_datetime - p_arrival_datetime);
2826                                 sprintf (h_sec, "%5.0f", sec*100.);
2827                                }
2828               
2829                         if (strlen (c_ttres) == 0)
2830                                strcpy (h_ttres, "    ");
2831                         else
2832                                {
2833                                 ttres = atof (c_ttres);
2834                                 sprintf (h_ttres, "%4.0f", ttres*100.);
2835                                }
2836
2837                         if (strlen (c_importance) == 0)
2838                                strcpy (h_importance, "    ");
2839                         else
2840                                {
2841                                 importance = atof (c_importance);
2842                                 sprintf (h_importance, "%4.0f", importance*1000.);
2843                                }
2844
2845                         if (strlen (c_sdelay) == 0)
2846                                strcpy (h_sdelay, "    ");
2847                         else
2848                                {
2849                                 sdelay = atof (c_sdelay);
2850                                 sprintf (h_sdelay, "%4.0f", sdelay*100.);
2851                                }
2852
2853                         if (c_onset == ' ')
2854                                strcpy (h_onset, "  ");
2855                         else sprintf (h_onset, "%c ", toupper (c_onset));
2856
2857                         if (strlen (c_uwt) == 0)
2858                                strcpy (h_uwt, "   ");
2859                         else
2860                                {
2861                                 uwt = atof (c_uwt);
2862                                 sprintf (h_uwt, "%3.0f", uwt*100.);
2863                                }
2864
2865                         /* Building hypoinverse station archive */
2866                         memcpy (station+41, h_sec, 5);
2867                         memcpy (station+50, h_ttres, 4);
2868                         memcpy (station+104, h_importance, 4);
2869                         memcpy (station+70, h_sdelay, 4);
2870                         memcpy (station+46, h_onset, 2);
2871                         memcpy (station+49, &h_wgt, 1);
2872                         memcpy (station+63, h_uwt, 3);
2873                         memcpy (station+47, "S", 1);
2874                        }
2875                 
2876                 /* Storing phase reading */
2877                 strcpy (phase[nb_phase].sta, h_staname);
2878                 strcpy (phase[nb_phase].net, h_netcode);
2879                 strcpy (phase[nb_phase].seedchan, h_seed);
2880                 phase[nb_phase].type = toupper (ptype);
2881                 strcpy (phase[nb_phase].time, h_date);
2882                 strcpy (phase[nb_phase].seconds, h_sec);
2883                 strcpy (phase[nb_phase].hypo_line, station);
2884                 strcpy (phase[nb_phase].hypo_shadow, sta_shadow);
2885                 nb_phase++;
2886                }
2887         else
2888                {
2889                   /*************************************/
2890                  /* Generating independent amplitudes */
2891                 /*************************************/
2892
2893                 /* Initializing the station archive format */
2894                 memset (station, 32, 120);
2895                 station[120] = '\0';
2896
2897                 sprintf (c_staname, "%.5s", PhaseList[k].sta);
2898                 Remove_Blank(c_staname);
2899                 sprintf (c_netcode, "%.2s", PhaseList[k].net);
2900                 Remove_Blank(c_netcode);
2901                 sprintf (c_seed, "%.3s", PhaseList[k].seedchan);
2902                 Remove_Blank(c_seed);
2903                 sprintf (c_location, "%.2s", PhaseList[k].location);
2904                 Remove_Blank(c_location);
2905
2906                 if (strlen (c_staname) == 0)
2907                        strcpy (h_staname, "     ");
2908                 else sprintf (h_staname, "%-5s", c_staname);
2909
2910                 if (strlen (c_netcode) == 0)
2911                        strcpy (h_netcode, "  ");
2912                 else sprintf (h_netcode, "%.2s", c_netcode);
2913
2914                 if (strlen (c_seed) == 0)
2915                        strcpy (h_seed, "   ");
2916                 else sprintf (h_seed, "%.3s", c_seed);
2917
2918                 if (strlen (c_location) == 0)
2919                        strcpy (h_location, "  ");
2920                 else sprintf (h_location, "%.2s", c_location);
2921
2922                 memcpy (station+0 , h_staname, 5);
2923                 memcpy (station+5, h_netcode, 2);
2924                 memcpy (station+9, h_seed, 3);
2925                 memcpy (station+111, h_location, 2);
2926
2927                 /* Workaround for Hypoinverse */
2928                 memcpy (station+17, ordt, 17);
2929                 station[14] = 'P';
2930                 station[16] = '4';
2931
2932                 /* Building hypoinverse station shadow */
2933                 memset (sta_shadow, 32, 95);
2934                 sta_shadow[95] = '\0';
2935                 sta_shadow[0] = '$';
2936
2937                 /* Retrieving amplitude information */
2938                 ampid = PhaseList[k].ampid;
2939
2940                 amp_amplitude   = 0.;
2941                 amp_per         = 0.;
2942                 assocamm_mag    = 0.;
2943                 assocamm_in_wgt = 0.;
2944
2945                 EXEC SQL SELECT        amplitude, ampmeas, per
2946                 INTO                   :amp_amplitude:amp_amplitude_ind,
2947                                        :amp_ampmeas:amp_ampmeas_ind, :amp_per:amp_per_ind
2948                 FROM                   amp
2949                 WHERE                  ampid = :ampid;
2950
2951                 EXEC SQL SELECT        mag, in_wgt
2952                 INTO                   :assocamm_mag:assocamm_mag_ind,
2953                                        :assocamm_in_wgt:assocamm_in_wgt_ind
2954                 FROM                   assocamm
2955                 WHERE                  ampid = :ampid AND
2956                                        magid = :pref_ml_magid;
2957
2958                 /* Changing types */
2959                 sprintf (c_assocam_wgt, "%4.3f", (4 - (assocamm_in_wgt*4)));
2960                 Remove_Blank(c_assocam_wgt);
2961                 assocam_wgt = atoi (c_assocam_wgt);
2962                 sprintf (c_assocam_wgt, "%d", assocam_wgt);
2963                 h_assocam_wgt = c_assocam_wgt[0];
2964
2965                 sprintf (c_assocam_mag, "%5.2f", assocamm_mag);
2966                 Remove_Blank(c_assocam_mag);
2967                 if (strlen (c_assocam_mag) == 0)
2968                        strcpy (h_assocam_mag, "   ");
2969                 else
2970                        {
2971                         assocam_mag = atof (c_assocam_mag);
2972                         sprintf (h_assocam_mag, "%3.0f", assocam_mag*100.);
2973                        }
2974
2975                 sprintf (c_amp_per, "%10.4f", amp_per);
2976                 Remove_Blank(c_amp_per);
2977                 if (strlen (c_amp_per) == 0)
2978                        strcpy (h_amp_per, "   ");
2979                 else
2980                        {
2981                         ampli_per = atof (c_amp_per);
2982                         sprintf (h_amp_per, "%3.0f", ampli_per*100.);
2983                        }
2984
2985                 sprintf (c_amp_amplitude, "%7.2f", amp_amplitude);
2986                 Remove_Blank(c_amp_amplitude);
2987                 if (strlen (c_amp_amplitude) == 0)
2988                        strcpy (h_amp_amplitude, "       ");
2989                 else
2990                        {
2991                         ampli_amplitude = atof (c_amp_amplitude);
2992                         sprintf (h_amp_amplitude, "%7.0f", ampli_amplitude*100.);
2993                        }
2994
2995                 sprintf (h_amp_ampmeas, " %c", amp_ampmeas);
2996
2997
2998                 /* Building hypoinverse station archive */
2999                 memcpy (station+81, &h_assocam_wgt, 1);
3000                 memcpy (station+97, h_assocam_mag, 3);
3001                 memcpy (station+83, h_amp_per, 3);
3002                 memcpy (station+54, h_amp_amplitude, 7);
3003                 memcpy (station+61, h_amp_ampmeas, 2);
3004
3005                 /* Storing phase reading */
3006                 strcpy (phase[nb_phase].sta, h_staname);
3007                 strcpy (phase[nb_phase].net, h_netcode);
3008                 strcpy (phase[nb_phase].seedchan, h_seed);
3009                 phase[nb_phase].type = toupper (ptype);
3010                 strcpy (phase[nb_phase].time, h_date);
3011                 strcpy (phase[nb_phase].seconds, h_sec);
3012                 strcpy (phase[nb_phase].hypo_line, station);
3013                 strcpy (phase[nb_phase].hypo_shadow, sta_shadow);
3014                 nb_phase++;
3015                }
3016        }
3017 
3018 for (z = 0;z < nb_phase; z ++)
3019        {
3020         printf ("\n%s", phase[z].hypo_line);
3021
3022         if (flag_shadow == 1)
3023                printf ("\n%s", phase[z].hypo_shadow);
3024        }
3025}
3026
3027/*********************************************************************************/
3028/*  angle: Convert string (degrees[:minutes[:seconds]]) to decimal angle value.  */
3029/*********************************************************************************/
3030double angle(p)
3031char    *p;
3032{
3033 double  sign;
3034 double  d, m, s;
3035
3036 sign = 1.0;
3037 if (*p == '+')
3038        p++;
3039 if (*p == '-')
3040        {
3041         sign = -1.0;
3042         p++;
3043        }
3044       
3045 d = atof(p);
3046 m = s = 0.;
3047 if ((p = index(p, ':')) != NULL)
3048        {
3049         m = atof(++p);
3050         if ((p = index(p, ':')) != NULL)
3051                s = atof(++p);
3052        }
3053       
3054 return sign*(d + (m + s/60.)/60.);
3055}
3056
3057
3058/************************************************************************/
3059/*  main:   main program.                                               */
3060/************************************************************************/
3061void main (int       argc, char      *argv[])
3062{
3063 int    i, j, k, z;
3064 char   c_temp[1024];   /* Temporary string */
3065 char   c_temp2[1024];  /* Temporary string */
3066 char   Format[255];
3067 
3068 char   terminator[73];         /* Event terminator format */
3069 char   ter_shadow[73];         /* Event terminator shadow */
3070
3071 int    Flag_Output     = 0;
3072 int    Flag_Hypo       = 0;
3073 int    Flag_Summ       = 2;
3074 int    Flag_time       = 0;
3075 int    Flag_lat        = 0;
3076 int    Flag_lon        = 0;
3077 int    Flag_depth      = 0;
3078 int    Flag_source     = 0;
3079 int    Flag_ssource    = 0;
3080 int    Flag_etype      = 0;
3081 int    Flag_rflag      = 0;
3082 int    Flag_nbobs      = 0;
3083 int    Flag_gap        = 0;
3084 int    Flag_distance   = 0;
3085 int    Flag_rms        = 0;
3086 int    Flag_errtime    = 0;
3087 int    Flag_errhor     = 0;
3088 int    Flag_errdepth   = 0;
3089 int    Flag_quality    = 0;
3090 int    Flag_mag        = 0;
3091 int    Flag_magtype    = 0;
3092 int    Flag_magobs     = 0;
3093 int    Flag_delta      = 0;
3094 int    Flag_poly       = 0;
3095 int    Flag_location   = 0;
3096 int    Flag_system     = 0;
3097 int    Flag_Start      = 0;
3098 int    Flag_End        = 0;
3099 int    Flag_Span       = 0;
3100 int    Flag_header     = 1;
3101 int    Flag_Event      = 0;
3102 int    Flag_Evid       = 0;
3103 int    Flag_Sha        = 0;
3104
3105 float  lat_min;
3106 float  lat_max;
3107 float  lon_min;
3108 float  lon_max;
3109 float  depth_min;
3110 float  depth_max;
3111 char   source[128][32];
3112 char   ssource[128][32];
3113 int    source_nb;
3114 int    ssource_nb;
3115 char   etype[128][9];
3116 int    etype_nb;
3117 char   rflag[128][2];
3118 int    rflag_nb;
3119 int    nbobs_min;
3120 int    nbobs_max;
3121 float  gap_min;
3122 float  gap_max;
3123 float  distance_min;
3124 float  distance_max;
3125 float  rms_min;
3126 float  rms_max;
3127 float  errtime_min;
3128 float  errtime_max;
3129 float  errhor_min;
3130 float  errhor_max;
3131 float  errdepth_min;
3132 float  errdepth_max;
3133 float  quality_min;
3134 float  quality_max;
3135 float  mag_min;
3136 float  mag_max;
3137 char   magtype[128][32];
3138 int    magtype_nb;
3139 int    magobs_min;
3140 int    magobs_max;
3141 float  delta_min;
3142 char   spoly[512];
3143 char   camp_per[8];
3144 char   eventids[128][32];
3145 int    eventids_nb;
3146
3147 INT_TIME start_time;
3148 INT_TIME end_time;
3149 INT_TIME *pt;
3150 EXT_TIME stime_et;
3151 EXT_TIME etime_et;
3152 char *span = NULL;
3153
3154 char           connect_file[255];      /* Connection File */
3155 FILE           *fcf;                   /* Connection File Descriptor */
3156
3157 /* Variables needed for getopt. */
3158 extern char    *optarg;
3159 extern int     optind, opterr;
3160 int            c;
3161 char           *p;
3162
3163
3164 oraca.orastxtf = ORASTFERR;
3165 /* Connection File */
3166 strcpy (connect_file, CONNECT_FILE);
3167
3168 if ((p = (char *) getenv ("DB_FILE")) != NULL)
3169        strcpy (connect_file, p);
3170
3171
3172 cmdname = ((p = strrchr(*argv,'/')) != NULL) ? ++p : *argv;
3173 /*     Parse command line options.                                     */
3174 while ( (c = getopt(argc,argv,"c:hF:f:t:s:E:l:L:d:A:U:e:R:n:g:i:r:T:H:D:q:m:M:O:C:P:QS:I")) != -1) switch (c) {
3175        case '?':
3176        case 'h':       print_syntax(cmdname,syntax,info); exit(0); break;
3177        case 'c':       strcpy (connect_file, optarg);
3178                        break;
3179        case 'F':       Flag_Summ = 0;
3180                        strcpy (Format, optarg);
3181                        if (!strcmp (Format, "detail"))
3182                                Flag_Output = 0;
3183                        else if (!strcmp (Format, "hypo"))
3184                                Flag_Hypo = 1;
3185                        else if (!strcmp (Format, "hyposum"))
3186                                Flag_Hypo = 2;
3187                        else if (!strcmp (Format, "hyposha"))
3188                                Flag_Hypo = 3;
3189                        else if (!strcmp (Format, "fpfit"))
3190                                Flag_Hypo = 4;
3191                        else if (!strcmp (Format, "summary"))
3192                                Flag_Summ = 1;
3193                        else if (!strcmp (Format, "ncedc"))
3194                                Flag_Summ = 2;
3195                        else if (!strcmp (Format, "uw2"))
3196                                {
3197                                Flag_Summ = 3;
3198                                Flag_header = 0;
3199                                }
3200                        else
3201                                {
3202                                 printf ("\n Error: [-F] Invalid format [%s].\n\n", Format);
3203                                 print_syntax(cmdname,syntax,info);
3204                                 exit (0);
3205                                }
3206                        break;
3207        case 'f':       if ((pt = parse_date (optarg)) == NULL)
3208                                {
3209                                 printf ("\n Error: [-f] Invalid date: [%s]\n", optarg);
3210                                 print_syntax(cmdname,syntax,info);
3211                                 exit(0);
3212                                }
3213                        start_time = *pt;
3214                        Flag_Start = 1;
3215                        break;
3216        case 't':       if ((pt = parse_date (optarg)) == NULL)
3217                                {
3218                                 printf ("\n Error: [-t] Invalid date: [%s]\n", optarg);
3219                                 print_syntax(cmdname,syntax,info);
3220                                 exit(0);
3221                                }
3222                        end_time = *pt;
3223                        Flag_End = 1;
3224                        break;
3225        case 's':       span = optarg;
3226                        Flag_Span = 1;
3227                        break;
3228        case 'l':       Flag_lat = 1; l_arg = add_to_list(l_arg,optarg);
3229                        lat_min = angle (l_arg.name[0]);
3230                        lat_max = angle (l_arg.name[1]);
3231
3232                        if (lat_min > lat_max)
3233                                {
3234                                 printf ("\n Error: [-l] minimum latitude is greater than maximum latitude.\n\n");
3235
3236                                 exit (0);
3237                                }
3238                        else if ((lat_min < -90.) || (lat_min > 90.) || (lat_max < -90.) || (lat_max > 90.))
3239                                {
3240                                 printf ("\n Error: [-l] latitude is not between -90. and 90..\n\n");
3241
3242                                 exit (0);
3243                                }
3244                        break;
3245        case 'L':       Flag_lon = 1; L_arg = add_to_list(L_arg,optarg);
3246                        lon_min = angle (L_arg.name[0]);
3247                        lon_max = angle (L_arg.name[1]);
3248                       
3249                        if (lon_min > lon_max)
3250                                {
3251                                 printf ("\n Error: [-L] minimum longitude is greater than maximum longitude.\n\n");
3252
3253                                 exit (0);
3254                                }
3255                        else if ((lon_min < -180.) || (lon_min > 180.) || (lon_max < -180.) || (lon_max > 180.))
3256                                {
3257                                 printf ("\n Error: [-L] longitude is not between -180. and 180..\n\n");
3258
3259                                 exit (0);
3260                                }
3261                        break;
3262        case 'd':       Flag_depth = 1; d_arg = add_to_list(d_arg,optarg);
3263                        depth_min = atof (d_arg.name[0]);
3264                        depth_max = atof (d_arg.name[1]);
3265
3266                        if (depth_min > depth_max)
3267                                {
3268                                 printf ("\n Error: [-d] minimum depth is greater than maximum depth.\n\n");
3269
3270                                 exit (0);
3271                                }
3272                        break;
3273        case 'A':       Flag_source = 1; s_arg = add_to_list(s_arg,optarg);
3274                        for (k=0;k<s_arg.n;k++)
3275                                 strcpy (source[k], s_arg.name[k]);
3276                                 
3277                        source_nb = s_arg.n;
3278                        break;
3279        case 'U':       Flag_ssource = 1; ss_arg = add_to_list(ss_arg,optarg);
3280                        for (k=0;k<ss_arg.n;k++)
3281                                strcpy (ssource[k], ss_arg.name[k]);
3282
3283                        ssource_nb = ss_arg.n;
3284                        break;
3285        case 'e':       Flag_etype = 1; e_arg = add_to_list(e_arg,optarg);
3286                        for (k=0;k<e_arg.n;k++)
3287                                 strcpy (etype[k], e_arg.name[k]);
3288                                 
3289                        etype_nb = e_arg.n;
3290                        break;
3291        case 'R':       Flag_rflag = 1; R_arg = add_to_list(R_arg,optarg);
3292                        for (k=0;k<R_arg.n;k++)
3293                                 strcpy (rflag[k], R_arg.name[k]);
3294                                 
3295                        rflag_nb = R_arg.n;
3296                        break;
3297        case 'n':       Flag_nbobs = 1; n_arg = add_to_list(n_arg,optarg);
3298                        nbobs_min = atof (n_arg.name[0]);
3299                        nbobs_max = atof (n_arg.name[1]);
3300
3301                        if (nbobs_min > nbobs_max)
3302                                {
3303                                 printf ("\n Error: [-n] minimum number of observations is greater than maximum number of observations.\n\n");
3304
3305                                 exit (0);
3306                                }
3307                        else if ((nbobs_min < 0) || (nbobs_max < 0))
3308                                {
3309                                 printf ("\n Error: [-n] number of observations is not positive.\n\n");
3310
3311                                 exit (0);
3312                                }
3313                        break;
3314        case 'g':       Flag_gap = 1; g_arg = add_to_list(g_arg,optarg);
3315                        gap_min = atof (g_arg.name[0]);
3316                        gap_max = atof (g_arg.name[1]);
3317
3318                        if (gap_min > gap_max)
3319                                {
3320                                 printf ("\n Error: [-g] minimum gap is greater than maximum gap.\n\n");
3321
3322                                 exit (0);
3323                                }
3324                        else if ((gap_min < 0.) || (gap_min > 360.) || (gap_max < 0.) || (gap_max > 360.))
3325                                {
3326                                 printf ("\n Error: [-g] gap is not between -0. and 360..\n\n");
3327
3328                                 exit (0);
3329                                }
3330                        break;
3331        case 'i':       Flag_distance = 1; i_arg = add_to_list(i_arg,optarg);
3332                        distance_min = atof (i_arg.name[0]);
3333                        distance_max = atof (i_arg.name[1]);
3334
3335                        if (distance_min > distance_max)
3336                                {
3337                                 printf ("\n Error: [-d] minimum distance is greater than maximum distance.\n\n");
3338
3339                                 exit (0);
3340                                }
3341                        else if ((distance_min < 0.) || (distance_max < 0.))
3342                                {
3343                                 printf ("\n Error: [-d] distance is not positive.\n\n");
3344
3345                                 exit (0);
3346                                }
3347                        break;
3348        case 'r':       Flag_rms = 1; r_arg = add_to_list(r_arg,optarg);
3349                        rms_min = atof (r_arg.name[0]);
3350                        rms_max = atof (r_arg.name[1]);
3351
3352                        if (rms_min > rms_max)
3353                                {
3354                                 printf ("\n Error: [-r] minimum RMS is greater than maximum RMS.\n\n");
3355
3356                                 exit (0);
3357                                }
3358                        else if ((rms_min < 0.) || (rms_max < 0.))
3359                                {
3360                                 printf ("\n Error: [-r] RMS is not positive.\n\n");
3361
3362                                 exit (0);
3363                                }
3364                        break;
3365        case 'T':       Flag_errtime = 1; T_arg = add_to_list(T_arg,optarg);
3366                        errtime_min = atof (T_arg.name[0]);
3367                        errtime_max = atof (T_arg.name[1]);
3368
3369                        if (errtime_min > errtime_max)
3370                                {
3371                                 printf ("\n Error: [-T] minimum time error is greater than maximum time error.\n\n");
3372
3373                                 exit (0);
3374                                }
3375                        else if ((errtime_min < 0.) || (errtime_max < 0.))
3376                                {
3377                                 printf ("\n Error: [-T] time error is not positive.\n\n");
3378
3379                                 exit (0);
3380                                }
3381                        break;
3382        case 'H':       Flag_errhor = 1; H_arg = add_to_list(H_arg,optarg);
3383                        errhor_min = atof (H_arg.name[0]);
3384                        errhor_max = atof (H_arg.name[1]);
3385
3386                        if (errhor_min > errhor_max)
3387                                {
3388                                 printf ("\n Error: [-H] minimum horizontal error is greater than maximum horizontal error.\n\n");
3389
3390                                 exit (0);
3391                                }
3392                        else if ((errhor_min < 0.) || (errhor_max < 0.))
3393                                {
3394                                 printf ("\n Error: [-H] hprizontal error is not positive.\n\n");
3395
3396                                 exit (0);
3397                                }
3398                        break;
3399        case 'D':       Flag_errdepth = 1; D_arg = add_to_list(D_arg,optarg);
3400                        errdepth_min = atof (D_arg.name[0]);
3401                        errdepth_max = atof (D_arg.name[1]);
3402
3403                        if (errdepth_min > errdepth_max)
3404                                {
3405                                 printf ("\n Error: [-D] minimum depth error is greater than maximum depth error.\n\n");
3406
3407                                 exit (0);
3408                                }
3409                        else if ((errdepth_min < 0.) || (errdepth_max < 0.))
3410                                {
3411                                 printf ("\n Error: [-D] depth error is not positive.\n\n");
3412
3413                                 exit (0);
3414                                }
3415                        break;
3416        case 'q':       Flag_quality = 1; q_arg = add_to_list(q_arg,optarg);
3417                        quality_min = atof (q_arg.name[0]);
3418                        quality_max = atof (q_arg.name[1]);
3419                        break;
3420        case 'm':       Flag_mag = 1; m_arg = add_to_list(m_arg,optarg);
3421                        mag_min = atof (m_arg.name[0]);
3422                        mag_max = atof (m_arg.name[1]);
3423
3424                        if (mag_min > mag_max)
3425                                {
3426                                 printf ("\n Error: [-m] minimum magnitude is greater than maximum magnitude.\n\n");
3427
3428                                 exit (0);
3429                                }
3430                        break;
3431        case 'M':       Flag_magtype = 1; M_arg = add_to_list(M_arg,optarg);
3432                        for (k=0;k<M_arg.n;k++)
3433                                 strcpy (magtype[k], M_arg.name[k]);
3434                                 
3435                        magtype_nb = M_arg.n;
3436                        break;
3437        case 'E':       Flag_Evid = 1; V_arg = add_to_list(V_arg,optarg);
3438                        if (V_arg.n > 100)
3439                                {
3440                                 printf ("Error: [-E] maximum number of events is 100.\n\n");
3441
3442                                 exit (0);
3443                                }
3444                        for (k=0;k<V_arg.n;k++)
3445                                strcpy (eventids[k], V_arg.name[k]);
3446
3447                        eventids_nb = V_arg.n;
3448                        break;
3449        case 'O':       Flag_magobs = 1; O_arg = add_to_list(O_arg,optarg);
3450                        magobs_min = atof (O_arg.name[0]);
3451                        magobs_max = atof (O_arg.name[1]);
3452
3453                        if (magobs_min > magobs_max)
3454                                {
3455                                 printf ("\n Error: [-O] minimum number of observations is greater than maximum number of observations.\n\n");
3456
3457                                 exit (0);
3458                                }
3459                        else if ((magobs_min < 0) || (magobs_max < 0))
3460                                {
3461                                 printf ("\n Error: [-O] number of observations is not positive.\n\n");
3462
3463                                 exit (0);
3464                                }
3465                        break;
3466        case 'C':       Flag_delta = 1; E_arg = add_to_list(E_arg,optarg);
3467                        delta_lat = atof (E_arg.name[0]);
3468                        delta_lon = atof (E_arg.name[1]);
3469                        delta_min = atof (E_arg.name[2]);
3470                        delta_max = atof (E_arg.name[3]);
3471
3472                        if ((delta_lat < -90.) || (delta_lat > 90.) || (delta_lon < -180.) || (delta_lon > 180.))
3473                                {
3474                                 printf ("\n Error: [-E] latitude/longitude is not between -90./-180. and 90./180..\n\n");
3475
3476                                 exit (0);
3477                                }
3478
3479                        if (delta_min > delta_max)
3480                                {
3481                                 printf ("\n Error: [-E] minimum distance is greater than maximum distance.\n\n");
3482
3483                                 exit (0);
3484                                }
3485                        else if ((delta_min < 0) || (delta_max < 0))
3486                                {
3487                                 printf ("\n Error: [-E] distance is not positive.\n\n");
3488
3489                                 exit (0);
3490                                }
3491                        break;
3492        case 'P':       Flag_poly = 1; P_arg = add_to_list(P_arg,optarg);
3493
3494                        if (Flag_delta)
3495                                {
3496                                 printf ("\n Error: Options -E and -P cannot be used together.\n\n");
3497
3498                                 exit (0);
3499                                }
3500
3501                         nb_points = 0;
3502                         strcpy (spoly, "Polygon (");
3503
3504                         for (k=0;k<P_arg.n - 1;k+=2)
3505                                {
3506                                 poly[nb_points].x      = atof (P_arg.name[k]);
3507                                 poly[nb_points].y      = atof (P_arg.name[k+1]);
3508
3509                                 if ((poly[nb_points].x < -90.) || (poly[nb_points].x > 90.))
3510                                        {
3511                                         printf ("\n Error: [-P] latitude is not between -90. and 90..\n\n");
3512
3513                                         exit (0);
3514                                        }
3515
3516                                 if ((poly[nb_points].y < -180.) || (poly[nb_points].y > 180.))
3517                                        {
3518                                         printf ("\n Error: [-P] longitude is not between -180. and 180..\n\n");
3519
3520                                         exit (0);
3521                                        }
3522
3523                                 nb_points++;
3524                                }
3525
3526                        for (j=0;j<nb_points;j++)
3527                                sprintf (spoly, "%s Point (%8.5f,%8.5f), ", spoly, poly[j].x, poly[j].y);
3528
3529                        spoly[strlen (spoly) - 2] = ')';
3530                        sprintf (spoly, "%s , %d", spoly, nb_points);
3531                        break;
3532        case 'S':       if (!strcmp (optarg, "master"))
3533                                Flag_system = 1;
3534                        else if (!strcmp (optarg, "slave"))
3535                                Flag_system = 2;
3536                        else
3537                                {
3538                                 printf ("\n Error: [-S] Invalid system.\n\n");
3539                                 print_syntax(cmdname,syntax,info);
3540                                 exit (0);
3541                                }
3542                        break;
3543        case 'Q':       Flag_location = 1; break;
3544        case 'I':       Flag_header = 0; break;
3545        default:        fprintf (info, "Unknown option: -%c\n", c); exit(1);
3546 }
3547 /*     Skip over all options and their arguments.                      */
3548 argv = &(argv[optind]);
3549 argc -= optind;
3550
3551
3552 /* Updating Flags */
3553 if (Flag_End && Flag_Span)
3554        {
3555         printf ("\n Error: span and endtime mutually exclusive\n");
3556         exit(1);
3557        }
3558 if (Flag_Span && ! Flag_Start)
3559        {
3560         printf ("\n Error: span not valid without start time\n");
3561         exit(1);
3562        }
3563 if (Flag_Span && ! valid_span(span))
3564        {
3565         printf ("\n Error: invalid span specification: %s\n", span);
3566         exit(1);
3567        }
3568 if (Flag_Span)
3569        {
3570         end_time = end_of_span (start_time, span);
3571         ++Flag_End;
3572        }
3573 if ((Flag_Start || Flag_End) && ! (Flag_Start && Flag_End))
3574        {
3575         printf ("\n Error: Must specify both start and end time if either specified\n");
3576         exit (1);
3577        }
3578 if (Flag_Start)
3579        {
3580         Flag_time = 1;
3581         stime_et = int_to_ext (start_time);
3582         sprintf (ctime_min, "%04d/%02d/%02d %02d:%02d:%02d\n",
3583                        stime_et.year, stime_et.month, stime_et.day,
3584                        stime_et.hour, stime_et.minute, stime_et.second);
3585        }
3586 if (Flag_End)
3587        {
3588         Flag_time = 1;
3589         etime_et = int_to_ext (end_time);
3590         sprintf (ctime_max, "%04d/%02d/%02d %02d:%02d:%02d\n",
3591                        etime_et.year, etime_et.month, etime_et.day,
3592                        etime_et.hour, etime_et.minute, etime_et.second);
3593        }
3594
3595 /* Connect to ORACLE. */
3596 if ((fcf = fopen (connect_file, "rt")) == NULL)
3597        {
3598         printf ("\nError: Couldn't open file %s.\n", connect_file);
3599         exit (-1);
3600        }
3601 else
3602        {
3603         fscanf (fcf, "%*s %*s %s", db_user);
3604         fscanf (fcf, "%*s %*s %s", db_password);
3605         fscanf (fcf, "%*s %*s %s", db_name);
3606
3607         fclose (fcf);
3608         sprintf (user_pwd, "%s/%s@%s", db_user, db_password, db_name);
3609        }
3610
3611 EXEC SQL WHENEVER SQLERROR DO sql_error();
3612 sprintf (user_pwd, "%s/%s@%s", db_user, db_password, db_name);
3613 EXEC SQL CONNECT :user_pwd;
3614
3615
3616 /* Building SQL statement */
3617 strcpy (SQL_Command, "SELECT e.evid, o.orid, n.magid, e.etype, e.version, o.rflag, o.datetime, o.lat, o.lon, o.depth, o.auth, o.subsource, o.gap, o.distance, o.ndef, o.wrms, o.stime, o.erhor, o.sdep, o.quality, o.nbs, o.nbfm, o.totalarr, n.magnitude, n.magtype, n.nsta, o.locevid, n.auth, n.uncertainty, n.gap, n.distance, o.cmodelid, o.vmodelid FROM event e, origin o, netmag n WHERE ");
3618
3619 if (Flag_Hypo)
3620        {
3621         /* Do nothing */
3622        }
3623 else if ((Flag_Summ == 1) && Flag_header)
3624        {
3625         printf ("Date       Time            Lat     Lon       Depth   Mag  Mt  Nst Gap    Clo    RMS   Auth Subsrc   DbId     JtId       Typ\n");
3626         printf ("---------------------------------------------------------------------------------------------------------------------------\n");
3627        }
3628 else if ((Flag_Summ == 2) && Flag_header)
3629        {
3630         printf ("Date       Time             Lat       Lon  Depth   Mag Magt  Nst Gap  Clo  RMS  SRC   Event ID\n");
3631         printf ("----------------------------------------------------------------------------------------------\n");
3632        }
3633 else if (!Flag_Output && Flag_header)
3634        {
3635         printf ("Date       Time            Lat     Lon       Depth  SRC  Subsrc   Gap    Clo    Ndef ErTim RMS  ErHor ErDep  Qual  Mag   Mt  Nst DbId     JtId       Typ\n");
3636         printf ("--------------------------------------------------------------------------------------------------------------------------------------------------------\n");
3637        }
3638 
3639 if (Flag_time)
3640        {
3641         EXEC SQL SELECT TRUETIME.PUTSTRING (:ctime_min) INTO :time_min FROM DUAL;
3642         EXEC SQL SELECT TRUETIME.PUTSTRING (:ctime_max) INTO :time_max FROM DUAL;
3643
3644         sprintf (c_temp, "o.datetime >= %f AND o.datetime <= %f AND ", time_min, time_max);
3645         strcat (SQL_Command, c_temp);
3646        }
3647
3648 if (Flag_lat)
3649        {
3650         sprintf (c_temp, "o.lat >= %f AND o.lat <= %f AND ", lat_min, lat_max);
3651         strcat (SQL_Command, c_temp);
3652        }
3653
3654 if (Flag_lon)
3655        {
3656         sprintf (c_temp, "o.lon >= %f AND o.lon <= %f AND ", lon_min, lon_max);
3657         strcat (SQL_Command, c_temp);
3658        }
3659
3660 if (Flag_depth)
3661        {
3662         sprintf (c_temp, "o.depth >= %f AND o.depth <= %f AND ", depth_min, depth_max);
3663         strcat (SQL_Command, c_temp);
3664        }
3665
3666 if (Flag_source)
3667        {
3668         strcpy (c_temp2, "o.auth IN (");
3669
3670         for (i=0;i<source_nb;i++)
3671                {
3672                 sprintf (c_temp, "'%s',", source[i]);
3673                 strcat (c_temp2, c_temp);
3674                }
3675
3676         c_temp2[strlen(c_temp2)-1] = ')';
3677         strcat (c_temp2, " AND ");
3678         strcat (SQL_Command, c_temp2);
3679        }
3680
3681 if (Flag_ssource)
3682        {
3683         strcpy (c_temp2, "o.subsource IN (");
3684
3685         for (i=0;i<ssource_nb;i++)
3686                {
3687                 sprintf (c_temp, "'%s',", ssource[i]);
3688                 strcat (c_temp2, c_temp);
3689                }
3690
3691         c_temp2[strlen(c_temp2)-1] = ')';
3692         strcat (c_temp2, " AND ");
3693         strcat (SQL_Command, c_temp2);
3694        }
3695
3696 if (Flag_etype)
3697        {
3698         strcpy (c_temp2, "e.etype IN (");
3699
3700         for (i=0;i<etype_nb;i++)
3701                {
3702                 sprintf (c_temp, "'%s',", etype[i]);
3703                 strcat (c_temp2, c_temp);
3704                }
3705
3706         c_temp2[strlen(c_temp2)-1] = ')';
3707         strcat (c_temp2, " AND ");
3708         strcat (SQL_Command, c_temp2);
3709        }
3710       
3711 if (Flag_rflag)
3712        {
3713         strcpy (c_temp2, "o.rflag IN (");
3714
3715         for (i=0;i<rflag_nb;i++)
3716                {
3717                 sprintf (c_temp, "'%s',", rflag[i]);
3718                 strcat (c_temp2, c_temp);
3719                }
3720
3721         c_temp2[strlen(c_temp2)-1] = ')';
3722         strcat (c_temp2, " AND ");
3723         strcat (SQL_Command, c_temp2);
3724        }
3725 else   {
3726         strcpy (c_temp2, "o.rflag IN ('A','F','H') AND ");
3727         strcat (SQL_Command, c_temp2);
3728        }
3729
3730 if (Flag_nbobs)
3731        {
3732         sprintf (c_temp, "o.ndef >= %d AND o.ndef <= %d AND ", nbobs_min, nbobs_max);
3733         strcat (SQL_Command, c_temp);
3734        }
3735
3736 if (Flag_gap)
3737        {
3738         sprintf (c_temp, "o.gap >= %f AND o.gap <= %f AND ", gap_min, gap_max);
3739         strcat (SQL_Command, c_temp);
3740        }
3741
3742 if (Flag_distance)
3743        {
3744         sprintf (c_temp, "o.distance >= %f AND o.distance <= %f AND ", distance_min, distance_max);
3745         strcat (SQL_Command, c_temp);
3746        }
3747
3748 if (Flag_rms)
3749        {
3750         sprintf (c_temp, "o.wrms >= %f AND o.wrms <= %f AND ", rms_min, rms_max);
3751         strcat (SQL_Command, c_temp);
3752        }
3753
3754 if (Flag_errtime)
3755        {
3756         sprintf (c_temp, "o.stime >= %f AND o.stime <= %f AND ", errtime_min, errtime_max);
3757         strcat (SQL_Command, c_temp);
3758        }
3759
3760 if (Flag_errhor)
3761        {
3762         sprintf (c_temp, "o.erhor >= %f AND o.erhor <= %f AND ", errhor_min, errhor_max);
3763         strcat (SQL_Command, c_temp);
3764        }
3765
3766 if (Flag_errdepth)
3767        {
3768         sprintf (c_temp, "o.sdep >= %f AND o.sdep <= %f AND ", errdepth_min, errdepth_max);
3769         strcat (SQL_Command, c_temp);
3770        }
3771
3772 if (Flag_quality)
3773        {
3774         sprintf (c_temp, "o.quality >= %f AND o.quality <= %f AND ", quality_min, quality_max);
3775         strcat (SQL_Command, c_temp);
3776        }
3777
3778 if (Flag_mag)
3779        {
3780         sprintf (c_temp, "n.magnitude >= %f AND n.magnitude <= %f AND ", mag_min, mag_max);
3781         strcat (SQL_Command, c_temp);
3782        }
3783
3784 if (Flag_magtype)
3785        {
3786         strcpy (c_temp2, "n.magtype IN (");
3787
3788         for (i=0;i<magtype_nb;i++)
3789                {
3790                 sprintf (c_temp, "'%s',", magtype[i]);
3791                 strcat (c_temp2, c_temp);
3792                }
3793
3794         c_temp2[strlen(c_temp2)-1] = ')';
3795         strcat (c_temp2, " AND ");
3796         strcat (SQL_Command, c_temp2);
3797        }
3798
3799if (Flag_Evid)
3800        {
3801         strcpy (c_temp2, "e.evid IN (");
3802
3803         for (i=0;i<eventids_nb;i++)
3804                {
3805                 sprintf (c_temp, "'%s',", eventids[i]);
3806                 strcat (c_temp2, c_temp);
3807                }
3808
3809         c_temp2[strlen(c_temp2)-1] = ')';
3810         strcat (c_temp2, " AND ");
3811         strcat (SQL_Command, c_temp2);
3812        }
3813
3814 if (Flag_magobs)
3815        {
3816         sprintf (c_temp, "n.nsta >= %d AND n.nsta <= %d AND ", magobs_min, magobs_max);
3817         strcat (SQL_Command, c_temp);
3818        }
3819
3820 if (Flag_system)
3821        {
3822         if (Flag_system == 1)
3823                {
3824                 sprintf (c_temp, "e.selectflag = 1 AND ");
3825                }
3826         else
3827                {
3828                 sprintf (c_temp, "e.selectflag = 0 AND ");
3829                }
3830
3831         strcat (SQL_Command, c_temp);
3832        }
3833
3834 if (Flag_delta)
3835        {
3836         /* Computing boundaries */
3837         EXEC SQL SELECT tools.latlon (:delta_lat, :delta_lon, :delta_max, 180., 0) INTO :lat_min FROM DUAL;
3838         EXEC SQL SELECT tools.latlon (:delta_lat, :delta_lon, :delta_max, 0., 0) INTO :lat_max FROM DUAL;
3839         EXEC SQL SELECT tools.latlon (:delta_lat, :delta_lon, :delta_max, 270., 1) INTO :lon_min FROM DUAL;
3840         EXEC SQL SELECT tools.latlon (:delta_lat, :delta_lon, :delta_max, 90., 1) INTO :lon_max FROM DUAL;
3841
3842         /* Computing distances to the poles */
3843         EXEC SQL SELECT tools.distance (:delta_lat, :delta_lon,  90., 0.) INTO :dn FROM DUAL;
3844         EXEC SQL SELECT tools.distance (:delta_lat, :delta_lon, -90., 0.) INTO :ds FROM DUAL;
3845
3846         if ((dn <= delta_max) || (ds <= delta_max))
3847                {
3848                 if (delta_lat >= 0.)
3849                        sprintf (c_temp, "o.lat >= %f AND o.lat <= 90. AND " , lat_min);
3850                 else   sprintf (c_temp, "o.lat >= -90. AND o.lat <= %f AND ", lat_max);
3851                }
3852         else if ((fabs (lon_min) >= 180.) || (fabs (lon_max) >= 180.))
3853                {
3854                 if (delta_lon >= 0.)
3855                        sprintf (c_temp, "((o.lat >= %f AND o.lat <= %f AND o.lon >= %f AND o.lon <= 180.) OR (o.lat >= %f AND o.lat <= %f AND o.lon >= -180. AND o.lon <= %f)) AND ", lat_min, lat_max, lon_min, lat_min, lat_max, lon_max - 360.);
3856                 else   sprintf (c_temp, "((o.lat >= %f AND o.lat <= %f AND o.lon >= %f AND o.lon <= 180.) OR (o.lat >= %f AND o.lat <= %f AND o.lon >= -180. AND o.lon <= %f)) AND ", lat_min, lat_max, 360. + lon_min, lat_min, lat_max, lon_max);
3857                }
3858         else   sprintf (c_temp, "o.lat >= %f AND o.lat <= %f AND o.lon >= %f AND o.lon <= %f AND ", lat_min, lat_max, lon_min, lon_max);
3859
3860         strcat (SQL_Command, c_temp);
3861        }
3862
3863 if (Flag_poly)
3864        {
3865         /* Computing boundaries */
3866         sprintf (minmax_Command, "SELECT tools.minmax (%s, 0,0), tools.minmax (%s, 0,1), tools.minmax (%s, 1,0), tools.minmax (%s, 1,1) FROM DUAL\0", spoly, spoly, spoly, spoly);
3867         if (debug) printf ("%s\n",minmax_Command);
3868
3869         EXEC SQL PREPARE minmax_stmt FROM :minmax_Command;
3870         EXEC SQL DECLARE minmax_cursor CURSOR FOR minmax_stmt;
3871         EXEC SQL OPEN minmax_cursor;
3872         EXEC SQL FETCH minmax_cursor INTO :lat_min, :lat_max, :lon_min, :lon_max;
3873         EXEC SQL CLOSE minmax_cursor;
3874
3875         sprintf (c_temp, "o.lat >= %f AND o.lat <= %f AND o.lon >= %f AND o.lon <= %f AND ", lat_min, lat_max, lon_min, lon_max);
3876
3877         strcat (SQL_Command, c_temp);
3878        }
3879
3880 strcat (SQL_Command, "e.prefor = o.orid AND e.prefmag = n.magid (+)");
3881
3882 /* Rows order */
3883 if (Flag_Evid)
3884        strcat (SQL_Command, " ORDER BY e.evid\0");
3885 else   strcat (SQL_Command, " ORDER BY o.datetime\0");
3886
3887
3888 /* Declaring cursor */
3889 EXEC SQL PREPARE sql_stmt FROM :SQL_Command;
3890 EXEC SQL DECLARE db_cursor CURSOR FOR sql_stmt;
3891
3892 /* Opening cursor */
3893 EXEC SQL OPEN db_cursor;
3894
3895 evid = 0;
3896 orid = 0;
3897 magid = 0;
3898 strcpy (eventype, "");
3899 tdatetime = 0.;
3900 lat = 0.;
3901 lon = 0.;
3902 depth = 0.;
3903 strcpy (auth, "");
3904 strcpy (subsource, "");
3905 gap = 0.;
3906 distance = 0.;
3907 ndef = 0;
3908 sdobs = 0.;
3909 stime = 0.;
3910 erhor = 0.;
3911 sdepth = 0.;
3912 quality = 0.;
3913 nbs = 0;
3914 nbfm = 0;
3915 totalarr = 0;
3916 magnitude = 0.;
3917 strcpy (mag_type, "");
3918 nsta = 0;
3919 strcpy (locevid, "");
3920 strcpy (magauth, "");
3921 maguncertainty = 0.;
3922 maggap = 0.;
3923 magdistance = 0.;
3924 strcpy (cmodelid, "");
3925 strcpy (vmodelid, "");
3926 eversion = 0;
3927 orflag = ' ';
3928
3929 /* fetch instances from the NCEDC database */
3930 EXEC SQL FETCH db_cursor INTO :evid, :orid, :magid:magid_ind, :eventype:eventype_ind, :eversion:eversion_ind, :orflag:orflag_ind, :tdatetime:tdatetime_ind, :lat:lat_ind, :lon:lon_ind, :depth:depth_ind, :auth:auth_ind, :subsource:subsource_ind, :gap:gap_ind, :distance:distance_ind, :ndef:ndef_ind, :sdobs:sdobs_ind, :stime:stime_ind, :erhor:erhor_ind, :sdepth:sdepth_ind, :quality:quality_ind, :nbs:nbs_ind, :nbfm:nbfm_ind, :totalarr:totalarr_ind, :magnitude:magnitude_ind, :mag_type:mag_type_ind, :nsta:nsta_ind, :locevid:locevid_ind, :magauth:magauth_ind, :maguncertainty:maguncertainty_ind, :maggap:maggap_ind, :magdistance:magdistance_ind, :cmodelid:cmodelid_ind, :vmodelid:vmodelid_ind;
3931
3932
3933 while (sqlca.sqlcode != NOMOREROWS)
3934        {
3935         Flag_Event = 0;
3936
3937         /* Location info */
3938         if (Flag_location)
3939                {
3940                 strcpy (qlocation, "");
3941                 EXEC SQL SELECT WHERES.getNearestTown(:lat, :lon) INTO :qlocation:qlocation_ind FROM DUAL;
3942                 Remove_Blank(qlocation);
3943                }
3944
3945         if (subsource_ind == (-1))
3946                strcpy (subsource, "");
3947
3948         Remove_Blank (locevid);
3949         Remove_Blank (mag_type);
3950         Remove_Blank (auth);
3951         Remove_Blank (eventype);
3952         Remove_Blank (subsource);
3953         Remove_Blank (magauth);
3954
3955         if (Flag_poly)
3956                {
3957                 sprintf (inpoly_Command, "SELECT tools.inpoly (%s, %f, %f) FROM DUAL\0", spoly, lat, lon);
3958                 if (debug) printf ("%s\n",inpoly_Command);
3959
3960                 EXEC SQL PREPARE inpoly_stmt FROM :inpoly_Command;
3961                 EXEC SQL DECLARE inpoly_cursor CURSOR FOR inpoly_stmt;
3962                 EXEC SQL OPEN inpoly_cursor;
3963                 EXEC SQL FETCH inpoly_cursor INTO :crossings;
3964                 EXEC SQL CLOSE inpoly_cursor;
3965
3966                 if (crossings%2 == 1)
3967                        Flag_Event = 1;
3968                }
3969         else if (Flag_delta)
3970                {
3971                 EXEC SQL SELECT tools.distance (:lat, :lon, :delta_lat, :delta_lon) INTO :delta FROM DUAL;
3972
3973                 if ((delta >= delta_min) && (delta <= delta_max))
3974                        Flag_Event = 1;
3975                }
3976         else   Flag_Event = 1;
3977
3978         /* Display event information */
3979         if (Flag_Event)
3980                {
3981                 if (Flag_Hypo)
3982                        {
3983                         char   c_evid[13];;
3984                         
3985                         sprintf (c_evid, "%10d", evid);
3986                         
3987                         if (strlen (eventype) == 0)
3988                                strcpy (eventype, "  ");
3989
3990                         /* Retrieving Preferred Md Magnitude */
3991                         assoccom_magid = 0;
3992                         EXEC SQL SELECT magid
3993                                INTO :assoccom_magid
3994                                FROM EventPrefMag
3995                                WHERE magtype = 'd' AND evid = :evid;
3996
3997                         /* Retrieving Preferred Ml Magnitude */
3998                         pref_ml_magid = 0;
3999                         EXEC SQL SELECT magid
4000                                INTO :pref_ml_magid
4001                                FROM EventPrefMag
4002                                WHERE magtype = 'l' AND evid = :evid;
4003
4004                         /* Retrieving Preferred Ma Magnitude */
4005                         pref_ma_magid = 0;
4006                         EXEC SQL SELECT magid
4007                                INTO :pref_ma_magid
4008                                FROM EventPrefMag
4009                                WHERE magtype = 'a' AND evid = :evid;
4010
4011                         if ((Flag_Hypo == 1) || (Flag_Hypo == 3))
4012                                {
4013                                 if (Flag_Hypo == 1)
4014                                        Flag_Sha = 0;
4015                                 else   Flag_Sha = 1;
4016
4017                                 /* Outputs Hypocenter/Magnitudes */
4018                                 Output_Loc_Hypo(evid, orid, assoccom_magid, pref_ma_magid, Flag_Sha);
4019
4020                                 /* Outputs Phases/Codas/Amplitudes Information */
4021                                 Output_Pic_Hypo(evid, orid, assoccom_magid, pref_ml_magid, Flag_Sha);
4022                         
4023                                 /* Building hypoinverse event terminator format */
4024                                 memset (terminator, 32, 72);
4025                                 terminator[72] = '\0';
4026                                 memcpy (terminator+62, c_evid, 10);
4027
4028                                 printf ("\n%s\n", terminator);
4029
4030                                 if (Flag_Hypo == 3)
4031                                        {
4032                                         /* Building hypoinverse event terminator shadow */
4033                                         memset (ter_shadow, 32, 72);
4034                                         ter_shadow[72] = '\0';
4035                                         ter_shadow[0] = '$';
4036                                         memcpy (ter_shadow+62, c_evid, 10);
4037
4038                                         printf ("%s\n", ter_shadow);
4039                                        }
4040                                }
4041                         else if (Flag_Hypo == 2)
4042                                {
4043                                 /* Outputs Hypocenter/Magnitudes */
4044                                 Output_Loc_Hypo(evid, orid, assoccom_magid, pref_ma_magid, 0);
4045                                 printf ("\n");
4046                                }
4047                         else   {
4048                                 /* Outputs focal mechanisms */
4049                                 Output_Fpfit(evid, orid);
4050                                }
4051                        }
4052                 else if (Flag_Summ == 1)
4053                        Output_Summary(qlocation);
4054                 else if (Flag_Summ == 2)
4055                        Output_NCEDC(qlocation);
4056                 else if (Flag_Summ == 3)
4057                        Output_UW2_Acard(qlocation);
4058                 else   Output_Detail(qlocation);
4059                }
4060 
4061         evid = 0;
4062         orid = 0;
4063         magid = 0;
4064         strcpy (eventype, "");
4065         tdatetime = 0.;
4066         lat = 0.;
4067         lon = 0.;
4068         depth = 0.;
4069         strcpy (auth, "");
4070         strcpy (subsource, "");
4071         gap = 0.;
4072         distance = 0.;
4073         ndef = 0;
4074         sdobs = 0.;
4075         stime = 0.;
4076         erhor = 0.;
4077         sdepth = 0.;
4078         quality = 0.;
4079         nbs = 0;
4080         nbfm = 0;
4081         totalarr = 0;
4082         magnitude = 0.;
4083         strcpy (mag_type, "");
4084         nsta = 0;
4085         strcpy (locevid, "");
4086         strcpy (magauth, "");
4087         maguncertainty = 0.;
4088         maggap = 0.;
4089         magdistance = 0.;
4090         strcpy (cmodelid, "");
4091         strcpy (vmodelid, "");
4092         eversion = 0;
4093         orflag = ' ';
4094               
4095         EXEC SQL FETCH db_cursor INTO :evid, :orid, :magid:magid_ind, :eventype:eventype_ind, :eversion:eversion_ind, :orflag:orflag_ind, :tdatetime:tdatetime_ind, :lat:lat_ind, :lon:lon_ind, :depth:depth_ind, :auth:auth_ind, :subsource:subsource_ind, :gap:gap_ind, :distance:distance_ind, :ndef:ndef_ind, :sdobs:sdobs_ind, :stime:stime_ind, :erhor:erhor_ind, :sdepth:sdepth_ind, :quality:quality_ind, :nbs:nbs_ind, :nbfm:nbfm_ind, :totalarr:totalarr_ind, :magnitude:magnitude_ind, :mag_type:mag_type_ind, :nsta:nsta_ind, :locevid:locevid_ind, :magauth:magauth_ind, :maguncertainty:maguncertainty_ind, :maggap:maggap_ind, :magdistance:magdistance_ind, :cmodelid:cmodelid_ind, :vmodelid:vmodelid_ind;
4096        }
4097
4098 /* Close the cursor */
4099 EXEC SQL CLOSE db_cursor;
4100
4101 /* Disconnect from the database. */
4102 EXEC SQL COMMIT WORK RELEASE;
4103
4104 exit(0);
4105}
4106
4107/* Handle errors. Exit on any error. */
4108void    sql_error()
4109{
4110 char msg[512];
4111 size_t buf_len, msg_len;
4112
4113
4114 EXEC SQL WHENEVER SQLERROR CONTINUE;
4115
4116 buf_len = sizeof(msg);
4117 sqlglm(msg, &buf_len, &msg_len);
4118
4119 sqlca.sqlerrm.sqlerrmc[sqlca.sqlerrm.sqlerrml] = '\0';
4120 oraca.orastxt.orastxtc[oraca.orastxt.orastxtl] = '\0';
4121 oraca.orasfnm.orasfnmc[oraca.orasfnm.orasfnml] = '\0';
4122 printf("\n\n%s", sqlca.sqlerrm.sqlerrmc);
4123 printf("in \"%s\"\n", oraca.orastxt.orastxtc);
4124 printf("on line %d of %s.\n\n", oraca.oraslnr, oraca.orasfnm.orasfnmc);
4125
4126 EXEC SQL ROLLBACK WORK RELEASE;
4127
4128 exit(-1);
4129}
Note: See TracBrowser for help on using the repository browser.