00001 #include <glib.h> 00002 #include <utils/utils.h> 00003 #include <sed/sed_sedflux.h> 00004 00005 static gchar* out_file = NULL; 00006 static double slope = .02; 00007 static gint n_x = 100; 00008 static gint n_y = 100; 00009 static double x_res = 1000.; 00010 static double y_res = 1000.; 00011 static gboolean big_endian = FALSE; 00012 static gboolean little_endian = FALSE; 00013 static gboolean ascii = FALSE; 00014 static gboolean version = FALSE; 00015 static gint verbosity = 0; 00016 static gboolean debug = FALSE; 00017 00018 GOptionEntry entries[] = 00019 { 00020 { "out-file" , 'o' , 0 , G_OPTION_ARG_FILENAME , &out_file , "Output bathymetry file" , "<file>" } , 00021 { "slope" , 's' , 0 , G_OPTION_ARG_DOUBLE , &slope , "Bathymetric slope" , "SLOPE" } , 00022 { "nx" , 'x' , 0 , G_OPTION_ARG_INT , &n_x , "Number of cell in x-dimension" , "NX" } , 00023 { "ny" , 'y' , 0 , G_OPTION_ARG_INT , &n_y , "Number of cell in y-dimension" , "NY" } , 00024 { "x-res" , 'X' , 0 , G_OPTION_ARG_DOUBLE , &x_res , "Cell size in x-dimension (m)" , "DX" } , 00025 { "y-res" , 'Y' , 0 , G_OPTION_ARG_DOUBLE , &y_res , "Cell size in y-dimension (m)" , "DY" } , 00026 { "big-endian" , 'b' , 0 , G_OPTION_ARG_NONE , &big_endian , "Byte order is big endian" , NULL } , 00027 { "little-endian" , 'l' , 0 , G_OPTION_ARG_NONE , &little_endian , "Byte order is little endian" , NULL } , 00028 { "ascii" , 'a' , 0 , G_OPTION_ARG_NONE , &ascii , "Space delimited ASCII output" , NULL } , 00029 { "verbose" , 'V' , 0 , G_OPTION_ARG_INT , &verbosity , "Verbosity level" , "n" } , 00030 { "version" , 'v' , 0 , G_OPTION_ARG_NONE , &version , "Version number" , NULL } , 00031 { "debug" , 'd' , 0 , G_OPTION_ARG_NONE , &debug , "Write debugging messages" , NULL } , 00032 { NULL } 00033 }; 00034 00035 int main( int argc , char* argv[] ) 00036 { 00037 GOptionContext* context = g_option_context_new( "Create a bathymetry file for sedflux-3d" ); 00038 GError* error = NULL; 00039 FILE* fp_out = stdout; 00040 gint byte_order = G_BYTE_ORDER; 00041 00042 g_option_context_add_main_entries( context , entries , NULL ); 00043 00044 if ( !g_option_context_parse( context , &argc , &argv , &error ) ) 00045 eh_error( "Error parsing command line arguments: %s" , error->message ); 00046 00047 if ( version ) 00048 eh_fprint_version_info( stdout , "sedflux-make-bathy" , 0 , 9 , 0 ), exit(0); 00049 00050 if ( debug ) 00051 g_setenv( "SEDFLUX_MAKE_BATHY_DEBUG" , "TRUE" , TRUE ); 00052 00053 eh_set_verbosity_level( verbosity ); 00054 00055 if ( out_file ) 00056 fp_out = eh_fopen_error( out_file , "w" , &error ); 00057 00058 if ( !fp_out ) 00059 eh_error( "Error opening file: %s" , error->message ); 00060 00061 if ( big_endian && little_endian ) 00062 eh_error( "Byte order must be big endian or little endian" ); 00063 else if ( !big_endian && !little_endian ) 00064 byte_order = G_BYTE_ORDER; 00065 else if ( big_endian ) 00066 byte_order = G_BIG_ENDIAN; 00067 else if ( little_endian ) 00068 byte_order = G_LITTLE_ENDIAN; 00069 00070 eh_message( "x-resolution (m) : %f" , x_res ); 00071 eh_message( "y-resolution (m) : %f" , y_res ); 00072 eh_message( "x-dimension : %d" , n_x ); 00073 eh_message( "y-dimension : %d" , n_y ); 00074 eh_message( "Bathymetric slope : %f" , slope ); 00075 00076 if ( ascii ) 00077 { 00078 eh_message( "Byte order : %s" , "ASCII" ); 00079 eh_message( "Format : %s" , "Space-delimited" ); 00080 } 00081 else 00082 { 00083 eh_message( "Byte order : %s" , byte_order==G_BIG_ENDIAN?"Big-endian":"Little-endian" ); 00084 eh_message( "Format : %s" , "<32-bit-integer=N> <<64-bit-double*N> ... EOF>" ); 00085 } 00086 00087 { 00088 gint i, j; 00089 Eh_dbl_grid g = eh_grid_new( double , n_x , n_y ); 00090 00091 for ( i=0 ; i<n_x ; i++ ) 00092 for ( j=0 ; j<n_y ; j++ ) 00093 eh_dbl_grid_set_val( g , i , j , -slope*j*y_res ); 00094 00095 if ( ascii ) 00096 eh_dlm_print_dbl_grid( out_file , ", " , g , NULL ); 00097 else if ( byte_order == G_BYTE_ORDER ) 00098 { 00099 fwrite( &n_y , sizeof(gint32) , 1 , fp_out ); 00100 fwrite( eh_grid_data_start(g) , sizeof(double) , n_x*n_y , fp_out ); 00101 } 00102 else 00103 { 00104 eh_fwrite_int32_swap( &n_y , sizeof(gint32) , 1 , fp_out ); 00105 eh_fwrite_dbl_swap ( eh_grid_data_start(g) , sizeof(double) , n_x*n_y , fp_out ); 00106 } 00107 00108 eh_grid_destroy( g , TRUE ); 00109 } 00110 00111 fclose( fp_out ); 00112 } 00113