Skip to content

Commit a58a4af

Browse files
committed
Copy original mask instead of creating a new file
This retains all other variables and global attributes.
1 parent 5b5a12b commit a58a4af

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

scripts/util/runmask.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import os
1111
import numpy as np
1212
from pathlib import Path
13+
import shutil
1314

1415
import util.input
1516
import util.param
@@ -469,10 +470,15 @@ def cmdline_run(args):
469470
# to replace the mask filename in the config file.
470471
mask_path = Path(mask_file)
471472
new_maskfile_name = str(mask_path.parent) + '/' + mask_path.stem + '_cmtfilter.nc'
472-
with nc.Dataset(new_maskfile_name, 'w') as nf:
473-
Y = nf.createDimension('Y', sizey)
474-
X = nf.createDimension('X', sizex)
475-
run = nf.createVariable('run', int, ('Y', 'X',))
473+
474+
# Copy the run mask file instead of creating a new one so that
475+
# it retains all other variables and global attributes.
476+
# copyfile() by default overwrites the destination file, so it's
477+
# safe to then use 'append' to open it.
478+
shutil.copyfile(mask_file, new_maskfile_name)
479+
480+
with nc.Dataset(new_maskfile_name, 'a') as nf:
481+
run = nf.variables['run']
476482
if args.verbose:
477483
print(f"Writing mask...{new_mask=}")
478484
run[:] = new_mask

0 commit comments

Comments
 (0)