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

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

Adding attribute reading of unstructured grid

+) Fix a minor bug relating to unstructured grid detection
+) Add attribute reading for unstructured domain

Test
+) On Curie
+) test_remap passes

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