Beispiel #1
0
        /// <summary>Performs request for climate parameter</summary>
        /// <param name="request">Request to perform</param>
        /// <param name="progressReport">Handler to call periodically with information about request</param>
        /// <param name="cacheFileName">Optional file name with Dmitrov dataset (CSV or NetCDF) to cache results. If specified file exists DataSet from it is returned and
        /// no actual request is made</param>
        /// <returns>Task that results in DataSet with climate parameter values, optional uncertainty and provenance information</returns>
        public static Task <DataSet> FetchAsync(IFetchRequest request, Action <FetchStatus> progressReport = null, string cacheFileName = null)
        {
            if (String.IsNullOrEmpty(cacheFileName))
            {
                return(Instance.FetchAsync(request, progressReport));
            }
            else
            {
                if (File.Exists(cacheFileName))
                {
                    try
                    {
                        return(Task.FromResult <DataSet>(DataSet.Open(cacheFileName))); // Try to open cached data set
                    }
                    catch (Exception exc)
                    {
                        Trace.WriteLine(String.Format("Cached response is not found at {0}. Performing request...", cacheFileName));
                    }
                }

                return(Instance.FetchAsync(request, progressReport).ContinueWith <DataSet>(t => {
                    var result = t.Result;
                    try
                    {
                        result = result.Clone(cacheFileName);
                    }
                    catch (Exception exc2)
                    {
                        Trace.WriteLine(String.Format("Error writing cached response to {0}: {1}", cacheFileName, exc2.Message));
                    }
                    return result;
                }, TaskContinuationOptions.OnlyOnRanToCompletion));
            }
        }