From 6a97b1724af8ae4e0aba49570a0c8d3893840f79 Mon Sep 17 00:00:00 2001 From: Erik Hofman Date: Thu, 19 Nov 2020 14:17:43 +0100 Subject: [PATCH] Add a function to construct a METAR string from climate data --- src/Environment/climate.cxx | 48 +++++++++++++++++++++++++++++++++---- src/Environment/climate.hxx | 4 +++- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/Environment/climate.cxx b/src/Environment/climate.cxx index 585ebae32..b16191392 100644 --- a/src/Environment/climate.cxx +++ b/src/Environment/climate.cxx @@ -93,7 +93,8 @@ void FGClimate::bind() // weather properties _tiedProperties.Tie( "weather-update", &_weather_update); - _tiedProperties.Tie( "pressure-hpa", &_air_pressure); + _tiedProperties.Tie( "pressure-hpa", &_air_pressure_gl); + _tiedProperties.Tie( "pressure-sea-level-hpa", &_air_pressure_gl); _tiedProperties.Tie( "relative-humidity", &_relative_humidity_gl); _tiedProperties.Tie( "relative-humidity-sea-level", &_relative_humidity_sl); _tiedProperties.Tie( "dewpoint-degc", &_dewpoint_gl); @@ -315,7 +316,8 @@ void FGClimate::update_air_pressure() double Pd = P - Pv; // air pressure - _air_pressure = 0.01*(Pd+Psat); // hPa + _air_pressure_gl = 0.01*(Pd+Psat); // hPa + _air_pressure_sl = 0.01*(P0-Pv+Psat); // air density static const double Md = 0.0289654; @@ -897,6 +899,41 @@ void FGClimate::setEnvironmentUpdate(bool value) } } +std::string FGClimate::get_metar() +{ + std::stringstream m; + + m << std::fixed << std::setprecision(0); + m << "XXXX "; + + m << fgGetInt("/sim/time/utc/month"); + m << fgGetInt("/sim/time/utc/hour"); + m << fgGetInt("/sim/time/utc/minute"); + m << "Z AUTO"; + + m << " 000" << setw(2) << std::setfill('0') << _wind << "KMH "; + + if (_temperature_gl < 0.0) { + m << "M" << setw(2) << std::setfill('0') << fabs(_temperature_gl); + } else { + m << setw(2) << std::setfill('0') << _temperature_gl; + } + + m << "/"; + + if (_dewpoint_gl < 0.0) { + m << "M" << setw(2) << std::setfill('0') << fabs(_dewpoint_gl); + } else { + m << setw(2) << std::setfill('0') << _dewpoint_gl; + } + + m << " Q" << _air_pressure_sl; + + m << " NOSIG"; + + return m.str(); +} + // --------------------------------------------------------------------------- const std::string FGClimate::_classification[MAX_CLIMATE_CLASSES] = { @@ -1135,7 +1172,9 @@ void FGClimate::report() << std::endl; std::cout << " Relative humidity: " << _relative_humidity_gl << " %" << std::endl; - std::cout << " Air Pressure: " << _air_pressure << " hPa" + std::cout << " Ground Air Pressure: " << _air_pressure_gl << " hPa" + << std::endl; + std::cout << " Sea level Air Pressure: " << _air_pressure_sl << " hPa" << std::endl; std::cout << " Wind: " << _wind << " km/h" << std::endl << std::endl; std::cout << " Snow level: " << _snow_level << " m." << std::endl; @@ -1151,7 +1190,8 @@ void FGClimate::report() << _lichen_cover << std::endl; std::cout << " Season (0.0 = summer .. 1.0 = late autumn): " << ((_has_autumn && _is_autumn > 0.05) ? _season_transistional : 0.0) - << std::endl; + << std::endl << std::endl; + std::cout << " METAR: " << get_metar() << std::endl; std::cout << "===============================================" << std::endl; } #endif // REPORT_TO_CONSOLE diff --git a/src/Environment/climate.hxx b/src/Environment/climate.hxx index accfa0209..51eeb3d2a 100644 --- a/src/Environment/climate.hxx +++ b/src/Environment/climate.hxx @@ -51,6 +51,7 @@ public: void unbind() override; void update(double dt) override;; + std::string get_metar(); double get_snow_level_m() { return _snow_level; } double get_snow_thickness() { return _snow_thickness; } double get_ice_cover() { return _ice_cover; } @@ -152,7 +153,8 @@ private: bool _weather_update = false; // enable weather updates double _relative_humidity_sl = -99999.0;// 0.0 = dry, 1.0 is fully humid double _relative_humidity_gl = -99999.0; - double _air_pressure = 0.0; // air pressure in hPa + double _air_pressure_gl = 0.0; // ground level air pressure in hPa + double _air_pressure_sl = 0.0; // sea level air pressure in hPa double _air_density = 0.0; // air density in kg/m2 double _dewpoint_gl = -99999.0; double _dewpoint_sl = -99999.0;