forked from LANDIS-II-Foundation/Library-Age-Cohort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSiteCohorts.cs
189 lines (159 loc) · 6.33 KB
/
SiteCohorts.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using Landis.Core;
using System.Collections;
using System.Collections.Generic;
using Landis.SpatialModeling;
using System;
namespace Landis.Library.AgeOnlyCohorts
{
public class SiteCohorts
: ISiteCohorts
{
private List<SpeciesCohorts> spp_cohorts;
//---------------------------------------------------------------------
public ISpeciesCohorts this[ISpecies species]
{
get {
return GetCohorts(species);
}
}
public IList<ISpecies> ListOfSpeciesPresent
{
get
{
IList<ISpecies> spp_list = new List<ISpecies>();
for (int i = 0; i < spp_cohorts.Count; i++)
{
SpeciesCohorts speciesCohorts = spp_cohorts[i];
if (speciesCohorts.Count > 0)
spp_list.Add(speciesCohorts.Species);
}
return spp_list;
}
}
//---------------------------------------------------------------------
private SpeciesCohorts GetCohorts(ISpecies species)
{
for (int i = 0; i < spp_cohorts.Count; i++) {
SpeciesCohorts speciesCohorts = spp_cohorts[i];
if (speciesCohorts.Species == species)
return speciesCohorts;
}
return null;
}
//---------------------------------------------------------------------
public SiteCohorts()
{
this.spp_cohorts = new List<SpeciesCohorts>();
}
//---------------------------------------------------------------------
public SiteCohorts(IEnumerable<ISpeciesCohorts> cohorts)
{
this.spp_cohorts = new List<SpeciesCohorts>();
foreach (ISpeciesCohorts speciesCohorts in cohorts)
{
this.spp_cohorts.Add(new SpeciesCohorts(speciesCohorts));
}
}
//---------------------------------------------------------------------
/// <summary>
/// Grows all the cohorts by advancing their ages.
/// </summary>
/// <param name="years">
/// The number of years to advance each cohort's age.
/// </param>
/// <param name="site">
/// The site where the cohorts are located.
/// </param>
/// <param name="successionTimestep">
/// Indicates whether the current timestep is a succession timestep.
/// If so, then all young cohorts (i.e., those whose ages are less than
/// or equal to the succession timestep are combined into a single
/// cohort whose age is the succession timestep.
/// </param>
public void Grow(ushort years,
ActiveSite site,
int? successionTimestep,
ICore mCore)
{
// Go through list of species cohorts from back to front so that
// a removal does not mess up the loop.
for (int i = spp_cohorts.Count - 1; i >= 0; i--) {
spp_cohorts[i].Grow(years, site, successionTimestep, mCore);
if (spp_cohorts[i].Count == 0)
spp_cohorts.RemoveAt(i);
}
}
//---------------------------------------------------------------------
public virtual void RemoveMarkedCohorts(ICohortDisturbance disturbance)
{
// Go through list of species cohorts from back to front so that
// a removal does not mess up the loop.
for (int i = spp_cohorts.Count - 1; i >= 0; i--) {
spp_cohorts[i].RemoveMarkedCohorts(disturbance);
if (spp_cohorts[i].Count == 0)
spp_cohorts.RemoveAt(i);
}
}
//---------------------------------------------------------------------
public virtual void RemoveMarkedCohorts(ISpeciesCohortsDisturbance disturbance)
{
// Go through list of species cohorts from back to front so that
// a removal does not mess up the loop.
for (int i = spp_cohorts.Count - 1; i >= 0; i--) {
spp_cohorts[i].RemoveCohorts(disturbance);
if (spp_cohorts[i].Count == 0)
spp_cohorts.RemoveAt(i);
}
}
//---------------------------------------------------------------------
/// <summary>
/// Adds a new cohort for a particular species.
/// </summary>
public void AddNewCohort(ISpecies species)
{
for (int i = 0; i < spp_cohorts.Count; i++) {
SpeciesCohorts speciesCohorts = spp_cohorts[i];
if (speciesCohorts.Species == species) {
speciesCohorts.AddNewCohort();
return;
}
}
// Species not present at the site.
spp_cohorts.Add(new SpeciesCohorts(species));
}
//---------------------------------------------------------------------
public bool IsMaturePresent(ISpecies species)
{
for (int i = 0; i < spp_cohorts.Count; i++) {
SpeciesCohorts speciesCohorts = spp_cohorts[i];
if (speciesCohorts.Species == species) {
return speciesCohorts.IsMaturePresent;
}
}
return false;
}
//---------------------------------------------------------------------
public IEnumerator<ISpeciesCohorts> GetEnumerator()
{
foreach (ISpeciesCohorts speciesCohorts in spp_cohorts)
yield return speciesCohorts;
}
//---------------------------------------------------------------------
public string Write()
{
string msg = "";
for (int i = 0; i < spp_cohorts.Count; i++)
{
SpeciesCohorts speciesCohorts = spp_cohorts[i];
if (speciesCohorts.Count > 0)
foreach (ICohort cohort in speciesCohorts)
msg += String.Format(" {0}/{1};", cohort.Species.Name, cohort.Age);
}
return msg;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}