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