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

Export NC files. More...

#include <WriteNC.hpp>

+ Inheritance diagram for moab::WriteNC:
+ Collaboration diagram for moab::WriteNC:

Classes

class  AttData
 
class  VarData
 

Public Member Functions

 WriteNC (Interface *impl=NULL)
 Constructor. More...
 
virtual ~WriteNC ()
 Destructor. More...
 
ErrorCode write_file (const char *file_name, const bool overwrite, const FileOptions &opts, const EntityHandle *output_list, const int num_sets, const std::vector< std::string > &qa_list, const Tag *tag_list=NULL, int num_tags=0, int export_dimension=3)
 Writes out a file. More...
 
- Public Member Functions inherited from moab::WriterIface
virtual ~WriterIface ()
 

Static Public Member Functions

static WriterIfacefactory (Interface *)
 Factory method. More...
 

Private Types

enum  EntityLocation {
  ENTLOCVERT = 0 , ENTLOCNSEDGE , ENTLOCEWEDGE , ENTLOCFACE ,
  ENTLOCSET , ENTLOCEDGE , ENTLOCREGION
}
 ENTLOCNSEDGE for north/south edge ENTLOCWEEDGE for west/east edge. More...
 

Private Member Functions

ErrorCode parse_options (const FileOptions &opts, std::vector< std::string > &var_names, std::vector< std::string > &desired_names, std::vector< int > &tstep_nums, std::vector< double > &tstep_vals)
 
ErrorCode process_conventional_tags (EntityHandle fileSet)
 
ErrorCode process_concatenated_attribute (const void *attPtr, int attSz, std::vector< int > &attLen, std::map< std::string, AttData > &attributes)
 

Private Attributes

std::vector< std::string > dimNames
 This info will be reconstructed from metadata stored on conventional fileSet tags Dimension names. More...
 
std::vector< int > dimLens
 Dimension lengths. More...
 
std::set< std::string > usedCoordinates
 Will collect used dimensions (coordinate variables) More...
 
std::set< std::string > dummyVarNames
 Dummy variables (for dimensions that have no corresponding coordinate variables) More...
 
std::map< std::string, AttDataglobalAtts
 Global attribs. More...
 
std::map< std::string, VarDatavarInfo
 Variable info. More...
 
InterfacembImpl
 Interface instance. More...
 
WriteUtilIfacemWriteIface
 
const char * fileName
 File var. More...
 
int fileId
 File numbers assigned by (p)netcdf. More...
 
DebugOutput dbgOut
 Debug stuff. More...
 
bool noMesh
 Write options. More...
 
bool noVars
 
bool append
 
Tag mGlobalIdTag
 Cached tags for writing. This will be important for ordering the data, in parallel. More...
 
bool isParallel
 Are we writing in parallel? (probably in the future) More...
 
std::string grid_type
 CAM Euler, etc,. More...
 
NCWriteHelpermyHelper
 Helper class instance. More...
 

Friends

class NCWriteHelper
 
class ScdNCWriteHelper
 
class UcdNCWriteHelper
 
class NCWriteEuler
 
class NCWriteFV
 
class NCWriteHOMME
 
class NCWriteMPAS
 
class NCWriteGCRM
 

Detailed Description

Export NC files.

Definition at line 70 of file WriteNC.hpp.

Member Enumeration Documentation

◆ EntityLocation

ENTLOCNSEDGE for north/south edge ENTLOCWEEDGE for west/east edge.

Enumerator
ENTLOCVERT 
ENTLOCNSEDGE 
ENTLOCEWEDGE 
ENTLOCFACE 
ENTLOCSET 
ENTLOCEDGE 
ENTLOCREGION 

Definition at line 105 of file WriteNC.hpp.

106  {
107  ENTLOCVERT = 0,
108  ENTLOCNSEDGE,
109  ENTLOCEWEDGE,
110  ENTLOCFACE,
111  ENTLOCSET,
112  ENTLOCEDGE,
114  };

Constructor & Destructor Documentation

◆ WriteNC()

moab::WriteNC::WriteNC ( Interface impl = NULL)

Constructor.

Definition at line 32 of file WriteNC.cpp.

33  : mbImpl( impl ), dbgOut( stderr ),
34 #ifdef MOAB_HAVE_MPI
35  myPcomm( NULL ),
36 #endif
37  noMesh( false ), noVars( false ), append( false ), mGlobalIdTag( 0 ), isParallel( false ), myHelper( NULL )
38 {
39  assert( impl != NULL );
40  impl->query_interface( mWriteIface );
41 }

