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

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

branch_openmp merged with XIOS_DEV_CMIP6@1459

  • 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: 27.7 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)), *(static_cast<MPI_Info*>(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->hasVariable(*var, path)) ? 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->hasVariable(*var, path)) ? this->getVariable(*var, path) : NC_GLOBAL;
221    std::list<StdString> retvalue;
222
223    if (var != NULL && this->hasVariable(*var, path))
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->hasVariable(*var, path)) ? this->getVariable(*var, path) : NC_GLOBAL;
252    std::map<StdString, StdSize> retvalue;
253
254    if (var != NULL && this->hasVariable(*var, path))
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->hasVariable(*var, path)) ? this->getVariable(*var, path) : NC_GLOBAL;
287
288    if (var != NULL && this->hasVariable(*var, path))
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->hasVariable(*var, path)) ? 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> varCoords = this->getCoordinatesIdList(name, path);
499    std::list<StdString> varDims = this->getDimensionsList(&name, path);
500    std::list<StdString>::const_iterator it = varCoords.begin(), end = varCoords.end();
501    std::set<StdString> varDims1D;
502
503    // Firstly, loop over coordinate list
504    for (; it != end; it++)
505    {
506      const StdString& coord = *it;
507      if (this->hasVariable(coord, path) && !this->isTemporal(coord, path) )
508      {
509        std::map<StdString, StdSize> coordDims = this->getDimensions(&coord, path);
510        for (std::map<StdString, StdSize>::const_iterator itTmp = coordDims.begin(); itTmp != coordDims.end(); itTmp++)
511        {
512          varDims.remove(itTmp->first);
513        }
514        if (this->isLonOrLat(coord, path) && coordDims.size() == 1)
515        {
516          varDims1D.insert(coordDims.begin()->first);
517          continue;
518        }
519      }
520    }
521    // Secondly, loop over remaining dimensions
522    for (it= varDims.begin(); it != varDims.end(); it++)
523    {
524      const StdString& coord = *it;
525      std::map<StdString, StdSize> coordDims = this->getDimensions(&coord, path);
526      if (this->hasVariable(coord, path) && !this->isTemporal(coord, path) )
527      {
528        if (this->isLonOrLat(coord, path) && coordDims.size() == 1)
529        {
530          varDims1D.insert(coordDims.begin()->first);
531          continue;
532        }
533      }
534    }
535
536    return (varDims1D.size() == 2);
537  }
538
539  bool CINetCDF4::isCurvilinear(const StdString& name, const CVarPath* const path)
540  {
541    if (this->isRectilinear(name, path) || !this->hasCoordinates(name, path))
542      return false;
543
544    bool isCurVi = true;
545    unsigned int nbLonLat = 0;
546    std::list<StdString> coords = this->getCoordinatesIdList(name, path);
547    std::list<StdString>::const_iterator it = coords.begin(), end = coords.end();
548    for (; it != end; it++)
549    {
550      const StdString& coord = *it;
551      if (this->hasVariable(coord, path) && !this->isTemporal(coord, path) && this->isLonOrLat(coord, path))
552      {
553        std::map<StdString, StdSize> dimvar = this->getDimensions(&coord, path);
554        if (2 == dimvar.size()) ++nbLonLat;
555      }
556    }
557    if (2 != nbLonLat) isCurVi = false;
558
559    return isCurVi;
560  }
561
562  bool CINetCDF4::isUnstructured(const StdString& name, const CVarPath* const path)
563  {
564    if (this->isRectilinear(name, path) ||
565        this->isCurvilinear(name, path) ||
566        !this->hasCoordinates(name, path))
567       return false;
568    else return true ;
569   
570// check this part above   
571    StdString dimname = this->getDimensionsList(&name, path).back();
572
573    std::list<StdString> coords = this->getCoordinatesIdList(name, path);
574    std::list<StdString>::const_iterator it = coords.begin(), end = coords.end();
575    for (; it != end; it++)
576    {
577      const StdString& coord = *it;
578      if (this->hasVariable(coord, path) && !this->isTemporal(coord, path) && this->isLonOrLat(coord, path))
579      {
580        std::map<StdString, StdSize> dimvar = this->getDimensions(&coord, path);
581        if ((dimvar.size() == 1) &&
582            (dimvar.find(dimname) != dimvar.end()))
583          continue;
584        else
585          return false;
586      }
587    }
588
589    return true;
590  }
591
592  bool CINetCDF4::isUnknown(const StdString& name, const CVarPath* const path)
593  {
594    return !(this->isRectilinear(name, path) || this->isCurvilinear(name, path) || this->isUnstructured(name, path));
595  }
596
597  bool CINetCDF4::isTemporal(const StdString& name, const CVarPath* const path)
598  {
599    std::list<StdString> dims = this->getDimensionsList(&name, path);
600    return (std::find(dims.begin(), dims.end(), timeCounterName) != dims.end());
601  }
602
603  bool CINetCDF4::is3Dim(const StdString& name, const CVarPath* const path)
604  {
605    int i = 0;
606    std::list<StdString> coords = this->getCoordinatesIdList(name, path);
607    std::list<StdString>::const_iterator it = coords.begin(), end = coords.end();
608    for (; it != end; it++)
609    {
610      const StdString& coord = *it;
611      if (this->hasVariable(coord, path))
612      {
613        if (this->isTemporal(coord, path))
614          continue;
615        i++;
616      }
617      else
618      {
619        StdString unlimitedDimName = this->getUnlimitedDimensionName();
620        if (coord.compare(0, unlimitedDimName.size(), unlimitedDimName) == 0)
621          continue;
622        i++;
623      }
624    }
625    return (i == 3);
626  }
627
628  bool CINetCDF4::isCellGrid(const StdString& name, const CVarPath* const path)
629  {
630    if (this->isCoordinate(name, path))
631    {
632      return this->hasBounds(name, path);
633    }
634    else
635    {
636      std::list<StdString> coords = this->getCoordinatesIdList(name, path);
637      std::list<StdString>::const_iterator it = coords.begin(), end = coords.end();
638      for (; it != end; it++)
639      {
640        const StdString& coord = *it;
641        if (this->hasVariable(coord, path))
642        {
643          if (this->isTemporal(coord, path))
644            continue;
645          if (this->isCellGrid(coord, path))
646            continue;
647          return false;
648        }
649        else
650        {
651          StdString unlimitedDimName = this->getUnlimitedDimensionName();
652          if (coord.compare(0, unlimitedDimName.size(), unlimitedDimName) == 0)
653            continue;
654          return false;
655        }
656      }
657    }
658
659    return true;
660  }
661
662  //---------------------------------------------------------------
663
664  std::list<StdString> CINetCDF4::getDataVariables(bool _is3D,       bool _isRecti,
665                                                   bool _isCurvi,    bool _isUnstr,
666                                                   bool _isCellData, bool _isTemporal,
667                                                   const CVarPath* const path)
668  {
669    std::list<StdString> retvalue;
670    std::list<StdString> allvars  = this->getVariables(path);
671    std::set<StdString> allcoords = this->getCoordVariables(path);
672
673    std::list<StdString>::const_iterator it = allvars.begin(), end = allvars.end();
674    for (; it != end; it++)
675    {
676      const StdString& var = *it;
677      if (this->isCoordinate(var, path)) continue;
678
679      if (!_isRecti && this->isRectilinear(var, path))  continue;
680      if (!_isCurvi && this->isCurvilinear(var, path))  continue;
681      if (!_isUnstr && this->isUnstructured(var, path)) continue;
682
683      if (!_isTemporal && this->isTemporal(var, path)) continue;
684      if (!_is3D       && this->is3Dim(var, path))     continue;
685      if (!_isCellData && this->isCellGrid(var, path)) continue;
686
687      if (this->isUnknown(var, path)) continue;
688
689      retvalue.push_back(var);
690    }
691    return retvalue;
692  }
693
694  //---------------------------------------------------------------
695
696  void CINetCDF4::getDataInfo(const StdString& var, const CVarPath* const path, StdSize record,
697                              std::vector<StdSize>& sstart, std::vector<StdSize>& scount, StdSize& array_size,
698                              const std::vector<StdSize>* start /*= NULL*/, const std::vector<StdSize>* count /*= NULL*/)
699  {
700    std::list<StdString> dimlist = this->getDimensionsList(&var, path);
701    std::map<StdString, StdSize> dimmap = this->getDimensions(&var, path);
702    std::list<StdString>::iterator it = dimlist.begin();
703    if (this->isTemporal(var, path))
704    {
705      if (record != UNLIMITED_DIM)
706        sstart.push_back(record);
707      else
708        sstart.push_back(0);
709      scount.push_back(1);
710      it++;
711    }
712    for (int i = 0; it != dimlist.end(); it++, i++)
713    {
714      if (start && count)
715      {
716        sstart.push_back((*start)[i]);
717        scount.push_back((*count)[i]);
718        array_size *= (*count)[i];
719      }
720      else
721      {
722        sstart.push_back(0);
723        scount.push_back(dimmap[*it]);
724        array_size *= dimmap[*it];
725      }
726    }
727  }
728
729  template <class T>
730  void CINetCDF4::getData(CArray<T, 1>& data, const StdString& var,
731                          const CVarPath* const path, StdSize record)
732  {
733    std::vector<StdSize> start, count;
734    int grpid = this->getGroup(path);
735    int varid = this->getVariable(var, path);
736    StdSize array_size = 1;
737    this->getDataInfo(var, path, record, start, count, array_size);
738    data.resize(array_size);
739    CNetCdfInterface::getVaraType(grpid, varid, &start[0], &count[0], data.dataFirst());
740  }
741
742  template <>
743  void CINetCDF4::getData(CArray<int, 1>& data, const StdString& var,
744                          const CVarPath* const path, StdSize record);
745  template <>
746  void CINetCDF4::getData(CArray<double, 1>& data, const StdString& var,
747                          const CVarPath* const path, StdSize record);
748  template <>
749  void CINetCDF4::getData(CArray<float, 1>& data, const StdString& var,
750                          const CVarPath* const path, StdSize record);
751
752  //---------------------------------------------------------------
753
754  StdString CINetCDF4::getLonCoordName(const StdString& varname,
755                                       const CVarPath* const path)
756  {
757    StdString lonName;
758    std::list<StdString>::const_iterator itbList, itList, iteList;
759    std::list<StdString> clist = this->getCoordinatesIdList(varname, path);
760    itbList = clist.begin(); iteList = clist.end();
761    for (itList = itbList; itList != iteList; ++itList)
762    {
763      if (this->hasAttribute(CCFKeywords::XIOS_CF_units, &(*itList), path))
764      {
765        StdString unit = this->getAttributeValue(CCFKeywords::XIOS_CF_units, &(*itList), path);
766        if (CCFConvention::XIOS_CF_Longitude_units.end() != CCFConvention::XIOS_CF_Longitude_units.find(unit))
767        {
768          lonName = *itList;
769          return lonName;
770        }
771      }
772    }
773    return lonName;
774  }
775
776  StdString CINetCDF4::getLatCoordName(const StdString& varname,
777                                       const CVarPath* const path)
778  {
779    StdString latName;
780    std::list<StdString>::const_iterator itbList, itList, iteList;
781    std::list<StdString> clist = this->getCoordinatesIdList(varname, path);
782    itbList = clist.begin(); iteList = clist.end();
783    for (itList = itbList; itList != iteList; ++itList)
784    {
785      if (this->hasAttribute(CCFKeywords::XIOS_CF_units, &(*itList), path))
786      {
787        StdString unit = this->getAttributeValue(CCFKeywords::XIOS_CF_units, &(*itList), path);
788        if (CCFConvention::XIOS_CF_Latitude_units.end() != CCFConvention::XIOS_CF_Latitude_units.find(unit))
789        {
790          latName = *itList;
791          return latName;
792        }
793      }
794    }
795    return latName;
796  }
797
798  StdString CINetCDF4::getVertCoordName(const StdString& varname,
799                                        const CVarPath* const path)
800  {
801    if (!this->is3Dim(varname, path)) return "";
802    std::list<StdString> clist = this->getCoordinatesIdList(varname, path);
803    if (this->hasCoordinates(varname, path))
804      return *(++(++clist.begin()));
805    else
806      return *(++(++clist.rbegin()));
807  }
808
809  bool CINetCDF4::isLonOrLat(const StdString& varname, const CVarPath* const path)
810  {
811  if (this->hasAttribute(CCFKeywords::XIOS_CF_units, &varname, path))
812  {
813    StdString unit = this->getAttributeValue(CCFKeywords::XIOS_CF_units, &varname, path);
814    return (CCFConvention::XIOS_CF_Latitude_units.end() != CCFConvention::XIOS_CF_Latitude_units.find(unit)
815            || CCFConvention::XIOS_CF_Longitude_units.end() != CCFConvention::XIOS_CF_Longitude_units.find(unit));
816    }
817  }
818
819} // namespace xios
Note: See TracBrowser for help on using the repository browser.