Skip Ribbon Commands
Skip to main content

Ed Ozols' Website

:

WeatherReportCSharp

 
using System.IO;
using System.Net;
 
// used to build entire input
            StringBuilder sb = new StringBuilder();
            //String for METAR file
            string metar;
            // Declare a single-dimensional array 
            string[] metarArray = new string[40];
            //char[] timeAndDateArray = new char[7];
            //metarArray counter
            int metarArrayCounter = 0;
            int loopCounter = 0;
            int date = 0;
            int temperature = 0;
            string tempString = null;
            int count = 0;
            int metarLocation = 0;
            int hour = 0;
            int minute = 0;
            int month = 0;
            int tempLocation = 0;
            // used on each read operation
            byte[] buf = new byte[8192];
 
 
            // prepare the web page we will be asking for
            HttpWebRequest request = (HttpWebRequest)
                WebRequest.Create("http://w1.weather.gov/data/METAR/KHSP.1.txt");
 
 
            // execute the request
            HttpWebResponse response = (HttpWebResponse)
                request.GetResponse();
 
 
            // we will read data via the response stream
            Stream resStream = response.GetResponseStream();
 
            do
            {
                // fill the buffer with data
                count = resStream.Read(buf, 0, buf.Length);
 
 
                // make sure we read some data
                if (count != 0)
                {
                    // translate from bytes to ASCII text
                    tempString = Encoding.ASCII.GetString(buf, 0, count);
 
 
                    // continue building the string
                    sb.Append(tempString);
                }
            }
            while (count > 0); // any more data to read?
            //
            metar = sb.ToString();
            // print out page source
            Console.WriteLine(sb.ToString());
            string[] split = metar.Split(new Char[] { });
 
 
            foreach (string s in split)
            {
 
 
                if (s.Trim() != "")
                {
                    metarArray[metarArrayCounter] = s.Trim();
                    metarArrayCounter++;
                }
 
 
 
 
            }
            while (loopCounter < metarArrayCounter)
            {
 
 
                if (metarArray[loopCounter] == "METAR")
                {
                    metarLocation = loopCounter;
                }
 
 
                loopCounter++;
            }
 
            //Get the time and date from METAR
            char[] timeAndDateArray = metarArray[metarLocation + 2].ToCharArray();
 
            //Create a new StringBuilder to put the date in
            System.Text.StringBuilder dateBuilder = new System.Text.StringBuilder();
            //Create a new StringBuilder to put the temperature in
            System.Text.StringBuilder temperatureBuilder = new System.Text.StringBuilder();
            //Append (add) the first char to dateBuilder
            dateBuilder.Append("").Append(timeAndDateArray[0]);
            //Append (add) the second char to dateBuilder
            dateBuilder.Append("").Append(timeAndDateArray[1]);
            date = int.Parse(dateBuilder.ToString());
 
 
            //*****************************************************//
            //*****************************************************//
            //Insert Stringbuilder for temp here                   //
            //*****************************************************//
            //*****************************************************//
            //Create a new stringbuilder to check for position of temperature
             //Get the temperature ftom METAR
            //********************************************//
            //Check to see if first char in metarLocation + 7 is between 0 and 9//
            //**************************************************//
            char[] checkTempArray = metarArray[metarLocation + 7].ToCharArray();
            if ((checkTempArray[0] >= '0') && (checkTempArray[0] <= '9'))
            {
                tempLocation = 7;
            }
            else
            {
                tempLocation = 8;
            }
 
 
 
           
 
 
 
 
            //Get the temperature ftom METAR
            char[] temperatureArray = metarArray[metarLocation + tempLocation].ToCharArray();
            //Append (add) the first char to temperatureBuilder
            temperatureBuilder.Append("").Append(temperatureArray[0]);
            //Append (add) the second char to dateBuilder
            temperatureBuilder.Append("").Append(temperatureArray[1]);
            //Make temperature an int so math operations can be used
            temperature = int.Parse(temperatureBuilder.ToString());
            //minute = int.Parse(minuteBuilder.ToString());
 
 
 
 
 
 
            //Create a new StringBuilder to put the hour in
            System.Text.StringBuilder hourBuilder = new System.Text.StringBuilder();
            //Append (add) the third char to hourBuilder
            hourBuilder.Append("").Append(timeAndDateArray[2]);
            //Append (add) the third char to hourBuilder
            hourBuilder.Append("").Append(timeAndDateArray[3]);
            //Create a new StringBuilder to put the minute in
            System.Text.StringBuilder minuteBuilder = new System.Text.StringBuilder();
            //Append (add) the fifth char to hourBuilder
            minuteBuilder.Append("").Append(timeAndDateArray[4]);
            //Append (add) the sixth char to hourBuilder
            minuteBuilder.Append("").Append(timeAndDateArray[5]);
            //Print out to make sure time is correct. Remove code when done
            Console.WriteLine("The time is " + hourBuilder + ":" + minuteBuilder);
            hour = int.Parse(hourBuilder.ToString());
            minute = int.Parse(minuteBuilder.ToString());
 
            //Append (add) the third char to hourBuilder
 
            //Get the current date and time
            DateTime date1 = DateTime.Now;
            //Check to see what format "month" is in. Delete when done
            Console.WriteLine(date1.Month);
            //Trasfer month to month variable
            month = date1.Month;
            //Transfer date to the date variable
           
            //The time is reported in GMT. If the hour is less than 4, it is not
            //yet midnight and we need to back uo a day
            if (hour < 4)
            {
                //The hours are reported using a 24 hour format
                //Adding 20 to the hour backs up the time by 4 hours
                hour = hour + 20;
                //We also need to change the date. If the date is greater than one, we
                //simply subtract 1.
                //If the date is 1, then we need to go back to the previous month
                //or the date will end up as a '0'
                if (date != 1)
                {
                    date--;
                }
                else
                {
                    //We need to check the current month so we know how to back up the date
                    //If the month is January ('1'), we change it to December ('12')
                    //and since the last day of the month is 31, we chage the date to that value
                    //If the month is any other month, we simply back up the month by 1 and
                    //change the date to the last day of the previous month.
                    switch (month)
                    {
                        case 1:
                            date = 31;
                            month = 12;
                            break;
                        case 2:
                            date = 31;
                            month--;
                            break;
                        case 3:
                            //Since we are backing up to February, we have to account
                            //for leap years. We will do a simple check that will take into account
                            //just whether the year is divisible by 4 and not check whether a century year
                            //(a year that ends in 00) is a leap year
                            if (date1.Year % 4 == 0)
                            {
                                date = 29;
                            }
                            else
                            {
                                date = 28;
                            }
                            month--;
                            break;
                        case 4:
                            date = 31;
                            month--;
                            break;
                        case 5:
                            date = 30;
                            month--;
                            break;
                        case 6:
                            date = 31;
                            month--;
                            break;
                        case 7:
                            date = 30;
                            month--;
                            break;
                        case 8:
                            date = 31;
                            month--;
                            break;
                        case 9:
                            date = 31;
                            month--;
                            break;
                        case 10:
                            date = 30;
                            month--;
                            break;
                        case 11:
                            date = 31;
                            month--;
                            break;
                        case 12:
                            date = 30;
                            month--;
                            break;
                        default:
                            break;
                    }
                }
            }
            else if (hour == 4)
            {
                hour = 0;
            }
            else
            {
                hour = hour - 4;
            }
            mnthLabel.Text = month.ToString();
            dateLabel.Text = date.ToString();
            hourLabel.Text = hour.ToString();
            minuteLabel.Text = minute.ToString();
            label2.Text = temperature.ToString();