//---------------------------------------------------------------------

        /// <summary>
        /// Selects all of a species' cohorts for harvesting.
        /// </summary>
        public static void All(ISpeciesCohorts         cohorts,
                               ISpeciesCohortBoolArray isHarvested)
        {
            //loop through all cohorts and mark as harvested
            for (int i = 0; i < isHarvested.Count; i++)
                isHarvested[i] = true;
        }
        //---------------------------------------------------------------------

        #region ISpeciesCohortsDisturbance members

        /// <summary>
        /// Mark which cohorts for a species are to be cut (harvested).
        /// </summary>
        public void MarkCohortsForDeath(ISpeciesCohorts cohorts,
                                        ISpeciesCohortBoolArray isKilled)
        {
            CohortSelector.Harvest(cohorts, isKilled);

            int numKilled = 0;
            for (int i = 0; i < isKilled.Count; i++)
            {
                if (isKilled[i])
                {
                    cohortCounts.IncrementCount(cohorts.Species);
                    numKilled++;
                }
            }

            if (isDebugEnabled)
            {
                if (numKilled > 0)
                {
                    string ageList = "";
                    int i = 0;
                    foreach (ICohort cohort in cohorts)
                    {
                        if (isKilled[i])
                            ageList += string.Format(" {0}", cohort.Age);
                        i++;
                    }
                    log.DebugFormat("      Cut {0} :{1}", cohorts.Species.Name, ageList);
                }
            }
        }
        public void OneCohort_True()
        {
            ISpeciesCohorts cohorts = CreateCohorts(77);

            SelectCohorts(cohorts, selector_2to99_250);
            CheckIsHarvested(true);
        }
        public void FourCohorts_True()
        {
            ISpeciesCohorts cohorts = CreateCohorts(250, 99, 50, 2);

            SelectCohorts(cohorts, selector_2to99_250);
            CheckIsHarvested(true, true, true, true);
        }
        //---------------------------------------------------------------------

        public void AllExceptYoungest(ISpeciesCohorts         cohorts,
                                      ISpeciesCohortBoolArray isDamaged)
        {
            //  Youngest is the last cohort (at index Count - 1)
            for (int i = 0; i < (isDamaged.Count - 1); i++)
                isDamaged[i] = true;
        }
        //---------------------------------------------------------------------

    	/// <summary>
    	/// Selects which of a species' cohorts are harvested.
    	/// </summary>
    	public void Harvest(ISpeciesCohorts         cohorts,
                            ISpeciesCohortBoolArray isHarvested)
    	{
    	    SelectCohorts.Method selectionMethod;
    	    if (selectionMethods.TryGetValue(cohorts.Species, out selectionMethod))
    	        selectionMethod(cohorts, isHarvested);
    	}
        //---------------------------------------------------------------------

        public void OldestOfSelectedSpecies(ISpeciesCohorts         cohorts,
                                            ISpeciesCohortBoolArray isDamaged)
        {
            if (cohorts.Species == SelectedSpecies)
                //  Oldest is first cohort
                isDamaged[0] = true;
        }
        public void ManyCohorts()
        {
            ISpeciesCohorts cohorts = CreateCohorts(501, 500, 300, 299, 201, 200, 155, 99, 75, 56, 50, 20);

            SelectCohorts(cohorts, selector_25_50_75_100to200_300to500);
            CheckIsHarvested(false, true, true, false, false, true, true, false, true, false, true, false);
        }
