Ignore:
Timestamp:
05/26/15 16:13:48 (9 years ago)
Author:
rlacroix
Message:

Implement reading fields from NetCDF files.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • XIOS/trunk/src/node/file.cpp

    r598 r599  
    55#include "group_template.hpp" 
    66#include "object_factory.hpp" 
    7 #include "data_output.hpp" 
    87#include "context.hpp" 
    98#include "context_server.hpp" 
    109#include "nc4_data_output.hpp" 
     10#include "nc4_data_input.hpp" 
    1111#include "calendar_util.hpp" 
    1212#include "date.hpp" 
     
    4848   //---------------------------------------------------------------- 
    4949   /*! 
    50    \brief Get data that will be written out. 
     50   \brief Get data writer object. 
    5151   Each enabled file in xml represents a physical netcdf file. 
    52    This function allows to access to data to be written out into netcdf file 
    53    \return data written out. 
     52   This function allows to access the data writer object. 
     53   \return data writer object. 
    5454   */ 
    5555   boost::shared_ptr<CDataOutput> CFile::getDataOutput(void) const 
    5656   { 
    57       return (data_out); 
     57      return data_out; 
     58   } 
     59 
     60   /*! 
     61   \brief Get data reader object. 
     62   Each enabled file in xml represents a physical netcdf file. 
     63   This function allows to access the data reader object. 
     64   \return data reader object. 
     65   */ 
     66   boost::shared_ptr<CDataInput> CFile::getDataInput(void) const 
     67   { 
     68      return data_in; 
    5869   } 
    5970 
     
    239250    void CFile::checkFile(void) 
    240251    { 
    241       if (!isOpen) createHeader() ; 
    242       checkSync() ; 
    243       checkSplit() ; 
     252      if (mode.isEmpty() || mode.getValue() == mode_attr::write) 
     253      { 
     254        if (!isOpen) createHeader(); 
     255        checkSync(); 
     256      } 
     257      else 
     258      { 
     259        if (!isOpen) openInReadMode(); 
     260      } 
     261      checkSplit(); 
    244262    } 
    245  
    246263 
    247264    /*! 
     
    283300          *lastSplit = *lastSplit + split_freq.getValue(); 
    284301          std::vector<CField*>::iterator it, end = this->enabledFields.end(); 
    285           for (it = this->enabledFields.begin() ;it != end; it++)  (*it)->resetNStep() ; 
    286           createHeader() ; 
     302          for (it = this->enabledFields.begin(); it != end; it++) 
     303          { 
     304            (*it)->resetNStep(); 
     305            (*it)->resetNStepMax(); 
     306          } 
     307          if (mode.isEmpty() || mode.getValue() == mode_attr::write) 
     308            createHeader() ; 
     309          else 
     310            openInReadMode(); 
    287311          return true ; 
    288312        } 
     
    398422   } 
    399423 
     424  /*! 
     425  \brief Open an existing NetCDF file in read-only mode 
     426  */ 
     427  void CFile::openInReadMode(void) 
     428  { 
     429    CContext* context = CContext::getCurrent(); 
     430    CContextServer* server=context->server; 
     431 
     432    if (!allDomainEmpty) 
     433    { 
     434      StdString filename = (!name.isEmpty()) ? name.getValue() : getId(); 
     435      StdOStringStream oss; 
     436      oss << filename; 
     437      if (!name_suffix.isEmpty()) oss << name_suffix.getValue(); 
     438 
     439      if (!split_freq.isEmpty()) 
     440      { 
     441        string splitFormat; 
     442        if (split_freq_format.isEmpty()) 
     443        { 
     444          if (split_freq.getValue().second != 0) splitFormat = "%y%mo%d%h%mi%s"; 
     445          else if (split_freq.getValue().minute != 0) splitFormat = "%y%mo%d%h%mi"; 
     446          else if (split_freq.getValue().hour != 0) splitFormat = "%y%mo%d%h"; 
     447          else if (split_freq.getValue().day != 0) splitFormat = "%y%mo%d"; 
     448          else if (split_freq.getValue().month != 0) splitFormat = "%y%mo"; 
     449          else splitFormat = "%y"; 
     450        } 
     451        else splitFormat=split_freq_format; 
     452        oss << "_" << lastSplit->getStr(splitFormat) 
     453        << "-" << (*lastSplit + split_freq.getValue() - 1 * Second).getStr(splitFormat); 
     454      } 
     455 
     456      bool multifile = true; 
     457      if (!type.isEmpty()) 
     458      { 
     459        if (type == type_attr::one_file) multifile = false; 
     460        else if (type == type_attr::multiple_file) multifile = true; 
     461      } 
     462  #ifndef USING_NETCDF_PAR 
     463      if (!multifile) 
     464      { 
     465        info(0) << "!!! Warning -> Using non parallel version of netcdf, switching in multiple_file mode for file : " << filename << " ..." << endl; 
     466        multifile = true; 
     467      } 
     468  #endif 
     469      if (multifile) 
     470      { 
     471        int commSize, commRank; 
     472        MPI_Comm_size(fileComm, &commSize); 
     473        MPI_Comm_rank(fileComm, &commRank); 
     474 
     475        if (server->intraCommSize > 1) 
     476        { 
     477          oss << "_" ; 
     478          int width = 0, n = commSize - 1; 
     479          while (n != 0) { n = n / 10; width++; } 
     480          if (!min_digits.isEmpty() && width < min_digits) 
     481            width = min_digits; 
     482          oss.width(width); 
     483          oss.fill('0'); 
     484          oss << right << commRank; 
     485        } 
     486      } 
     487      oss << ".nc"; 
     488 
     489      bool isCollective = par_access.isEmpty() || par_access == par_access_attr::collective; 
     490 
     491      if (isOpen) data_out->closeFile(); 
     492 
     493      data_in = shared_ptr<CDataInput>(new CNc4DataInput(oss.str(), fileComm, multifile, isCollective)); 
     494      isOpen = true; 
     495    } 
     496  } 
     497 
    400498   //! Close file 
    401499   void CFile::close(void) 
     
    406504       if (isOpen) 
    407505       { 
    408          this->data_out->closeFile(); 
     506         if (mode.isEmpty() || mode.getValue() == mode_attr::write) 
     507          this->data_out->closeFile(); 
     508         else 
     509          this->data_in->closeFile(); 
    409510       } 
    410511      if (fileComm != MPI_COMM_NULL) MPI_Comm_free(&fileComm) ; 
Note: See TracChangeset for help on using the changeset viewer.