From 1eaa5b6c29013908ab4fd1b8bf7375eb56c3024f Mon Sep 17 00:00:00 2001 From: minghangli-uni <24727729+minghangli-uni@users.noreply.github.com> Date: Thu, 26 Feb 2026 22:50:30 +1100 Subject: [PATCH] Use atomic write for bottom_roughness.nc to prevent netcdf corruption --- .../generate_bottom_roughness_regrid.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/external_tidal_generation/generate_bottom_roughness_regrid.py b/external_tidal_generation/generate_bottom_roughness_regrid.py index 54fa951..567ee1e 100644 --- a/external_tidal_generation/generate_bottom_roughness_regrid.py +++ b/external_tidal_generation/generate_bottom_roughness_regrid.py @@ -418,8 +418,17 @@ def main(): global_attrs["inputFile"] = ", ".join(file_hashes) regrid_depth_var.attrs.update(global_attrs) - regrid_depth_var.to_netcdf(args.output_file) - print(f"Output written to {args.output_file}") + output_path = Path(args.output_file) + tmp_path = output_path.with_suffix(output_path.suffix + ".tmp") + + # ensure tmp does not exist + if tmp_path.exists(): + tmp_path.unlink() + + regrid_depth_var.to_netcdf(tmp_path) + tmp_path.replace(output_path) + + print(f"Output written to {output_path}") if __name__ == "__main__":