References mWriteIface, and moab::Interface::query_interface().

Referenced by factory().

◆ ~WriteNC()

moab::WriteNC::~WriteNC ( )
virtual

Destructor.

Definition at line 43 of file WriteNC.cpp.

44 {
46  if( myHelper != NULL ) delete myHelper;
47 }

References mbImpl, mWriteIface, myHelper, and moab::Interface::release_interface().

Member Function Documentation

◆ factory()

WriterIface * moab::WriteNC::factory ( Interface iface)
static

Factory method.

Definition at line 27 of file WriteNC.cpp.

28 {
29  return new WriteNC( iface );
30 }

References iface, and WriteNC().

Referenced by moab::ReaderWriterSet::ReaderWriterSet().

◆ parse_options()

ErrorCode moab::WriteNC::parse_options ( const FileOptions opts,
std::vector< std::string > &  var_names,
std::vector< std::string > &  desired_names,
std::vector< int > &  tstep_nums,
std::vector< double > &  tstep_vals 
)
private

Definition at line 140 of file WriteNC.cpp.

145 {
146  int tmpval;
147  if( MB_SUCCESS == opts.get_int_option( "DEBUG_IO", 1, tmpval ) )
148  {
149  dbgOut.set_verbosity( tmpval );
150  dbgOut.set_prefix( "NCWrite" );
151  }
152 
153  ErrorCode rval = opts.get_strs_option( "VARIABLE", var_names );
154  if( MB_TYPE_OUT_OF_RANGE == rval )
155  noVars = true;
156  else
157  noVars = false;
158 
159  rval = opts.get_strs_option( "RENAME", desired_names );
160  if( MB_ENTITY_NOT_FOUND == rval )
161  {
162  if( !noVars )
163  {
164  desired_names.resize( var_names.size() );
165  std::copy( var_names.begin(), var_names.end(), desired_names.begin() );
166  }
167  }
168  // Either way
169  assert( desired_names.size() == var_names.size() );
170 
171  opts.get_ints_option( "TIMESTEP", tstep_nums );
172  opts.get_reals_option( "TIMEVAL", tstep_vals );
173  rval = opts.get_null_option( "NOMESH" );
174  if( MB_SUCCESS == rval ) noMesh = true;
175 
176  rval = opts.get_null_option( "APPEND" );
177  if( MB_SUCCESS == rval ) append = true;
178 
179  if( 2 <= dbgOut.get_verbosity() )
180  {
181  if( !var_names.empty() )
182  {
183  std::cerr << "Variables requested: ";
184  for( unsigned int i = 0; i < var_names.size(); i++ )
185  std::cerr << var_names[i];
186  std::cerr << std::endl;
187  }
188  if( !tstep_nums.empty() )
189  {
190  std::cerr << "Timesteps requested: ";
191  for( unsigned int i = 0; i < tstep_nums.size(); i++ )
192  std::cerr << tstep_nums[i];
193  std::cerr << std::endl;
194  }
195  if( !tstep_vals.empty() )
196  {
197  std::cerr << "Time vals requested: ";
198  for( unsigned int i = 0; i < tstep_vals.size(); i++ )
199  std::cerr << tstep_vals[i];
200  std::cerr << std::endl;
201  }
202  }
203 
204 // FIXME: copied from ReadNC, may need revise
205 #ifdef MOAB_HAVE_MPI
206  isParallel = ( opts.match_option( "PARALLEL", "WRITE_PART" ) != MB_ENTITY_NOT_FOUND );
207 
208  if( !isParallel )
209  // Return success here, since rval still has _NOT_FOUND from not finding option
210  // in this case, myPcomm will be NULL, so it can never be used; always check for isParallel
211  // before any use for myPcomm
212  return MB_SUCCESS;
213 
214  int pcomm_no = 0;
215  rval = opts.get_int_option( "PARALLEL_COMM", pcomm_no );
216  if( MB_TYPE_OUT_OF_RANGE == rval )
217  {
218  MB_SET_ERR( rval, "Invalid value for PARALLEL_COMM option" );
219  }
220 
221  myPcomm = ParallelComm::get_pcomm( mbImpl, pcomm_no );
222  if( 0 == myPcomm )
223  {
224  myPcomm = new ParallelComm( mbImpl, MPI_COMM_WORLD );
225  }
226 
227 #ifndef MOAB_HAVE_PNETCDF
228  const int procs = myPcomm->proc_config().proc_size();
229  if( procs > 1 )
230  {
231  MB_SET_ERR( MB_UNSUPPORTED_OPERATION, "Attempt to launch NC writer in parallel without pnetcdf support" );
232  }
233 #endif
234 
235  const int rank = myPcomm->proc_config().proc_rank();
236  dbgOut.set_rank( rank );
237 #endif
238 
239  return MB_SUCCESS;
240 }

