33 : mbImpl( impl ), dbgOut( stderr ),
37 noMesh( false ), noVars( false ), append( false ), mGlobalIdTag( 0 ), isParallel( false ), myHelper( NULL )
39 assert( impl != NULL );
55 const std::vector< std::string >&,
61 std::vector< std::string > var_names;
62 std::vector< std::string > desired_names;
63 std::vector< int > tstep_nums;
64 std::vector< double > tstep_vals;
70 if( num_set != 1 )
MB_SET_ERR( MB_FAILURE,
"We should write only one set (the file set used to read data into)" );
73 "Trouble parsing option string" );
82 dbgOut.
tprintf( 1,
"opening file %s for appending \n", file_name );
91 #ifdef MOAB_HAVE_PNETCDF
93 success =
NCFUNC( open )( myPcomm->proc_config().proc_comm(), file_name, omode, MPI_INFO_NULL, &
fileId );
95 success =
NCFUNC( open )( MPI_COMM_SELF, file_name, omode, MPI_INFO_NULL, &
fileId );
100 if( success )
MB_SET_ERR( MB_FAILURE,
"Trouble opening file " << file_name <<
" for appending" );
104 int cmode = overwrite ? NC_CLOBBER : NC_NOCLOBBER;
105 #ifdef MOAB_HAVE_PNETCDF
107 success =
NCFUNC( create )( myPcomm->proc_config().proc_comm(), file_name, cmode, MPI_INFO_NULL, &
fileId );
109 success =
NCFUNC( create )( MPI_COMM_SELF, file_name, cmode, MPI_INFO_NULL, &
fileId );
112 success =
NCFUNC( create )( file_name, cmode, &
fileId );
114 if( success )
MB_SET_ERR( MB_FAILURE,
"Trouble creating file " << file_name <<
" for writing" );
123 MB_SET_ERR( MB_FAILURE,
"Failed to get NCWriteHelper class instance" );
135 if( success )
MB_SET_ERR( MB_FAILURE,
"Trouble closing file" );
141 std::vector< std::string >& var_names,
142 std::vector< std::string >& desired_names,
143 std::vector< int >& tstep_nums,
144 std::vector< double >& tstep_vals )
164 desired_names.resize( var_names.size() );
165 std::copy( var_names.begin(), var_names.end(), desired_names.begin() );
169 assert( desired_names.size() == var_names.size() );
181 if( !var_names.empty() )
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;
188 if( !tstep_nums.empty() )
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;
195 if( !tstep_vals.empty() )
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;
218 MB_SET_ERR( rval,
"Invalid value for PARALLEL_COMM option" );
227 #ifndef MOAB_HAVE_PNETCDF
228 const int procs = myPcomm->proc_config().proc_size();
235 const int rank = myPcomm->proc_config().proc_rank();
250 std::string tag_name =
"__DIM_NAMES";
251 const void* data = NULL;
254 "Trouble getting conventional tag " << tag_name );
256 "Trouble getting data of conventional tag " << tag_name );
257 const char* p =
static_cast< const char*
>( data );
258 dbgOut.
tprintf( 1,
"__DIM_NAMES tag has string length %d\n", dimNamesSz );
260 std::size_t start = 0;
263 tag_name =
"__DIM_LENS";
267 "Trouble getting conventional tag " << tag_name );
269 "Trouble getting data of conventional tag " << tag_name );
270 const int* int_p =
static_cast< const int*
>( data );
271 dbgOut.
tprintf( 1,
"__DIM_LENS tag has %d values\n", dimLensSz );
275 for( std::size_t i = 0; i !=
static_cast< std::size_t
>( dimNamesSz ); i++ )
279 std::string dim_name( &p[start], i - start );
280 int len = int_p[idxDim];
283 dbgOut.
tprintf( 2,
"Dimension %s has length %d\n", dim_name.c_str(), len );
296 tag_name =
"__MESH_TYPE";
300 "Trouble getting conventional tag " << tag_name );
302 "Trouble getting data of conventional tag " << tag_name );
303 p =
static_cast< const char*
>( data );
304 grid_type = std::string( &p[0], meshTypeSz );
308 Tag varNamesLocsTag = 0;
309 tag_name =
"__VAR_NAMES_LOCATIONS";
311 int varNamesLocsSz = 0;
313 "Trouble getting conventional tag " << tag_name );
315 "Trouble getting data of conventional tag " << tag_name );
316 int_p =
static_cast< const int*
>( data );
317 std::vector< int > varNamesLocs( varNamesLocsSz );
318 std::copy( int_p, int_p + varNamesLocsSz, varNamesLocs.begin() );
321 tag_name =
"__VAR_NAMES";
323 "Trouble getting conventional tag " << tag_name );
327 "Trouble getting data of conventional tag " << tag_name );
328 dbgOut.
tprintf( 2,
"__VAR_NAMES tag has string length %d\n", varNamesSz );
329 p =
static_cast< const char*
>( data );
335 for( std::size_t i = 0; i !=
static_cast< std::size_t
>( varNamesSz ); i++ )
339 std::string var_name( &p[start], i - start );
341 dbgOut.
tprintf( 2,
"var name: %s index %d \n", var_name.c_str(), idxVar );
347 variableDataStruct.
varName = var_name;
348 variableDataStruct.
entLoc = varNamesLocs[idxVar];
350 dbgOut.
tprintf( 2,
"at var name %s varInfo size %d \n", var_name.c_str(), (
int)
varInfo.size() );
354 std::string dim_names =
"__" + var_name +
"_DIMS";
360 dbgOut.
tprintf( 2,
"tag : %s not found, continue \n", dim_names.c_str() );
365 MB_SET_ERR( rval,
"Trouble getting conventional tag " << dim_names );
368 "Trouble getting size of dimensions for variable " << var_name );
372 dbgOut.
tprintf( 2,
"var name: %s has %d dimensions \n", var_name.c_str(), sz );
374 variableDataStruct.
varDims.resize( sz );
375 const void* ptr = NULL;
378 const Tag* ptags =
static_cast< const moab::Tag*
>( ptr );
379 for( std::size_t j = 0; j !=
static_cast< std::size_t
>( sz ); j++ )
381 std::string dim_name;
383 "Trouble getting dimension of variable " << var_name );
384 dbgOut.
tprintf( 2,
"var name: %s has %s as dimension \n", var_name.c_str(), dim_name.c_str() );
385 std::vector< std::string >::iterator vit = std::find(
dimNames.begin(),
dimNames.end(), dim_name );
387 MB_SET_ERR( MB_FAILURE,
"Dimension " << dim_name <<
" not found for variable " << var_name );
393 std::stringstream ssTagName;
394 ssTagName <<
"__" << var_name <<
"_ATTRIBS";
395 tag_name = ssTagName.str();
399 "Trouble getting conventional tag " << tag_name );
400 const void* varAttPtr = NULL;
403 "Trouble getting data of conventional tag " << tag_name );
406 std::string attribString( (
char*)varAttPtr, (
char*)varAttPtr + varAttSz );
407 if( attribString ==
"NO_ATTRIBS" )
410 variableDataStruct.
numAtts = 0;
412 else if( attribString ==
"DUMMY_VAR" )
415 variableDataStruct.
numAtts = 0;
421 tag_name = ssTagName.str();
422 Tag varAttLenTag = 0;
425 "Trouble getting conventional tag " << tag_name );
428 "Trouble getting length of conventional tag " << tag_name );
429 std::vector< int > varAttLen( varAttLenSz );
431 "Trouble getting data of conventional tag " << tag_name );
435 "Trouble processing attributes of variable " << var_name );
447 tag_name =
"__GLOBAL_ATTRIBS";
448 Tag globalAttTag = 0;
451 "Trouble getting conventional tag " << tag_name );
452 std::vector< int > gattLen;
454 const void* gattptr = NULL;
457 "Trouble getting data of conventional tag " << tag_name );
459 if(
MB_SUCCESS == rval )
dbgOut.
tprintf( 2,
"Tag value retrieved for %s size %d\n", tag_name.c_str(), globalAttSz );
462 tag_name =
"__GLOBAL_ATTRIBS_LEN";
463 Tag globalAttLenTag = 0;
466 "Trouble getting conventional tag " << tag_name );
469 "Trouble getting length of conventional tag " << tag_name );
470 gattLen.resize( sizeGAtt );
472 "Trouble getting data of conventional tag " << tag_name );
476 "Trouble processing global attributes" );
484 std::vector< int >& attLen,
485 std::map< std::string, AttData >& attributes )
487 std::size_t start = 0;
488 std::size_t att_counter = 0;
489 std::string concatString( (
char*)attPtr, (
char*)attPtr + attSz );
491 for( std::size_t i = 0; i != (size_t)attSz; i++ )
493 if( concatString[i] ==
'\0' )
495 std::string att_name( &concatString[start], i - start );
497 while( concatString[i] !=
';' )
499 std::string data_type( &concatString[start], i - start );
502 i = attLen[att_counter];
503 if( concatString[i] !=
';' )
MB_SET_ERR( MB_FAILURE,
"Error parsing attributes" );
505 std::string data_val( &concatString[start], i - start );
508 AttData& attrib = attributes[att_name];
510 attrib.
attLen = data_val.size();
512 if( data_type ==
"char" )
514 else if( data_type ==
"double" )
516 else if( data_type ==
"float" )
518 else if( data_type ==
"int" )
520 else if( data_type ==
"short" )
524 dbgOut.
tprintf( 2,
" Process attribute %s with value %s \n", att_name.c_str(), data_val.c_str() );