source: XIOS/trunk/src/io/inetcdf4.cpp @ 811

Last change on this file since 811 was 811, checked in by ymipsl, 8 years ago

Bug fix when time dimension is not the unlimited dimension

YM

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