-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdifference_map.py
310 lines (273 loc) · 11.2 KB
/
difference_map.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# load required python packages
import argparse
import sys
from pathlib import Path
import cmcrameri.cm as cmc
import iconarray as iconvis # import self-written modules from iconarray
import matplotlib.pyplot as plt
import numpy as np
import psyplot.project as psy
import xarray as xr
from matplotlib.lines import Line2D
def is_size_one_dim(dim, data):
return data.sizes[dim] == 1
if __name__ == "__main__":
####################
# A) Parsing arguments
####################
parser = argparse.ArgumentParser()
parser.add_argument(
"--config", "-c", dest="config_path", help="path to config file"
)
parser.add_argument(
"--infile1", "-i1", dest="input_file1", help="path to input file 1", default=""
)
parser.add_argument(
"--infile2", "-i2", dest="input_file2", help="path to input file 2", default=""
)
parser.add_argument(
"--outdir", "-d", dest="output_dir", help="output directory", default=Path.cwd()
)
parser.add_argument(
"--outfile",
"-o",
dest="output_file",
help="name of output file",
default="difference_map_output.png",
)
parser.add_argument("-co", action="store_true", help="get config options")
args = parser.parse_args()
#####################
# B) Read config file or show available options
#####################
# Show options for config file
if args.co:
print(
"var, name (req): name of the variable as in the nc file\n"
+ "var, zname (req if the height dimension has a name other than height): Default: height\n"
+ "var, varlim (opt): lower and upper limit of color scale\n"
+ "var, grid_file (req if file is missing grid-information): path to grid file\n"
+ "var, height (opt): index for height dimension (default 0 = ground level)\n"
+ "map, lonmin/lonmax/latmin/latmax (opt): values for map extension\n"
+ "map, projection (opt): projection to draw on (e.g., robin)\n"
+ "map, add_grid (opt): set false to remove grid with lat and lon labels\n"
+ "map, title (opt): title of plot\n"
+ "map, cmap (opt): name of colorbar\n"
+ "map, diff (opt): relative difference with input diff=rel, else absolute difference\n"
+ "map, sig (opt): marks significant (sig=1) or insignificant (sig=2) data points\n"
+ "map, sig_leg (opt): add legend for significant markers\n"
+ "map, leg_loc (opt): location of legend for significant markers\n"
+ "map, alpha (opt): significance level (default 0.05)\n"
+ "map, col (opt): color of markers for sig/insig data points\n"
+ "map, marker (opt): marker for sig/insig data points\n"
+ "map, markersize (opt): marker size of markers for sig/insig data points\n"
+ "map, clabel (opt): label of colorbar\n"
+ "coord, name (opt): add markers at certain locations (several inputs possible)\n"
+ "coord, lon/lat (opt): lon and lat of the locations\n"
+ "coord, marker (opt): marker specifications for all locations\n"
+ "coord, marker_size (opt): marker sizes for all locations\n"
+ "coord, col (opt): colors of all markers for all locations"
)
sys.exit()
# read config file
var, map_c, coord, _ = iconvis.read_config(args.config_path)
#############
# C) Load data
#############
# Check if input files exists
input_file1 = Path(args.input_file1)
if not input_file1.is_file():
sys.exit(args.input_file1 + " is not a valid file name")
input_file2 = Path(args.input_file2)
if not input_file2.is_file():
sys.exit(args.input_file2 + " is not a valid file name")
# load data
if iconvis.check_grid_information(input_file1) and iconvis.check_grid_information(
input_file2
):
data1 = psy.open_dataset(input_file1)
data2 = psy.open_dataset(input_file2)
elif iconvis.check_grid_information(input_file1) or iconvis.check_grid_information(
input_file2
):
sys.exit("The provided files have a different structure and are not compatible")
elif "grid_file" in var.keys():
data1 = iconvis.combine_grid_information(input_file1, var["grid_file"])
data2 = iconvis.combine_grid_information(input_file2, var["grid_file"])
else:
sys.exit(
"The provided files are missing the grid information. Please provide a grid file in the config."
)
# Check if variable in both file have the same dimensions
if data1[var["name"]].dims != data2[var["name"]].dims:
sys.exit(
"The variable "
+ var["name"]
+ " has different dimensions in the provided files"
)
# variable and related things
values = {"data1": data1[var["name"]], "data2": data2[var["name"]]}
# Check if variable has height as dimension and if the length of the dim is >1
if (
var["zname"] in data1[var["name"]].dims
and data1[var["name"]].sizes[var["zname"]] > 1
):
values["data1"] = values["data1"].isel({var["zname"]: var["height"]})
dims_to_drop = [
dim
for dim in values["data1"].dims
if is_size_one_dim(dim, values["data1"]) and dim != "time"
]
values["data1"] = (values["data1"].squeeze(dim=dims_to_drop, drop=True)).values
values["data2"] = values["data2"].isel({var["zname"]: var["height"]})
dims_to_drop = [
dim
for dim in values["data2"].dims
if is_size_one_dim(dim, values["data2"]) and dim != "time"
]
values["data2"] = (values["data2"].squeeze(dim=dims_to_drop, drop=True)).values
else:
print(
"Warning: The variable "
+ var["name"]
+ " doesn't have the height dimension "
+ var["zname"]
+ ". Ignore this warning for 2D variables."
)
values["data1"] = values["data1"].values
values["data2"] = values["data2"].values
# Calculate mean, difference and p-values
var_mean, _, var_diff, pvals = iconvis.get_stats(values["data1"], values["data2"])
if map_c["diff"] == "rel":
nonan = np.argwhere((~np.isnan(var_diff)) & (var_mean != 0) & (var_diff != 0))
var_diff[nonan] = 100 * (var_diff[nonan] / var_mean[nonan])
# Create new dataset, which contains the mean var_diff values
data_com = xr.Dataset(
data_vars=dict(var_diff=(["ncells"], var_diff)),
coords=dict(
clon=(["ncells"], data1.clon.values[:]),
clon_bnds=(["ncells", "vertices"], data1.clon_bnds.values[:]),
clat=(["ncells"], data1.clat.values[:]),
clat_bnds=(["ncells", "vertices"], data1.clat_bnds.values[:]),
),
)
data_com["clon"].attrs["bounds"] = "clon_bnds"
data_com["clat"].attrs["bounds"] = "clat_bnds"
data_com["clon"].attrs["units"] = data1["clon"].attrs["units"]
data_com["clat"].attrs["units"] = data1["clat"].attrs["units"]
data_com.var_diff.encoding["coordinates"] = "clat clon"
#############
# D) Plot data
#############
# Get map extension
if "lonmin" not in map_c.keys():
map_c["lonmin"] = min(np.rad2deg(data_com.clon.values[:]))
if "lonmax" not in map_c.keys():
map_c["lonmax"] = max(np.rad2deg(data_com.clon.values[:]))
if "latmin" not in map_c.keys():
map_c["latmin"] = min(np.rad2deg(data_com.clat.values[:]))
if "latmax" not in map_c.keys():
map_c["latmax"] = max(np.rad2deg(data_com.clat.values[:]))
pp = data_com.psy.plot.mapplot(name="var_diff")
pp.update(
map_extent=[map_c["lonmin"], map_c["lonmax"], map_c["latmin"], map_c["latmax"]]
)
if "projection" in map_c.keys():
pp.update(projection=map_c["projection"])
if "varlim" in var.keys():
pp.update(
bounds={
"method": "minmax",
"vmin": var["varlim"][0],
"vmax": var["varlim"][1],
}
)
if "add_grid" in map_c.keys():
pp.update(xgrid=map_c["add_grid"], ygrid=map_c["add_grid"])
if "title" in map_c.keys():
pp.update(title=map_c["title"])
if "cmap" in map_c.keys():
pp.update(cmap=map_c["cmap"])
else:
pp.update(cmap=cmc.vik)
if "clabel" in map_c.keys():
pp.update(clabel=map_c["clabel"])
pp.update(borders=True, lakes=True, rivers=False)
fig = plt.gcf()
if coord:
# go to matplotlib level
llon = map_c["lonmax"] - map_c["lonmin"]
llat = map_c["latmax"] - map_c["latmin"]
for i in range(0, len(coord["lon"])):
pos_lon, pos_lat = iconvis.add_coordinates(
coord["lon"][i],
coord["lat"][i],
map_c["lonmin"],
map_c["lonmax"],
map_c["latmin"],
map_c["latmax"],
)
fig.axes[0].plot(
pos_lon,
pos_lat,
coord["col"][i],
marker=coord["marker"][i],
markersize=coord["marker_size"][i],
transform=fig.axes[0].transAxes,
)
if "name" in coord.keys():
fig.axes[0].text(
pos_lon + llon * 0.003,
pos_lat + llat * 0.003,
coord["name"][i],
transform=fig.axes[0].transAxes,
)
# Add dots for significant/insignificant datapoints
if map_c["sig"]:
pfdr = iconvis.wilks(pvals, map_c["alpha"])
if map_c["sig"] == 1:
sig = np.argwhere(pvals < pfdr)
sig_leg = "Significant differences"
elif map_c["sig"] == 2:
sig = np.argwhere((np.isnan(pvals)) | (pvals > pfdr))
sig_leg = "Insignificant differences"
else:
sys.exit("Invalid number for map,sig")
for i in sig:
pos_lon, pos_lat = iconvis.add_coordinates(
np.rad2deg(data_com.clon.values[i]),
np.rad2deg(data_com.clat.values[i]),
map_c["lonmin"],
map_c["lonmax"],
map_c["latmin"],
map_c["latmax"],
)
fig.axes[0].plot(
pos_lon,
pos_lat,
map_c["col"],
marker=map_c["marker"],
markersize=map_c["markersize"],
transform=fig.axes[0].transAxes,
)
if map_c["sig_leg"]:
leg_el = [
Line2D(
[0],
[0],
marker=map_c["marker"],
color="None",
label=sig_leg,
markerfacecolor=map_c["col"],
markersize=map_c["markersize"],
)
]
fig.axes[0].legend(handles=leg_el, loc=map_c["leg_loc"])
#############
# E) Save figure
#############
output_dir = Path(args.output_dir)
output_file = Path(output_dir, args.output_file)
output_dir.mkdir(parents=True, exist_ok=True)
print("The output is saved as " + str(output_file))
plt.savefig(output_file)