Skip to content

Commit

Permalink
ENH: Add cartesian projection (#168)
Browse files Browse the repository at this point in the history
* Added cartesian projection

* Added comment

* Changed idealized treatment to skip CRS creation

* Added ideal.nc test file

* Added test for grid of idealized WRF data

* Removed ideal from being tested for grid_mapping existance
  • Loading branch information
lpilz authored May 9, 2024
1 parent f200efe commit 59cbc05
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
12 changes: 12 additions & 0 deletions tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,15 @@ def test_grid_construction_against_own_latlon(test_grid, cf_grid_mapping_name):
decimal=2,
err_msg=f"Computed {varname + '_M'} does not match with raw output",
)


@pytest.mark.parametrize(
'test_grid',
[
'ideal',
],
indirect=True,
)
def test_ideal_grid(test_grid):
grid_params = _wrf_grid_from_dataset(test_grid)
assert grid_params['crs'] is None
2 changes: 1 addition & 1 deletion tests/test_postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_include_projection_coordinates(sample_dataset):


@pytest.mark.parametrize(
'sample_dataset', set(xwrf.tutorial.sample_datasets.keys()) - {'tiny'}, indirect=True
'sample_dataset', set(xwrf.tutorial.sample_datasets.keys()) - {'tiny', 'ideal'}, indirect=True
)
def test_grid_mapping_is_in_all_vars(sample_dataset):
dataset = xwrf.postprocess._include_projection_coordinates(sample_dataset)
Expand Down
21 changes: 15 additions & 6 deletions xwrf/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ def _wrf_grid_from_dataset(ds: xr.Dataset) -> Mapping[Hashable, pyproj.CRS | np.
'center_lon': cen_lon,
}

if proj_id == 1:
if proj_id == 0:
# Idealized Run, Cartesian grid
pass
elif proj_id == 1:
# Lambert
pargs['proj'] = 'lcc'
del pargs['center_lon']
Expand All @@ -57,14 +60,20 @@ def _wrf_grid_from_dataset(ds: xr.Dataset) -> Mapping[Hashable, pyproj.CRS | np.
else:
raise NotImplementedError(f'WRF proj not implemented yet: {proj_id}')

# Construct the pyproj CRS (letting errors fail through)
crs = pyproj.CRS(pargs)
if proj_id == 0:
# As this is an idealized run, there is no physical CRS
crs = None
e, n = cen_lon, cen_lat
else:
# 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)
e, n = trf.transform(cen_lon, cen_lat)

# Get grid specifications
trf = pyproj.Transformer.from_crs(wgs84, crs, always_xy=True)
nx = ds.sizes['west_east']
ny = ds.sizes['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

Expand Down
11 changes: 6 additions & 5 deletions xwrf/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ def _include_projection_coordinates(ds: xr.Dataset) -> xr.Dataset:
for dim in horizontal_dims:
ds[dim] = (dim, grid_components[dim], config.get(f'cf_attribute_map.{dim}'))

# Include CRS
ds['wrf_projection'] = (tuple(), grid_components['crs'], grid_components['crs'].to_cf())
for varname in ds.data_vars:
if any(dim in ds[varname].dims for dim in horizontal_dims):
ds[varname].attrs['grid_mapping'] = 'wrf_projection'
# Include CRS if we don't have idealized coords
if grid_components['crs'] is not None:
ds['wrf_projection'] = (tuple(), grid_components['crs'], grid_components['crs'].to_cf())
for varname in ds.data_vars:
if any(dim in ds[varname].dims for dim in horizontal_dims):
ds[varname].attrs['grid_mapping'] = 'wrf_projection'

return ds

Expand Down
2 changes: 2 additions & 0 deletions xwrf/tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def _construct_cache_dir(path):
'tiny': 'data/tiny.nc',
'met_em_sample': 'data/met_em.d01.2005-08-28_12:00:00.nc',
'wrfout': 'data/wrfout_d01_2099-10-01_00:00:00.nc',
'ideal': 'data/ideal.nc',
}


Expand Down Expand Up @@ -65,6 +66,7 @@ def open_dataset(
* ``"mercator"``
* ``"met_em_sample"``
* ``"wrfout"``
* ``"ideal"``
Parameters
----------
Expand Down

0 comments on commit 59cbc05

Please sign in to comment.