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

Annotation of /trunk/gadget/predatorprinter.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

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

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

Powered By FusionForge