Esempio n. 1
0
        /// END: Copied from http://cladosnippet.blogspot.com/2012/04/c-get-season-according-to-given-date.html
        public Weather()
        {
            // Position of Alexandria, VA on the map -- should be extensible to any "home" location of the application user
            longitude = 38.8047m; // 38.8047 degrees N (N is positive)
            latitude = -77.0472m; // 77.0472 degrees W (W is negative)
            weatherForecastProductType = productType.timeseries;
            startTime = DateTime.UtcNow; // Weather forecast begins at the current time
            endTime = DateTime.UtcNow.AddDays(1); // Weather forecast ends a day after the current time -- Daily weather forecast
            weatherUnit = unitType.m; // Weather forecast generated in metric (SI) units

            currentSeason = CurrentSeason(startTime);
            forecastParametersType = new weatherParametersType();
            forecastParametersType.maxt = true; // Maximum temperature
            forecastParametersType.mint = true; // Minimum temperature
            forecastParametersType.appt = true; // Apparent temperature
            forecastParametersType.maxrh = true; // Maximum relative humidity
            forecastParametersType.minrh = true; // Minimum relative humidity
            forecastParametersType.pop12 = true; // 12 hour probability of precipitation
            if (Equals(currentSeason, Seasons.Winter)) // Snowfall is almost certainly zero unless the query is made in the winter
            {
                forecastParametersType.snow = true; // Snowfall amount
            }
            forecastParametersType.wspd = true; // Wind speed
            forecastParametersType.wdir = true; // Wind direction

            weatherDictionary = new Dictionary<string, double>();
            weatherDictionary.Add("Maximum Temperature", 0.0);
            weatherDictionary.Add("Minimum Temperature", 0.0);
            weatherDictionary.Add("Apparent Temperature", 0.0);
            weatherDictionary.Add("12 Hour Probability of Precipitation", 0.0);
            weatherDictionary.Add("Wind Speed", 0.0);
            weatherDictionary.Add("Wind Direction", 0.0);
            weatherDictionary.Add("Maximum Relative Humidity", 0.0);
            weatherDictionary.Add("Minimum Relative Humidity", 0.0);
            weatherDictionary.Add("Snowfall Amount", 0.0); // Need to fix this for winter... will not work
        }
        /// <summary>
        /// Gets the forecast for upcoming market day.
        /// </summary>
        /// <returns></returns>
        private static WeatherForecastData GetForecastForUpcomingMarket()
        {
            WeatherForecastData result;

            // determine the local date/time for the start of this Saturday's market
            int daysTilSaturday = DayOfWeek.Saturday - DateTime.Today.DayOfWeek;
            DateTime marketStarts = DateTime.Today.AddDays(daysTilSaturday).AddHours(8);

            // geographic location of Carpterter Village
            const decimal latitude = (decimal)35.8188612;
            const decimal longitude = (decimal)-78.8594969;

            // create an instance of the web service proxy
            var weatherFetcher = new ndfdXML {Proxy = WebRequest.DefaultWebProxy};

            // setup the parameters to tell the service what data we want
            // see http://www.nws.noaa.gov/forecasts/xml/docs/elementInputNames.php for parameters
            var weatherParams = new weatherParametersType
                                    {
                                        appt = true,
                                        icons = true,
                                        maxt = true,
                                        mint = true,
                                        pop12 = true,
                                        rh = true,
                                        wx = true,
                                        wwa = true,
                                        wspd = true
                                    };

            try
            {
                result = WeatherForecastData.Parse(weatherFetcher.NDFDgen(latitude, longitude, "time-series",
                    marketStarts.ToUniversalTime(), marketStarts.AddHours(4).ToUniversalTime(), weatherParams));
            }
            catch (Exception)
            {
                result = new WeatherForecastData();
            }

            return result;
        }
Esempio n. 3
0
        /// <summary>
        /// Generate the reports
        /// </summary>
        private void GenerateReport()
        {
            records = new NodeCollection();
            maximum = states.Count;
            try
            {
                foreach (State state in states)
                {
                    weatherParametersType weatherType = new weatherParametersType();

                    //specifies parameters maximum temperature, weather condition and cloud amount
                    weatherType.maxt = true;
                    weatherType.sky  = true;
                    weatherType.wx   = true;


                    // call the weather service.
                    //weatherService.NDFDgenAsync(state.Latitude, state.Longitude, productType.timeseries, DateTime.Now.Date, DateTime.Now.AddDays(1).Date, unitType.e, weatherType, state);
                    string      str  = weatherService.NDFDgenByDay(state.Latitude, state.Longitude, DateTime.Now.Date, "1", unitType.e, formatType.Item24hourly);
                    WeatherNode node = new WeatherNode();

                    // get coordinates on map.
                    PointF location = ConvertCoordinates(state.Latitude, state.Longitude);
                    node.PinPoint = new PointF(location.X - node.Size.Width / 2, location.Y - node.Size.Height / 2);
                    node.State    = node.Name = state.Name;
                    if (!string.IsNullOrEmpty(str))
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.LoadXml(str);
                        node.Temperature = XmlConvert.ToDecimal(doc.SelectSingleNode("dwml/data/parameters/temperature[@type='maximum']/value/text()").Value);
                        XmlNode n       = doc.SelectSingleNode("dwml/data/parameters/weather/weather-conditions/value");
                        string  weather = null;
                        if (n != null)
                        {
                            XmlAttribute attr = n.Attributes["weather-type"];
                            if (attr != null)
                            {
                                weather = n.Attributes["weather-type"].Value;
                            }
                        }
                        if (weather == "freezing rain" ||
                            weather == "rain" ||
                            weather == "hail" ||
                            weather == "rain showers" ||
                            weather == "freezing drizzle" ||
                            weather == "drizzle")
                        {
                            node.WeatherCondition = WeatherCondition.Rain;
                        }
                        else if (weather == "show showers" ||
                                 weather == "blowing snow" ||
                                 weather == "snow" ||
                                 weather == "ice pellets")
                        {
                            node.WeatherCondition = WeatherCondition.Snow;
                        }
                        else if (weather == "thunderstorms")
                        {
                            node.WeatherCondition = WeatherCondition.ThunderStorm;
                        }
                        else
                        {
                            node.WeatherCondition = WeatherCondition.None;
                        }

                        records.Add(node);
                        value++;
                        backgroundWorker1.ReportProgress(value);
                    }
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show("Failed to establish connection with the service provider");
            }
        }