Skip to content

Conversation

ecomodeller
Copy link
Member

@ecomodeller ecomodeller commented Jun 19, 2025

This PR adds the possibility to create derived variables without reading the entire file into memory.

The proposed transform function operates on data from a single timestep.

import mikeio
from mikeio.generic import transform, DerivedItem
# calc current speed from u,v
derived = [
      DerivedItem(
          item=ItemInfo("Current Speed", mikeio.EUMType.Current_Speed),
          func=lambda x: np.sqrt(x["U velocity"] ** 2 + x["V velocity"] ** 2),
      ),
 # add more items, e.g. direction
]
# include existing items except Total Water Depth
infilename = "HD.dfsu"
dfs1 = mikeio.Dfsu2DH(infilename)
sel_items = [
      DerivedItem(item=item)
      for item in dfs1.items
      if item.name != "Total Water Depth"
]
sel_items.extend(derived)

# Transform
transform(infilename, "transformed.dfsu", vars=sel_items)

The default is to append derived items to the list of existing items, controlled by the keep_existing_items flag.

@ecomodeller
Copy link
Member Author

@buchmann-dhi we are trying to work out a simple syntax for creating derived items with the ambition of being flexible but still a terse and readable syntax. Any suggestions?

@ecomodeller ecomodeller marked this pull request as ready for review June 23, 2025 10:09
@ecomodeller ecomodeller requested a review from ryan-kipawa June 23, 2025 13:16
Copy link
Contributor

@ryan-kipawa ryan-kipawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like an easy to use syntax, nice documentation, and great typing. My only comment is in regards to whether files with many timesteps are performant. That could also be an improvement later on (if it's an issue)...


n_time_steps = dfs_i.FileInfo.TimeAxis.NumberOfTimeSteps

for timestep in range(n_time_steps):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you checked performance on files with many time steps?

Copy link
Member Author

@ecomodeller ecomodeller Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. But this is the same type of loop we have for all dfs files except dfs0. I.e. I would not use use this function to transform dfs0 files, which generally fits in memory.

@ecomodeller ecomodeller merged commit f76e85b into main Jun 24, 2025
8 checks passed
@ecomodeller ecomodeller deleted the derived_variables branch June 24, 2025 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants