/Users/huttone/Devel/sedflux-new/sedflux/trunk/ew/sedutils/sedwheeler.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 <glib.h>
00023 #include <utils/utils.h>
00024 #include <sed/sed_sedflux.h>
00025 
00026 #define DEFAULT_T0 (0)
00027 #define DEFAULT_T1 (-1)
00028 
00029 #define WHEELER_EROSION    (0)
00030 #define WHEELER_HIATUS     (1)
00031 #define WHEELER_DEPOSITION (2)
00032 
00033 #define EROSION_DEPOSITION (0)
00034 #define SEDIMENTATION_RATE (1)
00035 #define TIME_LINE          (2)
00036 
00037 double get_type_val( double z_0 , double z_1 , double z_min , int type );
00038 double get_rate_val( double z_0 , double z_1 , double z_min , int type );
00039 double get_time_line_val( double z_0 , double z_1 , double z_min , int type );
00040 
00041 #define FORGET_THIS_FOR_NOW
00042 
00043 int main( int argc , char *argv[] )
00044 {
00045 #if !defined(FORGET_THIS_FOR_NOW)
00046    gint64 i, j;
00047    gint64  vals_per_rec, n_recs, n_cols;
00048    int type = EROSION_DEPOSITION;
00049    int start, end;
00050    int rec_0, rec_1;
00051    FILE *fp_in, *fp_out;
00052    char *str = g_new( char , 2048 );
00053    char *infile, *outfile;
00054    gboolean verbose;
00055    double t_0, t_1;
00056    double *t, *x, **d;
00057    double d_min;
00058    Eh_args *args;
00059    Met_station_header hdr;
00060    double (*get_val)(double,double,double,int);
00061 
00062    args = eh_opts_init(argc,argv);
00063    if ( eh_check_opts( args , NULL , NULL , NULL )!=0 )
00064       eh_exit(-1);
00065 
00066    infile     = eh_get_opt_str ( args , "in"      , NULL       );
00067    outfile    = eh_get_opt_str ( args , "out"     , NULL       );
00068    t_0        = eh_get_opt_dbl ( args , "t0"      , DEFAULT_T0 );
00069    t_1        = eh_get_opt_dbl ( args , "t1"      , DEFAULT_T1 );
00070    verbose    = eh_get_opt_bool( args , "verbose" , FALSE      );
00071 
00072    if ( infile )
00073    {
00074       fp_in = fopen( infile , "r" );
00075       if ( !fp_in )
00076          perror( infile );
00077    }
00078    else
00079       fp_in = stdin;
00080    if ( outfile )
00081    {
00082       fp_out = fopen( outfile , "w" );
00083       if ( !fp_out )
00084          perror( outfile );
00085    }
00086    else
00087       fp_out = stdout;
00088 
00089    hdr = sed_read_measurement_header( fp_in );
00090 
00091    if ( verbose )
00092       sed_write_measurement_header( stderr , &hdr );
00093 
00094    fread( &vals_per_rec , sizeof(gint64) , 1 , fp_in );
00095 
00096    n_cols = (vals_per_rec-1)/2;
00097 
00098    start = ftell( fp_in );
00099    fseek( fp_in , 0 , SEEK_END );
00100    end   = ftell( fp_in );
00101    n_recs = (end-start)/(sizeof(double)*vals_per_rec);
00102    fseek( fp_in , start , SEEK_SET );
00103 
00104    t = g_new( double  , n_recs );
00105    x = g_new( double  , n_cols );
00106    d = g_new( double* , n_recs );
00107 
00108    for ( i=0 ; i<n_recs ; i++ )
00109    {
00110       d[i] = g_new( double , n_cols );
00111       fread( &(t[i]) , sizeof(double) , 1 , fp_in );
00112       fread( x       , sizeof(double) , n_cols , fp_in );
00113       fread( d[i]    , sizeof(double) , n_cols , fp_in );
00114    }
00115 
00116    for ( rec_0=0        ; rec_0<n_recs && t[rec_0]<t_0 ; rec_0++ );
00117    for ( rec_1=n_recs-1 ; rec_1>=0     && t[rec_1]>t_1 ; rec_1-- );
00118 
00119    if ( t_0<0 )
00120       rec_0 = 0;
00121    if ( t_1<0 )
00122       rec_1 = n_recs-1;
00123 
00124    g_free( hdr.parameter );
00125 
00126    if ( type == EROSION_DEPOSITION )
00127    {
00128       get_val = &get_type_val;
00129       hdr.parameter = g_strdup( "erosion / deposition" );
00130    }
00131    else if ( type == SEDIMENTATION_RATE )
00132    {
00133       get_val = &get_rate_val;
00134       hdr.parameter = g_strdup( "sedimentation rate" );
00135    }
00136    else if ( type == TIME_LINE )
00137    {
00138       get_val = &get_time_line_val;
00139       hdr.parameter = g_strdup( "sediment thickness" );
00140    }
00141    else
00142       eh_require_not_reached();
00143 
00144    for ( i=0 ; i<n_cols ; i++ )
00145    {
00146       for ( j=rec_1 ; j>=rec_0+1 ; j-- )
00147       {
00148          if ( d[j][i]<d[j-1][i] )
00149          {
00150             d_min = d[j][i];
00151             for ( ; j>=1 && d[j-1][i]>d_min ; j-- )
00152                d[j][i] = (*get_val)( d[j-1][i] , d[j][i] ,
00153                                   d_min     , WHEELER_EROSION );
00154 /*
00155                if ( d[j-1][i]-d_min<1e-5 )
00156                   d[j][i] = WHEELER_HIATUS;
00157 //                  d[j][i] = d_min - d[j-1][i];
00158 //                  d[j][i] = d_min;
00159                else
00160                   d[j][i] = WHEELER_EROSION;
00161 //                  d[j][i] = d_min - d[j-1][i];
00162 //                  d[j][i] = d_min;
00163 */
00164             j++;
00165          }
00166          else if ( d[j][i]-d[j-1][i]>.1 )
00167             d[j][i] = (*get_val)( d[j-1][i] , d[j][i] ,
00168                                d_min     , WHEELER_DEPOSITION );
00169 /*
00170             d[j][i] = WHEELER_DEPOSITION;
00171 //            d[j][i] = d[j][i]-d[j-1][i];
00172 //            d[j][i] = d[j][i];
00173 */
00174          else
00175             d[j][i] = (*get_val)( d[j-1][i] , d[j][i] ,
00176                                d_min     , WHEELER_HIATUS );
00177 /*
00178             d[j][i] = WHEELER_HIATUS;
00179 //            d[j][i] = d[j][i]-d[j-1][i];
00180 //            d[j][i] = d[j][i];
00181 */
00182       }
00183       d[j][i] = (*get_val)( d[j][i] , d[j][i] ,
00184                          d_min   , WHEELER_HIATUS );
00185 /*
00186       d[0][i] = WHEELER_HIATUS;
00187 //      d[0][i] = 0;
00188 //      d[0][i] = d[0][i];
00189 */
00190    }
00191 
00192    sed_write_measurement_header( fp_out , &hdr );
00193    fwrite( &vals_per_rec , sizeof(gint64) , 1 , fp_out );
00194    for ( i=rec_0 ; i<rec_1 ; i++ )
00195    {
00196       fwrite( &(t[i]) , sizeof(double) , 1      , fp_out );
00197       fwrite( x       , sizeof(double) , n_cols , fp_out );
00198       fwrite( d[i]    , sizeof(double) , n_cols , fp_out );
00199    }
00200 
00201    g_free( str );
00202 #endif
00203 
00204    return 0;
00205 }
00206 
00207 #undef FORGET_THIS_FOR_NOW
00208 
00209 double get_type_val( double z_0 , double z_1 , double z_min , int type )
00210 {
00211    double rtn;
00212    if ( type == WHEELER_EROSION )
00213    {
00214       if ( z_0-z_min<1e-5 )
00215          rtn = WHEELER_HIATUS;
00216       else
00217          rtn = WHEELER_EROSION;
00218    }
00219    else if ( type == WHEELER_HIATUS )
00220       rtn = WHEELER_HIATUS;
00221    else if ( type == WHEELER_DEPOSITION )
00222       rtn = WHEELER_DEPOSITION;
00223    else
00224       eh_require_not_reached();
00225 
00226    return rtn;
00227 }
00228 
00229 double get_rate_val( double z_0 , double z_1 , double z_min , int type )
00230 {
00231    double rtn;
00232    if ( type == WHEELER_EROSION )
00233       rtn = z_min - z_0;
00234    else if ( type == WHEELER_HIATUS )
00235       rtn = z_1 - z_0;
00236    else if ( type == WHEELER_DEPOSITION )
00237       rtn = z_1 - z_0;
00238    else
00239       eh_require_not_reached();
00240 
00241    return rtn;
00242 }
00243 
00244 double get_time_line_val( double z_0 , double z_1 , double z_min , int type )
00245 {
00246    double rtn;
00247    if ( type == WHEELER_EROSION )
00248       rtn = z_min;
00249    else if ( type == WHEELER_HIATUS )
00250       rtn = z_1;
00251    else if ( type == WHEELER_DEPOSITION )
00252       rtn = z_1;
00253    else
00254       eh_require_not_reached();
00255 
00256    return rtn;
00257 }
00258 

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