コード例 #1
0
ファイル: joebob.c プロジェクト: JOravetz/Depth_Conversion
int main (int argc, char **argv) {

   char *coeff_x, *coeff_x2, *coeff_x3, file[BUFSIZ];
   cwp_Bool active = TRUE;
	
   struct GRD_HEADER grd_x, grd_x2, grd_x3;
   struct GMT_EDGEINFO edgeinfo_x, edgeinfo_x2, edgeinfo_x3;
   struct GMT_BCR bcr_x, bcr_x2, bcr_x3;

   short  check, verbose;
   int    nz, ntr, ns;
   double value, scale_factor, dz, x_loc, y_loc;
   double weight_x, weight_x2, weight_x3;
   double value_coeff_x, value_coeff_x2, value_coeff_x3, tr_sec, dt_sec;
   float  depth_input, amp_output, *tr_amp, *depth;
   register int k, n;

   initargs(argc, argv);
   argc = GMT_begin (argc, argv);

   if (!getparstring("coeff_x", &coeff_x)) {
      fprintf ( stderr, "Must supply Coefficient_X GMT grid (COEFF_X Parameter) --> exiting\n" );
      return EXIT_FAILURE;
   }

   if (!getparstring("coeff_x2", &coeff_x2)) {
      fprintf ( stderr, "Must supply Coefficient_X2 GMT grid (COEFF_X2 Parameter)--> exiting\n" );
      return EXIT_FAILURE;
   }

   if (!getparstring("coeff_x3", &coeff_x3)) {
      fprintf ( stderr, "Must supply Coefficient_X3 GMT grid (COEFF_X3 Parameter)--> exiting\n" );
      return EXIT_FAILURE;
   }

   if (!getparshort("verbose" , &verbose)) verbose = 0;
   if (!getpardouble("weight_x", &weight_x)) weight_x  = 1.0;
   if (!getpardouble("weight_x2", &weight_x2)) weight_x2  = 1.0;
   if (!getpardouble("weight_x3", &weight_x3)) weight_x3  = 1.0;

   if ( verbose ) {
      fprintf ( stderr, "\n" );
      fprintf ( stderr, "X1 Coefficient GMT grid file name = %s\n", coeff_x );
      fprintf ( stderr, "X2 Coefficient GMT grid file name = %s\n", coeff_x2 );
      fprintf ( stderr, "X3 Coefficient GMT grid file name = %s\n", coeff_x3 );
      fprintf ( stderr, "X1 Grid Weighting Value = %f\n", weight_x );
      fprintf ( stderr, "X2 Grid Weighting Value = %f\n", weight_x2 );
      fprintf ( stderr, "X3 Grid Weighting Value = %f\n", weight_x3 );
      fprintf ( stderr, "\n" );
   }

   weight_x  = 1.0 / weight_x;
   weight_x2 = 1.0 / weight_x2;
   weight_x3 = 1.0 / weight_x3;

   GMT_boundcond_init (&edgeinfo_x);
   GMT_boundcond_init (&edgeinfo_x2);
   GMT_boundcond_init (&edgeinfo_x3);

   GMT_grd_init (&grd_x,  argc, argv, FALSE);
   GMT_grd_init (&grd_x2, argc, argv, FALSE);
   GMT_grd_init (&grd_x3, argc, argv, FALSE);

   if (GMT_read_grd_info (coeff_x,  &grd_x))  fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);
   if (GMT_read_grd_info (coeff_x2, &grd_x2)) fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);
   if (GMT_read_grd_info (coeff_x3, &grd_x3)) fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);
		
   f1 = (float *) GMT_memory (VNULL, (size_t)((grd_x.nx  + 4) * (grd_x.ny  + 4)), sizeof(float), GMT_program);
   f2 = (float *) GMT_memory (VNULL, (size_t)((grd_x2.nx + 4) * (grd_x2.ny + 4)), sizeof(float), GMT_program);
   f3 = (float *) GMT_memory (VNULL, (size_t)((grd_x3.nx + 4) * (grd_x3.ny + 4)), sizeof(float), GMT_program);

   GMT_pad[0] = GMT_pad[1] = GMT_pad[2] = GMT_pad[3] = 2;

   GMT_boundcond_param_prep (&grd_x,  &edgeinfo_x);
   GMT_boundcond_param_prep (&grd_x2, &edgeinfo_x2);
   GMT_boundcond_param_prep (&grd_x3, &edgeinfo_x3);

   GMT_boundcond_set (&grd_x,  &edgeinfo_x,  GMT_pad, f1);
   GMT_boundcond_set (&grd_x2, &edgeinfo_x2, GMT_pad, f2);
   GMT_boundcond_set (&grd_x3, &edgeinfo_x3, GMT_pad, f3);

   value = 0.0;
   GMT_bcr_init (&grd_x,  GMT_pad, active, value, &bcr_x);
   GMT_bcr_init (&grd_x2, GMT_pad, active, value, &bcr_x2);
   GMT_bcr_init (&grd_x3, GMT_pad, active, value, &bcr_x3);

   GMT_read_grd (coeff_x,  &grd_x,  f1, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);
   GMT_read_grd (coeff_x2, &grd_x2, f2, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);
   GMT_read_grd (coeff_x3, &grd_x3, f3, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);

   /* Get info from first trace */
   ntr     = gettra (&tr, 0);
   ns      = tr.ns;
   dt_sec  = tr.dt * 0.000001;
   scale_factor = tr.scalco;
   if (scale_factor < 0.0 ) scale_factor *= -1.0;
   if (scale_factor == 0.0 ) scale_factor = 1.0;

   if (!getpardouble ("dz",&dz)) dz = 2.0;
   if (!getparint    ("nz",&nz)) nz = ns;

   if ( verbose ) {
      fprintf ( stderr, "Output depth sample rate = %f\n", dz );
      fprintf ( stderr, "Coordinate scale factor = %f\n", scale_factor );
      fprintf ( stderr, "Number of output depth samples per trace = %d\n", nz );
      fprintf ( stderr, "number of traces = %d, number of samples per trace = %d\n", ntr, ns );
      fprintf ( stderr, "time sample rate (seconds) = %f\n", dt_sec );
   }

   rewind (stdin);

   depth  = ealloc1float ( ns );
   tr_amp = ealloc1float ( nz );

   /* Main loop over traces */
   for ( k = 0; k < ntr; ++k ) {
      gettr (&tr);
      x_loc = tr.sx / scale_factor;
      y_loc = tr.sy / scale_factor;

      check = 0;
      if ( x_loc >= grd_x.x_min && x_loc <= grd_x.x_max && y_loc >= grd_x.y_min && y_loc <= grd_x.y_max ) check = 1;

      if ( check ) {
         value_coeff_x  = GMT_get_bcr_z (&grd_x,  x_loc, y_loc, f1, &edgeinfo_x,  &bcr_x);
         value_coeff_x2 = GMT_get_bcr_z (&grd_x2, x_loc, y_loc, f2, &edgeinfo_x2, &bcr_x2);
         value_coeff_x3 = GMT_get_bcr_z (&grd_x3, x_loc, y_loc, f3, &edgeinfo_x3, &bcr_x3);

	 if ( verbose ) fprintf ( stderr, "Trace num = %d, X-Loc = %f, Y-Loc = %f, X Coefficient = %0.10f, X2 Coefficient = %0.10f, X3 Coefficient = %0.10f\n", 
         k+1, x_loc, y_loc, value_coeff_x, value_coeff_x2, value_coeff_x3 );

	 for ( n=0; n < ns; ++n ) {
            tr_amp[n] = tr.data[n];
	    tr_sec   = n * dt_sec;
	    depth[n] = (((value_coeff_x*tr_sec)*weight_x) + ((value_coeff_x2*pow(tr_sec,2))*weight_x2) + ((value_coeff_x3*pow(tr_sec,3))*weight_x3)) * -1.0;
            if ( verbose == 2 ) fprintf ( stderr, "Trace no. = %5d, Sample = %5d, TWT (secs.) = %.4f, Depth (feet) = %.4f\n", k, n, tr_sec, depth[n] ); 
	 }
	 for ( n=0; n < nz; ++n ) {
	    depth_input = n * dz;
	    intlin ( ns, depth, tr_amp, tr_amp[0], tr_amp[ns-1], 1, &depth_input, &amp_output );
	    dtr.data[n] = amp_output; 
         }
	 dtr.tracl  = tr.tracl;
	 dtr.tracr  = tr.tracr;
	 dtr.ep     = tr.ep;
	 dtr.ns     = nz;
	 dtr.dt     = nint (dz * 1000.0);
	 dtr.sx     = tr.sx;
	 dtr.sy     = tr.sy;
	 dtr.trid   = 1;
	 dtr.fldr   = tr.fldr;
	 dtr.cdp    = tr.cdp ;
	 puttr (&dtr);
      } else {
         fprintf ( stderr, "input trace = %d, xloc = %.0f yloc = %.0f is out of bounds\n", k, x_loc, y_loc);
      }
   }

   GMT_free ((void *)f1);
   GMT_free ((void *)f2);
   GMT_free ((void *)f3);
   GMT_end  (argc, argv);

   free1float (depth);
   free1float (tr_amp);

   return (0);
}
コード例 #2
0
ファイル: grdcut.c プロジェクト: likueimo/ubuntu_gmt4
int main (int argc, char **argv)
{
	GMT_LONG	error = FALSE;

	GMT_LONG	i, k, nx_old, ny_old, nx_new, ny_new, one_or_zero;
	
	GMT_LONG	nm;

	char *grd_in, format[BUFSIZ], za[GMT_TEXT_LEN], zb[GMT_TEXT_LEN];

	float	*grd = NULL;

	double	w_new = 0.0, e_new = 0.0, s_new = 0.0, n_new = 0.0;
	double	w_old, e_old, s_old, n_old;

	struct GRD_HEADER header, test_header;
	struct GRDCUT_CTRL *Ctrl = NULL;

	void *New_grdcut_Ctrl (), Free_grdcut_Ctrl (struct GRDCUT_CTRL *C);
	
	argc = (int)GMT_begin (argc, argv);

	Ctrl = (struct GRDCUT_CTRL *) New_grdcut_Ctrl ();	/* Allocate and initialize a new control structure */

	grd_in = CNULL;
	za[0] = zb[0] = '\0';

	/* Check and interpret the command line arguments */

	for (i = 1; i < argc; i++) {
		if (argv[i][0] == '-') {
			switch(argv[i][1]) {
				/* Common parameters */

				case 'R':
				case 'V':
				case 'f':
				case '\0':
					error += GMT_parse_common_options (argv[i], &w_new, &e_new, &s_new, &n_new);
					break;

	 			case 'G':
					Ctrl->G.active = TRUE;
					Ctrl->G.file = strdup (&argv[i][2]);
					break;
					
	 			case 'Z':
					Ctrl->Z.active = TRUE;
					k = 2;
					if (argv[i][k] == 'n') {
						Ctrl->Z.mode = NAN_IS_OUTSIDE;
						k = 3;
					}
					if (sscanf (&argv[i][k], "%[^/]/%s", za, zb) == 2) {
						if (!(za[0] == '-' && za[1] == '\0')) error += GMT_verify_expectations (GMT_io.in_col_type[GMT_Z], GMT_scanf_arg (za, GMT_io.in_col_type[GMT_Z], &Ctrl->Z.min), za);
						if (!(zb[0] == '-' && zb[1] == '\0')) error += GMT_verify_expectations (GMT_io.in_col_type[GMT_Z], GMT_scanf_arg (zb, GMT_io.in_col_type[GMT_Z], &Ctrl->Z.max), zb);
					}
					break;
	
				default:		/* Options not recognized */
					error = TRUE;
					GMT_default_error (argv[i][1]);
					break;
			}
		}
		else
	 		grd_in = argv[i];
	}

	if (argc == 1 || GMT_give_synopsis_and_exit) {
		fprintf (stderr,"grdcut %s - Extract subsets from grid files\n\n", GMT_VERSION);
		fprintf (stderr, "usage: grdcut <input_grd> -G<output_grd> %s [-V] [-Z[n][min/max]] [%s]\n", GMT_Rgeo_OPT, GMT_f_OPT);

		if (GMT_give_synopsis_and_exit) exit (EXIT_FAILURE);

		fprintf (stderr, "\t<input_grd> is file to extract a subset from.\n");
		fprintf (stderr, "\t-G specifies output grid file.\n");
		GMT_explain_option ('R');
		fprintf (stderr, "\t   Obviously, the WESN you specify must be within the WESN of the input file.\n");
		fprintf (stderr, "\t   If in doubt, run grdinfo first and check range of old file.\n");
		fprintf (stderr, "\n\tOPTIONS:\n");
		GMT_explain_option ('V');
		fprintf (stderr, "\t-Z Specify a range and determine the corresponding rectangular region so that\n");
		fprintf (stderr, "\t   all values outside this region are outside the range [-inf/+inf].\n");
		fprintf (stderr, "\t   Use -Zn to consider NaNs outside as well [Default just ignores NaNs].\n");
		GMT_explain_option ('f');
		exit (EXIT_FAILURE);
	}

	/* Check that the options selected make sense */

	if ((project_info.region_supplied + Ctrl->Z.active) != 1) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR:  Must specify either the -R or the -Z options\n", GMT_program);
		error++;
	}
	if (!Ctrl->G.file) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR -G option:  Must specify output file\n", GMT_program);
		error++;
	}
	if (!grd_in) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR:  Must specify input file\n", GMT_program);
		error++;
	}
	if (error) exit (EXIT_FAILURE);

	GMT_err_fail (GMT_read_grd_info (grd_in, &header), grd_in);

	if (Ctrl->Z.active) {	/* Must determine new region via -Z */
		GMT_LONG i0, i1, j0, j1, j, ij;
		
		nm = GMT_get_nm (header.nx, header.ny);
		grd = (float *) GMT_memory (VNULL, (size_t)nm, sizeof (float), GMT_program);
		GMT_err_fail (GMT_read_grd (grd_in, &header, grd, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE), grd_in);
		for (i = 0, i0 = -1; i0 == -1 && i < header.nx; i++) {	/* Scan from xmin towards xmax */
			for (j = 0, ij = i; i0 == -1 && j < header.ny; j++, ij += header.ny) {
				if (GMT_is_fnan (grd[ij])) {
					if (Ctrl->Z.mode == NAN_IS_OUTSIDE) i0 = i;	/* Must stop since this value defines the inner box */
				}
				else if (grd[ij] >= Ctrl->Z.min && grd[ij] <= Ctrl->Z.max)
					i0 = i;
			}
		}
		if (i0 == -1) {
			fprintf (stderr, "%s: The sub-region is empty - no file written\n", GMT_program);
			GMT_free ((void *)grd);
			exit (EXIT_FAILURE);
		}
		for (i = header.nx-1, i1 = -1; i1 == -1 && i > i0; i--) {	/* Scan from xmax towards xmin */
			for (j = 0, ij = i; i1 == -1 && j < header.ny; j++, ij += header.ny) {
				if (GMT_is_fnan (grd[ij])) {
					if (Ctrl->Z.mode == NAN_IS_INSIDE) i1 = i;	/* Must stop since this value defines the inner box */
				}
				else if (grd[ij] >= Ctrl->Z.min && grd[ij] <= Ctrl->Z.max)
					i1 = i;
			}
		}
		for (j = 0, j0 = -1; j0 == -1 && j < header.ny; j++) {	/* Scan from ymin towards ymax */
			for (i = i0, ij = GMT_IJ(j,i0,header.nx); j0 == -1 && i < i1; i++, ij++) {
				if (GMT_is_fnan (grd[ij])) {
					if (Ctrl->Z.mode == NAN_IS_INSIDE) j0 = j;	/* Must stop since this value defines the inner box */
				}
				else if (grd[ij] >= Ctrl->Z.min && grd[ij] <= Ctrl->Z.max)
					j0 = j;
			}
		}
		for (j = header.ny-1, j1 = -1; j1 == -1 && j >= j0; j--) {	/* Scan from ymax towards ymin */
			for (i = i0, ij = GMT_IJ(j,i0,header.nx); j1 == -1 && i < i1; i++, ij++) {
				if (GMT_is_fnan (grd[ij])) {
					if (Ctrl->Z.mode == NAN_IS_INSIDE) j1 = j;	/* Must stop since this value defines the inner box */
				}
				else if (grd[ij] >= Ctrl->Z.min && grd[ij] <= Ctrl->Z.max)
					j1 = j;
			}
		}
		if (i0 == 0 && j0 == 0 && i1 == (header.nx-1) && j1 == (header.ny-1)) {
			fprintf (stderr, "%s: Your -Z limits produced no subset - output grid is identical to input grid\n", GMT_program);
			w_new = header.x_min;	e_new = header.x_max;
			s_new = header.y_min;	n_new = header.y_max;
		}
		else {	/* Adjust boundaries inwards */
			w_new = header.x_min + i0 * header.x_inc;
			e_new = header.x_max - (header.nx - 1 - i1) * header.x_inc;
			s_new = header.y_min + (header.ny - 1 - j1) * header.y_inc;
			n_new = header.y_max - j0 * header.y_inc;
		}
		GMT_free ((void *)grd);
	}
	
	if (s_new < header.y_min || s_new > header.y_max) error = TRUE;
	if (n_new < header.y_min || n_new > header.y_max) error = TRUE;

	if (GMT_io.in_col_type[GMT_X] == GMT_IS_LON) {	/* Geographic data */
		if (w_new < header.x_min && e_new < header.x_min) {
			header.x_min -= 360.0;
			header.x_max -= 360.0;
		}
		if (w_new > header.x_max && e_new > header.x_max) {
			header.x_min += 360.0;
			header.x_max += 360.0;
		}
		if (!GMT_grd_is_global (&header) && (w_new < header.x_min || e_new > header.x_max)) error = TRUE;
	}
	else if (w_new < header.x_min || e_new > header.x_max)
		error = TRUE;

	if (error) {
		fprintf (stderr, "%s: Subset exceeds data domain!\n", GMT_program);
		exit (EXIT_FAILURE);
	}

	/* Make sure output grid is kosher */

	GMT_adjust_loose_wesn (&w_new, &e_new, &s_new, &n_new, &header);

	test_header.x_min = w_new;	test_header.x_max = e_new;	test_header.x_inc = header.x_inc;
	test_header.y_min = s_new;	test_header.y_max = n_new;	test_header.y_inc = header.y_inc;
	GMT_err_fail (GMT_grd_RI_verify (&test_header, 1), Ctrl->G.file);

	/* OK, so far so good. Check if new wesn differs from old wesn by integer dx/dy */

	if (GMT_minmaxinc_verify (header.x_min, w_new, header.x_inc, GMT_SMALL) == 1) {
		fprintf (stderr, "%s: Old and new x_min do not differ by N * dx\n", GMT_program);
		exit (EXIT_FAILURE);
	}
	if (GMT_minmaxinc_verify (e_new, header.x_max, header.x_inc, GMT_SMALL) == 1) {
		fprintf (stderr, "%s: Old and new x_max do not differ by N * dx\n", GMT_program);
		exit (EXIT_FAILURE);
	}
	if (GMT_minmaxinc_verify (header.y_min, s_new, header.y_inc, GMT_SMALL) == 1) {
		fprintf (stderr, "%s: Old and new y_min do not differ by N * dy\n", GMT_program);
		exit (EXIT_FAILURE);
	}
	if (GMT_minmaxinc_verify (n_new, header.y_max, header.y_inc, GMT_SMALL) == 1) {
		fprintf (stderr, "%s: Old and new y_max do not differ by N * dy\n", GMT_program);
		exit (EXIT_FAILURE);
	}

	GMT_grd_init (&header, argc, argv, TRUE);

	w_old = header.x_min;	e_old = header.x_max;
	s_old = header.y_min;	n_old = header.y_max;
	nx_old = header.nx;	ny_old = header.ny;
	one_or_zero = (header.node_offset) ? 0 : 1;
	nx_new = irint ((e_new - w_new) / header.x_inc) + one_or_zero;
	ny_new = irint ((n_new - s_new) / header.y_inc) + one_or_zero;

	nm = GMT_get_nm (nx_new, ny_new);
	
	grd = (float *) GMT_memory (VNULL, (size_t)nm, sizeof (float), GMT_program);
	GMT_err_fail (GMT_read_grd (grd_in, &header, grd, w_new, e_new, s_new, n_new, GMT_pad, FALSE), grd_in);

	if (gmtdefs.verbose) {
		sprintf (format, "\t%s\t%s\t%s\t%s\t%s\t%s\t%%ld\t%%ld\n", gmtdefs.d_format, gmtdefs.d_format,
			gmtdefs.d_format, gmtdefs.d_format, gmtdefs.d_format, gmtdefs.d_format);
		fprintf (stderr, "%s: File spec:\tW E S N dx dy nx ny:\n", GMT_program);
		fprintf (stderr, "%s: Old:", GMT_program);
		fprintf (stderr, format, w_old, e_old, s_old, n_old, header.x_inc, header.y_inc, nx_old, ny_old);
		fprintf (stderr, "%s: New:", GMT_program);
		fprintf (stderr, format, w_new, e_new, s_new, n_new, header.x_inc, header.y_inc, nx_new, ny_new);
	}

	GMT_err_fail (GMT_write_grd (Ctrl->G.file, &header, grd, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE), Ctrl->G.file);

	GMT_free ((void *) grd);

	Free_grdcut_Ctrl (Ctrl);	/* Deallocate control structure */

	GMT_end (argc, argv);

	exit (EXIT_SUCCESS);
}
コード例 #3
0
ファイル: grdproject.c プロジェクト: likueimo/ubuntu_gmt4
int main (int argc, char **argv)
{
	GMT_LONG error = FALSE, set_n = FALSE, shift_xy = FALSE, offset;

	char *infile = NULL, format[BUFSIZ], unit_name[GRD_UNIT_LEN], scale_unit_name[GRD_UNIT_LEN];

	GMT_LONG i, j, unit = 0, n_read;
	
	GMT_LONG nm;
	
	float *geo = NULL, *rect = NULL;
	
	double w, e, s, n;
	double xmin, xmax, ymin, ymax, inch_to_unit, unit_to_inch, fwd_scale, inv_scale;

	struct GRD_HEADER g_head, r_head;
	struct GMT_EDGEINFO edgeinfo;
	struct GRDPROJECT_CTRL *Ctrl = NULL;

	void *New_grdproject_Ctrl (), Free_grdproject_Ctrl (struct GRDPROJECT_CTRL *C);

	argc = (int)GMT_begin (argc, argv);

	Ctrl = (struct GRDPROJECT_CTRL *)New_grdproject_Ctrl ();	/* Allocate and initialize a new control structure */
	
	infile = CNULL;
	w = e = s = n = 0.0;

	for (i = 1; i < argc; i++) {
		if (argv[i][0] == '-') {
			switch (argv[i][1]) {
				/* Common parameters */

				case 'J':
				case 'R':
				case 'V':
				case '\0':
					error += GMT_parse_common_options (argv[i], &w, &e, &s, &n);
					break;

				/* Supplemental parameters */

				case 'C':
					Ctrl->C.active = TRUE;
					if (argv[i][2]) {	/* Also gave shifts */
	 					n_read = sscanf (&argv[i][2], "%lf/%lf", &Ctrl->C.easting, &Ctrl->C.northing);
						if (n_read != 2) {
							fprintf (stderr, "%s: GMT SYNTAX ERROR.  Expected -C[<false_easting>/<false_northing>]\n", GMT_program);
							error++;
						}
					}
					break;
				case 'D':
					Ctrl->D.active = TRUE;
					if (GMT_getinc (&argv[i][2], &Ctrl->D.xinc, &Ctrl->D.yinc)) {
						GMT_inc_syntax ('D', 1);
						error = TRUE;
					}
					break;
				case 'E':
					Ctrl->E.active = TRUE;
					Ctrl->E.dpi = atoi (&argv[i][2]);
					break;
				case 'A':
					Ctrl->A.active = TRUE;
					Ctrl->A.unit = argv[i][2];
					break;
				case 'F':
					Ctrl->F.active = TRUE;
					break;
				case 'G':
					Ctrl->G.file = strdup (&argv[i][2]);
					break;
				case 'I':
					Ctrl->I.active = TRUE;
					break;
				case 'M':	/* Directly specify units */
					Ctrl->M.active = TRUE;
					Ctrl->M.unit = argv[i][2];
					break;
				case 'N':
					sscanf (&argv[i][2], "%" GMT_LL "d/%" GMT_LL "d", &Ctrl->N.nx, &Ctrl->N.ny);
					if (Ctrl->N.ny == 0) Ctrl->N.ny = Ctrl->N.nx;
					Ctrl->N.active = TRUE;
					break;
				case 'S':
					Ctrl->S.active = TRUE;
					for (j = 2; j < 5 && argv[i][j]; j++) {
						switch (argv[i][j]) {
							case '-':
								Ctrl->S.antialias = FALSE; break;
							case 'n':
								Ctrl->S.interpolant = BCR_NEARNEIGHBOR; break;
							case 'l':
								Ctrl->S.interpolant = BCR_BILINEAR; break;
							case 'b':
								Ctrl->S.interpolant = BCR_BSPLINE; break;
							case 'c':
								Ctrl->S.interpolant = BCR_BICUBIC; break;
							case '/':
								Ctrl->S.threshold = atof (&argv[i][j+1]);
								j = 5; break;
							default:
								fprintf (stderr, "%s: Warning: The -S option has changed meaning. Use -S[-]b|c|l|n[/threshold] to specify interpolation mode.\n", GMT_program);
								j = 5; break;
						}
					}
					break;
				default:
					error = TRUE;
					GMT_default_error (argv[i][1]);
					break;
			}
		}
		else 
			infile = argv[i];
	}

	if ((Ctrl->D.active + Ctrl->E.active + Ctrl->N.active) == 0) Ctrl->N.active = set_n = TRUE;

	if (argc == 1 || GMT_give_synopsis_and_exit) {
		fprintf (stderr, "grdproject %s - Project geographical grid to/from rectangular grid\n\n", GMT_VERSION);
		fprintf (stderr, "usage: grdproject <in_grdfile> -G<out_grdfile> %s\n", GMT_J_OPT);
		fprintf (stderr, "\t[-A[k|m|n|i|c|p]] [-C[<dx/dy>]] [-D%s] [-E<dpi>] [-F]\n", GMT_inc_OPT);
		fprintf (stderr, "\t[-I] [-Mc|i|m] [-N<nx/ny>] [%s]\n", GMT_Rgeo_OPT);
		fprintf (stderr, "\t[-S[-]b|c|l|n[/<threshold>]] [-V]\n\n");

		if (GMT_give_synopsis_and_exit) exit (EXIT_FAILURE);

		fprintf (stderr, "\t<in_grdfile> is data set to be transformed.\n");
		fprintf (stderr, "\t-G name of output grid.\n");
		GMT_explain_option ('J');
		fprintf (stderr, "\n\tOPTIONS:\n");
		fprintf (stderr, "\t-A force projected values to be in actual meters [Default uses the given map scale].\n");
		fprintf (stderr, "\t   Specify another unit by appending k (km), m (miles), n (nautical miles), i (inch), c (cm), or p (points).\n");
		fprintf (stderr, "\t-C coordinates relative to projection center [Default is relative to lower left corner].\n");
		fprintf (stderr, "\t   Optionally append dx/dy to add (or subtract if -I) (i.e., false easting & northing) [0/0].\n");
		GMT_inc_syntax ('D', 0);
		fprintf (stderr, "\t-E sets dpi for output grid.\n");
		fprintf (stderr, "\t-F toggle between pixel and grid registration [Default is same as input].\n");
		fprintf (stderr, "\t-I Inverse transformation from rectangular to geographical.\n");
		fprintf (stderr, "\t-M Temporarily reset MEASURE_UNIT to be c (cm), i (inch), m (meter), or p (point).\n");
		fprintf (stderr, "\t   Cannot be used if -A is set.\n");
		fprintf (stderr, "\t-N sets the number of nodes for the new grid.\n");
		fprintf (stderr, "\t   Only one of -D, -E, and -N can be specified!\n");
		fprintf (stderr, "\t   If none are specified, nx,ny of the input file is used.\n");
		GMT_explain_option ('R');
		fprintf (stderr, "\t-S Determines the interpolation mode (b = B-spline, c = bicubic, l = bilinear,\n");
		fprintf (stderr, "\t   n = nearest-neighbor) [Default: bicubic].\n");
		fprintf (stderr, "\t   Optionally, prepend - to switch off antialiasing [Default: on].\n");
		fprintf (stderr, "\t   Append /<threshold> to change the minimum weight in vicinity of NaNs. A threshold of\n");
		fprintf (stderr, "\t   1.0 requires all nodes involved in interpolation to be non-NaN; 0.5 will interpolate\n");
		fprintf (stderr, "\t   about half way from a non-NaN to a NaN node [Default: 0.5].\n");
		GMT_explain_option ('V');

		exit (EXIT_FAILURE);
	}

	GMT_check_lattice (&Ctrl->D.xinc, &Ctrl->D.yinc, &Ctrl->F.active, &Ctrl->D.active);

	if (!infile) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR:  Must specify input file\n", GMT_program);
		error++;
	}
	if (!Ctrl->G.file) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR -G option:  Must specify output file\n", GMT_program);
		error++;
	}
	/*if (!project_info.region_supplied) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR:  Must specify -R option\n", GMT_program);
		error++;
	}*/
	if ((Ctrl->M.active + Ctrl->A.active) == 2) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR:  Can specify only one of -A and -M\n", GMT_program);
		error++;
	}
	if ((Ctrl->D.active + Ctrl->E.active + Ctrl->N.active) != 1) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR:  Must specify only one of -D, -E, or -N\n", GMT_program);
		error++;
	}
	if (Ctrl->D.active && (Ctrl->D.xinc <= 0.0 || Ctrl->D.yinc < 0.0)) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR -D option.  Must specify positive increment(s)\n", GMT_program);
		error++;
	}
	if (Ctrl->N.active && !set_n && (Ctrl->N.nx <= 0 || Ctrl->N.ny <= 0)) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR -N option.  Must specify positive integers\n", GMT_program);
		error++;
	}
	if (Ctrl->E.active && Ctrl->E.dpi <= 0) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR -E option.  Must specify positive dpi\n", GMT_program);
		error++;
	}
	if (Ctrl->S.active && (Ctrl->S.threshold < 0.0 || Ctrl->S.threshold > 1.0)) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR -S option:  threshold must be in [0,1] range\n", GMT_program);
		error++;
	}

	if (error) exit (EXIT_FAILURE);

	if (Ctrl->M.active) GMT_err_fail (GMT_set_measure_unit (Ctrl->M.unit), "-M");
	shift_xy = !(Ctrl->C.easting == 0.0 && Ctrl->C.northing == 0.0);
	
	unit = GMT_check_scalingopt ('A', Ctrl->A.unit, scale_unit_name);
	GMT_init_scales (unit, &fwd_scale, &inv_scale, &inch_to_unit, &unit_to_inch, unit_name);

	if (Ctrl->I.active) {	/* Must flip the column types since in is Cartesian and out is geographic */
		GMT_io.out_col_type[0] = GMT_IS_LON;	GMT_io.out_col_type[1] = GMT_IS_LAT;	/* Inverse projection expects x,y and gives lon, lat */
		GMT_io.in_col_type[0] = GMT_io.in_col_type[1] = GMT_IS_FLOAT;
		project_info.degree[0] = project_info.degree[1] = FALSE;
	}

	if (!project_info.region_supplied) {
		double ww, ee, ss, nn;
		char opt_R[BUFSIZ];
		struct GRD_HEADER head;
		if (project_info.projection == GMT_UTM && project_info.utm_hemisphere == 0) {
			fprintf (stderr, "%s: GMT SYNTAX ERROR -JU|u option: When -R is not provided you have to specify the hemisphere\n", GMT_program);
			exit (EXIT_FAILURE);
		}
		GMT_err_fail (GMT_read_grd_info (infile, &head), infile);
		w = head.x_min;		e = head.x_max;
		s = head.y_min;		n = head.y_max;
		if (!Ctrl->I.active) {
			sprintf (opt_R, "-R%.12f/%.12f/%.12f/%.12f", w, e, s, n);
			GMT_parse_R_option (opt_R, &ww, &ee, &ss, &nn);
			GMT_err_fail (GMT_map_setup (w, e, s, n), "");
		}
		else {			/* Do inverse transformation */
			double x_c, y_c, lon_t, lat_t;
			/* Obtain a first crude estimation of the good -R */
			x_c = (w + e) / 2.; 		/* mid point of projected coords */
			y_c = (s + n) / 2.;
			if (project_info.projection == GMT_UTM && !project_info.north_pole && y_c > 0) y_c *= -1;
			if (y_c > 0)
				GMT_parse_R_option ("-R-180/180/0/80", &ww, &ee, &ss, &nn);
			else
				GMT_parse_R_option ("-R-180/180/-80/0", &ww, &ee, &ss, &nn);
			if (project_info.projection == GMT_UTM && !project_info.north_pole && y_c < 0) y_c *= -1;	/* Undo the *-1 (only for the UTM case) */
			if (shift_xy) {
				x_c -= Ctrl->C.easting;
				y_c -= Ctrl->C.northing;
			}
			/* Convert from 1:1 scale */ 
			if (unit) {
				x_c *= fwd_scale;
				y_c *= fwd_scale;
			}

			GMT_err_fail (GMT_map_setup (ww, ee, ss, nn), "");

			x_c *= project_info.x_scale;
			y_c *= project_info.y_scale;

			if (Ctrl->C.active) {	/* Then correct so lower left corner is (0,0) */
				x_c += project_info.x0;
				y_c += project_info.y0;
			}
			GMT_xy_to_geo (&lon_t, &lat_t, x_c, y_c);
			sprintf (opt_R, "-R%.12f/%.12f/%.12f/%.12f", lon_t-1, lon_t+1, lat_t-1, lat_t+1);
			if (gmtdefs.verbose) fprintf (stderr, "First opt_R\t %s\t%g\t%g\n", opt_R, x_c, y_c);
			GMT_parse_R_option (opt_R, &ww, &ee, &ss, &nn);
			project_info.region = 0;	/* We need to reset this to not fall into non-wanted branch deeper down */
			GMT_err_fail (GMT_map_setup (ww, ee, ss, nn), "");

			/* Finally obtain the good limits */
			if (shift_xy) {
				w -= Ctrl->C.easting;	e -= Ctrl->C.easting;
				s -= Ctrl->C.northing;	n -= Ctrl->C.northing;
			}
			if (unit) {
				w *= fwd_scale;		e *= fwd_scale;
				s *= fwd_scale;		n *= fwd_scale;
			}
			w *= project_info.x_scale;	e *= project_info.x_scale;
			s *= project_info.y_scale;	n *= project_info.y_scale;

			if (Ctrl->C.active) {
				w += project_info.x0;	e += project_info.x0;
				s += project_info.y0;	n += project_info.y0;
			}

			GMT_xy_to_geo (&ww, &ss, w, s);		/* SW corner */
			GMT_xy_to_geo (&ee, &nn, e, n);		/* NE corner */
			sprintf (opt_R, "-R%.12f/%.12f/%.12f/%.12fr", ww, ss, ee, nn);
			if (gmtdefs.verbose) fprintf (stderr, "Second opt_R\t %s\n", opt_R);
			GMT_parse_common_options (opt_R, &ww, &ee, &ss, &nn);
			w = ww;		e = ee;
			s = ss;		n = nn;
		}
	}

	GMT_err_fail (GMT_map_setup (w, e, s, n), "");

	xmin = (Ctrl->C.active) ? project_info.xmin - project_info.x0 : project_info.xmin;
	xmax = (Ctrl->C.active) ? project_info.xmax - project_info.x0 : project_info.xmax;
	ymin = (Ctrl->C.active) ? project_info.ymin - project_info.y0 : project_info.ymin;
	ymax = (Ctrl->C.active) ? project_info.ymax - project_info.y0 : project_info.ymax;
	if (Ctrl->A.active) {	/* Convert to chosen units */
		strncpy (unit_name, scale_unit_name, (size_t)GRD_UNIT_LEN);
		xmin /= project_info.x_scale;
		xmax /= project_info.x_scale;
		ymin /= project_info.y_scale;
		ymax /= project_info.y_scale;
		if (unit) {	/* Change the 1:1 unit used */
			xmin *= fwd_scale;
			xmax *= fwd_scale;
			ymin *= fwd_scale;
			ymax *= fwd_scale;
		}
	}
	else {	/* Convert inches to chosen MEASURE */
		xmin *= inch_to_unit;
		xmax *= inch_to_unit;
		ymin *= inch_to_unit;
		ymax *= inch_to_unit;
	}
	if (shift_xy) {
		xmin += Ctrl->C.easting;
		xmax += Ctrl->C.easting;
		ymin += Ctrl->C.northing;
		ymax += Ctrl->C.northing;
	}

	GMT_grd_init (&r_head, argc, argv, FALSE);
	GMT_grd_init (&g_head, argc, argv, FALSE);

	sprintf (format, "(%s/%s/%s/%s)", gmtdefs.d_format, gmtdefs.d_format, gmtdefs.d_format, gmtdefs.d_format);

	if (Ctrl->I.active) {	/* Transforming from rectangular projection to geographical */

		/* if (!project_info.region) d_swap (s, e); */  /* Got w/s/e/n, make into w/e/s/n */

		if (GMT_IS_AZIMUTHAL && project_info.polar) {	/* Watch out for polar cap grids */
			if (project_info.pole == -90.0) {	/* Covers S pole; implies 360 longitude range */
				w = -180.0;	e = +180.0;	n = MAX(s, n);	s = -90.0;
			}
			else if (project_info.pole == +90.0) {	/* Covers N pole; implies 360 longitude range */
				w = -180.0;	e = +180.0;	s = MIN(s, n);	n = +90.0;
			}
		}
		g_head.x_min = w;	g_head.x_max = e;	g_head.y_min = s;	g_head.y_max = n;

		GMT_err_fail (GMT_read_grd_info (infile, &r_head), infile);

		GMT_boundcond_init (&edgeinfo);
		nm = GMT_get_nm (4 + r_head.nx, 4 + r_head.ny);
		GMT_pad[0] = GMT_pad[1] = GMT_pad[2] = GMT_pad[3] = 2;

		rect = (float *) GMT_memory (VNULL, (size_t)nm, sizeof (float), GMT_program);
		GMT_err_fail (GMT_read_grd (infile, &r_head, rect, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE), infile);
		offset = r_head.node_offset;		/* Same as input */
		if (Ctrl->F.active) offset = !offset;	/* Toggle */
		if (set_n) {
			Ctrl->N.nx = r_head.nx;
			Ctrl->N.ny = r_head.ny;
		}
		GMT_err_fail (GMT_grdproject_init (&g_head, Ctrl->D.xinc, Ctrl->D.yinc, Ctrl->N.nx, Ctrl->N.ny, Ctrl->E.dpi, offset), Ctrl->G.file);
		nm = GMT_get_nm (g_head.nx, g_head.ny);
		geo = (float *) GMT_memory (VNULL, (size_t)nm, sizeof (float), GMT_program);
		if (gmtdefs.verbose) {
			fprintf (stderr, "%s: Transform ", GMT_program);
			fprintf (stderr, format, g_head.x_min, g_head.x_max, g_head.y_min, g_head.y_max);
			fprintf (stderr, " <-- ");
			fprintf (stderr, format, xmin, xmax, ymin, ymax);
			fprintf (stderr, " [%s]\n", unit_name);
		}

		/* Modify input rect header if -A, -C, -M have been set */

		if (shift_xy) {
			r_head.x_min -= Ctrl->C.easting;
			r_head.x_max -= Ctrl->C.easting;
			r_head.y_min -= Ctrl->C.northing;
			r_head.y_max -= Ctrl->C.northing;

		}
		if (Ctrl->A.active) {	/* Convert from 1:1 scale */
			if (unit) {	/* Undo the 1:1 unit used */
				r_head.x_min *= inv_scale;
				r_head.x_max *= inv_scale;
				r_head.y_min *= inv_scale;
				r_head.y_max *= inv_scale;
			}
			r_head.x_min *= project_info.x_scale;
			r_head.x_max *= project_info.x_scale;
			r_head.y_min *= project_info.y_scale;
			r_head.y_max *= project_info.y_scale;
		}
		else if (gmtdefs.measure_unit != GMT_INCH) {	/* Convert from inch to whatever */
			r_head.x_min *= unit_to_inch;
			r_head.x_max *= unit_to_inch;
			r_head.y_min *= unit_to_inch;
			r_head.y_max *= unit_to_inch;
		}
		if (Ctrl->C.active) {	/* Then correct so lower left corner is (0,0) */
			r_head.x_min += project_info.x0;
			r_head.x_max += project_info.x0;
			r_head.y_min += project_info.y0;
			r_head.y_max += project_info.y0;
		}
		r_head.x_inc = GMT_get_inc (r_head.x_min, r_head.x_max, r_head.nx, r_head.node_offset);
		r_head.y_inc = GMT_get_inc (r_head.y_min, r_head.y_max, r_head.ny, r_head.node_offset);

		GMT_grd_project (rect, &r_head, geo, &g_head, &edgeinfo, Ctrl->S.antialias, Ctrl->S.interpolant, Ctrl->S.threshold, TRUE);

		GMT_pad[0] = GMT_pad[1] = GMT_pad[2] = GMT_pad[3] = 0;
		sprintf (g_head.x_units, "longitude [degrees_east]");
		sprintf (g_head.y_units, "latitude [degrees_north]");
		GMT_err_fail (GMT_write_grd (Ctrl->G.file, &g_head, geo, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE), Ctrl->G.file);
	}
	else {	/* Forward projection from geographical to rectangular grid */

		GMT_err_fail (GMT_read_grd_info (infile, &g_head), infile);

		GMT_boundcond_init (&edgeinfo);
		nm = GMT_get_nm (4 + g_head.nx, 4 + g_head.ny);
		GMT_pad[0] = GMT_pad[1] = GMT_pad[2] = GMT_pad[3] = 2;

		geo = (float *) GMT_memory (VNULL, (size_t)nm, sizeof (float), GMT_program);
		GMT_err_fail (GMT_read_grd (infile, &g_head, geo, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE), infile);

		r_head.x_min = project_info.xmin;	r_head.x_max = project_info.xmax;
		r_head.y_min = project_info.ymin;	r_head.y_max = project_info.ymax;
		if (Ctrl->A.active) {	/* Convert from 1:1 scale */
			if (unit) {	/* Undo the 1:1 unit used */
				Ctrl->D.xinc *= inv_scale;
				Ctrl->D.yinc *= inv_scale;
			}
			Ctrl->D.xinc *= project_info.x_scale;
			Ctrl->D.yinc *= project_info.y_scale;
		}
		else if (gmtdefs.measure_unit != GMT_INCH) {	/* Convert from inch to whatever */
			Ctrl->D.xinc *= unit_to_inch;
			Ctrl->D.yinc *= unit_to_inch;
		}
		if (set_n) {
			Ctrl->N.nx = g_head.nx;
			Ctrl->N.ny = g_head.ny;
		}

		if (gmtdefs.verbose) {
			fprintf (stderr, "%s: Transform ", GMT_program);
			fprintf (stderr, format, g_head.x_min, g_head.x_max, g_head.y_min, g_head.y_max);
			fprintf (stderr, " --> ");
			fprintf (stderr, format, xmin, xmax, ymin, ymax);
			fprintf (stderr, " [%s]\n", unit_name);
		}

		offset = g_head.node_offset;		/* Same as input */
		if (Ctrl->F.active) offset = !offset;	/* Toggle */

		GMT_err_fail (GMT_grdproject_init (&r_head, Ctrl->D.xinc, Ctrl->D.yinc, Ctrl->N.nx, Ctrl->N.ny, Ctrl->E.dpi, offset), Ctrl->G.file);
		nm = GMT_get_nm (r_head.nx, r_head.ny);
		rect = (float *) GMT_memory (VNULL, (size_t)nm, sizeof (float), GMT_program);
		GMT_grd_project (geo, &g_head, rect, &r_head, &edgeinfo, Ctrl->S.antialias, Ctrl->S.interpolant, Ctrl->S.threshold, FALSE);

		/* Modify output rect header if -A, -C, -M have been set */

		if (Ctrl->C.active) {	/* Change origin from lower left to projection center */
			r_head.x_min -= project_info.x0;
			r_head.x_max -= project_info.x0;
			r_head.y_min -= project_info.y0;
			r_head.y_max -= project_info.y0;
		}
		if (Ctrl->A.active) {	/* Convert to 1:1 scale */
			r_head.x_min /= project_info.x_scale;
			r_head.x_max /= project_info.x_scale;
			r_head.y_min /= project_info.y_scale;
			r_head.y_max /= project_info.y_scale;
			if (unit) {	/* Change the 1:1 unit used */
				r_head.x_min *= fwd_scale;
				r_head.x_max *= fwd_scale;
				r_head.y_min *= fwd_scale;
				r_head.y_max *= fwd_scale;
			}
		}
		else if (gmtdefs.measure_unit != GMT_INCH) {	/* Convert from inch to whatever */
			r_head.x_min /= unit_to_inch;
			r_head.x_max /= unit_to_inch;
			r_head.y_min /= unit_to_inch;
			r_head.y_max /= unit_to_inch;
		}
		if (shift_xy) {
			r_head.x_min += Ctrl->C.easting;
			r_head.x_max += Ctrl->C.easting;
			r_head.y_min += Ctrl->C.northing;
			r_head.y_max += Ctrl->C.northing;

		}
		r_head.x_inc = GMT_get_inc (r_head.x_min, r_head.x_max, r_head.nx, r_head.node_offset);
		r_head.y_inc = GMT_get_inc (r_head.y_min, r_head.y_max, r_head.ny, r_head.node_offset);

		/* rect xy values are here in GMT projected units chosen by user */

		GMT_pad[0] = GMT_pad[1] = GMT_pad[2] = GMT_pad[3] = 0;
		strcpy (r_head.x_units, unit_name);
		strcpy (r_head.y_units, unit_name);
		GMT_err_fail (GMT_write_grd (Ctrl->G.file, &r_head, rect, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE), Ctrl->G.file);
	}

	GMT_free ((void *)geo);
	GMT_free ((void *)rect);

	Free_grdproject_Ctrl (Ctrl);	/* Deallocate control structure */

	GMT_end (argc, argv);

	exit (EXIT_SUCCESS);
}
コード例 #4
0
ファイル: ggrd_grdtrack_util.c プロジェクト: geodynamics/hc
int ggrd_grdtrack_init(double *west, double *east,
		       double *south, double *north, 
		       float **f,int *mm,char *grdfile,
		       struct GRD_HEADER **grd,
		       struct GMT_EDGEINFO **edgeinfo,
		       char *edgeinfo_string, 
		       ggrd_boolean *geographic_in,
		       int *pad,ggrd_boolean three_d, 
		       char *dfile, float **z,int *nz,		
		       ggrd_boolean interpolant,
		       ggrd_boolean verbose,
		       ggrd_boolean change_depth_sign,
		       struct BCR *loc_bcr)
