Menu

[r4579]: / trunk / toolkits / basemap / examples / fcstmaps.py  Maximize  Restore  History

Download this file

116 lines (104 with data), 3.8 kB

  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
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# this example reads today's numerical weather forecasts
# from the NOAA OpenDAP servers and makes a multi-panel plot.
from pylab import title, show, figure, cm, figtext, \
meshgrid, axes, colorbar
import numpy
import sys
from numpy import ma
import datetime
from matplotlib.toolkits.basemap import Basemap, NetCDFFile, addcyclic
hrsgregstart = 13865688 # hrs from 00010101 to 15821015 in Julian calendar.
# times in many datasets use mixed Gregorian/Julian calendar, datetime
# module uses a proleptic Gregorian calendar. So, I use datetime to compute
# hours since start of Greg. calendar (15821015) and add this constant to
# get hours since 1-Jan-0001 in the mixed Gregorian/Julian calendar.
gregstart = datetime.datetime(1582,10,15) # datetime.datetime instance
def dateto_hrs_since_day1CE(curdate):
"""given datetime.datetime instance, compute hours since 1-Jan-0001"""
if curdate < gregstart:
msg = 'date must be after start of gregorian calendar (15821015)!'
raise ValueError, msg
difftime = curdate-gregstart
hrsdiff = 24*difftime.days + difftime.seconds/3600
return hrsdiff+hrsgregstart
def hrs_since_day1CE_todate(hrs):
"""return datetime.datetime instance given hours since 1-Jan-0001"""
if hrs < 0.0:
msg = "hrs must be positive!"
raise ValueError, msg
delta = datetime.timedelta(hours=1)
hrs_sincegreg = hrs - hrsgregstart
curdate = gregstart + hrs_sincegreg*delta
return curdate
# today's date is default.
if len(sys.argv) > 1:
YYYYMMDD = sys.argv[1]
else:
YYYYMMDD = datetime.datetime.today().strftime('%Y%m%d')
YYYYMM = YYYYMMDD[0:6]
# set OpenDAP server URL.
URLbase="http://nomad3.ncep.noaa.gov:9090/dods/mrf/mrf"
URL=URLbase+YYYYMMDD+'/mrf'+YYYYMMDD
print URL+'\n'
try:
data = NetCDFFile(URL)
except:
msg = """
opendap server not providing the requested data.
Try another date by providing YYYYMM on command line."""
raise IOError, msg
# read lats,lons,times.
print data.variables.keys()
latitudes = data.variables['lat']
longitudes = data.variables['lon']
fcsttimes = data.variables['time']
times = fcsttimes[0:6] # first 6 forecast times.
ntimes = len(times)
# put forecast times in YYYYMMDDHH format.
verifdates = []
fcsthrs=[]
print times
for time in times:
fcsthrs.append(int((time-times[0])*24))
fdate = hrs_since_day1CE_todate(int(time*24.0))
verifdates.append(fdate.strftime('%Y%m%d%H'))
print fcsthrs
print verifdates
lats = latitudes[:]
nlats = len(lats)
lons1 = longitudes[:]
nlons = len(lons1)
# unpack 2-meter temp forecast data.
t2mvar = data.variables['tmp2m']
t2min = t2mvar[0:ntimes,:,:]
t2m = numpy.zeros((ntimes,nlats,nlons+1),t2min.dtype)
# create Basemap instance for Orthographic projection.
m = Basemap(lon_0=-90,lat_0=60,projection='ortho')
# add wrap-around point in longitude.
for nt in range(ntimes):
t2m[nt,:,:], lons = addcyclic(t2min[nt,:,:], lons1)
# convert to celsius.
t2m = t2m-273.15
# contour levels
clevs = numpy.arange(-30,30.1,2.)
lons, lats = meshgrid(lons, lats)
x, y = m(lons, lats)
# create figure.
fig=figure(figsize=(6,8))
# make subplots.
for nt,fcsthr in enumerate(fcsthrs):
ax = fig.add_subplot(321+nt)
cs = m.contourf(x,y,t2m[nt,:,:],clevs,cmap=cm.jet,extend='both')
m.drawcoastlines(linewidth=0.5)
m.drawcountries()
m.drawparallels(numpy.arange(-80,81,20))
m.drawmeridians(numpy.arange(0,360,20))
# panel title
title(repr(fcsthr)+'-h forecast valid '+verifdates[nt],fontsize=9)
# figure title
figtext(0.5,0.95,u"2-m temp (\N{DEGREE SIGN}C) forecasts from %s"%verifdates[0],
horizontalalignment='center',fontsize=14)
# a single colorbar.
cax = axes([0.1, 0.03, 0.8, 0.025])
colorbar(cax=cax, orientation='horizontal')
show()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.