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