Skip to content

Commit

Permalink
Merge pull request #160 from jswhit/cartopy
Browse files Browse the repository at this point in the history
migrate test/*py from basemap to cartopy
  • Loading branch information
jswhit authored Dec 3, 2020
2 parents e93129d + 1782cf1 commit 6ce5c7b
Show file tree
Hide file tree
Showing 49 changed files with 352 additions and 310 deletions.
13 changes: 12 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,24 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install libproj-dev proj-bin libeccodes-dev
# sudo apt-get install libgeos-dev

- name: Update Pip
run: |
python -m pip install --upgrade pip
- name: Install pygrib dependencies via pip
run: |
python -m pip install cython numpy "pyproj<3.0.0"
python -m pip install "numpy>1.10"
python -m pip install cython
python -m pip install "pyproj<3.0.0"
# python -m pip install wheel
# python -m pip install pyshp
# python -m pip install six
# python -m pip install shapely --no-binary shapely
# python -m pip install matplotlib
# python -m pip install cartopy
# python -m pip install scipy

- name: Install pygrib
run: |
Expand All @@ -39,3 +49,4 @@ jobs:
- name: Test
run: |
python test.py
# cd test; python run_tests.py
6 changes: 6 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
version 2.1.1 (git tag v2.1.1rel)
=================================
* clean up docs.
* migrate examples in 'test' directory from basemap to cartopy.
* enable github actions for CI (PR #159).

version 2.1 (git tag v2.1rel)
============================
* split ncepgrib2 module into a separate project
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ The easiest way to get everything installed is to use the [conda](https://conda.
conda install -c conda-forge pygrib
```

If you don't use conda, be sure you have the required dependencies
(eccodes,numpy,cython,pyproj) installed first. Then, install pygrib with pip:
If you don't use conda, be sure you have the ECCODES library installed first.
Then you can install pygrib with pip:

```
ECCODES_DIR=path/to/eccodes pip install pygrib
Expand Down
2 changes: 1 addition & 1 deletion pygrib.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '2.1'
__version__ = '2.1.1'

import numpy as np
import warnings
Expand Down
1 change: 0 additions & 1 deletion requirements.in

This file was deleted.

7 changes: 0 additions & 7 deletions requirements.txt

This file was deleted.

Binary file removed sampledata/eumetsat_precip.grb
Binary file not shown.
Binary file removed sampledata/tpcprblty.grib2
Binary file not shown.
8 changes: 5 additions & 3 deletions test/README
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
these examples read GRIB files and generate
plots of the data using matplotlib and the basemap toolkit
(both availabe from http://matplotlib.sf.net).
plots of the data using matplotlib and cartopy

test_spectral.py also uses pyspharm, available from
http://code.google.com/p/pyspharm.
http://github.com/jswhit/pyspharm

to run all the tests with a non-gui backend (Agg) and compare
images to archived baselines, run 'python run_tests.py'.
33 changes: 19 additions & 14 deletions test/animate.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
import pygrib, time
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap
from cartopy.util import add_cyclic_point
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.animation as animation

# animation example.

grbs = pygrib.open('../sampledata/safrica.grib2')
# grab all "brightness temp" grib messages.
btemps = [grb for grb in grbs if grb['name']=='Brightness temperature']
btemps = [grb for grb in grbs if grb.name=='Brightness temperature']
grb = btemps[0]
lats, lons = grb.latlons()
projd = grb.projparams
print(projd)
grbs.close()

# create a map projection for the domain, plot 1st image on it.
m =\
Basemap(projection=projd['proj'],lat_ts=projd['lat_ts'],lon_0=projd['lon_0'],\
lat_0=projd['lat_0'],rsphere=(projd['a'],projd['b']),\
llcrnrlat=lats[0,0],urcrnrlat=lats[-1,-1],\
llcrnrlon=lons[0,0],urcrnrlon=lons[-1,-1],resolution='i')
fig = plt.figure(figsize=(8,7))
m.drawcoastlines()
m.drawcountries()
globe = ccrs.Globe(ellipse='sphere', semimajor_axis=projd['a'], semiminor_axis=projd['b'])
pj = ccrs.Stereographic(globe=globe,central_longitude=projd['lon_0'],
central_latitude=projd['lat_0'],
true_scale_latitude=projd['lat_ts'])
ax = fig.add_subplot(1,1,1,projection=pj)
coords = pj.transform_points(
ccrs.PlateCarree(), np.asarray([lons[0,0],lons[-1,-1]]), np.asarray([lats[0,0],lats[-1,-1]]))
ax.set_extent([coords[0, 0], coords[1, 0], coords[0, 1], coords[1, 1]], crs=pj)
ax.coastlines()
ax.add_feature(cfeature.BORDERS, linestyle='-');
grb = btemps[0]
im = m.imshow(grb['values'],interpolation='nearest',vmin=230,vmax=310)
plt.colorbar(orientation='horizontal')
m.drawparallels(np.arange(-80,10,10),labels=[1,0,0,0])
m.drawmeridians(np.arange(-80,81,20),labels=[0,0,0,1])
im = plt.imshow(np.empty(lons.shape),vmin=230,vmax=310,transform=ccrs.PlateCarree(),origin='lower')
im.set_data(grb.values)
plt.colorbar(im,orientation='horizontal')
txt = plt.title(grb,fontsize=8)

def updatefig(nt):
global im,txt,btemps,cnt,delay
grb = btemps[nt]
im.set_data(grb['values'])
im.set_data(grb.values)
txt.set_text(repr(grb))

ani = animation.FuncAnimation(fig, updatefig, frames=len(btemps))
Expand Down
Binary file added test/ectigge_baseline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/gaussian_baseline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/lambert_baseline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/ndfd_conus_baseline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/ndfd_pr_baseline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/reduced_ll_baseline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/reglatlon_baseline1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/reglatlon_baseline2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/reglatlon_baseline3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/rotated_ll_baseline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions test/run_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# run all matplotlib based tests
import glob
import matplotlib
matplotlib.rcParams.update(matplotlib.rcParamsDefault)
matplotlib.use('agg')
# Find all test files.
test_files = glob.glob('test_*.py')
#test_files.remove('test_spectral.py') # skip spectral transform test
for f in test_files:
print('running %s...' % f)
exec(open(f).read())
Binary file added test/set_bitmap_baseline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/spectral_baseline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/stere_baseline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/stere_baseline2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 21 additions & 16 deletions test/test_ectigge.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import pygrib
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from numpy import ma
from mpl_toolkits.basemap import Basemap

from cartopy.util import add_cyclic_point
import cartopy.crs as ccrs
from matplotlib.testing.compare import compare_images

grbs = pygrib.open('../sampledata/ecmwf_tigge.grb')
grb = grbs.select(parameterName='Soil moisture')[0]
fld = grb.values; lats,lons = grb.latlons()
lons1 = lons[0,:]; lats1 = lats[:,0]
# add cyclic (wrap-around) point to global grid
fld,lons1 = add_cyclic_point(fld, coord=lons1)
lons,lats = np.meshgrid(lons1,lats1)

m = Basemap(lon_0=180)
CS = m.contourf(lons,lats,fld,15,cmap=plt.cm.jet)
plt.colorbar(shrink=0.6)
m.drawcoastlines()
# draw parallels
delat = 30.
circles = np.arange(-90.,90.+delat,delat)
m.drawparallels(circles,labels=[1,0,0,0])
# draw meridians
delon = 60.
meridians = np.arange(0,360,delon)
m.drawmeridians(meridians,labels=[0,0,0,1])
plt.title(grb['parameterName']+' on ECMWF Reduced Gaussian Grid')
plt.figure()
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=0))
cs = ax.contourf(lons,lats,fld,15,cmap=plt.cm.jet)
plt.colorbar(cs, shrink=0.6)
ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.ylabels_top = False; gl.xlabels_top = False
gl.ylabels_right = False; gl.xlabels_right = False
plt.title(grb.parameterName+' on ECMWF Reduced Gaussian Grid')
if matplotlib.get_backend().lower() == 'agg':
# raise exception if generated image doesn't match baseline
plt.savefig('ectigge.png')
assert( compare_images('ectigge_baseline.png','ectigge.png',10) is None )
plt.show()
61 changes: 0 additions & 61 deletions test/test_eumetsat.py

This file was deleted.

28 changes: 20 additions & 8 deletions test/test_gaussian.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import pygrib
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from cartopy.util import add_cyclic_point
from matplotlib.testing.compare import compare_images
import cartopy.crs as ccrs
grbs = pygrib.open('../sampledata/flux.grb')
grb = grbs.message(2)
lats, lons = grb.latlons()
data = grb['values']
m = Basemap(lon_0=180)
#m.scatter(lons.flat,lats.flat,1,marker='o',color='k',zorder=10)
x,y = m(lons,lats)
m.drawcoastlines()
m.contourf(x,y,data,15)
#m.fillcontinents()
lons1 = lons[0,:]; lats1 = lats[:,0]
data = grb.values
# add cyclic (wrap-around) point to global grid
data,lons1 = add_cyclic_point(data, coord=lons1)
lons,lats = np.meshgrid(lons1,lats1)
plt.figure()
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=0))
ax.coastlines()
ax.contourf(lons,lats,data,15)
# plot location of every 4th grid point
plt.scatter(lons[::4,::4].ravel(),lats[::4,::4].ravel(),1,marker='o',color='k',zorder=10)
plt.title('Global Gaussian Grid')
if matplotlib.get_backend().lower() == 'agg':
# raise exception if generated image doesn't match baseline
plt.savefig('gaussian.png')
assert( compare_images('gaussian_baseline.png','gaussian.png',10) is None )
plt.show()
40 changes: 22 additions & 18 deletions test/test_lambert.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import pygrib
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import cartopy.crs as ccrs
from matplotlib.testing.compare import compare_images

grbs = pygrib.open('../sampledata/eta.grb')
grb = grbs.select(parameterName='Pressure',typeOfLevel='surface')[0]
data = grb.values
lats, lons = grb.latlons()
llcrnrlon = lons[0,0]
llcrnrlat = lats[0,0]
urcrnrlon = lons[-1,-1]
urcrnrlat = lats[-1,-1]
rsphere = (grb.projparams['a'], grb.projparams['b'])
lon_0 = grb.projparams['lon_0']
lat_1 = grb.projparams['lat_1']
lat_2 = grb.projparams['lat_2']
projection = grb.projparams['proj']
m = Basemap(llcrnrlon=llcrnrlon,llcrnrlat=llcrnrlat,
urcrnrlon=urcrnrlon,urcrnrlat=urcrnrlat,rsphere=rsphere,lon_0=lon_0,
lat_1=lat_1,lat_2=lat_2,resolution='l',projection=projection)
x,y = m(lons, lats)
m.scatter(x.flat,y.flat,3,marker='o',color='k',zorder=10)
m.drawcoastlines()
x,y = m(lons,lats)
m.contourf(x,y,data,15)

globe = ccrs.Globe(ellipse='sphere', semimajor_axis=grb.projparams['a'], semiminor_axis=grb.projparams['b'])
pj = ccrs.LambertConformal(globe=globe,central_longitude=grb.projparams['lon_0'],
central_latitude=grb.projparams['lat_0'],
standard_parallels =(grb.projparams['lat_1'],grb.projparams['lat_2']))
ax = plt.axes(projection=pj)
coords = pj.transform_points(
ccrs.PlateCarree(), np.asarray([lons[0,0],lons[-1,-1]]), np.asarray([lats[0,0],lats[-1,-1]]))
ax.set_extent([coords[0, 0], coords[1, 0], coords[0, 1], coords[1, 1]], crs=pj)
ax.scatter(lons.flat,lats.flat,3,marker='o',color='k',zorder=10,transform=ccrs.PlateCarree())
ax.coastlines()
coords = pj.transform_points(ccrs.PlateCarree(), lons, lats)
cs = ax.contourf(coords[:,:,0],coords[:,:,1],data,15)
plt.title('Lambert Conformal Model Grid')
if matplotlib.get_backend().lower() == 'agg':
# raise exception if generated image doesn't match baseline
plt.savefig('lambert.png')
assert( compare_images('lambert_baseline.png','lambert.png',10) is None )
plt.show()
Loading

0 comments on commit 6ce5c7b

Please sign in to comment.