Beispiel #9
0
        //---------------------------------------------------------------------

        #region ISpeciesCohortsDisturbance members

        /// <summary>
        /// Mark which cohorts for a species are to be cut (harvested).
        /// </summary>
        public void MarkCohortsForDeath(ISpeciesCohorts cohorts,
                                        ISpeciesCohortBoolArray isKilled)
        {
            CohortSelector.Harvest(cohorts, isKilled);

            int numKilled = 0;

            for (int i = 0; i < isKilled.Count; i++)
            {
                if (isKilled[i])
                {
                    cohortCounts.IncrementCount(cohorts.Species);
                    numKilled++;
                }
            }

            if (isDebugEnabled)
            {
                if (numKilled > 0)
                {
                    string ageList = "";
                    int    i       = 0;
                    foreach (ICohort cohort in cohorts)
                    {
                        if (isKilled[i])
                        {
                            ageList += string.Format(" {0}", cohort.Age);
                        }
                        i++;
                    }
                    log.DebugFormat("      Cut {0} :{1}", cohorts.Species.Name, ageList);
                }
            }
        }
        //---------------------------------------------------------------------

        /// <summary>
        /// Selects all of a species' cohorts for harvesting except the oldest.
        /// </summary>
        public static void AllExceptOldest(ISpeciesCohorts         cohorts,
                                           ISpeciesCohortBoolArray isHarvested)
        {
            //  Oldest is first (so start at i = 1 instead of i = 0)
            for (int i = 1; i < isHarvested.Count; i++)
                isHarvested[i] = true;
        }
        public void OneCohort_False()
        {
            ISpeciesCohorts cohorts = CreateCohorts(1);

            SelectCohorts(cohorts, selector_2to99_250);
            CheckIsHarvested(false);
        }
        public void ThreeCohorts()
        {
            ISpeciesCohorts cohorts = CreateCohorts(110, 50, 30);

            SelectCohorts(cohorts, selector_25_50_75_100to200_300to500);
            CheckIsHarvested(true, true, false);
        }
        public void NoCohorts()
        {
            ISpeciesCohorts cohorts = CreateCohorts();

            SelectCohorts(cohorts, selector_25_50_75_100to200_300to500);
            CheckIsHarvested();
        }
        //---------------------------------------------------------------------

        private void CheckCohorts(ISpeciesCohorts biomassCohorts,
                                  List <ushort> cohortAges)
        {
            Assert.IsNotNull(biomassCohorts);
            Assert.AreEqual(cohortAges.Count, biomassCohorts.Count);

            int i = 0;

            foreach (ICohort cohort in biomassCohorts)
            {
                ushort age = cohortAges[i];
                Assert.AreEqual(age, cohort.Age);

                //  If the timestep is 1, then annual growth calculations
                //  are called one less time than usual.
                int expectedBiomass;
                if (isTimestep1)
                {
                    expectedBiomass = initialBiomass + (age - 1) * annualBiomassChange;
                }
                else
                {
                    expectedBiomass = initialBiomass + age * annualBiomassChange;
                }
                Assert.AreEqual(expectedBiomass, cohort.Biomass);

                i++;
            }
        }
        public void FourCohorts_False()
        {
            ISpeciesCohorts cohorts = CreateCohorts(251, 249, 100, 1);

            SelectCohorts(cohorts, selector_2to99_250);
            CheckIsHarvested(false, false, false, false);
        }
