Ignore:
Timestamp:
11/02/15 11:46:25 (8 years ago)
Author:
mhnguyen
Message:

Implementing the reading of attributes of an axis from a file

+) 3d grid can be read directly from a file
+) Clean some redundant codes
+) Add new attribute declaration that allows to output only desired attributes

Test
+) On Curie
+) test_remap passes and result is correct

File:
1 edited

Legend:

Unmodified
Added
Removed
  • XIOS/trunk/src/io/nc4_data_input.cpp

    r770 r775  
    33#include "context.hpp" 
    44#include "context_server.hpp" 
     5#include "context_client.hpp" 
     6#include "domain.hpp" 
     7#include "axis.hpp" 
    58 
    69namespace xios 
     
    1215    , filename(filename) 
    1316    , isCollective(isCollective) 
     17    , readMetaDataDomains_(), readValueDomains_() 
     18    , readMetaDataAxis_(), readValueAxis_() 
    1419  { 
    1520    SuperClass::type = multifile ? MULTI_FILE : ONE_FILE; 
     
    126131  } 
    127132 
     133  void CNc4DataInput::readFieldAttributes_(CField* field, bool readAttributeValues) 
     134  { 
     135    StdString fieldId = !field->name.isEmpty() ? field->name.getValue() : field->getBaseFieldReference()->getId(); 
     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    std::map<StdString, StdSize> dimSizeMap = SuperClassWriter::getDimensions(&fieldId); 
     146    realGridDim = SuperClassWriter::isTemporal(fieldId) ? dimSizeMap.size() - 1 : dimSizeMap.size(); 
     147 
     148    if (gridDim != realGridDim) 
     149       ERROR("CNc4DataInput::readFieldAttributes_(CField* field, bool readAttributeValues)", 
     150        << "Field '" << fieldId << "' has incorrect dimension " << std::endl 
     151        << "Verify dimension of grid defined by 'grid_ref' or 'domain_ref'/'axis_ref' and dimension of grid in read file."); 
     152 
     153    // Remove unlimited dimension from the map, we dont need it anymore 
     154    if (SuperClassWriter::isTemporal(fieldId)) dimSizeMap.erase(SuperClassWriter::getUnlimitedDimensionName()); 
     155    int mapSize = dimSizeMap.size() - 1; 
     156 
     157    // Now process domain and axis 
     158    CArray<bool,1> axisDomainOrder = grid->axis_domain_order; 
     159    int numElement = domainP.size() + axisP.size(); 
     160    int elementPosition = 0; 
     161    int idxDomain = 0, idxAxis = 0; 
     162 
     163    std::pair<std::set<StdString>::iterator,bool> it; 
     164    for (int i = 0; i < numElement; ++i) 
     165    { 
     166      if(axisDomainOrder(i)) 
     167      { 
     168        if (readAttributeValues) 
     169        { 
     170           it = readValueDomains_.insert(domainP[idxDomain]->getId()); 
     171           if (it.second) readDomainAttributeValueFromFile(domainP[idxDomain], dimSizeMap, mapSize - 1 - elementPosition, fieldId); 
     172        } 
     173        else 
     174        { 
     175          it = readMetaDataDomains_.insert(domainP[idxDomain]->getId()); 
     176          if (it.second) readDomainAttributesFromFile(domainP[idxDomain], dimSizeMap, mapSize - 1 - elementPosition, fieldId); 
     177        } 
     178        ++idxDomain; 
     179        elementPosition += 2; 
     180      } 
     181      else 
     182      { 
     183        if (readAttributeValues) 
     184        { 
     185          it = readValueAxis_.insert(axisP[idxAxis]->getId()); 
     186          if (it.second) readAxisAttributeValueFromFile(axisP[idxAxis], dimSizeMap, mapSize - elementPosition, fieldId); 
     187        } 
     188        else 
     189        { 
     190          it = readMetaDataAxis_.insert(axisP[idxAxis]->getId()); 
     191          if (it.second) readAxisAttributesFromFile(axisP[idxAxis], dimSizeMap, mapSize - elementPosition, fieldId); 
     192        } 
     193        ++idxAxis; 
     194        ++elementPosition; 
     195      } 
     196    } 
     197  } 
     198 
     199  /*! 
     200    Read attributes of a domain from a file 
     201    \param [in] domain domain whose attributes are read from the file 
     202    \param [in] dimSizeMap Dimensions and and their corresponding names and size read from file 
     203    \param [in] emelentPosition position of domain in grid 
     204  */ 
     205  void CNc4DataInput::readDomainAttributeValueFromFile(CDomain* domain, std::map<StdString, StdSize>& dimSizeMap, 
     206                                                       int elementPosition, const StdString& fieldId) 
     207  { 
     208    // There are some optional attributes of a domain to retrieve from file    // + lon lat? 
     209    std::map<StdString, StdSize>::const_iterator itMapNj = dimSizeMap.begin(), itMapNi, 
     210                                                 iteMap  = dimSizeMap.end(); 
     211 
     212    for (int i = 0; i < elementPosition; ++i, ++itMapNj) {} 
     213    itMapNi = itMapNj; ++itMapNi; 
     214 
     215    if (this->isRectilinear(fieldId)) 
     216    { 
     217      // Ok, try to read some f.. attributes such as longitude and latitude 
     218      domain->latvalue_rectilinear_read_from_file.resize(itMapNj->second); 
     219      std::vector<StdSize> nBeginLat(1, 0), nSizeLat(1, itMapNj->second); 
     220      readFieldVariableValue(domain->latvalue_rectilinear_read_from_file, itMapNj->first, nBeginLat, nSizeLat, true); 
     221 
     222      domain->lonvalue_rectilinear_read_from_file.resize(itMapNi->second); 
     223      std::vector<StdSize> nBeginLon(1, 0), nSizeLon(1, itMapNi->second); 
     224      readFieldVariableValue(domain->lonvalue_rectilinear_read_from_file, itMapNi->first, nBeginLon, nSizeLon, true); 
     225      domain->fillInRectilinearLonLat(); 
     226    } 
     227    else if (this->isCurvilinear(fieldId)) 
     228    { 
     229 
     230    } 
     231    else if (this->isUnstructured(fieldId)) 
     232    { 
     233 
     234    } 
     235  } 
     236 
     237  /*! 
     238    Read attributes of a domain from a file 
     239    \param [in] domain domain whose attributes are read from the file 
     240    \param [in] dimSizeMap Dimensions and and their corresponding names and size read from file 
     241    \param [in] emelentPosition position of domain in grid 
     242  */ 
     243  void CNc4DataInput::readDomainAttributesFromFile(CDomain* domain, std::map<StdString, StdSize>& dimSizeMap, 
     244                                                   int elementPosition, const StdString& fieldId) 
     245  { 
     246    // There are some mandatory attributes of a domain to retrieve from file 
     247    // + ni_glo, nj_glo 
     248    std::map<StdString, StdSize>::const_iterator itMapNj = dimSizeMap.begin(), itMapNi, 
     249                                                 iteMap  = dimSizeMap.end(); 
     250    for (int i = 0; i < elementPosition; ++i, ++itMapNj) {} 
     251    itMapNi = itMapNj; ++itMapNi; 
     252 
     253    if (this->isRectilinear(fieldId)) 
     254    { 
     255      domain->nj_glo.setValue(itMapNj->second); 
     256      domain->ni_glo.setValue((itMapNi)->second); 
     257    } 
     258    else if (this->isCurvilinear(fieldId)) 
     259    { 
     260 
     261    } 
     262    else if (this->isUnstructured(fieldId)) 
     263    { 
     264 
     265    } 
     266  } 
     267 
     268  /*! 
     269    Read attributes of an axis from a file 
     270    \param [in] axis axis whose attributes are read from the file 
     271    \param [in] dimSizeMap Dimensions and and their corresponding names and size read from file 
     272    \param [in] emelentPosition position of axis in grid 
     273  */ 
     274  void CNc4DataInput::readAxisAttributesFromFile(CAxis* axis, std::map<StdString, StdSize>& dimSizeMap, 
     275                                                 int elementPosition, const StdString& fieldId) 
     276  { 
     277    std::map<StdString, StdSize>::const_iterator itMapN = dimSizeMap.begin(), 
     278                                                 iteMap  = dimSizeMap.end(); 
     279    for (int i = 0; i < elementPosition; ++i, ++itMapN) {} 
     280    axis->n_glo.setValue(itMapN->second); 
     281  } 
     282 
     283  /*! 
     284    Read attributes of an axis from a file 
     285    \param [in] axis axis whose attributes are read from the file 
     286    \param [in] dimSizeMap Dimensions and and their corresponding names and size read from file 
     287    \param [in] emelentPosition position of axis in grid 
     288  */ 
     289  void CNc4DataInput::readAxisAttributeValueFromFile(CAxis* axis, std::map<StdString, StdSize>& dimSizeMap, 
     290                                                    int elementPosition, const StdString& fieldId) 
     291  { 
     292    std::map<StdString, StdSize>::const_iterator itMapN = dimSizeMap.begin(), 
     293                                                 iteMap  = dimSizeMap.end(); 
     294    for (int i = 0; i < elementPosition; ++i, ++itMapN) {} 
     295 
     296    { // Read axis value 
     297      std::vector<StdSize> nBegin(1, 0), nSize(1, itMapN->second); 
     298      CArray<double,1> readAxisValue(itMapN->second); 
     299      readFieldVariableValue(readAxisValue, itMapN->first, nBegin, nSize, true); 
     300      int begin = 0, n = itMapN->second; 
     301      if (!axis->begin.isEmpty()) begin = axis->begin.getValue(); 
     302      if (!axis->n.isEmpty()) n = axis->n.getValue(); 
     303      axis->value.resize(n); 
     304      for (int i = 0; i < n; ++i) axis->value(i) = readAxisValue(begin + i); 
     305    } 
     306  } 
     307 
     308  void CNc4DataInput::readFieldVariableValue(CArray<double,1>& var, const StdString& varId, 
     309                                             const std::vector<StdSize>& nBegin, 
     310                                             const std::vector<StdSize>& nSize, 
     311                                             bool forceIndependent) 
     312  { 
     313    if (SuperClass::type==MULTI_FILE || !isCollective) return; 
     314 
     315    bool openCollective = isCollective; 
     316    if (forceIndependent) openCollective = !isCollective; 
     317    switch (SuperClass::type) 
     318    { 
     319      case MULTI_FILE: 
     320        SuperClassWriter::getData(var, varId, openCollective, 0); 
     321        break; 
     322      case ONE_FILE: 
     323      { 
     324        SuperClassWriter::getData(var, varId, openCollective, 0, &nBegin, &nSize); 
     325        break; 
     326      } 
     327    } 
     328  } 
     329 
    128330  void CNc4DataInput::closeFile_(void) 
    129331  { 
Note: See TracChangeset for help on using the changeset viewer.