source: XMLIO_V2/dev/common/src/xmlio/output/onetcdf4.hpp @ 286

Last change on this file since 286 was 286, checked in by ymipsl, 13 years ago

reprise en main de la version de H. Ozdoba. Correction de différentes erreurs de conception et bug.
Version NEMO operationnel en client/server, interoperabilita avec OASIS, reconstition de fichiers via netcdf4/HDF5

YM

File size: 6.6 KB
Line 
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>
14extern "C" {
15#include <netcdf_par.h>
16}
17
18
19#ifndef UNLIMITED_DIM
20   #define UNLIMITED_DIM (size_t)(-1)
21#endif  //UNLIMITED_DIM
22
23namespace xmlioserver
24{
25   namespace io
26   {
27      /// ////////////////////// Déclarations ////////////////////// ///
28      class CONetCDF4
29         : public virtual CDataOutput
30      {
31         public :
32
33            /// Définition de type ///
34            typedef std::vector<StdString> CONetCDF4Path;
35
36            /// Constructeurs ///
37            CONetCDF4(const StdString & filename, bool exist, const MPI_Comm * comm = NULL, bool multifile=true);
38
39            CONetCDF4(const CONetCDF4 & onetcdf4);       // Not implemented.
40            CONetCDF4(const CONetCDF4 * const onetcdf4); // Not implemented.
41
42
43            /// Initialisation ///
44            void initialize(const StdString & filename, bool exist, const MPI_Comm * comm, bool multifile);
45            void close(void) ;
46            void sync(void) ;
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            std::vector<StdString> getDimensionsIdList (const StdString * varname);
99            int       getUnlimitedDimension(void);
100            StdString getUnlimitedDimensionName(void);
101
102            bool varExist(const StdString & varname);
103
104      //----------------------------------------------------------------
105     
106         private :
107         
108            template <class T>
109               void writeData_(int grpid, int varid,
110                               const std::vector<StdSize> & sstart,
111                               const std::vector<StdSize> & scount, T * data);
112
113            void getWriteDataInfos(const StdString & name, StdSize record, StdSize & array_size,
114                                   std::vector<StdSize> & sstart,
115                                   std::vector<StdSize> & scount,
116                                   const std::vector<StdSize> * start,
117                                   const std::vector<StdSize> * count);
118
119            /// Vérification des erreurs NetCDF ///
120            void CheckError(int status);
121
122            /// Propriétés privées ///
123            CONetCDF4Path path;
124            int ncidp;
125            bool wmpi;
126
127      }; // class CONetCDF4
128
129      ///---------------------------------------------------------------
130           
131      template <class T, StdSize ndim>
132         void CONetCDF4::writeData(const ARRAY(T, ndim) data, const StdString & name,
133                                   bool collective, StdSize record,
134                                   const std::vector<StdSize> * start,
135                                   const std::vector<StdSize> * count)
136      {
137         int grpid = this->getCurrentGroup();
138         int varid = this->getVariable(name);
139         StdSize array_size = 1;
140         std::vector<StdSize> sstart, scount;
141
142         if (this->wmpi && collective)
143            CheckError(nc_var_par_access(grpid, varid, NC_COLLECTIVE));
144         if (this->wmpi && !collective)
145            CheckError(nc_var_par_access(grpid, varid, NC_INDEPENDENT));
146
147         this->getWriteDataInfos
148         (name, record, array_size,  sstart, scount, start, count);
149         if (data->num_elements() != array_size)
150         {
151            ERROR("CONetCDF4::writeData(...)",
152                  << "[ input array size = "  << data->num_elements()
153                  << ", intern array size = " << array_size
154                  << " ] Invalid input data !" );
155         }
156         
157         this->writeData_(grpid, varid, sstart, scount, data->data());
158      }
159     
160      //----------------------------------------------------------------
161           
162      template <class T>
163         void CONetCDF4::setDefaultValue(const StdString & varname, const T * value)
164      {
165         int grpid = this->getCurrentGroup();
166         int varid = this->getVariable(varname);
167         
168         if (value != NULL)
169         {
170            CheckError(nc_def_var_fill(grpid, varid, 0, value));
171            this->addAttribute(StdString("missing_value"), *value, &varname);
172         }
173         else
174            CheckError(nc_def_var_fill(grpid, varid, 1, NULL));         
175      }
176     
177      ///---------------------------------------------------------------
178
179   } // namespace io
180} // namespace xmlioserver
181
182#endif //__XMLIO_INETCDF4__
Note: See TracBrowser for help on using the repository browser.