Example #1
0
        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();
        }
Example #2
0
        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();
        }
Example #3
0
        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();
        }
Example #4
0
        void v_SimulationFinished(object sender, EventArgs e)
        {
            Model mshe = sender as Model;
            var   dfs  = DfsFileFactory.OpenFile(mshe.Files.SZ3DFileName);

            double[] percentiles = new double[] { 0.1 };
            string   filename    = Path.Combine(mshe.Files.ResultsDirectory, "SZ3D_percentiles.dfs3");
            var      dfsout      = DfsFileFactory.CreateFile(filename, percentiles.Count());

            dfsout.CopyFromTemplate(dfs);
            dfs.Percentile(1, dfsout, percentiles, 80000000);
            dfsout.Dispose();
            DFS3 dfsout2 = new DFS3(filename);

            Console.WriteLine(dfsout2.GetData(0, 1)[10, 10, 0]);
            dfsout2.Dispose();

            RunNext(mshe);
        }
Example #5
0
        /// <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();
        }
Example #6
0
        /// <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);
            }
        }
Example #7
0
        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();
        }
Example #8
0
        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();
            }
        }
Example #9
0
        public static string KSTResults(Model mshe)
        {
            string returnstring = "";

            double[] percentiles = new double[] { 0.1, 0.5, 0.9, 0.95 };

            if (!File.Exists(Path.GetFullPath(mshe.Files.SZ3DFileName)))
            {
                returnstring += "Could not find:" + Path.GetFullPath(mshe.Files.SZ3DFileName);
            }
            else
            {
                var dfs = DfsFileFactory.OpenFile(mshe.Files.SZ3DFileName);

                //Find first time step to use by skipping the firstyear
                int firsttimestep = 0;

                while (dfs.TimeOfFirstTimestep.Year + 5 == dfs.TimeSteps[firsttimestep].Year)
                {
                    firsttimestep++;
                }

                List <int> timesteps = new List <int>();
                for (int j = firsttimestep; j < dfs.NumberOfTimeSteps; j++)
                {
                    timesteps.Add(j);
                }


                string filename = Path.Combine(mshe.Files.ResultsDirectory, "SZ3D_percentiles.dfs3");
                var    dfsout   = DfsFileFactory.CreateFile(filename, percentiles.Count());
                dfsout.CopyFromTemplate(dfs);
                dfs.Percentile(1, timesteps.ToArray(), dfsout, percentiles, 400);
                dfsout.Dispose();

                for (int i = 1; i <= 12; i++)
                {
                    //Get the timesteps
                    timesteps.Clear();

                    for (int j = firsttimestep; j < dfs.TimeSteps.Count; j++)
                    {
                        if (dfs.TimeSteps[j].Month == i)
                        {
                            timesteps.Add(j);
                        }
                    }

                    if (timesteps.Count > 3)
                    {
                        string filename2 = Path.Combine(mshe.Files.ResultsDirectory, "SZ3D_percentiles_Month_" + i + ".dfs3");
                        var    dfsoutm   = DfsFileFactory.CreateFile(filename2, percentiles.Count());
                        dfsoutm.CopyFromTemplate(dfs);
                        dfs.Percentile(1, timesteps.ToArray(), dfsoutm, percentiles, 400);
                        dfsoutm.Dispose();
                    }
                }
                dfs.Dispose();
            }

            if (!File.Exists(Path.GetFullPath(mshe.Files.SZ2DFileName)))
            {
                returnstring += "\n Could not find:" + Path.GetFullPath(mshe.Files.SZ2DFileName);
            }
            else
            {
                var dfs = DfsFileFactory.OpenFile(mshe.Files.SZ2DFileName);

                //Find first time step to use by skipping the firstyear
                int firsttimestep = 0;

                while (dfs.TimeOfFirstTimestep.Year + 5 == dfs.TimeSteps[firsttimestep].Year)
                {
                    firsttimestep++;
                }

                List <int> timesteps = new List <int>();
                for (int j = firsttimestep; j < dfs.NumberOfTimeSteps; j++)
                {
                    timesteps.Add(j);
                }


                var filename = Path.Combine(mshe.Files.ResultsDirectory, "Phreatic_percentiles.dfs2");
                var dfsout   = DfsFileFactory.CreateFile(filename, percentiles.Count());
                dfsout.CopyFromTemplate(dfs);
                dfs.Percentile(1, timesteps.ToArray(), dfsout, percentiles, 400);
                dfsout.Dispose();

                for (int i = 1; i <= 12; i++)
                {
                    //Get the timesteps
                    timesteps.Clear();

                    for (int j = firsttimestep; j < dfs.TimeSteps.Count; j++)
                    {
                        if (dfs.TimeSteps[j].Month == i)
                        {
                            timesteps.Add(j);
                        }
                    }

                    if (timesteps.Count > 3)
                    {
                        string filename2 = Path.Combine(mshe.Files.ResultsDirectory, "Phreatic_percentiles_Month_" + i + ".dfs2");
                        var    dfsoutm   = DfsFileFactory.CreateFile(filename2, percentiles.Count());
                        dfsoutm.CopyFromTemplate(dfs);
                        dfs.Percentile(1, timesteps.ToArray(), dfsoutm, percentiles, 400);
                        dfsoutm.Dispose();
                    }
                }
                dfs.Dispose();
            }
            return(returnstring);
        }