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

Last change on this file since 887 was 887, checked in by mhnguyen, 5 years ago

Adding a new type of element into grid: Scalar

+) Add a new node Scalar for xml
+) Make some change on writing scalar value
+) Reorganize some codes
+) Remove some redundant codes

Test
+) On Curie
+) All tests pass

  • 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.5 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
9namespace xios
10{
11  CNc4DataInput::CNc4DataInput(const StdString& filename, MPI_Comm comm_file, bool multifile, bool isCollective /*= true*/, const StdString& timeCounterName /*= "time_counter"*/)
12    : SuperClass()
13    , SuperClassWriter(filename, &comm_file, multifile, timeCounterName)
14    , comm_file(comm_file)
15    , filename(filename)
16    , isCollective(isCollective)
17    , readMetaDataDomains_(), readValueDomains_()
18    , readMetaDataAxis_(), readValueAxis_()
19  {
20    SuperClass::type = multifile ? MULTI_FILE : ONE_FILE;
21  }
22
23  CNc4DataInput::~CNc4DataInput(void)
24  { /* Nothing more to do */ }
25
26  StdSize CNc4DataInput::getFieldNbRecords_(CField* field)
27  {
28    StdString fieldId = field->getFieldOutputName();
29
30    if (SuperClassWriter::isTemporal(fieldId))
31    {
32//      return SuperClassWriter::getDimensions(&fieldId)[SuperClassWriter::getUnlimitedDimensionName()];
33      return SuperClassWriter::getDimensions(&fieldId)[SuperClassWriter::getTimeCounterName()];
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
49    StdString fieldId = field->getFieldOutputName();
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)%field->nstepMax );
58        break;
59      case ONE_FILE:
60      {
61/*
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        }
76*/
77
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<int,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 (2 == 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 if (1 == axisDomainOrder(i))
111          {
112            start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
113            count.push_back(nZoomSizeServer[idx]);
114            --idx;
115           }
116        }
117
118        SuperClassWriter::getData(fieldData, fieldId, isCollective, (field->getNStep() - 1)%field->nstepMax, &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
134  void CNc4DataInput::readFieldAttributes_(CField* field, bool readAttributeValues)
135  {
136    StdString fieldId = field->getFieldOutputName();
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;
146    bool isUnstructuredGrid = SuperClassWriter::isUnstructured(fieldId);
147    std::map<StdString, StdSize> dimSizeMap = SuperClassWriter::getDimensions(&fieldId);
148    std::list<StdString> dimList = SuperClassWriter::getDimensionsList(&fieldId);
149
150    realGridDim = SuperClassWriter::isTemporal(fieldId) ? dimSizeMap.size() - 1 : dimSizeMap.size();
151    if (isUnstructuredGrid) ++realGridDim;
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
159    if (SuperClassWriter::isTemporal(fieldId))
160    {
161      dimSizeMap.erase(SuperClassWriter::getUnlimitedDimensionName());
162      dimList.pop_front() ;  // assume time dimension is first
163    }
164
165    int mapSize = dimSizeMap.size() - 1;
166    std::list<std::pair<StdString, StdSize> > listDimSize;
167/*
168    for (std::map<StdString, StdSize>::const_iterator itMap = dimSizeMap.begin(); itMap != dimSizeMap.end(); ++itMap)
169      listDimSize.push_front(*itMap);
170*/
171    for (std::list<StdString>::const_iterator it = dimList.begin(); it != dimList.end(); ++it)
172      listDimSize.push_front(*dimSizeMap.find(*it));
173
174    // Now process domain and axis
175    CArray<int,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(2 == axisDomainOrder(i))
184      {
185        if (readAttributeValues)
186        {
187           it = readValueDomains_.insert(domainP[idxDomain]->getId());
188           if (it.second) readDomainAttributeValueFromFile(domainP[idxDomain], listDimSize, elementPosition, fieldId);
189        }
190        else
191        {
192          it = readMetaDataDomains_.insert(domainP[idxDomain]->getId());
193          if (it.second) readDomainAttributesFromFile(domainP[idxDomain], listDimSize, elementPosition, fieldId);
194        }
195        ++idxDomain;
196        if (isUnstructuredGrid) ++elementPosition;
197        else elementPosition += 2;
198      }
199      else if (1 == axisDomainOrder(i))
200      {
201        if (readAttributeValues)
202        {
203          it = readValueAxis_.insert(axisP[idxAxis]->getId());
204          if (it.second) readAxisAttributeValueFromFile(axisP[idxAxis], listDimSize, elementPosition, fieldId);
205        }
206        else
207        {
208          it = readMetaDataAxis_.insert(axisP[idxAxis]->getId());
209          if (it.second) readAxisAttributesFromFile(axisP[idxAxis], listDimSize, elementPosition, fieldId);
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
222    \param [in] fieldId id (or name) associated with the grid
223  */
224  void CNc4DataInput::readDomainAttributeValueFromFile(CDomain* domain, std::list<std::pair<StdString, StdSize> >& dimSizeMap,
225                                                       int elementPosition, const StdString& fieldId)
226  {
227    // There are some optional attributes of a domain to retrieve from file    // + lon lat?
228    std::list<std::pair<StdString, StdSize> >::const_iterator itMapNi = dimSizeMap.begin(), itMapNj,
229                                                              iteMap  = dimSizeMap.end();
230
231    for (int i = 0; i < elementPosition; ++i, ++itMapNi) {}
232    itMapNj = itMapNi; ++itMapNj;
233
234    if ((CDomain::type_attr::rectilinear == domain->type))// || this->isRectilinear(fieldId))
235    {
236      // Ok, try to read some f.. attributes such as longitude and latitude
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      }
244
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      }
252      domain->fillInRectilinearLonLat();
253    }
254    else if ((CDomain::type_attr::curvilinear == domain->type))// || (this->isCurvilinear(fieldId)))
255    {
256      int ni = domain->ni;
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;
261
262      StdString latName = this->getLatCoordName(fieldId);
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      }
268      StdString lonName = this->getLonCoordName(fieldId);
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      }
274
275      StdString boundsLatName = this->getBoundsId(latName);
276      StdString boundsLonName = this->getBoundsId(lonName);
277
278      int nbVertex = this->getNbVertex(fieldId);
279      if (SuperClassWriter::hasVariable(boundsLatName) || SuperClassWriter::hasVariable(boundsLonName)) //if ((0 != boundsLatName.compare("")) || (0 != boundsLonName.compare("")))
280        domain->nvertex.setValue(nbVertex);
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;
285
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      }
297    }
298    else if ((CDomain::type_attr::unstructured == domain->type))// || (this->isUnstructured(fieldId)))
299    {
300      /*
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");
306
307      int ni = domain->i_index.numElements();
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      }
316
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
321      StdString latName = this->getLatCoordName(fieldId);
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      }
329
330      StdString lonName = this->getLonCoordName(fieldId);
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      }
338
339      StdString boundsLatName = this->getBoundsId(latName);
340      StdString boundsLonName = this->getBoundsId(lonName);
341
342      int nbVertex = this->getNbVertex(fieldId);
343      if (SuperClassWriter::hasVariable(boundsLatName) || SuperClassWriter::hasVariable(boundsLonName)) // (0 != boundsLatName.compare("")) || (0 != boundsLonName.compare("")))
344        domain->nvertex.setValue(nbVertex);
345
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;
349
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      }
359
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      }
369    }
370  }
371
372  /*!
373    Read attribute value of a domain from a file
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
377    \param [in] fieldId id (or name) associated with the grid
378  */
379  void CNc4DataInput::readDomainAttributesFromFile(CDomain* domain, std::list<std::pair<StdString, StdSize> >& dimSizeMap,
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
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;
388
389    if (this->isRectilinear(fieldId) || this->isCurvilinear(fieldId))
390    {
391      domain->nj_glo.setValue(itMapNj->second);
392      domain->ni_glo.setValue(itMapNi->second);
393    }
394    else if (this->isUnstructured(fieldId))
395    {
396      domain->nj_glo.setValue(1);
397      domain->ni_glo.setValue(itMapNi->second);
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
406    \param [in] fieldId id (or name) associated with the grid
407  */
408  void CNc4DataInput::readAxisAttributesFromFile(CAxis* axis, std::list<std::pair<StdString, StdSize> >& dimSizeMap,
409                                                 int elementPosition, const StdString& fieldId)
410  {
411    std::list<std::pair<StdString, StdSize> >::const_iterator itMapN = dimSizeMap.begin(),
412                                                              iteMap = dimSizeMap.end();
413    for (int i = 0; i < elementPosition; ++i, ++itMapN) {}
414    axis->n_glo.setValue(itMapN->second);
415  }
416
417  /*!
418    Read attribute value of an axis from a file
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
422    \param [in] fieldId id (or name) associated with the grid
423  */
424  void CNc4DataInput::readAxisAttributeValueFromFile(CAxis* axis, std::list<std::pair<StdString, StdSize> >& dimSizeMap,
425                                                    int elementPosition, const StdString& fieldId)
426  {
427    std::list<std::pair<StdString, StdSize> >::const_iterator itMapN = dimSizeMap.begin(),
428                                                              iteMap = dimSizeMap.end();
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
443  void CNc4DataInput::closeFile_(void)
444  {
445    SuperClassWriter::close();
446  }
447} // namespace xios
Note: See TracBrowser for help on using the repository browser.