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 >&,
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;
71 if( num_set != 1 )
MB_SET_ERR( MB_FAILURE,
"We should write only one set (the file set used to read data into)" );
73 rval =
parse_options( options, var_names, desired_names, tstep_nums, tstep_vals );
MB_CHK_SET_ERR( rval,
"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;
255 const char* p =
static_cast< const char*
>( data );
256 dbgOut.
tprintf( 1,
"__DIM_NAMES tag has string length %d\n", dimNamesSz );
258 std::size_t start = 0;
261 tag_name =
"__DIM_LENS";
266 const int* int_p =
static_cast< const int*
>( data );
267 dbgOut.
tprintf( 1,
"__DIM_LENS tag has %d values\n", dimLensSz );
271 for( std::size_t i = 0; i !=
static_cast< std::size_t
>( dimNamesSz ); i++ )
275 std::string dim_name( &p[start], i - start );
276 int len = int_p[idxDim];
279 dbgOut.
tprintf( 2,
"Dimension %s has length %d\n", dim_name.c_str(), len );
292 tag_name =
"__MESH_TYPE";
297 p =
static_cast< const char*
>( data );
298 grid_type = std::string( &p[0], meshTypeSz );
302 Tag varNamesLocsTag = 0;
303 tag_name =
"__VAR_NAMES_LOCATIONS";
305 int varNamesLocsSz = 0;
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() );
313 tag_name =
"__VAR_NAMES";
318 dbgOut.
tprintf( 2,
"__VAR_NAMES tag has string length %d\n", varNamesSz );
319 p =
static_cast< const char*
>( data );
325 for( std::size_t i = 0; i !=
static_cast< std::size_t
>( varNamesSz ); i++ )
329 std::string var_name( &p[start], i - start );
331 dbgOut.
tprintf( 2,
"var name: %s index %d \n", var_name.c_str(), idxVar );
337 variableDataStruct.
varName = var_name;
338 variableDataStruct.
entLoc = varNamesLocs[idxVar];
340 dbgOut.
tprintf( 2,
"at var name %s varInfo size %d \n", var_name.c_str(), (
int)
varInfo.size() );
344 std::string dim_names =
"__" + var_name +
"_DIMS";
350 dbgOut.
tprintf( 2,
"tag : %s not found, continue \n", dim_names.c_str() );
355 MB_SET_ERR( rval,
"Trouble getting conventional tag " << dim_names );
361 dbgOut.
tprintf( 2,
"var name: %s has %d dimensions \n", var_name.c_str(), sz );
363 variableDataStruct.
varDims.resize( sz );
364 const void* ptr = NULL;
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++ )
370 std::string dim_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 );
375 MB_SET_ERR( MB_FAILURE,
"Dimension " << dim_name <<
" not found for variable " << var_name );
381 std::stringstream ssTagName;
382 ssTagName <<
"__" << var_name <<
"_ATTRIBS";
383 tag_name = ssTagName.str();
387 const void* varAttPtr = NULL;
392 std::string attribString( (
char*)varAttPtr, (
char*)varAttPtr + varAttSz );
393 if( attribString ==
"NO_ATTRIBS" )
396 variableDataStruct.
numAtts = 0;
398 else if( attribString ==
"DUMMY_VAR" )
401 variableDataStruct.
numAtts = 0;
407 tag_name = ssTagName.str();
408 Tag varAttLenTag = 0;
412 std::vector< int > varAttLen( varAttLenSz );
427 tag_name =
"__GLOBAL_ATTRIBS";
428 Tag globalAttTag = 0;
430 std::vector< int > gattLen;
432 const void* gattptr = NULL;
436 if(
MB_SUCCESS == rval )
dbgOut.
tprintf( 2,
"Tag value retrieved for %s size %d\n", tag_name.c_str(), globalAttSz );
439 tag_name =
"__GLOBAL_ATTRIBS_LEN";
440 Tag globalAttLenTag = 0;
445 gattLen.resize( sizeGAtt );
457 std::vector< int >& attLen,
458 std::map< std::string, AttData >& attributes )
460 std::size_t start = 0;
461 std::size_t att_counter = 0;
462 std::string concatString( (
char*)attPtr, (
char*)attPtr + attSz );
464 for( std::size_t i = 0; i != (size_t)attSz; i++ )
466 if( concatString[i] ==
'\0' )
468 std::string att_name( &concatString[start], i - start );
470 while( concatString[i] !=
';' )
472 std::string data_type( &concatString[start], i - start );
475 i = attLen[att_counter];
476 if( concatString[i] !=
';' )
MB_SET_ERR( MB_FAILURE,
"Error parsing attributes" );
478 std::string data_val( &concatString[start], i - start );
481 AttData& attrib = attributes[att_name];
483 attrib.
attLen = data_val.size();
485 if( data_type ==
"char" )
487 else if( data_type ==
"double" )
489 else if( data_type ==
"float" )
491 else if( data_type ==
"int" )
493 else if( data_type ==
"short" )
497 dbgOut.
tprintf( 2,
" Process attribute %s with value %s \n", att_name.c_str(), data_val.c_str() );