Beispiel #16
0
        /// <summary>
        /// Checks that two sets of site cohorts are the same.  Also checks the
        /// site's total biomass.
        /// </summary>
        /// <param name="expected">
        /// Keys are expected species.  A species' value is an array of the
        /// ages and biomasses of its expected cohorts from oldest to youngest.
        /// Ages and biomasses are alternate:
        ///
        /// <pre>
        ///     item 0 = expected age of oldest cohort
        ///     item 1 =    "     biomass  "      "
        ///     item 2 =    "     age of 2nd oldest cohort
        ///     item 3 =    "     biomass "    "      "
        ///     ...
        /// </pre>
        /// </param>
        /// <param name="actual">
        /// The actual site cohorts to verify.
        /// </param>
        public static void CheckCohorts(Dictionary <ISpecies, ushort[]> expected,
                                        ISiteCohorts actual)
        {
            int expectedTotalBiomass = 0;

            foreach (ISpecies species in expected.Keys)
            {
                ISpeciesCohorts speciesCohorts = actual[species];
                Assert.IsNotNull(speciesCohorts);

                //  Assume cohorts are ordered from oldest to youngest
                ushort[] expectedCohortData = expected[species];
                Assert.AreEqual(expectedCohortData.Length, speciesCohorts.Count * 2);
                int i = 0;
                foreach (ICohort cohort in speciesCohorts)
                {
                    Assert.AreEqual(expectedCohortData[i], cohort.Age);
                    Assert.AreEqual(expectedCohortData[i + 1], cohort.Biomass);
                    expectedTotalBiomass += expectedCohortData[i + 1];
                    i += 2;
                }
            }

            //  Check if any extra species beyond those that were expected
            foreach (ISpeciesCohorts speciesCohorts in actual)
            {
                Assert.IsTrue(expected.ContainsKey(speciesCohorts.Species));
            }

            Assert.AreEqual(expectedTotalBiomass, actual.TotalBiomass);
        }
        //---------------------------------------------------------------------

        public void AllOfSelectedSpecies(ISpeciesCohorts         cohorts,
                                         ISpeciesCohortBoolArray isDamaged)
        {
            if (cohorts.Species == SelectedSpecies) {
                for (int i = 0; i < isDamaged.Count; i++)
                    isDamaged[i] = true;
            }
        }
 ////---------------------------------------------------------------------
 //public static string MakeSpeciesMapName(string species)
 //{
 //    return SpeciesMapNames.ReplaceTemplateVars(speciesMapNameTemplate,
 //                                               species,
 //                                               ModelCore.CurrentTime);
 //}
 //--------------------------------------------------------------
 public static double ComputeBiomass(ISpeciesCohorts cohorts)
 {
     double total = 0.0;
     if (cohorts != null)
         foreach (ICohort cohort in cohorts)
             total += (double) (cohort.LeafBiomass + cohort.WoodBiomass);
     return total;
 }
        //---------------------------------------------------------------------

        /// <summary>
        /// Selects which of a species' cohorts are harvested.
        /// </summary>
        public void SelectCohorts(ISpeciesCohorts cohorts,
                                  ISpeciesCohortBoolArray isHarvested)
        {
            for (int i = isHarvested.Count - N; i >= 0; i -= N)
            {
                isHarvested[i] = true;
            }
        }
        //---------------------------------------------------------------------

        public void ClearCut(ISpeciesCohorts cohorts,
                             ISpeciesCohortBoolArray isDamaged)
        {
            for (int i = 0; i < isDamaged.Count; i++)
            {
                isDamaged[i] = true;
            }
        }
        //---------------------------------------------------------------------

        /// <summary>
        /// Selects all of a species' cohorts for harvesting except the
        /// youngest.
        /// </summary>
        public static void AllExceptYoungest(ISpeciesCohorts         cohorts,
                                             ISpeciesCohortBoolArray isHarvested)
        {
            //  Youngest is last.
            int youngestIndex = isHarvested.Count - 1;
            for (int i = 0; i < youngestIndex; i++)
                isHarvested[i] = true;
        }
Beispiel #22
0
        //---------------------------------------------------------------------

        /// <summary>
        /// Selects which of a species' cohorts are harvested.
        /// </summary>
        public void Harvest(ISpeciesCohorts cohorts,
                            ISpeciesCohortBoolArray isHarvested)
        {
            for (int i = 0; i < isHarvested.Count; i++)
            {
                isHarvested[i] = true;
            }
        }
Beispiel #23
0
 public static int ComputeBiomass(ISpeciesCohorts cohorts)
 {
     int total = 0;
     if (cohorts != null)
         foreach (ICohort cohort in cohorts)
             total += cohort.Biomass;
     return total;
 }
        //---------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance by copying a set of species cohorts.
        /// </summary>
        public SpeciesCohorts(ISpeciesCohorts <ICohort> cohorts)
        {
            this.species = cohorts.Species;
            this.ages    = new List <ushort>();
            foreach (ushort age in cohorts.Ages)
            {
                this.ages.Add(age);
            }
        }
