38 std::string& grid_type = writeNC->
grid_type;
39 if( grid_type ==
"CAM_EUL" )
40 return new( std::nothrow )
NCWriteEuler( writeNC, fileId, opts, fileSet );
41 else if( grid_type ==
"CAM_FV" )
42 return new( std::nothrow )
NCWriteFV( writeNC, fileId, opts, fileSet );
43 else if( grid_type ==
"CAM_SE" )
44 return new( std::nothrow )
NCWriteHOMME( writeNC, fileId, opts, fileSet );
45 else if( grid_type ==
"MPAS" )
46 return new( std::nothrow )
NCWriteMPAS( writeNC, fileId, opts, fileSet );
47 else if( grid_type ==
"GCRM" )
48 return new( std::nothrow )
NCWriteGCRM( writeNC, fileId, opts, fileSet );
49 else if( grid_type ==
"SCRIP" )
50 return new( std::nothrow )
NCWriteScrip( writeNC, fileId, opts, fileSet );
51 else if( grid_type ==
"ESMF" )
52 return new( std::nothrow )
NCWriteESMF( writeNC, fileId, opts, fileSet );
53 else if( grid_type ==
"DOMAIN" )
54 return new( std::nothrow )
NCWriteDomain( writeNC, fileId, opts, fileSet );
70 usedCoordinates.clear();
75 tstep_nums.push_back( i );
78 for(
size_t i = 0; i < var_names.size(); i++ )
80 std::string varname = var_names[i];
81 std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( varname );
82 if( vit == varInfo.end() )
MB_SET_ERR( MB_FAILURE,
"Can't find variable " << varname );
86 dbgOut.
tprintf( 2,
" for variable %s varDims.size %d \n", varname.c_str(),
87 (
int)currentVarData.
varDims.size() );
88 for(
size_t j = 0; j < currentVarData.
varDims.size(); j++ )
90 std::string dimName = dimNames[currentVarData.
varDims[j]];
91 vit = varInfo.find( dimName );
92 if( vit == varInfo.end() )
MB_SET_ERR( MB_FAILURE,
"Can't find coordinate variable " << dimName );
94 usedCoordinates.insert( dimName );
95 dbgOut.
tprintf( 2,
" for variable %s need dimension %s with length %d\n", varname.c_str(),
96 dimName.c_str(), dimLens[currentVarData.
varDims[j]] );
100 if( usedCoordinates.find( varname ) != usedCoordinates.end() )
continue;
103 if( std::find( currentVarData.
varDims.begin(), currentVarData.
varDims.end(),
tDim ) !=
109 currentVarData.
varDims.end() ) )
126 currentVarData.
varTags.push_back( tag );
130 "Can't get data of tag " << varname );
139 assert( 0 == currentVarData.
memoryHogs.size() );
140 currentVarData.
memoryHogs.push_back( (
void*)data );
142 if( currentVarData.
varDims.empty() )
150 for(
size_t j = 0; j < currentVarData.
varDims.size(); j++ )
158 currentVarData.
sz = 1;
159 for( std::size_t idx = 0; idx != currentVarData.
writeCounts.size(); idx++ )
170 for(
unsigned int t = 0; t < tstep_nums.size(); t++ )
172 std::stringstream ssTagNameWithIndex;
173 ssTagNameWithIndex << varname << tstep_nums[t];
175 "Can't find tag " << ssTagNameWithIndex.str() );
176 dbgOut.
tprintf( 2,
" found indexed tag %d with name %s\n", tstep_nums[t],
177 ssTagNameWithIndex.str().c_str() );
178 currentVarData.
varTags.push_back( indexedTag );
185 std::stringstream ssTagNameWithIndex;
186 ssTagNameWithIndex << varname << 0;
188 "Can't find tag " << ssTagNameWithIndex.str() <<
" for a user-created variable" );
189 dbgOut.
tprintf( 2,
" found indexed tag 0 with name %s\n", ssTagNameWithIndex.str().c_str() );
190 currentVarData.
varTags.push_back( indexedTag );
204 for( std::set< std::string >::iterator setIt = usedCoordinates.begin(); setIt != usedCoordinates.end(); ++setIt )
206 const std::string& coordName = *setIt;
208 std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( coordName );
209 if( vit == varInfo.end() )
MB_SET_ERR( MB_FAILURE,
"Can't find coordinate variable " << coordName );
214 varCoordData.
varTags.push_back( coordTag );
219 "Can't get coordinate values of " << coordName );
220 dbgOut.
tprintf( 2,
" found coordinate tag with name %s and length %d\n", coordName.c_str(), sizeCoordinate );
229 assert( 1 == varCoordData.
varDims.size() );
230 int coordDimLen = dimLens[varCoordData.
varDims[0]];
232 if( dummyVarNames.find( coordName ) != dummyVarNames.end() )
236 assert( 1 == sizeCoordinate );
237 sizeCoordinate = coordDimLen;
247 if( varCoordData.
varDims[0] !=
tDim ) assert( sizeCoordinate == coordDimLen );
255 if( NULL != data ) assert( tstep_nums.size() > 0 && tstep_nums.size() <= (
size_t)sizeCoordinate );
257 sizeCoordinate = tstep_nums.size();
263 for(
unsigned int t = 0; t < tstep_nums.size(); t++ )
271 varCoordData.
sz = sizeCoordinate;
277 assert( 0 == varCoordData.
memoryHogs.size() );
278 varCoordData.
memoryHogs.push_back( (
void*)data );
285 std::vector< std::string >& desired_names,
291 std::map< std::string, WriteNC::VarData >& varInfo =
_writeNC->
varInfo;
295 int tDim_in_dimNames =
tDim;
296 int levDim_in_dimNames =
levDim;
303 if( errcode != NC_NOERR )
MB_SET_ERR( MB_FAILURE,
"Can't open file in redefine mode" );
308 for( std::set< std::string >::iterator setIt = usedCoordinates.begin(); setIt != usedCoordinates.end(); ++setIt )
310 const std::string& coordName = *setIt;
312 std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( coordName );
313 if( vit == varInfo.end() )
MB_SET_ERR( MB_FAILURE,
"Can't find coordinate variable " << coordName );
316 varCoordData.
varDims.resize( 1 );
326 if(
NCFUNC( inq_dimid )(
_fileId, coordName.c_str(), &dimId ) == NC_NOERR )
328 varCoordData.
varDims[0] = dimId;
329 dbgOut.
tprintf( 2,
" file already has coordName %s dim id is %d \n", coordName.c_str(),
330 (
int)varCoordData.
varDims[0] );
333 if( coordName == dimNames[tDim_in_dimNames] )
335 else if( coordName == dimNames[levDim_in_dimNames] )
339 if( dummyVarNames.find( coordName ) != dummyVarNames.end() )
continue;
344 if(
NCFUNC( inq_varid )(
_fileId, coordName.c_str(), &varId ) != NC_NOERR )
345 MB_SET_ERR( MB_FAILURE,
"We do not have a variable with the same name " << coordName );
347 varCoordData.
varId = varId;
348 dbgOut.
tprintf( 2,
" file already has coordinate %s and varId is %d \n", coordName.c_str(), varId );
361 MB_SET_ERR( MB_FAILURE,
"Failed to generate dimension " << coordName );
362 dbgOut.
tprintf( 2,
" for coordName %s dim id is %d \n", coordName.c_str(), (
int)varCoordData.
varDims[0] );
365 if( coordName == dimNames[tDim_in_dimNames] )
367 else if( coordName == dimNames[levDim_in_dimNames] )
379 if( dummyVarNames.find( coordName ) != dummyVarNames.end() )
continue;
383 &varCoordData.
varId ) != NC_NOERR )
384 MB_SET_ERR( MB_FAILURE,
"Failed to create coordinate variable " << coordName );
386 dbgOut.
tprintf( 2,
" for coordName %s variable id is %d \n", coordName.c_str(), varCoordData.
varId );
391 for(
size_t i = 0; i < var_names.size(); i++ )
393 std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( var_names[i] );
394 if( vit == varInfo.end() )
MB_SET_ERR( MB_FAILURE,
"Can't find requested variable " << var_names[i] );
397 if( usedCoordinates.find( var_names[i] ) != usedCoordinates.end() )
continue;
402 int numDims = (int)variableData.
varDims.size();
403 for(
int j = 0; j < numDims; j++ )
405 std::string dimName = dimNames[variableData.
varDims[j]];
406 std::map< std::string, WriteNC::VarData >::iterator vit2 = varInfo.find( dimName );
407 if( vit2 == varInfo.end() )
408 MB_SET_ERR( MB_FAILURE,
"Can't find requested coordinate variable " << dimName );
413 dbgOut.
tprintf( 2,
" dimension with index %d name %s has ID %d \n", j, dimName.c_str(),
421 if( errCode != NC_NOERR )
MB_SET_ERR( MB_FAILURE,
"Failed to create requested variable " << desired_names[i] );
423 dbgOut.
tprintf( 2,
" for variable %s with desired name %s variable id is %d \n", var_names[i].c_str(),
424 desired_names[i].c_str(), variableData.
varId );
430 std::map< std::string, WriteNC::AttData >::iterator attIt;
431 for( attIt = globalAtts.begin(); attIt != globalAtts.end(); ++attIt )
433 const std::string& attName = attIt->first;
437 const std::string& attValue = attData.
attValue;
439 switch( attDataType )
444 NCFUNC( put_att_text )(
_fileId, NC_GLOBAL, attName.c_str(), attLen, attValue.c_str() ) )
445 MB_SET_ERR( MB_FAILURE,
"Failed to define text type attribute" );
448 if( NC_NOERR !=
NCFUNC( put_att_double )(
_fileId, NC_GLOBAL, attName.c_str(), NC_DOUBLE, 1,
449 (
double*)attValue.c_str() ) )
450 MB_SET_ERR( MB_FAILURE,
"Failed to define double type attribute" );
453 if( NC_NOERR !=
NCFUNC( put_att_float )(
_fileId, NC_GLOBAL, attName.c_str(), NC_FLOAT, 1,
454 (
float*)attValue.c_str() ) )
455 MB_SET_ERR( MB_FAILURE,
"Failed to define float type attribute" );
459 NCFUNC( put_att_int )(
_fileId, NC_GLOBAL, attName.c_str(), NC_INT, 1, (
int*)attValue.c_str() ) )
460 MB_SET_ERR( MB_FAILURE,
"Failed to define int type attribute" );
463 if( NC_NOERR !=
NCFUNC( put_att_short )(
_fileId, NC_GLOBAL, attName.c_str(), NC_SHORT, 1,
464 (
short*)attValue.c_str() ) )
465 MB_SET_ERR( MB_FAILURE,
"Failed to define short type attribute" );
468 MB_SET_ERR( MB_FAILURE,
"Unknown attribute data type" );
482 std::map< std::string, WriteNC::VarData >& varInfo =
_writeNC->
varInfo;
484 std::vector< WriteNC::VarData > vdatas;
485 std::vector< WriteNC::VarData > vsetdatas;
488 for( std::set< std::string >::iterator setIt = usedCoordinates.begin(); setIt != usedCoordinates.end(); ++setIt )
490 const std::string& coordName = *setIt;
493 if( dummyVarNames.find( coordName ) != dummyVarNames.end() )
continue;
495 std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( coordName );
496 if( vit == varInfo.end() )
498 MB_SET_ERR( MB_FAILURE,
"Can't find coordinate variable " << coordName );
501 vsetdatas.push_back( vit->second );
505 for(
unsigned int i = 0; i < var_names.size(); i++ )
507 std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( var_names[i] );
508 if( vit == varInfo.end() )
510 MB_SET_ERR( MB_FAILURE,
"Can't find requested variable " << var_names[i] );
517 if( usedCoordinates.find( var_names[i] ) != usedCoordinates.end() )
continue;
519 vsetdatas.push_back( variableData );
522 vdatas.push_back( variableData );
536 std::vector< int >& )
543 #ifdef MOAB_HAVE_PNETCDF
546 if( success )
MB_SET_ERR( MB_FAILURE,
"Failed to begin independent I/O mode" );
560 for(
unsigned int i = 0; i < vsetdatas.size(); i++ )
578 MB_SET_ERR( MB_FAILURE,
"Failed to write double data for variable " << variableData.
varName );
586 MB_SET_ERR( MB_FAILURE,
"Failed to write int data for variable " << variableData.
varName );
594 #ifdef MOAB_HAVE_PNETCDF
597 if( success )
MB_SET_ERR( MB_FAILURE,
"Failed to end independent I/O mode" );
610 std::vector< std::string >::iterator vecIt;
611 if( ( vecIt = std::find( dimNames.begin(), dimNames.end(),
"time" ) ) != dimNames.end() )
612 tDim = vecIt - dimNames.begin();
613 else if( ( vecIt = std::find( dimNames.begin(), dimNames.end(),
"t" ) ) != dimNames.end() )
614 tDim = vecIt - dimNames.begin();
617 MB_SET_ERR( MB_FAILURE,
"Couldn't find 'time' or 't' dimension" );
622 if( ( vecIt = std::find( dimNames.begin(), dimNames.end(),
"lev" ) ) != dimNames.end() )
623 levDim = vecIt - dimNames.begin();
624 else if( ( vecIt = std::find( dimNames.begin(), dimNames.end(),
"ilev" ) ) != dimNames.end() )
625 levDim = vecIt - dimNames.begin();
628 MB_SET_ERR( MB_FAILURE,
"Couldn't find 'lev' or 'ilev' dimension" );
635 "Trouble getting conventional tag __slon_LOC_MINMAX" );
638 "Trouble getting data of conventional tag __slon_LOC_MINMAX" );
643 "Trouble getting conventional tag __slat_LOC_MINMAX" );
645 "Trouble getting data of conventional tag __slat_LOC_MINMAX" );
650 "Trouble getting conventional tag __lon_LOC_MINMAX" );
652 "Trouble getting data of conventional tag __lon_LOC_MINMAX" );
657 "Trouble getting conventional tag __lat_LOC_MINMAX" );
659 "Trouble getting data of conventional tag __lat_LOC_MINMAX" );
665 "Trouble getting local faces in current file set" );
677 "Trouble getting owned faces in current file set" );
686 std::vector< int >& tstep_nums )
690 std::map< std::string, WriteNC::VarData >& varInfo =
_writeNC->
varInfo;
692 for(
size_t i = 0; i < var_names.size(); i++ )
694 std::string varname = var_names[i];
695 std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( varname );
696 if( vit == varInfo.end() )
MB_SET_ERR( MB_FAILURE,
"Can't find variable " << varname );
700 std::vector< int >& varDims = currentVarData.
varDims;
710 unsigned int dim_idx = 0;
718 assert( 4 == varDims.size() || 3 == varDims.size() );
721 assert(
tDim == varDims[0] );
732 assert( 3 == varDims.size() || 2 == varDims.size() );
736 if( currentVarData.
numLev > 0 )
741 assert( 4 == varDims.size() || 3 == varDims.size() );
752 assert( 3 == varDims.size() || 2 == varDims.size() );
756 switch( currentVarData.
entLoc )
766 MB_SET_ERR( MB_FAILURE,
"Unexpected entity location type for variable " << varname );
771 currentVarData.
sz = 1;
772 for( std::size_t idx = 0; idx < dim_idx; idx++ )
783 std::vector< int >& tstep_nums )
790 for(
unsigned int i = 0; i < vdatas.size(); i++ )
795 switch( variableData.
entLoc )
801 MB_SET_ERR( MB_FAILURE,
"Unexpected entity location type for variable " << variableData.
varName );
804 unsigned int num_timesteps;
805 unsigned int lat_idx = 0;
806 unsigned int lon_idx = 1;
812 num_timesteps = tstep_nums.size();
824 unsigned int num_lev;
825 if( variableData.
numLev > 0 )
830 num_lev = variableData.
numLev;
847 for(
unsigned int t = 0; t < num_timesteps; t++ )
857 "Failed to iterate tag on owned faces" );
866 std::vector< double > tmpdoubledata( ni * nj * num_lev );
869 jik_to_kji( ni, nj, num_lev, &tmpdoubledata[0], (
double*)( dataptr ) );
873 MB_SET_ERR( MB_FAILURE,
"Failed to write double data for variable " << variableData.
varName );