#!/usr/bin/env python # coding: utf-8 # # Dfsu - Read # In[1]: import mikeio # In[2]: filename = "../tests/testdata/HD2D.dfsu" ds = mikeio.read(filename) ds # In[3]: # to read specific variables ds = mikeio.read(filename, items=["Surface elevation","Current speed"]) ds # In[4]: ds.describe() # Filter in space to the element at our POI, (discrete values, no interpolation) # In[5]: ds.sel(x=606200, y=6905480) # Interpolate in space to the location of our POI # In[6]: ds.interp(x=606200, y=6905480) # In[7]: ds.interp(x=606200, y=6905480).Surface_elevation.plot(); # Convert to a dataframe. # In[8]: df = ds.sel(x=606200, y=6905480).to_dataframe() df.head() # In[9]: df.plot(); # ## Other ways to subset data # Assume that we interested in these 3 points only # In[10]: pt1 = (606200, 6905480) pt2 = (606300, 6905410) pt3 = (606400, 6905520) pts_x = [pt1[0], pt2[0], pt3[0]] pts_y = [pt1[1], pt2[1], pt3[1]] elem_ids = ds.geometry.find_index(pts_x, pts_y) # We can use these element ids either when we select the data from the complete dataset using the method isel() as shown above or already when we read the data from file (particular useful for files larger than memory) # In[11]: ds_pts = mikeio.read(filename, elements=elem_ids) ds_pts # # Create a new dfsu file # # * Subset of items # * Renamed variables # # First inspect the source file: # In[12]: ds = mikeio.read("../tests/testdata/HD2D.dfsu") ds # In[13]: outfilename2 = "HD2D_selected.dfsu" newds = ds[["U velocity", "V velocity"]].rename({'U velocity': 'eastward_sea_water_velocity', 'V velocity': 'northward_sea_water_velocity'}) newds # In[14]: newds.to_dfs(outfilename2) # Read the newly created file to verify the contents. # In[15]: newds2 = mikeio.read(outfilename2) newds2 # # Write mesh from dfsu file # Don't you have the original mesh? No problem - you can re-create it from the dfsu file... # In[16]: outmesh = 'mesh_from_HD2D.mesh' ds.geometry.to_mesh(outmesh) # # Clean up # In[17]: import os os.remove(outfilename2) os.remove(outmesh)