1 // Metar station implementation code
3 #include <simgear/compiler.h>
8 #include "MetarStation.h"
11 SG_USING_STD(ostream);
16 double CMetarStation::decodeDMS( char *b )
24 r = (*b - '0') * 10.0; b++;
33 m = (*b - '0') * 10.0; b++;
40 s = (*b - '0') * 10.0; b++;
44 // Direction (E W N S)
45 if ( *b == 'W' || *b == 'S' ) r = -r;
47 return r * SGD_DEGREES_TO_RADIANS;
51 // Decodes METAR station string of this format:
52 // 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
54 CMetarStation::CMetarStation(
58 t = strchr( s, ';' ); *t = 0; t++;
60 s = t; t = strchr( s, ';' ); *t = 0; t++;
61 m_number = atoi( s ) * 1000;
62 s = t; t = strchr( s, ';' ); *t = 0; t++;
63 m_number += atoi( s );
64 s = t; t = strchr( s, ';' ); *t = 0; t++;
66 s = t; t = strchr( s, ';' ); *t = 0; t++;
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++;
73 double latitude = decodeDMS( s );
74 s = t; t = strchr( s, ';' ); *t = 0; t++;
75 double longitude = decodeDMS( s );
76 s = t; t = strchr( s, ';' ); *t = 0; t++;
77 double ulatitude = decodeDMS( s );
78 s = t; t = strchr( s, ';' ); *t = 0; t++;
79 double ulongitude = decodeDMS( s );
80 s = t; t = strchr( s, ';' ); *t = 0; t++;
81 double altitude = atoi( s ) * SG_FEET_TO_METER;
82 m_altitude = (int)altitude;
83 s = t; t = strchr( s, ';' ); *t = 0; t++;
84 double ualtitude = atoi( s ) * SG_FEET_TO_METER;
85 Point3D p( longitude, latitude, altitude+SG_EQUATORIAL_RADIUS_M );
87 m_locationCart = sgPolarToCart3d( p );
88 Point3D up( ulongitude, ulatitude, ualtitude+SG_EQUATORIAL_RADIUS_M );
89 m_upperLocationPolar = up;
90 m_upperLocationCart = sgPolarToCart3d( up );
97 void CMetarStation::dump()
99 cout << "ID:" << ID();
101 cout << "number:" << number();
103 cout << "name:" << name();
105 cout << "state:" << state();
107 cout << "country:" << country();
109 cout << "region:" << region();
111 cout << "Location (cart):" << locationCart();
113 cout << "Location (polar):" << locationPolar();
115 cout << "Upper Location (cart):" << upperLocationCart();
117 cout << "Upper Location (polar):" << upperLocationPolar();
119 cout << "P flag:" << pFlag();
125 CMetarStationDB::CMetarStationDB(const char * dbPath)
127 // Read the list of metar stations, decoding and adding to global list.
133 // Open the metar station list
134 FILE *f = fopen( dbPath, "r" );
138 // Read each line, create an instance of a station, and add it to the vector
139 while ( fgets( buf, 256, f) != NULL && feof( f ) == 0 ) {
140 // cout << buf << endl;
141 m = new CMetarStation( buf );
143 METAR_Stations[m->ID()]=( m );
148 // cout << METAR_Stations.size() << " Metar stations" << endl;
151 // cout << "Could not open MetarStations file " << endl;
158 CMetarStation * CMetarStationDB::find( std::string stationID )
160 std::map<std::string,CMetarStation*>::iterator target;
161 target = METAR_Stations.find(stationID);
162 if(target!= METAR_Stations.end() )
163 return target->second;
170 CMetarStation * CMetarStationDB::find( Point3D locationCart )
172 std::map<std::string,CMetarStation*>::iterator itr;
173 double bestDist = 99999999;
174 CMetarStation *bestStation = NULL;
175 Point3D curLocation = locationCart;
176 itr = METAR_Stations.begin();
177 while(itr != METAR_Stations.end())
179 double dist = itr->second->locationCart().distance3Dsquared( curLocation );
180 if (dist < bestDist )
183 bestStation = itr->second;
192 CMetarStationDB::~CMetarStationDB() {
193 std::map<std::string,CMetarStation*>::iterator itr;
194 for(itr = METAR_Stations.begin(); itr != METAR_Stations.end(); itr++)
201 operator << ( ostream& out, const CMetarStation& p )
204 << "ID:" << p.m_ID << endl
205 << "number:" << p.m_number << endl
206 << "name:" << p.m_name << endl
207 << "state:" << p.m_state << endl
208 << "country:" << p.m_country << endl
209 << "region:" << p.m_region << endl
210 << "Location (cart):" << p.m_locationCart << endl
211 << "Location (polar):" << p.m_locationCart << endl
212 << "Upper Location (cart):" << p.m_upperLocationCart << endl
213 << "Upper Location (polar):" << p.m_upperLocationPolar << endl
214 << "P flag:" << p.m_pFlag << endl;