Skip to content

Commit

Permalink
Added -dfsDiff and -dfsMerge tools
Browse files Browse the repository at this point in the history
  • Loading branch information
JesperGr committed Jul 12, 2019
1 parent 89ad280 commit 36d1ccd
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 74 deletions.
9 changes: 9 additions & 0 deletions Examples/CSharp/DHI.MikeCore.Util/DHI.MikeCore.Util.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<StartupObject>DHI.MikeCore.Util.Program</StartupObject>
</PropertyGroup>
<ItemGroup>
<Reference Include="DHI.Generic.MikeZero.DFS, Version=17.0.0.0, Culture=neutral, PublicKeyToken=c513450b5d0bf0bf, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand All @@ -53,12 +56,18 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\DfsDiff.cs">
<Link>DfsDiff.cs</Link>
</Compile>
<Compile Include="..\ExamplesDfs2.cs">
<Link>ExamplesDfs2.cs</Link>
</Compile>
<Compile Include="..\ExamplesDfsu.cs">
<Link>ExamplesDfsu.cs</Link>
</Compile>
<Compile Include="..\ExamplesMisc.cs">
<Link>ExamplesMisc.cs</Link>
</Compile>
<Compile Include="..\Extensions.cs">
<Link>Extensions.cs</Link>
</Compile>
Expand Down
129 changes: 111 additions & 18 deletions Examples/CSharp/DHI.MikeCore.Util/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace DHI.MikeCore.Util
{
class Program
{

/// <summary> Static constructor </summary>
static Program()
{
Expand All @@ -25,24 +24,68 @@ static Program()
}

public static readonly string usage =
@"
Usage
@"
Usage:
DHI.MikeCore.Util -[tool] [arguments]
Tools:
"
+ ExamplesDfs2.MaxVelocityFieldUsage
+ ExamplesDfs2.ResampleUsage
+ ExamplesDfsu.ExtractDfsu2DLayerFrom3DUsage
+ ExamplesDfsu.ExtractSubareaDfsu2DUsage
;
";
//+ DfsDiff.UsageCreateDiffFile
//+ ExamplesMisc.UsageMergeDfsFileItems
//+ ExamplesDfs2.UsageMaxVelocityField
//+ ExamplesDfs2.UsageResample
//+ ExamplesDfsu.UsageExtractDfsu2DLayerFrom3D
//+ ExamplesDfsu.UsageExtractSubareaDfsu2D;

static void PrintUsage()
{
Console.Out.WriteLine(usage);

List<string> usages = new List<string>();
usages.Add(DfsDiff.UsageCreateDiffFile);
usages.Add(ExamplesMisc.UsageMergeDfsFileItems);
usages.Add(ExamplesDfs2.UsageMaxVelocityField);
usages.Add(ExamplesDfs2.UsageResample);
usages.Add(ExamplesDfsu.UsageExtractDfsu2DLayerFrom3D);
usages.Add(ExamplesDfsu.UsageExtractSubareaDfsu2D);
foreach (string usage in usages)
{
int positionOfNewLine = usage.IndexOf("\r\n",2);

if (positionOfNewLine >= 0)
{
string shortUsage = usage.Substring(2, positionOfNewLine-2);
Console.Out.WriteLine(shortUsage);
}
}
Console.Out.WriteLine("");
Console.Out.WriteLine("Usage of each tool:");
foreach (string usage in usages)
{
Console.Out.WriteLine(usage);
}

}

static void PrintToolError(string error)
{
Console.Out.WriteLine("");
if (!string.IsNullOrEmpty(error))
{
Console.Out.WriteLine("ERROR: {0}", error);
}

if (!string.IsNullOrEmpty(_currentToolUsage))
{
Console.Out.WriteLine("");
Console.Out.WriteLine("Usage of this tool");
Console.Out.WriteLine(_currentToolUsage);
Console.Out.WriteLine("");
Console.Out.WriteLine("Run DHI.MikeCore.Util without arguments to see usage of all tools");
}
}

private static string _currentToolUsage;

static void Main(string[] args)
{
Expand Down Expand Up @@ -71,33 +114,83 @@ static void Main(string[] args)
// Remember to lowercase all case cases
switch (arg0)
{
case "-dfsdiff":
_currentToolUsage = DfsDiff.UsageCreateDiffFile;
CheckArguments(arg0, args, 2, 3);
DfsDiff.CreateDiffFile(args[1], args[2], ArgNull(args,3));
break;
case "-dfsmerge":
_currentToolUsage = ExamplesMisc.UsageMergeDfsFileItems;
CheckArguments(arg0, args, 2, 99);
ExamplesMisc.MergeDfsFileItems(args[1], ArgList(args, 2));
break;
case "-resample":
_currentToolUsage = ExamplesDfs2.UsageResample;
CheckArguments(arg0, args, 4);
ExamplesDfs2.Resample(args[1], args[2], ArgInt(args, 3), ArgInt(args, 4));
break;
case "-maxvelocityfield":
_currentToolUsage = ExamplesDfs2.UsageMaxVelocityField;
CheckArguments(arg0, args, 2);
ExamplesDfs2.MaxVelocityField(args[1], args[2]);
break;
case "-dfsuextractlayer":
_currentToolUsage = ExamplesDfsu.UsageExtractDfsu2DLayerFrom3D;
CheckArguments(arg0, args, 3);
ExamplesDfsu.ExtractDfsu2DLayerFrom3D(args[1], args[2], ArgInt(args, 3));
break;
case "-dfsuextractsubarea":
_currentToolUsage = ExamplesDfsu.UsageExtractSubareaDfsu2D;
CheckArguments(arg0, args, 6);
ExamplesDfsu.ExtractSubareaDfsu2D(args[1], args[2], ArgDouble(args, 3), ArgDouble(args, 4), ArgDouble(args, 5), ArgDouble(args, 6));
break;
default:
Console.Out.WriteLine("");
Console.Out.WriteLine("ERROR: Coult not recognize tool name: {0}", arg0);
Console.Out.WriteLine("");
Console.Out.WriteLine("Run DHI.MikeCore.Util without arguments to see usage of all tools");
Environment.Exit(-2);
break;
}
}

private static List<string> ArgList(string[] args, int startIndex)
{
List<string> list = new List<string>(args.Length - startIndex);
for (int i = startIndex; i < args.Length; i++) list.Add(args[i]);
return list;
}

/// <summary> Return argument number, or null </summary>
private static string ArgNull(string[] args, int argi)
{
if (argi < args.Length)
return args[argi];
return null;
}

/// <summary> Check number of arguments </summary>
private static void CheckArguments(string tool, string[] args, int argTest)
private static void CheckArguments(string tool, string[] args, int argNumTest)
{
int toolArgLength = args.Length - 1;
// First args is -[tool], so disregard that one
if (args.Length != argTest+1)
if (toolArgLength != argNumTest)
{
Console.Out.WriteLine("{0} requires {1} arguments.", tool, argTest);
PrintUsage();
string err = string.Format("{0} requires {1} arguments.", tool, argNumTest);
PrintToolError(err);
Environment.Exit(-1);
}
}

/// <summary> Check number of arguments </summary>
private static void CheckArguments(string tool, string[] args, int argNumFromTest, int argNumToTest)
{
int toolArgLength = args.Length - 1;
// First args is -[tool], so disregard that one
if (argNumFromTest > toolArgLength || toolArgLength > argNumToTest)
{
string err = string.Format("{0} requires {1}-{2} arguments.", tool, argNumFromTest, argNumToTest);
PrintToolError(err);
Environment.Exit(-1);
}
}
Expand All @@ -107,8 +200,8 @@ private static int ArgInt(string[] args, int argIndex)
{
if (int.TryParse(args[argIndex], out int result))
return result;
Console.Out.WriteLine("Argument {0} is not an integer.", argIndex);
PrintUsage();
string err = String.Format("Argument {0} is not an integer.", argIndex);
PrintToolError(err);
Environment.Exit(-1);
return -1;
}
Expand All @@ -118,10 +211,10 @@ private static double ArgDouble(string[] args, int argIndex)
{
if (double.TryParse(args[argIndex], NumberStyles.Any, CultureInfo.InvariantCulture, out double result))
return result;
Console.Out.WriteLine("Argument {0} is not a floating point number.", argIndex);
PrintUsage();
string err = String.Format("Argument {0} is not a floating point number.", argIndex);
PrintToolError(err);
Environment.Exit(-1);
return -1;
}
}
}
}
45 changes: 44 additions & 1 deletion Examples/CSharp/DHI.MikeCore.Util/README.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,65 @@