Beispiel #25
0
        //---------------------------------------------------------------------

        /// <summary>
        /// Selects which of a species' cohorts are harvested.
        /// </summary>
        public void Harvest(ISpeciesCohorts cohorts,
                            ISpeciesCohortBoolArray isHarvested)
        {
            SelectCohorts.Method selectionMethod;
            if (selectionMethods.TryGetValue(cohorts.Species, out selectionMethod))
            {
                selectionMethod(cohorts, isHarvested);
            }
        }
        //---------------------------------------------------------------------

        public void OldestOfSelectedSpecies(ISpeciesCohorts cohorts,
                                            ISpeciesCohortBoolArray isDamaged)
        {
            if (cohorts.Species == SelectedSpecies)
            {
                //  Oldest is first cohort
                isDamaged[0] = true;
            }
        }
        //---------------------------------------------------------------------

        public void AllExceptYoungest(ISpeciesCohorts cohorts,
                                      ISpeciesCohortBoolArray isDamaged)
        {
            //  Youngest is the last cohort (at index Count - 1)
            for (int i = 0; i < (isDamaged.Count - 1); i++)
            {
                isDamaged[i] = true;
            }
        }
        //---------------------------------------------------------------------

        /// <summary>
        /// Selects all of a species' cohorts for harvesting except the oldest.
        /// </summary>
        public static void AllExceptOldest(ISpeciesCohorts cohorts,
                                           ISpeciesCohortBoolArray isHarvested)
        {
            //  Oldest is first (so start at i = 1 instead of i = 0)
            for (int i = 1; i < isHarvested.Count; i++)
            {
                isHarvested[i] = true;
            }
        }
        //---------------------------------------------------------------------

        /// <summary>
        /// Selects all of a species' cohorts for harvesting.
        /// </summary>
        public static void All(ISpeciesCohorts cohorts,
                               ISpeciesCohortBoolArray isHarvested)
        {
            //loop through all cohorts and mark as harvested
            for (int i = 0; i < isHarvested.Count; i++)
            {
                isHarvested[i] = true;
            }
        }
        public static ushort GetStdDevAge(ISpeciesCohorts speciesCohorts)
        {
            if (speciesCohorts == null)
            {
                return(0);
            }
            ushort std_dev = (ushort)System.Math.Round(System.Math.Sqrt(GetVarAge(speciesCohorts)), 0);

            return(std_dev);
        }
Beispiel #31
0
		/// <summary>
		/// Gets the maximum age among a species' cohorts.
		/// </summary>
		/// <returns>
		/// The age of the oldest cohort or 0 if there are no cohorts.
		/// </returns>
		public static ushort GetMaxAge(ISpeciesCohorts<ICohort> cohorts)
		{
			if (cohorts == null)
				return 0;
			ushort max = 0;
			foreach (ushort age in cohorts.Ages)
				if (age > max)
					max = age;
			return max;
		}
