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

Annotation of /trunk/gadget/stockstdprinter.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : agomez 1 #include "areatime.h"
2 :     #include "errorhandler.h"
3 :     #include "stockstdprinter.h"
4 :     #include "stockaggregator.h"
5 :     #include "stockpreyaggregator.h"
6 :     #include "charptrvector.h"
7 :     #include "stockptrvector.h"
8 :     #include "preyptrvector.h"
9 :     #include "stock.h"
10 :     #include "stockprey.h"
11 :     #include "conversionindex.h"
12 :     #include "readword.h"
13 :     #include "gadget.h"
14 :     #include "runid.h"
15 :     #include "global.h"
16 :    
17 :     StockStdPrinter::StockStdPrinter(CommentStream& infile, const TimeClass* const TimeInfo)
18 :     : Printer(STOCKSTDPRINTER), stockname(0), LgrpDiv(0), saggregator(0), paggregator(0), salptr(0), palptr(0) {
19 :    
20 :     char text[MaxStrLength];
21 :     strncpy(text, "", MaxStrLength);
22 :    
23 :     filename = new char[MaxStrLength];
24 :     strncpy(filename, "", MaxStrLength);
25 :    
26 :     isaprey = 0;
27 :     stockname = new char[MaxStrLength];
28 :     strncpy(stockname, "", MaxStrLength);
29 :     readWordAndValue(infile, "stockname", stockname);
30 :    
31 :     infile >> text >> ws;
32 :     if (strcasecmp(text, "scale") == 0) {
33 :     infile >> scale >> ws >> text >> ws;
34 :     if (scale < rathersmall) {
35 :     handle.logFileMessage(LOGWARN, "scale should be a positive integer - set to default value 1");
36 :     scale = 1.0;
37 :     }
38 :    
39 :     } else
40 :     scale = 1.0;
41 :    
42 :     //JMB - removed the need to read in the area aggregation file
43 :     if (strcasecmp(text, "areaaggfile") == 0) {
44 :     infile >> text >> ws;
45 :     handle.logMessage(LOGWARN, "Warning in stockstdprinter - area aggregation file ignored");
46 :     infile >> text >> ws;
47 :     }
48 :    
49 :     //open the printfile
50 :     if (strcasecmp(text, "printfile") != 0)
51 :     handle.logFileUnexpected(LOGFAIL, "printfile", text);
52 :     infile >> filename >> ws;
53 :    
54 :     outfile.open(filename, ios::out);
55 :     handle.checkIfFailure(outfile, filename);
56 :    
57 :     infile >> text >> ws;
58 :     if (strcasecmp(text, "precision") == 0) {
59 :     infile >> precision >> ws >> text >> ws;
60 :     width = precision + 4;
61 :     } else {
62 :     // use default values
63 :     precision = largeprecision;
64 :     width = largewidth;
65 :     }
66 :    
67 :     if (precision < 0)
68 :     handle.logFileMessage(LOGFAIL, "\nError in stockstdprinter - invalid value of precision");
69 :    
70 :     if (strcasecmp(text, "printatstart") == 0)
71 :     infile >> printtimeid >> ws >> text >> ws;
72 :     else
73 :     printtimeid = 0;
74 :    
75 :     if (printtimeid != 0 && printtimeid != 1)
76 :     handle.logFileMessage(LOGFAIL, "\nError in stockstdprinter - invalid value of printatstart");
77 :    
78 :     if (strcasecmp(text, "yearsandsteps") != 0)
79 :     handle.logFileUnexpected(LOGFAIL, "yearsandsteps", text);
80 :     if (!AAT.readFromFile(infile, TimeInfo))
81 :     handle.logFileMessage(LOGFAIL, "\nError in stockstdprinter - wrong format for yearsandsteps");
82 :    
83 :     //prepare for next printfile component
84 :     infile >> ws;
85 :     if (!infile.eof()) {
86 :     infile >> text >> ws;
87 :     if (strcasecmp(text, "[component]") != 0)
88 :     handle.logFileUnexpected(LOGFAIL, "[component]", text);
89 :     }
90 :    
91 :     //finished initializing. Now print first lines
92 :     outfile << "; ";
93 :     RUNID.Print(outfile);
94 :     outfile << "; Standard output file for the stock " << stockname;
95 :    
96 :     if (scale != 1.0) {
97 :     outfile << "\n; Scaling factor for the number and number consumed is " << scale;
98 :     //JMB - store this as 1/scale
99 :     scale = 1.0 / scale;
100 :     }
101 :    
102 :     if (printtimeid == 0)
103 :     outfile << "\n; Printing the following information at the end of each timestep";
104 :     else
105 :     outfile << "\n; Printing the following information at the start of each timestep";
106 :    
107 :     outfile << "\n; year-step-area-age-number-mean length-mean weight-"
108 :     << "stddev length-number consumed-biomass consumed\n";
109 :     outfile.flush();
110 :     }
111 :    
112 :     StockStdPrinter::~StockStdPrinter() {
113 :     outfile.close();
114 :     outfile.clear();
115 :     if (isaprey)
116 :     delete paggregator;
117 :     delete saggregator;
118 :     delete LgrpDiv;
119 :     delete[] stockname;
120 :     }
121 :    
122 :     void StockStdPrinter::setStock(StockPtrVector& stockvec, const AreaClass* const Area) {
123 :     StockPtrVector stocks;
124 :     int i, maxage;
125 :    
126 :     for (i = 0; i < stockvec.Size(); i++)
127 :     if (strcasecmp(stockvec[i]->getName(), stockname) == 0)
128 :     stocks.resize(stockvec[i]);
129 :    
130 :     if (stocks.Size() != 1) {
131 :     handle.logMessage(LOGWARN, "Error in stockstdprinter - failed to match stocks");
132 :     for (i = 0; i < stocks.Size(); i++)
133 :     handle.logMessage(LOGWARN, "Error in stockstdprinter - found stock", stocks[i]->getName());
134 :     handle.logMessage(LOGFAIL, "Error in stockstdprinter - looking for stock", stockname);
135 :     }
136 :    
137 :     IntVector areas = stocks[0]->getAreas();
138 :     outerareas.resize(areas.Size(), 0);
139 :     for (i = 0; i < outerareas.Size(); i++)
140 :     outerareas[i] = Area->getModelArea(areas[i]);
141 :    
142 :     //prepare for the creation of the aggregator
143 :     minage = stocks[0]->minAge();
144 :     maxage = stocks[0]->maxAge();
145 :     IntMatrix agematrix(maxage - minage + 1, 1, 0);
146 :     for (i = 0; i < agematrix.Nrow(); i++)
147 :     agematrix[i][0] = i + minage;
148 :     IntMatrix areamatrix(areas.Size(), 1, 0);
149 :     for (i = 0; i < areamatrix.Nrow(); i++)
150 :     areamatrix[i][0] = areas[i];
151 :    
152 :     LgrpDiv = new LengthGroupDivision(*stocks[0]->getLengthGroupDiv());
153 :     if (LgrpDiv->Error())
154 :     handle.logMessage(LOGFAIL, "Error in stockstdprinter - failed to create length group");
155 :     saggregator = new StockAggregator(stocks, LgrpDiv, areamatrix, agematrix);
156 :    
157 :     if (stocks[0]->isEaten()) {
158 :     isaprey = 1;
159 :     PreyPtrVector preys;
160 :     preys.resize(stocks[0]->getPrey());
161 :     //need to construct length group based on the min/max lengths of the stock
162 :     double minl, maxl;
163 :     minl = stocks[0]->getLengthGroupDiv()->minLength();
164 :     maxl = stocks[0]->getLengthGroupDiv()->maxLength();
165 :     LengthGroupDivision* tmpLgrpDiv = new LengthGroupDivision(minl, maxl, maxl - minl);
166 :     if (LgrpDiv->Error())
167 :     handle.logMessage(LOGFAIL, "Error in stockstdprinter - failed to create length group");
168 :    
169 :     paggregator = new StockPreyAggregator(preys, tmpLgrpDiv, areamatrix, agematrix);
170 :     //JMB tmpLgrpDiv is no longer needed so delete it to free memory
171 :     delete tmpLgrpDiv;
172 :     }
173 :     }
174 :    
175 :     void StockStdPrinter::Print(const TimeClass* const TimeInfo, int printtime) {
176 :    
177 :     if ((!AAT.atCurrentTime(TimeInfo)) || (printtime != printtimeid))
178 :     return;
179 :    
180 :     saggregator->Sum();
181 :     salptr = &saggregator->getSum();
182 :     if (isaprey) {
183 :     paggregator->Sum();
184 :     palptr = &paggregator->getSum();
185 :     }
186 :    
187 :     int a, age;
188 :     for (a = 0; a < outerareas.Size(); a++) {
189 :     for (age = (*salptr)[a].minAge(); age <= (*salptr)[a].maxAge(); age++) {
190 :     outfile << setw(lowwidth) << TimeInfo->getYear() << sep
191 :     << setw(lowwidth) << TimeInfo->getStep() << sep
192 :     << setw(lowwidth) << outerareas[a] << sep << setw(lowwidth)
193 :     << age + minage << sep;
194 :    
195 :     ps.calcStatistics((*salptr)[a][age], LgrpDiv);
196 :     //JMB crude filters to remove the 'silly' values from the output
197 :     if (ps.totalNumber() < rathersmall) {
198 :     outfile << setw(width) << 0 << sep << setw(printwidth) << 0
199 :     << sep << setw(printwidth) << 0 << sep << setw(printwidth) << 0
200 :     << sep << setw(width) << 0 << sep << setw(width) << 0 << endl;
201 :    
202 :     } else {
203 :     outfile << setprecision(precision) << setw(width) << ps.totalNumber() * scale << sep
204 :     << setprecision(printprecision) << setw(printwidth) << ps.meanLength() << sep
205 :     << setprecision(printprecision) << setw(printwidth) << ps.meanWeight() << sep
206 :     << setprecision(printprecision) << setw(printwidth) << ps.sdevLength() << sep;
207 :    
208 :     if (isaprey) {
209 :     //JMB crude filter to remove the 'silly' values from the output
210 :     if (((*palptr)[a][age][0].N < rathersmall) || ((*palptr)[a][age][0].W < 0.0))
211 :     outfile << setw(width) << 0 << sep << setw(width) << 0 << endl;
212 :     else
213 :     outfile << setprecision(precision) << setw(width) << (*palptr)[a][age][0].N
214 :     << sep << setprecision(precision) << setw(width)
215 :     << (*palptr)[a][age][0].W * (*palptr)[a][age][0].N << endl;
216 :    
217 :     } else
218 :     outfile << setw(width) << 0 << sep << setw(width) << 0 << endl;
219 :    
220 :     }
221 :     }
222 :     }
223 :     outfile.flush();
224 :     }

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

Powered By FusionForge