| 1 | // Köppen-Geiger climate interface class |
| 2 | // |
| 3 | // Written by Erik Hofman, started October 2020 |
| 4 | // |
| 5 | // Copyright (C) 2020 by Erik Hofman <[email protected]> |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or modify |
| 8 | // it under the terms of the GNU General Public License as published by |
| 9 | // the Free Software Foundation; either version 2 of the License, or |
| 10 | // (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, |
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | // GNU General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License along |
| 18 | // with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 20 | // |
| 21 | // $Id$ |
| 22 | |
| 23 | #ifndef _FGCLIMATE_HXX |
| 24 | #define _FGCLIMATE_HXX |
| 25 | |
| 26 | #include <osg/ref_ptr> |
| 27 | #include <osg/Image> |
| 28 | |
| 29 | #include <simgear/props/tiedpropertylist.hxx> |
| 30 | #include <simgear/math/SGGeod.hxx> |
| 31 | |
| 32 | #define REPORT_TO_CONSOLE 0 |
| 33 | |
| 34 | /* |
| 35 | * Update environment parameters based on the Köppen-Geiger climate |
| 36 | * map of the world based on lattitude and longitude. |
| 37 | */ |
| 38 | |
| 39 | class FGLight; |
| 40 | |
| 41 | #define MAX_CLIMATE_CLASSES 32 |
| 42 | |
| 43 | class FGClimate : public SGSubsystem { |
| 44 | private: |
| 45 | struct _ground_tile { |
| 46 | SGGeod pos; |
| 47 | |
| 48 | double elevation_m = 0.0; |
| 49 | double temperature = -99999.0; // degrees Celsius |
| 50 | double temperature_mean = -99999.0; // degrees Celsius |
| 51 | double temperature_water = -99999.0; // degrees Celsius |
| 52 | double relative_humidity = -99999.0; // percent |
| 53 | double precipitation_annual = -99999.0; |
| 54 | double precipitation = -99999.0; |
| 55 | bool has_autumn = false; |
| 56 | |
| 57 | double dewpoint = -99999.0; // degrees Celsius |
| 58 | double pressure = 0.0; |
| 59 | |
| 60 | } _ground_tile; |
| 61 | using ClimateTile = struct _ground_tile; |
| 62 | |
| 63 | public: |
| 64 | FGClimate(); |
| 65 | virtual ~FGClimate() = default; |
| 66 | |
| 67 | void bind() override; |
| 68 | void init() override; |
| 69 | void reinit() override; |
| 70 | void unbind() override; |
| 71 | void update(double dt) override;; |
| 72 | |
| 73 | double get_snow_level_m() { return _snow_level; } |
| 74 | double get_snow_thickness() { return _snow_thickness; } |
| 75 | double get_ice_cover() { return _ice_cover; } |
| 76 | double get_dust_cover() { return _dust_cover; } |
| 77 | double get_wetness() { return _wetness; } |
| 78 | double get_lichen_cover() { return _lichen_cover; } |
| 79 | |
| 80 | double get_relative_humidity_pct() { return _gl.relative_humidity; } |
| 81 | double get_relative_humidity_sea_level_pct() { return _sl.relative_humidity; } |
| 82 | double get_pressure_hpa() { return _gl.pressure; } |
| 83 | double get_pressure_sea_leevel_hpa() { return _sl.pressure; } |
| 84 | double get_dewpoint_degc() { return _gl.dewpoint; } |
| 85 | double get_dewpoint_sl_degc() { return _sl.dewpoint; } |
| 86 | double get_temperature_degc() { return _gl.temperature; } |
| 87 | double get_temperature_sea_leevel_degc() { return _sl.temperature; } |
| 88 | double get_temperature_mean_degc() { return _gl.temperature_mean; } |
| 89 | double get_temperature_mean_sea_level_degc() { return _sl.temperature_mean; } |
| 90 | double get_temperature_water_degc() { return _gl.temperature_water; } |
| 91 | double get_temperature_seawater_degc() { return _sl.temperature_water; } |
| 92 | double get_precipitation_month() { return _gl.precipitation; } |
| 93 | double get_precipitation_annual() { return _gl.precipitation_annual; } |
| 94 | |
| 95 | double get_wind_mps() { return _wind_speed; } |
| 96 | double get_wind_direction_deg() { return _wind_direction; } |
| 97 | |
| 98 | bool getEnvironmentUpdate() const { return _environment_adjust; } |
| 99 | void setEnvironmentUpdate(bool value); |
| 100 | |
| 101 | const char* get_metar() const; |
| 102 | |
| 103 | void test(); |
| 104 | private: |
| 105 | static const std::string _classification[MAX_CLIMATE_CLASSES]; |
| 106 | static const std::string _description[MAX_CLIMATE_CLASSES]; |
| 107 | |
| 108 | #if REPORT_TO_CONSOLE |
| 109 | void report(); |
| 110 | #endif |
| 111 | inline void _set(double& prev, double val) { |
| 112 | prev = (prev < -1000.0) ? val : 0.99*prev + 0.01*val; |
| 113 | } |
| 114 | |
| 115 | // interpolate val (from 0.0 to 1.0) between min and max |
| 116 | double daytime(double val, double offset = 0.0); |
| 117 | double season(double val, double offset = 0.0); |
| 118 | |
| 119 | double linear(double val, double min, double max); |
| 120 | double triangular(double val, double min, double max); |
| 121 | double sinusoidal(double val, double min, double max); |
| 122 | double even(double val, double min, double max); |
| 123 | double long_low(double val, double min, double max); |
| 124 | double long_high(double val, double min, double max); |
| 125 | double monsoonal(double val, double min, double max); |
| 126 | |
| 127 | void set_ocean(); |
| 128 | void set_dry(); |
| 129 | void set_tropical(); |
| 130 | void set_temperate(); |
| 131 | void set_continetal(); |
| 132 | void set_polar(); |
| 133 | void set_environment(); |
| 134 | |
| 135 | void update_daylight(); |
| 136 | void update_day_factor(); |
| 137 | void update_season_factor(); |
| 138 | void update_pressure(); |
| 139 | void update_wind(); |
| 140 | |
| 141 | SGPropertyNode_ptr _rootNode; |
| 142 | simgear::TiedPropertyList _tiedProperties; |
| 143 | |
| 144 | SGPropertyNode_ptr _monthNode; |
| 145 | SGPropertyNode_ptr _gravityNode; |
| 146 | SGPropertyNode_ptr _metarSnowLevelNode; |
| 147 | SGPropertyNode_ptr _positionLatitudeNode; |
| 148 | SGPropertyNode_ptr _positionLongitudeNode; |
| 149 | |
| 150 | osg::ref_ptr<osg::Image> image; |
| 151 | double _image_width = 0; |
| 152 | double _image_height = 0; |
| 153 | |
| 154 | double _epsilon = 1.0; |
| 155 | double _prev_lat = -99999.0; |
| 156 | double _prev_lon = -99999.0; |
| 157 | |
| 158 | double _sun_latitude_deg = 0.0; |
| 159 | double _sun_longitude_deg = 0.0; |
| 160 | |
| 161 | double _adj_latitude_deg = 0.0; // viewer lat adjusted for sun lat |
| 162 | double _adj_longitude_deg = 0.0; // viewer lat adjusted for sun lon |
| 163 | double _elevation_m = 0.0; |
| 164 | |
| 165 | double _daytime = 0.0; |
| 166 | double _day_noon = 1.0; |
| 167 | double _day_light = 1.0; |
| 168 | double _season_summer = 1.0; |
| 169 | double _season_transistional = 0.0; |
| 170 | double _seasons_year = 0.0; |
| 171 | double _is_autumn = -99999.0; |
| 172 | |
| 173 | const double _max_visibility_m = 35000.0; |
| 174 | double _visibility_m = _max_visibility_m; |
| 175 | |
| 176 | // Köppen-Geiger classicfications |
| 177 | ClimateTile _tiles[3][3]; |
| 178 | |
| 179 | // environment |
| 180 | bool _environment_adjust = false; // enable automatic adjestments |
| 181 | bool _inland_ice_cover = false; // inland water bodies get frozen over |
| 182 | double _snow_level = -99999.0; // in meters |
| 183 | double _snow_thickness = -99999.0; // 0.0 = thin, 1.0 = thick |
| 184 | double _ice_cover = -99999.0; // 0.0 = none, 1.0 = thick |
| 185 | double _dust_cover = -99999.0; // 0.0 = none, 1.0 = dusty |
| 186 | double _wetness = -99999.0; // 0.0 = dry, 1.0 = wet |
| 187 | double _lichen_cover = -99999.0; // 0.0 = none, 1.0 = mossy |
| 188 | |
| 189 | // HDR |
| 190 | double _fog = 0.0; |
| 191 | double _mist = 0.0; |
| 192 | double _aerosol_density = 1.0e16; |
| 193 | |
| 194 | // weather |
| 195 | int _code = 0; // Köppen-Geiger classicfication |
| 196 | ClimateTile _gl; // ground level parameters |
| 197 | |
| 198 | ClimateTile _sl; // sea level parameters |
| 199 | bool _weather_update = false; // enable weather updates |
| 200 | double _wind_speed = 0.0; // wind in meters per second |
| 201 | double _wind_direction = -99999.0; // wind direction in degrees |
| 202 | |
| 203 | char _metar[256] = ""; |
| 204 | }; |
| 205 | |
| 206 | #endif // _FGCLIMATE_HXX |