]> git.mxchange.org Git - simgear.git/blob - simgear/metar/MetarStation.cpp
-Added .cvsignore files to clean up CVS messages
[simgear.git] / simgear / metar / MetarStation.cpp
1 // Metar station implementation code
2
3 #include <simgear/compiler.h>
4
5 #include STL_IOSTREAM
6 #include <stdio.h>
7
8 #include "MetarStation.h"
9 #include <algorithm>
10
11 #if !defined (SG_HAVE_NATIVE_SGI_COMPILERS)
12 SG_USING_STD(ostream);
13 SG_USING_STD(cout);
14 SG_USING_STD(endl);
15 #endif
16
17
18 double CMetarStation::decodeDMS( char *b )
19 {
20         double r = 0;
21         double m = 0;
22         double s = 0;
23         if ( *b )
24         {
25                 // Degrees
26                 r = (*b - '0') * 10.0; b++;
27                 r += (*b - '0'); b++;
28                 if ( *b != '-' )
29                 {
30                         r *= 10;
31                         r += (*b - '0'); b++;
32                 }
33                 b++;
34                 // Minutes
35                 m = (*b - '0') * 10.0; b++;
36                 m += (*b - '0'); b++;
37                 r += m/60.0;
38                 if ( *b == '-' )
39                 {
40                         // Seconds
41                         b++;
42                         s = (*b - '0') * 10.0; b++;
43                         s += (*b - '0'); b++;
44                 }
45                 r += s/3600.0;
46                 // Direction (E W N S)
47                 if ( *b == 'W' || *b == 'S' ) r = -r;
48         }
49         return r * SGD_DEGREES_TO_RADIANS;
50 }
51
52 // Constructor
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
55
56 CMetarStation::CMetarStation( 
57         char *s )
58 {
59         char *t;
60         t = strchr( s, ';' ); *t = 0; t++;
61         m_ID = s;
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++;
67         m_name = s;
68         s = t; t = strchr( s, ';' ); *t = 0; t++;
69         m_state = s;
70         s = t; t = strchr( s, ';' ); *t = 0; t++;
71         m_country = s;
72         s = t; t = strchr( s, ';' ); *t = 0; t++;
73         m_region = atoi( s );
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 );
88         m_locationPolar = p;
89         m_locationCart = sgPolarToCart3d( p );
90         Point3D up( ulongitude, ulatitude, ualtitude+SG_EQUATORIAL_RADIUS_M );
91         m_upperLocationPolar = up;
92         m_upperLocationCart = sgPolarToCart3d( up );
93         s = t;
94         m_pFlag = s[0];
95 }
96                 
97
98
99 void CMetarStation::dump()
100 {
101         cout << "ID:" << ID();
102         cout << endl;
103         cout << "number:" << number();
104         cout << endl;
105         cout << "name:" << name();
106         cout << endl;
107         cout << "state:" << state();
108         cout << endl;
109         cout << "country:" << country();
110         cout << endl;
111         cout << "region:" << region();
112         cout << endl;
113         cout << "Location (cart):" << locationCart();
114         cout << endl;
115         cout << "Location (polar):" << locationPolar();
116         cout << endl;
117         cout << "Upper Location (cart):" << upperLocationCart();
118         cout << endl;
119         cout << "Upper Location (polar):" << upperLocationPolar();
120         cout << endl;
121         cout << "P flag:" << pFlag();
122         cout << endl;
123 }
124
125
126
127 CMetarStationDB::CMetarStationDB(const char * dbPath) 
128 {
129     // Read the list of metar stations, decoding and adding to global list.
130
131     CMetarStation *m;
132     char buf[256];
133
134
135     // Open the metar station list
136     FILE *f = fopen( dbPath, "r" );
137         
138
139     if ( f != NULL ) {
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 );
144             //m->dump();
145             METAR_Stations[m->ID()]=( m );
146         }
147         
148         // Close the list
149         fclose( f );
150         // cout << METAR_Stations.size() << " Metar stations" << endl;
151         
152     } else {
153         // cout << "Could not open MetarStations file " << endl;
154         
155     }
156 }
157
158
159
160 CMetarStation * CMetarStationDB::find( std::string stationID )
161 {
162     std::map<std::string,CMetarStation*>::iterator target;
163     target = METAR_Stations.find(stationID);
164   if(target!= METAR_Stations.end() )
165       return target->second;
166   else 
167       return NULL;
168 }
169
170
171
172 CMetarStation * CMetarStationDB::find( Point3D locationCart )
173 {
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()) 
180       {
181         double dist = itr->second->locationCart().distance3Dsquared( curLocation );
182         if (dist < bestDist )
183           {
184             bestDist = dist;
185             bestStation = itr->second;
186           }
187         itr++;
188       }
189     
190     return bestStation;
191 }
192
193
194 CMetarStationDB::~CMetarStationDB() {
195     std::map<std::string,CMetarStation*>::iterator itr;
196     for(itr = METAR_Stations.begin(); itr != METAR_Stations.end(); itr++) 
197       {
198         delete itr->second;
199     }
200 }
201
202 ostream&
203 operator << ( ostream& out, const CMetarStation& p )
204 {
205     return out 
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;
217 }
218