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
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 ep_lib::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      ep_lib::MPI_Comm_size(*comm, &commSize);
16      if (commSize <= 1)
17        comm = NULL;
18    }
19    mpi = comm && !multifile;
20    ep_lib::MPI_Info info_null;
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)
25      CNetCdfInterface::openPar(filename, NC_NOWRITE | NC_MPIIO, *(static_cast<MPI_Comm*>(comm->mpi_comm)), info_null.mpi_info, this->ncidp);
26      //CNetCdfInterface::openPar(filename, NC_NOWRITE | NC_MPIIO, *(static_cast<MPI_Comm*>(comm->mpi_comm)), info_null.mpi_info, this->ncidp);
27    else
28      CNetCdfInterface::open(filename, NC_NOWRITE, this->ncidp);
29
30    this->timeCounterName = timeCounterName;
31    if (!CNetCdfInterface::isDimExisted(this->ncidp, this->timeCounterName)) this->timeCounterName=this->getUnlimitedDimensionName() ;
32
33  }
34
35  CINetCDF4::~CINetCDF4(void)
36  { /* Nothing to do */ }
37
38  //---------------------------------------------------------------
39
40  void CINetCDF4::close(void)
41  {
42    CNetCdfInterface::close(this->ncidp);
43  }
44
45  //---------------------------------------------------------------
46
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();
52
53    for (; it != end; it++)
54    {
55      const StdString& groupid = *it;
56      CNetCdfInterface::inqNcId(retvalue, groupid, retvalue);
57    }
58
59    return retvalue;
60  }
61
62  int CINetCDF4::getVariable(const StdString& varname,
63                             const CVarPath* const path)
64  {
65    int varid = 0;
66    int grpid = this->getGroup(path);
67    if (this->hasVariable(varname, path))
68      CNetCdfInterface::inqVarId(grpid, varname, varid);
69    return varid;
70  }
71
72  int CINetCDF4::getDimension(const StdString& dimname,
73                              const CVarPath* const path)
74  {
75    int dimid = 0;
76    int grpid = this->getGroup(path);
77    CNetCdfInterface::inqDimId(grpid, dimname, dimid);
78    return dimid;
79  }
80
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;
88    CNetCdfInterface::inqAtt(grpid, varid, attname, retvalue.first, retvalue.second);
89    return retvalue;
90  }
91
92  int CINetCDF4::getUnlimitedDimension(const CVarPath* const path)
93  {
94    int dimid = 0;
95    int grpid = this->getGroup(path);
96    CNetCdfInterface::inqUnLimDim(grpid, dimid);
97    return dimid;
98  }
99
100  StdString CINetCDF4::getUnlimitedDimensionName(const CVarPath* const path)
101  {
102    int grpid = this->getGroup(path);
103    int dimid = this->getUnlimitedDimension(path);
104
105    StdString dimname;
106    if (dimid != -1)
107      CNetCdfInterface::inqDimName(grpid, dimid, dimname);
108    return dimname;
109  }
110
111  //---------------------------------------------------------------
112
113  StdSize CINetCDF4::getNbVertex(const StdString& name,
114                                 const CVarPath* const path)
115  {
116
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();
128      return this->getDimensions(&bound, path)[dim];
129    }
130    return size_t(-1);
131  }
132
133  //---------------------------------------------------------------
134
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;
140
141    CNetCdfInterface::inqGrpIds(grpid, nbgroup, NULL);
142    groupid = new int[nbgroup]();
143    CNetCdfInterface::inqGrpIds(grpid, nbgroup, groupid);
144
145    for (int i = 0; i < nbgroup; i++)
146    {
147      StdString fullGrpName;
148      CNetCdfInterface::inqGrpFullName(groupid[i], fullGrpName);
149      retvalue.push_back(fullGrpName);
150    }
151
152    delete [] groupid;
153    return retvalue;
154  }
155
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;
161
162    CNetCdfInterface::inqVarIds(grpid, nbvar, NULL);
163    varid = new int[nbvar]();
164    CNetCdfInterface::inqVarIds(grpid, nbvar, varid);
165
166    for (int i = 0; i < nbvar; i++)
167    {
168      StdString varName;
169      CNetCdfInterface::inqVarName(grpid, varid[i], varName);
170      retvalue.push_back(varName);
171    }
172
173    delete [] varid;
174    return retvalue;
175  }
176
177  StdSize CINetCDF4::getNbOfTimestep(const CVarPath* const path)
178  {
179    return this->getDimensions(NULL, path)[this->getUnlimitedDimensionName(path)];
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++)
207      {
208        const StdString& coord = *it;
209        if (this->hasVariable(coord, path))
210          retvalue.insert(retvalue.end(), coord);
211      }
212    }
213    return retvalue;
214  }
215
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;
222
223    if (var != NULL)
224    {
225      CNetCdfInterface::inqVarNDims(grpid, varid, nbdim);
226      dimid = new int[nbdim]();
227      CNetCdfInterface::inqVarDimId(grpid, varid, dimid);
228    }
229    else
230    {
231      CNetCdfInterface::inqDimIds(grpid, nbdim, NULL, 1);
232      dimid = new int[nbdim]();
233      CNetCdfInterface::inqDimIds(grpid, nbdim, dimid, 1);
234    }
235
236    for (int i = 0; i < nbdim; i++)
237    {
238      std::string dimname;
239      CNetCdfInterface::inqDimName(grpid, dimid[i], dimname);
240      retvalue.push_back(dimname);
241    }
242    delete [] dimid;
243
244    return retvalue;
245  }
246
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;
253
254    if (var != NULL)
255    {
256      CNetCdfInterface::inqVarNDims(grpid, varid, nbdim);
257      dimid = new int[nbdim]();
258      CNetCdfInterface::inqVarDimId(grpid, varid, dimid);
259    }
260    else
261    {
262      CNetCdfInterface::inqDimIds(grpid, nbdim, NULL, 1);
263      dimid = new int[nbdim]();
264      CNetCdfInterface::inqDimIds(grpid, nbdim, dimid, 1);
265    }
266
267    for (int i = 0; i < nbdim; i++)
268    {
269      std::string dimname;
270      CNetCdfInterface::inqDimName(grpid, dimid[i], dimname);
271      StdSize size = 0;
272      CNetCdfInterface::inqDimLen(grpid, dimid[i], size);
273
274      retvalue.insert(retvalue.end(), std::make_pair(dimname, size));
275    }
276    delete [] dimid;
277
278    return retvalue;
279  }
280
281  std::list<StdString> CINetCDF4::getAttributes(const StdString* const var, const CVarPath* const path)
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;
287
288    if (var != NULL)
289      CNetCdfInterface::inqVarNAtts(grpid, varid, nbatt);
290    else
291      CNetCdfInterface::inqNAtts(grpid, nbatt);
292
293    for (int i = 0; i < nbatt; i++)
294    {
295      StdString attname;
296      CNetCdfInterface::inqAttName(grpid, varid, i, attname);
297      retvalue.push_back(attname);
298    }
299    return retvalue;
300  }
301
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;
312      if (attname.compare(0, name.size(), name) == 0)
313        return retvalue;
314      retvalue++;
315    }
316    return -1;
317  }
318
319  //---------------------------------------------------------------
320
321  bool CINetCDF4::hasMissingValue(const StdString& name,
322                                  const CVarPath* const path)
323  {
324    return (this->hasAttribute("missing_value", &name, path) || this->hasAttribute("_FillValue", &name, path));
325  }
326
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;
336      if (attname.compare(0, name.size(), name) == 0) return true;
337    }
338    return false;
339  }
340
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;
349      if ((varname.compare(0, name.size(), name) == 0) && (0 != name.size()))  return true;
350    }
351    return false;
352  }
353
354  bool CINetCDF4::hasCoordinates(const StdString& name,
355                                 const CVarPath* const path)
356  {
357    return this->hasAttribute(CCFKeywords::XIOS_CF_coordinates, &name, path);
358  }
359
360  bool CINetCDF4::hasBounds(const StdString& name,
361                            const CVarPath* const path)
362  {
363    return this->hasAttribute(CCFKeywords::XIOS_CF_bounds, &name, path);
364  }
365
366  bool CINetCDF4::hasTemporalDim(const CVarPath* const path)
367  {
368    std::list<StdString> dims = this->getDimensionsList(NULL, path);
369    return (std::find(dims.begin(), dims.end(), timeCounterName) != dims.end());
370  }
371
372  //---------------------------------------------------------------
373
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;
392  }
393
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);
406
407  StdString CINetCDF4::getAttributeValue(const StdString& name,
408                                         const StdString* const var,
409                                         const CVarPath* const path)
410  {
411    std::vector<char> data = this->getAttributeValue<char>(name, var, path);
412
413    return StdString(data.begin(), data.end());
414  }
415
416  template <class T>
417  T CINetCDF4::getMissingValue(const StdString& name, const CVarPath* const path)
418  {
419    if (this->hasAttribute("missing_value", &name, path))
420      return this->getAttributeValue<T>("missing_value", &name, path)[0];
421    if (this->hasAttribute("_FillValue", &name, path))
422      return this->getAttributeValue<T>("_FillValue", &name, path)[0];
423    return 0;
424  }
425
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);
430
431  //---------------------------------------------------------------
432
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);
437
438    boost::split(retvalue, value, boost::is_any_of(" "));
439
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  }
448
449  StdString CINetCDF4::getCoordinatesId(const StdString& name, const CVarPath* const path)
450  {
451    StdString retvalue;
452    if (this->hasAttribute(CCFKeywords::XIOS_CF_coordinates, &name, path))
453    {
454      return this->getAttributeValue(CCFKeywords::XIOS_CF_coordinates, &name, path);
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++)
461      {
462        const StdString& value = *it;
463        retvalue.append(value).push_back(' ');
464      }
465      retvalue.erase(retvalue.end() - 1) ;
466    }
467
468    return retvalue;
469  }
470
471  StdString CINetCDF4::getBoundsId(const StdString& name,
472                                   const CVarPath* const path)
473  {
474    StdString retvalue;
475    if (this->hasAttribute(CCFKeywords::XIOS_CF_bounds, &name, path))
476      retvalue = this->getAttributeValue(CCFKeywords::XIOS_CF_bounds, &name, path);
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))
504      {
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;
510      }
511    }
512    return true;
513  }
514
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
520    bool isCurVi = true;
521    unsigned int nbLonLat = 0;
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;
527      if (this->hasVariable(coord, path) && !this->isTemporal(coord, path))
528      {
529        std::map<StdString, StdSize> dimvar = this->getDimensions(&coord, path);
530        if (2 == dimvar.size()) ++nbLonLat;
531      }
532    }
533    if (2 != nbLonLat) isCurVi = false;
534
535    return isCurVi;
536  }
537
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;
544    else return true ;
545   
546// check this part above   
547    StdString dimname = this->getDimensionsList(&name, path).back();
548
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;
554      if (this->hasVariable(coord, path) && !this->isTemporal(coord, path))
555      {
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    }
564
565    return true;
566  }
567
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  }
572
573  bool CINetCDF4::isTemporal(const StdString& name, const CVarPath* const path)
574  {
575    std::list<StdString> dims = this->getDimensionsList(&name, path);
576    return (std::find(dims.begin(), dims.end(), timeCounterName) != dims.end());
577  }
578
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))
588      {
589        if (this->isTemporal(coord, path))
590          continue;
591        i++;
592      }
593      else
594      {
595        StdString unlimitedDimName = this->getUnlimitedDimensionName();
596        if (coord.compare(0, unlimitedDimName.size(), unlimitedDimName) == 0)
597          continue;
598        i++;
599      }
600    }
601    return (i == 3);
602  }
603
604  bool CINetCDF4::isCellGrid(const StdString& name, const CVarPath* const path)
605  {
606    if (this->isCoordinate(name, path))
607    {
608      return this->hasBounds(name, path);
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++)
615      {
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        {
627          StdString unlimitedDimName = this->getUnlimitedDimensionName();
628          if (coord.compare(0, unlimitedDimName.size(), unlimitedDimName) == 0)
629            continue;
630          return false;
631        }
632      }
633    }
634
635    return true;
636  }
637
638  //---------------------------------------------------------------
639
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);
648
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;
654
655      if (!_isRecti && this->isRectilinear(var, path))  continue;
656      if (!_isCurvi && this->isCurvilinear(var, path))  continue;
657      if (!_isUnstr && this->isUnstructured(var, path)) continue;
658
659      if (!_isTemporal && this->isTemporal(var, path)) continue;
660      if (!_is3D       && this->is3Dim(var, path))     continue;
661      if (!_isCellData && this->isCellGrid(var, path)) continue;
662
663      if (this->isUnknown(var, path)) continue;
664
665      retvalue.push_back(var);
666    }
667    return retvalue;
668  }
669
670  //---------------------------------------------------------------
671
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)
691      {
692        sstart.push_back((*start)[i]);
693        scount.push_back((*count)[i]);
694        array_size *= (*count)[i];
695      }
696      else
697      {
698        sstart.push_back(0);
699        scount.push_back(dimmap[*it]);
700        array_size *= dimmap[*it];
701      }
702    }
703  }
704
705  template <class T>
706  void CINetCDF4::getData(CArray<T, 1>& data, const StdString& var,
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);
715    CNetCdfInterface::getVaraType(grpid, varid, &start[0], &count[0], data.dataFirst());
716  }
717
718  template <>
719  void CINetCDF4::getData(CArray<int, 1>& data, const StdString& var,
720                          const CVarPath* const path, StdSize record);
721  template <>
722  void CINetCDF4::getData(CArray<double, 1>& data, const StdString& var,
723                          const CVarPath* const path, StdSize record);
724  template <>
725  void CINetCDF4::getData(CArray<float, 1>& data, const StdString& var,
726                          const CVarPath* const path, StdSize record);
727
728  //---------------------------------------------------------------
729
730  StdString CINetCDF4::getLonCoordName(const StdString& varname,
731                                       const CVarPath* const path)
732  {
733    StdString lonName;
734    std::list<StdString>::const_iterator itbList, itList, iteList;
735    std::list<StdString> clist = this->getCoordinatesIdList(varname, path);
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))
743        {
744          lonName = *itList;
745          return lonName;
746        }
747      }
748    }
749    return lonName;
750  }
751
752  StdString CINetCDF4::getLatCoordName(const StdString& varname,
753                                       const CVarPath* const path)
754  {
755    StdString latName;
756    std::list<StdString>::const_iterator itbList, itList, iteList;
757    std::list<StdString> clist = this->getCoordinatesIdList(varname, path);
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))
765        {
766          latName = *itList;
767          return latName;
768        }
769      }
770    }
771    return latName;
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))
780      return *(++(++clist.begin()));
781    else
782      return *(++(++clist.rbegin()));
783  }
784} // namespace xios
Note: See TracBrowser for help on using the repository browser.