Esempio n. 1
0
int GMT_esri_write_grd_info (struct GMT_CTRL *GMT, struct GMT_GRID_HEADER *header)
{
	FILE *fp = NULL;
	
	if (!strcmp (header->name, "="))	/* Write to stdout */
		fp = GMT->session.std[GMT_OUT];
	else if ((fp = GMT_fopen (GMT, header->name, "w")) == NULL)
		return (GMT_GRDIO_CREATE_FAILED);
	
	write_esri_info (GMT, fp, header);

	GMT_fclose (GMT, fp);
	
	return (GMT_NOERROR);
}
Esempio n. 2
0
int GMT_esri_read_grd_info (struct GMT_CTRL *GMT, struct GMT_GRID_HEADER *header)
{
	int error;
	FILE *fp = NULL;

	if (!strcmp (header->name, "="))	/* Pipe in from stdin */
		fp = GMT->session.std[GMT_IN];
	else if ((fp = GMT_fopen (GMT, header->name, "r")) == NULL)
		return (GMT_GRDIO_OPEN_FAILED);

	if ((error = read_esri_info (GMT, fp, header))) return (error);

	GMT_fclose (GMT, fp);
		
	return (GMT_NOERROR);
}
Esempio n. 3
0
int GMT_esri_read_grd (struct GMT_CTRL *GMT, struct GMT_GRID_HEADER *header, float *grid, double wesn[], unsigned int *pad, unsigned int complex_mode)
{
	int error;
	bool check, is_binary = false, swap = false;
	unsigned int col, height_in, ii, in_nx;
	int row, first_col, last_col, first_row, last_row;
	unsigned int row2, width_in, *actual_col = NULL;
	unsigned int nBits = 32U;
	uint64_t ij, kk, width_out, imag_offset, n_left = 0;
	size_t n_expected;
	char *r_mode = NULL;
	int16_t *tmp16 = NULL;
	float value, *tmp = NULL;
	FILE *fp = NULL;

	if (header->flags[0]) {	/* We are dealing with a ESRI .hdr file or GTOPO30, SRTM30, SRTM1|3 */
		r_mode = "rb";
		if (((header->flags[0] == 'M' || header->flags[0] == 'B') && !GMT_BIGENDIAN) ||
			(header->flags[0] == 'L' && GMT_BIGENDIAN)) 
			swap = true;
		nBits = header->bits;
		is_binary = true;
	}
	else
		r_mode = GMT->current.io.r_mode;

	if (!strcmp (header->name, "="))	/* Read from pipe */
		fp = GMT->session.std[GMT_IN];
	else if ((fp = GMT_fopen (GMT, header->name, r_mode)) != NULL) {
		if ((error = read_esri_info (GMT, fp, header))) return (error);
	}
	else
		return (GMT_GRDIO_OPEN_FAILED);
	
	GMT_err_pass (GMT, GMT_grd_prep_io (GMT, header, wesn, &width_in, &height_in, &first_col, &last_col, &first_row, &last_row, &actual_col), header->name);
	(void)GMT_init_complex (header, complex_mode, &imag_offset);	/* Set offset for imaginary complex component */

	width_out = width_in;		/* Width of output array */
	if (pad[XLO] > 0) width_out += pad[XLO];
	if (pad[XHI] > 0) width_out += pad[XHI];
	n_expected = header->nx;

	if (nBits == 32)		/* Either an ascii file or ESRI .HDR with NBITS = 32, in which case we assume it's a file of floats */
		tmp = GMT_memory (GMT, NULL, n_expected, float);
	else
Esempio n. 4
0
int main (int argc, char **argv)
{
	GMT_LONG i, j, ij, ij_f, n = 0;
	GMT_LONG n_expected_fields, n_args, m, n_fields, fno, n_files = 0;
	size_t n_alloc = GMT_CHUNK, nm;

	GMT_LONG error = FALSE, nofile = TRUE, done = FALSE;

	double sx, sy, cx, cy, w_min, w_max, sf = 1.0;
	double *xx = NULL, *yy = NULL, *zz = NULL, *ww = NULL, *surfd = NULL, *in = NULL;
	float *surf = NULL;

	char line[BUFSIZ], *not_used = NULL;
	FILE *fp = NULL;

	struct SPHINTERPOLATE_CTRL *Ctrl = NULL;
	struct GRD_HEADER h;
	void *New_sphinterpolate_Ctrl (), Free_sphinterpolate_Ctrl (struct SPHINTERPOLATE_CTRL *C);
	GMT_LONG get_args (char *arg, double pars[], char *msg);
	
	argc = (int)GMT_begin (argc, argv);

	Ctrl = (struct SPHINTERPOLATE_CTRL *)New_sphinterpolate_Ctrl ();	/* Allocate and initialize a new control structure */
	GMT_grd_init (&h, argc, argv, FALSE);

	for (i = 1; i < argc; i++) {
		if (argv[i][0] == '-') {
			switch (argv[i][1]) {
        
				/* Common parameters */
        
				case 'H':
				case 'M':
				case 'R':
				case 'V':
				case ':':
				case 'b':
				case 'm':
				case '\0':
					error += GMT_parse_common_options (argv[i], &h.x_min, &h.x_max, &h.y_min, &h.y_max);
					break;
        
				/* Supplemental parameters */
        
				case 'F':
					Ctrl->F.active = TRUE;
					break;
				case 'G':
					Ctrl->G.active = TRUE;
					Ctrl->G.file = strdup (&argv[i][2]);
					break;
				case 'I':
					Ctrl->I.active = TRUE;
					if (GMT_getinc (&argv[i][2], &Ctrl->I.xinc, &Ctrl->I.yinc)) {
						GMT_inc_syntax ('I', 1);
						error = TRUE;
					}
					break;
				case 'Q':
					Ctrl->Q.active = TRUE;
					switch (argv[i][2]) {
						case '0':	/* Linear */
							Ctrl->Q.mode = 0;
							break;
						case '1':
							Ctrl->Q.mode = 1;
							break;
						case '2':
							Ctrl->Q.mode = 2;
							if (argv[i][3] == '/') {	/* Gave optional n/m/dgmx */
								if ((m = get_args (&argv[i][4], Ctrl->Q.value, "-Q3/N[/M[/U]]")) < 0) error = TRUE;
							}
							break;
						case '3':
							Ctrl->Q.mode = 3;
							if (argv[i][3] == '/') {	/* Gave optional e/sm/niter */
								if ((m = get_args (&argv[i][4], Ctrl->Q.value, "-Q3/E[/U[/niter]]")) < 0) error = TRUE;
							}
							break;
						default:
							error = TRUE;
							fprintf (stderr, "GMT ERROR %s: -%c Mode must be in 0-3 range\n", GMT_program, argv[i][1]);
							break;
					}
					break;
				case 'T':
					Ctrl->T.active = TRUE;
					break;
				case 'Z':
					Ctrl->Z.active = TRUE;
					break;
				default:
					error = TRUE;
					GMT_default_error (argv[i][1]);
					break;
			}
		}
		else
			n_files++;
	}

	if (argc == 1 || GMT_give_synopsis_and_exit) {
		fprintf (stderr, "sphinterpolate %s - Spherical gridding in tension of data on a sphere\n", GMT_VERSION);
		fprintf (stderr, "==> The hard work is done by algorithms 772 (STRIPACK) & 773 (SSRFPACK) by R. J. Renka [1997] <==\n\n");
		fprintf (stderr, "usage: sphinterpolate [<infiles>] -G<grdfile> %s [-F] [%s]\n", GMT_I_OPT, GMT_H_OPT);
		fprintf (stderr, "\t[-Q<mode>][/args] [-T] [-V] [-Z] [%s] [%s] [%s]\n\n", GMT_t_OPT, GMT_b_OPT, GMT_m_OPT);
                
		fprintf (stderr, "\t-G Specify file name for the final gridded solution.\n");
		GMT_inc_syntax ('I', 0);
		if (GMT_give_synopsis_and_exit) exit (EXIT_FAILURE);
                
		fprintf (stderr, "\n\tOPTIONS:\n");
		fprintf (stderr, "\tinfiles (in ASCII) has 3 or more columns.  If no file(s) is given, standard input is read.\n");
		fprintf (stderr, "\t-F Force pixel registration for output grid [Default is gridline orientation]\n");
		GMT_explain_option ('H');
		fprintf (stderr, "\t-Q Select tension factors to achive the following [Default is no tension]:\n");
		fprintf (stderr, "\t   0: Piecewise linear interpolation ; no tension [Default]\n");
		fprintf (stderr, "\t   1: Smooth interpolation with local gradient estimates.\n");
		fprintf (stderr, "\t   2: Smooth interpolation with global gradient estimates and tension.  Optionally append /N/M/U:\n");
		fprintf (stderr, "\t      N = Number of iterations to converge solutions for gradients and variable tensions (-T only) [3]\n");
		fprintf (stderr, "\t      M = Number of Gauss-Seidel iterations when determining gradients [10]\n");
		fprintf (stderr, "\t      U = Maximum change in a gradient at the last iteration [0.01]\n");
		fprintf (stderr, "\t   3: Smoothing.  Optionally append /E/U/N, where\n");
		fprintf (stderr, "\t      E = Expected squared error in a typical (scaled) data value [0.01]\n");
		fprintf (stderr, "\t      U = Upper bound on  weighted sum of squares of deviations from data [npoints]\n");
		fprintf (stderr, "\t      N = Number of iterations to converge solutions for gradients and variable tensions (-T only) [3]\n");
		GMT_explain_option ('R');
		fprintf (stderr, "\t   If no region is specified we default to the entire world [-Rg]\n");
		fprintf (stderr, "\t-T Use variable tension (ignored for -Q0) [constant]\n");
		GMT_explain_option ('V');
		fprintf (stderr, "\t-Z Scale data by 1/(max-min) prior to gridding [no scaling]\n");
		GMT_explain_option (':');
		GMT_explain_option ('i');
		GMT_explain_option ('n');
		fprintf (stderr, "\t   Default is 3 input columns\n");
		GMT_explain_option ('m');
		GMT_explain_option ('.');
		exit (EXIT_FAILURE);
	}

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

	if (GMT_io.binary[GMT_IN] && GMT_io.io_header[GMT_IN]) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR.  Binary input data cannot have header -H\n", GMT_program);
		error++;
	}
	if (GMT_io.binary[GMT_IN] && GMT_io.ncol[GMT_IN] == 0) GMT_io.ncol[GMT_IN] = 3;
	if (GMT_io.binary[GMT_IN] && GMT_io.ncol[GMT_IN] < 3) {
            fprintf (stderr, "%s: GMT SYNTAX ERROR.  Binary input data (-bi) must have at least 3 columns\n", GMT_program);
		error++;
	}
	if (Ctrl->I.xinc <= 0.0 || Ctrl->I.yinc <= 0.0) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR -I option.  Must specify positive increment(s)\n", GMT_program);
		error = TRUE;
	}
	if (!Ctrl->G.file) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR -G:  Must specify output file\n", GMT_program);
		error = TRUE;
	}
	if (Ctrl->Q.mode < 0 || Ctrl->Q.mode > 3) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR -T:  Must specify a mode in the 0-3 range\n", GMT_program);
		error = TRUE;
	}
	if (error) exit (EXIT_FAILURE);

	if (GMT_io.binary[GMT_IN] && gmtdefs.verbose) {
		char *type[2] = {"double", "single"};
		fprintf (stderr, "%s: Expects %ld-column %s-precision binary data\n", GMT_program, GMT_io.ncol[GMT_IN], type[GMT_io.single_precision[GMT_IN]]);
	}
	if (!project_info.region_supplied) {	/* Default is global region */
		h.x_min = 0.0;	h.x_max = 360.0;	h.y_min = -90.0;	h.y_max = 90.0;
	}

