1 // Metar station implementation code
5 #include "MetarStation.h"
8 #include <simgear/misc/fgpath.hxx>
11 double CMetarStation::decodeDMS( char *b )
19 r = (*b - '0') * 10.0; b++;
28 m = (*b - '0') * 10.0; b++;
35 s = (*b - '0') * 10.0; b++;
39 // Direction (E W N S)
40 if ( *b == 'W' || *b == 'S' ) r = -r;
42 return r * DEG_TO_RAD;
46 // Decodes METAR station string of this format:
47 // 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
49 CMetarStation::CMetarStation(
53 t = strchr( s, ';' ); *t = 0; t++;
55 s = t; t = strchr( s, ';' ); *t = 0; t++;
56 m_number = atoi( s ) * 1000;
57 s = t; t = strchr( s, ';' ); *t = 0; t++;
58 m_number += atoi( s );
59 s = t; t = strchr( s, ';' ); *t = 0; t++;
61 s = t; t = strchr( s, ';' ); *t = 0; t++;
63 s = t; t = strchr( s, ';' ); *t = 0; t++;
65 s = t; t = strchr( s, ';' ); *t = 0; t++;
67 s = t; t = strchr( s, ';' ); *t = 0; t++;
68 double latitude = decodeDMS( s );
69 s = t; t = strchr( s, ';' ); *t = 0; t++;
70 double longitude = decodeDMS( s );
71 s = t; t = strchr( s, ';' ); *t = 0; t++;
72 double ulatitude = decodeDMS( s );
73 s = t; t = strchr( s, ';' ); *t = 0; t++;
74 double ulongitude = decodeDMS( s );
75 s = t; t = strchr( s, ';' ); *t = 0; t++;
76 double altitude = atoi( s ) * FEET_TO_METER;
77 m_altitude = altitude;
78 s = t; t = strchr( s, ';' ); *t = 0; t++;
79 double ualtitude = atoi( s ) * FEET_TO_METER;
80 Point3D p( longitude, latitude, altitude+EQUATORIAL_RADIUS_M );
82 m_locationCart = sgPolarToCart3d( p );
83 Point3D up( ulongitude, ulatitude, ualtitude+EQUATORIAL_RADIUS_M );
84 m_upperLocationPolar = up;
85 m_upperLocationCart = sgPolarToCart3d( up );
92 void CMetarStation::dump()
94 std::cout << "ID:" << ID();
95 std::cout << std::endl;
96 std::cout << "number:" << number();
97 std::cout << std::endl;
98 std::cout << "name:" << name();
99 std::cout << std::endl;
100 std::cout << "state:" << state();
101 std::cout << std::endl;
102 std::cout << "country:" << country();
103 std::cout << std::endl;
104 std::cout << "region:" << region();
105 std::cout << std::endl;
106 std::cout << "Location (cart):" << locationCart();
107 std::cout << std::endl;
108 std::cout << "Location (polar):" << locationPolar();
109 std::cout << std::endl;
110 std::cout << "Upper Location (cart):" << upperLocationCart();
111 std::cout << std::endl;
112 std::cout << "Upper Location (polar):" << upperLocationPolar();
113 std::cout << std::endl;
114 std::cout << "P flag:" << pFlag();
115 std::cout << std::endl;
120 CMetarStationDB::CMetarStationDB(const char * dbPath)
122 // Read the list of metar stations, decoding and adding to global list.
128 // Open the metar station list
129 FILE *f = fopen( dbPath, "r" );
133 // Read each line, create an instance of a station, and add it to the vector
134 while ( fgets( buf, 256, f) != NULL && feof( f ) == 0 ) {
135 //std::cout << buf << std::endl;
136 m = new CMetarStation( buf );
138 METAR_Stations[m->ID()]=( m );
143 // std::cout << METAR_Stations.size() << " Metar stations" << std::endl;
146 // std::cout << "Could not open MetarStations file " << std::endl;
153 CMetarStation * CMetarStationDB::find( std::string stationID )
155 std::map<std::string,CMetarStation*>::iterator target;
156 target = METAR_Stations.find(stationID);
157 if(target!= METAR_Stations.end() )
158 return target->second;
165 CMetarStation * CMetarStationDB::find( Point3D locationCart )
167 std::map<std::string,CMetarStation*>::iterator itr;
168 double bestDist = 99999999;
169 CMetarStation * bestStation;
170 Point3D curLocation = locationCart;
171 itr = METAR_Stations.begin();
172 while(itr != METAR_Stations.end())
174 double dist = itr->second->locationCart().distance3Dsquared( curLocation );
175 if (dist < bestDist )
178 bestStation = itr->second;
187 CMetarStationDB::~CMetarStationDB() {
188 std::map<std::string,CMetarStation*>::iterator itr;
189 for(itr = METAR_Stations.begin(); itr != METAR_Stations.end(); itr++)
196 operator << ( ostream& out, const CMetarStation& p )
199 << "ID:" << p.m_ID << std::endl
200 << "number:" << p.m_number << std::endl
201 << "name:" << p.m_name << std::endl
202 << "state:" << p.m_state << std::endl
203 << "country:" << p.m_country << std::endl
204 << "region:" << p.m_region << std::endl
205 << "Location (cart):" << p.m_locationCart << std::endl
206 << "Location (polar):" << p.m_locationCart << std::endl
207 << "Upper Location (cart):" << p.m_upperLocationCart << std::endl
208 << "Upper Location (polar):" << p.m_upperLocationPolar << std::endl
209 << "P flag:" << p.m_pFlag << std::endl;