[498] | 1 | /*! |
---|
| 2 | \file netCdfInterface.hpp |
---|
| 3 | \author Ha NGUYEN |
---|
| 4 | \date 08 Oct 2014 |
---|
| 5 | \since 03 Oct 2014 |
---|
| 6 | |
---|
| 7 | \brief Wrapper of netcdf functions. |
---|
| 8 | */ |
---|
| 9 | #ifndef __NETCDF_INTERFACE_HPP_ |
---|
| 10 | #define __NETCDF_INTERFACE_HPP_ |
---|
| 11 | |
---|
[591] | 12 | #include "xios_spl.hpp" |
---|
[525] | 13 | |
---|
| 14 | #if !defined(USING_NETCDF_PAR) |
---|
| 15 | #include "exception.hpp" |
---|
| 16 | #endif |
---|
| 17 | |
---|
[498] | 18 | #include "mpi.hpp" |
---|
| 19 | #include "netcdf.hpp" |
---|
| 20 | |
---|
| 21 | namespace xios |
---|
| 22 | { |
---|
| 23 | /*! |
---|
| 24 | \class CNetCdfInterface |
---|
| 25 | This class is wrapper of some common used functions of netCdf in Xios |
---|
| 26 | */ |
---|
| 27 | class CNetCdfInterface |
---|
| 28 | { |
---|
| 29 | public: |
---|
| 30 | //! Create a netcdf file |
---|
| 31 | static int create(const StdString& path, int cmode, int& ncId); |
---|
| 32 | |
---|
| 33 | //! Create a netcdf file on a parallel file system |
---|
| 34 | static int createPar(const StdString& path, int cmode, MPI_Comm comm, MPI_Info info, int& ncId); |
---|
| 35 | |
---|
| 36 | //! Open a netcdf file |
---|
| 37 | static int open(const StdString& path, int oMode, int& ncId); |
---|
| 38 | |
---|
| 39 | //! Open a netcdf file |
---|
| 40 | static int openPar(const StdString& path, int cmode, MPI_Comm comm, MPI_Info info, int& ncId); |
---|
| 41 | |
---|
| 42 | //! Close a netcdf file |
---|
| 43 | static int close(int ncId); |
---|
| 44 | |
---|
| 45 | //! Put netcdf file into define mode |
---|
| 46 | static int reDef(int ncId); |
---|
| 47 | |
---|
| 48 | //! End define mode of a netcdf file |
---|
| 49 | static int endDef(int ncId); |
---|
| 50 | |
---|
| 51 | //! Query identity of a named group |
---|
| 52 | static int inqNcId(int ncid, const StdString& grpName, int& grpId); |
---|
| 53 | |
---|
| 54 | //! Query identity of a named variable |
---|
| 55 | static int inqVarId(int ncid, const StdString& varName, int& varId); |
---|
| 56 | |
---|
[686] | 57 | //! Query the name of a variable given its id |
---|
| 58 | static int inqVarName(int ncid, int varId, StdString& varName); |
---|
| 59 | |
---|
[498] | 60 | //! Query identity of a named dimension |
---|
[686] | 61 | static int inqDimId(int ncid, const StdString& dimName, int& dimId); |
---|
[498] | 62 | |
---|
| 63 | //! Query identity of unlimited dimension |
---|
| 64 | static int inqUnLimDim(int ncid, int& dimId); |
---|
| 65 | |
---|
| 66 | //! Query name of a dimension with its id |
---|
| 67 | static int inqDimName(int ncid, int dimId, StdString& dimName); |
---|
| 68 | |
---|
| 69 | //! Query length of dimension with its id |
---|
| 70 | static int inqDimLen(int ncid, int dimId, StdSize& dimLen); |
---|
| 71 | |
---|
| 72 | //! Query number of dimension of a variable with its id |
---|
| 73 | static int inqVarNDims(int ncid, int varId, int& nDims); |
---|
| 74 | |
---|
| 75 | //! Query list of dimension of a variable with its id |
---|
| 76 | static int inqVarDimId(int, int, int*); |
---|
| 77 | |
---|
| 78 | //! Query dimensions of a group |
---|
| 79 | static int inqDimIds(int ncid, int& nDims, int* dimIds, int includeParents); |
---|
| 80 | |
---|
[686] | 81 | //! Query the full name of a group given its id |
---|
| 82 | static int inqGrpFullName(int ncid, StdString& grpFullName); |
---|
[498] | 83 | |
---|
[686] | 84 | //! Query the list of group ids given a location id |
---|
| 85 | static int inqGrpIds(int ncid, int& numgrps, int* ncids); |
---|
| 86 | |
---|
| 87 | //! Query the list of variable ids given a location id |
---|
| 88 | static int inqVarIds(int ncid, int& nvars, int* varids); |
---|
| 89 | |
---|
| 90 | //! Query the type and the size of an attribute given its name and the id of the variable to which it is attached. |
---|
| 91 | static int inqAtt(int ncid, int varid, const StdString& name, nc_type& type, size_t& len); |
---|
| 92 | |
---|
| 93 | //! Query the number of global attributes given a location id |
---|
| 94 | static int inqNAtts(int ncid, int& ngatts); |
---|
| 95 | |
---|
| 96 | //! Query the number of attributes given a location id and a variable id |
---|
| 97 | static int inqVarNAtts(int ncid, int varid, int& natts); |
---|
| 98 | |
---|
| 99 | //! Query the name of an attribute given a location id, a variable id and the attribute number |
---|
| 100 | static int inqAttName(int ncid, int varid, int attnum, StdString& name); |
---|
| 101 | |
---|
[498] | 102 | //! Define a group |
---|
[686] | 103 | static int defGrp(int parentNcid, const StdString& grpName, int& grpId); |
---|
[498] | 104 | |
---|
| 105 | //! Define a dimension |
---|
[686] | 106 | static int defDim(int ncid, const StdString& dimName, StdSize dimLen, int& dimId); |
---|
[498] | 107 | |
---|
| 108 | //! Define a variable |
---|
[686] | 109 | static int defVar(int ncid, const StdString& varName, nc_type xtype, |
---|
[498] | 110 | int nDims, const int dimIds[], int& varId); |
---|
| 111 | |
---|
| 112 | //! Define variable chunking size |
---|
| 113 | static int defVarChunking(int ncid, int varId, int storage, StdSize chunkSize[]); |
---|
| 114 | |
---|
[606] | 115 | //! Define variable compression level |
---|
| 116 | static int defVarDeflate(int ncid, int varId, int compressionLevel); |
---|
| 117 | |
---|
[517] | 118 | //! Set or unset the fill mode |
---|
| 119 | static int setFill(int ncid, bool fill); |
---|
| 120 | |
---|
[498] | 121 | //! Define variable fill parameters |
---|
| 122 | static int defVarFill(int ncid, int varId, int noFill, void* fillValue); |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | //! Change access type of a variable |
---|
| 126 | static int varParAccess(int ncid, int varid, int access); |
---|
| 127 | |
---|
[686] | 128 | //! Sync the file |
---|
[498] | 129 | static int sync(int ncId); |
---|
| 130 | |
---|
[686] | 131 | //! Read an attribute |
---|
| 132 | template<typename T> |
---|
| 133 | static int getAttType(int ncid, int varid, const StdString& attrName, T* data); |
---|
[498] | 134 | |
---|
[686] | 135 | //! Set an attribute |
---|
| 136 | template<typename T> |
---|
| 137 | static int putAttType(int ncid, int varid, const StdString& attrName, StdSize numVal, const T* data); |
---|
[498] | 138 | |
---|
[686] | 139 | //! Get data for a variable |
---|
[498] | 140 | template<typename T> |
---|
[686] | 141 | static int getVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, T* data); |
---|
[498] | 142 | |
---|
[686] | 143 | //! Set data for a variable |
---|
[498] | 144 | template<typename T> |
---|
[686] | 145 | static int putVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, const T* data); |
---|
[498] | 146 | |
---|
[686] | 147 | //! Get the NetCDF type corresponding to a specific C type |
---|
| 148 | template<typename T> |
---|
| 149 | static nc_type getNcType(); |
---|
| 150 | |
---|
[498] | 151 | private: |
---|
| 152 | template<typename T> |
---|
[686] | 153 | static int ncGetAttType(int ncid, int varid, const char* attrName, T* data); |
---|
[498] | 154 | |
---|
| 155 | template<typename T> |
---|
[686] | 156 | static int ncPutAttType(int ncid, int varid, const char* attrName, StdSize numVal, const T* data); |
---|
[498] | 157 | |
---|
[686] | 158 | template<typename T> |
---|
| 159 | static int ncGetVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, T* data); |
---|
| 160 | |
---|
| 161 | template<typename T> |
---|
| 162 | static int ncPutVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, const T* data); |
---|
| 163 | |
---|
[498] | 164 | private: |
---|
| 165 | static StdString openMode2String(int oMode); |
---|
| 166 | |
---|
| 167 | static StdString creationMode2String(int cMode); |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | public: |
---|
| 171 | // Some functions dedude from several special cases |
---|
| 172 | //! Query the existence of a variable |
---|
| 173 | static bool isVarExisted(int ncId, const StdString& varName); |
---|
[811] | 174 | static bool isDimExisted(int ncId, const StdString& dimName); |
---|
[498] | 175 | }; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | |
---|
| 179 | #endif // NETCDFINTERFACE_HPP_ |
---|