References append, dbgOut, ErrorCode, moab::FileOptions::get_int_option(), moab::FileOptions::get_ints_option(), moab::FileOptions::get_null_option(), moab::ParallelComm::get_pcomm(), moab::FileOptions::get_reals_option(), moab::FileOptions::get_strs_option(), moab::DebugOutput::get_verbosity(), isParallel, moab::FileOptions::match_option(), MB_ENTITY_NOT_FOUND, MB_SET_ERR, MB_SUCCESS, MB_TYPE_OUT_OF_RANGE, MB_UNSUPPORTED_OPERATION, mbImpl, noMesh, noVars, moab::DebugOutput::set_prefix(), moab::DebugOutput::set_rank(), and moab::DebugOutput::set_verbosity().

Referenced by write_file().

◆ process_concatenated_attribute()

ErrorCode moab::WriteNC::process_concatenated_attribute ( const void *  attPtr,
int  attSz,
std::vector< int > &  attLen,
std::map< std::string, AttData > &  attributes 
)
private

Definition at line 455 of file WriteNC.cpp.

459 {
460  std::size_t start = 0;
461  std::size_t att_counter = 0;
462  std::string concatString( (char*)attPtr, (char*)attPtr + attSz );
463 
464  for( std::size_t i = 0; i != (size_t)attSz; i++ )
465  {
466  if( concatString[i] == '\0' )
467  {
468  std::string att_name( &concatString[start], i - start );
469  start = i + 1;
470  while( concatString[i] != ';' )
471  ++i;
472  std::string data_type( &concatString[start], i - start );
473  ++i;
474  start = i;
475  i = attLen[att_counter];
476  if( concatString[i] != ';' ) MB_SET_ERR( MB_FAILURE, "Error parsing attributes" );
477 
478  std::string data_val( &concatString[start], i - start );
479  start = i + 1;
480 
481  AttData& attrib = attributes[att_name];
482  attrib.attValue = data_val;
483  attrib.attLen = data_val.size();
484 
485  if( data_type == "char" )
486  attrib.attDataType = NC_CHAR;
487  else if( data_type == "double" )
488  attrib.attDataType = NC_DOUBLE;
489  else if( data_type == "float" )
490  attrib.attDataType = NC_FLOAT;
491  else if( data_type == "int" )
492  attrib.attDataType = NC_INT;
493  else if( data_type == "short" )
494  attrib.attDataType = NC_SHORT;
495 
496  ++att_counter;
497  dbgOut.tprintf( 2, " Process attribute %s with value %s \n", att_name.c_str(), data_val.c_str() );
498  }
499  }
500 
501  return MB_SUCCESS;
502 }

References moab::WriteNC::AttData::attDataType, moab::WriteNC::AttData::attLen, moab::WriteNC::AttData::attValue, dbgOut, MB_SET_ERR, MB_SUCCESS, and moab::DebugOutput::tprintf().

Referenced by process_conventional_tags().

◆ process_conventional_tags()

ErrorCode moab::WriteNC::process_conventional_tags ( EntityHandle  fileSet)
private

Definition at line 244 of file WriteNC.cpp.

