source: XIOS2/trunk/src/io/nc4_data_input.cpp @ 2477

Last change on this file since 2477 was 2477, checked in by jderouillat, 16 months ago

Fix readFieldAttributes for fields not defined on a domain

  • 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.3 KB
Line 
1#include "nc4_data_input.hpp"
2
3#include "context.hpp"
4#include "context_server.hpp"
5#include "context_client.hpp"
6#include "domain.hpp"
7#include "axis.hpp"
8#include "scalar.hpp"
9#include "type.hpp"
10
11namespace xios
12{
13  CNc4DataInput::CNc4DataInput(const StdString& filename, MPI_Comm comm_file, bool multifile, bool isCollective /*= true*/,
14                               bool readMetaDataPar /*= false*/, bool ugridConvention /*= false*/, const StdString& timeCounterName /*= "time_counter"*/)
15    : SuperClass()
16    , SuperClassWriter(filename, &comm_file, multifile, readMetaDataPar, timeCounterName)
17    , comm_file(comm_file)
18    , filename(filename)
19    , isCollective(isCollective)
20    , ugridConvention(ugridConvention)
21    , readMetaDataDomains_(), readValueDomains_()
22    , readMetaDataAxis_(), readValueAxis_()
23    , readMetaDataScalar_(), readValueScalar_()
24  {
25    SuperClass::type = multifile ? MULTI_FILE : ONE_FILE;
26  }
27
28  CNc4DataInput::~CNc4DataInput(void)
29  { /* Nothing more to do */ }
30
31  StdSize CNc4DataInput::getFieldNbRecords_(CField* field)
32  TRY
33  {
34    StdString fieldId = field->getFieldOutputName();
35
36    if (SuperClassWriter::isTemporal(fieldId))
37    {
38      return SuperClassWriter::getDimensions(&fieldId)[SuperClassWriter::getTimeCounterName()];
39    }
40
41    return 1;
42  }
43  CATCH
44
45  void CNc4DataInput::readFieldData_(CField* field)
46  TRY
47  {
48    CContext* context = CContext::getCurrent();
49    CContextServer* server = context->server;
50
51    CGrid* grid = field->grid;
52
53    if (!grid->doGridHaveDataToWrite())
54      if (SuperClass::type==MULTI_FILE || !isCollective) return;
55
56    StdString fieldId = field->getFieldOutputName();
57
58    CArray<double,1> fieldData(grid->getWrittenDataSize());
59    if (!field->default_value.isEmpty()) fieldData = field->default_value;
60
61    switch (SuperClass::type)
62    {
63      case MULTI_FILE:
64        SuperClassWriter::getData(fieldData, fieldId, isCollective, (field->getNStep() - 1)%field->nstepMax );
65        break;
66      case ONE_FILE:
67      {
68        std::vector<StdSize> start, count;
69
70        CArray<int,1> axisDomainOrder = grid->axis_domain_order;
71        std::vector<StdString> domainList = grid->getDomainList();
72        std::vector<StdString> axisList   = grid->getAxisList();
73        int numElement = axisDomainOrder.numElements();
74        int idxDomain = domainList.size() - 1, idxAxis = axisList.size() - 1;
75        int idx = domainList.size() * 2 + axisList.size() - 1;
76
77        start.reserve(idx+1);
78        count.reserve(idx+1);
79
80        for (int i = numElement - 1; i >= 0; --i)
81        {
82          if (2 == axisDomainOrder(i))
83          {
84            CDomain* domain = CDomain::get(domainList[idxDomain]);
85            if ((domain->type) != CDomain::type_attr::unstructured)
86            {
87              start.push_back(domain->jbegin);
88              count.push_back(domain->nj);
89            }
90            start.push_back(domain->ibegin);
91            count.push_back(domain->ni);
92            --idxDomain;
93          }
94          else if (1 == axisDomainOrder(i))
95          {
96            CAxis* axis = CAxis::get(axisList[idxAxis]);
97            start.push_back(axis->begin);
98            count.push_back(axis->n);
99            --idxAxis ;
100          }
101          else
102          {
103            if (1 == axisDomainOrder.numElements())
104            {
105              start.push_back(0);
106              count.push_back(1);
107            }
108          }
109        }
110
111        SuperClassWriter::getData(fieldData, fieldId, isCollective, (field->getNStep() - 1)%field->nstepMax, &start, &count);
112        break;
113      }
114    }
115
116    field->inputField(fieldData);
117
118    if (!field->scale_factor.isEmpty() || !field->add_offset.isEmpty())
119    {
120      double scaleFactor = 1.0, addOffset = 0.0;
121      if (!field->scale_factor.isEmpty()) scaleFactor = field->scale_factor;
122      if (!field->add_offset.isEmpty()) addOffset = field->add_offset;
123      field->invertScaleFactorAddOffset(scaleFactor, addOffset);
124    }
125  }
126  CATCH
127
128  void CNc4DataInput::readFieldAttributes_(CField* field, bool readAttributeValues)
129  TRY
130  {
131    StdString fieldId = field->getFieldOutputName();
132
133    CGrid* grid = field->grid;
134
135    std::vector<CDomain*> domainP = grid->getDomains();
136    std::vector<CAxis*> axisP = grid->getAxis();
137    std::vector<CScalar*> scalarP = grid->getScalars();
138    int gridDim = domainP.size() * 2 + axisP.size();
139
140    // Nothing to do with scalar without timestep
141    if ((0 == gridDim) && (!SuperClassWriter::isTemporal(fieldId))) 
142      return;
143
144    // Verify the compatibility of dimension of declared grid and real grid in file
145    int realGridDim = 1;
146    bool isUnstructuredGrid(true);
147    bool domainNotExplicitelyDefined(false);
148    for (int idom=0;idom<domainP.size();idom++)
149    {
150      if ( (!domainP[idom]->type.isEmpty())&& (domainP[idom]->type.getValue()!=CDomain::type_attr::unstructured))
151      {
152        isUnstructuredGrid = false;
153      }
154      else if (domainP[idom]->type.isEmpty())
155      {
156        domainNotExplicitelyDefined = true;
157      }
158    }
159    if ((domainNotExplicitelyDefined)||(domainP.size()==0))
160    {
161      isUnstructuredGrid = ((gridDim < 2) ? false :  SuperClassWriter::isUnstructured(fieldId));
162    }
163    std::map<StdString, StdSize> dimSizeMap = SuperClassWriter::getDimensions(&fieldId);
164    std::list<StdString> dimList = SuperClassWriter::getDimensionsList(&fieldId);
165
166    realGridDim = SuperClassWriter::isTemporal(fieldId) ? dimSizeMap.size() - 1 : dimSizeMap.size();
167    if (isUnstructuredGrid) ++realGridDim;
168
169    if (gridDim != realGridDim)
170       ERROR("CNc4DataInput::readFieldAttributes_(CField* field, bool readAttributeValues)",
171        << "Field '" << fieldId << "' has incorrect dimension " << std::endl
172        << "Verify dimension of grid defined by 'grid_ref' or 'domain_ref'/'axis_ref' and dimension of grid in read file.");
173
174    // Remove unlimited dimension from the map, we dont need it anymore
175    if (SuperClassWriter::isTemporal(fieldId))
176    {
177      dimSizeMap.erase(SuperClassWriter::getTimeCounterName());
178      dimList.pop_front() ;  // assume time dimension is first
179    }
180
181    std::list<std::pair<StdString, StdSize> > listDimSize;
182/*
183    for (std::map<StdString, StdSize>::const_iterator itMap = dimSizeMap.begin(); itMap != dimSizeMap.end(); ++itMap)
184      listDimSize.push_front(*itMap);
185*/
186
187//    if (!SuperClassWriter::isRectilinear(fieldId))
188    if (true)
189    {
190      for (std::list<StdString>::const_iterator it = dimList.begin(); it != dimList.end(); ++it)
191        listDimSize.push_front(*dimSizeMap.find(*it));
192    }
193    else
194    {
195       std::list<StdString> coords = SuperClassWriter::getCoordinatesIdList(fieldId);
196       std::list<StdString>::const_iterator itCoord = coords.begin();
197       for (; itCoord != coords.end(); itCoord++)
198       {
199         const StdString& coord = *itCoord;
200         if (SuperClassWriter::hasVariable(coord) && !SuperClassWriter::isTemporal(coord))
201         {
202           std::map<StdString, StdSize> dimsTmp = SuperClassWriter::getDimensions(&coord);
203           StdString dimNameTmp = dimsTmp.begin()->first;
204           StdSize dimSizeTmp = dimsTmp.begin()->second;
205           listDimSize.push_front(make_pair(coord, dimSizeTmp));
206           dimSizeMap.erase(dimNameTmp);
207           dimList.remove(dimNameTmp);
208         }
209       }
210       for (std::list<StdString>::const_iterator it = dimList.begin(); it != dimList.end(); ++it)
211        listDimSize.push_front(*dimSizeMap.find(*it));
212    }
213   
214    // read specific field attribute
215    if (field->add_offset.isEmpty())
216    {
217      if (SuperClassWriter::hasAttribute<float>("add_offset",&fieldId)) 
218        field->add_offset =  SuperClassWriter::getAttributeValue<float>("add_offset",&fieldId)[0] ;
219      else if (SuperClassWriter::hasAttribute<double>("add_offset",&fieldId))
220        field->add_offset =  SuperClassWriter::getAttributeValue<double>("add_offset",&fieldId)[0] ;
221    }
222
223    if (field->scale_factor.isEmpty())
224    {
225      if (SuperClassWriter::hasAttribute<float>("scale_factor",&fieldId)) 
226        field->scale_factor =  SuperClassWriter::getAttributeValue<float>("scale_factor",&fieldId)[0] ;
227      else if (SuperClassWriter::hasAttribute<double>("scale_factor",&fieldId))
228        field->scale_factor =  SuperClassWriter::getAttributeValue<double>("scale_factor",&fieldId)[0] ;
229    }
230
231    // Now process domain and axis
232    CArray<int,1> axisDomainOrder = grid->axis_domain_order;
233    int numElement = domainP.size() + axisP.size() + scalarP.size();
234    int elementPosition = 0;
235    int idxDomain = 0, idxAxis = 0, idxScalar = 0;
236
237    std::pair<std::set<StdString>::iterator,bool> it;
238    for (int i = 0; i < numElement; ++i)
239    {
240      if(2 == axisDomainOrder(i))
241      {
242        if (readAttributeValues)
243        {
244           it = readValueDomains_.insert(domainP[idxDomain]->getId());
245           if (it.second) readDomainAttributeValueFromFile(domainP[idxDomain], listDimSize, elementPosition, fieldId);
246        }
247        else
248        {
249          it = readMetaDataDomains_.insert(domainP[idxDomain]->getId());
250          if (it.second) readDomainAttributesFromFile(domainP[idxDomain], listDimSize, elementPosition, fieldId);
251        }
252        ++idxDomain;
253        if (isUnstructuredGrid) ++elementPosition;
254        else elementPosition += 2;
255      }
256      else if (1 == axisDomainOrder(i))
257      {
258        if (readAttributeValues)
259        {
260          it = readValueAxis_.insert(axisP[idxAxis]->getId());
261          if (it.second) readAxisAttributeValueFromFile(axisP[idxAxis], listDimSize, elementPosition, fieldId);
262        }
263        else
264        {
265          it = readMetaDataAxis_.insert(axisP[idxAxis]->getId());
266          if (it.second) readAxisAttributesFromFile(axisP[idxAxis], listDimSize, elementPosition, fieldId);
267        }
268        ++idxAxis;
269        ++elementPosition;
270      }
271      else
272      {
273        if (readAttributeValues)
274        {
275          it = readValueScalar_.insert(scalarP[idxScalar]->getId());
276          if (it.second) readScalarAttributeValueFromFile(scalarP[idxScalar], listDimSize, elementPosition, fieldId);
277        }
278        else
279        {
280          it = readMetaDataScalar_.insert(scalarP[idxScalar]->getId());
281          if (it.second) readScalarAttributesFromFile(scalarP[idxScalar], listDimSize, elementPosition, fieldId);
282        }
283        ++idxScalar;
284        ++elementPosition;
285      }
286    }
287  }
288  CATCH
289
290  /*!
291    Read attributes of a domain from a file
292    \param [in] domain domain whose attributes are read from the file
293    \param [in] dimSizeMap Dimensions and and their corresponding names and size read from file
294    \param [in] emelentPosition position of domain in grid
295    \param [in] fieldId id (or name) associated with the grid
296  */
297  void CNc4DataInput::readDomainAttributeValueFromFile(CDomain* domain, std::list<std::pair<StdString, StdSize> >& dimSizeMap,
298                                                       int elementPosition, const StdString& fieldId)
299  TRY
300  {
301    // There are some optional attributes of a domain to retrieve from file    // + lon lat?
302    std::list<std::pair<StdString, StdSize> >::const_iterator itMapNi = dimSizeMap.begin(), itMapNj,
303                                                              iteMap  = dimSizeMap.end();
304
305    for (int i = 0; i < elementPosition; ++i, ++itMapNi) {}
306    itMapNj = itMapNi; ++itMapNj;
307
308    if ((CDomain::type_attr::rectilinear == domain->type))
309    {
310      // Ok, try to read some attributes such as longitude and latitude
311      bool hasLat = SuperClassWriter::hasVariable(itMapNj->first);
312      if (hasLat)
313      {
314        domain->latvalue_rectilinear_read_from_file.resize(itMapNj->second);
315        std::vector<StdSize> nBeginLat(1, 0), nSizeLat(1, itMapNj->second);
316        readFieldVariableValue(domain->latvalue_rectilinear_read_from_file, itMapNj->first, nBeginLat, nSizeLat, true);
317      }
318
319      bool hasLon = SuperClassWriter::hasVariable(itMapNi->first);
320      if (hasLon)
321      {
322        domain->lonvalue_rectilinear_read_from_file.resize(itMapNi->second);
323        std::vector<StdSize> nBeginLon(1, 0), nSizeLon(1, itMapNi->second);
324        readFieldVariableValue(domain->lonvalue_rectilinear_read_from_file, itMapNi->first, nBeginLon, nSizeLon, true);
325      }     
326    }
327    else if ((CDomain::type_attr::curvilinear == domain->type))
328    {
329      // Make sure that if there is no local domain defined on a process, the process still reads just one value.
330      int ni, nj, ibegin, jbegin;
331      if (domain->ni == 0)
332      {
333        ni = 1;
334        ibegin = 0;
335      }
336      else
337      {
338        ni = domain->ni;
339        ibegin = domain->ibegin;
340      }
341      if (domain->nj == 0)
342      {
343        nj = 1;
344        jbegin = 0;
345      }
346      else
347      {
348        nj = domain->nj;
349        jbegin = domain->jbegin;
350      }
351
352      std::vector<StdSize> nBeginLatLon(2), nSizeLatLon(2);
353      nBeginLatLon[0] = jbegin; nBeginLatLon[1] = ibegin;
354      nSizeLatLon[0]  = nj; nSizeLatLon[1] = ni;
355
356      StdString latName = this->getLatCoordName(fieldId);
357      if (SuperClassWriter::hasVariable(latName))
358      {
359        domain->latvalue_curvilinear_read_from_file.resize(ni, nj);
360        readFieldVariableValue(domain->latvalue_curvilinear_read_from_file, latName, nBeginLatLon, nSizeLatLon);
361      }
362      StdString lonName = this->getLonCoordName(fieldId);
363      if (SuperClassWriter::hasVariable(lonName))
364      {
365        domain->lonvalue_curvilinear_read_from_file.resize(ni, nj);
366        readFieldVariableValue(domain->lonvalue_curvilinear_read_from_file, lonName, nBeginLatLon, nSizeLatLon);
367      }
368
369      StdString boundsLatName = this->getBoundsId(latName);
370      StdString boundsLonName = this->getBoundsId(lonName);
371
372      int nbVertex = 4; //this->getNbVertex(fieldId);
373      if (!domain->nvertex.isEmpty() && (domain->nvertex != nbVertex))
374      {
375        ERROR("void CNc4DataInput::readDomainAttributeValueFromFile(...)",
376          << "The domain " << domain->getDomainOutputName()
377          << " has nvertex read from file " << this->filename << " and nvertex provided from model"
378          << " are not coherent. They should be the same." << std::endl
379          << " nvertex read from file: "<< nbVertex
380          << " nvertex from model: "<< domain->nvertex << std::endl);
381      } 
382
383      if (SuperClassWriter::hasVariable(boundsLatName) || SuperClassWriter::hasVariable(boundsLonName)) 
384        domain->nvertex.setValue(nbVertex);
385
386      std::vector<StdSize> nBeginBndsLatLon(3), nSizeBndsLatLon(3);
387      nBeginBndsLatLon[0] = jbegin; nSizeBndsLatLon[0] = nj;
388      nBeginBndsLatLon[1] = ibegin; nSizeBndsLatLon[1] = ni;
389      nBeginBndsLatLon[2] = 0; nSizeBndsLatLon[2] = nbVertex;
390
391      if (SuperClassWriter::hasVariable(boundsLatName))
392      {
393        domain->bounds_latvalue_curvilinear_read_from_file.resize(nbVertex, ni, nj);
394        readFieldVariableValue(domain->bounds_latvalue_curvilinear_read_from_file, boundsLatName, nBeginBndsLatLon, nSizeBndsLatLon);
395
396      }
397      if (SuperClassWriter::hasVariable(boundsLonName)) 
398      {
399        domain->bounds_lonvalue_curvilinear_read_from_file.resize(nbVertex, ni, nj);
400        readFieldVariableValue(domain->bounds_lonvalue_curvilinear_read_from_file, boundsLonName, nBeginBndsLatLon, nSizeBndsLatLon);
401      }     
402    }
403    else if ((CDomain::type_attr::unstructured == domain->type))// || (this->isUnstructured(fieldId)))
404    {
405      // Make sure that if there is no local domain defined on a process, the process still reads just one value.
406      int ni, ibegin;
407      if (domain->ni == 0)
408      {
409        ni = 1;
410        ibegin = 0;
411      }
412      else
413      {
414        ni = domain->ni;
415        ibegin = domain->ibegin;
416      }
417
418      std::vector<StdSize> nBeginLatLon(1,0), nSizeLatLon(1,0);
419      nBeginLatLon[0] = ibegin;
420      nSizeLatLon[0]  = ni;
421
422      StdString latName = this->getLatCoordName(fieldId);
423      if (SuperClassWriter::hasVariable(latName))
424      {
425        domain->latvalue_unstructured_read_from_file.resize(ni);
426        readFieldVariableValue(domain->latvalue_unstructured_read_from_file, latName, nBeginLatLon, nSizeLatLon); 
427      }
428
429      StdString lonName = this->getLonCoordName(fieldId);
430      if (SuperClassWriter::hasVariable(lonName)) //(0 != lonName.compare(""))
431      {
432        domain->lonvalue_unstructured_read_from_file.resize(ni);
433        readFieldVariableValue(domain->lonvalue_unstructured_read_from_file, lonName, nBeginLatLon, nSizeLatLon);
434      }
435
436      StdString boundsLatName = this->getBoundsId(latName);
437      StdString boundsLonName = this->getBoundsId(lonName);
438
439      if (ugridConvention && domain->nvertex.isEmpty())
440      {
441        ERROR("void CNc4DataInput::readDomainAttributeValueFromFile(...)",
442          << " Attribute nvertex must be specified for domain " << domain->getDomainOutputName()
443          << " read from UGRID file " << this->filename << " ."<< std::endl);
444      }
445//      int nbVertex = this->getNbVertex(fieldId);
446      int nbVertex = (ugridConvention) ? domain->nvertex : this->getNbVertex(fieldId);
447      if (!domain->nvertex.isEmpty() && (domain->nvertex != nbVertex))
448      {
449        ERROR("void CNc4DataInput::readDomainAttributeValueFromFile(...)",
450          << "The domain " << domain->getDomainOutputName()
451          << " has nvertex read from file " << this->filename << " and nvertex provided from model"
452          << " are not coherent. They should be the same." << std::endl
453          << " nvertex read from file: "<< nbVertex
454          << " nvertex from model: "<< domain->nvertex << std::endl);
455      } 
456     
457      if (SuperClassWriter::hasVariable(boundsLatName) || SuperClassWriter::hasVariable(boundsLonName)) 
458        domain->nvertex.setValue(nbVertex);
459
460      std::vector<StdSize> nBeginBndsLatLon(2), nSizeBndsLatLon(2);
461      nBeginBndsLatLon[0] = ibegin; nSizeBndsLatLon[0] = ni;
462      nBeginBndsLatLon[1] = 0; nSizeBndsLatLon[1] = nbVertex;
463
464      if (SuperClassWriter::hasVariable(boundsLatName)) 
465      {
466        domain->bounds_latvalue_unstructured_read_from_file.resize(nSizeBndsLatLon[1], nSizeBndsLatLon[0]);
467        readFieldVariableValue(domain->bounds_latvalue_unstructured_read_from_file, boundsLatName, nBeginBndsLatLon, nSizeBndsLatLon);
468      }
469
470      if (SuperClassWriter::hasVariable(boundsLonName)) 
471      {
472        domain->bounds_lonvalue_unstructured_read_from_file.resize(nSizeBndsLatLon[1], nSizeBndsLatLon[0]);
473        readFieldVariableValue(domain->bounds_lonvalue_unstructured_read_from_file, boundsLonName, nBeginBndsLatLon, nSizeBndsLatLon);
474      }     
475    }
476    domain->fillInLonLat();
477  }
478  CATCH
479
480  /*!
481    Read attribute value of a domain from a file
482    \param [in] domain domain whose attributes are read from the file
483    \param [in] dimSizeMap Dimensions and and their corresponding names and size read from file
484    \param [in] emelentPosition position of domain in grid
485    \param [in] fieldId id (or name) associated with the grid
486  */
487  void CNc4DataInput::readDomainAttributesFromFile(CDomain* domain, std::list<std::pair<StdString, StdSize> >& dimSizeMap,
488                                                   int elementPosition, const StdString& fieldId)
489  TRY
490  {
491    // There are some mandatory attributes of a domain to retrieve from file
492    // + ni_glo, nj_glo
493    std::list<std::pair<StdString, StdSize> >::const_iterator itMapNi = dimSizeMap.begin(), itMapNj,
494                                                              iteMap  = dimSizeMap.end();
495    for (int i = 0; i < elementPosition; ++i, ++itMapNi) {}
496    itMapNj = itMapNi; ++itMapNj;
497
498    if (CDomain::type_attr::rectilinear == domain->type || CDomain::type_attr::curvilinear == domain->type ||
499        this->isRectilinear(fieldId) || this->isCurvilinear(fieldId))
500    {
501      if (!domain->nj_glo.isEmpty() && (domain->nj_glo != itMapNj->second))
502      {
503        ERROR("void CNc4DataInput::readDomainAttributesFromFile(...)",
504          << "The domain " << domain->getDomainOutputName()
505          << " has nj_glo read from file " << this->filename << " and nj_glo provided from model"
506          << " are not coherent. They should be the same." << std::endl
507          << " nj_glo read from file: "<< itMapNj->second
508          << " nj_glo from model: "<< domain->nj_glo << std::endl);
509      } 
510      domain->nj_glo.setValue(itMapNj->second);
511
512      if (!domain->ni_glo.isEmpty() && (domain->ni_glo != itMapNi->second))
513      {
514        ERROR("void CNc4DataInput::readDomainAttributesFromFile(...)",
515          << "The domain " << domain->getDomainOutputName()
516          << " has ni_glo read from file " << this->filename << " and ni_glo provided from model"
517          << " are not coherent. They should be the same." << std::endl
518          << " ni_glo read from file: "<< itMapNi->second
519          << " ni_glo from model: "<< domain->ni_glo << std::endl);
520      } 
521      domain->ni_glo.setValue(itMapNi->second);
522    }
523    else if (CDomain::type_attr::unstructured == domain->type|| this->isUnstructured(fieldId))
524    {
525      domain->nj_glo.setValue(1);
526
527      if (!domain->ni_glo.isEmpty() && (domain->ni_glo != itMapNi->second))
528      {
529        ERROR("void CNc4DataInput::readDomainAttributesFromFile(...)",
530          << "The domain " << domain->getDomainOutputName()
531          << " has ni_glo read from file " << this->filename << " and ni_glo provided from model"
532          << " are not coherent. They should be the same." << std::endl
533          << " ni_glo read from file: "<< itMapNi->second
534          << " ni_glo from model: "<< domain->ni_glo << std::endl);
535      }       
536      domain->ni_glo.setValue(itMapNi->second);
537    }
538
539// determine if coordinates values are present in file
540    if ((CDomain::type_attr::rectilinear == domain->type))
541    {
542      // Ok, try to read some attributes such as longitude and latitude
543      domain->hasLatInReadFile_ = SuperClassWriter::hasVariable(itMapNj->first);
544      domain->hasLonInReadFile_  = SuperClassWriter::hasVariable(itMapNi->first);
545    }
546    else if ((CDomain::type_attr::curvilinear == domain->type) || (CDomain::type_attr::unstructured == domain->type) )
547    {
548      StdString latName = this->getLatCoordName(fieldId);
549      domain->hasLatInReadFile_ = SuperClassWriter::hasVariable(latName) ;
550      StdString lonName = this->getLonCoordName(fieldId);       
551      domain->hasLonInReadFile_ = SuperClassWriter::hasVariable(lonName) ; 
552      StdString boundsLatName = this->getBoundsId(latName);
553      domain->hasBoundsLatInReadFile_ = SuperClassWriter::hasVariable(boundsLatName) ; 
554      StdString boundsLonName = this->getBoundsId(lonName);
555      domain->hasBoundsLonInReadFile_ = SuperClassWriter::hasVariable(boundsLonName) ;
556    }
557  }
558  CATCH
559
560  /*!
561    Read attributes of an axis from a file
562    \param [in] axis axis whose attributes are read from the file
563    \param [in] dimSizeMap Dimensions and and their corresponding names and size read from file
564    \param [in] emelentPosition position of axis in grid
565    \param [in] fieldId id (or name) associated with the grid
566  */
567  void CNc4DataInput::readAxisAttributesFromFile(CAxis* axis, std::list<std::pair<StdString, StdSize> >& dimSizeMap,
568                                                 int elementPosition, const StdString& fieldId)
569  TRY
570  {
571    std::list<std::pair<StdString, StdSize> >::const_iterator itMapN = dimSizeMap.begin(),
572                                                              iteMap = dimSizeMap.end();
573    for (int i = 0; i < elementPosition; ++i, ++itMapN) {}
574
575    if (!axis->n_glo.isEmpty() && (axis->n_glo != itMapN->second))
576    {
577      ERROR("void CNc4DataInput::readAxisAttributesFromFile(...)",
578        << "The axis " << axis->getAxisOutputName()
579        << " has n_glo read from file " << this->filename << " and n_glo provided from model"
580        << " are not coherent. They should be the same." << std::endl
581        << " n_glo read from file: "<< itMapN->second
582        << " n_glo from model: "<< axis->n_glo << std::endl);
583    }   
584    axis->n_glo.setValue(itMapN->second);
585  }
586  CATCH
587
588  /*!
589    Read attribute value of an axis from a file
590    \param [in] axis axis whose attributes are read from the file
591    \param [in] dimSizeMap Dimensions and and their corresponding names and size read from file
592    \param [in] emelentPosition position of axis in grid
593    \param [in] fieldId id (or name) associated with the grid
594  */
595  void CNc4DataInput::readAxisAttributeValueFromFile(CAxis* axis, std::list<std::pair<StdString, StdSize> >& dimSizeMap,
596                                                    int elementPosition, const StdString& fieldId)
597  TRY
598  {
599    std::list<std::pair<StdString, StdSize> >::const_iterator itMapN = dimSizeMap.begin(),
600                                                              iteMap = dimSizeMap.end();
601    for (int i = 0; i < elementPosition; ++i, ++itMapN) {}
602
603    { // Read axis value
604      bool hasValue = SuperClassWriter::hasVariable(itMapN->first);
605      if (hasValue)
606      {
607        std::vector<StdSize> nBegin(1, 0), nSize(1, itMapN->second);
608        CArray<double,1> readAxisValue(itMapN->second);
609        readFieldVariableValue(readAxisValue, itMapN->first, nBegin, nSize, true);
610        int begin = 0, n = itMapN->second;
611        if (!axis->begin.isEmpty()) begin = axis->begin.getValue();
612        if (!axis->n.isEmpty()) n = axis->n.getValue();
613        axis->value.resize(n);
614        double convertFromFactor=1.0 ;
615        if (!axis->convert_from_factor.isEmpty()) convertFromFactor = axis->convert_from_factor ;
616        for (int i = 0; i < n; ++i) axis->value(i) = readAxisValue(begin + i)*convertFromFactor;
617      }
618    }
619  }
620  CATCH
621
622  /*!
623    Read attributes of a scalar from a file
624    \param [in] scalar scalar whose attributes are read from the file
625    \param [in] dimSizeMap Dimensions and and their corresponding names and size read from file
626    \param [in] emelentPosition position of scalar in grid
627    \param [in] fieldId id (or name) associated with the grid
628  */
629  void CNc4DataInput::readScalarAttributesFromFile(CScalar* scalar, std::list<std::pair<StdString, StdSize> >& dimSizeMap,
630                                                  int elementPosition, const StdString& fieldId)
631  {
632    /*Nothing to do */
633  }
634
635  /*!
636    Read attribute value of an axis from a file
637    \param [in] axis axis whose attributes are read from the file
638    \param [in] dimSizeMap Dimensions and and their corresponding names and size read from file
639    \param [in] emelentPosition position of axis in grid
640    \param [in] fieldId id (or name) associated with the grid
641  */
642  void CNc4DataInput::readScalarAttributeValueFromFile(CScalar* scalar, std::list<std::pair<StdString, StdSize> >& dimSizeMap,
643                                                      int elementPosition, const StdString& fieldId)
644  {
645    /*Nothing to do */
646  }
647
648  void CNc4DataInput::closeFile_(void)
649  TRY
650  {
651    SuperClassWriter::close();
652  }
653  CATCH
654} // namespace xios
Note: See TracBrowser for help on using the repository browser.