#endif
{
  FILE *din;
  float dz1,dz2;
  struct GRD_HEADER ogrd;
  int i,one_or_zero,nx,ny,mx,my;
  char filename[BUFSIZ*2],*cdummy;
  static int gmt_init = FALSE;
  /* 
     deal with edgeinfo 
  */
  *edgeinfo = (struct GMT_EDGEINFO *)
    GMT_memory (VNULL, (size_t)1, sizeof(struct GMT_EDGEINFO), "ggrd_grdtrack_init");
  /* init with nonsense to avoid compiler warning */
  ogrd.x_min = ogrd.y_min =ogrd.x_max = ogrd.y_max = -100;
  ogrd.x_inc = ogrd.y_inc = -1;
  ogrd.node_offset = 0;ogrd.nx = ogrd.ny = -1;
#ifndef USE_GMT3

  if(!gmt_init){
    /* this should be OK as is. init only once globally */
    GMT_program = "ggrd";
    GMT_make_fnan (GMT_f_NaN);
    GMT_make_dnan (GMT_d_NaN);
    GMT_io_init ();/* Init the table i/o structure */
    GMT_grdio_init();
    if(strcmp(edgeinfo_string,"-fg")==0){
      GMT_io.in_col_type[GMT_X] = GMT_io.out_col_type[GMT_X] = GMT_IS_LON;
      GMT_io.in_col_type[GMT_Y] = GMT_io.out_col_type[GMT_Y] = GMT_IS_LAT;
    }
    if(strcmp(edgeinfo_string,"-fx")==0){
      GMT_io.in_col_type[GMT_X] = GMT_io.out_col_type[GMT_X] = GMT_IS_LON;
    }
    if(strcmp(edgeinfo_string,"-fy")==0){
      GMT_io.in_col_type[GMT_Y] = GMT_io.out_col_type[GMT_Y] = GMT_IS_LAT;
    }
    gmt_init = TRUE;
  }

#endif
  /* 
     init first edgeinfo (period/global?)
  */
  GMT_boundcond_init (*edgeinfo);
  /* check if geographic */
  if (strlen(edgeinfo_string)>2){ /* the boundary flag was set */
    /* parse */
    GMT_boundcond_parse (*edgeinfo, (edgeinfo_string+2));
    if ((*edgeinfo)->gn)
      *geographic_in = 1;
    else if((*edgeinfo)->nxp == -1)
      *geographic_in = 2;
    else
      *geographic_in = 0;
  }else{
    *geographic_in = 0;
  }
  if(verbose >= 2)
    if(*geographic_in)
      fprintf(stderr,"ggrd_grdtrack_init: detected geographic region from geostring: %s\n",
	      edgeinfo_string);
  
  *z = (float *) GMT_memory 
    (VNULL, (size_t)1, sizeof(float), "ggrd_grdtrack_init");
 
  if(three_d){
    /*
      
    three D part first
    
    */
    /* 
       init the layers
    */
    din = fopen(dfile,"r");
    if(!din){
      fprintf(stderr,"ggrd_grdtrack_init: could not open depth file %s\n",
	      dfile);
      return 1;
    }
    /* read in the layers */
    *nz = 0;
    dz1 = -1;
    while(fscanf(din,"%f",(*z+ (*nz))) == 1){ 
      if(change_depth_sign)
	*(*z+ (*nz)) = -(*(*z+ (*nz)));
      /* read in each depth layer */
      *z = (float *) GMT_memory ((void *)(*z), (size_t)((*nz)+2), sizeof(float), "ggrd_grdtrack_init");
      if(*nz > 0){		/* check for increasing layers */
	if(dz1 < 0){	
	  /* init first interval */
	  dz1 = *(*z+(*nz)) - *(*z+(*nz)-1);
	  dz2 = dz1;
	}else{
	  /* later intervals */
	  dz2 = *(*z+(*nz)) - *(*z+(*nz)-1);
	}
	if(dz2 <= 0.0){		/* check for monotonic increase */
	  fprintf(stderr,"%s: error: levels in %s have to increase monotonically: n: %i dz; %g\n",
		  "ggrd_grdtrack_init",dfile,*nz,dz2);
	  return 2;
	}
      }
      *nz += 1;
    }
    fclose(din);
    /* end layer init"ggrd_grdtrack_initialization */
    if(*nz < 2){
      fprintf(stderr,"%s: error: need at least two layers in %s\n",
	      "ggrd_grdtrack_init", dfile);
      return 3;
    }
    if(verbose)
      fprintf(stderr,"%s: read %i levels from %s between zmin: %g and zmax: %g\n",
	      "ggrd_grdtrack_init",*nz,dfile,*(*z+0),*(*z+(*nz)-1));
  }else{
    *nz = 1;
    *(*z) = 0.0;
    if(verbose >= 2)
      fprintf(stderr,"ggrd_grdtrack_init: single level at z: %g\n",*(*z));
  }
  /* 
     get nz grd and edgeinfo structures 
  */
  *grd = (struct GRD_HEADER *)
    GMT_memory (NULL, (size_t)(*nz), sizeof(struct GRD_HEADER), "ggrd_grdtrack_init");
  *edgeinfo = (struct GMT_EDGEINFO *)
    GMT_memory (*edgeinfo, (size_t)(*nz), sizeof(struct GMT_EDGEINFO), "ggrd_grdtrack_init");
  if(verbose >= 2)
    fprintf(stderr,"ggrd_grdtrack_init: mem alloc ok\n");
#ifndef USE_GMT3  
  /* init the header */
  GMT_grd_init (*grd,0,&cdummy,FALSE);
#endif
  if(*nz == 1){
    if(verbose >= 2)
      
#ifdef USE_GMT3		/* old */
      fprintf(stderr,"ggrd_grdtrack_init: opening single file %s, GMT3 mode\n",grdfile);
    if (GMT_cdf_read_grd_info (grdfile,(*grd))) {
      fprintf (stderr, "%s: error opening file %s\n", 
	       "ggrd_grdtrack_init", grdfile);
      return 4;
    }
   
#else  /* >=4.1.2 */
    if(verbose >= 2)
      fprintf(stderr,"ggrd_grdtrack_init: opening single file %s, GMT4 mode\n",grdfile);
    if(GMT_read_grd_info (grdfile,*grd)){
      fprintf (stderr, "%s: error opening file %s for header\n", 
	       "ggrd_grdtrack_init", grdfile);
      return 4;
    }
#endif 
  }else{
    /* loop through headers for testing purposess */
    for(i=0;i<(*nz);i++){
      sprintf(filename,"%s.%i.grd",grdfile,i+1);
#ifdef USE_GMT3
      if (GMT_cdf_read_grd_info (filename, (*grd+i))) {
	fprintf (stderr, "%s: error opening file %s (-D option was used)\n", 
		 "ggrd_grdtrack_init", filename);
	return 6;
      }
#else  /* gmt 4 */
      if (GMT_read_grd_info (filename,(*grd+i))) {
	fprintf (stderr, "%s: error opening file %s (-D option was used)\n", 
		 "ggrd_grdtrack_init", filename);
	return 6;
      }
#endif
      if(i == 0){
	/* save the first grid parameters */
	ogrd.x_min = (*grd)[0].x_min;
	ogrd.y_min = (*grd)[0].y_min;
	ogrd.x_max = (*grd)[0].x_max;
	ogrd.y_max = (*grd)[0].y_max;
	ogrd.x_inc = (*grd)[0].x_inc;
	ogrd.y_inc = (*grd)[0].y_inc;
	ogrd.node_offset = (*grd)[0].node_offset;
	ogrd.nx = (*grd)[0].nx;
	ogrd.ny = (*grd)[0].ny;
	/* 
	   
	make sure we are in 0 ... 360 system

	*/
	if((ogrd.x_min < 0)||(ogrd.x_max<0)){
	  fprintf(stderr,"%s: WARNING: geographic grids should be in 0..360 lon system (found %g - %g)\n",
		  "ggrd_grdtrack_init",ogrd.x_min,ogrd.x_max);
	}
      }else{
	/* test */
	if((fabs(ogrd.x_min -  (*grd)[i].x_min)>5e-7)||
	   (fabs(ogrd.y_min -  (*grd)[i].y_min)>5e-7)||
	   (fabs(ogrd.x_max -  (*grd)[i].x_max)>5e-7)||
	   (fabs(ogrd.y_max -  (*grd)[i].y_max)>5e-7)||
	   (fabs(ogrd.x_inc -  (*grd)[i].x_inc)>5e-7)||
	   (fabs(ogrd.y_inc -  (*grd)[i].y_inc)>5e-7)||
	   (fabs(ogrd.nx    -  (*grd)[i].nx)>5e-7)||
	   (fabs(ogrd.ny    -  (*grd)[i].ny)>5e-7)||
	   (fabs(ogrd.node_offset - (*grd)[i].node_offset)>5e-7)){
	  fprintf(stderr,"%s: error: grid %i out of %i has different dimensions or setting from first\n",
		 "ggrd_grdtrack_init",i+1,(*nz));
	  return 8;
	}
      }
    }
  }
  if(verbose > 2)
    fprintf(stderr,"ggrd_grdtrack_init: read %i headers OK, grids appear to be same size\n",*nz);
  if (fabs(*west - (*east)) < 5e-7) {	/* No subset asked for , west same as east*/
    *west =  (*grd)[0].x_min;
    *east =  (*grd)[0].x_max;
    *south = (*grd)[0].y_min;
    *north = (*grd)[0].y_max;
  }
  one_or_zero = ((*grd)[0].node_offset) ? 0 : 1;
  nx = irint ( (*east - *west) / (*grd)[0].x_inc) + one_or_zero;
  ny = irint ( (*north - *south) / (*grd)[0].y_inc) + one_or_zero;
  /* real size of data */
  //nn = nx * ny;

  /* padded */
  mx = nx + 4;
  my = ny + 4;
  /* 
     get space for all layers
  */
  *mm = mx * my;

  *f = (float *) calloc((*mm) * (*nz) ,sizeof (float));
  if(!(*f)){
    fprintf(stderr,"ggrd_grdtrack_init: f memory error, mm: %i (%i by %i) by nz: %i \n",*mm,mx,my, *nz);
    return 9;
  }
  if(verbose >= 2){
    fprintf(stderr,"ggrd_grdtrack_init: mem alloc 2 ok, %g %g %g %g %i %i\n",
	    *west,*east,*south,*north,nx,ny);
  }
  /* 
     pad on sides 
  */
  pad[0] = pad[1] = pad[2] = pad[3] = 2;
  for(i=0;i < (*nz);i++){
    /* 
       loop through layers
    */
    if(i != 0)			/* copy first edgeinfo over */
      memcpy((*edgeinfo+i),(*edgeinfo),sizeof(struct GMT_EDGEINFO));
    if((*nz) == 1){
      sprintf(filename,"%s",grdfile);
    }else{			/* construct full filename */
      sprintf(filename,"%s.%i.grd",grdfile,i+1);
    }
    if (verbose) 
      fprintf(stderr,"ggrd_grdtrack_init: reading grd file %s (%g - %g (%i) %g - %g (%i); geo: %i flag: %s\n",
	      filename,*west,*east,nx,*south,*north,ny,
	      *geographic_in,edgeinfo_string);

    /* 
       read the grd files
    */
#ifndef USE_GMT3
    /* GMT 4 */
    if (GMT_read_grd (filename,(*grd+i), (*f+i* (*mm)), 
		      *west, *east, *south, *north, 
		      pad, FALSE)) {
      fprintf (stderr, "%s: error reading file %s\n", "ggrd_grdtrack_init", grdfile);
      return 10;
    }
    //fprintf(stderr,"%g %g %i %i %i %i\n",(*grd)->z_scale_factor,(*grd)->z_add_offset,nx,ny,mx,my);
#else
    /* old GMT */
    if (GMT_cdf_read_grd (filename, (*grd+i), (*f+i* (*mm)), 
			  *west, *east, *south, *north, 
			  pad, FALSE)) {
      fprintf (stderr, "%s: error reading file %s\n", "ggrd_grdtrack_init", grdfile);
      return 10;
    }
#endif
    /* 
       prepare the boundaries 
    */
    GMT_boundcond_param_prep ((*grd+i), (*edgeinfo+i));
    if(i == 0){
      
      /* 
	 Initialize bcr structure, this can be the same for 
	 all grids as long as they have the same dimensions

      */
#ifndef USE_GMT3
      GMT_bcr_init ((*grd+i), pad, interpolant,1.0,loc_bcr);
#else
      my_GMT_bcr_init ((*grd+i), pad, interpolant,loc_bcr);
#endif
     }
    /* Set boundary conditions  */
    GMT_boundcond_set ((*grd+i), (*edgeinfo+i), pad, 
		       (*f+i*(*mm)));
  } /* end layer loop */
  if(verbose){
    ggrd_print_layer_avg(*f,*z,mx,my,*nz,stderr,pad);
  }
  return 0;

}
コード例 #5
0
int main(int argc, char **argv) {

char *coeff_x, *coeff_x2, file[BUFSIZ];

struct GRD_HEADER grd_x, grd_x2;
struct GMT_EDGEINFO edgeinfo_x, edgeinfo_x2;
struct GMT_BCR bcr_x, bcr_x2;

int  *idx, n, ns, ntr, istart, istop, index;
float scale_factor;
double *x, *y, dt, zero, factor, rns;
double ri, **A, *B, d, sum;
double water_depth, error, rx, ry;
double value_coeff_x, value_coeff_x2, water_velocity, x_loc, y_loc;
short check, verbose, intercept;
register int i, j, k, l;

initargs(argc, argv);
argc = GMT_begin (argc, argv);

if (!getparint("n", &n)) n = 4;
if (!getparshort("intercept", &intercept)) intercept = 0;
if (!getparshort("verbose", &verbose)) verbose = 1;

if (!getparstring("coeff_x", &coeff_x)) coeff_x="/home/user/Field/GRIDS/wb.twt.sm2500.new.grd";
if (!getparstring("coeff_x2", &coeff_x2)) coeff_x2="/home/user/Field/HORIZONS/DC_Base_Reservoir.reform.dat.trimmed.sample.smooth.grd";

if ( verbose ) {
   fprintf ( stderr, "\n" );
   fprintf ( stderr, "WB TWT GMT grid file name = %s\n", coeff_x );
   fprintf ( stderr, "Bottom Horizon GMT grid file name = %s\n", coeff_x2 );
}

GMT_boundcond_init (&edgeinfo_x);
GMT_boundcond_init (&edgeinfo_x2);

GMT_grd_init (&grd_x,  argc, argv, FALSE);
GMT_grd_init (&grd_x2, argc, argv, FALSE);

if (GMT_read_grd_info (coeff_x,  &grd_x))  fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);
if (GMT_read_grd_info (coeff_x2, &grd_x2)) fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);

