-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_states.py
385 lines (329 loc) · 11.4 KB
/
prepare_states.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# Standard library
import argparse
import os
from datetime import datetime, timedelta
from glob import glob
# Third-party
import numpy as np
# First-party
from neural_lam import constants
def prepare_states(
in_directory, out_directory, n_states, prefix, start_date, end_date
):
"""
Processes and concatenates state sequences from numpy files.
Args:
in_directory (str): Directory containing the .npy files.
out_directory (str): Directory to store the concatenated files.
n_states (int): Number of consecutive states to concatenate.
prefix (str): Prefix for naming the output files.
start_date (str): Start date.
end_date (str): End date.
"""
# Parse dates
start_dt = datetime.strptime(start_date, "%Y-%m-%d")
end_dt = datetime.strptime(end_date, "%Y-%m-%d")
print(start_dt, end_dt)
# Get all numpy files sorted by date
all_files = sorted(glob(os.path.join(in_directory, "*.npy")))
files = [
f
for f in all_files
if start_dt
<= datetime.strptime(os.path.basename(f)[:8], "%Y%m%d")
<= end_dt
]
# Ensure output directory exists
os.makedirs(out_directory, exist_ok=True)
# Process each file, concatenate with the next t-1 files
for i in range(len(files) - n_states + 1):
# Name as first forecasted date
out_filename = f"{prefix}_{os.path.basename(files[i + 2])}"
out_file = os.path.join(out_directory, out_filename)
if os.path.isfile(out_file):
continue
state_sequence = []
# Load each state to concatenate
for j in range(n_states):
state = np.load(files[i + j])
state_sequence.append(state)
# Concatenate along new axis (time axis)
full_state = np.stack(state_sequence, axis=0)
# Save concatenated data to the output directory
np.save(out_file, full_state)
print(f"Saved states to: {out_file}")
def prepare_states_with_boundary(
in_directory,
static_dir_path,
out_directory,
n_states,
prefix,
start_date,
end_date,
):
"""
Concatenat analysis states and include forecast boundary.
Args:
in_directory (str): Directory containing the analysis .npy files.
static_dir_path (str): Directory containing the static files.
out_directory (str): Directory to store the concatenated files.
n_states (int): Number of consecutive states to concatenate.
prefix (str): Prefix for naming the output files.
start_date (str): Start date.
end_date (str): End date.
"""
# Parse dates
start_dt = datetime.strptime(start_date, "%Y-%m-%d")
end_dt = datetime.strptime(end_date, "%Y-%m-%d")
print(start_dt, end_dt)
# Load boundary mask
boundary_mask = np.load(
os.path.join(static_dir_path, "boundary_mask.npy")
) # (depths, h, w)
sea_mask = np.load(
os.path.join(static_dir_path, "sea_mask.npy")
) # (depths, h, w)
surface_mask = sea_mask[0]
boundary_mask = boundary_mask[:, surface_mask]
border_mask = []
for level_applies in constants.LEVELS:
if level_applies:
border_mask.append(boundary_mask)
else:
border_mask.append(boundary_mask[0][np.newaxis, :])
border_mask = np.concatenate(border_mask, axis=0).transpose(1, 0)[
np.newaxis, :, :
]
print("border mask", border_mask.shape) # 1, N_grid, d_features
# Get all analysis files sorted by date
all_files = sorted(glob(os.path.join(in_directory, "*.npy")))
files = [
f
for f in all_files
if start_dt
<= datetime.strptime(os.path.basename(f)[:8], "%Y%m%d")
<= end_dt
]
# Ensure output directory exists
os.makedirs(out_directory, exist_ok=True)
# Process each file, concatenate with the next t-1 files
for i in range(len(files) - n_states + 1):
forecast_date = os.path.basename(files[i + 2])
out_filename = f"{prefix}_{forecast_date}"
out_file = os.path.join(out_directory, out_filename)
# Stack analysis states
state_sequence = [np.load(files[i + j]) for j in range(n_states)]
full_state = np.stack(state_sequence, axis=0)
print("full state", full_state.shape) # (n_states, N_grid, d_features)
forecast_file = files[i + 2].replace("analysis", "forecast")
forecast_data = np.load(forecast_file)[2:]
print(
"forecast before", forecast_data.shape
) # (forecast_len, N_grid, d_features)
extra_states = 5
last_forecast_state = forecast_data[-1]
repeated_forecast_states = np.repeat(
last_forecast_state[np.newaxis, ...], extra_states, axis=0
)
forecast_data = np.concatenate(
[forecast_data, repeated_forecast_states], axis=0
)
print(
"forecast after", forecast_data.shape
) # (n_states - 2, N_grid, d_features)
# Concatenate preceding day analysis state with forecast data
forecast_data = np.concatenate(
(state_sequence[:2], forecast_data), axis=0
) # (n_states, N_grid, d_features)
full_state = (
full_state * (1 - border_mask) + forecast_data * border_mask
)
np.save(out_file, full_state.astype(np.float32))
print(f"Saved states to: {out_file}")
def prepare_forecast(in_directory, out_directory, prefix, start_date, end_date):
"""
Prepare forecast data by repeating the last state.
"""
forecast_dir = in_directory
start_dt = datetime.strptime(start_date, "%Y-%m-%d")
end_dt = datetime.strptime(end_date, "%Y-%m-%d")
os.makedirs(out_directory, exist_ok=True)
# Get files sorted by date
forecast_files = sorted(
glob(os.path.join(forecast_dir, "*.npy")),
key=lambda x: datetime.strptime(os.path.basename(x)[:8], "%Y%m%d"),
)
forecast_files = [
f
for f in forecast_files
if start_dt
<= datetime.strptime(os.path.basename(f)[:8], "%Y%m%d")
<= end_dt
]
for forecast_file in forecast_files:
# Load the current forecast data
forecast_data = np.load(forecast_file)
print(forecast_data.shape)
last_forecast_state = forecast_data[-1]
repeated_forecast_states = np.repeat(
last_forecast_state[np.newaxis, ...], repeats=5, axis=0
)
forecast_data = np.concatenate(
[forecast_data, repeated_forecast_states], axis=0
)
# Save concatenated data
out_filename = f"{prefix}_{os.path.basename(forecast_file)}"
out_file = os.path.join(out_directory, out_filename)
np.save(out_file, forecast_data)
print(f"Saved forecast to: {out_file}")
def prepare_forcing(in_directory, out_directory, prefix, start_date, end_date):
"""
Prepare atmospheric forcing data from forecasts.
"""
forecast_dir = in_directory
start_dt = datetime.strptime(start_date, "%Y-%m-%d")
end_dt = datetime.strptime(end_date, "%Y-%m-%d")
os.makedirs(out_directory, exist_ok=True)
# Get files sorted by date
forecast_files = sorted(
glob(os.path.join(forecast_dir, "*.npy")),
key=lambda x: datetime.strptime(os.path.basename(x)[:8], "%Y%m%d"),
)
forecast_files = [
f
for f in forecast_files
if start_dt
<= datetime.strptime(os.path.basename(f)[:8], "%Y%m%d")
<= end_dt
]
for forecast_file in forecast_files:
forecast_date = datetime.strptime(
os.path.basename(forecast_file)[:8], "%Y%m%d"
)
# Get files for the pre-preceding day
prepreceding_day_file = os.path.join(
forecast_dir,
(forecast_date - timedelta(days=2)).strftime("%Y%m%d") + ".npy",
)
prepreceding_day_data = np.load(prepreceding_day_file)[0:1]
# Get files for the preceding day
preceding_day_file = os.path.join(
forecast_dir,
(forecast_date - timedelta(days=1)).strftime("%Y%m%d") + ".npy",
)
preceding_day_data = np.load(preceding_day_file)[0:1]
# Load the current forecast data
current_forecast_data = np.load(forecast_file)[:15]
print(preceding_day_data.shape, current_forecast_data.shape)
prepreceding_day_data = prepreceding_day_data[:, :, :4]
preceding_day_data = preceding_day_data[:, :, :4]
current_forecast_data = current_forecast_data[:, :, :4]
# Concatenate all data along the time axis
concatenated_forcing = np.concatenate(
[prepreceding_day_data, preceding_day_data, current_forecast_data],
axis=0,
)
# Save concatenated data
out_filename = f"{prefix}_{os.path.basename(forecast_file)}"
out_file = os.path.join(out_directory, out_filename)
np.save(out_file, concatenated_forcing)
print(f"Saved forcing states to: {out_file}")
def main():
"""
Main function to parse arguments and prepare state sequences.
"""
parser = argparse.ArgumentParser(
description="Prepare state sequences from Baltic Sea data files."
)
parser.add_argument(
"-d",
"--data_dir",
type=str,
required=True,
help="Directory containing .npy files",
)
parser.add_argument(
"-o",
"--out_dir",
type=str,
required=True,
help="Output directory for concatenated files",
)
parser.add_argument(
"-m",
"--static_dir",
type=str,
default="data/mediterranean/static",
help="Directory containing static files",
)
parser.add_argument(
"-n",
"--n_states",
type=int,
default=6,
help="Number of states to concatenate",
)
parser.add_argument(
"-p",
"--prefix",
type=str,
required="ana_data",
help="Prefix for the output files",
)
parser.add_argument(
"-s",
"--start_date",
type=str,
default="1987-01-01",
help="Start date in YYYY-MM-DD format",
)
parser.add_argument(
"-e",
"--end_date",
type=str,
default="2024-05-25",
help="End date in YYYY-MM-DD format",
)
parser.add_argument(
"--forecast",
action="store_true",
)
args = parser.parse_args()
if args.forecast:
if args.data_dir.endswith("aifs") or args.data_dir.endswith("ens"):
prepare_forcing(
args.data_dir,
args.out_dir,
args.prefix,
args.start_date,
args.end_date,
)
elif args.data_dir.endswith("analysis"):
prepare_states_with_boundary(
args.data_dir,
args.static_dir,
args.out_dir,
args.n_states,
args.prefix,
args.start_date,
args.end_date,
)
else:
prepare_forecast(
args.data_dir,
args.out_dir,
args.prefix,
args.start_date,
args.end_date,
)
else:
prepare_states(
args.data_dir,
args.out_dir,
args.n_states,
args.prefix,
args.start_date,
args.end_date,
)
if __name__ == "__main__":
main()