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

Last change on this file since 219 was 219, checked in by hozdoba, 13 years ago

Préparation nouvelle arborescence

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