Mesh Oriented datABase  (version 5.4.1)
Array-based unstructured mesh datastructure
addPCdata.cpp
Go to the documentation of this file.
00001 /** @example addPCdata.cpp  Add point cloud data
00002  * this tool will take an existing h5m fine atm mesh file and add data from an h5m  type file with
00003  * point cloud mesh will support mainly showing the data with Visit
00004  *
00005  * example of usage:
00006  * ./mbaddpcdata -i wholeFineATM.h5m -s wholeLND_proj01.h5m -o atm_a2l.h5m -v a2lTbot_proj
00007  *
00008  * it should work also for pentagon file style data
00009  * ./mbaddpcdata -i <MOABsource>/MeshFiles/unittest/penta3d.h5m -s wholeLND_proj01.h5m -o
00010  * atm_a2l.h5m -v a2lTbot_proj -p 1
00011  *
00012  * Basically, will output a new h5m file (atm_a2l.h5m), which has an extra tag, corresponding to the
00013  * variable a2lTbot_proj from the file wholeLND_proj01.h5m; matching is based on the global ids
00014  * between what we think is the order on the original file (wholeFineATM.h5m) and the order of
00015  * surfdata_ne11np4_simyr1850_c160614.nc
00016  *
00017  *  file  wholeFineATM.h5m is obtained from a coupled run in e3sm, with the ne 11, np 4,
00018  *
00019  */
00020 #include "moab/ProgOptions.hpp"
00021 #include "moab/Core.hpp"
00022 
00023 #include <cmath>
00024 #include <sstream>
00025 
00026 using namespace moab;
00027 
00028 int main( int argc, char* argv[] )
00029 {
00030 
00031     ProgOptions opts;
00032 
00033     std::string inputfile, outfile( "out.h5m" ), sourcefile, variable_name;
00034     int dual_mesh = 0;
00035     opts.addOpt< std::string >( "input,i", "input mesh filename", &inputfile );
00036     opts.addOpt< std::string >( "source,s", "h5m file aligned with the mesh input file", &sourcefile );
00037     opts.addOpt< std::string >( "output,o", "output mesh filename", &outfile );
00038 
00039     opts.addOpt< std::string >( "var,v", "variable to extract and add to output file", &variable_name );
00040     opts.addOpt< int >( "pentagon,p", "switch for dual mesh ", &dual_mesh );
00041 
00042     opts.parseCommandLine( argc, argv );
00043 
00044     std::cout << "input file: " << inputfile << "\n";
00045     std::cout << "source file: " << sourcefile << "\n";
00046     std::cout << "variable to copy: " << variable_name << "\n";
00047     std::cout << "output file: " << outfile << "\n";
00048 
00049     if( inputfile.empty() )
00050     {
00051         opts.printHelp();
00052         return 0;
00053     }
00054     ErrorCode rval;
00055     Core* mb = new Core();
00056 
00057     rval = mb->load_file( inputfile.c_str() );MB_CHK_SET_ERR( rval, "can't load input file" );
00058 
00059     Core* mb2 = new Core();
00060     rval      = mb2->load_file( sourcefile.c_str() );MB_CHK_SET_ERR( rval, "can't load source file" );
00061 
00062     Tag sourceTag;
00063     rval = mb2->tag_get_handle( variable_name.c_str(), sourceTag );MB_CHK_SET_ERR( rval, "can't get tag from file" );
00064 
00065     int sizeTag = 0;
00066     rval        = mb2->tag_get_length( sourceTag, sizeTag );MB_CHK_SET_ERR( rval, "can't get size of tag " );
00067 
00068     DataType type;
00069     rval = mb2->tag_get_data_type( sourceTag, type );MB_CHK_SET_ERR( rval, "can't get type of tag " );
00070 
00071     int sizeInBytes = 0;
00072     rval            = mb2->tag_get_bytes( sourceTag, sizeInBytes );MB_CHK_SET_ERR( rval, "can't get size in bytes of tag " );
00073 
00074     Tag newTag;
00075     /*
00076      *  ErrorCode tag_get_handle( const char* name,
00077                                       int size,
00078                                       DataType type,
00079                                       Tag& tag_handle,
00080                                       unsigned flags = 0,
00081                                       const void* default_value = 0 )
00082      */
00083     void* defVal;
00084     int defInt       = -100000;
00085     double defDouble = -1.e10;
00086     if( type == MB_TYPE_INTEGER )
00087     {
00088         defVal = (void*)( &defInt );
00089     }
00090     else if( type == MB_TYPE_DOUBLE )
00091     {
00092         defVal = (void*)( &defDouble );
00093     }
00094 
00095     rval = mb->tag_get_handle( variable_name.c_str(), sizeTag, type, newTag, MB_TAG_DENSE | MB_TAG_CREAT, defVal );MB_CHK_SET_ERR( rval, "can't create new tag " );
00096 
00097     // get vertices on ini mesh; get global id on ini mesh
00098     // get global id on source mesh
00099     Tag gid;
00100     Tag gid2;
00101     rval = mb->tag_get_handle( "GLOBAL_ID", gid );MB_CHK_SET_ERR( rval, "can't get GLOBAL_ID tag on ini mesh " );
00102     rval = mb2->tag_get_handle( "GLOBAL_ID", gid2 );MB_CHK_SET_ERR( rval, "can't get GLOBAL_ID tag on source mesh " );
00103 
00104     // get vertices on ini mesh; build
00105     Range iniVerts;
00106     rval = mb->get_entities_by_dimension( 0, 0, iniVerts );MB_CHK_SET_ERR( rval, "can't get verts on initial mesh " );
00107     if( 0 != dual_mesh )
00108     {
00109         // verts will be polygons
00110         rval = mb->get_entities_by_dimension( 0, 2, iniVerts );MB_CHK_SET_ERR( rval, "can't get polygons on initial mesh " );
00111     }
00112     std::vector< int > gids;
00113     gids.resize( iniVerts.size() );
00114     rval = mb->tag_get_data( gid, iniVerts, &( gids[0] ) );MB_CHK_SET_ERR( rval, "can't get gid on initial verts " );
00115     // build now the map
00116     std::map< int, EntityHandle > fromGidToEh;
00117     int i = 0;
00118     for( Range::iterator vit = iniVerts.begin(); vit != iniVerts.end(); vit++, i++ )
00119     {
00120         fromGidToEh[gids[i]] = *vit;
00121     }
00122     // now get the source verts, and tags, and set it on new mesh
00123 
00124     char* valTag = new char[sizeInBytes];
00125 
00126     std::cout << " size of tag in bytes:" << sizeInBytes << "\n";
00127     Range sourceVerts;
00128     rval = mb2->get_entities_by_dimension( 0, 0, sourceVerts );MB_CHK_SET_ERR( rval, "can't get verts on source mesh " );
00129     for( Range::iterator sit = sourceVerts.begin(); sit != sourceVerts.end(); sit++ )
00130     {
00131         int globalId              = 0;
00132         EntityHandle sourceHandle = *sit;
00133         rval                      = mb2->tag_get_data( gid2, &sourceHandle, 1, &globalId );MB_CHK_SET_ERR( rval, "can't get id on source mesh " );
00134         // iniVert could be a polygon, actually
00135         EntityHandle iniVert = fromGidToEh[globalId];
00136         // find the value of source tag
00137         rval = mb2->tag_get_data( sourceTag, &sourceHandle, 1, (void*)valTag );MB_CHK_SET_ERR( rval, "can't get value on source tag " );
00138 
00139         rval = mb->tag_set_data( newTag, &iniVert, 1, (void*)valTag );MB_CHK_SET_ERR( rval, "can't set value on initial mesh, new tag " );
00140     }
00141 
00142     // save file
00143     rval = mb->write_file( outfile.c_str() );MB_CHK_SET_ERR( rval, "can't write file" );
00144 
00145     delete[] valTag;
00146     delete mb;
00147     delete mb2;
00148 
00149     return 0;
00150 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines