Question
How can I extract spatially varying scalar data from a dfs2 file to match a model setup using another projection?
Answer
Use MIKE SDK to create a small tool that can extract the values in a specific area, interpolate and save the scalar data to a new file - see example in Fig. 1.
Fig. 1 - Plot showing domain with input area (blue palette) and output area (rainbow palette)
The input data is defined in LONG/LAT and covers a large area (input.dfs2). The output data is derived in a smaller area, defined by a grid with the origin (50.632, -2.0114) oriented -8.555 deg.N. The grid data is saved in WGS_1984_UTM_Zone_32N using 119 x 104 grid cells with a grid spacing of 16668 m in both directions.
The source code for extracting the data to output.dfs2 looks as follows in the file Reproject.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using DHI.Generic.MikeZero.DFS;
using DHI.Generic.MikeZero.DFS.dfs123;
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(22, DHI.Mike.Install.MikeProducts.Mike1D))
throw new Exception("Could not find a MIKE installation");
}
/// <summary>
/// Main method, Extract spatially varying data from a dfs2 to match another projection
/// </summary>
public static void Main(string[] args)
{
string sourceFilename = "Input.dfs2";
string targetFilename = "Output.dfs2";
string targetProj = @"PROJCS[""WGS_1984_UTM_Zone_32N"",GEOGCS[""GCS_WGS_1984"",DATUM[""D_WGS_1984"",SPHEROID[""WGS_1984"",6378137,298.257223563]],PRIMEM[""Greenwich"",0],UNIT[""Degree"",0.017453292519943295]],PROJECTION[""Transverse_Mercator""],PARAMETER[""False_Easting"",500000],PARAMETER[""False_Northing"",0],PARAMETER[""Central_Meridian"",9],PARAMETER[""Scale_Factor"",0.9996],PARAMETER[""Latitude_Of_Origin"",0],UNIT[""Meter"",1]]";
Dfs2File dfs2source = DfsFileFactory.Dfs2FileOpen(sourceFilename);
Dfs2Reprojector reproj = new Dfs2Reprojector(dfs2source, targetFilename);
reproj.Interpolate = true;
reproj.SetTarget(targetProj, -2.0114, 50.632, -8.555, 119, 0, 16668, 104, 0, 16668);
reproj.Process();
// Load new file and ensure axis unit is meter
IDfsFile dfsFile = DfsFileFactory.DfsGenericOpenEdit("projected.dfs2");
foreach (var itemInfo in dfsFile.ItemInfo)
{
itemInfo.SpatialAxis.AxisUnit = eumUnit.eumUmeter;
}
// Close File
dfsFile.Close();
}
}
}
The build statement to create the executionable ‘ReprojectDfs2.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\2024\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" ReprojectDfs2.cs
pause
Note: MIKE Core SDK installation contains the source code for several more examples. Compiled versions of various tools are also available from GitHub at https://github.com/DHI/MIKECore-Examples/releases
FURTHER INFORMATION & USEFUL LINKS
Manuals and User Guides
MIKE Core SDK Documentation Index
Release Notes
MIKE Core SDK Release Note