Mesh Oriented datABase  (version 5.4.1)
Array-based unstructured mesh datastructure
VisTags.cpp
Go to the documentation of this file.
00001 /** @example VisTags.cpp \n
00002  * \brief tool for visualizing multi level tags  \n
00003  * <b>To run</b>: VisTags  <inp_file>  <outfile> -O <read_opts> -t <tags> -l <levels>  -d <dim> \n
00004  *
00005  * In this example, it is shown how to create some simple tags for those tags that come from
00006  *  climate data, multiple levels.
00007  *  you can read directly nc data, or *.h5m file that will have the tag with multi levels
00008  *   output will be a vtk file with dense tags of form tag_name_<level>
00009  * the tag name might contain a time index too, like T0 or U0
00010  * <tag> is a list of tags, separated by commas, no spaces
00011  * <levels> is a list of levels, separated by commas, no spaces
00012  *  dimension of entities with the tags will be specified with -d (default 2)
00013  *
00014  * an example of use
00015  *
00016  * VisTags gcrm_r3.nc out.vtk -O VARIABLE=u -t u0,u1 -l 0,1,2 -d 2
00017  * (we knew that it had variable u in the file, that it had 256 levels, that there are 2 time
00018  *  steps, etc)
00019  *
00020  * or
00021  *  VisTags gcrm_r3.nc out.vtk  -t u0 -l 0,1,2 -d 2
00022  *  (it will read all variables, but we need to know that u0 will be created as a tag)
00023  *
00024  *  the out.vtk file will contain u0_0, u0_1, as simple dense double tags
00025  */
00026 
00027 #include <iostream>
00028 #include <vector>
00029 #include <sstream>
00030 #include <string>
00031 
00032 // Include header for MOAB instance and tag conventions
00033 #include "moab/Core.hpp"
00034 #include "MBTagConventions.hpp"
00035 #include "moab/FileOptions.hpp"
00036 #ifdef MOAB_HAVE_MPI
00037 #include "moab/ParallelComm.hpp"
00038 #endif
00039 using namespace moab;
00040 using namespace std;
00041 
00042 int main( int argc, char** argv )
00043 {
00044 #ifdef MOAB_HAVE_NETCDF
00045 
00046 #ifdef MOAB_HAVE_MPI
00047     MPI_Init( &argc, &argv );
00048 #endif
00049 
00050     ErrorCode rval;
00051     string file_input, file_output;
00052     string read_opts, tags;  // Tags to write, separated by commas; it is the name of the tag
00053     if( argc < 2 )
00054     {
00055         file_input  = string( MESH_DIR ) + string( "/io/gcrm_r3.nc" );
00056         file_output = "VisTagsOut.vtk";
00057     }
00058     else
00059     {
00060         file_input  = argv[1];
00061         file_output = argv[2];
00062     }
00063     read_opts = "";
00064     tags      = "";
00065 
00066     // Instantiate
00067     Interface* mb = new( std::nothrow ) Core;
00068     if( NULL == mb ) return 1;
00069 
00070     int dimension = 2;
00071     // In MOAB, it may have index after reading (T0, T1, etc)
00072     char* levels = NULL;  // Levels, separated by commas, no spaces (like 0, 1, 19)
00073     if( argc > 3 )
00074     {
00075         int index = 3;
00076         while( index < argc )
00077         {
00078             if( !strcmp( argv[index], "-O" ) )  // This is for reading options, optional
00079                 read_opts = argv[++index];
00080             if( !strcmp( argv[index], "-t" ) ) tags = argv[++index];
00081             if( !strcmp( argv[index], "-l" ) ) levels = argv[++index];
00082             if( !strcmp( argv[index], "-d" ) ) dimension = atoi( argv[++index] );
00083             index++;
00084         }
00085     }
00086 
00087     ostringstream opts;
00088     opts << ";;TAGS=" << tags << ";LEVELS=" << levels << "\0";
00089     FileOptions fo( opts.str().c_str() );
00090 
00091     vector< string > tagsNames;
00092     vector< int > levelsArray;
00093     fo.get_strs_option( "TAGS", tagsNames );
00094     fo.get_ints_option( "LEVELS", levelsArray );
00095 
00096     // Load the input file with the specified options
00097     rval = mb->load_file( file_input.c_str(), 0, read_opts.c_str() );MB_CHK_SET_ERR( rval, "not loading file" );
00098 
00099     Range ents;
00100     rval = mb->get_entities_by_dimension( 0, dimension, ents );MB_CHK_SET_ERR( rval, "not getting ents" );
00101 
00102     // Now create double tags for entities of dimension
00103     for( size_t i = 0; i < tagsNames.size(); i++ )
00104     {
00105         string tagName = tagsNames[i];
00106         Tag tagh;
00107         rval = mb->tag_get_handle( tagName.c_str(), tagh );
00108         if( MB_SUCCESS != rval )
00109         {
00110             cout << "not getting tag " << tagName.c_str() << "\n";
00111             continue;
00112         }
00113 
00114         int len = 0;
00115         rval    = mb->tag_get_length( tagh, len );
00116         if( MB_SUCCESS != rval )
00117         {
00118             cout << "not getting tag len " << tagName.c_str() << "\n";
00119             continue;
00120         }
00121 
00122         DataType type;
00123         rval = mb->tag_get_data_type( tagh, type );
00124         if( MB_SUCCESS != rval )
00125         {
00126             cout << "not getting tag type " << tagName.c_str() << "\n";
00127             continue;
00128         }
00129 
00130         int count;
00131         void* dataptr;  // Assume double tags, for simplicity
00132         rval = mb->tag_iterate( tagh, ents.begin(), ents.end(), count, dataptr );
00133         if( MB_SUCCESS != rval || count != (int)ents.size() )
00134         {
00135             cout << "not getting tag iterate right " << tagName.c_str() << "\n";
00136             continue;
00137         }
00138 
00139         // Now create a new tag, with a new name, concatenated, and copy data there , for each level
00140         for( size_t j = 0; j < levelsArray.size(); j++ )
00141         {
00142             int level = levelsArray[j];
00143             if( level >= len )
00144             {
00145                 cout << "level too big at " << level << "\n";
00146                 continue;
00147             }
00148 
00149             ostringstream newTagName;
00150             newTagName << tagName << "_" << level;
00151             Tag newTagh;
00152             rval = mb->tag_get_handle( newTagName.str().c_str(), 1, type, newTagh, MB_TAG_DENSE | MB_TAG_CREAT );
00153             if( MB_SUCCESS != rval )
00154             {
00155                 cout << "not getting new tag " << newTagName.str() << "\n";
00156                 continue;
00157             }
00158 
00159             void* newDataPtr;
00160             rval = mb->tag_iterate( newTagh, ents.begin(), ents.end(), count, newDataPtr );
00161             if( MB_SUCCESS != rval || count != (int)ents.size() )
00162             {
00163                 cout << "not getting new tag iterate " << newTagName.str() << "\n";
00164                 continue;
00165             }
00166 
00167             if( MB_TYPE_DOUBLE == type )
00168             {
00169                 double* ptrD    = (double*)newDataPtr;
00170                 double* oldData = (double*)dataptr;
00171                 for( int k = 0; k < count; k++, ptrD++ )
00172                     *ptrD = oldData[level + count * k];
00173             }
00174         }  // for (size_t j = 0; j < levelsArray.size(); j++)
00175 
00176         mb->tag_delete( tagh );  // No need for the tag anymore, write it to the new file
00177     }                            // for (size_t i = 0; i < tagsNames.size(); i++)
00178 
00179     rval = mb->write_file( file_output.c_str() );MB_CHK_SET_ERR( rval, "Can't write file " << file_output );
00180     cout << "Successfully wrote file " << file_output << "\n";
00181 
00182     delete mb;
00183 
00184 #ifdef MOAB_HAVE_MPI
00185     MPI_Finalize();
00186 #endif
00187 #else
00188     std::cout << " configure with netcdf for this example to work\n";
00189 #endif
00190     return 0;
00191 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines