예제 #1
0
        ///// <summary>
        ///// Test Multiple Linear Regression (MLR)
        ///// with simple two series example.
        ///// Series1 is twice the value of Series2 (Jan,Feb,Mar)
        ///// Series1 is 3 times the value of Series2 (Apr...Dec)
        ///// </summary>
        //[Test]
        //public void SimpleMonthlyMLRJanFebMar()
        //{
        //    CreateSeries();

        //   var x = MultipleLinearRegression.MlrInterpolation(list, new int[] { 1, 2,3 }, .7, true);
        //   Console.WriteLine("Results:");
        //   x.EstimatedSeries.WriteToConsole();
        //   Console.WriteLine(String.Join("\n", x.Report));
        //   Point feb = x.EstimatedSeries["2000-2-1"];
        //   Assert.AreEqual(2, feb.Value,0.01, "Feb value should be 2");
        //   Point may = x.EstimatedSeries["2000-5-1"];
        //   Assert.IsTrue(may.IsMissing, "Value in May should not be estimated");
        //}
        ///// <summary>
        ///// Test Multiple Linear Regression (MLR)
        ///// with simple two series example.
        ///// </summary>
        //[Test]
        //public void SimpleMonthlyMLRAprMayJun()
        //{
        //    CreateSeries();

        //    var y = MultipleLinearRegression.MlrInterpolation(list, new int[] { 4, 5, 6 }, .7, true);
        //    y.EstimatedSeries.WriteToConsole();
        //    var may = y.EstimatedSeries["2000-5-1"];
        //    Assert.AreEqual(5, may.Value,0.01, "may value should be 5");
        //    var feb = y.EstimatedSeries["2000-2-1"];
        //    Assert.IsTrue(feb.IsMissing, "Value in Feb should not be estimated");

        //}
        /// Series1 is twice the value of Series2 (Jan,Feb,Mar)
        /// Series1 is 3 times the value of Series2 (Apr...Dec)
        private void CreateSeries()
        {
            s1      = new Series("", TimeInterval.Monthly);
            s1.Name = "snuggles";
            s2      = new Series("", TimeInterval.Monthly);
            s2.Name = "myza";
            DateTime t    = new DateTime(2000, 1, 1);
            double   val1 = t.Month;
            double   val2 = 0;

            while (t.Year == 2000)
            {
                val1 = t.Month;
                if (t.Month <= 3)
                {
                    val2 = val1 * 2;
                }
                else
                {
                    val2 = val1 * 3;
                }

                if (t.Month == 5)
                {
                    s1.AddMissing(t);
                }
                else if (t.Month == 2)
                {
                    s1.AddMissing(t);
                }
                else
                {
                    s1.Add(t, val1, "");
                }

                s2.Add(t, val2, "");


                t = t.AddMonths(1).FirstOfMonth();
            }
            list = new SeriesList();
            list.Add(s1);
            list.Add(s2);
            t1 = s1.MinDateTime;
            t2 = s1.MaxDateTime;
            Console.WriteLine("Input Series 1");
            s1.WriteToConsole();
            Console.WriteLine("Input Series 2");
            s2.WriteToConsole();
        }
예제 #2
0
        public void CountFlags()
        {
            DateTime t = DateTime.Now.Date;
            Series   s = new Series();

            s.AddMissing(t);
            t = t.AddDays(1);
            s.AddMissing(t);
            t = t.AddDays(1);
            s.Add(t, 123);

            Assert.AreEqual(3, s.Count);
            Assert.AreEqual(2, s.CountMissing());
        }
예제 #3
0
        public static Series DailyChange(Series s)
        {
            Series rval = new Series();

            rval.TimeInterval = TimeInterval.Daily;
            if (s.Count == 0)
            {
                return(new Series());
            }

            var t = s.MinDateTime.Date;

            while (t <= s.MaxDateTime)
            {
                int idx1 = s.IndexOf(t.Date);            // midnight
                int idx2 = s.IndexOf(t.AddDays(1).Date); // midnight

                if (idx1 >= 0 && idx2 >= 0 && !s[idx1].IsMissing && !s[idx2].IsMissing)
                {
                    double diff = s[idx2].Value - s[idx1].Value;
                    rval.Add(t, diff);
                }
                else
                {
                    rval.AddMissing(t);
                }

                t = t.AddDays(1).Date;
            }


            return(rval);
        }
