Exemplo n.º 1
0
        protected WeatherStationFull SearchWS(string state, string municipality, string ws)
        {
            WeatherStationFull answer = WeatherStations.SingleOrDefault(p => p.State.Equals(state) &&
                                                                        p.Municipality.Equals(municipality) &&
                                                                        p.Name.Equals(ws));

            return(answer);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Index(string state, string municipality, string station, string crop)
        {
            try
            {
                // Set the parameters
                ViewBag.s       = state ?? string.Empty;
                ViewBag.m       = municipality ?? string.Empty;
                ViewBag.w       = station ?? string.Empty;
                ViewBag.c       = crop ?? string.Empty;
                ViewBag.Section = SectionSite.Crop;

                // Setting data
                SetWS();
                ViewBag.Root = Root;

                // Searching the weather station, if the parameters don't come, it will redirect a default weather station
                if (string.IsNullOrEmpty(state) || string.IsNullOrEmpty(municipality) || string.IsNullOrEmpty(station) || string.IsNullOrEmpty(crop))
                {
                    var wsDefault = DefaultWeatherStationCrop();
                    return(RedirectToAction("Index", new { wsDefault.State, wsDefault.Municipality, wsDefault.Station, wsDefault.Crop }));
                }
                WeatherStationFull ws = SearchWS(state, municipality, station);
                ViewBag.ws = ws;

                // Getting the agronomic configuration
                RepositoryAgronomic     rA         = new RepositoryAgronomic(Root);
                IEnumerable <Agronomic> agronomics = await rA.ListAsync();

                Agronomic agronomic = agronomics.SingleOrDefault(p => p.Cp_Name.ToLower() == crop.ToLower());

                // Getting the forecast weather information
                RepositoryForecastYield rFY      = new RepositoryForecastYield(Root);;
                ForecastYield           forecast = await rFY.SearchAsync(ws.Id);

                ForecastYield forecast_exceedance = await rFY.SearchExceedanceAsync(ws.Id);

                IEnumerable <Yield> yield            = forecast.Yield.FirstOrDefault().Yield;
                IEnumerable <Yield> yield_exceedance = forecast_exceedance.Yield.FirstOrDefault().Yield.OrderByDescending(p => p.Data.First(p2 => p2.Measure.StartsWith("yield")).Avg);

                // Filtering cultivars
                IEnumerable <string> cultivars = yield.Select(p => p.Cultivar).Distinct().ToList();
                cultivars         = cultivars.Where(p => agronomic.Cultivars.Select(p2 => p2.Id).Distinct().Contains(p));
                ViewBag.cultivars = agronomic.Cultivars.Where(p => cultivars.Contains(p.Id));

                // Filtering soils
                IEnumerable <string> soils = yield.Select(p => p.Soil).Distinct().ToList();
                soils         = soils.Where(p => agronomic.Soils.Select(p2 => p2.Id).Distinct().Contains(p));
                ViewBag.soils = agronomic.Soils.Where(p => soils.Contains(p.Id)).AsEnumerable();

                //
                ViewBag.agronomic        = agronomic;
                ViewBag.yield            = yield;
                ViewBag.yield_exceedance = yield_exceedance;
                ViewBag.ranges           = ws.Ranges.Where(p => p.Crop_Name.ToLower() == crop.ToLower());


                return(View());
            }
            catch (Exception ex)
            {
                return(View("Error"));
            }
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Index(string state, string municipality, string station)
        {
            try
            {
                // Set the parameters
                ViewBag.s       = state ?? string.Empty;
                ViewBag.m       = municipality ?? string.Empty;
                ViewBag.w       = station ?? string.Empty;
                ViewBag.Section = SectionSite.Climate;

                // Setting data
                SetWS();

                // Searching the weather station, if the parameters don't come, it will redirect a default weather station
                if (string.IsNullOrEmpty(state) || string.IsNullOrEmpty(municipality) || string.IsNullOrEmpty(station))
                {
                    WeatherStationFull wsDefault = DefaultWeatherStation();
                    return(RedirectToAction("Index", new { wsDefault.State, wsDefault.Municipality, wsDefault.Station }));
                }
                WeatherStationFull ws = SearchWS(state, municipality, station);
                ViewBag.ws = ws;

                // Getting the forecast weather information
                RepositoryForecastWeather rFW      = new RepositoryForecastWeather(Root);
                ForecastWeather           forecast = await rFW.SearchAsync(ws.Id);

                // Sending the climate data to the view
                ViewBag.climate_data = forecast.Climate.First().Data;
                // Processing Scenarios data
                List <ForecastScenario> scenario = forecast.Scenario.ToList();
                var             scenario_name    = scenario.Select(p => p.Name).Distinct();
                var             measures         = scenario.SelectMany(p => p.Monthly_Data).SelectMany(p => p.Data).Select(p => p.Measure).Distinct();
                List <Scenario> scenario_list    = new List <Scenario>();
                foreach (var s in scenario)
                {
                    foreach (var md in s.Monthly_Data)
                    {
                        foreach (var da in md.Data)
                        {
                            Scenario i = scenario_list.SingleOrDefault(p => p.Measure == da.Measure && p.Year == s.Year && p.Month == md.Month);
                            if (i == null)
                            {
                                scenario_list.Add(new Scenario()
                                {
                                    Measure = da.Measure,
                                    Year    = s.Year,
                                    Month   = md.Month,
                                    Avg     = s.Name == "avg" ? da.Value : 0,
                                    Max     = s.Name == "max" ? da.Value : 0,
                                    Min     = s.Name == "min" ? da.Value : 0
                                });
                            }
                            else
                            {
                                if (s.Name == "avg")
                                {
                                    i.Avg = da.Value;
                                }
                                else if (s.Name == "max")
                                {
                                    i.Max = da.Value;
                                }
                                else if (s.Name == "min")
                                {
                                    i.Min = da.Value;
                                }
                            }
                        }
                    }
                }
                ViewBag.scenario = scenario_list;

                // Getting de historical climate
                RepositoryHistoricalClimate rHC = new RepositoryHistoricalClimate(Root);
                ViewBag.historical = await rHC.SearchAsync(ws.Id);

                // Getting climatology
                RepositoryHistoricalClimatology rHCy = new RepositoryHistoricalClimatology(Root);
                ViewBag.climatology = (await rHCy.SearchAsync(ws.Id)).FirstOrDefault();

                // Settings variables
                ViewBag.weather_vars = new string[] { "prec", "sol_rad", "t_max", "t_min" };

                return(View());
            }
            catch (Exception ex)
            {
                return(View("Error"));
            }
        }