|
| 1 | +from __future__ import print_function |
1 | 2 | from netCDF4 import Dataset
|
2 | 3 | from mpl_toolkits.basemap import Basemap
|
3 | 4 | import numpy as np
|
|
23 | 24 | plt.title('rotated pole data in polar stere map')
|
24 | 25 |
|
25 | 26 | plt.figure()
|
| 27 | + |
26 | 28 | # o_lon_p, o_lat_p: true lat/lon of pole in rotated coordinate system
|
| 29 | + |
27 | 30 | # mapping to CF metadata convention:
|
28 | 31 | # 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). |
30 | 33 | # grid_north_pole_latitude = o_lat_p
|
31 | 34 | # 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') |
37 | 54 | x,y = m(lons,lats)
|
38 | 55 | m.drawcoastlines()
|
39 | 56 | m.contourf(x,y,data,20)
|
|
43 | 60 | plt.title('rotated pole data in native map using real sphere corner lat/lons' )
|
44 | 61 |
|
45 | 62 | 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') |
53 | 66 | x,y = m(lons,lats)
|
54 | 67 | m.drawcoastlines()
|
55 | 68 | m.contourf(x,y,data,20)
|
|
0 commit comments