def flopy_mf6model_load(simulation, model, strict=True, model_rel_path='.'): """Execute the code in flopy.mf6.MFmodel.load_base on an existing instance of MF6model.""" instance = model modelname = model.name structure = model.structure # load name file instance.name_file.load(strict) # order packages vnum = mfstructure.MFStructure().get_version_string() # FIX: Transport - Priority packages maybe should not be hard coded priority_packages = { 'dis{}'.format(vnum): 1, 'disv{}'.format(vnum): 1, 'disu{}'.format(vnum): 1 } packages_ordered = [] package_recarray = instance.simulation_data.mfdata[(modelname, 'nam', 'packages', 'packages')] for item in package_recarray.get_data(): if item[0] in priority_packages: packages_ordered.insert(0, (item[0], item[1], item[2])) else: packages_ordered.append((item[0], item[1], item[2])) # load packages sim_struct = mfstructure.MFStructure().sim_struct instance._ftype_num_dict = {} for ftype, fname, pname in packages_ordered: ftype = ftype[0:-1].lower() if ftype in structure.package_struct_objs or ftype in \ sim_struct.utl_struct_objs: if model_rel_path and model_rel_path != '.': # strip off model relative path from the file path filemgr = simulation.simulation_data.mfpath fname = filemgr.strip_model_relative_path(modelname, fname) if simulation.simulation_data.verbosity_level.value >= \ VerbosityLevel.normal.value: print(' loading package {}...'.format(ftype)) # load package instance.load_package(ftype, fname, pname, strict, None) # load referenced packages if modelname in instance.simulation_data.referenced_files: for ref_file in \ instance.simulation_data.referenced_files[modelname].values(): if (ref_file.file_type in structure.package_struct_objs or ref_file.file_type in sim_struct.utl_struct_objs) and \ not ref_file.loaded: instance.load_package(ref_file.file_type, ref_file.file_name, None, strict, ref_file.reference_path) ref_file.loaded = True # TODO: fix jagged lists where appropriate return instance
def clean_class_string(name): if len(name) > 0: clean_string = name.replace(" ", "_") clean_string = clean_string.replace("-", "_") version = mfstructure.MFStructure().get_version_string() # FIX: remove all numbers if clean_string[-1] == version: clean_string = clean_string[:-1] return clean_string return name
def create_packages(): indent = " " init_string_def = " def __init__(self" # load JSON file file_structure = mfstructure.MFStructure(load_from_dfn_files=True) sim_struct = file_structure.sim_struct # assemble package list of buildable packages package_list = [] package_list.append(( sim_struct.name_file_struct_obj, PackageLevel.sim_level, "", sim_struct.name_file_struct_obj.dfn_list, sim_struct.name_file_struct_obj.file_type, )) for package in sim_struct.package_struct_objs.values(): # add simulation level package to list package_list.append(( package, PackageLevel.sim_level, "", package.dfn_list, package.file_type, )) for package in sim_struct.utl_struct_objs.values(): # add utility packages to list package_list.append(( package, PackageLevel.model_level, "utl", package.dfn_list, package.file_type, )) for model_key, model in sim_struct.model_struct_objs.items(): package_list.append(( model.name_file_struct_obj, PackageLevel.model_level, model_key, model.name_file_struct_obj.dfn_list, model.name_file_struct_obj.file_type, )) for package in model.package_struct_objs.values(): package_list.append(( package, PackageLevel.model_level, model_key, package.dfn_list, package.file_type, )) util_path, tail = os.path.split(os.path.realpath(__file__)) init_file = io.open( os.path.join(util_path, "..", "modflow", "__init__.py"), "w", newline="\n", ) init_file.write("# imports\n") init_file.write("from .mfsimulation import MFSimulation\n") nam_import_string = ( "from .. import mfmodel\nfrom ..data.mfdatautil " "import ListTemplateGenerator, ArrayTemplateGenerator") # loop through packages list for package in package_list: data_structure_dict = {} package_properties = [] init_vars = [] init_param_list = [] set_param_list = [] class_vars = [] template_gens = [] dfn_string = build_dfn_string(package[3]) package_abbr = clean_class_string("{}{}".format( clean_class_string(package[2]), package[0].file_type)).lower() package_name = clean_class_string("{}{}{}".format( clean_class_string(package[2]), package[0].file_prefix, package[0].file_type, )).lower() if package[0].description: doc_string = mfdatautil.MFDocString(package[0].description) else: if package[2]: package_container_text = " within a {} model".format( package[2]) else: package_container_text = "" ds = "Modflow{} defines a {} package" "{}.".format( package_name.title(), package[0].file_type, package_container_text, ) if package[0].file_type == "mvr": # mvr package warning if package[2]: ds = ("{} This package\n can only be used to move " "water between packages within a single model." "\n To move water between models use ModflowMvr" ".".format(ds)) else: ds = ("{} This package can only be used to move\n " "water between two different models. To move " "water between two packages\n in the same " 'model use the "model level" mover package (ex. ' "ModflowGwfmvr).".format(ds)) doc_string = mfdatautil.MFDocString(ds) if package[0].dfn_type == mfstructure.DfnType.exch_file: add_var( init_vars, None, init_param_list, package_properties, doc_string, data_structure_dict, None, "exgtype", "exgtype", build_doc_string( "exgtype", "<string>", "is the exchange type (GWF-GWF or " "GWF-GWT).", indent, ), None, None, True, ) add_var( init_vars, None, init_param_list, package_properties, doc_string, data_structure_dict, None, "exgmnamea", "exgmnamea", build_doc_string( "exgmnamea", "<string>", "is the name of the first model that is " "part of this exchange.", indent, ), None, None, True, ) add_var( init_vars, None, init_param_list, package_properties, doc_string, data_structure_dict, None, "exgmnameb", "exgmnameb", build_doc_string( "exgmnameb", "<string>", "is the name of the second model that is " "part of this exchange.", indent, ), None, None, True, ) init_vars.append( " simulation.register_exchange_file(self)\n") # loop through all blocks for block in package[0].blocks.values(): for data_structure in block.data_structures.values(): # only create one property for each unique data structure name if data_structure.name not in data_structure_dict: tg = add_var( init_vars, class_vars, init_param_list, package_properties, doc_string, data_structure_dict, data_structure.default_value, data_structure.name, data_structure.python_name, data_structure.get_doc_string(79, indent, indent), data_structure.path, data_structure.get_datatype(), False, data_structure.construct_package, data_structure.construct_data, data_structure.parameter_name, set_param_list, ) if tg is not None and tg not in template_gens: template_gens.append(tg) import_string = "from .. import mfpackage" if template_gens: import_string = "{}\nfrom ..data.mfdatautil import" " ".format( import_string) first_string = True for template in template_gens: if first_string: import_string = "{}{}".format(import_string, template) first_string = False else: import_string = "{}, {}".format(import_string, template) # add extra docstrings for additional variables doc_string.add_parameter(" filename : String\n " "File name for this package.") doc_string.add_parameter(" pname : String\n " "Package name for this package.") doc_string.add_parameter(" parent_file : MFPackage\n " "Parent package file that references this " "package. Only needed for\n utility " "packages (mfutl*). For example, mfutllaktab " "package must have \n a mfgwflak " "package parent_file.") # build package builder class string init_vars.append(" self._init_complete = True") init_vars = "\n".join(init_vars) package_short_name = clean_class_string(package[0].file_type).lower() class_def_string = "class Modflow{}(mfpackage.MFPackage):\n".format( package_name.title()) class_def_string = class_def_string.replace("-", "_") class_var_string = ('{}\n package_abbr = "{}"\n _package_type = ' '"{}"\n dfn_file_name = "{}"' "\n".format( "\n".join(class_vars), package_abbr, package[4], package[0].dfn_file_name, )) init_string_full = init_string_def init_string_model = "{}, simulation".format(init_string_def) # add variables to init string doc_string.add_parameter( " loading_package : bool\n " "Do not set this parameter. It is intended " "for debugging and internal\n " "processing purposes only.", beginning_of_list=True, ) if package[1] == PackageLevel.sim_level: doc_string.add_parameter( " simulation : MFSimulation\n " "Simulation that this package is a part " "of. Package is automatically\n " "added to simulation when it is " "initialized.", beginning_of_list=True, ) init_string_full = ("{}, simulation, loading_package=" "False".format(init_string_full)) else: doc_string.add_parameter( " model : MFModel\n " "Model that this package is a part of. " "Package is automatically\n added " "to model when it is initialized.", beginning_of_list=True, ) init_string_full = "{}, model, loading_package=False".format( init_string_full) init_param_list.append("filename=None") init_param_list.append("pname=None") init_param_list.append("parent_file=None") init_string_full = build_init_string(init_string_full, init_param_list) # build init code if package[1] == PackageLevel.sim_level: init_var = "simulation" else: init_var = "model" parent_init_string = (" super(Modflow{}, self)" ".__init__(".format(package_name.title())) spaces = " " * len(parent_init_string) parent_init_string = ('{}{}, "{}", filename, pname,\n{}' "loading_package, parent_file)\n\n" " # set up variables".format( parent_init_string, init_var, package_short_name, spaces)) local_datetime = datetime.datetime.now(datetime.timezone.utc) comment_string = ( "# DO NOT MODIFY THIS FILE DIRECTLY. THIS FILE " + "MUST BE CREATED BY\n# mf6/utils/createpackages.py\n# FILE " + "created on {} UTC".format( local_datetime.strftime("%B %d, %Y %H:%M:%S"))) # assemble full package string package_string = "{}\n{}\n\n\n{}{}\n{}\n{}\n\n{}{}\n{}\n".format( comment_string, import_string, class_def_string, doc_string.get_doc_string(), class_var_string, dfn_string, init_string_full, parent_init_string, init_vars, ) # open new Packages file pb_file = io.open( os.path.join(util_path, "..", "modflow", "mf{}.py".format(package_name)), "w", newline="\n", ) pb_file.write(package_string) if package[2] == "utl" and package_abbr != "utltab": set_param_list.append("filename=filename") set_param_list.append("pname=pname") set_param_list.append("parent_file=self._cpparent") whsp_1 = " " whsp_2 = " " chld_doc_string = (' """\n Utl{}Packages is a container ' "class for the ModflowUtl{} class.\n\n " "Methods\n ----------" "\n".format(package_short_name, package_short_name)) # write out child packages class chld_cls = ("\n\nclass Utl{}Packages(mfpackage.MFChildPackage" "s):\n".format(package_short_name)) chld_var = ' package_abbr = "utl{}packages"\n\n'.format( package_short_name) chld_init = " def initialize(self" chld_init = build_init_string(chld_init, init_param_list[:-1], whsp_1) init_pkg = "\n self._init_package(new_package, filename)" params_init = (" new_package = ModflowUtl{}(" "self._model".format(package_short_name)) params_init = build_init_string(params_init, set_param_list, whsp_2) chld_doc_string = ("{} initialize\n Initializes a new " "ModflowUtl{} package removing any sibling " "child\n packages attached to the same " "parent package. See ModflowUtl{} init\n " " documentation for definition of " "parameters.\n".format(chld_doc_string, package_short_name, package_short_name)) chld_appn = "" params_appn = "" append_pkg = "" if package_abbr != "utlobs": # Hard coded obs no multi-pkg support chld_appn = "\n\n def append_package(self" chld_appn = build_init_string(chld_appn, init_param_list[:-1], whsp_1) append_pkg = ("\n self._append_package(new_package, " "filename)") params_appn = (" new_package = ModflowUtl{}(" "self._model".format(package_short_name)) params_appn = build_init_string(params_appn, set_param_list, whsp_2) chld_doc_string = ("{} append_package\n Adds a " "new ModflowUtl{} package to the container." " See ModflowUtl{}\n init " "documentation for definition of " "parameters.\n".format( chld_doc_string, package_short_name, package_short_name)) chld_doc_string = '{} """\n'.format(chld_doc_string) packages_str = "{}{}{}{}{}{}{}{}{}\n".format( chld_cls, chld_doc_string, chld_var, chld_init, params_init[:-2], init_pkg, chld_appn, params_appn[:-2], append_pkg, ) pb_file.write(packages_str) pb_file.close() init_file.write("from .mf{} import " "Modflow{}\n".format(package_name, package_name.title())) if package[0].dfn_type == mfstructure.DfnType.model_name_file: # build model file model_param_list = init_param_list[:-3] init_vars = build_model_init_vars(model_param_list) model_param_list.insert(0, "model_rel_path='.'") model_param_list.insert(0, "exe_name='mf6.exe'") model_param_list.insert(0, "version='mf6'") model_param_list.insert(0, "model_nam_file=None") model_param_list.insert(0, "modelname='model'") model_param_list.append("**kwargs") init_string_model = build_init_string(init_string_model, model_param_list) model_name = clean_class_string(package[2]) class_def_string = "class Modflow{}(mfmodel.MFModel):\n".format( model_name.capitalize()) class_def_string = class_def_string.replace("-", "_") doc_string.add_parameter( " sim : MFSimulation\n " "Simulation that this model is a part " "of. Model is automatically\n " "added to simulation when it is " "initialized.", beginning_of_list=True, model_parameter=True, ) doc_string.description = "Modflow{} defines a {} model".format( model_name, model_name) class_var_string = " model_type = '{}'\n".format(model_name) mparent_init_string = (" super(Modflow{}, self)" ".__init__(".format( model_name.capitalize())) spaces = " " * len(mparent_init_string) mparent_init_string = ("{}simulation, model_type='{}6',\n{}" "modelname=modelname,\n{}" "model_nam_file=model_nam_file,\n{}" "version=version, exe_name=exe_name,\n{}" "model_rel_path=model_rel_path,\n{}" "**kwargs" ")\n".format( mparent_init_string, model_name, spaces, spaces, spaces, spaces, spaces, )) load_txt, doc_text = build_model_load(model_name) package_string = "{}\n{}\n\n\n{}{}\n{}\n{}\n{}{}\n{}\n\n{}".format( comment_string, nam_import_string, class_def_string, doc_string.get_doc_string(True), doc_text, class_var_string, init_string_model, mparent_init_string, init_vars, load_txt, ) md_file = io.open( os.path.join(util_path, "..", "modflow", "mf{}.py".format(model_name)), "w", newline="\n", ) md_file.write(package_string) md_file.close() init_file.write("from .mf{} import " "Modflow{}\n".format(model_name, model_name.capitalize())) init_file.close()
def create_packages(): indent = ' ' init_string_def = ' def __init__(self' # load JSON file file_structure = mfstructure.MFStructure(load_from_dfn_files=True) sim_struct = file_structure.sim_struct # assemble package list of buildable packages package_list = [] package_list.append( (sim_struct.name_file_struct_obj, PackageLevel.sim_level, '', sim_struct.name_file_struct_obj.dfn_list, sim_struct.name_file_struct_obj.file_type)) for package in sim_struct.package_struct_objs.values(): # add simulation level package to list package_list.append((package, PackageLevel.sim_level, '', package.dfn_list, package.file_type)) for package in sim_struct.utl_struct_objs.values(): # add utility packages to list package_list.append((package, PackageLevel.model_level, 'utl', package.dfn_list, package.file_type)) for model_key, model in sim_struct.model_struct_objs.items(): package_list.append( (model.name_file_struct_obj, PackageLevel.model_level, model_key, model.name_file_struct_obj.dfn_list, model.name_file_struct_obj.file_type)) for package in model.package_struct_objs.values(): package_list.append((package, PackageLevel.model_level, model_key, package.dfn_list, package.file_type)) util_path, tail = os.path.split(os.path.realpath(__file__)) init_file = open(os.path.join(util_path, '..', 'modflow', '__init__.py'), 'w') init_file.write('# imports\n') init_file.write('from .mfsimulation import MFSimulation\n') nam_import_string = 'from .. import mfmodel\nfrom ..data.mfdatautil ' \ 'import ListTemplateGenerator, ArrayTemplateGenerator' # loop through packages list for package in package_list: data_structure_dict = {} package_properties = [] init_vars = [] init_param_list = [] set_param_list = [] class_vars = [] template_gens = [] dfn_string = build_dfn_string(package[3]) package_abbr = clean_class_string( '{}{}'.format(clean_class_string(package[2]), package[0].file_type)).lower() package_name = clean_class_string( '{}{}{}'.format(clean_class_string(package[2]), package[0].file_prefix, package[0].file_type)).lower() if package[0].description: doc_string = mfdatautil.MFDocString(package[0].description) else: if package[2]: package_container_text = ' within a {} model'.format( package[2]) else: package_container_text = '' doc_string = mfdatautil.MFDocString( 'Modflow{} defines a {} package' '{}.'.format(package_name.title(), package[0].file_type, package_container_text)) if package[0].dfn_type == mfstructure.DfnType.exch_file: add_var(init_vars, None, init_param_list, package_properties, doc_string, data_structure_dict, None, 'exgtype', 'exgtype', build_doc_string('exgtype', '<string>', 'is the exchange type (GWF-GWF or ' 'GWF-GWT).', indent), None, None, True) add_var(init_vars, None, init_param_list, package_properties, doc_string, data_structure_dict, None, 'exgmnamea', 'exgmnamea', build_doc_string('exgmnamea', '<string>', 'is the name of the first model that is ' 'part of this exchange.', indent), None, None, True) add_var(init_vars, None, init_param_list, package_properties, doc_string, data_structure_dict, None, 'exgmnameb', 'exgmnameb', build_doc_string('exgmnameb', '<string>', 'is the name of the second model that is ' 'part of this exchange.', indent), None, None, True) init_vars.append( ' simulation.register_exchange_file(self)\n') # loop through all blocks for block in package[0].blocks.values(): for data_structure in block.data_structures.values(): # only create one property for each unique data structure name if data_structure.name not in data_structure_dict: tg = add_var( init_vars, class_vars, init_param_list, package_properties, doc_string, data_structure_dict, data_structure.default_value, data_structure.name, data_structure.python_name, data_structure.get_doc_string(79, indent, indent), data_structure.path, data_structure.get_datatype(), False, data_structure.construct_package, data_structure.construct_data, data_structure.parameter_name, set_param_list) if tg is not None and tg not in template_gens: template_gens.append(tg) import_string = 'from .. import mfpackage' if template_gens: import_string = '{}\nfrom ..data.mfdatautil import' \ ' '.format(import_string) first_string = True for template in template_gens: if first_string: import_string = '{}{}'.format(import_string, template) first_string = False else: import_string = '{}, {}'.format(import_string, template) # add extra docstrings for additional variables doc_string.add_parameter(' filename : String\n ' 'File name for this package.') doc_string.add_parameter(' pname : String\n ' 'Package name for this package.') doc_string.add_parameter(' parent_file : MFPackage\n ' 'Parent package file that references this ' 'package. Only needed for\n utility ' 'packages (mfutl*). For example, mfutllaktab ' 'package must have \n a mfgwflak ' 'package parent_file.') # build package builder class string init_vars.append(' self._init_complete = True') init_vars = '\n'.join(init_vars) package_short_name = clean_class_string(package[0].file_type).lower() class_def_string = 'class Modflow{}(mfpackage.MFPackage):\n'.format( package_name.title()) class_def_string = class_def_string.replace('-', '_') class_var_string = '{}\n package_abbr = "{}"\n _package_type = ' \ '"{}"\n dfn_file_name = "{}"' \ '\n'.format('\n'.join(class_vars), package_abbr, package[4], package[0].dfn_file_name) init_string_full = init_string_def init_string_model = '{}, simulation'.format(init_string_def) # add variables to init string doc_string.add_parameter(' loading_package : bool\n ' 'Do not set this parameter. It is intended ' 'for debugging and internal\n ' 'processing purposes only.', beginning_of_list=True) if package[1] == PackageLevel.sim_level: doc_string.add_parameter(' simulation : MFSimulation\n ' 'Simulation that this package is a part ' 'of. Package is automatically\n ' 'added to simulation when it is ' 'initialized.', beginning_of_list=True) init_string_full = '{}, simulation, loading_package=' \ 'False'.format(init_string_full) else: doc_string.add_parameter(' model : MFModel\n ' 'Model that this package is a part of. ' 'Package is automatically\n added ' 'to model when it is initialized.', beginning_of_list=True) init_string_full = '{}, model, loading_package=False'.format( init_string_full) init_param_list.append('filename=None') init_param_list.append('pname=None') init_param_list.append('parent_file=None') init_string_full = build_init_string(init_string_full, init_param_list) # build init code if package[1] == PackageLevel.sim_level: init_var = 'simulation' else: init_var = 'model' parent_init_string = ' super(Modflow{}, self)' \ '.__init__('.format(package_name.title()) spaces = ' ' * len(parent_init_string) parent_init_string = '{}{}, "{}", filename, pname,\n{}' \ 'loading_package, parent_file)\n\n' \ ' # set up variables'.format( parent_init_string, init_var, package_short_name, spaces) comment_string = '# DO NOT MODIFY THIS FILE DIRECTLY. THIS FILE ' \ 'MUST BE CREATED BY\n# mf6/utils/createpackages.py' # assemble full package string package_string = '{}\n{}\n\n\n{}{}\n{}\n{}\n\n{}{}\n{}\n'.format( comment_string, import_string, class_def_string, doc_string.get_doc_string(), class_var_string, dfn_string, init_string_full, parent_init_string, init_vars) # open new Packages file pb_file = open(os.path.join(util_path, '..', 'modflow', 'mf{}.py'.format(package_name)), 'w') pb_file.write(package_string) if package[2] == 'utl' and package_abbr != 'utltab': set_param_list.append('filename=filename') set_param_list.append('pname=pname') set_param_list.append('parent_file=self._cpparent') whsp_1 = ' ' whsp_2 = ' ' chld_doc_string = ' """\n Utl{}Packages is a container ' \ 'class for the ModflowUtl{} class.\n\n ' \ 'Methods\n ----------' \ '\n'.format(package_short_name, package_short_name) # write out child packages class chld_cls = '\n\nclass Utl{}Packages(mfpackage.MFChildPackage' \ 's):\n'.format(package_short_name) chld_var = ' package_abbr = "utl{}packages"\n\n'.format( package_short_name) chld_init = ' def initialize(self' chld_init = build_init_string(chld_init, init_param_list[:-1], whsp_1) init_pkg = '\n self._init_package(new_package, filename)' params_init = ' new_package = ModflowUtl{}(' \ 'self._model'.format(package_short_name) params_init = build_init_string(params_init, set_param_list, whsp_2) chld_doc_string = '{} initialize\n Initializes a new ' \ 'ModflowUtl{} package removing any sibling ' \ 'child\n packages attached to the same ' \ 'parent package. See ModflowUtl{} init\n ' \ ' documentation for definition of ' \ 'parameters.\n'.format(chld_doc_string, package_short_name, package_short_name) chld_appn = '' params_appn = '' append_pkg = '' if package_abbr != 'utlobs': # Hard coded obs no multi-pkg support chld_appn = '\n\n def append_package(self' chld_appn = build_init_string(chld_appn, init_param_list[:-1], whsp_1) append_pkg = '\n self._append_package(new_package, ' \ 'filename)' params_appn = ' new_package = ModflowUtl{}(' \ 'self._model'.format(package_short_name) params_appn = build_init_string(params_appn, set_param_list, whsp_2) chld_doc_string = '{} append_package\n Adds a ' \ 'new ModflowUtl{} package to the container.' \ ' See ModflowUtl{}\n init ' \ 'documentation for definition of ' \ 'parameters.\n'.format(chld_doc_string, package_short_name, package_short_name) chld_doc_string = '{} """\n'.format(chld_doc_string) packages_str = '{}{}{}{}{}{}{}{}{}\n'.format(chld_cls, chld_doc_string, chld_var, chld_init, params_init[:-2], init_pkg, chld_appn, params_appn[:-2], append_pkg,) pb_file.write(packages_str) pb_file.close() init_file.write('from .mf{} import ' 'Modflow{}\n'.format(package_name, package_name.title())) if package[0].dfn_type == mfstructure.DfnType.model_name_file: # build model file model_param_list = init_param_list[:-3] init_vars = build_model_init_vars(model_param_list) model_param_list.insert(0, "model_rel_path='.'") model_param_list.insert(0, "exe_name='mf6.exe'") model_param_list.insert(0, "version='mf6'") model_param_list.insert(0, 'model_nam_file=None') model_param_list.insert(0, "modelname='model'") model_param_list.append("**kwargs") init_string_model = build_init_string(init_string_model, model_param_list) model_name = clean_class_string(package[2]) class_def_string = 'class Modflow{}(mfmodel.MFModel):\n'.format( model_name.capitalize()) class_def_string = class_def_string.replace('-', '_') doc_string.add_parameter(' sim : MFSimulation\n ' 'Simulation that this model is a part ' 'of. Model is automatically\n ' 'added to simulation when it is ' 'initialized.', beginning_of_list=True, model_parameter=True) doc_string.description = 'Modflow{} defines a {} model'.format( model_name, model_name) class_var_string = " model_type = '{}'\n".format(model_name) mparent_init_string = ' super(Modflow{}, self)' \ '.__init__('.format(model_name.capitalize()) spaces = ' ' * len(mparent_init_string) mparent_init_string = "{}simulation, model_type='gwf6',\n{}" \ "modelname=modelname,\n{}" \ "model_nam_file=model_nam_file,\n{}" \ "version=version, exe_name=exe_name,\n{}" \ "model_rel_path=model_rel_path,\n{}" \ "**kwargs" \ ")\n".format(mparent_init_string, spaces, spaces, spaces, spaces, spaces) load_txt, doc_text = build_model_load('gwf') package_string = '{}\n{}\n\n\n{}{}\n{}\n{}\n{}{}\n{}\n\n{}'.format( comment_string, nam_import_string, class_def_string, doc_string.get_doc_string(True), doc_text, class_var_string, init_string_model, mparent_init_string, init_vars, load_txt) md_file = open(os.path.join(util_path, '..', 'modflow', 'mf{}.py'.format(model_name)), 'w') md_file.write(package_string) md_file.close() init_file.write('from .mf{} import ' 'Modflow{}\n'.format(model_name, model_name.capitalize())) init_file.close()
def flopy_mfsimulation_load(sim, model, strict=True): """Execute the code in flopy.mf6.MFSimulation.load on existing instances of flopy.mf6.MFSimulation and flopy.mf6.MF6model""" instance = sim if not isinstance(model, list): model_instances = [model] else: model_instances = model version = sim.version exe_name = sim.exe_name verbosity_level = instance.simulation_data.verbosity_level if verbosity_level.value >= VerbosityLevel.normal.value: print('loading simulation...') # load simulation name file if verbosity_level.value >= VerbosityLevel.normal.value: print(' loading simulation name file...') instance.name_file.load(strict) # load TDIS file tdis_pkg = 'tdis{}'.format(mfstructure.MFStructure().get_version_string()) tdis_attr = getattr(instance.name_file, tdis_pkg) instance._tdis_file = mftdis.ModflowTdis(instance, filename=tdis_attr.get_data()) instance._tdis_file._filename = instance.simulation_data.mfdata[( 'nam', 'timing', tdis_pkg)].get_data() if verbosity_level.value >= VerbosityLevel.normal.value: print(' loading tdis package...') instance._tdis_file.load(strict) # load models try: model_recarray = instance.simulation_data.mfdata[('nam', 'models', 'models')] models = model_recarray.get_data() except MFDataException as mfde: message = 'Error occurred while loading model names from the ' \ 'simulation name file.' raise MFDataException(mfdata_except=mfde, model=instance.name, package='nam', message=message) for item in models: # resolve model working folder and name file path, name_file = os.path.split(item[1]) model_obj = [m for m in model_instances if m.namefile == name_file] if len(model_obj) == 0: print('model {} attached to {} not found in {}'.format( item, instance, model_instances)) return model_obj = model_obj[0] #model_obj = PackageContainer.model_factory(item[0][:-1].lower()) # load model if verbosity_level.value >= VerbosityLevel.normal.value: print(' loading model {}...'.format(item[0].lower())) instance._models[item[2]] = flopy_mf6model_load(instance, model_obj, strict=strict, model_rel_path=path) #instance._models[item[2]] = model_obj.load( # instance, # instance.structure.model_struct_objs[item[0].lower()], item[2], # name_file, version, exe_name, strict, path) # # load exchange packages and dependent packages try: exchange_recarray = instance.name_file.exchanges has_exch_data = exchange_recarray.has_data() except MFDataException as mfde: message = 'Error occurred while loading exchange names from the ' \ 'simulation name file.' raise MFDataException(mfdata_except=mfde, model=instance.name, package='nam', message=message) if has_exch_data: try: exch_data = exchange_recarray.get_data() except MFDataException as mfde: message = 'Error occurred while loading exchange names from the ' \ 'simulation name file.' raise MFDataException(mfdata_except=mfde, model=instance.name, package='nam', message=message) for exgfile in exch_data: # get exchange type by removing numbers from exgtype exchange_type = ''.join( [char for char in exgfile[0] if not char.isdigit()]).upper() # get exchange number for this type if not exchange_type in instance._exg_file_num: exchange_file_num = 0 instance._exg_file_num[exchange_type] = 1 else: exchange_file_num = instance._exg_file_num[exchange_type] instance._exg_file_num[exchange_type] += 1 exchange_name = '{}_EXG_{}'.format(exchange_type, exchange_file_num) # find package class the corresponds to this exchange type package_obj = instance.package_factory( exchange_type.replace('-', '').lower(), '') if not package_obj: message = 'An error occurred while loading the ' \ 'simulation name file. Invalid exchange type ' \ '"{}" specified.'.format(exchange_type) type_, value_, traceback_ = sys.exc_info() raise MFDataException(instance.name, 'nam', 'nam', 'loading simulation name file', exchange_recarray.structure.name, inspect.stack()[0][3], type_, value_, traceback_, message, instance._simulation_data.debug) # build and load exchange package object exchange_file = package_obj(instance, exgtype=exgfile[0], exgmnamea=exgfile[2], exgmnameb=exgfile[3], filename=exgfile[1], pname=exchange_name, loading_package=True) if verbosity_level.value >= VerbosityLevel.normal.value: print(' loading exchange package {}..' '.'.format(exchange_file._get_pname())) exchange_file.load(strict) instance._exchange_files[exgfile[1]] = exchange_file # load simulation packages solution_recarray = instance.simulation_data.mfdata[('nam', 'solutiongroup', 'solutiongroup')] try: solution_group_dict = solution_recarray.get_data() except MFDataException as mfde: message = 'Error occurred while loading solution groups from ' \ 'the simulation name file.' raise MFDataException(mfdata_except=mfde, model=instance.name, package='nam', message=message) for solution_group in solution_group_dict.values(): for solution_info in solution_group: ims_file = mfims.ModflowIms(instance, filename=solution_info[1], pname=solution_info[2]) if verbosity_level.value >= VerbosityLevel.normal.value: print(' loading ims package {}..' '.'.format(ims_file._get_pname())) ims_file.load(strict) instance.simulation_data.mfpath.set_last_accessed_path() return instance
def create_packages(): init_string_def = ' def __init__(self' # load JSON file file_structure = mfstructure.MFStructure() sim_struct = file_structure.sim_struct # package_plot_dictionary = file_structure.package_plot_dictionary # assemble package list of buildable packages package_list = [] package_list.append( (sim_struct.name_file_struct_obj, PackageLevel.sim_level, '')) for key, package in sim_struct.package_struct_objs.items(): # add simulation level package to list package_list.append((package, PackageLevel.sim_level, '')) for key, package in sim_struct.utl_struct_objs.items(): # add utility packages to list package_list.append((package, PackageLevel.model_level, 'utl')) for model_key, model in sim_struct.model_struct_objs.items(): package_list.append( (model.name_file_struct_obj, PackageLevel.model_level, model_key)) for key, package in model.package_struct_objs.items(): package_list.append((package, PackageLevel.model_level, model_key)) util_path, tail = os.path.split(os.path.realpath(__file__)) init_file = open(os.path.join(util_path, '..', 'modflow', '__init__.py'), 'w') init_file.write('# imports\n') # loop through packages list for package in package_list: data_structure_dict = {} plotters = [] plotter_defs = [] package_properties = [] init_vars = [] init_param_list = [] class_vars = [] package_name = clean_class_string('{}{}'.format( clean_class_string(package[2]), package[0].file_type)).lower() if package[0].description: doc_string = mfdatautil.MFDocString(package[0].description) else: if package[2]: package_container_text = ' within a {} model'.format( package[2]) else: package_container_text = '' doc_string = mfdatautil.MFDocString( 'Modflow{} defines a {} package' '{}.'.format(package_name.title(), package[0].file_type, package_container_text)) import_string = 'from .. import mfpackage\nfrom ..data import mfdatautil' if package[0].dfn_type == mfstructure.DfnType.exch_file: add_var(init_vars, None, init_param_list, package_properties, doc_string, data_structure_dict, 'exgtype', 'exgtype', '<string>', 'is the exchange type (GWF-GWF or GWF-GWT).', None, None, True) add_var( init_vars, None, init_param_list, package_properties, doc_string, data_structure_dict, 'exgmnamea', 'exgmnamea', '<string>', 'is the name of the first model that is part of this exchange.', None, None, True) add_var( init_vars, None, init_param_list, package_properties, doc_string, data_structure_dict, 'exgmnameb', 'exgmnameb', '<string>', 'is the name of the second model that is part of this exchange.', None, None, True) init_vars.append( ' simulation.register_exchange_file(self)\n') # loop through all blocks for bl_key, block in package[0].blocks.items(): for ds_key, data_structure in block.data_structures.items(): # only create one property for each unique data structure name if data_structure.name not in data_structure_dict: add_var(init_vars, class_vars, init_param_list, package_properties, doc_string, data_structure_dict, data_structure.name, data_structure.python_name, data_structure.get_type_string(), data_structure.get_description(), data_structure.path, data_structure.get_datatype()) # build package builder class string init_vars = '\n'.join(init_vars) plotters_string = ''.join(plotters) plotters_def_string = '\n'.join(plotter_defs) package_short_name = clean_class_string(package[0].file_type).lower() class_def_string = 'class Modflow{}(mfpackage.MFPackage):\n'.format( package_name.title()) class_def_string = class_def_string.replace('-', '_') class_var_string = '{}\n package_abbr = "{}"'.format( '\n'.join(class_vars), package_name) line_chars = len(init_string_def) init_string_full = init_string_def # add variables to init string if package[1] == PackageLevel.sim_level: init_string_full = '{}, simulation, add_to_package_list=True'.format( init_string_full) else: init_string_full = '{}, model, add_to_package_list=True'.format( init_string_full) init_param_list.append('fname=None') init_param_list.append('pname=None') init_param_list.append('parent_file=None') for param in init_param_list: line_chars += len(param) + 1 if line_chars > 80: init_string_full = '{},\n {}'.format( init_string_full, param) line_chars = len(param) + 1 else: init_string_full = '{}, {}'.format(init_string_full, param) init_string_full = '{}):\n'.format(init_string_full) # build init code if package[1] == PackageLevel.sim_level: init_var = 'simulation' else: init_var = 'model' parent_init_string = ' super(Modflow{}, self).__init__({}, "{}", fname, pname, add_to_package_list,' \ ' parent_file) \n\n' \ ' # set up variables'.format( package_name.title(), init_var, package_short_name) # assemble full package string package_string = '{}\n\n\n{}{}\n{}\n\n{}{}{}\n{}\n\n{}'.format( import_string, class_def_string, doc_string.get_doc_string(), class_var_string, init_string_full, parent_init_string, plotters_string, init_vars, plotters_def_string) # open new Packages file pb_file = open( os.path.join(util_path, '..', 'modflow', 'mf{}.py'.format(package_name)), 'w') pb_file.write(package_string) pb_file.close() init_file.write('from .mf{} import Modflow{}\n'.format( package_name, package_name.title())) init_file.close()
def create_packages(): indent = ' ' init_string_def = ' def __init__(self' # load JSON file file_structure = mfstructure.MFStructure(load_from_dfn_files=True) sim_struct = file_structure.sim_struct # assemble package list of buildable packages package_list = [] package_list.append( (sim_struct.name_file_struct_obj, PackageLevel.sim_level, '', sim_struct.name_file_struct_obj.dfn_list, sim_struct.name_file_struct_obj.file_type)) for key, package in sim_struct.package_struct_objs.items(): # add simulation level package to list package_list.append((package, PackageLevel.sim_level, '', package.dfn_list, package.file_type)) for key, package in sim_struct.utl_struct_objs.items(): # add utility packages to list package_list.append((package, PackageLevel.model_level, 'utl', package.dfn_list, package.file_type)) for model_key, model in sim_struct.model_struct_objs.items(): package_list.append( (model.name_file_struct_obj, PackageLevel.model_level, model_key, model.name_file_struct_obj.dfn_list, model.name_file_struct_obj.file_type)) for key, package in model.package_struct_objs.items(): package_list.append((package, PackageLevel.model_level, model_key, package.dfn_list, package.file_type)) util_path, tail = os.path.split(os.path.realpath(__file__)) init_file = open(os.path.join(util_path, '..', 'modflow', '__init__.py'), 'w') init_file.write('# imports\n') # loop through packages list for package in package_list: data_structure_dict = {} package_properties = [] init_vars = [] init_param_list = [] class_vars = [] dfn_string = build_dfn_string(package[3]) package_abbr = clean_class_string('{}{}'.format( clean_class_string(package[2]), package[0].file_type)).lower() package_name = clean_class_string('{}{}{}'.format( clean_class_string(package[2]), package[0].file_prefix, package[0].file_type)).lower() if package[0].description: doc_string = mfdatautil.MFDocString(package[0].description) else: if package[2]: package_container_text = ' within a {} model'.format( package[2]) else: package_container_text = '' doc_string = mfdatautil.MFDocString( 'Modflow{} defines a {} package' '{}.'.format(package_name.title(), package[0].file_type, package_container_text)) import_string = 'from .. import mfpackage\nfrom ..data.mfdatautil ' \ 'import ListTemplateGenerator, ArrayTemplateGenerator' if package[0].dfn_type == mfstructure.DfnType.exch_file: add_var( init_vars, None, init_param_list, package_properties, doc_string, data_structure_dict, None, 'exgtype', 'exgtype', build_doc_string( 'exgtype', '<string>', 'is the exchange type (GWF-GWF or ' 'GWF-GWT).', indent), None, None, True) add_var( init_vars, None, init_param_list, package_properties, doc_string, data_structure_dict, None, 'exgmnamea', 'exgmnamea', build_doc_string( 'exgmnamea', '<string>', 'is the name of the first model that is ' 'part of this exchange.', indent), None, None, True) add_var( init_vars, None, init_param_list, package_properties, doc_string, data_structure_dict, None, 'exgmnameb', 'exgmnameb', build_doc_string( 'exgmnameb', '<string>', 'is the name of the second model that is ' 'part of this exchange.', indent), None, None, True) init_vars.append( ' simulation.register_exchange_file(self)\n') # loop through all blocks for bl_key, block in package[0].blocks.items(): for ds_key, data_structure in block.data_structures.items(): # only create one property for each unique data structure name if data_structure.name not in data_structure_dict: add_var(init_vars, class_vars, init_param_list, package_properties, doc_string, data_structure_dict, data_structure.default_value, data_structure.name, data_structure.python_name, data_structure.get_doc_string(79, indent, indent), data_structure.path, data_structure.get_datatype()) # add extra docstrings for additional variables doc_string.add_parameter(' fname : String\n ' 'File name for this package.') doc_string.add_parameter(' pname : String\n ' 'Package name for this package.') doc_string.add_parameter(' parent_file : MFPackage\n ' 'Parent package file that references this ' 'package. Only needed for\n utility ' 'packages (mfutl*). For example, mfutllaktab ' 'package must have \n a mfgwflak ' 'package parent_file.') # build package builder class string init_vars = '\n'.join(init_vars) package_short_name = clean_class_string(package[0].file_type).lower() class_def_string = 'class Modflow{}(mfpackage.MFPackage):\n'.format( package_name.title()) class_def_string = class_def_string.replace('-', '_') class_var_string = '{}\n package_abbr = "{}"\n package_type = ' \ '"{}"\n dfn_file_name = "{}"' \ '\n'.format('\n'.join(class_vars), package_abbr, package[4], package[0].dfn_file_name) init_string_full = init_string_def # add variables to init string doc_string.add_parameter( ' loading_package : bool\n ' 'Do not set this parameter. It is intended ' 'for debugging and internal\n ' 'processing purposes only.', beginning_of_list=True) if package[1] == PackageLevel.sim_level: doc_string.add_parameter( ' simulation : MFSimulation\n ' 'Simulation that this package is a part ' 'of. Package is automatically\n ' 'added to simulation when it is ' 'initialized.', beginning_of_list=True) init_string_full = '{}, simulation, loading_package=' \ 'False'.format(init_string_full) else: doc_string.add_parameter( ' model : MFModel\n ' 'Model that this package is a part of. ' 'Package is automatically\n added ' 'to model when it is initialized.', beginning_of_list=True) init_string_full = '{}, model, loading_package=False'.format( init_string_full) line_chars = len(init_string_full) init_param_list.append('fname=None') init_param_list.append('pname=None') init_param_list.append('parent_file=None') for index, param in enumerate(init_param_list): if index + 1 < len(init_param_list): line_chars += len(param) + 2 else: line_chars += len(param) + 3 if line_chars > 79: init_string_full = '{},\n {}'.format( init_string_full, param) line_chars = len(param) + len(' ') + 1 else: init_string_full = '{}, {}'.format(init_string_full, param) init_string_full = '{}):\n'.format(init_string_full) # build init code if package[1] == PackageLevel.sim_level: init_var = 'simulation' else: init_var = 'model' parent_init_string = ' super(Modflow{}, self)' \ '.__init__('.format(package_name.title()) spaces = ' ' * len(parent_init_string) parent_init_string = '{}{}, "{}", fname, pname,\n{}' \ 'loading_package, parent_file) \n\n' \ ' # set up variables'.format( parent_init_string, init_var, package_short_name, spaces) comment_string = '# DO NOT MODIFY THIS FILE DIRECTLY. THIS FILE ' \ 'MUST BE CREATED BY\n# mf6/utils/createpackages.py' # assemble full package string package_string = '{}\n{}\n\n\n{}{}\n{}\n{}\n\n{}{}\n{}\n'.format( comment_string, import_string, class_def_string, doc_string.get_doc_string(), class_var_string, dfn_string, init_string_full, parent_init_string, init_vars) # open new Packages file pb_file = open( os.path.join(util_path, '..', 'modflow', 'mf{}.py'.format(package_name)), 'w') pb_file.write(package_string) pb_file.close() init_file.write('from .mf{} import ' 'Modflow{}\n'.format(package_name, package_name.title())) init_file.close()