DataArray

The DataArray is the common MIKE IO data structure for item data from dfs files. The mikeio.read methods returns a Dataset as a container of DataArrays (Dfs items)

Each DataArray have the following properties:

Use DataArray’s string representation to get an overview of the DataArray

import mikeio

ds = mikeio.read("../data/HD2D.dfsu")
da = ds["Surface elevation"]
da
<mikeio.DataArray>
name: Surface elevation
dims: (time:9, element:884)
time: 1985-08-06 07:00:00 - 1985-08-07 03:00:00 (9 records)
geometry: Dfsu2D (884 elements, 529 nodes)

Temporal selection

da.sel(time="1985-08-06 12:00")
<mikeio.DataArray>
name: Surface elevation
dims: (element:884)
time: 1985-08-06 12:00:00 (time-invariant)
geometry: Dfsu2D (884 elements, 529 nodes)
values: [0.1012, 0.1012, ..., 0.105]
da["1985-8-7":]
<mikeio.DataArray>
name: Surface elevation
dims: (time:2, element:884)
time: 1985-08-07 00:30:00 - 1985-08-07 03:00:00 (2 records)
geometry: Dfsu2D (884 elements, 529 nodes)

Spatial selection

The sel method finds the nearest element.

da.sel(x=607002, y=6906734)
<mikeio.DataArray>
name: Surface elevation
dims: (time:9)
time: 1985-08-06 07:00:00 - 1985-08-07 03:00:00 (9 records)
geometry: GeometryPoint2D(x=607002.7094112666, y=6906734.833048992)
values: [0.4591, 0.8078, ..., -0.6311]

Modifying values

You can modify the values of a DataArray by changing its values:

da.values[0, 3] = 5.0

If you wish to change values of a subset of the DataArray you should be aware of the difference between a view and a copy of the data. Similar to NumPy, MIKE IO selection method will return a view of the data when using single index and slices, but a copy of the data using fancy indexing (a list of indicies or boolean indexing). Note that prior to release 1.3, MIKE IO would always return a copy.

It is recommended to change the values using values property directly on the original DataArray (like above), but it is also possible to change the values of the original DataArray by working on a subset DataArray if it is selected with single index or slice as explained above.

da_sub = da.isel(time=0)
da_sub.values[:] = 5.0    # will change da

Fancy indexing will return a copy and therefore not change the original:

da_sub = da.isel(time=[0,1,2])
da_sub.values[:] = 5.0    # will NOT change da

Plotting

The plotting of a DataArray is context-aware meaning that plotting behaviour depends on the geometry of the DataArray being plotted.

da.plot()

da.plot.contourf()

da.plot.mesh()

See details in the API specification below and in the bottom of the relevant pages e.g. DataArray Plotter Grid2D API.

Properties

The DataArray has several properties:

  • time - Time index
  • geometry - geometry of the data (e.g. spatial.GeometryFM2D)
  • shape - Shape of the data
  • deletevalue - File delete value (NaN value)
da.geometry
Flexible Mesh Geometry: Dfsu2D
number of nodes: 529
number of elements: 884
projection: UTM-29

Methods

DataArray has several useful methods for working with data, including different ways of selecting data:

  • sel() - Select subset along an axis
  • isel() - Select subset along an axis with an integer

Aggregations along an axis

  • mean() - Mean value along an axis
  • nanmean() - Mean value along an axis (NaN removed)
  • max() - Max value along an axis
  • nanmax() - Max value along an axis (NaN removed)
  • min() - Min value along an axis
  • nanmin() - Min value along an axis (NaN removed)
  • aggregate() - Aggregate along an axis
  • quantile() - Quantiles along an axis

Mathematical operations

  • ds + value
  • ds - value
  • ds * value

and + and - between two DataArrays (if number of items and shapes conform):

  • ds1 + ds2
  • ds1 - ds2

Multiply or add scalar

da1 = mikeio.read("../data/oresundHD_run1.dfsu", items="Surface elevation")[0]
da2 = mikeio.read("../data/oresundHD_run2.dfsu", items="Surface elevation")[0]

da1.values.mean()
np.float32(0.18681714)
da1_A = da1 + 1
da1_B = da1 - 1
da1_A.values.mean(), da1_B.values.mean()
(np.float32(1.1868172), np.float32(-0.81318283))
da1_C = da1 * 2
da1_D = da1 / 2
da1_C.values.mean(), da1_D.values.mean()
(np.float32(0.37363428), np.float32(0.09340857))

Difference between two DataArrays

Assume that we have two calibration runs and we wan’t to find the difference…

da_diff = da1-da2
da_diff.plot(title="Difference");

da_ratio = da1 / da2
da_ratio.plot(title="", label="Ratio", vmin=0.8, vmax=1.2, levels=9, cmap="coolwarm")

Other methods that also return a DataArray:

Conversion:

  • to_xarray() - Convert DataArray to a xarray DataArray (great for Dfs2)
  • to_dfs() - Write DataArray to a Dfs file