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

Annotation of /trunk/gadget/stockfullprinter.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : agomez 1 #include "stockaggregator.h"
2 :     #include "stockfullprinter.h"
3 :     #include "errorhandler.h"
4 :     #include "stock.h"
5 :     #include "conversionindex.h"
6 :     #include "stockptrvector.h"
7 :     #include "readword.h"
8 :     #include "readaggregation.h"
9 :     #include "gadget.h"
10 :     #include "runid.h"
11 :     #include "global.h"
12 :    
13 :     StockFullPrinter::StockFullPrinter(CommentStream& infile, const TimeClass* const TimeInfo)
14 :     : Printer(STOCKFULLPRINTER), stockname(0), aggregator(0), LgrpDiv(0), alptr(0) {
15 :    
16 :     char text[MaxStrLength];
17 :     strncpy(text, "", MaxStrLength);
18 :    
19 :     stockname = new char[MaxStrLength];
20 :     strncpy(stockname, "", MaxStrLength);
21 :     readWordAndValue(infile, "stockname", stockname);
22 :    
23 :     //JMB - removed the need to read in the area aggregation file
24 :     infile >> text >> ws;
25 :     if (strcasecmp(text, "areaaggfile") == 0) {
26 :     infile >> text >> ws;
27 :     handle.logMessage(LOGWARN, "Warning in stockfullprinter - area aggregation file ignored");
28 :     infile >> text >> ws;
29 :     }
30 :    
31 :     //open the printfile
32 :     filename = new char[MaxStrLength];
33 :     strncpy(filename, "", MaxStrLength);
34 :     //readWordAndValue(infile, "printfile", filename);
35 :     if (strcasecmp(text, "printfile") != 0)
36 :     handle.logFileUnexpected(LOGFAIL, "printfile", text);
37 :     infile >> filename >> ws;
38 :    
39 :     outfile.open(filename, ios::out);
40 :     handle.checkIfFailure(outfile, filename);
41 :    
42 :     infile >> text >> ws;
43 :     if (strcasecmp(text, "precision") == 0) {
44 :     infile >> precision >> ws >> text >> ws;
45 :     width = precision + 4;
46 :     } else {
47 :     // use default values
48 :     precision = largeprecision;
49 :     width = largewidth;
50 :     }
51 :    
52 :     if (precision < 0)
53 :     handle.logFileMessage(LOGFAIL, "\nError in stockfullprinter - invalid value of precision");
54 :    
55 :     if (strcasecmp(text, "printatstart") == 0)
56 :     infile >> printtimeid >> ws >> text >> ws;
57 :     else
58 :     printtimeid = 0;
59 :    
60 :     if (printtimeid != 0 && printtimeid != 1)
61 :     handle.logFileMessage(LOGFAIL, "\nError in stockfullprinter - invalid value of printatstart");
62 :    
63 :     if (strcasecmp(text, "yearsandsteps") != 0)
64 :     handle.logFileUnexpected(LOGFAIL, "yearsandsteps", text);
65 :     if (!AAT.readFromFile(infile, TimeInfo))
66 :     handle.logFileMessage(LOGFAIL, "\nError in stockfullprinter - wrong format for yearsandsteps");
67 :    
68 :     //prepare for next printfile component
69 :     infile >> ws;
70 :     if (!infile.eof()) {
71 :     infile >> text >> ws;
72 :     if (strcasecmp(text, "[component]") != 0)
73 :     handle.logFileUnexpected(LOGFAIL, "[component]", text);
74 :     }
75 :    
76 :     //finished initializing. Now print first lines
77 :     outfile << "; ";
78 :     RUNID.Print(outfile);
79 :     outfile << "; Full output file for the stock " << stockname;
80 :    
81 :     if (printtimeid == 0)
82 :     outfile << "\n; Printing the following information at the end of each timestep";
83 :     else
84 :     outfile << "\n; Printing the following information at the start of each timestep";
85 :    
86 :     outfile << "\n; year-step-area-age-length-number-mean weight\n";
87 :     outfile.flush();
88 :     }
89 :    
90 :     StockFullPrinter::~StockFullPrinter() {
91 :     outfile.close();
92 :     outfile.clear();
93 :     delete aggregator;
94 :     delete LgrpDiv;
95 :     delete[] stockname;
96 :     }
97 :    
98 :     void StockFullPrinter::setStock(StockPtrVector& stockvec, const AreaClass* const Area) {
99 :     StockPtrVector stocks;
100 :     int i, maxage;
101 :    
102 :     for (i = 0; i < stockvec.Size(); i++)
103 :     if (strcasecmp(stockvec[i]->getName(), stockname) == 0)
104 :     stocks.resize(stockvec[i]);
105 :    
106 :     if (stocks.Size() != 1) {
107 :     handle.logMessage(LOGWARN, "Error in stockfullprinter - failed to match stocks");
108 :     for (i = 0; i < stocks.Size(); i++)
109 :     handle.logMessage(LOGWARN, "Error in stockfullprinter - found stock", stocks[i]->getName());
110 :     handle.logMessage(LOGFAIL, "Error in stockfullprinter - looking for stock", stockname);
111 :     }
112 :    
113 :     IntVector areas = stocks[0]->getAreas();
114 :     outerareas.resize(areas.Size(), 0);
115 :     for (i = 0; i < outerareas.Size(); i++)
116 :     outerareas[i] = Area->getModelArea(areas[i]);
117 :    
118 :     //prepare for the creation of the aggregator
119 :     minage = stocks[0]->minAge();
120 :     maxage = stocks[0]->maxAge();
121 :     IntMatrix agematrix(maxage - minage + 1, 1, 0);
122 :     for (i = 0; i < agematrix.Nrow(); i++)
123 :     agematrix[i][0] = i + minage;
124 :     IntMatrix areamatrix(areas.Size(), 1, 0);
125 :     for (i = 0; i < areamatrix.Nrow(); i++)
126 :     areamatrix[i][0] = areas[i];
127 :    
128 :     LgrpDiv = new LengthGroupDivision(*stocks[0]->getLengthGroupDiv());
129 :     if (LgrpDiv->Error())
130 :     handle.logMessage(LOGFAIL, "Error in stockfullprinter - failed to create length group");
131 :     aggregator = new StockAggregator(stocks, LgrpDiv, areamatrix, agematrix);
132 :     }
133 :    
134 :     void StockFullPrinter::Print(const TimeClass* const TimeInfo, int printtime) {
135 :    
136 :     if ((!AAT.atCurrentTime(TimeInfo)) || (printtime != printtimeid))
137 :     return;
138 :    
139 :     aggregator->Sum();
140 :     int a, age, len;
141 :    
142 :     alptr = &aggregator->getSum();
143 :     for (a = 0; a < outerareas.Size(); a++) {
144 :     for (age = (*alptr)[a].minAge(); age <= (*alptr)[a].maxAge(); age++) {
145 :     for (len = (*alptr)[a].minLength(age); len < (*alptr)[a].maxLength(age); len++) {
146 :     outfile << setw(lowwidth) << TimeInfo->getYear() << sep
147 :     << setw(lowwidth) << TimeInfo->getStep() << sep
148 :     << setw(lowwidth) << outerareas[a] << sep << setw(lowwidth)
149 :     << age + minage << sep << setw(lowwidth)
150 :     << LgrpDiv->meanLength(len) << sep;
151 :    
152 :     //JMB crude filter to remove the 'silly' values from the output
153 :     if (((*alptr)[a][age][len].N < rathersmall) || ((*alptr)[a][age][len].W < 0.0))
154 :     outfile << setw(width) << 0 << sep << setw(width) << 0 << endl;
155 :     else
156 :     outfile << setprecision(precision) << setw(width) << (*alptr)[a][age][len].N << sep
157 :     << setprecision(precision) << setw(width) << (*alptr)[a][age][len].W << endl;
158 :    
159 :     }
160 :     }
161 :     }
162 :     outfile.flush();
163 :     }

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

Powered By FusionForge