예제 #4
0
        private Series CreateRuleLine(double forecast, DateTime t1, DateTime t2)
        {
            Series s = new Series();

            s.Name         = (forecast / 1000000.0).ToString("F2");
            s.TimeInterval = TimeInterval.Daily;
            string   flag = "";
            DateTime t    = t1;

            while (t <= t2)
            {
                var t3 = t;
                if (t.Month == 2 && t.Day == 29)
                {
                    t3 = t.AddDays(-1);
                }

                var d = LookupRequiredSpace(t3, forecast, out flag);
                if (d == HydrometRuleCurve.MissingValue)
                {
                    s.AddMissing(t);
                }
                else
                {
                    s.Add(t, d);
                }

                t = t.Date.AddDays(1);
            }
            return(s);
        }
예제 #5
0
        public static Series DailyMidnight(Series s)
        {
            Series rval = new Series();

            rval.TimeInterval = TimeInterval.Daily;
            if (s.Count == 0)
            {
                return(rval);
            }

            var t = s.MinDateTime.Date;

            while (t <= s.MaxDateTime)
            {
                int idx = s.IndexOf(t.AddDays(1).Date); // midnight
                if (idx >= 0)
                {
                    rval.Add(t, s[idx].Value);
                }
                else
                {
                    rval.AddMissing(t);
                }

                t = t.AddDays(1).Date;
            }


            return(rval);
        }
예제 #6
0
        public static Series HydrometResidualForecast(Series forecast, Series runoff, Series residual)
        {
            var rval = new Series();

            rval.TimeInterval = TimeInterval.Daily;

            if (runoff.Count < 30)
            {
                Logger.WriteLine("Missing or not enough runoff data: HydrometResidualForecast()");
                return(residual);
            }


            if (residual.Count == 0)
            {
                residual.Add(runoff[0].DateTime, 0);
            }
            double resid = residual[0].Value;



            for (int i = 0; i < runoff.Count; i++)
            {
                var t = runoff[i].DateTime;

                resid = ResetResidualBasedOnForecast(t, forecast, resid);

                bool missing = resid == Point.MissingValueFlag;

                if (!missing && !runoff[i].IsMissing && t <= runoff.MaxDateTime)
                {
                    var quTemp = runoff[t].Value;
                    if (quTemp < 0)
                    {
                        quTemp = 0;
                    }
                    resid = resid - quTemp * 1.98347;
                    if (resid < 0)
                    {
                        resid = 0;
                    }

                    rval.Add(t, resid);
                }
                else
                {
                    rval.AddMissing(t);
                    Console.WriteLine("Missing data: incremental: " + runoff[i].ToString());
                    missing = true;
                }
            }
            return(rval);
        }
예제 #7
0
파일: Math.cs 프로젝트: jmptrader/Pisces
 private static void DoAverage(Series sOut, DateTime t, Series sTemp)
 {
     if (sTemp.Count == 0)
     {
         sOut.AddMissing(t);
     }
     else
     {
         double avgVal = sTemp.Values.Average();
         sOut.Add(t, avgVal);
     }
 }
예제 #8
0
        public Series SelectionToMonthlySeries(bool endOfMonth = true)
        {
            var s = new Series();

            s.TimeInterval = TimeInterval.Monthly;
            if (!ValidCalculationRange)
            {
                return(s);
            }
            var t = SelectedDateRange.DateTime1.FirstOfMonth();

            if (endOfMonth)
            {
                t = SelectedDateRange.DateTime1.EndOfMonth();
            }

            //var t2 = SelectedDateRange.DateTime2;

            try
            {
                rng.WorkbookSet.GetLock();

                for (int i = 0; i < rng.RowCount; i++)
                {
                    if (rng[i, 0].Value == null)
                    {
                        s.AddMissing(t);
                    }
                    else
                    {
                        var val = Convert.ToDouble(rng[i, 0].Value);
                        s.Add(t, val);
                    }
                    if (endOfMonth)
                    {
                        t = t.AddMonths(1).EndOfMonth();
                    }
                    else
                    {
                        t = t.AddMonths(1).FirstOfMonth();
                    }
                }
            }
            finally
            {
                rng.WorkbookSet.ReleaseLock();
            }


            return(s);
        }
예제 #9
0
        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();
        }
