Exemplo n.º 1
0
Arquivo: NCDC.cs Projeto: quanted/hms
        /// <summary>
        /// Calls the appropriate aggregation function.
        /// </summary>
        /// <param name="errorMsg"></param>
        /// <param name="inputData"></param>
        /// <param name="results"></param>
        /// <param name="tempStartDate"></param>
        /// <param name="tempEndDate"></param>
        /// <returns></returns>
        private Dictionary <string, double> AggregateData(out string errorMsg, ITimeSeriesInput inputData, NCDCJson results, DateTime tempStartDate, DateTime tempEndDate)
        {
            Dictionary <string, double> sumValues = new Dictionary <string, double>();

            switch (inputData.TemporalResolution)
            {
            case "hourly":
                sumValues = SumHourlyValues(out errorMsg, inputData.DateTimeSpan.DateTimeFormat, results);
                if (errorMsg.Contains("ERROR"))
                {
                    return(null);
                }
                break;

            case "daily":
            case "default":
            default:
                sumValues = SumDailyValues(out errorMsg, inputData.DateTimeSpan.DateTimeFormat, results);
                if (errorMsg.Contains("ERROR"))
                {
                    return(null);
                }
                break;

            case "weekly":
                sumValues = SumDailyValues(out errorMsg, inputData.DateTimeSpan.DateTimeFormat, results);
                if (errorMsg.Contains("ERROR"))
                {
                    return(null);
                }
                // Weekly aggregation of ncdc data requires daily summed values.
                sumValues = SumWeeklyValues(out errorMsg, inputData.DateTimeSpan.DateTimeFormat, sumValues);
                if (errorMsg.Contains("ERROR"))
                {
                    return(null);
                }
                break;

            case "monthly":
                sumValues = SumDailyValues(out errorMsg, inputData.DateTimeSpan.DateTimeFormat, results);
                if (errorMsg.Contains("ERROR"))
                {
                    return(null);
                }
                // Weekly aggregation of ncdc data requires daily summed values.
                sumValues = SumWeeklyValues(out errorMsg, inputData.DateTimeSpan.DateTimeFormat, sumValues);
                if (errorMsg.Contains("ERROR"))
                {
                    return(null);
                }
                break;
            }
            return(sumValues);
        }
Exemplo n.º 2
0
Arquivo: NCDC.cs Projeto: quanted/hms
        /// <summary>
        /// Sums the values for each recorded value to return a dictionary of hourly summed values.
        /// </summary>
        /// <param name="errorMsg"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        private Dictionary <string, double> SumHourlyValues(out string errorMsg, string dateFormat, NCDCJson data)
        {
            errorMsg = "";
            Dictionary <string, double> dict = new Dictionary <string, double>();
            DateTime iDate;
            DateTime newDate;

            DateTime.TryParse(data.results[0].date, out newDate);
            double sum = 0.0;

            for (int i = 0; i < data.results.Count - 1; i++)
            {
                DateTime.TryParse(data.results[i].date, out iDate);
                if (iDate.Hour == newDate.Hour)
                {
                    sum += NCDCAttributeCheck(out errorMsg, data.results[i].value, data.results[i].attributes);
                    if (errorMsg.Contains("ERROR"))
                    {
                        return(null);
                    }
                }
                else
                {
                    dict.Add(newDate.ToString(dateFormat), sum);
                    newDate = iDate;
                    sum     = NCDCAttributeCheck(out errorMsg, data.results[i].value, data.results[i].attributes);
                    if (errorMsg.Contains("ERROR"))
                    {
                        return(null);
                    }
                }
            }
            return(dict);
        }
