/Users/huttone/Devel/sedflux-new/sedflux/trunk/ew/sedutils/read_hydro.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 #include <utils/utils.h>
00023 #include <sed/sed_sedflux.h>
00024 
00025 static char *help_msg[] = {
00026 " read_hydro - read a hydrotrend output file.          ",
00027 "                                                      ",
00028 " options:                                             ",
00029 "  nrecs : the number of records to read  [10]         ",
00030 "  len   : the length of the buffer [365]              ",
00031 "  nsig  : the number of significant events [10]       ",
00032 "  start : indicate the record to start on [0]         ",
00033 "  prop  : indicate the property to read [q]           ",
00034 "        : q   = water flux                            ",
00035 "        : qs  = suspended sediment flux               ",
00036 "        : v   = river velocity                        ",
00037 "        : w   = river width                           ",
00038 "        : d   = river depth                           ",
00039 "        : bed = bedload flux                          ",
00040 "  v     : be verbose [no]                             ",
00041 "  buf   : use a buffer to process the input file [no] ",
00042 "  in    : name of the input file [stdin]              ",
00043 "                                                      ",
00044 NULL };
00045 
00046 int main(int argc,char *argv[])
00047 {
00048    char *prop_vals[] = { "q" , "qs" , "v" , "w" , "d" , "bed" , NULL };
00049    int j;
00050    int n_recs, buf_len, n_sig;
00051    int start;
00052    double time, total_time;
00053    int prop;
00054    gboolean verbose, use_buf;
00055    char *infile;
00056    Eh_args *args;
00057    Hydro_get_val_func get_val;
00058    Sed_hydro_file fp;
00059    Sed_hydro rec;
00060    GPtrArray *rec_array;
00061 
00062    args = eh_opts_init(argc,argv);
00063    if ( eh_check_opts( args , NULL , NULL , help_msg )!=0 )
00064       eh_exit(-1);
00065 
00066    total_time = eh_get_opt_dbl ( args , "dt" , 365.    );
00067    n_recs  = eh_get_opt_int ( args , "nrecs" , 10    );
00068    buf_len = eh_get_opt_int ( args , "len"   , 365   );
00069    n_sig   = eh_get_opt_int ( args , "nsig"  , 5     );
00070    start   = eh_get_opt_int ( args , "start" , 0     );
00071    verbose = eh_get_opt_bool( args , "v"     , FALSE );
00072    use_buf = eh_get_opt_bool( args , "buf"   , FALSE );
00073    infile  = eh_get_opt_str ( args , "in"    , "-"   );
00074    prop    = eh_get_opt_key ( args , "prop"  , 0 , prop_vals );
00075    
00076    switch ( prop )
00077    {
00078       case 0:
00079          get_val = &sed_hydro_water_flux;
00080          break;
00081       case 1:
00082          get_val = &sed_hydro_suspended_flux;
00083          break;
00084       case 2:
00085          get_val = &sed_hydro_velocity;
00086          break;
00087       case 3:
00088          get_val = &sed_hydro_width;
00089          break;
00090       case 4:
00091          get_val = &sed_hydro_depth;
00092          break;
00093       case 5:
00094          get_val = &sed_hydro_bedload;
00095          break;
00096    }
00097    
00098    if ( verbose )
00099    {
00100       fprintf(stderr,"--- head ---\n");
00101       fprintf(stderr,"total time (days) : %f\n",total_time);
00102       fprintf(stderr,"n records    : %d\n",n_recs);
00103       fprintf(stderr,"buf length   : %d\n",buf_len);
00104       fprintf(stderr,"n sig events : %d\n",n_sig);
00105       fprintf(stderr,"start        : %d\n",start);
00106       fprintf(stderr,"property     : %s\n",prop_vals[prop]);
00107       fprintf(stderr,"buffer       : %d\n",use_buf);
00108    }
00109 
00110    fp = sed_hydro_file_new( infile , SED_HYDRO_HYDROTREND , use_buf , TRUE , NULL );
00111    sed_hydro_file_set_sig_values( fp , n_sig );
00112    sed_hydro_file_set_buffer_length( fp , buf_len );
00113 
00114    rec_array = g_ptr_array_new( );
00115    fprintf(stdout,"%s\n",prop_vals[prop]);
00116    for ( time=0 ; time<total_time ; )
00117    {
00118       rec = sed_hydro_file_read_record( fp );
00119       if ( sed_hydro_duration(rec) + time > total_time )
00120          sed_hydro_set_duration( rec , total_time-time );
00121       for ( j=0 ; j<sed_hydro_duration(rec) ; j++ )
00122          fprintf(stdout,"%f\n",(*get_val)( rec ));
00123       time += sed_hydro_duration(rec);
00124       g_ptr_array_add( rec_array , rec );
00125    }
00126    
00127    if ( verbose )
00128    {
00129       double total = 0, total_qs = 0;
00130 
00131       for ( j=0 ; j<rec_array->len ; j++ )
00132       {
00133          rec       = g_ptr_array_index( rec_array , j );
00134          total    += (*get_val)( rec )*sed_hydro_duration(rec);
00135          total_qs += sed_hydro_suspended_load( rec );
00136       }
00137 
00138       fprintf(stderr,"--- tail ---\n");
00139       fprintf(stderr,"n events         : %d\n",rec_array->len);
00140       fprintf(stderr,"total            : %f\n",total);
00141       fprintf(stderr,"total qs         : %f\n",total_qs);
00142    }
00143 
00144    g_ptr_array_free( rec_array , FALSE );
00145 
00146    sed_hydro_file_destroy( fp );
00147 
00148    return 0;
00149 }
00150 

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