245 {
246  ErrorCode rval;
247 
248  // Start copy
249  Tag dimNamesTag = 0;
250  std::string tag_name = "__DIM_NAMES";
251  const void* data = NULL;
252  int dimNamesSz = 0;
253  rval = mbImpl->tag_get_handle( tag_name.c_str(), 0, MB_TYPE_OPAQUE, dimNamesTag, MB_TAG_ANY );MB_CHK_SET_ERR( rval, "Trouble getting conventional tag " << tag_name );
254  rval = mbImpl->tag_get_by_ptr( dimNamesTag, &fileSet, 1, &data, &dimNamesSz );MB_CHK_SET_ERR( rval, "Trouble getting data of conventional tag " << tag_name );
255  const char* p = static_cast< const char* >( data );
256  dbgOut.tprintf( 1, "__DIM_NAMES tag has string length %d\n", dimNamesSz );
257 
258  std::size_t start = 0;
259 
260  Tag dimLensTag = 0;
261  tag_name = "__DIM_LENS";
262  data = NULL;
263  int dimLensSz = 0;
264  rval = mbImpl->tag_get_handle( tag_name.c_str(), 0, MB_TYPE_INTEGER, dimLensTag, MB_TAG_ANY );MB_CHK_SET_ERR( rval, "Trouble getting conventional tag " << tag_name );
265  rval = mbImpl->tag_get_by_ptr( dimLensTag, &fileSet, 1, &data, &dimLensSz );MB_CHK_SET_ERR( rval, "Trouble getting data of conventional tag " << tag_name );
266  const int* int_p = static_cast< const int* >( data );
267  dbgOut.tprintf( 1, "__DIM_LENS tag has %d values\n", dimLensSz );
268 
269  int idxDim = 0;
270  // Dim names are separated by '\0' in the string of __DIM_NAMES tag
271  for( std::size_t i = 0; i != static_cast< std::size_t >( dimNamesSz ); i++ )
272  {
273  if( p[i] == '\0' )
274  {
275  std::string dim_name( &p[start], i - start );
276  int len = int_p[idxDim];
277  dimNames.push_back( dim_name );
278  dimLens.push_back( len );
279  dbgOut.tprintf( 2, "Dimension %s has length %d\n", dim_name.c_str(), len );
280  // FIXME: Need info from moab to set unlimited dimension
281  // Currently assume each file has the same number of time dimensions
282  /*if ((dim_name == "time") || (dim_name == "Time"))
283  insert(dim_name, *(new pcdim(dim_name, len * m_file_names.size(), true)));
284  else
285  insert(dim_name, *(new pcdim(dim_name, len)));*/
286  start = i + 1;
287  idxDim++;
288  }
289  }
290 
291  Tag meshTypeTag = 0;
292  tag_name = "__MESH_TYPE";
293  data = NULL;
294  int meshTypeSz = 0;
295  rval = mbImpl->tag_get_handle( tag_name.c_str(), 0, MB_TYPE_OPAQUE, meshTypeTag, MB_TAG_ANY );MB_CHK_SET_ERR( rval, "Trouble getting conventional tag " << tag_name );
296  rval = mbImpl->tag_get_by_ptr( meshTypeTag, &fileSet, 1, &data, &meshTypeSz );MB_CHK_SET_ERR( rval, "Trouble getting data of conventional tag " << tag_name );
297  p = static_cast< const char* >( data );
298  grid_type = std::string( &p[0], meshTypeSz );
299  dbgOut.tprintf( 2, "Mesh type: %s\n", grid_type.c_str() );
300 
301  // Read <__VAR_NAMES_LOCATIONS> tag
302  Tag varNamesLocsTag = 0;
303  tag_name = "__VAR_NAMES_LOCATIONS";
304  data = NULL;
305  int varNamesLocsSz = 0;
306  rval = mbImpl->tag_get_handle( tag_name.c_str(), 0, MB_TYPE_INTEGER, varNamesLocsTag, MB_TAG_ANY );MB_CHK_SET_ERR( rval, "Trouble getting conventional tag " << tag_name );
307  rval = mbImpl->tag_get_by_ptr( varNamesLocsTag, &fileSet, 1, &data, &varNamesLocsSz );MB_CHK_SET_ERR( rval, "Trouble getting data of conventional tag " << tag_name );
308  int_p = static_cast< const int* >( data );
309  std::vector< int > varNamesLocs( varNamesLocsSz );
310  std::copy( int_p, int_p + varNamesLocsSz, varNamesLocs.begin() );
311 
312  Tag varNamesTag = 0;
313  tag_name = "__VAR_NAMES";
314  rval = mbImpl->tag_get_handle( tag_name.c_str(), 0, MB_TYPE_OPAQUE, varNamesTag, MB_TAG_ANY );MB_CHK_SET_ERR( rval, "Trouble getting conventional tag " << tag_name );
315  data = NULL;
316  int varNamesSz = 0;
317  rval = mbImpl->tag_get_by_ptr( varNamesTag, &fileSet, 1, &data, &varNamesSz );MB_CHK_SET_ERR( rval, "Trouble getting data of conventional tag " << tag_name );
318  dbgOut.tprintf( 2, "__VAR_NAMES tag has string length %d\n", varNamesSz );
319  p = static_cast< const char* >( data );
320 
321  start = 0;
322  int idxVar = 0;
323  int sz;
324  // Var names are separated by '\0' in the string of __VAR_NAMES tag
325  for( std::size_t i = 0; i != static_cast< std::size_t >( varNamesSz ); i++ )
326  {
327  if( p[i] == '\0' )
328  {
329  std::string var_name( &p[start], i - start );
330 
331  dbgOut.tprintf( 2, "var name: %s index %d \n", var_name.c_str(), idxVar );
332  // Process var name:
333  // This will create/initiate map; we will populate variableDataStruct with info about
334  // dims, tags, etc reference & is important; otherwise variableDataStruct will go out of
335  // scope, and deleted :(
336  VarData& variableDataStruct = varInfo[var_name];
337  variableDataStruct.varName = var_name;
338  variableDataStruct.entLoc = varNamesLocs[idxVar];
339 
340  dbgOut.tprintf( 2, "at var name %s varInfo size %d \n", var_name.c_str(), (int)varInfo.size() );
341 
342  sz = 0;
343  Tag dims_tag = 0;
344  std::string dim_names = "__" + var_name + "_DIMS";
345  rval = mbImpl->tag_get_handle( dim_names.c_str(), 0, MB_TYPE_OPAQUE, dims_tag, MB_TAG_ANY );
346  if( MB_SUCCESS != rval )
347  {
348  if( MB_TAG_NOT_FOUND == rval )
349  {
350  dbgOut.tprintf( 2, "tag : %s not found, continue \n", dim_names.c_str() );
351  start = i + 1;
352  idxVar++;
353  continue;
354  }
355  MB_SET_ERR( rval, "Trouble getting conventional tag " << dim_names );
356  }
357  rval = mbImpl->tag_get_length( dims_tag, sz );MB_CHK_SET_ERR( rval, "Trouble getting size of dimensions for variable " << var_name );
358  sz /= sizeof( Tag ); // The type is MB_TYPE_OPAQUE, but it is a list of tags, so we
359  // need to divide by the size of Tag
360  // sz is used for number of dimension tags in this list
361  dbgOut.tprintf( 2, "var name: %s has %d dimensions \n", var_name.c_str(), sz );
362 
363  variableDataStruct.varDims.resize( sz );
364  const void* ptr = NULL;
365  rval = mbImpl->tag_get_by_ptr( dims_tag, &fileSet, 1, &ptr );
366 
367  const Tag* ptags = static_cast< const moab::Tag* >( ptr );
368  for( std::size_t j = 0; j != static_cast< std::size_t >( sz ); j++ )
369  {
370  std::string dim_name;
371  rval = mbImpl->tag_get_name( ptags[j], dim_name );MB_CHK_SET_ERR( rval, "Trouble getting dimension of variable " << var_name );
372  dbgOut.tprintf( 2, "var name: %s has %s as dimension \n", var_name.c_str(), dim_name.c_str() );
373  std::vector< std::string >::iterator vit = std::find( dimNames.begin(), dimNames.end(), dim_name );
374  if( vit == dimNames.end() )
375  MB_SET_ERR( MB_FAILURE, "Dimension " << dim_name << " not found for variable " << var_name );
376  variableDataStruct.varDims[j] = (int)( vit - dimNames.begin() ); // Will be used for writing
377  // This will have to change to actual file dimension, for writing
378  }
379 
380  // Attributes for this variable
381  std::stringstream ssTagName;
382  ssTagName << "__" << var_name << "_ATTRIBS";
383  tag_name = ssTagName.str();
384  Tag varAttTag = 0;
385  rval =
386  mbImpl->tag_get_handle( tag_name.c_str(), 0, MB_TYPE_OPAQUE, varAttTag, MB_TAG_SPARSE | MB_TAG_VARLEN );MB_CHK_SET_ERR( rval, "Trouble getting conventional tag " << tag_name );
387  const void* varAttPtr = NULL;
388  int varAttSz = 0;
389  rval = mbImpl->tag_get_by_ptr( varAttTag, &fileSet, 1, &varAttPtr, &varAttSz );MB_CHK_SET_ERR( rval, "Trouble getting data of conventional tag " << tag_name );
390  if( MB_SUCCESS == rval ) dbgOut.tprintf( 2, "Tag retrieved for variable %s\n", tag_name.c_str() );
391 
392  std::string attribString( (char*)varAttPtr, (char*)varAttPtr + varAttSz );
393  if( attribString == "NO_ATTRIBS" )
394  {
395  // This variable has no attributes
396  variableDataStruct.numAtts = 0;
397  }
398  else if( attribString == "DUMMY_VAR" )
399  {
400  // This variable is a dummy coordinate variable
401  variableDataStruct.numAtts = 0;
402  dummyVarNames.insert( variableDataStruct.varName );
403  }
404  else
405  {
406  ssTagName << "_LEN";
407  tag_name = ssTagName.str();
408  Tag varAttLenTag = 0;
409  rval = mbImpl->tag_get_handle( tag_name.c_str(), 0, MB_TYPE_INTEGER, varAttLenTag, MB_TAG_ANY );MB_CHK_SET_ERR( rval, "Trouble getting conventional tag " << tag_name );
410  int varAttLenSz = 0;
411  rval = mbImpl->tag_get_length( varAttLenTag, varAttLenSz );MB_CHK_SET_ERR( rval, "Trouble getting length of conventional tag " << tag_name );
412  std::vector< int > varAttLen( varAttLenSz );
413  rval = mbImpl->tag_get_data( varAttLenTag, &fileSet, 1, &varAttLen[0] );MB_CHK_SET_ERR( rval, "Trouble getting data of conventional tag " << tag_name );
414 
415  rval = process_concatenated_attribute( varAttPtr, varAttSz, varAttLen, variableDataStruct.varAtts );MB_CHK_SET_ERR( rval, "Trouble processing attributes of variable " << var_name );
416 
417  if( MB_SUCCESS == rval ) dbgOut.tprintf( 2, "Tag metadata for variable %s\n", tag_name.c_str() );
418  }
419  // End attribute
420 
421  start = i + 1;
422  idxVar++;
423  } // if (p[i] == '\0')
424  }
425 
426  // Global attributes
427  tag_name = "__GLOBAL_ATTRIBS";
428  Tag globalAttTag = 0;
429  rval = mbImpl->tag_get_handle( tag_name.c_str(), 0, MB_TYPE_OPAQUE, globalAttTag, MB_TAG_SPARSE | MB_TAG_VARLEN );MB_CHK_SET_ERR( rval, "Trouble getting conventional tag " << tag_name );
430  std::vector< int > gattLen;
431 
432  const void* gattptr = NULL;
433  int globalAttSz = 0;
434  rval = mbImpl->tag_get_by_ptr( globalAttTag, &fileSet, 1, &gattptr, &globalAttSz );MB_CHK_SET_ERR( rval, "Trouble getting data of conventional tag " << tag_name );
435 
436  if( MB_SUCCESS == rval ) dbgOut.tprintf( 2, "Tag value retrieved for %s size %d\n", tag_name.c_str(), globalAttSz );
437 
438  // <__GLOBAL_ATTRIBS_LEN>
439  tag_name = "__GLOBAL_ATTRIBS_LEN";
440  Tag globalAttLenTag = 0;
441 
442  rval = mbImpl->tag_get_handle( tag_name.c_str(), 0, MB_TYPE_INTEGER, globalAttLenTag, MB_TAG_ANY );MB_CHK_SET_ERR( rval, "Trouble getting conventional tag " << tag_name );
443  int sizeGAtt = 0;
444  rval = mbImpl->tag_get_length( globalAttLenTag, sizeGAtt );MB_CHK_SET_ERR( rval, "Trouble getting length of conventional tag " << tag_name );
445  gattLen.resize( sizeGAtt );
446  rval = mbImpl->tag_get_data( globalAttLenTag, &fileSet, 1, &gattLen[0] );MB_CHK_SET_ERR( rval, "Trouble getting data of conventional tag " << tag_name );
447  if( MB_SUCCESS == rval ) dbgOut.tprintf( 2, "Tag retrieved for variable %s\n", tag_name.c_str() );
448 
449  rval = process_concatenated_attribute( gattptr, globalAttSz, gattLen, globalAtts );MB_CHK_SET_ERR( rval, "Trouble processing global attributes" );
450 
451  return MB_SUCCESS;
452 }

