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

neic2scn.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: neic2scn_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.4  2001/07/01 22:13:07  davidk
00011  *     Removed some unneeded function prototypes.
00012  *
00013  *     Revision 1.3  2000/09/27 18:57:08  lucky
00014  *     Fixed logit calls.
00015  *
00016  *     Revision 1.2  2000/05/02 19:46:15  lucky
00017  *     Cosmetic fixes needed for warning-free NT compilation
00018  *
00019  *     Revision 1.1  2000/03/31 18:25:48  lucky
00020  *     Initial revision
00021  *
00022  *
00023  */
00024 
00025 #include <stdio.h>
00026 #include <string.h>
00027 #include <stdlib.h>
00028 #include <earthworm.h>
00029 #include <transport.h>
00030 #include <neic2scn.h>
00031 
00032 
00033   /***************************************************************
00034    *                         GetNEICStaList()                    *
00035    *                                                             *
00036    *                     Read the station list                   *
00037    *                                                             *
00038    ***************************************************************/
00039 
00040 int GetNEICStaList (NEIC2SCN **Sta, int *Nsta, char *filename)
00041 {
00042    char    string[150];
00043    int     i;
00044    int     nsta;
00045    NEIC2SCN *sta;
00046    FILE    *fp;
00047 
00048         if ((Sta == NULL) || (Nsta == NULL) || (filename == NULL))
00049         {
00050                 logit ("e", "Invalid arguments passed in.\n");
00051                 return EW_FAILURE;
00052         }
00053 
00054 /* Open the station list file
00055    **************************/
00056    if ((fp = fopen (filename, "r") ) == NULL)
00057    {
00058       logit( "e", "Error opening station list file <%s>.\n",
00059              filename);
00060       return EW_FAILURE;
00061    }
00062 
00063 /* Count channels in the station file.
00064    Ignore comment lines and lines consisting of all whitespace.
00065    ***********************************************************/
00066    nsta = 0;
00067    while ( fgets( string, 130, fp ) != NULL )
00068       if ( !IsComment( string ) ) nsta++;
00069 
00070    rewind( fp );
00071 
00072 /* Allocate the station list
00073    *************************/
00074    sta = (NEIC2SCN *) calloc( nsta, sizeof(NEIC2SCN) );
00075    if ( sta == NULL )
00076    {
00077       logit( "e", "Cannot allocate the station array\n" );
00078       return EW_FAILURE;
00079    }
00080 
00081 
00082 /* Read stations from the station list file into the station array
00083    **************************************************************/
00084    i = 0;
00085    while ( fgets( string, 130, fp ) != NULL )
00086    {
00087       int ndecoded;
00088 
00089       if ( IsComment( string ) ) continue;
00090       ndecoded = sscanf( string,
00091               "%s%s%s%s", 
00092                sta[i].neic_sta,
00093                sta[i].sta,
00094                sta[i].comp,
00095                sta[i].net);
00096       if (ndecoded < 4)
00097       {
00098          logit( "e", "Error decoding station file.\n" );
00099          logit( "e", "ndecoded: %d\n", ndecoded );
00100          logit( "e", "Offending line:\n" );
00101          logit( "e", "%s\n", string );
00102          return EW_FAILURE;
00103       }
00104       i++;
00105    }
00106    fclose( fp );
00107    *Sta  = sta;
00108    *Nsta = nsta;
00109    return EW_SUCCESS;
00110 }
00111 
00112 
00113     /*********************************************************************
00114      *                             IsComment()                           *
00115      *                                                                   *
00116      *  Accepts: String containing one line from a pick_ew station list  *
00117      *  Returns: 1 if it's a comment line                                *
00118      *           0 if it's not a comment line                            *
00119      *********************************************************************/
00120 
00121 int IsComment( char string[] )
00122 {
00123    int i;
00124 
00125    for ( i = 0; i < (int)strlen( string ); i++ )
00126    {
00127       char test = string[i];
00128 
00129       if ( test!=' ' && test!='\t' && test!='\n' )
00130       {
00131          if ( test == '#'  )
00132             return 1;          /* It's a comment line */
00133          else
00134             return 0;          /* It's not a comment line */
00135       }
00136    }
00137    return 1;                   /* It contains only whitespace */
00138 }
00139 
00140 
00141     /*********************************************************************
00142      *                         MatchNeic2SCN ()                          *
00143      *                                                                   *
00144      *********************************************************************/
00145 int  MatchNeic2SCN (char *neic_sta, char *sta, char *comp, 
00146                         char *net, NEIC2SCN *neic2scn, int nSta)
00147 {
00148 
00149         int             i;
00150 
00151         if ((neic2scn == NULL) || (nSta < 0) || (neic_sta == NULL) ||
00152                         (sta == NULL) || (comp == NULL) || (net == NULL))
00153         {
00154                 logit ("", "Invalid arguments passed in.\n");
00155                 return EW_FAILURE;
00156         }
00157 
00158 
00159         /* Strip off any trailing spaces */
00160         i = 0;
00161         while ((i < 6) && (neic_sta[i] != ' '))
00162         {
00163                 i = i + 1;
00164         }
00165         if (i != 6)
00166                 neic_sta[i] = '\0';
00167         else
00168                 neic_sta[i + 1] = '\0';
00169 
00170 
00171         /* check against the list */
00172         i = 0;
00173         while (i < nSta)
00174         {
00175                 if (strcmp (neic2scn[i].neic_sta, neic_sta) == 0)
00176                 {
00177                         /* Found a Match! */
00178                         strcpy (sta, neic2scn[i].sta);
00179                         strcpy (comp, neic2scn[i].comp);
00180                         strcpy (net, neic2scn[i].net);
00181                         return EW_SUCCESS;
00182                 }
00183 
00184                 else
00185                 {
00186                         i = i + 1;
00187                 }
00188         }
00189 
00190         /* Not Found! */
00191         strcpy (sta, neic_sta);
00192         strcpy (comp, "???");
00193         strcpy (net, "???");
00194 
00195 
00196         return EW_SUCCESS;
00197 
00198 }

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