Esempio n. 1
0
        public string getWeather(string zip)
        {
            gov.weather.graphical.ndfdXML gg = new gov.weather.graphical.ndfdXML();
            gov.weather.graphical.weatherParametersType pa = new gov.weather.graphical.weatherParametersType();
            pa.maxt = true;
            pa.mint = true;
            // pa.iceaccum = true;
            pa.dryfireo = true;
            //pa.dew = true;
            string information = gg.LatLonListZipCode("85281");
            int    x           = information.IndexOf("</latLonList>");
            int    y           = information.IndexOf("<latLonList>");

            string location = information.Substring(information.IndexOf("<latLonList>") + 12, information.IndexOf("</latLonList>") - information.IndexOf("<latLonList>") - 12);

            string[] lat = location.Split(',');

            //33.4357,-111.917
            string w           = gg.NDFDgen(Convert.ToDecimal(lat[0]), Convert.ToDecimal(lat[1]), "time-series", DateTime.Now, DateTime.Now.AddDays(4), "e", pa);
            string TMbeg       = "<name>Daily Maximum Temperature</name>";
            string TMend       = "</temperature>";
            string Mtemepeture = w.Substring(w.IndexOf(TMbeg) + TMbeg.Length, w.IndexOf(TMend) - w.IndexOf(TMbeg) - TMbeg.Length);

            Mtemepeture = Mtemepeture.Trim();
            string[] max = System.Text.RegularExpressions.Regex.Split(Mtemepeture, "\n");
            returnTempeture = "Max Temepture: " + Regex.Replace(max[0], @"[^\d.\d]", "");
            for (int i = 1; i < max.Length; i++)
            {
                returnTempeture = returnTempeture + ",";
                returnTempeture = returnTempeture + Regex.Replace(max[i], @"[^\d.\d]", "");
            }


            string TNbeg       = "<name>Daily Minimum Temperature</name>";
            string duplicate   = "</temperature>";
            string TNend       = "<fire";
            int    ee          = w.IndexOf(TNend);
            int    e           = w.IndexOf(TMend);
            string Ntemepeture = w.Substring(w.IndexOf(TNbeg) + TNbeg.Length, w.IndexOf(TNend) - w.IndexOf(TNbeg) - TNbeg.Length);

            Ntemepeture = Ntemepeture.Trim();
            Ntemepeture = Ntemepeture.Substring(0, Ntemepeture.Length - duplicate.Length).Trim();
            string[] min = System.Text.RegularExpressions.Regex.Split(Ntemepeture, "\n");
            returnTempeture = returnTempeture + "\n";
            returnTempeture = returnTempeture + "Min Temepture: " + Regex.Replace(min[0], @"[^\d.\d]", "");
            for (int i = 1; i < min.Length; i++)
            {
                returnTempeture = returnTempeture + ",";
                returnTempeture = returnTempeture + Regex.Replace(min[i], @"[^\d.\d]", "");
            }
            return(returnTempeture);
        }