References dbgOut, dimLens, dimNames, dummyVarNames, moab::WriteNC::VarData::entLoc, ErrorCode, globalAtts, grid_type, MB_CHK_SET_ERR, MB_SET_ERR, MB_SUCCESS, MB_TAG_ANY, MB_TAG_NOT_FOUND, MB_TAG_SPARSE, MB_TAG_VARLEN, MB_TYPE_INTEGER, MB_TYPE_OPAQUE, mbImpl, moab::WriteNC::VarData::numAtts, process_concatenated_attribute(), moab::Interface::tag_get_by_ptr(), moab::Interface::tag_get_data(), moab::Interface::tag_get_handle(), moab::Interface::tag_get_length(), moab::Interface::tag_get_name(), moab::DebugOutput::tprintf(), moab::WriteNC::VarData::varAtts, moab::WriteNC::VarData::varDims, varInfo, and moab::WriteNC::VarData::varName.

Referenced by write_file().

◆ write_file()

ErrorCode moab::WriteNC::write_file ( const char *  file_name,
const bool  overwrite,
const FileOptions opts,
const EntityHandle output_list,
const int  num_sets,
const std::vector< std::string > &  qa_list,
const Tag tag_list = NULL,
int  num_tags = 0,
int  export_dimension = 3 
)
virtual

