Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

parse_usnsn.c

Go to the documentation of this file.
00001 
00002 /*
00003  *   THIS FILE IS UNDER RCS - DO NOT MODIFY UNLESS YOU HAVE
00004  *   CHECKED IT OUT USING THE COMMAND CHECKOUT.
00005  *
00006  *    $Id: parse__usnsn_8c-source.html 2161 2006-05-19 16:55:03Z paulf $
00007  *
00008  *    Revision history:
00009  *     $Log$
00009  *     Revision 1.1  2006/05/19 16:55:02  paulf
00009  *     first inclusion
00009  *
00010  *     Revision 1.12  2002/03/22 20:44:24  lucky
00011  *     pre v6.1 checkin
00012  *
00013  *     Revision 1.11  2001/08/02 22:40:21  davidk
00014  *     typecast some double to float assignments to eliminate compiler
00015  *     warnings on NT.
00016  *
00017  *     Revision 1.10  2001/04/17 17:49:37  lucky
00018  *     *** empty log message ***
00019  *
00020  *     Revision 1.8  2000/10/02 21:29:58  lucky
00021  *     The Debug printout of the entire file is now done only in extreme cases of
00022  *     Debug being set to 10.
00023  *     Changed the max length of phase field to PHA_LEN
00024  *
00025  *     Revision 1.7  2000/09/12 18:13:25  lucky
00026  *     Plugged memory leak with nsnbuf.
00027  *
00028  *     Revision 1.6  2000/06/26 20:36:41  lucky
00029  *     NSN deletion now works!
00030  *
00031  *     Revision 1.5  2000/06/26 20:04:39  lucky
00032  *     When encountering a delete message, we return RETURN_DELETE to the
00033  *     calling program so that it can handle it appropriately.
00034  *
00035  *     Revision 1.4  2000/05/31 16:16:58  lucky
00036  *     Fixed the check for deletion message -- used to be in line #4, now it
00037  *     #3
00038  *
00039  *     Revision 1.3  2000/05/02 19:50:54  lucky
00040  *     Cosmetic fixes needed to have NT compile without warnings.
00041  *
00042  *     Revision 1.2  2000/04/06 16:03:07  lucky
00043  *     We now check to see if we got a Delete message. For now, we don't do anything
00044  *     about it, only log a message and return FAILURE in hopes that the calling
00045  *     program will deal with the consequences.
00046  *
00047  *     Revision 1.1  2000/02/14 18:51:48  lucky
00048  *     Initial revision
00049  *
00050  *
00051  */
00052 
00053 /*
00054 
00055    parse_usnsn.c
00056 
00057 
00058      Routines used to parse a text message from NEIC.
00059     
00060      A successful call to ParseNSNMsg fills the NSNStruct
00061      with values from the text message contained in NsnMsg.
00062 
00063      If debug is not 0, debug info will be written. If debug > 1,
00064      a file with the parsed values of the message will be 
00065      written in the debug_dir directory.
00066 
00067      ParseNSNMsg returns EW_SUCCESS if everything goes well, 
00068      otherwise it returns EW_FAILURE.  
00069 
00070      Only ParseNSNMsg() should be called from outside of this
00071      file. All other functions in here are utilities used
00072      by ParseNSNMsg()
00073 
00074 */
00075       
00076 
00077 #include <stdio.h>
00078 #include <stdlib.h>
00079 #include <math.h>
00080 #include <parse_usnsn.h>
00081 #include <earthworm.h>
00082 #include <rw_mag.h>
00083 
00084 #define     MAX_LINES       1000
00085 #define     MAX_LINE_SIZE   300
00086 
00087 
00088 extern  int             epochsec17 (double *, char *);
00089 
00090 static  int             ConvertNSNMsg (char *, int, char **, int *);
00091 static  int             ParsePhaseMagnitude (char *, PhaseMag *);
00092 static  int             ParsePhase (char *, Phase *, char *, int *, double *);
00093 static  int             ParseMagnitude (char *, OriginMag *);
00094 static  int             ParseEllipse (char *, double, Err_Elipse *);
00095 static  int             AppendDateStr (char     *, char *);
00096 static  int             BuildDateStr (char *, char *);
00097 static  int     GetNextLine (char **, int *, char *);
00098 
00099 /************************************************************************
00100  * ParseNSNMsg() - Fill the msg struct from the text supplied in 
00101  *                 nsnbuf.
00102  ************************************************************************/
00103 int     ParseNSNMsg (char *NsnMsg, int msgLen, 
00104                                 NSNStruct *msgStruct, int debug, char *debug_dir)
00105 {
00106         int             i, j;
00107         char    datestr[15];
00108         char    DateStr[20];
00109         char    tmp1[256];
00110         char    tmp2[256];
00111         char    tmp3[256];
00112         int             lineno;
00113         FILE    *fp;
00114         char    filename[100];
00115     char    **nsnbuf;
00116     int     nlines;
00117     int     isAuto;
00118 
00119 
00120 
00121         if ((NsnMsg == NULL) || (msgStruct == NULL) || (debug_dir == NULL))
00122         {
00123                 logit ("e", "Invalid parameters passed in.\n");
00124                 return EW_FAILURE;
00125         }       
00126 
00127         nsnbuf = (char **) malloc (MAX_LINES * sizeof (char *));
00128         for (i = 0; i < MAX_LINES; i++)
00129                 nsnbuf[i] = (char *) malloc (MAX_LINE_SIZE * sizeof (char));
00130 
00131 
00132         /* Create nsnbuf (array of lines) for easier parsing
00133          ****************************************************/
00134         if (ConvertNSNMsg (NsnMsg, msgLen, nsnbuf, &nlines) != EW_SUCCESS)
00135         {
00136                 logit ("", "Call to ConvertNSNMsg failed\n");
00137                 return EW_FAILURE;
00138         }
00139 
00140 
00141         /* skip the first two lines */
00142         lineno = 2;
00143 
00144 
00145         /* First, we have to see if this is a deletion message.
00146            We don't do anything with it, so if we get it, we'll
00147            just return FAILURE, and let the calling program deal 
00148            with the ramifications.
00149          ********************************************************/
00150         sscanf (nsnbuf[lineno + 3], "%s\n", tmp1);
00151         if (strcmp (tmp1, "EVENT") == 0)
00152         {
00153                 /* Yup, it's a deletion message */
00154 
00155                 /* extract the NSN event key */
00156                 strncpy (msgStruct->EventKey, (nsnbuf[lineno + 3] + 21), 4);
00157                 msgStruct->EventKey[4] = '\0';
00158                 
00159                 return RETURN_DELETE;
00160         }
00161 
00162         /* see if this is an automatic, or a reviewed solution */
00163         sscanf (nsnbuf[lineno], "%s\n", tmp1);
00164         if (strcmp (tmp1, "The") == 0)
00165         {
00166                 /* Yup, it's an automatic: skip four more lines */
00167                 lineno = lineno + 4;
00168                 isAuto = 1;
00169         }
00170         else
00171         {
00172                 /* it's a reviewed solution message: we are at the first line */
00173                 isAuto = 0;
00174         }
00175 
00176 
00177 /*
00178  * line 1; extract date, comment, event key, whether it is preliminary
00179  *********************************************************************/
00180         strncpy (datestr, (nsnbuf[lineno] + DATE_BASE), 11);
00181         datestr[11] = '\0';
00182 
00183         if (BuildDateStr (datestr, msgStruct->EventDate) != EW_SUCCESS)
00184         {
00185                 logit ("e", "Call to BuildDateStr failed.\n");
00186                 return EW_FAILURE;
00187         }
00188 
00189         strncpy (msgStruct->EventKey, (nsnbuf[lineno] + EVENT_KEY_BASE), 4);
00190         msgStruct->EventKey[4] = '\0';
00191 
00192         if (nsnbuf[lineno][PRELIM_BASE] == 'p')
00193         {
00194                 msgStruct->automatic = 1;
00195         }
00196         else
00197         {
00198                 msgStruct->automatic = 0;
00199         }
00200 
00201         
00202 /*
00203  * line 2; extract origin time and error
00204  *********************************************************************/
00205         lineno = lineno + 1;
00206         if (GetNextLine (nsnbuf, &lineno, "ot") == EW_SUCCESS)
00207         {
00208                 sscanf (nsnbuf[lineno], "    ot  = %s   +/-   %s\n", tmp1, tmp2);
00209 
00210                 strcpy (DateStr, msgStruct->EventDate);
00211                 if (AppendDateStr (DateStr, tmp1) != EW_SUCCESS)
00212                 {
00213                         logit ("e", "Call to AppendDateStr failed.\n");
00214                         return EW_FAILURE;
00215                 }
00216 
00217                 epochsec17 (&msgStruct->ot, DateStr);
00218                 msgStruct->ot_err = (double) atof (tmp2);
00219         }
00220         else
00221         {
00222                 logit ("e", "GetNextLine returned EW_FAILURE; exitting\n");
00223                 return EW_FAILURE;
00224         }
00225 
00226 /*
00227  * line 3; extract latitude 
00228  *********************************************************************/
00229         lineno = lineno + 1;
00230         if (GetNextLine (nsnbuf, &lineno, "lat") == EW_SUCCESS)
00231         {
00232                 sscanf (nsnbuf[lineno], "    lat = %s   +/-   %s\n", tmp1, tmp2);
00233 
00234                 msgStruct->lat = (float) atof (tmp1);
00235                 msgStruct->lat_err = (float) atof (tmp2);
00236         }
00237         else
00238         {
00239                 logit ("e", "GetNextLine returned EW_FAILURE; exitting\n");
00240                 return EW_FAILURE;
00241         }
00242 
00243 /*
00244  * line 4; extract longitude 
00245  *********************************************************************/
00246         lineno = lineno + 1;
00247         if (GetNextLine (nsnbuf, &lineno, "lon") == EW_SUCCESS)
00248         {
00249                 sscanf (nsnbuf[lineno], "    lon = %s   +/-   %s\n", tmp1, tmp2);
00250 
00251                 msgStruct->lon = (float) atof (tmp1);
00252                 msgStruct->lon_err = (float) atof (tmp2);
00253         }
00254         else
00255         {
00256                 logit ("e", "GetNextLine returned EW_FAILURE; exitting\n");
00257                 return EW_FAILURE;
00258         }
00259 
00260 
00261 /*
00262  * line 5; extract depth 
00263  *********************************************************************/
00264         lineno = lineno + 1;
00265         if (GetNextLine (nsnbuf, &lineno, "dep") == EW_SUCCESS)
00266         {
00267                 /* Depth could have an error */
00268                 if (nsnbuf[lineno][DEPTH_BASE] == '+')
00269                 {
00270                         sscanf (nsnbuf[lineno], "    dep = %s   +/-   %s\n", tmp1, tmp2);
00271                         msgStruct->depth = (float) atof (tmp1);
00272                         msgStruct->depth_err = (float) atof (tmp2);
00273 
00274                 }
00275                 else
00276                 {
00277                         /* no +/- error, we have some string there */
00278                         sscanf (nsnbuf[lineno], "    dep = %s\n", tmp1);
00279                         msgStruct->depth = (float) atof (tmp1);
00280                         msgStruct->depth_err = (double) 0.0;
00281                         msgStruct->depth_fixed = 1;
00282                 }
00283         }
00284         else
00285         {
00286                 logit ("e", "GetNextLine returned EW_FAILURE; exitting\n");
00287                 return EW_FAILURE;
00288         }
00289 
00290 /*
00291  * line 6; extract number of phases and std error
00292  *********************************************************************/
00293         lineno = lineno + 1;
00294         if (GetNextLine (nsnbuf, &lineno, "nph") == EW_SUCCESS)
00295         {
00296                 sscanf (nsnbuf[lineno], "    nph = %s of %s    se = %s  \n", tmp1, tmp2, tmp3);
00297                 msgStruct->nph_used = atoi (tmp1);
00298                 msgStruct->nph_assoc = atoi (tmp2);
00299                 msgStruct->std_error = (double) atof (tmp3);
00300         }
00301         else
00302         {
00303                 logit ("e", "GetNextLine returned P3DB_FAILURE; exitting\n");
00304                 return EW_FAILURE;
00305         }
00306 
00307 
00308 /*
00309  * line 7; extract the error elipse
00310  *********************************************************************/
00311         lineno = lineno + 1;
00312         if (GetNextLine (nsnbuf, &lineno, "error") == EW_SUCCESS)
00313         {
00314                 strncpy (tmp1, (nsnbuf[lineno] + ELLIPSE_BASE), (size_t) ELLIPSE_LENGTH);
00315                 tmp1[ELLIPSE_LENGTH] ='\0';
00316 
00317                 if (ParseEllipse (tmp1, msgStruct->depth_err, &msgStruct->error) != EW_SUCCESS)
00318                 {
00319                         logit ("e", "Call to ParseEllipse failed.\n");
00320                         return EW_FAILURE;
00321                 }
00322         }
00323         else
00324         {
00325                 logit ("e", "GetNextLine returned EW_FAILURE; exitting\n");
00326                 return EW_FAILURE;
00327         }
00328 
00329 /*
00330  * line 8; extract magnitudes
00331  *********************************************************************/
00332         lineno = lineno + 1;
00333         if (GetNextLine (nsnbuf, &lineno, "mb") == EW_SUCCESS)
00334         {
00335                 msgStruct->numMags = 5;
00336                 if (ParseMagnitude (nsnbuf[lineno], msgStruct->O_mag) != EW_SUCCESS)
00337                 {
00338                         logit ("e", "Call to ParseMagnitude failed.\n");
00339                         return EW_FAILURE;
00340                 }
00341         }
00342         else
00343         {
00344                 logit ("e", "GetNextLine returned EW_FAILURE; exitting\n");
00345                 return EW_FAILURE;
00346         }
00347 
00348 /*
00349  * line 9; find the beginning of the picks section
00350  *********************************************************************/
00351         lineno = lineno + 1;
00352         if (GetNextLine (nsnbuf, &lineno, "sta") == EW_SUCCESS)
00353         {
00354 
00355                 /* Now, read and parse all the phases */
00356                 msgStruct->nph_actual = 0;
00357                 msgStruct->Dmin = EARTH_CIRCUM;
00358                 for (i = 0; i < msgStruct->nph_assoc; i++)
00359                 {
00360                         lineno = lineno + 1;
00361                         if (ParsePhase (nsnbuf[lineno], &msgStruct->phases[msgStruct->nph_actual],
00362                                                         msgStruct->EventDate, &msgStruct->nph_actual, 
00363                                                         &msgStruct->Dmin) != EW_SUCCESS)
00364                         {
00365                                 logit ("e", "Call to ParsePhase failed.\n");
00366                                 return EW_FAILURE;
00367                         }
00368                 }
00369 
00370 
00371                 if (debug > 10)
00372                 {
00373                         /* Print the parsed version of the message
00374                          ********************************************/
00375                         sprintf (filename, "%s/%s-%s", 
00376                                                 debug_dir, msgStruct->EventDate, msgStruct->EventKey);
00377                         
00378                         fp = fopen (filename, "wt");
00379 
00380 
00381                         /*** This is where we are done ****/
00382                         fprintf (fp, "\n===> PRINTING SUMMARY FOR EVENT %s \n", msgStruct->EventKey);
00383                         fprintf (fp, "Event Date: %s\n", msgStruct->EventDate);
00384                         fprintf (fp, 
00385                                                 "ot=%0.2f,%0.2f; lat=%0.2f,%0.2f; lon=%0.2f,%0.2f; dep=%0.2f,%0.2f\n", 
00386                                                         msgStruct->ot, msgStruct->ot_err,
00387                                                         msgStruct->lat, msgStruct->lat_err,
00388                                                         msgStruct->lon, msgStruct->lon_err,
00389                                                         msgStruct->depth, msgStruct->depth_err);
00390                         fprintf (fp, "Magnitudes computed: \n");
00391                         fprintf (fp, "\t %c (%d) = %0.2f from %d phases\n", 
00392                                                                                 msgStruct->O_mag[0].magLabel,
00393                                                                                 msgStruct->O_mag[0].MagType,
00394                                                                                 msgStruct->O_mag[0].magAvg, 
00395                                                                                 msgStruct->O_mag[0].numStas);
00396                         fprintf (fp, "\t %c (%d) = %0.2f from %d phases\n", 
00397                                                                                 msgStruct->O_mag[1].magLabel,
00398                                                                                 msgStruct->O_mag[1].MagType,
00399                                                                                 msgStruct->O_mag[1].magAvg, 
00400                                                                                 msgStruct->O_mag[1].numStas);
00401                         fprintf (fp, "\t %c (%d) = %0.2f from %d phases\n", 
00402                                                                                 msgStruct->O_mag[2].magLabel,
00403                                                                                 msgStruct->O_mag[2].MagType,
00404                                                                                 msgStruct->O_mag[2].magAvg, 
00405                                                                                 msgStruct->O_mag[2].numStas);
00406                         fprintf (fp, "\t %c (%d) = %0.2f from %d phases\n", 
00407                                                                                 msgStruct->O_mag[3].magLabel,
00408                                                                                 msgStruct->O_mag[3].MagType,
00409                                                                                 msgStruct->O_mag[3].magAvg,     
00410                                                                                 msgStruct->O_mag[3].numStas);
00411                         fprintf (fp, "\t %c (%d) = %0.2f from %d phases\n", 
00412                                                                                 msgStruct->O_mag[4].magLabel,
00413                                                                                 msgStruct->O_mag[4].MagType,
00414                                                                                 msgStruct->O_mag[4].magAvg, 
00415                                                                                 msgStruct->O_mag[4].numStas);
00416                         
00417                         fprintf (fp, "\nThere were total of %d phases, of which %d were marked as used\n",
00418                                                         msgStruct->nph_assoc, msgStruct->nph_used);
00419                         fprintf (fp, "Std error (RMS) was %0.2f: \n", msgStruct->std_error);
00420                         fprintf (fp, "Of those, we parsed the following %d phases: \n", msgStruct->nph_actual);
00421                         for (i = 0; i < msgStruct->nph_actual; i++)
00422                         {
00423                           fprintf (fp, "%s %s (%c %c) %0.2f   %0.1f,%0.1f,%d MAGS  ",
00424                                                         msgStruct->phases[i].sta, msgStruct->phases[i].phase,
00425                                                         msgStruct->phases[i].onset, msgStruct->phases[i].motion,
00426                                                         msgStruct->phases[i].ot, msgStruct->phases[i].res,
00427                                                         msgStruct->phases[i].dist, msgStruct->phases[i].azm);
00428                         
00429                           for (j = 0; j < msgStruct->phases[i].num_mags; j++)
00430                           {
00431                             if (msgStruct->phases[i].mag[j].used == 1)
00432                               fprintf (fp, "USED %c (%d),%0.1f,%0.2f ==> %0.1f  ", 
00433                                         msgStruct->phases[i].mag[j].magLabel,
00434                                         msgStruct->phases[i].mag[j].MagType,
00435                                         msgStruct->phases[i].mag[j].value,
00436                                         msgStruct->phases[i].mag[j].period,
00437                                         msgStruct->phases[i].mag[j].mag);
00438                             else
00439                               fprintf (fp, "NOT USED %c (%d),%0.1f,%0.2f ==> %0.1f  ", 
00440                                         msgStruct->phases[i].mag[j].magLabel,
00441                                         msgStruct->phases[i].mag[j].MagType,
00442                                         msgStruct->phases[i].mag[j].value,
00443                                         msgStruct->phases[i].mag[j].period,
00444                                         msgStruct->phases[i].mag[j].mag);
00445                           }
00446                           fprintf (fp, "\n");
00447                         
00448                         }
00449 
00450                         fclose (fp);
00451                 }
00452                 
00453         }
00454         else
00455         {
00456                 logit ("", "GetNextLine returned EW_FAILURE; exitting\n");
00457                 return EW_FAILURE;
00458         }
00459 
00460 
00461         /* free up allocated space */
00462         for (i = 0; i < MAX_LINES; i++)
00463                 free ((void *) nsnbuf[i]);
00464         free ((void *) nsnbuf);
00465 
00466 /*
00467 logit ("", "BEFORE RETURN\n");
00468 for (i = 0; i < msgStruct->nph_actual; i++)
00469   logit ("", "Chan %d, <%s>\n", i, msgStruct->phases[i].sta);
00470 */
00471 
00472 
00473         return EW_SUCCESS;
00474 
00475 }
00476 
00477 
00478 /************************************************************************
00479  * ConvertNSNMsg () - fill the buffer with the stuff in msg.
00480  *   set nlines to the number of lines read.
00481  ************************************************************************/
00482 static  int             ConvertNSNMsg (char *msg, int msgsize, char **buffer, int *nlines)
00483 {
00484 
00485         int             i, j, done;
00486         char    *line;
00487 
00488         if ((buffer == NULL) || (msg == NULL) || (msgsize < 0))
00489         {
00490                 logit ("e", "ConvertNSNMsg: Invalid parameters passed in.\n");
00491                 return EW_FAILURE;
00492         }
00493 
00494         *nlines = 0;
00495 
00496         done = FALSE;
00497         i = 0;
00498         j = 0;
00499         while (done == FALSE)
00500         {
00501                 
00502                 line = buffer[*nlines];
00503 
00504                 while ((msg[i] != '\n') && (i < msgsize))
00505                 {
00506                         line[j] = msg[i];
00507                         i = i + 1;
00508                         j = j + 1;
00509                 }
00510                 if (msg[i] == '\n')
00511                 {
00515                         line[j] = '\n';
00516                         *nlines = *nlines + 1;
00517                         j = 0;
00518                         i = i + 1;
00519                 }
00520                 else
00521                 {
00522                         line[j] = '\0';
00523                         done = TRUE;
00524                 }
00525                         
00526         }
00527 
00528         return EW_SUCCESS;
00529 }
00530 
00531 
00532 
00533 /************************************************************************
00534  * GetNextLine () - return the next line from the message (in buffer)
00535  *  where the first word of the line is given by argument word.
00536  ************************************************************************/
00537 static  int     GetNextLine (char **buffer, int *lineno, char *word)
00538 {
00539 
00540         char    tmp[256];
00541         int             done;
00542 
00543         if ((buffer == NULL) || (word == NULL))
00544         {
00545                 logit ("", "Invalid arguments passed in.\n");
00546                 return EW_FAILURE;
00547         }
00548 
00549         done = FALSE;
00550         while (done == FALSE)
00551         {
00552                 if (strlen (buffer[*lineno]) == 1)
00553                         ;
00554                 else
00555                 {
00556                         /* Check the first word */
00557 
00558                         sscanf (buffer[*lineno], "%s\n", tmp);
00559 
00560                         if (strcmp (tmp, word) == 0)
00561                         {
00562                                 done = TRUE;
00563                         }
00564                         else
00565                                 ;
00566                 }
00567 
00568                 if (done != TRUE)
00569                         *lineno = *lineno + 1;
00570         }
00571 
00572         return EW_SUCCESS;
00573 }
00574 
00575 
00576 /************************************************************************
00577  * BuildDateStr () - Create a date string suitable for a call to 
00578  *   epochsec17, from an NSN format date string.
00579  ************************************************************************/
00580 static  int             BuildDateStr (char *in, char *out)
00581 {
00582 
00583         char    year[6];
00584         char    month[6];
00585         char    day[6];
00586 
00587         if ((in == NULL) || (out == NULL))
00588         {
00589                 logit ("", "Invalid arguments passed in.\n");
00590                 return EW_FAILURE;
00591         }
00592 
00593         strncpy (day, in, 2);
00594         day[2] = '\0';
00595 
00596         if (day[0] == ' ')
00597                 day[0] ='0';
00598 
00599         /* figure out the month */
00600         if (in[3] == 'J')
00601         {
00602                 if (in[4] == 'A')
00603                         sprintf (month, "01");
00604                 else if (in[4] == 'U')
00605                 {
00606                         if (in[5] == 'N')
00607                                 sprintf (month, "06");
00608                         else if (in[5] == 'L')
00609                                 sprintf (month, "07");
00610                         else
00611                         {
00612                                 logit ("", "Unknown month %s!\n", month);
00613                                 return EW_FAILURE;
00614                         }
00615                 }
00616                 else
00617                 {
00618                         logit ("", "Unknown month %s!\n", month);
00619                         return EW_FAILURE;
00620                 }
00621         }
00622         else if (in[3] == 'M')
00623         {
00624                 if (in[5] == 'R')
00625                         sprintf (month, "03");
00626                 else if (in[5] == 'Y')
00627                         sprintf (month, "05");
00628                 else
00629                 {
00630                         logit ("", "Unknown month %s!\n", month);
00631                         return EW_FAILURE;
00632                 }
00633         }
00634         else if (in[3] == 'A')
00635         {
00636                 if (in[4] == 'P')
00637                         sprintf (month, "04");
00638                 else if (in[4] == 'U')
00639                         sprintf (month, "08");
00640                 else
00641                 {
00642                         logit ("", "Unknown month %s!\n", month);
00643                         return EW_FAILURE;
00644                 }
00645         }
00646         else if (in[3] == 'F')
00647                 sprintf (month, "02");
00648         else if (in[3] == 'S')
00649                 sprintf (month, "09");
00650         else if (in[3] == 'O')
00651                 sprintf (month, "10");
00652         else if (in[3] == 'N')
00653                 sprintf (month, "11");
00654         else if (in[3] == 'D')
00655                 sprintf (month, "12");
00656         else
00657         {
00658                 logit ("", "Unknown month %s!\n", month);
00659                 return EW_FAILURE;
00660         }
00661 
00662         strncpy (year, (in + 7), 4);
00663         year[4] = '\0';
00664 
00665         sprintf (out, "%s%s%s", year, month, day);
00666 
00667 
00668         return EW_SUCCESS;
00669 
00670 }
00671 
00672 
00673 /************************************************************************
00674  * AppendDateStr () - Append hours, minutes, and seconds to the 
00675  *   date, to create a string suitable for a call to epochsec17
00676  ************************************************************************/
00677 static  int             AppendDateStr (char     *orig, char *append)
00678 {
00679 
00680         int             i, j;
00681         char    tmp[10];
00682 
00683         /* strip off ':'s and append to the old date string */
00684 
00685         if ((orig == NULL) || (append == NULL))
00686         {
00687                 logit ("", "Invalid arguments passed in.\n");
00688                 return EW_FAILURE;
00689         }
00690 
00691         i = 0;
00692         j = 0;
00693 
00694         for (i = 0; i < 11; i++)
00695         {
00696                 if (append[i] == ':')
00697                         ;
00698                 else
00699                 {
00700                         tmp[j] = append[i];
00701                         j = j + 1;
00702                 }
00703         }
00704         tmp[j] = '\0';
00705 
00706         strcat (orig, tmp);
00707 
00708         return EW_SUCCESS;
00709                         
00710 
00711 }
00712 
00713 /************************************************************************
00714  * ParseEllipse () - fill the Ellipse structure from the NSN formatted
00715  *   error elipse string
00716  ************************************************************************/
00717 static  int             ParseEllipse (char *str, double depth_err, Err_Elipse *error)
00718 {
00719 
00720         int             i, j, k, l, index;
00721         double  err[9];
00722         char    tmp[10];
00723 
00724         if ((str == NULL) || (error == NULL))
00725         {
00726                 logit ("", "Invalid arguments passed in.\n");
00727                 return EW_FAILURE;
00728         }
00729 
00730         /* Extract numbers */
00731         i = 0;
00732         index = 0;
00733         /* Loop over 3 sets of numbers */
00734         for (k = 0; k < 3; k++)
00735         {
00736                 /* Loop over 3 numbers in each set */
00737                 for (l = 0; l < 3; l++)
00738                 {
00739                         j = 0;
00740                         while ((i < ELLIPSE_LENGTH) && (str[i] != ',') && (str[i] != ';'))
00741                         {
00742                                 tmp[j] = str[i];
00743                                 j = j + 1;
00744                                 i = i + 1;
00745                         }
00746                         
00747                         /* Got a number, convert it */
00748                         tmp[j] = '\0';
00749                         err[index] = atof (tmp);
00750                         index = index + 1;
00751                         i = i + 1;
00752                 }
00753         }
00754 
00755 
00756         /* For some reason, the meaning of the ellipse changes
00757          * if depth is fixed:
00758          *
00759          *   ind 0  =  Semi-major strike
00760          *   ind 1  =  Semi-minor strike
00761          *   ind 2  =  Intermediate strike
00762          *
00763          *   ind 3  =  Semi-major dip
00764          *   ind 4  =  Semi-minor dip
00765          *   ind 5  =  Intermediate dip
00766          *
00767          *   ind 6  =  Semi-major length
00768          *   ind 7  =  Semi-minor length
00769          *   ind 8  =  Intermediate length
00770          *
00771          *  For non-fixed depth, the Semi-minor and intermediate axes
00772          *  are swapped.
00773          *
00774          */
00775 
00776         if (depth_err != 0.0)
00777         {
00778                 error->maj_s = err[0];
00779                 error->maj_d = err[3];
00780                 error->maj_l = err[6];
00781 
00782                 error->int_s = err[1];
00783                 error->int_d = err[4];
00784                 error->int_l = err[7];
00785 
00786                 error->min_s = err[2];
00787                 error->min_d = err[5];
00788                 error->min_l = err[8];
00789         }
00790         else
00791         {
00792                 error->maj_s = err[0];
00793                 error->maj_d = err[3];
00794                 error->maj_l = err[6];
00795 
00796                 error->min_s = err[1];
00797                 error->min_d = err[4];
00798                 error->min_l = err[7];
00799 
00800                 error->int_s = err[2];
00801                 error->int_d = err[5];
00802                 error->int_l = err[8];
00803         }
00804 
00805         return EW_SUCCESS;
00806 
00807 }
00808 
00809 /************************************************************************
00810  * ParseMagnitude () - fill the Mag structure from the NSN formatted
00811  *   magnitudes string
00812  ************************************************************************/
00813 static  int             ParseMagnitude (char *str, OriginMag *mag)
00814 {
00815 
00816         char    tmp[10];
00817 
00818         if ((str == NULL) || (mag == NULL))
00819         {
00820                 logit ("", "Invalid arguments passed in.\n");
00821                 return EW_FAILURE;
00822         }
00823 
00824 
00825         /* HACK: we only know about duration and local mags */
00826         
00827         /* Mb */
00828         mag[0].magLabel = 'b';
00829         mag[0].MagType = MAGTYPE_LOCAL_ZERO2PEAK;
00830         strncpy (tmp, (str + MB_BASE), MAG_LEN);
00831         tmp[MAG_LEN] = '\0';
00832         mag[0].magAvg = (double) atof (tmp);
00833 
00834         strncpy (tmp, (str + MB_DATUM), DATUM_LEN);
00835         tmp[DATUM_LEN] = '\0';
00836         mag[0].numStas = (int) atoi (tmp);
00837 
00838 
00839         /* ML */
00840         mag[1].magLabel = 'L';
00841         mag[1].MagType = MAGTYPE_LOCAL_ZERO2PEAK;
00842         strncpy (tmp, (str + ML_BASE), MAG_LEN);
00843         tmp[MAG_LEN] = '\0';
00844         mag[1].magAvg = (double) atof (tmp);
00845 
00846         strncpy (tmp, (str + ML_DATUM), DATUM_LEN);
00847         tmp[DATUM_LEN] = '\0';
00848         mag[1].numStas = (int) atoi (tmp);
00849 
00850 
00851         /* Mblg */
00852         mag[2].magLabel = 'g';
00853         mag[2].MagType = MAGTYPE_LOCAL_ZERO2PEAK;
00854         strncpy (tmp, (str + MBLG_BASE), MAG_LEN);
00855         tmp[MAG_LEN] = '\0';
00856         mag[2].magAvg = (double) atof (tmp);
00857 
00858         strncpy (tmp, (str + MBLG_DATUM), DATUM_LEN);
00859         tmp[DATUM_LEN] = '\0';
00860         mag[2].numStas = (int) atoi (tmp);
00861 
00862 
00863         /* Md */
00864         mag[3].magLabel = 'd';
00865         mag[3].MagType = MAGTYPE_DURATION;
00866         strncpy (tmp, (str + MD_BASE), MAG_LEN);
00867         tmp[MAG_LEN] = '\0';
00868         mag[3].magAvg = (double) atof (tmp);
00869 
00870         strncpy (tmp, (str + MD_DATUM), DATUM_LEN);
00871         tmp[DATUM_LEN] = '\0';
00872         mag[3].numStas = (int) atoi (tmp);
00873 
00874 
00875         /* MS */
00876         mag[4].magLabel = 'S';
00877         mag[4].MagType = MAGTYPE_LOCAL_ZERO2PEAK;
00878         strncpy (tmp, (str + MS_BASE), MAG_LEN);
00879         tmp[MAG_LEN] = '\0';
00880         mag[4].magAvg = (double) atof (tmp);
00881 
00882         strncpy (tmp, (str + MS_DATUM), DATUM_LEN);
00883         tmp[DATUM_LEN] = '\0';
00884         mag[4].numStas = (int) atoi (tmp);
00885 
00886         return EW_SUCCESS;
00887 }
00888 
00889 /************************************************************************
00890  * ParsePhase () - fill the Phase structure from the NSN formatted
00891  *   phase string
00892  ************************************************************************/
00893 static  int             ParsePhase (char *str, Phase *phase, char *EventDate, 
00894                                                                         int *num_phases, double *Dmin)
00895 {
00896         char    tmp[256];
00897         char    datestr[256];
00898         int             i, j;
00899 
00900         if ((str == NULL) || (phase == NULL))
00901         {
00902                 logit ("", "Invalid arguments passed in\n");
00903                 return EW_FAILURE;
00904         }
00905 
00906 
00907         /* ingore phases that don't start with the station name */
00908         if (str[1] == ' ')
00909         {
00910                 return EW_SUCCESS;
00911         }
00912 
00913 
00914         strncpy (phase->sta, (str + STA_BASE), STA_LEN);
00915         phase->sta[STA_LEN] = '\0';
00916 
00917         strncpy (tmp, (str + PHA_BASE), PHA_LEN);
00918         tmp[PHA_LEN] = '\0';
00919 
00920         j = 0;
00921         phase->onset = ' ';
00922         phase->motion = ' ';
00923         for (i = 0; i < PHA_LEN; i++)
00924         {
00925                 if (tmp[i] == '?')
00926                         phase->automatic = 1;
00927                 /* onset codes */
00928                 else if (tmp[i] == 'i')
00929                         phase->onset = 'i';
00930                 else if (tmp[i] == 'e')
00931                         phase->onset = 'e';
00932                 else if (tmp[i] == 'q')
00933                         phase->onset = 'q';
00934 
00935                 /* first motion codes */
00936                 else if (tmp[i] == 'c')
00937                         phase->motion = 'c';
00938                 else if (tmp[i] == 'd')
00939                         phase->motion = 'd';
00940                 else if (tmp[i] == 'u')
00941                         phase->motion = 'u';
00942                 else if (tmp[i] == 'r')
00943                         phase->motion = 'r';
00944                 else if (tmp[i] == ' ')
00945                         ;
00946 
00947                 /* part of the phase code */
00948                 else
00949                 {
00950                         phase->phase[j] = tmp[i];
00951                         j = j + 1;
00952                 }
00953         }
00954 
00955         if (j >= PHA_LEN)
00956         {
00957                 logit ("e", "MAX Phase length (%d) exceeded!\n", PHA_LEN);
00958                 return EW_FAILURE;
00959         }
00960         phase->phase[j] = '\0';
00961 
00962         strncpy (tmp, (str + OT_BASE), OT_LEN);
00963         tmp[OT_LEN] = '\0';
00964 
00965         strcpy (datestr, EventDate);
00966         if (AppendDateStr (datestr, tmp) != EW_SUCCESS)
00967         {
00968                 logit ("", "Call to AppendDateStr failed.\n");
00969                 return EW_FAILURE;
00970         }
00971         epochsec17 (&phase->ot, datestr);
00972 
00973 
00974         strncpy (tmp, (str + RES_BASE), RES_LEN);
00975         tmp[RES_LEN] = '\0';
00976         phase->res = (double) atof (tmp);
00977 
00978         if (str[RES_USED] == 'X')
00979                 phase->res_used = 1;
00980         else
00981                 phase->res_used = 0;
00982 
00983         strncpy (tmp, (str + DIST_BASE), DIST_LEN);
00984         tmp[DIST_LEN] = '\0';
00985         phase->dist = (double) atof (tmp);
00986 
00987         /* convert from degrees to km */
00988         phase->dist = (phase->dist * EARTH_CIRCUM)/360.0;
00989 
00990         if (phase->dist <= *Dmin)
00991         {
00992                 *Dmin = phase->dist;
00993         }
00994 
00995         strncpy (tmp, (str + AZM_BASE), AZM_LEN);
00996         tmp[AZM_LEN] = '\0';
00997         phase->azm = (int) atoi (tmp);
00998 
00999 
01000         /* magnitudes */
01001         phase->num_mags = 0;
01002         if (str[MAG1_BASE] != ' ')
01003         {
01004                 if (ParsePhaseMagnitude ((str+MAG1_BASE), &phase->mag[0]) != EW_SUCCESS)
01005                 {
01006                         logit ("", "Call to ParsePhaseMagnitude failed\n");
01007                         return EW_FAILURE;
01008                 }
01009                 phase->num_mags = phase->num_mags + 1;
01010 
01011         }
01012         if (str[MAG2_BASE] != ' ')
01013         {
01014 
01015                 if (ParsePhaseMagnitude ((str+MAG2_BASE), &phase->mag[1]) != EW_SUCCESS)
01016                 {
01017                         logit ("", "Call to ParsePhaseMagnitude failed\n");
01018                         return EW_FAILURE;
01019                 }
01020                 phase->num_mags = phase->num_mags + 1;
01021         }
01022 
01023         *num_phases = *num_phases + 1;
01024         return EW_SUCCESS;
01025 }
01026 
01027 /************************************************************************
01028  * ParsePhaseMagnitude () - fill the PhaseMag structure from the NSN 
01029  *   formatted phase string
01030  ************************************************************************/
01031 static  int             ParsePhaseMagnitude (char *str, PhaseMag *mag)
01032 {
01033         char    *ptr;
01034         char    tmp[5];
01035         int             expn, mult;
01036 
01037         if ((str == NULL) || (mag == NULL))
01038         {
01039                 logit ("", "Invalid arguments passed in\n");
01040                 return EW_FAILURE;
01041         }
01042 
01043 
01044         /* Hack: we only know duration and local mags */
01045         mag->magLabel = str[0];
01046         if (mag->magLabel == 'b')
01047                 mag->MagType = MAGTYPE_LOCAL_ZERO2PEAK;
01048         else if (mag->magLabel == 'L')
01049                 mag->MagType = MAGTYPE_LOCAL_ZERO2PEAK;
01050         else if (mag->magLabel == 'g') /* HACK -- we don't have Mblg type yet */
01051                 mag->MagType = MAGTYPE_LOCAL_ZERO2PEAK;
01052         else if (mag->magLabel == 'd')
01053                 mag->MagType = MAGTYPE_DURATION;
01054         else if (mag->magLabel == 'S')
01055                 mag->MagType = MAGTYPE_LOCAL_ZERO2PEAK;
01056         else
01057         {
01058                 logit ("", "Invalid magnitude type: %c\n", mag->magLabel);
01059                 return EW_FAILURE;
01060         }
01061 
01062         ptr = str + 2;
01063         strncpy (tmp, ptr, 3);
01064         tmp[3] = '\0';
01065         mag->value = (double) atof (tmp);
01066 
01067         ptr = ptr + 3;
01068         if (ptr[0] == '+')
01069                 mult = 1;
01070         else if (ptr[0] == '-')
01071                 mult = -1;
01072 
01073         ptr = ptr + 1;
01074         strncpy (tmp, ptr, 1);
01075         tmp[1] = '\0';
01076         expn = mult * ((int) atoi (tmp));
01077         mag->value = (double) (mag->value * pow (10.0, (double) expn));
01078 
01079         ptr = ptr + 1;
01080         strncpy (tmp, ptr, 4);
01081         tmp[4] = '\0';
01082         mag->period = (double) atof (tmp);
01083 
01084         ptr = ptr + 5;
01085         strncpy (tmp, ptr, 3);
01086         tmp[3] = '\0';
01087         mag->mag = (double) atof (tmp);
01088 
01089         ptr = ptr + 3;
01090 
01091         if (ptr[0] != 'X')
01092                 mag->used = 1;
01093         else
01094                 mag->used = 0;
01095 
01096 
01097         return EW_SUCCESS;
01098 
01099 }
01100 
01101 
01102 

Generated on Tue May 6 09:16:07 2003 for Earthworm Libs by doxygen1.3-rc3