/[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 4353 by caltinay, Fri Apr 5 00:14:35 2013 UTC revision 4357 by caltinay, Thu Apr 11 06:07:14 2013 UTC
# Line 351  void Rectangle::readBinaryGrid(escript:: Line 351  void Rectangle::readBinaryGrid(escript::
351      f.close();      f.close();
352  }  }
353    
354  void Rectangle::writeBinaryGrid(const escript::Data& in, string filename, int byteOrder) const  void Rectangle::writeBinaryGrid(const escript::Data& in, string filename,
355                                    int byteOrder, int dataType) const
356    {
357        // the mapping is not universally correct but should work on our
358        // supported platforms
359        switch (dataType) {
360            case DATATYPE_INT32:
361                writeBinaryGridImpl<int>(in, filename, byteOrder);
362                break;
363            case DATATYPE_FLOAT32:
364                writeBinaryGridImpl<float>(in, filename, byteOrder);
365                break;
366            case DATATYPE_FLOAT64:
367                writeBinaryGridImpl<double>(in, filename, byteOrder);
368                break;
369            default:
370                throw RipleyException("writeBinaryGrid(): invalid or unsupported datatype");
371        }
372    }
373    
374    template<typename ValueType>
375    void Rectangle::writeBinaryGridImpl(const escript::Data& in,
376                                        const string& filename, int byteOrder) const
377  {  {
378      // check function space and determine number of points      // check function space and determine number of points
379      int myN0, myN1;      int myN0, myN1;
# Line 372  void Rectangle::writeBinaryGrid(const es Line 394  void Rectangle::writeBinaryGrid(const es
394    
395      const int numComp = in.getDataPointSize();      const int numComp = in.getDataPointSize();
396      const int dpp = in.getNumDataPointsPerSample();      const int dpp = in.getNumDataPointsPerSample();
     const int fileSize = sizeof(float)*numComp*dpp*totalN0*totalN1;  
397    
398      if (numComp > 1 || dpp > 1)      if (numComp > 1 || dpp > 1)
399          throw RipleyException("writeBinaryGrid(): only scalar, single-value data supported");          throw RipleyException("writeBinaryGrid(): only scalar, single-value data supported");
400    
401      escript::Data* _in = const_cast<escript::Data*>(&in);      escript::Data* _in = const_cast<escript::Data*>(&in);
402        const int fileSize = sizeof(ValueType)*numComp*dpp*totalN0*totalN1;
403    
404      // from here on we know that each sample consists of one value      // from here on we know that each sample consists of one value
405      FileWriter* fw = new FileWriter();      FileWriter* fw = new FileWriter();
# Line 385  void Rectangle::writeBinaryGrid(const es Line 407  void Rectangle::writeBinaryGrid(const es
407      MPIBarrier();      MPIBarrier();
408    
409      for (index_t y=0; y<myN1; y++) {      for (index_t y=0; y<myN1; y++) {
410          const int fileofs = (m_offset[0]+(m_offset[1]+y)*totalN0)*sizeof(float);          const int fileofs = (m_offset[0]+(m_offset[1]+y)*totalN0)*sizeof(ValueType);
411          ostringstream oss;          ostringstream oss;
412    
413          for (index_t x=0; x<myN0; x++) {          for (index_t x=0; x<myN0; x++) {
414              const double* sample = _in->getSampleDataRO(y*myN0+x);              const double* sample = _in->getSampleDataRO(y*myN0+x);
415              float fvalue = (float)(*sample);              ValueType fvalue = static_cast<ValueType>(*sample);
416              if (byteOrder == RIPLEY_BYTE_ORDER) {              if (byteOrder == BYTEORDER_NATIVE) {
417                  oss.write((char*)&fvalue, sizeof(fvalue));                  oss.write((char*)&fvalue, sizeof(fvalue));
418              } else {              } else {
419                  char* value = reinterpret_cast<char*>(&fvalue);                  char* value = reinterpret_cast<char*>(&fvalue);
420                  oss.write(RIPLEY_BYTE_SWAP32(value), sizeof(fvalue));                  oss.write(byte_swap32(value), sizeof(fvalue));
421              }              }
422          }          }
423          fw->writeAt(oss, fileofs);          fw->writeAt(oss, fileofs);

Legend:
Removed from v.4353  
changed lines
  Added in v.4357

  ViewVC Help
Powered by ViewVC 1.1.26