Writes out a file.

Implements moab::WriterIface.

Definition at line 50 of file WriteNC.cpp.

59 {
60  ErrorCode rval;
61  // See if opts has variable(s) specified
62  std::vector< std::string > var_names;
63  std::vector< std::string > desired_names;
64  std::vector< int > tstep_nums;
65  std::vector< double > tstep_vals;
66 
67  // Get and cache predefined tag handles
69 
70  // num set has to be 1, we will write only one set, the original file set used to load
71  if( num_set != 1 ) MB_SET_ERR( MB_FAILURE, "We should write only one set (the file set used to read data into)" );
72 
73  rval = parse_options( options, var_names, desired_names, tstep_nums, tstep_vals );MB_CHK_SET_ERR( rval, "Trouble parsing option string" );
74 
75  // Important to create some data that will be used to write the file; dimensions, variables, etc
76  // new variables still need to have some way of defining their dimensions
77  // maybe it will be passed as write options
78  rval = process_conventional_tags( *file_set );MB_CHK_SET_ERR( rval, "Trouble processing conventional tags" );
79 
80  // Create or append the file
81  if( append )
82  dbgOut.tprintf( 1, "opening file %s for appending \n", file_name );
83  else
84  dbgOut.tprintf( 1, "creating file %s\n", file_name );
85  fileName = file_name;
86  int success;
87 
88  if( append )
89  {
90  int omode = NC_WRITE;
91 #ifdef MOAB_HAVE_PNETCDF
92  if( isParallel )
93  success = NCFUNC( open )( myPcomm->proc_config().proc_comm(), file_name, omode, MPI_INFO_NULL, &fileId );
94  else
95  success = NCFUNC( open )( MPI_COMM_SELF, file_name, omode, MPI_INFO_NULL, &fileId );
96 #else
97  // This is a regular netcdf file, open in write mode
98  success = NCFUNC( open )( file_name, omode, &fileId );
99 #endif
100  if( success ) MB_SET_ERR( MB_FAILURE, "Trouble opening file " << file_name << " for appending" );
101  }
102  else
103  { // Case when the file is new, will be overwritten, most likely
104  int cmode = overwrite ? NC_CLOBBER : NC_NOCLOBBER;
105 #ifdef MOAB_HAVE_PNETCDF
106  if( isParallel )
107  success = NCFUNC( create )( myPcomm->proc_config().proc_comm(), file_name, cmode, MPI_INFO_NULL, &fileId );
108  else
109  success = NCFUNC( create )( MPI_COMM_SELF, file_name, cmode, MPI_INFO_NULL, &fileId );
110 #else
111  // This is a regular netcdf file
112  success = NCFUNC( create )( file_name, cmode, &fileId );
113 #endif
114  if( success ) MB_SET_ERR( MB_FAILURE, "Trouble creating file " << file_name << " for writing" );
115  }
116 
117  if( NULL != myHelper ) delete myHelper;
118 
119  // Get appropriate helper instance for WriteNC class based on some info in the file set
120  myHelper = NCWriteHelper::get_nc_helper( this, fileId, options, *file_set );
121  if( NULL == myHelper )
122  {
123  MB_SET_ERR( MB_FAILURE, "Failed to get NCWriteHelper class instance" );
124  }
125 
126  rval = myHelper->collect_mesh_info();MB_CHK_SET_ERR( rval, "Trouble collecting mesh information" );
127 
128  rval = myHelper->collect_variable_data( var_names, tstep_nums );MB_CHK_SET_ERR( rval, "Trouble collecting variable data" );
129 
130  rval = myHelper->init_file( var_names, desired_names, append );MB_CHK_SET_ERR( rval, "Trouble initializing file" );
131 
132  rval = myHelper->write_values( var_names, tstep_nums );MB_CHK_SET_ERR( rval, "Trouble writing values to file" );
133 
134  success = NCFUNC( close )( fileId );
135  if( success ) MB_SET_ERR( MB_FAILURE, "Trouble closing file" );
136 
137  return MB_SUCCESS;
138 }

