Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
moab::NCHelper Class Referenceabstract

Helper class to isolate reading of several different nc file formats. More...

#include <NCHelper.hpp>

+ Inheritance diagram for moab::NCHelper:
+ Collaboration diagram for moab::NCHelper:

Classes

class  Node3D
 

Public Member Functions

 NCHelper (ReadNC *readNC, int fileId, const FileOptions &opts, EntityHandle fileSet)
 
virtual ~NCHelper ()
 
virtual ErrorCode init_mesh_vals ()=0
 Interfaces to be implemented in child classes. More...
 
virtual ErrorCode check_existing_mesh ()=0
 
virtual ErrorCode create_mesh (Range &faces)=0
 
virtual ErrorCode read_variables (std::vector< std::string > &var_names, std::vector< int > &tstep_nums)=0
 
virtual std::string get_mesh_type_name ()=0
 
ErrorCode create_conventional_tags (const std::vector< int > &tstep_nums)
 Create NC conventional tags. More...
 
ErrorCode update_time_tag_vals ()
 Update time tag values if timesteps spread across files. More...
 

Static Public Member Functions

static ReadNC::NCFormatType get_nc_format (ReadNC *readNC, int fileId)
 Get appropriate format to read the file. More...
 
static std::string get_default_ncformat_options (ReadNC::NCFormatType format)
 Get appropriate format to read the file. More...
 
static NCHelperget_nc_helper (ReadNC *readNC, int fileId, const FileOptions &opts, EntityHandle fileSet)
 Get appropriate helper instance for ReadNC class. More...
 

Protected Member Functions

ErrorCode read_variables_setup (std::vector< std::string > &var_names, std::vector< int > &tstep_nums, std::vector< ReadNC::VarData > &vdatas, std::vector< ReadNC::VarData > &vsetdatas)
 Separate set and non-set variables (common to scd mesh and ucd mesh) More...
 
ErrorCode read_variables_to_set (std::vector< ReadNC::VarData > &vdatas, std::vector< int > &tstep_nums)
 Read set variables (common to scd mesh and ucd mesh) More...
 
ErrorCode read_coordinate (const char *var_name, int lmin, int lmax, std::vector< double > &cvals)
 
ErrorCode get_tag_to_set (ReadNC::VarData &var_data, int tstep_num, Tag &tagh)
 
ErrorCode get_tag_to_nonset (ReadNC::VarData &var_data, int tstep_num, Tag &tagh, int num_lev)
 
ErrorCode create_attrib_string (const std::map< std::string, ReadNC::AttData > &attMap, std::string &attString, std::vector< int > &attLen)
 Create a character string attString of attMap. with '\0' terminating each attribute name, ';' separating the data type and value, and ';' separating one name/data type/value from the next'. attLen stores the end position for each name/data type/ value. More...
 
ErrorCode create_dummy_variables ()
 For a dimension that does not have a corresponding coordinate variable (e.g. ncol for HOMME), create a dummy variable with a sparse tag to store the dimension length. More...
 

Protected Attributes

ReadNC_readNC
 Allow NCHelper to directly access members of ReadNC. More...
 
int _fileId
 Cache some information from ReadNC. More...
 
const FileOptions_opts
 
EntityHandle _fileSet
 
int nTimeSteps
 Dimensions of time and level. More...
 
int nLevels
 
std::vector< double > tVals
 Values for time and level. More...
 
std::vector< double > levVals
 
int tDim
 Dimension numbers for time and level. More...
 
int levDim
 
std::set< std::string > ignoredVarNames
 Ignored variables. More...
 
std::set< std::string > dummyVarNames
 Dummy variables. More...
 

Private Member Functions

ErrorCode read_variables_to_set_allocate (std::vector< ReadNC::VarData > &vdatas, std::vector< int > &tstep_nums)
 Used by read_variables_to_set() More...
 

Detailed Description

Helper class to isolate reading of several different nc file formats.

Definition at line 24 of file NCHelper.hpp.

Constructor & Destructor Documentation

◆ NCHelper()

moab::NCHelper::NCHelper ( ReadNC readNC,
int  fileId,
const FileOptions opts,
EntityHandle  fileSet 
)
inline

Definition at line 27 of file NCHelper.hpp.

28  : _readNC( readNC ), _fileId( fileId ), _opts( opts ), _fileSet( fileSet ), nTimeSteps( 0 ), nLevels( 1 ),
29  tDim( -1 ), levDim( -1 )
30  {
31  }

◆ ~NCHelper()

virtual moab::NCHelper::~NCHelper ( )
inlinevirtual

Definition at line 32 of file NCHelper.hpp.

32 {}

Member Function Documentation

◆ check_existing_mesh()

virtual ErrorCode moab::NCHelper::check_existing_mesh ( )
pure virtual

◆ create_attrib_string()

ErrorCode moab::NCHelper::create_attrib_string ( const std::map< std::string, ReadNC::AttData > &  attMap,
std::string &  attString,
std::vector< int > &  attLen 
)
protected

Create a character string attString of attMap. with '\0' terminating each attribute name, ';' separating the data type and value, and ';' separating one name/data type/value from the next'. attLen stores the end position for each name/data type/ value.

Definition at line 774 of file NCHelper.cpp.

777 {
778  int success;
779  std::stringstream ssAtt;
780  unsigned int sz = 0;
781  std::map< std::string, ReadNC::AttData >::const_iterator attIt = attMap.begin();
782  for( ; attIt != attMap.end(); ++attIt )
783  {
784  ssAtt << attIt->second.attName;
785  ssAtt << '\0';
786  void* attData = NULL;
787  switch( attIt->second.attDataType )
788  {
789  case NC_BYTE:
790  case NC_CHAR:
791  sz = attIt->second.attLen;
792  attData = (char*)malloc( sz );
793  success = NCFUNC( get_att_text )( _fileId, attIt->second.attVarId, attIt->second.attName.c_str(),
794  (char*)attData );
795  if( success )
796  MB_SET_ERR( MB_FAILURE, "Failed to read byte/char data for attribute " << attIt->second.attName );
797  ssAtt << "char;";
798  break;
799  case NC_SHORT:
800  sz = attIt->second.attLen * sizeof( short );
801  attData = (short*)malloc( sz );
802  success = NCFUNC( get_att_short )( _fileId, attIt->second.attVarId, attIt->second.attName.c_str(),
803  (short*)attData );
804  if( success )
805  MB_SET_ERR( MB_FAILURE, "Failed to read short data for attribute " << attIt->second.attName );
806  ssAtt << "short;";
807  break;
808  case NC_INT:
809  sz = attIt->second.attLen * sizeof( int );
810  attData = (int*)malloc( sz );
811  success = NCFUNC( get_att_int )( _fileId, attIt->second.attVarId, attIt->second.attName.c_str(),
812  (int*)attData );
813  if( success )
814  MB_SET_ERR( MB_FAILURE, "Failed to read int data for attribute " << attIt->second.attName );
815  ssAtt << "int;";
816  break;
817  case NC_INT64: // be careful here
818  sz = attIt->second.attLen * sizeof( long );
819  attData = (long*)malloc( sz );
820  success = NCFUNC( get_att_long )( _fileId, attIt->second.attVarId, attIt->second.attName.c_str(),
821  (long*)attData );
822  if( success )
823  MB_SET_ERR( MB_FAILURE, "Failed to read int data for attribute " << attIt->second.attName );
824  ssAtt << "long;";
825  break;
826  case NC_FLOAT:
827  sz = attIt->second.attLen * sizeof( float );
828  attData = (float*)malloc( sz );
829  success = NCFUNC( get_att_float )( _fileId, attIt->second.attVarId, attIt->second.attName.c_str(),
830  (float*)attData );
831  if( success )
832  MB_SET_ERR( MB_FAILURE, "Failed to read float data for attribute " << attIt->second.attName );
833  ssAtt << "float;";
834  break;
835  case NC_DOUBLE:
836  sz = attIt->second.attLen * sizeof( double );
837  attData = (double*)malloc( sz );
838  success = NCFUNC( get_att_double )( _fileId, attIt->second.attVarId, attIt->second.attName.c_str(),
839  (double*)attData );
840  if( success )
841  MB_SET_ERR( MB_FAILURE, "Failed to read double data for attribute " << attIt->second.attName );
842  ssAtt << "double;";
843  break;
844  default:
845  MB_SET_ERR( MB_FAILURE, "Unexpected data type for attribute " << attIt->second.attName );
846  }
847  char* tmpc = (char*)attData;
848  for( unsigned int counter = 0; counter != sz; ++counter )
849  ssAtt << tmpc[counter];
850  free( attData );
851  ssAtt << ';';
852  attLen.push_back( ssAtt.str().size() - 1 );
853  }
854  attVal = ssAtt.str();
855 
856  return MB_SUCCESS;
857 }

