public void PercentileTest() { DFSBase target = DfsFileFactory.OpenFile(@"..\..\..\TestData\novomr4_indv_dfs0_ud1.dfs0"); double[] Percentiles = new double[] { 0.1, 0.5, 0.9 }; DFSBase outf = DfsFileFactory.CreateFile(@"..\..\..\TestData\novomr4_indv_dfs0_ud1_percentiles.dfs0", Percentiles.Count()); outf.CopyFromTemplate(target); int Item = 1; // TODO: Initialize to an appropriate value int k = 0; //Create the items foreach (double j in Percentiles) { outf.Items[k].EumItem = target.Items[Item - 1].EumItem; outf.Items[k].EumUnit = target.Items[Item - 1].EumUnit; outf.Items[k].Name = j.ToString() + " Percentile"; k++; } int[] TSteps = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; target.Percentile(Item, TSteps, outf, Percentiles); outf.Dispose(); target.Dispose(); DFS0 df = new DFS0(@"..\..\..\TestData\novomr4_indv_dfs0_ud1_percentiles.dfs0"); Assert.AreEqual(25952, df.GetData(0, 1), 0.5); Assert.AreEqual(27294, df.GetData(0, 2), 0.5); Assert.AreEqual(33422, df.GetData(0, 3), 0.5); df.Dispose(); }
private void button1_Click(object sender, RoutedEventArgs e) { eumQuantity eq = Items.SelectedValue as eumQuantity; if (eq != null) { eq.Unit = (eumUnit)Enum.Parse(typeof(eumUnit), Units.SelectedItem.ToString()); } foreach (var f in FileNames) { DFSBase dfs = HydroNumerics.MikeSheTools.DFS.DfsFileFactory.OpenFile(f); foreach (var i in dfs.Items) { if (eq != null) { i.EumItem = eq.Item; i.EumUnit = eq.Unit; } if (Type.SelectedItem != null) { i.ValueType = (DataValueType)Enum.Parse(typeof(DataValueType), Type.SelectedItem.ToString()); } } dfs.Dispose(); } this.Close(); }
// //You can use the following additional attributes as you write your tests: // //Use ClassInitialize to run code before running the first test in the class //[ClassInitialize()] //public static void MyClassInitialize(TestContext testContext) //{ //} // //Use ClassCleanup to run code after all tests in a class have run //[ClassCleanup()] //public static void MyClassCleanup() //{ //} // //Use TestInitialize to run code before running each test //[TestInitialize()] //public void MyTestInitialize() //{ //} // //Use TestCleanup to run code after each test has run //[TestCleanup()] //public void MyTestCleanup() //{ //} // #endregion internal virtual DFSBase CreateDFSBase() { // TODO: Instantiate an appropriate concrete class. DFSBase target = null; return(target); }
public void SaveToNetCDFTest() { DFSBase dfs = null; // TODO: Initialize to an appropriate value string NetCDFFileName = @"C:\temp\netcdf.nc"; DFSExtension.SaveToNetCDF(dfs, NetCDFFileName); }
/// <summary> /// Does simple factor math on all or selected time steps and items in a dfs-file. /// </summary> /// <param name="OperationData"></param> public static void FactorMath(XElement OperationData) { DFS3.MaxEntriesInBuffer = 1; DFS2.MaxEntriesInBuffer = 1; string File1 = OperationData.Element("DFSFileName").Value; //DFSOutputFileName is optional. If it exists the input file is copied to this filename var outfile = OperationData.Element("DFSOutputFileName"); if (outfile != null && outfile.Value != "") { if (File1.ToLower() != outfile.Value.ToLower()) { File.Copy(File1, outfile.Value, true); } File1 = OperationData.Element("DFSOutputFileName").Value; } DFSBase dfs = DfsFileFactory.OpenFile(File1); int[] Items = ParseString(OperationData.Element("Items").Value, 1, dfs.Items.Count()); int[] TimeSteps = ParseString(OperationData.Element("TimeSteps").Value, 0, dfs.NumberOfTimeSteps - 1); string Operator = OperationData.Element("MathOperation").Value; double Factor = double.Parse(OperationData.Element("Factor").Value); foreach (int j in TimeSteps) { foreach (int i in Items) { switch (Operator) { case "+": dfs.AddToItemTimeStep(j, i, Factor); break; case "-": dfs.AddToItemTimeStep(j, i, -Factor); break; case "*": dfs.MultiplyItemTimeStep(j, i, Factor); break; case "/": dfs.MultiplyItemTimeStep(j, i, 1.0 / Factor); break; default: break; } } } dfs.Dispose(); }
public void PercentileTest3() { DFSBase target = DfsFileFactory.OpenFile(@"c:\temp\KFT-SJ_inv_3DSZ.dfs3"); double[] Percentiles = new double[] { 0.1, 0.5, 0.9 }; DFSBase outf = DfsFileFactory.CreateFile(@"c:\temp\TestDataSet_percentiles_limit.dfs3", Percentiles.Count()); DFSBase outf2 = DfsFileFactory.CreateFile(@"c:\temp\TestDataSet_percentiles.dfs3", Percentiles.Count()); outf.CopyFromTemplate(target); outf2.CopyFromTemplate(target); int Item = 1; int k = 0; //Create the items foreach (double j in Percentiles) { outf.Items[k].EumItem = target.Items[Item - 1].EumItem; outf.Items[k].EumUnit = target.Items[Item - 1].EumUnit; outf.Items[k].Name = j.ToString() + " Percentile"; outf2.Items[k].EumItem = target.Items[Item - 1].EumItem; outf2.Items[k].EumUnit = target.Items[Item - 1].EumUnit; outf2.Items[k].Name = j.ToString() + " Percentile"; k++; } int[] TSteps = new int[target.NumberOfTimeSteps]; for (int i = 0; i < target.NumberOfTimeSteps; i++) { TSteps[i] = i; } Stopwatch sw = new Stopwatch(); sw.Start(); target.Percentile(Item, TSteps, outf, Percentiles, 300); sw.Stop(); TimeSpan el = sw.Elapsed; sw.Reset(); sw.Start(); target.Percentile(Item, TSteps, outf2, Percentiles, 100); sw.Stop(); TimeSpan el2 = sw.Elapsed; outf.Dispose(); outf2.Dispose(); target.Dispose(); }
public static void MonthlyStats(XElement OperationData) { string File1 = OperationData.Element("DFSFileName").Value; DFSBase dfs = DfsFileFactory.OpenFile(File1); var outfile = OperationData.Element("DFSOutputFileName").Value; int itemnumber = int.Parse(OperationData.Element("Item").Value); DFSBase dfsout = DfsFileFactory.CreateFile(outfile, 3); dfsout.CopyFromTemplate(dfs); dfsout.TimeOfFirstTimestep = new DateTime(dfs.TimeOfFirstTimestep.Year, dfs.TimeOfFirstTimestep.Month, 15); dfsout.TimeStep = TimeSpan.FromDays(30); dfs.MonthAggregation(itemnumber, dfsout); dfsout.Dispose(); }
public static void SaveToNetCDF(this DFSBase dfs, string NetCDFFileName) { double[,,] grid = new double[360, 720, 10]; double[] x = new double[360]; double[] y = new double[720]; double[] t = new double[10]; for (int k = 0; k < 10; k++) { for (int i = 0; i < 360; i++) { x[i] = i; for (int j = 0; j < 720; j++) { y[j] = j; grid[i, j, k] = i ^ 2 + j ^ 2; } } } // ... compute grid, x and y values DataSet ds = DataSet.Open(NetCDFFileName + "?openMode=create"); // MemoryDataSet ds = new MemoryDataSet(); int vid = ds.AddVariable <Int16>("values", "x", "y", "t").ID; //ds.Append("values", grid, 2); //ds.IsAutocommitEnabled = false; //for (int i = 0; i < 10; i++) //{ // ds.Append("values", grid, 2); // ds.Append("t", i*1.1,0); //} ds.Commit(); //ds.Clone(NetCDFFileName + "?openMode=create").Dispose(); ds.Dispose(); }
/// <summary> /// Does simple factor math on all time steps and of selected items in a dfs-file. /// A different factor can be used for each month /// </summary> /// <param name="OperationData"></param> public static void Percentile(XElement OperationData) { DFS3.MaxEntriesInBuffer = 1; DFS2.MaxEntriesInBuffer = 1; int maxmem = 300; //Uses 300 mB of memory string File1 = OperationData.Element("DFSFileName").Value; string outfile = OperationData.Element("DFSOutputFileName").Value; DFSBase dfsinput = DfsFileFactory.OpenFile(File1); double[] Percentiles = ParseString(OperationData.Element("Percentiles").Value); int Item = int.Parse((OperationData.Element("Item").Value)); int[] TimeSteps = ParseString(OperationData.Element("TimeSteps").Value, 0, dfsinput.NumberOfTimeSteps - 1); var TimeintervalElement = OperationData.Element("TimeInterval"); string timeinterval = ""; //Percentiles are wanted for either each month or each year. if (TimeintervalElement != null) { timeinterval = TimeintervalElement.Value.ToLower(); } List <int> timesteps = new List <int>(); string ext = Path.GetExtension(outfile); switch (timeinterval) { case "month": for (int i = 1; i <= 12; i++) { timesteps.Clear(); foreach (int j in TimeSteps) { if (dfsinput.TimeSteps[j].Month == i) { timesteps.Add(j); } } if (timesteps.Count > 3) { string FileName = outfile.Substring(0, outfile.Length - ext.Length) + "_Month_" + i + ext; var dfsoutm = DfsFileFactory.CreateFile(FileName, Percentiles.Count()); dfsoutm.CopyFromTemplate(dfsinput); dfsinput.Percentile(1, timesteps.ToArray(), dfsoutm, Percentiles, maxmem); dfsoutm.Dispose(); } } break; case "year": int CurrentYear = dfsinput.TimeSteps[TimeSteps.First()].Year; foreach (int j in TimeSteps) { if (CurrentYear == dfsinput.TimeSteps[j].Year) { timesteps.Add(j); } else { if (timesteps.Count > 3) { string FileName = outfile.Substring(0, outfile.Length - ext.Length) + "_Year_" + CurrentYear + ext; var dfsoutm = DfsFileFactory.CreateFile(FileName, Percentiles.Count()); dfsoutm.CopyFromTemplate(dfsinput); dfsinput.Percentile(1, timesteps.ToArray(), dfsoutm, Percentiles, maxmem); dfsoutm.Dispose(); } timesteps.Clear(); CurrentYear = dfsinput.TimeSteps[j].Year; timesteps.Add(j); } } break; default: //Just do percentile on everything when not month or year DFSBase dfs = DfsFileFactory.CreateFile(outfile, Percentiles.Count()); dfs.CopyFromTemplate(dfsinput); dfsinput.Percentile(Item, TimeSteps, dfs, Percentiles, maxmem); dfs.Dispose(); break; } dfsinput.Dispose(); }
/// <summary> /// Does simple factor math on all time steps and of selected items in a dfs-file. /// A different factor can be used for each month /// </summary> /// <param name="OperationData"></param> public static void MonthlyMath(XElement OperationData) { DFS3.MaxEntriesInBuffer = 1; DFS2.MaxEntriesInBuffer = 1; string File1 = OperationData.Element("DFSFileName").Value; string Operator = OperationData.Element("MathOperation").Value; string[] FactorStrings = OperationData.Element("MonthlyValues").Value.Split(new string[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries); var outfile = OperationData.Element("DFSOutputFileName"); if (outfile != null && outfile.Value != "") { if (File1.ToLower() != outfile.Value.ToLower()) { File.Copy(File1, outfile.Value, true); } File1 = OperationData.Element("DFSOutputFileName").Value; } DFSBase dfs = DfsFileFactory.OpenFile(File1); int[] Items = ParseString(OperationData.Element("Items").Value, 1, dfs.Items.Count()); int[] TimeSteps = ParseString(OperationData.Element("TimeSteps").Value, 0, dfs.NumberOfTimeSteps - 1); double[] Factors = new double[12]; for (int i = 0; i < 12; i++) { Factors[i] = double.Parse(FactorStrings[i]); } foreach (var j in TimeSteps) { double Factor = Factors[dfs.TimeSteps[j].Month - 1]; foreach (int i in Items) { switch (Operator) { case "+": dfs.AddToItemTimeStep(j, i, Factor); break; case "-": dfs.AddToItemTimeStep(j, i, -Factor); break; case "*": dfs.MultiplyItemTimeStep(j, i, Factor); break; case "/": dfs.MultiplyItemTimeStep(j, i, 1.0 / Factor); break; default: break; } } } dfs.Dispose(); }
/// <summary> /// Does either summation or average on weekly, monthly or yearly basis /// </summary> /// <param name="OperationData"></param> /// <param name="sum"></param> private static void TimeAggregation(XElement OperationData, MathType mathtype) { DFS3.MaxEntriesInBuffer = 1; DFS2.MaxEntriesInBuffer = 1; string File1 = OperationData.Element("DFSFileName").Value; DFSBase dfs = DfsFileFactory.OpenFile(File1); int[] Items = ParseString(OperationData.Element("Items").Value, 1, dfs.Items.Count()); string timeinterval = OperationData.Element("TimeInterval").Value.ToLower(); var Tstep = OperationData.Element("TimeIntervalSteps"); int timesteps = 1; if (Tstep != null) { timesteps = int.Parse(Tstep.Value); } string File2; bool samefile = true; if (OperationData.Element("DFSOutputFileName") != null) { File2 = OperationData.Element("DFSOutputFileName").Value; samefile = false; } else { File2 = Path.Combine(Path.GetFileNameWithoutExtension(File1) + "_temp", Path.GetExtension(File1)); } DFSBase outfile = DfsFileFactory.CreateFile(File2, Items.Count()); outfile.CopyFromTemplate(dfs); int k = 0; //Create the items foreach (int j in Items) { int i = j - 1; outfile.Items[k].EumItem = dfs.Items[i].EumItem; outfile.Items[k].EumUnit = dfs.Items[i].EumUnit; outfile.Items[k].Name = dfs.Items[i].Name; k++; } switch (timeinterval) { case "month": outfile.TimeOfFirstTimestep = new DateTime(dfs.TimeOfFirstTimestep.Year, dfs.TimeOfFirstTimestep.Month, 15); outfile.TimeStep = TimeSpan.FromDays(365.0 / 12 * timesteps); dfs.TimeAggregation(Items, outfile, TimeInterval.Month, timesteps, mathtype); break; case "year": outfile.TimeOfFirstTimestep = new DateTime(dfs.TimeOfFirstTimestep.Year, 6, 1); outfile.TimeStep = TimeSpan.FromDays(365.0 * timesteps); dfs.TimeAggregation(Items, outfile, TimeInterval.Year, timesteps, mathtype); break; case "day": outfile.TimeStep = TimeSpan.FromDays(timesteps); dfs.TimeAggregation(Items, outfile, TimeInterval.Day, timesteps, mathtype); break; default: break; } //Close the files dfs.Dispose(); outfile.Dispose(); if (samefile) { File.Delete(File1); FileInfo f = new FileInfo(File2); File.Move(File2, File1); } }
public void PercentileTest2() { DFSBase target = DfsFileFactory.OpenFile(@"..\..\..\TestData\TestDataSet.dfs2"); double[] Percentiles = new double[] { 0.1, 0.5, 0.9 }; DFSBase outf = DfsFileFactory.CreateFile(@"..\..\..\TestData\TestDataSet_percentiles_limit.dfs2", Percentiles.Count()); DFSBase outf2 = DfsFileFactory.CreateFile(@"..\..\..\TestData\TestDataSet_percentiles.dfs2", Percentiles.Count()); outf.CopyFromTemplate(target); outf2.CopyFromTemplate(target); int Item = 1; int k = 0; //Create the items foreach (double j in Percentiles) { outf.Items[k].EumItem = target.Items[Item - 1].EumItem; outf.Items[k].EumUnit = target.Items[Item - 1].EumUnit; outf.Items[k].Name = j.ToString() + " Percentile"; outf2.Items[k].EumItem = target.Items[Item - 1].EumItem; outf2.Items[k].EumUnit = target.Items[Item - 1].EumUnit; outf2.Items[k].Name = j.ToString() + " Percentile"; k++; } int[] TSteps = new int[target.NumberOfTimeSteps]; for (int i = 0; i < target.NumberOfTimeSteps; i++) { TSteps[i] = i; } Stopwatch sw = new Stopwatch(); sw.Start(); target.Percentile(Item, TSteps, outf, Percentiles, 10); sw.Stop(); TimeSpan el = sw.Elapsed; sw.Reset(); sw.Start(); target.Percentile(Item, TSteps, outf2, Percentiles); sw.Stop(); TimeSpan el2 = sw.Elapsed; outf.Dispose(); outf2.Dispose(); target.Dispose(); DFS2 fil1 = new DFS2(@"..\..\..\TestData\TestDataSet_percentiles_limit.dfs2"); DFS2 fil2 = new DFS2(@"..\..\..\TestData\TestDataSet_percentiles.dfs2"); for (int i = 1; i <= Percentiles.Count(); i++) { var m1 = fil1.GetData(0, i); var m2 = fil2.GetData(0, i); for (int j = 0; j < m1.Data.Count(); j++) { Assert.AreEqual(m1.Data[j], m2.Data[j]); } } fil1.Dispose(); fil2.Dispose(); }
private void button4_Click(object sender, RoutedEventArgs e) { string ext = System.IO.Path.GetExtension(DFS2File.Text); Microsoft.Win32.SaveFileDialog SaveFileDialog = new Microsoft.Win32.SaveFileDialog(); SaveFileDialog.Filter = string.Format("Known file types (*{0})|*{0}", ext); SaveFileDialog.Title = "Merge into dfs-file"; if (SaveFileDialog.ShowDialog().HasValue) { DFS = DfsFileFactory.OpenFile(DFS2File.Text); var dfsout = DfsFileFactory.CreateFile(SaveFileDialog.FileName, 1); using (System.IO.StreamReader sr = new System.IO.StreamReader(TxtFile.Text)) { dfsout.CopyFromTemplate(DFS); dfsout.FirstItem.EumItem = DHI.Generic.MikeZero.eumItem.eumIPrecipitationRate; dfsout.FirstItem.EumUnit = DHI.Generic.MikeZero.eumUnit.eumUmmPerDay; dfsout.TimeStep = TimeControl.Value; dfsout.TimeOfFirstTimestep = datePicker1.DateTimeSelected; sr.ReadLine(); var gridcodes = DFS.ReadItemTimeStep(0, 1); Dictionary <int, List <int> > GridcodesIndex = new Dictionary <int, List <int> >(); for (int i = 0; i < gridcodes.Count(); i++) { if (gridcodes[i] != DFS.DeleteValue) { List <int> grids; if (!GridcodesIndex.TryGetValue((int)gridcodes[i], out grids)) { grids = new List <int>(); GridcodesIndex.Add((int)gridcodes[i], grids); } grids.Add(i); gridcodes[i] = (float)DFS.DeleteValue; } } var splits = sr.ReadLine().Split(new string[] { " ", "\t" }, StringSplitOptions.RemoveEmptyEntries); List <int> gridcodestxt = new List <int>(splits.Select(var => (int)float.Parse(var))); int tscounter = 0; while (!sr.EndOfStream) { splits = sr.ReadLine().Split(new string[] { " ", "\t" }, StringSplitOptions.RemoveEmptyEntries); for (int j = 0; j < splits.Count(); j++) { List <int> grids; if (GridcodesIndex.TryGetValue(gridcodestxt[j], out grids)) { float val = float.Parse(splits[j]); foreach (var k in grids) { gridcodes[k] = val; } } } dfsout.WriteItemTimeStep(tscounter, 1, gridcodes); tscounter++; } } DFS.Dispose(); dfsout.Dispose(); } }