-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHMASR_postprocess.py
287 lines (232 loc) · 9.05 KB
/
HMASR_postprocess.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
import pandas as pd
import rasterio
import xarray as xr
import numpy as np
import os
import argparse
import matplotlib.pyplot as plt
import scienceplots
plt.style.use(['science', 'grid'])
def geotiff2xr(file_path):
"""
Convert a GeoTIFF file into an xarray DataArray with spatial and temporal dimensions.
Parameters
----------
file_path : str
Path to the GeoTIFF file.
Returns
-------
xarray.DataArray or None
DataArray representing the GeoTIFF data, or None if the file does not contain SWE or MASK.
"""
with rasterio.open(file_path) as src:
data = src.read()
transform = src.transform
crs = src.crs
height = src.height
width = src.width
number_of_days = data.shape[0]
x_coords = np.linspace(transform.c, transform.c + (width - 1) * transform.a, width)
y_coords = np.linspace(transform.f, transform.f + (height - 1) * transform.e, height)
if "SWE" in file_path:
da = xr.DataArray(data, dims=("day", "y", "x"),
coords={"day": range(1, number_of_days + 1), "y": y_coords, "x": x_coords}, name="SWE")
da.attrs["crs"] = crs
da.attrs["transform"] = transform
return da
elif "MASK" in file_path:
ma = xr.DataArray(data, dims=("Non_seasonal_snow", "y", "x"),
coords={"Non_seasonal_snow": range(1, number_of_days + 1), "y": y_coords, "x": x_coords},
name="Non_seasonal_snow")
ma.attrs["crs"] = crs
ma.attrs["transform"] = transform
return ma
else:
return None
def select_tif(directory, keyword1, keyword2):
"""
Select GeoTIFF files in a directory matching specific keywords.
Parameters
----------
directory : str
Path to the directory containing GeoTIFF files.
keyword1 : str
First keyword to filter files.
keyword2 : str
Second keyword to filter files.
Returns
-------
list
List of file paths matching the specified keywords.
"""
specific_tif_files = [os.path.join(directory, file) for file in os.listdir(directory)
if file.endswith('.tif') and keyword1 in file and keyword2 in file]
return specific_tif_files
def plot_mean_swe_per_year(input_dir, start_year=1999, end_year=2016, output_fig="mean_swe_per_year.png"):
"""
Create a figure with mean SWE for each year, using subplots for visualization.
Parameters
----------
input_dir : str
Path to the directory containing input GeoTIFF files.
start_year : int
Start year for the analysis.
end_year : int
End year for the analysis.
output_fig : str
Path to save the output figure.
Returns
-------
None
"""
years = range(start_year, end_year + 1)
fig, axes = plt.subplots(6, 3, figsize=(15, 20), dpi=300) # Up to 18 plots
axes = axes.flatten()
for idx, year in enumerate(years):
if idx >= len(axes):
break
mask_tif = select_tif(input_dir, str(year), "MASK")
swe_tif = select_tif(input_dir, str(year), "SWE")
if not mask_tif or not swe_tif:
print(f"Missing files for year {year}. Skipping...")
continue
mask = geotiff2xr(mask_tif[0]).mean(dim="Non_seasonal_snow")
swe = geotiff2xr(swe_tif[0]).mean(dim="day")
# Mask non-seasonal snow
masked_swe = swe.where(mask == 0)
ax = axes[idx]
masked_swe.plot.imshow(ax=ax, cmap="viridis", add_colorbar=False)
mask.plot.imshow(ax=ax, cmap="Reds", alpha=0.4, add_colorbar=False)
ax.set_title(f"Mean SWE {year}")
ax.set_xlabel("X")
ax.set_ylabel("Y")
# Hide unused subplots
for ax in axes[len(years):]:
ax.axis("off")
plt.tight_layout()
plt.savefig(output_fig)
print(f"Mean SWE plots saved to {output_fig}")
def swe_means(input_dir, start_year=1999, end_year=2016):
"""
Calculate daily mean SWE values for a range of years.
Parameters
----------
input_dir : str
Path to the directory containing GeoTIFF files.
start_year : int
Start year for the analysis.
end_year : int
End year for the analysis.
Returns
-------
pandas.DataFrame
DataFrame containing daily mean SWE values with dates as the index.
"""
swe_list = []
years = range(start_year, end_year + 1)
for year in years:
mask_tif = select_tif(input_dir, str(year), "MASK")
swe_tif = select_tif(input_dir, str(year), "SWE")
if not mask_tif or not swe_tif:
print(f"Missing files for year {year}. Skipping...")
continue
mask = geotiff2xr(mask_tif[0])
swe = geotiff2xr(swe_tif[0])
masked_swe = swe.where(mask == 0)
mean_swe = masked_swe.mean(dim=['x', 'y'])
swe_list.append(mean_swe.values.tolist())
time_series_data = []
for year_data in swe_list:
for day_value in year_data:
time_series_data.append(round(day_value[0], 4))
date_range = pd.date_range(start=str(start_year) + '-10-01', end=str(end_year + 1) + '-09-30', freq="D")
swe_df = pd.DataFrame({"Date": date_range, "SWE_Mean": time_series_data})
swe_df.set_index("Date", inplace=True)
return swe_df
def plot_annual_swe(input_dir, start_year, end_year, output_file):
"""
Generate mean daily SWE plots for a range of years.
Parameters
----------
input_dir : str
Path to the directory containing GeoTIFF files.
start_year : int
Start year for the analysis.
end_year : int
End year for the analysis.
output_file : str
Path to save the output plot.
Returns
-------
None
"""
years = range(start_year, end_year + 1)
ncols = 4
nrows = -(-len(years) // ncols)
fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(15, nrows * 5))
axes = axes.flatten()
for idx, year in enumerate(years):
mask_tif = select_tif(input_dir, str(year), "MASK")
swe_tif = select_tif(input_dir, str(year), "SWE")
if not mask_tif or not swe_tif:
print(f"Missing files for year {year}. Skipping...")
continue
mask = geotiff2xr(mask_tif[0])
swe = geotiff2xr(swe_tif[0])
masked_swe = swe.where(swe != -999)
mean_annual_swe_2d = masked_swe.mean(dim="day")
vmin = mean_annual_swe_2d.min().values
vmax = mean_annual_swe_2d.max().values
ax = axes[idx]
im = mean_annual_swe_2d.plot.imshow(ax=ax, cmap="viridis", add_colorbar=False, vmin=vmin, vmax=vmax)
ax.ticklabel_format(style="sci", axis="both", scilimits=(0, 0))
ax.set_title(f"{year}", fontsize=32)
ax.set_xlabel("")
ax.set_ylabel("")
# Adjust layout and add colorbar
fig.tight_layout()
cbar = fig.colorbar(im, ax=axes, orientation='vertical', fraction=0.0175, pad=0.02)
cbar.set_label("SWE [mm]", fontsize=30)
cbar.ax.tick_params(labelsize=20)
fig.suptitle("Mean Daily Snow Water Equivalent", fontsize=40, y=1.05)
for ax in axes[len(years):]:
ax.remove()
plt.savefig(output_file, dpi=300, bbox_inches="tight")
plt.close(fig)
print(f"Annual SWE plots saved to {output_file}")
def main():
"""
Main function to parse arguments, calculate SWE means, and optionally generate plots.
Parameters
----------
None
Returns
-------
None
"""
parser = argparse.ArgumentParser(description="Calculate SWE means from GeoTIFF files and optionally generate plots.")
parser.add_argument("--input_dir", required=True,
help="Path to the directory containing the input GeoTIFF files.")
parser.add_argument("--output_csv", required=True,
help="Path to save the output CSV file containing SWE means.")
parser.add_argument("--output_fig", required=False, default="",
help="Path to save the output figure with annual SWE plots. Leave blank to skip plot generation.")
parser.add_argument("--start_year", type=int, default=1999,
help="Start year for the analysis (default: 1999).")
parser.add_argument("--end_year", type=int, default=2016,
help="End year for the analysis (default: 2016).")
args = parser.parse_args()
print(f"Calculating SWE means for {args.start_year} to {args.end_year}...")
print(f"Input directory: {args.input_dir}")
print(f"Output CSV: {args.output_csv}")
swe_df = swe_means(args.input_dir, start_year=args.start_year, end_year=args.end_year)
swe_df.to_csv(args.output_csv)
print(f"SWE means saved to {args.output_csv}")
if args.output_fig:
print("Generating annual SWE plots...")
plot_annual_swe(args.input_dir, args.start_year, args.end_year, args.output_fig)
print(f"Annual SWE plots saved to {args.output_fig}")
else:
print("No output figure specified. Skipping plot generation.")
if __name__ == "__main__":
main()