/// <summary>
 /// Creates new variable with data from the FetchClimate service for the current lat/lon coordinate system and given time.
 /// </summary>
 /// <param name="ds">Target DataSet.</param>
 /// <param name="parameter">A climate parameter to fetch.</param>
 /// <param name="name">Name of the variable.</param>
 /// <param name="time">Time moment to fetch data for.</param>
 /// <returns>New variable instance.</returns>
 /// <remarks>
 /// <para>
 /// The method looks up the <paramref name="ds"/> for the lat/lon coordinate system.
 /// An axis is considered as a latitude grid if at least one of the following conditions are satisfied
 /// (case is ignored in all rules):
 /// <list type="bullet">
 /// <item><description>axis name starts with either "lat" or "_lat";</description></item>
 /// <item><description>axis name contains substring "latitude";</description></item>
 /// <item><description>axis has attribute Units containing substring "degree" and ends with "n" or "north".</description></item>
 /// </list>
 /// Similar rules for longitude axis:
 /// <list type="bullet">
 /// <item><description>axis name starts with either "lon" or "_lon";</description></item>
 /// <item><description>axis name contains substring "longitude";</description></item>
 /// <item><description>axis has attribute Units containing substring "degree" and ends with "e" or "east".</description></item>
 /// </list>
 /// </para>
 /// <para>If the axes not found, an exception is thrown.</para>
 /// <para>When a coordinate system is found, the Fetch Climate service is requested using a single batch request;
 /// result is added to the DataSet as 2d-variable depending on lat/lon axes.
 /// The DisplayName, long_name, Units, MissingValue, Provenance and Time attributes of the variable are set.
 /// </para>
 /// <example>
 /// <code>
 ///  // Fetching climate parameters for fixed time moment
 ///  using (var ds = DataSet.Open("msds:memory"))
 ///  {
 ///      Console.WriteLine("Filling dataset...");
 ///      ds.AddAxis("lon", "degrees East", -12.5, 20.0, 0.5);
 ///      ds.AddAxis("lat", "degrees North", 35.0, 60.0, 0.5);
 ///
 ///      ds.Fetch(ClimateParameter.FC_TEMPERATURE, "airt", new DateTime(2000, 7, 19, 11, 0, 0)); // time is fixed hence airt is 2d (depends on lat and lon)
 ///      ds.Fetch(ClimateParameter.FC_SOIL_MOISTURE, "soilm", new DateTime(2000, 7, 19, 11, 0, 0));
 ///
 ///      Console.WriteLine("Running DataSet Viewer...");
 ///      ds.View(@"airt(lon,lat) Style:Colormap; Palette:-5=Blue,White=0,#F4EF2F=5,Red=20; MapType:Aerial; Transparency:0.57000000000000006;;soilm(lon,lat) Style:Colormap; Palette:0=#00000000,#827DAAEC=300,#0000A0=800; Transparency:0; MapType:Aerial");
 ///  }
 ///
 ///  // Fetching climate parameters for different time moments
 ///  using (var ds = DataSet.Open("msds:memory"))
 ///  {
 ///      Console.WriteLine("Filling dataset...");
 ///      ds.AddAxis("lon", "degrees East", -12.5, 20.0, 2.0);
 ///      ds.AddAxis("lat", "degrees North", 35.0, 60.0, 2.0);
 ///      ds.AddAxis("time", new DateTime(2000, 7, 19, 0, 0, 0), new DateTime(2000, 7, 19, 23, 0, 0), TimeSpan.FromHours(2));
 ///
 ///      ds.Fetch(ClimateParameter.FC_TEMPERATURE, "airt"); // airt depends on lat,lon,time
 ///      ds.Fetch(ClimateParameter.FC_SOIL_MOISTURE, "soilm");
 ///
 ///      Console.WriteLine("Running DataSet Viewer...");
 ///      ds.View(@"airt(lon,lat) Style:Colormap; Palette:-5=Blue,White=0,#F4EF2F=5,Red=20; MapType:Aerial; Transparency:0.57000000000000006;;soilm(lon,lat) Style:Colormap; Palette:0=#00000000,#827DAAEC=300,#0000A0=800; Transparency:0; MapType:Aerial");
 ///  }
 /// </code>
 /// </example>
 /// </remarks>
 public static Variable Fetch(this DataSet ds, ClimateParameter parameter, string name, DateTime time, string nameUncertainty = null, string nameProvenance = null, EnvironmentalDataSource dataSource = EnvironmentalDataSource.ANY)
 {
     return(Fetch(ds, ClimateService.ClimateParameterToFC2VariableName(parameter), name, time, nameUncertainty, nameProvenance, ClimateService.EnvironmentalDataSourceToArrayOfFC2DataSources(dataSource)));
 }