예제 #10
0
        public static Series DailyWaterYearRunningTotal(Series incremental, Series cumulative)
        {
            Series rval = new Series();

            rval.TimeInterval = TimeInterval.Daily;
            if (incremental.Count == 0 || cumulative.Count == 0 ||
                cumulative[0].IsMissing)
            {
                Console.WriteLine("Mising data: ");
                Console.WriteLine("incremental");
                incremental.WriteToConsole();
                Console.WriteLine("cumulative");
                cumulative.WriteToConsole();
                return(rval);
            }


            double sum     = cumulative[0].Value;
            bool   missing = false;

            rval.Add(cumulative[0]);
            for (int i = 1; i < incremental.Count; i++)
            {
                var t = incremental[i].DateTime;
                if (t.Month == 10 && t.Day == 1)
                {
                    sum = 0.0;
                }

                if (!missing && !incremental[i].IsMissing)
                {
                    sum += incremental[i].Value;
                    rval.Add(incremental[i].DateTime, sum);
                }
                else
                {
                    rval.AddMissing(incremental[i].DateTime);
                    Console.WriteLine("Missing data: incremental: " + incremental[i].ToString());
                    missing = true;
                }
            }

            return(rval);
        }
예제 #11
0
        /// <summary>
        /// median list into one time series
        /// </summary>
        /// <returns>series that is median of all series in list</returns>
        public Series Median()
        {
            if (this.Count == 1)
            {
                return(this[0]);
            }
            DataTable dt = this.ToDataTable(true);
            Series    s  = new Series();
            Point     pt = new Point();

            foreach (DataRow row in dt.Rows)
            {
                List <double> values = new List <double>();
                foreach (object item in row.ItemArray)
                {
                    if (item is DateTime)
                    {
                        pt.DateTime = DateTime.Parse(item.ToString());
                    }
                    else if (item is DBNull)
                    {
                        break;
                    }
                    else
                    {
                        values.Add(double.Parse(item.ToString()));
                    }
                }
                if (values.Count != this.Count)
                {
                    s.AddMissing(pt.DateTime);
                }
                else
                {
                    values.Sort();
                    int    size   = values.Count;
                    int    mid    = size / 2;
                    double median = (size % 2 != 0) ? values[mid] : (values[mid] + values[mid - 1]) / 2;
                    s.Add(pt.DateTime, median);
                }
            }
            return(s);
        }
예제 #12
0
        /// <summary>
        /// Add or Subtract ignoring dates.
        /// </summary>
        /// <param name="c"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        private SeriesList AddIgnoreDates(SeriesList a, SeriesList b, bool subtract = false)
        {
            if (a.Count != b.Count)
            {
                throw new ArgumentException("Error: number of series is not the same in the two lists");
            }

            var rval = new SeriesList();

            for (int i = 0; i < a.Count; i++)
            {
                var sa = a[i];
                var sb = b[i];
                if (sa.Count != sb.Count)
                {
                    throw new ArgumentException("Error with subtraction: series must have the same number of points");
                }
                Series s = new Series();
                s.TimeInterval = sa.TimeInterval;
                rval.Add(s);
                for (int j = 0; j < sa.Count; j++)
                {
                    if (sa[j].IsMissing || sb[j].IsMissing)
                    {
                        s.AddMissing(sa[j].DateTime);
                    }
                    else
                    {
                        if (subtract)
                        {
                            s.Add(sa[j].DateTime, sa[j].Value - sb[j].Value);
                        }
                        else
                        {
                            s.Add(sa[j].DateTime, sa[j].Value + sb[j].Value);
                        }
                    }
                }
            }

            return(rval);
        }
예제 #13
0
파일: Math.cs 프로젝트: jmptrader/Pisces
        internal static Series Pow(Series s, double p)
        {
            Series rval = s.Clone();

            Series.CopyAttributes(s, rval);

            for (int i = 0; i < s.Count; i++)
            {
                Point pt = s[i];

                if (pt.IsMissing)
                {
                    rval.AddMissing(pt.DateTime);
                }
                else
                {
                    double x = System.Math.Pow(pt.Value, p);
                    rval.Add(pt.DateTime, x);
                }
            }
            return(rval);
        }
예제 #14
0
        /// <summary>
        /// Creates a list of Series , one subset for each day
        /// used for math functions that need subset by day
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private static Series DailySubsetCalculatorValue(Series s, Func <Series, double> func,
                                                         int requiredNumberOfPoints = 0)
        {
            Series rval = new Series();

            rval.TimeInterval = TimeInterval.Daily;

            DateTime t;

            if (s.Count > 0)
            {
                t = s[0].DateTime.Date; // midnight
            }
            else
            {
                return(rval);
            }

            while (t < s.MaxDateTime)
            {
                var subset = Math.Subset(s, new DateRange(t, t.AddDays(1)), false);
                subset.RemoveMissing(true);

                if (subset.Count < requiredNumberOfPoints)
                {
                    Logger.WriteLine("Error: not enough data points " + s.Name);
                    Logger.WriteLine("expected " + requiredNumberOfPoints + " points, but had only " + subset.Count);
                    rval.AddMissing(t);
                }
                else
                if (subset.Count > 0)
                {
                    double d = func(subset);
                    rval.Add(t, d, PointFlag.Computed);
                }
                t = t.AddDays(1).Date;
            }
            return(rval);
        }
