Hello,
After the release of the new croco_pytools version, here are some personal feedback regarding the make_bry function.
I’m using croco_pytools to set up a new configuration over the northwestern Mediterranean Sea.
For the make_bry function, I use data from Mercator (Global Ocean Physics Reanalysis).
Here are the changes I made to make it easier to use for my case. I hope I didn’t introduce any mistakes during the process.
- Added units in get_uv
→ Added unit attributes to lon and lat (at the end of the function get_uv) to ensure correct vertical interpolation in get_3dfield
def get_uv(config, input_files, dico, z_rho, z_w, crocogrd, extrapolators=None,
velbar_ogcm = (myvel_masked * dz_masked).sum(dim=“depth”) / dz_masked.sum(
dim=“depth”
)
velbar_ogcm["lon"].attrs["units"] = "degrees_east"
velbar_ogcm["lat"].attrs["units"] = "degrees_north"
- Updated define_time_segments
→ Added one extra day at the end of each month to allow the creation of a restart file at midnight of the first day of the following month.
def define_time_segments(config, times):
if date_fmt == "MONTHLY":
current = date_start.replace(day=1, hour=config["Times"]["Hstart"])
while current <= date_end:
year, month = current.year, current.month
if (year, month) not in available_pairs:
raise ValueError(f" Missing data for {year}-{month:02d}")
next_month = current + relativedelta(months=1)# + relativedelta(hours=1)
segment_start, segment_end = search_realtimes(times, current, next_month)
segments.append((segment_start, segment_end + relativedelta(days=2)))
current = next_month
elif date_fmt == "YEARLY":
def define_time_segments(config, times):
# Add one step before if possible, else duplicate first timestep
if start_idx == 0:
print(f" No earlier time step for time {start}: duplicating {start}")
full_indices = full_indices
else:
full_indices = np.concatenate(([start_idx - 1], full_indices))
# Add one step after if possible, else duplicate last timestep
if end_idx >= len(times) - 1:
print(f" No later time step for time {end}: duplicating {end}")
full_indices = np.concatenate((full_indices-1, [full_indices[-2]]))
else:
full_indices = np.concatenate((full_indices, [end_idx + 1]))
- Converted dt to timedelta64
→ Allows proper calculations between seg_times and dt.
def define_time_segments(config, times):
seg_times.append(times[ind].astype(“datetime64[ms]”))
# Adjust first time if it’s a duplicate of the next one
if full_indices[0] == full_indices[1]:
dt_np = np.timedelta64(dt)
seg_times[0] = seg_times[1] - dt_np
# Adjust last time if it’s a duplicate of the previous one
if full_indices[-1] == full_indices[-2]:
dt_np = np.timedelta64(dt)
seg_times[-1] = seg_times[-2] + dt_np
# convert back to numpy datetimes for later
seg_times = np.array(seg_times, dtype=“datetime64[ms]”)
segments_times.append(seg_times)
return segments, segments_indices, segments_times
- Added two separate paths in ibc.ini for OBC and INI
→ Replaced config[“IBC_Input_Files”][“ibc_dir”] with
config[“IBC_Input_Files”][“ibc_dir_OBC”] or config[“IBC_Input_Files”][“ibc_dir_INI”]
in the functions create_ini_croco2croco, create_bry_croco2croco, and read_input_file_list.
def create_ini_croco2croco(config):
tracers = config["IBC_Options"]["tracers"]
prefix = config["IBC_Input_Files"]["ibc_prefix"]
input_dir = config["IBC_Input_Files"]["ibc_dir_INI"]
input_extension = config["IBC_Input_Files"]["ibc_extension"]
croco_dir = config["Croco_Files"]["croco_files_dir"]
croco_grdname = config["Croco_Files"]["croco_grd_prefix"] + "_zoom_offline.nc"
def create_bry_croco2croco(config):
tracers = config["IBC_Options"]["tracers"]
prefix = config["IBC_Input_Files"]["ibc_prefix"]
input_dir = config["IBC_Input_Files"]["ibc_dir_OBC"]
input_extension = config["IBC_Input_Files"]["ibc_extension"]
croco_dir = config["Croco_Files"]["croco_files_dir"]
croco_grdname = config["Croco_Files"]["croco_grd_prefix"] + "_zoom_offline.nc"
def read_input_file_list(config, ibc=None):
def select_file(config, ibc=ibc, var=None):
if ibc == "ini":
input_prefix = f"{config['IBC_Input_Files']['ibc_dir_INI']}/{config['IBC_Input_Files']['ibc_prefix']}"
if config["IBC_Input_Files"]["ibc_multi_files"]:
attr = "ibc_file_" + var
input_prefix = input_prefix + config["IBC_Input_Files"][attr]
elif ibc == "bry":
input_prefix = f"{config['IBC_Input_Files']['ibc_dir_OBC']}/{config['IBC_Input_Files']['ibc_prefix']}"
if config["IBC_Input_Files"]["ibc_multi_files"]:
attr = "ibc_file_" + var
input_prefix = input_prefix + config["IBC_Input_Files"][attr]
These changes helped fix several issues I encountered during the preprocessing for my regional setup.
Hopefully this feedback will be helpful for future updates of the tools.
Alice