Skip to content

Commit

Permalink
fix forcing nc files left unclosed
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshCu committed Jan 10, 2025
1 parent b1fb46a commit cfe83cc
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions modules/data_processing/forcings.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,12 @@ def compute_zonal_stats(
concatenated_da.to_dataset(name=variable).to_netcdf(forcings_dir/ "temp" / f"{variable}_{i}.nc")
# Merge the chunks back together
datasets = [xr.open_dataset(forcings_dir / "temp" / f"{variable}_{i}.nc") for i in range(len(time_chunks))]
xr.concat(datasets, dim="time").to_netcdf(forcings_dir / f"{variable}.nc")
result = xr.concat(datasets, dim="time")
result.to_netcdf(forcings_dir / f"{variable}.nc")
# close the datasets
result.close()
_ = [dataset.close() for dataset in datasets]

for file in forcings_dir.glob("temp/*.nc"):
file.unlink()
progress.remove_task(chunk_task)
Expand All @@ -237,6 +242,7 @@ def compute_zonal_stats(
)
write_outputs(forcings_dir, variables)


def write_outputs(forcings_dir, variables):

# start a dask cluster if there isn't one already running
Expand Down Expand Up @@ -289,9 +295,9 @@ def write_outputs(forcings_dir, variables):
final_ds["Time"].attrs["epoch_start"] = "01/01/1970 00:00:00" # not needed but suppresses the ngen warning

final_ds.to_netcdf(output_folder / "forcings.nc", engine="netcdf4")
# delete the individual variable files
for file in forcings_dir.glob("*.nc"):
file.unlink()
# close the datasets
_ = [result.close() for result in results]
final_ds.close()


def setup_directories(cat_id: str) -> file_paths:
Expand Down

0 comments on commit cfe83cc

Please sign in to comment.