예제 #15
0
        public static Series DailyMidnight(Series s)
        {
            Series rval = new Series();
            rval.TimeInterval = TimeInterval.Daily;
            if (s.Count == 0)
                return rval;

            var t = s.MinDateTime.Date;

            while (t <= s.MaxDateTime)
            {
                int idx = s.IndexOf(t.AddDays(1).Date); // midnight
                if (idx >= 0)
                    rval.Add(t,s[idx].Value);
                else
                    rval.AddMissing(t);

                t = t.AddDays(1).Date;
            }

            return rval;
        }
예제 #16
0
        public static Series DailyChange(Series s)
        {
            Series rval = new Series();
            rval.TimeInterval = TimeInterval.Daily;
            if (s.Count == 0)
                return new Series();

            var t = s.MinDateTime.Date;

            while (t <= s.MaxDateTime)
            {

                int idx1 = s.IndexOf(t.Date); // midnight
                int idx2 = s.IndexOf(t.AddDays(1).Date); // midnight

                if (idx1 >= 0 && idx2 >= 0 && !s[idx1].IsMissing && !s[idx2].IsMissing)
                {
                    double diff = s[idx2].Value - s[idx1].Value;
                    rval.Add(t, diff);
                }
                else
                {
                    rval.AddMissing(t);
                }

                t = t.AddDays(1).Date;
            }

            return rval;
        }
예제 #17
0
        /// <summary>
        /// Method to call the data download API, get the JSON reponse, and convert to Series()
        /// </summary>
        /// <param name="station"></param>
        /// <param name="parameter"></param>
        /// <param name="t1"></param>
        /// <param name="t2"></param>
        /// <returns></returns>
        private Series IdwrApiDownload(string station, string parameter,
                                       DateTime t1, DateTime t2)
        {
            var rval = new Series();

            var yearlist = YearsToQuery(station, t1, t2);

            var jsonResponse = new List <TsData>();

            try
            {
                if (dataType == DataType.HST)
                {
                    jsonResponse = IdwrApiQuerySiteDataHST(station, yearlist);
                }
                else
                {
                    jsonResponse = IdwrApiQuerySiteDataALC(station, yearlist);
                }
            }
            catch
            {
                return(rval);
            }

            foreach (var item in jsonResponse)
            {
                var t = DateTime.Parse(item.Date);
                if (t >= t1 && t <= t2)
                {
                    string value = "";
                    switch (parameter)
                    {
                    case ("GH"):
                        value = item.GH;
                        break;

                    case ("FB"):
                        value = item.FB;
                        break;

                    case ("AF"):
                        value = item.AF;
                        break;

                    case ("QD"):
                        value = item.QD;
                        break;

                    default:
                        value = "NaN";
                        break;
                    }
                    if (value == "NaN")
                    {
                        rval.AddMissing(t);
                    }
                    else
                    {
                        rval.Add(item.Date, Convert.ToDouble(value));
                    }
                }
            }

            return(rval);
        }
