Views:

Question
How can I crop dfsu files?

Answer
In general we can use Data Extraction FM to crop data from an existing dfsu file to make a new and smaller dfsu file. However this tool don’t work for special types of dfsu files.
It is possible to use MIKE Core SDK to create a small tool that reads the whole dfsu file and copies only a user-defined number of time steps from the start of the simulation to a new file. By using generic functionality this tool will also work for decoupled flux files.

The CSharp source code for the tool may look as follows in CropDfsuFile.cs:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using DHI.Generic.MikeZero.DFS;

namespace DHI.Generic.MikeZero.DFSTest
{
  class Program
  {
      /// <summary>
      /// Static constructor, setting up search paths for MIKE assemblies .
      /// </summary>
      static Program()
      {
         // The setup method will make your application find the MIKE assemblies at runtime. 
         if (!DHI.Mike.Install.MikeImport.Setup(21, DHI.Mike.Install.MikeProducts.Mike1D))
                  throw new Exception("Could not find the correct MIKE installation"); 
    }  

    /// <summary>
    /// Main method, used when compiling as a standalone application.
    /// </summary>
    public static void Main(string[] args)
    {
         string sourceFilename = args[0];       // input dfsu file
         string filename = args[1];                 // output dfsu file
         int NoSteps = Int32.Parse(args[2]);   // last time step number
      
  
       IDfsFile source = DfsFileFactory.DfsGenericOpen(sourceFilename);
  
       IDfsFileInfo fileInfo = source.FileInfo;

         DfsBuilder builder = DfsBuilder.Create(fileInfo.FileTitle, fileInfo.ApplicationTitle, fileInfo.ApplicationVersion);

        

         // Set up the header
         builder.SetDataType(fileInfo.DataType);
         builder.SetGeographicalProjection(fileInfo.Projection);
         builder.SetTemporalAxis(fileInfo.TimeAxis);
         builder.SetItemStatisticsType(fileInfo.StatsType);
         builder.DeleteValueByte = fileInfo.DeleteValueByte;
         builder.DeleteValueDouble = fileInfo.DeleteValueDouble;
         builder.DeleteValueFloat = fileInfo.DeleteValueFloat;
         builder.DeleteValueInt = fileInfo.DeleteValueInt;
         builder.DeleteValueUnsignedInt = fileInfo.DeleteValueUnsignedInt;  

         

         // Transfer compression keys - if any.
         if (fileInfo.IsFileCompressed)
         {
           int[] xkey;
           int[] ykey;
           int[] zkey;
           fileInfo.GetEncodeKey(out xkey, out ykey, out zkey);
           builder.SetEncodingKey(xkey, ykey, zkey);
         } 

         // Copy custom blocks - if any
         foreach (IDfsCustomBlock customBlock in fileInfo.CustomBlocks)
         {
           builder.AddCustomBlock(customBlock);
         }

         // Copy dynamic items
         foreach (var itemInfo in source.ItemInfo)
         {
           builder.AddDynamicItem(itemInfo);
         }

        

         // Create file
         builder.CreateFile(filename);

         // Copy static items
         IDfsStaticItem sourceStaticItem;
         while (null != (sourceStaticItem = source.ReadStaticItemNext()))
         {
           builder.AddStaticItem(sourceStaticItem);
         }

         // Get the file
         DfsFile file = builder.GetFile();

         // Copy dynamic item data UNTIL NoSteps+1
         for (int i = 0; i < NoSteps+1; i++)
         {
             for (int j = 0; j < source.ItemInfo.Count; j++)
             {
                 // Read data from existing dfsu
                 IDfsItemData<float> itemData = (IDfsItemData<float>)source.ReadItemTimeStep(j + 1, i);
                 file.WriteItemTimeStepNext(itemData.Time, itemData.Data);
             }
         }    

         source.Close();
         file.Close();
    }
  }
}

 

The build statement to create the executionable ‘CropDfsuFile.exe’ looks as follows:
 

set csc=C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
set sdkBin=C:\Program Files (x86)\DHI\MIKE Core SDK\2023\bin\x64
%csc% /r:"%sdkBin%\DHI.Generic.MikeZero.DFS.dll" /r:"%sdkBin%\DHI.Generic.MikeZero.EUM.dll" /r:"%sdkBin%\DHI.Mike.Install.dll" /r:"netstandard.dll" CropDfsuFile.cs
pause

 

 

Example

Assume that you are running a MIKE FM simulation using a number of sub-domains. If the simulation crash, for some reason or another, the individual sub-domains may not have been saved with the same number of time steps. In that case it is not possible to merge the result data to one file (see KA-01117).  In order to ensure the individual output file for a given sub-domain contain the same number of time steps, you can use this tool to create a dfsu file with a user specified length.
 

In order to copy a dfsu file from start to a certain time step number, e.g. TS 120, the command would be:

 

CropDfsuFile.exe Sim_DecouplingFlux_p1.dfsu CropFile_DecouplingFlux_p1.dfsu 120

 

Note: The MIKE Core SDK installation contains source code for several more examples. See also DHI.MIKECore.Util on GitHub.

 
 

FURTHER INFORMATION & USEFUL LINKS

Manuals and User Guides

MIKE Core SDK Documentation Index

Release Notes
MIKE Core SDK Release Note

Related Products: MIKE 21/3, MIKE+