public void KrabbenhoftExample()
    {
      Lake L = new Lake("Sparkling Lake", XYPolygon.GetSquare(0.81e6));
      L.Depth = 8.84e6 / L.Area;
      L.Output.LogAllChemicals = true;

      IsotopeWater LakeWater = new IsotopeWater(1);
      LakeWater.SetIsotopeRatio(5.75);
      TimestampSeries EvapoConcentrations = new TimestampSeries();
      EvapoConcentrations.AddSiValue(new DateTime(1985, 4, 1), 3.95);
      EvapoConcentrations.AddSiValue(new DateTime(1985, 5, 1), 13.9);
      EvapoConcentrations.AddSiValue(new DateTime(1985, 6, 1), 25.24);
      EvapoConcentrations.AddSiValue(new DateTime(1985, 7, 1), 23.97);
      EvapoConcentrations.AddSiValue(new DateTime(1985, 8, 1), 17.13);
      EvapoConcentrations.AddSiValue(new DateTime(1985, 9, 1), 10.40);
      EvapoConcentrations.AddSiValue(new DateTime(1985, 10, 1), 6.12);
      EvapoConcentrations.AddSiValue(new DateTime(1985, 10, 1), 33.24);
      EvapoConcentrations.AllowExtrapolation = true;
      EvapoConcentrations.ExtrapolationMethod = ExtrapolationMethods.RecycleYear;
      LakeWater.EvaporationConcentration = EvapoConcentrations;

      TimestampSeries PrecipConcentrations = new TimestampSeries();
      PrecipConcentrations.AddSiValue(new DateTime(1985, 1, 1), 22.8);
      PrecipConcentrations.AddSiValue(new DateTime(1985, 2, 1), 22.8);
      PrecipConcentrations.AddSiValue(new DateTime(1985, 3, 1), 22.8);
      PrecipConcentrations.AddSiValue(new DateTime(1985, 4, 1), 14.8);
      PrecipConcentrations.AddSiValue(new DateTime(1985, 5, 1), 10.7);
      PrecipConcentrations.AddSiValue(new DateTime(1985, 6, 1), 6.3);
      PrecipConcentrations.AddSiValue(new DateTime(1985, 7, 1), 5.1);
      PrecipConcentrations.AddSiValue(new DateTime(1985, 8, 1), 8.4);
      PrecipConcentrations.AddSiValue(new DateTime(1985, 9, 1), 11.1);
      PrecipConcentrations.AddSiValue(new DateTime(1985, 10, 1), 13.8);
      PrecipConcentrations.AddSiValue(new DateTime(1985, 10, 1), 21.9);
      PrecipConcentrations.AllowExtrapolation = true;
      PrecipConcentrations.ExtrapolationMethod = ExtrapolationMethods.RecycleYear;

      TimespanSeries Precipitation = new TimespanSeries();
      Precipitation.Unit = new HydroNumerics.Core.Unit("cm/month", 1.0 / 100.0 / (86400.0 * 30.0), 0);
      Precipitation.AddValue(new DateTime(1985, 1, 1), new DateTime(1985, 3, 1), 0);
      Precipitation.AddValue(new DateTime(1985, 3, 1), new DateTime(1985, 3, 31), 12.5);
      Precipitation.AddValue(new DateTime(1985, 4, 1), new DateTime(1985, 4, 30), 7.1);
      Precipitation.AddValue(new DateTime(1985, 5, 1), new DateTime(1985, 5, 31), 7.6);
      Precipitation.AddValue(new DateTime(1985, 6, 1), new DateTime(1985, 6, 30), 8.8);
      Precipitation.AddValue(new DateTime(1985, 7, 1), new DateTime(1985, 7, 31), 8.6);
      Precipitation.AddValue(new DateTime(1985, 8, 1), new DateTime(1985, 8, 31), 12.7);
      Precipitation.AddValue(new DateTime(1985, 9, 1), new DateTime(1985, 9, 30), 11);
      Precipitation.AddValue(new DateTime(1985, 10, 1), new DateTime(1985, 10, 31), 6.2);
      Precipitation.AddValue(new DateTime(1985, 11, 1), new DateTime(1985, 11, 30), 4.8);
      Precipitation.AddValue(new DateTime(1985, 11, 30), new DateTime(1985, 12, 31), 0);
      Precipitation.AllowExtrapolation = true;
      Precipitation.ExtrapolationMethod = ExtrapolationMethods.RecycleYear;

      Assert.AreEqual(79, 12*Precipitation.GetValue(new DateTime(1985,1,1), new DateTime(1985,12,31)),3);

      SourceBoundary Precip = new SourceBoundary(Precipitation);
      Precip.WaterSample = new IsotopeWater(1);
      Precip.AddChemicalConcentrationSeries(ChemicalFactory.Instance.GetChemical(ChemicalNames.IsotopeFraction), PrecipConcentrations);

      TimespanSeries Evaporation = new TimespanSeries();
      Evaporation.Unit = new HydroNumerics.Core.Unit("cm/month", 1.0 / 100.0 / (86400.0 * 30.0), 0);
      Evaporation.AddValue(new DateTime(1985, 1, 1), new DateTime(1985, 4, 1), 0);
      Evaporation.AddValue(new DateTime(1985, 4, 1), new DateTime(1985, 4, 30), 2.8);
      Evaporation.AddValue(new DateTime(1985, 5, 1), new DateTime(1985, 5, 31), 7.0);
      Evaporation.AddValue(new DateTime(1985, 6, 1), new DateTime(1985, 6, 30), 10.5);
      Evaporation.AddValue(new DateTime(1985, 7, 1), new DateTime(1985, 7, 31), 11.1);
      Evaporation.AddValue(new DateTime(1985, 8, 1), new DateTime(1985, 8, 31), 10.0);
      Evaporation.AddValue(new DateTime(1985, 9, 1), new DateTime(1985, 9, 30), 7.0);
      Evaporation.AddValue(new DateTime(1985, 10, 1), new DateTime(1985, 10, 31), 4.7);
      Evaporation.AddValue(new DateTime(1985, 11, 1), new DateTime(1985, 11, 30), 0.6);
      Evaporation.AddValue(new DateTime(1985, 11, 30), new DateTime(1985, 12, 31), 0);
      Evaporation.AllowExtrapolation = true;
      Evaporation.ExtrapolationMethod = ExtrapolationMethods.RecycleYear;
      EvaporationRateBoundary erb = new EvaporationRateBoundary(Evaporation);

      Assert.AreEqual(54, 12*Evaporation.GetValue(new DateTime(1985,1,1), new DateTime(1985,12,31)),3);

      
      GroundWaterBoundary grb = new GroundWaterBoundary(L, 1e-7, 1, 1, (XYPolygon) L.Geometry);
      grb.FlowType = GWType.Flow;
      grb.WaterFlow = new TimespanSeries();
      grb.WaterFlow.AddSiValue(DateTime.MinValue,DateTime.MaxValue, Evaporation.Unit.ToSiUnit(29/12) * L.Area);
      IsotopeWater gwsp25 = new IsotopeWater(1);
      gwsp25.SetIsotopeRatio(11.5);
      grb.WaterSample = gwsp25;

      GroundWaterBoundary gout = new GroundWaterBoundary(L, 1e-7, 1, -1, (XYPolygon)L.Geometry);
      gout.FlowType = GWType.Flow;
      gout.WaterFlow = new TimespanSeries();
      gout.WaterFlow.AddSiValue(DateTime.MinValue, DateTime.MaxValue, - Evaporation.Unit.ToSiUnit(54/12) * L.Area);
      
      DateTime Start = new DateTime(1985,1,1);
      L.Precipitation.Add(Precip);
      Precip.ContactGeometry = L.Geometry;
      L.EvaporationBoundaries.Add(erb);
      erb.ContactGeometry = L.Geometry;
      L.GroundwaterBoundaries.Add(grb);
      L.GroundwaterBoundaries.Add(gout);

      Model M = new Model();
      M.WaterBodies.Add(L);
      M.SetState("Initial", Start, LakeWater);

      L.Depth *= 1.5;
      ((IsotopeWater)L.CurrentStoredWater).CurrentTime = Start;
      M.MoveInTime(new DateTime(1985, 12, 31), TimeSpan.FromDays(10));

      M.Save(@"..\..\..\TestData\Krabbenhoft.xml");
    }
        public void GetValue01()  //GetValue(DateTime time)
        {
            TimespanSeries timeSeries = new TimespanSeries();
            timeSeries.AllowExtrapolation = true;
 
            try
            {
                timeSeries.GetValue(new DateTime(2010, 1, 1, 0, 0, 0));
            }
            catch (Exception ex)
            {
                //-- Expected exception when GetValues is invoked on an empty timeseries. --
                Assert.IsTrue(ex.GetType() == typeof(Exception));
            }
            
            timeSeries.RelaxationFactor = 0.0;
            timeSeries.Items.Add(new TimespanValue(new DateTime(2010, 1, 1, 0, 0, 0), new DateTime(2010, 1, 2, 0, 0, 0), 3.0));
            timeSeries.Items.Add(new TimespanValue(new DateTime(2010, 1, 2, 0, 0, 0), new DateTime(2010, 1, 3, 0, 0, 0), 6.0));
            timeSeries.Items.Add(new TimespanValue(new DateTime(2010, 1, 3, 0, 0, 0), new DateTime(2010, 1, 4, 0, 0, 0), 4.0));

            // v
            //     |------------|------------|------------|
            //            3            6            4           
            Assert.AreEqual(1.5, timeSeries.GetValue(new DateTime(2009, 12, 31, 12, 0, 0)));

            // v
            // |------------|------------|------------|
            Assert.AreEqual(3, timeSeries.GetValue(new DateTime(2010, 1, 1, 0, 0, 0)));

            //       v
            // |------------|------------|------------|
            Assert.AreEqual(3, timeSeries.GetValue(new DateTime(2010, 1, 1, 12, 0, 0)));

            //              v
            // |------------|------------|------------|
            Assert.AreEqual(6, timeSeries.GetValue(new DateTime(2010, 1, 2, 0, 0, 0)));

            //                    v
            // |------------|------------|------------|
            Assert.AreEqual(6, timeSeries.GetValue(new DateTime(2010, 1, 2, 12, 0, 0)));

            //                           v
            // |------------|------------|------------|
            Assert.AreEqual(4, timeSeries.GetValue(new DateTime(2010, 1, 3, 0, 0, 0)));

            //                                 v
            // |------------|------------|------------|
            Assert.AreEqual(4, timeSeries.GetValue(new DateTime(2010, 1, 3, 12, 0, 0)));

            //                                        v
            // |------------|------------|------------|
            Assert.AreEqual(4, timeSeries.GetValue(new DateTime(2010, 1, 4, 0, 0, 0)));

            //                                             v
            // |------------|------------|------------|
            Assert.AreEqual(3, timeSeries.GetValue(new DateTime(2010, 1, 4, 12, 0, 0)));

            timeSeries.RelaxationFactor = 1.0;

            // v
            //     |------------|------------|------------|
            //            3            6            4           
            Assert.AreEqual(3.0, timeSeries.GetValue(new DateTime(2009, 12, 31, 12, 0, 0)));

            //                    v
            // |------------|------------|------------|
            Assert.AreEqual(6, timeSeries.GetValue(new DateTime(2010, 1, 2, 12, 0, 0)));

            //                                             v
            // |------------|------------|------------|
            Assert.AreEqual(4, timeSeries.GetValue(new DateTime(2010, 1, 4, 12, 0, 0)));

            timeSeries.Items.RemoveAt(timeSeries.Items.Count - 1);
            timeSeries.Items.RemoveAt(0);
            timeSeries.RelaxationFactor = 1.0;

            //                               v
            //              |------------|
            //                     6
            Assert.AreEqual(6, timeSeries.GetValue(new DateTime(2010, 1, 3, 12, 0, 0)));

            //         v
            //              |------------|
            //                     6
            Assert.AreEqual(6, timeSeries.GetValue(new DateTime(2010, 1, 1, 12, 0, 0)));

            //                     v
            //              |------------|
            //                     6
            Assert.AreEqual(6, timeSeries.GetValue(new DateTime(2010, 1, 2, 12, 0, 0)));
        }
        public void Getvalue3()  
        {
            TimespanSeries ts = new TimespanSeries("Test", new DateTime(1999, 1, 1), 400, 1, TimestepUnit.Days, 0);
            ts.ExtrapolationMethod = ExtrapolationMethods.RecycleYear;
            ts.AllowExtrapolation = true;
            for (int i = 0; i < ts.Items.Count; i++)
            {
                ts.Items[i].Value = i;
            }

            Assert.AreEqual(7, ts.GetValue(new DateTime(1999, 1, 8)));
            Assert.AreEqual(7, ts.GetValue(new DateTime(1995, 1, 8)));
            Assert.AreEqual(365+7, ts.GetValue(new DateTime(2010, 1, 8)));
            Assert.AreEqual(7, ts.GetValue(new DateTime(1995, 1, 8, 12, 0, 0)));

            Assert.AreEqual(7, ts.GetValue(new DateTime(1999, 1, 8), new DateTime(1999, 1, 9)));
            Assert.AreEqual(7, ts.GetValue(new DateTime(1995, 1, 8), new DateTime(1995, 1, 9)));
            Assert.AreEqual(365+7, ts.GetValue(new DateTime(2010, 1, 8), new DateTime(2010, 1, 9)));
            Assert.AreEqual(365 + 7 + 0.5, ts.GetValue(new DateTime(2010, 1, 8), new DateTime(2010, 1, 10)));

        }
        public void Example()
        {
            //The code below demonstrates different ways of using the TimespanSeries class

            // -- Constructing the class (default constructor)--
            TimespanSeries ts = new TimespanSeries();

            // -- Constructing the class (overloaded  constructor) --
            //The timespanSeries below is given the name "My Timeseries, starts at January 1st, 2010 at 00:00:00, has
            //10 time timesteps with length 2 days and value 3
            ts = new TimespanSeries("My Timeseries", new DateTime(2010, 1, 1, 0, 0, 0), 10,  2, TimestepUnit.Days, 3);

            // -- Getting and setting values and time from the timeseries --
            // Direct access:
            double secondValue = ts.Items[1].Value; //The value in the unit as defined by the timeseries
            double secondValueInSI = ts.Unit.ToSiUnit(ts.Items[1].Value); // The value in SI unit
          
            DateTime startTimeforSecondTimestep = ts.Items[1].StartTime;
            DateTime endTimeforSecondTimestep = ts.Items[1].EndTime;
            
            ts.Items[1].Value = 4.5;  // when 4.5 is in the same unit at used by the timeseries
            ts.Items[1].Value = ts.Unit.FromSiToThisUnit(4.5); // when 4.5 is in SI units
            ts.Items[1].Value = ts.Unit.FromUnitToThisUnit(4.5, new Unit("cm/sec", 0.01, 0)); // when 4.5 is in cm pr second

            ts.Items[1].StartTime = new DateTime(2010, 1, 1, 0, 0, 0); //changing the starttime for the second timestep
            //TODO: see if it is possible to check that end time is later than start time.

            // -- Getting values for a given time --
            double x = ts.GetValue(new DateTime(2010, 1, 1, 12, 0, 0));  
            double xINSi = ts.GetSiValue(new DateTime(2010, 1, 1, 12, 0, 0));
            double xInMyUnit = ts.GetValue(new DateTime(2010, 1, 1, 12, 0, 0), new Unit("CM/SEC", 0.01, 0));
           
            // -- Getting values for a give timespan (period) --
            double y = ts.GetValue(new DateTime(2010, 1, 1, 0, 0, 0), new DateTime(2010, 1, 2, 0, 0, 0));
            double yInSi = ts.GetSiValue(new DateTime(2010, 1, 1, 0, 0, 0), new DateTime(2010, 1, 2, 0, 0, 0));
            double yInMyUnit = ts.GetValue(new DateTime(2010, 1, 1, 12, 0, 0), new DateTime(2010,1,2,0,0,0), new Unit("CM/SEC", 0.01, 0));

            // -- Adding values --
            ts.AddValue(new DateTime(2010, 1, 1, 12, 0, 0), new DateTime(2010, 1, 2, 0, 0, 0), 4.5);
            ts.AddSiValue(new DateTime(2010, 1, 1, 12, 0, 0), new DateTime(2010, 1, 2, 0, 0, 0), 4.5);
            
            // -- Append value --
            ts.AppendValue(4.5);  //Appending using the same timestep length as the last time step

            // -- Removing values --
            ts.RemoveAfter(new DateTime(2010, 1, 1, 12, 0, 0));
        }
        public void GetValue02() //GetValue(DateTime fromTime, DateTime toTime)
        {
            //-- Expected exception when GetValues is invoked on an empty timeseries. --
            TimespanSeries timeSeries = new TimespanSeries();

            try
            {
                timeSeries.GetValue(new DateTime(2010, 1, 1, 0, 0, 0), new DateTime(2010, 1, 2, 0, 0, 0));
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.GetType() == typeof(Exception));
            }

            timeSeries.Items.Add(new TimespanValue(new DateTime(2010, 1, 1, 0, 0, 0), new DateTime(2010, 1, 2, 0, 0, 0), 3.0));

            //Testing when only one record in timeseries
            Assert.AreEqual(3.0, timeSeries.GetValue(new DateTime(2010, 11, 1, 0, 0, 0), new DateTime(2010, 12, 1, 0, 0, 0)));

            try
            {
                timeSeries.GetValue(new DateTime(2010, 1, 2, 0, 0, 0), new DateTime(2010, 1, 1, 0, 0, 0));

            }
            catch (Exception ex)
            {
                //Testing invalid argument for timespan
                Assert.IsTrue(ex.GetType() == typeof(Exception));
            }

        
            timeSeries = new TimespanSeries();
            timeSeries.RelaxationFactor = 0.0;
            timeSeries.Items.Add(new TimespanValue(new DateTime(2010, 1, 3, 0, 0, 0),new DateTime(2010, 1, 4, 0, 0, 0), 2));
            timeSeries.Items.Add(new TimespanValue(new DateTime(2010, 1, 4, 0, 0, 0), new DateTime(2010, 1, 6, 0, 0, 0), 3));
            timeSeries.Items.Add(new TimespanValue(new DateTime(2010, 1, 6, 0, 0, 0), new DateTime(2010, 1, 7, 0, 0, 0), 4));
            timeSeries.Items.Add(new TimespanValue(new DateTime(2010, 1, 7, 0, 0, 0), new DateTime(2010, 1, 8, 0, 0, 0), 3));
            timeSeries.Items.Add(new TimespanValue(new DateTime(2010, 1, 8, 0, 0, 0), new DateTime(2010, 1, 10, 0, 0, 0), 5));

            //---------------------------------------------------------------------------------------
            //                            v-----v
            //                  |------|------------|------|------|------------|
            //                     2          3        4       3         5
            //    t--->         3      4            6      7      8            10
            //------------------------------------------------------------------------------------------
            Assert.AreEqual(3, timeSeries.GetValue(new DateTime(2010, 1, 4, 12, 0, 0), new DateTime(2010, 1, 5, 12, 0, 0)));

            //                         v------------v
            //                  |------|------------|------|------|------------|
            Assert.AreEqual(3, timeSeries.GetValue(new DateTime(2010, 1, 4, 0, 0, 0), new DateTime(2010, 1, 5, 0, 0, 0)));

            //                  v------v
            //                  |------|------------|------|------|------------|
            Assert.AreEqual(2, timeSeries.GetValue(new DateTime(2010, 1, 3, 0, 0, 0), new DateTime(2010, 1, 4, 0, 0, 0)));

            //                                                        v-----v
            //                  |------|------------|------|------|------------|
            Assert.AreEqual(5, timeSeries.GetValue(new DateTime(2010, 1, 8, 12, 0, 0), new DateTime(2010, 1, 9, 12, 0, 0)));

            //                                                    v------------v
            //                  |------|------------|------|------|------------|
            Assert.AreEqual(5, timeSeries.GetValue(new DateTime(2010, 1, 8, 0, 0, 0), new DateTime(2010, 1, 10, 0, 0, 0)));

            //                  v----------------------------------------------v
            //                  |------|------------|------|------|------------|
            Assert.AreEqual(25.0 / 7.0, timeSeries.GetValue(new DateTime(2010, 1, 3, 0, 0, 0), new DateTime(2010, 1, 10, 0, 0, 0)), 0.00000000001);

            //                               v----------------v
            //                  |------|------------|------|------|------------|
            Assert.AreEqual(3.4, timeSeries.GetValue(new DateTime(2010, 1, 5, 0, 0, 0), new DateTime(2010, 1, 7, 12, 0, 0)));

            //           v------v
            //                  |------|------------|------|------|------------|
            Assert.AreEqual(1.0, timeSeries.GetValue(new DateTime(2010, 1, 2, 0, 0, 0), new DateTime(2010, 1, 3, 0, 0, 0)), 0.00000000001);

            //    v------v
            //                  |------|------------|------|------|------------|
            Assert.AreEqual(0.0, timeSeries.GetValue(new DateTime(2010, 1, 1, 0, 0, 0), new DateTime(2010, 1, 2, 0, 0, 0)), 0.00000000001);

            //                  v------------v
            //                  |------|------------|------|------|------------|
            Assert.AreEqual(2.5, timeSeries.GetValue(new DateTime(2010, 1, 3, 0, 0, 0), new DateTime(2010, 1, 5, 0, 0, 0)), 0.00000000001);

            //            v------------------v
            //                  |------|------------|------|------|------------|
            Assert.AreEqual(2, timeSeries.GetValue(new DateTime(2010, 1, 2, 0, 0, 0), new DateTime(2010, 1, 5, 0, 0, 0)), 0.00000000001);

            //                                                                 v-----v
            //                  |------|------------|------|------|------------|
            Assert.AreEqual(6.0, timeSeries.GetValue(new DateTime(2010, 1, 10, 0, 0, 0), new DateTime(2010, 1, 11, 0, 0, 0)), 0.00000000001);

            //                                                                        v-----v
            //                  |------|------------|------|------|------------|
            Assert.AreEqual(7.0, timeSeries.GetValue(new DateTime(2010, 1, 11, 0, 0, 0), new DateTime(2010, 1, 12, 0, 0, 0)), 0.00000000001);

            //                                                           v----------v
            //                  |------|------------|------|------|------------|
            Assert.AreEqual(6.5, timeSeries.GetValue(new DateTime(2010, 1, 9, 0, 0, 0), new DateTime(2010, 1, 11, 0, 0, 0)), 0.00000000001);
         }