Log In | Get Help   
Home My Page Projects Code Snippets Project Openings Mareframe
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files
[mareframe] Annotation of /trunk/gadget/catchinkilos.cc
[mareframe] / trunk / gadget / catchinkilos.cc Repository:
ViewVC logotype

Annotation of /trunk/gadget/catchinkilos.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : agomez 1 #include "catchinkilos.h"
2 :     #include "readfunc.h"
3 :     #include "readword.h"
4 :     #include "readaggregation.h"
5 :     #include "errorhandler.h"
6 :     #include "areatime.h"
7 :     #include "fleet.h"
8 :     #include "stock.h"
9 :     #include "gadget.h"
10 :     #include "global.h"
11 :    
12 :     CatchInKilos::CatchInKilos(CommentStream& infile, const AreaClass* const Area,
13 :     const TimeClass* const TimeInfo, double weight, const char* name)
14 :     : Likelihood(CATCHINKILOSLIKELIHOOD, weight, name) {
15 :    
16 :     int i, j;
17 :     int numarea = 0;
18 :     int readfile = 0;
19 :     char text[MaxStrLength];
20 :     char datafilename[MaxStrLength];
21 :     char aggfilename[MaxStrLength];
22 :    
23 :     strncpy(text, "", MaxStrLength);
24 :     strncpy(datafilename, "", MaxStrLength);
25 :     strncpy(aggfilename, "", MaxStrLength);
26 :     ifstream datafile;
27 :     CommentStream subdata(datafile);
28 :    
29 :     functionname = new char[MaxStrLength];
30 :     strncpy(functionname, "", MaxStrLength);
31 :     readWordAndValue(infile, "datafile", datafilename);
32 :     readWordAndValue(infile, "function", functionname);
33 :    
34 :     yearly = 0;
35 :     epsilon = 10.0;
36 :     timeindex = 0;
37 :     functionnumber = 0;
38 :     if (strcasecmp(functionname, "sumofsquares") == 0)
39 :     functionnumber = 1;
40 :     else
41 :     handle.logFileMessage(LOGFAIL, "\nError in catchinkilos - unrecognised function", functionname);
42 :    
43 :     infile >> ws;
44 :     char c = infile.peek();
45 :     if ((c == 'a') || (c == 'A')) {
46 :     //we have found either aggregationlevel or areaaggfile ...
47 :     streampos pos = infile.tellg();
48 :    
49 :     infile >> text >> ws;
50 :     if ((strcasecmp(text, "aggregationlevel") == 0))
51 :     infile >> yearly >> ws;
52 :     else if (strcasecmp(text, "areaaggfile") == 0)
53 :     infile.seekg(pos);
54 :     else
55 :     handle.logFileUnexpected(LOGFAIL, "areaaggfile", text);
56 :    
57 :     //JMB - peek at the next char
58 :     c = infile.peek();
59 :    
60 :     if (yearly != 0 && yearly != 1)
61 :     handle.logFileMessage(LOGFAIL, "\nError in catchinkilos - aggregationlevel must be 0 or 1");
62 :     }
63 :    
64 :     //JMB - changed to make the reading of epsilon optional
65 :     c = infile.peek();
66 :     if ((c == 'e') || (c == 'E')) {
67 :     readWordAndVariable(infile, "epsilon", epsilon);
68 :     if (epsilon < verysmall) {
69 :     handle.logFileMessage(LOGWARN, "epsilon should be a positive number - set to default value 10");
70 :     epsilon = 10.0;
71 :     }
72 :     }
73 :    
74 :     readWordAndValue(infile, "areaaggfile", aggfilename);
75 :     datafile.open(aggfilename, ios::in);
76 :     handle.checkIfFailure(datafile, aggfilename);
77 :     handle.Open(aggfilename);
78 :     numarea = readAggregation(subdata, areas, areaindex);
79 :     handle.Close();
80 :     datafile.close();
81 :     datafile.clear();
82 :    
83 :     //Must change from outer areas to inner areas.
84 :     for (i = 0; i < areas.Nrow(); i++)
85 :     for (j = 0; j < areas.Ncol(i); j++)
86 :     areas[i][j] = Area->getInnerArea(areas[i][j]);
87 :    
88 :     //read in the fleetnames
89 :     i = 0;
90 :     infile >> text >> ws;
91 :     if (strcasecmp(text, "fleetnames") != 0)
92 :     handle.logFileUnexpected(LOGFAIL, "fleetnames", text);
93 :     infile >> text >> ws;
94 :     while (!infile.eof() && (strcasecmp(text, "stocknames") != 0)) {
95 :     fleetnames.resize(new char[strlen(text) + 1]);
96 :     strcpy(fleetnames[i++], text);
97 :     infile >> text >> ws;
98 :     }
99 :     if (fleetnames.Size() == 0)
100 :     handle.logFileMessage(LOGFAIL, "\nError in catchinkilos - failed to read fleets");
101 :     handle.logMessage(LOGMESSAGE, "Read fleet data - number of fleets", fleetnames.Size());
102 :    
103 :     //read in the stocknames
104 :     i = 0;
105 :     if (strcasecmp(text, "stocknames") != 0)
106 :     handle.logFileUnexpected(LOGFAIL, "stocknames", text);
107 :     infile >> text;
108 :     while (!infile.eof() && (strcasecmp(text, "[component]") != 0)) {
109 :     infile >> ws;
110 :     stocknames.resize(new char[strlen(text) + 1]);
111 :     strcpy(stocknames[i++], text);
112 :     infile >> text;
113 :     }
114 :     if (stocknames.Size() == 0)
115 :     handle.logFileMessage(LOGFAIL, "\nError in catchinkilos - failed to read stocks");
116 :     handle.logMessage(LOGMESSAGE, "Read stock data - number of stocks", stocknames.Size());
117 :    
118 :     //We have now read in all the data from the main likelihood file
119 :     //But we have to read in the statistics data from datafilename
120 :     datafile.open(datafilename, ios::in);
121 :     handle.checkIfFailure(datafile, datafilename);
122 :     handle.Open(datafilename);
123 :     readCatchInKilosData(subdata, TimeInfo, numarea);
124 :     handle.Close();
125 :     datafile.close();
126 :     datafile.clear();
127 :     }
128 :    
129 :     void CatchInKilos::Reset(const Keeper* const keeper) {
130 :     Likelihood::Reset(keeper);
131 :     if (isZero(weight))
132 :     handle.logMessage(LOGWARN, "Warning in catchinkilos - zero weight for", this->getName());
133 :     modelDistribution.setToZero();
134 :     if (handle.getLogLevel() >= LOGMESSAGE)
135 :     handle.logMessage(LOGMESSAGE, "Reset catchinkilos component", this->getName());
136 :     }
137 :    
138 :     void CatchInKilos::Print(ofstream& outfile) const {
139 :     int i;
140 :     outfile << "\nCatch in Kilos " << this->getName() << " - likelihood value " << likelihood
141 :     << "\n\tFunction " << functionname;
142 :     outfile << "\n\tStock names:";
143 :     for (i = 0; i < stocknames.Size(); i++)
144 :     outfile << sep << stocknames[i];
145 :     outfile << "\n\tFleet names:";
146 :     for (i = 0; i < fleetnames.Size(); i++)
147 :     outfile << sep << fleetnames[i];
148 :     outfile << endl;
149 :     outfile.flush();
150 :     }
151 :    
152 :     double CatchInKilos::calcLikSumSquares(const TimeClass* const TimeInfo) {
153 :     int r, a, f, p;
154 :     double totallikelihood = 0.0;
155 :    
156 :     for (r = 0; r < areas.Nrow(); r++) {
157 :     likelihoodValues[timeindex][r] = 0.0;
158 :     for (a = 0; a < areas.Ncol(r); a++)
159 :     for (f = 0; f < predators.Size(); f++)
160 :     for (p = 0; p < preyindex.Ncol(f); p++)
161 :     modelDistribution[timeindex][r] += predators[f]->getConsumptionBiomass(preyindex[f][p], areas[r][a]);
162 :    
163 :     if ((!yearly) || (TimeInfo->getStep() == TimeInfo->numSteps())) {
164 :     likelihoodValues[timeindex][r] +=
165 :     (log(modelDistribution[timeindex][r] + epsilon) - log(obsDistribution[timeindex][r] + epsilon))
166 :     * (log(modelDistribution[timeindex][r] + epsilon) - log(obsDistribution[timeindex][r] + epsilon));
167 :    
168 :     totallikelihood += likelihoodValues[timeindex][r];
169 :     }
170 :     }
171 :     return totallikelihood;
172 :     }
173 :    
174 :     void CatchInKilos::addLikelihood(const TimeClass* const TimeInfo) {
175 :    
176 :     if ((!(AAT.atCurrentTime(TimeInfo))) || (isZero(weight)))
177 :     return;
178 :    
179 :     if ((handle.getLogLevel() >= LOGMESSAGE) && ((!yearly) || (TimeInfo->getStep() == TimeInfo->numSteps())))
180 :     handle.logMessage(LOGMESSAGE, "Calculating likelihood score for catchinkilos component", this->getName());
181 :    
182 :     int i;
183 :     if (yearly) {
184 :     for (i = 0; i < Years.Size(); i++)
185 :     if (Years[i] == TimeInfo->getYear())
186 :     timeindex = i;
187 :     } else {
188 :     for (i = 0; i < Years.Size(); i++)
189 :     if ((Years[i] == TimeInfo->getYear()) && (Steps[i] == TimeInfo->getStep()))
190 :     timeindex = i;
191 :     }
192 :    
193 :     double l = 0.0;
194 :     switch (functionnumber) {
195 :     case 1:
196 :     l = calcLikSumSquares(TimeInfo);
197 :     break;
198 :     default:
199 :     handle.logMessage(LOGWARN, "Warning in catchinkilos - unrecognised function", functionname);
200 :     break;
201 :     }
202 :    
203 :     if ((!yearly) || (TimeInfo->getStep() == TimeInfo->numSteps())) {
204 :     likelihood += l;
205 :     if (handle.getLogLevel() >= LOGMESSAGE)
206 :     handle.logMessage(LOGMESSAGE, "The likelihood score for this component on this timestep is", l);
207 :     }
208 :     }
209 :    
210 :     CatchInKilos::~CatchInKilos() {
211 :     int i;
212 :     for (i = 0; i < stocknames.Size(); i++)
213 :     delete[] stocknames[i];
214 :     for (i = 0; i < fleetnames.Size(); i++)
215 :     delete[] fleetnames[i];
216 :     for (i = 0; i < areaindex.Size(); i++)
217 :     delete[] areaindex[i];
218 :     delete[] functionname;
219 :     }
220 :    
221 :     void CatchInKilos::setFleetsAndStocks(FleetPtrVector& Fleets, StockPtrVector& Stocks) {
222 :     int i, j, k, found;
223 :     StockPtrVector stocks;
224 :     FleetPtrVector fleets;
225 :    
226 :     for (i = 0; i < fleetnames.Size(); i++) {
227 :     found = 0;
228 :     for (j = 0; j < Fleets.Size(); j++) {
229 :     if (strcasecmp(fleetnames[i], Fleets[j]->getName()) == 0) {
230 :     found++;
231 :     fleets.resize(Fleets[j]);
232 :     }
233 :     }
234 :     if (found == 0)
235 :     handle.logMessage(LOGFAIL, "Error in catchinkilos - unrecognised fleet", fleetnames[i]);
236 :     }
237 :    
238 :     for (i = 0; i < fleets.Size(); i++)
239 :     for (j = 0; j < fleets.Size(); j++)
240 :     if ((strcasecmp(fleets[i]->getName(), fleets[j]->getName()) == 0) && (i != j))
241 :     handle.logMessage(LOGFAIL, "Error in catchinkilos - repeated fleet", fleets[i]->getName());
242 :    
243 :     for (i = 0; i < stocknames.Size(); i++) {
244 :     found = 0;
245 :     for (j = 0; j < Stocks.Size(); j++) {
246 :     if (Stocks[j]->isEaten()) {
247 :     if (strcasecmp(stocknames[i], Stocks[j]->getName()) == 0) {
248 :     found++;
249 :     stocks.resize(Stocks[j]);
250 :     }
251 :     }
252 :     }
253 :     if (found == 0)
254 :     handle.logMessage(LOGFAIL, "Error in catchinkilos - unrecognised stock", stocknames[i]);
255 :     }
256 :    
257 :     for (i = 0; i < stocks.Size(); i++)
258 :     for (j = 0; j < stocks.Size(); j++)
259 :     if ((strcasecmp(stocks[i]->getName(), stocks[j]->getName()) == 0) && (i != j))
260 :     handle.logMessage(LOGFAIL, "Error in catchinkilos - repeated stock", stocks[i]->getName());
261 :    
262 :     //check fleet and stock areas
263 :     if (handle.getLogLevel() >= LOGWARN) {
264 :     for (j = 0; j < areas.Nrow(); j++) {
265 :     found = 0;
266 :     for (i = 0; i < fleets.Size(); i++)
267 :     for (k = 0; k < areas.Ncol(j); k++)
268 :     if (fleets[i]->isInArea(areas[j][k]))
269 :     found++;
270 :     if (found == 0)
271 :     handle.logMessage(LOGWARN, "Warning in catchinkilos - fleet not defined on all areas");
272 :     }
273 :    
274 :     for (j = 0; j < areas.Nrow(); j++) {
275 :     found = 0;
276 :     for (i = 0; i < stocks.Size(); i++)
277 :     for (k = 0; k < areas.Ncol(j); k++)
278 :     if (stocks[i]->isInArea(areas[j][k]))
279 :     found++;
280 :     if (found == 0)
281 :     handle.logMessage(LOGWARN, "Warning in catchinkilos - stock not defined on all areas");
282 :     }
283 :     }
284 :    
285 :     //JMB its simpler to just store pointers to the predators rather than the fleets
286 :     for (i = 0; i < fleets.Size(); i++)
287 :     predators.resize(fleets[i]->getPredator());
288 :    
289 :     for (i = 0; i < predators.Size(); i++) {
290 :     found = 0;
291 :     preyindex.AddRows(1, 0, 0);
292 :     for (j = 0; j < predators[i]->numPreys(); j++)
293 :     for (k = 0; k < stocknames.Size(); k++)
294 :     if (strcasecmp(stocknames[k], predators[i]->getPrey(j)->getName()) == 0) {
295 :     found++;
296 :     preyindex[i].resize(1, j);
297 :     }
298 :    
299 :     if (found == 0)
300 :     handle.logMessage(LOGWARN, "Warning in catchinkilos - found no stocks for fleet", fleetnames[i]);
301 :     }
302 :     }
303 :    
304 :     void CatchInKilos::readCatchInKilosData(CommentStream& infile,
305 :     const TimeClass* TimeInfo, int numarea) {
306 :    
307 :     int i, year, step, count, reject;
308 :     double tmpnumber = 0.0;
309 :     char tmparea[MaxStrLength];
310 :     char tmpfleet[MaxStrLength];
311 :     strncpy(tmparea, "", MaxStrLength);
312 :     strncpy(tmpfleet, "", MaxStrLength);
313 :     int keepdata, timeid, areaid, fleetid, check;
314 :    
315 :     //Check the number of columns in the inputfile
316 :     infile >> ws;
317 :     check = countColumns(infile);
318 :     if (!(((yearly) && ((check == 4) || (check == 5))) || ((!yearly) && (check == 5))))
319 :     handle.logFileMessage(LOGFAIL, "wrong number of columns in inputfile - should be 4 or 5");
320 :    
321 :     step = 1; //default value in case there are only 4 columns in the datafile
322 :     year = count = reject = 0;
323 :     while (!infile.eof()) {
324 :     keepdata = 1;
325 :     if ((yearly) && (check == 4))
326 :     infile >> year >> tmparea >> tmpfleet >> tmpnumber >> ws;
327 :     else
328 :     infile >> year >> step >> tmparea >> tmpfleet >> tmpnumber >> ws;
329 :    
330 :     //crude check to see if something has gone wrong and avoid infinite loops
331 :     if (strlen(tmparea) == 0)
332 :     handle.logFileMessage(LOGFAIL, "failed to read data from file");
333 :    
334 :     //if tmparea is in areaindex find areaid, else dont keep the data
335 :     areaid = -1;
336 :     for (i = 0; i < areaindex.Size(); i++)
337 :     if (strcasecmp(areaindex[i], tmparea) == 0)
338 :     areaid = i;
339 :    
340 :     if (areaid == -1)
341 :     keepdata = 0;
342 :    
343 :     //if tmpfleet is a required fleet keep the data, else ignore it
344 :     fleetid = -1;
345 :     for (i = 0; i < fleetnames.Size(); i++)
346 :     if (strcasecmp(fleetnames[i], tmpfleet) == 0)
347 :     fleetid = i;
348 :    
349 :     if (fleetid == -1)
350 :     keepdata = 0;
351 :    
352 :     //check if the year and step are in the simulation
353 :     timeid = -1;
354 :     if ((TimeInfo->isWithinPeriod(year, step)) && (keepdata == 1)) {
355 :     //if this is a new timestep, resize to store the data
356 :     for (i = 0; i < Years.Size(); i++)
357 :     if ((Years[i] == year) && (yearly || (Steps[i] == step)))
358 :     timeid = i;
359 :    
360 :     if (timeid == -1) {
361 :     Years.resize(1, year);
362 :     if (!(yearly))
363 :     Steps.resize(1, step);
364 :     timeid = (Years.Size() - 1);
365 :     obsDistribution.AddRows(1, numarea, 0.0);
366 :     modelDistribution.AddRows(1, numarea, 0.0);
367 :     likelihoodValues.AddRows(1, numarea, 0.0);
368 :     }
369 :    
370 :     } else
371 :     keepdata = 0;
372 :    
373 :     if (keepdata == 1) {
374 :     //distribution data is required, so store it
375 :     count++;
376 :     //note that we use += to sum the data over all fleets (and possibly time)
377 :     obsDistribution[timeid][areaid] += tmpnumber;
378 :     } else
379 :     reject++; //count number of rejected data points read from file
380 :     }
381 :    
382 :     if (yearly)
383 :     AAT.addActionsAllSteps(Years, TimeInfo);
384 :     else
385 :     AAT.addActions(Years, Steps, TimeInfo);
386 :    
387 :     if (count == 0)
388 :     handle.logMessage(LOGWARN, "Warning in catchinkilos - found no data in the data file for", this->getName());
389 :     if (reject != 0)
390 :     handle.logMessage(LOGMESSAGE, "Discarded invalid catchinkilos data - number of invalid entries", reject);
391 :     handle.logMessage(LOGMESSAGE, "Read catchinkilos data file - number of entries", count);
392 :     }
393 :    
394 :     void CatchInKilos::printLikelihood(ofstream& outfile, const TimeClass* const TimeInfo) {
395 :     if (!AAT.atCurrentTime(TimeInfo))
396 :     return;
397 :    
398 :     if ((yearly) && (TimeInfo->getStep() != TimeInfo->numSteps()))
399 :     return; //if data is aggregated into years then we need the last timestep
400 :    
401 :     int i, area, age, len;
402 :     timeindex = -1;
403 :     if (yearly) {
404 :     for (i = 0; i < Years.Size(); i++)
405 :     if (Years[i] == TimeInfo->getYear())
406 :     timeindex = i;
407 :     } else {
408 :     for (i = 0; i < Years.Size(); i++)
409 :     if ((Years[i] == TimeInfo->getYear()) && (Steps[i] == TimeInfo->getStep()))
410 :     timeindex = i;
411 :     }
412 :    
413 :     if (timeindex == -1)
414 :     handle.logMessage(LOGFAIL, "Error in catchinkilos - invalid timestep");
415 :    
416 :     for (area = 0; area < modelDistribution.Ncol(timeindex); area++) {
417 :     if (yearly)
418 :     outfile << setw(lowwidth) << Years[timeindex] << " all "
419 :     << setw(printwidth) << areaindex[area];
420 :     else
421 :     outfile << setw(lowwidth) << Years[timeindex] << sep << setw(lowwidth)
422 :     << Steps[timeindex] << sep << setw(printwidth) << areaindex[area];
423 :    
424 :     if (fleetnames.Size() == 1)
425 :     outfile << sep << setw(printwidth) << fleetnames[0] << sep;
426 :     else
427 :     outfile << " all ";
428 :    
429 :     //JMB crude filter to remove the 'silly' values from the output
430 :     if (modelDistribution[timeindex][area] < rathersmall)
431 :     outfile << setw(largewidth) << 0 << endl;
432 :     else
433 :     outfile << setprecision(largeprecision) << setw(largewidth)
434 :     << modelDistribution[timeindex][area] << endl;
435 :     }
436 :     }
437 :    
438 :     void CatchInKilos::printSummary(ofstream& outfile) {
439 :     int year, area;
440 :    
441 :     for (year = 0; year < likelihoodValues.Nrow(); year++) {
442 :     for (area = 0; area < likelihoodValues.Ncol(year); area++) {
443 :     if (yearly) {
444 :     outfile << setw(lowwidth) << Years[year] << " all "
445 :     << setw(printwidth) << areaindex[area] << sep
446 :     << setw(largewidth) << this->getName() << sep << setprecision(smallprecision)
447 :     << setw(smallwidth) << weight << sep << setprecision(largeprecision)
448 :     << setw(largewidth) << likelihoodValues[year][area] << endl;
449 :     } else {
450 :     outfile << setw(lowwidth) << Years[year] << sep << setw(lowwidth)
451 :     << Steps[year] << sep << setw(printwidth) << areaindex[area] << sep
452 :     << setw(largewidth) << this->getName() << sep << setprecision(smallprecision)
453 :     << setw(smallwidth) << weight << sep << setprecision(largeprecision)
454 :     << setw(largewidth) << likelihoodValues[year][area] << endl;
455 :     }
456 :     }
457 :     }
458 :     outfile.flush();
459 :     }

root@forge.cesga.es
ViewVC Help
Powered by ViewVC 1.0.0  

Powered By FusionForge