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

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

Allowing each process to retrieve its own distribution with i_index

+) Read value and data from unstructured grid with provided i_index value

Test
+) On Curie
+) test_remap works

  • 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: 17.2 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->isCurvilinear(fieldId))
235    {
236      int ni = domain->ni;
237      int nj = domain->nj;
238      std::vector<StdSize> nBeginLatLon(2), nSizeLatLon(2);
239      nBeginLatLon[0] = domain->jbegin.getValue(); nBeginLatLon[1] = domain->ibegin.getValue();
240      nSizeLatLon[0]  = nj; nSizeLatLon[1] = ni;
241
242      StdString latName = this->getLatCoordName(fieldId);
243      domain->latvalue_2d.resize(ni,nj);
244      readFieldVariableValue(domain->latvalue_2d, latName, nBeginLatLon, nSizeLatLon);
245      StdString lonName = this->getLonCoordName(fieldId);
246      domain->lonvalue_2d.resize(ni,nj);
247      readFieldVariableValue(domain->lonvalue_2d, lonName, nBeginLatLon, nSizeLatLon);
248
249      StdString boundsLatName = this->getBoundsId(latName);
250      if (0 == boundsLatName.compare(""))
251         ERROR("CNc4DataInput::readDomainAttributeValueFromFile(...)",
252              << "Field '" << fieldId << std::endl
253              << "Trying to read attributes from curvilinear grid."
254              << "Latitude variable " << latName << " does not have bounds.");
255      StdString boundsLonName = this->getBoundsId(lonName);
256      if (0 == boundsLonName.compare(""))
257         ERROR("CNc4DataInput::readDomainAttributeValueFromFile(...)",
258              << "Field '" << fieldId << std::endl
259              << "Trying to read attributes from curvilinear grid."
260              << "Longitude variable " << lonName << " does not have bounds.");
261
262      int nbVertex = this->getNbVertex(fieldId);
263      domain->nvertex.setValue(nbVertex);
264      std::vector<StdSize> nBeginBndsLatLon(3), nSizeBndsLatLon(3);
265      nBeginBndsLatLon[0] = domain->jbegin.getValue(); nSizeBndsLatLon[0] = nj;
266      nBeginBndsLatLon[1] = domain->ibegin.getValue(); nSizeBndsLatLon[1] = ni;
267      nBeginBndsLatLon[2] = 0; nSizeBndsLatLon[2] = nbVertex;
268
269      domain->bounds_lat_2d.resize(nbVertex,ni,nj);
270      readFieldVariableValue(domain->bounds_lat_2d, boundsLatName, nBeginBndsLatLon, nSizeBndsLatLon);
271      domain->bounds_lon_2d.resize(nbVertex,ni,nj);
272      readFieldVariableValue(domain->bounds_lon_2d, boundsLonName, nBeginBndsLatLon, nSizeBndsLatLon);
273    }
274    else if (this->isUnstructured(fieldId))
275    {
276      if (domain->i_index.isEmpty())
277         ERROR("CNc4DataInput::readDomainAttributeValueFromFile(...)",
278              << "Field '" << fieldId << std::endl
279              << "Trying to read attributes from unstructured grid."
280              << "i_index of domain" << domain->getId() << " is mandatory");
281
282      int ni = domain->i_index.numElements();
283      std::vector<StdSize> nBeginLatLon(1,0), nSizeLatLon(1,0);
284      nSizeLatLon[0]  = domain->ni_glo.getValue();
285      CArray<double,1> globalLonLat(domain->ni_glo.getValue());
286
287      StdString latName = this->getLatCoordName(fieldId);
288      readFieldVariableValue(globalLonLat, latName, nBeginLatLon, nSizeLatLon);
289      domain->latvalue_1d.resize(ni);
290      for (int idx = 0; idx < ni; ++idx)
291        domain->latvalue_1d(idx) =  globalLonLat(domain->i_index(idx));
292
293      StdString lonName = this->getLonCoordName(fieldId);
294      readFieldVariableValue(globalLonLat, lonName, nBeginLatLon, nSizeLatLon);
295      domain->lonvalue_1d.resize(ni);
296      for (int idx = 0; idx < ni; ++idx)
297        domain->lonvalue_1d(idx) = globalLonLat(domain->i_index(idx));
298
299      StdString boundsLatName = this->getBoundsId(latName);
300      if (0 == boundsLatName.compare(""))
301         ERROR("CNc4DataInput::readDomainAttributeValueFromFile(...)",
302              << "Field '" << fieldId << std::endl
303              << "Trying to read attributes from unstructured grid."
304              << "Latitude variable " << latName << " does not have bounds.");
305      StdString boundsLonName = this->getBoundsId(lonName);
306      if (0 == boundsLonName.compare(""))
307         ERROR("CNc4DataInput::readDomainAttributeValueFromFile(...)",
308              << "Field '" << fieldId << std::endl
309              << "Trying to read attributes from unstructured grid."
310              << "Longitude variable " << lonName << " does not have bounds.");
311
312      int nbVertex = this->getNbVertex(fieldId);
313      domain->nvertex.setValue(nbVertex);
314      std::vector<StdSize> nBeginBndsLatLon(2), nSizeBndsLatLon(2);
315      nBeginBndsLatLon[0] = 0; nSizeBndsLatLon[0] = domain->ni_glo.getValue();
316      nBeginBndsLatLon[1] = 0; nSizeBndsLatLon[1] = nbVertex;
317
318      CArray<double,2> globalBndsLonLat(nSizeBndsLatLon[1], nSizeBndsLatLon[0]);
319      readFieldVariableValue(globalBndsLonLat, boundsLatName, nBeginBndsLatLon, nSizeBndsLatLon);
320      domain->bounds_lat_1d.resize(nbVertex,ni);
321      for (int idx = 0; idx < ni; ++idx)
322        for (int jdx = 0; jdx < nbVertex; ++jdx)
323          domain->bounds_lat_1d(jdx,idx) = globalBndsLonLat(jdx, domain->i_index(idx));
324
325      readFieldVariableValue(globalBndsLonLat, boundsLonName, nBeginBndsLatLon, nSizeBndsLatLon);
326      domain->bounds_lon_1d.resize(nbVertex,ni);
327      for (int idx = 0; idx < ni; ++idx)
328        for (int jdx = 0; jdx < nbVertex; ++jdx)
329          domain->bounds_lon_1d(jdx,idx) = globalBndsLonLat(jdx, domain->i_index(idx));
330    }
331  }
332
333  /*!
334    Read attribute value of a domain from a file
335    \param [in] domain domain whose attributes are read from the file
336    \param [in] dimSizeMap Dimensions and and their corresponding names and size read from file
337    \param [in] emelentPosition position of domain in grid
338    \param [in] fieldId id (or name) associated with the grid
339  */
340  void CNc4DataInput::readDomainAttributesFromFile(CDomain* domain, std::list<std::pair<StdString, StdSize> >& dimSizeMap,
341                                                   int elementPosition, const StdString& fieldId)
342  {
343    // There are some mandatory attributes of a domain to retrieve from file
344    // + ni_glo, nj_glo
345    std::list<std::pair<StdString, StdSize> >::const_iterator itMapNi = dimSizeMap.begin(), itMapNj,
346                                                              iteMap  = dimSizeMap.end();
347    for (int i = 0; i < elementPosition; ++i, ++itMapNi) {}
348    itMapNj = itMapNi; ++itMapNj;
349
350    if (this->isRectilinear(fieldId) || this->isCurvilinear(fieldId))
351    {
352      domain->nj_glo.setValue(itMapNj->second);
353      domain->ni_glo.setValue(itMapNi->second);
354    }
355    else if (this->isUnstructured(fieldId))
356    {
357      domain->nj_glo.setValue(1);
358      domain->ni_glo.setValue(itMapNi->second);
359    }
360  }
361
362  /*!
363    Read attributes of an axis from a file
364    \param [in] axis axis whose attributes are read from the file
365    \param [in] dimSizeMap Dimensions and and their corresponding names and size read from file
366    \param [in] emelentPosition position of axis in grid
367    \param [in] fieldId id (or name) associated with the grid
368  */
369  void CNc4DataInput::readAxisAttributesFromFile(CAxis* axis, std::list<std::pair<StdString, StdSize> >& dimSizeMap,
370                                                 int elementPosition, const StdString& fieldId)
371  {
372    std::list<std::pair<StdString, StdSize> >::const_iterator itMapN = dimSizeMap.begin(),
373                                                              iteMap = dimSizeMap.end();
374    for (int i = 0; i < elementPosition; ++i, ++itMapN) {}
375    axis->n_glo.setValue(itMapN->second);
376  }
377
378  /*!
379    Read attribute value of an axis from a file
380    \param [in] axis axis whose attributes are read from the file
381    \param [in] dimSizeMap Dimensions and and their corresponding names and size read from file
382    \param [in] emelentPosition position of axis in grid
383    \param [in] fieldId id (or name) associated with the grid
384  */
385  void CNc4DataInput::readAxisAttributeValueFromFile(CAxis* axis, std::list<std::pair<StdString, StdSize> >& dimSizeMap,
386                                                    int elementPosition, const StdString& fieldId)
387  {
388    std::list<std::pair<StdString, StdSize> >::const_iterator itMapN = dimSizeMap.begin(),
389                                                              iteMap = dimSizeMap.end();
390    for (int i = 0; i < elementPosition; ++i, ++itMapN) {}
391
392    { // Read axis value
393      std::vector<StdSize> nBegin(1, 0), nSize(1, itMapN->second);
394      CArray<double,1> readAxisValue(itMapN->second);
395      readFieldVariableValue(readAxisValue, itMapN->first, nBegin, nSize, true);
396      int begin = 0, n = itMapN->second;
397      if (!axis->begin.isEmpty()) begin = axis->begin.getValue();
398      if (!axis->n.isEmpty()) n = axis->n.getValue();
399      axis->value.resize(n);
400      for (int i = 0; i < n; ++i) axis->value(i) = readAxisValue(begin + i);
401    }
402  }
403
404  void CNc4DataInput::closeFile_(void)
405  {
406    SuperClassWriter::close();
407  }
408} // namespace xios
Note: See TracBrowser for help on using the repository browser.