Example #1
0
        public void GetFirstTimeStampTest()
        {
            db.TruncateTables();

            var f = db.GetFirstTimeStamp().ToList();

            Assert.AreEqual(f.Count, 1);
            Assert.AreEqual(f[0].TimeStamp, null);

            db.AddDataSource(
                "NCEP/NCAR Reanalysis 1 (regular grid)",
                "The NCEP/NCAR Reanalysis 1 project is using a state-of-the-art analysis/forecast system to perform data assimilation using past data from 1948 to the present",
                "NCEP Reanalysis data provided by the NOAA/OAR/ESRL PSD, Boulder, Colorado, USA, from their Web site at http://www.esrl.noaa.gov/psd/",
                "Microsoft.Research.Science.FetchClimate2.DataSources.NCEPReanalysisRegularGridDataSource, NCEPReanalysisDataSource",
                "msds:az?name=ReanalysisRegular&DefaultEndpointsProtocol=http&AccountName=fetch&AccountKey=1Y0EOrnCX6ULY8c3iMHg9rrul2BWbPHKsHUceZ7SSh+ShM/q9K0ml49gQm+PE7G7i7zCvrpuT",
                null, null);
            Thread.Sleep(time * 10);
            DateTime TIME_AFTER_FIRST = DateTime.UtcNow;

            db.AddVariable("MyVariable", "MyDescription", "MyUnits");
            Thread.Sleep(time * 10);
            db.SetMapping("NCEP/NCAR Reanalysis 1 (regular grid)", "MyVariable", "TestDataSourceVariable", true, true);


            db.SetFetchEngine("TestEngine");

            var f2 = db.GetFirstTimeStamp().ToList();

            Assert.AreEqual(f.Count, 1);

            if (f2[0].TimeStamp > TIME_AFTER_FIRST)
            {
                Assert.Fail();
            }
        }
        /// <summary>Returns the ExtendedConfiguration for the time specifed or null if time is before first timestamp in configuration database</summary>
        /// <param name="utcTime">UTC time to fetch the configuration for or DateTime.MaxValue for latest timestamp </param>
        /// <returns>ExtendedConfiguration object or null</returns>
        public ExtendedConfiguration GetConfiguration(DateTime utcTime)
        {
            using (FetchConfigurationDataClassesDataContext db = new FetchConfigurationDataClassesDataContext(connectionString))
            {
                if (utcTime == DateTime.MaxValue)
                {
                    utcTime = (DateTime)db.GetLatestTimeStamp().First().TimeStamp;
                }
                else if (utcTime < db.GetFirstTimeStamp().First().TimeStamp)
                {
                    return(null);
                }

                List <ExtendedDataSourceDefinition> dataSourcesList = new List <ExtendedDataSourceDefinition>();

                Dictionary <string, VariableDefinition> supportedVars = new Dictionary <string, VariableDefinition>();

                var dataSources = db.GetDataSources(utcTime);
                var dsVariables = db.GetEnvVariables().ToArray();

                foreach (var ds in dataSources)
                {
                    var mapping      = db.GetMapping(utcTime, ds.Name).ToArray();
                    var providedVars = mapping.Where(mp => mp.IsOutbound != null && (bool)mp.IsOutbound).Select(mp => mp.FetchVariableName).ToArray();

                    ExtendedDataSourceDefinition dsd = new ExtendedDataSourceDefinition((ushort)ds.ID, ds.Name, ds.Description, ds.Copyright, ds.Uri, ds.FullClrTypeName, providedVars,
                                                                                        (ds.RemoteName != null) ? ds.Uri : string.Empty,
                                                                                        mapping.ToDictionary(map => map.FetchVariableName, map => map.DataVariableName),
                                                                                        ds.RemoteName,
                                                                                        (ushort)(ds.RemoteID == null ? -1 : ds.RemoteID));

                    dataSourcesList.Add(dsd);
                    foreach (var envVar in providedVars)
                    {
                        if (!supportedVars.ContainsKey(envVar))
                        {
                            var v = dsVariables.Where(dsv => dsv.DisplayName == envVar).First();
                            supportedVars[envVar] = new VariableDefinition(v.DisplayName, v.Units, v.Description);
                        }
                    }
                }

                return(new ExtendedConfiguration(utcTime, dataSourcesList.ToArray(), supportedVars.Values.ToArray())
                {
                    FetchEngineTypeName = db.GetFetchEngine(utcTime).First().FullClrTypeName
                });
            }
        }
        /// <summary>
        /// Produces the configuration for the time specified
        /// </summary>
        /// <param name="utcTime">A time to produce the configuration for</param>
        /// <returns></returns>
        public IFetchConfiguration GetConfiguration(DateTime utcTime)
        {
            using (FetchConfigurationDataClassesDataContext db = new FetchConfigurationDataClassesDataContext(connectionString))
            {
                if (utcTime == DateTime.MaxValue)
                {
                    utcTime = (DateTime)db.GetLatestTimeStamp().First().TimeStamp;
                }
                else if (utcTime < db.GetFirstTimeStamp().First().TimeStamp)
                {
                    throw new ArgumentException("No configuration exists for given timestamp");
                }

                List <IDataSourceDefinition> dataSourcesList = new List <IDataSourceDefinition>();

                Dictionary <string, IVariableDefinition> supportedVars = new Dictionary <string, IVariableDefinition>();
                var dataSources = db.GetDataSources(utcTime);
                var dsVariables = db.GetEnvVariables().ToArray();
                foreach (var ds in dataSources)
                {
                    var mappings     = db.GetMapping(utcTime, ds.Name);
                    var providedVars = mappings.Where(mp => mp.IsOutbound != null && (bool)mp.IsOutbound).Select(mp => mp.FetchVariableName).ToArray();

                    if (providedVars.Length > 0) //otherwise there are no mappings for the data source
                    {
                        IDataSourceDefinition dsd = new DataSourceDefinition((ushort)ds.ID, ds.Name, ds.Description, ds.Copyright,
                                                                             (ds.RemoteName != null) ? ds.Uri : string.Empty,
                                                                             providedVars);
                        dataSourcesList.Add(dsd);
                        foreach (var evn in providedVars)
                        {
                            if (!supportedVars.ContainsKey(evn))
                            {
                                var v = dsVariables.Where(dsv => dsv.DisplayName == evn).First();
                                supportedVars[v.DisplayName] = new VariableDefinition(v.DisplayName, v.Units, v.Description);
                            }
                        }
                    }
                }

                return(new FetchConfiguration(utcTime, dataSourcesList.ToArray(), supportedVars.Values.ToArray()));
            }
        }