source: XMLIO_V2/dev/dev_rv/src/xmlio/output/onetcdf4.hpp @ 258

Last change on this file since 258 was 258, checked in by hozdoba, 13 years ago
File size: 6.1 KB
RevLine 
[152]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"
[157]8#include "data_output.hpp"
9#include "mpi_manager.hpp"
[152]10
11#include <mpi.h>
12#define MPI_INCLUDED
13#include <netcdf.h>
[258]14extern "C"
15{
[254]16#include <netcdf_par.h>
17}
[258]18>>>>>>> .r256
[152]19
[254]20
[152]21#ifndef UNLIMITED_DIM
22   #define UNLIMITED_DIM (size_t)(-1)
23#endif  //UNLIMITED_DIM
24
25namespace xmlioserver
26{
27   namespace io
28   {
29      /// ////////////////////// Déclarations ////////////////////// ///
30      class CONetCDF4
31         : public virtual CDataOutput
32      {
33         public :
34
35            /// Définition de type ///
36            typedef std::vector<StdString> CONetCDF4Path;
37
38            /// Constructeurs ///
39            CONetCDF4(const StdString & filename, bool exist, const MPI_Comm * comm = NULL);
40            CONetCDF4(const StdString & filename, bool exist, const comm::MPIComm * comm, bool);
41
42            CONetCDF4(const CONetCDF4 & onetcdf4);       // Not implemented.
43            CONetCDF4(const CONetCDF4 * const onetcdf4); // Not implemented.
44
45
46            /// Initialisation ///
47            void initialize(const StdString & filename, bool exist, const MPI_Comm * comm);
48            void definition_start(void);
49            void definition_end(void);
50
51            /// Mutateurs ///
52            void setCurrentPath(const CONetCDF4Path & path);
53
54            int addGroup(const StdString & name);
55            int addDimension(const StdString& name, const StdSize size = UNLIMITED_DIM);
56            int addVariable(const StdString & name, nc_type type,
[189]57                            const std::vector<StdString> & dim);
58                           
59      //----------------------------------------------------------------
60         public :
61         
[152]62            template <class T>
[201]63               void setDefaultValue(const StdString & varname, const T * value = NULL);
64         
65            template <class T>
[152]66               void addAttribute
67                  (const StdString & name, const T & value, const StdString * varname = NULL);
68
69            /// Ecriture des données ///
[189]70            template <class T, StdSize ndim>
71               void writeData(const ARRAY(T, ndim) data, const StdString & name,
[152]72                              bool collective, StdSize record,
73                              const std::vector<StdSize> * start = NULL,
74                              const std::vector<StdSize> * count = NULL);
75
[183]76            void writeData(const ARRAY(int, 2) data, const StdString & name);
[152]77
78            /// Accesseur ///
79            const CONetCDF4Path & getCurrentPath(void) const;
80
81            /// Destructeur ///
82            virtual ~CONetCDF4(void);
[189]83           
84      //----------------------------------------------------------------
85     
[152]86         protected :
87
88            /// Ecriture ///
89            virtual void writeField_ (const boost::shared_ptr<tree::CField>  field)  = 0;
90            virtual void writeDomain_(const boost::shared_ptr<tree::CDomain> domain) = 0;
91            virtual void writeAxis_  (const boost::shared_ptr<tree::CAxis>   axis)   = 0;
92
93            /// Accesseurs ///
94            int getCurrentGroup(void);
95            int getGroup(const CONetCDF4Path & path);
96            int getVariable(const StdString & varname);
97            int getDimension(const StdString & dimname);
98            std::vector<StdSize> getDimensions(const StdString & varname);
99            int getUnlimitedDimension(void);
[202]100
101            bool varExist(const StdString & varname);
102
[189]103      //----------------------------------------------------------------
104     
[152]105         private :
[189]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);
[152]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
[189]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         this->writeData_(grpid, varid, sstart, scount, data->data());
149      }
[201]150     
151      //----------------------------------------------------------------
152           
153      template <class T>
154         void CONetCDF4::setDefaultValue(const StdString & varname, const T * value)
155      {
156         int grpid = this->getCurrentGroup();
157         int varid = this->getVariable(varname);
158         
159         if (value != NULL)
160         {
161            CheckError(nc_def_var_fill(grpid, varid, 0, value));
162            this->addAttribute(StdString("missing_value"), *value, &varname);
163         }
164         else
165            CheckError(nc_def_var_fill(grpid, varid, 1, NULL));         
166      }
[189]167     
168      ///---------------------------------------------------------------
169
[152]170   } // namespace io
171} // namespace xmlioserver
172
173#endif //__XMLIO_INETCDF4__
Note: See TracBrowser for help on using the repository browser.