12 :
UcdNCHelper( readNC, fileId, opts, fileSet ), _spectralOrder( -1 ), connectId( -1 ), isConnFile( false )
15 std::map< std::string, ReadNC::AttData >::iterator attIt = readNC->
globalAtts.find(
"np" );
18 int success =
NCFUNC( get_att_int )( readNC->
fileId, attIt->second.attVarId, attIt->second.attName.c_str(),
37 std::map< std::string, ReadNC::AttData >::iterator attIt = readNC->
globalAtts.find(
"source" );
38 if( attIt == readNC->
globalAtts.end() )
return false;
39 unsigned int sz = attIt->second.attLen;
41 att_data.resize( sz + 1 );
42 att_data[sz] =
'\000';
44 NCFUNC( get_att_text )( fileId, attIt->second.attVarId, attIt->second.attName.c_str(), &att_data[0] );
45 if( success )
return false;
46 if( att_data.find(
"CAM" ) == std::string::npos )
return false;
54 std::vector< std::string >& dimNames = readNC->
dimNames;
55 if( ( std::find( dimNames.begin(), dimNames.end(), std::string(
"ncol" ) ) != dimNames.end() ) &&
56 ( std::find( dimNames.begin(), dimNames.end(), std::string(
"ncorners" ) ) != dimNames.end() ) &&
57 ( std::find( dimNames.begin(), dimNames.end(), std::string(
"ncells" ) ) != dimNames.end() ) )
68 std::map< std::string, ReadNC::VarData >& varInfo =
_readNC->
varInfo;
72 std::vector< std::string >::iterator vit;
81 if( ( vit = std::find( dimNames.begin(), dimNames.end(),
"time" ) ) != dimNames.end() )
82 idx = vit - dimNames.begin();
83 else if( ( vit = std::find( dimNames.begin(), dimNames.end(),
"t" ) ) != dimNames.end() )
84 idx = vit - dimNames.begin();
87 MB_SET_ERR( MB_FAILURE,
"Couldn't find 'time' or 't' dimension" );
94 if( ( vit = std::find( dimNames.begin(), dimNames.end(),
"ncol" ) ) != dimNames.end() )
95 idx = vit - dimNames.begin();
98 MB_SET_ERR( MB_FAILURE,
"Couldn't find 'ncol' dimension" );
113 if( ( vit = std::find( dimNames.begin(), dimNames.end(),
"lev" ) ) != dimNames.end() )
114 idx = vit - dimNames.begin();
115 else if( ( vit = std::find( dimNames.begin(), dimNames.end(),
"ilev" ) ) != dimNames.end() )
116 idx = vit - dimNames.begin();
119 MB_SET_ERR( MB_FAILURE,
"Couldn't find 'lev' or 'ilev' dimension" );
126 std::map< std::string, ReadNC::VarData >::iterator vmit;
127 if( ( vmit = varInfo.find(
"lon" ) ) != varInfo.end() && ( *vmit ).second.varDims.size() == 1 )
133 MB_SET_ERR( MB_FAILURE,
"Couldn't find 'lon' variable" );
137 if( ( vmit = varInfo.find(
"lat" ) ) != varInfo.end() && ( *vmit ).second.varDims.size() == 1 )
143 MB_SET_ERR( MB_FAILURE,
"Couldn't find 'lat' variable" );
153 if( ( vmit = varInfo.find(
"lev" ) ) != varInfo.end() && ( *vmit ).second.varDims.size() == 1 )
158 char posval[10] = { 0 };
159 int success =
NCFUNC( get_att_text )(
_fileId, ( *vmit ).second.varId,
"positive", posval );
160 if( 0 == success && !strcmp( posval,
"down" ) )
162 for( std::vector< double >::iterator dvit =
levVals.begin(); dvit !=
levVals.end(); ++dvit )
168 MB_SET_ERR( MB_FAILURE,
"Couldn't find 'lev' variable" );
179 if( ( vmit = varInfo.find(
"time" ) ) != varInfo.end() && ( *vmit ).second.varDims.size() == 1 )
183 else if( ( vmit = varInfo.find(
"t" ) ) != varInfo.end() && ( *vmit ).second.varDims.size() == 1 )
191 tVals.push_back( (
double)t );
196 std::map< std::string, ReadNC::VarData >::iterator mit;
197 for( mit = varInfo.begin(); mit != varInfo.end(); ++mit )
240 if( !local_verts.
empty() )
242 std::vector< int > gids( local_verts.
size() );
245 rval = mbImpl->
tag_get_data( mGlobalIdTag, local_verts, &gids[0] );
MB_CHK_SET_ERR( rval,
"Trouble getting local gid values of vertices" );
283 std::string conn_fname;
298 conn_fname = std::string( fileName );
299 size_t idx = conn_fname.find_last_of(
"/" );
300 if( idx != std::string::npos )
301 conn_fname = conn_fname.substr( 0, idx ).append(
"/HommeMapping.nc" );
303 conn_fname =
"HommeMapping.nc";
305 #ifdef MOAB_HAVE_PNETCDF
314 success =
NCFUNC( open )( MPI_COMM_SELF, conn_fname.c_str(), 0, MPI_INFO_NULL, &
connectId );
319 if( success )
MB_SET_ERR( MB_FAILURE,
"Failed on open" );
322 std::vector< std::string > conn_names;
323 std::vector< int > conn_vals;
327 int num_fine_quads = 0;
328 int num_coarse_quads = 0;
330 std::vector< std::string >::iterator vit;
332 if( ( vit = std::find( conn_names.begin(), conn_names.end(),
"ncells" ) ) != conn_names.end() )
333 idx = vit - conn_names.begin();
334 else if( ( vit = std::find( conn_names.begin(), conn_names.end(),
"ncenters" ) ) != conn_names.end() )
335 idx = vit - conn_names.begin();
338 MB_SET_ERR( MB_FAILURE,
"Failed to get number of quads" );
340 int num_quads = conn_vals[idx];
344 "Warning: number of quads from %s and cells from %s are inconsistent; "
345 "num_quads = %d, nCells = %d.\n",
346 conn_fname.c_str(), fileName.c_str(), num_quads,
nCells );
351 success =
NCFUNC( inq_varid )(
connectId,
"element_corners", &cornerVarId );
352 if( success )
MB_SET_ERR( MB_FAILURE,
"Failed to get variable id of 'element_corners'" );
355 std::vector< int > tmp_conn( 4 * num_quads ), tmp_conn2( 4 * num_quads );
356 success =
NCFUNCAG( _vara_int )(
connectId, cornerVarId, tmp_starts, tmp_counts, &tmp_conn2[0] );
357 if( success )
MB_SET_ERR( MB_FAILURE,
"Failed to get temporary connectivity" );
365 if( success )
MB_SET_ERR( MB_FAILURE,
"Failed on close" );
368 for(
int i = 0; i < num_quads; i++ )
370 tmp_conn[4 * i] = tmp_conn2[i];
371 tmp_conn[4 * i + 1] = tmp_conn2[i + 1 * num_quads];
372 tmp_conn[4 * i + 2] = tmp_conn2[i + 2 * num_quads];
373 tmp_conn[4 * i + 3] = tmp_conn2[i + 3 * num_quads];
378 bool create_gathers =
false;
379 if( rank == gatherSetRank ) create_gathers =
true;
382 int shifted_rank = rank;
383 if( procs >= 2 && trivialPartitionShift > 0 ) shifted_rank = ( rank + trivialPartitionShift ) % procs;
390 num_coarse_quads = int( std::floor( 1.0 * num_quads / ( spectral_unit * procs ) ) );
393 start_idx = 4 * shifted_rank * num_coarse_quads * spectral_unit;
395 int iextra = num_quads % ( procs * spectral_unit );
396 if( shifted_rank < iextra ) num_coarse_quads++;
397 start_idx += 4 * spectral_unit * std::min( shifted_rank, iextra );
400 num_fine_quads = spectral_unit * num_coarse_quads;
415 ( create_gathers ? num_coarse_quads + num_quads
416 : num_coarse_quads ) );
MB_CHK_SET_ERR( rval,
"Failed to create local quads" );
417 tmp_range.
insert( start_quad, start_quad + num_coarse_quads - 1 );
418 int* tmp_conn_end = ( &tmp_conn[start_idx + 4 * num_fine_quads - 1] ) + 1;
419 std::copy( &tmp_conn[start_idx], tmp_conn_end, conn_arr );
428 (
void*&)sv_ptr );
MB_CHK_SET_ERR( rval,
"Failed to get fine connectivity of spectral elements" );
433 std::vector< double* > arrays;
440 double* xptr = arrays[0];
441 double* yptr = arrays[1];
442 double* zptr = arrays[2];
452 const double pideg = acos( -1.0 ) / 180.0;
456 double cosphi = cos(
pideg * yptr[i] );
457 double zmult = sin(
pideg * yptr[i] );
458 double xmult = cosphi * cos( xptr[i] *
pideg );
459 double ymult = cosphi * sin( xptr[i] *
pideg );
460 xptr[i] = rad * xmult;
461 yptr[i] = rad * ymult;
462 zptr[i] = rad * zmult;
469 rval = mbImpl->
tag_iterate( mGlobalIdTag, vert_range.
begin(), vert_range.
end(), count, data );
MB_CHK_SET_ERR( rval,
"Failed to iterate global id tag on local vertices" );
471 int* gid_data = (
int*)data;
477 rval = mbImpl->
tag_iterate( *mpFileIdTag, vert_range.
begin(), vert_range.
end(), count, data );
MB_CHK_SET_ERR( rval,
"Failed to iterate file id tag on local vertices" );
479 int bytes_per_tag = 4;
481 if( 4 == bytes_per_tag )
483 gid_data = (
int*)data;
486 else if( 8 == bytes_per_tag )
488 long* handle_tag_data = (
long*)data;
494 std::map< EntityHandle, EntityHandle > vert_handles;
496 vert_handles[*rit] = start_vertex + i;
499 for(
int q = 0; q < 4 * num_coarse_quads; q++ )
501 conn_arr[q] = vert_handles[conn_arr[q]];
502 assert( conn_arr[q] );
507 for(
int q = 0; q < verts_per_quad * num_coarse_quads; q++ )
509 sv_ptr[q] = vert_handles[sv_ptr[q]];
515 faces.
merge( tmp_range );
544 xptr[i] = rad * xmult;
545 yptr[i] = rad * ymult;
546 zptr[i] = rad * zmult;
550 Range gather_set_verts_range( start_vertex, start_vertex +
nVertices - 1 );
551 rval = mbImpl->
tag_iterate( mGlobalIdTag, gather_set_verts_range.
begin(), gather_set_verts_range.
end(), count,
552 data );
MB_CHK_SET_ERR( rval,
"Failed to iterate global id tag on gather set vertices" );
554 gid_data = (
int*)data;
560 rval = mbImpl->
tag_iterate( *mpFileIdTag, gather_set_verts_range.
begin(), gather_set_verts_range.
end(),
561 count, data );
MB_CHK_SET_ERR( rval,
"Failed to iterate file id tag on gather set vertices" );
563 int bytes_per_tag = 4;
565 if( 4 == bytes_per_tag )
567 gid_data = (
int*)data;
571 else if( 8 == bytes_per_tag )
573 long* handle_tag_data = (
long*)data;
579 rval = mbImpl->
add_entities( gather_set, gather_set_verts_range );
MB_CHK_SET_ERR( rval,
"Failed to add vertices to the gather set" );
582 Range gather_set_quads_range;
586 gather_set_quads_range.
insert( start_quad, start_quad + num_quads - 1 );
587 int* tmp_conn_end = ( &tmp_conn[4 * num_quads - 1] ) + 1;
588 std::copy( &tmp_conn[0], tmp_conn_end, conn_arr );
589 for( i = 0; i != 4 * num_quads; i++ )
590 conn_arr[i] += start_vertex - 1;
591 rval = mbImpl->
add_entities( gather_set, gather_set_quads_range );
MB_CHK_SET_ERR( rval,
"Failed to add quads to the gather set" );
598 std::vector< int >& tstep_nums )
609 assert(
"Should only have a single vertex subrange, since they were read in one shot" && verts.
psize() == 1 );
611 for(
unsigned int i = 0; i < vdatas.size(); i++ )
614 assert( 3 == vdatas[i].varDims.size() );
617 assert(
tDim == vdatas[i].varDims[0] );
620 vdatas[i].readStarts.resize( 3 );
621 vdatas[i].readCounts.resize( 3 );
624 vdatas[i].readStarts[0] = 0;
625 vdatas[i].readCounts[0] = 1;
628 vdatas[i].readStarts[1] = 0;
629 vdatas[i].readCounts[1] = vdatas[i].numLev;
632 switch( vdatas[i].entLoc )
643 MB_SET_ERR( MB_FAILURE,
"Unexpected entity location type for variable " << vdatas[i].varName );
648 for( std::size_t idx = 0; idx != 3; idx++ )
649 vdatas[i].sz *= vdatas[i].readCounts[idx];
651 for(
unsigned int t = 0; t < tstep_nums.size(); t++ )
653 dbgOut.
tprintf( 2,
"Reading variable %s, time step %d\n", vdatas[i].varName.c_str(), tstep_nums[t] );
655 if( tstep_nums[t] >= dimLens[
tDim] )
661 if( !vdatas[i].varTags[t] )
663 rval =
get_tag_to_nonset( vdatas[i], tstep_nums[t], vdatas[i].varTags[t], vdatas[i].numLev );
MB_CHK_SET_ERR( rval,
"Trouble getting tag for variable " << vdatas[i].varName );
669 rval = mbImpl->
tag_iterate( vdatas[i].varTags[t], range->
begin(), range->
end(), count, data );
MB_CHK_SET_ERR( rval,
"Failed to iterate tag for variable " << vdatas[i].varName );
670 assert( (
unsigned)count == range->
size() );
671 vdatas[i].varDatas[t] = data;
678 #ifdef MOAB_HAVE_PNETCDF
679 ErrorCode NCHelperHOMME::read_ucd_variables_to_nonset_async( std::vector< ReadNC::VarData >& vdatas,
680 std::vector< int >& tstep_nums )
689 for(
unsigned int i = 0; i < vdatas.size(); i++ )
691 std::size_t sz = vdatas[i].sz;
695 size_t ni = vdatas[i].readCounts[2];
697 size_t nk = vdatas[i].readCounts[1];
699 for(
unsigned int t = 0; t < tstep_nums.size(); t++ )
704 std::vector< int > requests( nb_reads ), statuss( nb_reads );
708 void* data = vdatas[i].varDatas[t];
711 vdatas[i].readStarts[0] = tstep_nums[t];
713 switch( vdatas[i].varDataType )
718 std::vector< double > tmpdoubledata( sz );
725 size_t indexInDoubleArray = 0;
732 vdatas[i].readStarts[2] = (
NCDF_SIZE)( starth - 1 );
733 vdatas[i].readCounts[2] = (
NCDF_SIZE)( endh - starth + 1 );
738 NCFUNCREQG( _vara_double )(
_fileId, vdatas[i].varId, &( vdatas[i].readStarts[0] ),
739 &( vdatas[i].readCounts[0] ),
740 &( tmpdoubledata[indexInDoubleArray] ), &requests[idxReq++] );
743 "Failed to read double data in a loop for variable " << vdatas[i].varName );
746 indexInDoubleArray += ( endh - starth + 1 ) * 1 * vdatas[i].numLev;
750 success = ncmpi_wait_all(
_fileId, requests.size(), &requests[0], &statuss[0] );
751 if( success )
MB_SET_ERR( MB_FAILURE,
"Failed on wait_all" );
753 if( vdatas[i].numLev > 1 )
758 for( std::size_t idx = 0; idx != tmpdoubledata.size(); idx++ )
759 ( (
double*)data )[idx] = tmpdoubledata[idx];
765 MB_SET_ERR( MB_FAILURE,
"Unexpected data type for variable " << vdatas[i].varName );
773 dbgOut.
printf( 1,
"Read variables: %s", vdatas.begin()->varName.c_str() );
774 for(
unsigned int i = 1; i < vdatas.size(); i++ )
775 dbgOut.
printf( 1,
", %s ", vdatas[i].varName.c_str() );
783 std::vector< int >& tstep_nums )
791 for(
unsigned int i = 0; i < vdatas.size(); i++ )
793 std::size_t sz = vdatas[i].sz;
797 size_t ni = vdatas[i].readCounts[2];
799 size_t nk = vdatas[i].readCounts[1];
801 for(
unsigned int t = 0; t < tstep_nums.size(); t++ )
804 void* data = vdatas[i].varDatas[t];
807 vdatas[i].readStarts[0] = tstep_nums[t];
809 switch( vdatas[i].varDataType )
814 std::vector< double > tmpdoubledata( sz );
821 size_t indexInDoubleArray = 0;
828 vdatas[i].readStarts[2] = (
NCDF_SIZE)( starth - 1 );
829 vdatas[i].readCounts[2] = (
NCDF_SIZE)( endh - starth + 1 );
831 success =
NCFUNCAG( _vara_double )(
_fileId, vdatas[i].varId, &( vdatas[i].readStarts[0] ),
832 &( vdatas[i].readCounts[0] ),
833 &( tmpdoubledata[indexInDoubleArray] ) );
836 "Failed to read double data in a loop for variable " << vdatas[i].varName );
839 indexInDoubleArray += ( endh - starth + 1 ) * 1 * vdatas[i].numLev;
843 if( vdatas[i].numLev > 1 )
848 for( std::size_t idx = 0; idx != tmpdoubledata.size(); idx++ )
849 ( (
double*)data )[idx] = tmpdoubledata[idx];
855 MB_SET_ERR( MB_FAILURE,
"Unexpected data type for variable " << vdatas[i].varName );
863 dbgOut.
printf( 1,
"Read variables: %s", vdatas.begin()->varName.c_str() );
864 for(
unsigned int i = 1; i < vdatas.size(); i++ )
865 dbgOut.
printf( 1,
", %s ", vdatas[i].varName.c_str() );