Skip to content

Commit 3d65547

Browse files
Merge pull request #21 from CMCC-Foundation/gebco
Changes on gebco handling and plotting
2 parents 1c1ab54 + 4c5c87d commit 3d65547

File tree

6 files changed

+83
-13
lines changed

6 files changed

+83
-13
lines changed

config.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
advanced_parameters_path = "WITOIL_iMagine/src/parameters.toml" # this path shuld be provided only if "advanced_parameters" is = true
1414
[download]
1515
download_data = true # = true if data are not provided by the user
16+
download_bath = true # = true if user wants to crop gebco from ERDDAP
1617
download_curr = true # = true : OCE data are downloaded from Copernicus Marine Service
1718
download_wind = true # = true : MET data are downloaded from ECMWF ERA5 product
1819
copernicus_user = "none"
@@ -25,7 +26,7 @@
2526
delta = [0.75] # default domain length in degrees (applied to both lon/lat), to download or crop data
2627
# note: delta is used only if set_domain = false
2728
[input_files.dtm]
28-
bathymetry_path = "WITOIL_iMagine/data/gebco/GEBCO.nc"
29+
bathymetry_path = false
2930
coastline_path = "WITOIL_iMagine/data/gshhs/GSHHS_shp/f/GSHHS_f_L1.shp"
3031
[input_files.metoce]
3132
oce_data_path = false # to provide if dowload_curr = false

main.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,28 @@ def data_download_medslik(
159159
else:
160160
down = "global"
161161

162+
if config["download"]["download_bath"]:
163+
output_path = "data/gebco/"
164+
output_name = output_path + "GEBCO_slice_{}_{}_mdk.nc".format(
165+
identifier, config["simulation"]["name"]
166+
)
167+
logger.info("Obtaining GEBCO bathymetry from ERDDAP")
168+
gebco_errdap(
169+
lat_min,
170+
lat_max,
171+
lon_min,
172+
lon_max,
173+
output_name=output_name
174+
)
175+
# Copy downloaded GEBCO file(s) to the bnc_files directory
176+
source_files = gg(f"{output_path}*{identifier}*{config['simulation']['name']}*.nc")
177+
destination = os.path.join(root_directory, "bnc_files")
178+
for file in source_files:
179+
shutil.copy(file, destination)
180+
# Remove the temporary GEBCO files from the output path
181+
for file in source_files:
182+
os.remove(file)
183+
162184
if config["download"]["download_curr"]:
163185

164186
output_path = "data/COPERNICUS/"

src/download/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from .download_copernicus_parser import *
22
from .download_era5_parser import *
3+
from .download_gebco_ERDDAP import *
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import xarray as xr
2+
3+
# Functions outside this script
4+
from WITOIL_iMagine.src.utils.utils import *
5+
6+
def gebco_errdap(
7+
min_lat,
8+
max_lat,
9+
min_lon,
10+
max_lon,
11+
output_name=str,
12+
):
13+
14+
ds = xr.open_dataset('https://erddap.cmcc-opa.eu/erddap/griddap/Surf_f204_4c2a_5962')
15+
16+
ds = ds.sel(latitude = slice(min_lat,max_lat), longitude = slice(min_lon,max_lon))
17+
18+
# Rename variables only if they exist in the dataset
19+
ds = Utils.rename_netcdf_variables_mdk3(ds)
20+
21+
ds.to_netcdf(output_name)

src/plot/plot_mdk3.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,28 @@ def plot_matplotlib(self,lon_min,lon_max,lat_min,lat_max):
9797
# cropping coastline to area of interest
9898
rec = land.cx[lon_min:lon_max, lat_min:lat_max]
9999

100-
# selecting simulation domain
101-
curr = curr.sel(lon=slice(lon_min, lon_max), lat=slice(lat_min, lat_max))
100+
# Get the full coordinate arrays from the dataset
101+
lon_arr = curr.lon.values
102+
lat_arr = curr.lat.values
103+
104+
# Find indices for the outward slice
105+
# For the lower bound - the grid value that is <= the lon_min or lat_min.
106+
i_min = np.searchsorted(lon_arr, lon_min, side="right") - 1
107+
j_min = np.searchsorted(lat_arr, lat_min, side="right") - 1
108+
109+
# For the upper bound - the grid value that is >= the lon_max or lat_max.
110+
i_max = np.searchsorted(lon_arr, lon_max, side="left")
111+
j_max = np.searchsorted(lat_arr, lat_max, side="left")
112+
113+
# Make sure indices are within bounds
114+
i_min = max(i_min, 0)
115+
j_min = max(j_min, 0)
116+
i_max = min(i_max, len(lon_arr) - 1)
117+
j_max = min(j_max, len(lat_arr) - 1)
118+
119+
### Subset the current data to the plot boundaries ###
120+
# Slice the dataset using these indices
121+
curr = curr.isel(lon=slice(i_min, i_max + 1), lat=slice(j_min, j_max + 1))
102122

103123
min_concentration = 0.0
104124
max_concentration = (ds_particles.concentration * 1000).max().values
@@ -782,9 +802,9 @@ def __init__(self, x, y):
782802
self.width = x[-1] - x[0]
783803
self.height = y[-1] - y[0]
784804

785-
if not np.allclose(np.diff(x), self.width / (self.nx - 1), atol=1e-5):
805+
if not np.allclose(np.diff(x), self.width / (self.nx - 1), atol=1e-4):
786806
raise ValueError("'x' values must be equally spaced")
787-
if not np.allclose(np.diff(y), self.height / (self.ny - 1), atol=1e-5):
807+
if not np.allclose(np.diff(y), self.height / (self.ny - 1), atol=1e-4):
788808
raise ValueError("'y' values must be equally spaced")
789809

790810
@property

src/preprocessing/preprocessing_mdk3.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,20 @@ def _copy_nc_files(self, src_files: str, dst_dir: str) -> None:
130130
def process_bathymetry(self,gebco):
131131

132132
grid = self.grid
133-
gebco = xr.open_dataset(gebco)
134-
135-
if 'nav_lat' in grid and 'nav_lon' in grid:
136-
grid = grid.rename({'nav_lat': 'lat', 'nav_lon': 'lon'})
133+
134+
if gebco is None or not gebco:
135+
bnc_path = f"{self.exp_folder}/bnc_files/*.nc"
136+
else:
137+
bnc_path = gebco
137138

138-
# try:
139-
# grid = grid.rename({'nav_lat':'lat','nav_lon':'lon'})
140-
# except:
141-
# pass
139+
# Use open_mfdataset with the netcdf4 engine (instead of open_dataset)
140+
gebco = xr.open_mfdataset(bnc_path, engine="netcdf4")
141+
142+
# --- NEW: Rename variables if they are 'latitude'/'longitude'
143+
try:
144+
gebco = gebco.rename({"latitude": "lat", "longitude": "lon"})
145+
except Exception:
146+
pass
142147

143148
# interpolation on medslik grid
144149
med = gebco.interp(lon=grid.lon.values.tolist(),lat=grid.lat.values.tolist())

0 commit comments

Comments
 (0)