예제 #18
0
        internal Series ConvertToDaily()
        {
            Series estimatedDaily = new Series();
            estimatedDaily.HasFlags = true;
            estimatedDaily.TimeInterval = TimeInterval.Daily;

            if (FillMissingWithZero)
            {
                daily = Math.FillMissingWithZero(daily, daily.MinDateTime, daily.MaxDateTime);
            }
            else
            {
                daily.RemoveMissing();
            }
            //daily.RemoveMissing();

            //int[] levels = {5,10,20,30,40,50,60,70,80,90,95};
            //int[] levels = {10,20,30,40,50,60,70,80,90};

            List<int> levels = new List<int>();

            if (MedianOnly)
            {
                levels.Add(50);
            }
            else
            {
                for (int i = 5; i <= 95; i += 2)
                {
                    levels.Add(i);
                }
            }

            var sHydrograph = Math.SummaryHydrograph(daily, levels.ToArray(), new DateTime(2008, 1, 1), false, false, false, false);//, false);
            var summaryHydrographTable = sHydrograph.ToDataTable(true);

            for (int i = 1; i < summaryHydrographTable.Columns.Count; i++)
            {
                summaryHydrographTable.Columns[i].ColumnName = levels[i - 1].ToString();
            }

            //DataTableOutput.Write(summaryHydrographTable, @"c:\temp\junk.csv", false);

            SeriesList monthlySum = new SeriesList();
            for (int i = 0; i < sHydrograph.Count; i++)
            {
                Series sum = Math.MonthlyValues(sHydrograph[i], Math.Sum);
                sum.Name = levels[i].ToString();
                monthlySum.Add(sum);
            }

            var monthlyExceedanceSums = monthlySum.ToDataTable(true);
            if (monthlySum.Count == 1 && levels.Count == 1)
                monthlyExceedanceSums.Columns[1].ColumnName = levels[0].ToString();

            var monthlyTable = monthly.Table;

            DateTime t = monthly.MinDateTime;
            DateTime t2 = monthly.MaxDateTime;
            t2 = new DateTime(t2.Year, t2.Month, DateTime.DaysInMonth(t2.Year, t2.Month));

            while (t < t2)
            {
                var tm = new DateTime(t.Year, t.Month, 1);
                if (monthly.IndexOf(tm) < 0)
                {
                    estimatedDaily.AddMissing(t);
                }
                else
                {
                    double mv = monthly[tm].Value;
                    double mvcfsdays = mv / 1.98347;
                    double exceedanceValue = 0;
                    int exceedancePercent = LookupExceedance(monthlyExceedanceSums, t, mvcfsdays, out exceedanceValue);
                    double ratio = 0;
                    if (exceedanceValue != 0)
                        ratio = mvcfsdays / exceedanceValue;
                    else
                        ratio = 0;

                    double shcfs = LookupSummaryHydrograph(summaryHydrographTable, t, exceedancePercent);

                    estimatedDaily.Add(t, shcfs * ratio,"scaled with "+exceedancePercent+"%");
                }
                t = t.AddDays(1);
            }

            VerifyWithMonthlyVolume(monthly, estimatedDaily);
              //  SmoothSpikes(monthly, daily, estimatedDaily);

            return estimatedDaily;
        }
예제 #19
0
        internal Series ConvertToDaily()
        {
            Series estimatedDaily = new Series();

            estimatedDaily.HasFlags     = true;
            estimatedDaily.TimeInterval = TimeInterval.Daily;


            if (FillMissingWithZero)
            {
                daily = Math.FillMissingWithZero(daily, daily.MinDateTime, daily.MaxDateTime);
            }
            else
            {
                daily.RemoveMissing();
            }
            //daily.RemoveMissing();

            //int[] levels = {5,10,20,30,40,50,60,70,80,90,95};
            //int[] levels = {10,20,30,40,50,60,70,80,90};

            List <int> levels = new List <int>();

            if (MedianOnly)
            {
                levels.Add(50);
            }
            else
            {
                for (int i = 5; i <= 95; i += 2)
                {
                    levels.Add(i);
                }
            }

            var sHydrograph            = Math.SummaryHydrograph(daily, levels.ToArray(), new DateTime(2008, 1, 1), false, false, false, false);//, false);
            var summaryHydrographTable = sHydrograph.ToDataTable(true);

            for (int i = 1; i < summaryHydrographTable.Columns.Count; i++)
            {
                summaryHydrographTable.Columns[i].ColumnName = levels[i - 1].ToString();
            }

            //DataTableOutput.Write(summaryHydrographTable, @"c:\temp\junk.csv", false);


            SeriesList monthlySum = new SeriesList();

            for (int i = 0; i < sHydrograph.Count; i++)
            {
                Series sum = Math.MonthlyValues(sHydrograph[i], Math.Sum);
                sum.Name = levels[i].ToString();
                monthlySum.Add(sum);
            }

            var monthlyExceedanceSums = monthlySum.ToDataTable(true);

            if (monthlySum.Count == 1 && levels.Count == 1)
            {
                monthlyExceedanceSums.Columns[1].ColumnName = levels[0].ToString();
            }

            var monthlyTable = monthly.Table;

            DateTime t  = monthly.MinDateTime;
            DateTime t2 = monthly.MaxDateTime;

            t2 = new DateTime(t2.Year, t2.Month, DateTime.DaysInMonth(t2.Year, t2.Month));


            while (t < t2)
            {
                var tm = new DateTime(t.Year, t.Month, 1);
                if (monthly.IndexOf(tm) < 0)
                {
                    estimatedDaily.AddMissing(t);
                }
                else
                {
                    double mv                = monthly[tm].Value;
                    double mvcfsdays         = mv / 1.98347;
                    double exceedanceValue   = 0;
                    int    exceedancePercent = LookupExceedance(monthlyExceedanceSums, t, mvcfsdays, out exceedanceValue);
                    double ratio             = 0;
                    if (exceedanceValue != 0)
                    {
                        ratio = mvcfsdays / exceedanceValue;
                    }
                    else
                    {
                        ratio = 0;
                    }

                    double shcfs = LookupSummaryHydrograph(summaryHydrographTable, t, exceedancePercent);

                    estimatedDaily.Add(t, shcfs * ratio, "scaled with " + exceedancePercent + "%");
                }
                t = t.AddDays(1);
            }

            VerifyWithMonthlyVolume(monthly, estimatedDaily);
            //  SmoothSpikes(monthly, daily, estimatedDaily);

            return(estimatedDaily);
        }