Exemplo n.º 3
0
Arquivo: NCDC.cs Projeto: quanted/hms
        /// <summary>
        /// Starts the sequence of functions to download ncdc data.
        /// </summary>
        /// <param name="errorMsg"></param>
        /// <param name="dataset"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        private Dictionary <string, double> BeginDataDownload(out string errorMsg, string dataset, ITimeSeriesInput input)
        {
            errorMsg = "";

            Dictionary <string, double> data = new Dictionary <string, double>();
            DateTime tempStartDate           = input.DateTimeSpan.StartDate;
            DateTime tempEndDate             = input.DateTimeSpan.EndDate;

            string station = input.Geometry.GeometryMetadata["stationID"];
            string token   = input.Geometry.GeometryMetadata["token"];

            //string token = (input.Geometry.GeometryMetadata.ContainsKey("token")) ? input.Geometry.GeometryMetadata["token"] : (string)HttpContext.Current.Application["ncdc_token"];
            string url = "";

            //Check if date difference is greater than one year, if true splits dates apart into multiple requests that are equal to 1y-1day.
            int requiredCalls = Convert.ToInt16(Math.Ceiling((input.DateTimeSpan.EndDate - input.DateTimeSpan.StartDate).TotalDays / 365));

            for (int i = 0; i < requiredCalls; i++)
            {
                if (i == 0 && requiredCalls == 1)       //url constructed for a single call being made
                {
                    tempStartDate = input.DateTimeSpan.StartDate;
                    tempEndDate   = input.DateTimeSpan.EndDate;
                    url           = ConstructURL(out errorMsg, station, input.BaseURL.First(), tempStartDate, tempEndDate);
                    if (errorMsg.Contains("ERROR"))
                    {
                        return(null);
                    }
                }
                else if (i == 0 && requiredCalls != 1)  //url constructed for first call of multiple
                {
                    tempStartDate = input.DateTimeSpan.StartDate;
                    tempEndDate   = tempStartDate.AddYears(1).AddDays(-1);
                    url           = ConstructURL(out errorMsg, station, input.BaseURL.First(), tempStartDate, tempEndDate);
                    if (errorMsg.Contains("ERROR"))
                    {
                        return(null);
                    }
                }
                else if (i != 0 && i == requiredCalls - 1) //url constructed for last call of multiple
                {
                    tempStartDate = tempEndDate;
                    tempEndDate   = input.DateTimeSpan.EndDate;
                    url           = ConstructURL(out errorMsg, station, input.BaseURL.First(), tempStartDate, tempEndDate);
                    if (errorMsg.Contains("ERROR"))
                    {
                        return(null);
                    }
                }
                else                                   //url constructed for calls that are not the start or end
                {
                    tempStartDate = tempEndDate;
                    tempEndDate   = tempStartDate.AddYears(1).AddDays(-1);
                    url           = ConstructURL(out errorMsg, station, input.BaseURL.First(), tempStartDate, tempEndDate);
                    if (errorMsg.Contains("ERROR"))
                    {
                        return(null);
                    }
                }
                string json = DownloadData(out errorMsg, token, url);
                if (errorMsg.Contains("ERROR"))
                {
                    return(null);
                }
                if (!json.Equals("{}"))
                {
                    NCDCJson results = JSON.Deserialize <NCDCJson>(json);

                    double total = results.metadata.resultset.count;        //checking if available results exceed 1000 entry limit.
                    if (total > 1000)
                    {
                        for (int j = 1; j < Math.Ceiling(total / 1000); j++)
                        {
                            url  = url + "&offset=" + (j) * 1000;
                            json = DownloadData(out errorMsg, input.Geometry.GeometryMetadata["token"], url);
                            if (errorMsg.Contains("ERROR"))
                            {
                                return(null);
                            }

                            NCDCJson tempResults = JSON.Deserialize <NCDCJson>(json);

                            results.results.AddRange(tempResults.results);                              //Adds the additional calls to the results.results variable
                        }
                    }
                    Dictionary <string, double> sumValues = AggregateData(out errorMsg, input, results, tempStartDate, tempEndDate);
                    AppendData(ref data, sumValues, input.DateTimeSpan.DateTimeFormat, tempStartDate, tempEndDate, input.TemporalResolution);
                    if (errorMsg.Contains("ERROR"))
                    {
                        return(null);
                    }
                }
                else
                {
                    AppendData(ref data, new Dictionary <string, double>(), input.DateTimeSpan.DateTimeFormat, tempStartDate, tempEndDate, input.TemporalResolution);
                    if (errorMsg.Contains("ERROR"))
                    {
                        return(null);
                    }
                }
            }
            return(data);
        }