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" );
90 dbgOut.
tprintf( 2,
"Skipping process_conventional_tags; grid_type already set by WRITE_FORMAT: %s\n",
96 dbgOut.
tprintf( 1,
"opening file %s for appending \n", file_name );
108 const int write_mpi_size =
isParallel ? myPcomm->proc_config().proc_size() : 1;
110 const int write_mpi_size = 1;
115 int omode = NC_WRITE;
121 int rank = myPcomm->proc_config().proc_rank();
123 MPI_Bcast( &existingFormat, 1, MPI_INT, 0, myPcomm->proc_config().proc_comm() );
134 MB_SET_ERR( MB_FAILURE,
"Cannot find a compatible parallel writer for appending to '"
135 << file_name <<
"' (file format not recognized or no compatible backend)" );
146 mbnc_open_par( backend, myPcomm->proc_config().proc_comm(), MPI_INFO_NULL, file_name, omode, &
fileId );
151 if( success )
MB_SET_ERR( MB_FAILURE,
"Trouble opening file " << file_name <<
" for appending" );
159 int cmode = overwrite ? NC_CLOBBER : NC_NOCLOBBER;
164 MB_SET_ERR( MB_FAILURE,
"Cannot find a compatible parallel writer for creating '" << file_name <<
"'" );
175 mbnc_create_par( backend, myPcomm->proc_config().proc_comm(), MPI_INFO_NULL, file_name, cmode, &
fileId );
180 if( success )
MB_SET_ERR( MB_FAILURE,
"Trouble creating file " << file_name <<
" for writing" );
189 MB_SET_ERR( MB_FAILURE,
"Failed to get NCWriteHelper class instance" );
201 if( success )
MB_SET_ERR( MB_FAILURE,
"Trouble closing file" );
207 std::vector< std::string >& var_names,
208 std::vector< std::string >& desired_names,
209 std::vector< int >& tstep_nums,
210 std::vector< double >& tstep_vals )
230 desired_names.resize( var_names.size() );
231 std::copy( var_names.begin(), var_names.end(), desired_names.begin() );
235 assert( desired_names.size() == var_names.size() );
252 std::string requestedFormat;
254 if(
MB_SUCCESS == rval && !requestedFormat.empty() )
262 if( !var_names.empty() )
264 std::cerr <<
"Variables requested: ";
265 for(
unsigned int i = 0; i < var_names.size(); i++ )
266 std::cerr << var_names[i];
267 std::cerr << std::endl;
269 if( !tstep_nums.empty() )
271 std::cerr <<
"Timesteps requested: ";
272 for(
unsigned int i = 0; i < tstep_nums.size(); i++ )
273 std::cerr << tstep_nums[i];
274 std::cerr << std::endl;
276 if( !tstep_vals.empty() )
278 std::cerr <<
"Time vals requested: ";
279 for(
unsigned int i = 0; i < tstep_vals.size(); i++ )
280 std::cerr << tstep_vals[i];
281 std::cerr << std::endl;
299 MB_SET_ERR( rval,
"Invalid value for PARALLEL_COMM option" );
308 #ifndef MOAB_HAVE_PNETCDF
309 const int procs = myPcomm->proc_config().proc_size();
316 const int rank = myPcomm->proc_config().proc_rank();
331 std::string tag_name =
"__DIM_NAMES";
332 const void* data = NULL;
335 "Trouble getting conventional tag " << tag_name );
337 "Trouble getting data of conventional tag " << tag_name );
338 const char* p =
static_cast< const char*
>( data );
339 dbgOut.
tprintf( 1,
"__DIM_NAMES tag has string length %d\n", dimNamesSz );
341 std::size_t start = 0;
344 tag_name =
"__DIM_LENS";
348 "Trouble getting conventional tag " << tag_name );
350 "Trouble getting data of conventional tag " << tag_name );
351 const int* int_p =
static_cast< const int*
>( data );
352 dbgOut.
tprintf( 1,
"__DIM_LENS tag has %d values\n", dimLensSz );
356 for( std::size_t i = 0; i !=
static_cast< std::size_t
>( dimNamesSz ); i++ )
360 std::string dim_name( &p[start], i - start );
361 int len = int_p[idxDim];
364 dbgOut.
tprintf( 2,
"Dimension %s has length %d\n", dim_name.c_str(), len );
377 tag_name =
"__MESH_TYPE";
381 "Trouble getting conventional tag " << tag_name );
383 "Trouble getting data of conventional tag " << tag_name );
384 p =
static_cast< const char*
>( data );
385 grid_type = std::string( &p[0], meshTypeSz );
389 Tag varNamesLocsTag = 0;
390 tag_name =
"__VAR_NAMES_LOCATIONS";
392 int varNamesLocsSz = 0;
394 "Trouble getting conventional tag " << tag_name );
396 "Trouble getting data of conventional tag " << tag_name );
397 int_p =
static_cast< const int*
>( data );
398 std::vector< int > varNamesLocs( varNamesLocsSz );
399 std::copy( int_p, int_p + varNamesLocsSz, varNamesLocs.begin() );
402 tag_name =
"__VAR_NAMES";
404 "Trouble getting conventional tag " << tag_name );
408 "Trouble getting data of conventional tag " << tag_name );
409 dbgOut.
tprintf( 2,
"__VAR_NAMES tag has string length %d\n", varNamesSz );
410 p =
static_cast< const char*
>( data );
416 for( std::size_t i = 0; i !=
static_cast< std::size_t
>( varNamesSz ); i++ )
420 std::string var_name( &p[start], i - start );
422 dbgOut.
tprintf( 2,
"var name: %s index %d \n", var_name.c_str(), idxVar );
428 variableDataStruct.
varName = var_name;
429 variableDataStruct.
entLoc = varNamesLocs[idxVar];
431 dbgOut.
tprintf( 2,
"at var name %s varInfo size %d \n", var_name.c_str(), (
int)
varInfo.size() );
435 std::string dim_names =
"__" + var_name +
"_DIMS";
441 dbgOut.
tprintf( 2,
"tag : %s not found, continue \n", dim_names.c_str() );
446 MB_SET_ERR( rval,
"Trouble getting conventional tag " << dim_names );
449 "Trouble getting size of dimensions for variable " << var_name );
453 dbgOut.
tprintf( 2,
"var name: %s has %d dimensions \n", var_name.c_str(), sz );
455 variableDataStruct.
varDims.resize( sz );
456 const void* ptr = NULL;
458 "Could not get tag by pointer" );
460 const Tag* ptags =
static_cast< const moab::Tag*
>( ptr );
461 for( std::size_t j = 0; j !=
static_cast< std::size_t
>( sz ); j++ )
463 std::string dim_name;
465 "Trouble getting dimension of variable " << var_name );
466 dbgOut.
tprintf( 2,
"var name: %s has %s as dimension \n", var_name.c_str(), dim_name.c_str() );
467 std::vector< std::string >::iterator vit = std::find(
dimNames.begin(),
dimNames.end(), dim_name );
469 MB_SET_ERR( MB_FAILURE,
"Dimension " << dim_name <<
" not found for variable " << var_name );
475 std::stringstream ssTagName;
476 ssTagName <<
"__" << var_name <<
"_ATTRIBS";
477 tag_name = ssTagName.str();
481 "Trouble getting conventional tag " << tag_name );
482 const void* varAttPtr = NULL;
485 "Trouble getting data of conventional tag " << tag_name );
486 dbgOut.
tprintf( 2,
"Tag retrieved for variable %s\n", tag_name.c_str() );
488 std::string attribString( (
char*)varAttPtr, (
char*)varAttPtr + varAttSz );
489 if( attribString ==
"NO_ATTRIBS" )
492 variableDataStruct.
numAtts = 0;
494 else if( attribString ==
"DUMMY_VAR" )
497 variableDataStruct.
numAtts = 0;
503 tag_name = ssTagName.str();
504 Tag varAttLenTag = 0;
507 "Trouble getting conventional tag " << tag_name );
510 "Trouble getting length of conventional tag " << tag_name );
511 std::vector< int > varAttLen( varAttLenSz );
513 "Trouble getting data of conventional tag " << tag_name );
517 "Trouble processing attributes of variable " << var_name );
519 dbgOut.
tprintf( 2,
"Tag metadata for variable %s\n", tag_name.c_str() );
529 tag_name =
"__GLOBAL_ATTRIBS";
530 Tag globalAttTag = 0;
533 "Trouble getting conventional tag " << tag_name );
534 std::vector< int > gattLen;
536 const void* gattptr = NULL;
539 "Trouble getting data of conventional tag " << tag_name );
541 dbgOut.
tprintf( 2,
"Tag value retrieved for %s size %d\n", tag_name.c_str(), globalAttSz );
544 tag_name =
"__GLOBAL_ATTRIBS_LEN";
545 Tag globalAttLenTag = 0;
548 "Trouble getting conventional tag " << tag_name );
551 "Trouble getting length of conventional tag " << tag_name );
553 gattLen.resize( sizeGAtt );
555 "Trouble getting data of conventional tag " << tag_name );
557 dbgOut.
tprintf( 2,
"Tag retrieved for variable %s\n", tag_name.c_str() );
560 "Trouble processing global attributes" );
568 std::vector< int >& attLen,
569 std::map< std::string, AttData >& attributes )
571 std::size_t start = 0;
572 std::size_t att_counter = 0;
573 std::string concatString( (
char*)attPtr, (
char*)attPtr + attSz );
575 for( std::size_t i = 0; i != (size_t)attSz; i++ )
577 if( concatString[i] ==
'\0' )
579 std::string att_name( &concatString[start], i - start );
581 while( concatString[i] !=
';' )
583 std::string data_type( &concatString[start], i - start );
586 i = attLen[att_counter];
587 if( concatString[i] !=
';' )
MB_SET_ERR( MB_FAILURE,
"Error parsing attributes" );
589 std::string data_val( &concatString[start], i - start );
592 AttData& attrib = attributes[att_name];
594 attrib.
attLen = data_val.size();
596 if( data_type ==
"char" )
598 else if( data_type ==
"double" )
600 else if( data_type ==
"float" )
602 else if( data_type ==
"int" )
604 else if( data_type ==
"short" )
608 dbgOut.
tprintf( 2,
" Process attribute %s with value %s \n", att_name.c_str(), data_val.c_str() );