source: XIOS/trunk/src/io/netCdfInterface.hpp @ 811

Last change on this file since 811 was 811, checked in by ymipsl, 7 years ago

Bug fix when time dimension is not the unlimited dimension

YM

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
File size: 5.8 KB
Line 
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
12#include "xios_spl.hpp"
13
14#if !defined(USING_NETCDF_PAR)
15#include "exception.hpp"
16#endif
17
18#include "mpi.hpp"
19#include "netcdf.hpp"
20
21namespace 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
57    //! Query the name of a variable given its id
58    static int inqVarName(int ncid, int varId, StdString& varName);
59
60    //! Query identity of a named dimension
61    static int inqDimId(int ncid, const StdString& dimName, int& dimId);
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
81    //! Query the full name of a group given its id
82    static int inqGrpFullName(int ncid, StdString& grpFullName);
83
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
102    //! Define a group
103    static int defGrp(int parentNcid, const StdString& grpName, int& grpId);
104
105    //! Define a dimension
106    static int defDim(int ncid, const StdString& dimName, StdSize dimLen, int& dimId);
107
108    //! Define a variable
109    static int defVar(int ncid, const StdString& varName, nc_type xtype,
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
115    //! Define variable compression level
116    static int defVarDeflate(int ncid, int varId, int compressionLevel);
117
118    //! Set or unset the fill mode
119    static int setFill(int ncid, bool fill);
120
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
128    //! Sync the file
129    static int sync(int ncId);
130
131    //! Read an attribute
132    template<typename T>
133    static int getAttType(int ncid, int varid, const StdString& attrName, T* data);
134
135    //! Set an attribute
136    template<typename T>
137    static int putAttType(int ncid, int varid, const StdString& attrName, StdSize numVal, const T* data);
138
139    //! Get data for a variable
140    template<typename T>
141    static int getVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, T* data);
142
143    //! Set data for a variable
144    template<typename T>
145    static int putVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, const T* data);
146
147    //! Get the NetCDF type corresponding to a specific C type
148    template<typename T>
149    static nc_type getNcType();
150
151  private:
152    template<typename T>
153    static int ncGetAttType(int ncid, int varid, const char* attrName, T* data);
154
155    template<typename T>
156    static int ncPutAttType(int ncid, int varid, const char* attrName, StdSize numVal, const T* data);
157
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
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);
174    static bool isDimExisted(int ncId, const StdString& dimName);
175  };
176}
177
178
179#endif // NETCDFINTERFACE_HPP_
Note: See TracBrowser for help on using the repository browser.