1 : |
agomez |
1 |
#include "stray.h" |
2 : |
|
|
#include "errorhandler.h" |
3 : |
|
|
#include "keeper.h" |
4 : |
|
|
#include "areatime.h" |
5 : |
|
|
#include "mathfunc.h" |
6 : |
|
|
#include "readfunc.h" |
7 : |
|
|
#include "readword.h" |
8 : |
|
|
#include "readaggregation.h" |
9 : |
|
|
#include "gadget.h" |
10 : |
|
|
#include "global.h" |
11 : |
|
|
|
12 : |
|
|
StrayData::StrayData(CommentStream& infile, const LengthGroupDivision* const lgrpdiv,
|
13 : |
|
|
const IntVector& areas, const AreaClass* const Area, const char* givenname,
|
14 : |
|
|
const TimeClass* const TimeInfo, Keeper* const keeper) : HasName(givenname), LivesOnAreas(areas) {
|
15 : |
|
|
|
16 : |
|
|
keeper->addString("stray");
|
17 : |
|
|
int i, tmpint = 0;
|
18 : |
|
|
istagged = 0;
|
19 : |
|
|
ratioscale = 1.0; //JMB used to scale the ratios to ensure that they sum to 1
|
20 : |
|
|
|
21 : |
|
|
char text[MaxStrLength];
|
22 : |
|
|
strncpy(text, "", MaxStrLength);
|
23 : |
|
|
ifstream datafile;
|
24 : |
|
|
CommentStream subdata(datafile);
|
25 : |
|
|
|
26 : |
|
|
LgrpDiv = new LengthGroupDivision(*lgrpdiv);
|
27 : |
|
|
if (LgrpDiv->Error())
|
28 : |
|
|
handle.logMessage(LOGFAIL, "Error in straying data - failed to create length group");
|
29 : |
|
|
strayProportion.resize(LgrpDiv->numLengthGroups(), 0.0);
|
30 : |
|
|
|
31 : |
|
|
infile >> text >> ws;
|
32 : |
|
|
if ((strcasecmp(text, "straystep") != 0) && (strcasecmp(text, "straysteps") != 0))
|
33 : |
|
|
handle.logFileUnexpected(LOGFAIL, "straysteps", text);
|
34 : |
|
|
|
35 : |
|
|
while (isdigit(infile.peek()) && !infile.eof()) {
|
36 : |
|
|
infile >> tmpint >> ws;
|
37 : |
|
|
strayStep.resize(1, tmpint);
|
38 : |
|
|
}
|
39 : |
|
|
|
40 : |
|
|
for (i = 0; i < strayStep.Size(); i++)
|
41 : |
|
|
if (strayStep[i] < 1 || strayStep[i] > TimeInfo->numSteps())
|
42 : |
|
|
handle.logFileMessage(LOGFAIL, "invalid straying step", strayStep[i]);
|
43 : |
|
|
|
44 : |
|
|
infile >> text >> ws;
|
45 : |
|
|
if ((strcasecmp(text, "strayarea") != 0) && (strcasecmp(text, "strayareas") != 0))
|
46 : |
|
|
handle.logFileUnexpected(LOGFAIL, "strayareas", text);
|
47 : |
|
|
|
48 : |
|
|
while (isdigit(infile.peek()) && !infile.eof()) {
|
49 : |
|
|
infile >> tmpint >> ws;
|
50 : |
|
|
strayArea.resize(1, tmpint);
|
51 : |
|
|
}
|
52 : |
|
|
|
53 : |
|
|
for (i = 0; i < strayArea.Size(); i++)
|
54 : |
|
|
strayArea[i] = Area->getInnerArea(strayArea[i]);
|
55 : |
|
|
|
56 : |
|
|
infile >> text >> ws;
|
57 : |
|
|
if (strcasecmp(text, "straystocksandratios") != 0)
|
58 : |
|
|
handle.logFileUnexpected(LOGFAIL, "straystocksandratios", text);
|
59 : |
|
|
|
60 : |
|
|
i = 0;
|
61 : |
|
|
infile >> text >> ws;
|
62 : |
|
|
while (strcasecmp(text, "proportionfunction") != 0 && !infile.eof()) {
|
63 : |
|
|
strayStockNames.resize(new char[strlen(text) + 1]);
|
64 : |
|
|
strcpy(strayStockNames[i], text);
|
65 : |
|
|
strayRatio.resize(1, keeper);
|
66 : |
|
|
if (!(infile >> strayRatio[i]))
|
67 : |
|
|
handle.logFileMessage(LOGFAIL, "invalid format for stray ratio");
|
68 : |
|
|
strayRatio[i].Inform(keeper);
|
69 : |
|
|
|
70 : |
|
|
infile >> text >> ws;
|
71 : |
|
|
i++;
|
72 : |
|
|
}
|
73 : |
|
|
|
74 : |
|
|
if (infile.eof())
|
75 : |
|
|
handle.logFileEOFMessage(LOGFAIL);
|
76 : |
|
|
if (strcasecmp(text, "proportionfunction") != 0)
|
77 : |
|
|
handle.logFileUnexpected(LOGFAIL, "proportionfunction", text);
|
78 : |
|
|
|
79 : |
|
|
infile >> text >> ws;
|
80 : |
|
|
if (strcasecmp(text, "constant") == 0)
|
81 : |
|
|
fnProportion = new ConstSelectFunc();
|
82 : |
|
|
else if (strcasecmp(text, "straightline") == 0)
|
83 : |
|
|
fnProportion = new StraightSelectFunc();
|
84 : |
|
|
else if (strcasecmp(text, "exponential") == 0)
|
85 : |
|
|
fnProportion = new ExpSelectFunc();
|
86 : |
|
|
else
|
87 : |
|
|
handle.logFileMessage(LOGFAIL, "unrecognised proportion function", text);
|
88 : |
|
|
|
89 : |
|
|
fnProportion->readConstants(infile, TimeInfo, keeper);
|
90 : |
|
|
|
91 : |
|
|
infile >> ws;
|
92 : |
|
|
if (!infile.eof()) {
|
93 : |
|
|
infile >> text >> ws;
|
94 : |
|
|
handle.logFileUnexpected(LOGFAIL, "<end of file>", text);
|
95 : |
|
|
}
|
96 : |
|
|
handle.logMessage(LOGMESSAGE, "Read straying data file");
|
97 : |
|
|
keeper->clearLast();
|
98 : |
|
|
}
|
99 : |
|
|
|
100 : |
|
|
StrayData::~StrayData() {
|
101 : |
|
|
int i;
|
102 : |
|
|
for (i = 0; i < strayStockNames.Size(); i++)
|
103 : |
|
|
delete[] strayStockNames[i];
|
104 : |
|
|
for (i = 0; i < CI.Size(); i++)
|
105 : |
|
|
delete CI[i];
|
106 : |
|
|
delete LgrpDiv;
|
107 : |
|
|
delete fnProportion;
|
108 : |
|
|
}
|
109 : |
|
|
|
110 : |
|
|
void StrayData::setStock(StockPtrVector& stockvec) {
|
111 : |
|
|
int i, j, index;
|
112 : |
|
|
|
113 : |
|
|
for (i = 0; i < strayStockNames.Size(); i++)
|
114 : |
|
|
for (j = 0; j < strayStockNames.Size(); j++)
|
115 : |
|
|
if ((strcasecmp(strayStockNames[i], strayStockNames[j]) == 0) && (i != j))
|
116 : |
|
|
handle.logMessage(LOGFAIL, "Error in straying data - repeated stock", strayStockNames[i]);
|
117 : |
|
|
|
118 : |
|
|
for (i = 0; i < stockvec.Size(); i++)
|
119 : |
|
|
for (j = 0; j < strayStockNames.Size(); j++)
|
120 : |
|
|
if (strcasecmp(stockvec[i]->getName(), strayStockNames[j]) == 0)
|
121 : |
|
|
strayStocks.resize(stockvec[i]);
|
122 : |
|
|
|
123 : |
|
|
if (strayStocks.Size() != strayStockNames.Size()) {
|
124 : |
|
|
handle.logMessage(LOGWARN, "Error in straying data - failed to match straying stocks");
|
125 : |
|
|
for (i = 0; i < stockvec.Size(); i++)
|
126 : |
|
|
handle.logMessage(LOGWARN, "Error in straying data - found stock", stockvec[i]->getName());
|
127 : |
|
|
for (i = 0; i < strayStockNames.Size(); i++)
|
128 : |
|
|
handle.logMessage(LOGWARN, "Error in straying data - looking for stock", strayStockNames[i]);
|
129 : |
|
|
handle.logMessage(LOGFAIL, ""); //JMB this will exit gadget
|
130 : |
|
|
}
|
131 : |
|
|
|
132 : |
|
|
//JMB ensure that the ratio vector is indexed in the right order
|
133 : |
|
|
ratioindex.resize(strayStocks.Size(), 0);
|
134 : |
|
|
for (i = 0; i < strayStocks.Size(); i++)
|
135 : |
|
|
for (j = 0; j < strayStockNames.Size(); j++)
|
136 : |
|
|
if (strcasecmp(strayStocks[i]->getName(), strayStockNames[j]) == 0)
|
137 : |
|
|
ratioindex[i] = j;
|
138 : |
|
|
|
139 : |
|
|
//JMB check that the straying stocks are defined on all the areas
|
140 : |
|
|
int minStrayAge = 9999;
|
141 : |
|
|
int maxStrayAge = 0;
|
142 : |
|
|
double minlength = 9999.0;
|
143 : |
|
|
for (i = 0; i < strayStocks.Size(); i++) {
|
144 : |
|
|
CI.resize(new ConversionIndex(LgrpDiv, strayStocks[i]->getLengthGroupDiv()));
|
145 : |
|
|
if (CI[i]->Error())
|
146 : |
|
|
handle.logMessage(LOGFAIL, "Error in straying data - error when checking length structure");
|
147 : |
|
|
index = 0;
|
148 : |
|
|
for (j = 0; j < strayArea.Size(); j++)
|
149 : |
|
|
if (!strayStocks[i]->isInArea(strayArea[j]))
|
150 : |
|
|
index++;
|
151 : |
|
|
|
152 : |
|
|
if (index != 0)
|
153 : |
|
|
handle.logMessage(LOGWARN, "Warning in straying data - straying stock isnt defined on all areas");
|
154 : |
|
|
|
155 : |
|
|
minStrayAge = min(strayStocks[i]->minAge(), minStrayAge);
|
156 : |
|
|
maxStrayAge = max(strayStocks[i]->maxAge(), maxStrayAge);
|
157 : |
|
|
minlength = min(strayStocks[i]->getLengthGroupDiv()->minLength(), minlength);
|
158 : |
|
|
}
|
159 : |
|
|
|
160 : |
|
|
minStrayLength = LgrpDiv->numLengthGroup(minlength);
|
161 : |
|
|
IntVector minlv(maxStrayAge - minStrayAge + 1, 0);
|
162 : |
|
|
IntVector sizev(maxStrayAge - minStrayAge + 1, LgrpDiv->numLengthGroups());
|
163 : |
|
|
Storage.resize(areas.Size(), minStrayAge, minlv, sizev);
|
164 : |
|
|
for (i = 0; i < Storage.Size(); i++)
|
165 : |
|
|
Storage[i].setToZero();
|
166 : |
|
|
}
|
167 : |
|
|
|
168 : |
|
|
void StrayData::storeStrayingStock(int area, AgeBandMatrix& Alkeys, const TimeClass* const TimeInfo) {
|
169 : |
|
|
|
170 : |
|
|
int age, len;
|
171 : |
|
|
int inarea = this->areaNum(area);
|
172 : |
|
|
double straynumber;
|
173 : |
|
|
|
174 : |
|
|
for (age = Storage[inarea].minAge(); age < Storage[inarea].maxAge(); age++) {
|
175 : |
|
|
for (len = Storage[inarea].minLength(age); len < Storage[inarea].maxLength(age); len++) {
|
176 : |
|
|
straynumber = Alkeys[age][len].N * strayProportion[len];
|
177 : |
|
|
Storage[inarea][age][len].N = straynumber;
|
178 : |
|
|
Storage[inarea][age][len].W = Alkeys[age][len].W;
|
179 : |
|
|
|
180 : |
|
|
if (len >= minStrayLength)
|
181 : |
|
|
Alkeys[age][len].N -= straynumber;
|
182 : |
|
|
}
|
183 : |
|
|
}
|
184 : |
|
|
}
|
185 : |
|
|
|
186 : |
|
|
void StrayData::storeStrayingStock(int area, AgeBandMatrix& Alkeys,
|
187 : |
|
|
AgeBandMatrixRatio& TagAlkeys, const TimeClass* const TimeInfo) {
|
188 : |
|
|
|
189 : |
|
|
if (!istagged)
|
190 : |
|
|
handle.logMessage(LOGFAIL, "Error in stray - invalid tagging experiment");
|
191 : |
|
|
|
192 : |
|
|
int age, len, tag;
|
193 : |
|
|
int inarea = this->areaNum(area);
|
194 : |
|
|
double straynumber;
|
195 : |
|
|
|
196 : |
|
|
for (age = Storage[inarea].minAge(); age < Storage[inarea].maxAge(); age++) {
|
197 : |
|
|
for (len = Storage[inarea].minLength(age); len < Storage[inarea].maxLength(age); len++) {
|
198 : |
|
|
straynumber = Alkeys[age][len].N * strayProportion[len];
|
199 : |
|
|
Storage[inarea][age][len].N = straynumber;
|
200 : |
|
|
Storage[inarea][age][len].W = Alkeys[age][len].W;
|
201 : |
|
|
|
202 : |
|
|
if (len >= minStrayLength)
|
203 : |
|
|
Alkeys[age][len].N -= straynumber;
|
204 : |
|
|
|
205 : |
|
|
for (tag = 0; tag < TagAlkeys.numTagExperiments(); tag++) {
|
206 : |
|
|
straynumber = *(TagAlkeys[age][len][tag].N) * strayProportion[len];
|
207 : |
|
|
if (straynumber < verysmall)
|
208 : |
|
|
*(tagStorage[inarea][age][len][tag].N) = 0.0;
|
209 : |
|
|
else
|
210 : |
|
|
*(tagStorage[inarea][age][len][tag].N) = straynumber;
|
211 : |
|
|
|
212 : |
|
|
if (len >= minStrayLength)
|
213 : |
|
|
*(TagAlkeys[age][len][tag].N) -= straynumber;
|
214 : |
|
|
}
|
215 : |
|
|
}
|
216 : |
|
|
}
|
217 : |
|
|
}
|
218 : |
|
|
|
219 : |
|
|
void StrayData::addStrayStock(int area, const TimeClass* const TimeInfo) {
|
220 : |
|
|
|
221 : |
|
|
int i, inarea = this->areaNum(area);
|
222 : |
|
|
double ratio;
|
223 : |
|
|
for (i = 0; i < strayStocks.Size(); i++) {
|
224 : |
|
|
if (!strayStocks[i]->isInArea(area))
|
225 : |
|
|
handle.logMessage(LOGFAIL, "Error in straying - stray stock doesnt live on area", area);
|
226 : |
|
|
|
227 : |
|
|
if (strayStocks[i]->isBirthday(TimeInfo)) {
|
228 : |
|
|
Storage[inarea].IncrementAge();
|
229 : |
|
|
if (istagged && tagStorage.numTagExperiments() > 0)
|
230 : |
|
|
tagStorage[inarea].IncrementAge(Storage[inarea]);
|
231 : |
|
|
}
|
232 : |
|
|
|
233 : |
|
|
ratio = strayRatio[ratioindex[i]] * ratioscale;
|
234 : |
|
|
strayStocks[i]->Add(Storage[inarea], CI[i], area, ratio);
|
235 : |
|
|
if (istagged && tagStorage.numTagExperiments() > 0)
|
236 : |
|
|
strayStocks[i]->Add(tagStorage, CI[i], area, ratio);
|
237 : |
|
|
}
|
238 : |
|
|
Storage[inarea].setToZero();
|
239 : |
|
|
if (istagged && tagStorage.numTagExperiments() > 0)
|
240 : |
|
|
tagStorage[inarea].setToZero();
|
241 : |
|
|
}
|
242 : |
|
|
|
243 : |
|
|
int StrayData::isStrayStepArea(int area, const TimeClass* const TimeInfo) {
|
244 : |
|
|
int i, j;
|
245 : |
|
|
|
246 : |
|
|
for (i = 0; i < strayStep.Size(); i++)
|
247 : |
|
|
for (j = 0; j < strayArea.Size(); j++)
|
248 : |
|
|
if ((strayStep[i] == TimeInfo->getStep()) && (strayArea[j] == area))
|
249 : |
|
|
return 1;
|
250 : |
|
|
return 0;
|
251 : |
|
|
}
|
252 : |
|
|
|
253 : |
|
|
void StrayData::Reset(const TimeClass* const TimeInfo) {
|
254 : |
|
|
int i;
|
255 : |
|
|
|
256 : |
|
|
//JMB check that the sum of the ratios is 1
|
257 : |
|
|
if (TimeInfo->getTime() == 1) {
|
258 : |
|
|
ratioscale = 0.0;
|
259 : |
|
|
for (i = 0; i < strayRatio.Size(); i++ )
|
260 : |
|
|
ratioscale += strayRatio[i];
|
261 : |
|
|
|
262 : |
|
|
if (isZero(ratioscale)) {
|
263 : |
|
|
handle.logMessage(LOGWARN, "Warning in straying - specified ratios are zero");
|
264 : |
|
|
ratioscale = 1.0;
|
265 : |
|
|
} else if (isEqual(ratioscale, 1.0)) {
|
266 : |
|
|
// do nothing
|
267 : |
|
|
} else {
|
268 : |
|
|
handle.logMessage(LOGWARN, "Warning in straying - scaling ratios using", ratioscale);
|
269 : |
|
|
ratioscale = 1.0 / ratioscale;
|
270 : |
|
|
}
|
271 : |
|
|
}
|
272 : |
|
|
|
273 : |
|
|
fnProportion->updateConstants(TimeInfo);
|
274 : |
|
|
if (fnProportion->didChange(TimeInfo)) {
|
275 : |
|
|
for (i = 0; i < LgrpDiv->numLengthGroups(); i++) {
|
276 : |
|
|
strayProportion[i] = fnProportion->calculate(LgrpDiv->meanLength(i));
|
277 : |
|
|
if (strayProportion[i] < 0.0) {
|
278 : |
|
|
handle.logMessage(LOGWARN, "Warning in straying - function outside bounds", strayProportion[i]);
|
279 : |
|
|
strayProportion[i] = 0.0;
|
280 : |
|
|
}
|
281 : |
|
|
if (strayProportion[i] > 1.0) {
|
282 : |
|
|
handle.logMessage(LOGWARN, "Warning in straying - function outside bounds", strayProportion[i]);
|
283 : |
|
|
strayProportion[i] = 1.0;
|
284 : |
|
|
}
|
285 : |
|
|
}
|
286 : |
|
|
|
287 : |
|
|
if (handle.getLogLevel() >= LOGMESSAGE)
|
288 : |
|
|
handle.logMessage(LOGMESSAGE, "Reset straying data for stock", this->getName());
|
289 : |
|
|
}
|
290 : |
|
|
}
|
291 : |
|
|
|
292 : |
|
|
void StrayData::Print(ofstream& outfile) const {
|
293 : |
|
|
int i;
|
294 : |
|
|
outfile << "\nStraying information\n\tNames of straying stocks:";
|
295 : |
|
|
for (i = 0; i < strayStockNames.Size(); i++)
|
296 : |
|
|
outfile << sep << strayStockNames[i];
|
297 : |
|
|
outfile << "\n\tRatio moving into each stock:";
|
298 : |
|
|
for (i = 0; i < strayRatio.Size(); i++)
|
299 : |
|
|
outfile << sep << (strayRatio[ratioindex[i]] * ratioscale);
|
300 : |
|
|
outfile << "\n\tStraying timesteps:";
|
301 : |
|
|
for (i = 0; i < strayStep.Size(); i++)
|
302 : |
|
|
outfile << sep << strayStep[i];
|
303 : |
|
|
outfile << endl;
|
304 : |
|
|
}
|
305 : |
|
|
|
306 : |
|
|
const StockPtrVector& StrayData::getStrayStocks() {
|
307 : |
|
|
return strayStocks;
|
308 : |
|
|
}
|
309 : |
|
|
|
310 : |
|
|
void StrayData::setTagged() {
|
311 : |
|
|
istagged = 1;
|
312 : |
|
|
//resize tagStorage to be the same size as Storage
|
313 : |
|
|
int i, minage, maxage;
|
314 : |
|
|
minage = Storage[0].minAge();
|
315 : |
|
|
maxage = Storage[0].maxAge();
|
316 : |
|
|
IntVector lower(maxage - minage + 1, 0);
|
317 : |
|
|
IntVector size(maxage - minage + 1, LgrpDiv->numLengthGroups());
|
318 : |
|
|
tagStorage.resize(areas.Size(), minage, lower, size);
|
319 : |
|
|
for (i = 0; i < tagStorage.Size(); i++)
|
320 : |
|
|
tagStorage[i].setToZero();
|
321 : |
|
|
}
|
322 : |
|
|
|
323 : |
|
|
void StrayData::addStrayTag(const char* tagname) {
|
324 : |
|
|
if (!istagged)
|
325 : |
|
|
handle.logMessage(LOGFAIL, "Error in stray - invalid tagging experiment", tagname);
|
326 : |
|
|
tagStorage.addTag(tagname);
|
327 : |
|
|
}
|
328 : |
|
|
|
329 : |
|
|
void StrayData::deleteStrayTag(const char* tagname) {
|
330 : |
|
|
if (!istagged)
|
331 : |
|
|
handle.logMessage(LOGFAIL, "Error in stray - invalid tagging experiment", tagname);
|
332 : |
|
|
|
333 : |
|
|
int minage, maxage, age, len, a;
|
334 : |
|
|
int id = tagStorage.getTagID(tagname);
|
335 : |
|
|
|
336 : |
|
|
if (id >= 0) {
|
337 : |
|
|
minage = tagStorage[0].minAge();
|
338 : |
|
|
maxage = tagStorage[0].maxAge();
|
339 : |
|
|
//free memory allocated for tagging experiment
|
340 : |
|
|
for (a = 0; a < tagStorage.Size(); a++) {
|
341 : |
|
|
for (age = minage; age <= maxage; age++) {
|
342 : |
|
|
for (len = tagStorage[a].minLength(age); len < tagStorage[a].maxLength(age); len++) {
|
343 : |
|
|
delete[] (tagStorage[a][age][len][id].N);
|
344 : |
|
|
(tagStorage[a][age][len][id].N) = NULL;
|
345 : |
|
|
}
|
346 : |
|
|
}
|
347 : |
|
|
}
|
348 : |
|
|
tagStorage.deleteTag(tagname);
|
349 : |
|
|
|
350 : |
|
|
} else
|
351 : |
|
|
handle.logMessage(LOGWARN, "Warning in stray - failed to delete tagging experiment", tagname);
|
352 : |
|
|
}
|