Skip to content

Commit

Permalink
updating capability to check if files exist before overwriting.
Browse files Browse the repository at this point in the history
  • Loading branch information
negin513 committed Feb 10, 2022
1 parent 324e9e7 commit dbc48b7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
38 changes: 35 additions & 3 deletions python/ctsm/site_and_regional/base_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import xarray as xr

# -- import local classes for this script
from ctsm.utils import abort
from ctsm.git_utils import get_ctsm_git_short_hash

USRDAT_DIR = "CLM_USRDAT_DIR"
Expand Down Expand Up @@ -46,6 +47,9 @@ class BaseCase:
flag for creating DATM files
create_user_mods
flag for creating a user_mods directory
overwrite : bool
flag for overwriting if the file already exists
Methods
-------
create_1d_coord(filename, lon_varname , lat_varname,x_dim , y_dim )
Expand All @@ -56,7 +60,7 @@ class BaseCase:
"""

def __init__(self, create_domain, create_surfdata, create_landuse, create_datm,
create_user_mods):
create_user_mods, overwrite):
"""
Initializes BaseCase with the given arguments.
Expand All @@ -72,12 +76,15 @@ def __init__(self, create_domain, create_surfdata, create_landuse, create_datm,
Flag for creating datm files a region/single point
create_user_mods : bool
Flag for creating user mods directories and files for running CTSM
overwrite : bool
flag for overwriting if the file already exists
"""
self.create_domain = create_domain
self.create_surfdata = create_surfdata
self.create_landuse = create_landuse
self.create_datm = create_datm
self.create_user_mods = create_user_mods
self.overwrite = overwrite

def __str__(self):
"""
Expand Down Expand Up @@ -108,9 +115,10 @@ def create_1d_coord(filename, lon_varname, lat_varname, x_dim, y_dim):
f_out (xarray Dataset): Xarray Dataset with 1-d coords
"""
logger.debug("Open file: %s", filename)

if os.path.exists:
if os.path.exists(filename):
logger.debug("Open file: %s", filename)

f_in = xr.open_dataset(filename)
else:
err_msg = "File not found : " + filename
Expand Down Expand Up @@ -184,3 +192,27 @@ def write_to_file(text, file):
Writes text to a file, surrounding text with \n characters
"""
file.write("\n{}\n".format(text))

def write_to_netcdf (self, xr_ds, nc_fname):
"""
Writes a netcdf file if
- the file does not exist.
or
- overwrite flag is chosen.
Args:
xr_ds : Xarray Dataset
The xarray dataset that we are write out to netcdf file.
nc_fname : str
Netcdf file name
Raises:
Error and aborts the code if the file exists and --overwrite is not used.
"""
if not os.path.exists(nc_fname) or self.overwrite:
# mode 'w' overwrites file
xr_ds.to_netcdf(path=nc_fname, mode="w", format="NETCDF3_64BIT")
else:
err_msg = ("File "+nc_fname+ " already exists."+
"\n Either remove the file or use --overwrite to overwrite the existing files.")
abort (err_msg)

12 changes: 8 additions & 4 deletions python/ctsm/site_and_regional/regional_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class RegionalCase(BaseCase):
flag for creating DATM files
create_user_mods : bool
flag for creating user mods files and folders
overwrite : bool
flag for over-writing files if they already exist
Methods
-------
Expand Down Expand Up @@ -75,12 +78,13 @@ def __init__(
create_datm,
create_user_mods,
out_dir,
overwrite,
):
"""
Initializes RegionalCase with the given arguments.
"""
super().__init__(create_domain, create_surfdata, create_landuse, create_datm,
create_user_mods)
create_user_mods, overwrite)
self.lat1 = lat1
self.lat2 = lat2
self.lon1 = lon1
Expand Down Expand Up @@ -129,7 +133,7 @@ def create_domain_at_reg(self, indir, file):

# mode 'w' overwrites file
wfile = os.path.join(self.out_dir, fdomain_out)
f_out.to_netcdf(path=wfile, mode="w", format='NETCDF3_64BIT')
self.write_to_netcdf (f_out, wfile)
logger.info("Successfully created file (fdomain_out) %s", wfile)
f_in.close()
f_out.close()
Expand Down Expand Up @@ -163,7 +167,7 @@ def create_surfdata_at_reg(self, indir, file, user_mods_dir):

# mode 'w' overwrites file
wfile = os.path.join(self.out_dir, fsurf_out)
f_out.to_netcdf(path=wfile, mode="w", format='NETCDF3_64BIT')
self.write_to_netcdf (f_out, wfile)
logger.info("created file (fsurf_out) %s", wfile)
f_in.close()
f_out.close()
Expand Down Expand Up @@ -205,7 +209,7 @@ def create_landuse_at_reg(self, indir, file, user_mods_dir):