f1 = (float *) GMT_memory (VNULL, (size_t)((grd_x.nx  + 4) * (grd_x.ny  + 4)), sizeof(float), GMT_program);
f2 = (float *) GMT_memory (VNULL, (size_t)((grd_x2.nx + 4) * (grd_x2.ny + 4)), sizeof(float), GMT_program);

GMT_pad[0] = GMT_pad[1] = GMT_pad[2] = GMT_pad[3] = 2;

GMT_boundcond_param_prep (&grd_x,  &edgeinfo_x);
GMT_boundcond_param_prep (&grd_x2, &edgeinfo_x2);

GMT_boundcond_set (&grd_x, &edgeinfo_x, GMT_pad, f1);
GMT_boundcond_set (&grd_x2, &edgeinfo_x2, GMT_pad, f2);

GMT_bcr_init (&grd_x,  GMT_pad, BCR_BSPLINE, 1, &bcr_x);
GMT_bcr_init (&grd_x2, GMT_pad, BCR_BSPLINE, 1, &bcr_x2);

GMT_read_grd (coeff_x,  &grd_x,  f1, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);
GMT_read_grd (coeff_x2, &grd_x2, f2, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);

ntr = gettra (&tr, 0);
ns  = tr.ns;
dt  = tr.dt * 0.001;
scale_factor = tr.scalco;
if (scale_factor < 0.0 ) scale_factor *= -1.0;
if (scale_factor == 0.0 ) scale_factor = 1.0;

