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 );
61 usedCoordinates.clear();
66 tstep_nums.push_back( i );
69 for(
size_t i = 0; i < var_names.size(); i++ )
71 std::string varname = var_names[i];
72 std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( varname );
73 if( vit == varInfo.end() )
MB_SET_ERR( MB_FAILURE,
"Can't find variable " << varname );
77 dbgOut.
tprintf( 2,
" for variable %s varDims.size %d \n", varname.c_str(),
78 (
int)currentVarData.
varDims.size() );
79 for(
size_t j = 0; j < currentVarData.
varDims.size(); j++ )
81 std::string dimName = dimNames[currentVarData.
varDims[j]];
82 vit = varInfo.find( dimName );
83 if( vit == varInfo.end() )
MB_SET_ERR( MB_FAILURE,
"Can't find coordinate variable " << dimName );
85 usedCoordinates.insert( dimName );
86 dbgOut.
tprintf( 2,
" for variable %s need dimension %s with length %d\n", varname.c_str(),
87 dimName.c_str(), dimLens[currentVarData.
varDims[j]] );
91 if( usedCoordinates.find( varname ) != usedCoordinates.end() )
continue;
100 currentVarData.
varDims.end() ) )
117 currentVarData.
varTags.push_back( tag );
121 "Can't get data of tag " << varname );
130 assert( 0 == currentVarData.
memoryHogs.size() );
131 currentVarData.
memoryHogs.push_back( (
void*)data );
133 if( currentVarData.
varDims.empty() )
141 for(
size_t j = 0; j < currentVarData.
varDims.size(); j++ )
149 currentVarData.
sz = 1;
150 for( std::size_t idx = 0; idx != currentVarData.
writeCounts.size(); idx++ )
161 for(
unsigned int t = 0; t < tstep_nums.size(); t++ )
163 std::stringstream ssTagNameWithIndex;
164 ssTagNameWithIndex << varname << tstep_nums[t];
166 "Can't find tag " << ssTagNameWithIndex.str() );
167 dbgOut.
tprintf( 2,
" found indexed tag %d with name %s\n", tstep_nums[t],
168 ssTagNameWithIndex.str().c_str() );
169 currentVarData.
varTags.push_back( indexedTag );
176 std::stringstream ssTagNameWithIndex;
177 ssTagNameWithIndex << varname << 0;
179 "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 "Can't get coordinate values of " << coordName );
211 dbgOut.
tprintf( 2,
" found coordinate tag with name %s and length %d\n", coordName.c_str(), sizeCoordinate );
220 assert( 1 == varCoordData.
varDims.size() );
221 int coordDimLen = dimLens[varCoordData.
varDims[0]];
223 if( dummyVarNames.find( coordName ) != dummyVarNames.end() )
227 assert( 1 == sizeCoordinate );
228 sizeCoordinate = coordDimLen;
238 if( varCoordData.
varDims[0] !=
tDim ) assert( sizeCoordinate == coordDimLen );
246 if( NULL != data ) assert( tstep_nums.size() > 0 && tstep_nums.size() <= (
size_t)sizeCoordinate );
248 sizeCoordinate = tstep_nums.size();
254 for(
unsigned int t = 0; t < tstep_nums.size(); t++ )
262 varCoordData.
sz = sizeCoordinate;
268 assert( 0 == varCoordData.
memoryHogs.size() );
269 varCoordData.
memoryHogs.push_back( (
void*)data );
276 std::vector< std::string >& desired_names,
282 std::map< std::string, WriteNC::VarData >& varInfo =
_writeNC->
varInfo;
286 int tDim_in_dimNames =
tDim;
287 int levDim_in_dimNames =
levDim;
294 if( errcode != NC_NOERR )
MB_SET_ERR( MB_FAILURE,
"Can't open file in redefine mode" );
299 for( std::set< std::string >::iterator setIt = usedCoordinates.begin(); setIt != usedCoordinates.end(); ++setIt )
301 const std::string& coordName = *setIt;
303 std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( coordName );
304 if( vit == varInfo.end() )
MB_SET_ERR( MB_FAILURE,
"Can't find coordinate variable " << coordName );
307 varCoordData.
varDims.resize( 1 );
317 if(
NCFUNC( inq_dimid )(
_fileId, coordName.c_str(), &dimId ) == NC_NOERR )
319 varCoordData.
varDims[0] = dimId;
320 dbgOut.
tprintf( 2,
" file already has coordName %s dim id is %d \n", coordName.c_str(),
321 (
int)varCoordData.
varDims[0] );
324 if( coordName == dimNames[tDim_in_dimNames] )
326 else if( coordName == dimNames[levDim_in_dimNames] )
330 if( dummyVarNames.find( coordName ) != dummyVarNames.end() )
continue;
335 if(
NCFUNC( inq_varid )(
_fileId, coordName.c_str(), &varId ) != NC_NOERR )
336 MB_SET_ERR( MB_FAILURE,
"We do not have a variable with the same name " << coordName );
338 varCoordData.
varId = varId;
339 dbgOut.
tprintf( 2,
" file already has coordinate %s and varId is %d \n", coordName.c_str(), varId );
352 MB_SET_ERR( MB_FAILURE,
"Failed to generate dimension " << coordName );
353 dbgOut.
tprintf( 2,
" for coordName %s dim id is %d \n", coordName.c_str(), (
int)varCoordData.
varDims[0] );
356 if( coordName == dimNames[tDim_in_dimNames] )
358 else if( coordName == dimNames[levDim_in_dimNames] )
370 if( dummyVarNames.find( coordName ) != dummyVarNames.end() )
continue;
374 &varCoordData.
varId ) != NC_NOERR )
375 MB_SET_ERR( MB_FAILURE,
"Failed to create coordinate variable " << coordName );
377 dbgOut.
tprintf( 2,
" for coordName %s variable id is %d \n", coordName.c_str(), varCoordData.
varId );
382 for(
size_t i = 0; i < var_names.size(); i++ )
384 std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( var_names[i] );
385 if( vit == varInfo.end() )
MB_SET_ERR( MB_FAILURE,
"Can't find requested variable " << var_names[i] );
388 if( usedCoordinates.find( var_names[i] ) != usedCoordinates.end() )
continue;
393 int numDims = (int)variableData.
varDims.size();
394 for(
int j = 0; j < numDims; j++ )
396 std::string dimName = dimNames[variableData.
varDims[j]];
397 std::map< std::string, WriteNC::VarData >::iterator vit2 = varInfo.find( dimName );
398 if( vit2 == varInfo.end() )
399 MB_SET_ERR( MB_FAILURE,
"Can't find requested coordinate variable " << dimName );
404 dbgOut.
tprintf( 2,
" dimension with index %d name %s has ID %d \n", j, dimName.c_str(),
412 if( errCode != NC_NOERR )
MB_SET_ERR( MB_FAILURE,
"Failed to create requested variable " << desired_names[i] );
414 dbgOut.
tprintf( 2,
" for variable %s with desired name %s variable id is %d \n", var_names[i].c_str(),
415 desired_names[i].c_str(), variableData.
varId );
421 std::map< std::string, WriteNC::AttData >::iterator attIt;
422 for( attIt = globalAtts.begin(); attIt != globalAtts.end(); ++attIt )
424 const std::string& attName = attIt->first;
428 const std::string& attValue = attData.
attValue;
430 switch( attDataType )
435 NCFUNC( put_att_text )(
_fileId, NC_GLOBAL, attName.c_str(), attLen, attValue.c_str() ) )
436 MB_SET_ERR( MB_FAILURE,
"Failed to define text type attribute" );
439 if( NC_NOERR !=
NCFUNC( put_att_double )(
_fileId, NC_GLOBAL, attName.c_str(), NC_DOUBLE, 1,
440 (
double*)attValue.c_str() ) )
441 MB_SET_ERR( MB_FAILURE,
"Failed to define double type attribute" );
444 if( NC_NOERR !=
NCFUNC( put_att_float )(
_fileId, NC_GLOBAL, attName.c_str(), NC_FLOAT, 1,
445 (
float*)attValue.c_str() ) )
446 MB_SET_ERR( MB_FAILURE,
"Failed to define float type attribute" );
450 NCFUNC( put_att_int )(
_fileId, NC_GLOBAL, attName.c_str(), NC_INT, 1, (
int*)attValue.c_str() ) )
451 MB_SET_ERR( MB_FAILURE,
"Failed to define int type attribute" );
454 if( NC_NOERR !=
NCFUNC( put_att_short )(
_fileId, NC_GLOBAL, attName.c_str(), NC_SHORT, 1,
455 (
short*)attValue.c_str() ) )
456 MB_SET_ERR( MB_FAILURE,
"Failed to define short type attribute" );
459 MB_SET_ERR( MB_FAILURE,
"Unknown attribute data type" );
473 std::map< std::string, WriteNC::VarData >& varInfo =
_writeNC->
varInfo;
475 std::vector< WriteNC::VarData > vdatas;
476 std::vector< WriteNC::VarData > vsetdatas;
479 for( std::set< std::string >::iterator setIt = usedCoordinates.begin(); setIt != usedCoordinates.end(); ++setIt )
481 const std::string& coordName = *setIt;
484 if( dummyVarNames.find( coordName ) != dummyVarNames.end() )
continue;
486 std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( coordName );
487 if( vit == varInfo.end() )
489 MB_SET_ERR( MB_FAILURE,
"Can't find coordinate variable " << coordName );
492 vsetdatas.push_back( vit->second );
496 for(
unsigned int i = 0; i < var_names.size(); i++ )
498 std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( var_names[i] );
499 if( vit == varInfo.end() )
501 MB_SET_ERR( MB_FAILURE,
"Can't find requested variable " << var_names[i] );
508 if( usedCoordinates.find( var_names[i] ) != usedCoordinates.end() )
continue;
510 vsetdatas.push_back( variableData );
513 vdatas.push_back( variableData );
527 std::vector< int >& )
534 #ifdef MOAB_HAVE_PNETCDF
537 if( success )
MB_SET_ERR( MB_FAILURE,
"Failed to begin independent I/O mode" );
551 for(
unsigned int i = 0; i < vsetdatas.size(); i++ )
569 MB_SET_ERR( MB_FAILURE,
"Failed to write double data for variable " << variableData.
varName );
577 MB_SET_ERR( MB_FAILURE,
"Failed to write int data for variable " << variableData.
varName );
585 #ifdef MOAB_HAVE_PNETCDF
588 if( success )
MB_SET_ERR( MB_FAILURE,
"Failed to end independent I/O mode" );
601 std::vector< std::string >::iterator vecIt;
602 if( ( vecIt = std::find( dimNames.begin(), dimNames.end(),
"time" ) ) != dimNames.end() )
603 tDim = vecIt - dimNames.begin();
604 else if( ( vecIt = std::find( dimNames.begin(), dimNames.end(),
"t" ) ) != dimNames.end() )
605 tDim = vecIt - dimNames.begin();
608 MB_SET_ERR( MB_FAILURE,
"Couldn't find 'time' or 't' dimension" );
613 if( ( vecIt = std::find( dimNames.begin(), dimNames.end(),
"lev" ) ) != dimNames.end() )
614 levDim = vecIt - dimNames.begin();
615 else if( ( vecIt = std::find( dimNames.begin(), dimNames.end(),
"ilev" ) ) != dimNames.end() )
616 levDim = vecIt - dimNames.begin();
619 MB_SET_ERR( MB_FAILURE,
"Couldn't find 'lev' or 'ilev' dimension" );
626 "Trouble getting conventional tag __slon_LOC_MINMAX" );
629 "Trouble getting data of conventional tag __slon_LOC_MINMAX" );
634 "Trouble getting conventional tag __slat_LOC_MINMAX" );
636 "Trouble getting data of conventional tag __slat_LOC_MINMAX" );
641 "Trouble getting conventional tag __lon_LOC_MINMAX" );
643 "Trouble getting data of conventional tag __lon_LOC_MINMAX" );
648 "Trouble getting conventional tag __lat_LOC_MINMAX" );
650 "Trouble getting data of conventional tag __lat_LOC_MINMAX" );
656 "Trouble getting local faces in current file set" );
668 "Trouble getting owned faces in current file set" );
677 std::vector< int >& tstep_nums )
681 std::map< std::string, WriteNC::VarData >& varInfo =
_writeNC->
varInfo;
683 for(
size_t i = 0; i < var_names.size(); i++ )
685 std::string varname = var_names[i];
686 std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( varname );
687 if( vit == varInfo.end() )
MB_SET_ERR( MB_FAILURE,
"Can't find variable " << varname );
691 std::vector< int >& varDims = currentVarData.
varDims;
701 unsigned int dim_idx = 0;
709 assert( 4 == varDims.size() || 3 == varDims.size() );
712 assert(
tDim == varDims[0] );
723 assert( 3 == varDims.size() || 2 == varDims.size() );
727 if( currentVarData.
numLev > 0 )
732 assert( 4 == varDims.size() || 3 == varDims.size() );
743 assert( 3 == varDims.size() || 2 == varDims.size() );
747 switch( currentVarData.
entLoc )
757 MB_SET_ERR( MB_FAILURE,
"Unexpected entity location type for variable " << varname );
762 currentVarData.
sz = 1;
763 for( std::size_t idx = 0; idx < dim_idx; idx++ )
774 std::vector< int >& tstep_nums )
781 for(
unsigned int i = 0; i < vdatas.size(); i++ )
786 switch( variableData.
entLoc )
792 MB_SET_ERR( MB_FAILURE,
"Unexpected entity location type for variable " << variableData.
varName );
795 unsigned int num_timesteps;
796 unsigned int lat_idx = 0;
797 unsigned int lon_idx = 1;
803 num_timesteps = tstep_nums.size();
815 unsigned int num_lev;
816 if( variableData.
numLev > 0 )
821 num_lev = variableData.
numLev;
838 for(
unsigned int t = 0; t < num_timesteps; t++ )
848 "Failed to iterate tag on owned faces" );
857 std::vector< double > tmpdoubledata( ni * nj * num_lev );
860 jik_to_kji( ni, nj, num_lev, &tmpdoubledata[0], (
double*)( dataptr ) );
864 MB_SET_ERR( MB_FAILURE,
"Failed to write double data for variable " << variableData.
varName );