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 #include <stdlib.h>
00023 #include <string.h>
00024 #include <math.h>
00025 #include <unistd.h>
00026 #include <glib.h>
00027 #include <utils/utils.h>
00028 #include <sed/sed_sedflux.h>
00029
00030
00031 static char *help_msg[] =
00032 {
00033 " ",
00034 " sedextract [options] [parameters] [filein] ",
00035 " extract a portion of a sedflux output file. ",
00036 " ",
00037 " Options ",
00038 " -b - Extraction positions will be given by row and column numbers.",
00039 " This is the default. ",
00040 " -m - Extraction positions will be given in units of meters. ",
00041 " out=outfile - send output to outfile instead of stdout. [stdout] ",
00042 " in=infile - read input from infile rather than stdin. [stdin] ",
00043 " verbose=yes - be verbose. [no] ",
00044 " -h - print this help message. ",
00045 " ",
00046 " Parameters ",
00047 " x0=value - Begin extraction at trace value. [0] ",
00048 " x1=value - End extraction at trace value. [end] ",
00049 " y0=value - Begin extraction at value value. [0] ",
00050 " y1=value - End extraction at row value. [end] ",
00051 " nx=value - Write out every value-th trace. [10] ",
00052 " ny=value - Resample each trace using every value-th row. [1] ",
00053 NULL
00054 };
00055
00056 #define UNITS_BINS (0)
00057 #define UNITS_METERS (1)
00058
00059
00060 #define DEFAULT_UNITS UNITS_BINS
00061 #define DEFAULT_X_START (-1)
00062 #define DEFAULT_X_STOP (-1)
00063 #define DEFAULT_Y_START (-1)
00064 #define DEFAULT_Y_STOP (-1)
00065 #define DEFAULT_X_INCREMENT (10)
00066 #define DEFAULT_Y_INCREMENT (1)
00067 #define DEFAULT_PRINT_HEADER 0
00068 #define DEFAULT_VERBOSE 0
00069 #define DEFAULT_IN_FILE stdin
00070 #define DEFAULT_OUT_FILE stdout
00071 #define DEFAULT_IN_FILE_NAME "stdin"
00072 #define DEFAULT_OUT_FILE_NAME "stdout"
00073
00074 #define FORGET_THIS_FOR_NOW
00075
00076 int main(int argc, char *argv[])
00077 {
00078
00079 #if !defined( FORGET_THIS_FOR_NOW )
00080 double x0, x1, y0, y1;
00081 double nx, ny;
00082 char *units;
00083 char *header_flag;
00084 FILE *fpin, *fpout;
00085 char *infile, *outfile;
00086 char *verbose;
00087 Profile_header *header, *header_new;
00088 unsigned char **data;
00089 int i,j;
00090 int row, column;
00091 Eh_args *args;
00092
00093 args = eh_opts_init(argc,argv);
00094 if ( eh_check_opts( args , NULL , NULL , help_msg )!=0 )
00095 eh_exit(-1);
00096
00097 x0 = eh_get_opt_dbl( args , "x0" , DEFAULT_X_START );
00098 x1 = eh_get_opt_dbl( args , "x1" , DEFAULT_X_STOP );
00099 y0 = eh_get_opt_dbl( args , "y0" , DEFAULT_Y_START );
00100 y1 = eh_get_opt_dbl( args , "y1" , DEFAULT_Y_STOP );
00101 nx = eh_get_opt_dbl( args , "nx" , DEFAULT_X_INCREMENT );
00102 ny = eh_get_opt_dbl( args , "ny" , DEFAULT_Y_INCREMENT );
00103 units = eh_get_opt_str( args , "units" , "bins" );
00104 header_flag = eh_get_opt_str( args , "header" , "no" );
00105 verbose = eh_get_opt_str( args , "verbose" , "no" );
00106 infile = eh_get_opt_str( args , "in" , NULL );
00107 outfile = eh_get_opt_str( args , "out" , NULL );
00108
00109 if ( !infile )
00110 fpin = stdin;
00111 else
00112 if ( !(fpin=fopen(infile,"r")) )
00113 perror(infile), eh_exit(-1);
00114 if ( !outfile )
00115 fpout = stdout;
00116 else
00117 if ( !(fpout=fopen(outfile,"w")) )
00118 perror(outfile), eh_exit(-1);
00119
00120
00121 header = sed_read_profile_header( fpin );
00122
00123 if ( g_strcasecmp( header_flag , "yes" )==0 )
00124 {
00125 fprintf( stderr , "Number of rows : %d\n" , header->n_rows );
00126 fprintf( stderr , "Number of columns : %ld\n" , header->n_cols );
00127 fprintf( stderr , "Row height : %f\n" , header->cell_height );
00128 fprintf( stderr , "Column Width : %f\n" , header->cell_width );
00129 fprintf( stderr , "Minimum value : %f\n" , header->min_value );
00130 fprintf( stderr , "Maximum value : %f\n" , header->max_value );
00131
00132 eh_exit(0);
00133 }
00134
00135 if ( g_strcasecmp( units , "meters" )==0 )
00136 {
00137 y0 /= header->cell_height;
00138 y1 /= header->cell_height;
00139 x0 /= header->cell_width;
00140 x1 /= header->cell_width;
00141
00142
00143
00144
00145 }
00146 else
00147 fprintf(stderr,"error : invalid unit argument, %s.\n",units), eh_exit(-1);
00148
00149
00150 data = g_new( unsigned char* , header->n_cols );
00151 data[0] = g_new( unsigned char , header->n_cols*header->n_rows);
00152 for ( i=1 ; i<header->n_cols ; i++ )
00153 data[i] = data[i-1]+header->n_rows;
00154 fread( data[0] , sizeof(unsigned char) , header->n_rows*header->n_cols , fpin );
00155
00156
00157 if ( y1 < 0 ) y1 = header->n_rows;
00158 if ( x1 < 0 ) x1 = header->n_cols;
00159
00160 if ( y1 > header->n_rows ) y1 = header->n_rows;
00161 if ( x1 > header->n_cols ) x1 = header->n_cols;
00162
00163 if ( g_strcasecmp( verbose , "yes" )==0 )
00164 {
00165 fprintf(stderr,"sedextract : extracting image from (%f,%f) -> (%f,%f)\n",x0,y0,x1,y1);
00166 fprintf(stderr,"sedextract : resampling interval in the x-direction is %.0f\n",nx);
00167 fprintf(stderr,"sedextract : resampling interval in the y-direction is %.0f\n",ny);
00168 }
00169
00170
00171 header_new = g_new( Profile_header , 1 );
00172 header_new->n_rows = (int)((y1-y0)/ny+.5);
00173 header_new->n_cols = (int)((x1-x0)/nx+.5);
00174 header_new->cell_height = header->cell_height*ny;
00175 header_new->cell_width = header->cell_width*nx;
00176 header_new->max_value = header->max_value;
00177 header_new->min_value = header->min_value;
00178 sed_print_profile_header( fpout , header_new );
00179
00180
00181 for ( j=(int)x0,column=0 ; column<header_new->n_cols ; j+=(int)nx,column++ )
00182 for ( i=(int)y0,row=0 ; row<header_new->n_rows ; i+=(int)ny,row++ )
00183 fwrite(&data[j][i],sizeof(unsigned char),1,fpout);
00184
00185 g_free(data);
00186
00187 fclose(fpin);
00188 fclose(fpout);
00189
00190 #endif
00191
00192 return 0;
00193 }
00194
00195 #undef FORGET_THIS_FOR_NOW