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: site_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.2 2000/08/15 00:50:41 dietz 00011 * Fixed NT bug in site_read() where the 'E' longitude designator was being 00012 * read as scientific notation instead of East-West. Caused east longitudes 00013 * to show up as negative longitudes. 00014 * 00015 * Revision 1.1 2000/02/14 18:51:48 lucky 00016 * Initial revision 00017 * 00018 * 00019 */ 00020 00021 /* 00022 * site.c : Station parameter routines. 00023 * 00024 *$ 95Aug31 LDD Added "site_file" command to site_com() 00025 *$ 95Sep01 LDD Added 2nd & 3rd args to site_index() 00026 *$ 95Oct19 LDD Explicitly declared return types for all functions. 00027 * Added function prototypes to site.h 00028 */ 00029 #include <sys/types.h> 00030 #include <string.h> 00031 #include <stdlib.h> 00032 #include <stdio.h> 00033 #include <kom.h> 00034 #include <site.h> 00035 00036 /* Initialization constants 00037 **************************/ 00038 static int initSite = 0; 00039 00040 /* Changed from 1000 to 1800 by WMK 2/12/96 */ 00041 static int maxSite = 1800; /* Change to alter size of mem allocation */ 00042 00043 00044 /************************************************************************** 00045 * site_init() Allocate the site table * 00046 **************************************************************************/ 00047 void site_init(void) 00048 { 00049 if(initSite) 00050 return; 00051 initSite = 1; 00052 nSite = 0; 00053 Site = (SITE *)calloc(maxSite, sizeof(SITE)); 00054 if(!Site) { 00055 fprintf(stderr, "site_init: Could not allocate site table; exiting!\n"); 00056 exit(0); 00057 } 00058 return; 00059 } 00060 00061 00062 /************************************************************************** 00063 * site_load(name) Process a kom.c-style command file that contains only * 00064 * commands recognized by site_com * 00065 **************************************************************************/ 00066 00067 int site_load(char *name) 00068 { 00069 char *com; 00070 00071 if(!k_open(name)) { 00072 fprintf(stderr, "site_load: Cannot open site file <%s>\n", name); 00073 return 0; 00074 } 00075 while(k_rd()) { 00076 com = k_str(); 00077 if ( !com ) continue; 00078 if ( site_com() ) continue; 00079 fprintf(stderr, "site_load: <%s> Unknown command\n", com); 00080 } 00081 k_close(); 00082 /* fprintf(stderr, "Site file <%s> loaded, nSite = %d\n", name, nSite); */ 00083 return 1; 00084 } 00085 00086 00087 /************************************************************************** 00088 * site_read(name) Read in a HYPOINVERSE format, universal station * 00089 * code file * 00090 **************************************************************************/ 00091 00092 /* Sample station line: 00093 R8075 MN BHZ 41 10.1000N121 10.1000E 01.0 0.00 0.00 0.00 0.00 1 0.00 00094 0123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 00095 */ 00096 00097 void site_read(char *name) 00098 { 00099 FILE *stafile; 00100 char line[256]; 00101 int dlat, dlon, elev; 00102 float mlat, mlon; 00103 char comp, ns, ew; 00104 int n; 00105 00106 /* initialize site table 00107 *********************/ 00108 site_init(); 00109 00110 /* open station file 00111 *****************/ 00112 if( (stafile = fopen( name, "r" )) == (FILE *) NULL ) { 00113 fprintf(stderr, 00114 "site_read: Cannot open site file <%s>; exiting!\n", name); 00115 exit(0); 00116 } 00117 00118 /* read in one line of the site file at a time 00119 *******************************************/ 00120 while( fgets( line, sizeof(line), stafile ) != (char *) NULL ) 00121 { 00122 00123 /* see if internal site table has room left */ 00124 if( nSite >= maxSite ) { 00125 fprintf( stderr, 00126 "site_read: Site table full; cannot load entire file <%s>\n", name ); 00127 fprintf( stderr, 00128 "site_read: Use <maxsite> command to increase table size; exiting!\n" ); 00129 exit(0); 00130 } 00131 00132 /* decode each line of the file */ 00133 00134 strncpy( Site[nSite].name, &line[0], 5); 00135 strncpy( Site[nSite].net, &line[6], 2); 00136 strncpy( Site[nSite].comp, &line[10], 3); 00137 comp = line[9]; 00138 00139 line[42] = '\0'; 00140 n = sscanf( &line[38], "%d", &elev ); 00141 if( n < 1 ) { 00142 fprintf( stderr, 00143 "site_read: Error reading elevation from line in station file\n%s\n", 00144 line ); 00145 continue; 00146 } 00147 00148 ew = line[37]; 00149 line[37] = '\0'; 00150 n = sscanf( &line[26], "%d %f", &dlon, &mlon ); 00151 if( n < 2 ) { 00152 fprintf( stderr, 00153 "site_read: Error reading longitude from line in station file\n%s\n", 00154 line ); 00155 continue; 00156 } 00157 00158 ns = line[25]; 00159 line[25] = '\0'; 00160 n = sscanf( &line[15], "%d %f", &dlat, &mlat ); 00161 if ( n < 2 ) { 00162 fprintf( stderr, 00163 "site_read: Error reading latitude from line in station file\n%s\n", 00164 line ); 00165 continue; 00166 } 00167 00168 /* printf( "%-5s %-2s %-3s %d %.4f%c%d %.4f%c%4d\n", 00169 Site[nSite].name, Site[nSite].net, Site[nSite].comp, 00170 dlat, mlat, ns, 00171 dlon, mlon, ew, elev ); */ /*DEBUG*/ 00172 00173 /* use one-letter component if there is no 3-letter component given */ 00174 if ( !strcmp(Site[nSite].comp, " ") ) sprintf( Site[nSite].comp, "%c ", comp ); 00175 00176 /* convert to decimal degrees */ 00177 if ( dlat < 0 ) dlat = -dlat; 00178 if ( dlon < 0 ) dlon = -dlon; 00179 Site[nSite].lat = (double) dlat + (mlat/60.0); 00180 Site[nSite].lon = (double) dlon + (mlon/60.0); 00181 00182 /* make south-latitudes and west-longitudes negative */ 00183 if ( ns=='s' || ns=='S' ) 00184 Site[nSite].lat = -Site[nSite].lat; 00185 if ( ew=='w' || ew=='W' || ew==' ' ) 00186 Site[nSite].lon = -Site[nSite].lon; 00187 Site[nSite].elev = (double) elev/1000.; 00188 00189 /* printf("%-5s %-2s %-3s %.4f %.4f %.0f\n\n", 00190 Site[nSite].name, Site[nSite].net, Site[nSite].comp, 00191 Site[nSite].lat, Site[nSite].lon, Site[nSite].elev ); */ /*DEBUG*/ 00192 00193 /* update the total number of stations loaded */ 00194 if(nSite < maxSite) ++nSite; 00195 00196 } /*end while*/ 00197 00198 fclose( stafile ); 00199 return; 00200 } 00201 00202 00203 /********************************************************************** 00204 * site_com(): Process all recognized commands. * 00205 * Return 1 on success, 0 if command was not recognized * 00206 **********************************************************************/ 00207 00208 int site_com( void ) 00209 { 00210 char *name; 00211 00212 if(k_its("site")) { 00213 site_init(); 00214 if(nSite >= maxSite) 00215 return 1; 00216 name = k_str(); 00217 if(!name) return 1; 00218 strcpy(Site[nSite].name, name); 00219 strcpy(Site[nSite].net, ""); /*added 950901:ldd*/ 00220 strcpy(Site[nSite].comp, ""); /*added 950901:ldd*/ 00221 Site[nSite].lat = k_val(); 00222 Site[nSite].lon = k_val(); 00223 Site[nSite].elev = 0.0; 00224 Site[nSite].elev = k_val(); 00225 if(nSite < maxSite) 00226 nSite++; 00227 return 1; 00228 } 00229 00230 if(k_its("maxsite")) { 00231 if(initSite) { 00232 fprintf( stderr, "site_com: Error: site table already allocated.\n" ); 00233 fprintf( stderr, 00234 "site_com: Use <maxsite> before any <site> or <site_file> commands" ); 00235 fprintf( stderr, "; exiting!\n" ); 00236 exit( 0 ); 00237 } 00238 maxSite = k_int(); 00239 return 1; 00240 } 00241 00242 if(k_its("site_file")) { /* added command to read in a HYPOINVERSE format */ 00243 name = k_str(); /* "universal code" station file. 950831:ldd */ 00244 if(!name) return 1; 00245 site_read(name); 00246 return 1; 00247 } 00248 00249 return 0; 00250 } 00251 00252 00253 /*************************************************************************** 00254 * site_index(site, net, comp) : Returns index of site, or -1 if not found * 00255 * 950901:ldd added 2nd & 3rd arguments to handle "universal" station * 00256 * naming convention * 00257 ***************************************************************************/ 00258 00259 int site_index(char *site, char *net, char *comp) 00260 { 00261 int i; 00262 00263 site_init(); 00264 for(i=0; i<nSite; i++) { 00265 if( !strcmp(Site[i].name, site ) && 00266 !strcmp(Site[i].net, net ) && 00267 !strcmp(Site[i].comp, comp ) ) return i; 00268 } 00269 return -1; 00270 } 00271