File: grdedit.c

package info (click to toggle)
gmt 3.3.3-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 3,288 kB
  • ctags: 2,962
  • sloc: ansic: 50,470; sh: 1,760; makefile: 284; asm: 38
file content (209 lines) | stat: -rw-r--r-- 5,934 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*--------------------------------------------------------------------
 *    The GMT-system:	@(#)grdedit.c	2.36  09/21/99
 *
 *	Copyright (c) 1991-1999 by P. Wessel and W. H. F. Smith
 *	See COPYING file for copying and redistribution conditions.
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; version 2 of the License.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	Contact info: www.soest.hawaii.edu/gmt
 *--------------------------------------------------------------------*/
/*
 * grdedit reads an existing grdfile and takes command line
 * arguments to redefine some of the grdheader parameters:
 *
 *	x_min/x_max OR x_inc (the other is recomputed)
 *	y_min/y_max OR y_inc (the other is recomputed)
 *	z_scale_factor/z_add_offset
 *	x_units/y_units/z_units
 *	title/command/remark
 *
 * Author:	Paul Wessel
 * Date:	9-SEP-1998
 * Version:	3.1
 */
 
#include "gmt.h"

float *a;

main (int argc, char **argv)
{
	BOOLEAN error = FALSE, got_input = FALSE, got_region = FALSE, shift = FALSE, adjust = FALSE;
	
	int i, entry, one_or_zero;

	double w, e, s, n, shift_amount = 0.0;
	
	char *grdfile, input[BUFSIZ], *ptr;
	
	struct GRD_HEADER grd;
	
	input[0] = 0;
	grdfile = CNULL;
	w = e = s = n = 0.0;
	
	argc = GMT_begin (argc, argv);

	for (i = 1; i < argc; i++) {
		if (argv[i][0] == '-') {
			switch (argv[i][1]) {
				/* Common parameters */
			
				case 'R':
				case '\0':
					error += GMT_get_common_args (argv[i], &w, &e, &s, &n);
					if (argv[i][1] == 'R') got_region = TRUE;
					break;

				/* Supplemental parameters */
			
				case 'D':
					strcpy (input, &argv[i][2]);
					got_input = TRUE;
					break;
				case 'A':
					adjust = TRUE;
					break;
				case 'S':
					shift = TRUE;
					break;
				default:
					error = TRUE;
					GMT_default_error (argv[i][1]);
					break;
			}
		}
		else
			grdfile = argv[i];
	}
		
	if (argc == 1 || GMT_quick) {
		fprintf (stderr, "grdedit %s - Modifying the header in a netCDF grdfile\n\n", GMT_VERSION);
		fprintf (stderr, "usage: grdedit grdfile [-A] [-R<west>/<east>/<south>/<north>]\n");
		fprintf (stderr, "	[-S] [-Dxunit/yunit/zunit/scale/offset/title/remark]\n");
		
		if (GMT_quick) exit (EXIT_FAILURE);
		
		fprintf (stderr, "	grdfile is file to be modified\n");
		fprintf (stderr, "\n\tOPTIONS:\n");
		fprintf (stderr, "	-A will adjust dx/dy to be compatible with the files domain or -R\n");
		fprintf (stderr, "	-D to enter information.  Specify '=' to get default value\n");
		GMT_explain_option ('R');
		fprintf (stderr, "	-S For global grids of 360 degree longitude range.\n");
		fprintf (stderr, "	   Will rotate entire grid to coincide with new borders in -R\n");
		exit (EXIT_FAILURE);
	}
	
	if (shift && adjust) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR -S option:  Incompatible with -A\n", GMT_program);
		error++;
	}
	if (shift && !got_region) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR -S option:  Must also specify -R\n", GMT_program);
		error++;
	}
	if (shift && fabs (w - e) != 360.0) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR -S option:  -R longitudes must span exactly 360 degrees\n", GMT_program);
		error++;
	}
	if (!grdfile) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR:  Must specify input file\n", GMT_program);
		error++;
	}
	if (error) exit (EXIT_FAILURE);

	GMT_put_history (argc, argv);	/* Update .gmtcommands */

			
	if (!strcmp (grdfile,  "=")) {
		fprintf (stderr, "%s: Piping of grdfile not supported!\n", GMT_program);
		exit (EXIT_FAILURE);
	}

	if (GMT_read_grd_info (grdfile, &grd)) {
		fprintf (stderr, "%s: Error opening file %s\n", GMT_program, grdfile);
		exit (EXIT_FAILURE);
	}
	
	if (shift && fabs (grd.x_max - grd.x_min) != 360.0) {
		fprintf (stderr, "%s: Shift only allowed for global grids\n", GMT_program);
		exit (EXIT_FAILURE);
	}
	
	/* Decode grd information given, if any */
	
	if (got_input) {
		ptr = strtok (input, "/");
		entry = 0;
		while (ptr) {
			switch (entry) {
				case 0:
					if (ptr[0] != '=') strcpy (grd.x_units, ptr);
					break;
				case 1:
					if (ptr[0] != '=') strcpy (grd.y_units, ptr);
					break;
				case 2:
					if (ptr[0] != '=') strcpy (grd.z_units, ptr);
					break;
				case 3:
					if (ptr[0] != '=') grd.z_scale_factor = atof (ptr);
					break;
				case 4:
					if (ptr[0] != '=') grd.z_add_offset = atof (ptr);
					break;
				case 5:
					if (ptr[0] != '=') strcpy (grd.title, ptr);
					break;
				case 6:
					if (ptr[0] != '=') strcpy (grd.remark, ptr);
					break;
				default:
					break;
			}
			ptr = strtok (CNULL, "/");
			entry++;
		}
	}
	
	if (!shift) {
		one_or_zero = (grd.node_offset) ? 0 : 1;
		if (got_region) {
			grd.x_min = w;	grd.x_max = e;
			grd.y_min = s;	grd.y_max = n;
			adjust = TRUE;	/* Must ensure -R -I compatibility */
		}
		if (adjust) {
			grd.x_inc = (grd.x_max - grd.x_min) / (grd.nx - one_or_zero);
			grd.y_inc = (grd.y_max - grd.y_min) / (grd.ny - one_or_zero);
		}
		if (GMT_write_grd_info (grdfile, &grd)) {
			fprintf (stderr, "%s: Error updating file %s\n", GMT_program, grdfile);
			exit (EXIT_FAILURE);
		}
	}
	else {
		shift_amount = w - grd.x_min;
		a = (float *) GMT_memory (VNULL, (size_t)(grd.nx * grd.ny), sizeof (float), GMT_program);
		if (GMT_read_grd (grdfile, &grd, a, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE)) {
			fprintf (stderr, "%s: Error reading file %s\n", GMT_program, grdfile);
			exit (EXIT_FAILURE);
		}
		GMT_grd_shift (&grd, a, shift_amount);
		if (GMT_write_grd (grdfile, &grd, a, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE)) {
			fprintf (stderr, "%s: Error writing file %s\n", GMT_program, grdfile);
			exit (EXIT_FAILURE);
		}
		GMT_free ((void *)a);
	}
	
	GMT_end (argc, argv);
}