-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_utils.py
160 lines (125 loc) · 4.46 KB
/
plot_utils.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
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import warnings
# resolution: use 'h' for high or 'f' for full (much slower render time)
BASEMAP_RESOLUTION = 'l'
def plot_tb(TB, lat, lon, lat_bounds, lon_bounds, colorAxisMin, colorAxisMax):
''' Use Basemap to visualize brightness temperature (TB) products on a map.'''
# the basemap bounding
m = Basemap(projection = 'merc',
resolution = BASEMAP_RESOLUTION,
lat_ts = 20,
llcrnrlat = lat_bounds[0],
urcrnrlat = lat_bounds[1],
llcrnrlon = lon_bounds[0],
urcrnrlon = lon_bounds[1])
# set min/max values of the colorbar
with warnings.catch_warnings():
warnings.simplefilter("ignore")
im1 = m.pcolor(lon[:], lat[:] ,TB[:],
shading = 'nearest',
cmap = 'turbo',
latlon = True,
vmin = colorAxisMin,
vmax = colorAxisMax)
# draw the coast lines
m.drawcoastlines()
# set parallels and meridians
dparal = 2 #separation in deg between drawn parallels
parallels = np.arange(lat_bounds[0], lat_bounds[1], dparal)
dmerid = 2 #separation in deg between drawn meridians
meridians = np.arange(lon_bounds[0], lon_bounds[1], dmerid)
m.drawparallels(parallels, labels=[1,0,0,0], fontsize=15)
m.drawmeridians(meridians, labels=[0,0,0,1], fontsize=15)
# add colorbar.
cbar = m.colorbar(location='right', pad="5%")
cbar.set_label('Kelvin (K)') # temperature in Kelvin
def plot_tb_all(TBs, lat, lon, lat_bounds, lon_bounds, colorAxisMin, colorAxisMax, show=True, filepath=None):
''' Loop through TB data for each channel and plot the data on a map.'''
# create a figure to draw the data into a map plot
fig = plt.figure(figsize=(24, 10))
# draw the plots as subplots
subplot_index = 1
for TB in TBs:
# plot location
ax = fig.add_subplot(2, 3, subplot_index)
# check that TB data is given
if TB:
# draw the plot for the TB at the given channel
plot_tb(TB[1], lat, lon, lat_bounds, lon_bounds, colorAxisMin, colorAxisMax)
# plot title
plt.title(TB[0])
else:
# draw empty plot if no TB data for the channel
plt.axis('off')
# increment index for the placement of the next subplot
subplot_index += 1
# write the plots into an image file
if filepath:
plt.savefig(
f'{filepath}',
bbox_inches='tight',
dpi = 300)
# show the plots!
if show: plt.show()
def plot_rr(RR, lat, lon, lat_bounds, lon_bounds, colorAxisMin, colorAxisMax, title="Surface Precipitation", show=True, filepath=None):
''' Use Basemap to visualize rainfall rates (surface precipitation) products on a map.'''
fig = plt.figure()
ax = fig.add_subplot(111)
# the basemap bounding
# resolution: use 'h' for high or 'f' for full (much slower render time)
m = Basemap(projection = 'merc',
resolution = BASEMAP_RESOLUTION,
lat_ts = 20,
llcrnrlat = lat_bounds[0],
urcrnrlat = lat_bounds[1],
llcrnrlon = lon_bounds[0],
urcrnrlon = lon_bounds[1])
# set min/max values of the colorbar
with warnings.catch_warnings():
warnings.simplefilter("ignore")
im1 = m.pcolor(lon[:], lat[:], RR[:],
shading = 'nearest',
cmap = 'turbo',
latlon = True,
vmin = colorAxisMin,
vmax = colorAxisMax)
# draw coast lines
m.drawcoastlines()
# set parallels and meridians
dparal = 2 #separation in deg between drawn parallels
parallels = np.arange(lat_bounds[0], lat_bounds[1], dparal)
dmerid = 2 #separation in deg between drawn meridians
meridians = np.arange(lon_bounds[0], lon_bounds[1], dmerid)
m.drawparallels(parallels, labels=[1,0,0,0], fontsize=15)
m.drawmeridians(meridians, labels=[0,0,0,1], fontsize=15)
# add colorbar.
cbar = m.colorbar(location='right', pad="5%")
cbar.set_label('Rainfall Rate [mm/h]')
# add title
plt.title(title, fontsize=12)
# write the plots into an image file
if filepath:
plt.savefig(
f'{filepath}',
bbox_inches='tight',
dpi = 300)
# show the plots!
if show: plt.show()
def plot_learning_curves(history, show=True, filepath=None):
''' Plot the training's learning curve. '''
plt.title('Learning Curves')
plt.xlabel('Epoch')
plt.ylabel('MSE loss')
plt.plot((history.history['loss']), label='train')
plt.plot((history.history['val_loss']), label='test')
plt.legend()
# write the plots into an image file
if filepath:
plt.savefig(
f'{filepath}',
bbox_inches='tight',
dpi = 300)
# show the plots!
if show: plt.show()