Esempio n. 2
0
        public void TestParameterIgnoringProvenance(ClimateParameter p, EnvironmentalDataSource ds)
        {
            ClimateService.ServiceUrl = "http://fetchclimate2.cloudapp.net/";

            const double MoscowLat = 55.7;
            const double MoscowLon = 37.5;

            const double PacificLat = -20;
            const double PacificLon = 170;

            const double PacificLatA = -15;
            const double PacificLonA = 175;

            const double KrasnoyarskLat = 56.017;
            const double KrasnoyarskLon = 92.867;

            const double AroundKrasnoyarskLatMin = 55;
            const double AroundKrasnoyarskLonMin = 91;

            const double AroundKrasnoyarskLatMax = 60;
            const double AroundKrasnoyarskLonMax = 95;

            const double SriLankaLatMin = 5;
            const double SriLankaLonMin = 70;

            const double SriLankaLatMax = 20;
            const double SriLankaLonMax = 87;

            string varName = ClimateService.ClimateParameterToFC2VariableName(p);

            Assert.AreNotEqual("", varName, string.Format("Mapping for {0} does not exist.", p.ToString()));

            string[] sources = ClimateService.EnvironmentalDataSourceToArrayOfFC2DataSources(ds);

            //Single point fetch
            var tr1 = new TimeRegion(1961, 1990);
            var tr2 = new TimeRegion(1990, 2000);
            //Moscow
            var request1 = new FetchRequest(
                varName,
                FetchDomain.CreatePoints(
                    new double[] { MoscowLat },
                    new double[] { MoscowLon },
                    tr1),
                sources);
            var    result1 = ClimateService.FetchAsync(request1).Result;
            double sd1     = ((double[])result1["sd"].GetData())[0];
            double value1  = ((double[])result1["values"].GetData())[0];

            Assert.AreEqual(sd1, ClimateService.FetchClimateUncertainty(p, MoscowLat, MoscowLat, MoscowLon, MoscowLon, dataSource: ds));
            Assert.AreEqual(value1, ClimateService.FetchClimate(p, MoscowLat, MoscowLat, MoscowLon, MoscowLon, dataSource: ds));

            //somewhere in Pacific Ocean
            var request2 = new FetchRequest(
                varName,
                FetchDomain.CreatePoints(
                    new double[] { PacificLat },
                    new double[] { PacificLon },
                    tr1),
                sources);
            var    result2 = ClimateService.FetchAsync(request2).Result;
            double sd2     = ((double[])result2["sd"].GetData())[0];
            double value2  = ((double[])result2["values"].GetData())[0];

            Assert.AreEqual(sd2, ClimateService.FetchClimateUncertainty(p, PacificLat, PacificLat, PacificLon, PacificLon, dataSource: ds));
            Assert.AreEqual(value2, ClimateService.FetchClimate(p, PacificLat, PacificLat, PacificLon, PacificLon, dataSource: ds));

            //Cell around Krasnoyarsk
            var request3 = new FetchRequest(
                varName,
                FetchDomain.CreateCells(
                    new double[] { AroundKrasnoyarskLatMin },
                    new double[] { AroundKrasnoyarskLonMin },
                    new double[] { AroundKrasnoyarskLatMax },
                    new double[] { AroundKrasnoyarskLonMax },
                    tr2),
                sources);
            var    result3 = ClimateService.FetchAsync(request3).Result;
            double sd3     = ((double[])result3["sd"].GetData())[0];
            double value3  = ((double[])result3["values"].GetData())[0];

            Assert.AreEqual(sd3, ClimateService.FetchClimateUncertainty(p, AroundKrasnoyarskLatMin, AroundKrasnoyarskLatMax, AroundKrasnoyarskLonMin, AroundKrasnoyarskLonMax, startyear: 1990, stopyear: 2000, dataSource: ds));
            Assert.AreEqual(value3, ClimateService.FetchClimate(p, AroundKrasnoyarskLatMin, AroundKrasnoyarskLatMax, AroundKrasnoyarskLonMin, AroundKrasnoyarskLonMax, startyear: 1990, stopyear: 2000, dataSource: ds));

            //Cell somewhere in Pacific Ocean
            var request4 = new FetchRequest(
                varName,
                FetchDomain.CreateCells(
                    new double[] { PacificLat },
                    new double[] { PacificLon },
                    new double[] { PacificLatA },
                    new double[] { PacificLonA },
                    tr2),
                sources);
            var    result4 = ClimateService.FetchAsync(request4).Result;
            double sd4     = ((double[])result4["sd"].GetData())[0];
            double value4  = ((double[])result4["values"].GetData())[0];

            Assert.AreEqual(sd4, ClimateService.FetchClimateUncertainty(p, PacificLat, PacificLatA, PacificLon, PacificLonA, startyear: 1990, stopyear: 2000, dataSource: ds));
            Assert.AreEqual(value4, ClimateService.FetchClimate(p, PacificLat, PacificLatA, PacificLon, PacificLonA, startyear: 1990, stopyear: 2000, dataSource: ds));

            //batch request
            double[] batchLonMin    = new double[] { PacificLon, AroundKrasnoyarskLonMin };
            double[] batchLonMax    = new double[] { PacificLon, AroundKrasnoyarskLonMax };
            double[] batchLatMin    = new double[] { PacificLat, AroundKrasnoyarskLatMin };
            double[] batchLatMax    = new double[] { PacificLat, AroundKrasnoyarskLatMax };
            int[]    batchStartYear = new int[] { 1961, 1990 };
            int[]    batchStopYear  = new int[] { 1990, 2000 };

            double[] sdGuess1    = ClimateService.FetchClimateUncertainty(p, batchLatMin, batchLatMax, batchLonMin, batchLonMax, null, null, null, null, batchStartYear, batchStopYear, ds);
            double[] valueGuess1 = ClimateService.FetchClimate(p, batchLatMin, batchLatMax, batchLonMin, batchLonMax, null, null, null, null, batchStartYear, batchStopYear, ds);

            Assert.AreEqual(sd2, sdGuess1[0]);
            Assert.AreEqual(sd3, sdGuess1[1]);
            Assert.AreEqual(value2, valueGuess1[0]);
            Assert.AreEqual(value3, valueGuess1[1]);

            //grid request
            var request5 = new FetchRequest(
                varName,
                FetchDomain.CreateCellGrid(
                    Enumerable.Range(0, (int)Math.Round((SriLankaLatMax - SriLankaLatMin) / 1) + 1).Select(i => SriLankaLatMin + i).ToArray(),
                    Enumerable.Range(0, (int)Math.Round((SriLankaLonMax - SriLankaLonMin) / 1) + 1).Select(i => SriLankaLonMin + i).ToArray(),
                    tr2),
                sources);
            var result5 = ClimateService.FetchAsync(request5).Result;

            double[,] gridSds    = (double[, ])result5["sd"].GetData();
            double[,] gridValues = (double[, ])result5["values"].GetData();
            int len0 = gridSds.GetLength(0), len1 = gridSds.GetLength(1);

            double[,] sdGuess2    = ClimateService.FetchUncertaintyGrid(p, SriLankaLatMin, SriLankaLatMax, SriLankaLonMin, SriLankaLonMax, 1, 1, yearmin: 1990, yearmax: 2000, dataSource: ds);
            double[,] valueGuess2 = ClimateService.FetchClimateGrid(p, SriLankaLatMin, SriLankaLatMax, SriLankaLonMin, SriLankaLonMax, 1, 1, yearmin: 1990, yearmax: 2000, dataSource: ds);

            //in FC2 grid is lon x lat while in FC1 it was lat x lon
            Assert.AreEqual(len0, sdGuess2.GetLength(1));
            Assert.AreEqual(len1, sdGuess2.GetLength(0));
            Assert.AreEqual(len0, valueGuess2.GetLength(1));
            Assert.AreEqual(len1, valueGuess2.GetLength(0));
            for (int i = 0; i < len0; ++i)
            {
                for (int j = 0; j < len1; ++j)
                {
                    Assert.AreEqual(gridSds[i, j], sdGuess2[j, i]);
                    Assert.AreEqual(gridValues[i, j], valueGuess2[j, i]);
                }
            }

            //Yearly TimeSeries for Krasnoyarsk
            var tr3      = new TimeRegion().GetYearlyTimeseries(1990, 2000);
            var request6 = new FetchRequest(
                varName,
                FetchDomain.CreatePoints(
                    new double[] { KrasnoyarskLat },
                    new double[] { KrasnoyarskLon },
                    tr3),
                sources);
            var result6 = ClimateService.FetchAsync(request6).Result;

            double[,] seriesSds1    = (double[, ])result6["sd"].GetData();
            double[,] seriesValues1 = (double[, ])result6["values"].GetData();

            double[] seriesSdsGuess1    = ClimateService.FetchClimateYearlyUncertainty(p, KrasnoyarskLat, KrasnoyarskLat, KrasnoyarskLon, KrasnoyarskLon, yearmin: 1990, yearmax: 2000, dataSource: ds);
            double[] seriesValuesGuess1 = ClimateService.FetchClimateYearly(p, KrasnoyarskLat, KrasnoyarskLat, KrasnoyarskLon, KrasnoyarskLon, yearmin: 1990, yearmax: 2000, dataSource: ds);

            Assert.AreEqual(seriesSds1.Length, seriesSdsGuess1.Length);
            Assert.AreEqual(seriesValues1.Length, seriesValuesGuess1.Length);
            for (int i = 0; i < seriesValues1.Length; ++i)
            {
                Assert.AreEqual(seriesSds1[0, i], seriesSdsGuess1[i]);
                Assert.AreEqual(seriesValues1[0, i], seriesValuesGuess1[i]);
            }

            //Monthly TimeSeries for Krasnoyarsk
            var tr4      = new TimeRegion(1990, 1991).GetSeasonlyTimeseries(30, 40);
            var request7 = new FetchRequest(
                varName,
                FetchDomain.CreatePoints(
                    new double[] { KrasnoyarskLat },
                    new double[] { KrasnoyarskLon },
                    tr4),
                sources);
            var result7 = ClimateService.FetchAsync(request7).Result;

            double[,] seriesSds2    = (double[, ])result7["sd"].GetData();
            double[,] seriesValues2 = (double[, ])result7["values"].GetData();

            double[] seriesSdsGuess2    = ClimateService.FetchClimateSeasonlyUncertainty(p, KrasnoyarskLat, KrasnoyarskLat, KrasnoyarskLon, KrasnoyarskLon, daymin: 30, daymax: 40, yearmin: 1990, yearmax: 1991, dataSource: ds);
            double[] seriesValuesGuess2 = ClimateService.FetchClimateSeasonly(p, KrasnoyarskLat, KrasnoyarskLat, KrasnoyarskLon, KrasnoyarskLon, daymin: 30, daymax: 40, yearmin: 1990, yearmax: 1991, dataSource: ds);

            Assert.AreEqual(seriesSds2.Length, seriesSdsGuess2.Length);
            Assert.AreEqual(seriesValues2.Length, seriesValuesGuess2.Length);
            for (int i = 0; i < seriesValues2.Length; ++i)
            {
                Assert.AreEqual(seriesSds2[0, i], seriesSdsGuess2[i]);
                Assert.AreEqual(seriesValues2[0, i], seriesValuesGuess2[i]);
            }

            //Hourly TimeSeries for Krasnoyarsk
            var tr5      = new TimeRegion(1990, 1991, 30, 31).GetHourlyTimeseries(isIntervalTimeseries: true);
            var request8 = new FetchRequest(
                varName,
                FetchDomain.CreatePoints(
                    new double[] { KrasnoyarskLat },
                    new double[] { KrasnoyarskLon },
                    tr5),
                sources);
            var result8 = ClimateService.FetchAsync(request8).Result;

            double[,] seriesSds3    = (double[, ])result8["sd"].GetData();
            double[,] seriesValues3 = (double[, ])result8["values"].GetData();

            double[] seriesSdsGuess3    = ClimateService.FetchClimateHourlyUncertainty(p, KrasnoyarskLat, KrasnoyarskLat, KrasnoyarskLon, KrasnoyarskLon, daymin: 30, daymax: 31, yearmin: 1990, yearmax: 1991, dataSource: ds);
            double[] seriesValuesGuess3 = ClimateService.FetchClimateHourly(p, KrasnoyarskLat, KrasnoyarskLat, KrasnoyarskLon, KrasnoyarskLon, daymin: 30, daymax: 31, yearmin: 1990, yearmax: 1991, dataSource: ds);

            Assert.AreEqual(seriesSds3.Length, seriesSdsGuess3.Length);
            Assert.AreEqual(seriesValues3.Length, seriesValuesGuess3.Length);
            for (int i = 0; i < seriesValues3.Length; ++i)
            {
                Assert.AreEqual(seriesSds3[0, i], seriesSdsGuess3[i]);
                Assert.AreEqual(seriesValues3[0, i], seriesValuesGuess3[i]);
            }
        }