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

Last change on this file since 782 was 782, checked in by mhnguyen, 8 years ago

Reading attributes of curvilinear grid from file

+) Correct some minor bugs detecting type of grid
+) Use constant string for attributes conforming to CF convention
+) Add part of code to read attributes of curvilinear grid

Test
+) On Curie
+) test_remap passes

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