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/predatorpreyprinter.cc
[mareframe] / trunk / gadget / predatorpreyprinter.cc Repository:
ViewVC logotype

Annotation of /trunk/gadget/predatorpreyprinter.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : agomez 1 #include "predatorpreyprinter.h"
2 :     #include "predatorpreyaggregator.h"
3 :     #include "areatime.h"
4 :     #include "readword.h"
5 :     #include "readaggregation.h"
6 :     #include "errorhandler.h"
7 :     #include "predator.h"
8 :     #include "stockprey.h"
9 :     #include "gadget.h"
10 :     #include "runid.h"
11 :     #include "global.h"
12 :    
13 :     PredatorPreyPrinter::PredatorPreyPrinter(CommentStream& infile, const TimeClass* const TimeInfo)
14 :     : Printer(PREDATORPREYPRINTER), aggregator(0), alptr(0), dptr(0) {
15 :    
16 :     char text[MaxStrLength];
17 :     strncpy(text, "", MaxStrLength);
18 :    
19 :     //read in the predator names
20 :     int i = 0;
21 :     infile >> text >> ws;
22 :     if (strcasecmp(text, "predatornames") != 0)
23 :     handle.logFileUnexpected(LOGFAIL, "predatornames", text);
24 :     infile >> text >> ws;
25 :     while (!infile.eof() && (strcasecmp(text, "preynames") != 0)) {
26 :     predatornames.resize(new char[strlen(text) + 1]);
27 :     strcpy(predatornames[i++], text);
28 :     infile >> text >> ws;
29 :     }
30 :     if (predatornames.Size() == 0)
31 :     handle.logFileMessage(LOGFAIL, "\nError in predatorpreyprinter - failed to read predators");
32 :     handle.logMessage(LOGMESSAGE, "Read predator data - number of predators", predatornames.Size());
33 :    
34 :     //read in the prey names
35 :     i = 0;
36 :     infile >> text >> ws;
37 :     while (!infile.eof() && (strcasecmp(text, "areaaggfile") != 0)) {
38 :     preynames.resize(new char[strlen(text) + 1]);
39 :     strcpy(preynames[i++], text);
40 :     infile >> text >> ws;
41 :     }
42 :     if (preynames.Size() == 0)
43 :     handle.logFileMessage(LOGFAIL, "\nError in predatorpreyprinter - failed to read preys");
44 :     handle.logMessage(LOGMESSAGE, "Read prey data - number of preys", preynames.Size());
45 :    
46 :     //read in area aggregation from file
47 :     filename = new char[MaxStrLength];
48 :     strncpy(filename, "", MaxStrLength);
49 :     ifstream datafile;
50 :     CommentStream subdata(datafile);
51 :    
52 :     infile >> filename >> ws;
53 :     datafile.open(filename, ios::in);
54 :     handle.checkIfFailure(datafile, filename);
55 :     handle.Open(filename);
56 :     i = readAggregation(subdata, areas, areaindex);
57 :     handle.Close();
58 :     datafile.close();
59 :     datafile.clear();
60 :    
61 :     //read in age aggregation from file
62 :     readWordAndValue(infile, "ageaggfile", filename);
63 :     datafile.open(filename, ios::in);
64 :     handle.checkIfFailure(datafile, filename);
65 :     handle.Open(filename);
66 :     i = readAggregation(subdata, ages, ageindex);
67 :     handle.Close();
68 :     datafile.close();
69 :     datafile.clear();
70 :    
71 :     //read in length aggregation from file
72 :     DoubleVector lengths;
73 :     readWordAndValue(infile, "lenaggfile", filename);
74 :     datafile.open(filename, ios::in);
75 :     handle.checkIfFailure(datafile, filename);
76 :     handle.Open(filename);
77 :     i = readLengthAggregation(subdata, lengths, lenindex);
78 :     handle.Close();
79 :     datafile.close();
80 :     datafile.clear();
81 :    
82 :     LgrpDiv = new LengthGroupDivision(lengths);
83 :     if (LgrpDiv->Error())
84 :     handle.logFileMessage(LOGFAIL, "\nError in predatorpreyprinter - failed to create length group");
85 :    
86 :     //open the printfile
87 :     readWordAndValue(infile, "printfile", filename);
88 :     outfile.open(filename, ios::out);
89 :     handle.checkIfFailure(outfile, filename);
90 :    
91 :     infile >> text >> ws;
92 :     if (strcasecmp(text, "precision") == 0) {
93 :     infile >> precision >> ws >> text >> ws;
94 :     width = precision + 4;
95 :     } else {
96 :     // use default values
97 :     precision = largeprecision;
98 :     width = largewidth;
99 :     }
100 :    
101 :     if (precision < 0)
102 :     handle.logFileMessage(LOGFAIL, "\nError in predatorpreyprinter - invalid value of precision");
103 :    
104 :     if (strcasecmp(text, "printatstart") == 0)
105 :     infile >> printtimeid >> ws >> text >> ws;
106 :     else
107 :     printtimeid = 0;
108 :    
109 :     if (printtimeid != 0 && printtimeid != 1)
110 :     handle.logFileMessage(LOGFAIL, "\nError in predatorpreyprinter - invalid value of printatstart");
111 :    
112 :     if (strcasecmp(text, "yearsandsteps") != 0)
113 :     handle.logFileUnexpected(LOGFAIL, "yearsandsteps", text);
114 :     if (!AAT.readFromFile(infile, TimeInfo))
115 :     handle.logFileMessage(LOGFAIL, "\nError in predatorpreyprinter - wrong format for yearsandsteps");
116 :    
117 :     //prepare for next printfile component
118 :     infile >> ws;
119 :     if (!infile.eof()) {
120 :     infile >> text >> ws;
121 :     if (strcasecmp(text, "[component]") != 0)
122 :     handle.logFileUnexpected(LOGFAIL, "[component]", text);
123 :     }
124 :    
125 :     //finished initializing. Now print first lines
126 :     outfile << "; ";
127 :     RUNID.Print(outfile);
128 :     outfile << "; Predation output file for the following predators";
129 :     for (i = 0; i < predatornames.Size(); i++)
130 :     outfile << sep << predatornames[i];
131 :    
132 :     outfile << "\n; Consuming the following preys";
133 :     for (i = 0; i < preynames.Size(); i++)
134 :     outfile << sep << preynames[i];
135 :    
136 :     if (printtimeid == 0)
137 :     outfile << "\n; Printing the following information at the end of each timestep";
138 :     else
139 :     outfile << "\n; Printing the following information at the start of each timestep";
140 :    
141 :     outfile << "\n; year-step-area-age-length-number consumed-biomass consumed-mortality\n";
142 :     outfile.flush();
143 :     }
144 :    
145 :     void PredatorPreyPrinter::setPredAndPrey(PredatorPtrVector& predatorvec,
146 :     PreyPtrVector& preyvec, const AreaClass* const Area) {
147 :    
148 :     PredatorPtrVector predators;
149 :     PreyPtrVector preys;
150 :     int i, j, k, found, minage, maxage;
151 :     delete aggregator;
152 :    
153 :     for (i = 0; i < predatorvec.Size(); i++)
154 :     for (j = 0; j < predatornames.Size(); j++)
155 :     if (strcasecmp(predatorvec[i]->getName(), predatornames[j]) == 0)
156 :     predators.resize(predatorvec[i]);
157 :    
158 :     if (predators.Size() != predatornames.Size()) {
159 :     handle.logMessage(LOGWARN, "Error in predatorpreyprinter - failed to match predators");
160 :     for (i = 0; i < predatorvec.Size(); i++)
161 :     handle.logMessage(LOGWARN, "Error in predatorpreyprinter - found predator", predatorvec[i]->getName());
162 :     for (i = 0; i < predatornames.Size(); i++)
163 :     handle.logMessage(LOGWARN, "Error in predatorpreyprinter - looking for predator", predatornames[i]);
164 :     handle.logMessage(LOGFAIL, ""); //JMB this will exit gadget
165 :     }
166 :    
167 :     for (i = 0; i < predators.Size(); i++)
168 :     for (j = 0; j < predators.Size(); j++)
169 :     if ((strcasecmp(predators[i]->getName(), predators[j]->getName()) == 0) && (i != j))
170 :     handle.logMessage(LOGFAIL, "Error in predatorpreyprinter - repeated predator", predators[i]->getName());
171 :    
172 :     for (i = 0; i < preyvec.Size(); i++)
173 :     for (j = 0; j < preynames.Size(); j++)
174 :     if (strcasecmp(preyvec[i]->getName(), preynames[j]) == 0)
175 :     preys.resize(preyvec[i]);
176 :    
177 :     if (preys.Size() != preynames.Size()) {
178 :     handle.logMessage(LOGWARN, "Error in predatorpreyprinter - failed to match preys");
179 :     for (i = 0; i < preyvec.Size(); i++)
180 :     handle.logMessage(LOGWARN, "Error in predatorpreyprinter - found prey", preyvec[i]->getName());
181 :     for (i = 0; i < preynames.Size(); i++)
182 :     handle.logMessage(LOGWARN, "Error in predatorpreyprinter - looking for prey", preynames[i]);
183 :     handle.logMessage(LOGFAIL, ""); //JMB this will exit gadget
184 :     }
185 :    
186 :     for (i = 0; i < preys.Size(); i++)
187 :     for (j = 0; j < preys.Size(); j++)
188 :     if ((strcasecmp(preys[i]->getName(), preys[j]->getName()) == 0) && (i != j))
189 :     handle.logMessage(LOGFAIL, "Error in predatorpreyprinter - repeated prey", preys[i]->getName());
190 :    
191 :     //check that the preys are stocks and not otherfood
192 :     for (i = 0; i < preys.Size(); i++)
193 :     if (preys[i]->getType() == LENGTHPREY)
194 :     handle.logMessage(LOGFAIL, "Error in predatorpreyprinter - cannot print prey", preys[i]->getName());
195 :    
196 :     //change from outer areas to inner areas.
197 :     for (i = 0; i < areas.Nrow(); i++)
198 :     for (j = 0; j < areas.Ncol(i); j++)
199 :     areas[i][j] = Area->getInnerArea(areas[i][j]);
200 :    
201 :     //check predator and prey areas, ages and length groups
202 :     if (handle.getLogLevel() >= LOGWARN) {
203 :     for (j = 0; j < areas.Nrow(); j++) {
204 :     found = 0;
205 :     for (i = 0; i < predators.Size(); i++)
206 :     for (k = 0; k < areas.Ncol(j); k++)
207 :     if (predators[i]->isInArea(areas[j][k]))
208 :     found++;
209 :     if (found == 0)
210 :     handle.logMessage(LOGWARN, "Warning in predatorpreyprinter - predators not defined on all areas");
211 :     }
212 :    
213 :     for (j = 0; j < areas.Nrow(); j++) {
214 :     found = 0;
215 :     for (i = 0; i < preys.Size(); i++)
216 :     for (k = 0; k < areas.Ncol(j); k++)
217 :     if (preys[i]->isInArea(areas[j][k]))
218 :     found++;
219 :     if (found == 0)
220 :     handle.logMessage(LOGWARN, "Warning in predatorpreyprinter - preys not defined on all areas");
221 :     }
222 :    
223 :     minage = 9999;
224 :     maxage = -1;
225 :     for (i = 0; i < ages.Nrow(); i++) {
226 :     for (j = 0; j < ages.Ncol(i); j++) {
227 :     minage = min(ages[i][j], minage);
228 :     maxage = max(ages[i][j], maxage);
229 :     }
230 :     }
231 :    
232 :     found = 0;
233 :     for (i = 0; i < preys.Size(); i++)
234 :     if (minage >= ((StockPrey*)preys[i])->minAge())
235 :     found++;
236 :     if (found == 0)
237 :     handle.logMessage(LOGWARN, "Warning in predatorpreyprinter - minimum age less than prey age");
238 :    
239 :     found = 0;
240 :     for (i = 0; i < preys.Size(); i++)
241 :     if (maxage <= ((StockPrey*)preys[i])->maxAge())
242 :     found++;
243 :     if (found == 0)
244 :     handle.logMessage(LOGWARN, "Warning in predatorpreyprinter - maximum age greater than prey age");
245 :    
246 :     found = 0;
247 :     for (i = 0; i < preys.Size(); i++)
248 :     if (LgrpDiv->maxLength(0) > preys[i]->getLengthGroupDiv()->minLength())
249 :     found++;
250 :     if (found == 0)
251 :     handle.logMessage(LOGWARN, "Warning in predatorpreyprinter - minimum length group less than prey length");
252 :    
253 :     found = 0;
254 :     for (i = 0; i < preys.Size(); i++)
255 :     if (LgrpDiv->minLength(LgrpDiv->numLengthGroups()) < preys[i]->getLengthGroupDiv()->maxLength())
256 :     found++;
257 :     if (found == 0)
258 :     handle.logMessage(LOGWARN, "Warning in predatorpreyprinter - maximum length group greater than prey length");
259 :     }
260 :    
261 :     aggregator = new PredatorPreyAggregator(predators, preys, LgrpDiv, areas, ages);
262 :     }
263 :    
264 :     void PredatorPreyPrinter::Print(const TimeClass* const TimeInfo, int printtime) {
265 :    
266 :     if ((!AAT.atCurrentTime(TimeInfo)) || (printtime != printtimeid))
267 :     return;
268 :    
269 :     int a, age, len;
270 :     aggregator->Sum(TimeInfo);
271 :     alptr = &aggregator->getConsumption();
272 :     for (a = 0; a < areas.Nrow(); a++) {
273 :     dptr = aggregator->getMortality()[a];
274 :     for (age = (*alptr)[a].minAge(); age <= (*alptr)[a].maxAge(); age++) {
275 :     for (len = (*alptr)[a].minLength(age); len < (*alptr)[a].maxLength(age); len++) {
276 :     outfile << setw(lowwidth) << TimeInfo->getYear() << sep
277 :     << setw(lowwidth) << TimeInfo->getStep() << sep
278 :     << setw(printwidth) << areaindex[a] << sep
279 :     << setw(printwidth) << ageindex[age] << sep
280 :     << setw(printwidth) << lenindex[len] << sep;
281 :    
282 :     //JMB crude filter to remove the 'silly' values from the output
283 :     if (((*alptr)[a][age][len].N < rathersmall) || ((*alptr)[a][age][len].W < 0.0))
284 :     outfile << setw(width) << 0 << sep << setw(width) << 0 << sep << setw(width) << 0 << endl;
285 :     else
286 :     outfile << setprecision(precision) << setw(width) << (*alptr)[a][age][len].N
287 :     << sep << setprecision(precision) << setw(width)
288 :     << (*alptr)[a][age][len].N * (*alptr)[a][age][len].W
289 :     << sep << setprecision(precision) << setw(width)
290 :     << (*dptr)[age][len] << endl;
291 :     }
292 :     }
293 :     }
294 :     outfile.flush();
295 :     }
296 :    
297 :     PredatorPreyPrinter::~PredatorPreyPrinter() {
298 :     outfile.close();
299 :     outfile.clear();
300 :     delete LgrpDiv;
301 :     delete aggregator;
302 :     int i;
303 :     for (i = 0; i < predatornames.Size(); i++)
304 :     delete[] predatornames[i];
305 :     for (i = 0; i < preynames.Size(); i++)
306 :     delete[] preynames[i];
307 :     for (i = 0; i < lenindex.Size(); i++)
308 :     delete[] lenindex[i];
309 :     for (i = 0; i < ageindex.Size(); i++)
310 :     delete[] ageindex[i];
311 :     for (i = 0; i < areaindex.Size(); i++)
312 :     delete[] areaindex[i];
313 :     }

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

Powered By FusionForge