source: trunk/src/libsrc/util/read_arc.c @ 4928

Revision 4928, 17.8 KB checked in by paulf, 6 years ago (diff)

cleaned up some minor compiler warnings in read_arc.c

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1
2/*
3 *   THIS FILE IS UNDER RCS - DO NOT MODIFY UNLESS YOU HAVE
4 *   CHECKED IT OUT USING THE COMMAND CHECKOUT.
5 *
6 *    $Id$
7 *
8 *    Revision history:
9 *     $Log$
10 *     Revision 1.10  2009/08/11 14:21:23  quintiliani
11 *     Set numerical value for the origin version from the ARC Summary
12 *
13 *     Revision 1.9  2004/05/28 22:35:52  dietz
14 *     changed read_phs to read loc field from phase line
15 *
16 *     Revision 1.8  2002/12/06 22:30:45  dietz
17 *     Added 3 fields (nphS,nphtot,nPfm) to read_hyp()
18 *
19 *     Revision 1.7  2002/10/29 18:47:42  lucky
20 *     Added origin version number
21 *
22 *     Revision 1.6  2000/12/06 17:50:32  lucky
23 *     Make sure that the onset is read and stored
24 *
25 *     Revision 1.5  2000/09/12 19:07:34  lucky
26 *     Defined separate FM fields for S and P picks
27 *
28 *     Revision 1.4  2000/08/21 19:49:39  lucky
29 *     Fixed read_phs so that the Hpck structure is filled with the information
30 *     for both the P and S wave picks.
31 *
32 *     Revision 1.3  2000/04/04 22:51:23  davidk
33 *     changed the starting column position for the read of sumP->e1dp
34 *     (the intermediate principal error) from 61 to 64.  Due to the error,
35 *     e1dp was being set to an excessively large value that caused an
36 *     error to occur in orareport as it inserted the bad value into the DB.
37 *
38 *     Revision 1.2  2000/03/30 15:43:39  davidk
39 *     added code to handle additional arc message fields in both
40 *     the summary and phase lines, that are used by orareport,
41 *     so that orareport can use the read_arc routines.
42 *
43 *     Revision 1.1  2000/02/14 18:51:48  lucky
44 *     Initial revision
45 *
46 *
47 */
48
49/* Routines for reading HYP2000ARC summary lines and phase lines.
50 * These routines are provided for the use of arc2trig and two UW-spcific
51 * modules, uw_report and eqtee. They only read the fields that are
52 * required by these modules.
53 * If you need to read more info from the HYP2000ARC message, feel free to
54 * make additions here. You probably will need to add to the structures in
55 * read_arc.h as well.
56 * Completely rewritten by Pete Lombard, 12/03/1998
57 */
58
59#include <stdio.h>
60#include <stdlib.h>  /* for atof() */
61#include <string.h>
62#include <chron3.h>
63#include <read_arc.h>
64
65/********************************
66 * Internal function prototypes *
67 ********************************/
68static int strib( char* );
69static void read_fl2( char*, float* );
70static void read_fl1( char*, float* );
71static void read_i(char*, int* );
72
73/********************************
74 * External function prototypes *
75 ********************************/
76void logit( char *, char *, ... );          /* logit.c      sys-independent  */
77
78
79/* this used to live in hyp2000_mgr, but it could be used elsewhere - quality for H71Sum card */
80char ComputeAverageQuality(float rms, float erh, float erz, float depth, float dmin, int no, int gap)
81{
82   int qs, qd, qa;
83/* Compute qs, qd, and average quality
84   ***********************************/
85   if     (rms <0.15 && erh<=1.0 && erz <= 2.0) qs=4;  /* qs is A */
86   else if(rms <0.30 && erh<=2.5 && erz <= 5.0) qs=3;  /* qs is B */
87   else if(rms <0.50 && erh<=5.0)               qs=2;  /* qs is C */
88   else                                         qs=1;  /* qs is D */
89
90   if     (no >= 6 && gap <=  90 && (dmin<=depth    || dmin<= 5)) qd=4; /* qd is A */
91   else if(no >= 6 && gap <= 135 && (dmin<=2.*depth || dmin<=10)) qd=3; /* qd is B */
92   else if(no >= 6 && gap <= 180 &&  dmin<=50)                    qd=2; /* qd is C */
93   else                                                           qd=1; /* qd is D */
94
95   qa = (qs+qd)/2; /* use integer truncation to round down */
96   if(qa>=4) return('A');
97   if(qa==3) return('B');
98   if(qa==2) return('C');
99   if(qa<=1) return('D');
100
101   return(' '); /* should never reach here */
102}
103/**************************************************************
104 * read_hyp() reads the hypocenter line from an archive msg into an
105 *            Hsum structure (defined in read_arc.h)
106 *
107 *      Inputs: sumLine - character string holding the summary line
108 *              shdw - character string holding the summary shadow line
109 *              sumP - pointer to Hsum structure provided by the caller
110 *      returns 0 on success, -1 on parsing errors
111 *
112 ****************************************************************/
113
114int read_hyp( char *sumLine, char *shdw, struct Hsum *sumP )
115{
116  char   str[200];
117  float  deg, min;
118  float  sign;
119  int    len;
120
121  /* Initialize the output structure
122  **********************************/
123  memset( sumP, 0, (size_t) sizeof(struct Hsum) ); 
124 
125  /* Copy incoming string to a local buffer so we can alter it.
126  ************************************************************/
127  strncpy ( str, sumLine, (size_t) 165 ); /* this should be enough chars for us */
128  len = strlen( sumLine );       /* note length of original summary line */
129
130/*------------------------------------------------------------------------
131Sample HYP2000ARC archive summary line and its shadow.  The summary line
132may be up to 188 characters long.  Its full format is described in
133documentation (hyp2000.shadow.doc) by Fred Klein.
134199204290117039536 2577120 2407  475  0 18 98 17  16 5975 128175 6  58343COA  38    0  57 124 21   0 218  0  8COA WW D 24X   0  0L  0  0     10123D343 218Z  0   0  \n
135YYYYMMDDHHMMSSssLLsLLLLlllsllllDDDDDMMMpppGGGnnnRRRRAAADDEEEEAAADDMMMMCCCrrreeeerrssshhhhvvvvpppssssddddsssdddmmmapdacpppsaeeewwwmaaawwwIIIIIIIIIIlmmmwwwwammmwwwwVvN
136$1                                                                                0343   0   0\n
1370123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 12345
1380         1         2         3         4         5         6         7         8         9        10        11        12        13        14        15        16
139------------------------------------------------------------------------*/
140 
141  /* Read origin time
142  ******************/
143  strncpy( sumP->cdate,    str,     (size_t) 14 ); /* YYYYMMDDHHMMSS */
144  sumP->cdate[14] = '.';
145  strncpy( sumP->cdate+15, str+14,  (size_t) 2 );  /* hundreths of seconds */
146  sumP->cdate[17] = '\0';
147  sumP->ot = julsec17( sumP->cdate );
148  if ( sumP->ot == 0.0 ) 
149  {
150    logit( "t", "read_hyp: Error decoding origin time: %s\n",
151           sumP->cdate );
152    return( -1 );
153  }
154
155  /* Read the "version" of the information,
156   * i.e. what stage of processing or completeness the event is in. */
157  if(str[162] >= '0'  &&  str[162] <= '9') {
158      sumP->version = str[162] - '0';
159  } else {
160      /* Set version to 1 as default */
161      sumP->version = 1;
162  }
163 
164 
165  /* Read the rest of the card, back to front
166  ******************************************/
167  if( len >= 154 )
168  {
169  /* We're not reading fields beyond str[154] (Fortran column 155) */
170    str[154] = '\0';
171    read_fl1( str+150, &sumP->wtpref );
172    str[150] = '\0';
173    read_fl2( str+147, &sumP->Mpref );
174    sumP->labelpref = str[146];
175  }
176  str[146] = '\0';  sumP->qid  = atol( str+136 );
177 
178  str[121] = '\0'; 
179  read_i( str+118, &sumP->nphtot );
180 
181  /* Added by davidk 20000317 for use by DB PutArc */
182  sumP->mdtype=str[117]; /* coda dur mag type */
183
184  str[107+3]  = '\0'; 
185  read_fl2( str+107, &sumP->mdmad ); /* Mag XX */
186
187  str[100+4]  = '\0'; 
188  read_fl1( str+100, &sumP->mdwt ); /* Mag Wt. */
189  /* End: Added by davidk 20000317  */
190
191  str[96] = '\0'; 
192  read_i( str+93, &sumP->nPfm );
193  str[93]  = '\0'; 
194  read_fl2( str+89, &sumP->erz );
195  str[89]  = '\0';
196  read_fl2( str+85, &sumP->erh );
197  str[85]  = '\0';
198  read_i( str+82, &sumP->nphS );
199  str[80]  = '\0'; 
200  read_fl2( str+76, &sumP->e2 );
201  str[76]  = '\0';
202  strncpy( sumP->reg, str+73, (size_t) 4 );
203  str[73]  = '\0';
204  read_fl2( str+70, &sumP->Md );
205  str[70]  = '\0';
206  read_fl2( str+66, &sumP->e1 );
207  str[66]  = '\0'; 
208  read_i( str+64, &sumP->e1dp );
209  str[64]  = '\0';
210  read_i( str+61, &sumP->e1az );
211  str[61]  = '\0';
212  read_fl2( str+57, &sumP->e0 );
213  str[57]  = '\0';
214  read_i( str+55, &sumP->e0dp );
215  str[55]  = '\0';
216  read_i( str+52, &sumP->e0az );
217  str[52]  = '\0';
218  read_fl2( str+48, &sumP->rms );
219  str[48]  = '\0';
220  read_i( str+45, &sumP->dmin );
221  str[45]  = '\0';
222  read_i( str+42, &sumP->gap );
223  str[42]  = '\0';
224  read_i( str+39, &sumP->nph );
225  str[36]  = '\0';
226  read_fl2( str+31, &sumP->z );
227 
228  /* longitude */
229  str[31]  = '\0';  min        = (float)(atof( str+27 )/100.0);
230  if ( str[26] == ' ' || str[26] == 'W' || str[26] == 'w' ) 
231    sign = -1.0;
232  else
233    sign = 1.0;
234  str[26]  = '\0';  deg        = (float)atof( str+23 );
235  sumP->lon = (float)(sign * ( deg + min / 60.0 ));
236
237  /* latitude */
238  str[23]  = '\0';  min        = (float)(atof( str+19 )/100.0);
239  if ( str[18] == 'S' || str[18] == 's' )
240    sign = -1.0;
241  else
242    sign = 1.0;
243  str[18]  = '\0';  deg        = (float)atof( str+16 );
244  sumP->lat = (float)(sign * ( deg + min / 60.0 ));
245
246  return ( 0 );
247}
248
249
250/***************************************************************
251 * read_phs() reads a phase line & its shadow from archive msg
252 *            into an Hpck structure (defined in read_arc.h)
253 *      Inputs: phs - character string holding one phase line
254 *              shdw - character string holding the phase shadow line
255 *              pckP - pointer to Hpck structure provided by the caller
256 *     
257 *      returns 0 on success, -1 on parsing errors
258 *
259 ***************************************************************/
260
261int read_phs( char *phs, char *shdw, struct Hpck *pckP )
262{
263        char    str[120];
264        float   Psec, Ssec;
265        int     i;
266        double  Cat; /* common arrival time up to yyyymmddhhmm */
267
268   
269/*------------------------------------------------------------------------
270Sample TYPE_HYP2000ARC station archive card (P-arrival) and its shadow.
271  Phase card is 116 chars (including newline);
272  Shadow is up to 96 chars (including newline).
273PWM  NC VVHZ  PD0199204290117  877  -8136    0   0   0      0 0  0  61   0 169 8400  0   77 88325  0 932   0WD 01  \n
274SSSSSNNxcCCCxrrFWYYYYMMDDHHMMPPPPPRRRRWWWSSSSSrrxWRRRRAAAAAAAuuWWWDDDDddddEEEEAAAwwppprDDDDAAAMMMmmmppppssssSDALLaa\
275$   6 5.49 1.80 7.91 3.30 0.10 PSN0   77 PHP3 1853 39 340 47 245 55 230 63  86 71  70 77  48   \n
276$xnnnAA.AAQQ.QQaa.aaqq.qqRR.RRxccccDDDDDxPHpwAAAAATTTAAAAtttaaaaTTTAAAAtttaaaaTTTAAAAtttaaaaMMM\
2770123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456
278--------------------------------------------------------------------------*/
279
280
281  /* Copy phase string to a local buffer so we can alter it.
282  ************************************************************/
283  memset(str,0,(size_t) 120);
284  strncpy ( str, phs, (size_t) 120 );
285
286  /* Data source code */
287  pckP->datasrc = str[108];
288
289  /* Duration magnitude */
290  str[94+3] = '\0';
291  read_fl2( str+94, &pckP->Md);
292
293  /* azimuth to station */
294  str[91+3] = '\0';
295  read_i( str+91, &pckP->azm);
296
297  /* Coda duration */
298  str[87+4] = '\0';
299  read_i( str+87, &pckP->codalen );
300
301  /* duration magnitude weight code */
302  str[82+1] = '\0';
303  read_i( str+82, &pckP->codawt );
304
305  /* emergence angle at source */
306  str[78+3] = '\0';
307  read_i( str+78, &pckP->takeoff);
308
309  /* epicentral distance */
310  str[74+4] = '\0';
311  read_fl1( str+74, &pckP->dist);
312
313  /* S weight actually used */
314  str[63+3]  = '\0';
315  read_fl2( str+63, &pckP->Swt);
316
317  /* S travel time residual */
318  str[50+4]  = '\0';
319  read_fl2( str+50, &pckP->Sres );
320
321  /* Assigned S weight code */
322  str[49+1]  = '\0';
323  read_i( str+49, &pckP->Squal );
324
325  pckP->Sfm = str[48];
326
327  /* S phase label */
328  pckP->Slabel = str[47];
329
330  /* S phase onset */
331  pckP->Sonset = str[46];
332
333  /* Second of S arrival */
334  str[41+5]  = '\0'; 
335  Ssec = (float)(atof( str+41 )/100.0 );
336
337  /* P weight actually used */
338  str[38+3]  = '\0';
339  read_fl2( str+38, &pckP->Pwt);
340
341  /* P travel time residual */
342  str[34+4]  = '\0';
343  read_fl2( str+34, &pckP->Pres );
344
345  /* Seconds of P arrival */
346  str[29+5]  = '\0'; 
347  Psec = (float)(atof( str+29 )/100.0 );
348
349  /* Figure out the common time */
350  strncpy( pckP->cdate, str+17, (size_t) 12 ); /* YYYYMMDDHHMM */
351  pckP->cdate[12] = '\0';
352  strcat(pckP->cdate, "00.00" );
353  Cat = julsec17( pckP->cdate );
354  if ( Cat == 0.0 ) 
355  {
356    logit( "t", "read_phs: Error decoding arrival time: %s\n",
357                    pckP->cdate );
358    return( -1 );
359  }
360
361  /* Calculate P and S arrival times */
362  pckP->Pat = Cat + Psec;
363  pckP->Sat = Cat + Ssec;
364
365  /* Assigned P weight code */
366  str[16+1]  = '\0';
367  read_i( str+16, &pckP->Pqual );
368
369  pckP->Pfm = str[15];
370
371  /* P phase label */
372  pckP->Plabel = str[14];
373
374  /* P phase onset */
375  pckP->Ponset = str[13];
376
377  /* Location code */
378  str[111+2]  = '\0'; 
379  strcpy(pckP->loc, str+111); 
380  strib(pckP->loc);
381
382  /* Component code */
383  str[9+3]  = '\0'; 
384  strcpy(pckP->comp, str+9); 
385  strib(pckP->comp);
386
387  /* network code */
388  str[5+2]   = '\0'; 
389  strcpy(pckP->net,  str +5); 
390  strib(pckP->net);
391
392  /* station site code */
393  str[5]   = '\0'; 
394  strcpy(pckP->site, str); 
395  strib(pckP->site);
396
397
398  /* Copy shadow string to a local buffer so we can alter it.
399  ************************************************************/
400  memset(str,0,(size_t) 120);
401  strncpy ( str, shdw, (size_t) 120 );
402
403  str[88+4] = '\0';
404  read_i( str+88,  &(pckP->caav[5]) );
405
406  str[85+3] = '\0';
407  read_i( str+85, &(pckP->ccntr[5]) );
408
409  str[81+4] = '\0';
410  read_i( str+81,  &(pckP->caav[4]) );
411
412  str[78+3] = '\0';
413  read_i( str+78, &(pckP->ccntr[4]) );
414
415  str[74+4] = '\0';
416  read_i( str+74,  &(pckP->caav[3]) );
417
418  str[71+3] = '\0';
419  read_i( str+71, &(pckP->ccntr[3]) );
420
421  str[67+4] = '\0';
422  read_i( str+67,  &(pckP->caav[2]) );
423
424  str[64+3] = '\0';
425  read_i( str+64, &(pckP->ccntr[2]) );
426
427  str[60+4] = '\0';
428  read_i( str+60,  &(pckP->caav[1]) );
429
430  str[57+3] = '\0';
431  read_i( str+57, &(pckP->ccntr[1]) );
432
433  str[53+4] = '\0';
434  read_i( str+53,  &(pckP->caav[0]) );
435
436  str[50+3] = '\0';
437  read_i( str+50, &(pckP->ccntr[0]) );
438
439  for( i=0; i<6; i++ ) 
440  {
441    if( pckP->ccntr[i]==0 ) 
442    {
443            pckP->ccntr[i]    = (long)NULL;
444      pckP->caav[i]     = (long)NULL;
445    }
446  }
447
448  str[45+5] = '\0';
449  read_i( str+45, &pckP->pamp );
450
451  str[35+5] = '\0';
452  read_i( str+35, &pckP->codalenObs);
453
454
455  return ( 0 );
456}
457
458
459/*
460 * Strip trailing blanks and newlines from string.
461 */
462static int strib( char *string )
463{
464  int i, length;
465 
466  length = strlen( string );
467  if ( length )
468  {
469    for ( i = length-1; i >= 0; i-- )
470    {
471      if ( string[i] == ' ' || string[i] == '\n' )
472        string[i] = '\0';
473      else
474        return ( i+1 );
475    }
476  } 
477  else
478    return length;
479  return ( i+1 );
480}
481
482static void read_fl2( char* str, float* quant )
483{
484  if ( strib( str ) )
485    *quant = (float)(atof( str)/100.0 );
486  else
487    *quant = NO_FLOAT_VAL;
488}
489
490static void read_fl1( char* str, float* quant )
491{
492  if ( strib( str ) )
493    *quant = (float)(atof( str)/10.0 );
494  else
495    *quant = NO_FLOAT_VAL;
496}
497
498static void read_i( char* str, int* quant )
499{
500  if ( strib( str ) )
501    *quant = atoi( str );
502  else
503    *quant = NO_INT_VAL;
504}
505
506/* Function to search in string src, the pointer of next characters \0 or \n
507 * return the pointer or NULL if src is NULL */
508char *parse_arc_next_shadow(char *src) {
509    char *ret = NULL;
510    if(src) {
511        ret = src;
512        while( (*ret != 0)  &&  (*ret != '\n') ) {
513            ret++;
514        }
515        if(*ret == 0) {
516            ret = NULL;
517        } else {
518            ret++;
519        }
520    }
521    return ret;
522}
523
524int free_phases(HypoArc *arc)
525{
526  int i = 0;
527 
528  for(i=0; i< arc->num_phases; i++) 
529  {
530     free(arc->phases[i]);
531  }
532  return(0);
533}
534
535 /* parse_arc: parse the hyp2000 archive message into the Arc structure
536 *   Returns: 0 on success
537 *           -1 on parse errors
538 */
539int parse_arc(char *msg, HypoArc *arc)
540{
541  char *cur_msg = NULL;
542  char *cur_sdw = NULL;
543  int error = 0;
544  int nline = 0;
545
546  cur_msg = msg;
547  cur_sdw = parse_arc_next_shadow(cur_msg);
548  while( error != -1 && cur_msg != NULL && cur_sdw != NULL ) {
549
550    if(cur_msg[0] != '$') {
551      cur_sdw = parse_arc_next_shadow(cur_msg);
552      nline++;
553
554      if(cur_sdw[0] != '$') {
555        logit("", "parse_arc: error reading arc shadow line:\n%s\n", cur_sdw);
556        error = -1;
557      } else {
558
559        if (nline == 1) {
560          /* Summary ARC line */
561
562          /* Process the hypocenter card (1st line of msg) */
563          if ( read_hyp( cur_msg, cur_sdw, &(arc->sum) ) < 0 )
564          {  /* Should we send an error message? */
565            logit( "", "parse_arc: error parsing HYPO summary message.\n" );
566            error = -1;
567          } else {
568
569            logit( "", "parse_arc: parsed HYPO summary message.\n" );
570            arc->num_phases = 0;
571          }
572        } else {
573          /* Station ARC line */
574          if(arc->num_phases < MAX_PHASES) {
575            arc->phases[arc->num_phases] = (struct Hpck *) calloc(1, sizeof(struct Hpck));
576            if (read_phs (cur_msg, cur_sdw, arc->phases[arc->num_phases]) < 0) {
577              logit("", "parse_arc: error reading phase line info.\n");
578              error = -1;
579            } else {
580              if(arc->phases[arc->num_phases]->site[0] != 0 ) {
581                logit( "", "parse_arc: parsed HYPO phase line (%s.%s).\n", 
582                        arc->phases[arc->num_phases]->site, arc->phases[arc->num_phases]->comp );
583                arc->num_phases++;
584              } else {
585                logit( "", "parse_arc: parsing HYPO phase lines is terminated.\n");
586                cur_msg = NULL;
587                cur_sdw = NULL;
588              }
589            }
590          } else {
591            logit("", "parse_arc: WARNING number of arc phases execeds number of allocated %d\n", MAX_PHASES );
592            cur_msg = NULL;
593            cur_sdw = NULL;
594          }
595        }
596
597      }
598    } else {
599      logit("", "parse_arc: error reading arc no shadow line.\n");
600      error = -1;
601    }
602
603    if(error != -1) {
604      cur_msg = parse_arc_next_shadow(cur_sdw);
605      nline++;
606    }
607  }
608
609  if(error != -1) {
610      logit("", "parse_arc: read %d lines and %d phases of the arc message.\n", nline, arc->num_phases);
611  } else {
612      logit("", "parse_arc: error reading arc message (%d lines and %d phases).\n", nline, arc->num_phases);
613  }
614
615  return 0;
616}
Note: See TracBrowser for help on using the repository browser.