openModeller  Version 1.4.0
om_histogram.cpp
Go to the documentation of this file.
00001 
00006 #include <openmodeller/om.hh>
00007 
00008 #include <openmodeller/env_io/Raster.hh>
00009 
00010 #include <string.h>
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 
00014 #include <stdexcept>
00015 
00016 #include <string>
00017 #include <vector>
00018 #include <map>
00019 using std::string;
00020 using std::vector;
00021 using std::map;
00022 
00023 
00024 /**************************************************************/
00025 /**************************** main ****************************/
00026 
00027 int
00028 main( int argc, char **argv )
00029 {
00030   // Reconfigure the global logger.
00031   Log::instance()->setLevel( Log::Info );
00032   Log::instance()->setPrefix( "" );
00033 
00034   if ( argc < 3 ) {
00035 
00036       Log::instance()->info( "\n%s <map_file> <num_intervals>\n\n", argv[0] );
00037       exit( 1 );
00038   }
00039 
00040   string map_file( argv[1] );
00041   int intervals = atoi( argv[2] );
00042 
00043   vector<int> histogram;
00044   vector<double> min_val;
00045   vector<double> max_val;
00046   map<double,int> values;
00047 
00048   if ( intervals > 0 ) {
00049 
00050     double interval = 1.0/intervals;
00051 
00052     for ( int i = 0; i < intervals; ++i ) {
00053 
00054       histogram.push_back( 0 );
00055       min_val.push_back( i*interval );
00056       max_val.push_back( min_val[i] + interval );
00057     }
00058 
00059     max_val[intervals-1] = max_val[intervals-1] + 0.1;
00060   }
00061 
00062   try {
00063 
00064     Map * map = new Map( RasterFactory::instance().create( map_file ) );
00065 
00066     Coord xmin, ymin, xmax, ymax, xstep, ystep;
00067     map->getRegion( &xmin, &ymin, &xmax, &ymax );
00068     map->getCell( &xstep, &ystep );
00069 
00070     Scalar val[ map->numBand() ];
00071 
00072     for ( Coord y = ymin; y < ymax; y += ystep ) {
00073 
00074       for ( Coord x = xmin; x < xmax; x += xstep ) {
00075 
00076         if ( map->get( x, y, val ) ) {
00077 
00078           if ( intervals > 0 ) {
00079 
00080             for ( int i = 0; i < intervals; ++i ) {
00081 
00082               if ( val[0] >= min_val[i] && val[0] < max_val[i] ) {
00083 
00084                 ++histogram[i];
00085                 break;
00086               }
00087             }
00088           }
00089           else {
00090 
00091             if ( values.find( val[0] ) != values.end() ) {
00092 
00093               ++values[val[0]];
00094             }
00095             else {
00096 
00097               values[val[0]] = 1;
00098             }
00099     }
00100         }
00101       }
00102     }
00103 
00104     Log::instance()->info( "Histogram:\n" );
00105 
00106     if ( intervals > 0 ) {
00107 
00108       for ( int i = 0; i < intervals; ++i ) {
00109 
00110         Log::instance()->info( "[%f-%f): %d\n", min_val[i], max_val[i], histogram[i] );
00111       }
00112     }
00113     else {
00114 
00115       for ( std::map<double, int>::const_iterator it = values.begin(); it != values.end(); it++ ) {
00116 
00117         Log::instance()->info( "%f: %d\n", (*it).first, (*it).second );
00118       }
00119     }
00120 
00121     delete map;
00122   }
00123   catch ( std::exception& e ) {
00124     Log::instance()->info( "Exception occurred\n" );
00125     Log::instance()->info( "Message is %s\n", e.what() );
00126   }
00127   catch ( ... ) {
00128     Log::instance()->info( "Unknown Error occurred\n" );
00129   }
00130 }
00131 
00132 
00133 
00134