1 | /* ************************************************************************** * |
---|
2 | * Copyright © IPSL/LSCE, XMLIOServer, Avril 2010 - Octobre 2011 * |
---|
3 | * ************************************************************************** */ |
---|
4 | |
---|
5 | #ifndef __ONETCDF4_HPP__ |
---|
6 | #define __ONETCDF4_HPP__ |
---|
7 | |
---|
8 | #ifndef __XIOS_NO_EXTERN |
---|
9 | |
---|
10 | // M(essage) P(assing) I(nterface) headers |
---|
11 | #include <mpi.h> |
---|
12 | |
---|
13 | // NetCDF headers |
---|
14 | #define MPI_INCLUDED |
---|
15 | #include <netcdf.h> |
---|
16 | extern "C" |
---|
17 | { |
---|
18 | #include <netcdf_par.h> |
---|
19 | } |
---|
20 | |
---|
21 | // C++ standard headers |
---|
22 | #include <vector> |
---|
23 | |
---|
24 | // boost Headers |
---|
25 | #include <boost/multi_array.hpp> |
---|
26 | #include <boost/optional.hpp> |
---|
27 | |
---|
28 | #endif // __XIOS_NO_EXTERN |
---|
29 | |
---|
30 | // XMLIOServer headers |
---|
31 | #include "xmlioserver_spl.hpp" |
---|
32 | |
---|
33 | #ifndef UNLIMITED_DIM |
---|
34 | #define UNLIMITED_DIM (size_t)(-1) |
---|
35 | #endif //UNLIMITED_DIM |
---|
36 | |
---|
37 | // ////////////////////////////// Déclarations ///////////////////////////// // |
---|
38 | |
---|
39 | namespace xmlioserver |
---|
40 | { |
---|
41 | namespace io |
---|
42 | { |
---|
43 | /** |
---|
44 | * \class CONetCDF4 |
---|
45 | * \brief Classe permettant d'écrire des fichiers de données au format netCDF-4. |
---|
46 | */ |
---|
47 | class CONetCDF4 |
---|
48 | { |
---|
49 | public : // Définition de type |
---|
50 | |
---|
51 | //! \brief Redéfinition de std::vector<std::string> pour simplification d'écriture. |
---|
52 | typedef std::vector<std::string> CNetCDF4Path; |
---|
53 | |
---|
54 | public : // Constructeurs |
---|
55 | |
---|
56 | //! \brief Constructeur simple. |
---|
57 | CONetCDF4(void); // NIY |
---|
58 | |
---|
59 | /*! |
---|
60 | * \brief Constructeur à partir d'un nom de fichier au format netCDF4. |
---|
61 | * \param _filename : un nom de fichier au format netCDF4. |
---|
62 | */ |
---|
63 | explicit CONetCDF4(const std::string & _filename, bool exist = false, |
---|
64 | const MPI_Comm * _comm_server = NULL); |
---|
65 | |
---|
66 | /*! |
---|
67 | * \brief Constructeur par copie . |
---|
68 | * \param _onetcdf4 : référence de l'objet à copier. |
---|
69 | */ |
---|
70 | CONetCDF4(const CONetCDF4 & _onetcdf4);// NIY |
---|
71 | |
---|
72 | /*! |
---|
73 | * \brief Constructeur par copie. |
---|
74 | * \param _onetcdf4_ptr : le pointeur sur l'objet à copier. |
---|
75 | */ |
---|
76 | CONetCDF4(const CONetCDF4 * const _onetcdf4_ptr); // NIY |
---|
77 | |
---|
78 | public : // Destructeur |
---|
79 | |
---|
80 | //! \brief Destructeur de l'objet (le fichier est clos lors de son appel). |
---|
81 | virtual ~CONetCDF4(void); |
---|
82 | |
---|
83 | public : // Mutateur |
---|
84 | |
---|
85 | void setCurrentPath(const CNetCDF4Path & _path); |
---|
86 | |
---|
87 | int addDimension(const std::string & _name, std::size_t _size = UNLIMITED_DIM); |
---|
88 | int addVariable (const std::string & _name, nc_type _type, |
---|
89 | const std::vector<std::string> & _dims); |
---|
90 | int addGroup (const std::string & _name); |
---|
91 | |
---|
92 | public : // Accesseurs |
---|
93 | |
---|
94 | const CNetCDF4Path & getCurrentPath(void) const; |
---|
95 | |
---|
96 | std::string getUnlimitedDimensionName(void); |
---|
97 | std::vector<std::string> getDimensionsIdList (const std::string * _varname); |
---|
98 | |
---|
99 | protected : // Accesseurs protégés |
---|
100 | |
---|
101 | /*! |
---|
102 | * \brief Retourne l'identifiant du groupe courant |
---|
103 | * (le chemin est gardée en mémoire dans la variable path). |
---|
104 | * \return l'identifiant du groupe courant |
---|
105 | * \see path |
---|
106 | */ |
---|
107 | int getCurrentGroup(void); |
---|
108 | |
---|
109 | /*! |
---|
110 | * \brief Retourne l'identifiant du groupe dont le chemin est passé en argument. |
---|
111 | * \param _path : Un chemin dans la structure de donnée netCDF. |
---|
112 | * \return l'identifiant du groupe. |
---|
113 | * |
---|
114 | * <i> Exemple d'utilisation: </i> |
---|
115 | * \code |
---|
116 | * //> Retourne l'identifiant d'un group. |
---|
117 | * CINetCDF4 ncinput(std::string("mon_fichier.nc")); |
---|
118 | * CNetCDF4Path path; |
---|
119 | * path.push_back("mes_donnees"); path.push_back("mes_tableaux"); |
---|
120 | * int groupid = ncinput.getGroup(path); |
---|
121 | * \endcode |
---|
122 | */ |
---|
123 | int getGroup(const CNetCDF4Path & _path); |
---|
124 | |
---|
125 | /*! |
---|
126 | * \brief Retourne l'identifiant d'une variable dans le groupe courant. |
---|
127 | * \param _varname : Le nom d'une variable dans le fichier de données. |
---|
128 | * \return l'identifiant de la variable. |
---|
129 | */ |
---|
130 | int getVariable(const std::string & _varname); |
---|
131 | |
---|
132 | /*! |
---|
133 | * \brief Retourne l'identifiant d'une dimension dans le groupe courant. |
---|
134 | * \param _dimname : Le nom d'une dimension dans le fichier de données. |
---|
135 | * \return l'identifiant de la dimension. |
---|
136 | */ |
---|
137 | int getDimension(const std::string & _dimname); |
---|
138 | |
---|
139 | /*! |
---|
140 | * \brief Retourne les dimensions d'une variable. |
---|
141 | * \param _varname : Le nom d'une variable dans le fichier de données. |
---|
142 | * \return Un vecteur des dimensions associées à la variables |
---|
143 | */ |
---|
144 | std::vector<std::size_t> getDimensions(const std::string & _varname); |
---|
145 | |
---|
146 | /*! |
---|
147 | * \brief Retourne l'identifiant de la dimension non limité dans le fichier. |
---|
148 | * \return l'identifiant de la dimension non limité ou <b>-1</b> s'il n'y en a pas. |
---|
149 | */ |
---|
150 | int getUnlimitedDimension(void); |
---|
151 | |
---|
152 | /*! |
---|
153 | * \brief Indique si une variable existe dans le groupe courant. |
---|
154 | * \param _varname : Le nom d'une variable dans le fichier de données. |
---|
155 | * \return <b>true</b> si la variable existe, <b>false</b> sinon. |
---|
156 | */ |
---|
157 | bool varExist(const std::string & _varname); |
---|
158 | |
---|
159 | public : // Ecriture |
---|
160 | |
---|
161 | template <class DataType> |
---|
162 | void writeAttribute |
---|
163 | (const std::string & _attname, |
---|
164 | const std::vector<DataType> & _value, |
---|
165 | const std::string * _varname = NULL); |
---|
166 | |
---|
167 | void writeAttribute |
---|
168 | (const std::string & _attname, |
---|
169 | const std::string & _value, |
---|
170 | const std::string * _varname = NULL); |
---|
171 | |
---|
172 | template <class DataType, std::size_t ndim> |
---|
173 | void writeData( const boost::multi_array<DataType, ndim> & _data, |
---|
174 | const std::string & _varname, |
---|
175 | std::size_t _record = 0, |
---|
176 | bool _collective = true, |
---|
177 | const std::vector<std::size_t> * _start = NULL, |
---|
178 | const std::vector<std::size_t> * _count = NULL); |
---|
179 | |
---|
180 | private : // Ecriture privée |
---|
181 | |
---|
182 | template <class DataType> |
---|
183 | void writeData_(int _grpid, |
---|
184 | int _varid, |
---|
185 | const std::vector<std::size_t> & _sstart, |
---|
186 | const std::vector<std::size_t> & _scount, |
---|
187 | const DataType * _data); |
---|
188 | |
---|
189 | template <class DataType> |
---|
190 | void writeAttribute_ |
---|
191 | (const std::string & _attname, |
---|
192 | const DataType * _value, |
---|
193 | std::size_t _size, |
---|
194 | int _grpid, |
---|
195 | int _varid = NC_GLOBAL); |
---|
196 | |
---|
197 | private : // Obtention des informations d'écriture |
---|
198 | |
---|
199 | void getWriteDataInfos(const std::string & _varname, |
---|
200 | std::size_t _record, |
---|
201 | std::size_t & _array_size, |
---|
202 | std::vector<std::size_t> & _sstart, |
---|
203 | std::vector<std::size_t> & _scount, |
---|
204 | const std::vector<std::size_t> * _start, |
---|
205 | const std::vector<std::size_t> * _count); |
---|
206 | |
---|
207 | public : // Début/Fin de définition |
---|
208 | |
---|
209 | void definition_start(void); |
---|
210 | void definition_end(void); |
---|
211 | |
---|
212 | private : // Vérification des erreurs NetCDF |
---|
213 | |
---|
214 | static void CheckError(int _status) throw (CException); |
---|
215 | |
---|
216 | public : // Propriétés privées |
---|
217 | |
---|
218 | boost::optional<MPI_Comm> comm_server; |
---|
219 | std::vector<std::string> path; |
---|
220 | int ncidp; |
---|
221 | |
---|
222 | }; // class CONetCDF4 |
---|
223 | |
---|
224 | } // namespace io |
---|
225 | |
---|
226 | } // namespace xmlioserver |
---|
227 | |
---|
228 | #endif// __ONETCDF4_HPP__ |
---|