Skip to content

Commit

Permalink
wip: incorporating AP's fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aleaf committed Sep 23, 2024
1 parent 91c6792 commit 4175efe
Showing 1 changed file with 55 additions and 48 deletions.
103 changes: 55 additions & 48 deletions mfsetup/sourcedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1381,53 +1381,60 @@ def setup_array(model, package, var, data=None,
# special handling of some variables
# (for lakes)
simulate_high_k_lakes = model.cfg['high_k_lakes']['simulate_high_k_lakes']
if var == 'botm' and simulate_high_k_lakes:
# only execute this code if building the model (not loading)
if not model._load:
bathy = model.lake_bathymetry

# save a copy of original top elevations
# (prior to adjustment for lake bathymetry)
# name of the copy:
original_top_file = Path(model.external_path,
f"{model.name}_{model.cfg[package]['top_filename_fmt']}.original")

try:
top = model.load_array(original_top_file)
original_top_load_fail = False
except:
original_top_load_fail = True

# if the copy doesn't exist
# (or if the existing file is invalid), make it
if original_top_load_fail:
# if remake_top is False, however,
# there may be no preexisting top file to copy
# first check for a preexisting top file
# get the path and add to intermediate files dict if it's not in there
if 'top' not in model.cfg['intermediate_data']:
apply_lake_bathymetry_to_model_top = False
if var == 'botm':
if 'lak' in model.cfg['model']['packages'] and model.cfg['lak']['source_data']:
# only execute this code if building the model (not loading)
# and if lake bathymetry was supplied
# (bathymetry is loaded from the configuration file input
# when the model.lake_bathymetry property is called for the first time;
# see MFsetupMixin._set_lake_bathymetry)
if not model._load and np.sum(model.lake_bathymetry) != 0:

apply_lake_bathymetry_to_model_top = True
bathy = model.lake_bathymetry

# save a copy of original top elevations
# (prior to adjustment for lake bathymetry)
# name of the copy:
original_top_file = Path(model.external_path,
f"{model.name}_{model.cfg[package]['top_filename_fmt']}.original")

try:
top = model.load_array(original_top_file)
original_top_load_fail = False
except:
original_top_load_fail = True

# if the copy doesn't exist
# (or if the existing file is invalid), make it
if original_top_load_fail:
# if remake_top is False, however,
# there may be no preexisting top file to copy
# first check for a preexisting top file
# get the path and add to intermediate files dict if it's not in there
if 'top' not in model.cfg['intermediate_data']:
model.setup_external_filepaths('dis', 'top',
model.cfg['dis']['top_filename_fmt'])
existing_model_top_file = Path(model.cfg['intermediate_data']['top'][0])
if not existing_model_top_file.exists():
raise ValueError((f"Model top text array file {existing_model_top_file} doesn't exist.\n"
f"If remake_top is False in the dis configuration block, "
f"{existing_model_top_file} needs to have been made previously."))
# copy the preexisting top file
shutil.copy(model.cfg['intermediate_data']['top'][0],
original_top_file)
top = model.load_array(original_top_file)
if model.version == 'mf6':
# reset the model top to the lake bottom
top[bathy != 0] -= bathy[bathy != 0]
# if loading the model; use the model top that was just loaded in
else:
top_filename = model.cfg['dis']['griddata'].get('top')
if top_filename is None:
model.setup_external_filepaths('dis', 'top',
model.cfg['dis']['top_filename_fmt'])
existing_model_top_file = Path(model.cfg['intermediate_data']['top'][0])
if not existing_model_top_file.exists():
raise ValueError((f"Model top text array file {existing_model_top_file} doesn't exist.\n"
f"If remake_top is False in the dis configuration block, "
f"{existing_model_top_file} needs to have been made previously."))
# copy the preexisting top file
shutil.copy(model.cfg['intermediate_data']['top'][0],
original_top_file)
top = model.load_array(original_top_file)
lake_botm_elevations = top[bathy != 0] - bathy[bathy != 0]
if model.version == 'mf6':
# reset the model top to the lake bottom
top[bathy != 0] -= bathy[bathy != 0]
# if loading the model; use the model top that was just loaded in
else:
top_filename = model.cfg['dis']['griddata'].get('top')
if top_filename is None:
model.setup_external_filepaths('dis', 'top',
model.cfg['dis']['top_filename_fmt'])
top = model.load_array(model.cfg['dis']['griddata']['top'][0]['filename'])
model.cfg['dis']['top_filename_fmt'])
top = model.load_array(model.cfg['dis']['griddata']['top'][0]['filename'])

# fill missing layers if any
if len(data) < model.nlay:
Expand Down Expand Up @@ -1605,9 +1612,9 @@ def setup_array(model, package, var, data=None,

# write the top array again, because top was filled
# with botm array above
# NOTE: that tends to corrupt the top array,
# NOTE: that tends to corrupt the top array,
# is that only applicable to simulate_high_k_lakes ?)
if var == 'botm' and simulate_high_k_lakes:
if var == 'botm' and apply_lake_bathymetry_to_model_top:
top_filepath = model.setup_external_filepaths(package, 'top',
model.cfg[package]['top_filename_fmt'])[0]
save_array(top_filepath, top,
Expand Down

0 comments on commit 4175efe

Please sign in to comment.