Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
moab::ScdNCWriteHelper Class Reference

Child helper class for scd mesh, e.g. CAM_EL or CAM_FV. More...

#include <NCWriteHelper.hpp>

+ Inheritance diagram for moab::ScdNCWriteHelper:
+ Collaboration diagram for moab::ScdNCWriteHelper:

Public Member Functions

 ScdNCWriteHelper (WriteNC *writeNC, int fileId, const FileOptions &opts, EntityHandle fileSet)
 
virtual ~ScdNCWriteHelper ()
 
- Public Member Functions inherited from moab::NCWriteHelper
 NCWriteHelper (WriteNC *writeNC, int fileId, const FileOptions &opts, EntityHandle fileSet)
 
virtual ~NCWriteHelper ()
 
ErrorCode init_file (std::vector< std::string > &var_names, std::vector< std::string > &desired_names, bool _append)
 Initialize file: this is where all defines are done The VarData dimension ids are filled up after define. More...
 
ErrorCode write_values (std::vector< std::string > &var_names, std::vector< int > &tstep_nums)
 Take the info from VarData and write first non-set variables, then set variables. More...
 

Protected Attributes

int lDims [6]
 Dimensions of my local part of grid. More...
 
int lCDims [6]
 Center dimensions of my local part of grid. More...
 
- Protected Attributes inherited from moab::NCWriteHelper
WriteNC_writeNC
 Allow NCWriteHelper to directly access members of WriteNC. More...
 
int _fileId
 Cache some information from WriteNC. More...
 
const FileOptions_opts
 
EntityHandle _fileSet
 
int nTimeSteps
 Dimensions of time and level. More...
 
int nLevels
 
int tDim
 Dimension numbers for time and level. More...
 
int levDim
 
Range localCellsOwned
 Local owned cells, edges and vertices. More...
 
Range localEdgesOwned
 
Range localVertsOwned
 
std::vector< double > timeStepVals
 Time values of output timesteps. More...
 

Private Member Functions

virtual ErrorCode collect_mesh_info ()
 Implementation of NCWriteHelper::collect_mesh_info() More...
 
virtual ErrorCode collect_variable_data (std::vector< std::string > &var_names, std::vector< int > &tstep_nums)
 Collect data for specified variables. More...
 
virtual ErrorCode write_nonset_variables (std::vector< WriteNC::VarData > &vdatas, std::vector< int > &tstep_nums)
 Implementation of NCWriteHelper::write_nonset_variables() More...
 
template<typename T >
void jik_to_kji (size_t ni, size_t nj, size_t nk, T *dest, T *source)
 

Additional Inherited Members

- Static Public Member Functions inherited from moab::NCWriteHelper
static NCWriteHelperget_nc_helper (WriteNC *writeNC, int fileId, const FileOptions &opts, EntityHandle fileSet)
 Get appropriate helper instance for WriteNC class based on some info in the file set. More...
 

Detailed Description

Child helper class for scd mesh, e.g. CAM_EL or CAM_FV.

Definition at line 81 of file NCWriteHelper.hpp.

Constructor & Destructor Documentation

◆ ScdNCWriteHelper()

moab::ScdNCWriteHelper::ScdNCWriteHelper ( WriteNC writeNC,
int  fileId,
const FileOptions opts,
EntityHandle  fileSet 
)
inline

Definition at line 84 of file NCWriteHelper.hpp.

85  : NCWriteHelper( writeNC, fileId, opts, fileSet )
86  {
87  for( unsigned int i = 0; i < 6; i++ )
88  {
89  lDims[i] = -1;
90  lCDims[i] = -1;
91  }
92  }

References lCDims, and lDims.

◆ ~ScdNCWriteHelper()

virtual moab::ScdNCWriteHelper::~ScdNCWriteHelper ( )
inlinevirtual

Definition at line 93 of file NCWriteHelper.hpp.

93 {}

Member Function Documentation

◆ collect_mesh_info()

ErrorCode moab::ScdNCWriteHelper::collect_mesh_info ( )
privatevirtual

Implementation of NCWriteHelper::collect_mesh_info()

Implements moab::NCWriteHelper.

Definition at line 593 of file NCWriteHelper.cpp.

