/
azeq.py
281 lines (246 loc) · 9.31 KB
/
azeq.py
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
'''
Created on Dec 18, 2012
@author: itpp
'''
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import shapely.geometry as sgeom
import iris
import iris.plot as iplt
import iris.quickplot as qplt
import cartopy.crs as ccrs
import iris.tests.stock as istk
# assumed radius of spherical earth in PROJ4 (pinched from code in pj_ellps.c)
EARTH_SPHERE_RADIUS = 6370997.0
EARTH_SPHERE_CIRCUMFERENCE = EARTH_SPHERE_RADIUS * 2.0 * np.pi
class AzEq(ccrs.Projection):
# Attempt at defining Azimuthal Equidistant projection for Cartopy.
# -- mostly pinched from Gnomonic type etc.
def __init__(self, central_latitude=90.0, central_longitude=0.0):
proj4_params = {'proj': 'aeqd',
'lat_0': central_latitude,
'lon_0': central_longitude,
# NOTE: spherical assumption should work better for
# this projection.
'ellps':'sphere'
}
super(AzEq, self).__init__(proj4_params)
# Map limit is one-half earth circumference.
# This is why 'ellps:sphere' is a good idea.
self._max = 0.5*EARTH_SPHERE_CIRCUMFERENCE
@property
def boundary(self):
# Copied -- don't really understand.
return sgeom.Point(0, 0).buffer(self._max).exterior
@property
def threshold(self):
# Copied -- don't really understand.
# Seems to control the precision of drawn lines.
# See in crs.py that this is typically -
# * 0.5 for degree-based projections
# * ~1e5-1e7 for metre-based projections (=100-10000km?)
return 1e3
@property
def x_limits(self):
# Copied -- don't really understand. A square coordinate box?
return (-self._max, self._max)
@property
def y_limits(self):
# Copied -- don't really understand. A square coordinate box?
return (-self._max, self._max)
_proj_cyl = ccrs.PlateCarree()
def apply_dict_defaults(kwargs, defaults_dict):
for (argname, value) in defaults_dict.iteritems():
if not argname in kwargs:
kwargs[argname] = value
# Properties of the PufferSphere
PUFFERSPHERE_FULL_WIDTH = 1400
PUFFERSPHERE_FULL_HEIGHT = 1050
PUFFERSPHERE_TOTAL_PIXELS = (PUFFERSPHERE_FULL_WIDTH, PUFFERSPHERE_FULL_HEIGHT)
PUFFERSPHERE_USED_WIDTH = 1050
PUFFERSPHERE_USED_HEIGHT = 1050
# Set "dpi".
# In principle arbitrary, as results are rescaled from Figure 'inch' sizes.
# In practice controls *default linewidths* (as in pixels).
USE_DPI = 150
# Size of main figure.
_figure_inches = [x*1.0/USE_DPI for x in PUFFERSPHERE_TOTAL_PIXELS]
# Normalised corners of the useful area within the main figure
# (left,bottom,width,height).
_margin_pixels = (PUFFERSPHERE_FULL_WIDTH - PUFFERSPHERE_USED_WIDTH) / 2
_width_norm_scale = 1.0 / PUFFERSPHERE_FULL_WIDTH
_axes_normalised_rect = [
_margin_pixels * _width_norm_scale,
0.0,
PUFFERSPHERE_USED_WIDTH * _width_norm_scale,
1.0
]
def make_puffersphere_figure(**kwargs):
"""
Init a pyplot.Figure for full-globe spherical projection.
From : http://wiki.openstreetmap.org/wiki/Pufferfish_Display ...
"A pre-warped image consists of a 1050x1050 pixel square image with a 75 pixal black band on either side. With the north pole at in the centre."
"""
# Create a figure with useful defaults
apply_dict_defaults(
kwargs,
{
'figsize': _figure_inches,
'dpi': USE_DPI,
'facecolor': 'black',
'edgecolor': None,
})
figure = plt.figure(**kwargs)
return figure
def make_puffersphere_axes(projection_kwargs={}, **kwargs):
"""
Create a pyplot.Axes for full-globe spherical projection.
Uses Azimuthal Equidistant projection, and
"""
# Create a central global-map axes to plot on.
apply_dict_defaults(
kwargs,
{
# 'figure': figure,
# 'rect': _axes_normalised_rect,
# NOTE: for plt.axes() : 'figure' is implicit, 'rect' is an arg, not kwarg.
'frameon': False,
'projection': AzEq(**projection_kwargs),
'axisbg': 'black',
})
axes = plt.axes(_axes_normalised_rect, **kwargs)
return axes
def save_figure_for_puffersphere(figure, filename, **savefig_kwargs):
"""
Plot the given Figure in a suitable form for the puffersphere.
"""
# # A list of modified defaults
# apply_dict_defaults(
# savefig_kwargs,
# {
# 'format': 'png',
# })
# For correct results, must re-assert dpi and facecolor?
figure.savefig(
filename,
dpi=USE_DPI,
facecolor='black'
)
def draw_gridlines(n_meridians=12, n_parallels=8, lon_color='#180000', lat_color='#000018'):
line_artists = []
for longitude in np.linspace(-180.0, +180.0, n_meridians, endpoint=False):
line_artists += [
plt.plot([longitude, longitude], [-90.0, 90.0],
color=lon_color, transform=_proj_cyl)]
for latitude in np.linspace(-90.0, +90.0, n_parallels, endpoint=False):
line_artists += [
plt.plot([-180.0, 180.0], [latitude, latitude],
color=lat_color, transform=_proj_cyl)]
return line_artists
def simpletest(do_savefig=True, do_showfig=True, savefig_file='./puffer.png'):
figure = make_puffersphere_figure()
axes = make_puffersphere_axes()
axes.stock_img()
data = istk.global_pp()
axes.coastlines()
qplt.contour(data)
draw_gridlines()
#axes.coastlines()
if do_savefig:
save_figure_for_puffersphere(figure=plt.gcf(), filename=savefig_file)
if do_showfig:
plt.show()
def rotating_sequence(show_frames=True, save_frames=True,
save_ani=False, show_ani=False,
ani_path='./puffer.mp4',
frames_basename='./puffer_frame_',
airtemp_cubes=None,
precip_cubes=None,
n_steps_round=20,
tilt_angle=21.7
):
plt.interactive(show_frames)
figure = make_puffersphere_figure()
# per_image_artists = []
for (i_plt, lon_rotate) in enumerate(np.linspace(0.0, 360.0, n_steps_round, endpoint=False)):
print 'rotate=', lon_rotate,' ...'
axes = make_puffersphere_axes(
projection_kwargs={'central_longitude': lon_rotate, 'central_latitude': tilt_angle})
image = axes.stock_img()
coast = axes.coastlines()
# data = istk.global_pp()
data = airtemp_cubes[i_plt]
transparent_blue = (0.0, 0.0, 1.0, 0.25)
transparent_red = (1.0, 0.0, 0.0, 0.25)
cold_thresh = -10.0
cold_fill = iplt.contourf(
data,
levels=[cold_thresh, cold_thresh],
colors=[transparent_blue],
extend='min')
cold_contour = iplt.contour(
data,
levels=[cold_thresh], colors=['blue'],
linestyles=['solid'])
data = precip_cubes[i_plt]
precip_thresh = 0.0001
precip_fill = iplt.contourf(
data,
levels=[precip_thresh, precip_thresh],
colors=[transparent_red],
extend='max')
precip_contour = iplt.contour(
data,
levels=[precip_thresh], colors=['red'],
linestyles=['solid'])
gridlines = draw_gridlines(n_meridians=6)
# artists = []
# artists += [coast]
# artists += [image]
# artists += cold_fill.collections
# artists += precip_fill.collections
# for gridline in gridlines:
# artists += gridline
if show_frames:
plt.draw()
if save_frames:
save_path = frames_basename+str(i_plt)+'.png'
save_figure_for_puffersphere(figure, save_path)
# per_image_artists.append(artists)
print ' ..done.'
# if save_ani:
# print 'Saving to {}...'.format(ani_path)
# ani = animation.ArtistAnimation(
# figure, per_image_artists,
# interval=150, repeat=True, repeat_delay=500
# )
# ani.save(ani_path, writer='ffmpeg')
#
# if show_ani:
# print 'Showing...'
# ani = animation.ArtistAnimation(
# figure, per_image_artists,
# interval=250, repeat=True, repeat_delay=5000,
## blit=True
# )
# plt.show(block=True)
if __name__ == '__main__':
# simpletest()
# get some basic data
airtemp_data, precip_data = iris.load_cubes('/data/local/dataZoo/PP/decadal/*.pp', ['air_temperature', 'precipitation_flux'])
# create a rolling map from these
n_frames = 12
# i_images = [int(x) for x in np.linspace(0, airtemp_raw.shape[0], n_frames, endpoint=False)]
# airtemp_data = airtemp_raw[i_images]
airtemp_data = airtemp_data[0:n_frames+1]
precip_data = precip_data[0:n_frames+1]
units_degC = iris.unit.Unit('degC')
airtemp_data.data = airtemp_data.units.convert(airtemp_data.data, units_degC)
airtemp_data.units = units_degC
rotating_sequence(airtemp_cubes=airtemp_data, precip_cubes=precip_data, n_steps_round=airtemp_data.shape[0])
#>>> for x in pf:
#... plt.clf()
#... plt.axes(projection=ccrs.PlateCarree());plt.gca().stock_img()
#... iplt.contourf(x, levels=[0.0002,0.0002], extend='max', colors=[(1.0,0,0,0.2)])
#... print raw_input()