Usage
Usage:
DHI.MikeCore.Util -[tool] [arguments]

Tools:

-dfsDiff: Create difference file between two dfs files
-dfsMerge: Merge two or more dfs files into one
-MaxVelocityField: Create maximum velocity field for a dfs2 file
-Resample: Resample dfs2 file in x/y space
-dfsuExtractLayer: Extract layer from 3D dfsu
-dfsuExtractSubArea: Extract subarea of dfsu file

Usage of each tool:

-dfsDiff: Create difference file between two dfs files

DHI.MikeCore.Util -dfsDiff [file1] [file2]
DHI.MikeCore.Util -dfsDiff [file1] [file2] [outDiffFile]

The two input files must be equal in structure, e.g. coming
from the same simulation but giving different results.
Header and static data must be identical, only difference
must be in values of the dynamic data.

If the [outDiffFile] is not specified, no difference file
is created, but a report outputted to the console.


-dfsMerge: Merge two or more dfs files into one

DHI.MikeCore.Util -dfsMerge [outMergeFile] [file1] [file2]
DHI.MikeCore.Util -dfsMerge [outMergeFile] [file1] [file2] [file3] ...

Merge any number of dfs files. The merger is on dynamic item basis,
i.e. add all dynamic items of a number of dfs files to a new dfs file.
Static data is copied from [file1].

It is assumed that all files has the same time stepping layout. It will merge
as many time steps as the file with the least number of timesteps.

If merging one of the specific types of dfs files, dfs0 or dfs1 or dfs2 or dfs3,
the structure of the files must be identical, i.e. the sizes of the axis must equal.
Otherwise, the outcome will not be a valid dfs0/1/2/3 file.


-MaxVelocityField: Create maximum velocity field for a dfs2 file

DHI.MikeCore.Util -MaxVelocityField [sourceFilename] [outputFilename]

From a dfs2 file containing items (H-P-Q), (P-Q-Speed) or (u-v-Speed),
find maximum velocity for each cell and store in [outputFilename]


-Resample: Resample dfs2 file in x/y space

DHI.MikeCore.Util -Resample [inputFilename] [outputFilename] [xCount] [yCount]

Resample the [inputFilename] to contain [xCount] x [yCount] number of cells, and
store the result in [outputFilename]


-dfsuExtractLayer: Extract layer from 3D dfsu

DHI.MikeCore.Util -dfsuExtractLayer [filenameDfsu3] [outputFilenameDfsu2] [layerNumber]
Expand All @@ -33,6 +75,7 @@ Tools:
If a layer value does not exist for a certain 2D element, delete value is written
to the 2D resut file. This is relevant for Sigma-Z type of files.


-dfsuExtractSubArea: Extract subarea of dfsu file

DHI.MikeCore.Util -dfsuExtractSubArea [sourceFilename] [outputFilename] [x1] [y2] [x2] [y2]
Expand Down
Loading

0 comments on commit 36d1ccd

Please sign in to comment.