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" );
111 FGPath weatherPath( ":Data" );
113 FGPath weatherPath( current_options.get_fg_root() );
116 weatherPath.append( "Weather" );
117 weatherPath.append( "MetarStations" );
118 // Open the metar station list
119 FILE *f = fopen( weatherPath.c_str(), "r" );
123 // Read each line, create an instance of a station, and add it to the vector
124 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;
136 // std::cout << "Could not open MetarStations file " << std::endl;
142 int CMetarStation::sameName( CMetarStation *m )
144 return m->m_ID == tempName;
148 CMetarStation *CMetarStation::find( std::string stationID )
150 tempName = stationID;
151 CMetarStation **m = std::find_if( METAR_Stations.begin(), METAR_Stations.end(), sameName );
152 if ( m != METAR_Stations.end() ) return *m;
157 CMetarStation *bestStation;
160 void findHelper( CMetarStation *s )
162 double dist = s->locationCart().distance3Dsquared( curLocation );
163 if (dist < bestDist )
170 CMetarStation *CMetarStation::find( Point3D locationCart )
174 curLocation = locationCart;
176 for_each( findHelper );
181 void CMetarStation::for_each( void f( CMetarStation *s ) )
183 std::for_each( METAR_Stations.begin(), METAR_Stations.end(), f );
187 void CMetarStation::dump()
189 std::cout << "ID:" << ID();
190 std::cout << std::endl;
191 std::cout << "number:" << number();
192 std::cout << std::endl;
193 std::cout << "name:" << name();
194 std::cout << std::endl;
195 std::cout << "state:" << state();
196 std::cout << std::endl;
197 std::cout << "country:" << country();
198 std::cout << std::endl;
199 std::cout << "region:" << region();
200 std::cout << std::endl;
201 std::cout << "Location (cart):" << locationCart();
202 std::cout << std::endl;
203 std::cout << "Location (polar):" << locationPolar();
204 std::cout << std::endl;
205 std::cout << "Upper Location (cart):" << upperLocationCart();
206 std::cout << std::endl;
207 std::cout << "Upper Location (polar):" << upperLocationPolar();
208 std::cout << std::endl;
209 std::cout << "P flag:" << pFlag();
210 std::cout << std::endl;
214 operator << ( ostream& out, const CMetarStation& p )
217 << "ID:" << p.m_ID << std::endl
218 << "number:" << p.m_number << std::endl
219 << "name:" << p.m_name << std::endl
220 << "state:" << p.m_state << std::endl
221 << "country:" << p.m_country << std::endl
222 << "region:" << p.m_region << std::endl
223 << "Location (cart):" << p.m_locationCart << std::endl
224 << "Location (polar):" << p.m_locationCart << std::endl
225 << "Upper Location (cart):" << p.m_upperLocationCart << std::endl
226 << "Upper Location (polar):" << p.m_upperLocationPolar << std::endl
227 << "P flag:" << p.m_pFlag << std::endl;