예제 #20
0
        /// <summary>
        /// Residual is forecast minus QU
        /// </summary>
        private void ComputeResidualAndRequiredSpace()
        {
            SpaceRequired.Clear();
            SpaceRequired.Name = "Space Required";
            Residual.Clear();
            Residual.Name = "Residual";
            DateTime t     = _t1;
            double   resid = 0; // 998877;

            int wy = t.Year;

            if (t.Month > 9)
            {
                wy++;
            }



            m_ruleCurve = RuleCurveFactory.Create(controlPoint, wy);

            while (t <= _t2)
            {
                if (controlPoint.FillType == FillType.Variable)
                {
                    resid = ResetResidualBasedOnForecast(t, resid);

                    if (resid != HydrometRuleCurve.MissingValue && t <= qu.MaxDateTime)
                    {
                        var quTemp = qu[t].Value;
                        if (quTemp < 0)
                        {
                            quTemp = 0;
                        }
                        resid = resid - quTemp * 1.98347;
                        if (resid < 0)
                        {
                            resid = 0;
                        }

                        Residual.Add(t, resid);
                    }
                    else
                    {
                        Residual.AddMissing(t);
                    }
                }
                else
                {
                    resid = HydrometRuleCurve.MissingValue;
                }
                var t2 = t;
                if (t.Month == 2 && t.Day == 29)
                {
                    t2 = t.AddDays(-1);
                }
                string flag = "";
                double req  = 0;

                req = m_ruleCurve.LookupRequiredSpace(t2, resid, out flag);

                if (req == HydrometRuleCurve.MissingValue)
                {
                    SpaceRequired.AddMissing(t);
                }
                else
                {
                    req = req * controlPoint.PercentSpace / 100.0;
                    if (req < 0)
                    {
                        req = 0;
                    }
                    SpaceRequired.Add(t, req);
                }
                flags.Add(flag);

                t = t.AddDays(1);
            }
        }
