/[escript]/trunk/ripley/src/Rectangle.cpp
ViewVC logotype

Diff of /trunk/ripley/src/Rectangle.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3943 by caltinay, Thu Aug 9 04:43:24 2012 UTC revision 3971 by caltinay, Wed Sep 19 02:55:35 2012 UTC
# Line 157  bool Rectangle::operator==(const Abstrac Line 157  bool Rectangle::operator==(const Abstrac
157      return false;      return false;
158  }  }
159    
160    void Rectangle::readBinaryGrid(escript::Data& out, string filename,
161                const vector<int>& first, const vector<int>& numValues) const
162    {
163        // check destination function space
164        int myN0, myN1;
165        if (out.getFunctionSpace().getTypeCode() == Nodes) {
166            myN0 = m_N0;
167            myN1 = m_N1;
168        } else if (out.getFunctionSpace().getTypeCode() == Elements ||
169                    out.getFunctionSpace().getTypeCode() == ReducedElements) {
170            myN0 = m_NE0;
171            myN1 = m_NE1;
172        } else
173            throw RipleyException("readBinaryGrid(): invalid function space for output data object");
174    
175        // check file existence and size
176        ifstream f(filename.c_str(), ifstream::binary);
177        if (f.fail()) {
178            throw RipleyException("readBinaryGrid(): cannot open file");
179        }
180        f.seekg(0, ios::end);
181        const int numComp = out.getDataPointSize();
182        const int filesize = f.tellg();
183        const int reqsize = numValues[0]*numValues[1]*numComp*sizeof(float);
184        if (filesize < reqsize) {
185            f.close();
186            throw RipleyException("readBinaryGrid(): not enough data in file");
187        }
188    
189        // check if this rank contributes anything
190        if (first[0] >= m_offset0+myN0 || first[0]+numValues[0] <= m_offset0 ||
191                first[1] >= m_offset1+myN1 || first[1]+numValues[1] <= m_offset1) {
192            f.close();
193            return;
194        }
195    
196        // now determine how much this rank has to write
197    
198        // first coordinates in data object to write to
199        const int first0 = max(0, first[0]-m_offset0);
200        const int first1 = max(0, first[1]-m_offset1);
201        // indices to first value in file
202        const int idx0 = max(0, m_offset0-first[0]);
203        const int idx1 = max(0, m_offset1-first[1]);
204        // number of values to write
205        const int num0 = min(numValues[0]-idx0, myN0-first0);
206        const int num1 = min(numValues[1]-idx1, myN1-first1);
207    
208        out.requireWrite();
209        vector<float> values(num0*numComp);
210        const int dpp = out.getNumDataPointsPerSample();
211    
212        for (index_t y=0; y<num1; y++) {
213            const int fileofs = numComp*(idx0+(idx1+y)*numValues[0]);
214            f.seekg(fileofs*sizeof(float));
215            f.read((char*)&values[0], num0*numComp*sizeof(float));
216            for (index_t x=0; x<num0; x++) {
217                double* dest = out.getSampleDataRW(first0+x+(first1+y)*myN0);
218                for (index_t c=0; c<numComp; c++) {
219                    for (index_t q=0; q<dpp; q++) {
220                        *dest++ = static_cast<double>(values[x*numComp+c]);
221                    }
222                }
223            }
224        }
225    
226        f.close();
227    }
228    
229  void Rectangle::dump(const string& fileName) const  void Rectangle::dump(const string& fileName) const
230  {  {
231  #if USE_SILO  #if USE_SILO

Legend:
Removed from v.3943  
changed lines
  Added in v.3971

  ViewVC Help
Powered by ViewVC 1.1.26