00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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 <sed/sed_sedflux.h>
00029
00030 #include "compact.h"
00031
00032
00033 char *help_msg[] =
00034 {
00035 " ",
00036 " compact [options] [parameters] [filein] ",
00037 " compact a sedflux profile. ",
00038 " ",
00039 " Options ",
00040 " -v - be verbose. [off] ",
00041 " -h - print this help message. ",
00042 " -hfile - print a help message on the file formats. ",
00043 " -a - input will be ascii. This is the default. ",
00044 " -b - input will be a binary file from a sedflux dump. ",
00045 " ",
00046 " Parameters ",
00047 " -pdy=value - set the height of sediment cells in the output ASCII file to ",
00048 " be value. The deafult is to not rebin the cells. ",
00049 " ",
00050 " Files ",
00051 " -fsed=file - specify the name of the file containing the sediment ",
00052 " information. [compact.sed] ",
00053 " -fin=file - specify the name of the input file. [stdin] ",
00054 " -fout=file - specify the name of the output file. [stdout] ",
00055 " ",
00056 NULL
00057 };
00058
00059 char *file_msg[] =
00060 {
00061 " ",
00062 " The input file consists of a header line followed by data lines. The header",
00063 " line consists of a single number stating the number of data lines to follow.",
00064 " For each cell in the sediment column that is to be compacted, the input file",
00065 " gives the thickness of a cell and the fractions of each grain type that make",
00066 " up that cell. Each line of the file describes packages of sediment with the",
00067 " first being the top of the sediment column. The lines are divided into at ",
00068 " least two columns. The first is the thickness (in meters) of that sediment ",
00069 " package, and the rest are the fractions of each grain that compose that ",
00070 " cell. The fractions are renormalized and so don't have to add up to one. ",
00071 " ",
00072 NULL
00073 };
00074
00075 Sed_column _scan_sediment_column ( const gchar* file , double dy , GError** error );
00076 gint _print_sediment_column( const gchar* file , Sed_column s , GError** error );
00077
00078 static double dy = 1.0;
00079 static gboolean rebin = FALSE;
00080 static gboolean verbose = FALSE;
00081 static gboolean version = FALSE;
00082 static gchar* infile = NULL;
00083 static gchar* outfile = NULL;
00084 static gchar* sedfile = NULL;
00085 static gboolean diag = FALSE;
00086
00087 static GOptionEntry entries[] =
00088 {
00089 { "cell-height" , 'h' , 0 , G_OPTION_ARG_DOUBLE , &dy , "Cell thickness (m)" , "dz" } ,
00090 { "rebin" , 'r' , 0 , G_OPTION_ARG_NONE , &rebin , "Rebin cells after compaction" , NULL } ,
00091 { "verbose" , 'V' , 0 , G_OPTION_ARG_NONE , &verbose , "Be verbose" , NULL } ,
00092 { "version" , 'v' , 0 , G_OPTION_ARG_NONE , &version , "Print version number" , NULL } ,
00093 { "in-file" , 'i' , 0 , G_OPTION_ARG_FILENAME , &infile , "Input file" , "FILE" } ,
00094 { "out-file" , 'o' , 0 , G_OPTION_ARG_FILENAME , &outfile , "Output file" , "FILE" } ,
00095 { "sed-file" , 's' , 0 , G_OPTION_ARG_FILENAME , &sedfile , "Sediment file" , "FILE" } ,
00096 { "diag" , 0 , 0 , G_OPTION_ARG_NONE , &diag , "Run a diagnostics" , NULL } ,
00097 { NULL }
00098 };
00099
00100 gint
00101 main( gint argc , gchar *argv[] )
00102 {
00103 GError* error = NULL;
00104
00105 eh_init_glib();
00106
00107 {
00108 GOptionContext* context = g_option_context_new( "Run compaction model." );
00109
00110 g_option_context_add_main_entries( context , entries , NULL );
00111
00112 if ( !g_option_context_parse( context , &argc , &argv , &error ) )
00113 eh_error( "Error parsing command line arguments: %s" , error->message );
00114 }
00115
00116 if ( !error && version )
00117 {
00118 eh_fprint_version_info( stdout , COMPACTION_PROGRAM_NAME ,
00119 COMPACTION_MAJOR_VERSION ,
00120 COMPACTION_MINOR_VERSION ,
00121 COMPACTION_MICRO_VERSION );
00122 eh_exit( EXIT_SUCCESS );
00123 }
00124
00125 if ( !error )
00126 {
00127 Sed_column col;
00128 Sed_sediment sed;
00129
00130 sed = sed_sediment_scan( sedfile , &error );
00131 if ( !error )
00132 {
00133 sed_sediment_set_env( sed );
00134 sed_sediment_destroy( sed );
00135 }
00136
00137 if ( !error ) col = _scan_sediment_column( infile , dy , &error );
00138
00139 if ( !error )
00140 {
00141 Sed_diag d = NULL;
00142
00143 if ( diag )
00144 {
00145 d = sed_diag_new_target_column( col );
00146 sed_diag_start( d );
00147 }
00148
00149 eh_debug( "Compact the column." );
00150
00151 compact( col );
00152
00153 if ( rebin ) sed_column_rebin( col );
00154
00155 if ( diag )
00156 {
00157 sed_diag_stop ( d );
00158 sed_diag_fprint ( stderr , d );
00159 sed_diag_destroy( d );
00160 }
00161
00162 eh_debug( "Write the output bulk densities." );
00163 _print_sediment_column( outfile , col , &error );
00164 }
00165
00166 eh_exit_on_error( error , "Error in compaction program" );
00167
00168 sed_column_destroy( col );
00169 sed_sediment_unset_env();
00170 }
00171
00172 return EXIT_SUCCESS;
00173 }
00174
00175 Sed_column
00176 _scan_sediment_column( const gchar* file , double dy , GError** error )
00177 {
00178 Sed_column s = NULL;
00179
00180 eh_return_val_if_fail( error==NULL || *error==NULL , NULL );
00181
00182 eh_require( dy > 0 );
00183
00184 if ( dy>0 )
00185 {
00186 GError* tmp_err = NULL;
00187 double** data = NULL;
00188 gint n_cells;
00189 gint n_grains;
00190
00191 data = eh_dlm_read( file , ";" , &n_cells , &n_grains , &tmp_err );
00192
00193 n_grains -= 1;
00194
00195 if ( !tmp_err && n_grains!=sed_sediment_env_n_types() )
00196 {
00197 g_set_error( &tmp_err , COMPACT_ERROR , COMPACT_ERROR_INPUT_FILE ,
00198 "Number of grain types in sediment file and input file are unequal (%d!=%d)",
00199 sed_sediment_env_n_types() , n_grains );
00200 }
00201 else
00202 {
00203 Sed_cell c = sed_cell_new_sized( n_grains , dy , data[0]+1 );
00204 gssize i;
00205
00206 s = sed_column_new( n_cells );
00207 sed_column_set_z_res( s , dy );
00208
00209 eh_debug( "Fill the sediment column with sediment" );
00210
00211 for ( i=0 ; i<n_cells ; i++ )
00212 {
00213 sed_cell_set_fraction ( c , data[n_cells-1-i]+1 );
00214 sed_column_add_cell ( s , c );
00215 }
00216
00217 for ( i=0 ; i<n_cells ; i++ )
00218 sed_column_resize_cell( s , i , data[n_cells-1-i][0] );
00219
00220 sed_cell_destroy( c );
00221 }
00222
00223 if ( tmp_err )
00224 {
00225 g_propagate_error( error , tmp_err );
00226 s = sed_column_destroy(s);
00227 }
00228
00229 eh_free_2( data );
00230 }
00231
00232 return s;
00233 }
00234
00235 gint
00236 _print_sediment_column( const gchar* file , Sed_column s , GError** error )
00237 {
00238 gint n = 0;
00239
00240 eh_return_val_if_fail( error==NULL || *error==NULL , 0 );
00241
00242 eh_require( s );
00243
00244 if ( s )
00245 {
00246 GError* tmp_err = NULL;
00247 gint n_rows = sed_column_len(s);
00248 gint n_grains = sed_sediment_env_n_types();
00249 double** data = eh_new_2( double , n_rows , n_grains+1 );
00250 Sed_cell c = NULL;
00251 gint i, j;
00252
00253 for ( i=0 ; i<n_rows ; i++ )
00254 {
00255 c = sed_column_nth_cell( s , n_rows-1-i );
00256 data[i][0] = sed_cell_size( c );
00257 for ( j=0 ; j<n_grains ; j++ )
00258 data[i][j+1] = sed_cell_nth_fraction( c , j );
00259 }
00260
00261 n = eh_dlm_print( file , ";" , data , n_rows , n_grains+1 , &tmp_err );
00262
00263 if ( tmp_err )
00264 g_propagate_error( error , tmp_err );
00265
00266 eh_free_2( data );
00267 }
00268
00269 return n;
00270 }
00271