예제 #21
0
        /// <summary>
        /// Method to call the data download API, get the JSON reponse, and convert to Series()
        /// </summary>
        /// <param name="station"></param>
        /// <param name="parameter"></param>
        /// <param name="t1"></param>
        /// <param name="t2"></param>
        /// <returns></returns>
        private Series IdwrApiDownload(string station, string parameter,
                                       DateTime t1, DateTime t2)
        {
            var rval = new Series();

            var yearlist = YearsToQuery(station, t1, t2);

            try
            {
                var pars = parameter.Split('.');
                if (pars.Count() > 1)
                {
                    if (pars[0].ToLower() != "hst")
                    {
                        dataType = DataType.ALC;
                    }
                    parameter = pars[1];
                }
                if (dataType == DataType.HST)
                {
                    var jsonResponse = IdwrApiQuerySiteDataHST(station, yearlist);
                    foreach (var item in jsonResponse)
                    {
                        var t = DateTime.Parse(item.Date);
                        if (t >= t1 && t <= t2)
                        {
                            string value = "";
                            switch (parameter)
                            {
                            case ("GH"):
                                value = item.GH;
                                break;

                            case ("FB"):
                                value = item.FB;
                                break;

                            case ("AF"):
                                value = item.AF;
                                break;

                            case ("QD"):
                                value = item.QD;
                                break;

                            default:
                                value = "NaN";
                                break;
                            }
                            if (value == "NaN")
                            {
                                rval.AddMissing(t);
                            }
                            else
                            {
                                rval.Add(item.Date, Convert.ToDouble(value));
                            }
                        }
                    }
                }
                else
                {
                    var jsonResponse = IdwrApiQuerySiteDataALC(station, yearlist);
                    foreach (var item in jsonResponse)
                    {
                        var t = DateTime.Parse(item.Date);
                        if (t >= t1 && t <= t2)
                        {
                            string value = "";
                            switch (parameter)
                            {
                            case ("NATQ"):
                                value = item.NATQ;
                                break;

                            case ("ACTQ"):
                                value = item.ACTQ;
                                break;

                            case ("STRQ"):
                                value = item.STRQ;
                                break;

                            case ("GANQ"):
                                value = item.GANQ;
                                break;

                            case ("EVAP"):
                                value = item.EVAP;
                                break;

                            case ("TOTEVAP"):
                                value = item.TOTEVAP;
                                break;

                            case ("STORACC"):
                                value = item.STORACC;
                                break;

                            case ("TOTACC"):
                                value = item.TOTACC;
                                break;

                            case ("CURSTOR"):
                                value = item.CURSTOR;
                                break;

                            case ("DIV"):
                                value = item.DIV;
                                break;

                            case ("TOTDIVVOL"):
                                value = item.TOTDIVVOL;
                                break;

                            case ("STORDIV"):
                                value = item.STORDIV;
                                break;

                            case ("STORDIVVOL"):
                                value = item.STORDIVVOL;
                                break;

                            case ("STORBAL"):
                                value = item.STORBAL;
                                break;

                            default:
                                value = "NaN";
                                break;
                            }
                            if (value == "NaN")
                            {
                                rval.AddMissing(t);
                            }
                            else
                            {
                                rval.Add(item.Date, Convert.ToDouble(value));
                            }
                        }
                    }
                }
            }
            catch
            {
                return(rval);
            }

            return(rval);
        }
예제 #22
0
        private static Series DailyCumulative(Series incremental, Series cumulative, int resetMonth, int resetDay)
        {
            //cumulative.Normalize();
            //incremental.Normalize();

            DateTime t;
            Series   rval = new Series();

            rval.TimeInterval = TimeInterval.Daily;
            if (incremental.Count == 0)
            {
                Console.WriteLine("there is no data ");
                return(rval);
            }
            if (incremental.TimeInterval != TimeInterval.Daily || cumulative.TimeInterval != TimeInterval.Daily)
            {
                throw new ArgumentException("Error: arguments must both have daily interval");
            }

            double sum     = 0.0;
            bool   missing = false;
            bool   primed  = false;
            int    index   = 0;

            t = GetRunningTotalStartDate(incremental, cumulative);
            if (cumulative.IndexOf(t) >= 0 && !cumulative[t].IsMissing)
            {
                sum = cumulative[t].Value;
                rval.Add(cumulative[t]);
                primed = true;
                index  = incremental.IndexOf(t) + 1;
            }
            else
            {
                index = incremental.IndexOf(t);
            }


            for ( ; index < incremental.Count; index++)
            {
                var t2 = incremental[index].DateTime;
                if (t2.Month == resetMonth && t2.Day == resetDay)
                {
                    sum     = 0.0;
                    primed  = true;
                    missing = false;
                }

                if (primed && !missing && !incremental[index].IsMissing)
                {
                    sum += incremental[index].Value;
                    rval.Add(incremental[index].DateTime, sum);
                }
                else
                {
                    rval.AddMissing(incremental[index].DateTime);
                    Console.WriteLine("Missing data: incremental: " + incremental[index].ToString());
                    missing = true;
                }
            }
            return(rval);
        }
예제 #23
0
        public static Series HydrometResidualForecast(Series forecast, Series runoff, Series residual)
        {
            var rval = new Series();
            rval.TimeInterval = TimeInterval.Daily;

            if (runoff.Count < 30 )
            {
                Logger.WriteLine("Missing or not enough runoff data: HydrometResidualForecast()");
                return residual;
            }

            if (residual.Count == 0)
            {
                residual.Add(runoff[0].DateTime, 0);
            }
            double resid = residual[0].Value;

            for (int i = 0; i < runoff.Count; i++)
            {
                var t = runoff[i].DateTime;

                resid = ResetResidualBasedOnForecast(t, forecast, resid);

                bool missing = Point.IsMissingValue(resid);

                if (!missing && !runoff[i].IsMissing && t <=runoff.MaxDateTime)
                {

                    var quTemp = runoff[t].Value;
                    if (quTemp < 0)
                        quTemp = 0;
                    resid = resid - quTemp * 1.98347;
                    if (resid < 0)
                        resid = 0;

                    rval.Add(t, resid);
                }
                else
                {
                    rval.AddMissing(t);
                    Console.WriteLine("Missing data: incremental: " + runoff[i].ToString());
                    missing = true;
                }

            }
            return rval;
        }
