File: animate.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,637 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, time
import matplotlib.pyplot as plt
import numpy as np
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']
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.
fig = plt.figure(figsize=(8,7))
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 = 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)
    txt.set_text(repr(grb))

ani = animation.FuncAnimation(fig, updatefig, frames=len(btemps))

plt.show()