zero = 0.0;
factor = 0.0005;
rns = ns;

if ( verbose ) {
   fprintf ( stderr, "\n" );
   fprintf ( stderr, "Degree of Polynomial = %d\n", n );
   fprintf ( stderr, "Intercept = %d\n", intercept );
   fprintf ( stderr, "Number of input traces = %d\n", ntr );
   fprintf ( stderr, "Number of samples per trace = %d\n", ns );
   fprintf ( stderr, "Sample rate = %f ms.\n", dt );
   fprintf ( stderr, "Scale Factor for X and Y Coordinates = %f\n", scale_factor );
   fprintf ( stderr, "\n" );
}

A = dmatrix (1,n,1,n);
B = dvector (1,n);

x = ealloc1double (ns);
y = ealloc1double (ns);

rewind (stdin);
for (l=0;l<ntr;l++) {
   gettr (&tr);
   x_loc = tr.sx = nint ( tr.sx / scale_factor );
   y_loc = tr.sy = nint ( tr.sy / scale_factor );

   check = 0;
   istop = ns;
   if ( x_loc >= grd_x.x_min && x_loc <= grd_x.x_max && y_loc >= grd_x.y_min && y_loc <= grd_x.y_max ) check = 1;

   if ( check ) {
      value_coeff_x  = GMT_get_bcr_z (&grd_x,  x_loc, y_loc, f1, &edgeinfo_x,  &bcr_x);
      value_coeff_x2 = GMT_get_bcr_z (&grd_x2, x_loc, y_loc, f2, &edgeinfo_x2, &bcr_x2);
      if (GMT_is_dnan (value_coeff_x) || GMT_is_dnan (value_coeff_x2)) {
         check = 0;
      } else {
         if ( value_coeff_x < 0.0 ) value_coeff_x *= -1.0;
         if ( value_coeff_x2 < 0.0 ) value_coeff_x2 *= -1.0;
         value_coeff_x2 += 100.0;
         istop = nint ( value_coeff_x2 / dt );
         istop = max ( min ( istop, ns ), 0 ); 
         if ( tr.wevel > 0 ) {
            water_velocity = tr.wevel;
         } else {
            index = max ( min ( nint(value_coeff_x/dt), ns ), 0 );
            water_velocity = tr.data[index];
         }
         water_depth = water_velocity * value_coeff_x * factor;
      }
   }

   if ( verbose == 3 && check )  {
      fprintf ( stderr, "input trace = %6d, xloc = %8.2f yloc = %8.2f, WB TWT = %8.2f ms., WB Depth = %10.4f meters, Water Velocity = %8.2f mps, Bottom Horizon = %8.2f\n",
      l, x_loc, y_loc, value_coeff_x, water_depth, water_velocity, value_coeff_x2 );
   }

   if ( check ) {
      for (k=0;k<=istop;k++) {
         x[k] = k * dt;
         y[k] = ( ( tr.data[k] * x[k] ) * factor ) - water_depth; 
         x[k] -= value_coeff_x;
      }

      istart = 0;
      for (k=0;k<=istop;k++) {
         if ( x[k] >= zero && y[k] >= zero ) {
            istart = k;
            break;
         }
      }

      if ( verbose == 3 ) fprintf ( stderr, "istart = %d, istop = %d\n", istart, istop );

      for (i=1;i<=n;i++) {
         for (j=1;j<=n;j++) {
            sum = zero;
            ri = i+j-2;
            for (k=istart;k<=istop;k++) sum += pow ( x[k], ri );
            if (i>1&&j==1&&intercept==0) sum = zero;
            A[i][j] = sum;
         }
      }

      for (i=1;i<=n;i++) {
         sum = zero;
         ri = i - 1;
         for (j=istart;j<=istop;j++) sum += pow( x[j], ri ) * y[j];
         B[i] = sum;
      }

      idx  = ivector (1, n); 
      ludcmp ( A, n, idx, &d ); 
      lubksb ( A, n, idx, B ); 

      error = ry = zero;
      if ( intercept ) {
         for ( i=istart; i<=istop; i++ ) {
            rx = x[i];
            ry = B[1]; 
            for (j=2;j<=n;j++) ry += B[j] * pow ( rx, j-1 );
            error += abs ( ry - y[i] );
            if ( verbose == 2 ) fprintf ( stderr, "%-6d %12.2f %12.2f %12.2f %12.4f %12.4f\n", i, rx, y[i], ry, ry - y[i], error );
         }
      } else {
         for ( i=istart; i<=istop; i++ ) {
            rx = x[i];
            ry = zero;
            for (j=1;j<=n;j++) ry += B[j] * pow ( rx, j-1 );
            error += abs ( ry - y[i] );
            if ( verbose == 2 ) fprintf ( stderr, "%-6d %12.2f %12.2f %12.2f %12.4f %12.4f\n", i, rx, y[i], ry, ry - y[i], error );
         }
      }

      rns = istop - istart + 1;
      if ( verbose ) {
         if ( intercept ) {
            for (i=1;i<=n;i++) printf ( "coefficient number = %5d, Solution vector = %30.25f, Average Error = %12.4f\n", i, B[i], error / rns );
         } else {
            for (i=2;i<=n;i++) printf ( "coefficient number = %5d, Solution vector = %30.25f, Average Error = %12.4f\n", i, B[i], error / rns );
         }
      } else {
         printf ( "%-12.2f %12.2f ", x_loc, y_loc );
         if ( intercept ) {
            for (i=1;i<n;i++) printf ( "%30.25f ", B[i] );
         } else {
            for (i=2;i<n;i++) printf ( "%30.25f ", B[i] );
         }
         printf ( "%30.25f %12.4f %12.4f %12.4f %12.4f %12.4f\n", B[n], error / rns, water_velocity, water_depth, value_coeff_x, value_coeff_x2 );
      }
   }
}

free_dmatrix (A,1,n,1,n);
free_dvector (B,1,n);

free1double (x);
free1double (y);

GMT_free ((void *)f1);
GMT_free ((void *)f2);

GMT_end (argc, argv);

return EXIT_SUCCESS;

}
コード例 #6
0
int main(int argc, char **argv) {

   char *coeff_x, *coeff_x2, *coeff_x3, *coeff_x4, file[BUFSIZ];
   struct GRD_HEADER grd_x, grd_x2, grd_x3, grd_x4;
   struct GMT_EDGEINFO edgeinfo_x, edgeinfo_x2, edgeinfo_x3, edgeinfo_x4;
   struct GMT_BCR bcr_x, bcr_x2, bcr_x3, bcr_x4;

   double x_loc, y_loc, value_coeff_x, value_coeff_x2, value_coeff_x3, value_coeff_x4;

   char temp[256];
   cwp_String pfile;
   FILE *fpp;
   short  verbose, check;
   int    kount, nump1;
   double sum, error1, sign;
   double delta, thresh;
   double *vzero_opt, *k_opt;
   double *datain, *samp, *xloc_array, *yloc_array;
   double depth_water, factor, num, sample;
   double vzero, k;
   double *wb_twt_array, *wb_z_array;
   double error, x, y, zero;
   register int i;

   initargs(argc, argv);
   argc = GMT_begin (argc, argv);

   if (!getparstring("pfile",&pfile)) pfile = "tops.lis";
   if (!getparshort("verbose", &verbose)) verbose = 1;
   if (!getpardouble("delta", &delta)) delta = 0.00001;
   if (!getpardouble("thresh", &thresh)) thresh = 0.01;

   zero = 0.0;

   if (!getparstring("coeff_x", &coeff_x)) coeff_x="wb.twt.grd";
   if (!getparstring("coeff_x2", &coeff_x2)) coeff_x2="wvavg.dat.trimmed.sample.smooth.grd";
   if (!getparstring("coeff_x3", &coeff_x3)) coeff_x3="vzero.seismic.dat.trimmed.sample.smooth.grd";
   if (!getparstring("coeff_x4", &coeff_x4)) coeff_x4="k.seismic.dat.trimmed.sample.smooth.grd";

   if ( verbose ) {
      fprintf ( stderr, "\n" );
      fprintf ( stderr, "WB TWT (ms.) GMT grid file name = %s\n", coeff_x );
      fprintf ( stderr, "WB VAVG (m)  GMT grid file name = %s\n", coeff_x2 );
      fprintf ( stderr, "Seismic VZERO GMT grid file name = %s\n", coeff_x3 );
      fprintf ( stderr, "Seismic K GMT grid file name = %s\n", coeff_x4 );
      fprintf ( stderr, "Delta = %.10f\n", delta );
      fprintf ( stderr, "Threshold = %f\n", thresh );
   }

   GMT_boundcond_init (&edgeinfo_x);
   GMT_boundcond_init (&edgeinfo_x2);
   GMT_boundcond_init (&edgeinfo_x3);
   GMT_boundcond_init (&edgeinfo_x4);

   GMT_grd_init (&grd_x,  argc, argv, FALSE);
   GMT_grd_init (&grd_x2, argc, argv, FALSE);
   GMT_grd_init (&grd_x3, argc, argv, FALSE);
   GMT_grd_init (&grd_x4, argc, argv, FALSE);

   if (GMT_read_grd_info (coeff_x,  &grd_x))  fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);
   if (GMT_read_grd_info (coeff_x2, &grd_x2)) fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);
   if (GMT_read_grd_info (coeff_x3, &grd_x3)) fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);
   if (GMT_read_grd_info (coeff_x4, &grd_x4)) fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);

   f1 = (float *) GMT_memory (VNULL, (size_t)((grd_x.nx  + 4) * (grd_x.ny  + 4)), sizeof(float), GMT_program);
   f2 = (float *) GMT_memory (VNULL, (size_t)((grd_x2.nx + 4) * (grd_x2.ny + 4)), sizeof(float), GMT_program);
   f3 = (float *) GMT_memory (VNULL, (size_t)((grd_x3.nx + 4) * (grd_x3.ny + 4)), sizeof(float), GMT_program);
   f4 = (float *) GMT_memory (VNULL, (size_t)((grd_x4.nx + 4) * (grd_x4.ny + 4)), sizeof(float), GMT_program);

   GMT_pad[0] = GMT_pad[1] = GMT_pad[2] = GMT_pad[3] = 2;

   GMT_boundcond_param_prep (&grd_x,  &edgeinfo_x);
   GMT_boundcond_param_prep (&grd_x2, &edgeinfo_x2);
   GMT_boundcond_param_prep (&grd_x3, &edgeinfo_x3);
   GMT_boundcond_param_prep (&grd_x4, &edgeinfo_x4);

   GMT_boundcond_set (&grd_x, &edgeinfo_x, GMT_pad, f1);
   GMT_boundcond_set (&grd_x2, &edgeinfo_x2, GMT_pad, f2);
   GMT_boundcond_set (&grd_x3, &edgeinfo_x3, GMT_pad, f3);
   GMT_boundcond_set (&grd_x4, &edgeinfo_x4, GMT_pad, f4);

   GMT_bcr_init (&grd_x,  GMT_pad, BCR_BSPLINE, 1, &bcr_x);
   GMT_bcr_init (&grd_x2, GMT_pad, BCR_BSPLINE, 1, &bcr_x2);
   GMT_bcr_init (&grd_x3, GMT_pad, BCR_BSPLINE, 1, &bcr_x3);
   GMT_bcr_init (&grd_x4, GMT_pad, BCR_BSPLINE, 1, &bcr_x4);

   GMT_read_grd (coeff_x,  &grd_x,  f1, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);
   GMT_read_grd (coeff_x2, &grd_x2, f2, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);
   GMT_read_grd (coeff_x3, &grd_x3, f3, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);
   GMT_read_grd (coeff_x4, &grd_x4, f4, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);

   fpp = efopen (pfile, "r");

   kount = 0;
   while (NULL != fgets ( temp, sizeof(temp), fpp )) {
      ++kount;
      (void) sscanf ( ((&(temp[0]))), "%lf%lf%lf%lf", &x_loc, &y_loc, &num, &sample );
   }

   if ( verbose != 0) {
      fprintf (stderr,"\n");
      fprintf (stderr,"Data file name = %s, number of input samples = %d\n", pfile, kount);
      fprintf (stderr,"\n");
   }

   samp       = ealloc1double ( kount );
   datain     = ealloc1double ( kount );
   xloc_array = ealloc1double ( kount );
   yloc_array = ealloc1double ( kount );
   vzero_opt  = ealloc1double ( kount );
   k_opt      = ealloc1double ( kount );

   wb_twt_array = ealloc1double ( kount );
   wb_z_array   = ealloc1double ( kount );

   rewind ( fpp );
   kount = -1;
   while (NULL != fgets ( temp, sizeof(temp), fpp )) {
      ++kount;
      (void) sscanf ( ((&(temp[0]))), "%lf%lf%lf%lf", &x_loc, &y_loc, &num, &sample );
      xloc_array[kount] = x_loc;
      yloc_array[kount] = y_loc;
      samp[kount]   = num;
      datain[kount] = sample;
      if ( verbose == 2 ) fprintf ( stderr, "kount = %5d, x_loc = %12.2f, y_loc = %12.2f, num = %8.2f, sample = %8.2f\n", kount, xloc_array[kount], yloc_array[kount], samp[kount], datain[kount] );
   }
   if ( verbose == 2 ) fprintf ( stderr, "\n" );
   efclose (fpp);

   factor = 0.0005;

   nump1 = kount + 1;
   depth_water = error = zero;
   for ( i=0; i<=kount; i++ ) {
      check = 0;
      x_loc = xloc_array[i];
      y_loc = yloc_array[i];
      if ( x_loc >= grd_x.x_min && x_loc <= grd_x.x_max && y_loc >= grd_x.y_min && y_loc <= grd_x.y_max ) check = 1;

      if ( check ) {
         value_coeff_x  = GMT_get_bcr_z (&grd_x,  x_loc, y_loc, f1, &edgeinfo_x,  &bcr_x);
         value_coeff_x2 = GMT_get_bcr_z (&grd_x2, x_loc, y_loc, f2, &edgeinfo_x2, &bcr_x2);
         value_coeff_x3 = GMT_get_bcr_z (&grd_x3, x_loc, y_loc, f3, &edgeinfo_x3, &bcr_x3);
         value_coeff_x4 = GMT_get_bcr_z (&grd_x4, x_loc, y_loc, f4, &edgeinfo_x4, &bcr_x4);
         if (GMT_is_dnan (value_coeff_x) || GMT_is_dnan (value_coeff_x2) || GMT_is_dnan (value_coeff_x3) || GMT_is_dnan (value_coeff_x4) ) {
            check = 0;
         } else {
            if ( value_coeff_x < 0.0 ) value_coeff_x *= -1.0;
            if ( value_coeff_x2 < 0.0 ) value_coeff_x2 *= -1.0;
            if ( value_coeff_x3 < 0.0 ) value_coeff_x3 *= -1.0;
            if ( value_coeff_x4 < 0.0 ) value_coeff_x4 *= -1.0;
            samp[i] -= value_coeff_x;        
            depth_water = value_coeff_x * value_coeff_x2 * factor;
            datain[i] -= depth_water;        
            wb_twt_array[i] = value_coeff_x;
            wb_z_array[i] = depth_water;
            if ( verbose == 3 ) fprintf ( stderr, "num = %5d, xloc = %10.2f yloc = %10.2f, WB TWT = %8.2f ms., WB Depth = %8.2f m., WB VEL = %8.2f mps., Vzero = %20.15f, K = %20.15f\n", 
              i, x_loc, y_loc, value_coeff_x, depth_water, value_coeff_x2, value_coeff_x3, value_coeff_x4 );
            vzero_opt[i] = value_coeff_x3;
            k_opt[i] = value_coeff_x4;
         }
      }
   }
   if ( verbose == 3 ) fprintf ( stderr, "\n" );

   sum = zero;
   for ( i=0; i<=kount; i++ ) {
      vzero = vzero_opt[i];
      k = k_opt[i];
      x = samp[i];
      y = ( vzero / k ) * ( exp ( k * x * factor ) - 1.0 );
      error += abs ( y - datain[i] );
      if ( verbose == 3 ) fprintf ( stderr, "%-5d %12.2f %12.2f %12.2f %12.4f %12.4f\n", i, x, datain[i], y, y - datain[i], error );

      sign = 1.0;
      error = error1 = y - datain[i];

      for (;;) {
         if ( abs(error1) < thresh ) break;

         vzero = vzero_opt[i];
         k += ( delta * sign );

         x = samp[i];
         y = ( vzero / k ) * ( exp ( k * x * factor ) - 1.0 );
         error1 = y - datain[i];

         if ( verbose == 2 ) fprintf ( stderr, "%-5d %12.2f %12.2f %12.2f %12.4f\n", i, x, datain[i], y, error1 );
         if ( (error1 > zero && error1 > error) || (error1 < zero && error1 < error) ) sign *= -1.0;
      }
      sum += abs ( y - datain[i] );
      if ( verbose ) {
         fprintf ( stderr, "%-5d %12.2f %12.2f %12.2f %12.4f %12.4f\n", i, x, datain[i], y, y - datain[i], sum );
         fprintf ( stderr, "%12.2f %12.2f ", xloc_array[i], yloc_array[i] );
         fprintf ( stderr, "%30.25f %30.25f %8.2f %8.2f %8.2f\n", vzero, k, wb_twt_array[i], wb_z_array[i], sum / (float) nump1 );
      }
      printf ( "%12.2f %12.2f ", xloc_array[i], yloc_array[i] );
      printf ( "%30.25f %30.25f %8.2f %8.2f %8.2f\n", vzero, k, wb_twt_array[i], wb_z_array[i], sum / (float) nump1 );
   }

   free1double (samp);
   free1double (datain);
   free1double (xloc_array);
   free1double (yloc_array);
   free1double (vzero_opt);
   free1double (k_opt);
   free1double (wb_twt_array);
   free1double (wb_z_array);

   GMT_free ((void *)f1);
   GMT_free ((void *)f2);
   GMT_free ((void *)f3);
   GMT_free ((void *)f4);

   GMT_end (argc, argv);

   return EXIT_SUCCESS;
}
コード例 #7
0
int main (int argc, char **argv) {

   char file[BUFSIZ];
   char *coeff_top_k, *coeff_bottom_k;
   char *coeff_wb_twt, *coeff_top_twt, *coeff_middle_twt, *coeff_bottom_twt;
   char *coeff_middle_vint, *coeff_bottom_vint, *coeff_campan_vint;
   char *coeff_top_z, *coeff_bottom_z;

   struct GRD_HEADER grd_top_k, grd_bottom_k;
   struct GRD_HEADER grd_wb_twt, grd_top_twt, grd_middle_twt, grd_bottom_twt;
   struct GRD_HEADER grd_middle_vint, grd_bottom_vint, grd_campan_vint;
   struct GRD_HEADER grd_top_z, grd_bottom_z;

   struct GMT_EDGEINFO edgeinfo_top_k, edgeinfo_bottom_k;
   struct GMT_EDGEINFO edgeinfo_wb_twt, edgeinfo_top_twt, edgeinfo_middle_twt, edgeinfo_bottom_twt;
   struct GMT_EDGEINFO edgeinfo_middle_vint, edgeinfo_bottom_vint, edgeinfo_campan_vint;
   struct GMT_EDGEINFO edgeinfo_top_z, edgeinfo_bottom_z;

   struct GMT_BCR bcr_top_k, bcr_bottom_k;
   struct GMT_BCR bcr_wb_twt, bcr_top_twt, bcr_middle_twt, bcr_bottom_twt;
   struct GMT_BCR bcr_middle_vint, bcr_bottom_vint, bcr_campan_vint;
   struct GMT_BCR bcr_top_z, bcr_bottom_z;

   double value_coeff_top_k, value_coeff_bottom_k;
   double value_coeff_wb_twt, value_coeff_top_twt, value_coeff_middle_twt, value_coeff_bottom_twt;
   double value_coeff_middle_vint, value_coeff_bottom_vint, value_coeff_campan_vint;
   double value_coeff_top_z, value_coeff_bottom_z;

   short  verbose;
   int    scalar, nz, ntr, ns;
   double water_depth, vwater, ratio, factor1, dz, x_loc, y_loc; 
   double tr_msec, tr_msec_orig, dt_msec, depth_input, amp_output;
   double delrt_depth, delta_twt, delta_k, gradient, K;
   double *tr_amp, *depth;
   register int k, n;

   initargs(argc, argv);
   argc = GMT_begin (argc, argv);

   if (!getparstring ("coeff_top_k",       &coeff_top_k))       coeff_top_k       = "/home/user/FIELD.new/PICKS/DEPTH_GRIDS/below.ml.K.grd";
   if (!getparstring ("coeff_bottom_k",    &coeff_bottom_k))    coeff_bottom_k    = "/home/user/FIELD.new/PICKS/DEPTH_GRIDS/bottom.K.grd";
   if (!getparstring ("coeff_wb_twt",      &coeff_wb_twt))      coeff_wb_twt      = "/home/user/FIELD.new/PICKS/wb.twt.grd";
   if (!getparstring ("coeff_top_twt",     &coeff_top_twt))     coeff_top_twt     = "/home/user/FIELD.new/PICKS/TWT_GRIDS/01_FIELD_Top_Reservoir_twt.reform.dat.trimmed.grd";
   if (!getparstring ("coeff_middle_twt",  &coeff_middle_twt))  coeff_middle_twt  = "/home/user/FIELD.new/PICKS/TWT_GRIDS/06_FIELD_C_top_twt.reform.dat.trimmed.grd";
   if (!getparstring ("coeff_bottom_twt",  &coeff_bottom_twt))  coeff_bottom_twt  = "/home/user/FIELD.new/PICKS/TWT_GRIDS/10_FIELD_80MaSB_twt.reform.dat.trimmed.grd";
   if (!getparstring ("coeff_middle_vint", &coeff_middle_vint)) coeff_middle_vint = "/home/user/FIELD.new/PICKS/VINT_GRIDS/vint.top_res.C_Sand.mps.filter.grd";
   if (!getparstring ("coeff_bottom_vint", &coeff_bottom_vint)) coeff_bottom_vint = "/home/user/FIELD.new/PICKS/VINT_GRIDS/vint.C_Sand.80MaSB.mps.filter.grd";
   if (!getparstring ("coeff_campan_vint", &coeff_campan_vint)) coeff_campan_vint = "/home/user/FIELD.new/PICKS/VINT_GRIDS/vint.top_res.80MaSB.mps.filter.grd";
   if (!getparstring ("coeff_top_z",       &coeff_top_z))       coeff_top_z       = "/home/user/FIELD.new/PICKS/DEPTH_GRIDS/01_FIELD_Top_Reservoir.depth.new.grd";
   if (!getparstring ("coeff_bottom_z",    &coeff_bottom_z))    coeff_bottom_z    = "/home/user/FIELD.new/PICKS/DEPTH_GRIDS/10_FIELD_80MaSB.depth.new.grd";

   if (!getparshort ("verbose", &verbose)) verbose = 0;

   if ( verbose ) {
      fprintf ( stderr, "\n" );
      fprintf ( stderr, "Top_K       - GMT grid file name = %s\n", coeff_top_k );
      fprintf ( stderr, "Bottom_K    - GMT grid file name = %s\n", coeff_bottom_k );
      fprintf ( stderr, "WB_TWT      - GMT grid file name = %s\n", coeff_wb_twt );
      fprintf ( stderr, "TOP_TWT     - GMT grid file name = %s\n", coeff_top_twt );
      fprintf ( stderr, "MIDDLE_TWT  - GMT grid file name = %s\n", coeff_middle_twt );
      fprintf ( stderr, "BOTTOM_TWT  - GMT grid file name = %s\n", coeff_bottom_twt );
      fprintf ( stderr, "MIDDLE_VINT - GMT grid file name = %s\n", coeff_middle_vint );
      fprintf ( stderr, "BOTTOM_VINT - GMT grid file name = %s\n", coeff_bottom_vint );
      fprintf ( stderr, "CAMPAN_VINT - GMT grid file name = %s\n", coeff_campan_vint );
      fprintf ( stderr, "TOP_Z       - GMT grid file name = %s\n", coeff_top_z );
      fprintf ( stderr, "BOTTOM_Z    - GMT grid file name = %s\n", coeff_bottom_z );
      fprintf ( stderr, "\n" );
   }

   GMT_boundcond_init (&edgeinfo_top_k);
   GMT_boundcond_init (&edgeinfo_bottom_k);
   GMT_boundcond_init (&edgeinfo_wb_twt);
   GMT_boundcond_init (&edgeinfo_top_twt);
   GMT_boundcond_init (&edgeinfo_middle_twt);
   GMT_boundcond_init (&edgeinfo_bottom_twt);
   GMT_boundcond_init (&edgeinfo_middle_vint);
   GMT_boundcond_init (&edgeinfo_bottom_vint);
   GMT_boundcond_init (&edgeinfo_campan_vint);
   GMT_boundcond_init (&edgeinfo_top_z);
   GMT_boundcond_init (&edgeinfo_bottom_z);

   GMT_grd_init (&grd_top_k,       argc, argv, FALSE);
   GMT_grd_init (&grd_bottom_k,    argc, argv, FALSE);
   GMT_grd_init (&grd_wb_twt,      argc, argv, FALSE);
   GMT_grd_init (&grd_top_twt,     argc, argv, FALSE);
   GMT_grd_init (&grd_middle_twt,  argc, argv, FALSE);
   GMT_grd_init (&grd_bottom_twt,  argc, argv, FALSE);
   GMT_grd_init (&grd_middle_vint, argc, argv, FALSE);
   GMT_grd_init (&grd_bottom_vint, argc, argv, FALSE);
   GMT_grd_init (&grd_campan_vint, argc, argv, FALSE);
   GMT_grd_init (&grd_top_z,       argc, argv, FALSE);
   GMT_grd_init (&grd_bottom_z,    argc, argv, FALSE);

   if (GMT_read_grd_info (coeff_top_k, &grd_top_k))             fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);
   if (GMT_read_grd_info (coeff_bottom_k, &grd_bottom_k))       fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);
   if (GMT_read_grd_info (coeff_wb_twt, &grd_wb_twt))           fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);
   if (GMT_read_grd_info (coeff_top_twt, &grd_top_twt))         fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);
   if (GMT_read_grd_info (coeff_middle_twt, &grd_middle_twt))   fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);
   if (GMT_read_grd_info (coeff_bottom_twt, &grd_bottom_twt))   fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);
   if (GMT_read_grd_info (coeff_middle_vint, &grd_middle_vint)) fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);
   if (GMT_read_grd_info (coeff_bottom_vint, &grd_bottom_vint)) fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);
   if (GMT_read_grd_info (coeff_campan_vint, &grd_campan_vint)) fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);
   if (GMT_read_grd_info (coeff_top_z, &grd_top_z))             fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);
   if (GMT_read_grd_info (coeff_bottom_z, &grd_bottom_z))       fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);

   f_top_k       = (float *) GMT_memory (VNULL, (size_t)((grd_top_k.nx  + 4)       * (grd_top_k.ny  + 4)),       sizeof(float), GMT_program);
   f_bottom_k    = (float *) GMT_memory (VNULL, (size_t)((grd_bottom_k.nx  + 4)    * (grd_bottom_k.ny  + 4)),    sizeof(float), GMT_program);
   f_wb_twt      = (float *) GMT_memory (VNULL, (size_t)((grd_wb_twt.nx  + 4)      * (grd_wb_twt.ny  + 4)),      sizeof(float), GMT_program);
   f_top_twt     = (float *) GMT_memory (VNULL, (size_t)((grd_top_twt.nx  + 4)     * (grd_top_twt.ny  + 4)),     sizeof(float), GMT_program);
   f_middle_twt  = (float *) GMT_memory (VNULL, (size_t)((grd_middle_twt.nx  + 4)  * (grd_middle_twt.ny  + 4)),  sizeof(float), GMT_program);
   f_bottom_twt  = (float *) GMT_memory (VNULL, (size_t)((grd_bottom_twt.nx  + 4)  * (grd_bottom_twt.ny  + 4)),  sizeof(float), GMT_program);
   f_middle_vint = (float *) GMT_memory (VNULL, (size_t)((grd_middle_vint.nx  + 4) * (grd_middle_vint.ny  + 4)), sizeof(float), GMT_program);
   f_bottom_vint = (float *) GMT_memory (VNULL, (size_t)((grd_bottom_vint.nx  + 4) * (grd_bottom_vint.ny  + 4)), sizeof(float), GMT_program);
   f_campan_vint = (float *) GMT_memory (VNULL, (size_t)((grd_campan_vint.nx  + 4) * (grd_campan_vint.ny  + 4)), sizeof(float), GMT_program);
   f_top_z       = (float *) GMT_memory (VNULL, (size_t)((grd_top_z.nx  + 4)       * (grd_top_z.ny  + 4)),       sizeof(float), GMT_program);
   f_bottom_z    = (float *) GMT_memory (VNULL, (size_t)((grd_bottom_z.nx  + 4)    * (grd_bottom_z.ny  + 4)),    sizeof(float), GMT_program);

   GMT_pad[0] = GMT_pad[1] = GMT_pad[2] = GMT_pad[3] = 2;

   GMT_boundcond_param_prep (&grd_top_k,       &edgeinfo_top_k);
   GMT_boundcond_param_prep (&grd_bottom_k,    &edgeinfo_bottom_k);
   GMT_boundcond_param_prep (&grd_wb_twt,      &edgeinfo_wb_twt);
   GMT_boundcond_param_prep (&grd_top_twt,     &edgeinfo_top_twt);
   GMT_boundcond_param_prep (&grd_middle_twt,  &edgeinfo_middle_twt);
   GMT_boundcond_param_prep (&grd_bottom_twt,  &edgeinfo_bottom_twt);
   GMT_boundcond_param_prep (&grd_middle_vint, &edgeinfo_middle_vint);
   GMT_boundcond_param_prep (&grd_bottom_vint, &edgeinfo_bottom_vint);
   GMT_boundcond_param_prep (&grd_campan_vint, &edgeinfo_campan_vint);
   GMT_boundcond_param_prep (&grd_top_z,       &edgeinfo_top_z);
   GMT_boundcond_param_prep (&grd_bottom_z,    &edgeinfo_bottom_z);

   GMT_boundcond_set (&grd_top_k,       &edgeinfo_top_k,       GMT_pad, f_top_k);
   GMT_boundcond_set (&grd_bottom_k,    &edgeinfo_bottom_k,    GMT_pad, f_bottom_k);
   GMT_boundcond_set (&grd_wb_twt,      &edgeinfo_wb_twt,      GMT_pad, f_wb_twt);
   GMT_boundcond_set (&grd_top_twt,     &edgeinfo_top_twt,     GMT_pad, f_top_twt);
   GMT_boundcond_set (&grd_middle_twt,  &edgeinfo_middle_twt,  GMT_pad, f_middle_twt);
   GMT_boundcond_set (&grd_bottom_twt,  &edgeinfo_bottom_twt,  GMT_pad, f_bottom_twt);
   GMT_boundcond_set (&grd_middle_vint, &edgeinfo_middle_vint, GMT_pad, f_middle_vint);
   GMT_boundcond_set (&grd_bottom_vint, &edgeinfo_bottom_vint, GMT_pad, f_bottom_vint);
   GMT_boundcond_set (&grd_campan_vint, &edgeinfo_campan_vint, GMT_pad, f_campan_vint);
   GMT_boundcond_set (&grd_top_z,       &edgeinfo_top_z,       GMT_pad, f_top_z);
   GMT_boundcond_set (&grd_bottom_z,    &edgeinfo_bottom_z,    GMT_pad, f_bottom_z);

   GMT_bcr_init (&grd_top_k,       GMT_pad, BCR_BSPLINE, 1, &bcr_top_k);
   GMT_bcr_init (&grd_bottom_k,    GMT_pad, BCR_BSPLINE, 1, &bcr_bottom_k);
   GMT_bcr_init (&grd_wb_twt,      GMT_pad, BCR_BSPLINE, 1, &bcr_wb_twt);
   GMT_bcr_init (&grd_top_twt,     GMT_pad, BCR_BSPLINE, 1, &bcr_top_twt);
   GMT_bcr_init (&grd_middle_twt,  GMT_pad, BCR_BSPLINE, 1, &bcr_middle_twt);
   GMT_bcr_init (&grd_bottom_twt,  GMT_pad, BCR_BSPLINE, 1, &bcr_bottom_twt);
   GMT_bcr_init (&grd_middle_vint, GMT_pad, BCR_BSPLINE, 1, &bcr_middle_vint);
   GMT_bcr_init (&grd_bottom_vint, GMT_pad, BCR_BSPLINE, 1, &bcr_bottom_vint);
   GMT_bcr_init (&grd_campan_vint, GMT_pad, BCR_BSPLINE, 1, &bcr_campan_vint);
   GMT_bcr_init (&grd_top_z,       GMT_pad, BCR_BSPLINE, 1, &bcr_top_z);
   GMT_bcr_init (&grd_bottom_z,    GMT_pad, BCR_BSPLINE, 1, &bcr_bottom_z);

   GMT_read_grd (coeff_top_k,       &grd_top_k,       f_top_k,       0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);
   GMT_read_grd (coeff_bottom_k,    &grd_bottom_k,    f_bottom_k,    0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);
   GMT_read_grd (coeff_wb_twt,      &grd_wb_twt,      f_wb_twt,      0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);
   GMT_read_grd (coeff_top_twt,     &grd_top_twt,     f_top_twt,     0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);
   GMT_read_grd (coeff_middle_twt,  &grd_middle_twt,  f_middle_twt,  0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);
   GMT_read_grd (coeff_bottom_twt,  &grd_bottom_twt,  f_bottom_twt,  0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);
   GMT_read_grd (coeff_middle_vint, &grd_middle_vint, f_middle_vint, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);
   GMT_read_grd (coeff_bottom_vint, &grd_bottom_vint, f_bottom_vint, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);
   GMT_read_grd (coeff_campan_vint, &grd_campan_vint, f_campan_vint, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);
   GMT_read_grd (coeff_top_z,       &grd_top_z,       f_top_z,       0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);
   GMT_read_grd (coeff_bottom_z,    &grd_bottom_z,    f_bottom_z,    0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);

   /* Get info from first trace */
   ntr     = gettra (&tr, 0);
   ns      = tr.ns;
   scalar  = abs ( tr.scalel );
   if ( scalar == 0 ) scalar = 1; 
   dt_msec = tr.dt * 0.001;

   if (!getpardouble ("dz",&dz)) dz = 1;
   if (!getpardouble ("vwater",&vwater)) vwater = 1452.05;
   if (!getparint    ("nz",&nz)) nz = ns;

   if ( verbose ) {
      fprintf ( stderr, "Output depth sample rate (dz) = %f\n", dz );
      fprintf ( stderr, "Number of output depth samples per trace = %d\n", nz );
      fprintf ( stderr, "Number of traces = %d, number of samples per trace = %d\n", ntr, ns );
      fprintf ( stderr, "Time sample rate (milliseconds) = %f\n", dt_msec );
      fprintf ( stderr, "Vwater = %f (meters/second)\n", vwater );
      fprintf ( stderr, "Location scalar = %d\n", scalar );
      fprintf ( stderr, "\n" );
   }

   rewind (stdin);

   if ( ns > nz ) {
      depth = ealloc1double ( ns );
   } else {
      depth = ealloc1double ( nz );
   }
   tr_amp = ealloc1double ( ns );

   factor1 = 0.0005;
   delrt_depth = 0.0;

   /* Main loop over traces */
   for ( k = 0; k < ntr; ++k ) {
      gettr (&tr);
      x_loc = tr.sx / scalar;
      y_loc = tr.sy / scalar;
      for ( n=ns; n < nz; ++n ) depth[n] = 0;

      if ( x_loc >= grd_wb_twt.x_min && x_loc <= grd_wb_twt.x_max && y_loc >= grd_wb_twt.y_min && y_loc <= grd_wb_twt.y_max ) {

         value_coeff_top_k       = GMT_get_bcr_z (&grd_top_k,       x_loc, y_loc, f_top_k,       &edgeinfo_top_k,       &bcr_top_k);
         value_coeff_bottom_k    = GMT_get_bcr_z (&grd_bottom_k,    x_loc, y_loc, f_bottom_k,    &edgeinfo_bottom_k,    &bcr_bottom_k);
         value_coeff_wb_twt      = GMT_get_bcr_z (&grd_wb_twt,      x_loc, y_loc, f_wb_twt,      &edgeinfo_wb_twt,      &bcr_wb_twt);
         value_coeff_top_twt     = GMT_get_bcr_z (&grd_top_twt,     x_loc, y_loc, f_top_twt,     &edgeinfo_top_twt,     &bcr_top_twt);
         value_coeff_middle_twt  = GMT_get_bcr_z (&grd_middle_twt,  x_loc, y_loc, f_middle_twt,  &edgeinfo_middle_twt,  &bcr_middle_twt);
         value_coeff_bottom_twt  = GMT_get_bcr_z (&grd_bottom_twt,  x_loc, y_loc, f_bottom_twt,  &edgeinfo_bottom_twt,  &bcr_bottom_twt);
         value_coeff_middle_vint = GMT_get_bcr_z (&grd_middle_vint, x_loc, y_loc, f_middle_vint, &edgeinfo_middle_vint, &bcr_middle_vint);
         value_coeff_bottom_vint = GMT_get_bcr_z (&grd_bottom_vint, x_loc, y_loc, f_bottom_vint, &edgeinfo_bottom_vint, &bcr_bottom_vint);
         value_coeff_campan_vint = GMT_get_bcr_z (&grd_campan_vint, x_loc, y_loc, f_campan_vint, &edgeinfo_campan_vint, &bcr_campan_vint);
         value_coeff_top_z       = GMT_get_bcr_z (&grd_top_z,       x_loc, y_loc, f_top_z,       &edgeinfo_top_z,       &bcr_top_z);
         value_coeff_bottom_z    = GMT_get_bcr_z (&grd_bottom_z,    x_loc, y_loc, f_bottom_z,    &edgeinfo_bottom_z,    &bcr_bottom_z);

         if ( GMT_is_dnan (value_coeff_wb_twt)     || GMT_is_dnan (value_coeff_top_k)       || GMT_is_dnan (value_coeff_top_twt) ||\
              GMT_is_dnan (value_coeff_bottom_twt) || GMT_is_dnan (value_coeff_campan_vint) || GMT_is_dnan (value_coeff_bottom_k) ||\
              GMT_is_dnan (value_coeff_top_z)      || GMT_is_dnan (value_coeff_bottom_z) ) {

            for ( n=0; n < nz; ++n )  tr.data[n] = 0;
            tr.delrt = 0;
            tr.trid = 0;

         } else {

            water_depth = value_coeff_wb_twt * factor1 * vwater;
            ratio = vwater / value_coeff_top_k;

	    if ( verbose == 2 ) {
               fprintf ( stderr, "\n" );
               fprintf ( stderr, "Trace num = %5d X-Loc = %10.2f Y-Loc = %10.2f\n", k+1, x_loc, y_loc );
               fprintf ( stderr, "Top_K          = %8.5f\n", value_coeff_top_k );
               fprintf ( stderr, "Bottom_K       = %8.5f\n", value_coeff_bottom_k );
               fprintf ( stderr, "WB_TWT         = %8.2f\n", value_coeff_wb_twt );
               fprintf ( stderr, "TOP_TWT        = %8.2f\n", value_coeff_top_twt );
               fprintf ( stderr, "MIDDLE_TWT     = %8.2f\n", value_coeff_middle_twt );
               fprintf ( stderr, "BOTTOM_TWT     = %8.2f\n", value_coeff_bottom_twt );
               fprintf ( stderr, "MIDDLE_VINT    = %8.2f\n", value_coeff_middle_vint );
               fprintf ( stderr, "BOTTOM_VINT    = %8.2f\n", value_coeff_bottom_vint );
               fprintf ( stderr, "CAMPANIAN_VINT = %8.2f\n", value_coeff_campan_vint );
               fprintf ( stderr, "TOP_Z          = %8.2f\n", value_coeff_top_z );
               fprintf ( stderr, "BOTTOM_Z       = %8.2f\n", value_coeff_bottom_z );
            }

            delta_twt = value_coeff_bottom_twt - value_coeff_top_twt;
            delta_k   = value_coeff_bottom_k - value_coeff_top_k;
            gradient  = delta_k / delta_twt;

            for ( n=0; n < ns; ++n ) {
               tr_amp[n] = tr.data[n];

               if ( tr.delrt == 0 ) {
                  tr_msec = n * dt_msec;
               } else {
                  tr_msec = tr.delrt + ( n * dt_msec );
               }
 
               if ( tr_msec <= value_coeff_wb_twt ) {
                  depth[n] = tr_msec * factor1 * vwater;
                  if ( tr_msec <= tr.delrt ) delrt_depth = depth[n];
               } else if ( tr_msec <= value_coeff_top_twt ) {
                  tr_msec_orig = tr_msec;
                  tr_msec = ( tr_msec - value_coeff_wb_twt ) * factor1;
                  depth[n] = ( ratio * (exp (value_coeff_top_k*tr_msec) - 1.0) ) + water_depth;
                  if ( tr_msec_orig <= tr.delrt ) delrt_depth = depth[n];
               } else if ( tr_msec > value_coeff_top_twt && tr_msec <= value_coeff_bottom_twt ) {
                  K = value_coeff_top_k + ( ( tr_msec - value_coeff_top_twt ) * gradient ); 
                  tr_msec_orig = tr_msec;
                  tr_msec = ( tr_msec - value_coeff_wb_twt ) * factor1;
                  ratio = vwater / K;
                  depth[n] = ( ratio * (exp (K*tr_msec) - 1.0) ) + water_depth;
                  if ( tr_msec_orig <= tr.delrt ) delrt_depth = depth[n];

                  /* fprintf ( stderr, "Trace = %5d, Sample = %5d, Top_TWT = %8.2f, TWT = %8.2f, Bottom_TWT = %8.2f, Top_Z = %8.2f, Depth = %8.2f, Bottom_Z = %8.2f, Delta_twt = %8.2f, Delta_K = %10.6f, Gradient = %10.6f, K = %10.6f\n",\
                  k, n, value_coeff_top_twt, n * dt_msec, value_coeff_bottom_twt, value_coeff_top_z, depth[n], value_coeff_bottom_z, delta_twt, delta_k, gradient, K ); */

               } else if ( tr_msec > value_coeff_bottom_twt ) {
                  tr_msec_orig = tr_msec;
                  tr_msec = ( tr_msec - value_coeff_wb_twt ) * factor1;
                  ratio = vwater / value_coeff_bottom_k;
                  depth[n] = ( ratio * (exp (value_coeff_bottom_k*tr_msec) - 1.0) ) + water_depth;
                  if ( tr_msec_orig <= tr.delrt ) delrt_depth = depth[n];
               }
            }

            for ( n=0; n < nz; ++n ) {
               depth_input = n * dz;
               if ( depth_input < delrt_depth ) {
                  tr.data[n] = 0.0;
               } else {
                  dintlin ( ns, depth, tr_amp, tr_amp[0], tr_amp[ns-1], 1, &depth_input, &amp_output );
                  tr.data[n] = (float) amp_output;
               }
            }
            tr.trid = 1;
         }

         tr.ns = nz;
         tr.delrt = 0;
         tr.dt = nint(dz*1000);
         puttr (&tr);

      } else {
         fprintf ( stderr, "Input trace = %d, Xloc = %.0f Yloc = %.0f is out of bounds\n", k, x_loc, y_loc);
      }
   }

   GMT_free ((void *)f_top_k);
   GMT_free ((void *)f_bottom_k);
   GMT_free ((void *)f_wb_twt);
   GMT_free ((void *)f_top_twt);
   GMT_free ((void *)f_middle_twt);
   GMT_free ((void *)f_bottom_twt);
   GMT_free ((void *)f_middle_vint);
   GMT_free ((void *)f_bottom_vint);
   GMT_free ((void *)f_campan_vint);

   free1double (depth);
   free1double (tr_amp);

   GMT_end (argc, argv);

   return (0);
}
コード例 #8
0
ファイル: grdreformat.c プロジェクト: likueimo/ubuntu_gmt4
int main (int argc, char **argv)
{
#include "grdreformat.h"	/* Defines N_GRDTXT_LINES and char *grd_formats[N_GRDTXT_LINES] array used in usage message */
	GMT_LONG error = FALSE, global = FALSE, no_header;

	GMT_LONG i, nfiles = 0, nx, ny, type[2];

	size_t nm;
	
	double w, e, s, n;

	float *z = NULL;

	char *file[2], fname[2][BUFSIZ];

	struct GRD_HEADER grd;
	struct GRDREFORMAT_CTRL *Ctrl = NULL;

	void *New_grdreformat_Ctrl (), Free_grdreformat_Ctrl (struct GRDREFORMAT_CTRL *C);

	argc = (int)GMT_begin (argc, argv);

	Ctrl = (struct GRDREFORMAT_CTRL *)New_grdreformat_Ctrl ();	/* Allocate and initialize a new control structure */
	
	file[0] = file[1] = CNULL;
	w = e = s = n = 0.0;

	for (i = 1; i < argc; i++) {
		if (argv[i][0] == '-') {
			switch (argv[i][1]) {
				/* Common parameters */

				case 'R':
				case 'V':
				case 'f':
				case '\0':
					error += GMT_parse_common_options (argv[i], &w, &e, &s, &n);
					break;

				case 'N':
					Ctrl->N.active = TRUE;
					break;
				default:
					error = TRUE;
					GMT_default_error (argv[i][1]);
					break;
			}
		}
		else if (nfiles < 2) {
			file[nfiles] = argv[i];
			nfiles++;
		}
		else
			nfiles++;
	}

	if (argc == 1 || GMT_give_synopsis_and_exit) {	/* Display usage */
		fprintf (stderr, "grdreformat %s - Converting between different grid file formats\n\n", GMT_VERSION);
		fprintf( stderr, "usage: grdreformat ingrdfile[=id[/scale/offset]] outgrdfile[=id[/scale/offset]] [-N]\n\t[%s] [%s] [-V]\n", GMT_Rgeo_OPT, GMT_f_OPT);

		if (GMT_give_synopsis_and_exit) exit (EXIT_FAILURE);

		fprintf (stderr, "\tingrdfile is the grid file to convert.\n");
		fprintf (stderr, "\toutgrdfile is the new converted grid file.\n");
		fprintf( stderr, "\tscale and offset, if given, will multiply data by scale and add offset.\n");
		fprintf (stderr, "\n\tOPTIONS:\n");
		fprintf (stderr, "\t-N  Do NOT write the header (native grids only - ignored otherwise).\n");
		fprintf (stderr, "\t\t  Useful when creating files to be used by grdraster.\n");
		GMT_explain_option ('r');
		GMT_explain_option ('f');
		GMT_explain_option ('V');

		fprintf (stderr, "\n	The following grid file formats are supported:\n\n");
#ifdef USE_GDAL
		for (i = 0; i < N_GRDTXT_LINES; i++) fprintf (stderr, "\t%s\n", grd_formats[i]);
#else
		for (i = 0; i < N_GRDTXT_LINES; i++) if (i != 33) fprintf (stderr, "\t%s\n", grd_formats[i]);
#endif
		exit (EXIT_FAILURE);
	}

	if (nfiles != 2) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR: Must specify both input and output file names.\n", GMT_program);
		error++;
	}

	if (error) exit (EXIT_FAILURE);

	GMT_grd_init (&grd, argc, argv, FALSE);
	no_header = (Ctrl->N.active) ? 64 : 0;
	GMT_err_fail (GMT_grd_get_format (file[0], &grd, TRUE), file[0]);
	type[0] = grd.type;
	strcpy (fname[0], grd.name);
	GMT_err_fail (GMT_grd_get_format (file[1], &grd, FALSE), file[1]);
	type[1] = grd.type;
	strcpy (fname[1], grd.name);

	if (type[1] == 20) {	/* Golden Surfer format 7 (double) is read-only */
		fprintf (stderr, "%s: Grid format sd (Golden Software Surfer format 7 (double)) is read-only!\n", GMT_program);
		exit (EXIT_FAILURE);
	}
#ifdef USE_GDAL
	if (type[1] == 22) {	/* GDAL format is read-only */
		fprintf (stderr, "%s: Grid format gd (GDAL) is read-only!\n", GMT_program);
		exit (EXIT_FAILURE);
	}
#endif	
	if (gmtdefs.verbose) {
		if (file[0][0] == '=') strcpy (fname[0], "<stdin>");
		if (file[1][0] == '=') strcpy (fname[1], "<stdout>");
		fprintf (stderr, "%s: Translating file %s (format = %ld) to file %s (format = %ld)\n", GMT_program, fname[0], type[0], fname[1], type[1]);
		if (no_header && GMT_grdformats[type[1]][0] != 'c' && GMT_grdformats[type[1]][0] != 'n') fprintf (stderr, "%s: No grd header will be written\n", GMT_program);
	}

	GMT_err_fail (GMT_read_grd_info (file[0], &grd), fname[0]);

	if (e > w && n > s) {
		global = GMT_grd_is_global (&grd);
		if (!global && (w < grd.x_min || e > grd.x_max)) error = TRUE;
		if (s < grd.y_min || n > grd.y_max) error = TRUE;
		if (error) {
			fprintf (stderr, "%s: Subset exceeds data domain!\n", GMT_program);
			exit (EXIT_FAILURE);
		}
		nx = GMT_get_n (w, e, grd.x_inc, grd.node_offset);
		ny = GMT_get_n (s, n, grd.y_inc, grd.node_offset);
		nm = GMT_get_nm (nx, ny);
	}
	else
		nm = GMT_get_nm (grd.nx, grd.ny);

	z = (float *) GMT_memory (VNULL, nm, sizeof (float), GMT_program);
	GMT_err_fail (GMT_read_grd (file[0], &grd, z, w, e, s, n, GMT_pad, FALSE), fname[0]);

	grd.type = type[1];

	GMT_grd_init (&grd, argc, argv, TRUE);

	GMT_err_fail (GMT_write_grd (file[1], &grd, z, 0.0, 0.0, 0.0, 0.0, GMT_pad, no_header), fname[1]);

	GMT_free ((void *)z);

	Free_grdreformat_Ctrl (Ctrl);	/* Deallocate control structure */

	GMT_end (argc, argv);

	exit (EXIT_SUCCESS);
}