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

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

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

revision 4615 by caltinay, Mon Jan 13 05:05:33 2014 UTC revision 4616 by caltinay, Tue Jan 14 22:57:47 2014 UTC
# Line 27  using namespace boost::python; Line 27  using namespace boost::python;
27    
28  namespace ripley {  namespace ripley {
29    
30    template<typename T>
31    std::vector<T> extractPyArray(const object& obj, const std::string& name,
32                                  int expectedLength=0)
33    {
34        std::vector<T> result;
35        if (extract<tuple>(obj).check() || extract<list>(obj).check()) {
36            if (expectedLength==0 || len(obj)==expectedLength) {
37                for (int i=0; i<len(obj); i++) {
38                    result.push_back(extract<T>(obj[i]));
39                }
40            } else {
41                std::stringstream ssError;
42                ssError << "argument '" << name << "' has wrong length";
43                std::string error(ssError.str());
44                throw RipleyException(error.c_str());
45            }
46        } else {
47            std::stringstream ssError;
48            ssError << "argument '" << name << "' must be a tuple or list";
49            std::string error(ssError.str());
50            throw RipleyException(error.c_str());
51        }
52    
53        return result;
54    }
55    
56  escript::Data readBinaryGrid(std::string filename, escript::FunctionSpace fs,  escript::Data readBinaryGrid(std::string filename, escript::FunctionSpace fs,
57            const object& pyShape, double fill, int byteOrder, int dataType,
58          const object& pyFirst, const object& pyNum, const object& pyMultiplier,          const object& pyFirst, const object& pyNum, const object& pyMultiplier,
59          const object& pyShape, double fill, int byteOrder, int dataType)          const object& pyReverse)
60  {  {
61      int dim=fs.getDim();      int dim=fs.getDim();
62      GridParameters params;      GridParameters params;
     std::vector<int> shape;  
   
     if (extract<tuple>(pyFirst).check() || extract<list>(pyFirst).check()) {  
         if (len(pyFirst)==dim) {  
             for (int i=0; i<dim; i++) {  
                 params.first.push_back(extract<int>(pyFirst[i]));  
             }  
         } else  
             throw RipleyException("Argument 'first' has wrong length");  
     } else  
         throw RipleyException("Argument 'first' must be a tuple or list");  
   
     if (extract<tuple>(pyNum).check() || extract<list>(pyNum).check()) {  
         if (len(pyNum)==dim) {  
             for (int i=0; i<dim; i++) {  
                 params.numValues.push_back(extract<int>(pyNum[i]));  
             }  
         } else  
             throw RipleyException("Argument 'numValues' has wrong length");  
     } else  
         throw RipleyException("Argument 'numValues' must be a tuple or list");  
   
     if (extract<tuple>(pyShape).check() || extract<list>(pyShape).check()) {  
         for (int i=0; i<len(pyShape); i++) {  
             shape.push_back(extract<int>(pyShape[i]));  
         }  
     } else  
         throw RipleyException("Argument 'shape' must be a tuple or list");  
63    
64      if (extract<tuple>(pyMultiplier).check() || extract<list>(pyMultiplier).check()) {      params.first = extractPyArray<int>(pyFirst, "first", dim);
65          if (len(pyMultiplier)==dim) {      params.numValues = extractPyArray<int>(pyNum, "numValues", dim);
66              for (int i=0; i<dim; i++) {      params.multiplier = extractPyArray<int>(pyMultiplier, "multiplier", dim);
67                  params.multiplier.push_back(extract<int>(pyMultiplier[i]));      params.reverse = extractPyArray<int>(pyReverse, "reverse", dim);
68              }      params.byteOrder = byteOrder;
69          } else      params.dataType = dataType;
70              throw RipleyException("Argument 'multiplier' has wrong length");      std::vector<int> shape(extractPyArray<int>(pyShape, "shape"));
     } else  
         throw RipleyException("Argument 'multiplier' must be a tuple or list");  
71    
72      const RipleyDomain* dom=dynamic_cast<const RipleyDomain*>(fs.getDomain().get());      const RipleyDomain* dom=dynamic_cast<const RipleyDomain*>(fs.getDomain().get());
73      if (!dom)      if (!dom)
74          throw RipleyException("Function space must be on a ripley domain");          throw RipleyException("Function space must be on a ripley domain");
75    
   
76      escript::Data res(fill, shape, fs, true);      escript::Data res(fill, shape, fs, true);
   
77      dom->readBinaryGrid(res, filename, params);      dom->readBinaryGrid(res, filename, params);
78      return res;      return res;
79  }  }
80    
81  escript::Data readNcGrid(std::string filename, std::string varname,  escript::Data readNcGrid(std::string filename, std::string varname,
82          escript::FunctionSpace fs, const object& pyFirst, const object& pyNum,          escript::FunctionSpace fs, const object& pyShape, double fill,
83          const object& pyMultiplier, const object& pyShape, double fill=0.)          const object& pyFirst, const object& pyNum, const object& pyMultiplier,
84            const object& pyReverse)
85  {  {
86      int dim=fs.getDim();      int dim=fs.getDim();
87      GridParameters params;      GridParameters params;
     std::vector<int> shape;  
   
     if (extract<tuple>(pyFirst).check() || extract<list>(pyFirst).check()) {  
         if (len(pyFirst)==dim) {  
             for (int i=0; i<dim; i++) {  
                 params.first.push_back(extract<int>(pyFirst[i]));  
             }  
         } else  
             throw RipleyException("Argument 'first' has wrong length");  
     } else  
         throw RipleyException("Argument 'first' must be a tuple or list");  
88    
89      if (extract<tuple>(pyNum).check() || extract<list>(pyNum).check()) {      params.first = extractPyArray<int>(pyFirst, "first", dim);
90          if (len(pyNum)==dim) {      params.numValues = extractPyArray<int>(pyNum, "numValues", dim);
91              for (int i=0; i<dim; i++) {      params.multiplier = extractPyArray<int>(pyMultiplier, "multiplier", dim);
92                  params.numValues.push_back(extract<int>(pyNum[i]));      params.reverse = extractPyArray<int>(pyReverse, "reverse", dim);
93              }      std::vector<int> shape(extractPyArray<int>(pyShape, "shape"));
         } else  
             throw RipleyException("Argument 'numValues' has wrong length");  
     } else  
         throw RipleyException("Argument 'numValues' must be a tuple or list");  
   
     if (extract<tuple>(pyMultiplier).check() || extract<list>(pyMultiplier).check()) {  
         if (len(pyMultiplier)==dim) {  
             for (int i=0; i<dim; i++) {  
                 params.multiplier.push_back(extract<int>(pyMultiplier[i]));  
             }  
         } else  
             throw RipleyException("Argument 'multiplier' has wrong length");  
     } else  
         throw RipleyException("Argument 'multiplier' must be a tuple or list");  
   
     if (extract<tuple>(pyShape).check() || extract<list>(pyShape).check()) {  
         for (int i=0; i<len(pyShape); i++) {  
             shape.push_back(extract<int>(pyShape[i]));  
         }  
     } else  
         throw RipleyException("Argument 'shape' must be a tuple or list");  
94    
95      const RipleyDomain* dom=dynamic_cast<const RipleyDomain*>(fs.getDomain().get());      const RipleyDomain* dom=dynamic_cast<const RipleyDomain*>(fs.getDomain().get());
96      if (!dom)      if (!dom)
97          throw RipleyException("Function space must be on a ripley domain");          throw RipleyException("Function space must be on a ripley domain");
98    
   
99      escript::Data res(fill, shape, fs, true);      escript::Data res(fill, shape, fs, true);
   
100      dom->readNcGrid(res, filename, varname, params);      dom->readNcGrid(res, filename, varname, params);
101      return res;      return res;
102  }  }
# Line 265  BOOST_PYTHON_MODULE(ripleycpp) Line 227  BOOST_PYTHON_MODULE(ripleycpp)
227  ":param d1: number of subdivisions in direction 1\n:type d1: ``int``");  ":param d1: number of subdivisions in direction 1\n:type d1: ``int``");
228      def("_theculprit_", ripley::_who);      def("_theculprit_", ripley::_who);
229    
230      def("_readBinaryGrid", &ripley::readBinaryGrid, (arg("filename"), arg("functionspace"), arg("first"), arg("numValues"), arg("multiplier"), arg("shape"), arg("fill")=0., arg("byteOrder"), arg("dataType")));      def("_readBinaryGrid", &ripley::readBinaryGrid, (arg("filename"),
231                    arg("functionspace"), arg("shape"), arg("fill")=0.,
232      def("_readNcGrid", &ripley::readNcGrid, (arg("filename"), arg("varname"), arg("functionspace"), arg("first"), arg("numValues"), arg("multiplier"), arg("shape"), arg("fill")=0.));                  arg("byteOrder"), arg("dataType"), arg("first"),
233                    arg("numValues"), arg("multiplier"), arg("reverse")),
234    "Reads a binary Grid");
235    
236        def("_readNcGrid", &ripley::readNcGrid, (arg("filename"), arg("varname"),
237                    arg("functionspace"), arg("shape"), arg("fill"), arg("first"),
238                    arg("numValues"), arg("multiplier"), arg("reverse")),
239    "Reads a grid from a netCDF file");
240    
241      class_<ripley::RipleyDomain, bases<escript::AbstractContinuousDomain>, boost::noncopyable >      class_<ripley::RipleyDomain, bases<escript::AbstractContinuousDomain>, boost::noncopyable >
242          ("RipleyDomain", "", no_init)          ("RipleyDomain", "", no_init)

Legend:
Removed from v.4615  
changed lines
  Added in v.4616

  ViewVC Help
Powered by ViewVC 1.1.26