1 : |
agomez |
1 |
#include "stockvariable.h" |
2 : |
|
|
#include "errorhandler.h" |
3 : |
|
|
#include "ecosystem.h" |
4 : |
|
|
#include "readword.h" |
5 : |
|
|
#include "gadget.h" |
6 : |
|
|
#include "global.h" |
7 : |
|
|
|
8 : |
|
|
extern Ecosystem* EcoSystem;
|
9 : |
|
|
|
10 : |
|
|
|
11 : |
|
|
void StockVariable::read(CommentStream& infile) {
|
12 : |
|
|
|
13 : |
|
|
int i;
|
14 : |
|
|
char text[MaxStrLength];
|
15 : |
|
|
strncpy(text, "", MaxStrLength);
|
16 : |
|
|
|
17 : |
|
|
infile >> ws;
|
18 : |
|
|
char c = infile.peek();
|
19 : |
|
|
biomass = 1;
|
20 : |
|
|
if ((c == 'b') || (c == 'B'))
|
21 : |
|
|
readWordAndVariable(infile, "biomass", biomass);
|
22 : |
|
|
if (biomass != 0 && biomass != 1)
|
23 : |
|
|
handle.logFileMessage(LOGFAIL, "\nError in stockvariable - biomass must be 0 or 1");
|
24 : |
|
|
|
25 : |
|
|
i = 0;
|
26 : |
|
|
infile >> ws;
|
27 : |
|
|
while (!infile.eof()) {
|
28 : |
|
|
infile >> text >> ws;
|
29 : |
|
|
stocknames.resize(new char[strlen(text) + 1]);
|
30 : |
|
|
strcpy(stocknames[i++], text);
|
31 : |
|
|
}
|
32 : |
|
|
if (stocknames.Size() == 0)
|
33 : |
|
|
handle.logFileMessage(LOGFAIL, "\nError in stockvariable - failed to read stocks");
|
34 : |
|
|
|
35 : |
|
|
handle.logMessage(LOGMESSAGE, "Read stockvariable data - number of stocks", stocknames.Size());
|
36 : |
|
|
}
|
37 : |
|
|
|
38 : |
|
|
StockVariable::~StockVariable() {
|
39 : |
|
|
int i;
|
40 : |
|
|
for (i = 0; i < stocknames.Size(); i++)
|
41 : |
|
|
delete[] stocknames[i];
|
42 : |
|
|
}
|
43 : |
|
|
|
44 : |
|
|
void StockVariable::Update() {
|
45 : |
|
|
int i,j;
|
46 : |
|
|
|
47 : |
|
|
//JMB need to find the stocks - only need to do this once
|
48 : |
|
|
if (stocks.Size() == 0) {
|
49 : |
|
|
|
50 : |
|
|
StockPtrVector stockvec;
|
51 : |
|
|
stockvec = EcoSystem->getModelStockVector();
|
52 : |
|
|
|
53 : |
|
|
for (i = 0; i < stockvec.Size(); i++)
|
54 : |
|
|
for (j = 0; j < stocknames.Size(); j++)
|
55 : |
|
|
if (strcasecmp(stockvec[i]->getName(), stocknames[j]) == 0)
|
56 : |
|
|
stocks.resize(stockvec[i]);
|
57 : |
|
|
|
58 : |
|
|
if (stocks.Size() != stocknames.Size()) {
|
59 : |
|
|
handle.logMessage(LOGWARN, "Error in stockvariable - failed to match stocks");
|
60 : |
|
|
for (i = 0; i < stocks.Size(); i++)
|
61 : |
|
|
handle.logMessage(LOGWARN, "Error in stockvariable - found stock", stocks[i]->getName());
|
62 : |
|
|
for (i = 0; i < stocknames.Size(); i++)
|
63 : |
|
|
handle.logMessage(LOGWARN, "Error in stockvariable - looking for stock", stocknames[i]);
|
64 : |
|
|
handle.logMessage(LOGFAIL, ""); //JMB this will exit gadget
|
65 : |
|
|
}
|
66 : |
|
|
}
|
67 : |
|
|
|
68 : |
|
|
value = 0.0;
|
69 : |
|
|
for (i = 0; i < stocks.Size(); i++) {
|
70 : |
|
|
if (biomass == 1)
|
71 : |
|
|
value += stocks[i]->getTotalStockBiomassAllAreas();
|
72 : |
|
|
else
|
73 : |
|
|
value += stocks[i]->getTotalStockNumberAllAreas();
|
74 : |
|
|
}
|
75 : |
|
|
}
|
76 : |
|
|
|
77 : |
|
|
void StockVariable::Delete() const {
|
78 : |
|
|
int i;
|
79 : |
|
|
for (i = 0; i < stocknames.Size(); i++)
|
80 : |
|
|
delete[] stocknames[i];
|
81 : |
|
|
}
|
82 : |
|
|
|
83 : |
|
|
void StockVariable::Interchange(StockVariable& newSV) const {
|
84 : |
|
|
newSV.value = value;
|
85 : |
|
|
newSV.biomass = biomass;
|
86 : |
|
|
newSV.stocks = stocks;
|
87 : |
|
|
newSV.stocknames = stocknames;
|
88 : |
|
|
}
|