-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Salem-style horizontal coordinates with dimension renaming
- Loading branch information
Showing
9 changed files
with
345 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
netCDF4>=1.5.5 | ||
xarray>=0.18,!=0.20.0,!=0.20.1 | ||
donfig>=0.6.0 | ||
pyproj>=2.4.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import numpy as np | ||
import pyproj | ||
import pytest | ||
|
||
import xwrf | ||
from xwrf.grid import _wrf_grid_from_dataset, wgs84 | ||
|
||
|
||
@pytest.fixture(scope='session', params=['dummy']) | ||
def dummy_dataset(request): | ||
return xwrf.tutorial.open_dataset(request.param) | ||
|
||
|
||
@pytest.fixture(scope='session', params=['dummy_salem_parsed']) | ||
def dummy_salem(request): | ||
return xwrf.tutorial.open_dataset(request.param) | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
def test_grid(request): | ||
return xwrf.tutorial.open_dataset(request.param) | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
def cf_grid_mapping_name(request): | ||
"""A no-op to allow parallel indirect use with open dataset fixture.""" | ||
return request.param | ||
|
||
|
||
def test_grid_construction_against_salem(dummy_dataset, dummy_salem): | ||
grid_params = _wrf_grid_from_dataset(dummy_dataset) | ||
|
||
# Projection coordinate values | ||
np.testing.assert_array_almost_equal( | ||
grid_params['south_north'], dummy_salem['south_north'].values | ||
) | ||
np.testing.assert_array_almost_equal(grid_params['west_east'], dummy_salem['west_east'].values) | ||
|
||
# Projection CRS | ||
assert grid_params['crs'] == pyproj.CRS(dummy_salem['Q2'].attrs['pyproj_srs']) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'test_grid, cf_grid_mapping_name', | ||
[ | ||
('polar_stereographic_1', 'polar_stereographic'), | ||
('polar_stereographic_2', 'polar_stereographic'), | ||
('lambert_conformal', 'lambert_conformal_conic'), | ||
('mercator', 'mercator') | ||
], | ||
indirect=True, | ||
) | ||
def test_grid_construction_against_own_latlon(test_grid, cf_grid_mapping_name): | ||
grid_params = _wrf_grid_from_dataset(test_grid) | ||
trf = pyproj.Transformer.from_crs(grid_params['crs'], wgs84, always_xy=True) | ||
recalculated = {} | ||
recalculated['XLONG'], recalculated['XLAT'] = trf.transform( | ||
*np.meshgrid(grid_params['west_east'], grid_params['south_north']) | ||
) | ||
recalculated['XLONG_U'], recalculated['XLAT_U'] = trf.transform( | ||
*np.meshgrid(grid_params['west_east_stag'], grid_params['south_north']) | ||
) | ||
recalculated['XLONG_V'], recalculated['XLAT_V'] = trf.transform( | ||
*np.meshgrid(grid_params['west_east'], grid_params['south_north_stag']) | ||
) | ||
|
||
assert grid_params['crs'].to_cf()['grid_mapping_name'] == cf_grid_mapping_name | ||
for varname, recalculated_values in recalculated.items(): | ||
if varname in test_grid.data_vars: | ||
np.testing.assert_array_almost_equal( | ||
recalculated_values, | ||
test_grid[varname].values[0], | ||
decimal=2, | ||
err_msg=f'Computed {varname} does not match with raw output', | ||
) | ||
elif '_' not in varname and varname + '_M' in test_grid.data_vars: | ||
np.testing.assert_array_almost_equal( | ||
recalculated_values, | ||
test_grid[varname + '_M'].values[0], | ||
decimal=2, | ||
err_msg=f"Computed {varname + '_M'} does not match with raw output", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
"""Provide grid-related functionality specific to WRF datasets. | ||
This submodule contains code reused with modification from Salem under the terms of the BSD | ||
3-Clause License. Salem is Copyright (c) 2014-2021, Fabien Maussion and Salem Development Team All | ||
rights reserved. | ||
""" | ||
from __future__ import annotations # noqa: F401 | ||
|
||
from typing import Hashable, Mapping | ||
|
||
import numpy as np | ||
import pyproj | ||
import xarray as xr | ||
|
||
# Default CRS (lon/lat on WGS84, which is EPSG:4326) | ||
wgs84 = pyproj.CRS(4326) | ||
|
||
|
||
def _wrf_grid_from_dataset(ds: xr.Dataset) -> Mapping[Hashable, pyproj.CRS | np.ndarray]: | ||
"""Get the WRF projection and dimension coordinates out of the file.""" | ||
|
||
# Use standards from a typical WRF file | ||
cen_lon = ds.CEN_LON | ||
cen_lat = ds.CEN_LAT | ||
dx = ds.DX | ||
dy = ds.DY | ||
proj_id = ds.MAP_PROJ | ||
|
||
pargs = { | ||
'x_0': 0, | ||
'y_0': 0, | ||
'a': 6370000, | ||
'b': 6370000, | ||
'lat_1': ds.TRUELAT1, | ||
'lat_2': getattr(ds, 'TRUELAT2', ds.TRUELAT1), | ||
'lat_0': ds.MOAD_CEN_LAT, | ||
'lon_0': ds.STAND_LON, | ||
'center_lon': cen_lon, | ||
} | ||
|
||
if proj_id == 1: | ||
# Lambert | ||
pargs['proj'] = 'lcc' | ||
del pargs['center_lon'] | ||
elif proj_id == 2: | ||
# Polar stereo | ||
pargs['proj'] = 'stere' | ||
pargs['lat_ts'] = pargs['lat_1'] | ||
pargs['lat_0'] = 90.0 | ||
del pargs['lat_1'], pargs['lat_2'], pargs['center_lon'] | ||
elif proj_id == 3: | ||
# Mercator | ||
pargs['proj'] = 'merc' | ||
pargs['lat_ts'] = pargs['lat_1'] | ||
pargs['lon_0'] = pargs['center_lon'] | ||
del pargs['lat_0'], pargs['lat_1'], pargs['lat_2'], pargs['center_lon'] | ||
else: | ||
raise NotImplementedError(f'WRF proj not implemented yet: {proj_id}') | ||
|
||
# Construct the pyproj CRS (letting errors fail through) | ||
crs = pyproj.CRS(pargs) | ||
|
||
# Get grid specifications | ||
trf = pyproj.Transformer.from_crs(wgs84, crs, always_xy=True) | ||
nx = ds.dims['west_east'] | ||
ny = ds.dims['south_north'] | ||
e, n = trf.transform(cen_lon, cen_lat) | ||
x0 = -(nx - 1) / 2.0 * dx + e # DL corner | ||
y0 = -(ny - 1) / 2.0 * dy + n # DL corner | ||
|
||
return { | ||
'crs': crs, | ||
'south_north': y0 + np.arange(ny) * dy, | ||
'west_east': x0 + np.arange(nx) * dx, | ||
'south_north_stag': y0 + (np.arange(ny + 1) - 0.5) * dy, | ||
'west_east_stag': x0 + (np.arange(nx + 1) - 0.5) * dx, | ||
} |
Oops, something went wrong.