Skip to content

Commit aa12d7d

Browse files
author
Jeff Whitaker
committed
Merge pull request #105 from jswhit/master
update test_rotpole.py example
2 parents 83e28f1 + 6911f4c commit aa12d7d

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

examples/test_rotpole.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
from netCDF4 import Dataset
23
from mpl_toolkits.basemap import Basemap
34
import numpy as np
@@ -23,17 +24,33 @@
2324
plt.title('rotated pole data in polar stere map')
2425

2526
plt.figure()
27+
2628
# o_lon_p, o_lat_p: true lat/lon of pole in rotated coordinate system
29+
2730
# mapping to CF metadata convention:
2831
# grid_north_pole_longitude = normalize180(180 + lon_0), where normalize180
29-
# is a function that maps to interval [-180,180].
32+
# is a function that maps to interval [-180,180).
3033
# grid_north_pole_latitude = o_lat_p
3134
# north_pole_grid_longitude = o_lon_p (optional, assumed zero if not present)
32-
m = Basemap(projection='rotpole',lon_0=rotpole.grid_north_pole_longitude-180.,\
33-
o_lon_p=rotpole.north_pole_grid_longitude,\
34-
o_lat_p=rotpole.grid_north_pole_latitude,\
35-
llcrnrlat = lats[0,0], urcrnrlat = lats[-1,-1],\
36-
llcrnrlon = lons[0,0], urcrnrlon = lons[-1,-1],resolution='c')
35+
36+
def normalize180(lon):
37+
"""Normalize lon to range [180, 180)"""
38+
lower = -180.; upper = 180.
39+
if lon > upper or lon == lower:
40+
lon = lower + abs(lon + upper) % (abs(lower) + abs(upper))
41+
if lon < lower or lon == upper:
42+
lon = upper - abs(lon - lower) % (abs(lower) + abs(upper))
43+
return lower if lon == upper else lon
44+
45+
lon_0 = normalize180(rotpole.grid_north_pole_longitude-180.)
46+
o_lon_p = rotpole.north_pole_grid_longitude
47+
o_lat_p = rotpole.grid_north_pole_latitude
48+
print( rotpole )
49+
print( 'lon_0,o_lon_p,o_lat_p=',lon_0,o_lon_p,o_lat_p)
50+
51+
m= Basemap(projection='rotpole',lon_0=lon_0,o_lon_p=o_lon_p,o_lat_p=o_lat_p,\
52+
llcrnrlat = lats[0,0], urcrnrlat = lats[-1,-1],\
53+
llcrnrlon = lons[0,0], urcrnrlon = lons[-1,-1],resolution='c')
3754
x,y = m(lons,lats)
3855
m.drawcoastlines()
3956
m.contourf(x,y,data,20)
@@ -43,13 +60,9 @@
4360
plt.title('rotated pole data in native map using real sphere corner lat/lons' )
4461

4562
plt.figure()
46-
m = Basemap(projection='rotpole',lon_0=rotpole.grid_north_pole_longitude-180.,\
47-
o_lon_p=rotpole.north_pole_grid_longitude,\
48-
o_lat_p=rotpole.grid_north_pole_latitude,\
49-
llcrnry = rlats[0,0], urcrnry = rlats[-1,-1],\
50-
llcrnrx = rlons[0,0], urcrnrx = rlons[-1,-1],resolution='c')
51-
print m.llcrnrx,m.llcrnry
52-
print m.urcrnrx,m.urcrnry
63+
m= Basemap(projection='rotpole',lon_0=lon_0,o_lon_p=o_lon_p,o_lat_p=o_lat_p,\
64+
llcrnry = rlats[0,0], urcrnry = rlats[-1,-1],\
65+
llcrnrx = rlons[0,0], urcrnrx = rlons[-1,-1],resolution='c')
5366
x,y = m(lons,lats)
5467
m.drawcoastlines()
5568
m.contourf(x,y,data,20)

0 commit comments

Comments
 (0)