Beispiel #1
0
        public void Conditionals()
        {
            SeriesExpressionParser.Debug = true;
            var tiew_qj = new Series("tiew_qj");
            var nscw_qj = new Series("nscw_qj");

            tiew_qj.TimeInterval = TimeInterval.Daily;
            nscw_qj.TimeInterval = TimeInterval.Daily;

            DateTime t1 = DateTime.Parse("1/1/2015");
            DateTime t2 = DateTime.Parse("1/10/2015");

            tiew_qj.AfterRead += new EventHandler(delegate(object o, EventArgs a)
            {
                tiew_qj.AddRange(t1, new double[] { 10, 10, 1, 1 });
            });
            nscw_qj.AfterRead += new EventHandler(delegate(object o, EventArgs a)
            {
                nscw_qj.AddRange(t1, new double[] { 11, 10, 1, 100 });
            });
            var expected  = new double[]  { 35, 0, 0, 35 };
            var expected2 = new double[] { 0, 0, 35, 0 };

            var qu = new CalculationSeries();

            qu.Expression   = "If( tiew_qj + nscw_qj > 20.0 , 35.0, 0.0)";
            qu.TimeInterval = TimeInterval.Daily;
            qu.Parser.VariableResolver.Add("tiew_qj", tiew_qj);
            qu.Parser.VariableResolver.Add("nscw_qj", nscw_qj);
            qu.Calculate(t1, t2);
            qu.WriteToConsole();
            Assert.AreEqual(4, qu.Count, " expected 4 QU values");


            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], qu[i].Value, 0.001);
            }
            expected      = null;
            qu.Expression = "If( tiew_qj + nscw_qj < 20.0 , 35.0, 0.0)";
            qu.Calculate(t1, t2);
            qu.WriteToConsole();
            Assert.AreEqual(4, qu.Count, " expected 4 QU values");

            for (int i = 0; i < expected2.Length; i++)
            {
                Assert.AreEqual(expected2[i], qu[i].Value, 0.001);
            }
        }
        public void MathInDatabase()
        {
            _svr.CreateDataBase(_fn);

            Series one = new Series();

            one.Name = "o";
            one.Add(DateTime.Parse("2001-01-01"), 1);
            one.Add(DateTime.Parse("2001-01-02"), 1);

            Series two = new Series();

            two.Name = "two";
            two.Add(DateTime.Parse("2001-01-01"), 2);
            two.Add(DateTime.Parse("2001-01-02"), 2);


            CalculationSeries onePlusTwo = new CalculationSeries();

            onePlusTwo.Name       = "one+two"; // this name will match 'one%' in SQL
            onePlusTwo.Expression = "o+two";
            _db.AddSeries(one);
            _db.AddSeries(two);
            _db.AddSeries(onePlusTwo);

            onePlusTwo.Calculate();
            Assert.AreEqual(2, onePlusTwo.Count);
            Assert.AreEqual(3, onePlusTwo[0].Value);
            Assert.AreEqual(3, onePlusTwo[1].Value);
            onePlusTwo.WriteToConsole();
        }
