moab
NCHelper.hpp
Go to the documentation of this file.
00001 //-------------------------------------------------------------------------
00002 // Filename      : NCHelper.hpp
00003 //
00004 // Purpose       : Climate NC file helper
00005 //
00006 // Creator       : Danqing Wu
00007 //-------------------------------------------------------------------------
00008 
00009 #ifndef NCHELPER_HPP
00010 #define NCHELPER_HPP
00011 
00012 #include "ReadNC.hpp"
00013 
00014 namespace moab {
00015 
00017 class NCHelper
00018 {
00019 public:
00020   NCHelper(ReadNC* readNC, int fileId, const FileOptions& opts, EntityHandle fileSet)
00021 :_readNC(readNC), _fileId(fileId), _opts(opts), _fileSet(fileSet),
00022   nTimeSteps(0), nLevels(1), tDim(-1), levDim(-1) {}
00023   virtual ~NCHelper() {}
00024 
00026   static NCHelper* get_nc_helper(ReadNC* readNC, int fileId, const FileOptions& opts, EntityHandle fileSet);
00027 
00029   virtual ErrorCode init_mesh_vals() = 0;
00030   virtual ErrorCode check_existing_mesh() = 0;
00031   virtual ErrorCode create_mesh(Range& faces) = 0;
00032   virtual ErrorCode read_variables(std::vector<std::string>& var_names, std::vector<int>& tstep_nums) = 0;
00033   virtual std::string get_mesh_type_name() = 0;
00034 
00036   ErrorCode create_conventional_tags(const std::vector<int>& tstep_nums);
00037 
00038 protected:
00040   ErrorCode read_variable_setup(std::vector<std::string>& var_names,
00041                                             std::vector<int>& tstep_nums,
00042                                             std::vector<ReadNC::VarData>& vdatas,
00043                                             std::vector<ReadNC::VarData>& vsetdatas);
00044 
00046   ErrorCode read_variable_to_set(std::vector<ReadNC::VarData>& vdatas, std::vector<int>& tstep_nums);
00047 
00049   ErrorCode convert_variable(ReadNC::VarData& var_data, int tstep_num);
00050 
00051   ErrorCode read_coordinate(const char* var_name, int lmin, int lmax,
00052                             std::vector<double>& cvals);
00053 
00054   ErrorCode get_tag_to_set(ReadNC::VarData& var_data, int tstep_num, Tag& tagh);
00055 
00056   ErrorCode get_tag_to_nonset(ReadNC::VarData& var_data, int tstep_num, Tag& tagh, int num_lev);
00057 
00063   ErrorCode create_attrib_string(const std::map<std::string, ReadNC::AttData>& attMap,
00064                                  std::string& attString,
00065                                  std::vector<int>& attLen);
00066 
00069   ErrorCode create_dummy_variables();
00070 
00071 private:
00073   ErrorCode read_variable_to_set_allocate(std::vector<ReadNC::VarData>& vdatas, std::vector<int>& tstep_nums);
00074 
00075 protected:
00077   ReadNC* _readNC;
00078 
00080   int _fileId;
00081   const FileOptions& _opts;
00082   EntityHandle _fileSet;
00083 
00085   int nTimeSteps, nLevels;
00086 
00088   std::vector<double> tVals, levVals;
00089 
00091   int tDim, levDim;
00092 
00094   std::set<std::string> ignoredVarNames;
00095 
00097   std::set<std::string> dummyVarNames;
00098 };
00099 
00101 class ScdNCHelper : public NCHelper
00102 {
00103 public:
00104   ScdNCHelper(ReadNC* readNC, int fileId, const FileOptions& opts, EntityHandle fileSet)
00105 : NCHelper(readNC, fileId, opts, fileSet),
00106   iDim(-1), jDim(-1), iCDim(-1), jCDim(-1)
00107   {
00108     for (unsigned int i = 0; i < 6; i++) {
00109       gDims[i] = -1;
00110       lDims[i] = -1;
00111       gCDims[i] = -1;
00112       lCDims[i] = -1;
00113     }
00114 
00115     locallyPeriodic[0] = locallyPeriodic[1] = locallyPeriodic[2] = 0;
00116     globallyPeriodic[0] = globallyPeriodic[1] = globallyPeriodic[2] = 0;
00117   }
00118   virtual ~ScdNCHelper() {}
00119 
00120 private:
00122   virtual ErrorCode check_existing_mesh();
00124   virtual ErrorCode create_mesh(Range& faces);
00126   virtual ErrorCode read_variables(std::vector<std::string>& var_names, std::vector<int>& tstep_nums);
00127 
00129   ErrorCode read_scd_variable_to_nonset_allocate(std::vector<ReadNC::VarData>& vdatas,
00130                                                  std::vector<int>& tstep_nums);
00131   ErrorCode read_scd_variable_to_nonset(std::vector<ReadNC::VarData>& vdatas,
00132                                         std::vector<int>& tstep_nums);
00133 
00135   ErrorCode create_quad_coordinate_tag();
00136 
00137   template <typename T> ErrorCode kji_to_jik(size_t ni, size_t nj, size_t nk, void* dest, T* source)
00138   {
00139     size_t nik = ni * nk, nij = ni * nj;
00140     T* tmp_data = reinterpret_cast<T*>(dest);
00141     for (std::size_t j = 0; j != nj; j++)
00142       for (std::size_t i = 0; i != ni; i++)
00143         for (std::size_t k = 0; k != nk; k++)
00144           tmp_data[j*nik + i*nk + k] = source[k*nij + j*ni + i];
00145     return MB_SUCCESS;
00146   }
00147 
00148 protected:
00150   int gDims[6];
00151 
00153   int lDims[6];
00154 
00156   int gCDims[6];
00157 
00159   int lCDims[6];
00160 
00162   std::vector<double> ilVals, jlVals;
00163 
00165   std::vector<double> ilCVals, jlCVals;
00166 
00168   int iDim, jDim;
00169 
00171   int iCDim, jCDim;
00172 
00174   int locallyPeriodic[3];
00175 
00177   int globallyPeriodic[3];
00178 };
00179 
00181 class UcdNCHelper : public NCHelper
00182 {
00183 public:
00184   UcdNCHelper(ReadNC* readNC, int fileId, const FileOptions& opts, EntityHandle fileSet)
00185 : NCHelper(readNC, fileId, opts, fileSet),
00186   nCells(0), nEdges(0), nVertices(0),
00187   nLocalCells(0), nLocalEdges(0), nLocalVertices(0),
00188   cDim(-1), eDim(-1), vDim(-1) {}
00189   virtual ~UcdNCHelper() {}
00190 
00191 private:
00193   virtual ErrorCode read_variables(std::vector<std::string>& var_names,
00194                                    std::vector<int> &tstep_nums);
00195 
00197   virtual ErrorCode read_ucd_variable_to_nonset_allocate(std::vector<ReadNC::VarData>& vdatas,
00198                                                          std::vector<int>& tstep_nums) = 0;
00199 #ifdef PNETCDF_FILE
00200   virtual ErrorCode read_ucd_variable_to_nonset_async(std::vector<ReadNC::VarData>& vdatas,
00201                                                       std::vector<int>& tstep_nums) = 0;
00202 #else
00203   virtual ErrorCode read_ucd_variable_to_nonset(std::vector<ReadNC::VarData>& vdatas,
00204                                                 std::vector<int>& tstep_nums) = 0;
00205 #endif
00206 
00207 protected:
00210   template <typename T> ErrorCode kji_to_jik_stride(size_t , size_t nj, size_t nk, void* dest, T* source, Range& localGid)
00211   {
00212     std::size_t idxInSource = 0; // Position of the start of the stride
00213     // For each subrange, we will transpose a matrix of size
00214     // subrange*nj*nk (subrange takes the role of ni)
00215     T* tmp_data = reinterpret_cast<T*>(dest);
00216     for (Range::pair_iterator pair_iter = localGid.pair_begin();
00217         pair_iter != localGid.pair_end(); ++pair_iter) {
00218       std::size_t size_range = pair_iter->second - pair_iter->first + 1;
00219       std::size_t nik = size_range * nk, nij = size_range * nj;
00220       for (std::size_t j = 0; j != nj; j++)
00221         for (std::size_t i = 0; i != size_range; i++)
00222           for (std::size_t k = 0; k != nk; k++)
00223             tmp_data[idxInSource + j*nik + i*nk + k] = source[idxInSource + k*nij + j*size_range + i];
00224       idxInSource += (size_range*nj*nk);
00225     }
00226     return MB_SUCCESS;
00227   }
00228 
00230   int nCells;
00231   int nEdges;
00232   int nVertices;
00233 
00235   int nLocalCells;
00236   int nLocalEdges;
00237   int nLocalVertices;
00238 
00240   std::vector<double> xVertVals, yVertVals, zVertVals;
00241 
00243   int cDim, eDim, vDim;
00244 
00246   Range localGidCells, localGidEdges, localGidVerts;
00247 };
00248 
00249 } // namespace moab
00250 
00251 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines