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;
71 std::vector< std::string >::iterator vit;
80 if( ( vit = std::find( dimNames.begin(), dimNames.end(),
"time" ) ) != dimNames.end() )
81 idx = vit - dimNames.begin();
82 else if( ( vit = std::find( dimNames.begin(), dimNames.end(),
"t" ) ) != dimNames.end() )
83 idx = vit - dimNames.begin();
86 MB_SET_ERR( MB_FAILURE,
"Couldn't find 'time' or 't' dimension" );
93 if( ( vit = std::find( dimNames.begin(), dimNames.end(),
"ncol" ) ) != dimNames.end() )
94 idx = vit - dimNames.begin();
97 MB_SET_ERR( MB_FAILURE,
"Couldn't find 'ncol' dimension" );
112 if( ( vit = std::find( dimNames.begin(), dimNames.end(),
"lev" ) ) != dimNames.end() )
113 idx = vit - dimNames.begin();
114 else if( ( vit = std::find( dimNames.begin(), dimNames.end(),
"ilev" ) ) != dimNames.end() )
115 idx = vit - dimNames.begin();
118 MB_SET_ERR( MB_FAILURE,
"Couldn't find 'lev' or 'ilev' dimension" );
125 std::map< std::string, ReadNC::VarData >::iterator vmit;
126 if( ( vmit = varInfo.find(
"lon" ) ) != varInfo.end() && ( *vmit ).second.varDims.size() == 1 )
132 MB_SET_ERR( MB_FAILURE,
"Couldn't find 'lon' variable" );
136 if( ( vmit = varInfo.find(
"lat" ) ) != varInfo.end() && ( *vmit ).second.varDims.size() == 1 )
142 MB_SET_ERR( MB_FAILURE,
"Couldn't find 'lat' variable" );
152 if( ( vmit = varInfo.find(
"lev" ) ) != varInfo.end() && ( *vmit ).second.varDims.size() == 1 )
157 char posval[10] = { 0 };
158 int success =
NCFUNC( get_att_text )(
_fileId, ( *vmit ).second.varId,
"positive", posval );
159 if( 0 == success && !strcmp( posval,
"down" ) )
161 for( std::vector< double >::iterator dvit =
levVals.begin(); dvit !=
levVals.end(); ++dvit )
167 MB_SET_ERR( MB_FAILURE,
"Couldn't find 'lev' variable" );
178 if( ( vmit = varInfo.find(
"time" ) ) != varInfo.end() && ( *vmit ).second.varDims.size() == 1 )
182 else if( ( vmit = varInfo.find(
"t" ) ) != varInfo.end() && ( *vmit ).second.varDims.size() == 1 )
190 tVals.push_back( (
double)t );
195 std::map< std::string, ReadNC::VarData >::iterator mit;
196 for( mit = varInfo.begin(); mit != varInfo.end(); ++mit )
238 "Trouble getting local vertices in current file set" );
240 if( !local_verts.
empty() )
242 std::vector< int > gids( local_verts.
size() );
246 "Trouble getting local gid values of vertices" );
283 std::string conn_fname;
297 conn_fname = std::string( fileName );
298 size_t idx = conn_fname.find_last_of(
"/" );
299 if( idx != std::string::npos )
300 conn_fname = conn_fname.substr( 0, idx ).append(
"/HommeMapping.nc" );
302 conn_fname =
"HommeMapping.nc";
304 #ifdef MOAB_HAVE_PNETCDF
313 success =
NCFUNC( open )( MPI_COMM_SELF, conn_fname.c_str(), 0, MPI_INFO_NULL, &
connectId );
318 if( success )
MB_SET_ERR( MB_FAILURE,
"Failed on open" );
321 std::vector< std::string > conn_names;
322 std::vector< int > conn_vals;
324 "Failed to get dimensions for connectivity" );
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;
414 num_coarse_quads, 4,
MBQUAD, 0, start_quad, conn_arr,
416 ( create_gathers ? num_coarse_quads + num_quads : num_coarse_quads ) ),
417 "Failed to create local quads" );
418 tmp_range.
insert( start_quad, start_quad + num_coarse_quads - 1 );
419 int* tmp_conn_end = ( &tmp_conn[start_idx + 4 * num_fine_quads - 1] ) + 1;
420 std::copy( &tmp_conn[start_idx], tmp_conn_end, conn_arr );
427 "Failed to create spectral elements" );
430 "Failed to get connectivity of spectral elements" );
432 count, (
void*&)sv_ptr ),
433 "Failed to get fine connectivity of spectral elements" );
438 std::vector< double* > arrays;
443 "Failed to create local vertices" );
447 double* xptr = arrays[0];
448 double* yptr = arrays[1];
449 double* zptr = arrays[2];
459 const double pideg = acos( -1.0 ) / 180.0;
463 double cosphi = cos(
pideg * yptr[i] );
464 double zmult = sin(
pideg * yptr[i] );
465 double xmult = cosphi * cos( xptr[i] *
pideg );
466 double ymult = cosphi * sin( xptr[i] *
pideg );
467 xptr[i] = rad * xmult;
468 yptr[i] = rad * ymult;
469 zptr[i] = rad * zmult;
477 "Failed to iterate global id tag on local vertices" );
479 int* gid_data = (
int*)data;
486 "Failed to iterate file id tag on local vertices" );
488 int bytes_per_tag = 4;
490 "Can't get number of bytes for file id tag" );
491 if( 4 == bytes_per_tag )
493 gid_data = (
int*)data;
496 else if( 8 == bytes_per_tag )
498 long* handle_tag_data = (
long*)data;
504 std::map< EntityHandle, EntityHandle > vert_handles;
506 vert_handles[*rit] = start_vertex + i;
509 for(
int q = 0; q < 4 * num_coarse_quads; q++ )
511 conn_arr[q] = vert_handles[conn_arr[q]];
512 assert( conn_arr[q] );
517 for(
int q = 0; q < verts_per_quad * num_coarse_quads; q++ )
519 sv_ptr[q] = vert_handles[sv_ptr[q]];
525 faces.
merge( tmp_range );
528 "Failed to add new vertices and quads to current file set" );
534 "Trouble creating SPECTRAL_ORDER tag" );
536 "Trouble setting data to SPECTRAL_ORDER tag" );
548 "Failed to create gather set vertices" );
559 xptr[i] = rad * xmult;
560 yptr[i] = rad * ymult;
561 zptr[i] = rad * zmult;
565 Range gather_set_verts_range( start_vertex, start_vertex +
nVertices - 1 );
568 "Failed to iterate global id tag on gather set vertices" );
570 gid_data = (
int*)data;
577 gather_set_verts_range.
end(), count, data ),
578 "Failed to iterate file id tag on gather set vertices" );
580 int bytes_per_tag = 4;
582 "Can't get number of bytes for file id tag" );
583 if( 4 == bytes_per_tag )
585 gid_data = (
int*)data;
589 else if( 8 == bytes_per_tag )
591 long* handle_tag_data = (
long*)data;
598 "Failed to add vertices to the gather set" );
601 Range gather_set_quads_range;
605 "Failed to create gather set quads" );
606 gather_set_quads_range.
insert( start_quad, start_quad + num_quads - 1 );
607 int* tmp_conn_end = ( &tmp_conn[4 * num_quads - 1] ) + 1;
608 std::copy( &tmp_conn[0], tmp_conn_end, conn_arr );
609 for( i = 0; i != 4 * num_quads; i++ )
610 conn_arr[i] += start_vertex - 1;
612 "Failed to add quads to the gather set" );
619 std::vector< int >& tstep_nums )
630 "Trouble getting vertices in current file set" );
631 assert(
"Should only have a single vertex subrange, since they were read in one shot" && verts.
psize() == 1 );
633 for(
unsigned int i = 0; i < vdatas.size(); i++ )
636 assert( 3 == vdatas[i].varDims.size() );
639 assert(
tDim == vdatas[i].varDims[0] );
642 vdatas[i].readStarts.resize( 3 );
643 vdatas[i].readCounts.resize( 3 );
646 vdatas[i].readStarts[0] = 0;
647 vdatas[i].readCounts[0] = 1;
650 vdatas[i].readStarts[1] = 0;
651 vdatas[i].readCounts[1] = vdatas[i].numLev;
654 switch( vdatas[i].entLoc )
665 MB_SET_ERR( MB_FAILURE,
"Unexpected entity location type for variable " << vdatas[i].varName );
670 for( std::size_t idx = 0; idx != 3; idx++ )
671 vdatas[i].sz *= vdatas[i].readCounts[idx];
673 for(
unsigned int t = 0; t < tstep_nums.size(); t++ )
675 dbgOut.
tprintf( 2,
"Reading variable %s, time step %d\n", vdatas[i].varName.c_str(), tstep_nums[t] );
677 if( tstep_nums[t] >= dimLens[
tDim] )
683 if( !vdatas[i].varTags[t] )
686 "Trouble getting tag for variable " << vdatas[i].varName );
693 "Failed to iterate tag for variable " << vdatas[i].varName );
694 assert( (
unsigned)count == range->
size() );
695 vdatas[i].varDatas[t] = data;
702 #ifdef MOAB_HAVE_PNETCDF
703 ErrorCode NCHelperHOMME::read_ucd_variables_to_nonset_async( std::vector< ReadNC::VarData >& vdatas,
704 std::vector< int >& tstep_nums )
709 "Trouble allocating space to read non-set variables" );
714 for(
unsigned int i = 0; i < vdatas.size(); i++ )
716 std::size_t sz = vdatas[i].sz;
720 size_t ni = vdatas[i].readCounts[2];
722 size_t nk = vdatas[i].readCounts[1];
724 for(
unsigned int t = 0; t < tstep_nums.size(); t++ )
729 std::vector< int > requests( nb_reads ), statuss( nb_reads );
733 void* data = vdatas[i].varDatas[t];
736 vdatas[i].readStarts[0] = tstep_nums[t];
738 switch( vdatas[i].varDataType )
743 std::vector< double > tmpdoubledata( sz );
750 size_t indexInDoubleArray = 0;
757 vdatas[i].readStarts[2] = (
NCDF_SIZE)( starth - 1 );
758 vdatas[i].readCounts[2] = (
NCDF_SIZE)( endh - starth + 1 );
763 NCFUNCREQG( _vara_double )(
_fileId, vdatas[i].varId, &( vdatas[i].readStarts[0] ),
764 &( vdatas[i].readCounts[0] ),
765 &( tmpdoubledata[indexInDoubleArray] ), &requests[idxReq++] );
768 "Failed to read double data in a loop for variable " << vdatas[i].varName );
771 indexInDoubleArray += ( endh - starth + 1 ) * 1 * vdatas[i].numLev;
775 success = ncmpi_wait_all(
_fileId, requests.size(), &requests[0], &statuss[0] );
776 if( success )
MB_SET_ERR( MB_FAILURE,
"Failed on wait_all" );
778 if( vdatas[i].numLev > 1 )
783 for( std::size_t idx = 0; idx != tmpdoubledata.size(); idx++ )
784 ( (
double*)data )[idx] = tmpdoubledata[idx];
790 MB_SET_ERR( MB_FAILURE,
"Unexpected data type for variable " << vdatas[i].varName );
798 dbgOut.
printf( 1,
"Read variables: %s", vdatas.begin()->varName.c_str() );
799 for(
unsigned int i = 1; i < vdatas.size(); i++ )
800 dbgOut.
printf( 1,
", %s ", vdatas[i].varName.c_str() );
808 std::vector< int >& tstep_nums )
813 "Trouble allocating space to read non-set variables" );
817 for(
unsigned int i = 0; i < vdatas.size(); i++ )
819 std::size_t sz = vdatas[i].sz;
823 size_t ni = vdatas[i].readCounts[2];
825 size_t nk = vdatas[i].readCounts[1];
827 for(
unsigned int t = 0; t < tstep_nums.size(); t++ )
830 void* data = vdatas[i].varDatas[t];
833 vdatas[i].readStarts[0] = tstep_nums[t];
835 switch( vdatas[i].varDataType )
840 std::vector< double > tmpdoubledata( sz );
847 size_t indexInDoubleArray = 0;
854 vdatas[i].readStarts[2] = (
NCDF_SIZE)( starth - 1 );
855 vdatas[i].readCounts[2] = (
NCDF_SIZE)( endh - starth + 1 );
857 success =
NCFUNCAG( _vara_double )(
_fileId, vdatas[i].varId, &( vdatas[i].readStarts[0] ),
858 &( vdatas[i].readCounts[0] ),
859 &( tmpdoubledata[indexInDoubleArray] ) );
862 "Failed to read double data in a loop for variable " << vdatas[i].varName );
865 indexInDoubleArray += ( endh - starth + 1 ) * 1 * vdatas[i].numLev;
869 if( vdatas[i].numLev > 1 )
874 for( std::size_t idx = 0; idx != tmpdoubledata.size(); idx++ )
875 ( (
double*)data )[idx] = tmpdoubledata[idx];
881 MB_SET_ERR( MB_FAILURE,
"Unexpected data type for variable " << vdatas[i].varName );
889 dbgOut.
printf( 1,
"Read variables: %s", vdatas.begin()->varName.c_str() );
890 for(
unsigned int i = 1; i < vdatas.size(); i++ )
891 dbgOut.
printf( 1,
", %s ", vdatas[i].varName.c_str() );