Beispiel #3
0
        public void TestRectangularContractedWeir()
        {
            // bully creek feder canal
            Series   ch = new Series("bul_ch");
            DateTime t1 = DateTime.Parse("2015-2-8 8:15");
            DateTime t2 = DateTime.Parse("2015-2-12 7:15");

            ch.AfterRead += new EventHandler(delegate(object o, EventArgs a)
            {
                ch.Add(t1, 2);
                ch.Add(t2, 1.69);
            });


            Logger.EnableLogger();
            SeriesExpressionParser.Debug = true;
            var c = new CalculationSeries("bul_qc");

            c.Parser.VariableResolver.Add("bul_ch", new ParserResult(ch));
            c.TimeInterval = TimeInterval.Irregular;
            c.Expression   = "RectangularContractedWeir(bul_ch,10)";
            c.Calculate();


            Assert.AreEqual(2, c.Count, " expected two flow calculations");
            Assert.AreEqual(90.42, c[t1].Value, 0.01);
            Assert.AreEqual(70.69, c[t2].Value, 0.01);

            ch.Read();
            var x = Reclamation.TimeSeries.Math.RectangularContractedWeir(ch, 10);

            Assert.AreEqual(90.42, c[0].Value, 0.01);
        }
        public void SpaceInVariableName()
        {
            _svr.CreateDataBase(_fn);

            Series one = new Series();

            one.Name = "o";
            one.Add(DateTime.Parse("2001-01-01"), 1);
            one.Add(DateTime.Parse("2001-01-02"), 1);


            SeriesExpressionParser.Debug = true;
            one.Name = "jck af"; // put a space in the name
            _db.AddSeries(one);

            CalculationSeries onePlusTwo = new CalculationSeries();

            onePlusTwo.Name       = "space_in_expression";
            onePlusTwo.Expression = "'jck af'+2"; // using single quotes '
            _db.AddSeries(onePlusTwo);

            onePlusTwo.Calculate();
            Assert.AreEqual(2, onePlusTwo.Count);
            Assert.AreEqual(3, onePlusTwo[0].Value);
            Assert.AreEqual(3, onePlusTwo[1].Value);
        }
