1 | #ifndef __XMLIO_INETCDF4__ |
---|
2 | #define __XMLIO_INETCDF4__ |
---|
3 | |
---|
4 | /// xmlioserver headers /// |
---|
5 | #include "xmlioserver_spl.hpp" |
---|
6 | #include "exception.hpp" |
---|
7 | #include "array.hpp" |
---|
8 | #include "data_output.hpp" |
---|
9 | #include "mpi_manager.hpp" |
---|
10 | |
---|
11 | #include <mpi.h> |
---|
12 | #define MPI_INCLUDED |
---|
13 | #include <netcdf.h> |
---|
14 | extern "C" |
---|
15 | { |
---|
16 | #include <netcdf_par.h> |
---|
17 | } |
---|
18 | |
---|
19 | |
---|
20 | #ifndef UNLIMITED_DIM |
---|
21 | #define UNLIMITED_DIM (size_t)(-1) |
---|
22 | #endif //UNLIMITED_DIM |
---|
23 | |
---|
24 | namespace xmlioserver |
---|
25 | { |
---|
26 | namespace io |
---|
27 | { |
---|
28 | /// ////////////////////// Déclarations ////////////////////// /// |
---|
29 | class CONetCDF4 |
---|
30 | : public virtual CDataOutput |
---|
31 | { |
---|
32 | public : |
---|
33 | |
---|
34 | /// Définition de type /// |
---|
35 | typedef std::vector<StdString> CONetCDF4Path; |
---|
36 | |
---|
37 | /// Constructeurs /// |
---|
38 | CONetCDF4(const StdString & filename, bool exist, const MPI_Comm * comm = NULL); |
---|
39 | CONetCDF4(const StdString & filename, bool exist, const comm::MPIComm * comm, bool); |
---|
40 | |
---|
41 | CONetCDF4(const CONetCDF4 & onetcdf4); // Not implemented. |
---|
42 | CONetCDF4(const CONetCDF4 * const onetcdf4); // Not implemented. |
---|
43 | |
---|
44 | |
---|
45 | /// Initialisation /// |
---|
46 | void initialize(const StdString & filename, bool exist, const MPI_Comm * comm); |
---|
47 | void definition_start(void); |
---|
48 | void definition_end(void); |
---|
49 | |
---|
50 | /// Mutateurs /// |
---|
51 | void setCurrentPath(const CONetCDF4Path & path); |
---|
52 | |
---|
53 | int addGroup(const StdString & name); |
---|
54 | int addDimension(const StdString& name, const StdSize size = UNLIMITED_DIM); |
---|
55 | int addVariable(const StdString & name, nc_type type, |
---|
56 | const std::vector<StdString> & dim); |
---|
57 | |
---|
58 | //---------------------------------------------------------------- |
---|
59 | public : |
---|
60 | |
---|
61 | template <class T> |
---|
62 | void setDefaultValue(const StdString & varname, const T * value = NULL); |
---|
63 | |
---|
64 | template <class T> |
---|
65 | void addAttribute |
---|
66 | (const StdString & name, const T & value, const StdString * varname = NULL); |
---|
67 | |
---|
68 | /// Ecriture des données /// |
---|
69 | template <class T, StdSize ndim> |
---|
70 | void writeData(const ARRAY(T, ndim) data, const StdString & name, |
---|
71 | bool collective, StdSize record, |
---|
72 | const std::vector<StdSize> * start = NULL, |
---|
73 | const std::vector<StdSize> * count = NULL); |
---|
74 | |
---|
75 | void writeData(const ARRAY(int, 2) data, const StdString & name); |
---|
76 | |
---|
77 | /// Accesseur /// |
---|
78 | const CONetCDF4Path & getCurrentPath(void) const; |
---|
79 | |
---|
80 | /// Destructeur /// |
---|
81 | virtual ~CONetCDF4(void); |
---|
82 | |
---|
83 | //---------------------------------------------------------------- |
---|
84 | |
---|
85 | protected : |
---|
86 | |
---|
87 | /// Ecriture /// |
---|
88 | virtual void writeField_ (const boost::shared_ptr<tree::CField> field) = 0; |
---|
89 | virtual void writeDomain_(const boost::shared_ptr<tree::CDomain> domain) = 0; |
---|
90 | virtual void writeAxis_ (const boost::shared_ptr<tree::CAxis> axis) = 0; |
---|
91 | |
---|
92 | /// Accesseurs /// |
---|
93 | int getCurrentGroup(void); |
---|
94 | int getGroup(const CONetCDF4Path & path); |
---|
95 | int getVariable(const StdString & varname); |
---|
96 | int getDimension(const StdString & dimname); |
---|
97 | std::vector<StdSize> getDimensions(const StdString & varname); |
---|
98 | int getUnlimitedDimension(void); |
---|
99 | |
---|
100 | bool varExist(const StdString & varname); |
---|
101 | |
---|
102 | //---------------------------------------------------------------- |
---|
103 | |
---|
104 | private : |
---|
105 | |
---|
106 | template <class T> |
---|
107 | void writeData_(int grpid, int varid, |
---|
108 | const std::vector<StdSize> & sstart, |
---|
109 | const std::vector<StdSize> & scount, T * data); |
---|
110 | |
---|
111 | void getWriteDataInfos(const StdString & name, StdSize record, StdSize & array_size, |
---|
112 | std::vector<StdSize> & sstart, |
---|
113 | std::vector<StdSize> & scount, |
---|
114 | const std::vector<StdSize> * start, |
---|
115 | const std::vector<StdSize> * count); |
---|
116 | |
---|
117 | /// Vérification des erreurs NetCDF /// |
---|
118 | void CheckError(int status); |
---|
119 | |
---|
120 | /// Propriétés privées /// |
---|
121 | CONetCDF4Path path; |
---|
122 | int ncidp; |
---|
123 | bool wmpi; |
---|
124 | |
---|
125 | }; // class CONetCDF4 |
---|
126 | |
---|
127 | ///--------------------------------------------------------------- |
---|
128 | |
---|
129 | template <class T, StdSize ndim> |
---|
130 | void CONetCDF4::writeData(const ARRAY(T, ndim) data, const StdString & name, |
---|
131 | bool collective, StdSize record, |
---|
132 | const std::vector<StdSize> * start, |
---|
133 | const std::vector<StdSize> * count) |
---|
134 | { |
---|
135 | int grpid = this->getCurrentGroup(); |
---|
136 | int varid = this->getVariable(name); |
---|
137 | StdSize array_size = 1; |
---|
138 | std::vector<StdSize> sstart, scount; |
---|
139 | |
---|
140 | if (this->wmpi && collective) |
---|
141 | CheckError(nc_var_par_access(grpid, varid, NC_COLLECTIVE)); |
---|
142 | if (this->wmpi && !collective) |
---|
143 | CheckError(nc_var_par_access(grpid, varid, NC_INDEPENDENT)); |
---|
144 | |
---|
145 | this->getWriteDataInfos |
---|
146 | (name, record, array_size, sstart, scount, start, count); |
---|
147 | this->writeData_(grpid, varid, sstart, scount, data->data()); |
---|
148 | } |
---|
149 | |
---|
150 | //---------------------------------------------------------------- |
---|
151 | |
---|
152 | template <class T> |
---|
153 | void CONetCDF4::setDefaultValue(const StdString & varname, const T * value) |
---|
154 | { |
---|
155 | int grpid = this->getCurrentGroup(); |
---|
156 | int varid = this->getVariable(varname); |
---|
157 | |
---|
158 | if (value != NULL) |
---|
159 | { |
---|
160 | CheckError(nc_def_var_fill(grpid, varid, 0, value)); |
---|
161 | this->addAttribute(StdString("missing_value"), *value, &varname); |
---|
162 | } |
---|
163 | else |
---|
164 | CheckError(nc_def_var_fill(grpid, varid, 1, NULL)); |
---|
165 | } |
---|
166 | |
---|
167 | ///--------------------------------------------------------------- |
---|
168 | |
---|
169 | } // namespace io |
---|
170 | } // namespace xmlioserver |
---|
171 | |
---|
172 | #endif //__XMLIO_INETCDF4__ |
---|