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

Annotation of /trunk/gadget/readfunc.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : agomez 1 #include "readfunc.h"
2 :     #include "errorhandler.h"
3 :     #include "gadget.h"
4 :     #include "global.h"
5 :    
6 :     void readRefWeights(CommentStream& infile, DoubleMatrix& M) {
7 :     //Check the number of columns in the inputfile
8 :     if (countColumns(infile) != 2)
9 :     handle.logFileMessage(LOGFAIL, "wrong number of columns in inputfile - should be 2");
10 :    
11 :     int i = 0;
12 :     M.Reset();
13 :     infile >> ws;
14 :     while (!infile.eof()) {
15 :     //crude check to see if something has gone wrong and avoid infinite loops
16 :     if (!(isdigit(infile.peek())))
17 :     handle.logFileMessage(LOGFAIL, "failed to read data from file");
18 :    
19 :     M.AddRows(1, 2, 0.0);
20 :     infile >> M[i][0] >> M[i][1];
21 :     if (infile.fail())
22 :     handle.logFileMessage(LOGFAIL, "failed to read reference weights");
23 :     infile >> ws;
24 :     i++;
25 :     }
26 :    
27 :     //Check the data to make sure that it is continuous and the weights are positive
28 :     if (M[0][0] < 0.0)
29 :     handle.logFileMessage(LOGFAIL, "lengths for reference weights must be positive");
30 :     for (i = 1; i < M.Nrow(); i++)
31 :     if ((M[i][0] - M[i - 1][0]) < verysmall)
32 :     handle.logFileMessage(LOGFAIL, "lengths for reference weights must be strictly increasing");
33 :     for (i = 1; i < M.Nrow(); i++)
34 :     if (M[i][1] < verysmall)
35 :     handle.logFileMessage(LOGFAIL, "weights for reference weights must be positive");
36 :    
37 :     handle.logMessage(LOGMESSAGE, "Read reference weights OK - number of entries", M.Nrow());
38 :     }
39 :    
40 :     int countColumns(CommentStream& infile) {
41 :     if (infile.fail())
42 :     return 0;
43 :    
44 :     char line[MaxStrLength];
45 :     char temp[MaxStrLength];
46 :     strncpy(line, "", MaxStrLength);
47 :     strncpy(temp, "", MaxStrLength);
48 :     streampos pos = infile.tellg();
49 :    
50 :     infile >> ws;
51 :     infile.getLine(line, MaxStrLength);
52 :     if (infile.fail())
53 :     return 0;
54 :    
55 :     istringstream istr(line);
56 :     istr >> ws;
57 :    
58 :     char c;
59 :     int i = 0;
60 :     int p = 0;
61 :     while (!istr.eof()) {
62 :     // with the new formula syntax a column can contain whitespace,
63 :     // so this code was changed to keep track of opening and closing
64 :     // parens when counting columns. [mnaa]
65 :     c = (char)istr.peek(); //JMB keep GCC 4.3 happy
66 :     if (c == '(') {
67 :     istr.get(c);
68 :     p++;
69 :     while (p > 0) {
70 :     if (istr.eof())
71 :     return 0;
72 :     istr.get(c);
73 :     if (c == '(')
74 :     p++;
75 :     if (c == ')')
76 :     p--;
77 :     }
78 :     istr >> ws;
79 :     i++;
80 :     } else {
81 :     istr >> temp;
82 :     if (istr.fail() && !istr.eof())
83 :     return 0;
84 :     istr >> ws;
85 :     i++;
86 :     }
87 :     }
88 :    
89 :     infile.seekg(pos);
90 :     return i;
91 :     }
92 :    
93 :     void readAmounts(CommentStream& infile, const IntVector& tmpareas,
94 :     const TimeClass* const TimeInfo, const AreaClass* const Area,
95 :     FormulaMatrix& amount, const char* givenname) {
96 :    
97 :     char c;
98 :     char tmpname[MaxStrLength];
99 :     strncpy(tmpname, "", MaxStrLength);
100 :     int i, year, step, area;
101 :     int keepdata, timeid, areaid, tmpareaid, count, reject;
102 :    
103 :     //Check the number of columns in the inputfile
104 :     infile >> ws;
105 :     if (countColumns(infile) != 5)
106 :     handle.logFileMessage(LOGFAIL, "wrong number of columns in inputfile - should be 5");
107 :    
108 :     //Create storage for the data - size of the data is known
109 :     amount.AddRows(TimeInfo->numTotalSteps() + 1, tmpareas.Size(), 0.0);
110 :    
111 :     year = step = area = count = reject = 0;
112 :     while (!infile.eof()) {
113 :     keepdata = 1;
114 :     infile >> year >> step >> area >> tmpname;
115 :    
116 :     //crude check to see if something has gone wrong and avoid infinite loops
117 :     if (strlen(tmpname) == 0)
118 :     handle.logFileMessage(LOGFAIL, "failed to read data for", givenname);
119 :    
120 :     //check if the year and step are in the simulation
121 :     timeid = -1;
122 :     if (TimeInfo->isWithinPeriod(year, step))
123 :     timeid = TimeInfo->calcSteps(year, step);
124 :     else
125 :     keepdata = 0;
126 :    
127 :     //only keep the data if tmpname matches givenname
128 :     if (strcasecmp(givenname, tmpname) != 0)
129 :     keepdata = 0;
130 :    
131 :     //only keep the data if area is a required area
132 :     areaid = -1;
133 :     tmpareaid = Area->getInnerArea(area);
134 :     for (i = 0; i < tmpareas.Size(); i++)
135 :     if (tmpareas[i] == tmpareaid)
136 :     areaid = i;
137 :    
138 :     if (areaid == -1)
139 :     keepdata = 0;
140 :    
141 :     if (keepdata == 1) {
142 :     //data is required, so store it
143 :     count++;
144 :     infile >> amount[timeid][areaid] >> ws;
145 :    
146 :     } else { //data not required - skip rest of line
147 :     reject++;
148 :     infile.get(c);
149 :     while (c != '\n' && !infile.eof())
150 :     infile.get(c);
151 :     infile >> ws;
152 :     }
153 :     }
154 :    
155 :     if (count == 0)
156 :     handle.logMessage(LOGWARN, "Warning in readamounts - found no data in the data file for", givenname);
157 :     if (reject != 0)
158 :     handle.logMessage(LOGMESSAGE, "Discarded invalid amounts data - number of invalid entries", reject);
159 :     handle.logMessage(LOGMESSAGE, "Read amounts data file - number of entries", count);
160 :     }
161 :    
162 :     void readGrowthAmounts(CommentStream& infile, const TimeClass* const TimeInfo,
163 :     const AreaClass* const Area, FormulaMatrixPtrVector& amount,
164 :     const CharPtrVector& lenindex, const IntVector& tmpareas) {
165 :    
166 :     int i, year, step, area;
167 :     char c;
168 :     char tmplength[MaxStrLength];
169 :     strncpy(tmplength, "", MaxStrLength);
170 :     int keepdata, timeid, areaid, lenid, tmpareaid, count, reject;
171 :    
172 :     //Check the number of columns in the inputfile
173 :     infile >> ws;
174 :     if (countColumns(infile) != 5)
175 :     handle.logFileMessage(LOGFAIL, "wrong number of columns in inputfile - should be 5");
176 :    
177 :     year = step = area = count = reject = 0;
178 :     while (!infile.eof()) {
179 :     keepdata = 1;
180 :     infile >> year >> step >> area >> tmplength;
181 :    
182 :     //crude check to see if something has gone wrong and avoid infinite loops
183 :     if (strlen(tmplength) == 0)
184 :     handle.logFileMessage(LOGFAIL, "failed to read growth data");
185 :    
186 :     //check if the year and step are in the simulation
187 :     timeid = -1;
188 :     if (TimeInfo->isWithinPeriod(year, step))
189 :     timeid = TimeInfo->calcSteps(year, step);
190 :     else
191 :     keepdata = 0;
192 :    
193 :     //if tmplength is in lenindex find lengthid, else dont keep the data
194 :     lenid = -1;
195 :     for (i = 0; i < lenindex.Size(); i++)
196 :     if (strcasecmp(lenindex[i], tmplength) == 0)
197 :     lenid = i;
198 :    
199 :     if (lenid == -1)
200 :     keepdata = 0;
201 :    
202 :     //only keep the data if area is a required area
203 :     areaid = -1;
204 :     tmpareaid = Area->getInnerArea(area);
205 :     for (i = 0; i < tmpareas.Size(); i++)
206 :     if (tmpareas[i] == tmpareaid)
207 :     areaid = i;
208 :    
209 :     if (areaid == -1)
210 :     keepdata = 0;
211 :    
212 :     if (keepdata == 1) {
213 :     count++;
214 :     infile >> (*amount[areaid])[timeid][lenid] >> ws;
215 :    
216 :     } else { //data not required - skip rest of line
217 :     reject++;
218 :     infile.get(c);
219 :     while (c != '\n' && !infile.eof())
220 :     infile.get(c);
221 :     infile >> ws;
222 :     }
223 :     }
224 :    
225 :     if (count == 0)
226 :     handle.logMessage(LOGWARN, "Warning in growthamounts - found no data in the data file");
227 :     if (reject != 0)
228 :     handle.logMessage(LOGMESSAGE, "Discarded invalid growth data - number of invalid entries", reject);
229 :     handle.logMessage(LOGMESSAGE, "Read growth data file - number of entries", count);
230 :     }

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

Powered By FusionForge