References append, moab::NCWriteHelper::collect_mesh_info(), moab::NCWriteHelper::collect_variable_data(), dbgOut, ErrorCode, fileId, fileName, moab::NCWriteHelper::get_nc_helper(), moab::Interface::globalId_tag(), moab::NCWriteHelper::init_file(), isParallel, MB_CHK_SET_ERR, MB_SET_ERR, MB_SUCCESS, mbImpl, mGlobalIdTag, myHelper, NCFUNC, parse_options(), process_conventional_tags(), moab::DebugOutput::tprintf(), and moab::NCWriteHelper::write_values().

Friends And Related Function Documentation

◆ NCWriteEuler

friend class NCWriteEuler
friend

Definition at line 75 of file WriteNC.hpp.

◆ NCWriteFV

friend class NCWriteFV
friend

Definition at line 76 of file WriteNC.hpp.

◆ NCWriteGCRM

friend class NCWriteGCRM
friend

Definition at line 79 of file WriteNC.hpp.

◆ NCWriteHelper

friend class NCWriteHelper
friend

Definition at line 72 of file WriteNC.hpp.

◆ NCWriteHOMME

friend class NCWriteHOMME
friend

Definition at line 77 of file WriteNC.hpp.

◆ NCWriteMPAS

friend class NCWriteMPAS
friend

