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";
323 const int conn_mpi_size = isParallel ?
_readNC->myPcomm->proc_config().proc_size() : 1;
325 const int conn_mpi_size = 1;
331 MB_SET_ERR( MB_FAILURE,
"Cannot find a compatible reader for HOMME connectivity file '"
332 << conn_fname <<
"'" );
349 if( success )
MB_SET_ERR( MB_FAILURE,
"Failed on open" );
352 std::vector< std::string > conn_names;
353 std::vector< int > conn_vals;
355 "Failed to get dimensions for connectivity" );
358 int num_fine_quads = 0;
359 int num_coarse_quads = 0;
361 std::vector< std::string >::iterator vit;
363 if( ( vit = std::find( conn_names.begin(), conn_names.end(),
"ncells" ) ) != conn_names.end() )
364 idx = vit - conn_names.begin();
365 else if( ( vit = std::find( conn_names.begin(), conn_names.end(),
"ncenters" ) ) != conn_names.end() )
366 idx = vit - conn_names.begin();
369 MB_SET_ERR( MB_FAILURE,
"Failed to get number of quads" );
371 int num_quads = conn_vals[idx];
375 "Warning: number of quads from %s and cells from %s are inconsistent; "
376 "num_quads = %d, nCells = %d.\n",
377 conn_fname.c_str(), fileName.c_str(), num_quads,
nCells );
382 success =
NCFUNC( inq_varid )(
connectId,
"element_corners", &cornerVarId );
383 if( success )
MB_SET_ERR( MB_FAILURE,
"Failed to get variable id of 'element_corners'" );
386 std::vector< int > tmp_conn( 4 * num_quads ), tmp_conn2( 4 * num_quads );
387 success =
NCFUNCAG( _vara_int )(
connectId, cornerVarId, tmp_starts, tmp_counts, &tmp_conn2[0] );
388 if( success )
MB_SET_ERR( MB_FAILURE,
"Failed to get temporary connectivity" );
396 if( success )
MB_SET_ERR( MB_FAILURE,
"Failed on close" );
399 for(
int i = 0; i < num_quads; i++ )
401 tmp_conn[4 * i] = tmp_conn2[i];
402 tmp_conn[4 * i + 1] = tmp_conn2[i + 1 * num_quads];
403 tmp_conn[4 * i + 2] = tmp_conn2[i + 2 * num_quads];
404 tmp_conn[4 * i + 3] = tmp_conn2[i + 3 * num_quads];
409 bool create_gathers =
false;
410 if( rank == gatherSetRank ) create_gathers =
true;
413 int shifted_rank = rank;
414 if( procs >= 2 && trivialPartitionShift > 0 ) shifted_rank = ( rank + trivialPartitionShift ) % procs;
421 num_coarse_quads = int( std::floor( 1.0 * num_quads / ( spectral_unit * procs ) ) );
424 start_idx = 4 * shifted_rank * num_coarse_quads * spectral_unit;
426 int iextra = num_quads % ( procs * spectral_unit );
427 if( shifted_rank < iextra ) num_coarse_quads++;
428 start_idx += 4 * spectral_unit * std::min( shifted_rank, iextra );
431 num_fine_quads = spectral_unit * num_coarse_quads;
445 num_coarse_quads, 4,
MBQUAD, 0, start_quad, conn_arr,
447 ( create_gathers ? num_coarse_quads + num_quads : num_coarse_quads ) ),
448 "Failed to create local quads" );
449 tmp_range.
insert( start_quad, start_quad + num_coarse_quads - 1 );
450 int* tmp_conn_end = ( &tmp_conn[start_idx + 4 * num_fine_quads - 1] ) + 1;
451 std::copy( &tmp_conn[start_idx], tmp_conn_end, conn_arr );
458 "Failed to create spectral elements" );
461 "Failed to get connectivity of spectral elements" );
463 count, (
void*&)sv_ptr ),
464 "Failed to get fine connectivity of spectral elements" );
469 std::vector< double* > arrays;
474 "Failed to create local vertices" );
478 double* xptr = arrays[0];
479 double* yptr = arrays[1];
480 double* zptr = arrays[2];
490 const double pideg = acos( -1.0 ) / 180.0;
494 double cosphi = cos(
pideg * yptr[i] );
495 double zmult = sin(
pideg * yptr[i] );
496 double xmult = cosphi * cos( xptr[i] *
pideg );
497 double ymult = cosphi * sin( xptr[i] *
pideg );
498 xptr[i] = rad * xmult;
499 yptr[i] = rad * ymult;
500 zptr[i] = rad * zmult;
508 "Failed to iterate global id tag on local vertices" );
510 int* gid_data = (
int*)data;
517 "Failed to iterate file id tag on local vertices" );
519 int bytes_per_tag = 4;
521 "Can't get number of bytes for file id tag" );
522 if( 4 == bytes_per_tag )
524 gid_data = (
int*)data;
527 else if( 8 == bytes_per_tag )
529 long* handle_tag_data = (
long*)data;
535 std::map< EntityHandle, EntityHandle > vert_handles;
537 vert_handles[*rit] = start_vertex + i;
540 for(
int q = 0; q < 4 * num_coarse_quads; q++ )
542 conn_arr[q] = vert_handles[conn_arr[q]];
543 assert( conn_arr[q] );
548 for(
int q = 0; q < verts_per_quad * num_coarse_quads; q++ )
550 sv_ptr[q] = vert_handles[sv_ptr[q]];
556 faces.
merge( tmp_range );
559 "Failed to add new vertices and quads to current file set" );
565 "Trouble creating SPECTRAL_ORDER tag" );
567 "Trouble setting data to SPECTRAL_ORDER tag" );
579 "Failed to create gather set vertices" );
590 xptr[i] = rad * xmult;
591 yptr[i] = rad * ymult;
592 zptr[i] = rad * zmult;
596 Range gather_set_verts_range( start_vertex, start_vertex +
nVertices - 1 );
599 "Failed to iterate global id tag on gather set vertices" );
601 gid_data = (
int*)data;
608 gather_set_verts_range.
end(), count, data ),
609 "Failed to iterate file id tag on gather set vertices" );
611 int bytes_per_tag = 4;
613 "Can't get number of bytes for file id tag" );
614 if( 4 == bytes_per_tag )
616 gid_data = (
int*)data;
620 else if( 8 == bytes_per_tag )
622 long* handle_tag_data = (
long*)data;
629 "Failed to add vertices to the gather set" );
632 Range gather_set_quads_range;
636 "Failed to create gather set quads" );
637 gather_set_quads_range.
insert( start_quad, start_quad + num_quads - 1 );
638 int* tmp_conn_end = ( &tmp_conn[4 * num_quads - 1] ) + 1;
639 std::copy( &tmp_conn[0], tmp_conn_end, conn_arr );
640 for( i = 0; i != 4 * num_quads; i++ )
641 conn_arr[i] += start_vertex - 1;
643 "Failed to add quads to the gather set" );
650 std::vector< int >& tstep_nums )
661 "Trouble getting vertices in current file set" );
662 assert(
"Should only have a single vertex subrange, since they were read in one shot" && verts.
psize() == 1 );
664 for(
unsigned int i = 0; i < vdatas.size(); i++ )
667 assert( 3 == vdatas[i].varDims.size() );
670 assert(
tDim == vdatas[i].varDims[0] );
673 vdatas[i].readStarts.resize( 3 );
674 vdatas[i].readCounts.resize( 3 );
677 vdatas[i].readStarts[0] = 0;
678 vdatas[i].readCounts[0] = 1;
681 vdatas[i].readStarts[1] = 0;
682 vdatas[i].readCounts[1] = vdatas[i].numLev;
685 switch( vdatas[i].entLoc )
696 MB_SET_ERR( MB_FAILURE,
"Unexpected entity location type for variable " << vdatas[i].varName );
701 for( std::size_t idx = 0; idx != 3; idx++ )
702 vdatas[i].sz *= vdatas[i].readCounts[idx];
704 for(
unsigned int t = 0; t < tstep_nums.size(); t++ )
706 dbgOut.
tprintf( 2,
"Reading variable %s, time step %d\n", vdatas[i].varName.c_str(), tstep_nums[t] );
708 if( tstep_nums[t] >= dimLens[
tDim] )
714 if( !vdatas[i].varTags[t] )
717 "Trouble getting tag for variable " << vdatas[i].varName );
724 "Failed to iterate tag for variable " << vdatas[i].varName );
725 assert( (
unsigned)count == range->
size() );
726 vdatas[i].varDatas[t] = data;
733 #ifdef MOAB_HAVE_PNETCDF
734 ErrorCode NCHelperHOMME::read_ucd_variables_to_nonset_async( std::vector< ReadNC::VarData >& vdatas,
735 std::vector< int >& tstep_nums )
740 "Trouble allocating space to read non-set variables" );
745 for(
unsigned int i = 0; i < vdatas.size(); i++ )
747 std::size_t sz = vdatas[i].sz;
751 size_t ni = vdatas[i].readCounts[2];
753 size_t nk = vdatas[i].readCounts[1];
755 for(
unsigned int t = 0; t < tstep_nums.size(); t++ )
760 std::vector< int > requests( nb_reads ), statuss( nb_reads );
764 void* data = vdatas[i].varDatas[t];
767 vdatas[i].readStarts[0] = tstep_nums[t];
769 switch( vdatas[i].varDataType )
774 std::vector< double > tmpdoubledata( sz );
781 size_t indexInDoubleArray = 0;
788 vdatas[i].readStarts[2] = (
NCDF_SIZE)( starth - 1 );
789 vdatas[i].readCounts[2] = (
NCDF_SIZE)( endh - starth + 1 );
794 NCFUNCREQG( _vara_double )(
_fileId, vdatas[i].varId, &( vdatas[i].readStarts[0] ),
795 &( vdatas[i].readCounts[0] ),
796 &( tmpdoubledata[indexInDoubleArray] ), &requests[idxReq++] );
799 "Failed to read double data in a loop for variable " << vdatas[i].varName );
802 indexInDoubleArray += ( endh - starth + 1 ) * 1 * vdatas[i].numLev;
807 if( success )
MB_SET_ERR( MB_FAILURE,
"Failed on wait_all" );
809 if( vdatas[i].numLev > 1 )
814 for( std::size_t idx = 0; idx != tmpdoubledata.size(); idx++ )
815 ( (
double*)data )[idx] = tmpdoubledata[idx];
821 MB_SET_ERR( MB_FAILURE,
"Unexpected data type for variable " << vdatas[i].varName );
829 dbgOut.
printf( 1,
"Read variables: %s", vdatas.begin()->varName.c_str() );
830 for(
unsigned int i = 1; i < vdatas.size(); i++ )
831 dbgOut.
printf( 1,
", %s ", vdatas[i].varName.c_str() );
839 std::vector< int >& tstep_nums )
844 "Trouble allocating space to read non-set variables" );
848 for(
unsigned int i = 0; i < vdatas.size(); i++ )
850 std::size_t sz = vdatas[i].sz;
854 size_t ni = vdatas[i].readCounts[2];
856 size_t nk = vdatas[i].readCounts[1];
858 for(
unsigned int t = 0; t < tstep_nums.size(); t++ )
861 void* data = vdatas[i].varDatas[t];
864 vdatas[i].readStarts[0] = tstep_nums[t];
866 switch( vdatas[i].varDataType )
871 std::vector< double > tmpdoubledata( sz );
878 size_t indexInDoubleArray = 0;
885 vdatas[i].readStarts[2] = (
NCDF_SIZE)( starth - 1 );
886 vdatas[i].readCounts[2] = (
NCDF_SIZE)( endh - starth + 1 );
888 success =
NCFUNCAG( _vara_double )(
_fileId, vdatas[i].varId, &( vdatas[i].readStarts[0] ),
889 &( vdatas[i].readCounts[0] ),
890 &( tmpdoubledata[indexInDoubleArray] ) );
893 "Failed to read double data in a loop for variable " << vdatas[i].varName );
896 indexInDoubleArray += ( endh - starth + 1 ) * 1 * vdatas[i].numLev;
900 if( vdatas[i].numLev > 1 )
905 for( std::size_t idx = 0; idx != tmpdoubledata.size(); idx++ )
906 ( (
double*)data )[idx] = tmpdoubledata[idx];
912 MB_SET_ERR( MB_FAILURE,
"Unexpected data type for variable " << vdatas[i].varName );
920 dbgOut.
printf( 1,
"Read variables: %s", vdatas.begin()->varName.c_str() );
921 for(
unsigned int i = 1; i < vdatas.size(); i++ )
922 dbgOut.
printf( 1,
", %s ", vdatas[i].varName.c_str() );