source: XIOS/trunk/src/io/onetcdf4.cpp @ 1048

Last change on this file since 1048 was 878, checked in by oabramkina, 8 years ago

Sequential version for UGRID norms. File attribute "convention" has been added with two possible values "CF" and "UGRID". The default value is "CF".

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
File size: 20.5 KB
RevLine 
[528]1#include <fstream>
2
[219]3#include "onetcdf4.hpp"
[352]4#include "group_template.hpp"
[382]5#include "mpi.hpp"
[379]6#include "netcdf.hpp"
[498]7#include "netCdfInterface.hpp"
8#include "netCdfException.hpp"
[219]9
[335]10namespace xios
[219]11{
12      /// ////////////////////// Définitions ////////////////////// ///
13
[686]14      CONetCDF4::CONetCDF4(const StdString& filename, bool append, bool useClassicFormat,
[878]15                                                        bool useCFConvention,
[802]16                           const MPI_Comm* comm, bool multifile, const StdString& timeCounterName)
[686]17        : path()
18        , wmpi(false)
19        , useClassicFormat(useClassicFormat)
[878]20        , useCFConvention(useCFConvention)
[219]21      {
[878]22         this->initialize(filename, append, useClassicFormat, useCFConvention, comm, multifile, timeCounterName);
[219]23      }
[498]24
[219]25      //---------------------------------------------------------------
[286]26
[219]27      CONetCDF4::~CONetCDF4(void)
28      {
29      }
30
31      ///--------------------------------------------------------------
32
[878]33      void CONetCDF4::initialize(const StdString& filename, bool append, bool useClassicFormat, bool useCFConvention, 
[802]34                                 const MPI_Comm* comm, bool multifile, const StdString& timeCounterName)
[219]35      {
[517]36         this->useClassicFormat = useClassicFormat;
[878]37         this->useCFConvention = useCFConvention;
[517]38
39         int mode = useClassicFormat ? 0 : NC_NETCDF4;
[605]40
41         // Don't use parallel mode if there is only one process
42         if (comm)
43         {
44            int commSize = 0;
45            MPI_Comm_size(*comm, &commSize);
46            if (commSize <= 1)
47               comm = NULL;
48         }
49         wmpi = comm && !multifile;
50
51         if (wmpi)
[517]52            mode |= useClassicFormat ? NC_PNETCDF : NC_MPIIO;
53
[528]54         // If the file does not exist, we always create it
55         if (!append || !std::ifstream(filename.c_str()))
[219]56         {
[605]57            if (wmpi)
58               CNetCdfInterface::createPar(filename, mode, *comm, MPI_INFO_NULL, this->ncidp);
59            else
60               CNetCdfInterface::create(filename, mode, this->ncidp);
[528]61
62            this->appendMode = false;
[219]63         }
64         else
65         {
[528]66            mode |= NC_WRITE;
[605]67            if (wmpi)
68               CNetCdfInterface::openPar(filename, mode, *comm, MPI_INFO_NULL, this->ncidp);
69            else
70               CNetCdfInterface::open(filename, mode, this->ncidp);
[528]71
72            this->appendMode = true;
[219]73         }
[517]74
75         // If the classic NetCDF format is used, we enable the "no-fill mode" globally.
76         // This is done per variable for the NetCDF4 format.
77         if (useClassicFormat)
[606]78            CNetCdfInterface::setFill(this->ncidp, false);
[802]79
80         this->timeCounterName = timeCounterName;
[219]81      }
[498]82
[286]83      void CONetCDF4::close()
84      {
[686]85        CNetCdfInterface::close(this->ncidp);
[286]86      }
[498]87
[219]88      //---------------------------------------------------------------
[498]89
[219]90      void CONetCDF4::definition_start(void)
[498]91      {
[686]92         CNetCdfInterface::reDef(this->ncidp);
[219]93      }
[498]94
[219]95      //---------------------------------------------------------------
[498]96
[219]97      void CONetCDF4::definition_end(void)
98      {
[686]99         CNetCdfInterface::endDef(this->ncidp);
[219]100      }
101
102      //---------------------------------------------------------------
[498]103
[219]104      int CONetCDF4::getCurrentGroup(void)
105      {
[686]106         return this->getGroup(this->getCurrentPath());
[219]107      }
[498]108
[219]109      //---------------------------------------------------------------
[498]110
[686]111      int CONetCDF4::getGroup(const CONetCDF4Path& path)
[219]112      {
113         int retvalue = this->ncidp;
[498]114
[686]115         CONetCDF4Path::const_iterator it = path.begin(), end = path.end();
[219]116
[686]117         for (; it != end; it++)
[219]118         {
[686]119            const StdString& groupid = *it;
120            CNetCdfInterface::inqNcId(retvalue, groupid, retvalue);
[219]121         }
[686]122         return retvalue;
[219]123      }
[498]124
[219]125      //---------------------------------------------------------------
[498]126
[686]127      int CONetCDF4::getVariable(const StdString& varname)
[219]128      {
129         int varid = 0;
130         int grpid = this->getCurrentGroup();
[686]131         CNetCdfInterface::inqVarId(grpid, varname, varid);
132         return varid;
[219]133      }
[498]134
[219]135      //---------------------------------------------------------------
[498]136
[686]137      int CONetCDF4::getDimension(const StdString& dimname)
[219]138      {
139         int dimid = 0;
140         int grpid = this->getCurrentGroup();
[686]141         CNetCdfInterface::inqDimId(grpid, dimname, dimid);
142         return dimid;
[219]143      }
[498]144
[219]145      //---------------------------------------------------------------
[498]146
[219]147      int CONetCDF4::getUnlimitedDimension(void)
148      {
149         int dimid = 0;
150         int grpid = this->getCurrentGroup();
[686]151         CNetCdfInterface::inqUnLimDim(grpid, dimid);
152         return dimid;
[219]153      }
[498]154
[266]155      StdString CONetCDF4::getUnlimitedDimensionName(void)
156      {
157         int grpid = this->getGroup(path);
158         int dimid = this->getUnlimitedDimension();
[498]159
160         StdString dimname;
[686]161         if (dimid != -1)
162           CNetCdfInterface::inqDimName(grpid, dimid, dimname);
163         return dimname;
[266]164      }
[498]165
[219]166      //---------------------------------------------------------------
[498]167
[686]168      std::vector<StdSize> CONetCDF4::getDimensions(const StdString& varname)
[219]169      {
170         StdSize size = 0;
171         std::vector<StdSize> retvalue;
172         int grpid = this->getCurrentGroup();
173         int varid = this->getVariable(varname);
174         int nbdim = 0, *dimid = NULL;
175
[686]176         CNetCdfInterface::inqVarNDims(grpid, varid, nbdim);
[219]177         dimid = new int[nbdim]();
[686]178         CNetCdfInterface::inqVarDimId(grpid, varid, dimid);
[219]179
180         for (int i = 0; i < nbdim; i++)
181         {
[686]182            CNetCdfInterface::inqDimLen(grpid, dimid[i], size);
[219]183            if (size == NC_UNLIMITED)
184                size = UNLIMITED_DIM;
185            retvalue.push_back(size);
186         }
[401]187         delete [] dimid;
[686]188         return retvalue;
[219]189      }
190
[686]191      std::vector<std::string> CONetCDF4::getDimensionsIdList(const std::string* _varname)
[266]192      {
[498]193         int nDimNull = 0;
[266]194         int nbdim = 0, *dimid = NULL;
195         int grpid = this->getCurrentGroup();
196         int varid = (_varname != NULL) ? this->getVariable(*_varname) : NC_GLOBAL;
197         std::vector<std::string> retvalue;
[498]198
[266]199         if (_varname != NULL)
200         {
[686]201            CNetCdfInterface::inqVarNDims(grpid, varid, nbdim);
[266]202            dimid = new int[nbdim]();
[686]203            CNetCdfInterface::inqVarDimId(grpid, varid, dimid);
[266]204         }
205         else
206         {
[686]207            CNetCdfInterface::inqDimIds(grpid, nbdim, NULL, 1);
[266]208            dimid = new int[nbdim]();
[686]209            CNetCdfInterface::inqDimIds(grpid, nDimNull, dimid, 1);
[266]210         }
[498]211
[266]212         for (int i = 0; i < nbdim; i++)
213         {
[498]214            std::string dimname;
[686]215            CNetCdfInterface::inqDimName(grpid, dimid[i], dimname);
[266]216            retvalue.push_back(dimname);
217         }
218         delete [] dimid;
[498]219
[686]220         return retvalue;
[266]221      }
222
[219]223      //---------------------------------------------------------------
224
[707]225      void CONetCDF4::getTimeAxisBounds(CArray<double,2>& timeAxisBounds, const StdString& name, bool collective)
226      {
227        int grpid = this->getCurrentGroup();
228        int varid = this->getVariable(name);
229
230        std::vector<StdSize> start(2), count(2);
231        start[0] = 0;
232        // Find out how many temporal records have been written already to the file we are opening
233        int ncUnlimitedDimId;
234        CNetCdfInterface::inqUnLimDim(this->ncidp, ncUnlimitedDimId);
235        CNetCdfInterface::inqDimLen(this->ncidp, ncUnlimitedDimId, count[0]);
236        start[1] = 0;
237        count[1] = 2;
238
239        timeAxisBounds.resize(count[1], count[0]);
240
241        if (this->wmpi && collective)
242          CNetCdfInterface::varParAccess(grpid, varid, NC_COLLECTIVE);
243        if (this->wmpi && !collective)
244          CNetCdfInterface::varParAccess(grpid, varid, NC_INDEPENDENT);
245
246        CNetCdfInterface::getVaraType(grpid, varid, &start[0], &count[0], timeAxisBounds.dataFirst());
247      }
248
249      //---------------------------------------------------------------
250
[686]251      const CONetCDF4::CONetCDF4Path& CONetCDF4::getCurrentPath(void) const
252      { return this->path; }
[219]253
[686]254      void CONetCDF4::setCurrentPath(const CONetCDF4Path& path)
[219]255      { this->path = path; }
256
257      //---------------------------------------------------------------
258
[686]259      int CONetCDF4::addGroup(const StdString& name)
[219]260      {
261         int retvalue = 0;
262         int grpid = this->getCurrentGroup();
[686]263         CNetCdfInterface::defGrp(grpid, name, retvalue);
264         return retvalue;
[219]265      }
[498]266
[219]267      //---------------------------------------------------------------
[498]268
[219]269      int CONetCDF4::addDimension(const StdString& name, const StdSize size)
270      {
271         int retvalue = 0;
272         int grpid = this->getCurrentGroup();
273         if (size != UNLIMITED_DIM)
[686]274            CNetCdfInterface::defDim(grpid, name, size, retvalue);
[219]275         else
[686]276            CNetCdfInterface::defDim(grpid, name, NC_UNLIMITED, retvalue);
277         return retvalue;
[219]278      }
[498]279
[219]280      //---------------------------------------------------------------
[498]281
[686]282      int CONetCDF4::addVariable(const StdString& name, nc_type type,
283                                 const std::vector<StdString>& dim)
[219]284      {
[350]285         int varid = 0;
[219]286         std::vector<int> dimids;
[606]287         std::vector<StdSize> dimsizes;
[857]288         int dimSize = dim.size();
[878]289         
[606]290         StdSize size;
291         StdSize totalSize;
292         StdSize maxSize = 1024 * 1024 * 256; // == 2GB/8 if output double
[498]293
[219]294         int grpid = this->getCurrentGroup();
[498]295
[606]296         std::vector<StdString>::const_iterator it = dim.begin(), end = dim.end();
[219]297
[878]298         for (int idx = 0; it != end; it++, ++idx)
[219]299         {
[686]300            const StdString& dimid = *it;
[219]301            dimids.push_back(this->getDimension(dimid));
[606]302            CNetCdfInterface::inqDimLen(grpid, this->getDimension(dimid), size);
303            if (size == NC_UNLIMITED) size = 1;
304            dimsizes.push_back(size);
[219]305         }
[453]306
[606]307         CNetCdfInterface::defVar(grpid, name, type, dimids.size(), &dimids[0], varid);
[498]308
[517]309         // The classic NetCDF format does not support chunking nor fill parameters
310         if (!useClassicFormat)
[453]311         {
[517]312            // set chunksize : size of one record
313            // but must not be > 2GB (netcdf or HDF5 problem)
314            totalSize = 1;
315            for (vector<StdSize>::reverse_iterator it = dimsizes.rbegin(); it != dimsizes.rend(); ++it)
316            {
317              totalSize *= *it;
318              if (totalSize >= maxSize) *it = 1;
319            }
[857]320            int storageType = (0 == dimSize) ? NC_CONTIGUOUS : NC_CHUNKED;
321            CNetCdfInterface::defVarChunking(grpid, varid, storageType, &dimsizes[0]);
[517]322            CNetCdfInterface::defVarFill(grpid, varid, true, NULL);
[453]323         }
324
[606]325         return varid;
[219]326      }
327
328      //---------------------------------------------------------------
329
[606]330      void CONetCDF4::setCompressionLevel(const StdString& varname, int compressionLevel)
331      {
[607]332         if (compressionLevel < 0 || compressionLevel > 9)
333           ERROR("void CONetCDF4::setCompressionLevel(const StdString& varname, int compressionLevel)",
334                 "Invalid compression level for variable \"" << varname << "\", the value should range between 0 and 9.");
335         if (compressionLevel && wmpi)
336           ERROR("void CONetCDF4::setCompressionLevel(const StdString& varname, int compressionLevel)",
337                 "Impossible to use compression for variable \"" << varname << "\" when using parallel mode.");
338
[606]339         int grpid = this->getCurrentGroup();
340         int varid = this->getVariable(varname);
341         CNetCdfInterface::defVarDeflate(grpid, varid, compressionLevel);
342      }
343
344      //---------------------------------------------------------------
345
[219]346      template <>
[686]347      void CONetCDF4::addAttribute(const StdString& name, const StdString& value, const StdString* varname)
[219]348      {
349         int grpid = this->getCurrentGroup();
350         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname);
[686]351         CNetCdfInterface::putAttType(grpid, varid, name, value.size(), value.c_str());
[219]352      }
[498]353
[219]354      //---------------------------------------------------------------
[498]355
[219]356      template <>
[686]357      void CONetCDF4::addAttribute(const StdString& name, const double& value, const StdString* varname)
[219]358      {
359         int grpid = this->getCurrentGroup();
360         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname);
[686]361         CNetCdfInterface::putAttType(grpid, varid, name, 1, &value);
[219]362      }
[391]363
[686]364      template <>
365      void CONetCDF4::addAttribute(const StdString& name, const CArray<double,1>& value, const StdString* varname)
[391]366      {
367         int grpid = this->getCurrentGroup();
368         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname);
[686]369         CNetCdfInterface::putAttType(grpid, varid, name, value.numElements(), value.dataFirst());
[498]370      }
[219]371      //---------------------------------------------------------------
[498]372
[219]373      template <>
[686]374      void CONetCDF4::addAttribute(const StdString& name, const float& value, const StdString* varname)
[219]375      {
376         int grpid = this->getCurrentGroup();
377         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname);
[686]378         CNetCdfInterface::putAttType(grpid, varid, name, 1, &value);
[219]379      }
[391]380
[686]381      template <>
382      void CONetCDF4::addAttribute(const StdString& name, const CArray<float,1>& value, const StdString* varname)
[391]383      {
384         int grpid = this->getCurrentGroup();
385         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname);
[686]386         CNetCdfInterface::putAttType(grpid, varid, name, value.numElements(), value.dataFirst());
[498]387      }
388
[219]389      //---------------------------------------------------------------
[498]390
[219]391      template <>
[686]392      void CONetCDF4::addAttribute(const StdString& name, const int& value, const StdString* varname)
[219]393      {
394         int grpid = this->getCurrentGroup();
395         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname);
[686]396         CNetCdfInterface::putAttType(grpid, varid, name, 1, &value);
[219]397      }
398
[686]399      template <>
400      void CONetCDF4::addAttribute(const StdString& name, const CArray<int,1>& value, const StdString* varname)
[391]401      {
402         int grpid = this->getCurrentGroup();
403         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname);
[686]404         CNetCdfInterface::putAttType(grpid, varid, name, value.numElements(), value.dataFirst());
[498]405      }
406
[472]407      template <>
[686]408      void CONetCDF4::addAttribute(const StdString& name, const short int& value, const StdString* varname)
[472]409      {
410         int grpid = this->getCurrentGroup();
411         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname);
[686]412         CNetCdfInterface::putAttType(grpid, varid, name, 1, &value);
[472]413      }
[219]414
[686]415      template <>
416      void CONetCDF4::addAttribute(const StdString& name, const CArray<short int,1>& value, const StdString* varname)
[472]417      {
418         int grpid = this->getCurrentGroup();
419         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname);
[686]420         CNetCdfInterface::putAttType(grpid, varid, name, value.numElements(), value.dataFirst());
[498]421      }
422
[472]423      template <>
[686]424      void CONetCDF4::addAttribute(const StdString& name, const long int& value, const StdString* varname)
[472]425      {
426         int grpid = this->getCurrentGroup();
427         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname);
[686]428         CNetCdfInterface::putAttType(grpid, varid, name, 1, &value);
[472]429      }
430
[686]431      template <>
432      void CONetCDF4::addAttribute(const StdString& name, const CArray<long int,1>& value, const StdString* varname)
[472]433      {
434         int grpid = this->getCurrentGroup();
435         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname);
[686]436         CNetCdfInterface::putAttType(grpid, varid, name, value.numElements(), value.dataFirst());
[498]437      }
438
[686]439      //---------------------------------------------------------------
[498]440
[686]441      void CONetCDF4::getWriteDataInfos(const StdString& name, StdSize record, StdSize& array_size,
442                                        std::vector<StdSize>& sstart,
443                                        std::vector<StdSize>& scount,
444                                        const std::vector<StdSize>* start,
445                                        const std::vector<StdSize>* count)
[498]446      {
[266]447         std::vector<std::size_t> sizes  = this->getDimensions(name);
[857]448         if (sizes.size()==0) 
[266]449         {
[857]450            array_size=1 ;
451            sstart.push_back(0);
[606]452            scount.push_back(1);
[266]453         }
[857]454         else
455         {
456           std::vector<std::string> iddims = this->getDimensionsIdList (&name);
457           std::vector<std::size_t>::const_iterator
458           it  = sizes.begin(), end = sizes.end();
459           int i = 0;
[219]460
[857]461           if (iddims.begin()->compare(timeCounterName) == 0)
462           {
463             sstart.push_back(record);
464             scount.push_back(1);
465              if ((start == NULL) &&
466                  (count == NULL)) i++;
467              it++;
468           }
469
470           for (;it != end; it++)
471           {
472              if ((start != NULL) && (count != NULL))
473              {
474                 sstart.push_back((*start)[i]);
475                 scount.push_back((*count)[i]);
476                 array_size *= (*count)[i];
477                 i++;
478              }
479              else
480              {
481                 sstart.push_back(0);
482                 scount.push_back(sizes[i]);
483                 array_size *= sizes[i];
484                 i++;
485              }
486           }
487
[219]488         }
489      }
[498]490
[857]491
[219]492      template <>
[686]493      void CONetCDF4::writeData_(int grpid, int varid,
494                                 const std::vector<StdSize>& sstart,
495                                 const std::vector<StdSize>& scount, const double* data)
[219]496      {
[686]497         CNetCdfInterface::putVaraType(grpid, varid, &sstart[0], &scount[0], data);
[219]498      }
[498]499
[219]500      //---------------------------------------------------------------
[498]501
[219]502      template <>
[686]503      void CONetCDF4::writeData_(int grpid, int varid,
504                                 const std::vector<StdSize>& sstart,
505                                 const std::vector<StdSize>& scount, const int* data)
[219]506      {
[686]507          CNetCdfInterface::putVaraType(grpid, varid, &sstart[0], &scount[0], data);
[219]508      }
[498]509
[219]510      //---------------------------------------------------------------
[498]511
[219]512      template <>
[686]513      void CONetCDF4::writeData_(int grpid, int varid,
514                                 const std::vector<StdSize>& sstart,
515                                 const std::vector<StdSize>& scount, const float* data)
[219]516      {
[686]517          CNetCdfInterface::putVaraType(grpid, varid, &sstart[0], &scount[0], data);
[219]518      }
519
520      //---------------------------------------------------------------
521
[686]522      void CONetCDF4::writeData(const CArray<int, 2>& data, const StdString& name)
[219]523      {
524         int grpid = this->getCurrentGroup();
525         int varid = this->getVariable(name);
526         StdSize array_size = 1;
527         std::vector<StdSize> sstart, scount;
528
529         this->getWriteDataInfos(name, 0, array_size,  sstart, scount, NULL, NULL);
[369]530         this->writeData_(grpid, varid, sstart, scount, data.dataFirst());
[219]531      }
532
[686]533      void CONetCDF4::writeTimeAxisData(const CArray<double, 1>& data, const StdString& name,
[413]534                                        bool collective, StdSize record, bool isRoot)
535      {
536         int grpid = this->getCurrentGroup();
537         int varid = this->getVariable(name);
[498]538
[686]539         map<int,size_t>::iterator it=timeAxis.find(varid);
540         if (it == timeAxis.end()) timeAxis[varid] = record;
[498]541         else
[413]542         {
[686]543           if (it->second >= record) return;
544           else it->second =record;
[413]545         }
[498]546
[413]547         StdSize array_size = 1;
548         std::vector<StdSize> sstart, scount;
[498]549
[413]550         if (this->wmpi && collective)
[686]551            CNetCdfInterface::varParAccess(grpid, varid, NC_COLLECTIVE);
[413]552         if (this->wmpi && !collective)
[686]553            CNetCdfInterface::varParAccess(grpid, varid, NC_INDEPENDENT);
[498]554
[413]555         this->getWriteDataInfos(name, record, array_size,  sstart, scount, NULL, NULL);
[686]556         if (using_netcdf_internal)
557         {
558           if (!isRoot)
559           {
560             sstart[0] = sstart[0] + 1;
561             scount[0] = 0;
562           }
563         }
[413]564         this->writeData_(grpid, varid, sstart, scount, data.dataFirst());
565       }
566
[219]567      //---------------------------------------------------------------
[498]568
[686]569      bool CONetCDF4::varExist(const StdString& varname)
[219]570      {
571         int grpid = this->getCurrentGroup();
[686]572         return CNetCdfInterface::isVarExisted(grpid, varname);
[219]573      }
574
[878]575      bool CONetCDF4::dimExist(const StdString& dimname)
576      {
577         int grpid = this->getCurrentGroup();
578         return CNetCdfInterface::isDimExisted(grpid, dimname);
579      }
580
[286]581      void CONetCDF4::sync(void)
582      {
[686]583         CNetCdfInterface::sync(this->ncidp);
[498]584      }
[219]585      ///--------------------------------------------------------------
[337]586 } // namespace xios
Note: See TracBrowser for help on using the repository browser.