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

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

remove "io" namespace

YM

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