35 std::string& grid_type = writeNC->
grid_type;
36 if( grid_type ==
"CAM_EUL" )
37 return new( std::nothrow )
NCWriteEuler( writeNC, fileId, opts, fileSet );
38 else if( grid_type ==
"CAM_FV" )
39 return new( std::nothrow )
NCWriteFV( writeNC, fileId, opts, fileSet );
40 else if( grid_type ==
"CAM_SE" )
41 return new( std::nothrow )
NCWriteHOMME( writeNC, fileId, opts, fileSet );
42 else if( grid_type ==
"MPAS" )
43 return new( std::nothrow )
NCWriteMPAS( writeNC, fileId, opts, fileSet );
44 else if( grid_type ==
"GCRM" )
45 return new( std::nothrow )
NCWriteGCRM( writeNC, fileId, opts, fileSet );
63 usedCoordinates.clear();
69 tstep_nums.push_back( i );
72 for(
size_t i = 0; i < var_names.size(); i++ )
74 std::string varname = var_names[i];
75 std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( varname );
76 if( vit == varInfo.end() )
MB_SET_ERR( MB_FAILURE,
"Can't find variable " << varname );
80 dbgOut.
tprintf( 2,
" for variable %s varDims.size %d \n", varname.c_str(),
81 (
int)currentVarData.
varDims.size() );
82 for(
size_t j = 0; j < currentVarData.
varDims.size(); j++ )
84 std::string dimName = dimNames[currentVarData.
varDims[j]];
85 vit = varInfo.find( dimName );
86 if( vit == varInfo.end() )
MB_SET_ERR( MB_FAILURE,
"Can't find coordinate variable " << dimName );
88 usedCoordinates.insert( dimName );
89 dbgOut.
tprintf( 2,
" for variable %s need dimension %s with length %d\n", varname.c_str(),
90 dimName.c_str(), dimLens[currentVarData.
varDims[j]] );
94 if( usedCoordinates.find( varname ) != usedCoordinates.end() )
continue;
103 currentVarData.
varDims.end() ) )
120 currentVarData.
varTags.push_back( tag );
132 assert( 0 == currentVarData.
memoryHogs.size() );
133 currentVarData.
memoryHogs.push_back( (
void*)data );
135 if( currentVarData.
varDims.empty() )
143 for(
size_t j = 0; j < currentVarData.
varDims.size(); j++ )
151 currentVarData.
sz = 1;
152 for( std::size_t idx = 0; idx != currentVarData.
writeCounts.size(); idx++ )
163 for(
unsigned int t = 0; t < tstep_nums.size(); t++ )
165 std::stringstream ssTagNameWithIndex;
166 ssTagNameWithIndex << varname << tstep_nums[t];
167 rval = mbImpl->
tag_get_handle( ssTagNameWithIndex.str().c_str(), indexedTag );
MB_CHK_SET_ERR( rval,
"Can't find tag " << ssTagNameWithIndex.str() );
168 dbgOut.
tprintf( 2,
" found indexed tag %d with name %s\n", tstep_nums[t],
169 ssTagNameWithIndex.str().c_str() );
170 currentVarData.
varTags.push_back( indexedTag );
177 std::stringstream ssTagNameWithIndex;
178 ssTagNameWithIndex << varname << 0;
179 rval = mbImpl->
tag_get_handle( ssTagNameWithIndex.str().c_str(), indexedTag );
MB_CHK_SET_ERR( rval,
"Can't find tag " << ssTagNameWithIndex.str() <<
" for a user-created variable" );
180 dbgOut.
tprintf( 2,
" found indexed tag 0 with name %s\n", ssTagNameWithIndex.str().c_str() );
181 currentVarData.
varTags.push_back( indexedTag );
195 for( std::set< std::string >::iterator setIt = usedCoordinates.begin(); setIt != usedCoordinates.end(); ++setIt )
197 const std::string& coordName = *setIt;
199 std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( coordName );
200 if( vit == varInfo.end() )
MB_SET_ERR( MB_FAILURE,
"Can't find coordinate variable " << coordName );
205 varCoordData.
varTags.push_back( coordTag );
210 dbgOut.
tprintf( 2,
" found coordinate tag with name %s and length %d\n", coordName.c_str(), sizeCoordinate );
219 assert( 1 == varCoordData.
varDims.size() );
220 int coordDimLen = dimLens[varCoordData.
varDims[0]];
222 if( dummyVarNames.find( coordName ) != dummyVarNames.end() )
226 assert( 1 == sizeCoordinate );
227 sizeCoordinate = coordDimLen;
237 if( varCoordData.
varDims[0] !=
tDim ) assert( sizeCoordinate == coordDimLen );
245 if( NULL != data ) assert( tstep_nums.size() > 0 && tstep_nums.size() <= (
size_t)sizeCoordinate );
247 sizeCoordinate = tstep_nums.size();
253 for(
unsigned int t = 0; t < tstep_nums.size(); t++ )
261 varCoordData.
sz = sizeCoordinate;
267 assert( 0 == varCoordData.
memoryHogs.size() );
268 varCoordData.
memoryHogs.push_back( (
void*)data );
275 std::vector< std::string >& desired_names,
281 std::map< std::string, WriteNC::VarData >& varInfo =
_writeNC->
varInfo;
285 int tDim_in_dimNames =
tDim;
286 int levDim_in_dimNames =
levDim;
293 if( errcode != NC_NOERR )
MB_SET_ERR( MB_FAILURE,
"Can't open file in redefine mode" );
298 for( std::set< std::string >::iterator setIt = usedCoordinates.begin(); setIt != usedCoordinates.end(); ++setIt )
300 const std::string& coordName = *setIt;
302 std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( coordName );
303 if( vit == varInfo.end() )
MB_SET_ERR( MB_FAILURE,
"Can't find coordinate variable " << coordName );
306 varCoordData.
varDims.resize( 1 );
316 if(
NCFUNC( inq_dimid )(
_fileId, coordName.c_str(), &dimId ) == NC_NOERR )
318 varCoordData.
varDims[0] = dimId;
319 dbgOut.
tprintf( 2,
" file already has coordName %s dim id is %d \n", coordName.c_str(),
320 (
int)varCoordData.
varDims[0] );
323 if( coordName == dimNames[tDim_in_dimNames] )
325 else if( coordName == dimNames[levDim_in_dimNames] )
329 if( dummyVarNames.find( coordName ) != dummyVarNames.end() )
continue;
334 if(
NCFUNC( inq_varid )(
_fileId, coordName.c_str(), &varId ) != NC_NOERR )
335 MB_SET_ERR( MB_FAILURE,
"We do not have a variable with the same name " << coordName );
337 varCoordData.
varId = varId;
338 dbgOut.
tprintf( 2,
" file already has coordinate %s and varId is %d \n", coordName.c_str(), varId );
351 MB_SET_ERR( MB_FAILURE,
"Failed to generate dimension " << coordName );
352 dbgOut.
tprintf( 2,
" for coordName %s dim id is %d \n", coordName.c_str(), (
int)varCoordData.
varDims[0] );
355 if( coordName == dimNames[tDim_in_dimNames] )
357 else if( coordName == dimNames[levDim_in_dimNames] )
369 if( dummyVarNames.find( coordName ) != dummyVarNames.end() )
continue;
373 &varCoordData.
varId ) != NC_NOERR )
374 MB_SET_ERR( MB_FAILURE,
"Failed to create coordinate variable " << coordName );
376 dbgOut.
tprintf( 2,
" for coordName %s variable id is %d \n", coordName.c_str(), varCoordData.
varId );
381 for(
size_t i = 0; i < var_names.size(); i++ )
383 std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( var_names[i] );
384 if( vit == varInfo.end() )
MB_SET_ERR( MB_FAILURE,
"Can't find requested variable " << var_names[i] );
387 if( usedCoordinates.find( var_names[i] ) != usedCoordinates.end() )
continue;
392 int numDims = (int)variableData.
varDims.size();
393 for(
int j = 0; j < numDims; j++ )
395 std::string dimName = dimNames[variableData.
varDims[j]];
396 std::map< std::string, WriteNC::VarData >::iterator vit2 = varInfo.find( dimName );
397 if( vit2 == varInfo.end() )
398 MB_SET_ERR( MB_FAILURE,
"Can't find requested coordinate variable " << dimName );
403 dbgOut.
tprintf( 2,
" dimension with index %d name %s has ID %d \n", j, dimName.c_str(),
411 if( errCode != NC_NOERR )
MB_SET_ERR( MB_FAILURE,
"Failed to create requested variable " << desired_names[i] );
413 dbgOut.
tprintf( 2,
" for variable %s with desired name %s variable id is %d \n", var_names[i].c_str(),
414 desired_names[i].c_str(), variableData.
varId );
420 std::map< std::string, WriteNC::AttData >::iterator attIt;
421 for( attIt = globalAtts.begin(); attIt != globalAtts.end(); ++attIt )
423 const std::string& attName = attIt->first;
427 const std::string& attValue = attData.
attValue;
429 switch( attDataType )
434 NCFUNC( put_att_text )(
_fileId, NC_GLOBAL, attName.c_str(), attLen, attValue.c_str() ) )
435 MB_SET_ERR( MB_FAILURE,
"Failed to define text type attribute" );
438 if( NC_NOERR !=
NCFUNC( put_att_double )(
_fileId, NC_GLOBAL, attName.c_str(), NC_DOUBLE, 1,
439 (
double*)attValue.c_str() ) )
440 MB_SET_ERR( MB_FAILURE,
"Failed to define double type attribute" );
443 if( NC_NOERR !=
NCFUNC( put_att_float )(
_fileId, NC_GLOBAL, attName.c_str(), NC_FLOAT, 1,
444 (
float*)attValue.c_str() ) )
445 MB_SET_ERR( MB_FAILURE,
"Failed to define float type attribute" );
449 NCFUNC( put_att_int )(
_fileId, NC_GLOBAL, attName.c_str(), NC_INT, 1, (
int*)attValue.c_str() ) )
450 MB_SET_ERR( MB_FAILURE,
"Failed to define int type attribute" );
453 if( NC_NOERR !=
NCFUNC( put_att_short )(
_fileId, NC_GLOBAL, attName.c_str(), NC_SHORT, 1,
454 (
short*)attValue.c_str() ) )
455 MB_SET_ERR( MB_FAILURE,
"Failed to define short type attribute" );
458 MB_SET_ERR( MB_FAILURE,
"Unknown attribute data type" );
472 std::map< std::string, WriteNC::VarData >& varInfo =
_writeNC->
varInfo;
474 std::vector< WriteNC::VarData > vdatas;
475 std::vector< WriteNC::VarData > vsetdatas;
478 for( std::set< std::string >::iterator setIt = usedCoordinates.begin(); setIt != usedCoordinates.end(); ++setIt )
480 const std::string& coordName = *setIt;
483 if( dummyVarNames.find( coordName ) != dummyVarNames.end() )
continue;
485 std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( coordName );
486 if( vit == varInfo.end() )
488 MB_SET_ERR( MB_FAILURE,
"Can't find coordinate variable " << coordName );
491 vsetdatas.push_back( vit->second );
495 for(
unsigned int i = 0; i < var_names.size(); i++ )
497 std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( var_names[i] );
498 if( vit == varInfo.end() )
500 MB_SET_ERR( MB_FAILURE,
"Can't find requested variable " << var_names[i] );
507 if( usedCoordinates.find( var_names[i] ) != usedCoordinates.end() )
continue;
509 vsetdatas.push_back( variableData );
512 vdatas.push_back( variableData );
526 std::vector< int >& )
533 #ifdef MOAB_HAVE_PNETCDF
536 if( success )
MB_SET_ERR( MB_FAILURE,
"Failed to begin independent I/O mode" );
550 for(
unsigned int i = 0; i < vsetdatas.size(); i++ )
568 MB_SET_ERR( MB_FAILURE,
"Failed to write double data for variable " << variableData.
varName );
576 MB_SET_ERR( MB_FAILURE,
"Failed to write int data for variable " << variableData.
varName );
584 #ifdef MOAB_HAVE_PNETCDF
587 if( success )
MB_SET_ERR( MB_FAILURE,
"Failed to end independent I/O mode" );
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();
609 MB_SET_ERR( MB_FAILURE,
"Couldn't find 'time' or 't' dimension" );
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();
620 MB_SET_ERR( MB_FAILURE,
"Couldn't find 'lev' or 'ilev' dimension" );
668 std::vector< int >& tstep_nums )
672 std::map< std::string, WriteNC::VarData >& varInfo =
_writeNC->
varInfo;
674 for(
size_t i = 0; i < var_names.size(); i++ )
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 );
682 std::vector< int >& varDims = currentVarData.
varDims;
692 unsigned int dim_idx = 0;
700 assert( 4 == varDims.size() || 3 == varDims.size() );
703 assert(
tDim == varDims[0] );
714 assert( 3 == varDims.size() || 2 == varDims.size() );
718 if( currentVarData.
numLev > 0 )
723 assert( 4 == varDims.size() || 3 == varDims.size() );
734 assert( 3 == varDims.size() || 2 == varDims.size() );
738 switch( currentVarData.
entLoc )
748 MB_SET_ERR( MB_FAILURE,
"Unexpected entity location type for variable " << varname );
753 currentVarData.
sz = 1;
754 for( std::size_t idx = 0; idx < dim_idx; idx++ )
765 std::vector< int >& tstep_nums )
772 for(
unsigned int i = 0; i < vdatas.size(); i++ )
777 switch( variableData.
entLoc )
783 MB_SET_ERR( MB_FAILURE,
"Unexpected entity location type for variable " << variableData.
varName );
786 unsigned int num_timesteps;
787 unsigned int lat_idx = 0;
788 unsigned int lon_idx = 1;
794 num_timesteps = tstep_nums.size();
806 unsigned int num_lev;
807 if( variableData.
numLev > 0 )
812 num_lev = variableData.
numLev;
829 for(
unsigned int t = 0; t < num_timesteps; t++ )
847 std::vector< double > tmpdoubledata( ni * nj * num_lev );
850 jik_to_kji( ni, nj, num_lev, &tmpdoubledata[0], (
double*)( dataptr ) );
854 MB_SET_ERR( MB_FAILURE,
"Failed to write double data for variable " << variableData.
varName );