1 // Metar station implementation code
3 #include <simgear/compiler.h>
8 #include "MetarStation.h"
11 #include <simgear/misc/fgpath.hxx>
13 #if !defined (FG_HAVE_NATIVE_SGI_COMPILERS)
14 FG_USING_STD(ostream);
20 double CMetarStation::decodeDMS( char *b )
28 r = (*b - '0') * 10.0; b++;
37 m = (*b - '0') * 10.0; b++;
44 s = (*b - '0') * 10.0; b++;
48 // Direction (E W N S)
49 if ( *b == 'W' || *b == 'S' ) r = -r;
51 return r * DEG_TO_RAD;
55 // Decodes METAR station string of this format:
56 // KPUB;72;464;Pueblo, Pueblo Memorial Airport;CO;United States;4;38-17-24N;104-29-54W;38-17-03N;104-29-43W;1440;1420;P
58 CMetarStation::CMetarStation(
62 t = strchr( s, ';' ); *t = 0; t++;
64 s = t; t = strchr( s, ';' ); *t = 0; t++;
65 m_number = atoi( s ) * 1000;
66 s = t; t = strchr( s, ';' ); *t = 0; t++;
67 m_number += atoi( s );
68 s = t; t = strchr( s, ';' ); *t = 0; t++;
70 s = t; t = strchr( s, ';' ); *t = 0; t++;
72 s = t; t = strchr( s, ';' ); *t = 0; t++;
74 s = t; t = strchr( s, ';' ); *t = 0; t++;
76 s = t; t = strchr( s, ';' ); *t = 0; t++;
77 double latitude = decodeDMS( s );
78 s = t; t = strchr( s, ';' ); *t = 0; t++;
79 double longitude = decodeDMS( s );
80 s = t; t = strchr( s, ';' ); *t = 0; t++;
81 double ulatitude = decodeDMS( s );
82 s = t; t = strchr( s, ';' ); *t = 0; t++;
83 double ulongitude = decodeDMS( s );
84 s = t; t = strchr( s, ';' ); *t = 0; t++;
85 double altitude = atoi( s ) * FEET_TO_METER;
86 m_altitude = altitude;
87 s = t; t = strchr( s, ';' ); *t = 0; t++;
88 double ualtitude = atoi( s ) * FEET_TO_METER;
89 Point3D p( longitude, latitude, altitude+EQUATORIAL_RADIUS_M );
91 m_locationCart = sgPolarToCart3d( p );
92 Point3D up( ulongitude, ulatitude, ualtitude+EQUATORIAL_RADIUS_M );
93 m_upperLocationPolar = up;
94 m_upperLocationCart = sgPolarToCart3d( up );
101 void CMetarStation::dump()
103 cout << "ID:" << ID();
105 cout << "number:" << number();
107 cout << "name:" << name();
109 cout << "state:" << state();
111 cout << "country:" << country();
113 cout << "region:" << region();
115 cout << "Location (cart):" << locationCart();
117 cout << "Location (polar):" << locationPolar();
119 cout << "Upper Location (cart):" << upperLocationCart();
121 cout << "Upper Location (polar):" << upperLocationPolar();
123 cout << "P flag:" << pFlag();
129 CMetarStationDB::CMetarStationDB(const char * dbPath)
131 // Read the list of metar stations, decoding and adding to global list.
137 // Open the metar station list
138 FILE *f = fopen( dbPath, "r" );
142 // Read each line, create an instance of a station, and add it to the vector
143 while ( fgets( buf, 256, f) != NULL && feof( f ) == 0 ) {
144 // cout << buf << endl;
145 m = new CMetarStation( buf );
147 METAR_Stations[m->ID()]=( m );
152 // cout << METAR_Stations.size() << " Metar stations" << endl;
155 // cout << "Could not open MetarStations file " << endl;
162 CMetarStation * CMetarStationDB::find( std::string stationID )
164 std::map<std::string,CMetarStation*>::iterator target;
165 target = METAR_Stations.find(stationID);
166 if(target!= METAR_Stations.end() )
167 return target->second;
174 CMetarStation * CMetarStationDB::find( Point3D locationCart )
176 std::map<std::string,CMetarStation*>::iterator itr;
177 double bestDist = 99999999;
178 CMetarStation * bestStation;
179 Point3D curLocation = locationCart;
180 itr = METAR_Stations.begin();
181 while(itr != METAR_Stations.end())
183 double dist = itr->second->locationCart().distance3Dsquared( curLocation );
184 if (dist < bestDist )
187 bestStation = itr->second;
196 CMetarStationDB::~CMetarStationDB() {
197 std::map<std::string,CMetarStation*>::iterator itr;
198 for(itr = METAR_Stations.begin(); itr != METAR_Stations.end(); itr++)
205 operator << ( ostream& out, const CMetarStation& p )
208 << "ID:" << p.m_ID << endl
209 << "number:" << p.m_number << endl
210 << "name:" << p.m_name << endl
211 << "state:" << p.m_state << endl
212 << "country:" << p.m_country << endl
213 << "region:" << p.m_region << endl
214 << "Location (cart):" << p.m_locationCart << endl
215 << "Location (polar):" << p.m_locationCart << endl
216 << "Upper Location (cart):" << p.m_upperLocationCart << endl
217 << "Upper Location (polar):" << p.m_upperLocationPolar << endl
218 << "P flag:" << p.m_pFlag << endl;