]> git.mxchange.org Git - simgear.git/blob - simgear/metar/MetarStation.cpp
Changes for the native Irix CC compiler contributed by Erik Hofman.
[simgear.git] / simgear / metar / MetarStation.cpp
1 // Metar station implementation code
2
3 #include <stdio.h>
4
5 #include "MetarStation.h"
6 #include <algorithm>
7
8 #include <simgear/misc/fgpath.hxx>
9
10
11 double CMetarStation::decodeDMS( char *b )
12 {
13         double r = 0;
14         double m = 0;
15         double s = 0;
16         if ( *b )
17         {
18                 // Degrees
19                 r = (*b - '0') * 10.0; b++;
20                 r += (*b - '0'); b++;
21                 if ( *b != '-' )
22                 {
23                         r *= 10;
24                         r += (*b - '0'); b++;
25                 }
26                 b++;
27                 // Minutes
28                 m = (*b - '0') * 10.0; b++;
29                 m += (*b - '0'); b++;
30                 r += m/60.0;
31                 if ( *b == '-' )
32                 {
33                         // Seconds
34                         b++;
35                         s = (*b - '0') * 10.0; b++;
36                         s += (*b - '0'); b++;
37                 }
38                 r += s/3600.0;
39                 // Direction (E W N S)
40                 if ( *b == 'W' || *b == 'S' ) r = -r;
41         }
42         return r * DEG_TO_RAD;
43 }
44
45 // Constructor
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
48
49 CMetarStation::CMetarStation( 
50         char *s )
51 {
52         char *t;
53         t = strchr( s, ';' ); *t = 0; t++;
54         m_ID = s;
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++;
60         m_name = s;
61         s = t; t = strchr( s, ';' ); *t = 0; t++;
62         m_state = s;
63         s = t; t = strchr( s, ';' ); *t = 0; t++;
64         m_country = s;
65         s = t; t = strchr( s, ';' ); *t = 0; t++;
66         m_region = atoi( s );
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 );
81         m_locationPolar = p;
82         m_locationCart = sgPolarToCart3d( p );
83         Point3D up( ulongitude, ulatitude, ualtitude+EQUATORIAL_RADIUS_M );
84         m_upperLocationPolar = up;
85         m_upperLocationCart = sgPolarToCart3d( up );
86         s = t;
87         m_pFlag = s[0];
88 }
89                 
90
91
92 void CMetarStation::dump()
93 {
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;
116 }
117
118
119
120 CMetarStationDB::CMetarStationDB(const char * dbPath) 
121 {
122     // Read the list of metar stations, decoding and adding to global list.
123
124     CMetarStation *m;
125     char buf[256];
126
127
128     // Open the metar station list
129     FILE *f = fopen( dbPath, "r" );
130         
131
132     if ( f != NULL ) {
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 );
137             //m->dump();
138             METAR_Stations[m->ID()]=( m );
139         }
140         
141         // Close the list
142         fclose( f );
143         // std::cout << METAR_Stations.size() << " Metar stations" << std::endl;
144         
145     } else {
146         // std::cout << "Could not open MetarStations file " << std::endl;
147         
148     }
149 }
150
151
152
153 CMetarStation * CMetarStationDB::find( std::string stationID )
154 {
155     std::map<std::string,CMetarStation*>::iterator target;
156     target = METAR_Stations.find(stationID);
157   if(target!= METAR_Stations.end() )
158       return target->second;
159   else 
160       return NULL;
161 }
162
163
164
165 CMetarStation * CMetarStationDB::find( Point3D locationCart )
166 {
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()) 
173       {
174         double dist = itr->second->locationCart().distance3Dsquared( curLocation );
175         if (dist < bestDist )
176           {
177             bestDist = dist;
178             bestStation = itr->second;
179           }
180         itr++;
181       }
182     
183     return bestStation;
184 }
185
186
187 CMetarStationDB::~CMetarStationDB() {
188     std::map<std::string,CMetarStation*>::iterator itr;
189     for(itr = METAR_Stations.begin(); itr != METAR_Stations.end(); itr++) 
190       {
191         delete itr->second;
192     }
193 }
194
195 std::ostream&
196 operator << ( ostream& out, const CMetarStation& p )
197 {
198     return out 
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;
210 }
211