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