source: XIOS/trunk/src/io/nc4_data_input.cpp @ 827

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

Relaxing constraints on reading attributes from a file.

+) If there is no coordinate (lon, lat) in file, there wont be error anymore.
It's user who will provide these info.

Test
+) On Curie
+) Correct on test_remap

  • 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: 18.4 KB
RevLine 
[219]1#include "nc4_data_input.hpp"
2
[599]3#include "context.hpp"
4#include "context_server.hpp"
[775]5#include "context_client.hpp"
6#include "domain.hpp"
7#include "axis.hpp"
[599]8
[335]9namespace xios
[219]10{
[802]11  CNc4DataInput::CNc4DataInput(const StdString& filename, MPI_Comm comm_file, bool multifile, bool isCollective /*= true*/, const StdString& timeCounterName /*= "time_counter"*/)
[599]12    : SuperClass()
[802]13    , SuperClassWriter(filename, &comm_file, multifile, timeCounterName)
[599]14    , comm_file(comm_file)
15    , filename(filename)
16    , isCollective(isCollective)
[775]17    , readMetaDataDomains_(), readValueDomains_()
18    , readMetaDataAxis_(), readValueAxis_()
[599]19  {
20    SuperClass::type = multifile ? MULTI_FILE : ONE_FILE;
21  }
[219]22
[599]23  CNc4DataInput::~CNc4DataInput(void)
24  { /* Nothing more to do */ }
[219]25
[599]26  StdSize CNc4DataInput::getFieldNbRecords_(CField* field)
27  {
[770]28    StdString fieldId = field->getFieldOutputName();
[599]29
30    if (SuperClassWriter::isTemporal(fieldId))
31    {
[811]32//      return SuperClassWriter::getDimensions(&fieldId)[SuperClassWriter::getUnlimitedDimensionName()];
33      return SuperClassWriter::getDimensions(&fieldId)[SuperClassWriter::getTimeCounterName()];
[599]34    }
35
36    return 1;
37  }
38
39  void CNc4DataInput::readFieldData_(CField* field)
40  {
41    CContext* context = CContext::getCurrent();
42    CContextServer* server = context->server;
43
44    CGrid* grid = field->grid;
45
46    if (!grid->doGridHaveDataToWrite())
47      if (SuperClass::type==MULTI_FILE || !isCollective) return;
48
[770]49    StdString fieldId = field->getFieldOutputName();
[599]50
51    CArray<double,1> fieldData(grid->getWrittenDataSize());
52    if (!field->default_value.isEmpty()) fieldData = field->default_value;
53
54    switch (SuperClass::type)
55    {
56      case MULTI_FILE:
57        SuperClassWriter::getData(fieldData, fieldId, isCollective, field->getNStep() - 1);
58        break;
59      case ONE_FILE:
60      {
[765]61/*
[599]62        std::vector<int> nZoomBeginGlobal = grid->getDistributionServer()->getZoomBeginGlobal();
63        std::vector<int> nZoomBeginServer = grid->getDistributionServer()->getZoomBeginServer();
64        std::vector<int> nZoomSizeServer  = grid->getDistributionServer()->getZoomSizeServer();
65
66        int ssize = nZoomBeginGlobal.size();
67
68        std::vector<StdSize> start(ssize);
69        std::vector<StdSize> count(ssize);
70
71        for (int i = 0; i < ssize; ++i)
72        {
73          start[i] = nZoomBeginServer[ssize - i - 1] - nZoomBeginGlobal[ssize - i - 1];
74          count[i] = nZoomSizeServer[ssize - i - 1];
75        }
[765]76*/
[599]77
[765]78        std::vector<int> nZoomBeginGlobal = grid->getDistributionServer()->getZoomBeginGlobal();
79        std::vector<int> nZoomBeginServer = grid->getDistributionServer()->getZoomBeginServer();
80        std::vector<int> nZoomSizeServer  = grid->getDistributionServer()->getZoomSizeServer();
81
82        std::vector<StdSize> start, count;
83
84        CArray<bool,1> axisDomainOrder = grid->axis_domain_order;
85        std::vector<StdString> domainList = grid->getDomainList();
86        std::vector<StdString> axisList   = grid->getAxisList();
87        int numElement = axisDomainOrder.numElements();
88        int idxDomain = domainList.size() - 1, idxAxis = axisList.size() - 1;
89        int idx = nZoomBeginGlobal.size() - 1;
90
91        start.reserve(nZoomBeginGlobal.size());
92        count.reserve(nZoomBeginGlobal.size());
93
94        for (int i = numElement - 1; i >= 0; --i)
95        {
96          if (axisDomainOrder(i))
97          {
98            CDomain* domain = CDomain::get(domainList[idxDomain]);
99            if ((domain->type) != CDomain::type_attr::unstructured)
100            {
101              start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
102              count.push_back(nZoomSizeServer[idx]);
103            }
104            --idx ;
105            start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
106            count.push_back(nZoomSizeServer[idx]);
107            --idx ;
108            --idxDomain;
109          }
110          else
111          {
112            start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
113            count.push_back(nZoomSizeServer[idx]);
114            --idx;
115           }
116        }
[777]117
[599]118        SuperClassWriter::getData(fieldData, fieldId, isCollective, field->getNStep() - 1, &start, &count);
119        break;
120      }
121    }
122
123    field->inputField(fieldData);
124
125    if (!field->scale_factor.isEmpty() || !field->add_offset.isEmpty())
126    {
127      double scaleFactor = 1.0, addOffset = 0.0;
128      if (!field->scale_factor.isEmpty()) scaleFactor = field->scale_factor;
129      if (!field->add_offset.isEmpty()) addOffset = field->add_offset;
130      field->invertScaleFactorAddOffset(scaleFactor, addOffset);
131    }
132  }
133
[775]134  void CNc4DataInput::readFieldAttributes_(CField* field, bool readAttributeValues)
135  {
[777]136    StdString fieldId = field->getFieldOutputName();
[775]137
138    CGrid* grid = field->grid;
139
140    std::vector<CDomain*> domainP = grid->getDomains();
141    std::vector<CAxis*> axisP = grid->getAxis();
142    int gridDim = domainP.size() * 2 + axisP.size();
143
144    // Verify the compatibility of dimension of declared grid and real grid in file
145    int realGridDim = 1;
[783]146    bool isUnstructuredGrid = SuperClassWriter::isUnstructured(fieldId);
[775]147    std::map<StdString, StdSize> dimSizeMap = SuperClassWriter::getDimensions(&fieldId);
[807]148    std::list<StdString> dimList = SuperClassWriter::getDimensionsList(&fieldId);
[825]149
[775]150    realGridDim = SuperClassWriter::isTemporal(fieldId) ? dimSizeMap.size() - 1 : dimSizeMap.size();
[783]151    if (isUnstructuredGrid) ++realGridDim;
[775]152
153    if (gridDim != realGridDim)
154       ERROR("CNc4DataInput::readFieldAttributes_(CField* field, bool readAttributeValues)",
155        << "Field '" << fieldId << "' has incorrect dimension " << std::endl
156        << "Verify dimension of grid defined by 'grid_ref' or 'domain_ref'/'axis_ref' and dimension of grid in read file.");
157
158    // Remove unlimited dimension from the map, we dont need it anymore
[825]159    if (SuperClassWriter::isTemporal(fieldId))
[807]160    {
161      dimSizeMap.erase(SuperClassWriter::getUnlimitedDimensionName());
162      dimList.pop_front() ;  // assume time dimension is first
163    }
[825]164
[775]165    int mapSize = dimSizeMap.size() - 1;
[783]166    std::list<std::pair<StdString, StdSize> > listDimSize;
[807]167/*
[783]168    for (std::map<StdString, StdSize>::const_iterator itMap = dimSizeMap.begin(); itMap != dimSizeMap.end(); ++itMap)
169      listDimSize.push_front(*itMap);
[807]170*/
171    for (std::list<StdString>::const_iterator it = dimList.begin(); it != dimList.end(); ++it)
172      listDimSize.push_front(*dimSizeMap.find(*it));
[775]173
174    // Now process domain and axis
175    CArray<bool,1> axisDomainOrder = grid->axis_domain_order;
176    int numElement = domainP.size() + axisP.size();
177    int elementPosition = 0;
178    int idxDomain = 0, idxAxis = 0;
179
180    std::pair<std::set<StdString>::iterator,bool> it;
181    for (int i = 0; i < numElement; ++i)
182    {
183      if(axisDomainOrder(i))
184      {
185        if (readAttributeValues)
186        {
187           it = readValueDomains_.insert(domainP[idxDomain]->getId());
[783]188           if (it.second) readDomainAttributeValueFromFile(domainP[idxDomain], listDimSize, elementPosition, fieldId);
[775]189        }
190        else
191        {
192          it = readMetaDataDomains_.insert(domainP[idxDomain]->getId());
[783]193          if (it.second) readDomainAttributesFromFile(domainP[idxDomain], listDimSize, elementPosition, fieldId);
[775]194        }
195        ++idxDomain;
[783]196        if (isUnstructuredGrid) ++elementPosition;
197        else elementPosition += 2;
[775]198      }
199      else
200      {
201        if (readAttributeValues)
202        {
203          it = readValueAxis_.insert(axisP[idxAxis]->getId());
[783]204          if (it.second) readAxisAttributeValueFromFile(axisP[idxAxis], listDimSize, elementPosition, fieldId);
[775]205        }
206        else
207        {
208          it = readMetaDataAxis_.insert(axisP[idxAxis]->getId());
[783]209          if (it.second) readAxisAttributesFromFile(axisP[idxAxis], listDimSize, elementPosition, fieldId);
[775]210        }
211        ++idxAxis;
212        ++elementPosition;
213      }
214    }
215  }
216
217  /*!
218    Read attributes of a domain from a file
219    \param [in] domain domain whose attributes are read from the file
220    \param [in] dimSizeMap Dimensions and and their corresponding names and size read from file
221    \param [in] emelentPosition position of domain in grid
[782]222    \param [in] fieldId id (or name) associated with the grid
[775]223  */
[783]224  void CNc4DataInput::readDomainAttributeValueFromFile(CDomain* domain, std::list<std::pair<StdString, StdSize> >& dimSizeMap,
[775]225                                                       int elementPosition, const StdString& fieldId)
226  {
227    // There are some optional attributes of a domain to retrieve from file    // + lon lat?
[783]228    std::list<std::pair<StdString, StdSize> >::const_iterator itMapNi = dimSizeMap.begin(), itMapNj,
229                                                              iteMap  = dimSizeMap.end();
[775]230
[783]231    for (int i = 0; i < elementPosition; ++i, ++itMapNi) {}
232    itMapNj = itMapNi; ++itMapNj;
[775]233
[825]234    if ((CDomain::type_attr::rectilinear == domain->type))// || this->isRectilinear(fieldId))
[775]235    {
236      // Ok, try to read some f.. attributes such as longitude and latitude
[825]237      bool hasLat = SuperClassWriter::hasVariable(itMapNj->first);
238      if (hasLat)
239      {
240        domain->latvalue_rectilinear_read_from_file.resize(itMapNj->second);
241        std::vector<StdSize> nBeginLat(1, 0), nSizeLat(1, itMapNj->second);
242        readFieldVariableValue(domain->latvalue_rectilinear_read_from_file, itMapNj->first, nBeginLat, nSizeLat, true);
243      }
[775]244
[825]245      bool hasLon = SuperClassWriter::hasVariable(itMapNi->first);
246      if (hasLon)
247      {
248        domain->lonvalue_rectilinear_read_from_file.resize(itMapNi->second);
249        std::vector<StdSize> nBeginLon(1, 0), nSizeLon(1, itMapNi->second);
250        readFieldVariableValue(domain->lonvalue_rectilinear_read_from_file, itMapNi->first, nBeginLon, nSizeLon, true);
251      }
[775]252      domain->fillInRectilinearLonLat();
253    }
[825]254    else if ((CDomain::type_attr::curvilinear == domain->type))// || (this->isCurvilinear(fieldId)))
[783]255    {
256      int ni = domain->ni;
[785]257      int nj = domain->nj;
258      std::vector<StdSize> nBeginLatLon(2), nSizeLatLon(2);
259      nBeginLatLon[0] = domain->jbegin.getValue(); nBeginLatLon[1] = domain->ibegin.getValue();
260      nSizeLatLon[0]  = nj; nSizeLatLon[1] = ni;
[783]261
262      StdString latName = this->getLatCoordName(fieldId);
[825]263      if (SuperClassWriter::hasVariable(latName)) //(0 != latName.compare(""))
264      {
265        domain->latvalue_2d.resize(ni,nj);
266        readFieldVariableValue(domain->latvalue_2d, latName, nBeginLatLon, nSizeLatLon);
267      }
[783]268      StdString lonName = this->getLonCoordName(fieldId);
[825]269      if (SuperClassWriter::hasVariable(lonName)) //(0 != lonName.compare(""))
270      {
271        domain->lonvalue_2d.resize(ni,nj);
272        readFieldVariableValue(domain->lonvalue_2d, lonName, nBeginLatLon, nSizeLatLon);
273      }
[783]274
275      StdString boundsLatName = this->getBoundsId(latName);
276      StdString boundsLonName = this->getBoundsId(lonName);
277
278      int nbVertex = this->getNbVertex(fieldId);
[825]279      if (SuperClassWriter::hasVariable(boundsLatName) || SuperClassWriter::hasVariable(boundsLonName)) //if ((0 != boundsLatName.compare("")) || (0 != boundsLonName.compare("")))
280        domain->nvertex.setValue(nbVertex);
[785]281      std::vector<StdSize> nBeginBndsLatLon(3), nSizeBndsLatLon(3);
282      nBeginBndsLatLon[0] = domain->jbegin.getValue(); nSizeBndsLatLon[0] = nj;
283      nBeginBndsLatLon[1] = domain->ibegin.getValue(); nSizeBndsLatLon[1] = ni;
284      nBeginBndsLatLon[2] = 0; nSizeBndsLatLon[2] = nbVertex;
[783]285
[825]286      if (SuperClassWriter::hasVariable(boundsLatName)) //(0 != boundsLatName.compare(""))
287      {
288        domain->bounds_lat_2d.resize(nbVertex,ni,nj);
289        readFieldVariableValue(domain->bounds_lat_2d, boundsLatName, nBeginBndsLatLon, nSizeBndsLatLon);
290
291      }
292      if (SuperClassWriter::hasVariable(boundsLonName)) //(0 != boundsLonName.compare(""))
293      {
294        domain->bounds_lon_2d.resize(nbVertex,ni,nj);
295        readFieldVariableValue(domain->bounds_lon_2d, boundsLonName, nBeginBndsLatLon, nSizeBndsLatLon);
296      }
[783]297    }
[825]298    else if ((CDomain::type_attr::unstructured == domain->type))// || (this->isUnstructured(fieldId)))
[775]299    {
[807]300      /*
[785]301      if (domain->i_index.isEmpty())
302         ERROR("CNc4DataInput::readDomainAttributeValueFromFile(...)",
303              << "Field '" << fieldId << std::endl
304              << "Trying to read attributes from unstructured grid."
305              << "i_index of domain" << domain->getId() << " is mandatory");
[825]306
[785]307      int ni = domain->i_index.numElements();
[807]308*/
309      int ni     = domain->ni;
310      int ibegin = domain->ibegin;
311      if (domain->i_index.isEmpty())
312      {
313        domain->i_index.resize(ni) ;
314        for(int idx = 0; idx < ni; ++idx) domain->i_index(idx)=ibegin+idx ;
315      }
[825]316
[785]317      std::vector<StdSize> nBeginLatLon(1,0), nSizeLatLon(1,0);
318      nSizeLatLon[0]  = domain->ni_glo.getValue();
319      CArray<double,1> globalLonLat(domain->ni_glo.getValue());
320
[782]321      StdString latName = this->getLatCoordName(fieldId);
[825]322      if (SuperClassWriter::hasVariable(latName)) //(0 != latName.compare(""))
323      {
324        readFieldVariableValue(globalLonLat, latName, nBeginLatLon, nSizeLatLon);
325        domain->latvalue_1d.resize(ni);
326        for (int idx = 0; idx < ni; ++idx)
327          domain->latvalue_1d(idx) =  globalLonLat(domain->i_index(idx));
328      }
[785]329
[782]330      StdString lonName = this->getLonCoordName(fieldId);
[825]331      if (SuperClassWriter::hasVariable(lonName)) //(0 != lonName.compare(""))
332      {
333        readFieldVariableValue(globalLonLat, lonName, nBeginLatLon, nSizeLatLon);
334        domain->lonvalue_1d.resize(ni);
335        for (int idx = 0; idx < ni; ++idx)
336          domain->lonvalue_1d(idx) = globalLonLat(domain->i_index(idx));
337      }
[782]338
339      StdString boundsLatName = this->getBoundsId(latName);
340      StdString boundsLonName = this->getBoundsId(lonName);
341
[783]342      int nbVertex = this->getNbVertex(fieldId);
[825]343      if (SuperClassWriter::hasVariable(boundsLatName) || SuperClassWriter::hasVariable(boundsLonName)) // (0 != boundsLatName.compare("")) || (0 != boundsLonName.compare("")))
344        domain->nvertex.setValue(nbVertex);
345
[785]346      std::vector<StdSize> nBeginBndsLatLon(2), nSizeBndsLatLon(2);
347      nBeginBndsLatLon[0] = 0; nSizeBndsLatLon[0] = domain->ni_glo.getValue();
348      nBeginBndsLatLon[1] = 0; nSizeBndsLatLon[1] = nbVertex;
[782]349
[825]350      if (SuperClassWriter::hasVariable(boundsLatName)) //(0 != boundsLatName.compare(""))
351      {
352        CArray<double,2> globalBndsLonLat(nSizeBndsLatLon[1], nSizeBndsLatLon[0]);
353        readFieldVariableValue(globalBndsLonLat, boundsLatName, nBeginBndsLatLon, nSizeBndsLatLon);
354        domain->bounds_lat_1d.resize(nbVertex,ni);
355        for (int idx = 0; idx < ni; ++idx)
356          for (int jdx = 0; jdx < nbVertex; ++jdx)
357            domain->bounds_lat_1d(jdx,idx) = globalBndsLonLat(jdx, domain->i_index(idx));
358      }
[785]359
[825]360      if (SuperClassWriter::hasVariable(boundsLonName)) //(0 != boundsLonName.compare(""))
361      {
362        CArray<double,2> globalBndsLonLat(nSizeBndsLatLon[1], nSizeBndsLatLon[0]);
363        readFieldVariableValue(globalBndsLonLat, boundsLonName, nBeginBndsLatLon, nSizeBndsLatLon);
364        domain->bounds_lon_1d.resize(nbVertex,ni);
365        for (int idx = 0; idx < ni; ++idx)
366          for (int jdx = 0; jdx < nbVertex; ++jdx)
367            domain->bounds_lon_1d(jdx,idx) = globalBndsLonLat(jdx, domain->i_index(idx));
368      }
[775]369    }
370  }
371
372  /*!
[782]373    Read attribute value of a domain from a file
[775]374    \param [in] domain domain whose attributes are read from the file
375    \param [in] dimSizeMap Dimensions and and their corresponding names and size read from file
376    \param [in] emelentPosition position of domain in grid
[782]377    \param [in] fieldId id (or name) associated with the grid
[775]378  */
[783]379  void CNc4DataInput::readDomainAttributesFromFile(CDomain* domain, std::list<std::pair<StdString, StdSize> >& dimSizeMap,
[775]380                                                   int elementPosition, const StdString& fieldId)
381  {
382    // There are some mandatory attributes of a domain to retrieve from file
383    // + ni_glo, nj_glo
[783]384    std::list<std::pair<StdString, StdSize> >::const_iterator itMapNi = dimSizeMap.begin(), itMapNj,
385                                                              iteMap  = dimSizeMap.end();
386    for (int i = 0; i < elementPosition; ++i, ++itMapNi) {}
387    itMapNj = itMapNi; ++itMapNj;
[775]388
[782]389    if (this->isRectilinear(fieldId) || this->isCurvilinear(fieldId))
[775]390    {
391      domain->nj_glo.setValue(itMapNj->second);
[783]392      domain->ni_glo.setValue(itMapNi->second);
[775]393    }
394    else if (this->isUnstructured(fieldId))
395    {
[783]396      domain->nj_glo.setValue(1);
397      domain->ni_glo.setValue(itMapNi->second);
[775]398    }
399  }
400
401  /*!
402    Read attributes of an axis from a file
403    \param [in] axis axis whose attributes are read from the file
404    \param [in] dimSizeMap Dimensions and and their corresponding names and size read from file
405    \param [in] emelentPosition position of axis in grid
[782]406    \param [in] fieldId id (or name) associated with the grid
[775]407  */
[783]408  void CNc4DataInput::readAxisAttributesFromFile(CAxis* axis, std::list<std::pair<StdString, StdSize> >& dimSizeMap,
[775]409                                                 int elementPosition, const StdString& fieldId)
410  {
[783]411    std::list<std::pair<StdString, StdSize> >::const_iterator itMapN = dimSizeMap.begin(),
412                                                              iteMap = dimSizeMap.end();
[775]413    for (int i = 0; i < elementPosition; ++i, ++itMapN) {}
414    axis->n_glo.setValue(itMapN->second);
415  }
416
417  /*!
[782]418    Read attribute value of an axis from a file
[775]419    \param [in] axis axis whose attributes are read from the file
420    \param [in] dimSizeMap Dimensions and and their corresponding names and size read from file
421    \param [in] emelentPosition position of axis in grid
[782]422    \param [in] fieldId id (or name) associated with the grid
[775]423  */
[783]424  void CNc4DataInput::readAxisAttributeValueFromFile(CAxis* axis, std::list<std::pair<StdString, StdSize> >& dimSizeMap,
[775]425                                                    int elementPosition, const StdString& fieldId)
426  {
[783]427    std::list<std::pair<StdString, StdSize> >::const_iterator itMapN = dimSizeMap.begin(),
428                                                              iteMap = dimSizeMap.end();
[775]429    for (int i = 0; i < elementPosition; ++i, ++itMapN) {}
430
431    { // Read axis value
432      std::vector<StdSize> nBegin(1, 0), nSize(1, itMapN->second);
433      CArray<double,1> readAxisValue(itMapN->second);
434      readFieldVariableValue(readAxisValue, itMapN->first, nBegin, nSize, true);
435      int begin = 0, n = itMapN->second;
436      if (!axis->begin.isEmpty()) begin = axis->begin.getValue();
437      if (!axis->n.isEmpty()) n = axis->n.getValue();
438      axis->value.resize(n);
439      for (int i = 0; i < n; ++i) axis->value(i) = readAxisValue(begin + i);
440    }
441  }
442
[599]443  void CNc4DataInput::closeFile_(void)
444  {
445    SuperClassWriter::close();
446  }
[335]447} // namespace xios
Note: See TracBrowser for help on using the repository browser.