|
6 | 6 | import pytz |
7 | 7 |
|
8 | 8 | from rocketpy import Environment |
| 9 | +from rocketpy.environment.weather_model_mapping import WeatherModelMapping |
9 | 10 | from rocketpy.environment.tools import geodesic_to_utm, utm_to_geodesic |
10 | 11 |
|
11 | 12 |
|
@@ -243,3 +244,70 @@ def test_environment_export_environment_exports_valid_environment_json( |
243 | 244 | ) |
244 | 245 |
|
245 | 246 | os.remove("environment.json") |
| 247 | + |
| 248 | + |
| 249 | +class _DummyDataset: |
| 250 | + """Small test double that mimics a netCDF dataset variables mapping.""" |
| 251 | + |
| 252 | + def __init__(self, variable_names): |
| 253 | + self.variables = {name: object() for name in variable_names} |
| 254 | + |
| 255 | + |
| 256 | +def test_resolve_dictionary_keeps_compatible_mapping(example_plain_env): |
| 257 | + """Keep the user-selected mapping when it already matches dataset keys.""" |
| 258 | + gfs_mapping = example_plain_env._Environment__weather_model_map.get("GFS") |
| 259 | + dataset = _DummyDataset( |
| 260 | + [ |
| 261 | + "time", |
| 262 | + "lat", |
| 263 | + "lon", |
| 264 | + "isobaric", |
| 265 | + "Temperature_isobaric", |
| 266 | + "Geopotential_height_isobaric", |
| 267 | + "u-component_of_wind_isobaric", |
| 268 | + "v-component_of_wind_isobaric", |
| 269 | + ] |
| 270 | + ) |
| 271 | + |
| 272 | + resolved = example_plain_env._Environment__resolve_dictionary_for_dataset( |
| 273 | + gfs_mapping, dataset |
| 274 | + ) |
| 275 | + |
| 276 | + assert resolved is gfs_mapping |
| 277 | + |
| 278 | + |
| 279 | +def test_resolve_dictionary_falls_back_to_legacy_mapping(example_plain_env): |
| 280 | + """Fallback to a compatible built-in mapping for legacy NOMADS-style files.""" |
| 281 | + thredds_gfs_mapping = example_plain_env._Environment__weather_model_map.get("GFS") |
| 282 | + dataset = _DummyDataset( |
| 283 | + [ |
| 284 | + "time", |
| 285 | + "lat", |
| 286 | + "lon", |
| 287 | + "lev", |
| 288 | + "tmpprs", |
| 289 | + "hgtprs", |
| 290 | + "ugrdprs", |
| 291 | + "vgrdprs", |
| 292 | + ] |
| 293 | + ) |
| 294 | + |
| 295 | + resolved = example_plain_env._Environment__resolve_dictionary_for_dataset( |
| 296 | + thredds_gfs_mapping, dataset |
| 297 | + ) |
| 298 | + |
| 299 | + # Explicit legacy mappings should be preferred over unrelated model mappings. |
| 300 | + assert resolved == example_plain_env._Environment__weather_model_map.get( |
| 301 | + "GFS_LEGACY" |
| 302 | + ) |
| 303 | + assert resolved["level"] == "lev" |
| 304 | + assert resolved["temperature"] == "tmpprs" |
| 305 | + assert resolved["geopotential_height"] == "hgtprs" |
| 306 | + |
| 307 | + |
| 308 | +def test_weather_model_mapping_exposes_legacy_aliases(): |
| 309 | + """Legacy mapping names should be available and case-insensitive.""" |
| 310 | + mapping = WeatherModelMapping() |
| 311 | + |
| 312 | + assert mapping.get("GFS_LEGACY")["temperature"] == "tmpprs" |
| 313 | + assert mapping.get("gfs_legacy")["temperature"] == "tmpprs" |
0 commit comments