imod.idf.open#

imod.idf.open(path, use_cftime=False, pattern=None)[source]#

Open one or more IDF files as an xarray.DataArray.

In accordance with xarray’s design, open loads the data of IDF files lazily. This means the data of the IDFs are not loaded into memory until the data is needed. This allows for easier handling of large datasets, and more efficient computations.

Parameters
  • path (str, Path or list) – This can be a single file, ‘head_l1.idf’, a glob pattern expansion, ‘head_l*.idf’, or a list of files, [‘head_l1.idf’, ‘head_l2.idf’]. Note that each file needs to be of the same name (part before the first underscore) but have a different layer and/or timestamp, such that they can be combined in a single xarray.DataArray.

  • use_cftime (bool, optional) –

    Use cftime.DatetimeProlepticGregorian instead of np.datetime64[ns] for the time axis.

    Dates are normally encoded as np.datetime64[ns]; however, if dates fall before 1678 or after 2261, they are automatically encoded as cftime.DatetimeProlepticGregorian objects rather than np.datetime64[ns].

  • pattern (str, regex pattern, optional) – If the filenames do match default naming conventions of {name}_{time}_l{layer}, a custom pattern can be defined here either as a string, or as a compiled regular expression pattern. See the examples below.

Returns

A float xarray.DataArray of the values in the IDF file(s). All metadata needed for writing the file to IDF or other formats using imod.rasterio are included in the xarray.DataArray.attrs.

Return type

xarray.DataArray

Examples

Open an IDF file:

>>> da = imod.idf.open("example.idf")

Open an IDF file, relying on default naming conventions to identify layer:

>>> da = imod.idf.open("example_l1.idf")

Open an IDF file, relying on default naming conventions to identify layer and time:

>>> head = imod.idf.open("head_20010101_l1.idf")

To ignore the naming conventions, specify pattern="{name}". This will disable parsing of the filename into xarray coordinates.

>>> head = imod.idf.open("head_20010101_l1.idf", pattern="{name}")

Open multiple IDF files, in this case files for the year 2001 for all layers, again relying on default conventions for naming:

>>> head = imod.idf.open("head_2001*_l*.idf")

The same, this time explicitly specifying name, time, and layer:

>>> head = imod.idf.open("head_2001*_l*.idf", pattern="{name}_{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+]*)", re.IGNORECASE)
>>> head = imod.idf.open("headL11", pattern=pattern)

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