PyObject * array2numpy(const ArrayBase & a) { initNumpy(); npy_intp dims[1]; dims[0] = npy_intp(a.getCount()); NTA_BasicType t = a.getType(); int dtype; switch (t) { case NTA_BasicType_Byte: dtype = NPY_INT8; break; case NTA_BasicType_Int16: dtype = NPY_INT16; break; case NTA_BasicType_UInt16: dtype = NPY_UINT16; break; case NTA_BasicType_Int32: dtype = NPY_INT32; break; case NTA_BasicType_UInt32: dtype = NPY_UINT32; break; case NTA_BasicType_Int64: dtype = NPY_INT64; break; case NTA_BasicType_UInt64: dtype = NPY_UINT64; break; case NTA_BasicType_Real32: dtype = NPY_FLOAT32; break; case NTA_BasicType_Real64: dtype = NPY_FLOAT64; break; case NTA_BasicType_Bool: dtype = NPY_BOOL; break; default: NTA_THROW << "Unknown basic type: " << t; }; return (PyObject *)PyArray_SimpleNewFromData(1, dims, dtype, a.getBuffer()); }
ulong read_surface( const std::string& fname, ulong nx, ulong ny, smart_ptr< h5_pool_iface > pool, const std::string& surf_name = "", const bool invert_z = false ) { // set C locale for proper numbers reading setlocale(LC_NUMERIC, "C"); // open file std::ifstream f(fname.c_str(), std::ios::in); if(!f) { BSERR << "Error opening file " << fname << bs_end; return 0; } std::string linebuf; std::istringstream line_s; //std::size_t pos; //t_float point[3]; //ulong row, col; // setup buffer with dimensions passed ulong dims[3] = {nx, ny}; spv_float databuf = BS_KERNEL.create_object(v_float::bs_type()); databuf->resize(dims[0] * dims[1] * 3); std::fill(databuf->begin(), databuf->end(), 0.); // actually read file ulong n_points = 0; while(std::getline(f, linebuf)) { line_s.clear(); // parse some info from comments if(linebuf[0] == '#') { if(linebuf.find("Information from grid:") != std::string::npos) { // EarthVision grid format n_points = read_earth_vision_grid(f, dims, databuf); break; } continue; } // skip ! comments if(linebuf[0] == '!') continue; if(linebuf.substr(0, 6) == "FSASCI") { // CPS-3 grid format n_points = read_cps3_grid(f, dims, databuf); break; } if(linebuf[0] == '@') { if(linebuf.find("grid") != std::string::npos) { // Zmap+grid format n_points = read_zmap_grid(f, dims, databuf); break; } } // try to read as Irap classic grid // TODO: impleent Irap reading } // restore locale setlocale(LC_NUMERIC, ""); // check if we read nothing if(!n_points) { BSERR << "No valid data is read from " << fname << bs_end; return n_points; } else { BSOUT << "read_surface: succefully read " << n_points << " points from " << fname << bs_end; } // find out surface name std::string surf_name_ = surf_name; if(!surf_name.size()) { surf_name_ = fname.substr(0, fname.rfind('.')); const std::size_t slashpos = std::min(surf_name_.rfind('/'), surf_name_.rfind('\\')); if(slashpos != std::string::npos) surf_name_ = surf_name_.substr(slashpos + 1); } // write array to pool if(invert_z) { for(ulong i = 0; i < databuf->size() / 3; ++i) { databuf->ss(i*3 + 2) = -databuf->ss(i*3 + 2); } } //npy_intp h5p_dims[] = { 0, npy_intp(dims[0]), 0, npy_intp(dims[1]), 0, 1 }; npy_intp h5p_dims[] = { npy_intp(dims[0]), npy_intp(dims[1]), 3 }; //pool->declare_fp_data(surf_name_, 0, 3, &h5p_dims[0], 1); pool->declare_fp_data(surf_name_, 0, 3, &h5p_dims[0], 0); pool->set_fp_data(surf_name_, databuf); return n_points; }