Beispiel #5
0
        public void AddBug()
        {
            DateTime           t1  = new DateTime(2013, 1, 1);
            var                t2  = t1.AddDays(365);
            var                fn  = FileUtility.GetTempFileNameInDirectory(@"c:\temp\", ".pdb");
            SQLiteServer       svr = new SQLiteServer(fn);
            TimeSeriesDatabase db  = new Reclamation.TimeSeries.TimeSeriesDatabase(svr, false);
            var                s   = new HydrometDailySeries("pal", "af");

            s.Name = "pal_af";
            s.Read(t1, t2);
            db.AddSeries(s);

            var cs = new CalculationSeries("add_test");

            cs.Expression = "pal_af + pal_af";
            db.AddSeries(cs);


            cs = db.GetSeriesFromName("add_test") as CalculationSeries;
            cs.Calculate(t1, t2);


            cs = db.GetSeriesFromName("add_test") as CalculationSeries;
            cs.Read();

            Assert.IsTrue(cs.Count > 0);
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            if (args.Length != 3)
            {
                Console.WriteLine("Usage: ImportCsv site_list.csv instant|daily daysBack");
                return;
            }
            FileUtility.CleanTempPath();
            Logger.EnableLogger();

            CsvFile csv      = new CsvFile(args[0]);
            int     daysBack = int.Parse(args[2]);

/*
 *
 * interval,filename,date_column,value_column,cbtt,pcode
 * instant,kid.csv,Time,MB1.Hw_FlowMeter,ACHO,QC1
 * instant,kid.csv,Time,MB1.Hw_FlowCalc,ACHO,QC2
 * instant,kid.csv,Time,MB1.MillerHill_FlowPump1,HMPO,QP1
 * instant,kid.csv,Time,MB1.MillerHill_FlowPump2,HMPO,QP2
 * instant,kid.csv,Time,MB1.MillerHill_FlowPump3,HMPO,QP3
 *
 */
            var rows     = csv.Select("interval = '" + args[1] + "'");
            var interval = TimeInterval.Daily;

            if (args[1] == "instant")
            {
                interval = TimeInterval.Irregular;
            }

            Console.WriteLine("Processing " + rows.Length + " parameters");
            for (int i = 0; i < rows.Length; i++)
            {
                var filename    = rows[i]["filename"].ToString();
                var dateColumn  = rows[i]["date_column"].ToString();
                var valueColumn = rows[i]["value_column"].ToString();
                var cbtt        = rows[i]["cbtt"].ToString();
                var pcode       = rows[i]["pcode"].ToString();

                DataTable tbl = new CsvFile(filename);

                CalculationSeries s = new CalculationSeries();
                s.TimeInterval            = interval;
                s.Expression              = valueColumn;
                s.Parser.VariableResolver = new DataTableVariableResolver(tbl, dateColumn);
                s.Calculate(DateTime.Now.AddDays(-daysBack), DateTime.Now);

                Console.WriteLine("Processing " + cbtt + "_" + pcode + " " + s.Count + " records");

                TimeSeriesTransfer.Import(s, cbtt, pcode);
            }
        }
Beispiel #7
0
        public void JacksonQU()
        { // daily data based on other daily data
            //DailyCalculator.
            Reclamation.TimeSeries.Parser.SeriesExpressionParser.Debug = true;
            var s = new CalculationSeries();

            s.Expression              = "(jck_af-jck_af[t-1])/1.98347+jck_qd";
            s.TimeInterval            = TimeInterval.Daily;
            s.Parser.VariableResolver = new HydrometVariableResolver();

            s.Calculate(new DateTime(2012, 11, 1), new DateTime(2012, 11, 15));

            Assert.AreEqual(298, s["2012-11-15"].Value, 0.01);
        }
Beispiel #8
0
        public void MpollQU()
        {
            var qu = new CalculationSeries();

            qu.Expression              = "WOD_AF-WOD_AF[t-1]+WOD_OM";
            qu.TimeInterval            = TimeInterval.Monthly;
            qu.Parser.VariableResolver = new HydrometVariableResolver();
            DateTime t1 = DateTime.Parse("9/1/1957");
            DateTime t2 = DateTime.Parse("12/23/1957");

            qu.Calculate(t1, t2);
            Assert.AreEqual(4120, qu["10/1/1957"].Value);
            qu.WriteToConsole();
        }
Beispiel #9
0
        /// <summary>
        /// Adds new Calculation Series
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void AddCalculationClick(object sender, EventArgs e)
        {
            CalculationSeries     s = new CalculationSeries(DB);
            CalculationProperties p = new CalculationProperties(s, new TimeSeriesTreeModel(DB), DB.GetUniqueUnits());

            if (p.ShowDialog() == DialogResult.OK)
            {
                DB.AddSeries(s, CurrentFolder);
                // tree refresh.. Add node.
                s.Calculate(); // save again
                // refresh.
                DB.RefreshFolder(CurrentFolder);
            }
        }
Beispiel #10
0
        public void SimpleDailyAverageWithInterval()
        {
            var mm = new CalculationSeries();

            mm.SiteID                  = "pici"; // test feautre that replaces %site% with SiteID
            mm.Expression              = "DailyAverage(instant_%site%_ob)";
            mm.TimeInterval            = TimeInterval.Daily;
            mm.Parser.VariableResolver = new HydrometVariableResolver();
            DateTime t1 = DateTime.Parse("5/2/2013");
            DateTime t2 = DateTime.Parse("5/2/2013");

            mm.Calculate(t1, t2);
            Assert.AreEqual(44.56, mm["5/2/2013"].Value, 0.01);
            mm.WriteToConsole();
        }
Beispiel #11
0
        public void SimpleDailyAverageNoPrefix()
        {
            var mm = new CalculationSeries();

            mm.SiteID                  = "pici";
            mm.Expression              = "DailyAverage(%site%_ob)";
            mm.TimeInterval            = TimeInterval.Irregular;
            mm.Parser.VariableResolver = new HydrometVariableResolver();
            DateTime t1 = DateTime.Parse("5/2/2013");
            DateTime t2 = DateTime.Parse("5/2/2013");

            mm.Calculate(t1, t2);
            Assert.AreEqual(44.56, mm["5/2/2013"].Value, 0.01);
            mm.WriteToConsole();
        }
Beispiel #12
0
        public void ConstantInteger()
        {
            CalculationSeries c = new CalculationSeries();

            c.TimeInterval = TimeInterval.Daily;
            c.Name         = "constant_series";
            c.Expression   = "14";
            db.AddSeries(c);

            DateTime t1 = new DateTime(2014, 10, 1);
            DateTime t2 = new DateTime(2014, 10, 5);

            c.Calculate(t1, t2);

            Assert.AreEqual(5, c.Count);
            Assert.AreEqual(14, c[0].Value, 0.001);
        }
Beispiel #13
0
        public void ValeQU()
        { // daily data based on other daily data
            //DailyCalculator.
            Reclamation.TimeSeries.Parser.SeriesExpressionParser.Debug = true;
            var s = new CalculationSeries();

            s.Expression              = "(WAR_AF[t-2]-WAR_AF[t-3]+BEU_AF[t-2]-BEU_AF[t-3]+BUL_AF-BUL_AF[t-1])/1.98347+VALO_QD";
            s.TimeInterval            = TimeInterval.Daily;
            s.Parser.VariableResolver = new HydrometVariableResolver();

            DateTime t = new DateTime(2012, 12, 6);

            s.Calculate(t, t);

            Assert.AreEqual(1, s.Count);
            Assert.AreEqual(573.75, s[t].Value, 0.01);
        }
Beispiel #14
0
        /// <summary>
        /// Adds new Calculation Series
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void AddCalculationClick(object sender, EventArgs e)
        {
            CalculationSeries     s = new CalculationSeries(DB);
            CalculationProperties p = new CalculationProperties(s, new TimeSeriesTreeModel(DB), DB);

            if (p.ShowDialog() == DialogResult.OK)
            {
                DB.AddSeries(s, CurrentFolder);
                // tree refresh.. Add node.
                if (p.Calculate)
                {
                    s.Calculate();     // save again
                }
                // refresh.
                //DB.RefreshFolder(CurrentFolder);
                DatabaseChanged();;
            }
        }
        public void FunctionNames()
        {
            _svr.CreateDataBase(_fn);

            ParserFunction f;
            string         subExpr = "";
            bool           ok      = ParserUtility.TryGetFunctionCall("Merge(series1,'series 2')+'Series 5'", out subExpr, out f);

            Assert.IsTrue(ok);
            Assert.AreEqual("Merge", f.Name);
            Assert.AreEqual(2, f.Parameters.Length);

            Series observed = new Series();

            observed.Name = "observed";
            observed.Add(DateTime.Parse("2001-01-01"), 1);
            observed.AddMissing(DateTime.Parse("2001-1-02"));
            observed.Add(DateTime.Parse("2001-01-04"), 1);
            observed.TimeInterval = TimeInterval.Daily;
            Series estimated = new Series();

            estimated.Name = "estimated";
            estimated.Add(DateTime.Parse("2001-1-02"), 2);
            estimated.Add(DateTime.Parse("2001-1-03"), 2);
            estimated.Add(DateTime.Parse("2000-12-25"), 2);
            estimated.Add(DateTime.Parse("2001-12-26"), 2);
            estimated.TimeInterval = TimeInterval.Daily;

            CalculationSeries c = new CalculationSeries();

            //c.SetMissingDataToZero = true;
            c.TimeInterval = TimeInterval.Daily;
            c.Name         = "merged";
            c.Expression   = "Merge(observed, estimated)";
            _db.AddSeries(observed);
            _db.AddSeries(estimated);
            _db.AddSeries(c);

            c.Calculate();

            Assert.AreEqual(2, c["2001-1-03"].Value, 0.0001);

            c.WriteToConsole();
        }
        public void Constant()
        {
            _svr.CreateDataBase(_fn);

            CalculationSeries c = new CalculationSeries();

            c.TimeInterval = TimeInterval.Daily;
            c.Name         = "constant_series";
            c.Expression   = "15.0";
            _db.AddSeries(c);

            DateTime t1 = new DateTime(2014, 10, 1);
            DateTime t2 = new DateTime(2014, 10, 5);

            c.Calculate(t1, t2);

            Assert.AreEqual(5, c.Count);
            Assert.AreEqual(15, c[0].Value, 0.001);
        }
Beispiel #17
0
        public void MathMax()
        {
            //DailyCalculator.
            DateTime       t  = new DateTime(2012, 12, 6);
            ConstantSeries s1 = new ConstantSeries("s1", "aa", -1, TimeInterval.Daily);

            s1.TimeInterval = TimeInterval.Daily;
            Reclamation.TimeSeries.Parser.SeriesExpressionParser.Debug = true;
            var s = new CalculationSeries();

            s.Expression              = "Max(s1,0)";
            s.TimeInterval            = TimeInterval.Daily;
            s.Parser.VariableResolver = new VariableResolver();
            s.Parser.VariableResolver.Add("s1", s1);

            s.Calculate(t, t.AddDays(2));
            s.WriteToConsole();
            Assert.AreEqual(3, s.Count);
        }
Beispiel #18
0
        private static void MonthlyCalculation(SpreadsheetRange ssRng, string cbtt, string pcode)
        {
            var db = Database.DB();

            CalculationSeries series = db.GetCalculationSeries(cbtt, pcode, TimeInterval.Monthly);

            if (series == null)
            {
                return;
            }

            var rng = ssRng.SelectedDateRange;

            series.Calculate(rng.DateTime1.FirstOfMonth(), rng.DateTime2.EndOfMonth());

            series.Name = "new";
            var old = ssRng.SelectionToMonthlySeries(false);

            old.Name = "old";

            var diff = series - old;

            diff.Name = "Difference";

            var list = new SeriesList();

            list.Add(series);
            list.Add(old);
            list.Add(diff);

            var dlg = new MonthlyCalculationPreview();

            dlg.DataSource = list.ToDataTable(true);


            if (dlg.ShowDialog() == DialogResult.OK)
            {
                // insert into range..
                ssRng.InsertSeriesValues(series, dlg.SelectedFlag);
            }
        }
Beispiel #19
0
        public void ConvertToCelcius()
        {
            var    fn = Path.Combine(TestData.DataPath, "CalculationTests.xlsx");
            Series s  = new ExcelDataReaderSeries(fn, "ConvertToCelcius", "Date", "value");

            s.Units = "degrees C";
            s.Name  = "series1";

            CalculationSeries c = new CalculationSeries();

            c.Parser.VariableResolver.Add("series1", s);
            c.Expression = "5/9*(series1-32)";
            c.Read();

            // c.WriteToConsole();
            c.Clear();
            c.Calculate(); //t,t.AddDays(3));
            c.WriteToConsole();
            Assert.AreEqual(0, c[0].Value, 0.01);
            Assert.AreEqual(100, c[1].Value, 0.01);
        }
Beispiel #20
0
        public void UnregulatedFlow()
        {
            var    fn  = Path.Combine(TestData.DataPath, "calculationtests_jck_qu.csv");
            var    csv = new CsvFile(fn);
            Series af  = new DataTableSeries(csv, TimeInterval.Daily, "Date", "jck af");
            Series qd  = new DataTableSeries(csv, TimeInterval.Daily, "Date", "jck qd");

            var qu = new CalculationSeries();

            qu.Parser.VariableResolver.Add("jck_af", af);
            qu.Parser.VariableResolver.Add("jck_qd", qd);

            qu.Expression = "(jck_af[t]-jck_af[t-1])/1.98347+jck_qd";

            DateTime t1 = DateTime.Parse("12/25/2010");
            DateTime t2 = DateTime.Parse("12/27/2010");

            qu.Calculate(t1, t2);

            qu.WriteToConsole();
            Assert.AreEqual(674.39, qu["12/26/2010"].Value, .01);
        }
Beispiel #21
0
        public void ConvertToCelcius()
        {
            var    fn  = Path.Combine(TestData.DataPath, "calculationtests_convert.csv");
            var    csv = new CsvFile(fn);
            Series s   = new DataTableSeries(csv, TimeInterval.Daily, "Date", "value");

            s.Units = "degrees C";
            s.Name  = "series1";

            CalculationSeries c = new CalculationSeries();

            c.Parser.VariableResolver.Add("series1", s);
            c.Expression = "5/9*(series1-32)";
            c.Read();

            // c.WriteToConsole();
            c.Clear();
            c.Calculate(); //t,t.AddDays(3));
            c.WriteToConsole();
            Assert.AreEqual(0, c[0].Value, 0.01);
            Assert.AreEqual(100, c[1].Value, 0.01);
        }
Beispiel #22
0
        public void UnregulatedFlow()
        {
            var fn = Path.Combine(TestData.DataPath, "CalculationTests.xlsx");

            Series af = new ExcelDataReaderSeries(fn, "jck_qu", "Date", "jck af");
            Series qd = new ExcelDataReaderSeries(fn, "jck_qu", "Date", "jck qd");

            var qu = new CalculationSeries();

            qu.Parser.VariableResolver.Add("jck_af", af);
            qu.Parser.VariableResolver.Add("jck_qd", qd);

            qu.Expression = "(jck_af[t]-jck_af[t-1])/1.98347+jck_qd";

            DateTime t1 = DateTime.Parse("12/25/2010");
            DateTime t2 = DateTime.Parse("12/27/2010");

            qu.Calculate(t1, t2);

            qu.WriteToConsole();
            Assert.AreEqual(674.39, qu["12/26/2010"].Value, .01);
        }
Beispiel #23
0
        public void TestGenericWeirAtStAnthonyUnionFeederCanal()
        {
            // bully creek feder canal
            Series   ch = new Series("afci_ch");
            DateTime t1 = DateTime.Parse("2014-6-11 1:15");
            DateTime t2 = DateTime.Parse("2014-6-11 14:00");

            ch.AfterRead += new EventHandler(delegate(object o, EventArgs a)
            {
                AddSomeData(ch, t1, t2);
            });
            AddSomeData(ch, t1, t2);
            ch.Read();
            Series tmp = Reclamation.TimeSeries.Math.GenericWeir(ch, 1.2, 28.5, 1.5);

            Assert.AreEqual(2, tmp.Count);


            ch.Properties.Set("shift", "-0.22");
            //6/11/2014  1:15	0.67	    60.4
            //6/11/2014 14:00	0.08	    31.1

            Logger.EnableLogger();
            SeriesExpressionParser.Debug = true;
            var c = new CalculationSeries("canal_flow");

            c.Parser.VariableResolver.Add("afci_ch", new ParserResult(ch));
            c.TimeInterval = TimeInterval.Irregular;

            c.Expression = "GenericWeir(afci_ch,1.2,28.5,1.5)";
            c.Calculate();


            Assert.AreEqual(2, c.Count, " expected two flow calculations");
            Assert.AreEqual(60.4, c[t1].Value, 0.1);
            Assert.AreEqual(31.1, c[t2].Value, 0.1);
        }
        private static void AddSeries(string period, string scenario,
                                      TimeSeriesDatabase db, VariableResolver vr,
                                      string name, string equation, ModsimType mType)
        {
            if (name.Trim().ToLower() == "nan")
            {
                return;
            }

            if (name.Trim() == "")
            {
                Console.WriteLine("--- WARNING modsim Node Name is missing. Type = " +
                                  mType.ToString());
                return;
            }

            CalculationSeries cs = new CalculationSeries(name);

            cs.TimeInterval = TimeInterval.Monthly;

            if (mType == ModsimType.Gain)
            {
                cs.Expression = string.Format("Max({0}, 0)", equation);
            }
            if (mType == ModsimType.Negative)
            {
                cs.Expression = string.Format("Abs(Min({0}, 0))", equation);
            }
            cs.TimeSeriesDatabase      = db;
            cs.Parser.VariableResolver = vr;
            cs.Units = "cfs";
            db.AddSeries(cs); // add series before calcualte to get an id assigned.
            cs.Calculate();   // Calculate also saves the data.

            SetSeriesDatesToBeginningOfMonth(cs);
            ConsolePrintSeriesNameAndCount(cs);
        }
Beispiel #25
0
        /// <summary>
        /// Adds new Calculation Series
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void AddCalculationClick(object sender, EventArgs e)
        {
            CalculationSeries s = new CalculationSeries(DB);
            CalculationProperties p = new CalculationProperties(s, new TimeSeriesTreeModel( DB), DB.GetUniqueUnits());

            if (p.ShowDialog() == DialogResult.OK)
            {
                DB.AddSeries(s, CurrentFolder);
                // tree refresh.. Add node.
                if( p.Calculate)
                        s.Calculate(); // save again
                // refresh.
                DB.RefreshFolder(CurrentFolder);

            }
        }