# mode 'w' overwrites file
wfile = os.path.join(self.out_dir, fluse_out)
f_out.to_netcdf(path=wfile, mode="w", format='NETCDF3_64BIT')
self.write_to_netcdf (f_out, wfile)
logger.info("Successfully created file (fluse_out) %s", wfile)
f_in.close()
f_out.close()
Expand Down
20 changes: 10 additions & 10 deletions python/ctsm/site_and_regional/single_point_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class SinglePointCase(BaseCase):
flag for creating datasets using uniform snowpack
saturation_excess : bool
flag for making dataset using saturation excess
overwrite : bool
flag for over-writing files if they already exist
Methods
-------
Expand Down Expand Up @@ -107,9 +109,10 @@ def __init__(
uni_snow,
cap_saturation,
out_dir,
overwrite,
):
super().__init__(create_domain, create_surfdata, create_landuse, create_datm,
create_user_mods)
create_user_mods, overwrite)
self.plat = plat
self.plon = plon
self.site_name = site_name
Expand Down Expand Up @@ -310,7 +313,7 @@ def create_domain_at_point(self, indir, file):
f_out.attrs["Created_from"] = fdomain_in

wfile = os.path.join(self.out_dir, fdomain_out)
f_out.to_netcdf(path=wfile, mode="w", format="NETCDF3_64BIT")
self.write_to_netcdf (f_out, wfile)
logger.info("Successfully created file (fdomain_out) %s", wfile)
f_in.close()
f_out.close()
Expand Down Expand Up @@ -354,8 +357,7 @@ def create_landuse_at_point(self, indir, file, user_mods_dir):
f_out.attrs["Created_from"] = fluse_in

wfile = os.path.join(self.out_dir, fluse_out)
# mode 'w' overwrites file
f_out.to_netcdf(path=wfile, mode="w", format="NETCDF3_64BIT")
self.write_to_netcdf (f_out, wfile)
logger.info("Successfully created file (fluse_out), %s", wfile)
f_in.close()
f_out.close()
Expand Down Expand Up @@ -469,9 +471,9 @@ def create_surfdata_at_point(self, indir, file, user_mods_dir):
# update attributes
self.update_metadata(f_out)
f_out.attrs["Created_from"] = fsurf_in
# mode 'w' overwrites file

wfile = os.path.join(self.out_dir, fsurf_out)
f_out.to_netcdf(path=wfile, mode="w", format="NETCDF3_64BIT")
self.write_to_netcdf (f_out, wfile)
logger.info("Successfully created file (fsurf_out) %s", wfile)
f_in.close()
f_out.close()
Expand Down Expand Up @@ -510,9 +512,8 @@ def create_datmdomain_at_point(self, datm_tuple: DatmFiles):
self.update_metadata(f_out)
f_out.attrs["Created_from"] = fdatmdomain_in

# mode 'w' overwrites file
wfile = os.path.join(self.out_dir, fdatmdomain_out)
f_out.to_netcdf(path=wfile, mode="w", format = 'NETCDF3_64BIT')
self.write_to_netcdf (f_out, wfile)
logger.info("Successfully created file (fdatmdomain_out) : %s", wfile)
f_in.close()
f_out.close()
Expand All @@ -537,8 +538,7 @@ def extract_datm_at(self, file_in, file_out):
self.update_metadata(f_out)
f_out.attrs["Created_from"] = file_in

# mode 'w' overwrites file
f_out.to_netcdf(path=file_out, mode="w")
self.write_to_netcdf (f_out, file_out)
logger.info("Successfully created file : %s", file_out)
f_in.close()
f_out.close()
Expand Down
8 changes: 8 additions & 0 deletions python/ctsm/subset_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ def get_parser():
type=str,
default="",
)
subparser.add_argument(
"--overwrite",
help="Flag to overwrite if the files already exists.",
action="store_true",
dest="overwrite",
)
add_logging_args(subparser)

# -- print help for both subparsers
Expand Down Expand Up @@ -440,6 +446,7 @@ def subset_point(args, file_dict: dict):
uni_snow = args.uni_snow,
cap_saturation = args.cap_saturation,
out_dir = args.out_dir,
overwrite = args.overwrite,
)

logger.debug(single_point)
Expand Down Expand Up @@ -496,6 +503,7 @@ def subset_region(args, file_dict: dict):
create_datm = args.create_datm,
create_user_mods = args.create_user_mods,
out_dir = args.out_dir,
overwrite = args.overwrite,
)

logger.debug(region)
Expand Down

0 comments on commit dbc48b7

Please sign in to comment.