imod.util - Miscellaneous Utilities

Miscellaneous Utilities.

Conventional IDF filenames can be understood and constructed using imod.util.decompose() and imod.util.compose(). These are used automatically in imod.idf().

Furthermore there are some utility functions for dealing with the spatial location of rasters: imod.util.coord_reference(), imod.util.spatial_reference() and imod.util.transform(). These are used internally, but are not private since they may be useful to users as well.

imod.util.cd(path)[source]

Change directory, and change it back after the with block.

Examples

>>> with imod.util.cd("docs"):
        do_something_in_docs()
imod.util.compose(d, pattern=None)[source]

From a dict of parts, construct a filename, following the iMOD conventions. Returns a pathlib.Path.

imod.util.coord_reference(da_coord)[source]

Extracts dx, xmin, xmax for a coordinate DataArray, where x is any coordinate.

If the DataArray coordinates are nonequidistant, dx will be returned as 1D ndarray instead of float.

Parameters

a (xarray.DataArray of a coordinate) –

Returns

(dx, xmin, xmax) for a coordinate x

Return type

tuple

imod.util.decompose(path, pattern=None)[source]

Parse a path, returning a dict of the parts, following the iMOD conventions.

Parameters
  • path (str or pathlib.Path) – Path to the file. Upper case is ignored.

  • pattern (str, regex pattern, optional) – If the path is not made up of standard paths, and the default decompose does not produce the right result, specify the used pattern here. See the examples below.

Returns

d – Dictionary with name of variable and dimensions

Return type

dict

Examples

Decompose a path, relying on default conventions:

>>> decompose("head_20010101_l1.idf")

Do the same, by specifying a format string pattern, excluding extension:

>>> decompose("head_20010101_l1.idf", pattern="{name}_{time}_l{layer}")

This supports an arbitrary number of variables:

>>> decompose("head_slr_20010101_l1.idf", pattern="{name}_{scenario}_{time}_l{layer}")

The format string pattern will only work on tidy paths, where variables are separated by underscores. You can also pass a compiled regex pattern. Make sure to include the re.IGNORECASE flag since all paths are lowered.

>>> import re
>>> pattern = re.compile(r"(?P<name>[\w]+)L(?P<layer>[\d+]*)")
>>> decompose("headL11", pattern=pattern)

However, this requires constructing regular expressions, which is generally a fiddly process. The website https://regex101.com is a nice help. Alternatively, the most pragmatic solution may be to just rename your files.

imod.util.spatial_reference(a)[source]

Extracts spatial reference from DataArray.

If the DataArray coordinates are nonequidistant, dx and dy will be returned as 1D ndarray instead of float.

Parameters

a (xarray.DataArray) –

Returns

(dx, xmin, xmax, dy, ymin, ymax)

Return type

tuple

imod.util.to_datetime(s)[source]
imod.util.transform(a)[source]

Extract the spatial reference information from the DataArray coordinates, into an affine.Affine object for writing to e.g. rasterio supported formats.

Parameters

a (xarray.DataArray) –

Returns

Return type

affine.Affine