-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_observations.py
388 lines (330 loc) · 12.2 KB
/
prepare_observations.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
386
387
388
# Standard library
import argparse
import os
from glob import glob
# Third-party
import copernicusmarine as cm
import numpy as np
import pandas as pd
import xarray as xr
from dask import compute, delayed
from dask.diagnostics import ProgressBar
from tqdm import tqdm
# First-party
from neural_lam import constants
def prepare_sst(output_dir):
"""
Prepare SST data.
"""
ds = cm.open_dataset(
dataset_id="SST_MED_SST_L3S_NRT_OBSERVATIONS_010_012_a",
variables=["sea_surface_temperature"],
minimum_longitude=-6,
start_datetime="2024-07-01T00:00:00",
end_datetime="2025-01-15T23:59:59",
)
ds["sea_surface_temperature"] = (
ds.sea_surface_temperature.where(
~((ds.longitude < 2) & (ds.latitude > 42))
)
- 273.15
)
new_times = ds["time"] - pd.Timedelta(days=1)
ds = ds.assign_coords(time=new_times)
output_file = os.path.join(output_dir, "sst.nc")
ds.to_netcdf(output_file)
def prepare_mhw(output_dir):
"""
Prepare MHW thresholds following Hobday et al. (2016).
"""
ds = cm.open_dataset(
dataset_id="SST_MED_SST_L3S_NRT_OBSERVATIONS_010_012_a",
variables=["sea_surface_temperature"],
minimum_longitude=-6,
start_datetime="2008-01-01T00:00:00",
end_datetime="2023-12-31T23:59:59",
)
ds["sea_surface_temperature"] = ds.sea_surface_temperature.where(
~((ds.longitude < 2) & (ds.latitude > 42))
)
# Match time to forecast
new_times = ds["time"] - pd.Timedelta(days=1)
ds = ds.assign_coords(time=new_times)
# Convert to Celsius
sst_obs = ds["sea_surface_temperature"] - 273.15
# Smooth the SST time series using an 11-day rolling average
sst_smoothed = sst_obs.rolling(time=11, center=True, min_periods=1).mean()
# Compute day-of-year for the smoothed data
time_obs = pd.to_datetime(ds["time"].values)
doy = xr.DataArray(time_obs.dayofyear, coords=[time_obs], dims="time")
# Compute the 90th percentile climatology for each doy
clim = sst_smoothed.groupby(doy).reduce(np.nanpercentile, q=90)
# Rename the resulting group dimension to dayofyear
clim = clim.rename({"group": "dayofyear"})
# Create a new dataset to store the thresholds
ds_threshold = xr.Dataset(
{"sst_threshold": (("dayofyear",) + sst_obs.dims[1:], clim.data)},
coords={
"dayofyear": clim.dayofyear,
"latitude": sst_obs.latitude,
"longitude": sst_obs.longitude,
},
)
# Save the computed thresholds to NetCDF
output_file = os.path.join(output_dir, "mhw_thresholds.nc")
ds_threshold.to_netcdf(output_file)
print("MHW thresholds saved to:", output_file)
def pad_dataset_list(ds_list, dim, constant_values=np.nan):
"""
Pad each ds along the given dimension so that all have the same size.
Parameters:
ds_list: list of ds objects.
dim: the dimension along which to pad (e.g., obs or file).
constant_values: the value to pad with (default: nan).
Returns:
A list of ds objects padded along the specified dim to the same size.
"""
target_size = max(ds.sizes[dim] for ds in ds_list)
padded_list = []
for ds in ds_list:
current_size = ds.sizes[dim]
pad_width = target_size - current_size
if pad_width > 0:
ds = ds.pad(
**{dim: (0, pad_width)}, constant_values=constant_values
)
padded_list.append(ds)
return padded_list
def prepare_sla(input_dir, output_dir):
"""
Prepare SLA data.
sla = sla_filtered + dac + ocean_tide + internal_tide
For each day, combine the observations from all missions
by stacking the time dimension into a new obs dimension,
then assign a unique daily time coordinate. Finally, pad
and concatenate the daily datasets along time.
"""
missions = [
"HY-2B",
"Jason-3",
"Sentinel-3A",
"Sentinel-3B",
"Sentinel-6A",
"Swon",
]
dates = pd.date_range("2024-07-01", "2025-01-15", freq="D")
daily_ds_list = []
# Process each day
for date in tqdm(dates, desc="Processing days"):
date_str = date.strftime("%Y%m%d")
day_ds_parts = []
for mission in missions:
month_str = date.strftime("%m")
mission_dir = os.path.join(
input_dir, "sla", mission, str(date.year), month_str
)
pattern = os.path.join(mission_dir, f"*5hz_{date_str}_*.nc")
files = glob(pattern)
for f in files:
ds = xr.open_dataset(f)
# Compute SLA
ds["sla"] = (
ds["sla_filtered"]
+ ds["dac"]
+ ds["ocean_tide"]
+ ds["internal_tide"]
)
# Floor the time coordinate to the day
ds = ds.assign_coords(time=ds["time"].dt.floor("D"))
# Select only measurements for the current day
ds_day = ds.where(ds.time == np.datetime64(date), drop=True)
# Stack the time dimension into a new obs dimension
ds_day = ds_day.stack(obs=("time",)).reset_index(
"obs", drop=True
)
# Keep only the SLA variable
ds_day = ds_day[["sla"]]
day_ds_parts.append(ds_day)
ds.close()
# Pad each file's obs dimension within the day to have the same size
padded_day_ds = pad_dataset_list(
day_ds_parts, "obs", constant_values=np.nan
)
# Concatenate along a new file dimension
day_ds = xr.concat(padded_day_ds, dim="file", combine_attrs="drop")
# Assign a unique time coordinate for the day
day_ds = day_ds.expand_dims(time=[np.datetime64(date, "ns")])
daily_ds_list.append(day_ds)
# Pad daily datasets along the obs dimension across days
padded_ds_obs = pad_dataset_list(
daily_ds_list, "obs", constant_values=np.nan
)
# Pad daily datasets along the file dimension across days
padded_ds_files = pad_dataset_list(
padded_ds_obs, "file", constant_values=np.nan
)
# Concatenate daily datasets along the time dimension
ds_all = xr.concat(padded_ds_files, dim="time")
ds_all = ds_all.sortby("time")
output_file = os.path.join(output_dir, "sla.nc")
ds_all.to_netcdf(output_file)
print(f"Merged SLA dataset saved to {output_file}")
def compute_grid_bins(surface_mask):
"""
Compute bin edges for latitude and longitude from the surface mask.
"""
# Get unique grid cell centers
lat_centers = np.array(surface_mask.latitude.values)
lon_centers = np.array(surface_mask.longitude.values)
# Sort if not already sorted
lat_centers = np.sort(lat_centers)
lon_centers = np.sort(lon_centers)
# Calculate bin edges as midpoints between adjacent centers
lat_edges = np.concatenate(
(
[lat_centers[0] - (lat_centers[1] - lat_centers[0]) / 2],
(lat_centers[:-1] + lat_centers[1:]) / 2,
[lat_centers[-1] + (lat_centers[-1] - lat_centers[-2]) / 2],
)
)
lon_edges = np.concatenate(
(
[lon_centers[0] - (lon_centers[1] - lon_centers[0]) / 2],
(lon_centers[:-1] + lon_centers[1:]) / 2,
[lon_centers[-1] + (lon_centers[-1] - lon_centers[-2]) / 2],
)
)
return lat_edges, lon_edges
@delayed
def process_file(f, lat_edges, lon_edges, depth_edges, variables, date):
"""Delayed function to process a single file."""
ds = xr.open_dataset(f)
# Skip file if none of the variables are present
if not any(v in ds.data_vars for v in variables):
return None
# Keep only quality checked data
present_meas = set(variables) & set(ds.data_vars)
for meas in present_meas:
qc_var = f"{meas}_QC" if f"{meas}_QC" in ds.data_vars else "QCflag"
ds[meas] = ds[meas].where(ds[qc_var] == 1)
try:
ds = ds[present_meas]
except KeyError:
return None
source = ds.attrs.get("source", "unknown")
if source == "unknown":
return None
# Convert dataset to df
df_file = ds.to_dataframe().reset_index()
if "DEPH" not in df_file.columns:
df_file["DEPH"] = df_file["PRES"] / 1.019716
# Create latitude, longitude, and depth bins
df_file["lat_bin"] = pd.cut(
df_file["LATITUDE"], bins=lat_edges, include_lowest=True
)
df_file["lon_bin"] = pd.cut(
df_file["LONGITUDE"], bins=lon_edges, include_lowest=True
)
df_file["depth_bin"] = pd.cut(
df_file["DEPH"], bins=depth_edges, include_lowest=True
)
# Group by the grid cell bins and depth bin
group_cols = ["lat_bin", "lon_bin", "depth_bin"]
grouped = df_file.groupby(group_cols, observed=False)
dfs_local = []
for (lat_bin, lon_bin, depth_bin), group in grouped:
if group.empty:
continue
# Check if all measurement variables are nan in this group
if group[list(present_meas)].isna().all().all():
continue
df_mean = group.mean(skipna=True, numeric_only=True)
df_mean["date"] = date
df_mean["source"] = source
dfs_local.append(df_mean)
if dfs_local:
return pd.concat(dfs_local, axis=1).T
else:
return None
def prepare_in_situ(input_dir, output_dir):
data_dir = os.path.join(input_dir, "in_situ")
dates = pd.date_range("2024-07-01", "2025-01-15", freq="D")
variables = ["EWCT", "NSCT", "PSAL", "TEMP"]
delayed_results = []
# Load bathymetry to get the surface mask
bathy_path = os.path.join(
"data", "mediterranean", "static", "bathy_mask.nc"
)
bathy_data = xr.load_dataset(bathy_path)
sea_mask = bathy_data.where(bathy_data.mask, drop=True).mask
surface_mask = sea_mask.isel(depth=0)
# Compute latitude and longitude bin edges from the surface mask
lat_edges, lon_edges = compute_grid_bins(surface_mask)
# Define depth bins using the depth centers
centers = constants.DEPTHS
depth_edges = (
[0]
+ [(centers[i] + centers[i + 1]) / 2 for i in range(len(centers) - 1)]
+ [200]
)
print("Depth edges:", depth_edges)
for date in dates:
date_str = date.strftime("%Y%m%d")
date_dir = os.path.join(data_dir, date_str)
files = glob(os.path.join(date_dir, "*.nc"))
for f in files:
delayed_results.append(
process_file(
f, lat_edges, lon_edges, depth_edges, variables, date
)
)
# Compute all the delayed tasks in parallel
with ProgressBar():
results = compute(*delayed_results, scheduler="processes")
# Filter results
dfs = [r for r in results if r is not None]
df_all = pd.concat(dfs, axis=0)
# Define columns to keep
cols_to_keep = [
"date",
"LATITUDE",
"LONGITUDE",
"DEPH",
"source",
] + variables
df_all = df_all[cols_to_keep]
df_all = df_all.rename(columns={"DEPH": "depth"})
df_all.columns = df_all.columns.str.lower()
df_all = df_all.sort_values("date")
df_all = df_all.reset_index(drop=True)
ds_out = xr.Dataset.from_dataframe(df_all)
output_file = os.path.join(output_dir, "in_situ.nc")
ds_out.to_netcdf(output_file)
print(f"Merged in situ data saved to {output_file}")
def main():
parser = argparse.ArgumentParser(description="Prepare observation data.")
parser.add_argument(
"-d",
"--data",
nargs="+",
choices=["sst", "mhw", "sla", "in_situ"],
required=True,
)
parser.add_argument(
"--input_dir", type=str, default="data/mediterranean/raw"
)
parser.add_argument(
"--output_dir", type=str, default="data/mediterranean/observations"
)
args = parser.parse_args()
os.makedirs(args.output_dir, exist_ok=True)
if "sst" in args.data:
prepare_sst(args.output_dir)
if "mhw" in args.data:
prepare_mhw(args.output_dir)
if "sla" in args.data:
prepare_sla(args.input_dir, args.output_dir)
if "in_situ" in args.data:
prepare_in_situ(args.input_dir, args.output_dir)
if __name__ == "__main__":
main()