594 {
595  Interface*& mbImpl = _writeNC->mbImpl;
596  std::vector< std::string >& dimNames = _writeNC->dimNames;
597  std::vector< int >& dimLens = _writeNC->dimLens;
598 
599  ErrorCode rval;
600 
601  // Look for time dimension
602  std::vector< std::string >::iterator vecIt;
603  if( ( vecIt = std::find( dimNames.begin(), dimNames.end(), "time" ) ) != dimNames.end() )
604  tDim = vecIt - dimNames.begin();
605  else if( ( vecIt = std::find( dimNames.begin(), dimNames.end(), "t" ) ) != dimNames.end() )
606  tDim = vecIt - dimNames.begin();
607  else
608  {
609  MB_SET_ERR( MB_FAILURE, "Couldn't find 'time' or 't' dimension" );
610  }
611  nTimeSteps = dimLens[tDim];
612 
613  // Get number of levels
614  if( ( vecIt = std::find( dimNames.begin(), dimNames.end(), "lev" ) ) != dimNames.end() )
615  levDim = vecIt - dimNames.begin();
616  else if( ( vecIt = std::find( dimNames.begin(), dimNames.end(), "ilev" ) ) != dimNames.end() )
617  levDim = vecIt - dimNames.begin();
618  else
619  {
620  MB_SET_ERR( MB_FAILURE, "Couldn't find 'lev' or 'ilev' dimension" );
621  }
622  nLevels = dimLens[levDim];
623 
624  // __<dim_name>_LOC_MINMAX (for slon, slat, lon and lat)
625  Tag convTag = 0;
626  rval = mbImpl->tag_get_handle( "__slon_LOC_MINMAX", 0, MB_TYPE_INTEGER, convTag, MB_TAG_ANY );MB_CHK_SET_ERR( rval, "Trouble getting conventional tag __slon_LOC_MINMAX" );
627  int val[2];
628  rval = mbImpl->tag_get_data( convTag, &_fileSet, 1, val );MB_CHK_SET_ERR( rval, "Trouble getting data of conventional tag __slon_LOC_MINMAX" );
629  lDims[0] = val[0];
630  lDims[3] = val[1];
631 
632  rval = mbImpl->tag_get_handle( "__slat_LOC_MINMAX", 0, MB_TYPE_INTEGER, convTag, MB_TAG_ANY );MB_CHK_SET_ERR( rval, "Trouble getting conventional tag __slat_LOC_MINMAX" );
633  rval = mbImpl->tag_get_data( convTag, &_fileSet, 1, val );MB_CHK_SET_ERR( rval, "Trouble getting data of conventional tag __slat_LOC_MINMAX" );
634  lDims[1] = val[0];
635  lDims[4] = val[1];
636 
637  rval = mbImpl->tag_get_handle( "__lon_LOC_MINMAX", 0, MB_TYPE_INTEGER, convTag, MB_TAG_ANY );MB_CHK_SET_ERR( rval, "Trouble getting conventional tag __lon_LOC_MINMAX" );
638  rval = mbImpl->tag_get_data( convTag, &_fileSet, 1, val );MB_CHK_SET_ERR( rval, "Trouble getting data of conventional tag __lon_LOC_MINMAX" );
639  lCDims[0] = val[0];
640  lCDims[3] = val[1];
641 
642  rval = mbImpl->tag_get_handle( "__lat_LOC_MINMAX", 0, MB_TYPE_INTEGER, convTag, MB_TAG_ANY );MB_CHK_SET_ERR( rval, "Trouble getting conventional tag __lat_LOC_MINMAX" );
643  rval = mbImpl->tag_get_data( convTag, &_fileSet, 1, val );MB_CHK_SET_ERR( rval, "Trouble getting data of conventional tag __lat_LOC_MINMAX" );
644  lCDims[1] = val[0];
645  lCDims[4] = val[1];
646 
647  // Get local faces
648  rval = mbImpl->get_entities_by_dimension( _fileSet, 2, localCellsOwned );MB_CHK_SET_ERR( rval, "Trouble getting local faces in current file set" );
649  assert( !localCellsOwned.empty() );
650 
651 #ifdef MOAB_HAVE_MPI
652  bool& isParallel = _writeNC->isParallel;
653  if( isParallel )
654  {
655  ParallelComm*& myPcomm = _writeNC->myPcomm;
656  int procs = myPcomm->proc_config().proc_size();
657  if( procs > 1 )
658  {
659  rval = myPcomm->filter_pstatus( localCellsOwned, PSTATUS_NOT_OWNED, PSTATUS_NOT );MB_CHK_SET_ERR( rval, "Trouble getting owned faces in current file set" );
660  }
661  }
662 #endif
663 
664  return MB_SUCCESS;
665 }

References moab::NCWriteHelper::_fileSet, moab::NCWriteHelper::_writeNC, moab::WriteNC::dimLens, moab::WriteNC::dimNames, moab::Range::empty(), ErrorCode, moab::ParallelComm::filter_pstatus(), moab::Interface::get_entities_by_dimension(), moab::WriteNC::isParallel, lCDims, lDims, moab::NCWriteHelper::levDim, moab::NCWriteHelper::localCellsOwned, MB_CHK_SET_ERR, MB_SET_ERR, MB_SUCCESS, MB_TAG_ANY, MB_TYPE_INTEGER, moab::WriteNC::mbImpl, moab::NCWriteHelper::nLevels, moab::NCWriteHelper::nTimeSteps, moab::ParallelComm::proc_config(), moab::ProcConfig::proc_size(), PSTATUS_NOT, PSTATUS_NOT_OWNED, moab::Interface::tag_get_data(), moab::Interface::tag_get_handle(), and moab::NCWriteHelper::tDim.

◆ collect_variable_data()

ErrorCode moab::ScdNCWriteHelper::collect_variable_data ( std::vector< std::string > &  var_names,
std::vector< int > &  tstep_nums 
)
privatevirtual

Collect data for specified variables.

Reimplemented from moab::NCWriteHelper.

Definition at line 667 of file NCWriteHelper.cpp.

669 {
670  NCWriteHelper::collect_variable_data( var_names, tstep_nums );
671 
672  std::map< std::string, WriteNC::VarData >& varInfo = _writeNC->varInfo;
673 
674  for( size_t i = 0; i < var_names.size(); i++ )
675  {
676  std::string varname = var_names[i];
677  std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( varname );
678  if( vit == varInfo.end() ) MB_SET_ERR( MB_FAILURE, "Can't find variable " << varname );
679 
680  WriteNC::VarData& currentVarData = vit->second;
681 #ifndef NDEBUG
682  std::vector< int >& varDims = currentVarData.varDims;
683 #endif
684 
685  // Skip set variables, which were already processed in
686  // NCWriteHelper::collect_variable_data()
687  if( WriteNC::ENTLOCSET == currentVarData.entLoc ) continue;
688 
689  // Set up writeStarts and writeCounts (maximum number of dimensions is 4)
690  currentVarData.writeStarts.resize( 4 );
691  currentVarData.writeCounts.resize( 4 );
692  unsigned int dim_idx = 0;
693 
694  // First: time
695  if( currentVarData.has_tsteps )
696  {
697  // Non-set variables with timesteps
698  // 4 dimensions like (time, lev, lat, lon)
699  // 3 dimensions like (time, lat, lon)
700  assert( 4 == varDims.size() || 3 == varDims.size() );
701 
702  // Time should be the first dimension
703  assert( tDim == varDims[0] );
704 
705  currentVarData.writeStarts[dim_idx] = 0; // This value is timestep dependent, will be set later
706  currentVarData.writeCounts[dim_idx] = 1;
707  dim_idx++;
708  }
709  else
710  {
711  // Non-set variables without timesteps
712  // 3 dimensions like (lev, lat, lon)
713  // 2 dimensions like (lat, lon)
714  assert( 3 == varDims.size() || 2 == varDims.size() );
715  }
716 
717  // Next: lev
718  if( currentVarData.numLev > 0 )
719  {
720  // Non-set variables with levels
721  // 4 dimensions like (time, lev, lat, lon)
722  // 3 dimensions like (lev, lat, lon)
723  assert( 4 == varDims.size() || 3 == varDims.size() );
724 
725  currentVarData.writeStarts[dim_idx] = 0;
726  currentVarData.writeCounts[dim_idx] = currentVarData.numLev;
727  dim_idx++;
728  }
729  else
730  {
731  // Non-set variables without levels
732  // 3 dimensions like (time, lat, lon)
733  // 2 dimensions like (lat, lon)
734  assert( 3 == varDims.size() || 2 == varDims.size() );
735  }
736 
737  // Finally: lat and lon
738  switch( currentVarData.entLoc )
739  {
740  case WriteNC::ENTLOCFACE:
741  // Faces
742  currentVarData.writeStarts[dim_idx] = lCDims[1];
743  currentVarData.writeCounts[dim_idx] = lCDims[4] - lCDims[1] + 1;
744  currentVarData.writeStarts[dim_idx + 1] = lCDims[0];
745  currentVarData.writeCounts[dim_idx + 1] = lCDims[3] - lCDims[0] + 1;
746  break;
747  default:
748  MB_SET_ERR( MB_FAILURE, "Unexpected entity location type for variable " << varname );
749  }
750  dim_idx += 2;
751 
752  // Get variable size
753  currentVarData.sz = 1;
754  for( std::size_t idx = 0; idx < dim_idx; idx++ )
755  currentVarData.sz *= currentVarData.writeCounts[idx];
756  } // for (size_t i = 0; i < var_names.size(); i++)
757 
758  return MB_SUCCESS;
759 }

