-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeopolmap.c
404 lines (311 loc) · 9.03 KB
/
geopolmap.c
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
-------------------------------------------------------------------------
OBJECT NAME: geopolmap.c
FULL NAME: Direction Arrows.
ENTRY POINTS: DrawGeoPolMapXY()
DrawGeoPolMapXYZ()
ToggleGeoPolMap()
STATIC FNS: createCoastCommand()
setColor()
DESCRIPTION:
COPYRIGHT: University Corporation for Atmospheric Research, 1997-2006
-------------------------------------------------------------------------
*/
#include "define.h"
#include "ps.h"
#include <sys/stat.h>
#include <Xm/TextF.h>
static bool GeoPolMap = False;
static char gmt_path[128];
static void createCoastCommand(char buf[], struct axisInfo *xAxis, struct axisInfo *yAxis);
static void setColor(PLOT_INFO * plot, char str[], FILE *fp);
/* -------------------------------------------------------------------- */
void ToggleGeoPolMap(Widget w, XtPointer client, XtPointer call)
{
GeoPolMap = !GeoPolMap;
DrawMainWindow();
} /* END TOGGLEGEOPOLMAP */
/* -------------------------------------------------------------------- */
void DrawGeoPolMapXY(PLOT_INFO *plot, FILE *fp)
{
FILE *in;
int i, cnt, reqSize;
XPoint *pts;
NR_TYPE datumX, datumY;
float xScale, yScale, xMin, yMin;
struct axisInfo *xAxis, *yAxis;
struct plot_offset *offsets;
if (!GeoPolMap)
return;
/* Setup useful consts
*/
xAxis = &plot->Xaxis;
yAxis = &plot->Yaxis[0];
xMin = xAxis->min;
yMin = yAxis->min;
if (fp)
offsets = &plot->ps;
else
offsets = &plot->x;
xScale = (float)offsets->HD / (xAxis->max - xMin);
yScale = (float)offsets->VD / (yAxis->max - yMin);
/* Open pipe to pscoast
*/
createCoastCommand(buffer, xAxis, yAxis);
if ((in = popen(buffer, "r")) == NULL)
{
fprintf(stderr, "pscoast command failed.\n");
fprintf(stderr, " %s\n", buffer);
return;
}
if (fp) /* PostScript */
{
fprintf(fp, "gsave\n");
fprintf(fp, "stroke 0 0 0 setrgbcolor\n");
PSclip(fp, plot);
}
else
{
XSetForeground(plot->dpy, plot->gc, GetColor(0));
setClippingX(plot);
}
reqSize = (XMaxRequestSize(plot->dpy) - 3) >> 1;
pts = new XPoint[reqSize];
while (fgets(buffer, 1024, in))
{
if (buffer[0] == '#' || buffer[0] == '>')
{
setColor(plot, buffer, fp);
continue;
}
cnt = 0;
do
{
sscanf(buffer, "%f %f", &datumX, &datumY);
if (datumX > 180.0)
datumX -= 360.0;
if (fp)
{
pts[cnt].x = (int)(xScale * (datumX - xMin));
pts[cnt].y = (int)(yScale * (datumY - yMin));
}
else
{
pts[cnt].x = (int)(offsets->LV + (xScale * (datumX - xMin)));
pts[cnt].y = (int)(offsets->BH - (yScale * (datumY - yMin)));
}
++cnt;
}
while (fgets(buffer, 1024, in) && buffer[0] != '>');
setColor(plot, buffer, fp);
if (fp)
{
fprintf(fp, moveto, pts[0].x, pts[0].y);
for (i = 1; i < cnt; ++i)
fprintf(fp, lineto, pts[i].x, pts[i].y);
fprintf(fp, "stroke\n");
}
else
XDrawLines(plot->dpy, plot->win, plot->gc, pts, cnt, CoordModeOrigin);
}
delete [] pts;
pclose(in);
if (fp) /* PostScript */
{
PSclearClip(fp);
fprintf(fp, "stroke grestore\n");
}
else
{
XSetClipMask(plot->dpy, plot->gc, None);
XSetForeground(plot->dpy, plot->gc, CurrentColor());
}
} /* END DRAWGEOPOLMAP */
/* -------------------------------------------------------------------- */
void DrawGeoPolMapXYZ(PLOT_INFO *plot, int ZD, float cosFac, float sinFac, FILE *fp)
{
FILE *in;
int i, cnt, reqSize;
XPoint *pts;
NR_TYPE datumX, datumY;
float xScale, yScale, zScale, xMin, zMin, xMax, zMax, x, y;
struct axisInfo *xAxis, *yAxis;
struct plot_offset *offsets;
if (!GeoPolMap)
return;
/* Setup useful consts
*/
xAxis = &plot->Xaxis;
yAxis = &plot->Yaxis[0];
xMin = xAxis->min;
xMax = xAxis->max;
zMin = plot->Zaxis.min;
zMax = plot->Zaxis.max;
if (fp)
offsets = &plot->ps;
else
offsets = &plot->x;
xScale = (float)offsets->HD / (xAxis->max - xMin);
yScale = (float)offsets->VD / (yAxis->max - yAxis->min);
zScale = (float)ZD / (zMax - zMin);
/* Open pipe to pscoast
*/
createCoastCommand(buffer, xAxis, &plot->Zaxis);
if ((in = popen(buffer, "r")) == NULL)
{
fprintf(stderr, "pscoast command failed.\n");
fprintf(stderr, " %s\n", buffer);
return;
}
if (fp) /* PostScript */
{
fprintf(fp, "gsave\n");
fprintf(fp, "stroke 0 0 0 setrgbcolor\n");
}
else
XSetForeground(plot->dpy, plot->gc, GetColor(0));
reqSize = (XMaxRequestSize(plot->dpy) - 3) >> 1;
pts = new XPoint[reqSize];
while (fgets(buffer, 1024, in))
{
if (buffer[0] == '#' || buffer[0] == '>')
{
setColor(plot, buffer, fp);
continue;
}
cnt = 0;
do
{
sscanf(buffer, "%f %f", &datumX, &datumY);
if (datumX > 180.0)
datumX -= 360.0;
if (datumY < zMin || datumY > zMax || datumX < xMin || datumX > xMax)
continue;
if (fp)
{
x = xScale * (datumX - xMin);
y = yScale * (0.0 - yAxis->min);
x += cosFac * (zScale * (datumY - zMin));
y += sinFac * (zScale * (datumY - zMin));
}
else
{
x = offsets->LV + (xScale * (datumX - xMin));
y = offsets->BH - (yScale * (0.0 - yAxis->min));
x += cosFac * (zScale * (datumY - zMin));
y -= sinFac * (zScale * (datumY - zMin));
}
pts[cnt].x = (int)x;
pts[cnt].y = (int)y;
++cnt;
}
while (fgets(buffer, 1024, in) && buffer[0] != '>');
setColor(plot, buffer, fp);
if (fp)
{
fprintf(fp, moveto, pts[0].x, pts[0].y);
for (i = 1; i < cnt; ++i)
fprintf(fp, lineto, pts[i].x, pts[i].y);
fprintf(fp, "stroke\n");
}
else
XDrawLines(plot->dpy, plot->win, plot->gc, pts, cnt, CoordModeOrigin);
}
delete [] pts;
pclose(in);
if (fp) /* PostScript */
fprintf(fp, "stroke grestore\n");
else
XSetForeground(plot->dpy, plot->gc, CurrentColor());
} /* END DRAWGEOPOLMAPXYZ */
/* -------------------------------------------------------------------- */
bool TestForGMT()
{
bool rc = false;
char *env;
struct stat sb;
gmt_path[0] = '\0';
if ( (env = getenv("GMTHOME")) ) // home built GMT
{
char test_path[128];
strcpy(test_path, env);
strcat(test_path, "/bin/gmt");
stat(test_path, &sb);
if ((sb.st_mode & S_IFMT) == S_IFREG) {
strcpy(gmt_path, env);
rc = true;
}
}
stat("/usr/local/bin/gmt", &sb);
if ((sb.st_mode & S_IFMT) == S_IFREG) {
strcpy(gmt_path, "/usr/local");
rc = true;
}
stat("/opt/homebrew/bin/gmt", &sb);
if ((sb.st_mode & S_IFMT) == S_IFREG) {
strcpy(gmt_path, "/opt/homebrew");
rc = true;
}
stat("/bin/gmt", &sb);
if ((sb.st_mode & S_IFMT) == S_IFREG) {
rc = true;
}
return rc;
}
/* -------------------------------------------------------------------- */
static void createCoastCommand(char buf[], struct axisInfo *xAxis, struct axisInfo *yAxis)
{
char scale_str[16], river_str[16], command[256];
float scale;
int xMin, xMax, yMin, yMax;
xMin = (xAxis->min < 0.0) ? (int)xAxis->min-1 : (int)xAxis->min;
yMin = (yAxis->min < 0.0) ? (int)yAxis->min-1 : (int)yAxis->min;
xMax = (xAxis->max < 0.0) ? (int)xAxis->max : (int)xAxis->max+1;
yMax = (yAxis->max < 0.0) ? (int)yAxis->max : (int)yAxis->max+1;
/* Determine which resolution GMT database to use.
*/
scale = sqrt((xAxis->max - xAxis->min) * (yAxis->max - yAxis->min));
if (scale > 60) strcpy(scale_str, "-Dc"); else
if (scale > 40) strcpy(scale_str, "-Dl"); else
if (scale > 20) strcpy(scale_str, "-Di"); else
if (scale > 10) strcpy(scale_str, "-Dh"); else
strcpy(scale_str, "-Df");
if (scale > 40) strcpy(river_str, ""); else
if (scale > 20) strcpy(river_str, " -I1"); else
if (scale > 10) strcpy(river_str, " -Ir"); else
strcpy(river_str, " -Ia");
/* GMT5 only supports one of borders, shores, or rivers in in one command.
* So now we have to string multiple pscoast commands together.
*/
snprintf(command, 256, "%s/bin/gmt pscoast -R%d/%d/%d/%d -M -Jx1d %s",
gmt_path, xMin, xMax, yMin, yMax, scale_str);
snprintf(buf, 1024, "(%s -Na; %s -W;", command, command);
if (strlen(river_str) > 0)
{
strcat(buf, command);
strcat(buf, river_str);
}
strcat(buf, ")");
} /* END CREATECOASTCOMMAND */
/* -------------------------------------------------------------------- */
static void setColor(PLOT_INFO * plot, char str[], FILE *fp)
{
if (buffer[0] == '>')
{
int color_idx = 0; // Default black.
if (strstr(str, "Border"))
color_idx = 13; // grey
if (strstr(str, "River"))
color_idx = 9; //light blue
if (strstr(str, "Shore"))
color_idx = 2; // blue
if (fp)
{
float *rgb = GetColorRGB_PS(color_idx);
fprintf(fp, "stroke %f %f %f setrgbcolor\n", rgb[0],rgb[1],rgb[2]);
}
else
XSetForeground(plot->dpy, plot->gc, GetColor(color_idx));
}
} /* END SETCOLOR */
/* END GEOPOLMAP.C */