@@ -43,7 +43,8 @@ def prepare_states(
43
43
44
44
# Process each file, concatenate with the next t-1 files
45
45
for i in range (len (files ) - n_states + 1 ):
46
- out_filename = f"{ prefix } _{ os .path .basename (files [i + 2 ])} "
46
+ # Name as today's date
47
+ out_filename = f"{ prefix } _{ os .path .basename (files [i + 1 ])} "
47
48
out_file = os .path .join (out_directory , out_filename )
48
49
49
50
if os .path .isfile (out_file ):
@@ -61,63 +62,133 @@ def prepare_states(
61
62
62
63
# Save concatenated data to the output directory
63
64
np .save (out_file , full_state )
64
- print (f"Saved concatenated file : { out_file } " )
65
+ print (f"Saved states to : { out_file } " )
65
66
66
67
67
68
def prepare_forcing (in_directory , out_directory , prefix , start_date , end_date ):
68
69
"""
69
- Prepare atmospheric forcing data from HRES files .
70
+ Prepare atmospheric forcing data from forecasts .
70
71
"""
71
- hres_dir = in_directory
72
+ forecast_dir = in_directory
72
73
73
74
start_dt = datetime .strptime (start_date , "%Y-%m-%d" )
74
75
end_dt = datetime .strptime (end_date , "%Y-%m-%d" )
75
76
76
77
os .makedirs (out_directory , exist_ok = True )
77
78
78
- # Get HRES files sorted by date
79
- hres_files = sorted (
80
- glob (os .path .join (hres_dir , "*.npy" )),
79
+ # Get files sorted by date
80
+ forecast_files = sorted (
81
+ glob (os .path .join (forecast_dir , "*.npy" )),
81
82
key = lambda x : datetime .strptime (os .path .basename (x )[:8 ], "%Y%m%d" ),
82
83
)
83
- hres_files = [
84
+ forecast_files = [
84
85
f
85
- for f in hres_files
86
+ for f in forecast_files
86
87
if start_dt
87
88
<= datetime .strptime (os .path .basename (f )[:8 ], "%Y%m%d" )
88
89
<= end_dt
89
90
]
90
91
91
- for hres_file in hres_files :
92
- hres_date = datetime .strptime (os . path . basename ( hres_file )[: 8 ], "%Y%m%d" )
93
- # Get files for the two preceding days
94
- preceding_days_files = [
95
- os . path . join (
96
- hres_dir ,
97
- ( hres_date - timedelta ( days = i )). strftime ( "%Y%m%d" ) + ".npy" ,
98
- )
99
- for i in range ( 1 , 3 )
100
- ]
92
+ for forecast_file in forecast_files :
93
+ forecast_date = datetime .strptime (
94
+ os . path . basename ( forecast_file )[: 8 ], "%Y%m%d"
95
+ )
96
+ # Get files for the preceding day
97
+ preceding_day_file = os . path . join (
98
+ forecast_dir ,
99
+ ( forecast_date - timedelta ( days = 1 )). strftime ( "%Y%m%d" ) + ".npy" ,
100
+ )
101
+ preceding_day_data = np . load ( preceding_day_file )[ 0 : 1 ]
101
102
102
- # Load the first timestep from each preceding day's HRES file
103
- init_states = []
104
- for file_path in preceding_days_files :
105
- data = np .load (file_path )
106
- init_states .append (data [0 :1 ])
103
+ # Load the current forecast data
104
+ current_forecast_data = np .load (forecast_file )[:15 ]
107
105
108
- # Load the current HRES data
109
- current_hres_data = np .load (hres_file )
106
+ print (preceding_day_data .shape , current_forecast_data .shape )
110
107
111
108
# Concatenate all data along the time axis
112
109
concatenated_forcing = np .concatenate (
113
- init_states + [ current_hres_data ], axis = 0
110
+ [ preceding_day_data , current_forecast_data ], axis = 0
114
111
)
115
112
116
113
# Save concatenated data
117
- out_filename = f"{ prefix } _{ os .path .basename (hres_file )} "
114
+ out_filename = f"{ prefix } _{ os .path .basename (forecast_file )} "
118
115
out_file = os .path .join (out_directory , out_filename )
119
116
np .save (out_file , concatenated_forcing )
120
- print (f"Saved combined forcing data file: { out_file } " )
117
+ print (f"Saved forcing states to: { out_file } " )
118
+
119
+
120
+ def prepare_aifs_forcing (
121
+ in_directory , out_directory , prefix , start_date , end_date
122
+ ):
123
+ """
124
+ Prepare atmospheric forcing data from AIFS forecasts (add SSR from ENS).
125
+ """
126
+ start_dt = datetime .strptime (start_date , "%Y-%m-%d" )
127
+ end_dt = datetime .strptime (end_date , "%Y-%m-%d" )
128
+
129
+ os .makedirs (out_directory , exist_ok = True )
130
+
131
+ # Get files sorted by date
132
+ forecast_files = sorted (
133
+ glob (os .path .join (in_directory , "*.npy" )),
134
+ key = lambda x : datetime .strptime (os .path .basename (x )[:8 ], "%Y%m%d" ),
135
+ )
136
+ forecast_files = [
137
+ f
138
+ for f in forecast_files
139
+ if start_dt
140
+ <= datetime .strptime (os .path .basename (f )[:8 ], "%Y%m%d" )
141
+ <= end_dt
142
+ ]
143
+
144
+ ifs_variables = ["u10" , "v10" , "t2m" , "msl" , "ssr" , "tp" ]
145
+
146
+ for forecast_file in forecast_files :
147
+ forecast_date = datetime .strptime (
148
+ os .path .basename (forecast_file )[:8 ], "%Y%m%d"
149
+ )
150
+
151
+ # Load the preceding day's data
152
+ preceding_day_file = os .path .join (
153
+ in_directory ,
154
+ (forecast_date - timedelta (days = 1 )).strftime ("%Y%m%d" ) + ".npy" ,
155
+ )
156
+ preceding_day_data = np .load (preceding_day_file )[0 :1 ]
157
+
158
+ # Insert SSR from ENS data
159
+ preceding_day_ens_data = np .load (
160
+ preceding_day_file .replace ("aifs" , "ens" )
161
+ )[0 :1 ]
162
+ preceding_day_ssr_data = preceding_day_ens_data [
163
+ ..., ifs_variables .index ("ssr" )
164
+ ]
165
+ preceding_day_data = np .insert (
166
+ preceding_day_data ,
167
+ ifs_variables .index ("ssr" ),
168
+ preceding_day_ssr_data ,
169
+ axis = - 1 ,
170
+ )
171
+
172
+ # Load the current forecast data
173
+ current_forecast_data = np .load (forecast_file )[:15 ]
174
+
175
+ # Insert SSR from ENS data
176
+ ens_data = np .load (forecast_file .replace ("aifs" , "ens" ))[:15 ]
177
+ ssr_data = ens_data [..., ifs_variables .index ("ssr" )]
178
+ current_forecast_data = np .insert (
179
+ current_forecast_data , ifs_variables .index ("ssr" ), ssr_data , axis = - 1
180
+ )
181
+
182
+ # Concatenate preceding day data with current forecast data
183
+ aifs_data = np .concatenate (
184
+ [preceding_day_data , current_forecast_data ], axis = 0
185
+ )
186
+
187
+ # Save combined data
188
+ out_filename = f"{ prefix } _{ os .path .basename (forecast_file )} "
189
+ out_file = os .path .join (out_directory , out_filename )
190
+ np .save (out_file , aifs_data )
191
+ print (f"Saved forcing states to: { out_file } " )
121
192
122
193
123
194
def main ():
@@ -176,13 +247,22 @@ def main():
176
247
args = parser .parse_args ()
177
248
178
249
if args .forecast_forcing :
179
- prepare_forcing (
180
- args .data_dir ,
181
- args .out_dir ,
182
- args .prefix ,
183
- args .start_date ,
184
- args .end_date ,
185
- )
250
+ if args .data_dir .endswith ("aifs" ):
251
+ prepare_aifs_forcing (
252
+ args .data_dir ,
253
+ args .out_dir ,
254
+ args .prefix ,
255
+ args .start_date ,
256
+ args .end_date ,
257
+ )
258
+ else :
259
+ prepare_forcing (
260
+ args .data_dir ,
261
+ args .out_dir ,
262
+ args .prefix ,
263
+ args .start_date ,
264
+ args .end_date ,
265
+ )
186
266
else :
187
267
prepare_states (
188
268
args .data_dir ,
0 commit comments