From 4175efe6cad2f2aef8913a19c1cef5d9e588546e Mon Sep 17 00:00:00 2001 From: "Leaf, Andrew T" Date: Thu, 25 Jul 2024 11:18:55 -0500 Subject: [PATCH] wip: incorporating AP's fixes --- mfsetup/sourcedata.py | 103 ++++++++++++++++++++++-------------------- 1 file changed, 55 insertions(+), 48 deletions(-) diff --git a/mfsetup/sourcedata.py b/mfsetup/sourcedata.py index acfa0672..c9bee8ee 100644 --- a/mfsetup/sourcedata.py +++ b/mfsetup/sourcedata.py @@ -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: @@ -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,