References moab::NCWriteHelper::_writeNC, moab::NCWriteHelper::collect_variable_data(), moab::WriteNC::VarData::entLoc, moab::WriteNC::ENTLOCFACE, moab::WriteNC::ENTLOCSET, moab::WriteNC::VarData::has_tsteps, lCDims, MB_SET_ERR, MB_SUCCESS, moab::WriteNC::VarData::numLev, moab::WriteNC::VarData::sz, moab::NCWriteHelper::tDim, moab::WriteNC::VarData::varDims, moab::WriteNC::varInfo, moab::WriteNC::VarData::writeCounts, and moab::WriteNC::VarData::writeStarts.

◆ jik_to_kji()

template<typename T >
void moab::ScdNCWriteHelper::jik_to_kji ( size_t  ni,
size_t  nj,
size_t  nk,
T *  dest,
T *  source 
)
inlineprivate

Definition at line 106 of file NCWriteHelper.hpp.

107  {
108  size_t nik = ni * nk, nij = ni * nj;
109  for( std::size_t k = 0; k != nk; k++ )
110  for( std::size_t j = 0; j != nj; j++ )
111  for( std::size_t i = 0; i != ni; i++ )
112  dest[k * nij + j * ni + i] = source[j * nik + i * nk + k];
113  }

Referenced by write_nonset_variables().

◆ write_nonset_variables()

ErrorCode moab::ScdNCWriteHelper::write_nonset_variables ( std::vector< WriteNC::VarData > &  vdatas,
std::vector< int > &  tstep_nums 
)
privatevirtual

Implementation of NCWriteHelper::write_nonset_variables()

Implements moab::NCWriteHelper.

Definition at line 764 of file NCWriteHelper.cpp.

