File: test_set_bitmap.py

package info (click to toggle)
pygrib 2.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,444 kB
  • sloc: javascript: 1,365; python: 1,356; makefile: 39; ansic: 13
file content (46 lines) | stat: -rw-r--r-- 1,160 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import pygrib
import pytest

grib1 = False
if grib1:
    infile = '../sampledata/regular_latlon_surface.grib1'
else:
    infile = '../sampledata/regular_latlon_surface.grib2'
outfile = 'out.grib'
grbs = pygrib.open(infile)
grb = grbs.readline()
data = grb['values']
grb['missingValue']=9999.
grb['bitmapPresent']=1
nx = grb['Ni']; ny = grb['Nj']
data[3*ny//8:5*ny//8,3*nx//8:5*nx//8]=grb['missingValue']
grb['values']=data
msg = grb.tostring()
grbs.close()
f = open(outfile,'wb')
f.write(msg)
f.close()
 
grbs = pygrib.open('out.grib')
grb = grbs.readline()
data = grb['values']
lats,lons = grb.latlons()
grbs.close()
# should be a hole in the middle of the plot
from cartopy.util import add_cyclic_point
import cartopy.crs as ccrs
import matplotlib
import matplotlib.pyplot as plt

@pytest.mark.mpl_image_compare(tolerance=20,remove_text=True)
def test_set_bitmap():
    fig = plt.figure()
    ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=0))
    ax.coastlines()
    cs = ax.contourf(lons,lats,data,15)
    return fig

# if running with GUI backend, show plot.
if matplotlib.get_backend().lower() != 'agg':
    test_set_bitmap()
    plt.show()