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

Annotation of /trunk/gadget/predatoroverprinter.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

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

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

Powered By FusionForge