Menu

[r910]: / trunk / toolkits / basemap / examples / test.py  Maximize  Restore  History

Download this file

260 lines (245 with data), 9.5 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# make plots of etopo bathymetry/topography data on
# various map projections, drawing coastlines, state and
# country boundaries, filling continents and drawing
# parallels/meridians
from matplotlib.toolkits.basemap import Basemap, interp
from pylab import *
import cPickle
def shiftgrid(lon0,datain,lonsin,start=True):
"""
shift global lat/lon grid east or west.
assumes wraparound (or cyclic point) is included.
lon0: starting longitude for shifted grid
(ending longitude if start=False). lon0 must be on
input grid (with the range of lonsin).
datain: original data.
lonsin: original longitudes.
start[True]: if True, lon0 represents he starting longitude
of the new grid. if False, lon0 is the ending longitude.
returns dataout,lonsout (data and longitudes on shifted grid).
"""
if fabs(lonsin[-1]-lonsin[0]-360.) > 1.e-4:
raise ValueError, 'cyclic point not included'
if lon0 < lonsin[0] or lon0 > lonsin[-1]:
raise ValueError, 'lon0 outside of range of lonsin'
i0 = argsort(fabs(lonsin-lon0))[0]
dataout = zeros(datain.shape,datain.typecode())
lonsout = zeros(lonsin.shape,lonsin.typecode())
if start:
lonsout[0:len(lonsin)-i0] = lonsin[i0:]
else:
lonsout[0:len(lonsin)-i0] = lonsin[i0:]-360.
dataout[:,0:len(lonsin)-i0] = datain[:,i0:]
if start:
lonsout[len(lonsin)-i0:] = lonsin[1:i0+1]+360.
else:
lonsout[len(lonsin)-i0:] = lonsin[1:i0+1]
dataout[:,len(lonsin)-i0:] = datain[:,1:i0+1]
return dataout,lonsout
def addcyclic(arrin,lonsin):
"""
add cyclic (wraparound) point in longitude.
"""
nlats = arrin.shape[0]
nlons = arrin.shape[1]
arrout = zeros((nlats,nlons+1),arrin.typecode())
arrout[:,0:nlons] = arrin[:,:]
arrout[:,nlons] = arrin[:,0]
lonsout = zeros(nlons+1,lonsin.typecode())
lonsout[0:nlons] = lonsin[:]
lonsout[nlons] = lonsin[-1] + lonsin[1]-lonsin[0]
return arrout,lonsout
# read in topo data from pickle (on a regular lat/lon grid)
# longitudes go from 20 to 380.
topodict = cPickle.load(open('etopo20.pickle','rb'))
topoin = topodict['data']; lons = topodict['lons']; lats = topodict['lats']
# shift data so lons go from 0 to 360 instead of 20 to 380.
topoin,lons = shiftgrid(360.,topoin,lons,start=False)
print 'min/max etopo20 data:'
print min(ravel(topoin)),max(ravel(topoin))
m = Basemap(lons[0],lats[0],lons[-1],lats[-1],\
resolution='c',area_thresh=10000.,projection='cyl')
xsize = rcParams['figure.figsize'][0]
fig=figure(figsize=(xsize,m.aspect*xsize))
ax = fig.add_axes([0.1,0.1,0.75,0.75])
im = imshow(topoin,cm.jet,extent=(m.llcrnrx,m.urcrnrx,m.llcrnry,m.urcrnry),origin='lower')
cax = axes([0.875, 0.1, 0.05, 0.75])
colorbar(tickfmt='%d', cax=cax) # draw colorbar
axes(ax) # make the original axes current again
m.drawcoastlines(ax)
#m.drawcountries(ax)
#m.drawstates(ax)
#m.fillcontinents(ax)
# draw parallels
delat = 30.
circles = arange(0.,90.+delat,delat).tolist()+\
arange(-delat,-90.-delat,-delat).tolist()
m.drawparallels(ax,circles)
# draw meridians
delon = 60.
lon1 = int(lons[0]/delon)*delon
lon2 = (int(lons[-1]/delon)+1)*delon
meridians = arange(lon1,lon2,delon)
m.drawmeridians(ax,meridians)
ax.set_xticks([]) # no ticks
ax.set_yticks([])
title('Cylindrical Equidistant')
print 'plotting Cylindrical Equidistant example, close plot window to proceed ...'
show()
m = Basemap(lons[0],-80.,lons[-1],80.,\
resolution='c',area_thresh=10000.,projection='merc',\
lon_0=0.5*(lons[0]+lons[-1]),lat_ts=20.)
# define grid (nx x ny regularly spaced native projection grid)
nx = len(lons); ny = int(80.*len(lats)/90.)
lonsout, latsout = m.makegrid(nx,ny)
topodat = interp(topoin,lons,lats,lonsout,latsout)
xsize = rcParams['figure.figsize'][0]
fig=figure(figsize=(xsize,m.aspect*xsize))
fig.add_axes([0.1,0.1,0.8,0.8])
im = imshow(topodat,cm.jet,extent=(m.llcrnrx,m.urcrnrx,m.llcrnry,m.urcrnry),origin='lower')
ax = gca() # get current axis instance
m.drawcoastlines(ax)
m.drawcountries(ax)
m.drawstates(ax)
m.fillcontinents(ax)
# draw parallels
m.drawparallels(ax,circles)
# draw meridians
m.drawmeridians(ax,meridians)
ax.set_xticks([]) # no ticks
ax.set_yticks([])
title('Mercator')
print 'plotting Mercator example, close plot window to proceed ...'
show()
m = Basemap(-145.5,1.,-2.566,46.352,\
resolution='c',area_thresh=10000.,projection='lcc',\
lat_1=50.,lon_0=-107.)
# define grid (nx x ny regularly spaced native projection grid)
nx = int((m.xmax-m.xmin)/40000.)+1; ny = int((m.ymax-m.ymin)/40000.)+1
lonsout, latsout = m.makegrid(nx,ny)
# adjust longitudes to be consistent with etopo data
# (which goes from 20 to 380, instead of 0 to 360).
topodat = interp(topoin,lons,lats,lonsout,latsout)
xsize = rcParams['figure.figsize'][0]
fig=figure(figsize=(xsize,m.aspect*xsize))
ax = fig.add_axes([0.1,0.1,0.75,0.75])
im = imshow(topodat,cm.jet,extent=(m.llcrnrx,m.urcrnrx,m.llcrnry,m.urcrnry),origin='lower')
cax = axes([0.875, 0.1, 0.05, 0.75])
colorbar(tickfmt='%d', cax=cax) # draw colorbar
axes(ax) # make the original axes current again
m.drawcoastlines(ax)
m.drawcountries(ax)
m.drawstates(ax)
#m.fillcontinents(ax)
# draw parallels
delat = 20.
circles = arange(0.,90.+delat,delat).tolist()+\
arange(-delat,-90.-delat,-delat).tolist()
m.drawparallels(ax,circles)
# draw meridians
delon = 30.
meridians = arange(0.,360.,delon)
m.drawmeridians(ax,meridians)
ax.set_xticks([]) # no ticks
ax.set_yticks([])
title('Lambert Conformal Conic')
print 'plotting Lambert Conformal example, close plot window to proceed ...'
show()
m = Basemap(-10.,20.,55.,75.,
resolution='l',projection='aea',\
lat_1=40.,lat_2=60,lon_0=35.)
# define grid (nx x ny regularly spaced native projection grid)
nx = int((m.xmax-m.xmin)/40000.)+1; ny = int((m.ymax-m.ymin)/40000.)+1
lonsout, latsout = m.makegrid(nx,ny)
# adjust longitudes to be consistent with etopo data
# (which goes from 20 to 380, instead of 0 to 360).
lonsout = where(lonsout < lons[0], lonsout+360., lonsout)
topodat = interp(topoin,lons,lats,lonsout,latsout)
xsize = rcParams['figure.figsize'][0]
fig=figure(figsize=(xsize,m.aspect*xsize))
ax = fig.add_axes([0.1,0.1,0.75,0.75])
im = imshow(topodat,cm.jet,extent=(m.llcrnrx,m.urcrnrx,m.llcrnry,m.urcrnry),origin='lower')
im.set_clim(-4000.,3000.)
cax = axes([0.875, 0.1, 0.05, 0.75])
colorbar(tickfmt='%d', cax=cax) # draw colorbar
axes(ax) # make the original axes current again
m.drawcoastlines(ax)
m.drawcountries(ax)
# draw parallels
delat = 20.
circles = arange(0.,90.+delat,delat).tolist()+\
arange(-delat,-90.-delat,-delat).tolist()
m.drawparallels(ax,circles)
# draw meridians
delon = 30.
meridians = arange(0.,360.,delon)
m.drawmeridians(ax,meridians)
ax.set_xticks([]) # no ticks
ax.set_yticks([])
title('Albers Equal Area Conic')
print 'plotting Albers Equal Area example, close plot window to proceed ...'
show()
# north polar projection.
#m = Basemap(-150.,-20.826,30.,-20.826,
# resolution='c',area_thresh=10000.,projection='stere',\
# lat_0=90.,lon_0=-105.,lat_ts=90.)
# south polar projection.
m = Basemap(-150.,20.826,30.,20.826,
resolution='c',area_thresh=10000.,projection='stere',\
lat_0=-90.,lon_0=-105.,lat_ts=-90.)
# define grid (nx x ny regularly spaced native projection grid)
nx = int((m.xmax-m.xmin)/40000.)+1; ny = int((m.ymax-m.ymin)/40000.)+1
lonsout, latsout = m.makegrid(nx,ny)
# adjust longitudes to be consistent with etopo data
lonsout = where(lonsout < lons[0], lonsout+360., lonsout)
topodat = interp(topoin,lons,lats,lonsout,latsout)
xsize = rcParams['figure.figsize'][0]
fig=figure(figsize=(xsize,m.aspect*xsize))
ax = fig.add_axes([0.1,0.1,0.75,0.75])
im = imshow(topodat,cm.jet,extent=(m.llcrnrx,m.urcrnrx,m.llcrnry,m.urcrnry),origin='lower')
cax = axes([0.875, 0.1, 0.05, 0.75])
colorbar(tickfmt='%d', cax=cax) # draw colorbar
axes(ax) # make the original axes current again
m.drawcoastlines(ax)
m.drawcountries(ax)
#m.fillcontinents(ax)
# draw parallels
m.drawparallels(ax,circles)
# draw meridians
m.drawmeridians(ax,meridians)
ax.set_xticks([]) # no ticks
ax.set_yticks([])
title('Polar Stereographic')
print 'plotting Stereographic example, close plot window to proceed ...'
show()
# lambert azimuthal north polar projection.
m = Basemap(-150.,-20.826,30.,-20.826,
resolution='c',area_thresh=10000.,projection='laea',\
lat_0=90.,lon_0=-105.,lat_ts=90.)
# define grid (nx x ny regularly spaced native projection grid)
nx = int((m.xmax-m.xmin)/40000.)+1; ny = int((m.ymax-m.ymin)/40000.)+1
lonsout, latsout = m.makegrid(nx,ny)
# adjust longitudes to be consistent with etopo data
lonsout = where(lonsout < lons[0], lonsout+360., lonsout)
topodat = interp(topoin,lons,lats,lonsout,latsout)
xsize = rcParams['figure.figsize'][0]
fig=figure(figsize=(xsize,m.aspect*xsize))
ax = fig.add_axes([0.1,0.1,0.75,0.75])
im = imshow(topodat,cm.jet,extent=(m.llcrnrx,m.urcrnrx,m.llcrnry,m.urcrnry),origin='lower')
cax = axes([0.875, 0.1, 0.05, 0.75])
colorbar(tickfmt='%d', cax=cax) # draw colorbar
axes(ax) # make the original axes current again
m.drawcoastlines(ax)
m.drawcountries(ax)
m.drawstates(ax)
#m.fillcontinents(ax)
# draw parallels
m.drawparallels(ax,circles)
# draw meridians
m.drawmeridians(ax,meridians)
ax.set_xticks([]) # no ticks
ax.set_yticks([])
title('Lambert Azimuthal Equal Area')
print 'plotting Lambert Azimuthal example, close plot window to proceed ...'
show()
print 'done'
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.