References _fileId, MB_SET_ERR, MB_SUCCESS, and NCFUNC.

Referenced by create_conventional_tags().

◆ create_conventional_tags()

ErrorCode moab::NCHelper::create_conventional_tags ( const std::vector< int > &  tstep_nums)

Create NC conventional tags.

Definition at line 120 of file NCHelper.cpp.

121 {
122  Interface*& mbImpl = _readNC->mbImpl;
123  std::vector< std::string >& dimNames = _readNC->dimNames;
124  std::vector< int >& dimLens = _readNC->dimLens;
125  std::map< std::string, ReadNC::AttData >& globalAtts = _readNC->globalAtts;
126  std::map< std::string, ReadNC::VarData >& varInfo = _readNC->varInfo;
127  DebugOutput& dbgOut = _readNC->dbgOut;
128  int& partMethod = _readNC->partMethod;
129  ScdInterface* scdi = _readNC->scdi;
130 
131  std::string tag_name;
132 
133  // <__NUM_DIMS>
134  Tag numDimsTag = 0;
135  tag_name = "__NUM_DIMS";
136  int numDims = dimNames.size();
137  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.c_str(), 1, MB_TYPE_INTEGER, numDimsTag,
139  "Trouble creating conventional tag " << tag_name );
140  MB_CHK_SET_ERR( mbImpl->tag_set_data( numDimsTag, &_fileSet, 1, &numDims ),
141  "Trouble setting data to conventional tag " << tag_name );
142  dbgOut.tprintf( 2, "Conventional tag %s created\n", tag_name.c_str() );
143 
144  // <__NUM_VARS>
145  Tag numVarsTag = 0;
146  tag_name = "__NUM_VARS";
147  int numVars = varInfo.size();
148  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.c_str(), 1, MB_TYPE_INTEGER, numVarsTag,
150  "Trouble creating conventional tag " << tag_name );
151  MB_CHK_SET_ERR( mbImpl->tag_set_data( numVarsTag, &_fileSet, 1, &numVars ),
152  "Trouble setting data to conventional tag " << tag_name );
153  dbgOut.tprintf( 2, "Conventional tag %s created\n", tag_name.c_str() );
154 
155  // <__DIM_NAMES>
156  Tag dimNamesTag = 0;
157  tag_name = "__DIM_NAMES";
158  std::string dimnames;
159  unsigned int dimNamesSz = dimNames.size();
160  for( unsigned int i = 0; i != dimNamesSz; i++ )
161  {
162  dimnames.append( dimNames[i] );
163  dimnames.push_back( '\0' );
164  }
165  int dimnamesSz = dimnames.size();
166  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.c_str(), 0, MB_TYPE_OPAQUE, dimNamesTag,
168  "Trouble creating conventional tag " << tag_name );
169  const void* ptr = dimnames.c_str();
170  MB_CHK_SET_ERR( mbImpl->tag_set_by_ptr( dimNamesTag, &_fileSet, 1, &ptr, &dimnamesSz ),
171  "Trouble setting data to conventional tag " << tag_name );
172  dbgOut.tprintf( 2, "Conventional tag %s created\n", tag_name.c_str() );
173 
174  // <__DIM_LENS>
175  Tag dimLensTag = 0;
176  tag_name = "__DIM_LENS";
177  int dimLensSz = dimLens.size();
178  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.c_str(), 0, MB_TYPE_INTEGER, dimLensTag,
180  "Trouble creating conventional tag " << tag_name );
181  ptr = &( dimLens[0] );
182  MB_CHK_SET_ERR( mbImpl->tag_set_by_ptr( dimLensTag, &_fileSet, 1, &ptr, &dimLensSz ),
183  "Trouble setting data to conventional tag " << tag_name );
184  dbgOut.tprintf( 2, "Conventional tag %s created\n", tag_name.c_str() );
185 
186  // <__VAR_NAMES>
187  Tag varNamesTag = 0;
188  tag_name = "__VAR_NAMES";
189  std::string varnames;
190  std::map< std::string, ReadNC::VarData >::iterator mapIter;
191  for( mapIter = varInfo.begin(); mapIter != varInfo.end(); ++mapIter )
192  {
193  varnames.append( mapIter->first );
194  varnames.push_back( '\0' );
195  }
196  int varnamesSz = varnames.size();
197  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.c_str(), 0, MB_TYPE_OPAQUE, varNamesTag,
199  "Trouble creating conventional tag " << tag_name );
200  ptr = varnames.c_str();
201  MB_CHK_SET_ERR( mbImpl->tag_set_by_ptr( varNamesTag, &_fileSet, 1, &ptr, &varnamesSz ),
202  "Trouble setting data to conventional tag " << tag_name );
203  dbgOut.tprintf( 2, "Conventional tag %s created\n", tag_name.c_str() );
204 
205  // __<dim_name>_LOC_MINMAX (for time)
206  for( unsigned int i = 0; i != dimNamesSz; i++ )
207  {
208  if( dimNames[i] == "time" || dimNames[i] == "Time" || dimNames[i] == "t" )
209  {
210  // some files might have Time dimension as 0, it will not appear in any
211  // variables, so skip it
212  if( nTimeSteps == 0 ) continue;
213  std::stringstream ss_tag_name;
214  ss_tag_name << "__" << dimNames[i] << "_LOC_MINMAX";
215  tag_name = ss_tag_name.str();
216  Tag tagh = 0;
217  std::vector< int > val( 2, 0 );
218  val[0] = 0;
219  val[1] = nTimeSteps - 1;
220  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.c_str(), 2, MB_TYPE_INTEGER, tagh,
222  "Trouble creating conventional tag " << tag_name );
223  MB_CHK_SET_ERR( mbImpl->tag_set_data( tagh, &_fileSet, 1, &val[0] ),
224  "Trouble setting data to conventional tag " << tag_name );
225  dbgOut.tprintf( 2, "Conventional tag %s created\n", tag_name.c_str() );
226  }
227  }
228 
229  // __<dim_name>_LOC_VALS (for time)
230  for( unsigned int i = 0; i != dimNamesSz; i++ )
231  {
232  if( dimNames[i] == "time" || dimNames[i] == "Time" || dimNames[i] == "t" )
233  {
234  std::vector< int > val;
235  if( !tstep_nums.empty() )
236  val = tstep_nums;
237  else
238  {
239  // some files might have Time dimension as 0, it will not appear in any
240  // variables, so skip it
241  if( tVals.empty() ) continue;
242  val.resize( tVals.size() );
243  for( unsigned int j = 0; j != tVals.size(); j++ )
244  val[j] = j;
245  }
246  Tag tagh = 0;
247  std::stringstream ss_tag_name;
248  ss_tag_name << "__" << dimNames[i] << "_LOC_VALS";
249  tag_name = ss_tag_name.str();
250  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.c_str(), val.size(), MB_TYPE_INTEGER, tagh,
252  "Trouble creating conventional tag " << tag_name );
253  MB_CHK_SET_ERR( mbImpl->tag_set_data( tagh, &_fileSet, 1, &val[0] ),
254  "Trouble setting data to conventional tag " << tag_name );
255  dbgOut.tprintf( 2, "Conventional tag %s created\n", tag_name.c_str() );
256  }
257  }
258 
259  // __<var_name>_DIMS
260  for( mapIter = varInfo.begin(); mapIter != varInfo.end(); ++mapIter )
261  {
262  Tag varNamesDimsTag = 0;
263  std::stringstream ss_tag_name;
264  ss_tag_name << "__" << mapIter->first << "_DIMS";
265  tag_name = ss_tag_name.str();
266  unsigned int varDimSz = varInfo[mapIter->first].varDims.size();
267  if( varDimSz == 0 ) continue;
268  std::vector< Tag > varDimTags( varDimSz );
269  for( unsigned int i = 0; i != varDimSz; i++ )
270  {
271  Tag tmptag = 0;
272  std::string tmptagname = dimNames[varInfo[mapIter->first].varDims[i]];
273  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tmptagname.c_str(), 0, MB_TYPE_OPAQUE, tmptag, MB_TAG_ANY ),
274  "Trouble getting tag " << tmptagname );
275  varDimTags[i] = tmptag;
276  }
277  // rval = mbImpl->tag_get_handle(tag_name.c_str(), varDimSz, MB_TYPE_HANDLE,
278  // varNamesDimsTag, MB_TAG_SPARSE | MB_TAG_CREAT); We should not use MB_TYPE_HANDLE for Tag
279  // here. Tag is a pointer, which is 4 bytes on 32 bit machines and 8 bytes on 64 bit
280  // machines. Normally, entity handle is 8 bytes on 64 bit machines, but it can also be
281  // configured to 4 bytes.
282  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.c_str(), varDimSz * sizeof( Tag ), MB_TYPE_OPAQUE,
283  varNamesDimsTag, MB_TAG_SPARSE | MB_TAG_CREAT ),
284  "Trouble creating conventional tag " << tag_name );
285  MB_CHK_SET_ERR( mbImpl->tag_set_data( varNamesDimsTag, &_fileSet, 1, &( varDimTags[0] ) ),
286  "Trouble setting data to conventional tag " << tag_name );
287  dbgOut.tprintf( 2, "Conventional tag %s created\n", tag_name.c_str() );
288  }
289 
290  // <PARTITION_METHOD>
291  Tag part_tag = scdi->part_method_tag();
292  if( !part_tag ) MB_SET_ERR( MB_FAILURE, "Trouble getting PARTITION_METHOD tag" );
293  MB_CHK_SET_ERR( mbImpl->tag_set_data( part_tag, &_fileSet, 1, &partMethod ),
294  "Trouble setting data to PARTITION_METHOD tag" );
295  dbgOut.tprintf( 2, "Conventional tag %s created\n", tag_name.c_str() );
296 
297  // <__GLOBAL_ATTRIBS>
298  tag_name = "__GLOBAL_ATTRIBS";
299  Tag globalAttTag = 0;
300  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.c_str(), 0, MB_TYPE_OPAQUE, globalAttTag,
302  "Trouble creating conventional tag " << tag_name );
303  std::string gattVal;
304  std::vector< int > gattLen;
305  MB_CHK_SET_ERR( create_attrib_string( globalAtts, gattVal, gattLen ), "Trouble creating global attribute string" );
306  const void* gattptr = gattVal.c_str();
307  int globalAttSz = gattVal.size();
308  MB_CHK_SET_ERR( mbImpl->tag_set_by_ptr( globalAttTag, &_fileSet, 1, &gattptr, &globalAttSz ),
309  "Trouble setting data to conventional tag " << tag_name );
310  dbgOut.tprintf( 2, "Conventional tag %s created\n", tag_name.c_str() );
311 
312  // <__GLOBAL_ATTRIBS_LEN>
313  tag_name = "__GLOBAL_ATTRIBS_LEN";
314  Tag globalAttLenTag = 0;
315  if( gattLen.size() == 0 ) gattLen.push_back( 0 );
316  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.c_str(), gattLen.size(), MB_TYPE_INTEGER, globalAttLenTag,
318  "Trouble creating conventional tag " << tag_name );
319  MB_CHK_SET_ERR( mbImpl->tag_set_data( globalAttLenTag, &_fileSet, 1, &gattLen[0] ),
320  "Trouble setting data to conventional tag " << tag_name );
321  dbgOut.tprintf( 2, "Conventional tag %s created\n", tag_name.c_str() );
322 
323  // __<var_name>_ATTRIBS and __<var_name>_ATTRIBS_LEN
324  for( mapIter = varInfo.begin(); mapIter != varInfo.end(); ++mapIter )
325  {
326  std::stringstream ssTagName;
327  ssTagName << "__" << mapIter->first << "_ATTRIBS";
328  tag_name = ssTagName.str();
329  Tag varAttTag = 0;
330  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.c_str(), 0, MB_TYPE_OPAQUE, varAttTag,
332  "Trouble creating conventional tag " << tag_name );
333 
334  std::string varAttVal;
335  std::vector< int > varAttLen;
336  if( mapIter->second.numAtts < 1 )
337  {
338  if( dummyVarNames.find( mapIter->first ) != dummyVarNames.end() )
339  {
340  // This variable is a dummy coordinate variable
341  varAttVal = "DUMMY_VAR";
342  }
343  else
344  {
345  // This variable has no attributes
346  varAttVal = "NO_ATTRIBS";
347  }
348  }
349  else
350  {
351  MB_CHK_SET_ERR( create_attrib_string( mapIter->second.varAtts, varAttVal, varAttLen ),
352  "Trouble creating attribute string for variable " << mapIter->first );
353  }
354  const void* varAttPtr = varAttVal.c_str();
355  int varAttSz = varAttVal.size();
356  if( 0 == varAttSz ) varAttSz = 1;
357  MB_CHK_SET_ERR( mbImpl->tag_set_by_ptr( varAttTag, &_fileSet, 1, &varAttPtr, &varAttSz ),
358  "Trouble setting data to conventional tag " << tag_name );
359  dbgOut.tprintf( 2, "Conventional tag %s created\n", tag_name.c_str() );
360 
361  ssTagName << "_LEN";
362  tag_name = ssTagName.str();
363  Tag varAttLenTag = 0;
364  if( 0 == varAttLen.size() ) varAttLen.push_back( 0 );
365  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.c_str(), varAttLen.size(), MB_TYPE_INTEGER, varAttLenTag,
367  "Trouble creating conventional tag " << tag_name );
368  MB_CHK_SET_ERR( mbImpl->tag_set_data( varAttLenTag, &_fileSet, 1, &varAttLen[0] ),
369  "Trouble setting data to conventional tag " << tag_name );
370  dbgOut.tprintf( 2, "Conventional tag %s created\n", tag_name.c_str() );
371  }
372 
373  // <__VAR_NAMES_LOCATIONS>
374  tag_name = "__VAR_NAMES_LOCATIONS";
375  Tag varNamesLocsTag = 0;
376  std::vector< int > varNamesLocs( varInfo.size() );
377  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.c_str(), varNamesLocs.size(), MB_TYPE_INTEGER, varNamesLocsTag,
379  "Trouble creating conventional tag " << tag_name );
380  for( mapIter = varInfo.begin(); mapIter != varInfo.end(); ++mapIter )
381  {
382  varNamesLocs[std::distance( varInfo.begin(), mapIter )] = mapIter->second.entLoc;
383  }
384  MB_CHK_SET_ERR( mbImpl->tag_set_data( varNamesLocsTag, &_fileSet, 1, &varNamesLocs[0] ),
385  "Trouble setting data to conventional tag " << tag_name );
386  dbgOut.tprintf( 2, "Conventional tag %s created\n", tag_name.c_str() );
387 
388  // <__MESH_TYPE>
389  Tag meshTypeTag = 0;
390  tag_name = "__MESH_TYPE";
391  std::string meshTypeName = get_mesh_type_name();
392 
393  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.c_str(), 0, MB_TYPE_OPAQUE, meshTypeTag,
395  "Trouble creating conventional tag " << tag_name );
396  ptr = meshTypeName.c_str();
397  int leng = meshTypeName.size();
398  MB_CHK_SET_ERR( mbImpl->tag_set_by_ptr( meshTypeTag, &_fileSet, 1, &ptr, &leng ),
399  "Trouble setting data to conventional tag " << tag_name );
400  dbgOut.tprintf( 2, "Conventional tag %s created\n", tag_name.c_str() );
401 
402  return MB_SUCCESS;
403 }

