1 : |
agomez |
1 |
#include "surveyindices.h" |
2 : |
|
|
#include "readfunc.h" |
3 : |
|
|
#include "readword.h" |
4 : |
|
|
#include "readaggregation.h" |
5 : |
|
|
#include "areatime.h" |
6 : |
|
|
#include "errorhandler.h" |
7 : |
|
|
#include "stock.h" |
8 : |
|
|
#include "sibylengthonstep.h" |
9 : |
|
|
#include "sibyageonstep.h" |
10 : |
|
|
#include "sibyfleetonstep.h" |
11 : |
|
|
#include "sibyeffortonstep.h" |
12 : |
|
|
#include "sibyacousticonstep.h" |
13 : |
|
|
#include "gadget.h" |
14 : |
|
|
#include "global.h" |
15 : |
|
|
|
16 : |
|
|
SurveyIndices::SurveyIndices(CommentStream& infile, const AreaClass* const Area,
|
17 : |
|
|
const TimeClass* const TimeInfo, double weight, const char* name)
|
18 : |
|
|
: Likelihood(SURVEYINDICESLIKELIHOOD, weight, name) {
|
19 : |
|
|
|
20 : |
|
|
char text[MaxStrLength];
|
21 : |
|
|
strncpy(text, "", MaxStrLength);
|
22 : |
|
|
int i, j, biomass;
|
23 : |
|
|
|
24 : |
|
|
IntMatrix ages;
|
25 : |
|
|
DoubleVector lengths;
|
26 : |
|
|
CharPtrVector charindex, areaindex;
|
27 : |
|
|
|
28 : |
|
|
char datafilename[MaxStrLength];
|
29 : |
|
|
char aggfilename[MaxStrLength];
|
30 : |
|
|
char sitype[MaxStrLength];
|
31 : |
|
|
strncpy(datafilename, "", MaxStrLength);
|
32 : |
|
|
strncpy(aggfilename, "", MaxStrLength);
|
33 : |
|
|
strncpy(sitype, "", MaxStrLength);
|
34 : |
|
|
|
35 : |
|
|
ifstream datafile;
|
36 : |
|
|
CommentStream subdata(datafile);
|
37 : |
|
|
|
38 : |
|
|
readWordAndValue(infile, "datafile", datafilename);
|
39 : |
|
|
readWordAndValue(infile, "sitype", sitype);
|
40 : |
|
|
|
41 : |
|
|
//JMB - added the optional reading of the biomass flag
|
42 : |
|
|
infile >> ws;
|
43 : |
|
|
char c = infile.peek();
|
44 : |
|
|
biomass = 0;
|
45 : |
|
|
if ((c == 'b') || (c == 'B'))
|
46 : |
|
|
readWordAndVariable(infile, "biomass", biomass);
|
47 : |
|
|
if (biomass != 0 && biomass != 1)
|
48 : |
|
|
handle.logFileMessage(LOGFAIL, "\nError in surveyindex - biomass must be 0 or 1");
|
49 : |
|
|
|
50 : |
|
|
//read in area aggregation from file
|
51 : |
|
|
readWordAndValue(infile, "areaaggfile", aggfilename);
|
52 : |
|
|
datafile.open(aggfilename, ios::in);
|
53 : |
|
|
handle.checkIfFailure(datafile, aggfilename);
|
54 : |
|
|
handle.Open(aggfilename);
|
55 : |
|
|
i = readAggregation(subdata, areas, areaindex);
|
56 : |
|
|
handle.Close();
|
57 : |
|
|
datafile.close();
|
58 : |
|
|
datafile.clear();
|
59 : |
|
|
|
60 : |
|
|
//Must change from outer areas to inner areas.
|
61 : |
|
|
for (i = 0; i < areas.Nrow(); i++)
|
62 : |
|
|
for (j = 0; j < areas.Ncol(i); j++)
|
63 : |
|
|
areas[i][j] = Area->getInnerArea(areas[i][j]);
|
64 : |
|
|
|
65 : |
|
|
if ((strcasecmp(sitype, "lengths") == 0) || (strcasecmp(sitype, "length") == 0)) {
|
66 : |
|
|
readWordAndValue(infile, "lenaggfile", aggfilename);
|
67 : |
|
|
datafile.open(aggfilename, ios::in);
|
68 : |
|
|
handle.checkIfFailure(datafile, aggfilename);
|
69 : |
|
|
handle.Open(aggfilename);
|
70 : |
|
|
i = readLengthAggregation(subdata, lengths, charindex);
|
71 : |
|
|
handle.Close();
|
72 : |
|
|
datafile.close();
|
73 : |
|
|
datafile.clear();
|
74 : |
|
|
//read the word 'stocknames'
|
75 : |
|
|
infile >> text >> ws;
|
76 : |
|
|
|
77 : |
|
|
} else if ((strcasecmp(sitype, "ages") == 0) || (strcasecmp(sitype, "age") == 0)) {
|
78 : |
|
|
readWordAndValue(infile, "ageaggfile", aggfilename);
|
79 : |
|
|
datafile.open(aggfilename, ios::in);
|
80 : |
|
|
handle.checkIfFailure(datafile, aggfilename);
|
81 : |
|
|
handle.Open(aggfilename);
|
82 : |
|
|
i = readAggregation(subdata, ages, charindex);
|
83 : |
|
|
handle.Close();
|
84 : |
|
|
datafile.close();
|
85 : |
|
|
datafile.clear();
|
86 : |
|
|
//read the word 'stocknames'
|
87 : |
|
|
infile >> text >> ws;
|
88 : |
|
|
|
89 : |
|
|
} else if ((strcasecmp(sitype, "fleets") == 0) || (strcasecmp(sitype, "fleet") == 0)) {
|
90 : |
|
|
readWordAndValue(infile, "lenaggfile", aggfilename);
|
91 : |
|
|
datafile.open(aggfilename, ios::in);
|
92 : |
|
|
handle.checkIfFailure(datafile, aggfilename);
|
93 : |
|
|
handle.Open(aggfilename);
|
94 : |
|
|
i = readLengthAggregation(subdata, lengths, charindex);
|
95 : |
|
|
handle.Close();
|
96 : |
|
|
datafile.close();
|
97 : |
|
|
datafile.clear();
|
98 : |
|
|
|
99 : |
|
|
//read in the fleetnames
|
100 : |
|
|
i = 0;
|
101 : |
|
|
infile >> text >> ws;
|
102 : |
|
|
if (strcasecmp(text, "fleetnames") != 0)
|
103 : |
|
|
handle.logFileUnexpected(LOGFAIL, "fleetnames", text);
|
104 : |
|
|
infile >> text;
|
105 : |
|
|
while (!infile.eof() && (strcasecmp(text, "stocknames") != 0)) {
|
106 : |
|
|
fleetnames.resize(new char[strlen(text) + 1]);
|
107 : |
|
|
strcpy(fleetnames[i++], text);
|
108 : |
|
|
infile >> text >> ws;
|
109 : |
|
|
}
|
110 : |
|
|
if (fleetnames.Size() == 0)
|
111 : |
|
|
handle.logFileMessage(LOGFAIL, "\nError in surveyindex - failed to read fleets");
|
112 : |
|
|
handle.logMessage(LOGMESSAGE, "Read fleet data - number of fleets", fleetnames.Size());
|
113 : |
|
|
|
114 : |
|
|
} else if ((strcasecmp(sitype, "effort") == 0) || (strcasecmp(sitype, "cpue") == 0)) {
|
115 : |
|
|
//read in the fleetnames
|
116 : |
|
|
i = 0;
|
117 : |
|
|
infile >> text >> ws;
|
118 : |
|
|
if (strcasecmp(text, "fleetnames") != 0)
|
119 : |
|
|
handle.logFileUnexpected(LOGFAIL, "fleetnames", text);
|
120 : |
|
|
infile >> text;
|
121 : |
|
|
while (!infile.eof() && (strcasecmp(text, "stocknames") != 0)) {
|
122 : |
|
|
fleetnames.resize(new char[strlen(text) + 1]);
|
123 : |
|
|
strcpy(fleetnames[i++], text);
|
124 : |
|
|
infile >> text >> ws;
|
125 : |
|
|
}
|
126 : |
|
|
if (fleetnames.Size() == 0)
|
127 : |
|
|
handle.logFileMessage(LOGFAIL, "\nError in surveyindex - failed to read fleets");
|
128 : |
|
|
handle.logMessage(LOGMESSAGE, "Read fleet data - number of fleets", fleetnames.Size());
|
129 : |
|
|
|
130 : |
|
|
if (biomass) //JMB the biomass flag is probably not needed for effort index
|
131 : |
|
|
handle.logMessage(LOGWARN, "Warning in surveyindex - biomass flag specified for effort index");
|
132 : |
|
|
|
133 : |
|
|
} else if (strcasecmp(sitype, "acoustic") == 0) {
|
134 : |
|
|
//read in the fleetnames
|
135 : |
|
|
i = 0;
|
136 : |
|
|
infile >> text >> ws;
|
137 : |
|
|
if (strcasecmp(text, "surveynames") != 0)
|
138 : |
|
|
handle.logFileUnexpected(LOGFAIL, "surveynames", text);
|
139 : |
|
|
infile >> text;
|
140 : |
|
|
while (!infile.eof() && (strcasecmp(text, "stocknames") != 0)) {
|
141 : |
|
|
charindex.resize(new char[strlen(text) + 1]);
|
142 : |
|
|
strcpy(charindex[i++], text);
|
143 : |
|
|
infile >> text >> ws;
|
144 : |
|
|
}
|
145 : |
|
|
if (charindex.Size() == 0)
|
146 : |
|
|
handle.logFileMessage(LOGFAIL, "\nError in surveyindex - failed to read acoustic survey names");
|
147 : |
|
|
handle.logMessage(LOGMESSAGE, "Read acoustic survey data - number of survey names", charindex.Size());
|
148 : |
|
|
|
149 : |
|
|
if (biomass) //JMB the biomass flag is probably not needed for acoustic index
|
150 : |
|
|
handle.logMessage(LOGWARN, "Warning in surveyindex - biomass flag specified for acoustic index");
|
151 : |
|
|
|
152 : |
|
|
} else if (strcasecmp(sitype, "ageandlengths") == 0) {
|
153 : |
|
|
handle.logFileMessage(LOGFAIL, "\nThe ageandlengths surveyindex likelihood component is no longer supported\nUse the surveydistribution likelihood component instead");
|
154 : |
|
|
|
155 : |
|
|
} else
|
156 : |
|
|
handle.logFileMessage(LOGFAIL, "\nError in surveyindex - unrecognised type", sitype);
|
157 : |
|
|
|
158 : |
|
|
if (strcasecmp(text, "stocknames") != 0)
|
159 : |
|
|
handle.logFileUnexpected(LOGFAIL, "stocknames", text);
|
160 : |
|
|
|
161 : |
|
|
i = 0;
|
162 : |
|
|
infile >> text;
|
163 : |
|
|
//read in the stocknames
|
164 : |
|
|
while (!infile.eof() && (strcasecmp(text, "fittype") != 0)) {
|
165 : |
|
|
stocknames.resize(new char[strlen(text) + 1]);
|
166 : |
|
|
strcpy(stocknames[i++], text);
|
167 : |
|
|
infile >> text >> ws;
|
168 : |
|
|
}
|
169 : |
|
|
if (stocknames.Size() == 0)
|
170 : |
|
|
handle.logFileMessage(LOGFAIL, "\nError in surveyindex - failed to read stocks");
|
171 : |
|
|
handle.logMessage(LOGMESSAGE, "Read stock data - number of stocks", stocknames.Size());
|
172 : |
|
|
|
173 : |
|
|
//We have now read in all the data from the main likelihood file
|
174 : |
|
|
if ((strcasecmp(sitype, "lengths") == 0) || (strcasecmp(sitype, "length") == 0)) {
|
175 : |
|
|
SI = new SIByLengthOnStep(infile, areas, lengths, areaindex,
|
176 : |
|
|
charindex, TimeInfo, datafilename, this->getName(), biomass);
|
177 : |
|
|
|
178 : |
|
|
} else if ((strcasecmp(sitype, "ages") == 0) || (strcasecmp(sitype, "age") == 0)) {
|
179 : |
|
|
SI = new SIByAgeOnStep(infile, areas, ages, areaindex,
|
180 : |
|
|
charindex, TimeInfo, datafilename, this->getName(), biomass);
|
181 : |
|
|
|
182 : |
|
|
} else if ((strcasecmp(sitype, "fleets") == 0) || (strcasecmp(sitype, "fleet") == 0)) {
|
183 : |
|
|
SI = new SIByFleetOnStep(infile, areas, lengths, areaindex,
|
184 : |
|
|
charindex, TimeInfo, datafilename, this->getName(), biomass);
|
185 : |
|
|
|
186 : |
|
|
} else if ((strcasecmp(sitype, "effort") == 0) || (strcasecmp(sitype, "cpue") == 0)) {
|
187 : |
|
|
SI = new SIByEffortOnStep(infile, areas, areaindex,
|
188 : |
|
|
fleetnames, TimeInfo, datafilename, this->getName(), biomass);
|
189 : |
|
|
|
190 : |
|
|
} else if (strcasecmp(sitype, "acoustic") == 0) {
|
191 : |
|
|
SI = new SIByAcousticOnStep(infile, areas, areaindex,
|
192 : |
|
|
charindex, TimeInfo, datafilename, this->getName(), biomass);
|
193 : |
|
|
|
194 : |
|
|
} else
|
195 : |
|
|
handle.logFileMessage(LOGFAIL, "\nError in surveyindex - unrecognised type", sitype);
|
196 : |
|
|
|
197 : |
|
|
//delete objects that are no longer needed to free memory
|
198 : |
|
|
for (i = 0; i < charindex.Size(); i++)
|
199 : |
|
|
delete[] charindex[i];
|
200 : |
|
|
for (i = 0; i < areaindex.Size(); i++)
|
201 : |
|
|
delete[] areaindex[i];
|
202 : |
|
|
|
203 : |
|
|
//prepare for next likelihood component
|
204 : |
|
|
infile >> ws;
|
205 : |
|
|
if (!infile.eof()) {
|
206 : |
|
|
infile >> text >> ws;
|
207 : |
|
|
if (strcasecmp(text, "[component]") != 0)
|
208 : |
|
|
handle.logFileUnexpected(LOGFAIL, "[component]", text);
|
209 : |
|
|
}
|
210 : |
|
|
}
|
211 : |
|
|
|
212 : |
|
|
SurveyIndices::~SurveyIndices() {
|
213 : |
|
|
int i;
|
214 : |
|
|
for (i = 0; i < stocknames.Size(); i++)
|
215 : |
|
|
delete[] stocknames[i];
|
216 : |
|
|
for (i = 0; i < fleetnames.Size(); i++)
|
217 : |
|
|
delete[] fleetnames[i];
|
218 : |
|
|
delete SI;
|
219 : |
|
|
}
|
220 : |
|
|
|
221 : |
|
|
void SurveyIndices::addLikelihood(const TimeClass* const TimeInfo) {
|
222 : |
|
|
SI->Sum(TimeInfo);
|
223 : |
|
|
if (TimeInfo->getTime() == TimeInfo->numTotalSteps())
|
224 : |
|
|
likelihood += SI->calcSSE();
|
225 : |
|
|
}
|
226 : |
|
|
|
227 : |
|
|
void SurveyIndices::setFleetsAndStocks(FleetPtrVector& Fleets, StockPtrVector& Stocks) {
|
228 : |
|
|
int i, j, k, found;
|
229 : |
|
|
FleetPtrVector f;
|
230 : |
|
|
StockPtrVector s;
|
231 : |
|
|
|
232 : |
|
|
for (i = 0; i < fleetnames.Size(); i++) {
|
233 : |
|
|
found = 0;
|
234 : |
|
|
for (j = 0; j < Fleets.Size(); j++) {
|
235 : |
|
|
if (strcasecmp(fleetnames[i], Fleets[j]->getName()) == 0) {
|
236 : |
|
|
found++;
|
237 : |
|
|
f.resize(Fleets[j]);
|
238 : |
|
|
}
|
239 : |
|
|
}
|
240 : |
|
|
if (found == 0)
|
241 : |
|
|
handle.logMessage(LOGFAIL, "Error in surveyindex - failed to match fleet", fleetnames[i]);
|
242 : |
|
|
}
|
243 : |
|
|
|
244 : |
|
|
for (i = 0; i < f.Size(); i++)
|
245 : |
|
|
for (j = 0; j < f.Size(); j++)
|
246 : |
|
|
if ((strcasecmp(f[i]->getName(), f[j]->getName()) == 0) && (i != j))
|
247 : |
|
|
handle.logMessage(LOGFAIL, "Error in surveyindex - repeated fleet", f[i]->getName());
|
248 : |
|
|
|
249 : |
|
|
for (i = 0; i < stocknames.Size(); i++) {
|
250 : |
|
|
found = 0;
|
251 : |
|
|
for (j = 0; j < Stocks.Size(); j++) {
|
252 : |
|
|
if (strcasecmp(stocknames[i], Stocks[j]->getName()) == 0) {
|
253 : |
|
|
found++;
|
254 : |
|
|
s.resize(Stocks[j]);
|
255 : |
|
|
}
|
256 : |
|
|
}
|
257 : |
|
|
if (found == 0)
|
258 : |
|
|
handle.logMessage(LOGFAIL, "Error in surveyindex - failed to match stock", stocknames[i]);
|
259 : |
|
|
}
|
260 : |
|
|
|
261 : |
|
|
for (i = 0; i < s.Size(); i++)
|
262 : |
|
|
for (j = 0; j < s.Size(); j++)
|
263 : |
|
|
if ((strcasecmp(s[i]->getName(), s[j]->getName()) == 0) && (i != j))
|
264 : |
|
|
handle.logMessage(LOGFAIL, "Error in surveyindex - repeated stock", s[i]->getName());
|
265 : |
|
|
|
266 : |
|
|
//check fleet and stock areas
|
267 : |
|
|
if (handle.getLogLevel() >= LOGWARN) {
|
268 : |
|
|
if (fleetnames.Size() > 0) { //JMB might not be any fleets
|
269 : |
|
|
for (j = 0; j < areas.Nrow(); j++) {
|
270 : |
|
|
found = 0;
|
271 : |
|
|
for (i = 0; i < f.Size(); i++)
|
272 : |
|
|
for (k = 0; k < areas.Ncol(j); k++)
|
273 : |
|
|
if (f[i]->isInArea(areas[j][k]))
|
274 : |
|
|
found++;
|
275 : |
|
|
if (found == 0)
|
276 : |
|
|
handle.logMessage(LOGWARN, "Warning in surveyindex - fleet not defined on all areas");
|
277 : |
|
|
}
|
278 : |
|
|
}
|
279 : |
|
|
|
280 : |
|
|
for (j = 0; j < areas.Nrow(); j++) {
|
281 : |
|
|
found = 0;
|
282 : |
|
|
for (i = 0; i < s.Size(); i++)
|
283 : |
|
|
for (k = 0; k < areas.Ncol(j); k++)
|
284 : |
|
|
if (s[i]->isInArea(areas[j][k]))
|
285 : |
|
|
found++;
|
286 : |
|
|
if (found == 0)
|
287 : |
|
|
handle.logMessage(LOGWARN, "Warning in surveyindex - stock not defined on all areas");
|
288 : |
|
|
}
|
289 : |
|
|
}
|
290 : |
|
|
|
291 : |
|
|
SI->setFleetsAndStocks(f, s);
|
292 : |
|
|
}
|
293 : |
|
|
|
294 : |
|
|
void SurveyIndices::Reset(const Keeper* const keeper) {
|
295 : |
|
|
Likelihood::Reset(keeper);
|
296 : |
|
|
SI->Reset();
|
297 : |
|
|
if (handle.getLogLevel() >= LOGMESSAGE)
|
298 : |
|
|
handle.logMessage(LOGMESSAGE, "Reset surveyindex component", this->getName());
|
299 : |
|
|
}
|
300 : |
|
|
|
301 : |
|
|
void SurveyIndices::Print(ofstream& outfile) const {
|
302 : |
|
|
int i;
|
303 : |
|
|
outfile << "\nSurvey Indices " << this->getName() << " - likelihood value " << likelihood;
|
304 : |
|
|
if (stocknames.Size() > 0) {
|
305 : |
|
|
outfile << "\n\tStock names: ";
|
306 : |
|
|
for (i = 0; i < stocknames.Size(); i++)
|
307 : |
|
|
outfile << stocknames[i] << sep;
|
308 : |
|
|
}
|
309 : |
|
|
if (fleetnames.Size() > 0) {
|
310 : |
|
|
outfile << "\n\tFleet names: ";
|
311 : |
|
|
for (i = 0; i < fleetnames.Size(); i++)
|
312 : |
|
|
outfile << fleetnames[i] << sep;
|
313 : |
|
|
}
|
314 : |
|
|
outfile << endl;
|
315 : |
|
|
SI->Print(outfile);
|
316 : |
|
|
outfile.flush();
|
317 : |
|
|
}
|