mfsetup.fileio module

Functions for reading and writing stuff to disk, and working with file paths.

mfsetup.fileio.add_version_to_fileheader(filename, model_info=None)[source]

Add modflow-setup, flopy and optionally model version info to an existing file header denoted by the comment characters #, !, or //.

mfsetup.fileio.append_csv(filename, df, **kwargs)[source]

Read data from filename, append to dataframe, and write appended dataframe back to filename.

mfsetup.fileio.check_source_files(fileslist)[source]

Check that the files in fileslist exist.

mfsetup.fileio.dump(filename, data)[source]

Write a dictionary to a configuration file.

mfsetup.fileio.dump_json(jsonfile, data)[source]

Write a dictionary to a json file.

mfsetup.fileio.dump_yml(yml_file, data)[source]

Write a dictionary to a yaml file.

mfsetup.fileio.exe_exists(exe_name)[source]
mfsetup.fileio.flopy_mf2005_load(m, load_only=None, forgive=False, check=False)[source]

Execute the code in flopy.modflow.Modflow.load on an existing flopy.modflow.Modflow instance.

mfsetup.fileio.flopy_mf6model_load(simulation, model, strict=True, model_rel_path='.', load_only=None)[source]

Execute the code in flopy.mf6.MFmodel.load_base on an existing instance of MF6model.

mfsetup.fileio.flopy_mfsimulation_load(sim, model, strict=True, load_only=None, verify_data=False)[source]

Execute the code in flopy.mf6.MFSimulation.load on existing instances of flopy.mf6.MFSimulation and flopy.mf6.MF6model

mfsetup.fileio.load(filename)[source]

Load a configuration file.

mfsetup.fileio.load_array(filename, shape=None, nodata=-9999)[source]

Load an array, ensuring the correct shape.

mfsetup.fileio.load_cfg(cfgfile, verbose=False, default_file=None)[source]

This method loads a YAML or JSON configuration file, applies configuration defaults from a default_file if specified, adds the absolute file path of the configuration file to the configuration dictionary, and converts any relative paths in the configuration dictionary to absolute paths, assuming the paths are relative to the configuration file location.

Parameters:
cfgfilestr

Path to MFsetup configuration file (json or yaml)

Returns:
cfgdict

Dictionary of configuration data

Notes

This function is used by the model instance load and setup_from_yaml classmethods, so that configuration defaults can be applied to the simulation and model blocks before they are passed to the flopy simulation constructor and the model constructor.

mfsetup.fileio.load_json(jsonfile)[source]

Convenience function to load a json file; replacing some escaped characters.

mfsetup.fileio.load_modelgrid(filename)[source]

Create a MFsetupGrid instance from model config json file.

mfsetup.fileio.load_yml(yml_file)[source]

Load yaml file into a dictionary.

mfsetup.fileio.read_ggofile(gagefile, model, start_datetime='1970-01-01', keep_only_last_timestep=True)[source]
mfsetup.fileio.read_lak_ggo(f, model, start_datetime='1970-01-01', keep_only_last_timestep=True)[source]
mfsetup.fileio.read_mf6_block(filename, blockname)[source]
mfsetup.fileio.remove_file_header(filename)[source]

Remove the header of a MODFLOW input file, to allow comparison betwee files that have different headers but are otherwise the same, for example.

mfsetup.fileio.save_array(filename, arr, nodata=-9999, **kwargs)[source]

Save and array and print that it was written.

mfsetup.fileio.set_cfg_paths_to_absolute(cfg, config_file_location)[source]
mfsetup.fileio.setup_external_filepaths(model, package, variable_name, filename_format, file_numbers=None, relative_external_paths=True)[source]

Set up external file paths for a MODFLOW package variable. Sets paths for intermediate files, which are written from the (processed) source data. Intermediate files are supplied to Flopy as external files for a given package variable. Flopy writes external files to a specified location when the MODFLOW package file is written. This method gets the external file paths that will be written by FloPy, and puts them in the configuration dictionary under their respective variables.

Parameters:
modelmfsetup.MF6model or mfsetup.MFnwtModel instance

Model with cfg attribute to update.

packagestr

Three-letter package abreviation (e.g. ‘DIS’ for discretization)

variable_namestr

FloPy name of variable represented by external files (e.g. ‘top’ or ‘botm’)

filename_formatstr

File path to the external file(s). Can be a string representing a single file (e.g. ‘top.dat’), or for variables where a file is written for each layer or stress period, a format string that will be formated with the zero-based layer number (e.g. ‘botm{}.dat’) for files botm0.dat, botm1.dat, …

file_numberslist of ints

List of numbers for the external files. Usually these represent zero-based layers or stress periods.

Returns:
filepathslist

List of external file paths

Adds intermediated file paths to model.cfg[<package>][‘intermediate_data’]
For MODFLOW-6 models, Adds external file paths to model.cfg[<package>][<variable_name>]
mfsetup.fileio.which(program)[source]

Check for existance of executable. https://stackoverflow.com/questions/377017/test-if-executable-exists-in-python