1 : |
agomez |
1 |
#include "preyoverprinter.h" |
2 : |
|
|
#include "conversionindex.h" |
3 : |
|
|
#include "preyoveraggregator.h" |
4 : |
|
|
#include "areatime.h" |
5 : |
|
|
#include "readfunc.h" |
6 : |
|
|
#include "readword.h" |
7 : |
|
|
#include "readaggregation.h" |
8 : |
|
|
#include "errorhandler.h" |
9 : |
|
|
#include "prey.h" |
10 : |
|
|
#include "gadget.h" |
11 : |
|
|
#include "runid.h" |
12 : |
|
|
#include "global.h" |
13 : |
|
|
|
14 : |
|
|
PreyOverPrinter::PreyOverPrinter(CommentStream& infile, const TimeClass* const TimeInfo)
|
15 : |
|
|
: Printer(PREYOVERPRINTER), preyLgrpDiv(0), aggregator(0), dptr(0) {
|
16 : |
|
|
|
17 : |
|
|
char text[MaxStrLength];
|
18 : |
|
|
strncpy(text, "", MaxStrLength);
|
19 : |
|
|
int i, j;
|
20 : |
|
|
|
21 : |
|
|
//read in the prey names
|
22 : |
|
|
i = 0;
|
23 : |
|
|
infile >> text >> ws;
|
24 : |
|
|
if ((strcasecmp(text, "preys") != 0) && (strcasecmp(text, "preynames") != 0))
|
25 : |
|
|
handle.logFileUnexpected(LOGFAIL, "preynames", text);
|
26 : |
|
|
infile >> text >> ws;
|
27 : |
|
|
while (!infile.eof() && (strcasecmp(text, "areaaggfile") != 0)) {
|
28 : |
|
|
preynames.resize(new char[strlen(text) + 1]);
|
29 : |
|
|
strcpy(preynames[i++], text);
|
30 : |
|
|
infile >> text >> ws;
|
31 : |
|
|
}
|
32 : |
|
|
if (preynames.Size() == 0)
|
33 : |
|
|
handle.logFileMessage(LOGFAIL, "\nError in preyoverprinter - failed to read preys");
|
34 : |
|
|
handle.logMessage(LOGMESSAGE, "Read prey data - number of preys", preynames.Size());
|
35 : |
|
|
|
36 : |
|
|
//read in area aggregation from file
|
37 : |
|
|
filename = new char[MaxStrLength];
|
38 : |
|
|
strncpy(filename, "", MaxStrLength);
|
39 : |
|
|
ifstream datafile;
|
40 : |
|
|
CommentStream subdata(datafile);
|
41 : |
|
|
|
42 : |
|
|
infile >> filename >> ws;
|
43 : |
|
|
datafile.open(filename, ios::in);
|
44 : |
|
|
handle.checkIfFailure(datafile, filename);
|
45 : |
|
|
handle.Open(filename);
|
46 : |
|
|
i = readAggregation(subdata, areas, areaindex);
|
47 : |
|
|
handle.Close();
|
48 : |
|
|
datafile.close();
|
49 : |
|
|
datafile.clear();
|
50 : |
|
|
|
51 : |
|
|
//read in length aggregation from file
|
52 : |
|
|
DoubleVector lengths;
|
53 : |
|
|
readWordAndValue(infile, "lenaggfile", filename);
|
54 : |
|
|
datafile.open(filename, ios::in);
|
55 : |
|
|
handle.checkIfFailure(datafile, filename);
|
56 : |
|
|
handle.Open(filename);
|
57 : |
|
|
i = readLengthAggregation(subdata, lengths, lenindex);
|
58 : |
|
|
handle.Close();
|
59 : |
|
|
datafile.close();
|
60 : |
|
|
datafile.clear();
|
61 : |
|
|
|
62 : |
|
|
//Finished reading from infile.
|
63 : |
|
|
preyLgrpDiv = new LengthGroupDivision(lengths);
|
64 : |
|
|
if (preyLgrpDiv->Error())
|
65 : |
|
|
handle.logFileMessage(LOGFAIL, "\nError in preyoverprinter - failed to create length group");
|
66 : |
|
|
|
67 : |
|
|
//Open the printfile
|
68 : |
|
|
readWordAndValue(infile, "printfile", filename);
|
69 : |
|
|
outfile.open(filename, ios::out);
|
70 : |
|
|
handle.checkIfFailure(outfile, filename);
|
71 : |
|
|
|
72 : |
|
|
infile >> text >> ws;
|
73 : |
|
|
if (strcasecmp(text, "precision") == 0) {
|
74 : |
|
|
infile >> precision >> ws >> text >> ws;
|
75 : |
|
|
width = precision + 4;
|
76 : |
|
|
} else {
|
77 : |
|
|
// use default values
|
78 : |
|
|
precision = largeprecision;
|
79 : |
|
|
width = largewidth;
|
80 : |
|
|
}
|
81 : |
|
|
|
82 : |
|
|
if (precision < 0)
|
83 : |
|
|
handle.logFileMessage(LOGFAIL, "\nError in preyoverprinter - invalid value of precision");
|
84 : |
|
|
|
85 : |
|
|
if (strcasecmp(text, "printatstart") == 0)
|
86 : |
|
|
infile >> printtimeid >> ws >> text >> ws;
|
87 : |
|
|
else
|
88 : |
|
|
printtimeid = 0;
|
89 : |
|
|
|
90 : |
|
|
if (printtimeid != 0 && printtimeid != 1)
|
91 : |
|
|
handle.logFileMessage(LOGFAIL, "\nError in preyoverprinter - invalid value of printatstart");
|
92 : |
|
|
|
93 : |
|
|
if (strcasecmp(text, "yearsandsteps") != 0)
|
94 : |
|
|
handle.logFileUnexpected(LOGFAIL, "yearsandsteps", text);
|
95 : |
|
|
if (!AAT.readFromFile(infile, TimeInfo))
|
96 : |
|
|
handle.logFileMessage(LOGFAIL, "\nError in preyoverprinter - wrong format for yearsandsteps");
|
97 : |
|
|
|
98 : |
|
|
//prepare for next printfile component
|
99 : |
|
|
infile >> ws;
|
100 : |
|
|
if (!infile.eof()) {
|
101 : |
|
|
infile >> text >> ws;
|
102 : |
|
|
if (strcasecmp(text, "[component]") != 0)
|
103 : |
|
|
handle.logFileUnexpected(LOGFAIL, "[component]", text);
|
104 : |
|
|
}
|
105 : |
|
|
|
106 : |
|
|
//finished initializing. Now print first lines
|
107 : |
|
|
outfile << "; ";
|
108 : |
|
|
RUNID.Print(outfile);
|
109 : |
|
|
outfile << "; Prey overconsumption output file for the following preys";
|
110 : |
|
|
for (i = 0; i < preynames.Size(); i++)
|
111 : |
|
|
outfile << sep << preynames[i];
|
112 : |
|
|
|
113 : |
|
|
if (printtimeid == 0)
|
114 : |
|
|
outfile << "\n; Printing the following information at the end of each timestep";
|
115 : |
|
|
else
|
116 : |
|
|
outfile << "\n; Printing the following information at the start of each timestep";
|
117 : |
|
|
|
118 : |
|
|
outfile << "\n; year-step-area-length-overconsumption biomass\n";
|
119 : |
|
|
outfile.flush();
|
120 : |
|
|
}
|
121 : |
|
|
|
122 : |
|
|
void PreyOverPrinter::setPrey(PreyPtrVector& preyvec, const AreaClass* const Area) {
|
123 : |
|
|
PreyPtrVector preys;
|
124 : |
|
|
delete aggregator;
|
125 : |
|
|
int i, j, k, found;
|
126 : |
|
|
|
127 : |
|
|
for (i = 0; i < preyvec.Size(); i++)
|
128 : |
|
|
for (j = 0; j < preynames.Size(); j++)
|
129 : |
|
|
if (strcasecmp(preyvec[i]->getName(), preynames[j]) == 0)
|
130 : |
|
|
preys.resize(preyvec[i]);
|
131 : |
|
|
|
132 : |
|
|
if (preys.Size() != preynames.Size()) {
|
133 : |
|
|
handle.logMessage(LOGWARN, "Error in preyoverprinter - failed to match preys");
|
134 : |
|
|
for (i = 0; i < preyvec.Size(); i++)
|
135 : |
|
|
handle.logMessage(LOGWARN, "Error in preyoverprinter - found prey", preyvec[i]->getName());
|
136 : |
|
|
for (i = 0; i < preynames.Size(); i++)
|
137 : |
|
|
handle.logMessage(LOGWARN, "Error in preyoverprinter - looking for prey", preynames[i]);
|
138 : |
|
|
handle.logMessage(LOGFAIL, ""); //JMB this will exit gadget
|
139 : |
|
|
}
|
140 : |
|
|
|
141 : |
|
|
for (i = 0; i < preys.Size(); i++)
|
142 : |
|
|
for (j = 0; j < preys.Size(); j++)
|
143 : |
|
|
if ((strcasecmp(preys[i]->getName(), preys[j]->getName()) == 0) && (i != j))
|
144 : |
|
|
handle.logMessage(LOGFAIL, "Error in preyoverprinter - repeated prey", preys[i]->getName());
|
145 : |
|
|
|
146 : |
|
|
//change from outer areas to inner areas.
|
147 : |
|
|
for (i = 0; i < areas.Nrow(); i++)
|
148 : |
|
|
for (j = 0; j < areas.Ncol(i); j++)
|
149 : |
|
|
areas[i][j] = Area->getInnerArea(areas[i][j]);
|
150 : |
|
|
|
151 : |
|
|
//check prey areas and lengths
|
152 : |
|
|
if (handle.getLogLevel() >= LOGWARN) {
|
153 : |
|
|
for (j = 0; j < areas.Nrow(); j++) {
|
154 : |
|
|
found = 0;
|
155 : |
|
|
for (i = 0; i < preys.Size(); i++)
|
156 : |
|
|
for (k = 0; k < areas.Ncol(j); k++)
|
157 : |
|
|
if (preys[i]->isInArea(areas[j][k]))
|
158 : |
|
|
found++;
|
159 : |
|
|
if (found == 0)
|
160 : |
|
|
handle.logMessage(LOGWARN, "Warning in preyoverprinter - prey not defined on all areas");
|
161 : |
|
|
}
|
162 : |
|
|
|
163 : |
|
|
found = 0;
|
164 : |
|
|
for (i = 0; i < preys.Size(); i++)
|
165 : |
|
|
if (preyLgrpDiv->maxLength(0) > preys[i]->getLengthGroupDiv()->minLength())
|
166 : |
|
|
found++;
|
167 : |
|
|
if (found == 0)
|
168 : |
|
|
handle.logMessage(LOGWARN, "Warning in preyoverprinter - minimum length group less than prey length");
|
169 : |
|
|
|
170 : |
|
|
found = 0;
|
171 : |
|
|
for (i = 0; i < preys.Size(); i++)
|
172 : |
|
|
if (preyLgrpDiv->minLength(preyLgrpDiv->numLengthGroups()) < preys[i]->getLengthGroupDiv()->maxLength())
|
173 : |
|
|
found++;
|
174 : |
|
|
if (found == 0)
|
175 : |
|
|
handle.logMessage(LOGWARN, "Warning in preyoverprinter - maximum length group greater than prey length");
|
176 : |
|
|
}
|
177 : |
|
|
|
178 : |
|
|
aggregator = new PreyOverAggregator(preys, areas, preyLgrpDiv);
|
179 : |
|
|
}
|
180 : |
|
|
|
181 : |
|
|
void PreyOverPrinter::Print(const TimeClass* const TimeInfo, int printtime) {
|
182 : |
|
|
|
183 : |
|
|
if ((!AAT.atCurrentTime(TimeInfo)) || (printtime != printtimeid))
|
184 : |
|
|
return;
|
185 : |
|
|
|
186 : |
|
|
aggregator->Sum();
|
187 : |
|
|
dptr = &aggregator->getSum();
|
188 : |
|
|
int a, len, p, w;
|
189 : |
|
|
|
190 : |
|
|
for (a = 0; a < areas.Nrow(); a++) {
|
191 : |
|
|
for (len = 0; len < dptr->Ncol(a); len++) {
|
192 : |
|
|
outfile << setw(lowwidth) << TimeInfo->getYear() << sep
|
193 : |
|
|
<< setw(lowwidth) << TimeInfo->getStep() << sep
|
194 : |
|
|
<< setw(printwidth) << areaindex[a] << sep
|
195 : |
|
|
<< setw(printwidth) << lenindex[len] << sep;
|
196 : |
|
|
|
197 : |
|
|
//JMB crude filter to remove the 'silly' values from the output
|
198 : |
|
|
if ((*dptr)[a][len] < rathersmall)
|
199 : |
|
|
outfile << setw(width) << 0 << endl;
|
200 : |
|
|
else
|
201 : |
|
|
outfile << setprecision(precision) << setw(width) << (*dptr)[a][len] << endl;
|
202 : |
|
|
}
|
203 : |
|
|
}
|
204 : |
|
|
outfile.flush();
|
205 : |
|
|
}
|
206 : |
|
|
|
207 : |
|
|
PreyOverPrinter::~PreyOverPrinter() {
|
208 : |
|
|
outfile.close();
|
209 : |
|
|
outfile.clear();
|
210 : |
|
|
delete preyLgrpDiv;
|
211 : |
|
|
delete aggregator;
|
212 : |
|
|
int i;
|
213 : |
|
|
for (i = 0; i < preynames.Size(); i++)
|
214 : |
|
|
delete[] preynames[i];
|
215 : |
|
|
for (i = 0; i < areaindex.Size(); i++)
|
216 : |
|
|
delete[] areaindex[i];
|
217 : |
|
|
for (i = 0; i < lenindex.Size(); i++)
|
218 : |
|
|
delete[] lenindex[i];
|
219 : |
|
|
}
|