Skip to content

Commit

Permalink
Add WRF lon-lat projection (#224)
Browse files Browse the repository at this point in the history
* add map_proj = 6 (Lat-lon) (#221)

* add test and remove geogrid simulator

Co-authored-by: Matthias Demuzere <mdemuzere@gmail.com>
  • Loading branch information
fmaussion and matthiasdemuzere committed Sep 6, 2022
1 parent c42d3e5 commit b9e353c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion salem/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _lazy_property(self):
if not path.exists(download_dir):
makedirs(download_dir)

sample_data_gh_commit = 'd975954c9bbd6505b2e7a2e06aa42f4696a0ad6b'
sample_data_gh_commit = '57e6d694aa470b967336f5ca2d4fc743c5c8efd6'
sample_data_dir = path.join(cache_dir, 'salem-sample-data-' +
sample_data_gh_commit)

Expand Down
13 changes: 8 additions & 5 deletions salem/sio.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ def _wrf_grid_from_dataset(ds):
if ds.PROJ_NAME in ['Lambert Conformal Conic',
'WRF Lambert Conformal']:
proj_id = 1
elif ds.PROJ_NAME in ['lat-lon']:
proj_id = 6
else:
proj_id = 99 # pragma: no cover
else:
Expand Down Expand Up @@ -172,6 +174,12 @@ def _wrf_grid_from_dataset(ds):
'+lon_0={center_lon} ' \
'+x_0=0 +y_0=0 +a=6370000 +b=6370000'
p4 = p4.format(**pargs)
elif proj_id == 6:
# Lat-long
p4 = '+proj=eqc ' \
'+lon_0={lon_0} ' \
'+x_0=0 +y_0=0 +a=6370000 +b=6370000'
p4 = p4.format(**pargs)
else:
raise NotImplementedError('WRF proj not implemented yet: '
'{}'.format(proj_id))
Expand Down Expand Up @@ -515,11 +523,6 @@ def roi(self, ds=None, **kwargs):
kwargs.setdefault('grid', grid)

mask = self.grid.region_of_interest(**kwargs)
# coords = {self.y_dim: self._obj[self.y_dim].values,
# self.x_dim: self._obj[self.x_dim].values}
# mask = xr.DataArray(mask, coords=coords,
# dims=(self.y_dim, self.x_dim))

out = self._obj.where(mask, other=other)

# keep attrs and encoding
Expand Down
19 changes: 19 additions & 0 deletions salem/tests/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,25 @@ def test_wrf_polar(self):
np.testing.assert_allclose(reflon, mylon, atol=1e-4)
np.testing.assert_allclose(reflat, mylat, atol=1e-4)

@requires_geopandas
def test_wrf_latlon(self):

d = GeoNetcdf(get_demo_file('geo_em.d01_lon-lat.nc'))
mylon, mylat = d.grid.ll_coordinates
reflon = np.squeeze(d.get_vardata('XLONG_M'))
reflat = np.squeeze(d.get_vardata('XLAT_M'))

np.testing.assert_allclose(reflon, mylon, atol=1e-4)
np.testing.assert_allclose(reflat, mylat, atol=1e-4)

d = GeoNetcdf(get_demo_file('geo_em.d04_lon-lat.nc'))
mylon, mylat = d.grid.ll_coordinates
reflon = np.squeeze(d.get_vardata('XLONG_M'))
reflat = np.squeeze(d.get_vardata('XLAT_M'))

np.testing.assert_allclose(reflon, mylon, atol=1e-4)
np.testing.assert_allclose(reflat, mylat, atol=1e-4)

def test_longtime(self):
"""There was a bug with time"""

Expand Down

0 comments on commit b9e353c

Please sign in to comment.