Esempio n. 2
0
    /* This portion of code is executed when the submit button is clicked
     *
     *  Instantiation of a new Service reference ndfdXML ~ for client's SOAP connection to the weather service
     *
     *  Accept input from textboxes (Location coordinate)
     *      --+ Latitude => lat
     *      --+ Longitude => lng
     *
     *  Call the method for the current weather observation (NDFDgenByDay) from the weather service class
     *      --+ return result as a string
     *      --+ write the result to a file in form of XML
     *      --+ handle the appropriate nodes and get their values
     */
    protected void submitButton_Click(object sender, EventArgs e)
    {
        gov.weather.graphical.ndfdXML myweather = new gov.weather.graphical.ndfdXML();

        try
        {                                                         // Get input form textboxes
            decimal lat       = Decimal.Parse(txtLatitude.Text);  // latitude value
            decimal lng       = Decimal.Parse(txtLongitude.Text); // longitude value
            string  numOfDays = "1";                              // number of days for weather observations to be fetched


            gov.weather.graphical.weatherParametersType wth_para = new gov.weather.graphical.weatherParametersType();
            wth_para.temp = true;

            // call the method for the current observation (NDFDgenByDay) to get weather information
            string result = myweather.NDFDgenByDay(lat, lng, DateTime.UtcNow, numOfDays, gov.weather.graphical.unitType.e, gov.weather.graphical.formatType.Item12hourly);

            // define the directory path to write result (current observation) from the  national weather service station
            string result_path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "", "result.xml");

            // write the result to a file on the created path
            System.IO.StreamWriter file = new System.IO.StreamWriter(result_path);
            file.WriteLine(result);
            file.Close();


            XmlDocument result_xml = new XmlDocument();
            result_xml.Load(result_path);                              // load the XML file from the path

            // process the nodes needed from the XML file
            XmlNode max_temp_node = result_xml.SelectSingleNode("//data/parameters/temperature[@type ='maximum']/value/text()");
            XmlNode min_temp_node = result_xml.SelectSingleNode("//data/parameters/temperature[@type ='minimum']/value/text()");
            XmlNode prob_prep     = result_xml.SelectSingleNode("//data/parameters/probability-of-precipitation/value/text()");
            XmlNode weather_icon  = result_xml.SelectSingleNode("//data/parameters/conditions-icon/icon-link/text()");

            string maxTemperature      = max_temp_node.Value;        // get maximum temperature value
            string minTemperature      = min_temp_node.Value;        // get minimum temperature value
            string probOfPrepipitation = prob_prep.Value;            // get probability of precipitation value
            string weatherIcon         = weather_icon.Value;         // get the weather icon value

            // pass the corresponding values from the nodes to its specific label
            Label3.Text  = maxTemperature;
            Label5.Text  = minTemperature;
            Label10.Text = probOfPrepipitation;
            Label12.Text = "<img alt='' src='" + weatherIcon + "' />";
        }catch (Exception ex)
        {
            System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('" + ex.Message + "')</SCRIPT>");
        }
    }
 private string GetLatLongListFromZip()
 {
     try
     {
         gov.weather.graphical.ndfdXML ndfdClient = new gov.weather.graphical.ndfdXML();
         string latlon         = ndfdClient.LatLonListZipCode(zip.ToString());
         var    latLonXElement = XElement.Parse(latlon);
         var    latLonNode     = latLonXElement.Descendants().Where(n => n.Name == "latLonList").FirstOrDefault();
         return(latLonNode.Value);
     }
     catch (Exception e)
     {
         NdfdErrorHandler(e);
         return(null);
     }
 }
 private string CallWebService(gov.weather.graphical.weatherParametersType weatherParameters)
 {
     try
     {
         gov.weather.graphical.ndfdXML ndfdClient = new gov.weather.graphical.ndfdXML();
         string latlonglist = GetLatLongListFromZip();
         if (latlonglist == ",")
         {
             return(null);
         }
         string dwmlReturn = ndfdClient.NDFDgenLatLonList(latlonglist,
                                                          gov.weather.graphical.productType.timeseries,
                                                          DateTime.Today,
                                                          DateTime.Today.AddDays(7),
                                                          gov.weather.graphical.unitType.e,
                                                          weatherParameters);
         return(dwmlReturn);
     }
     catch (Exception e)
     {
         NdfdErrorHandler(e);
         return(null);
     }
 }
    public String[] weatherservice(String zip)
    {
        try
        {

            gov.weather.graphical.ndfdXML Obj = new gov.weather.graphical.ndfdXML();                   // Using the service at http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl
            DateTime dateNow = DateTime.Now;
            String[] resultOutpt = new String[6];

            XmlDocument xDoc = new XmlDocument();                                                        //Xml Document is Created
            xDoc.LoadXml(Obj.LatLonListZipCode(zip));
            XmlNodeList nodeList = xDoc.SelectNodes("/dwml");                                                //XMl file is being parsed

            String latlon = nodeList[0].InnerText;                                                        // String contained the latitude and longitude is obtained

            String[] tSplit = latlon.Split(',');                                                          // Using comma as the splitting criteria for longitude and latitude
            String latit = tSplit[0];                                                                  //  latitude
            String longit = tSplit[1];                                                                 //longitude

            xDoc.LoadXml(Obj.NDFDgenByDay(Convert.ToDecimal(latit), Convert.ToDecimal(longit), DateTime.Today.Date, "6", "e", "24 hourly"));   // Xml containing 5days of weather forecast is obtained
            XmlNodeList gNode = xDoc.DocumentElement.SelectNodes("/dwml/data/parameters");                                                        // Parsing
            String[] maximumTemp = new String[7];
            String[] minimumTemp = new String[7];
            XmlNodeList cNode = gNode[0].ChildNodes;                                                         // Child nodes are being accessed.

            int x = 0;
            for (int i = 0; i < cNode.Count; i++)
            {
                int q = 0;
                if (cNode[i].Name == "temperature" && x == 0)                                             // values of temperature accessed

                {
                    XmlNodeList mValues = cNode[i].ChildNodes;                                          // values of maximum temp are stored
                    for (int j = 1; j < 6; j++)
                    {
                        maximumTemp[q] = mValues[j].InnerText;
                        q++;
                    }
                    x = 1;
                }
                else if (cNode[i].Name == "temperature" && x == 1)                                         // valued of minimum temp are stored
                {
                    XmlNodeList mValues = cNode[i].ChildNodes;
                    for (int j = 1; j < 6; j++)
                    {
                        minimumTemp[q] = mValues[j].InnerText;
                        q++;
                    }
                }

            }

            for (int i = 0; i < 5; i++)
            {

                resultOutpt[i] = "Date: " + dateNow.ToString("d") + ", " + "Day: " + dateNow.DayOfWeek + "@Max Temp: " + maximumTemp[i] + "@Min Temp: " + minimumTemp[i];       // The Page called is receiving the data
                resultOutpt[i] = resultOutpt[i].Replace("@", System.Environment.NewLine);
                dateNow = dateNow.AddDays(1);
            }

            return resultOutpt;
        }
        catch (Exception)                                                           // exception Handling
        {
            String[] str = new String[1];
            str[0] = "ZipCode entered not valid";
            return str;
        }
    }