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

Annotation of /trunk/gadget/stockprinter.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

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

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

Powered By FusionForge