1 // Metar station implementation code
3 #include <simgear/compiler.h>
8 #include "MetarStation.h"
11 #if !defined (SG_HAVE_NATIVE_SGI_COMPILERS)
12 SG_USING_STD(ostream);
18 double CMetarStation::decodeDMS( char *b )
26 r = (*b - '0') * 10.0; b++;
35 m = (*b - '0') * 10.0; b++;
42 s = (*b - '0') * 10.0; b++;
46 // Direction (E W N S)
47 if ( *b == 'W' || *b == 'S' ) r = -r;
49 return r * SGD_DEGREES_TO_RADIANS;
53 // Decodes METAR station string of this format:
54 // 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
56 CMetarStation::CMetarStation(
60 t = strchr( s, ';' ); *t = 0; t++;
62 s = t; t = strchr( s, ';' ); *t = 0; t++;
63 m_number = atoi( s ) * 1000;
64 s = t; t = strchr( s, ';' ); *t = 0; t++;
65 m_number += atoi( s );
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++;
74 s = t; t = strchr( s, ';' ); *t = 0; t++;
75 double latitude = decodeDMS( s );
76 s = t; t = strchr( s, ';' ); *t = 0; t++;
77 double longitude = decodeDMS( s );
78 s = t; t = strchr( s, ';' ); *t = 0; t++;
79 double ulatitude = decodeDMS( s );
80 s = t; t = strchr( s, ';' ); *t = 0; t++;
81 double ulongitude = decodeDMS( s );
82 s = t; t = strchr( s, ';' ); *t = 0; t++;
83 double altitude = atoi( s ) * SG_FEET_TO_METER;
84 m_altitude = (int)altitude;
85 s = t; t = strchr( s, ';' ); *t = 0; t++;
86 double ualtitude = atoi( s ) * SG_FEET_TO_METER;
87 Point3D p( longitude, latitude, altitude+SG_EQUATORIAL_RADIUS_M );
89 m_locationCart = sgPolarToCart3d( p );
90 Point3D up( ulongitude, ulatitude, ualtitude+SG_EQUATORIAL_RADIUS_M );
91 m_upperLocationPolar = up;
92 m_upperLocationCart = sgPolarToCart3d( up );
99 void CMetarStation::dump()
101 cout << "ID:" << ID();
103 cout << "number:" << number();
105 cout << "name:" << name();
107 cout << "state:" << state();
109 cout << "country:" << country();
111 cout << "region:" << region();
113 cout << "Location (cart):" << locationCart();
115 cout << "Location (polar):" << locationPolar();
117 cout << "Upper Location (cart):" << upperLocationCart();
119 cout << "Upper Location (polar):" << upperLocationPolar();
121 cout << "P flag:" << pFlag();
127 CMetarStationDB::CMetarStationDB(const char * dbPath)
129 // Read the list of metar stations, decoding and adding to global list.
135 // Open the metar station list
136 FILE *f = fopen( dbPath, "r" );
140 // Read each line, create an instance of a station, and add it to the vector
141 while ( fgets( buf, 256, f) != NULL && feof( f ) == 0 ) {
142 // cout << buf << endl;
143 m = new CMetarStation( buf );
145 METAR_Stations[m->ID()]=( m );
150 // cout << METAR_Stations.size() << " Metar stations" << endl;
153 // cout << "Could not open MetarStations file " << endl;
160 CMetarStation * CMetarStationDB::find( std::string stationID )
162 std::map<std::string,CMetarStation*>::iterator target;
163 target = METAR_Stations.find(stationID);
164 if(target!= METAR_Stations.end() )
165 return target->second;
172 CMetarStation * CMetarStationDB::find( Point3D locationCart )
174 std::map<std::string,CMetarStation*>::iterator itr;
175 double bestDist = 99999999;
176 CMetarStation *bestStation = NULL;
177 Point3D curLocation = locationCart;
178 itr = METAR_Stations.begin();
179 while(itr != METAR_Stations.end())
181 double dist = itr->second->locationCart().distance3Dsquared( curLocation );
182 if (dist < bestDist )
185 bestStation = itr->second;
194 CMetarStationDB::~CMetarStationDB() {
195 std::map<std::string,CMetarStation*>::iterator itr;
196 for(itr = METAR_Stations.begin(); itr != METAR_Stations.end(); itr++)
203 operator << ( ostream& out, const CMetarStation& p )
206 << "ID:" << p.m_ID << endl
207 << "number:" << p.m_number << endl
208 << "name:" << p.m_name << endl
209 << "state:" << p.m_state << endl
210 << "country:" << p.m_country << endl
211 << "region:" << p.m_region << endl
212 << "Location (cart):" << p.m_locationCart << endl
213 << "Location (polar):" << p.m_locationCart << endl
214 << "Upper Location (cart):" << p.m_upperLocationCart << endl
215 << "Upper Location (polar):" << p.m_upperLocationPolar << endl
216 << "P flag:" << p.m_pFlag << endl;