forked from NCAR/wrf-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojtest.py
More file actions
222 lines (175 loc) · 6.76 KB
/
projtest.py
File metadata and controls
222 lines (175 loc) · 6.76 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
import unittest as ut
from os.path import join, basename
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from netCDF4 import Dataset as NetCDF
from wrf import get_proj_params
from wrf.projection import getproj, RotatedLatLon, PolarStereographic
PYNGL = True
try:
import Ngl
except ImportError:
PYNGL = False
BASEMAP = True
try:
import mpl_toolkits.basemap
except ImportError:
BASEMAP = False
CARTOPY = True
try:
from cartopy import crs, feature
except ImportError:
CARTOPY = False
FILE_DIR = "/Users/ladwig/Documents/wrf_files/"
WRF_FILES = [join(FILE_DIR, "norway", "geo_em.d01.nc"),
join(FILE_DIR, "rotated_pole", "EAS_geo_em.d01.nc"),
join(FILE_DIR, "rotated_pole", "EUR_geo_em.d01.nc"),
join(FILE_DIR, "wrfout_d01_2016-02-25_18_00_00"),
join(FILE_DIR, "wrfout_d01_2008-09-29_23-30-00"),
join(FILE_DIR, "wrfout_d01_2010-06-13_21:00:00")]
def nz_proj():
lats = np.array([[-47.824014, -47.824014],
[-32.669853, -32.669853]])
lons = np.array([[163.839595, -179.693502],
[163.839595, -179.693502]])
params = {"MAP_PROJ": 6,
"CEN_LAT": -41.814869,
"CEN_LON": 179.693502,
"TRUELAT1": 0,
"TRUELAT2": 0,
"MOAD_CEN_LAT": -41.814869,
"STAND_LON": 180.0 - 179.693502,
"POLE_LAT": 48.185131,
"POLE_LON": 0.0}
return lats, lons, RotatedLatLon(lats=lats, lons=lons, **params)
def argentina_proj():
lats = np.array([[-57.144064, -57.144064],
[-21.154470, -21.154470]])
lons = np.array([[-86.893797, -37.089724],
[-86.893797, -37.089724]])
params = {"MAP_PROJ": 6,
"CEN_LAT": -39.222954,
"CEN_LON": -65.980109,
"TRUELAT1": 0,
"TRUELAT2": 0,
"MOAD_CEN_LAT": -39.222954,
"STAND_LON": 180.0 - -65.980109,
"POLE_LAT": 90 + -39.222954,
"POLE_LON": 0.0}
return lats, lons, RotatedLatLon(lats=lats, lons=lons, **params)
def south_polar_proj():
lats = np.array([[-30.0, -30.0],
[-30.0, -30.0]])
lons = np.array([[-120, 60],
[-120, 60]])
params = {"MAP_PROJ": 2,
"CEN_LAT": -90.0,
"CEN_LON": 0,
"TRUELAT1": -10.0,
"MOAD_CEN_LAT": -90.0,
"STAND_LON": 0}
return lats, lons, PolarStereographic(lats=lats, lons=lons, **params)
def north_polar_proj():
lats = np.array([[30.0, 30.0],
[30.0, 30.0]])
lons = np.array([[-45, 140],
[-45, 140]])
params = {"MAP_PROJ": 2,
"CEN_LAT": 90.0,
"CEN_LON": 10,
"TRUELAT1": 10.0,
"MOAD_CEN_LAT": 90.0,
"STAND_LON": 10}
return lats, lons, PolarStereographic(lats=lats, lons=lons, **params)
def dateline_rot_proj():
lats = np.array([[60.627974, 60.627974],
[71.717521, 71.717521]])
lons = np.array([[170.332771, -153.456292],
[170.332771, -153.456292]])
params = {"MAP_PROJ": 6,
"CEN_LAT": 66.335764,
"CEN_LON": -173.143792,
"TRUELAT1": 0,
"TRUELAT2": 0,
"MOAD_CEN_LAT": 66.335764,
"STAND_LON": 173.143792,
"POLE_LAT": 90.0 - 66.335764,
"POLE_LON": 180.0}
return lats, lons, RotatedLatLon(lats=lats, lons=lons, **params)
class WRFProjTest(ut.TestCase):
longMessage = True
def make_test(wrf_file=None, fixed_case=None):
if wrf_file is not None:
ncfile = NetCDF(wrf_file)
lats, lons, proj_params = get_proj_params(ncfile)
proj = getproj(lats=lats, lons=lons, **proj_params)
name_suffix = basename(wrf_file)
elif fixed_case is not None:
name_suffix = fixed_case
if fixed_case == "south_rot":
lats, lons, proj = nz_proj()
elif fixed_case == "arg_rot":
lats, lons, proj = argentina_proj()
elif fixed_case == "south_polar":
lats, lons, proj = south_polar_proj()
elif fixed_case == "north_polar":
lats, lons, proj = north_polar_proj()
elif fixed_case == "dateline_rot":
lats, lons, proj = dateline_rot_proj()
print("wrf proj4: {}".format(proj.proj4()))
if PYNGL:
# PyNGL plotting
wks_type = bytes("png")
wks = Ngl.open_wks(wks_type, bytes("pyngl_{}".format(name_suffix)))
mpres = proj.pyngl()
map = Ngl.map(wks, mpres)
Ngl.delete_wks(wks)
if BASEMAP:
# Basemap plotting
fig = plt.figure(figsize=(10, 10))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
# Define and plot the meridians and parallels
min_lat = np.amin(lats)
max_lat = np.amax(lats)
min_lon = np.amin(lons)
max_lon = np.amax(lons)
parallels = np.arange(np.floor(min_lat), np.ceil(max_lat),
(max_lat - min_lat)/5.0)
meridians = np.arange(np.floor(min_lon), np.ceil(max_lon),
(max_lon - min_lon)/5.0)
bm = proj.basemap()
bm.drawcoastlines(linewidth=.5)
# bm.drawparallels(parallels,labels=[1,1,1,1],fontsize=10)
# bm.drawmeridians(meridians,labels=[1,1,1,1],fontsize=10)
print("basemap proj4: {}".format(bm.proj4string))
plt.savefig("basemap_{}.png".format(name_suffix))
plt.close(fig)
if CARTOPY:
# Cartopy plotting
fig = plt.figure(figsize=(10, 10))
ax = plt.axes([0.1, 0.1, 0.8, 0.8], projection=proj.cartopy())
print("cartopy proj4: {}".format(proj.cartopy().proj4_params))
ax.coastlines('50m', linewidth=0.8)
# print proj.x_extents()
# print proj.y_extents()
ax.set_xlim(proj.cartopy_xlim())
ax.set_ylim(proj.cartopy_ylim())
ax.gridlines()
plt.savefig("cartopy_{}.png".format(name_suffix))
plt.close(fig)
if __name__ == "__main__":
for wrf_file in WRF_FILES:
test_func = make_test(wrf_file=wrf_file)
setattr(WRFProjTest, "test_proj", test_func)
test_func2 = make_test(fixed_case="south_rot")
setattr(WRFProjTest, "test_south_rot", test_func2)
test_func3 = make_test(fixed_case="arg_rot")
setattr(WRFProjTest, "test_arg_rot", test_func3)
test_func4 = make_test(fixed_case="south_polar")
setattr(WRFProjTest, "test_south_polar", test_func4)
test_func5 = make_test(fixed_case="north_polar")
setattr(WRFProjTest, "test_north_polar", test_func5)
test_func6 = make_test(fixed_case="dateline_rot")
setattr(WRFProjTest, "test_dateline_rot", test_func6)
ut.main()