References _fileSet, _readNC, create_attrib_string(), moab::ReadNC::dbgOut, moab::ReadNC::dimLens, moab::ReadNC::dimNames, dummyVarNames, get_mesh_type_name(), moab::ReadNC::globalAtts, MB_CHK_SET_ERR, MB_SET_ERR, MB_SUCCESS, MB_TAG_ANY, MB_TAG_CREAT, MB_TAG_SPARSE, MB_TAG_VARLEN, MB_TYPE_INTEGER, MB_TYPE_OPAQUE, moab::ReadNC::mbImpl, nTimeSteps, moab::ScdInterface::part_method_tag(), moab::ReadNC::partMethod, moab::ReadNC::scdi, moab::Interface::tag_get_handle(), moab::Interface::tag_set_by_ptr(), moab::Interface::tag_set_data(), moab::DebugOutput::tprintf(), tVals, and moab::ReadNC::varInfo.

Referenced by moab::ReadNC::load_file().

◆ create_dummy_variables()

ErrorCode moab::NCHelper::create_dummy_variables ( )
protected

For a dimension that does not have a corresponding coordinate variable (e.g. ncol for HOMME), create a dummy variable with a sparse tag to store the dimension length.

Definition at line 859 of file NCHelper.cpp.

860 {
861  Interface*& mbImpl = _readNC->mbImpl;
862  std::vector< std::string >& dimNames = _readNC->dimNames;
863  std::vector< int >& dimLens = _readNC->dimLens;
864  std::map< std::string, ReadNC::VarData >& varInfo = _readNC->varInfo;
865  DebugOutput& dbgOut = _readNC->dbgOut;
866 
867  // Hack: look at all dimensions, and see if we have one that does not appear in the list of
868  // varInfo names Right now, candidates are from unstructured meshes, such as ncol (HOMME) and
869  // nCells (MPAS) For each of them, create a dummy coordinate variable with a sparse tag to store
870  // the dimension length
871  for( unsigned int i = 0; i < dimNames.size(); i++ )
872  {
873  // If there is a variable with this dimension name, skip
874  if( varInfo.find( dimNames[i] ) != varInfo.end() ) continue;
875 
876  // Create a dummy coordinate variable
877  int sizeTotalVar = varInfo.size();
878  std::string var_name( dimNames[i] );
879  ReadNC::VarData& data = varInfo[var_name];
880  data.varName = var_name;
881  data.varId = sizeTotalVar;
882  data.varTags.resize( 1, 0 );
883  data.varDataType = NC_INT;
884  data.varDims.resize( 1 );
885  data.varDims[0] = (int)i;
886  data.numAtts = 0;
887  data.entLoc = ReadNC::ENTLOCSET;
888  dummyVarNames.insert( var_name );
889  dbgOut.tprintf( 2, "Dummy coordinate variable created for dimension %s\n", var_name.c_str() );
890 
891  // Create a corresponding sparse tag
892  Tag tagh;
893  MB_CHK_SET_ERR( mbImpl->tag_get_handle( var_name.c_str(), 0, MB_TYPE_INTEGER, tagh,
895  "Trouble creating tag for dummy coordinate variable " << var_name );
896 
897  // Tag value is the dimension length
898  const void* ptr = &dimLens[i];
899  // Tag size is 1
900  int size = 1;
901  MB_CHK_SET_ERR( mbImpl->tag_set_by_ptr( tagh, &_fileSet, 1, &ptr, &size ),
902  "Trouble setting tag data for dummy coordinate variable " << var_name );
903 
904  dbgOut.tprintf( 2, "Sparse tag created for dimension %s\n", var_name.c_str() );
905  }
906 
907  return MB_SUCCESS;
908 }

References _fileSet, _readNC, moab::ReadNC::dbgOut, moab::ReadNC::dimLens, moab::ReadNC::dimNames, dummyVarNames, moab::ReadNC::VarData::entLoc, moab::ReadNC::ENTLOCSET, MB_CHK_SET_ERR, MB_SUCCESS, MB_TAG_CREAT, MB_TAG_SPARSE, MB_TAG_VARLEN, MB_TYPE_INTEGER, moab::ReadNC::mbImpl, moab::ReadNC::VarData::numAtts, moab::Interface::tag_get_handle(), moab::Interface::tag_set_by_ptr(), moab::DebugOutput::tprintf(), moab::ReadNC::VarData::varDataType, moab::ReadNC::VarData::varDims, moab::ReadNC::VarData::varId, moab::ReadNC::varInfo, moab::ReadNC::VarData::varName, and moab::ReadNC::VarData::varTags.

Referenced by moab::NCHelperESMF::init_mesh_vals(), moab::NCHelperEuler::init_mesh_vals(), moab::NCHelperFV::init_mesh_vals(), moab::NCHelperGCRM::init_mesh_vals(), moab::NCHelperHOMME::init_mesh_vals(), and moab::NCHelperMPAS::init_mesh_vals().

◆ create_mesh()

virtual ErrorCode moab::NCHelper::create_mesh ( Range faces)
pure virtual

◆ get_default_ncformat_options()

std::string moab::NCHelper::get_default_ncformat_options ( ReadNC::NCFormatType  format)
static

Get appropriate format to read the file.

Get appropriate file read options depending on the format of the NC file.

Definition at line 66 of file NCHelper.cpp.

67 {
68  switch( format )
69  {
70  case moab::ReadNC::NC_FORMAT_GCRM: // GCRM format reader
71  case moab::ReadNC::NC_FORMAT_MPAS: // MPAS format reader
72  return "PARALLEL=READ_PART;PARTITION_METHOD=RCBZOLTAN;"
73  "PARALLEL_RESOLVE_SHARED_ENTS;NO_EDGES;NO_MIXED_ELEMENTS;VARIABLE=;";
74  case moab::ReadNC::NC_FORMAT_SCRIP: // SCRIP format reader
75  return "PARALLEL=READ_PART;PARTITION_METHOD=RCBZOLTAN;";
76  case moab::ReadNC::NC_FORMAT_ESMF: // ESMF unstructured format reader
77  return "PARALLEL=READ_PART;PARTITION_METHOD=RCBZOLTAN;PARALLEL_RESOLVE_SHARED_ENTS;VARIABLE=;";
78  case moab::ReadNC::NC_FORMAT_DOMAIN: // Climate Domain reader
79  return "PARALLEL=READ_PART;PARTITION_METHOD=SQIJ;VARIABLE=;";
80  case moab::ReadNC::NC_FORMAT_HOMME: // HOMME format reader
81  return "PARALLEL=READ_PART;PARTITION_METHOD=TRIVIAL;PARALLEL_RESOLVE_SHARED_ENTS;";
82  case moab::ReadNC::NC_FORMAT_EULER: // Euler format reader
83  case moab::ReadNC::NC_FORMAT_FV: // FV climate format reader
84  return "PARALLEL=READ_PART;PARTITION;PARALLEL_RESOLVE_SHARED_ENTS;"
85  "PARTITION_METHOD=SQIJ;VARIABLE=;";
86  default:
87  return "PARALLEL=READ_PART;PARTITION_METHOD=RCBZOLTAN;PARALLEL_RESOLVE_SHARED_ENTS;";
88  }
89 }

References moab::ReadNC::NC_FORMAT_DOMAIN, moab::ReadNC::NC_FORMAT_ESMF, moab::ReadNC::NC_FORMAT_EULER, moab::ReadNC::NC_FORMAT_FV, moab::ReadNC::NC_FORMAT_GCRM, moab::ReadNC::NC_FORMAT_HOMME, moab::ReadNC::NC_FORMAT_MPAS, and moab::ReadNC::NC_FORMAT_SCRIP.

◆ get_mesh_type_name()

virtual std::string moab::NCHelper::get_mesh_type_name ( )
pure virtual

◆ get_nc_format()

ReadNC::NCFormatType moab::NCHelper::get_nc_format ( ReadNC readNC,
int  fileId 
)
static

Get appropriate format to read the file.

Definition at line 24 of file NCHelper.cpp.

25 {
26  // Check if CF convention is being followed
27  bool is_CF = false;
28 
29  std::map< std::string, ReadNC::AttData >& globalAtts = readNC->globalAtts;
30  std::map< std::string, ReadNC::AttData >::iterator attIt = globalAtts.find( "conventions" );
31  if( attIt == globalAtts.end() ) attIt = globalAtts.find( "Conventions" );
32 
33  if( attIt != globalAtts.end() )
34  {
35  unsigned int sz = attIt->second.attLen;
36  std::string att_data;
37  att_data.resize( sz + 1 );
38  att_data[sz] = '\000';
39  int success =
40  NCFUNC( get_att_text )( fileId, attIt->second.attVarId, attIt->second.attName.c_str(), &att_data[0] );
41  if( 0 == success && att_data.find( "CF" ) != std::string::npos ) is_CF = true;
42  }
43 
44  // Depending on the format, update FileOptions as well
45  if( NCHelperMPAS::can_read_file( readNC ) )
47  else if( NCHelperScrip::can_read_file( readNC, fileId ) )
49  else if( NCHelperESMF::can_read_file( readNC ) )
51  else if( NCHelperDomain::can_read_file( readNC, fileId ) ) // && is_CF )
53  else if( NCHelperHOMME::can_read_file( readNC, fileId ) )
55  else if( NCHelperEuler::can_read_file( readNC, fileId ) && is_CF )
57  else if( NCHelperGCRM::can_read_file( readNC ) )
59  else if( NCHelperFV::can_read_file( readNC, fileId ) && is_CF )
60  return ReadNC::NC_FORMAT_FV;
61  else // Unknown NetCDF grid (will fill this in later for POP, CICE and CLM)
63 }

References moab::NCHelperESMF::can_read_file(), moab::NCHelperGCRM::can_read_file(), moab::NCHelperMPAS::can_read_file(), moab::NCHelperDomain::can_read_file(), moab::NCHelperEuler::can_read_file(), moab::NCHelperFV::can_read_file(), moab::NCHelperHOMME::can_read_file(), moab::NCHelperScrip::can_read_file(), moab::ReadNC::globalAtts, moab::ReadNC::NC_FORMAT_DOMAIN, moab::ReadNC::NC_FORMAT_ESMF, moab::ReadNC::NC_FORMAT_EULER, moab::ReadNC::NC_FORMAT_FV, moab::ReadNC::NC_FORMAT_GCRM, moab::ReadNC::NC_FORMAT_HOMME, moab::ReadNC::NC_FORMAT_MPAS, moab::ReadNC::NC_FORMAT_SCRIP, moab::ReadNC::NC_FORMAT_UNKNOWN_TYPE, and NCFUNC.

Referenced by get_nc_helper().

◆ get_nc_helper()

NCHelper * moab::NCHelper::get_nc_helper ( ReadNC readNC,
int  fileId,
const FileOptions opts,
EntityHandle  fileSet 
)
static

Get appropriate helper instance for ReadNC class.

Definition at line 91 of file NCHelper.cpp.

92 {
93  ReadNC::NCFormatType nctype = NCHelper::get_nc_format( readNC, fileId );
94 
95  switch( nctype )
96  {
97  case ReadNC::NC_FORMAT_MPAS: // MPAS format reader
98  return new( std::nothrow ) NCHelperMPAS( readNC, fileId, opts, fileSet );
99  case ReadNC::NC_FORMAT_SCRIP: // SCRIP format reader
100  // SCRIP can be CF or non CF, if it comes from MPAS :)
101  return new( std::nothrow ) NCHelperScrip( readNC, fileId, opts, fileSet );
102  case ReadNC::NC_FORMAT_ESMF: // ESMF unstructured format reader
103  return new( std::nothrow ) NCHelperESMF( readNC, fileId, opts, fileSet );
104  case ReadNC::NC_FORMAT_DOMAIN: // Climate Domain reader
105  return new( std::nothrow ) NCHelperDomain( readNC, fileId, opts, fileSet );
106  case ReadNC::NC_FORMAT_HOMME: // HOMME format reader
107  // For a HOMME connectivity file, there might be no CF convention
108  return new( std::nothrow ) NCHelperHOMME( readNC, fileId, opts, fileSet );
109  case ReadNC::NC_FORMAT_GCRM: // GCRM format reader
110  return new( std::nothrow ) NCHelperGCRM( readNC, fileId, opts, fileSet );
111  case ReadNC::NC_FORMAT_EULER: // Euler format reader
112  return new( std::nothrow ) NCHelperEuler( readNC, fileId, opts, fileSet );
113  case ReadNC::NC_FORMAT_FV: // FV climate format reader
114  return new( std::nothrow ) NCHelperFV( readNC, fileId, opts, fileSet );
115  default: // Unknown NetCDF grid (will fill this in later for POP, CICE and CLM)
116  return nullptr;
117  }
118 }

References get_nc_format(), moab::ReadNC::NC_FORMAT_DOMAIN, moab::ReadNC::NC_FORMAT_ESMF, moab::ReadNC::NC_FORMAT_EULER, moab::ReadNC::NC_FORMAT_FV, moab::ReadNC::NC_FORMAT_GCRM, moab::ReadNC::NC_FORMAT_HOMME, moab::ReadNC::NC_FORMAT_MPAS, and moab::ReadNC::NC_FORMAT_SCRIP.

Referenced by moab::ReadNC::load_file().

◆ get_tag_to_nonset()

ErrorCode moab::NCHelper::get_tag_to_nonset ( ReadNC::VarData var_data,
int  tstep_num,
Tag tagh,
int  num_lev 
)
protected

Definition at line 732 of file NCHelper.cpp.

733 {
734  Interface*& mbImpl = _readNC->mbImpl;
735  DebugOutput& dbgOut = _readNC->dbgOut;
736  int& tStepBase = _readNC->tStepBase;
737 
738  if( tStepBase > 0 ) tstep_num += tStepBase;
739 
740  std::ostringstream tag_name;
741  tag_name << var_data.varName << tstep_num;
742 
743  tagh = 0;
744  switch( var_data.varDataType )
745  {
746  case NC_BYTE:
747  case NC_CHAR:
748  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.str().c_str(), num_lev, MB_TYPE_OPAQUE, tagh,
750  "Trouble creating tag " << tag_name.str() );
751  break;
752  case NC_SHORT:
753  case NC_INT:
754  case NC_INT64:
755  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.str().c_str(), num_lev, MB_TYPE_INTEGER, tagh,
757  "Trouble creating tag " << tag_name.str() );
758  break;
759  case NC_FLOAT:
760  case NC_DOUBLE:
761  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.str().c_str(), num_lev, MB_TYPE_DOUBLE, tagh,
763  "Trouble creating tag " << tag_name.str() );
764  break;
765  default:
766  MB_SET_ERR( MB_FAILURE, "Unexpected data type for variable " << var_data.varName );
767  }
768 
769  dbgOut.tprintf( 2, "Tag %s created\n", tag_name.str().c_str() );
770 
771  return MB_SUCCESS;
772 }