#ifdef SET_IO_MODE
	GMT_setmode (GMT_OUT);
#endif

	/* Now we are ready to take on some input values */

	if (n_files > 0)
		nofile = FALSE;
	else
		n_files = 1;
	n_args = (argc > 1) ? argc : 3;
	n_expected_fields = (GMT_io.ncol[GMT_IN]) ? GMT_io.ncol[GMT_IN] : 3;

	n_alloc = GMT_CHUNK;
	xx = (double *) GMT_memory (VNULL, (size_t)n_alloc, sizeof (double), GMT_program);
	yy = (double *) GMT_memory (VNULL, (size_t)n_alloc, sizeof (double), GMT_program);
	zz = (double *) GMT_memory (VNULL, (size_t)n_alloc, sizeof (double), GMT_program);
	ww = (double *) GMT_memory (VNULL, (size_t)n_alloc, sizeof (double), GMT_program);
	n = 0;
	w_min = DBL_MAX;	w_max = -DBL_MAX;
	for (fno = 1; !done && fno < n_args; fno++) {	/* Loop over input files, if any */
		if (!nofile && argv[fno][0] == '-') continue;

		if (nofile) {	/* Just read standard input */
			fp = GMT_stdin;
			done = TRUE;
#ifdef SET_IO_MODE
			GMT_setmode (GMT_IN);
#endif
		}
		else if ((fp = GMT_fopen (argv[fno], GMT_io.r_mode)) == NULL) {
			fprintf (stderr, "%s: Cannot open file %s\n", GMT_program, argv[fno]);
			continue;
		}

		if (!nofile && gmtdefs.verbose) fprintf (stderr, "%s: Reading file %s\n", GMT_program, argv[fno]);

		if (GMT_io.io_header[GMT_IN]) for (i = 0; i < GMT_io.n_header_recs; i++) not_used = GMT_fgets (line, BUFSIZ, fp);

		while ((n_fields = GMT_input (fp, &n_expected_fields, &in)) >= 0 && !(GMT_io.status & GMT_IO_EOF)) {	/* Not yet EOF */

			if (GMT_io.status & GMT_IO_MISMATCH) {
				fprintf (stderr, "%s: Mismatch between actual (%ld) and expected (%ld) fields near line %ld\n", GMT_program, n_fields, n_expected_fields, n);
				exit (EXIT_FAILURE);
			}
			while (GMT_io.status & GMT_IO_SEGMENT_HEADER) {	/* Segment header, get next record */
				n_fields = GMT_input (fp, &n_expected_fields, &in);
			}
			sincosd (in[GMT_Y], &sy, &cy);
			sincosd (in[GMT_X], &sx, &cx);
			xx[n] = cy * cx;
			yy[n] = cy * sx;
			zz[n] = sy;
			ww[n] = in[GMT_Z];
			if (Ctrl->Z.active) {
				if (ww[n] < w_min) w_min = ww[n];
				if (ww[n] > w_max) w_max = ww[n];
			}
			n++;

			if (n == (int)n_alloc) {	/* Get more memory */
				n_alloc <<= 1;
				xx = (double *) GMT_memory ((void *)xx, (size_t)n_alloc, sizeof (double), GMT_program);
				yy = (double *) GMT_memory ((void *)yy, (size_t)n_alloc, sizeof (double), GMT_program);
				zz = (double *) GMT_memory ((void *)zz, (size_t)n_alloc, sizeof (double), GMT_program);
				ww = (double *) GMT_memory ((void *)ww, (size_t)n_alloc, sizeof (double), GMT_program);
			}
		}

		if (fp != GMT_stdin) GMT_fclose (fp);
	}

	xx = (double *) GMT_memory ((void *)xx, (size_t)n, sizeof (double), GMT_program);
	yy = (double *) GMT_memory ((void *)yy, (size_t)n, sizeof (double), GMT_program);
	zz = (double *) GMT_memory ((void *)zz, (size_t)n, sizeof (double), GMT_program);
	ww = (double *) GMT_memory ((void *)ww, (size_t)n, sizeof (double), GMT_program);

	if (gmtdefs.verbose) fprintf (stderr, "%s: Do Delaunay triangulation using %ld points\n", GMT_program, n);

	if (Ctrl->Z.active && w_max > w_min) {	/* Scale the data */
		sf = 1.0 / (w_max - w_min);
		for (i = 0; i < n; i++) ww[i] *= sf;
	}
	
	/* Set up output grid */
	
	if (gmtdefs.verbose) fprintf (stderr, "%s: Evaluate output grid\n", GMT_program);
	h.node_offset = (int)Ctrl->F.active;
	h.x_inc = Ctrl->I.xinc;
	h.y_inc = Ctrl->I.yinc;
	GMT_RI_prepare (&h);	/* Ensure -R -I consistency and set nx, ny */
	GMT_err_fail (GMT_grd_RI_verify (&h, 1), Ctrl->G.file);

	h.xy_off = 0.5 * h.node_offset;
	nm = GMT_get_nm (h.nx, h.ny);
	surfd = (double *) GMT_memory (VNULL, (size_t)nm, sizeof(double), GMT_program);
	
	/* Do the interpolation */
	
	ssrfpack_grid (xx, yy, zz, ww, n, Ctrl->Q.mode, Ctrl->Q.value, Ctrl->T.active, gmtdefs.verbose, &h, surfd);
	GMT_free ((void *)xx);
	GMT_free ((void *)yy);
	GMT_free ((void *)zz);
	GMT_free ((void *)ww);
	
	/* Convert the doubles to float and unto the Fortran transpose order */
	
	sf = (w_max - w_min);
	surf = (float *) GMT_memory (VNULL, (size_t)nm, sizeof(float), GMT_program);
	for (j = ij = 0; j < h.ny; j++) {
		for (i = 0; i < h.nx; i++, ij++) {
			ij_f = i * h.ny + j;
			surf[ij] = (float)surfd[ij_f];
			if (Ctrl->Z.active) surf[ij] *= (float)sf;
		}
	}
	GMT_free ((void *)surfd);
	
	/* Write solution */
	
	GMT_err_fail (GMT_write_grd (Ctrl->G.file, &h, surf, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE), Ctrl->G.file);
	
	/* Free variables */
	
	GMT_free ((void *)surf);

	if (gmtdefs.verbose) fprintf (stderr, "%s: Done!\n", GMT_program);

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

	GMT_end (argc, argv);

	exit (EXIT_SUCCESS);
}
Esempio n. 5
0
int main (int argc, char **argv)
{
    GMT_LONG     i, symbol = 0, n, n_files = 0, fno;
    GMT_LONG     n_args;
    GMT_LONG     form_s = 0, justify_s = 5;
    
    GMT_LONG error = FALSE, nofile = TRUE;
    GMT_LONG done, greenwich, label_s = FALSE;
    GMT_LONG change_position = FALSE;
    GMT_LONG get_position = FALSE, get_size = FALSE, get_symbol = FALSE;
    GMT_LONG outline_E = FALSE, outline_G = FALSE, outline_F = FALSE;
    GMT_LONG old_GMT_world_map, skip_if_outside = TRUE;
    GMT_LONG plot_polS = FALSE, vecS = FALSE, scolor = FALSE, outline_s = FALSE;
    GMT_LONG def_cpen = FALSE, def_fpen = FALSE, def_tpen = FALSE;
    GMT_LONG def_gpen = FALSE, def_epen = FALSE, def_spen = FALSE;
    GMT_LONG hypo = FALSE;
    
    double west = 0.0, east = 0.0, south = 0.0, north = 0.0;
    double plot_x, plot_y, symbol_size = 0.0, symbol_size2;
    double lon, lat, plot_x0, plot_y0;
    double new_lon, new_lat, new_plot_x0, new_plot_y0;
    double radius, ech = 0., azimut, ih, plongement;
    double c_pointsize = 0.015, fontsize_s = 12.0;
    double angle_s = 0.0;
    double azS, sizeS = GMT_d_NaN;
    double v_width = GMT_d_NaN, h_length = GMT_d_NaN, h_width = GMT_d_NaN, shape;
    double si, co;

    char line[BUFSIZ], symbol_type, col[4][GMT_TEXT_LEN];
    char pol, *not_used = NULL;
    char txt_a[GMT_TEXT_LEN],txt_b[GMT_TEXT_LEN],txt_c[GMT_TEXT_LEN], txt_d[GMT_TEXT_LEN];
    char stacode[GMT_TEXT_LEN];
    
    FILE *fp = NULL;
    
    struct GMT_PEN pen, cpen, fpen, gpen, epen, spen, tpen;
    struct GMT_FILL fill, ffill, gfill, efill, sfill;
    struct GMT_FILL black, nofill;
    
    argc = (int)GMT_begin (argc, argv);
    
    GMT_init_pen (&pen, GMT_PENWIDTH);
    GMT_init_fill (&nofill, -1, -1, -1); 
    GMT_init_fill (&black, 0, 0, 0);     
    GMT_init_fill (&efill, 250, 250, 250);
    
    fill = nofill;
    ffill = nofill;
    sfill = nofill;
    gfill = black;

    /* Check and interpret the command line arguments */
    
    for (i = 1; !error && i < argc; i++) {
        if (argv[i][0] == '-') {
            switch(argv[i][1]) {
        
                /* Common parameters */
            
                case 'B':
                case 'H':
                case 'J':
                case 'K':
                case 'O':
                case 'P':
                case 'R':
                case 'U':
                case 'V':
                case 'X':
                case 'x':
                case 'Y':
                case 'y':
                case 'c':
                case '\0':
                    error += GMT_parse_common_options (argv[i], &west, &east, &south, &north);
                    break;
                
                /* Supplemental parameters */
            
                case 'C':       /* New coordinates */
                         change_position = TRUE;
                         sscanf(&argv[i][2], "%lf/%lf", &new_lon, &new_lat);
                         if(strchr(argv[i], 'W')) {
                             GMT_getpen (strchr(argv[i]+1, 'W')+1, &cpen);
                             def_cpen = TRUE;
                         }
                         if(strchr(argv[i], 'P')) {
                             sscanf(strchr(argv[i]+1, 'P')+1, "%lf", &c_pointsize);
                         }
                         break;
                case 'D':       /* Coordinates */
                         get_position = TRUE;
                         sscanf(&argv[i][2], "%lf/%lf", &lon, &lat);
                         break;
                case 'E':        /* Set color for station in extensive part */
                         GMT_getfill (&argv[i][2], &efill);
                         break;
                case 'e':        /* Outline station symbol in extensive part */
                         outline_E = TRUE;
                         if(strlen(argv[i]) > 2) {
                             GMT_getpen (&argv[i][2], &epen);
                             def_epen = TRUE;
                         }
                         break;
                case 'F':        /* Set background color of beach ball */
                         GMT_getfill (&argv[i][2], &ffill);
                         break;
                case 'f':        /* Outline beach ball */
                         outline_F = TRUE;
                         if(strlen(argv[i]) > 2) {
                             GMT_getpen (&argv[i][2], &fpen);
                             def_fpen = TRUE;
                         }
                         break;
                case 'G':        /* Set color for station in compressive part */
                         GMT_getfill (&argv[i][2], &gfill);
                         break;
                case 'g':        /* Outline station symbol in compressive part */
                         outline_G = TRUE;
                         if(strlen(argv[i]) > 2) {
                             GMT_getpen (&argv[i][2], &gpen);
                             def_gpen = TRUE;
                         }
                         break;
                case 'h':    /* Use HYPO71 format */
                         hypo = TRUE;
                         break;
                case 'M':    /* Focal sphere size */
                         get_size = TRUE;
                         sscanf(&argv[i][2], "%s", txt_a);
                         ech = GMT_convert_units (txt_a, GMT_INCH);
                         break;
                case 'N':        /* Do not skip points outside border */
                         skip_if_outside = FALSE;
                         break;
                case 'S':        /* Get symbol [and size] */
                         symbol_type = argv[i][2];
                         symbol_size = GMT_convert_units (&argv[i][3], GMT_INCH);
                         get_symbol = TRUE;
                         switch (symbol_type) {
                             case 'a':
                                 symbol = STAR;
                                 break;
                             case 'c':
                                 symbol = CIRCLE;
                                 break;
                             case 'd':
                                 symbol = DIAMOND;
                                 break;
                             case 'h':
                                 symbol = HEXAGON;
                                 break;
                             case 'i':
                                 symbol = ITRIANGLE;
                                 break;
                             case 'p':
                                 symbol = POINT;
                                 break;
                             case 's':
                                 symbol = SQUARE;
                                 break;
                             case 't':
                                 symbol = TRIANGLE;
                                 break;
                             case 'x':
                                 symbol = CROSS;
                                 break;
                             default:
                                 error = TRUE;
                                 fprintf (stderr, "%s: GMT SYNTAX ERROR -S option:  Unrecognized symbol type %c\n", argv[0], symbol_type);
                                 break;
                         }
                         break;
                case 's':        /* Get S polarity */
                         plot_polS = TRUE;
                         strcpy(txt_a, &argv[i][3]);
                         n=0; while (txt_a[n] && txt_a[n] != '/' && txt_a[n] != 'V' && txt_a[n] != 'G' && txt_a[n] != 'L') n++; txt_a[n]=0;
                         sizeS = GMT_convert_units (txt_a, GMT_INCH);

                         if(strchr(argv[i], 'V')) {
                             vecS = TRUE;
                             strcpy(txt_a,strchr(argv[i], 'V'));
                             if(strncmp(txt_a,"VG",(size_t)2) == 0 || strncmp(txt_a,"VL",(size_t)2) == 0 || strlen(txt_a) == 1) {
                                 v_width = 0.03; h_length = 0.12; h_width = 0.1; shape = gmtdefs.vector_shape;
                                 if (!gmtdefs.measure_unit) {
                                     v_width = 0.075; h_length = 0.3; h_width = 0.25; shape = gmtdefs.vector_shape;
                                 }
                             }
                             else {
                                strcpy(txt_a, strchr(argv[i], 'V')+1);
                                strcpy(txt_b, strchr(txt_a+1, '/')+1);
                                strcpy(txt_c, strchr(txt_b+1, '/')+1);
                                strcpy(txt_d, strchr(txt_c+1, '/')+1);
                                n=0; while (txt_a[n] && txt_a[n] != '/') n++; txt_a[n]=0;
                                n=0; while (txt_b[n] && txt_b[n] != '/') n++; txt_b[n]=0;
                                n=0; while (txt_c[n] && txt_c[n] != '/') n++; txt_c[n]=0;
                                n=0; while (txt_d[n] && txt_d[n] != '/' && txt_d[n] != 'L' && txt_d[n] != 'G') n++; txt_d[n]=0;
                                v_width = GMT_convert_units (txt_a, GMT_INCH);
                                h_length = GMT_convert_units (txt_b, GMT_INCH);
                                h_width = GMT_convert_units (txt_c, GMT_INCH);
                                shape = atof(txt_d);
                             }
                         }
                         if(strchr(argv[i], 'G')) {
                             sscanf (strchr(argv[i]+1,'G')+1,"%d/%d/%d",&sfill.rgb[0],&sfill.rgb[1],&sfill.rgb[2]);
                             sprintf(txt_a, "%d/%d/%d",sfill.rgb[0],sfill.rgb[1],sfill.rgb[2]);
                             GMT_getfill (txt_a, &sfill);
                             scolor = TRUE;
                         }
                         if(strchr(argv[i], 'L')) outline_s = TRUE;
                         break;
                case 'T':       /* Information about label printing */
                        label_s = TRUE;
                        if (strlen(argv[i]) > 2) {
                             sscanf (&argv[i][2], "%lf/%" GMT_LL "d/%" GMT_LL "d/%lf/", &angle_s,
                                     &form_s, &justify_s, &fontsize_s);
                        }
                        break;
                case 't':       /* Set color for station label */
                         GMT_getpen (&argv[i][2], &tpen);
                         def_tpen = TRUE;
                         break;

                case 'W':        /* Set line attributes */
                         GMT_getpen (&argv[i][2], &pen);
                         break;
                    
                /* Illegal options */
            
                default:        /* Options not recognized */
                         error = TRUE;
                         GMT_default_error (argv[i][1]);
                         break;
            }
        }
        else
            n_files++;
    }

    /* Check that the options selected are consistent */
    
    if (!project_info.region_supplied) {
        error++;
    }
    if(ech <= 0.) {
        error++;
    }
    if(get_position + get_size + get_symbol < 3) {
        error++;
    }
    
    if (argc == 1 || GMT_give_synopsis_and_exit || error) {    /* Display usage */
        fprintf (stderr,"%s %s - Plot polarities on the inferior focal half-sphere on maps\n\n",argv[0], GMT_VERSION);
        fprintf (stderr,"usage: argv[0] <infiles> %s %s\n", GMT_J_OPT, GMT_Rgeo_OPT);
        fprintf (stderr, " -Dlongitude/latitude -Msize[i/c] -S<symbol><size>[i/c]\n");
        fprintf (stderr, " [-A] [%s] [-Clongitude/latitude[W<pen>][Ppointsize]] [-E<fill>]\n", GMT_B_OPT);
        fprintf (stderr, " [-e[<pen>]] [-F<fill>] [-f[<pen>]] [-G<fill>] [-g[<pen>]] [%s] [-K] [-N] [-O] [-P]\n", GMT_Ho_OPT);
        fprintf (stderr, " [-s<half-size>/[V[<v_width/h_length/h_width/shape]][G<r/g/b>][L]\n");
        fprintf (stderr, " [-T[<labelinfo>]] [-t<pen>] [%s] [-V] [-W<pen>]\n", GMT_U_OPT);
        fprintf (stderr, " [%s] [%s] [%s]\n", GMT_X_OPT, GMT_Y_OPT, GMT_c_OPT);
        
        if (GMT_give_synopsis_and_exit) exit (EXIT_FAILURE);
        
        fprintf (stderr, "    <infiles> is one or more files.  If no, read standard input\n");
        GMT_explain_option ('j');
        GMT_explain_option ('R');
        fprintf (stderr, "        -D Set longitude/latitude\n");
        fprintf (stderr, "        -M Set size of beach ball in %s\n", GMT_unit_names[gmtdefs.measure_unit]);
        fprintf (stderr, "        -S to select symbol type and symbol size (in %s).  Choose between\n", GMT_unit_names[gmtdefs.measure_unit]);
        fprintf (stderr, "           st(a)r, (c)ircle, (d)iamond, (h)exagon, (i)nvtriangle\n");
        fprintf (stderr, "           (p)oint, (s)quare, (t)riangle, (x)cross\n");
        fprintf (stderr, "\n\tOPTIONS:\n");
        GMT_explain_option ('B');
        GMT_explain_option ('b');
        fprintf (stderr, "        -C Set new_longitude/new_latitude[W<pen>][Ppointsize]\n");
        fprintf (stderr, "           A line will be plotted between both positions\n");
        fprintf (stderr, "           Default is width = 3, color = current pen and pointsize = 0.015\n");
        fprintf (stderr, "        -E Specify color symbol for station in extensive part.\n");
        fprintf (stderr, "           Fill can be either <r/g/b> (each 0-255) for color \n");
        fprintf (stderr, "           or <gray> (0-255) for gray-shade [0].\n");
        fprintf (stderr, "           Default is light gray.\n");
        fprintf (stderr, "        -e Outline of station symbol in extensive part.\n");
        fprintf (stderr, "           Default is current pen.\n");
        fprintf (stderr, "        -F Specify background color of beach ball. It can be\n");
        fprintf (stderr, "           <r/g/b> (each 0-255) for color or <gray> (0-255) for gray-shade [0].\n");
        fprintf (stderr, "           Default is no fill\n");
        fprintf (stderr, "        -f Outline beach ball\n");
        fprintf (stderr, "           Add <pen attributes> if not current pen.\n");
        fprintf (stderr, "        -G Specify color symbol for station in compressive part. Fill can be either\n");
        fprintf (stderr, "           Fill can be either <r/g/b> (each 0-255) for color\n");
        fprintf (stderr, "           or <gray> (0-255) for gray-shade [0].\n");
        fprintf (stderr, "           Add L[<pen>] to outline\n");
        fprintf (stderr, "           Default is black.\n");
        fprintf (stderr, "        -g Outline of station symbol in compressive part.\n");
        fprintf (stderr, "           Add <pen attributes> if not current pen.\n");
        fprintf (stderr, "        -h Use special format derived from HYPO71 output.\n");
        GMT_explain_option ('H');
        GMT_explain_option ('K');
        fprintf (stderr, "        -N Do Not skip/clip symbols that fall outside map border\n");
        fprintf (stderr, "           [Default will ignore those outside]\n");
        GMT_explain_option ('O');
        GMT_explain_option ('P');
        fprintf (stderr, "        -s to plot S polarity azimuth.\n");
        fprintf (stderr, "           Azimuth of S polarity is in last column.\n");
        fprintf (stderr, "           It may be a vector (V option) or a segment. Give half-size in cm.\n");
        fprintf (stderr, "           L option is for outline\n");
        fprintf (stderr, "           -s<half-size>/[V[<v_width/h_length/h_width/shape>]][G<r/g/b>][L]\n");
        fprintf (stderr, "           Default definition of v is 0.075/0.3/0.25/1\n");
        fprintf (stderr, "           Outline is current pen\n");
        fprintf (stderr, "        -T[<info about labal printing>] to write station code.\n");
        fprintf (stderr, "           <angle/form/justify/fontsize in points>\n");
        fprintf (stderr, "           Default is 0.0/0/5/12\n");
        fprintf (stderr, "        -t sets pen attributes to write station codes [default is current pen]\n");
        GMT_explain_option ('U');
        GMT_explain_option ('V');
        fprintf (stderr, "        -W sets current pen attributes [width = %gp, color = (%d/%d/%d), texture = solid line].\n", pen.width, pen.rgb[0], pen.rgb[1], pen.rgb[2]);
        GMT_explain_option ('X');
        GMT_explain_option ('c');
        GMT_explain_option ('.');
        exit (EXIT_FAILURE);
    }
    
    if(!def_cpen) {
        cpen = pen; cpen.width = 3;
    }                                         /* pen for change position */
    if(!def_fpen) fpen = pen;                 /* outline beach ball */
    if(!def_gpen) gpen = pen;                 /* outline compressive stations */
    if(!def_epen) epen = pen;                 /* outline extensive stations */
    if(!def_spen) spen = pen;                 /* outline S_pol segment */
    if(!def_tpen) tpen = pen;                 /* pen to print station name */

    if (n_files > 0)
        nofile = FALSE;
    else
        n_files = 1;
    n_args = (argc > 1) ? argc : 2;
    
    greenwich = (west < 0.0 || east <= 0.0);
    
    GMT_err_fail (GMT_map_setup (west, east, south, north), "");

    GMT_plotinit (argc, argv);
    
    if (label_s) ps_setfont (gmtdefs.annot_font[0]);

    if (symbol > 0 && skip_if_outside) GMT_map_clip_on (GMT_no_rgb, 3);
    
    old_GMT_world_map = GMT_world_map;
    
    done = FALSE;


    for (fno = 1; !done && fno < n_args; fno++) {    /* Loop over all input files */
        if (!nofile && argv[fno][0] == '-') continue;
        if (nofile) {
            fp = GMT_stdin;
            done = TRUE;
        }
        else if ((fp = GMT_fopen (argv[fno], "r")) == NULL) {
            fprintf (stderr, "%s: Cannot open file %s\n", argv[0], argv[fno]);
            continue;
        }

        if (!nofile && gmtdefs.verbose) {
            fprintf (stderr, "%s: Working on file %s\n", argv[0], argv[fno]);
            sprintf (line, "File: %s", argv[fno]);
            ps_comment (line);
        }
        if (GMT_io.io_header[GMT_IN]) for (i = 0; i < GMT_io.n_header_recs; i++) not_used = GMT_fgets (line, 512, fp);
        
        GMT_world_map = TRUE;
        
        GMT_geo_to_xy(lon, lat, &plot_x0, &plot_y0);
        if(change_position) {
            GMT_setpen (&cpen);
            GMT_geo_to_xy(new_lon, new_lat, &new_plot_x0, &new_plot_y0);
            ps_circle(plot_x0, plot_y0, c_pointsize, cpen.rgb, 1);
            ps_plot(plot_x0, plot_y0, PSL_PEN_MOVE);
            ps_plot(new_plot_x0, new_plot_y0, PSL_PEN_DRAW_AND_STROKE);
            plot_x0 = new_plot_x0;
            plot_y0 = new_plot_y0;
        }
        if (skip_if_outside) {
            GMT_map_outside (lon, lat);
            if (GMT_abs (GMT_x_status_new) > 1 || GMT_abs (GMT_y_status_new) > 1) continue;
        }

        GMT_setpen (&fpen);
        ps_circle (plot_x0, plot_y0, ech, ffill.rgb, outline_F);

        while (GMT_fgets (line, BUFSIZ, fp)) {
            switch (hypo) {
                case 0 :
                    if(!plot_polS) {
                        sscanf (line, "%s %lf %lf %c", stacode, &azimut, &ih, &pol);
                    }
                    else {
                        n = sscanf (line, "%s %lf %lf %c %lf", stacode, &azimut, &ih, &pol, &azS);
                        if(n == 4) azS = -1.;
                    }
                    break;
                case 1 :
 	    	    memset ((void *)col, 0, 4 * GMT_TEXT_LEN * sizeof (char));
                   if(!plot_polS) {
                        sscanf (line, "%s %s %s %s %lf %lf %c", col[0], col[1], col[2], stacode, &azimut, &ih, col[3]);
                        pol = col[3][2];
                    }
                    else {
                        n = sscanf (line, "%s %s %s %s %lf %lf %c %lf", col[0], col[1], col[2], stacode, &azimut, &ih, col[3], &azS);
                        pol = col[3][2];
                        if(n == 7) azS = -1.;
                    }
                    break;
            }
        
            if(strcmp(col[0],"000000")!=0) {
                plongement = (ih - 90.) * M_PI / 180.;
                if(plongement  < 0.) {
                    plongement = -plongement;
                    azimut += 180 ;
                    symbol_size2 = symbol_size * 0.8;
                }
                else symbol_size2 = symbol_size;
                radius = sqrt(1. - sin(plongement));
                if(radius >= 0.97) radius = 0.97;
                azimut += 180;
                azimut *= M_PI / 180.;
                sincos (azimut, &si, &co);
                plot_x = radius * si * ech / 2. + plot_x0;
                plot_y = radius * co * ech / 2. + plot_y0;
                if (symbol == CROSS || symbol == POINT) ps_setpaint (fill.rgb);
            
                if(label_s) {
                    GMT_setpen (&tpen);
                    switch (justify_s) {
                        case 11 :
                            ps_text(plot_x-symbol_size2-0.1, plot_y-symbol_size2-0.1, fontsize_s, stacode, angle_s, 11, form_s);
                            break;
                        case 10 :
                            ps_text(plot_x, plot_y-symbol_size2-0.1, fontsize_s, stacode, angle_s, 10, form_s);
                            break;
                        case 9 :
                            ps_text(plot_x+symbol_size2+0.1, plot_y-symbol_size2-0.1, fontsize_s, stacode, angle_s, 9, form_s);
                            break;
                        case 7:
                            ps_text(plot_x-symbol_size2-0.1, plot_y, fontsize_s, stacode, angle_s, 7, form_s);
                            break;
                        case 6:
                            ps_text(plot_x, plot_y, fontsize_s, stacode, angle_s, 6, form_s);
                            break;
                        case 5:
                            ps_text(plot_x+symbol_size2+0.1, plot_y, fontsize_s, stacode, angle_s, 5, form_s);
                            break;
                        case 3:
                            ps_text(plot_x-symbol_size2-0.1, plot_y+symbol_size2+0.1, fontsize_s, stacode, angle_s, 3, form_s);
                            break;
                        case 2:
                            ps_text(plot_x, plot_y+symbol_size2+0.1, fontsize_s, col[3], angle_s, 2, form_s);
                            break;
                        case 1:
                            ps_text(plot_x+symbol_size2+0.1, plot_y+symbol_size2+0.1, fontsize_s, stacode, angle_s, 1, form_s);
                            break;
                    }
                }
    
                switch (symbol) {
                    case STAR:
                              if(pol == 'u' || pol == 'U' || pol == 'c' || pol == 'C' || pol == '+') {
                                  GMT_setpen (&gpen);
                                  ps_star (plot_x, plot_y, symbol_size2, gfill.rgb, outline_G);
                              }
                              else if(pol == 'r' || pol == 'R' || pol == 'd' || pol == 'D' || pol == '-') {
                                  GMT_setpen (&epen);
                                  ps_star (plot_x, plot_y, symbol_size2, efill.rgb, outline_E);
                              }
                              else {
                                  GMT_setpen (&pen);
                                  ps_cross (plot_x, plot_y, symbol_size2);
                              }
                              break;
                    case CROSS:
                              GMT_setpen (&pen);
                              ps_cross (plot_x, plot_y, symbol_size2);
                              break;
                    case POINT:
                              GMT_setpen (&pen);
                              ps_cross (plot_x, plot_y, POINTSIZE);
                              break;
                    case CIRCLE:
                              if(pol == 'u' || pol == 'U' || pol == 'c' || pol == 'C' || pol == '+') {
                                  GMT_setpen (&gpen);
                                  ps_circle (plot_x, plot_y, symbol_size2, gfill.rgb, outline_G);
                              }
                              else if(pol == 'r' || pol == 'R' || pol == 'd' || pol == 'D' || pol == '-') {
                                  GMT_setpen (&epen);
                                  ps_circle (plot_x, plot_y, symbol_size2, efill.rgb, outline_E);
                              }
                              else {
                                  GMT_setpen (&pen);
                                  ps_cross (plot_x, plot_y, symbol_size2);
                              }
                              break;
                    case SQUARE:
                              if(pol == 'u' || pol == 'U' || pol == 'c' || pol == 'C' || pol == '+') {
                                  GMT_setpen (&gpen);
                                  ps_square (plot_x, plot_y, symbol_size2, gfill.rgb, outline_G);
                              }
                              else if(pol == 'r' || pol == 'R' || pol == 'd' || pol == 'D' || pol == '-') {
                                  GMT_setpen (&epen);
                                  ps_square (plot_x, plot_y, symbol_size2, efill.rgb, outline_E);
                              }
                              else {
                                  GMT_setpen (&pen);
                                  ps_cross (plot_x, plot_y, symbol_size2);
                              }
                              break;
                    case HEXAGON:
                              if(pol == 'u' || pol == 'U' || pol == 'c' || pol == 'C' || pol == '+') {
                                  GMT_setpen (&gpen);
                                  ps_hexagon (plot_x, plot_y, symbol_size2, gfill.rgb, outline_G);
                              }
                              else if(pol == 'r' || pol == 'R' || pol == 'd' || pol == 'D' || pol == '-') {
                                  GMT_setpen (&epen);
                                  ps_hexagon (plot_x, plot_y, symbol_size2, efill.rgb, outline_E);
                              }
                              else {
                                  GMT_setpen (&pen);
                                  ps_cross (plot_x, plot_y, symbol_size2);
                              }
                              break;
                    case TRIANGLE:
                              if(pol == 'u' || pol == 'U' || pol == 'c' || pol == 'C' || pol == '+') {
                                  GMT_setpen (&gpen);
                                  ps_triangle (plot_x, plot_y, symbol_size2, gfill.rgb, outline_G);
                              }
                              else if(pol == 'r' || pol == 'R' || pol == 'd' || pol == 'D' || pol == '-') {
                                  GMT_setpen (&epen);
                                  ps_triangle (plot_x, plot_y, symbol_size2, efill.rgb, outline_E);
                              }
                              else {
                                  GMT_setpen (&pen);
                                  ps_cross (plot_x, plot_y, symbol_size2);
                              }
                              break;
                    case ITRIANGLE:
                              if(pol == 'u' || pol == 'U' || pol == 'c' || pol == 'C' || pol == '+') {
                                  GMT_setpen (&gpen);
                                  ps_itriangle (plot_x, plot_y, symbol_size2, gfill.rgb, outline_G);
                              }
                              else if(pol == 'r' || pol == 'R' || pol == 'd' || pol == 'D' || pol == '-') {
                                  GMT_setpen (&epen);
                                  ps_itriangle (plot_x, plot_y, symbol_size2, efill.rgb, outline_E);
                              }
                              else {
                                  GMT_setpen (&pen);
                                  ps_cross (plot_x, plot_y, symbol_size2);
                              }
                              break;
                    case DIAMOND:
                              if(pol == 'u' || pol == 'U' || pol == 'c' || pol == 'C' || pol == '+') {
                                  GMT_setpen (&gpen);
                                  ps_diamond (plot_x, plot_y, symbol_size2, gfill.rgb, outline_G);
                              }
                              else if(pol == 'r' || pol == 'R' || pol == 'd' || pol == 'D' || pol == '-') {
                                  GMT_setpen (&epen);
                                  ps_diamond (plot_x, plot_y, symbol_size2, efill.rgb, outline_E);
                              }
                              else {
                                  GMT_setpen (&pen);
                                  ps_cross (plot_x, plot_y, symbol_size2);
                              }
                              break;
                }
                if(plot_polS && azS >= 0.) {
                    GMT_setpen (&spen);
                    sincos (azS*M_PI/180., &si, &co);
                    if(vecS) {
                        ps_vector(plot_x - sizeS*si, plot_y - sizeS*co, 
                            plot_x + sizeS*si, plot_y + sizeS*co, v_width, 
                            h_length, h_width, gmtdefs.vector_shape, sfill.rgb, outline_s);
                    }
                    else { 
                        if(scolor) ps_setpaint (sfill.rgb);
                        else ps_setpaint (pen.rgb);
                        ps_plot(plot_x - sizeS*si, plot_y - sizeS*co, PSL_PEN_MOVE);
                        ps_plot(plot_x + sizeS*si, plot_y + sizeS*co, PSL_PEN_DRAW_AND_STROKE); 
                    }
                }
            }
        }
        if (fp != stdin) GMT_fclose (fp);
    }
    
    if (skip_if_outside) GMT_map_clip_off ();

    GMT_world_map = old_GMT_world_map;
    
    if (pen.texture[0]) ps_setdash (CNULL, 0);

    GMT_map_basemap ();

    GMT_plotend ();
    
    GMT_end (argc, argv);

    exit (EXIT_SUCCESS);
}
Esempio n. 6
0
int GMT_is_esri_grid (struct GMT_CTRL *GMT, struct GMT_GRID_HEADER *header) {
	/* Determine if file is an ESRI Interchange ASCII file */
	FILE *fp = NULL;
	char record[GMT_BUFSIZ];

	if (!strcmp (header->name, "="))
		return (GMT_GRDIO_PIPE_CODECHECK);	/* Cannot check on pipes */
	if ((fp = GMT_fopen (GMT, header->name, "r")) == NULL)
		return (GMT_GRDIO_OPEN_FAILED);

	if (fgets (record, GMT_BUFSIZ, fp) == NULL) {	/* Just get first line. Not using GMT_fgets since we may be reading a binary file */
		return (GMT_GRDIO_OPEN_FAILED);
	}
	GMT_fclose (GMT, fp);
	if (strncmp (record, "ncols ", 6) ) {
		/* Failed to find "ncols"; probably a binary file */
		char *file = NULL;
		size_t name_len;

		/* If it got here, see if a companion .hdr file exists (must test upper & lower cases names) */
		file = strdup (header->name);
		GMT_chop_ext (file);
		name_len = strlen (header->name);
		if (name_len < strlen(file) + 4) {
			/* The file extension had less than 3 chars, which means that 1) it's not an esri file.
			   2) would corrupt the heap with the later strcat (file, ".hdr");
			      On Win this would later cause a crash upon freeing 'file' */
			free (file);
			return (-1);
		}
		if (isupper ((unsigned char) header->name[name_len - 1]))
			strcat (file, ".HDR");
		else
			strcat (file, ".hdr");

		if (!GMT_access (GMT, file, F_OK)) {	/* Now, if first line has BYTEORDER or ncols keywords we are in the game */
			if ((fp = GMT_fopen (GMT, file, "r")) == NULL)
				return (GMT_GRDIO_OPEN_FAILED);
			GMT_fgets (GMT, record, GMT_BUFSIZ, fp);	/* Just get first line */
			GMT_fclose (GMT, fp);

			if (!strncmp (record, "BYTEORDER", 4) ) {
				sscanf (record, "%*s %c", &header->flags[0]);	/* Store the endianness flag here */
				strncpy (header->title, file, GMT_GRID_TITLE_LEN80);
			}
			else if (!strncmp (record, "ncols ", 6) ) {	/* Ah. A Arc/Info float binary file with a separate .hdr */
				strncpy (header->title, file, GMT_GRID_TITLE_LEN80);
				header->flags[0] = 'L';	/* If is truly 'L' or 'B' we'll find only when parsing the whole header */
				header->flags[1] = '2';	/* Flag to let us know the file type */
			}
			else {	/* Cannot do anything with this data */
				free (file);
				return (-1);
			}

			free (file);
		}
		else {
			/* No header file; see if filename contains w/e/s/n information, as in W|ExxxN|Syy.dem
			 * for GTOPO30 (e.g W020N90.DEM) or N|SyyW|Exxx.hgt for SRTM1|3 (e.g. N00E006.hgt)  */
			size_t len;

			while (GMT_chop_ext (file));	/* Remove all extensions so we know exactly where to look */
			len = strlen (file);
			if ((file[len-3] == 'N' || file[len-3] == 'n' || file[len-3] == 'S' || file[len-3] == 's') &&
				(file[len-7] == 'W' || file[len-7] == 'w' || file[len-7] == 'E' || file[len-7] == 'e')) {
				/* It is a GTOPO30 or SRTM30 source file without a .hdr companion. */
				/* see http://dds.cr.usgs.gov/srtm/version1/SRTM30/GTOPO30_Documentation */
				header->flags[0] = 'B';		/* GTOPO30 & SRTM30 are Big Endians */
				header->flags[1] = '0';		/* Flag to let us know the file type */
				/* Store the file name with all extensions removed.
				 * We'll use this to create header from file name info */
				strncpy (header->title, file, GMT_GRID_TITLE_LEN80);
				strcpy  (header->remark, "Assumed to be a GTOPO30 or SRTM30 tile");
			}
			else if (name_len > 3 && !(strncmp (&header->name[name_len-4], ".hgt", 4) && strncmp (&header->name[name_len-4], ".HGT", 4))) {
				/* Probably a SRTM1|3 file. In read_esri_info we'll check further if it is a 1 or 3 sec */
				if ((file[len-4] == 'E' || file[len-4] == 'e' || file[len-4] == 'W' || file[len-4] == 'w') &&
					(file[len-7] == 'N' || file[len-7] == 'n' || file[len-7] == 'S' || file[len-7] == 's')) {
					header->flags[0] = 'B';	/* SRTM1|3 are Big Endians */
					header->flags[1] = '1';	/* Flag to let us know the file type */
					/* Store the file name with all extensions removed.
					 * We'll use this to create header from file name info */
					strncpy (header->title, file, GMT_GRID_TITLE_LEN80);
				}
			}
			else {
				/* Cannot do anything with this data */
				free (file);
				return (-1);	/* Not this kind of file */
			}
		}
	}

	header->type = GMT_GRID_IS_EI;
	return GMT_NOERROR;
}
Esempio n. 7
0
int read_esri_info (struct GMT_CTRL *GMT, FILE *fp, struct GMT_GRID_HEADER *header)
{
	int c;
	char record[GMT_BUFSIZ];
	FILE *fp2 = NULL, *fpBAK = NULL;

	header->registration = GMT_GRID_NODE_REG;
	header->z_scale_factor = 1.0;
	header->z_add_offset   = 0.0;

	if (header->flags[0] == 'M' || header->flags[0] == 'I') {	/* We are dealing with a ESRI .hdr file */
		int error;
		if ((error = read_esri_info_hdr (GMT, header))) 		/* Continue the work someplace else */
			return (error);
		else
			return (GMT_NOERROR);
	}
	else if (header->flags[0] == 'B' && header->flags[1] == '0') {	/* A GTOPO30 or SRTM30 file */
		size_t len = strlen (header->title);

		header->inc[GMT_X] = header->inc[GMT_Y] = 30.0 * GMT_SEC2DEG;	/* 30 arc seconds */
		header->wesn[YHI] = atof (&header->title[len-2]);
		if ( header->title[len-3] == 'S' || header->title[len-3] == 's' ) header->wesn[YHI] *= -1; 
		c = header->title[len-3];
		header->title[len-3] = '\0';
		header->wesn[XLO] = atof (&header->title[len-6]);
		header->title[len-3] = (char)c;		/* Reset because this function is called at least twice */
		if ( header->title[len-7] == 'W' || header->title[len-7] == 'w' ) header->wesn[XLO] *= -1; 
		if (header->wesn[YHI] > -60) {
			header->wesn[YLO] = header->wesn[YHI] - 50; 
			header->wesn[XHI] = header->wesn[XLO] + 40; 
			header->nx = 4800;
			header->ny = 6000;
		}
		else {	/* Antarctica tiles cover 30 degrees of latitude and 60 degrees of longitude each have 3,600 rows and 7,200 columns */
			header->wesn[YLO] = -90; 
			header->wesn[XHI] = header->wesn[XLO] + 60; 
			header->nx = 7200;
			header->ny = 3600;
		}
		header->registration = GMT_GRID_PIXEL_REG;
		GMT_set_geographic (GMT, GMT_IN);
		gmt_grd_set_units (GMT, header);
		
		/* Different sign of NaN value between GTOPO30 and SRTM30 grids */
		if (strstr (header->name, ".DEM") || strstr (header->name, ".dem"))
			header->nan_value = -9999.0f;
		else
			header->nan_value = 9999.0f;
		header->bits = 16;		/* Temp pocket to store number of bits */
		return (GMT_NOERROR);
	}
	else if (header->flags[0] == 'B' && header->flags[1] == '1') {	/* A SRTM3 or SRTM1 file */
		size_t len = strlen (header->title);
		struct stat F;

		header->wesn[XLO] = atof (&header->title[len-3]);
		if ( header->title[len-4] == 'W' || header->title[len-4] == 'W' ) header->wesn[XLO] *= -1; 
		c = header->title[len-4];
		header->title[len-4] = '\0';
		header->wesn[YLO] = atof (&header->title[len-6]);
		header->title[len-4] = (char)c;		/* Reset because this function is called at least twice */
		if ( header->title[len-7] == 'S' || header->title[len-7] == 's' ) header->wesn[YLO] *= -1; 
		header->wesn[YHI] = header->wesn[YLO] + 1; 
		header->wesn[XHI] = header->wesn[XLO] + 1; 
		header->nan_value = -32768.0f;
		header->bits = 16;		/* Temp pocket to store number of bits */
		stat (header->name, &F);	/* Must finally find out if it is a 1 or 3 arcseconds file */
		if (F.st_size < 3e6) {		/* Actually the true size is 2884802 */
			header->inc[GMT_X] = header->inc[GMT_Y] = 3.0 * GMT_SEC2DEG;	/* 3 arc seconds */
			strcpy (header->remark, "Assumed to be a SRTM3 tile");
		}
		else {
			header->inc[GMT_X] = header->inc[GMT_Y] = 1.0 * GMT_SEC2DEG;	/* 1 arc second  */
			strcpy (header->remark, "Assumed to be a SRTM1 tile");
		}
		GMT_set_geographic (GMT, GMT_IN);
		gmt_grd_set_units (GMT, header);
		return (GMT_NOERROR);
	}
	else if ((header->flags[0] == 'L' || header->flags[0] == 'B') && header->flags[1] == '2') {	/* A Arc/Info BINARY file */
		if ((fp2 = GMT_fopen (GMT, header->title, "r")) == NULL) return (GMT_GRDIO_OPEN_FAILED);
		/* To use the same parsing header code as in the ASCII file case where header and data are in the
		   same file, we will swap the file pointers and undo the swap at the end of this function */
		fpBAK = fp;	/* Copy of the input argument '*fp' */
		fp = fp2;
	}

	GMT_fgets (GMT, record, GMT_BUFSIZ, fp);
	if (sscanf (record, "%*s %d", &header->nx) != 1) {
		GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Arc/Info ASCII Grid: Error decoding ncols record\n");
		return (GMT_GRDIO_READ_FAILED);
	}
	GMT_fgets (GMT, record, GMT_BUFSIZ, fp);
	if (sscanf (record, "%*s %d", &header->ny) != 1) {
		GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Arc/Info ASCII Grid: Error decoding nrows record\n");
		return (GMT_GRDIO_READ_FAILED);
	}
	GMT_fgets (GMT, record, GMT_BUFSIZ, fp);
	if (sscanf (record, "%*s %lf", &header->wesn[XLO]) != 1) {
		GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Arc/Info ASCII Grid: Error decoding xll record\n");
		return (GMT_GRDIO_READ_FAILED);
	}
	GMT_str_tolower (record);
	if (!strncmp (record, "xllcorner", 9U)) header->registration = GMT_GRID_PIXEL_REG;	/* Pixel grid */
	GMT_fgets (GMT, record, GMT_BUFSIZ, fp);
	if (sscanf (record, "%*s %lf", &header->wesn[YLO]) != 1) {
		GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Arc/Info ASCII Grid: Error decoding yll record\n");
		return (GMT_GRDIO_READ_FAILED);
	}
	GMT_str_tolower (record);
	if (!strncmp (record, "yllcorner", 9U)) header->registration = GMT_GRID_PIXEL_REG;	/* Pixel grid */
	GMT_fgets (GMT, record, GMT_BUFSIZ, fp);
	if (sscanf (record, "%*s %lf", &header->inc[GMT_X]) != 1) {
		GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Arc/Info ASCII Grid: Error decoding cellsize record\n");
		return (GMT_GRDIO_READ_FAILED);
	}
	/* Handle the optional nodata_value record */
	c = fgetc (fp);	/* Get first char of next line... */
	ungetc (c, fp);	/* ...and put it back where it came from */
	if (c == 'n' || c == 'N') {	/*	Assume this is a nodata_value record since we found an 'n|N' */
		GMT_fgets (GMT, record, GMT_BUFSIZ, fp);
		if (sscanf (record, "%*s %f", &header->nan_value) != 1) {
			GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Arc/Info ASCII Grid: Error decoding nan_value_value record\n");
			return (GMT_GRDIO_READ_FAILED);
		}
	}
	header->inc[GMT_Y] = header->inc[GMT_X];
	header->wesn[XHI] = header->wesn[XLO] + (header->nx - 1 + header->registration) * header->inc[GMT_X];
	header->wesn[YHI] = header->wesn[YLO] + (header->ny - 1 + header->registration) * header->inc[GMT_Y];

	GMT_err_fail (GMT, GMT_grd_RI_verify (GMT, header, 1), header->name);

	if (fpBAK) {		/* Case of Arc/Info binary file with a separate header file. We still have things to do. */
		char tmp[16];
		/* Read an extra record containing the endianness info */
		GMT_fgets (GMT, record, GMT_BUFSIZ, fp);
		if (sscanf (record, "%*s %s", tmp) != 1) {
			GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Arc/Info BINARY Grid: Error decoding endianness record\n");
			return (GMT_GRDIO_READ_FAILED);
		}
		header->flags[0] = (tmp[0] == 'L') ? 'L' : 'B';

		header->bits = 32;	/* Those float binary files */
		/* Ok, now as mentioned above undo the file pointer swapping (point again to data file) */
		fp = fpBAK;
		GMT_fclose (GMT, fp2);
	}

	return (GMT_NOERROR);
}
Esempio n. 8
0
int read_esri_info_hdr (struct GMT_CTRL *GMT, struct GMT_GRID_HEADER *header) {
	/* Parse the contents of a .HDR file */
	int nB;
	char record[GMT_BUFSIZ];
	FILE *fp = NULL;

	if ((fp = GMT_fopen (GMT, header->title, "r")) == NULL) return (GMT_GRDIO_OPEN_FAILED);

	header->registration = GMT_GRID_NODE_REG;
	header->z_scale_factor = 1.0;
	header->z_add_offset   = 0.0;

	GMT_fgets (GMT, record, GMT_BUFSIZ, fp);		/* BYTEORDER */ 
	GMT_fgets (GMT, record, GMT_BUFSIZ, fp);		/* LAYOUT */
	GMT_fgets (GMT, record, GMT_BUFSIZ, fp);
	if (sscanf (record, "%*s %d", &header->ny) != 1) {
		GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Arc/Info ASCII Grid: Error decoding NROWS record\n");
		return (GMT_GRDIO_READ_FAILED);
	}
	GMT_fgets (GMT, record, GMT_BUFSIZ, fp);
	if (sscanf (record, "%*s %d", &header->nx) != 1) {
		GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Arc/Info ASCII Grid: Error decoding NCOLS record\n");
		return (GMT_GRDIO_READ_FAILED);
	}
	GMT_fgets (GMT, record, GMT_BUFSIZ, fp);
	if (sscanf (record, "%*s %d", &nB) != 1) {
		GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Arc/Info ASCII Grid: Error decoding NBANDS record\n");
		return (GMT_GRDIO_READ_FAILED);
	}
	if (nB != 1) {
		GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Arc/Info ASCII Grid: Cannot read file with number of Bands != 1 \n");
		return (GMT_GRDIO_READ_FAILED);
	}
	GMT_fgets (GMT, record, GMT_BUFSIZ, fp);
	if (sscanf (record, "%*s %d", &header->bits) != 1) {
		GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Arc/Info ASCII Grid: Error decoding NBITS record\n");
		return (GMT_GRDIO_READ_FAILED);
	}
	if ( header->bits != 16 && header->bits != 32 ) {
		GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Arc/Info ASCII Grid: This data type (%d bits) is not supported\n", header->bits);
		return (GMT_GRDIO_READ_FAILED);
	}
	GMT_fgets (GMT, record, GMT_BUFSIZ, fp);		/* BANDROWBYTES  */ 
	GMT_fgets (GMT, record, GMT_BUFSIZ, fp);		/* TOTALROWBYTES */
	GMT_fgets (GMT, record, GMT_BUFSIZ, fp);		/* BANDGAPBYTES  */
	GMT_fgets (GMT, record, GMT_BUFSIZ, fp);
	while (strncmp (record, "NODATA", 4) )		/* Keep reading till find this keyword */
		GMT_fgets (GMT, record, GMT_BUFSIZ, fp);
	if (sscanf (record, "%*s %f", &header->nan_value) != 1) {
		GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Arc/Info ASCII Grid: Error decoding nan_value_value record\n");
		return (GMT_GRDIO_READ_FAILED);
	}
	GMT_fgets (GMT, record, GMT_BUFSIZ, fp);
	if (sscanf (record, "%*s %lf", &header->wesn[XLO]) != 1) {
		GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Arc/Info ASCII Grid: Error decoding ULXMAP record\n");
		return (GMT_GRDIO_READ_FAILED);
	}
	GMT_fgets (GMT, record, GMT_BUFSIZ, fp);
	if (sscanf (record, "%*s %lf", &header->wesn[YHI]) != 1) {
		GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Arc/Info ASCII Grid: Error decoding ULYMAP record\n");
		return (GMT_GRDIO_READ_FAILED);
	}
	GMT_fgets (GMT, record, GMT_BUFSIZ, fp);
	if (sscanf (record, "%*s %lf", &header->inc[GMT_X]) != 1) {
		GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Arc/Info ASCII Grid: Error decoding XDIM record\n");
		return (GMT_GRDIO_READ_FAILED);
	}
	GMT_fgets (GMT, record, GMT_BUFSIZ, fp);
	if (sscanf (record, "%*s %lf", &header->inc[GMT_Y]) != 1) {
		GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Arc/Info ASCII Grid: Error decoding YDIM record\n");
		return (GMT_GRDIO_READ_FAILED);
	}
			
	GMT_fclose (GMT, fp);

	header->wesn[XHI] = header->wesn[XLO] + (header->nx - 1 + header->registration) * header->inc[GMT_X];
	header->wesn[YLO] = header->wesn[YHI] - (header->ny - 1 + header->registration) * header->inc[GMT_Y];

	GMT_err_fail (GMT, GMT_grd_RI_verify (GMT, header, 1), header->name);

	return (GMT_NOERROR);
}