1 // Metar station implementation code
5 #include "MetarStation.h"
9 // options is too tightly integrated into FlightGear to use in a test program
10 #include <Main/options.hxx>
12 #include <simgear/misc/fgpath.hxx>
15 std::vector< CMetarStation *> METAR_Stations;
17 int CMetarStation::initialized( CMetarStation::initialize() );
19 std::string CMetarStation::tempName;
22 double CMetarStation::decodeDMS( char *b )
30 r = (*b - '0') * 10.0; b++;
39 m = (*b - '0') * 10.0; b++;
46 s = (*b - '0') * 10.0; b++;
50 // Direction (E W N S)
51 if ( *b == 'W' || *b == 'S' ) r = -r;
53 return r * DEG_TO_RAD;
57 CMetarStation::CMetarStation(
61 t = strchr( s, ';' ); *t = 0; t++;
63 s = t; t = strchr( s, ';' ); *t = 0; t++;
64 m_number = atoi( s ) * 1000;
65 s = t; t = strchr( s, ';' ); *t = 0; t++;
66 m_number += atoi( s );
67 s = t; t = strchr( s, ';' ); *t = 0; t++;
69 s = t; t = strchr( s, ';' ); *t = 0; t++;
71 s = t; t = strchr( s, ';' ); *t = 0; t++;
73 s = t; t = strchr( s, ';' ); *t = 0; t++;
75 s = t; t = strchr( s, ';' ); *t = 0; t++;
76 double latitude = decodeDMS( s );
77 s = t; t = strchr( s, ';' ); *t = 0; t++;
78 double longitude = decodeDMS( s );
79 s = t; t = strchr( s, ';' ); *t = 0; t++;
80 double ulatitude = decodeDMS( s );
81 s = t; t = strchr( s, ';' ); *t = 0; t++;
82 double ulongitude = decodeDMS( s );
83 s = t; t = strchr( s, ';' ); *t = 0; t++;
84 double altitude = atoi( s ) * FEET_TO_METER;
85 s = t; t = strchr( s, ';' ); *t = 0; t++;
86 double ualtitude = atoi( s ) * FEET_TO_METER;
87 Point3D p( longitude, latitude, altitude+EQUATORIAL_RADIUS_M );
89 m_locationCart = fgPolarToCart3d( p );
90 Point3D up( ulongitude, ulatitude, ualtitude+EQUATORIAL_RADIUS_M );
91 m_upperLocationPolar = up;
92 m_upperLocationCart = fgPolarToCart3d( up );
97 // Decodes METAR station string of this format:
98 // 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
101 int CMetarStation::initialize()
103 // Read the list of metar stations, decoding and adding to global list.
108 // Goto the Flight Gear installation directory
110 FGPath weatherPath( "/mkv/Build/FlightGear" );
112 FGPath weatherPath( current_options.get_fg_root() );
115 weatherPath.append( "Weather/MetarStations" );
116 // Open the metar station list
117 FILE *f = fopen( weatherPath.c_str(), "r" );
122 // Read each line, create an instance of a station, and add it to the vector
123 while ( fgets( buf, 256, f) != NULL && feof( f ) == 0 )
125 //std::cout << buf << std::endl;
126 m = new CMetarStation( buf );
128 METAR_Stations.push_back( m );
133 std::cout << METAR_Stations.size() << " Metar stations" << std::endl;
138 std::cout << "Could not open MetarStations file " << std::endl;
144 int CMetarStation::sameName( CMetarStation *m )
146 return m->m_ID == tempName;
150 CMetarStation *CMetarStation::find( std::string stationID )
152 tempName = stationID;
153 CMetarStation **m = std::find_if( METAR_Stations.begin(), METAR_Stations.end(), sameName );
154 if ( m != METAR_Stations.end() ) return *m;
159 CMetarStation *bestStation;
162 void findHelper( CMetarStation *s )
164 double dist = s->locationCart().distance3Dsquared( curLocation );
165 if (dist < bestDist )
172 CMetarStation *CMetarStation::find( Point3D locationCart )
176 curLocation = locationCart;
178 for_each( findHelper );
183 void CMetarStation::for_each( void f( CMetarStation *s ) )
185 std::for_each( METAR_Stations.begin(), METAR_Stations.end(), f );
189 void CMetarStation::dump()
191 std::cout << "ID:" << ID();
192 std::cout << std::endl;
193 std::cout << "number:" << number();
194 std::cout << std::endl;
195 std::cout << "name:" << name();
196 std::cout << std::endl;
197 std::cout << "state:" << state();
198 std::cout << std::endl;
199 std::cout << "country:" << country();
200 std::cout << std::endl;
201 std::cout << "region:" << region();
202 std::cout << std::endl;
203 std::cout << "Location (cart):" << locationCart();
204 std::cout << std::endl;
205 std::cout << "Location (polar):" << locationPolar();
206 std::cout << std::endl;
207 std::cout << "Upper Location (cart):" << upperLocationCart();
208 std::cout << std::endl;
209 std::cout << "Upper Location (polar):" << upperLocationPolar();
210 std::cout << std::endl;
211 std::cout << "P flag:" << pFlag();
212 std::cout << std::endl;
216 operator << ( ostream& out, const CMetarStation& p )
219 << "ID:" << p.m_ID << std::endl
220 << "number:" << p.m_number << std::endl
221 << "name:" << p.m_name << std::endl
222 << "state:" << p.m_state << std::endl
223 << "country:" << p.m_country << std::endl
224 << "region:" << p.m_region << std::endl
225 << "Location (cart):" << p.m_locationCart << std::endl
226 << "Location (polar):" << p.m_locationCart << std::endl
227 << "Upper Location (cart):" << p.m_upperLocationCart << std::endl
228 << "Upper Location (polar):" << p.m_upperLocationPolar << std::endl
229 << "P flag:" << p.m_pFlag << std::endl;