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

# glevt_2_ewevent.c

Go to the documentation of this file.

    00001 /*
    00002  *   THIS FILE IS UNDER RCS - DO NOT MODIFY UNLESS YOU HAVE
    00003  *   CHECKED IT OUT USING THE COMMAND CHECKOUT.
    00004  *
    00005  *    $Id: glevt__2__ewevent_8c-source.html,v 1.1 2006/05/19 16:55:02 paulf Exp $
    00006  *
    00007  *    Revision history:
    00008  *     $Log: glevt__2__ewevent_8c-source.html,v $
    00008  *     Revision 1.1  2006/05/19 16:55:02  paulf
    00008  *     first inclusion
    00008  *
    00009  *     Revision 1.3  2002/07/16 20:03:20  davidk
    00010  *     removed //comments
    00011  *
    00012  *     Revision 1.2  2002/07/16 19:48:12  davidk
    00013  *     Changed the code that parses the global event message, so
    00014  *     that it reads the message one line at a time, instead of a fixed
    00015  *     number of bytes (supposing a line) at a time.
    00016  *     By reading exactly 96 bytes for the first line, the code bombed
    00017  *     when reading the message as a text file on NT because of the
    00018  *     0x0d 0x0a (CR LF) 2 character EOL sequence.  The 2 char sequence
    00019  *     caused there to be 1 extra char on each line, and thus the parser
    00020  *     was off by 1 extra char(byte) each line.
    00021  *     The new code extracts one line per time from the message, no matter
    00022  *     the line's length, but then uses the same sscanf() parsing code
    00023  *     as before.
    00024  *
    00025  *     Revision 1.1  2002/06/28 21:06:22  lucky
    00026  *     Initial revision
    00027  *
    00028  *
    00029  *
    00030  */
    00031
    00032
    00033 /*
    00034  * glevt_2_ewevent.c :  Routines to produce and parse global solution
    00035  *   messages of type TYPE_EVENTGLOBAL
    00036  *
    00037  *
    00038  */
    00039
    00040 #include <stdio.h>
    00041 #include <stdlib.h>
    00042 #include <string.h>
    00043 #include <time.h>
    00044 #include <math.h>
    00045 #include <chron3.h>
    00046 #include <earthworm.h>
    00047 #include <rw_glevt.h>
    00048
    00049 /******************************************************************************
    00050  GlEvt2EWEvent() fills EWEvent structure from a string in GlobalEvent format
    00051 ******************************************************************************/
    00052 int     GlEvt2EWEvent (EWEventInfoStruct *pEWEvent, char *pGlEvt, int GlEvtLen)
    00053 {
    00054
    00055         char            line[256];
    00056         char            *tmpstr;
    00057         int                     i;
    00058         GlarcSum        Sum;
    00059         GlarcPhase      Phase;
    00060   char    *pCurrLine, *pEOL;
    00061   int     iLineNum=1;
    00062   int     iLineSize;
    00063
    00064
    00065
    00066
    00067         if ((pEWEvent == NULL) || (pGlEvt == NULL) || (GlEvtLen <= 0))
    00068         {
    00069                 logit ("", "Invalid arguments passed in.\n");
    00070                 return EW_FAILURE;
    00071         }
    00072
    00073
    00074   /* set iLineSize for later user */
    00075   iLineSize = sizeof(line);
    00076
    00077         /* First read the summary line */
    00078
    00079   pCurrLine = pGlEvt;
    00080   pEOL = strchr(pCurrLine,'\n');
    00081   if(!pEOL)
    00082   {
    00083     logit("","Error reading line %d from global event message\n",iLineNum);
    00084     return(EW_FAILURE);
    00085   }
    00086   *pEOL = (char)NULL;
    00087   strncpy(line,pCurrLine,iLineSize);
    00088   pCurrLine = pEOL + 1;
    00089   iLineNum++;
    00090
    00091 /*      strncpy (line, pGlEvt, 78);
    00092         line[78] = '\0';
    00093 */
    00094         if (read_glevt_sumcard (&Sum, line) != EW_SUCCESS)
    00095         {
    00096                 logit ("", "Call to read_glevt_sumcard failed.\n");
    00097                 return EW_FAILURE;
    00098         }
    00099
    00100         /* Initialize the Event structure */
    00101         if (InitEWEvent (pEWEvent) != EW_SUCCESS)
    00102         {
    00103                 logit ("", "Call to InitEWEvent failed.\n");
    00104                 return EW_FAILURE;
    00105         }
    00106
    00107
    00108         /*
    00109          * Set magnitude values to 0 as we don't have any info on it here
    00110          */
    00111         pEWEvent->iNumMags = 0;
    00112         pEWEvent->iPrefMag = -1;
    00113         pEWEvent->iMd = -1;
    00114         pEWEvent->iML = -1;
    00115
    00116         pEWEvent->iNumChans = Sum.nph;
    00117         pEWEvent->Event.idEvent = Sum.qid;
    00118         pEWEvent->PrefOrigin.tOrigin = Sum.ot;
    00119         pEWEvent->PrefOrigin.dLat = Sum.lat;
    00120         pEWEvent->PrefOrigin.dLon = Sum.lon;
    00121         pEWEvent->PrefOrigin.dDepth = Sum.z;
    00122         pEWEvent->PrefOrigin.dRms = Sum.rms;
    00123         pEWEvent->PrefOrigin.dDmin = (float) Sum.dmin;
    00124         pEWEvent->PrefOrigin.iGap = Sum.gap;
    00125
    00126         pEWEvent->PrefOrigin.iAssocRd = Sum.nph;
    00127         pEWEvent->PrefOrigin.iAssocPh = Sum.nph;
    00128         pEWEvent->PrefOrigin.iUsedRd = Sum.nph;
    00129         pEWEvent->PrefOrigin.iUsedPh = Sum.nph;
    00130
    00131         pEWEvent->PrefOrigin.BindToEvent = TRUE;
    00132         pEWEvent->PrefOrigin.SetPreferred = TRUE;
    00133
    00134
    00135         if (pEWEvent->iNumChans > pEWEvent->iNumAllocChans)
    00136         {
    00137                 free (pEWEvent->pChanInfo);
    00138
    00139                 if ((pEWEvent->pChanInfo = (EWChannelDataStruct *) malloc
    00140                                                 (pEWEvent->iNumChans * sizeof (EWChannelDataStruct))) == NULL)
    00141                 {
    00142                         logit ("", "Could not malloc %d channel info structs\n", pEWEvent->iNumChans);
    00143                         return EW_FAILURE;
    00144                 }
    00145
    00146                 pEWEvent->iNumAllocChans = pEWEvent->iNumChans;
    00147
    00148                 for (i = 0; i < pEWEvent->iNumAllocChans; i++)
    00149                 {
    00150                         InitEWChan(&pEWEvent->pChanInfo[i]);
    00151                 }
    00152         }
    00153
    00154
    00155         for (i = 0; i < pEWEvent->iNumChans; i++)
    00156         {
    00157
    00158     pEOL = strchr(pCurrLine,'\n');
    00159     if(!pEOL)
    00160     {
    00161       logit("","Error reading line %d from global event message\n",iLineNum);
    00162       return(EW_FAILURE);
    00163     }
    00164     *pEOL = (char)NULL;
    00165     strncpy(line,pCurrLine,iLineSize);
    00166     pCurrLine = pEOL + 1;
    00167     iLineNum++;
    00168
    00169
    00170
    00171
    00172                 if (read_glevt_phasecard (&Phase, line) != EW_SUCCESS)
    00173                 {
    00174                         logit ("", "Call to read_glevt_phasecard failed.\n");
    00175                         return EW_FAILURE;
    00176                 }
    00177
    00178                 strcpy (pEWEvent->pChanInfo[i].Station.Sta, Phase.sta);
    00179                 strcpy (pEWEvent->pChanInfo[i].Station.Comp, Phase.comp);
    00180                 strcpy (pEWEvent->pChanInfo[i].Station.Net, Phase.net);
    00181
    00182     /* the location should be null, not a single <SPACE> DK 071002 */
    00183                 strcpy (pEWEvent->pChanInfo[i].Station.Loc, "");
    00184
    00185
    00186                 /* Arrival pick info */
    00187                 pEWEvent->pChanInfo[i].iNumArrivals = 1;
    00188                 pEWEvent->pChanInfo[i].Arrivals[0].cMotion  = Phase.fm;
    00189
    00190                 if (Phase.wt == 3)
    00191                         pEWEvent->pChanInfo[i].Arrivals[0].dSigma  = 0.08;
    00192                 else if (Phase.wt == 2)
    00193                         pEWEvent->pChanInfo[i].Arrivals[0].dSigma  = 0.05;
    00194                 else if (Phase.wt == 1)
    00195                         pEWEvent->pChanInfo[i].Arrivals[0].dSigma  = 0.03;
    00196                 else if (Phase.wt == 0)
    00197                         pEWEvent->pChanInfo[i].Arrivals[0].dSigma  = 0.02;
    00198                 else
    00199                         pEWEvent->pChanInfo[i].Arrivals[0].dSigma  = 99.99;
    00200
    00201
    00202                 pEWEvent->pChanInfo[i].Arrivals[0].tCalcPhase  = Phase.at;
    00203                 strcpy (pEWEvent->pChanInfo[i].Arrivals[0].szCalcPhase, Phase.phase);
    00204                 pEWEvent->pChanInfo[i].Arrivals[0].tObsPhase  = Phase.at;
    00205                 strcpy (pEWEvent->pChanInfo[i].Arrivals[0].szObsPhase, Phase.phase);
    00206                 strcpy (pEWEvent->pChanInfo[i].Arrivals[0].szExtSource, Phase.pickAuthor);
    00207                 strcpy (pEWEvent->pChanInfo[i].Arrivals[0].szExternalPickID, Phase.ExtPickID);
    00208
    00209
    00210                 /* Amplitude information */
    00211                 pEWEvent->pChanInfo[i].iNumStaMags = 1;
    00212                 pEWEvent->pChanInfo[i].Stamags[0].StaMagUnion.PeakAmp.dAmp1 = (float) Phase.pAmp;
    00213                 pEWEvent->pChanInfo[i].Stamags[0].StaMagUnion.PeakAmp.tAmp1 = Phase.ampTime;
    00214                 pEWEvent->pChanInfo[i].Stamags[0].StaMagUnion.PeakAmp.dAmpPeriod1 = (float) Phase.period;
    00215
    00216                 pEWEvent->pChanInfo[i].Stamags[0].StaMagUnion.PeakAmp.dAmp2 = (float) MAG_NULL;
    00217                 pEWEvent->pChanInfo[i].Stamags[0].StaMagUnion.PeakAmp.tAmp2 = (float) MAG_NULL;
    00218                 pEWEvent->pChanInfo[i].Stamags[0].StaMagUnion.PeakAmp.dAmpPeriod2 = (float) MAG_NULL;
    00219
    00220
    00221                 tmpstr = tmpstr + 97;
    00222         }
    00223
    00224         return EW_SUCCESS;
    00225 }
    00226
    00227
    00228
    00229 /*********************************************************************************/
    00230 /* read_glevt_sumcard() reads the first line of a global arc message             */
    00231 /*                               Fills information in the GlarcSum structure                     */
    00232 /*********************************************************************************/
    00233 int     read_glevt_sumcard (GlarcSum *pSum, char *sumcard )
    00234 {
    00235         char    otime[16], lat[16], lon[16], depth[16], version[16];
    00236         char    nph[16], gap[16], dmin[16], rms[16], evtid[16];
    00237         int             narg;
    00238
    00239         if ((pSum == NULL) || (sumcard == NULL))
    00240         {
    00241                 logit ("", "Invalid arguments passed in.\n");
    00242                 return EW_FAILURE;
    00243         }
    00244
    00245         /* Here is what it should look like:
    00246
    00247                 20020603191112.39  52.4299 -120.0782  399.32   9    0     0  3.18       1139 1
    00248                 20020430185616.12 -12.1426 -112.8750  200.00  14    0     0  1.39         14 1
    00249                 01234567890123456789012345678901234567890123456789012345678901234567890123456789
    00250         */
    00251
    00252         narg =  sscanf (sumcard, "%s %s %s %s %s %s %s %s %s %s",
    00253                                                 otime, lat, lon, depth,  nph,
    00254                                                 gap, dmin, rms, evtid, version);
    00255
    00256         pSum->nph = atoi (nph);
    00257         pSum->qid = atoi (evtid);
    00258         epochsec17 (&pSum->ot, otime);
    00259         pSum->lat = (float) atof (lat);
    00260         pSum->lon = (float) atof (lon);
    00261         pSum->z = (float) atof (depth);
    00262         pSum->rms = (float) atof (rms);
    00263         pSum->dmin = atoi (dmin);
    00264         pSum->gap = atoi (gap);
    00265
    00266         return EW_SUCCESS;
    00267 }
    00268
    00269
    00270 /*********************************************************************************/
    00271 /* read_glevt_phasecard() reads the phase lines of a global arc message          */
    00272 /*                               Fills information in the GlarcPhase structure                   */
    00273 /*********************************************************************************/
    00274 int     read_glevt_phasecard (GlarcPhase *pPhase, char *phasecard )
    00275 {
    00276         char    atime[32], sta[16], comp[16], net[16], fmwt[16], label[16];
    00277         char    amp[16], period[16], amptime[32], author[32], pickID[32];
    00278         int             narg;
    00279
    00280         if ((pPhase == NULL) || (phasecard == NULL))
    00281         {
    00282                 logit ("", "Invalid arguments passed in.\n");
    00283                 return EW_FAILURE;
    00284         }
    00285
    00286
    00287         /* Here is what it should look like:
    00288
    00289                 CMN  VHZ NC U1 xxxxxxxxxP 19950831183134.90      275   0.2 20020531195801.29\n
    00290                 CMN  VHZ NC U1 xxxxxxxxxP 19950831183134.90 xxxxauthor ExtPckID      275   0.2 20020531195801.29\n
    00291                 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678
    00292         */
    00293
    00294
    00295         narg =  sscanf (phasecard, "%s %s %s %s %s %s %s %s %s %s %s %s",
    00296                                                 sta, comp, net, fmwt, label, atime, author, pickID, amp, period, amptime);
    00297
    00298
    00299         strcpy (pPhase->sta, sta);
    00300         strcpy (pPhase->comp, comp);
    00301         strcpy (pPhase->net, net);
    00302         strcpy (pPhase->phase, label);
    00303         strcpy (pPhase->pickAuthor, author);
    00304         strcpy (pPhase->ExtPickID, pickID);
    00305         epochsec17 (&pPhase->at, atime);
    00306
    00307         epochsec17 (&pPhase->ampTime, amptime);
    00308         pPhase->pAmp = (long) atoi (amp);
    00309         pPhase->period = atof (period);
    00310
    00311         if (strlen (fmwt) == 2)
    00312         {
    00313                 pPhase->fm = fmwt[0];
    00314                 pPhase->wt = fmwt[1] - '0';
    00315         }
    00316         else
    00317         {
    00318                 pPhase->fm = ' ';
    00319                 pPhase->wt = fmwt[0] - '0';
    00320         }
    00321
    00322         return EW_SUCCESS;
    00323 }
    00324
    00325
    00326
    00327
    00328 /*******************************************************************************************/
    00329 /* write_glevt_phasecard() builds a character-string phase-card from GlarcPhase structure  */
    00330 /*******************************************************************************************/
    00331 int     write_glevt_phasecard (GlarcPhase *pPhs, char *phscard )
    00332 {
    00333         char   line[120];
    00334         char   timestr[18];
    00335         char   timestr2[18];
    00336
    00337         if ((pPhs == NULL) || (phscard == NULL))
    00338         {
    00339                 logit ("", "Invalid arguments passed in.\n");
    00340                 return EW_FAILURE;
    00341         }
    00342
    00343 /*----------------------------------------------------------------------
    00344 Sample format phase card
    00345 CMN  VHZ NC U1 xxxxxxxxxP 19950831183134.90      275   0.2 20020531195801.29\n
    00346 CMN  VHZ NC U1 xxxxxxxxxP 19950831183134.90 xxxxauthor ExtPckID      275   0.2 20020531195801.29\n
    00347 0123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
    00348
    00349 -------------------------------------------------------------------------*/
    00350
    00351 /***  Convert julian seconds character string  ***/
    00352                 date17( pPhs->at, timestr );
    00353
    00354                 if (pPhs->ampTime <= 0)
    00355                         strcpy (timestr2, "            -1.00");
    00356                 else
    00357                         date17( pPhs->ampTime, timestr2 );
    00358
    00359
    00360 /***  Write all info to an global phase card  ***/
    00361                 sprintf( line,     "%-5s ",      pPhs->sta);
    00362                 sprintf( line+5,   "%3s ",       pPhs->comp);
    00363                 sprintf( line+9,   "%2s ",       pPhs->net);
    00364                 sprintf( line+12,  "%c",         pPhs->fm);
    00365                 sprintf( line+13,  "%c ",        pPhs->wt);
    00366                 sprintf( line+15,  "%10s ",      pPhs->phase);
    00367                 sprintf( line+26,  "%17s ",      timestr);
    00368                 sprintf( line+44,  "%10s ",  pPhs->pickAuthor);
    00369                 sprintf( line+55,  "%8s ",   pPhs->ExtPickID);
    00370                 sprintf( line+64,  "%8d ",       pPhs->pAmp);
    00371                 sprintf( line+73,  "%5.1f ", pPhs->period);
    00372                 sprintf( line+79,  "%17s\n\0",   timestr2);
    00373
    00374                 strcpy( phscard, line );
    00375                 return (EW_SUCCESS);
    00376 }
    00377
    00378 /*********************************************************************************/
    00379 /* write_glevt_sumcard() builds a summary card for global arc message            */
    00380 /*                               from the RPT_HYP structure                                                                      */
    00381 /*********************************************************************************/
    00382 int     write_glevt_sumcard (GlarcSum *pSum, char *sumcard )
    00383 {
    00384                 char   line[109];
    00385                 char   timestr[18];
    00386
    00387         if ((pSum == NULL) || (sumcard == NULL))
    00388         {
    00389                 logit ("", "Invalid arguments passed in.\n");
    00390                 return EW_FAILURE;
    00391         }
    00392
    00393 /*----------------------------------------------------------------------------------------
    00394 Proposed Global hypocenter built below. Base format is Hypo71; event id from Glass
    00395   (78 chars, including newline):
    00396 0123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
    00397 20020501181423.40  54.4589 -108.6209  100.00  11    0     0  7.03         36 1
    00398 20020430185616.12 -12.1426 -112.8750  200.00  14    0     0  1.39         14 1
    00399 ------------------------------------------------------------------------------------------*/
    00400
    00401 /***  Convert julian seconds character string  ***/
    00402                 date17( pSum->ot, timestr );
    00403
    00404 /***  Write all info to an Earthworm hypocenter card  ***/
    00405                 sprintf( line,    "%17s ",       timestr          );
    00406                 sprintf( line+18, "%8.4f ",      pSum->lat        );
    00407                 sprintf( line+27, "%9.4f ",      pSum->lon        );
    00408                 sprintf( line+37, "%7.2f ",      pSum->z          );
    00409                 sprintf( line+45, "%3d ",        pSum->nph  );
    00410                 sprintf( line+49, "%4.0f ",      pSum->gap  );
    00411                 sprintf( line+54, "%5.0f ",      pSum->dmin );
    00412                 sprintf( line+60, "%5.2f ",      pSum->rms  );
    00413                 sprintf( line+66, "%10ld ",      pSum->qid   );
    00414                 sprintf( line+77, "1\n\0");
    00415
    00416                 strcpy( sumcard, line );
    00417
    00418                 return (EW_SUCCESS);
    00419 }
    00420
    00421
    00422