References _readNC, moab::ReadNC::dbgOut, MB_CHK_SET_ERR, MB_SET_ERR, MB_SUCCESS, MB_TAG_CREAT, MB_TAG_DENSE, MB_TYPE_DOUBLE, MB_TYPE_INTEGER, MB_TYPE_OPAQUE, moab::ReadNC::mbImpl, moab::Interface::tag_get_handle(), moab::DebugOutput::tprintf(), moab::ReadNC::tStepBase, moab::ReadNC::VarData::varDataType, and moab::ReadNC::VarData::varName.

Referenced by moab::ScdNCHelper::read_scd_variables_to_nonset_allocate(), moab::NCHelperGCRM::read_ucd_variables_to_nonset_allocate(), moab::NCHelperHOMME::read_ucd_variables_to_nonset_allocate(), and moab::NCHelperMPAS::read_ucd_variables_to_nonset_allocate().

◆ get_tag_to_set()

ErrorCode moab::NCHelper::get_tag_to_set ( ReadNC::VarData var_data,
int  tstep_num,
Tag tagh 
)
protected

Definition at line 687 of file NCHelper.cpp.

688 {
689  Interface*& mbImpl = _readNC->mbImpl;
690  DebugOutput& dbgOut = _readNC->dbgOut;
691  int& tStepBase = _readNC->tStepBase;
692 
693  if( tStepBase > 0 ) tstep_num += tStepBase;
694 
695  std::ostringstream tag_name;
696  if( var_data.has_tsteps )
697  tag_name << var_data.varName << tstep_num;
698  else
699  tag_name << var_data.varName;
700 
701  tagh = 0;
702  switch( var_data.varDataType )
703  {
704  case NC_BYTE:
705  case NC_CHAR:
706  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.str().c_str(), 0, MB_TYPE_OPAQUE, tagh,
708  "Trouble creating tag " << tag_name.str() );
709  break;
710  case NC_SHORT:
711  case NC_INT:
712  case NC_INT64: // this is a big stretch; we should introduce LONG tag
713  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.str().c_str(), 0, MB_TYPE_INTEGER, tagh,
715  "Trouble creating tag " << tag_name.str() );
716  break;
717  case NC_FLOAT:
718  case NC_DOUBLE:
719  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.str().c_str(), 0, MB_TYPE_DOUBLE, tagh,
721  "Trouble creating tag " << tag_name.str() );
722  break;
723  default:
724  MB_SET_ERR( MB_FAILURE, "Unexpected data type for variable " << var_data.varName );
725  }
726 
727  dbgOut.tprintf( 2, "Tag %s created\n", tag_name.str().c_str() );
728 
729  return MB_SUCCESS;
730 }

References _readNC, moab::ReadNC::dbgOut, moab::ReadNC::VarData::has_tsteps, MB_CHK_SET_ERR, MB_SET_ERR, MB_SUCCESS, MB_TAG_CREAT, MB_TAG_SPARSE, MB_TAG_VARLEN, MB_TYPE_DOUBLE, MB_TYPE_INTEGER, MB_TYPE_OPAQUE, moab::ReadNC::mbImpl, moab::Interface::tag_get_handle(), moab::DebugOutput::tprintf(), moab::ReadNC::tStepBase, moab::ReadNC::VarData::varDataType, and moab::ReadNC::VarData::varName.

Referenced by read_variables_to_set_allocate().

◆ init_mesh_vals()

virtual ErrorCode moab::NCHelper::init_mesh_vals ( )
pure virtual

◆ read_coordinate()

ErrorCode moab::NCHelper::read_coordinate ( const char *  var_name,
int  lmin,
int  lmax,
std::vector< double > &  cvals 
)
protected

Definition at line 655 of file NCHelper.cpp.

656 {
657  std::map< std::string, ReadNC::VarData >& varInfo = _readNC->varInfo;
658  std::map< std::string, ReadNC::VarData >::iterator vmit = varInfo.find( var_name );
659  if( varInfo.end() == vmit ) MB_SET_ERR( MB_FAILURE, "Couldn't find variable " << var_name );
660 
661  assert( lmin >= 0 && lmax >= lmin );
662  NCDF_SIZE tstart = lmin;
663  NCDF_SIZE tcount = lmax - lmin + 1;
664  NCDF_DIFF dum_stride = 1;
665  int success;
666 
667  // Check size
668  if( (std::size_t)tcount != cvals.size() ) cvals.resize( tcount );
669 
670  // Check to make sure it's a float or double
671  switch( ( *vmit ).second.varDataType )
672  {
673  case NC_FLOAT:
674  case NC_DOUBLE:
675  // Read float as double
676  success =
677  NCFUNCAG( _vars_double )( _fileId, ( *vmit ).second.varId, &tstart, &tcount, &dum_stride, &cvals[0] );
678  if( success ) MB_SET_ERR( MB_FAILURE, "Failed to read float/double data for variable " << var_name );
679  break;
680  default:
681  MB_SET_ERR( MB_FAILURE, "Unexpected data type for variable " << var_name );
682  }
683 
684  return MB_SUCCESS;
685 }

References _fileId, _readNC, MB_SET_ERR, MB_SUCCESS, NCDF_DIFF, NCDF_SIZE, NCFUNCAG, and moab::ReadNC::varInfo.

Referenced by moab::NCHelperGCRM::create_local_vertices(), moab::NCHelperEuler::init_mesh_vals(), moab::NCHelperFV::init_mesh_vals(), moab::NCHelperGCRM::init_mesh_vals(), moab::NCHelperHOMME::init_mesh_vals(), and moab::NCHelperMPAS::init_mesh_vals().

◆ read_variables()

virtual ErrorCode moab::NCHelper::read_variables ( std::vector< std::string > &  var_names,
std::vector< int > &  tstep_nums 
)
pure virtual

Implemented in moab::UcdNCHelper, and moab::ScdNCHelper.

Referenced by moab::ReadNC::load_file().

◆ read_variables_setup()

ErrorCode moab::NCHelper::read_variables_setup ( std::vector< std::string > &  var_names,
std::vector< int > &  tstep_nums,
std::vector< ReadNC::VarData > &  vdatas,
std::vector< ReadNC::VarData > &  vsetdatas 
)
protected

Separate set and non-set variables (common to scd mesh and ucd mesh)

Definition at line 455 of file NCHelper.cpp.

459 {
460  std::map< std::string, ReadNC::VarData >& varInfo = _readNC->varInfo;
461  std::vector< std::string >& dimNames = _readNC->dimNames;
462 
463  std::map< std::string, ReadNC::VarData >::iterator mit;
464 
465  // If empty read them all (except ignored variables)
466  if( var_names.empty() )
467  {
468  for( mit = varInfo.begin(); mit != varInfo.end(); ++mit )
469  {
470  ReadNC::VarData vd = ( *mit ).second;
471 
472  // If read all variables at once, skip ignored ones
473  if( ignoredVarNames.find( vd.varName ) != ignoredVarNames.end() ) continue;
474 
475  // Coordinate variables (include dummy ones) were read to the file set by default
476  if( std::find( dimNames.begin(), dimNames.end(), vd.varName ) != dimNames.end() ) continue;
477 
478  if( vd.entLoc == ReadNC::ENTLOCSET )
479  vsetdatas.push_back( vd );
480  else
481  vdatas.push_back( vd );
482  }
483  }
484  else
485  {
486  // Read specified variables (might include ignored ones)
487  for( unsigned int i = 0; i < var_names.size(); i++ )
488  {
489  mit = varInfo.find( var_names[i] );
490  if( mit != varInfo.end() )
491  {
492  ReadNC::VarData vd = ( *mit ).second;
493 
494  // Upon creation of dummy coordinate variables, tag values have already been set
495  if( dummyVarNames.find( vd.varName ) != dummyVarNames.end() ) continue;
496 
497  if( vd.entLoc == ReadNC::ENTLOCSET )
498  vsetdatas.push_back( vd );
499  else
500  vdatas.push_back( vd );
501  }
502  else
503  {
504  MB_SET_ERR( MB_FAILURE, "Couldn't find specified variable " << var_names[i] );
505  }
506  }
507  }
508 
509  if( tstep_nums.empty() && nTimeSteps > 0 )
510  {
511  // No timesteps input, get them all
512  for( int i = 0; i < nTimeSteps; i++ )
513  tstep_nums.push_back( i );
514  }
515 
516  if( !tstep_nums.empty() )
517  {
518  for( unsigned int i = 0; i < vdatas.size(); i++ )
519  {
520  vdatas[i].varTags.resize( tstep_nums.size(), 0 );
521  vdatas[i].varDatas.resize( tstep_nums.size() );
522  // NC reader assumes that non-set variables always have timesteps
523  assert( std::find( vdatas[i].varDims.begin(), vdatas[i].varDims.end(), tDim ) != vdatas[i].varDims.end() );
524  vdatas[i].has_tsteps = true;
525  }
526 
527  for( unsigned int i = 0; i < vsetdatas.size(); i++ )
528  {
529  if( ( std::find( vsetdatas[i].varDims.begin(), vsetdatas[i].varDims.end(), tDim ) !=
530  vsetdatas[i].varDims.end() ) &&
531  ( vsetdatas[i].varName != dimNames[tDim] ) )
532  {
533  // Set variables with timesteps: e.g. xtime(Time) or xtime(Time, StrLen)
534  vsetdatas[i].varTags.resize( tstep_nums.size(), 0 );
535  vsetdatas[i].varDatas.resize( tstep_nums.size() );
536  vsetdatas[i].has_tsteps = true;
537  }
538  else
539  {
540  // Set variables without timesteps: no time dimension, or time itself
541  vsetdatas[i].varTags.resize( 1, 0 );
542  vsetdatas[i].varDatas.resize( 1 );
543  vsetdatas[i].has_tsteps = false;
544  }
545  }
546  }
547 
548  return MB_SUCCESS;
549 }

References _readNC, moab::ReadNC::dimNames, dummyVarNames, moab::ReadNC::VarData::entLoc, moab::ReadNC::ENTLOCSET, ignoredVarNames, MB_SET_ERR, MB_SUCCESS, nTimeSteps, tDim, moab::ReadNC::varInfo, and moab::ReadNC::VarData::varName.

Referenced by moab::ScdNCHelper::read_variables(), and moab::UcdNCHelper::read_variables().

◆ read_variables_to_set()

ErrorCode moab::NCHelper::read_variables_to_set ( std::vector< ReadNC::VarData > &  vdatas,
std::vector< int > &  tstep_nums 
)
protected

Read set variables (common to scd mesh and ucd mesh)

Definition at line 551 of file NCHelper.cpp.

552 {
553  Interface*& mbImpl = _readNC->mbImpl;
554  DebugOutput& dbgOut = _readNC->dbgOut;
555 
556  MB_CHK_SET_ERR( read_variables_to_set_allocate( vdatas, tstep_nums ),
557  "Trouble allocating space to read set variables" );
558 
559  // Finally, read into that space
560  int success;
561  for( unsigned int i = 0; i < vdatas.size(); i++ )
562  {
563  // Note, for set variables without timesteps, loop one time and then break
564  for( unsigned int t = 0; t < tstep_nums.size(); t++ )
565  {
566  void* data = vdatas[i].varDatas[t];
567 
568  // Set variables with timesteps, e.g. xtime(Time) or xtime(Time, StrLen)
569  if( vdatas[i].has_tsteps )
570  {
571  // Set readStart for each timestep along time dimension
572  vdatas[i].readStarts[0] = tstep_nums[t];
573  }
574 
575  switch( vdatas[i].varDataType )
576  {
577  case NC_BYTE:
578  case NC_CHAR:
579  success = NCFUNCAG( _vara_text )( _fileId, vdatas[i].varId, &vdatas[i].readStarts[0],
580  &vdatas[i].readCounts[0], (char*)data );
581  if( success )
582  MB_SET_ERR( MB_FAILURE, "Failed to read byte/char data for variable " << vdatas[i].varName );
583  break;
584  case NC_SHORT:
585  case NC_INT:
586  success = NCFUNCAG( _vara_int )( _fileId, vdatas[i].varId, &vdatas[i].readStarts[0],
587  &vdatas[i].readCounts[0], (int*)data );
588  if( success )
589  MB_SET_ERR( MB_FAILURE, "Failed to read short/int data for variable " << vdatas[i].varName );
590  break;
591  case NC_INT64:
592  success = NCFUNCAG( _vara_long )( _fileId, vdatas[i].varId, &vdatas[i].readStarts[0],
593  &vdatas[i].readCounts[0], (long*)data );
594  if( success )
595  MB_SET_ERR( MB_FAILURE, "Failed to read long data for variable " << vdatas[i].varName );
596  break;
597  case NC_FLOAT:
598  case NC_DOUBLE:
599  success = NCFUNCAG( _vara_double )( _fileId, vdatas[i].varId, &vdatas[i].readStarts[0],
600  &vdatas[i].readCounts[0], (double*)data );
601  if( success )
602  MB_SET_ERR( MB_FAILURE, "Failed to read float/double data for variable " << vdatas[i].varName );
603  break;
604  default:
605  MB_SET_ERR( MB_FAILURE, "Unexpected data type for variable " << vdatas[i].varName );
606  }
607 
608  dbgOut.tprintf( 2, "Setting data for variable %s, time step %d\n", vdatas[i].varName.c_str(),
609  tstep_nums[t] );
610  MB_CHK_SET_ERR( mbImpl->tag_set_by_ptr( vdatas[i].varTags[t], &_fileSet, 1, &data, &vdatas[i].sz ),
611  "Trouble setting tag data for variable " << vdatas[i].varName );
612 
613  // Memory pointed by pointer data can be deleted, as tag_set_by_ptr() has already copied
614  // the tag values
615  switch( vdatas[i].varDataType )
616  {
617  case NC_BYTE:
618  case NC_CHAR:
619  delete[](char*)data;
620  break;
621  case NC_SHORT:
622  case NC_INT:
623  delete[](int*)data;
624  break;
625  case NC_INT64:
626  delete[](long*)data;
627  break;
628  case NC_FLOAT:
629  case NC_DOUBLE:
630  delete[](double*)data;
631  break;
632  default:
633  break;
634  }
635  vdatas[i].varDatas[t] = NULL;
636 
637  // Loop continues only for set variables with timesteps, e.g. xtime(Time) or xtime(Time,
638  // StrLen)
639  if( !vdatas[i].has_tsteps ) break;
640  }
641  }
642 
643  // Debug output, if requested
644  if( 1 == dbgOut.get_verbosity() )
645  {
646  dbgOut.printf( 1, "Read variables: %s", vdatas.begin()->varName.c_str() );
647  for( unsigned int i = 1; i < vdatas.size(); i++ )
648  dbgOut.printf( 1, ", %s ", vdatas[i].varName.c_str() );
649  dbgOut.tprintf( 1, "\n" );
650  }
651 
652  return MB_SUCCESS;
653 }

References _fileId, _fileSet, _readNC, moab::ReadNC::dbgOut, moab::DebugOutput::get_verbosity(), MB_CHK_SET_ERR, MB_SET_ERR, MB_SUCCESS, moab::ReadNC::mbImpl, NCFUNCAG, moab::DebugOutput::printf(), read_variables_to_set_allocate(), moab::Interface::tag_set_by_ptr(), and moab::DebugOutput::tprintf().

Referenced by moab::ScdNCHelper::read_variables(), and moab::UcdNCHelper::read_variables().

◆ read_variables_to_set_allocate()

ErrorCode moab::NCHelper::read_variables_to_set_allocate ( std::vector< ReadNC::VarData > &  vdatas,
std::vector< int > &  tstep_nums 
)
private

Used by read_variables_to_set()

Definition at line 910 of file NCHelper.cpp.

912 {
913  std::vector< int >& dimLens = _readNC->dimLens;
914  DebugOutput& dbgOut = _readNC->dbgOut;
915 
916  for( unsigned int i = 0; i < vdatas.size(); i++ )
917  {
918  // Set up readStarts and readCounts
919  if( vdatas[i].has_tsteps )
920  {
921  // First: time
922  vdatas[i].readStarts.push_back( 0 ); // This value is timestep dependent, will be set later
923  vdatas[i].readCounts.push_back( 1 );
924 
925  // Next: other dimensions
926  for( unsigned int idx = 1; idx != vdatas[i].varDims.size(); idx++ )
927  {
928  vdatas[i].readStarts.push_back( 0 );
929  vdatas[i].readCounts.push_back( dimLens[vdatas[i].varDims[idx]] );
930  }
931  }
932  else
933  {
934  if( vdatas[i].varDims.empty() )
935  {
936  // Scalar variable
937  vdatas[i].readStarts.push_back( 0 );
938  vdatas[i].readCounts.push_back( 1 );
939  }
940  else
941  {
942  for( unsigned int idx = 0; idx != vdatas[i].varDims.size(); idx++ )
943  {
944  vdatas[i].readStarts.push_back( 0 );
945  vdatas[i].readCounts.push_back( dimLens[vdatas[i].varDims[idx]] );
946  }
947  }
948  }
949 
950  // Get variable size
951  vdatas[i].sz = 1;
952  for( std::size_t idx = 0; idx != vdatas[i].readCounts.size(); idx++ )
953  vdatas[i].sz *= vdatas[i].readCounts[idx];
954 
955  // Note, for set variables without timesteps, loop one time and then break
956  for( unsigned int t = 0; t < tstep_nums.size(); t++ )
957  {
958  dbgOut.tprintf( 2, "Reading variable %s, time step %d\n", vdatas[i].varName.c_str(), tstep_nums[t] );
959 
960  if( tstep_nums[t] >= dimLens[tDim] )
961  {
962  MB_SET_ERR( MB_INDEX_OUT_OF_RANGE, "Wrong value for timestep number " << tstep_nums[t] );
963  }
964 
965  // Get the tag to read into
966  if( !vdatas[i].varTags[t] )
967  {
968  MB_CHK_SET_ERR( get_tag_to_set( vdatas[i], tstep_nums[t], vdatas[i].varTags[t] ),
969  "Trouble getting tag to set variable " << vdatas[i].varName );
970  }
971 
972  switch( vdatas[i].varDataType )
973  {
974  case NC_BYTE:
975  case NC_CHAR:
976  vdatas[i].varDatas[t] = new char[vdatas[i].sz];
977  break;
978  case NC_SHORT:
979  case NC_INT:
980  vdatas[i].varDatas[t] = new int[vdatas[i].sz];
981  break;
982  case NC_INT64:
983  vdatas[i].varDatas[t] = new long[vdatas[i].sz];
984  break;
985  case NC_FLOAT:
986  case NC_DOUBLE:
987  vdatas[i].varDatas[t] = new double[vdatas[i].sz];
988  break;
989  default:
990  MB_SET_ERR( MB_FAILURE, "Unexpected data type for variable " << vdatas[i].varName );
991  }
992 
993  // Loop continues only for set variables with timesteps, e.g. xtime(Time) or xtime(Time,
994  // StrLen)
995  if( !vdatas[i].has_tsteps ) break;
996  }
997  }
998 
999  return MB_SUCCESS;
1000 }

