forked from akvas/gravity_plots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GOCO06s_panels.py
243 lines (182 loc) · 7.58 KB
/
GOCO06s_panels.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
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import cartopy.crs as ccrs
from cartopy.img_transform import warp_array
from PIL import Image
import io
def alpha_scaling(x, c, s):
"""
Function to compute alpha values based on data magnitude.
"""
return 1 - (1 - np.exp(-c * s) + np.exp((x - c) * s)) ** -1
class DataLayer:
"""
Convenience class for plotting projected lon/lat data.
"""
def __init__(self, data):
self.__data = data
self.__vmin = None
self.__vmax = None
def draw(self, ax, vmin, vmax, target_projection, target_resolution=(1000, 1000), c=0.5, s=1, cmap='RdBu'):
"""
Draws the projected data set in a given Axes instance.
Parameters
----------
ax : matplotlib.axes.Axes
axes instance
vmin : float
lower data limit
vmax : float
upper data limit
target_projection : cartopy.crs.Projection
projection applied to the lon/lat data
target_resolution : tuple
resolution of the output grid
c : float
offset parameter for alpha computation
s : float
slope parameter for alpha computation
cmap : str
matplotlib colormap
"""
if isinstance(cmap, str):
cmap = plt.get_cmap(cmap)
data, extent = warp_array(self.__data, target_projection, ccrs.PlateCarree(), target_res=target_resolution)
values_normalized = (data - vmin) / (vmax - vmin)
magnitude = np.abs(data / vmax)
magnitude[magnitude > 1] = 1
magnitude[data.mask] = 0
alpha = Image.fromarray(np.uint8(alpha_scaling(magnitude, c, s) * 255))
bitmap = Image.fromarray(np.uint8(cmap(values_normalized) * 255))
bitmap.putalpha(alpha)
return ax.imshow(bitmap, extent=extent)
class BlueMarble:
"""
Convenience class for handling NASA Blue Marble images in map projections.
"""
def __init__(self, file_name):
img = Image.open(file_name)
self.__data = np.array(img)
def draw(self, ax, target_projection, target_resolution=(1000, 1000)):
"""
Draws the projected image in a given Axes instance.
Parameters
----------
ax : matplotlib.axes.Axes
axes instance
target_projection : cartopy.crs.Projection
projection applied to the lon/lat data
target_resolution : tuple
resolution of the output grid
"""
data, extent = warp_array(self.__data, target_projection, ccrs.PlateCarree(), target_res=target_resolution)
img = Image.fromarray(data)
alpha = Image.fromarray(np.uint8(~data.mask[:, :, 0]*255))
img.putalpha(alpha)
return ax.imshow(img, extent=extent)
class OrthographicAtmosphere:
"""
Simulates atmospheric glow for global orthographic projections.
"""
def __init__(self, atmosphere_height, color, radius_earth=6378136.3):
self.radius = radius_earth + atmosphere_height
self.radius_earth = radius_earth
self.color = color
def draw(self, ax):
"""
Draws a circular atmosphere in a given Axes instance.
Parameters
----------
ax : matplotlib.axes.Axes
axes instance
"""
patch_count = 100
radii = np.linspace(self.radius, self.radius_earth, patch_count)
alpha_values = np.linspace(0.5/patch_count, 1/patch_count, radii.size)
circles = []
for k in range(patch_count):
circle = mpl.patches.Circle((0, 0), radius=radii[k], alpha=alpha_values[k], fill=True,
fc=self.color, edgecolor=None, linewidth=0, zorder=-10000+k)
ax.add_patch(circle)
circles.append(circle)
return circles
class PanelMetaData:
"""
Container for panel metadata.
"""
def __init__(self, data_set, vmin, vmax, title, label, cmap, lon0, lat0):
self.data_set = data_set
self.vmin = vmin
self.vmax = vmax
self.title = title
self.label = label
self.cmap = cmap
self.projection1 = ccrs.Orthographic(central_latitude=-lat0, central_longitude=lon0)
self.projection2 = ccrs.Orthographic(central_latitude=lat0, central_longitude=lon0 if lon0 == 0 else lon0+180)
trend = np.load('data/GOCO06s_trend_density.npy')
amplitude = np.load('data/GOCO06s_annualAmplitude_water_height.npy')
anomalies = np.load('data/GOCO06s_static_anomalies.npy')
blue_marble = BlueMarble('data/bm_lowres.png')
atmosphere = OrthographicAtmosphere(750e3, 'w')
panels = [PanelMetaData(trend, -300, 300, 'a) long term trend', 'mass change [kg m$^{-2}$ year$^{-1}$]', 'RdBu', 0, 90),
PanelMetaData(amplitude, 0, 35, 'b) annual amplitude', 'water height [cm]', 'Blues', -70, -20),
PanelMetaData(anomalies, -100, 100, 'c) static gravity field', 'gravity anomalies [mgal]', 'PiYG', 135, 35)]
text_color = 'w'
font_size = 10
plt.rcParams.update({'font.family': 'arial', 'font.weight': 'bold',
'font.size': font_size, 'text.color': text_color,
'axes.labelcolor': text_color, 'xtick.color': text_color,
'ytick.color': text_color})
target_resolution = (2000, 2000)
panel_bitmaps = []
for k, panel in enumerate(panels):
fig = plt.figure(figsize=(5.25, 5.25))
ax1 = plt.subplot(1, 1, 1)
ax1.set_title(panel.title)
atmosphere.draw(ax1)
blue_marble.draw(ax1, panel.projection1, target_resolution=target_resolution)
data_layer = DataLayer(panel.data_set)
data_layer.draw(ax1, panel.vmin, panel.vmax, panel.projection1, c=0.4, s=10,
cmap=panel.cmap, target_resolution=target_resolution)
ax1.set_xlim((-atmosphere.radius, atmosphere.radius))
ax1.set_ylim((-atmosphere.radius, atmosphere.radius))
ax1.set_axis_off()
ax1_bbox = ax1.get_position()
p1 = ax1_bbox.get_points()[0]
p2 = ax1_bbox.get_points()[1]
ax1_width = p2[0] - p1[0]
ax1_height = p2[1] - p1[1]
colorbar_width = 0.75
ax2 = fig.add_axes([p1[0]+ax1_width*(1-colorbar_width)*0.5, p1[1]-0.01, ax1_width*colorbar_width, 0.02])
norm = mpl.colors.Normalize(vmin=panel.vmin, vmax=panel.vmax)
cb1 = mpl.colorbar.ColorbarBase(ax2, cmap=plt.get_cmap(panel.cmap), norm=norm, orientation='horizontal',
extend='max' if panel.vmin == 0 else 'both', extendfrac=0.1)
cb1.set_label(panel.label)
ax3 = fig.add_axes([p1[0], p1[1]-ax1_height-0.075, ax1_width, ax1_height])
atmosphere.draw(ax3)
blue_marble.draw(ax3, panel.projection2, target_resolution=target_resolution)
data_layer.draw(ax3, panel.vmin, panel.vmax, panel.projection2, c=0.4, s=10,
cmap=panel.cmap, target_resolution=target_resolution)
ax3.set_xlim((-atmosphere.radius, atmosphere.radius))
ax3.set_ylim((-atmosphere.radius, atmosphere.radius))
ax3.set_axis_off()
inmemory_file = io.BytesIO()
plt.savefig(inmemory_file, dpi=300, bbox_inches='tight', facecolor='k')
inmemory_file.seek(0)
panel_bitmaps.append(Image.open(inmemory_file))
buffer = 50
total_width = 0
total_height = 0
for image in panel_bitmaps:
width, height = image.size
total_width += width
total_height = max(total_height, height)
total_width += (len(panel_bitmaps)-1)*buffer
canvas = Image.new('RGB', (total_width, total_height), (0, 0, 0))
offsetx = 0
for image in panel_bitmaps:
width, height = image.size
canvas.paste(image, (offsetx, 0))
offsetx += width + buffer
canvas.save('GOCO06s_mosaic.png')