예제 #24
0
파일: Math.cs 프로젝트: usbr/Pisces
 private static void DoAverage(Series sOut, DateTime t, Series sTemp)
 {
     if (sTemp.Count == 0)
         sOut.AddMissing(t);
     else
     {
         double avgVal = sTemp.Values.Average();
         sOut.Add(t, avgVal);
     }
 }
예제 #25
0
        private static Series DailyCumulative(Series incremental, Series cumulative, int resetMonth, int resetDay)
        {
            //cumulative.Normalize();
            //incremental.Normalize();

            DateTime t;
            Series rval = new Series();
            rval.TimeInterval = TimeInterval.Daily;
            if (incremental.Count == 0)
            {
                Console.WriteLine("there is no data ");
                return rval;
            }
            if (incremental.TimeInterval != TimeInterval.Daily || cumulative.TimeInterval != TimeInterval.Daily)
            {
                throw new ArgumentException("Error: arguments must both have daily interval");
            }

            double sum = 0.0;
            bool missing = false;
            bool primed = false;
            int index = 0;

            t = GetRunningTotalStartDate(incremental, cumulative);
            if (cumulative.IndexOf(t) >= 0 && !cumulative[t].IsMissing)
            {
                sum = cumulative[t].Value;
                rval.Add(cumulative[t]);
                primed = true;
                index = incremental.IndexOf(t) + 1;
            }
            else
            {
                index = incremental.IndexOf(t);
            }

            for ( ; index < incremental.Count; index++)
            {
                var t2 = incremental[index].DateTime;
                if (t2.Month == resetMonth && t2.Day == resetDay)
                {
                    sum = 0.0;
                    primed = true;
                    missing = false;
                }

                if (primed && !missing && !incremental[index].IsMissing)
                {
                    sum += incremental[index].Value;
                    rval.Add(incremental[index].DateTime, sum);
                }
                else
                {
                    rval.AddMissing(incremental[index].DateTime);
                    Console.WriteLine("Missing data: incremental: " + incremental[index].ToString());
                    missing = true;
                }
            }
            return rval;
        }
예제 #26
0
        /// <summary>
        /// Creates a list of Series , one subset for each day
        /// used for math functions that need subset by day
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private static Series DailySubsetCalculatorValue(Series s, Func<Series,double> func, 
            int requiredNumberOfPoints = 0)
        {
            Series rval = new Series();
            rval.TimeInterval = TimeInterval.Daily;

            DateTime t;
            if (s.Count > 0)
            {
                t = s[0].DateTime.Date; // midnight
            }
            else
            {
                return rval;
            }

            while (t < s.MaxDateTime)
            {
              var subset = Math.Subset(s, new DateRange(t, t.AddDays(1)), false);
              subset.RemoveMissing(true);

              if (subset.Count < requiredNumberOfPoints)
              {
                  Logger.WriteLine("Error: not enough data points "+s.Name);
                  Logger.WriteLine("expected "+requiredNumberOfPoints+" points, but had only "+subset.Count);
                  rval.AddMissing(t);
              }
              else
              if (subset.Count > 0)
              {
                  double d = func(subset);
                  rval.Add(t, d, PointFlag.Computed);
              }
              t = t.AddDays(1).Date;
            }
            return rval;
        }
예제 #27
0
        private Series CreateRuleLine(double forecast, DateTime t1, DateTime t2)
        {
            Series s = new Series();
            s.Name = (forecast/1000000.0 ).ToString("F2");
            s.TimeInterval = TimeInterval.Daily;
            string flag = "";
            DateTime t = t1;
            while (t <= t2)
            {
                var t3 = t;
                if (t.Month == 2 && t.Day == 29)
                    t3 = t.AddDays(-1);

                var d = LookupRequiredSpace(t3, forecast, out flag);
                if (d == HydrometRuleCurve.MissingValue)
                    s.AddMissing(t);
                else
                    s.Add(t, d);

                t = t.Date.AddDays(1);
            }
            return s;
        }