/Users/huttone/Devel/sedflux-new/sedflux/trunk/ew/bio/bio_main.c

Go to the documentation of this file.
00001 //---
00002 //
00003 // This file is part of sedflux.
00004 //
00005 // sedflux is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 2 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // sedflux is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with sedflux; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //
00019 //---
00020 
00021 #include <stdio.h>
00022 
00023 #include <math.h>
00024 #include <string.h>
00025 #include <unistd.h>
00026 
00027 #include <utils/utils.h>
00028 #include "bio.h"
00029 
00030 G_GNUC_INTERNAL double** bio_scan_column_file( const gchar* file , gint* n_grains , gint* n_layers , GError** error );
00031 G_GNUC_INTERNAL gint     bio_print_output    ( const gchar* file , double** col , gint n_grains , gint n_layers , GError** error );
00032 G_GNUC_INTERNAL gint     bio_print_layers    ( const gchar* file , double** col , gint n_layers , GError** error );
00033 
00034 // Command line arguments
00035 static double   layer_dz  = .01;
00036 static gint     n_layers  = 10;
00037 static double   k         = 10.;
00038 static double   r         = 1.;
00039 static double   duration  = 1.;
00040 static gchar*   method_s  = "DIFFUSION";
00041 static gchar*   in_file   = NULL;
00042 static gchar*   out_file  = NULL;
00043 static gboolean version   = FALSE;
00044 static gboolean verbose   = FALSE;
00045 
00046 static GOptionEntry entries[] =
00047 {
00048    { "dz"          , 'h' , 0 , G_OPTION_ARG_DOUBLE   , &layer_dz   , "Layer thickness (cm)"          , "DZ"     } ,
00049    { "n-layers"    , 'n' , 0 , G_OPTION_ARG_INT      , &n_layers   , "Number of layers"              , "N"      } ,
00050    { "diff-coef"   , 'k' , 0 , G_OPTION_ARG_DOUBLE   , &k          , "Diffusion coefficient (cm/y)"  , "K"      } ,
00051    { "remove-rate" , 'r' , 0 , G_OPTION_ARG_DOUBLE   , &r          , "Removal rate (cm/d)"           , "R"      } ,
00052    { "duration"    , 'T' , 0 , G_OPTION_ARG_DOUBLE   , &duration   , "Duration of model run (d)"     , "DAYS"   } ,
00053    { "method"      , 'm' , 0 , G_OPTION_ARG_STRING   , &method_s   , "Bioturbation method"           , "NAME"   } ,
00054    { "in-file"     , 'i' , 0 , G_OPTION_ARG_FILENAME , &in_file    , "Input file"                    , "<file>" } ,
00055    { "out-file"    , 'o' , 0 , G_OPTION_ARG_FILENAME , &out_file   , "Output file"                   , "<file>" } ,
00056    { "verbose"     , 'V' , 0 , G_OPTION_ARG_NONE     , &verbose    , "Verbose"                       , NULL     } ,
00057    { "version"     , 'v' , 0 , G_OPTION_ARG_NONE     , &version    , "Version number"                , NULL     } ,
00058    { NULL }
00059 };
00060 
00061 gint
00062 main( int argc, char *argv[])
00063 {
00064    GOptionContext* context;
00065    GError*         error   = NULL;
00066 
00067    eh_init_glib();
00068 
00069    context = g_option_context_new( "Run bioturbation model." );
00070 
00071    g_option_context_add_main_entries( context , entries , NULL );
00072 
00073    if ( !g_option_context_parse( context , &argc , &argv , &error ) )
00074       eh_error( "Error parsing command line arguments: %s" , error->message );
00075 
00076    if (    g_ascii_strcasecmp( method_s , "DIFFUSION" )!=0 
00077         && g_ascii_strcasecmp( method_s , "CONVEYOR"  )!=0 )
00078       eh_error( "Error parsing command line arguments: method must be either 'diffusion' or 'conveyor'." );
00079 
00080    if ( version )
00081    {
00082       eh_fprint_version_info( stdout , BIO_PROGRAM_NAME , BIO_MAJOR_VERSION , BIO_MINOR_VERSION , BIO_MICRO_VERSION );
00083       eh_exit( 0 );
00084    }
00085    else
00086    {
00087       gint n_grains;
00088       gint method;
00089 //      double** col = bio_scan_column_file( in_file , &n_grains , &n_layers , &error );
00090 
00091       if      ( g_ascii_strcasecmp( method_s , "DIFFUSION" )==0 ) method = 1;
00092       else if ( g_ascii_strcasecmp( method_s , "CONVEYOR"  )==0 ) method = 2;
00093 
00094       if ( verbose )
00095       {
00096          eh_info( "Diffusion coefficient (cm^2/y) : %f" , k                          );
00097          eh_info( "Diffusion depth (cm)           : %f" , n_layers*layer_dz          );
00098          eh_info( "Duration (d)                   : %f" , duration                   );
00099          eh_info( "Method                         : %s" , g_ascii_strup(method_s,-1) );
00100       }
00101 
00102       k        *= 1e-4/S_SECONDS_PER_YEAR;
00103       r        *= 1e-2/S_SECONDS_PER_DAY;
00104       layer_dz *= 1e-2;
00105       duration *= S_SECONDS_PER_DAY;
00106 
00107       if ( !error )
00108       {
00109          double** layers = NULL;
00110          double*  t      = eh_new( double , n_layers );
00111 
00112          eh_dbl_array_set( t , n_layers , layer_dz );
00113 
00114          if ( method==1 ) layers = bio_diffuse_layers ( t , n_layers , layer_dz , k , duration );
00115          else             layers = bio_conveyor_layers( t , n_layers , layer_dz , r , duration );
00116 
00117          bio_print_layers( out_file , layers , n_layers , &error );
00118 
00119          g_strfreev( layers );
00120          eh_free_2 ( t      );
00121       }
00122 
00123       if ( error ) eh_error( "bio: %s" , error->message );
00124    }
00125 
00126    return 0;
00127 }
00128 
00129 double**
00130 bio_scan_column_file( const gchar* file , gint* n_grains , gint* n_layers , GError** error )
00131 {
00132    double** col = NULL;
00133 
00134    eh_return_val_if_fail( error==NULL || *error==NULL , NULL );
00135 
00136    if ( file )
00137    {
00138       col = eh_dlm_read_swap( file , ";," , n_grains , n_layers , error );
00139    }
00140 
00141    return col;
00142 }
00143 
00144 gint
00145 bio_print_layers( const gchar* file , double** col , gint n_layers , GError** error )
00146 {
00147    gint n = 0;
00148 
00149    eh_return_val_if_fail( error==NULL || *error==NULL , 0 );
00150 
00151    if ( col && col[0] )
00152    {
00153       gint len = g_strv_length( col );
00154       n = eh_dlm_print( file , ";" , col , len , n_layers , error );
00155    }
00156 
00157    return n;
00158 }
00159 
00160 gint
00161 bio_print_output( const gchar* file , double** col , gint n_grains , gint n_layers , GError** error )
00162 {
00163    gint n = 0;
00164 
00165    eh_return_val_if_fail( error==NULL || *error==NULL , 0 );
00166 
00167    if ( col && col[0] )
00168    {
00169       n = eh_dlm_print_swap( file , ";" , col , n_grains , n_layers , error );
00170    }
00171 
00172    return n;
00173 }
00174 

Generated on Fri Jan 4 18:04:13 2008 for sedflux by  doxygen 1.5.2