示例#1
0
    /// <summary>
    /// Small program that extracts a time series from a time variant dfs2.
    /// </summary>
    /// <param name="args"></param>
    static void Main(string[] args)
    {

      TimespanSeries PrecipTS = new TimespanSeries();
      PrecipTS.Name = "Precipitation";
      PrecipTS.Description = "Precipitation extracted from dfs2";
      PrecipTS.Unit = UnitFactory.Instance.GetUnit(NamedUnits.millimeterperday);

      //Open the DFS2-file
      DFS2 precip = new DFS2(@"C:\Users\Jacob\Projekter\MikeSheWrapperForGEUS\novomr6\2-layer-filer\Standard_korrigeret_Prec_DK_10km_1990-2008.dfs2");
      
      //UTM-coordinates for Gjeller sø
      double XUTM = 456198;
      double YUTM = 6272321;

      //Get column and row index from UTM- coordinates
      int col = precip.GetColumnIndex(XUTM);
      int row = precip.GetRowIndex(YUTM);

      //Loop all the time steps
      for (int i = 0; i < precip.NumberOfTimeSteps; i++)
      {
        //Extract the value
        var val =  precip.GetData(i, 1)[row, col];
        //Insert into timeseries
        PrecipTS.AddValue(precip.TimeSteps[i].Subtract(TimeSpan.FromDays(1)), precip.TimeSteps[i], val);
      }
      precip.Dispose();

      //Now do the same for evaporation. DFS2-file may have another grid and timesteps
      DFS2 evap = new DFS2(@"C:\Users\Jacob\Projekter\MikeSheWrapperForGEUS\novomr6\2-layer-filer\Novana_DK_EPmak_40km_1990-1998_20km_1999-2008_ed.dfs2");
      TimespanSeries EvapTS = new TimespanSeries();
      EvapTS.Name = "Evaporation";
      EvapTS.Description = "Evaporation extracted from dfs2";
      EvapTS.Unit = UnitFactory.Instance.GetUnit(NamedUnits.millimeterperday);


      //Get column and row index from UTM- coordinates
      col = evap.GetColumnIndex(XUTM);
      row = evap.GetRowIndex(YUTM);

      for (int i = 0; i < evap.NumberOfTimeSteps; i++)
      {
        //Extract the value
        var val = evap.GetData(i, 1)[row, col];
        //Insert into timeseries
        EvapTS.AddValue(evap.TimeSteps[i].Subtract(TimeSpan.FromDays(1)), evap.TimeSteps[i], val);
      }
      

      //Put all time series into a group and save
      TimeSeriesGroup tsgroup = new TimeSeriesGroup();
      tsgroup.Items.Add(PrecipTS);
      tsgroup.Items.Add(EvapTS);
      tsgroup.Save(@"C:\Users\Jacob\Projekter\GWSW-Interaction\Gjeller Sø\climate.xts");
    }
    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 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));
        }
    private void AddMonthlyValues(TimespanSeries TS, int year, double[] values)
    {
      double conversion1 = 1.0 / 1000 / 86400 / 31;
      double conversion2 = 1.0 / 1000 / 86400 / 28;
      double conversion3 = 1.0 / 1000 / 86400 / 30;
      TS.Unit = new HydroNumerics.Core.Unit("mm/month", conversion3, 0);

      TS.AddValue(new DateTime(year, 1, 1), new DateTime(year, 2, 1), values[0]);
      TS.AddValue(new DateTime(year, 2, 1), new DateTime(year, 3, 1), values[1]);
      TS.AddValue(new DateTime(year, 3, 1), new DateTime(year, 4, 1), values[2]);
      TS.AddValue(new DateTime(year, 4, 1), new DateTime(year, 5, 1), values[3]);
      TS.AddValue(new DateTime(year, 5, 1), new DateTime(year, 6, 1), values[4]);
      TS.AddValue(new DateTime(year, 6, 1), new DateTime(year, 7, 1), values[5]);
      TS.AddValue(new DateTime(year, 7, 1), new DateTime(year, 8, 1), values[6]);
      TS.AddValue(new DateTime(year, 8, 1), new DateTime(year, 9, 1), values[7]);
      TS.AddValue(new DateTime(year, 9, 1), new DateTime(year, 10, 1), values[8]);
      TS.AddValue(new DateTime(year, 10, 1), new DateTime(year, 11, 1), values[9]);
      TS.AddValue(new DateTime(year, 11, 1), new DateTime(year, 12, 1), values[10]);
      TS.AddValue(new DateTime(year, 12, 1), new DateTime(year + 1, 1, 1), values[11]);

    }