Skip to content

Commit

Permalink
fix(sourcedata.py::setup_array): only make adjustments involving the …
Browse files Browse the repository at this point in the history
…original model top in a model creation context (not when working with a loaded model)
  • Loading branch information
aleaf committed May 7, 2024
1 parent 15b5ad7 commit 5c98f7a
Showing 1 changed file with 45 additions and 39 deletions.
84 changes: 45 additions & 39 deletions mfsetup/sourcedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1378,43 +1378,52 @@ def setup_array(model, package, var, data=None,
# (for lakes)
simulate_high_k_lakes = model.cfg['high_k_lakes']['simulate_high_k_lakes']
if var == 'botm':
bathy = model.lake_bathymetry
# 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")
# 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']:
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)
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'])
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]
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 @@ -1467,7 +1476,6 @@ def setup_array(model, package, var, data=None,
if not isvalid:
raise Exception('Model layers less than {} {} thickness'.format(min_thickness,
model.length_units))

# fill nan values adjacent to active cells to avoid cell thickness errors
top, botm = fill_cells_vertically(top, botm)
# the top may have been modified by fill_cells_vertically
Expand Down Expand Up @@ -1512,21 +1520,19 @@ def setup_array(model, package, var, data=None,
elif var == 'ss' and simulate_high_k_lakes:
for i, arr in data.items():
data[i][model.isbc[i] == 2] = model.cfg['high_k_lakes']['ss']

# intermediate data
# set paths to intermediate files and external files
filepaths = model.setup_external_filepaths(package, var,
model.cfg[package]['{}_filename_fmt'.format(var)],
file_numbers=list(data.keys()))

# write out array data to intermediate files
# assign lake recharge values (water balance surplus) for any high-K lakes
if write_nodata is None:
write_nodata = model._nodata_value
for i, arr in data.items():
save_array(filepaths[i], arr,
nodata=write_nodata,
fmt=write_fmt)
nodata=write_nodata,
fmt=write_fmt)
# still write intermediate files for MODFLOW-6
# even though input and output filepaths are same
if model.version == 'mf6':
Expand Down

0 comments on commit 5c98f7a

Please sign in to comment.