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

Annotation of /trunk/gadget/stockpreyfullprinter.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

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

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

Powered By FusionForge