source: XIOS/trunk/src/output/onetcdf4.hpp @ 314

Last change on this file since 314 was 314, checked in by ymipsl, 12 years ago

Removing obsolete files and some cleaning...

YM

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