766 {
767  Interface*& mbImpl = _writeNC->mbImpl;
768 
769  int success;
770 
771  // For each indexed variable tag, write a time step data
772  for( unsigned int i = 0; i < vdatas.size(); i++ )
773  {
774  WriteNC::VarData& variableData = vdatas[i];
775 
776  // Assume this variable is on faces for the time being
777  switch( variableData.entLoc )
778  {
779  case WriteNC::ENTLOCFACE:
780  // Faces
781  break;
782  default:
783  MB_SET_ERR( MB_FAILURE, "Unexpected entity location type for variable " << variableData.varName );
784  }
785 
786  unsigned int num_timesteps;
787  unsigned int lat_idx = 0;
788  unsigned int lon_idx = 1;
789  if( variableData.has_tsteps )
790  {
791  // Non-set variables with timesteps
792  // 4 dimensions like (time, lev, lat, lon)
793  // 3 dimensions like (time, lat, lon)
794  num_timesteps = tstep_nums.size();
795  lat_idx++;
796  lon_idx++;
797  }
798  else
799  {
800  // Non-set variables without timesteps
801  // 3 dimensions like (lev, lat, lon)
802  // 2 dimensions like (lat, lon)
803  num_timesteps = 1;
804  }
805 
806  unsigned int num_lev;
807  if( variableData.numLev > 0 )
808  {
809  // Non-set variables with levels
810  // 4 dimensions like (time, lev, lat, lon)
811  // 3 dimensions like (lev, lat, lon)
812  num_lev = variableData.numLev;
813  lat_idx++;
814  lon_idx++;
815  }
816  else
817  {
818  // Non-set variables without levels
819  // 3 dimensions like (time, lat, lon)
820  // 2 dimensions like (lat, lon)
821  num_lev = 1;
822  }
823 
824  size_t ni = variableData.writeCounts[lon_idx]; // lon
825  size_t nj = variableData.writeCounts[lat_idx]; // lat
826 
827  // At each timestep, we need to transpose tag format (lat, lon, lev) back
828  // to NC format (lev, lat, lon) for writing
829  for( unsigned int t = 0; t < num_timesteps; t++ )
830  {
831  // We will write one time step, and count will be one; start will be different
832  // Use tag_iterate to get tag data (assume that localCellsOwned is contiguous)
833  // We should also transpose for level so that means deep copy for transpose
834  if( tDim == variableData.varDims[0] ) variableData.writeStarts[0] = t; // This is start for time
835  int count;
836  void* dataptr;
837  ErrorCode rval = mbImpl->tag_iterate( variableData.varTags[t], localCellsOwned.begin(),
838  localCellsOwned.end(), count, dataptr );MB_CHK_SET_ERR( rval, "Failed to iterate tag on owned faces" );
839  assert( count == (int)localCellsOwned.size() );
840 
841  // Now transpose and write tag data
842  // Use collective I/O mode put (synchronous write) for the time being, we can try
843  // nonblocking put (request aggregation) later
844  switch( variableData.varDataType )
845  {
846  case NC_DOUBLE: {
847  std::vector< double > tmpdoubledata( ni * nj * num_lev );
848  if( num_lev > 1 )
849  // Transpose (lat, lon, lev) back to (lev, lat, lon)
850  jik_to_kji( ni, nj, num_lev, &tmpdoubledata[0], (double*)( dataptr ) );
851  success = NCFUNCAP( _vara_double )( _fileId, variableData.varId, &variableData.writeStarts[0],
852  &variableData.writeCounts[0], &tmpdoubledata[0] );
853  if( success )
854  MB_SET_ERR( MB_FAILURE, "Failed to write double data for variable " << variableData.varName );
855  break;
856  }
857  default:
858  MB_SET_ERR( MB_NOT_IMPLEMENTED, "Writing non-double data is not implemented yet" );
859  }
860  }
861  }
862 
863  return MB_SUCCESS;
864 }

References moab::NCWriteHelper::_fileId, moab::NCWriteHelper::_writeNC, moab::Range::begin(), moab::Range::end(), moab::WriteNC::VarData::entLoc, moab::WriteNC::ENTLOCFACE, ErrorCode, moab::WriteNC::VarData::has_tsteps, jik_to_kji(), moab::NCWriteHelper::localCellsOwned, MB_CHK_SET_ERR, MB_NOT_IMPLEMENTED, MB_SET_ERR, MB_SUCCESS, moab::WriteNC::mbImpl, NCFUNCAP, moab::WriteNC::VarData::numLev, moab::Range::size(), moab::Interface::tag_iterate(), moab::NCWriteHelper::tDim, moab::WriteNC::VarData::varDataType, moab::WriteNC::VarData::varDims, moab::WriteNC::VarData::varId, moab::WriteNC::VarData::varName, moab::WriteNC::VarData::varTags, moab::WriteNC::VarData::writeCounts, and moab::WriteNC::VarData::writeStarts.

Member Data Documentation

◆ lCDims

int moab::ScdNCWriteHelper::lCDims[6]
protected

Center dimensions of my local part of grid.

Definition at line 120 of file NCWriteHelper.hpp.

Referenced by collect_mesh_info(), collect_variable_data(), and ScdNCWriteHelper().

◆ lDims

int moab::ScdNCWriteHelper::lDims[6]
protected

Dimensions of my local part of grid.

Definition at line 117 of file NCWriteHelper.hpp.

Referenced by collect_mesh_info(), and ScdNCWriteHelper().


The documentation for this class was generated from the following files: