35 #define GET_DIM( ncdim, name, val ) \
37 int gdfail = nc_inq_dimid( ncFile, name, &( ncdim ) ); \
38 if( NC_NOERR == gdfail ) \
41 gdfail = nc_inq_dimlen( ncFile, ncdim, &tmp_val ); \
42 if( NC_NOERR != gdfail ) \
44 MB_SET_ERR( MB_FAILURE, "addncdata:: Couldn't get dimension length" ); \
53 #define GET_VAR( name, id, dims ) \
56 int gvfail = nc_inq_varid( ncFile, name, &( id ) ); \
57 if( NC_NOERR == gvfail ) \
60 gvfail = nc_inq_varndims( ncFile, id, &ndims ); \
61 if( NC_NOERR == gvfail ) \
63 ( dims ).resize( ndims ); \
64 gvfail = nc_inq_vardimid( ncFile, id, &( dims )[0] ); \
65 if( NC_NOERR != gvfail ) \
67 MB_SET_ERR( MB_FAILURE, "addncdata:: Couldn't get variable dimension IDs" ); \
73 #define GET_1D_INT_VAR( name, id, vals ) \
75 GET_VAR( name, id, vals ); \
79 int ivfail = nc_inq_dimlen( ncFile, ( vals )[0], &ntmp ); \
80 if( NC_NOERR != ivfail ) \
82 MB_SET_ERR( MB_FAILURE, "addncdata:: Couldn't get dimension length" ); \
84 ( vals ).resize( ntmp ); \
86 ivfail = nc_get_vara_int( ncFile, id, &ntmp1, &ntmp, &( vals )[0] ); \
87 if( NC_NOERR != ivfail ) \
89 MB_SET_ERR( MB_FAILURE, "addncdata:: Problem getting variable " << ( name ) ); \
94 #define GET_1D_DBL_VAR( name, id, vals ) \
96 std::vector< int > dum_dims; \
97 GET_VAR( name, id, dum_dims ); \
101 int dvfail = nc_inq_dimlen( ncFile, dum_dims[0], &ntmp ); \
102 if( NC_NOERR != dvfail ) \
104 MB_SET_ERR( MB_FAILURE, "addncdata:: Couldn't get dimension length" ); \
106 ( vals ).resize( ntmp ); \
108 dvfail = nc_get_vara_double( ncFile, id, &ntmp1, &ntmp, &( vals )[0] ); \
109 if( NC_NOERR != dvfail ) \
111 MB_SET_ERR( MB_FAILURE, "addncdata:: Problem getting variable " << ( name ) ); \
116 #define GET_1D_FLT_VAR( name, id, vals ) \
118 std::vector< int > dum_dims; \
119 GET_VAR( name, id, dum_dims ); \
123 int dvfail = nc_inq_dimlen( ncFile, dum_dims[0], &ntmp ); \
124 if( NC_NOERR != dvfail ) \
126 MB_SET_ERR( MB_FAILURE, "addncdata:: Couldn't get dimension length" ); \
128 ( vals ).resize( ntmp ); \
130 dvfail = nc_get_vara_float( ncFile, id, &ntmp1, &ntmp, &( vals )[0] ); \
131 if( NC_NOERR != dvfail ) \
133 MB_SET_ERR( MB_FAILURE, "addncdata:: Problem getting variable " << ( name ) ); \
138 int main(
int argc,
char* argv[] )
143 std::string inputfile, outfile(
"out.h5m" ), netcdfFile, variable_name, sefile_name;
145 opts.
addOpt< std::string >(
"input,i",
"input mesh filename", &inputfile );
146 opts.
addOpt< std::string >(
"netcdfFile,n",
"netcdf file aligned with the mesh input file", &netcdfFile );
147 opts.
addOpt< std::string >(
"output,o",
"output mesh filename", &outfile );
149 opts.
addOpt< std::string >(
"var,v",
"variable to extract and add to output file", &variable_name );
151 opts.
addOpt< std::string >(
"sefile,s",
"spectral elements file (coarse SE mesh)", &sefile_name );
158 std::cout <<
" opened " << inputfile <<
" with initial h5m data.\n";
170 std::cout <<
" it has " << nodes.
size() <<
" vertices " << edges.
size() <<
" edges " << cells.
size() <<
" cells\n";
173 std::map< int, EntityHandle > vGidHandle;
174 std::map< int, EntityHandle > eGidHandle;
175 std::map< int, EntityHandle > cGidHandle;
176 std::vector< int > gids;
179 gids.resize( nodes.
size() );
184 vGidHandle[gids[i++]] = *vit;
187 gids.resize( edges.
size() );
192 eGidHandle[gids[i++]] = *vit;
195 gids.resize( cells.
size() );
200 cGidHandle[gids[i++]] = *vit;
204 int fail = nc_open( netcdfFile.c_str(), 0, &
ncFile );
205 if( NC_NOWRITE !=
fail )
210 std::cout <<
" opened " << netcdfFile <<
" with new data \n";
211 std::vector< int > dims;
215 char recname[NC_MAX_NAME + 1];
217 std::cout <<
" looking for variable " << variable_name <<
"\n";
218 GET_VAR( variable_name.c_str(), nc_var, dims );
219 std::cout <<
" it has " << dims.size() <<
" dimensions\n";
222 bool vertex_data =
false;
223 bool cell_data =
false;
224 for(
size_t j = 0; j < dims.size(); j++ )
226 fail = nc_inq_dim(
ncFile, dims[j], recname, &recs );
227 if( NC_NOERR !=
fail )
MB_SET_ERR( MB_FAILURE,
"addncdata:: Couldn't get dimension" );
228 std::string name_dim( recname );
229 std::cout <<
" dimension index " << j <<
" in file: " << dims[j] <<
" name: " << name_dim <<
" recs:" << recs
231 if( recs == nodes.
size() )
236 if( recs == cells.
size() )
243 int otherDim = 1 - dimIndex;
245 std::vector< int > evals;
246 std::vector< double > dvals;
250 fail = nc_inq_vartype(
ncFile, nc_var, &dataType );
251 if( NC_NOERR !=
fail )
MB_SET_ERR( MB_FAILURE,
"addncdata:: Couldn't get variable type" );
253 bool float_var =
false;
254 if( NC_INT == dataType )
256 else if( NC_DOUBLE != dataType && NC_FLOAT != dataType )
259 if( NC_FLOAT == dataType ) float_var =
true;
261 bool use_time =
false;
263 std::vector< float > times;
264 fail = nc_inq_varid(
ncFile,
"time", &time_id );
265 if( NC_NOERR ==
fail )
273 if( ( dims.size() >= 1 && dims.size() <= 2 ) && ( vertex_data || cell_data ) )
276 if( dims.size() == 2 )
278 fail = nc_inq_dim(
ncFile, dims[otherDim], recname, &size_tag );
279 if( NC_NOERR !=
fail )
MB_SET_ERR( MB_FAILURE,
"addncdata:: Couldn't get dimension" );
286 if( NC_INT == dataType )
288 std::vector< int > vals;
289 if( 1 == dims.size() )
294 for(
size_t k = 0; k < vals.size(); k++ )
302 for(
size_t k = 0; k < vals.size(); k++ )
312 size_t start[2] = { 0, 0 }, count[2] = { 1, 1 };
313 count[dimIndex] = recs;
314 count[otherDim] = size_tag;
315 vals.resize( recs * size_tag );
316 fail = nc_get_vara_int(
ncFile, nc_var, start, count, &vals[0] );
317 evals.resize( size_tag );
321 for(
size_t k = 0; k < recs; k++ )
324 size_t start_in_vals = k * size_tag, stride = 1;
325 for(
size_t j = 0; j < size_tag; j++ )
326 evals[j] = vals[start_in_vals + j * stride];
332 for(
size_t k = 0; k < recs; k++ )
335 size_t start_in_vals = k * size_tag, stride = 1;
336 for(
size_t j = 0; j < size_tag; j++ )
337 evals[j] = vals[start_in_vals + j * stride];
345 std::vector< double > vals;
346 if( 1 == dims.size() )
351 for(
size_t k = 0; k < vals.size(); k++ )
359 for(
size_t k = 0; k < vals.size(); k++ )
369 size_t start[2] = { 0, 0 }, count[2] = { 1, 1 };
370 count[dimIndex] = recs;
371 count[otherDim] = size_tag;
372 vals.resize( recs * size_tag );
373 fail = nc_get_vara_double(
ncFile, nc_var, start, count, &vals[0] );
374 dvals.resize( size_tag );
378 for(
size_t k = 0; k < recs; k++ )
381 size_t start_in_vals = k * size_tag, stride = 1;
382 for(
size_t j = 0; j < size_tag; j++ )
383 dvals[j] = vals[start_in_vals + j * stride];
389 for(
size_t k = 0; k < recs; k++ )
392 size_t start_in_vals = k * size_tag, stride = 1;
393 for(
size_t j = 0; j < size_tag; j++ )
394 dvals[j] = vals[start_in_vals + j * stride];
401 else if( ( dims.size() == 3 ) && vertex_data && dimIndex == 2 && mbtype ==
MB_TYPE_DOUBLE &&
406 char recname0[NC_MAX_NAME + 1], recname1[NC_MAX_NAME + 1];
407 fail = nc_inq_dim(
ncFile, dims[0], recname0, &dim0 );
408 if( NC_NOERR !=
fail )
MB_SET_ERR( MB_FAILURE,
"addncdata:: Couldn't get dimension" );
409 fail = nc_inq_dim(
ncFile, dims[1], recname1, &dim1 );
410 if( NC_NOERR !=
fail )
MB_SET_ERR( MB_FAILURE,
"addncdata:: Couldn't get dimension" );
411 std::string name0( recname0 );
412 std::string name1( recname1 );
413 std::string timestr(
"time" );
414 size_t start[3] = { 0, 0, 0 };
415 size_t count[3] = { 1, 0, 0 };
417 count[2] = nodes.
size();
419 if( name0.compare(
"time" ) == 0 )
422 std::vector< double > dvalues( dim1 * count[2] );
423 std::vector< float > fvalues( dim1 * count[2] );
424 std::vector< double > transp( dim1 * count[2] );
426 for(
size_t k = 0; k < dim0; k++ )
429 std::stringstream tag_name;
430 tag_name << variable_name <<
"_t" << times[k];
431 std::vector< double > defvals( dim1, 0. );
432 rval =
mb->
tag_get_handle( tag_name.str().c_str(), (
int)dim1, mbtype, newTag,
438 fail = nc_get_vara_float(
ncFile, nc_var, start, count, &fvalues[0] );
440 for(
size_t ii = 0; ii < dim1; ii++ )
441 for(
size_t j = 0; j < count[2]; j++ )
443 transp[j * dim1 + ii] = fvalues[ii * count[2] + j];
448 fail = nc_get_vara_double(
ncFile, nc_var, start, count, &dvalues[0] );
450 for(
size_t ii = 0; ii < dim1; ii++ )
451 for(
size_t j = 0; j < count[2]; j++ )
453 transp[j * dim1 + ii] = dvalues[ii * count[2] + j];
456 for(
size_t ii = 0; ii < nodes.
size(); ii++ )
465 std::cout <<
" wrote file " << outfile <<
"\n";
468 if( !sefile_name.empty() )
473 std::cout <<
" loaded spectral file " << sefile_name <<
"\n";
479 int np = (int)sqrt( 1.0 * sizeTag );
480 std::cout <<
" size of tag: " << sizeTag <<
" np = " << np <<
"\n";
481 std::vector< int > gdofs;
484 gdofs.resize( cells2.
size() * sizeTag );
488 std::vector< double > dfield;
489 dfield.resize( sizeTag, 0.0 );
500 std::vector< double > oldData;
501 oldData.resize( dataTagLen * nodes.
size() );
509 for(
int k = 0; k < sizeTag; k++ )
511 int gdof = gdofs[i1 + k];
513 int index = nodes.
index( node );
515 double val = oldData[index * dataTagLen + dataTagLen - 1];
524 std::cout <<
" wrote file atm2.h5m \n";