Skip to content

Commit

Permalink
Fix small bug in geogrid_simulator (#121)
Browse files Browse the repository at this point in the history
* Fix small bug in geogrid_simulator

* Add key

* Forgot something

* Fix mpl version in tests tempeorarily

* Simplify Travis config
  • Loading branch information
fmaussion committed Oct 10, 2018
1 parent 4d04d16 commit ed4a76d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 29 deletions.
37 changes: 13 additions & 24 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: python
language: minimal
sudo: false
notifications:
email: false
Expand All @@ -13,34 +13,21 @@ branches:
matrix:
fast_finish: true
include:
- python: 2.7
env: CONDA_ENV=py27-all MPL=--mpl
- python: 3.6
env: CONDA_ENV=py36-all MPL=--mpl
- python: 3.6
env: CONDA_ENV=py36-min MPL=
- python: 3.6
env: CONDA_ENV=py36-xr MPL=
- python: 3.6
env: CONDA_ENV=py36-xarray-dev MPL=--mpl
- python: 3.6
env: CONDA_ENV=py36-all-rc MPL=--mpl
- env: CONDA_ENV=py27-all MPL=--mpl
- env: CONDA_ENV=py36-all MPL=--mpl
- env: CONDA_ENV=py36-min MPL=
- env: CONDA_ENV=py36-xr MPL=
- env: CONDA_ENV=py36-xarray-dev MPL=--mpl
- env: CONDA_ENV=py36-all-rc MPL=--mpl
allow_failures:
- python: 3.6
env: CONDA_ENV=py36-xr MPL=
- python: 3.6
env: CONDA_ENV=py36-xarray-dev MPL=--mpl
- python: 3.6
env: CONDA_ENV=py36-all-rc MPL=--mpl
- env: CONDA_ENV=py36-xr MPL=
- env: CONDA_ENV=py36-xarray-dev MPL=--mpl
- env: CONDA_ENV=py36-all-rc MPL=--mpl

before_install:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
- if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then
wget https://repo.continuum.io/miniconda/Miniconda2-latest-Linux-x86_64.sh -O miniconda.sh;
else
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh;
fi
- wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh;
- bash miniconda.sh -b -p $HOME/miniconda
- export PATH="$HOME/miniconda/bin:$PATH"
- hash -r
Expand All @@ -54,6 +41,8 @@ install:
- pip install -e .

script:
- which python
- python --version
- pytest salem $MPL --verbose --cov=salem --cov-report term-missing;

after_success:
Expand Down
2 changes: 1 addition & 1 deletion ci/requirements-py36-all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies:
- pandas
- xarray
- dask
- matplotlib
- matplotlib<3.0.0
- Pillow
- descartes
- cartopy
Expand Down
3 changes: 2 additions & 1 deletion docs/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Bug fixes

- the xarray accessor method ``roi()`` now preserves encoding (:issue:`109`).
By `Johannes Landmann <https://github.com/jlandmann>`_

- fixed a bug in ``geogrid_simulator`` that would occur if `stand_lon` and
`ref_lon` were not set to the same value (a strange idea tbh)


v0.2.2 (21 June 2018)
Expand Down
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 = '8f9d87afa68de6262124961926df16c28d65fb74'
sample_data_gh_commit = 'b6d201fd8c228d5a1a6ea97964ef769dfef186ec'
sample_data_dir = path.join(cache_dir, 'salem-sample-data-' +
sample_data_gh_commit)

Expand Down
17 changes: 17 additions & 0 deletions salem/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from salem import wgs84
from salem import utils, gis, wrftools, sio

API_KEY = None

def _to_scalar(x):
"""If a list then scalar"""
Expand Down Expand Up @@ -507,11 +508,19 @@ def __init__(self, center_ll=(11.38, 47.26), size_x=640, size_y=640,
any keyword accepted by motionless.CenterMap (e.g. `key` for the API)
"""

global API_KEY

# Google grid
grid = gis.googlestatic_mercator_grid(center_ll=center_ll,
nx=size_x, ny=size_y,
zoom=zoom, scale=scale)

if 'key' not in kwargs:
if API_KEY is None:
with open(utils.get_demo_file('.api_key'), 'r') as f:
API_KEY = f.read().replace('\n', '')
kwargs['key'] = API_KEY

# Motionless
googleurl = motionless.CenterMap(lon=center_ll[0], lat=center_ll[1],
size_x=size_x, size_y=size_y,
Expand Down Expand Up @@ -576,6 +585,8 @@ def __init__(self, x, y, crs=wgs84, size_x=640, size_y=640, scale=1,
play with the `size_x` and `size_y` kwargs.
"""

global API_KEY

if 'zoom' in kwargs or 'center_ll' in kwargs:
raise ValueError('incompatible kwargs.')

Expand All @@ -601,6 +612,12 @@ def __init__(self, x, y, crs=wgs84, size_x=640, size_y=640, scale=1,
else:
break

if 'key' not in kwargs:
if API_KEY is None:
with open(utils.get_demo_file('.api_key'), 'r') as f:
API_KEY = f.read().replace('\n', '')
kwargs['key'] = API_KEY

GoogleCenterMap.__init__(self, center_ll=mc, size_x=size_x,
size_y=size_y, zoom=zoom, scale=scale,
maptype=maptype, use_cache=use_cache, **kwargs)
16 changes: 16 additions & 0 deletions salem/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,22 @@ def test_lambert(self):
assert_allclose(lon, nc['XLONG_M'][0, ...], atol=1e-4)
assert_allclose(lat, nc['XLAT_M'][0, ...], atol=1e-4)

@requires_geopandas
def test_lambert_tuto(self):

from salem.wrftools import geogrid_simulator

g, m = geogrid_simulator(get_demo_file('namelist_tutorial.wps'))

assert len(g) == 1

fg = get_demo_file('geo_em.d01_tutorial.nc')
with netCDF4.Dataset(fg) as nc:
nc.set_auto_mask(False)
lon, lat = g[0].ll_coordinates
assert_allclose(lon, nc['XLONG_M'][0, ...], atol=1e-4)
assert_allclose(lat, nc['XLAT_M'][0, ...], atol=1e-4)

@requires_geopandas
def test_mercator(self):

Expand Down
9 changes: 7 additions & 2 deletions salem/wrftools.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,11 +648,16 @@ def geogrid_simulator(fpath, do_maps=True, map_kwargs=None):
if s0 == 'REF_LAT':
pargs['lat_0'] = float(s1[0])
if s0 == 'REF_LON':
pargs['lon_0'] = float(s1[0])
pargs['ref_lon'] = float(s1[0])
if s0 == 'TRUELAT1':
pargs['lat_1'] = float(s1[0])
if s0 == 'TRUELAT2':
pargs['lat_2'] = float(s1[0])
if s0 == 'STAND_LON':
pargs['lon_0'] = float(s1[0])

# Sometimes files are not complete
pargs.setdefault('lon_0', pargs['ref_lon'])

# define projection
if map_proj == 'LAMBERT':
Expand All @@ -674,7 +679,7 @@ def geogrid_simulator(fpath, do_maps=True, map_kwargs=None):
pwrf = gis.check_crs(pwrf)

# get easting and northings from dom center (probably unnecessary here)
e, n = pyproj.transform(wgs84, pwrf, pargs['lon_0'], pargs['lat_0'])
e, n = pyproj.transform(wgs84, pwrf, pargs['ref_lon'], pargs['lat_0'])

# LL corner
nx, ny = e_we[0]-1, e_sn[0]-1
Expand Down

0 comments on commit ed4a76d

Please sign in to comment.