source: XIOS/dev/branch_openmp/src/io/inetcdf4.cpp @ 1356

Last change on this file since 1356 was 1356, checked in by yushan, 6 years ago

unify MPI_Comm type

  • 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: 26.1 KB
RevLine 
[219]1#include "inetcdf4.hpp"
[599]2#include "netCdfInterface.hpp"
[782]3#include "netCdf_cf_constant.hpp"
[219]4
5#include <boost/algorithm/string.hpp>
6
[335]7namespace xios
[219]8{
[1328]9  CINetCDF4::CINetCDF4(const StdString& filename, const ep_lib::MPI_Comm* comm /*= NULL*/, bool multifile /*= true*/, const StdString& timeCounterName /*= "time_counter"*/)
[599]10  {
[736]11    // Don't use parallel mode if there is only one process
12    if (comm)
[599]13    {
[736]14      int commSize = 0;
[1328]15      ep_lib::MPI_Comm_size(*comm, &commSize);
[736]16      if (commSize <= 1)
17        comm = NULL;
[599]18    }
[736]19    mpi = comm && !multifile;
[1328]20    ep_lib::MPI_Info info_null;
[736]21
22    // The file format will be detected automatically by NetCDF, it is safe to always set NC_MPIIO
23    // even if Parallel NetCDF ends up being used.
24    if (mpi)
[1356]25      CNetCdfInterface::openPar(filename, NC_NOWRITE | NC_MPIIO, *(static_cast<MPI_Comm*>(comm->mpi_comm)), info_null.mpi_info, this->ncidp);
[1355]26      //CNetCdfInterface::openPar(filename, NC_NOWRITE | NC_MPIIO, *(static_cast<MPI_Comm*>(comm->mpi_comm)), info_null.mpi_info, this->ncidp);
[736]27    else
28      CNetCdfInterface::open(filename, NC_NOWRITE, this->ncidp);
[802]29
30    this->timeCounterName = timeCounterName;
[811]31    if (!CNetCdfInterface::isDimExisted(this->ncidp, this->timeCounterName)) this->timeCounterName=this->getUnlimitedDimensionName() ;
[825]32
[599]33  }
[219]34
[599]35  CINetCDF4::~CINetCDF4(void)
36  { /* Nothing to do */ }
[219]37
[599]38  //---------------------------------------------------------------
[219]39
[599]40  void CINetCDF4::close(void)
41  {
[686]42    CNetCdfInterface::close(this->ncidp);
[599]43  }
[219]44
[599]45  //---------------------------------------------------------------
[219]46
[599]47  int CINetCDF4::getGroup(const CVarPath* const path)
48  {
49    int retvalue = this->ncidp;
50    if (path == NULL) return retvalue;
51    CVarPath::const_iterator it = path->begin(), end = path->end();
[219]52
[599]53    for (; it != end; it++)
54    {
55      const StdString& groupid = *it;
[686]56      CNetCdfInterface::inqNcId(retvalue, groupid, retvalue);
[599]57    }
[219]58
[599]59    return retvalue;
60  }
[219]61
[599]62  int CINetCDF4::getVariable(const StdString& varname,
63                             const CVarPath* const path)
64  {
65    int varid = 0;
66    int grpid = this->getGroup(path);
[825]67    if (this->hasVariable(varname, path))
68      CNetCdfInterface::inqVarId(grpid, varname, varid);
[599]69    return varid;
70  }
[219]71
[599]72  int CINetCDF4::getDimension(const StdString& dimname,
73                              const CVarPath* const path)
74  {
75    int dimid = 0;
76    int grpid = this->getGroup(path);
[686]77    CNetCdfInterface::inqDimId(grpid, dimname, dimid);
[599]78    return dimid;
79  }
[219]80
[599]81  std::pair<nc_type, StdSize> CINetCDF4::getAttribute(const StdString& attname,
82                                                      const StdString* const var,
83                                                      const CVarPath* const path)
84  {
85    std::pair<nc_type, StdSize> retvalue;
86    int grpid = this->getGroup(path);
87    int varid = (var != NULL) ? this->getVariable(*var, path) : NC_GLOBAL;
[686]88    CNetCdfInterface::inqAtt(grpid, varid, attname, retvalue.first, retvalue.second);
[599]89    return retvalue;
90  }
[219]91
[599]92  int CINetCDF4::getUnlimitedDimension(const CVarPath* const path)
93  {
94    int dimid = 0;
95    int grpid = this->getGroup(path);
[686]96    CNetCdfInterface::inqUnLimDim(grpid, dimid);
[599]97    return dimid;
98  }
[219]99
[599]100  StdString CINetCDF4::getUnlimitedDimensionName(const CVarPath* const path)
101  {
102    int grpid = this->getGroup(path);
103    int dimid = this->getUnlimitedDimension(path);
[219]104
[686]105    StdString dimname;
106    if (dimid != -1)
107      CNetCdfInterface::inqDimName(grpid, dimid, dimname);
[599]108    return dimname;
109  }
[219]110
[599]111  //---------------------------------------------------------------
[686]112
[599]113  StdSize CINetCDF4::getNbVertex(const StdString& name,
114                                 const CVarPath* const path)
115  {
[219]116
[599]117    if (this->isRectilinear(name, path) ||
118       this->isCurvilinear(name, path))
119    {
120      if (this->is3Dim(name, path)) return 8;
121      else return 4;
122    }
123    if (this->isUnstructured(name, path))
124    {
125      StdString bound = this->getBoundsId
126            (this->getCoordinatesIdList(name, path).back(), path);
127      StdString dim = this->getDimensionsList(&bound, path).back();
[686]128      return this->getDimensions(&bound, path)[dim];
[599]129    }
130    return size_t(-1);
131  }
[219]132
[599]133  //---------------------------------------------------------------
[219]134
[599]135  std::list<StdString> CINetCDF4::getGroups(const CVarPath* const path)
136  {
137    int nbgroup = 0, *groupid = NULL;
138    int grpid = this->getGroup(path);
139    std::list<StdString> retvalue;
[219]140
[686]141    CNetCdfInterface::inqGrpIds(grpid, nbgroup, NULL);
[599]142    groupid = new int[nbgroup]();
[686]143    CNetCdfInterface::inqGrpIds(grpid, nbgroup, groupid);
[219]144
[599]145    for (int i = 0; i < nbgroup; i++)
146    {
[686]147      StdString fullGrpName;
148      CNetCdfInterface::inqGrpFullName(groupid[i], fullGrpName);
149      retvalue.push_back(fullGrpName);
[599]150    }
[219]151
[599]152    delete [] groupid;
153    return retvalue;
154  }
[219]155
[599]156  std::list<StdString> CINetCDF4::getVariables(const CVarPath* const path)
157  {
158    int nbvar = 0, *varid = NULL;
159    int grpid = this->getGroup(path);
160    std::list<StdString> retvalue;
[219]161
[686]162    CNetCdfInterface::inqVarIds(grpid, nbvar, NULL);
[599]163    varid = new int[nbvar]();
[686]164    CNetCdfInterface::inqVarIds(grpid, nbvar, varid);
[219]165
[599]166    for (int i = 0; i < nbvar; i++)
167    {
[686]168      StdString varName;
169      CNetCdfInterface::inqVarName(grpid, varid[i], varName);
170      retvalue.push_back(varName);
[599]171    }
172
173    delete [] varid;
174    return retvalue;
175  }
176
177  StdSize CINetCDF4::getNbOfTimestep(const CVarPath* const path)
178  {
[686]179    return this->getDimensions(NULL, path)[this->getUnlimitedDimensionName(path)];
[599]180  }
181
182  std::set<StdString> CINetCDF4::getBoundVariables(const CVarPath* const path)
183  {
184    std::set<StdString> retvalue;
185    std::list<StdString> variables = this->getVariables(path);
186    std::list<StdString>::const_iterator it = variables.begin(), end = variables.end();
187    for (; it != end; it++)
188    {
189      const StdString& var = *it;
190      if (this->hasBounds(var, path))
191        retvalue.insert(retvalue.end(), this->getBoundsId(var, path));
192    }
193    return retvalue;
194  }
195
196  std::set<StdString> CINetCDF4::getCoordVariables(const CVarPath* const path)
197  {
198    std::set<StdString> retvalue;
199    std::list<StdString> variables = this->getVariables(path);
200    std::list<StdString>::const_iterator it = variables.begin(), end = variables.end();
201    for (; it != end; it++)
202    {
203      const StdString& var = *it;
204      std::list<StdString> coords = this->getCoordinatesIdList(var, path);
205      std::list<StdString>::const_iterator it = coords.begin(), end = coords.end();
206      for (; it != end; it++)
[219]207      {
[599]208        const StdString& coord = *it;
209        if (this->hasVariable(coord, path))
210          retvalue.insert(retvalue.end(), coord);
[219]211      }
[599]212    }
213    return retvalue;
214  }
[219]215
[599]216  std::list<StdString> CINetCDF4::getDimensionsList(const StdString* const var, const CVarPath* const path)
217  {
218    int nbdim = 0, *dimid = NULL;
219    int grpid = this->getGroup(path);
220    int varid = (var != NULL) ? this->getVariable(*var, path) : NC_GLOBAL;
221    std::list<StdString> retvalue;
[219]222
[599]223    if (var != NULL)
224    {
[686]225      CNetCdfInterface::inqVarNDims(grpid, varid, nbdim);
[599]226      dimid = new int[nbdim]();
[686]227      CNetCdfInterface::inqVarDimId(grpid, varid, dimid);
[599]228    }
229    else
230    {
[686]231      CNetCdfInterface::inqDimIds(grpid, nbdim, NULL, 1);
[599]232      dimid = new int[nbdim]();
[686]233      CNetCdfInterface::inqDimIds(grpid, nbdim, dimid, 1);
[599]234    }
[219]235
[599]236    for (int i = 0; i < nbdim; i++)
237    {
[686]238      std::string dimname;
239      CNetCdfInterface::inqDimName(grpid, dimid[i], dimname);
[599]240      retvalue.push_back(dimname);
241    }
242    delete [] dimid;
[219]243
[599]244    return retvalue;
245  }
[219]246
[599]247  std::map<StdString, StdSize> CINetCDF4::getDimensions(const StdString* const var, const CVarPath* const path)
248  {
249    int nbdim = 0, *dimid = NULL;
250    int grpid = this->getGroup(path);
251    int varid = (var != NULL) ? this->getVariable(*var, path) : NC_GLOBAL;
252    std::map<StdString, StdSize> retvalue;
[219]253
[599]254    if (var != NULL)
255    {
[686]256      CNetCdfInterface::inqVarNDims(grpid, varid, nbdim);
[599]257      dimid = new int[nbdim]();
[686]258      CNetCdfInterface::inqVarDimId(grpid, varid, dimid);
[599]259    }
260    else
261    {
[686]262      CNetCdfInterface::inqDimIds(grpid, nbdim, NULL, 1);
[599]263      dimid = new int[nbdim]();
[686]264      CNetCdfInterface::inqDimIds(grpid, nbdim, dimid, 1);
[599]265    }
[219]266
[599]267    for (int i = 0; i < nbdim; i++)
268    {
[686]269      std::string dimname;
270      CNetCdfInterface::inqDimName(grpid, dimid[i], dimname);
271      StdSize size = 0;
272      CNetCdfInterface::inqDimLen(grpid, dimid[i], size);
[219]273
[599]274      retvalue.insert(retvalue.end(), std::make_pair(dimname, size));
275    }
276    delete [] dimid;
[219]277
[599]278    return retvalue;
279  }
[219]280
[686]281  std::list<StdString> CINetCDF4::getAttributes(const StdString* const var, const CVarPath* const path)
[599]282  {
283    int nbatt = 0;
284    std::list<StdString> retvalue;
285    int grpid = this->getGroup(path);
286    int varid = (var != NULL) ? this->getVariable(*var, path) : NC_GLOBAL;
[219]287
[599]288    if (var != NULL)
[686]289      CNetCdfInterface::inqVarNAtts(grpid, varid, nbatt);
[599]290    else
[686]291      CNetCdfInterface::inqNAtts(grpid, nbatt);
[219]292
[599]293    for (int i = 0; i < nbatt; i++)
294    {
[686]295      StdString attname;
296      CNetCdfInterface::inqAttName(grpid, varid, i, attname);
[599]297      retvalue.push_back(attname);
298    }
299    return retvalue;
300  }
[219]301
[599]302  int CINetCDF4::getAttributeId(const StdString& name,
303                                const StdString* const var,
304                                const CVarPath* const path)
305  {
306    int retvalue = 0;
307    std::list<StdString> atts = this->getAttributes(var, path);
308    std::list<StdString>::const_iterator it = atts.begin(), end = atts.end();
309    for (; it != end; it++)
310    {
311      const StdString& attname = *it;
[782]312      if (attname.compare(0, name.size(), name) == 0)
[599]313        return retvalue;
314      retvalue++;
315    }
316    return -1;
317  }
[219]318
[599]319  //---------------------------------------------------------------
[219]320
[599]321  bool CINetCDF4::hasMissingValue(const StdString& name,
322                                  const CVarPath* const path)
323  {
[686]324    return (this->hasAttribute("missing_value", &name, path) || this->hasAttribute("_FillValue", &name, path));
[599]325  }
[219]326
[599]327  bool CINetCDF4::hasAttribute(const StdString& name,
328                               const StdString* const var ,
329                               const CVarPath* const path)
330  {
331    std::list<StdString> atts = this->getAttributes(var, path);
332    std::list<StdString>::const_iterator it = atts.begin(), end = atts.end();
333    for (; it != end; it++)
334    {
335      const StdString& attname = *it;
[782]336      if (attname.compare(0, name.size(), name) == 0) return true;
[599]337    }
338    return false;
339  }
[219]340
[599]341  bool CINetCDF4::hasVariable(const StdString& name,
342                              const CVarPath* const path)
343  {
344    std::list<StdString> variables = this->getVariables(path);
345    std::list<StdString>::const_iterator it = variables.begin(), end = variables.end();
346    for (; it != end; it++)
347    {
348      const StdString& varname = *it;
[825]349      if ((varname.compare(0, name.size(), name) == 0) && (0 != name.size()))  return true;
[599]350    }
351    return false;
352  }
[219]353
[599]354  bool CINetCDF4::hasCoordinates(const StdString& name,
355                                 const CVarPath* const path)
356  {
[782]357    return this->hasAttribute(CCFKeywords::XIOS_CF_coordinates, &name, path);
[599]358  }
[219]359
[599]360  bool CINetCDF4::hasBounds(const StdString& name,
361                            const CVarPath* const path)
362  {
[782]363    return this->hasAttribute(CCFKeywords::XIOS_CF_bounds, &name, path);
[599]364  }
[219]365
[599]366  bool CINetCDF4::hasTemporalDim(const CVarPath* const path)
367  {
[802]368    std::list<StdString> dims = this->getDimensionsList(NULL, path);
369    return (std::find(dims.begin(), dims.end(), timeCounterName) != dims.end());
[599]370  }
[219]371
[599]372  //---------------------------------------------------------------
[219]373
[686]374  template <class T>
375  std::vector<T> CINetCDF4::getAttributeValue(const StdString& name,
376                                              const StdString* const var,
377                                              const CVarPath* const path)
378  {
379    int grpid = this->getGroup(path);
380    int varid = (var != NULL) ? this->getVariable(*var, path) : NC_GLOBAL;
381    std::pair<nc_type , StdSize> attinfos = this->getAttribute(name, var, path);
382    std::vector<T> retvalue(attinfos.second);
383    nc_type type = CNetCdfInterface::getNcType<T>();
384    if (attinfos.first != type)
385      ERROR("CINetCDF4::getAttributeValue<T>(name, var, path)",
386            << "[ name : " << name
387            << ", type requested :" << attinfos.first
388            << ", type stored : " << type << "]"
389            << " Invalid type !");
390    CNetCdfInterface::getAttType(grpid, varid, name.c_str(), &retvalue[0]);
391    return retvalue;
[599]392  }
[219]393
[686]394  template std::vector<double> CINetCDF4::getAttributeValue(const StdString& name,
395                                                            const StdString* const var,
396                                                            const CVarPath* const path);
397  template std::vector<float> CINetCDF4::getAttributeValue(const StdString& name,
398                                                           const StdString* const var,
399                                                           const CVarPath* const path);
400  template std::vector<int> CINetCDF4::getAttributeValue(const StdString& name,
401                                                         const StdString* const var,
402                                                         const CVarPath* const path);
403  template std::vector<char> CINetCDF4::getAttributeValue(const StdString& name,
404                                                          const StdString* const var,
405                                                          const CVarPath* const path);
[219]406
[599]407  StdString CINetCDF4::getAttributeValue(const StdString& name,
408                                         const StdString* const var,
409                                         const CVarPath* const path)
410  {
[686]411    std::vector<char> data = this->getAttributeValue<char>(name, var, path);
[219]412
[686]413    return StdString(data.begin(), data.end());
[599]414  }
[219]415
[686]416  template <class T>
417  T CINetCDF4::getMissingValue(const StdString& name, const CVarPath* const path)
[599]418  {
419    if (this->hasAttribute("missing_value", &name, path))
[686]420      return this->getAttributeValue<T>("missing_value", &name, path)[0];
[599]421    if (this->hasAttribute("_FillValue", &name, path))
[686]422      return this->getAttributeValue<T>("_FillValue", &name, path)[0];
[599]423    return 0;
424  }
[219]425
[686]426  template double CINetCDF4::getMissingValue(const StdString& name, const CVarPath* const path);
427  template float CINetCDF4::getMissingValue(const StdString& name, const CVarPath* const path);
428  template int CINetCDF4::getMissingValue(const StdString& name, const CVarPath* const path);
429  template char CINetCDF4::getMissingValue(const StdString& name, const CVarPath* const path);
[219]430
[599]431  //---------------------------------------------------------------
[219]432
[599]433  std::list<StdString> CINetCDF4::getCoordinatesIdList(const StdString& name, const CVarPath* const path)
434  {
435    std::list<StdString> retvalue;
436    StdString value = this->getCoordinatesId(name, path);
[219]437
[599]438    boost::split(retvalue, value, boost::is_any_of(" "));
[219]439
[599]440    std::list<StdString>::iterator it = retvalue.begin(), end = retvalue.end();
441    for (; it != end; it++)
442    {
443      StdString& coord = *it;
444      coord.assign(coord.data());
445    }
446    return retvalue;
447  }
[219]448
[599]449  StdString CINetCDF4::getCoordinatesId(const StdString& name, const CVarPath* const path)
450  {
451    StdString retvalue;
[782]452    if (this->hasAttribute(CCFKeywords::XIOS_CF_coordinates, &name, path))
[599]453    {
[782]454      return this->getAttributeValue(CCFKeywords::XIOS_CF_coordinates, &name, path);
[599]455    }
456    else
457    {
458      std::list<StdString> dims = this->getDimensionsList(&name, path);
459      std::list<StdString>::const_iterator it = dims.begin(), end = dims.end();
460      for (; it != end; it++)
[219]461      {
[599]462        const StdString& value = *it;
463        retvalue.append(value).push_back(' ');
[219]464      }
[599]465      retvalue.erase(retvalue.end() - 1) ;
466    }
[219]467
[599]468    return retvalue;
469  }
470
471  StdString CINetCDF4::getBoundsId(const StdString& name,
472                                   const CVarPath* const path)
473  {
474    StdString retvalue;
[782]475    if (this->hasAttribute(CCFKeywords::XIOS_CF_bounds, &name, path))
476      retvalue = this->getAttributeValue(CCFKeywords::XIOS_CF_bounds, &name, path);
[599]477    return retvalue;
478  }
479
480  //---------------------------------------------------------------
481
482  bool CINetCDF4::isBound(const StdString& name,
483                          const CVarPath* const path)
484  {
485    std::set<StdString> bounds = this->getBoundVariables(path);
486    return (bounds.find(name) != bounds.end());
487  }
488
489  bool CINetCDF4::isCoordinate(const StdString& name,
490                               const CVarPath* const path)
491  {
492    std::set<StdString> coords = this->getCoordVariables(path);
493    return (coords.find(name) != coords.end());
494  }
495
496  bool CINetCDF4::isRectilinear(const StdString& name, const CVarPath* const path)
497  {
498    std::list<StdString> coords = this->getCoordinatesIdList(name, path);
499    std::list<StdString>::const_iterator it = coords.begin(), end = coords.end();
500    for (; it != end; it++)
501    {
502      const StdString& coord = *it;
503      if (this->hasVariable(coord, path) && !this->isTemporal(coord, path))
[219]504      {
[599]505        std::map<StdString, StdSize> dimvar = this->getDimensions(&coord, path);
506        if ((dimvar.size() == 1) && (dimvar.find(coord) != dimvar.end()))
507          continue;
508        else
509          return false;
[219]510      }
[599]511    }
512    return true;
513  }
[219]514
[599]515  bool CINetCDF4::isCurvilinear(const StdString& name, const CVarPath* const path)
516  {
517    if (this->isRectilinear(name, path) || !this->hasCoordinates(name, path))
518      return false;
519
[782]520    bool isCurVi = true;
521    unsigned int nbLonLat = 0;
[599]522    std::list<StdString> coords = this->getCoordinatesIdList(name, path);
523    std::list<StdString>::const_iterator it = coords.begin(), end = coords.end();
524    for (; it != end; it++)
525    {
526      const StdString& coord = *it;
[782]527      if (this->hasVariable(coord, path) && !this->isTemporal(coord, path))
[219]528      {
[599]529        std::map<StdString, StdSize> dimvar = this->getDimensions(&coord, path);
[782]530        if (2 == dimvar.size()) ++nbLonLat;
[219]531      }
[599]532    }
[782]533    if (2 != nbLonLat) isCurVi = false;
534
535    return isCurVi;
[599]536  }
[219]537
[599]538  bool CINetCDF4::isUnstructured(const StdString& name, const CVarPath* const path)
539  {
540    if (this->isRectilinear(name, path) ||
541        this->isCurvilinear(name, path) ||
542        !this->hasCoordinates(name, path))
543       return false;
[948]544    else return true ;
545   
546// check this part above   
[599]547    StdString dimname = this->getDimensionsList(&name, path).back();
[219]548
[599]549    std::list<StdString> coords = this->getCoordinatesIdList(name, path);
550    std::list<StdString>::const_iterator it = coords.begin(), end = coords.end();
551    for (; it != end; it++)
552    {
553      const StdString& coord = *it;
[782]554      if (this->hasVariable(coord, path) && !this->isTemporal(coord, path))
[219]555      {
[599]556        std::map<StdString, StdSize> dimvar = this->getDimensions(&coord, path);
557        if ((dimvar.size() == 1) &&
558            (dimvar.find(dimname) != dimvar.end()))
559          continue;
560        else
561          return false;
562      }
563    }
[219]564
[599]565    return true;
566  }
[219]567
[599]568  bool CINetCDF4::isUnknown(const StdString& name, const CVarPath* const path)
569  {
570    return !(this->isRectilinear(name, path) || this->isCurvilinear(name, path) || this->isUnstructured(name, path));
571  }
[219]572
[599]573  bool CINetCDF4::isTemporal(const StdString& name, const CVarPath* const path)
574  {
[802]575    std::list<StdString> dims = this->getDimensionsList(&name, path);
576    return (std::find(dims.begin(), dims.end(), timeCounterName) != dims.end());
[599]577  }
[219]578
[599]579  bool CINetCDF4::is3Dim(const StdString& name, const CVarPath* const path)
580  {
581    int i = 0;
582    std::list<StdString> coords = this->getCoordinatesIdList(name, path);
583    std::list<StdString>::const_iterator it = coords.begin(), end = coords.end();
584    for (; it != end; it++)
585    {
586      const StdString& coord = *it;
587      if (this->hasVariable(coord, path))
[219]588      {
[599]589        if (this->isTemporal(coord, path))
590          continue;
591        i++;
[219]592      }
[599]593      else
[219]594      {
[782]595        StdString unlimitedDimName = this->getUnlimitedDimensionName();
596        if (coord.compare(0, unlimitedDimName.size(), unlimitedDimName) == 0)
[599]597          continue;
598        i++;
[219]599      }
[599]600    }
601    return (i == 3);
602  }
[219]603
[599]604  bool CINetCDF4::isCellGrid(const StdString& name, const CVarPath* const path)
605  {
606    if (this->isCoordinate(name, path))
607    {
[686]608      return this->hasBounds(name, path);
[599]609    }
610    else
611    {
612      std::list<StdString> coords = this->getCoordinatesIdList(name, path);
613      std::list<StdString>::const_iterator it = coords.begin(), end = coords.end();
614      for (; it != end; it++)
[219]615      {
[599]616        const StdString& coord = *it;
617        if (this->hasVariable(coord, path))
618        {
619          if (this->isTemporal(coord, path))
620            continue;
621          if (this->isCellGrid(coord, path))
622            continue;
623          return false;
624        }
625        else
626        {
[782]627          StdString unlimitedDimName = this->getUnlimitedDimensionName();
628          if (coord.compare(0, unlimitedDimName.size(), unlimitedDimName) == 0)
[599]629            continue;
630          return false;
631        }
[219]632      }
[599]633    }
[219]634
[599]635    return true;
636  }
[219]637
[599]638  //---------------------------------------------------------------
[219]639
[599]640  std::list<StdString> CINetCDF4::getDataVariables(bool _is3D,       bool _isRecti,
641                                                   bool _isCurvi,    bool _isUnstr,
642                                                   bool _isCellData, bool _isTemporal,
643                                                   const CVarPath* const path)
644  {
645    std::list<StdString> retvalue;
646    std::list<StdString> allvars  = this->getVariables(path);
647    std::set<StdString> allcoords = this->getCoordVariables(path);
[219]648
[599]649    std::list<StdString>::const_iterator it = allvars.begin(), end = allvars.end();
650    for (; it != end; it++)
651    {
652      const StdString& var = *it;
653      if (this->isCoordinate(var, path)) continue;
[219]654
[599]655      if (!_isRecti && this->isRectilinear(var, path))  continue;
656      if (!_isCurvi && this->isCurvilinear(var, path))  continue;
657      if (!_isUnstr && this->isUnstructured(var, path)) continue;
[219]658
[599]659      if (!_isTemporal && this->isTemporal(var, path)) continue;
660      if (!_is3D       && this->is3Dim(var, path))     continue;
661      if (!_isCellData && this->isCellGrid(var, path)) continue;
[219]662
[599]663      if (this->isUnknown(var, path)) continue;
[219]664
[599]665      retvalue.push_back(var);
666    }
667    return retvalue;
668  }
[219]669
[599]670  //---------------------------------------------------------------
[219]671
[599]672  void CINetCDF4::getDataInfo(const StdString& var, const CVarPath* const path, StdSize record,
673                              std::vector<StdSize>& sstart, std::vector<StdSize>& scount, StdSize& array_size,
674                              const std::vector<StdSize>* start /*= NULL*/, const std::vector<StdSize>* count /*= NULL*/)
675  {
676    std::list<StdString> dimlist = this->getDimensionsList(&var, path);
677    std::map<StdString, StdSize> dimmap = this->getDimensions(&var, path);
678    std::list<StdString>::iterator it = dimlist.begin();
679    if (this->isTemporal(var, path))
680    {
681      if (record != UNLIMITED_DIM)
682        sstart.push_back(record);
683      else
684        sstart.push_back(0);
685      scount.push_back(1);
686      it++;
687    }
688    for (int i = 0; it != dimlist.end(); it++, i++)
689    {
690      if (start && count)
[219]691      {
[599]692        sstart.push_back((*start)[i]);
693        scount.push_back((*count)[i]);
694        array_size *= (*count)[i];
[219]695      }
[599]696      else
[219]697      {
[599]698        sstart.push_back(0);
699        scount.push_back(dimmap[*it]);
700        array_size *= dimmap[*it];
[219]701      }
[599]702    }
703  }
[219]704
[686]705  template <class T>
706  void CINetCDF4::getData(CArray<T, 1>& data, const StdString& var,
[599]707                          const CVarPath* const path, StdSize record)
708  {
709    std::vector<StdSize> start, count;
710    int grpid = this->getGroup(path);
711    int varid = this->getVariable(var, path);
712    StdSize array_size = 1;
713    this->getDataInfo(var, path, record, start, count, array_size);
714    data.resize(array_size);
[686]715    CNetCdfInterface::getVaraType(grpid, varid, &start[0], &count[0], data.dataFirst());
[599]716  }
[219]717
[599]718  template <>
[686]719  void CINetCDF4::getData(CArray<int, 1>& data, const StdString& var,
720                          const CVarPath* const path, StdSize record);
721  template <>
[599]722  void CINetCDF4::getData(CArray<double, 1>& data, const StdString& var,
[686]723                          const CVarPath* const path, StdSize record);
[599]724  template <>
725  void CINetCDF4::getData(CArray<float, 1>& data, const StdString& var,
[686]726                          const CVarPath* const path, StdSize record);
[219]727
[599]728  //---------------------------------------------------------------
729
730  StdString CINetCDF4::getLonCoordName(const StdString& varname,
731                                       const CVarPath* const path)
732  {
[825]733    StdString lonName;
[782]734    std::list<StdString>::const_iterator itbList, itList, iteList;
[599]735    std::list<StdString> clist = this->getCoordinatesIdList(varname, path);
[782]736    itbList = clist.begin(); iteList = clist.end();
737    for (itList = itbList; itList != iteList; ++itList)
738    {
739      if (this->hasAttribute(CCFKeywords::XIOS_CF_units, &(*itList), path))
740      {
741        StdString unit = this->getAttributeValue(CCFKeywords::XIOS_CF_units, &(*itList), path);
742        if (CCFConvention::XIOS_CF_Longitude_units.end() != CCFConvention::XIOS_CF_Longitude_units.find(unit))
[825]743        {
744          lonName = *itList;
745          return lonName;
746        }
[782]747      }
748    }
[825]749    return lonName;
[599]750  }
751
752  StdString CINetCDF4::getLatCoordName(const StdString& varname,
753                                       const CVarPath* const path)
754  {
[825]755    StdString latName;
[782]756    std::list<StdString>::const_iterator itbList, itList, iteList;
[599]757    std::list<StdString> clist = this->getCoordinatesIdList(varname, path);
[782]758    itbList = clist.begin(); iteList = clist.end();
759    for (itList = itbList; itList != iteList; ++itList)
760    {
761      if (this->hasAttribute(CCFKeywords::XIOS_CF_units, &(*itList), path))
762      {
763        StdString unit = this->getAttributeValue(CCFKeywords::XIOS_CF_units, &(*itList), path);
764        if (CCFConvention::XIOS_CF_Latitude_units.end() != CCFConvention::XIOS_CF_Latitude_units.find(unit))
[825]765        {
766          latName = *itList;
767          return latName;
768        }
[782]769      }
770    }
[825]771    return latName;
[599]772  }
773
774  StdString CINetCDF4::getVertCoordName(const StdString& varname,
775                                        const CVarPath* const path)
776  {
777    if (!this->is3Dim(varname, path)) return "";
778    std::list<StdString> clist = this->getCoordinatesIdList(varname, path);
779    if (this->hasCoordinates(varname, path))
[686]780      return *(++(++clist.begin()));
[599]781    else
[686]782      return *(++(++clist.rbegin()));
[599]783  }
[335]784} // namespace xios
Note: See TracBrowser for help on using the repository browser.