Esempio n. 1
0
        public FeedResult GetData(DateTime thisDate, string dataType)
        {
            //Initialize variables
            values = new List <double>();
            List <string> lines;
            FeedResult    result = new FeedResult();

            //First try to get text lines from cache
            lines = cacheManager.getDataFromCache(thisDate, dataType);
            //If not found, pull from the web feed
            if (lines == null)
            {
                Console.WriteLine("Downloading: " + thisDate.ToString("MM/dd/yyyy"));
                lines = GetDataFromURL(thisDate, dataType);
            }
            else
            {
                Console.WriteLine("From cache : " + thisDate.ToString("MM/dd/yyyy"));
            }

            //Process the lines of text
            foreach (string line in lines)
            {
                //Get the 3rd item in the list, add to arraylist
                char[]   separators = new char[] { ' ' };
                string[] words      = line.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                values.Add(Convert.ToDouble(words[2]));
            }

            //Report the number of items in the list
            result.count = values.Count;

            //Now calculate and package the results
            //Start with mean, rounded to 2 decimals.
            result.mean = values.Average();

            //Calculate the median value; first sort the values in ascending order
            values.Sort();

            //Get the median index as a double
            double medianIndex = (double)(values.Count - 1) / 2;

            //Check whether median index is fractional or whole, get the value
            if (Math.Truncate(medianIndex) == medianIndex)
            {
                result.median = values[Convert.ToInt32(medianIndex)];
            }
            else
            {
                int lowIndex  = (Int32)Math.Truncate(medianIndex);
                int highIndex = lowIndex + 1;
                result.median = ((values[lowIndex] + values[highIndex]) / 2);
            }

            return(result);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            //Declare local date variables
            DateTime startDate;
            DateTime endDate;

            try
            {
                switch (args.Length)
                {
                case 1:
                    //Treat 1 arg as both start and end date
                    startDate = DateTime.Parse(args[0]);
                    endDate   = DateTime.Parse(args[0]);
                    break;

                case 2:
                    //Treat 2 args as start and end dates
                    startDate = DateTime.Parse(args[0]);
                    endDate   = DateTime.Parse(args[1]);
                    break;

                default:
                    //If wrong number of args, display usage hints and exit
                    DisplayUsageHints();
                    return;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.GetType() + ": " + e.Message);
                Console.WriteLine("--------");
                DisplayUsageHints();
                return;
            }

            //Check that dates either match, or start is before end
            if (DateTime.Compare(startDate, endDate) > 0)
            {
                Console.WriteLine("End date can't be before start date");
                return;
            }

            //Date range is set; get the data
            DateTime    thisDate = startDate;
            FeedManager fm       = new FeedManager();

            //Loop from start to end date
            while (!thisDate.Equals(endDate.AddDays(1)))
            {
                try
                {
                    FeedResult windResult = fm.GetData(thisDate, FeedManager.WIND_SPEED);
                    Console.WriteLine("Wind: " + windResult);

                    FeedResult baroResult = fm.GetData(thisDate, FeedManager.BARO_PRESSURE);
                    Console.WriteLine("Baro: " + baroResult);

                    FeedResult airTempResult = fm.GetData(thisDate, FeedManager.AIR_TEMP);
                    Console.WriteLine("Temp: " + airTempResult);

                    Console.WriteLine("---------");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.GetType() + ": " + e.Message);
                }

                thisDate = thisDate.AddDays(1);
            }
        }