[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> |
---|
[265] | 14 | extern "C" { |
---|
[254] | 15 | #include <netcdf_par.h> |
---|
| 16 | } |
---|
[152] | 17 | |
---|
[254] | 18 | |
---|
[152] | 19 | #ifndef UNLIMITED_DIM |
---|
| 20 | #define UNLIMITED_DIM (size_t)(-1) |
---|
| 21 | #endif //UNLIMITED_DIM |
---|
| 22 | |
---|
| 23 | namespace 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); |
---|
| 38 | CONetCDF4(const StdString & filename, bool exist, const comm::MPIComm * comm, bool); |
---|
| 39 | |
---|
| 40 | CONetCDF4(const CONetCDF4 & onetcdf4); // Not implemented. |
---|
| 41 | CONetCDF4(const CONetCDF4 * const onetcdf4); // Not implemented. |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | /// Initialisation /// |
---|
| 45 | void initialize(const StdString & filename, bool exist, const MPI_Comm * comm); |
---|
| 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, |
---|
[189] | 55 | const std::vector<StdString> & dim); |
---|
| 56 | |
---|
| 57 | //---------------------------------------------------------------- |
---|
| 58 | public : |
---|
| 59 | |
---|
[152] | 60 | template <class T> |
---|
[201] | 61 | void setDefaultValue(const StdString & varname, const T * value = NULL); |
---|
| 62 | |
---|
| 63 | template <class T> |
---|
[152] | 64 | void addAttribute |
---|
| 65 | (const StdString & name, const T & value, const StdString * varname = NULL); |
---|
| 66 | |
---|
| 67 | /// Ecriture des données /// |
---|
[189] | 68 | template <class T, StdSize ndim> |
---|
| 69 | void writeData(const ARRAY(T, ndim) data, const StdString & name, |
---|
[152] | 70 | bool collective, StdSize record, |
---|
| 71 | const std::vector<StdSize> * start = NULL, |
---|
| 72 | const std::vector<StdSize> * count = NULL); |
---|
| 73 | |
---|
[183] | 74 | void writeData(const ARRAY(int, 2) data, const StdString & name); |
---|
[152] | 75 | |
---|
| 76 | /// Accesseur /// |
---|
| 77 | const CONetCDF4Path & getCurrentPath(void) const; |
---|
| 78 | |
---|
| 79 | /// Destructeur /// |
---|
| 80 | virtual ~CONetCDF4(void); |
---|
[189] | 81 | |
---|
| 82 | //---------------------------------------------------------------- |
---|
| 83 | |
---|
[152] | 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); |
---|
[265] | 96 | std::vector<StdSize> getDimensions (const StdString & varname); |
---|
| 97 | std::vector<StdString> getDimensionsIdList (const StdString * varname); |
---|
| 98 | int getUnlimitedDimension(void); |
---|
| 99 | StdString getUnlimitedDimensionName(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); |
---|
[265] | 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 | |
---|
[189] | 156 | this->writeData_(grpid, varid, sstart, scount, data->data()); |
---|
| 157 | } |
---|
[201] | 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 | } |
---|
[189] | 175 | |
---|
| 176 | ///--------------------------------------------------------------- |
---|
| 177 | |
---|
[152] | 178 | } // namespace io |
---|
| 179 | } // namespace xmlioserver |
---|
| 180 | |
---|
| 181 | #endif //__XMLIO_INETCDF4__ |
---|