-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathabd.py
226 lines (169 loc) · 9.34 KB
/
abd.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
#!/usr/bin/env python
#************************************************************************
#
# Plot figures and output numbers for Surface Albedo (ABD) section.
# For BAMS SotC 2016
#
#************************************************************************
# SVN Info
# $Rev:: 30 $: Revision of last commit
# $Author:: rdunn $: Author of last commit
# $Date:: 2021-06-15 10:41:02 +0100 (Tue, 15 Jun #$: Date of last commit
#************************************************************************
# START
#************************************************************************
import datetime as dt
import struct
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import utils # RJHD utilities
import settings
DATALOC = "{}/{}/data/ABD/".format(settings.ROOTLOC, settings.YEAR)
LEGEND_LOC = "lower left"
minor_tick_interval = 1
major_tick_interval = 5
minorLocator = MultipleLocator(minor_tick_interval)
majorLocator = MultipleLocator(major_tick_interval)
# http://modis-atmos.gsfc.nasa.gov/ALBEDO/
start = dt.datetime(2003, 1, 1)
# get difference in days, 10.15 day period (36 points per year)
PERIOD = 10.15
print("faking time axis - {} day period".format(PERIOD))
print("Built-in round() might cause issues in Python3")
DURATION = int(round(((dt.datetime(int(settings.YEAR), 12, 31) - start).days)/PERIOD))
dates = [start + dt.timedelta(days=i*PERIOD) for i in range(DURATION)]
times = np.ma.array([(d - start).days/365. for d in dates]) + 2003
times.mask = np.zeros(times.shape)
#************************************************************************
def read_binary(filename):
'''
Read from binary file - using "struct"
:param str filename: file to read
:returns: np.array
'''
with open(filename, "rb") as f:
fileContent = f.read()
data = struct.unpack("d" * (len(fileContent) // 8), fileContent)
return np.array(data) # read_binary
#************************************************************************
def read_binary_ts(filename):
'''
Read from binary file - using "struct"
:param str filename: file to read
:returns: np.array
'''
with open(filename, "rb") as f:
fileContent = f.read()
globe = np.array(struct.unpack("d" * (DURATION), fileContent[:DURATION*8]))
north = np.array(struct.unpack("d" * (DURATION), fileContent[DURATION*8:DURATION*8*2]))
south = np.array(struct.unpack("d" * (DURATION), fileContent[DURATION*8*2:DURATION*8*3]))
aver = struct.unpack("f" * (DURATION*3), fileContent[DURATION*8*3:])
aver = np.array(aver).reshape(3, DURATION)
globe = utils.Timeseries("Globe", times, globe)
north = utils.Timeseries("N. Hemisphere", times, north)
south = utils.Timeseries("S. Hemisphere", times, south)
globe_sm = utils.Timeseries("Globe Smoothed", times, aver[0, :])
north_sm = utils.Timeseries("N. Hemisphere Smoothed", times, aver[1, :])
south_sm = utils.Timeseries("S. Hemisphere Smoothed", times, aver[2, :])
return [globe, north, south, globe_sm, north_sm, south_sm] # read_binary_ts
#************************************************************************
def run_all_plots():
#************************************************************************
# Timeseries
if True:
IRdata = read_binary_ts(DATALOC + "TimeseriesBHRNIR_C7_poids_{}.bin".format(int(settings.YEAR)+1))
Vdata = read_binary_ts(DATALOC + "TimeseriesBHRV_C7_poids_{}.bin".format(int(settings.YEAR)+1))
fig, (ax1, ax2) = plt.subplots(2, figsize=(8, 6.5), sharex=True)
COLOURS = settings.COLOURS["land_surface"]
for dataset in Vdata:
print(dataset.name)
locs, = np.where(dataset.data != 0)
ls = "--"
lw = 1
if dataset.name.split(" ")[-1] == "Smoothed":
ls = "-"
lw = 2
ax1.plot(dataset.times[locs], dataset.data[locs], c=COLOURS[dataset.name], ls=ls, label=dataset.name, lw=lw)
ax1.axhline(0, c='0.5', ls='--')
utils.thicken_panel_border(ax1)
for dataset in IRdata:
print(dataset.name)
locs, = np.where(dataset.data != 0) # remove zero bits (mainly for smoothed)
ls = "--"
lw = 1
if dataset.name.split(" ")[-1] == "Smoothed":
ls = "-"
lw = 2
ax2.plot(dataset.times[locs], dataset.data[locs], c=COLOURS[dataset.name], ls=ls, label=dataset.name, lw=lw)
ax2.legend(loc=LEGEND_LOC, ncol=2, frameon=False, prop={'size':settings.LEGEND_FONTSIZE * 0.8}, labelspacing=0.1, columnspacing=0.5)
ax2.axhline(0, c='0.5', ls='--')
utils.thicken_panel_border(ax2)
#*******************
# prettify
fig.text(0.01, 0.5, "Normalized Anomalies (%)", va='center', rotation='vertical', fontsize=settings.FONTSIZE)
plt.xlim([2002.5, int(settings.YEAR)+1.5])
ax2.set_ylim([-5.8, None])
for ax in [ax1, ax2]:
for tick in ax.yaxis.get_major_ticks():
tick.label.set_fontsize(settings.FONTSIZE)
ax.xaxis.set_minor_locator(minorLocator)
ax.xaxis.set_major_locator(majorLocator)
ax.set_yticks(ax.get_yticks()[1:-1])
ax.yaxis.set_ticks_position('left')
for tick in ax2.xaxis.get_major_ticks():
tick.label.set_fontsize(settings.FONTSIZE)
ax1.text(0.02, 0.9, "(a) Visible", transform=ax1.transAxes, fontsize=settings.LABEL_FONTSIZE)
ax2.text(0.02, 0.9, "(b) Near Infrared", transform=ax2.transAxes, fontsize=settings.LABEL_FONTSIZE)
fig.subplots_adjust(right=0.95, top=0.95, bottom=0.05, hspace=0.001)
plt.savefig(settings.IMAGELOC + "ABD_ts{}".format(settings.OUTFMT))
plt.close()
#************************************************************************
# Hovmullers
if True:
IRdata = read_binary(DATALOC + "HovMullerBHRNIR_C7_{}.bin".format(int(settings.YEAR)+1))
Vdata = read_binary(DATALOC + "HovMullerBHRV_C7_{}.bin".format(int(settings.YEAR)+1))
# reshape - from Readme
IRdata = IRdata.reshape(360, DURATION)
Vdata = Vdata.reshape(360, DURATION)
IRdata = np.ma.masked_where(IRdata <= -100., IRdata)# * 100 # from readme
Vdata = np.ma.masked_where(Vdata <= -100., Vdata)# * 100
lats = np.arange(-90, 90, 0.5)
# curtail times
locs, = np.where(times > int(settings.YEAR) + 1)
IRdata.mask[:, locs] = True
Vdata.mask[:, locs] = True
times.mask[locs] = True
# cant use "cmap.set_bad" for contour as ignored when contouring
bounds = [-100, -20, -15, -10, -5, -1, 1, 5, 10, 15, 20, 100]
utils.plot_hovmuller(settings.IMAGELOC + "ABD_NIR_hovmuller", times, lats, IRdata, settings.COLOURMAP_DICT["land_surface_r"], bounds, "Normalized Anomalies (%)", figtext="(b)", background="0.7")
utils.plot_hovmuller(settings.IMAGELOC + "ABD_V_hovmuller", times, lats, Vdata, settings.COLOURMAP_DICT["land_surface_r"], bounds, "Normalized Anomalies (%)", figtext="(a)", background="0.7")
#************************************************************************
# Anomalies
if True:
IRdata = read_binary(DATALOC + "Annual_BHRNIR_C7_{}.bin".format(int(settings.YEAR)+1))
Vdata = read_binary(DATALOC + "Annual_BHRV_C7_{}.bin".format(int(settings.YEAR)+1))
# reshape - from Readme
IRdata = IRdata.reshape(360, 720)
Vdata = Vdata.reshape(360, 720)
IRdata = np.ma.masked_where(IRdata <= -9999., IRdata)
Vdata = np.ma.masked_where(Vdata <= -9999., Vdata)
IRdata = np.ma.masked_where(IRdata == 0., IRdata)
Vdata = np.ma.masked_where(Vdata == 0., Vdata)
delta = 0.5
lons = np.arange(-180 + (delta/2.), 180, delta)
lats = np.arange(-90 + (delta/2.), 90, delta)
ir_cube = utils.make_iris_cube_2d(IRdata, lats, lons, "IR Albedo", "%")
v_cube = utils.make_iris_cube_2d(Vdata, lats, lons, "V Albedo", "%")
bounds = [-100, -20, -15, -10, -5, 0, 5, 10, 15, 20, 100]
utils.plot_smooth_map_iris(settings.IMAGELOC + "p2.1_ABD_V_{}".format(settings.YEAR), v_cube, settings.COLOURMAP_DICT["land_surface_r"], bounds, "Anomalies from 2003-{} (%)".format(2010), figtext="(ad) Land Surface Albedo in the Visible")
utils.plot_smooth_map_iris(settings.IMAGELOC + "ABD_V_{}".format(settings.YEAR), v_cube, settings.COLOURMAP_DICT["land_surface_r"], bounds, "Anomalies from 2003-{} (%)".format(2010))
utils.plot_smooth_map_iris(settings.IMAGELOC + "p2.1_ABD_NIR_{}".format(settings.YEAR), ir_cube, settings.COLOURMAP_DICT["land_surface_r"], bounds, "Anomalies from 2003-{} (%)".format(2010), figtext="(ae) Land Surface Albedo in the Near Infrared")
utils.plot_smooth_map_iris(settings.IMAGELOC + "ABD_NIR_{}".format(settings.YEAR), ir_cube, settings.COLOURMAP_DICT["land_surface_r"], bounds, "Anomalies from 2003-{} (%)".format(2010))
return # run_all_plots
#************************************************************************
if __name__ == "__main__":
run_all_plots()
#************************************************************************
# END
#************************************************************************