Beispiel #32
0
        //---------------------------------------------------------------------

        private byte CalcForestType(ISiteCohorts <AgeCohort.ICohort> cohorts,
                                    IForestType[]                   forestTypes)
        {
            double[] forTypValue = new double[forestTypes.Length];
            int      forTypeCnt  = 0;

            foreach (IForestType ftype in forestTypes)
            {
                ushort   maxSpeciesAge  = 0;
                IDataset SpeciesDataset = Model.Species;
                foreach (ISpecies species in SpeciesDataset)
                {
                    double sppValue = 0.0;
                    ISpeciesCohorts <AgeCohort.ICohort> speciesCohorts =
                        cohorts[species];
                    maxSpeciesAge = MaxAge(speciesCohorts);

                    if (maxSpeciesAge > 0)
                    {
                        sppValue = (double)maxSpeciesAge /
                                   (double)species.Longevity *
                                   (double)reclassCoefs[species.Index];
                    }

                    if (ftype[species.Index] != 0)
                    {
                        if (ftype[species.Index] == -1)
                        {
                            forTypValue[forTypeCnt] -= sppValue;
                        }
                        if (ftype[species.Index] == 1)
                        {
                            forTypValue[forTypeCnt] += sppValue;
                        }
                    }
                }
                forTypeCnt++;
            }

            int    finalForestType = 0;
            double maxValue        = 0.0;

            forTypeCnt = 0;
            foreach (IForestType ftype in forestTypes)
            {
                //System.Console.WriteLine("ForestTypeNum={0}, Value={1}.",forTypeCnt,forTypValue[forTypeCnt]);
                if (forTypValue[forTypeCnt] > maxValue)
                {
                    maxValue        = forTypValue[forTypeCnt];
                    finalForestType = forTypeCnt + 1;
                }
                forTypeCnt++;
            }
            return((byte)finalForestType);
        }
        //---------------------------------------------------------------------

        /// <summary>
        /// Selects all of a species' cohorts for harvesting except the
        /// youngest.
        /// </summary>
        public static void AllExceptYoungest(ISpeciesCohorts cohorts,
                                             ISpeciesCohortBoolArray isHarvested)
        {
            //  Youngest is last.
            int youngestIndex = isHarvested.Count - 1;

            for (int i = 0; i < youngestIndex; i++)
            {
                isHarvested[i] = true;
            }
        }
 //---------------------------------------------------------------------
 /// <summary>
 /// Selects which of a species' cohorts are harvested.
 /// </summary>
 public void SelectCohorts(ISpeciesCohorts         cohorts,
     ISpeciesCohortBoolArray isHarvested)
 {
     int i = 0;
     foreach (ICohort cohort in cohorts) {
         AgeRange? notUsed;
         if (agesAndRanges.Contains(cohort.Age, out notUsed))
             isHarvested[i] = true;
         i++;
     }
 }
        //---------------------------------------------------------------------

        public void AllOfSelectedSpecies(ISpeciesCohorts cohorts,
                                         ISpeciesCohortBoolArray isDamaged)
        {
            if (cohorts.Species == SelectedSpecies)
            {
                for (int i = 0; i < isDamaged.Count; i++)
                {
                    isDamaged[i] = true;
                }
            }
        }
Beispiel #36
0
 /// <summary>
 /// Gets the maximum age among a species' cohorts.
 /// </summary>
 /// <returns>
 /// The age of the oldest cohort or 0 if there are no cohorts.
 /// </returns>
 public static ushort GetMaxAge(ISpeciesCohorts cohorts)
 {
     if (cohorts == null)
         return 0;
     ushort max = 0;
     foreach (ICohort cohort in cohorts) {
         //  First cohort is the oldest
         max = cohort.Age;
         break;
     }
     return max;
 }
        //---------------------------------------------------------------------

        public void Every2ndCohort(ISpeciesCohorts cohorts,
                                   ISpeciesCohortBoolArray isDamaged)
        {
            int N = 2;
            //  Every Nth cohort, working from youngest to oldest
            int youngestIndex = isDamaged.Count - 1;

            for (int i = youngestIndex - (N - 1); i >= 0; i -= N)
            {
                isDamaged[i] = true;
            }
        }
        //---------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance by copying a set of species cohorts.
        /// </summary>
        public SpeciesCohorts(ISpeciesCohorts cohorts)
        {
            this.species = cohorts.Species;
            this.ages = new List<ushort>(cohorts.Count);
            this.isMaturePresent = false;
            foreach (ICohort cohort in cohorts) {
                ushort age = cohort.Age;
                this.ages.Add(age);
                if (age >= species.Maturity)
                    this.isMaturePresent = true;
            }
        }
Beispiel #39
0
 /// <summary>
 /// Gets the maximum age among a species' cohorts.
 /// </summary>
 /// <returns>
 /// The age of the oldest cohort or 0 if there are no cohorts.
 /// </returns>
 public static ushort GetMaxAge(ISpeciesCohorts cohorts)
 {
     if (cohorts == null || !cohorts.Any())
     {
         return(0);
     }
     else
     {
         //  First cohort is the oldest
         return(cohorts.First().Age);
     }
 }