Definition at line 78 of file WriteNC.hpp.

◆ ScdNCWriteHelper

friend class ScdNCWriteHelper
friend

Definition at line 73 of file WriteNC.hpp.

◆ UcdNCWriteHelper

friend class UcdNCWriteHelper
friend

Definition at line 74 of file WriteNC.hpp.

Member Data Documentation

◆ append

bool moab::WriteNC::append
private

Definition at line 202 of file WriteNC.hpp.

Referenced by parse_options(), and write_file().

◆ dbgOut

◆ dimLens

◆ dimNames

◆ dummyVarNames

std::set< std::string > moab::WriteNC::dummyVarNames
private

Dummy variables (for dimensions that have no corresponding coordinate variables)

Definition at line 158 of file WriteNC.hpp.

Referenced by moab::NCWriteHelper::collect_variable_data(), moab::NCWriteHelper::init_file(), process_conventional_tags(), and moab::NCWriteHelper::write_values().

◆ fileId

int moab::WriteNC::fileId
private

File numbers assigned by (p)netcdf.

Definition at line 190 of file WriteNC.hpp.

Referenced by write_file().

◆ fileName

const char* moab::WriteNC::fileName
private

File var.

Definition at line 187 of file WriteNC.hpp.

Referenced by write_file().

◆ globalAtts

std::map< std::string, AttData > moab::WriteNC::globalAtts
private

Global attribs.

Definition at line 161 of file WriteNC.hpp.

Referenced by moab::NCWriteHelper::init_file(), and process_conventional_tags().

◆ grid_type

std::string moab::WriteNC::grid_type
private

CAM Euler, etc,.

Definition at line 211 of file WriteNC.hpp.

Referenced by moab::NCWriteHelper::get_nc_helper(), and process_conventional_tags().

◆ isParallel

◆ mbImpl

◆ mGlobalIdTag

Tag moab::WriteNC::mGlobalIdTag
private

Cached tags for writing. This will be important for ordering the data, in parallel.

Definition at line 205 of file WriteNC.hpp.

Referenced by moab::NCWriteGCRM::collect_mesh_info(), moab::NCWriteHOMME::collect_mesh_info(), moab::NCWriteMPAS::collect_mesh_info(), and write_file().

◆ mWriteIface

WriteUtilIface* moab::WriteNC::mWriteIface
private

Definition at line 184 of file WriteNC.hpp.

Referenced by WriteNC(), and ~WriteNC().

◆ myHelper

NCWriteHelper* moab::WriteNC::myHelper
private

Helper class instance.

Definition at line 214 of file WriteNC.hpp.

Referenced by write_file(), and ~WriteNC().

◆ noMesh

bool moab::WriteNC::noMesh
private

Write options.

Definition at line 200 of file WriteNC.hpp.

Referenced by parse_options().

◆ noVars

bool moab::WriteNC::noVars
private

Definition at line 201 of file WriteNC.hpp.

Referenced by parse_options().

◆ usedCoordinates

std::set< std::string > moab::WriteNC::usedCoordinates
private

Will collect used dimensions (coordinate variables)

Definition at line 155 of file WriteNC.hpp.

Referenced by moab::NCWriteHelper::collect_variable_data(), moab::NCWriteHelper::init_file(), and moab::NCWriteHelper::write_values().

◆ varInfo


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