/// <summary>
 /// Forecast Plot
 /// </summary>
 /// <returns>Returns the view</returns>
 public JsonResult ForecastPlot(long USGSID)
 {
     try
     {
         ForecastData Fd = new ForecastData();
         Fd = this.conductivityService.GetForecastdata(USGSID);
         return Json(Fd, JsonRequestBehavior.AllowGet);
     }
     catch
     {
         throw;
     }
 }
Example #2
0
        /// <summary>
        /// Gets Forecast data
        /// </summary>
        /// <param name="usgsId">USGSID parameter</param>
        /// <returns>Returns forecast data</returns>
        public ForecastData GetForecastdata(long usgsId)
        {
            try
            {
                ForecastData Fd = new ForecastData();
                var AllData = modifiedwaterdataRepository.GetAll();
                var data = (from r in AllData where r.sourceID == usgsId select r).ToList();
                DateTime currentDate = DateTime.Now;
                DateTime firstDate = data.Count > 0 ? DateTime.Parse(data[0].measurment_date, new System.Globalization.CultureInfo("en-US", true)) : new DateTime();
                var numberOfYears = Convert.ToInt32(currentDate.Year) - Convert.ToInt32(firstDate.Year);
                DateTime forcastEndDate = currentDate.AddDays(182);
                DateTime forcastStartDate = currentDate;
                TimeSpan interval = data.Count > 0 ? (DateTime.Parse(data[2].measurment_date, new System.Globalization.CultureInfo("en-US", true)) - DateTime.Parse(data[1].measurment_date, new System.Globalization.CultureInfo("en-US", true))) : new TimeSpan();
                Dictionary<DateTime, int>[] forcastRawData = new Dictionary<DateTime, int>[numberOfYears];
                Dictionary<DateTime, double> forcastDataAverage = new Dictionary<DateTime, double>();
                Dictionary<DateTime, int> forcastDataMax = new Dictionary<DateTime, int>();
                for (int i = 1; i <= numberOfYears; i++)
                {

                    var dateTime = forcastStartDate;
                    forcastRawData[i - 1] = new Dictionary<DateTime, int>();
                    foreach (var element in data)
                    {
                        var conductivity = element.cond;
                        TimeSpan span = TimeSpan.Zero;
                        if ((DateTime.Parse(element.measurment_date, new System.Globalization.CultureInfo("en-US", true)) >= forcastStartDate.AddYears(-1 * i)) && (DateTime.Parse(element.measurment_date, new System.Globalization.CultureInfo("en-US", true)) <= forcastEndDate.AddYears(-1 * i)))
                        {
                            forcastRawData[i - 1].Add(dateTime, Convert.ToInt32(conductivity));
                            span = span + interval;
                            dateTime = dateTime.Add(span);
                        }
                    }

                    if (i == numberOfYears)
                    {
                        forcastDataAverage = forcastRawData.SelectMany(d => d).GroupBy(kvp => kvp.Key).ToDictionary(g => g.Key, g => g.Average(kvp => kvp.Value));
                        forcastDataMax = forcastRawData.SelectMany(d => d).GroupBy(kvp => kvp.Key).ToDictionary(g => g.Key, g => g.Max(kvp => kvp.Value));
                    }
                }

                foreach (var entry in forcastDataAverage)
                {
                    Fd.AverageForecastData.Add(new water_data() { measurment_date = entry.Key.ToString(), cond = Convert.ToInt32(entry.Value) });
                }
                foreach (var entry in forcastDataMax)
                {
                    Fd.MaximumForecastData.Add(new water_data() { measurment_date = entry.Key.ToString(), cond = Convert.ToInt32(entry.Value) });
                }
                return Fd;
            }
            catch
            {
                throw;
            }
        }