Beispiel #40
0
        //---------------------------------------------------------------------
        private static int ComputeSpeciesBiomass(ISpeciesCohorts cohorts)
        {
            int total = 0;

            if (cohorts != null)
            {
                foreach (ICohort cohort in cohorts)
                {
                    total += cohort.Biomass;
                }
            }
            return(total);
        }
        private int CalcPercentDeadFir(ActiveSite site)
        {
            int numDeadFir = 0;

            if (SiteVars.NumberDeadFirCohorts == null)
            {
                return(0);
            }

            int minimumStartTime = System.Math.Max(0, SiteVars.TimeOfLastFire[site]);

            for (int i = minimumStartTime; i <= modelCore.CurrentTime; i++)
            {
                if (modelCore.CurrentTime - i <= deadFirMaxAge)
                {
                    if (SiteVars.NumberDeadFirCohorts[site].ContainsKey(i))
                    {
                        numDeadFir += SiteVars.NumberDeadFirCohorts[site][i];
                    }
                }
            }

            int numSiteCohorts = 0;
            int percentDeadFir = 0;

            //ISpeciesDataset SpeciesDataset = modelCore.Species;

            foreach (ISpecies species in modelCore.Species)
            {
                ISpeciesCohorts speciesCohorts = (Landis.Library.BiomassCohorts.ISpeciesCohorts)SiteVars.Cohorts[site][species];
                if (speciesCohorts == null)
                {
                    continue;
                }
                foreach (ICohort cohort in speciesCohorts)
                {
                    numSiteCohorts++;
                }
            }


            //foreach (ISpeciesCohorts speciesCohorts in (Landis.Library.BiomassCohorts.ISpeciesCohorts) SiteVars.Cohorts[site])
            //foreach (ICohort cohort in speciesCohorts)
            //    numSiteCohorts++;


            percentDeadFir = (int)(((double)numDeadFir / (double)(numSiteCohorts + numDeadFir)) * 100.0 + 0.5);


            return(System.Math.Min(percentDeadFir, 100));
        }
        public static int ComputeBiomass(Site site, ISpecies species)
        {
            ISpeciesCohorts cohorts = (Landis.Library.BiomassCohorts.ISpeciesCohorts)SiteVars.Cohorts[site][species];
            int             total   = 0;

            if (cohorts != null)
            {
                foreach (ICohort cohort in cohorts)
                {
                    total += cohort.Biomass;
                }
            }
            return(total);
        }
        //---------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance by copying a set of species cohorts.
        /// </summary>
        public SpeciesCohorts(ISpeciesCohorts cohorts)
        {
            this.species         = cohorts.Species;
            this.ages            = new List <ushort>(cohorts.Count);
            this.isMaturePresent = false;
            foreach (ICohort cohort in cohorts)
            {
                ushort age = cohort.Age;
                this.ages.Add(age);
                if (age >= species.Maturity)
                {
                    this.isMaturePresent = true;
                }
            }
        }
        //---------------------------------------------------------------------

        /// <summary>
        /// Selects which of a species' cohorts are harvested.
        /// </summary>
        public void SelectCohorts(ISpeciesCohorts cohorts,
                                  ISpeciesCohortBoolArray isHarvested)
        {
            int i = 0;

            foreach (ICohort cohort in cohorts)
            {
                AgeRange?notUsed;
                if (agesAndRanges.Contains(cohort.Age, out notUsed))
                {
                    isHarvested[i] = true;
                }
                i++;
            }
        }
        //---------------------------------------------------------------------

        /// <summary>
        /// Selects which of a species' cohorts are harvested.
        /// </summary>
        public void SelectCohorts(ISpeciesCohorts         cohorts,
                                  ISpeciesCohortBoolArray isHarvested)
        {
            int i = 0;
            foreach (ICohort cohort in cohorts) {
                if (ages.Contains(cohort.Age))
                    isHarvested[i] = true;
                else {
                    foreach (AgeRange range in ranges) {
                        if (range.Contains(cohort.Age)) {
                            isHarvested[i] = true;
                            break;
                        }
                    }
                }
                i++;
            }
        }
 public static ushort GetStdDevAge(ISpeciesCohorts speciesCohorts)
 {
     if (speciesCohorts == null)
         return 0;
     ushort std_dev = (ushort)System.Math.Round(System.Math.Sqrt(GetVarAge(speciesCohorts)),0);
     return std_dev;
 }
 public static ushort GetMinAge(ISpeciesCohorts speciesCohorts)
 {
     if (speciesCohorts == null)
         return 0;
     ushort min = 65535;//maxof ushort
     foreach (ICohort cohort in speciesCohorts)
     {
         if(cohort.Age<min)
             min = cohort.Age;
     }
     return min;
 }
        public static ushort GetMedianAge(ISpeciesCohorts speciesCohorts)
        {
            if (speciesCohorts == null)
                return 0;
            ushort median = 0;
            double dbl_median = 0.0;

            List<ushort> cohort_ages = new List<ushort>();
            foreach (ICohort cohort in speciesCohorts)
            {
                cohort_ages.Add(cohort.Age);
            }
            int count = cohort_ages.Count;
            if (count == 0)
            {
                return 0;
            }

            else if (count == 1)
            {
                return cohort_ages[0];
            }

            cohort_ages.Sort();//sorts in ascending order

            if (count % 2 == 0)
            {
                dbl_median = (cohort_ages[count / 2] + cohort_ages[(count / 2) - 1]) / 2.0;
                median = (ushort)dbl_median;
            }
            else
            {
                median = cohort_ages[count / 2];
            }
            return median;
        }
        public static ushort GetAvgAge(ISpeciesCohorts speciesCohorts)
        {
            if (speciesCohorts == null)
                return 0;
            ushort avg = 0;
            uint sum = 0;
            ushort count = 0;

            foreach (ICohort cohort in speciesCohorts)
            {
                sum += cohort.Age;
                count++;
            }

            if (count == 0)
            {
                return 0;
            }

            avg = (ushort)(sum / count);
            return avg;
        }
        //---------------------------------------------------------------------

    	/// <summary>
    	/// Selects which of a species' cohorts are harvested.
    	/// </summary>
    	public void Harvest(ISpeciesCohorts         cohorts,
                            ISpeciesCohortBoolArray isHarvested)
    	{
    	    for (int i = 0; i < isHarvested.Count; i++)
    	        isHarvested[i] = true;
    	}
        //---------------------------------------------------------------------

        /// <summary>
        /// Selects the youngest of a species' cohorts for harvesting.
        /// </summary>
        public static void Youngest(ISpeciesCohorts         cohorts,
                                    ISpeciesCohortBoolArray isHarvested)
        {
            //  Youngest is last.
            isHarvested[isHarvested.Count - 1] = true;
        }
        //---------------------------------------------------------------------

        private void CheckCohorts(ISpeciesCohorts speciesCohorts,
                                  params int[]    agesAsInts)
        {
            ushort[] expectedAges = ToUShorts(agesAsInts);
            if (expectedAges.Length == 0) {
                Assert.IsNull(speciesCohorts);
            }
            else {
                Assert.IsNotNull(speciesCohorts);
                List<ushort> ages = new List<ushort>();
                foreach (ICohort cohort in speciesCohorts)
                    ages.Add(cohort.Age);
                AssertAreEqual(expectedAges, ages);
            }
        }
        //---------------------------------------------------------------------
        void ISpeciesCohortsDisturbance.MarkCohortsForDeath(ISpeciesCohorts cohorts,
                                                         ISpeciesCohortBoolArray isDamaged)
        {
            cohortSelector.Harvest(cohorts, isDamaged);

            int cohortsDamaged = 0;
            for (int i = 0; i < isDamaged.Count; i++) {
                if (isDamaged[i]) {
                    
                    //if this cohort is killed, update the damage table (for the stand of this site) with this species name
                    SiteVars.Stand[currentSite].UpdateDamageTable(cohorts.Species.Name);
                    //PlugIn.ModelCore.UI.WriteLine("Damaged:  {0}.", cohorts.Species.Name);
                    
                    //and increment the cohortsDamaged
                    cohortsDamaged++;
                }
            }
            SiteVars.CohortsDamaged[currentSite] += cohortsDamaged;
        }
        //---------------------------------------------------------------------

    	/// <summary>
    	/// Selects which of a species' cohorts are harvested.
    	/// </summary>
    	public void SelectCohorts(ISpeciesCohorts         cohorts,
                                  ISpeciesCohortBoolArray isHarvested)
    	{
    	    for (int i = isHarvested.Count - N; i >= 0; i -= N)
    	        isHarvested[i] = true;
    	}
        //---------------------------------------------------------------------

        public void Every2ndCohort(ISpeciesCohorts         cohorts,
                                   ISpeciesCohortBoolArray isDamaged)
        {
            int N = 2;
            //  Every Nth cohort, working from youngest to oldest
            int youngestIndex = isDamaged.Count - 1;
            for (int i = youngestIndex - (N - 1); i >= 0; i -= N)
                isDamaged[i] = true;
        }
 public static uint GetVarAge(ISpeciesCohorts speciesCohorts)
 {
     if (speciesCohorts == null)
         return 0;
     ushort avg = GetAvgAge(speciesCohorts);
     double sum = 0;
     ushort count = 0;
     foreach (ICohort cohort in speciesCohorts)
     {
         sum += System.Math.Pow(cohort.Age - avg, 2);
         count++;
     }
     if (count <= 1)
         return 0;
     return (uint)System.Math.Round((sum / (count - 1)));
 }
 //---------------------------------------------------------------------
 void ISpeciesCohortsDisturbance.MarkCohortsForDeath(ISpeciesCohorts cohorts,
     ISpeciesCohortBoolArray isKilled)
 {
     cohortSelector.Harvest(cohorts, isKilled);
 }
        //---------------------------------------------------------------------

        void ISpeciesCohortsDisturbance.Damage(ISpeciesCohorts         cohorts,
                                               ISpeciesCohortBoolArray isDamaged)
        {
            cohortSelector.Harvest(cohorts, isDamaged);

            int cohortsKilled = 0;
            for (int i = 0; i < isDamaged.Count; i++)
                if (isDamaged[i])
                    cohortsKilled++;
            SiteVars.CohortsKilled[currentSite] = cohortsKilled;
        }
        //---------------------------------------------------------------------
        private void CheckCohorts(ISpeciesCohorts biomassCohorts,
                                  List<ushort>    cohortAges)
        {
            Assert.IsNotNull(biomassCohorts);
            Assert.AreEqual(cohortAges.Count, biomassCohorts.Count);

            int i = 0;
            foreach (ICohort cohort in biomassCohorts) {
                ushort age = cohortAges[i];
                Assert.AreEqual(age, cohort.Age);

                //  If the timestep is 1, then annual growth calculations
                //  are called one less time than usual.
                int expectedBiomass;
                if (isTimestep1)
                    expectedBiomass = initialBiomass + (age-1) * annualBiomassChange;
                else
                    expectedBiomass = initialBiomass + age * annualBiomassChange;
                Assert.AreEqual(expectedBiomass, cohort.Biomass);

                i++;
            }
        }
        //---------------------------------------------------------------------

        /// <summary>
        /// Selects the oldest of a species' cohorts for harvesting.
        /// </summary>
        public static void Oldest(ISpeciesCohorts         cohorts,
                                  ISpeciesCohortBoolArray isHarvested)
        {
            //  Oldest is first.
            isHarvested[0] = true;
        }