References _readNC, moab::ReadNC::dbgOut, moab::ReadNC::dimLens, get_tag_to_set(), MB_CHK_SET_ERR, MB_INDEX_OUT_OF_RANGE, MB_SET_ERR, MB_SUCCESS, tDim, and moab::DebugOutput::tprintf().

Referenced by read_variables_to_set().

◆ update_time_tag_vals()

ErrorCode moab::NCHelper::update_time_tag_vals ( )

Update time tag values if timesteps spread across files.

Definition at line 405 of file NCHelper.cpp.

406 {
407  Interface*& mbImpl = _readNC->mbImpl;
408  std::vector< std::string >& dimNames = _readNC->dimNames;
409 
410  // The time tag might be a dummy one (e.g. 'Time' for MPAS)
411  std::string time_tag_name = dimNames[tDim];
412  if( dummyVarNames.find( time_tag_name ) != dummyVarNames.end() ) return MB_SUCCESS;
413 
414  Tag time_tag = 0;
415  const void* data = NULL;
416  int time_tag_size = 0;
417  MB_CHK_SET_ERR( mbImpl->tag_get_handle( time_tag_name.c_str(), 0, MB_TYPE_DOUBLE, time_tag, MB_TAG_VARLEN ),
418  "Trouble getting tag " << time_tag_name );
419  MB_CHK_SET_ERR( mbImpl->tag_get_by_ptr( time_tag, &_fileSet, 1, &data, &time_tag_size ),
420  "Trouble getting data of tag " << time_tag_name );
421  const double* time_tag_vals = static_cast< const double* >( data );
422 
423  // Merge tVals (read from current file) to existing time tag
424  // Assume that time_tag_vals and tVals are both sorted
425  std::vector< double > merged_time_vals;
426  merged_time_vals.reserve( time_tag_size + nTimeSteps );
427  int i = 0;
428  int j = 0;
429 
430  // Merge time values from time_tag_vals and tVals
431  while( i < time_tag_size && j < nTimeSteps )
432  {
433  if( time_tag_vals[i] < tVals[j] )
434  merged_time_vals.push_back( time_tag_vals[i++] );
435  else
436  merged_time_vals.push_back( tVals[j++] );
437  }
438 
439  // Append remaining time values of time_tag_vals (if any)
440  while( i < time_tag_size )
441  merged_time_vals.push_back( time_tag_vals[i++] );
442 
443  // Append remaining time values of tVals (if any)
444  while( j < nTimeSteps )
445  merged_time_vals.push_back( tVals[j++] );
446 
447  data = &merged_time_vals[0];
448  time_tag_size = merged_time_vals.size();
449  MB_CHK_SET_ERR( mbImpl->tag_set_by_ptr( time_tag, &_fileSet, 1, &data, &time_tag_size ),
450  "Trouble setting data to tag " << time_tag_name );
451 
452  return MB_SUCCESS;
453 }

References _fileSet, _readNC, moab::ReadNC::dimNames, dummyVarNames, MB_CHK_SET_ERR, MB_SUCCESS, MB_TAG_VARLEN, MB_TYPE_DOUBLE, moab::ReadNC::mbImpl, nTimeSteps, moab::Interface::tag_get_by_ptr(), moab::Interface::tag_get_handle(), moab::Interface::tag_set_by_ptr(), tDim, and tVals.

Referenced by moab::ReadNC::load_file().

Member Data Documentation

◆ _fileId

◆ _fileSet

◆ _opts

const FileOptions& moab::NCHelper::_opts
protected

◆ _readNC

ReadNC* moab::NCHelper::_readNC
protected

Allow NCHelper to directly access members of ReadNC.

Definition at line 106 of file NCHelper.hpp.

Referenced by moab::ScdNCHelper::check_existing_mesh(), moab::NCHelperGCRM::check_existing_mesh(), moab::NCHelperHOMME::check_existing_mesh(), moab::NCHelperMPAS::check_existing_mesh(), create_conventional_tags(), create_dummy_variables(), moab::NCHelperMPAS::create_gather_set_cells(), moab::NCHelperGCRM::create_gather_set_edges(), moab::NCHelperMPAS::create_gather_set_edges(), moab::NCHelperGCRM::create_gather_set_vertices(), moab::NCHelperMPAS::create_gather_set_vertices(), moab::NCHelperESMF::create_local_cells(), moab::NCHelperMPAS::create_local_cells(), moab::NCHelperGCRM::create_local_edges(), moab::NCHelperMPAS::create_local_edges(), moab::NCHelperESMF::create_local_vertices(), moab::NCHelperGCRM::create_local_vertices(), moab::NCHelperMPAS::create_local_vertices(), moab::ScdNCHelper::create_mesh(), moab::NCHelperDomain::create_mesh(), moab::NCHelperESMF::create_mesh(), moab::NCHelperGCRM::create_mesh(), moab::NCHelperHOMME::create_mesh(), moab::NCHelperMPAS::create_mesh(), moab::NCHelperScrip::create_mesh(), moab::NCHelperGCRM::create_padded_gather_set_cells(), moab::NCHelperMPAS::create_padded_gather_set_cells(), moab::NCHelperESMF::create_padded_local_cells(), moab::NCHelperGCRM::create_padded_local_cells(), moab::NCHelperMPAS::create_padded_local_cells(), moab::ScdNCHelper::create_quad_coordinate_tag(), get_tag_to_nonset(), get_tag_to_set(), moab::NCHelperDomain::init_mesh_vals(), moab::NCHelperESMF::init_mesh_vals(), moab::NCHelperEuler::init_mesh_vals(), moab::NCHelperFV::init_mesh_vals(), moab::NCHelperGCRM::init_mesh_vals(), moab::NCHelperHOMME::init_mesh_vals(), moab::NCHelperMPAS::init_mesh_vals(), moab::NCHelperScrip::init_mesh_vals(), read_coordinate(), moab::ScdNCHelper::read_scd_variables_to_nonset(), moab::ScdNCHelper::read_scd_variables_to_nonset_allocate(), moab::NCHelperGCRM::read_ucd_variables_to_nonset(), moab::NCHelperHOMME::read_ucd_variables_to_nonset(), moab::NCHelperMPAS::read_ucd_variables_to_nonset(), moab::NCHelperGCRM::read_ucd_variables_to_nonset_allocate(), moab::NCHelperHOMME::read_ucd_variables_to_nonset_allocate(), moab::NCHelperMPAS::read_ucd_variables_to_nonset_allocate(), read_variables_setup(), read_variables_to_set(), read_variables_to_set_allocate(), and update_time_tag_vals().

◆ dummyVarNames

std::set< std::string > moab::NCHelper::dummyVarNames
protected

Dummy variables.

Definition at line 126 of file NCHelper.hpp.

Referenced by create_conventional_tags(), create_dummy_variables(), read_variables_setup(), and update_time_tag_vals().

◆ ignoredVarNames

std::set< std::string > moab::NCHelper::ignoredVarNames
protected

◆ levDim

◆ levVals

◆ nLevels

◆ nTimeSteps

◆ tDim

◆ tVals


The documentation for this class was generated from the following files: