-
Notifications
You must be signed in to change notification settings - Fork 375
/
Copy pathgrdview.c
2169 lines (1929 loc) · 99.2 KB
/
grdview.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*--------------------------------------------------------------------
*
* Copyright (c) 1991-2025 by the GMT Team (https://www.generic-mapping-tools.org/team.html)
* See LICENSE.TXT 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 Lesser General Public License as published by
* the Free Software Foundation; version 3 or any later version.
*
* 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 Lesser General Public License for more details.
*
* Contact info: www.generic-mapping-tools.org
*--------------------------------------------------------------------*/
/*
* Brief synopsis: grdview will read a topofile and produce a 3-D perspective plot
* of the surface z = f(x,y) using PostScript. The surface can
* be represented as:
* 1) A Mesh plot
* 2) A shaded (or colored) surface w/wo contourlines and w/wo
* illumination by artificial sun(s).
*
* grdview calls contours to find the line segments that make up the
* contour lines. This allows the user to specify that the contours
* should be smoothed before plotting. This will make the resulting
* image smoother, especially if n_columns and n_rows are relatively small.
* As an option, a drape grid file can be specified. Then, the colors
* are calculated from that file while the topo file is used for shape.
* Alternatively, give three drape files (red, green, blue components)
* to bypass the z -> rgb via the CPT.
*
* Author: Paul Wessel
* Date: 1-JAN-2010
* Version: 6 API
*/
#include "gmt_dev.h"
#include "longopt/grdview_inc.h"
#define THIS_MODULE_CLASSIC_NAME "grdview"
#define THIS_MODULE_MODERN_NAME "grdview"
#define THIS_MODULE_LIB "core"
#define THIS_MODULE_PURPOSE "Create 3-D perspective image or surface mesh from a grid"
#define THIS_MODULE_KEYS "<G{,CC(,GG(,GI(,IG(,>X}"
#define THIS_MODULE_NEEDS "Jg"
#define THIS_MODULE_OPTIONS "->BJKOPRUVXYfnptxy" GMT_OPT("Ec")
/* grdview needs to work on "tiles" which is defined as the "square" regions made
* up by 4 nodes. Since painting takes place within the tiles, any pixel-registered
* grids will loose 1/2 a grid-spacing in extend in all directions. We simplify this
* difference by converting the headers of all pixel-registered grids to become grid-
* line-registered grids thus: (1) shrink w/e/s/n by 1/2 grid spacing, and (2) set
* the registration parameter to 0.
* We can then proceed with algorithms that require gridline registration. */
/* Declarations needed for binning of smooth contours */
#define GRDVIEW_MESH 0 /* Default */
#define GRDVIEW_SURF 1
#define GRDVIEW_IMAGE 2
#define GRDVIEW_WATERFALL_X 3
#define GRDVIEW_WATERFALL_Y 4
struct GRDVIEW_CTRL {
struct GRDVIEW_In {
bool active;
char *file;
} In;
struct GRDVIEW_C { /* -C<cpt> or -C<color1>,<color2>[,<color3>,...][+i<dz>] */
bool active;
double dz;
char *file;
char *savecpt; /* For when we want to save the automatically generated CPT */
} C;
struct GRDVIEW_G { /* -G<drapefile>|<drapeimage> */
/* Also handle deprecated triples -Gred.grd -Ggreen.grd -Gblue.grd or -Gred.grd,green.grd,blue.grd */
bool active;
bool image;
unsigned int n;
char *file[3];
} G;
struct GRDVIEW_I { /* -I<intensfile>|<value>|<modifiers> */
bool active;
bool constant;
bool derive;
double value;
char *azimuth; /* Default azimuth(s) for shading */
char *file;
char *method; /* Default scaling method */
char *ambient; /* Default ambient offset */
} I;
struct GRDVIEW_N { /* -N<level>[+g<fill>] */
bool active;
bool facade;
bool implicit;
struct GMT_FILL fill;
double level;
} N;
struct GRDVIEW_Q { /* -Q<type>[g] */
bool active, special;
bool mask;
bool monochrome;
bool cpt;
int outline;
unsigned int mode; /* GRDVIEW_MESH, GRDVIEW_SURF, GRDVIEW_IMAGE */
double dpi;
struct GMT_FILL fill;
} Q;
struct GRDVIEW_S { /* -S<smooth> */
bool active;
unsigned int value;
} S;
struct GRDVIEW_T { /* -T[s][o[<pen>]] */
bool active;
bool skip;
bool outline;
struct GMT_PEN pen;
} T;
struct GRDVIEW_W { /* -W[+]<type><pen> */
bool active;
bool contour;
struct GMT_PEN pen[3];
} W;
};
struct GRDVIEW_BIN {
struct GRDVIEW_CONT *first_cont;
};
struct GRDVIEW_CONT {
struct GRDVIEW_POINT *first_point;
struct GRDVIEW_CONT *next_cont;
double value;
};
struct GRDVIEW_POINT {
double x, y;
struct GRDVIEW_POINT *next_point;
};
GMT_LOCAL struct GRDVIEW_CONT *grdview_get_cont_struct(struct GMT_CTRL *GMT, uint64_t bin, struct GRDVIEW_BIN *binij, double value) {
struct GRDVIEW_CONT *cont, *new_cont;
if (!binij[bin].first_cont) binij[bin].first_cont = gmt_M_memory (GMT, NULL, 1, struct GRDVIEW_CONT);
for (cont = binij[bin].first_cont; cont->next_cont && cont->next_cont->value <= value; cont = cont->next_cont);
new_cont = gmt_M_memory (GMT, NULL, 1, struct GRDVIEW_CONT);
if (cont->next_cont) { /* Put it in the link */
new_cont->next_cont = cont->next_cont;
cont->next_cont = new_cont;
}
else /* End of list */
cont->next_cont = new_cont;
return (new_cont);
}
GMT_LOCAL struct GRDVIEW_POINT *grdview_get_point(struct GMT_CTRL *GMT, double x, double y) {
struct GRDVIEW_POINT *point = gmt_M_memory (GMT, NULL, 1, struct GRDVIEW_POINT);
point->x = x;
point->y = y;
return (point);
}
#if 0
/* RS: Removed this because it yields unpredictable results, making it impossible to line up different 3D plots */
GMT_LOCAL void grdview_init_setup (struct GMT_CTRL *GMT, struct GMT_GRID *Topo, int draw_plane, double plane_level) {
int row, col, ij;
double xtmp, ytmp, tmp;
/* We must find the projected min/max in y-direction */
/* Reset from whatever they were */
GMT->current.proj.z_project.ymin = +DBL_MAX;
GMT->current.proj.z_project.ymax = -DBL_MAX;
gmt_M_grd_loop (Topo, row, col, ij) { /* First loop over all the grid-nodes */
if (gmt_M_is_fnan (Topo->data[ij])) continue;
gmt_geoz_to_xy (GMT, gmt_M_grd_col_to_x (col, Topo->header), gmt_M_grd_row_to_y (row, Topo->header), (double)Topo->data[ij], &xtmp, &ytmp);
GMT->current.proj.z_project.ymin = MIN (GMT->current.proj.z_project.ymin, ytmp);
GMT->current.proj.z_project.ymax = MAX (GMT->current.proj.z_project.ymax, ytmp);
}
if (draw_plane) { /* The plane or facade may exceed the current min/max so loop over these as well */
for (col = 0; col < Topo->header->n_columns; col++) {
tmp = gmt_M_grd_col_to_x (col, Topo->header);
gmt_geoz_to_xy (GMT, tmp, Topo->header->wesn[YLO], plane_level, &xtmp, &ytmp);
GMT->current.proj.z_project.ymin = MIN (GMT->current.proj.z_project.ymin, ytmp);
GMT->current.proj.z_project.ymax = MAX (GMT->current.proj.z_project.ymax, ytmp);
gmt_geoz_to_xy (GMT, tmp, Topo->header->wesn[YHI], plane_level, &xtmp, &ytmp);
GMT->current.proj.z_project.ymin = MIN (GMT->current.proj.z_project.ymin, ytmp);
GMT->current.proj.z_project.ymax = MAX (GMT->current.proj.z_project.ymax, ytmp);
}
for (row = 0; row < Topo->header->n_rows; row++) {
tmp = gmt_M_grd_row_to_y (row, Topo->header);
gmt_geoz_to_xy (GMT, Topo->header->wesn[XLO], tmp, plane_level, &xtmp, &ytmp);
GMT->current.proj.z_project.ymin = MIN (GMT->current.proj.z_project.ymin, ytmp);
GMT->current.proj.z_project.ymax = MAX (GMT->current.proj.z_project.ymax, ytmp);
gmt_geoz_to_xy (GMT, Topo->header->wesn[XHI], tmp, plane_level, &xtmp, &ytmp);
GMT->current.proj.z_project.ymin = MIN (GMT->current.proj.z_project.ymin, ytmp);
GMT->current.proj.z_project.ymax = MAX (GMT->current.proj.z_project.ymax, ytmp);
}
}
}
#endif
GMT_LOCAL double grdview_get_intensity (struct GMT_GRID *I, uint64_t k) {
/* Returns the average intensity for this tile */
return (0.25 * (I->data[k] + I->data[k+1] + I->data[k-I->header->mx] + I->data[k-I->header->mx+1]));
}
GMT_LOCAL unsigned int grdview_pixel_inside (struct GMT_CTRL *GMT, int ip, int jp, int *ix, int *iy, int64_t bin, int bin_inc[]) {
/* Returns true of the ip,jp point is inside the polygon defined by the tile */
unsigned int i, what;
double x[6], y[6];
for (i = 0; i < 4; i++) {
x[i] = (double)ix[bin+bin_inc[i]];
y[i] = (double)iy[bin+bin_inc[i]];
}
x[4] = x[0]; y[4] = y[0];
what = gmt_non_zero_winding (GMT, (double)ip, (double)jp, x, y, 5);
return (what);
}
GMT_LOCAL int grdview_quick_idist (int x1, int y1, int x2, int y2) {
/* Fast integer distance calculation */
if ((x2 -= x1) < 0) x2 = -x2;
if ((y2 -= y1) < 0) y2 = -y2;
return (x2 + y2 - (((x2 > y2) ? y2 : x2) >> 1));
}
GMT_LOCAL unsigned int grdview_get_side (double x, double y, double x_left, double y_bottom, double inc[], double inc2[]) {
/* Figure out on what side this point sits on */
double del_x, del_y;
unsigned int side;
del_x = x - x_left;
if (del_x > inc2[GMT_X]) del_x = inc[GMT_X] - del_x;
del_y = y - y_bottom;
if (del_y > inc2[GMT_Y]) del_y = inc[GMT_Y] - del_y;
if (del_x < del_y) /* Cutting N-S gridlines */
side = ((x-x_left) > inc2[GMT_X]) ? 1 : 3;
else /* Cutting E-W gridlines */
side = ((y-y_bottom) > inc2[GMT_Y]) ? 2 : 0;
return (side);
}
GMT_LOCAL void grdview_copy_points_fw (double x[], double y[], double z[], double v[], double xcont[], double ycont[], double zcont[], double vcont[], unsigned int ncont, uint64_t *n) {
unsigned int k;
for (k = 0; k < ncont; k++, (*n)++) {
x[*n] = xcont[k];
y[*n] = ycont[k];
z[*n] = zcont[k];
v[*n] = vcont[k];
}
}
GMT_LOCAL void grdview_copy_points_bw (double x[], double y[], double z[], double v[], double xcont[], double ycont[], double zcont[], double vcont[], unsigned int ncont, uint64_t *n) {
unsigned int k, k2;
for (k2 = 0, k = ncont - 1; k2 < ncont; k2++, k--, (*n)++) {
x[*n] = xcont[k];
y[*n] = ycont[k];
z[*n] = zcont[k];
v[*n] = vcont[k];
}
}
GMT_LOCAL double grdview_get_z_ave (double v[], double next_up, uint64_t n) {
uint64_t k;
double z_ave;
for (k = 0, z_ave = 0.0; k < n; k++) z_ave += MIN (v[k], next_up);
return (z_ave / n);
}
GMT_LOCAL void grdview_add_node (bool used[], double x[], double y[], double z[], double v[], uint64_t *k, unsigned int node, double X_vert[], double Y_vert[], gmt_grdfloat topo[], gmt_grdfloat zgrd[], uint64_t ij) {
/* Adds a corner node to list of points and increments *k */
if (used) { /* If we keep track of which nodes we have used then we do not want to use one twice */
if (used[node]) return;
used[node] = true;
}
x[*k] = X_vert[node];
y[*k] = Y_vert[node];
z[*k] = topo[ij];
v[*k] = zgrd[node];
(*k)++;
}
GMT_LOCAL void grdview_paint_it (struct GMT_CTRL *GMT, struct PSL_CTRL *PSL, struct GMT_PALETTE *P, double *x, double *y, int n, double z, bool intens, bool monochrome, double intensity, int outline) {
int index;
double rgb[4];
struct GMT_FILL *f = NULL;
struct GMT_PALETTE_HIDDEN *PH = gmt_get_C_hidden (P);
if (n < 3) return; /* Need at least 3 points to make a polygon */
index = gmt_get_rgb_from_z (GMT, P, z, rgb); /* This sets P->skip as well */
if (PH->skip) return; /* Skip this z-slice */
PSL_comment (PSL, "Paint polygon z = %g\n", z);
/* Now we must paint, with colors or patterns */
if ((index >= 0 && (f = P->data[index].fill) != NULL) || (index < 0 && (f = P->bfn[index+3].fill) != NULL)) /* Pattern */
gmt_setfill (GMT, f, outline);
else { /* Solid color/gray */
if (intens) gmt_illuminate (GMT, intensity, rgb);
if (monochrome) rgb[0] = rgb[1] = rgb[2] = gmt_M_yiq (rgb); /* gmt_M_yiq transformation */
PSL_setfill (PSL, rgb, outline);
}
PSL_plotpolygon (PSL, x, y, n);
}
GMT_LOCAL void grdview_set_loop_order (struct GMT_CTRL *GMT, struct GMT_GRID *Z, int start[], int stop[], int inc[], unsigned int id[]) {
/* We want to loop over the grid from "back" to "front". What is back and what is front depends on the
* projection (-J) and view angles (-p). We pick a point in the middle of the grid and the point above it
* (i.e., north of it) and compute their projected coordinates, then calculate the angle from center to north.
* We use this orientation of the grid to determine which way we loop (rows then columns or columns then rows)
* and which direction.
* The start and stop items work like this: start is the lower-left row,col values of the first tile,
* while stop is 1 beyond the last valid index - we loop as long as current index is NOT equal to stop.
*/
unsigned int col, row, oct, one = 1;
double az, x0, x1, y0, y1;
char *kind = "xy";
//if (gmt_M_is_azimuthal (GMT) && gmt_M_360_range (Z->header->wesn[XLO], Z->header->wesn[XHI])) one = 0; /* Need to connect across the gap */
col = Z->header->n_columns / 2;
row = Z->header->n_rows / 2;
gmt_geoz_to_xy (GMT, Z->x[col], Z->y[row], GMT->current.proj.z_project.level, &x0, &y0);
gmt_geoz_to_xy (GMT, Z->x[col], Z->y[row-1], GMT->current.proj.z_project.level, &x1, &y1);
if (doubleAlmostEqualZero (x0, x1)) /* Vertical orientation */
az = (y1 > y0) ? 90.0 : -90.0;
else
az = d_atan2d (y1 - y0, x1 - x0);
if (az < 0.0) az += 360.0;
else if (az >= 360.0) az -= 360.0;
oct = 1 + urint (floor (az / 45.0)); /* Gives octants 1 - 8 */
id[0] = id[1] = start[0] = start[1] = stop[0] = stop[1] = inc[0] = inc[1] = 0; /* Init arrays */
switch (oct) {
case 1: /* 0-45: Outer loop over columns */
id[0] = GMT_X; start[0] = 0; stop[0] = Z->header->n_columns - one; inc[0] = +1;
id[1] = GMT_Y; start[1] = 1; stop[1] = Z->header->n_rows; inc[1] = +1;
break;
case 2: /* 45-90: Outer loop over rows */
id[0] = GMT_Y; start[0] = 1; stop[0] = Z->header->n_rows; inc[0] = +1;
id[1] = GMT_X; start[1] = 0; stop[1] = Z->header->n_columns - one; inc[1] = +1;
break;
case 3: /* 90-135: Outer loop over rows */
id[0] = GMT_Y; start[0] = 1; stop[0] = Z->header->n_rows; inc[0] = +1;
id[1] = GMT_X; start[1] = Z->header->n_columns - 1 - one; stop[1] = -1; inc[1] = -1;
break;
case 4: /* 135-180: Outer loop over columns */
id[0] = GMT_X; start[0] = Z->header->n_columns - 1 - one; stop[0] = -1; inc[0] = -1;
id[1] = GMT_Y; start[1] = 1; stop[1] = Z->header->n_rows; inc[1] = +1;
break;
case 5: /* 180-225: Outer loop over columns */
id[0] = GMT_X; start[0] = Z->header->n_columns - 1 - one; stop[0] = -1; inc[0] = -1;
id[1] = GMT_Y; start[1] = Z->header->n_rows - 1; stop[1] = 0; inc[1] = -1;
break;
case 6: /* 225-270: Outer loop over rows */
id[0] = GMT_Y; start[0] = Z->header->n_rows - 1; stop[0] = 0; inc[0] = -1;
id[1] = GMT_X; start[1] = Z->header->n_columns - 1 - one; stop[1] = -1; inc[1] = -1;
break;
case 7: /* 270-315: Outer loop over rows */
id[0] = GMT_Y; start[0] = Z->header->n_rows - 1; stop[0] = 0; inc[0] = -1;
id[1] = GMT_X; start[1] = 0; stop[1] = Z->header->n_columns - one; inc[1] = +1;
break;
case 8: /* 315-360: Outer loop over columns */
id[0] = GMT_X; start[0] = 0; stop[0] = Z->header->n_columns - one; inc[0] = +1;
id[1] = GMT_Y; start[1] = Z->header->n_rows - 1; stop[1] = 0; inc[1] = -1;
break;
default: /* For Coverity */
break;
}
if (GMT->current.proj.projection == GMT_POLAR) { /* Polar cylindrical has 0 at center, not pole */
unsigned int k = (id[0] == GMT_Y) ? 0 : 1, off = 0;
GMT_Report (GMT->parent, GMT_MSG_DEBUG, "Loop over y flipped for cylindrical/polar projection\n");
off = (start[k] == 1) ? -1 : +1;
gmt_M_int_swap (start[k], stop[k]);
start[k] += off; stop[k] += off;
inc[k] = -inc[k];
}
GMT_Report (GMT->parent, GMT_MSG_DEBUG, "Octant %d (az = %g) one = %d\n", oct, az, one);
GMT_Report (GMT->parent, GMT_MSG_DEBUG, "Outer loop over %c doing %d:%d:%d\n", kind[id[0]], start[0], inc[0], stop[0]);
GMT_Report (GMT->parent, GMT_MSG_DEBUG, "Inner loop over %c doing %d:%d:%d\n", kind[id[1]], start[1], inc[1], stop[1]);
}
static void *New_Ctrl (struct GMT_CTRL *GMT) { /* Allocate and initialize a new control structure */
struct GRDVIEW_CTRL *C = gmt_M_memory (GMT, NULL, 1, struct GRDVIEW_CTRL);
/* Initialize values whose defaults are not 0/false/NULL */
gmt_init_fill (GMT, &C->N.fill, -1.0, -1.0, -1.0); /* Default is no fill of facade */
C->I.azimuth = strdup ("-45.0"); /* Default azimuth for shading when -I is used */
C->I.method = strdup ("t1"); /* Default normalization for shading when -I is used */
C->T.pen = C->W.pen[0] = C->W.pen[1] = C->W.pen[2] = GMT->current.setting.map_default_pen; /* Tile and mesh pens */
C->W.pen[0].width *= 3.0; /* Contour pen */
C->W.pen[2].width *= 3.0; /* Facade pen */
C->Q.dpi = GMT->current.setting.graphics_dpu; /* Set the default dpu for -Qi */
if (GMT->current.setting.graphics_dpu_unit == 'c') C->Q.dpi *= 2.54; /* Convert dpc to dpi */
gmt_init_fill (GMT, &C->Q.fill, GMT->current.setting.ps_page_rgb[0], GMT->current.setting.ps_page_rgb[1], GMT->current.setting.ps_page_rgb[2]);
return (C);
}
static void Free_Ctrl (struct GMT_CTRL *GMT, struct GRDVIEW_CTRL *C) { /* Deallocate control structure */
unsigned int i;
if (!C) return;
gmt_M_str_free (C->In.file);
gmt_M_str_free (C->C.file);
gmt_M_str_free (C->C.savecpt);
for (i = 0; i < C->G.n; i++) gmt_M_str_free (C->G.file[i]);
gmt_M_str_free (C->I.file);
gmt_M_str_free (C->I.azimuth);
gmt_M_str_free (C->I.method);
gmt_M_free (GMT, C);
}
static int usage (struct GMTAPI_CTRL *API, int level) {
struct GMT_PEN P;
const char *name = gmt_show_name_and_purpose (API, THIS_MODULE_LIB, THIS_MODULE_CLASSIC_NAME, THIS_MODULE_PURPOSE);
if (level == GMT_MODULE_PURPOSE) return (GMT_NOERROR);
GMT_Usage (API, 0, "usage: %s <topogrid> %s [%s] [-C%s] [-G<drapegrid>|<drapeimage>] "
"[-I[<intensgrid>|<value>|<modifiers>]] [%s] %s[-N[<level>][+g<fill>]] %s%s[-Qc|i|m[x|y]|s[<color>][+m]] [%s] [-S<smooth>] "
"[-T[+o[<pen>]][+s]] [%s] [%s] [-W<type><pen>] [%s] [%s] %s[%s] [%s] [%s] [%s] [%s]\n",
name, GMT_J_OPT, GMT_B_OPT, CPT_OPT_ARGS, GMT_Jz_OPT, API->K_OPT, API->O_OPT, API->P_OPT, GMT_Rgeoz_OPT, GMT_U_OPT, GMT_V_OPT,
GMT_X_OPT, GMT_Y_OPT, API->c_OPT, GMT_f_OPT, GMT_n_OPT, GMT_p_OPT, GMT_t_OPT, GMT_PAR_OPT);
if (level == GMT_SYNOPSIS) return (GMT_MODULE_SYNOPSIS);
GMT_Message (API, GMT_TIME_NONE, " REQUIRED ARGUMENTS:\n");
GMT_Usage (API, 1, "\n<topogrid> is the grid surface shape to be plotted.");
GMT_Option (API, "J-Z");
GMT_Message (API, GMT_TIME_NONE, "\n OPTIONAL ARGUMENTS:\n");
GMT_Option (API, "B-");
gmt_explain_cpt_input (API, 'C');
GMT_Usage (API, 1, "\n-G<drapegrid>|<drapeimage>");
GMT_Usage (API, -2, "Specify how to color the 3-D surface geometry defined by <topogrid>:");
GMT_Usage (API, 3, "%s Provide a grid (<drapegrid>) and colors will be determined from it and the cpt.", GMT_LINE_BULLET);
GMT_Usage (API, 3, "%s Provide an image (<drapeimage>) and it will be draped over the surface.", GMT_LINE_BULLET);
GMT_Usage (API, -2, "Notes: 1) -JZ|z and -N always refer to the data in the <topogrid>. "
"2) The -G option requires the -Qc|i[<dpu>] option.");
GMT_Usage (API, 1, "\n-I[<intensgrid>|<value>|<modifiers>]");
GMT_Usage (API, -2, "Apply directional illumination. Append name of an intensity grid, or "
"for a constant intensity (i.e., change the ambient light), just give a scalar. "
"To derive intensities from <topogrid> instead, append desired modifiers:");
GMT_Usage (API, 3, "+a Specify <azim> of illumination [-45].");
GMT_Usage (API, 3, "+n Set the <method> and <scale> to use [t1].");
GMT_Usage (API, 3, "+m Set <ambient> light to add [0].");
GMT_Usage (API, -2, "Alternatively, use -I+d to accept the default values (see grdgradient for more details). "
"To derive intensities from another grid than <topogrid>, give the alternative data grid with suitable modifiers.");
GMT_Option (API, "K");
GMT_Usage (API, 1, "\n-N[<level>][+g<fill>]");
GMT_Usage (API, -2, "Draw a horizontal plane at z = <level> [minimum grid (or -R) value]. For rectangular projections, append +g<fill> "
"to paint the facade between the plane and the data perimeter.");
GMT_Option (API, "O,P");
GMT_Usage (API, 1, "\n-Qc|i|m[x|y]|s[<color>][+m]");
GMT_Usage (API, -2, "Set plot type request via on of the following directives:");
GMT_Usage (API, 3, "c: Transform polygons to raster-image via scanline conversion. Append effective <dpu> [%lg%c]. "
"Set PS Level 3 color-masking for nodes with z = NaN.",
API->GMT->current.setting.graphics_dpu, API->GMT->current.setting.graphics_dpu_unit);
GMT_Usage (API, 3, "i: As c but no color-masking. Append effective <dpu> [%lg%c].",
API->GMT->current.setting.graphics_dpu, API->GMT->current.setting.graphics_dpu_unit);
GMT_Usage (API, 3, "m: Mesh plot [Default]. Append <color> for mesh paint [%s]. "
"Alternatively, append x or y for row or column \"waterfall\" profiles.",
gmt_putcolor (API->GMT, API->GMT->PSL->init.page_rgb));
GMT_Usage (API, 3, "s: Colored or shaded surface. Optionally, append m to draw mesh-lines on the surface.");
GMT_Usage (API, -2, "Color may be further adjusted by a modifier:");
GMT_Usage (API, 3, "+m Force a monochrome image using the YIQ transformation.");
GMT_Option (API, "R");
GMT_Usage (API, 1, "\n-S<smooth>");
GMT_Usage (API, -2, "Smooth contours first (see grdcontour for <smooth> value info) [no smoothing].");
GMT_Usage (API, 1, "\n-T[+o[<pen>]][+s]");
GMT_Usage (API, -2, "Image the data without interpolation by painting polygonal tiles.");
GMT_Usage (API, 3, "+s Skip tiles for nodes with z = NaN [Default paints all tiles].");
GMT_Usage (API, 3, "+o to draw tile outline; optionally append <pen> [Default uses no outline].");
GMT_Usage (API, -2, "Note: Cannot be used with -Jz|Z as it produces a flat image.");
GMT_Option (API, "U,V");
gmt_pen_syntax (API->GMT, 'W', NULL, "Set pen attributes for various features in form <type><pen>.", NULL, 0);
GMT_Usage (API, -2, "Note: <type> can be c for contours, m for mesh, and f for facade:");
P = API->GMT->current.setting.map_default_pen;
GMT_Usage (API, 3, "m: Sets attributes for mesh lines [%s]. "
"Requires -Qm or -Qsm to take effect.", gmt_putpen (API->GMT, &P));
P.width *= 3.0;
GMT_Usage (API, 3, "c: Draw contours on top of surface or mesh [Default is no contours]. "
"Optionally append pen attributes [%s].", gmt_putpen (API->GMT, &P));
GMT_Usage (API, 3, "f: Set attributes for facade outline [%s]. "
"Requires -N to take effect.", gmt_putpen (API->GMT, &P));
GMT_Option (API, "X,c,f,n,p,t,.");
return (GMT_MODULE_USAGE);
}
GMT_LOCAL double grdview_get_dpi (char *text) {
double dpi = atoi (text);
if (text[strlen(text)-1] == 'c') dpi *= 2.54; /* Got dpc, convert to dpi */
return (dpi);
}
static int parse (struct GMT_CTRL *GMT, struct GRDVIEW_CTRL *Ctrl, struct GMT_OPTION *options) {
/* This parses the options provided to grdview and sets parameters in Ctrl.
* Note Ctrl has already been initialized and non-zero default values set.
* Any GMT common options will override values set previously by other commands.
* It also replaces any file names specified as input or output with the data ID
* returned when registering these sources/destinations with the API.
*/
unsigned int n_errors = 0, q_set = 0, n_commas, j, k, n, id;
int sval;
bool no_cpt = false;
char *c = NULL, *f = NULL;
struct GMT_OPTION *opt = NULL;
struct GMTAPI_CTRL *API = GMT->parent;
for (opt = options; opt; opt = opt->next) { /* Process all the options given */
switch (opt->option) {
/* Common parameters */
case '<': /* Input file (only one is accepted) */
n_errors += gmt_M_repeated_module_option (API, Ctrl->In.active);
n_errors += gmt_get_required_file (GMT, opt->arg, opt->option, 0, GMT_IS_GRID, GMT_IN, GMT_FILE_REMOTE, &(Ctrl->In.file));
break;
/* Processes program-specific parameters */
case 'C': /* Cpt file */
n_errors += gmt_M_repeated_module_option (API, Ctrl->C.active);
gmt_M_str_free (Ctrl->C.file);
if (opt->arg[0]) Ctrl->C.file = strdup (opt->arg);
if (opt->arg[0] && (f = gmt_strrstr (Ctrl->C.file, "+s")) != NULL) { /* Filename has a +s<outname>, extract that part */
Ctrl->C.savecpt = &f[2];
f[0] = '\0'; /* Remove the +s<outname> from Ctrl->C.file */
}
gmt_cpt_interval_modifier (GMT, &(Ctrl->C.file), &(Ctrl->C.dz));
break;
case 'G': /* One grid or image or three separate r,g,b grids */
Ctrl->G.active = true;
for (k = 0, n_commas = 0; opt->arg[k]; k++) if (opt->arg[k] == ',') n_commas++;
if (n_commas == 2) { /* Old-style -Ga,b,c option */
/* Three r,g,b grids for draping */
char A[GMT_LEN256] = {""}, B[GMT_LEN256] = {""}, C[GMT_LEN256] = {""};
sscanf (opt->arg, "%[^,],%[^,],%s", A, B, C);
n_errors += gmt_get_required_file (GMT, A, opt->option, 0, GMT_IS_GRID, GMT_IN, GMT_FILE_REMOTE, &(Ctrl->G.file[0]));
n_errors += gmt_get_required_file (GMT, B, opt->option, 0, GMT_IS_GRID, GMT_IN, GMT_FILE_REMOTE, &(Ctrl->G.file[1]));
n_errors += gmt_get_required_file (GMT, C, opt->option, 0, GMT_IS_GRID, GMT_IN, GMT_FILE_REMOTE, &(Ctrl->G.file[2]));
Ctrl->G.n = 3;
}
else if (n_commas == 0 && Ctrl->G.n < 3) { /* Just got another drape grid or image */
Ctrl->G.file[Ctrl->G.n] = strdup (opt->arg);
if (GMT_Get_FilePath (API, GMT_IS_GRID, GMT_IN, GMT_FILE_REMOTE, &(Ctrl->G.file[Ctrl->G.n]))) n_errors++;
Ctrl->G.n++;
}
else {
GMT_Report (API, GMT_MSG_ERROR, "Option -G: Usage is -G<drapegrid|drapeimage>\n");
n_errors++;
}
break;
case 'I': /* Use intensity from grid or constant or auto-compute it */
n_errors += gmt_M_repeated_module_option (API, Ctrl->I.active);
if ((c = strstr (opt->arg, "+d"))) { /* Gave +d, so derive intensities from the input grid using default settings */
Ctrl->I.derive = true;
c[0] = '\0'; /* Chop off modifier */
}
else if ((c = gmt_first_modifier (GMT, opt->arg, "amn"))) { /* Want to control how grdgradient is run */
unsigned int pos = 0;
char p[GMT_BUFSIZ] = {""};
Ctrl->I.derive = true;
while (gmt_getmodopt (GMT, 'I', c, "amn", &pos, p, &n_errors) && n_errors == 0) {
switch (p[0]) {
case 'a': gmt_M_str_free (Ctrl->I.azimuth); Ctrl->I.azimuth = strdup (&p[1]); break;
case 'm': gmt_M_str_free (Ctrl->I.ambient); Ctrl->I.ambient = strdup (&p[1]); break;
case 'n': gmt_M_str_free (Ctrl->I.method); Ctrl->I.method = strdup (&p[1]); break;
default: break; /* These are caught in gmt_getmodopt so break is just for Coverity */
}
}
c[0] = '\0'; /* Chop off all modifiers so range can be determined */
}
else if (!opt->arg[0] || !strcmp (opt->arg, "+")) { /* Gave deprecated option to derive intensities from the input grid using default settings */
Ctrl->I.derive = true;
if (opt->arg[0]) opt->arg[0] = '\0'; /* Remove the single + */
}
if (opt->arg[0]) { /* Gave an argument in addition to or instead of a modifier */
if (!gmt_access (GMT, opt->arg, R_OK)) /* Got a file */
Ctrl->I.file = strdup (opt->arg);
else if (gmt_M_file_is_remote (opt->arg)) /* Got a remote file */
Ctrl->I.file = strdup (opt->arg);
else if (opt->arg[0] && gmt_is_float (GMT, opt->arg)) { /* Looks like a constant value */
Ctrl->I.value = atof (opt->arg);
Ctrl->I.constant = true;
}
}
else if (!Ctrl->I.active) {
GMT_Report (API, GMT_MSG_ERROR, "Option -I: Requires a valid grid file or a constant\n");
n_errors++;
}
if (c) c[0] = '+'; /* Restore the plus */
break;
case 'L': /* GMT4 BCs */
if (gmt_M_compat_check (GMT, 4)) {
GMT_Report (API, GMT_MSG_COMPAT,
"Option -L is deprecated; -n+b%s was set instead, use this in the future.\n", opt->arg);
gmt_strncpy (GMT->common.n.BC, opt->arg, 4U);
}
else
n_errors += gmt_default_option_error (GMT, opt);
break;
case 'N': /* Facade */
if (opt->arg[0]) {
char colors[GMT_LEN64] = {""};
n_errors += gmt_M_repeated_module_option (API, Ctrl->N.active);
if ((c = strstr (opt->arg, "+g")) != NULL) { /* Gave modifier +g<fill> */
c[0] = '\0'; /* Truncate string temporarily */
if (opt->arg[0])
Ctrl->N.level = atof (opt->arg);
else
Ctrl->N.implicit = true; /* Set level when we know grids zmin */
c[0] = '+'; /* Restore the + */
n_errors += gmt_M_check_condition (GMT, gmt_getfill (GMT, &c[2], &Ctrl->N.fill),
"Option -N: Usage is -N[<level>][+g<fill>]\n");
Ctrl->N.facade = true;
}
else if (gmt_M_compat_check (GMT, 4) && (c = strchr (opt->arg, '/')) != NULL) { /* Deprecated <level>/<fill> */
GMT_Report (API, GMT_MSG_COMPAT,
"Option -N<level>[/<fill>] is deprecated; use -N[<level>][+g<fill>] in the future.\n");
c[0] = ' '; /* Take out the slash for now */
sscanf (opt->arg, "%lf %s", &Ctrl->N.level, colors);
n_errors += gmt_M_check_condition (GMT, gmt_getfill (GMT, colors, &Ctrl->N.fill),
"Option -N: Usage is -N<level>[+g<fill>]\n");
Ctrl->N.facade = true;
c[0] = '/'; /* Restore the slash */
}
else /* Just got the level */
Ctrl->N.level = atof (opt->arg);
}
else
Ctrl->N.implicit = true; /* Set level when we know grids zmin */
break;
case 'Q': /* Plot mode */
n_errors += gmt_M_repeated_module_option (API, Ctrl->Q.active);
q_set++;
if ((c = strstr (opt->arg, "+m")) != NULL) {
Ctrl->Q.monochrome = true;
c[0] = '\0'; /* Chop off +m */
}
switch (opt->arg[0]) {
case 'c': /* Image with colormask */
if (opt->arg[1] && isdigit ((int)opt->arg[1])) Ctrl->Q.dpi = grdview_get_dpi (&opt->arg[1]);
Ctrl->Q.mode = GRDVIEW_IMAGE;
Ctrl->Q.cpt = true; /* Will need a CPT */
Ctrl->Q.mask = true;
break;
case 't': /* Image without color interpolation */
Ctrl->Q.special = true;
Ctrl->Q.cpt = true; /* Will need a CPT */
/* Intentionally fall through - to 'i' */
case 'i': /* Image with clipmask */
Ctrl->Q.mode = GRDVIEW_IMAGE;
if (opt->arg[1] && isdigit ((int)opt->arg[1])) Ctrl->Q.dpi = grdview_get_dpi (&opt->arg[1]);
Ctrl->Q.cpt = true; /* Will need a CPT */
break;
case 'm': /* Mesh plot */
n = 0;
if (opt->arg[1] && opt->arg[1] == 'x') {
Ctrl->Q.mode = GRDVIEW_WATERFALL_X;
Ctrl->Q.fill.rgb[0] = Ctrl->Q.fill.rgb[1] = Ctrl->Q.fill.rgb[2] = 1; /* Default to white */
n = 1;
}
else if (opt->arg[1] && opt->arg[1] == 'y') {
Ctrl->Q.mode = GRDVIEW_WATERFALL_Y;
Ctrl->Q.fill.rgb[0] = Ctrl->Q.fill.rgb[1] = Ctrl->Q.fill.rgb[2] = 1; /* Default to white */
n = 1;
}
else
Ctrl->Q.mode = GRDVIEW_MESH;
if (opt->arg[n+1]) { /* Appended /<color> or just <color> */
k = ((opt->arg[n+1] == '/') ? 2 : 1) + n;
n_errors += gmt_M_check_condition (GMT, gmt_getfill (GMT, &opt->arg[k], &Ctrl->Q.fill),
"Option -Qm: To give mesh color, use -Qm[x|y]<color>\n");
}
break;
case 's': /* Color without contours */
Ctrl->Q.mode = GRDVIEW_SURF;
if (opt->arg[1] == 'm') Ctrl->Q.outline = 1;
Ctrl->Q.cpt = true; /* Will need a CPT */
break;
default:
GMT_Report (API, GMT_MSG_ERROR, "Option -Q: Unrecognized qualifier (%c)\n", opt->arg[0]);
n_errors++;
break;
}
if (c != NULL && Ctrl->Q.monochrome)
c[0] = '+'; /* Restore the chopped off +m */
else if (gmt_M_compat_check (GMT, 4) && opt->arg[strlen(opt->arg)-1] == 'g') {
GMT_Report (API, GMT_MSG_COMPAT, "Option -Q<args>[g] is deprecated; use -Q<args>[+m] in the future.\n");
Ctrl->Q.monochrome = true;
}
break;
case 'S': /* Smoothing of contours */
n_errors += gmt_M_repeated_module_option (API, Ctrl->S.active);
n_errors += gmt_get_required_int (GMT, opt->arg, opt->option, 0, &sval);
n_errors += gmt_M_check_condition (GMT, sval <= 0, "Option -S: smooth value must be positive\n");
Ctrl->S.value = sval;
break;
case 'T': /* Tile plot -T[+s][+o<pen>] */
n_errors += gmt_M_repeated_module_option (API, Ctrl->T.active);
if (opt->arg[0] && (strchr (opt->arg, '+') || gmt_M_compat_check (GMT, 6))) { /* New syntax */
if (strstr (opt->arg, "+s"))
Ctrl->T.skip = true;
if ((c = strstr (opt->arg, "+o"))) {
if (gmt_getpen (GMT, &c[2], &Ctrl->T.pen)) {
gmt_pen_syntax (GMT, 'T', NULL, " ", NULL, 0);
n_errors++;
}
else
Ctrl->T.outline = true;
}
}
else if (opt->arg[0]) { /* Old-style syntax -T[s][o<pen>] */
k = 0;
if (opt->arg[0] == 's') Ctrl->T.skip = true, k = 1;
if (opt->arg[k] == 'o') { /* Want tile outline also */
Ctrl->T.outline = true;
k++;
if (opt->arg[k] && gmt_getpen (GMT, &opt->arg[k], &Ctrl->T.pen)) {
gmt_pen_syntax (GMT, 'T', NULL, " ", NULL, 0);
n_errors++;
}
}
}
break;
case 'W': /* Contour, mesh, or facade pens */
n_errors += gmt_M_repeated_module_option (API, Ctrl->W.active);
j = (opt->arg[0] == 'm' || opt->arg[0] == 'c' || opt->arg[0] == 'f');
id = 0;
if (j == 1) { /* First check that the c|f|m is not part of a color name instead */
char txt_a[GMT_LEN256] = {""};
n = j+1;
while (opt->arg[n] && opt->arg[n] != ',' && opt->arg[n] != '/') n++; /* Wind until end or , or / */
strncpy (txt_a, opt->arg, n); txt_a[n] = '\0';
if (gmt_colorname2index (GMT, txt_a) >= 0) /* Found a colorname: reset j to 0 and parse normally */
j = id = 0;
else /* Got a type, set the id index accordingly */
id = (opt->arg[0] == 'f') ? 2 : ((opt->arg[0] == 'm') ? 1 : 0);
}
if (gmt_getpen (GMT, &opt->arg[j], &Ctrl->W.pen[id])) {
gmt_pen_syntax (GMT, 'W', NULL, " ", NULL, 0);
n_errors++;
}
if (j == 0) /* Copy pen when using just -W */
Ctrl->W.pen[2] = Ctrl->W.pen[1] = Ctrl->W.pen[0];
if (id == 0) Ctrl->W.contour = true; /* Switch contouring ON for -Wc or -W */
break;
default: /* Report bad options */
n_errors += gmt_default_option_error (GMT, opt);
break;
}
}
gmt_consider_current_cpt (API, &Ctrl->C.active, &(Ctrl->C.file));
if (Ctrl->G.active) {
if (gmt_M_file_is_image (Ctrl->G.file[0])) no_cpt = true;
if (no_cpt) Ctrl->Q.cpt = false;
if (Ctrl->G.n == 3)
Ctrl->G.image = true;
}
if (Ctrl->N.facade && GMT->common.R.oblique) { /* Cannot yet do facade with oblique grid */
GMT_Report (API, GMT_MSG_WARNING, "Cannot paint facade for oblique projection\n");
Ctrl->N.facade = false;
}
n_errors += gmt_M_check_condition(GMT, !Ctrl->In.file, "Must specify input file\n");
n_errors += gmt_M_check_condition(GMT, Ctrl->In.file && !strcmp (Ctrl->In.file, "="),
"Piping of topofile not supported!\n");
n_errors += gmt_M_check_condition(GMT, !GMT->common.J.active,
"Must specify a map projection with the -J option\n");
/* Gave more than one -Q setting */
n_errors += gmt_M_check_condition(GMT, q_set > 1, "Options -Qm, -Qs, -Qc, and -Qi are mutually exclusive.\n");
/* Gave both -Q and -T */
n_errors += gmt_M_check_condition(GMT, Ctrl->T.active && Ctrl->Q.active, "Options -Q and -T are mutually exclusive.\n");
n_errors += gmt_M_check_condition(GMT, Ctrl->G.active && (Ctrl->G.n < 1 || Ctrl->G.n > 3),
"Option -G: Requires either 1 or 3 grids (%d)\n",Ctrl->G.n);
if (Ctrl->G.active) { /* Draping was requested */
for (k = 0; k < Ctrl->G.n; k++)
n_errors += gmt_M_check_condition(GMT, !Ctrl->G.file[k][0], "Option -G: Must specify drape file\n");
n_errors += gmt_M_check_condition(GMT, Ctrl->G.n == 3 && Ctrl->Q.mode != GRDVIEW_IMAGE,
"The draping option requires the -Qc|i option\n");
}
n_errors += gmt_M_check_condition(GMT, Ctrl->I.active && !Ctrl->I.constant && !Ctrl->I.file && !Ctrl->I.derive,
"Option -I: Must specify intensity file, value, or modifiers\n");
n_errors += gmt_M_check_condition(GMT, (Ctrl->Q.mode == GRDVIEW_SURF || Ctrl->Q.mode == GRDVIEW_IMAGE || Ctrl->W.contour) &&
!Ctrl->C.file && Ctrl->G.n != 3 && !no_cpt && GMT->current.setting.run_mode == GMT_CLASSIC, "Must specify color palette table\n");
n_errors += gmt_M_check_condition(GMT, Ctrl->Q.mode == GRDVIEW_IMAGE && Ctrl->Q.dpi <= 0.0,
"Option -Qi: Must specify positive dpi\n");
n_errors += gmt_M_check_condition(GMT, Ctrl->Q.mode == GRDVIEW_SURF && !Ctrl->C.active,
"Option -Qs: Must also specify a cpt via -C\n");
n_errors += gmt_M_check_condition(GMT, Ctrl->T.active && GMT->current.proj.JZ_set,
"Option -T: Cannot specify -JZ|z\n");
return (n_errors ? GMT_PARSE_ERROR : GMT_NOERROR);
}
#define bailout(code) {gmt_M_free_options (mode); return (code);}
#define Return(code) {Free_Ctrl (GMT, Ctrl); gmt_end_module (GMT, GMT_cpy); bailout (code);}
EXTERN_MSC int GMT_grdview(void *V_API, int mode, void *args) {
bool get_contours, bad, pen_set, begin, saddle, drape_resample = false;
bool nothing_inside = false, use_intensity_grid, do_G_reading = true, read_z = false;
openmp_int row, col;
unsigned int c, nk, n4, d_reg[3], i_reg = 0, id[2], good;
unsigned int t_reg, n_out, k, k1, ii, jj, PS_colormask_off = 0, n_max = 0;
int i, j, i_bin, j_bin, i_bin_old, j_bin_old, way, bin_inc[4], ij_inc[4], error = 0;
int start[2], stop[2], inc[2];
uint64_t sw, se, nw, ne, n, pt, ij, bin;
int64_t ns;
size_t max_alloc;
gmt_grdfloat *saved_data_pointer = NULL;
double cval, x_left, x_right, y_top, y_bottom, small = FLT_EPSILON, z_ave;
double inc2[2], wesn[4] = {0.0, 0.0, 0.0, 0.0}, z_val, x_pixel_size, y_pixel_size;
double this_intensity = 0.0, next_up = 0.0, xmesh[4], ymesh[4], rgb[4];
double *x_imask = NULL, *y_imask = NULL, x_inc[4], y_inc[4], *x = NULL, *y = NULL;
double *z = NULL, *v = NULL, *xx = NULL, *yy = NULL, *xval = NULL, *yval = NULL;
struct GRDVIEW_CONT *start_cont = NULL, *this_cont = NULL, *last_cont = NULL;
struct GRDVIEW_POINT *this_point = NULL, *last_point = NULL;
struct GMT_GRID *Drape[3] = {NULL, NULL, NULL}, *Topo = NULL, *Intens = NULL, *Z = NULL;
struct GRDVIEW_BIN *binij = NULL;
struct GMT_PALETTE *P = NULL;
struct GRDVIEW_CTRL *Ctrl = NULL;
struct GMT_CTRL *GMT = NULL, *GMT_cpy = NULL; /* General GMT internal parameters */
struct GMT_OPTION *options = NULL;
struct PSL_CTRL *PSL = NULL; /* General PSL internal parameters */
struct GMTAPI_CTRL *API = gmt_get_api_ptr (V_API); /* Cast from void to GMTAPI_CTRL pointer */
/*----------------------- Standard module initialization and parsing ----------------------*/
if (API == NULL) return (GMT_NOT_A_SESSION);
if (mode == GMT_MODULE_PURPOSE) return (usage (API, GMT_MODULE_PURPOSE)); /* Return the purpose of program */
options = GMT_Create_Options (API, mode, args); if (API->error) return (API->error); /* Set or get option list */
if ((error = gmt_report_usage (API, options, 0, usage)) != GMT_NOERROR) bailout (error); /* Give usage if requested */
/* Parse the command-line arguments */
if ((GMT = gmt_init_module (API, THIS_MODULE_LIB, THIS_MODULE_CLASSIC_NAME, THIS_MODULE_KEYS, THIS_MODULE_NEEDS, module_kw, &options, &GMT_cpy)) == NULL) bailout (API->error); /* Save current state */
if (GMT_Parse_Common (API, THIS_MODULE_OPTIONS, options)) Return (API->error);
Ctrl = New_Ctrl (GMT); /* Allocate and initialize a new control structure */
if ((error = parse (GMT, Ctrl, options)) != 0) Return (error);
/*---------------------------- This is the grdview main code ----------------------------*/
gmt_grd_set_datapadding (GMT, true); /* Turn on gridpadding when reading a subset */
GMT->current.plot.mode_3D = 1; /* Only do background axis first; do foreground at end */
use_intensity_grid = (Ctrl->I.active && !Ctrl->I.constant); /* We want to use the intensity grid */
if ((Topo = GMT_Read_Data (API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, GMT_CONTAINER_ONLY|GMT_GRID_NEEDS_PAD2, NULL, Ctrl->In.file, NULL)) == NULL) { /* Get header only */
Return (API->error);
}
if ((API->error = gmt_img_sanitycheck (GMT, Topo->header))) { /* Used map projection on a Mercator (cartesian) grid */
Return (API->error);
}
if (use_intensity_grid && !Ctrl->I.derive) {
GMT_Report (API, GMT_MSG_INFORMATION, "Read intensity grid header from file %s\n", Ctrl->I.file);
if ((Intens = GMT_Read_Data (API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, GMT_CONTAINER_ONLY|GMT_GRID_NEEDS_PAD2, NULL, Ctrl->I.file, NULL)) == NULL) { /* Get header only */
Return (API->error);
}
}
if (!Ctrl->C.active && (Ctrl->Q.cpt || Ctrl->T.active) && Ctrl->G.n != 3)
Ctrl->C.active = true; /* Use default CPT (GMT->current.setting.cpt) and auto-stretch or under modern reuse current CPT */
/* Determine what wesn to pass to map_setup */
if (!GMT->common.R.active[RSET]) { /* No -R, use grid region */
gmt_set_R_from_grd(GMT, Topo->header);
GMT->common.R.active[RSET] = true; /* Needed to have gmtapi_import_image pick the right branch when draping image is referenced */
}
gmt_M_memcpy (wesn, GMT->common.R.wesn, 4, double);
if (gmt_file_is_tiled_list (API, Ctrl->In.file, NULL, NULL, NULL)) { /* Must read it so we can get zmin/zmax set */
/* PW Note: This step could go away if we pre-saved the z-range of all remote global grids */
if (GMT_Read_Data (API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, GMT_DATA_ONLY|GMT_GRID_NEEDS_PAD2, wesn, Ctrl->In.file, Topo) == NULL) { /* Get topo data */
Return (API->error);
}
read_z = true; /* So we don't do it again later */
}
n_max = MAX (Topo->header->n_columns, Topo->header->n_rows); /* Get max dimension so far */
/* Set default z-range for plot to be that of the grid if not specified via -R */
if (GMT->common.R.wesn[ZLO] == 0.0 && GMT->common.R.wesn[ZHI] == 0.0) {
GMT->common.R.wesn[ZLO] = Topo->header->z_min;
GMT->common.R.wesn[ZHI] = Topo->header->z_max;
if (Ctrl->N.active && Ctrl->N.level < GMT->common.R.wesn[ZLO]) GMT->common.R.wesn[ZLO] = Ctrl->N.level;
if (Ctrl->N.active && Ctrl->N.level > GMT->common.R.wesn[ZHI]) GMT->common.R.wesn[ZHI] = Ctrl->N.level;
}
if (gmt_map_setup (GMT, GMT->common.R.wesn)) Return (GMT_PROJECTION_ERROR);
/* Determine the wesn to be used to read the grid file */
if (!gmt_grd_setregion (GMT, Topo->header, wesn, BCR_BILINEAR))
nothing_inside = true;
else if (use_intensity_grid && !Ctrl->I.derive && !gmt_grd_setregion (GMT, Intens->header, wesn, BCR_BILINEAR))
nothing_inside = true;
if (nothing_inside) {
/* No grid to plot; just do empty map and bail */
if ((PSL = gmt_plotinit (GMT, options)) == NULL) Return (GMT_RUNTIME_ERROR);
gmt_plane_perspective (GMT, GMT->current.proj.z_project.view_plane, GMT->current.proj.z_level);
gmt_set_basemap_orders (GMT, GMT_BASEMAP_FRAME_BEFORE, GMT_BASEMAP_GRID_BEFORE, GMT_BASEMAP_ANNOT_BEFORE);
GMT->current.plot.mode_3D |= 2; /* Ensure that foreground axis is drawn */
gmt_plotcanvas (GMT); /* Fill canvas if requested */
gmt_map_basemap (GMT);
gmt_plane_perspective (GMT, -1, 0.0);
gmt_plotend (GMT);
Return (GMT_NOERROR);
}
if (Ctrl->I.derive) { /* Auto-create intensity grid from data grid */
char int_grd[GMT_VF_LEN] = {""}, data_file[PATH_MAX] = {""}, cmd[GMT_LEN256] = {""};
struct GMT_GRID *I_data = NULL;
GMT_Report (API, GMT_MSG_INFORMATION, "Derive intensity grid from data grid\n");
/* Create a virtual file to hold the intensity grid */
if (GMT_Open_VirtualFile (API, GMT_IS_GRID, GMT_IS_SURFACE, GMT_OUT|GMT_IS_REFERENCE, NULL, int_grd))
Return (API->error);
if (Ctrl->I.file) { /* Gave a file to derive from */
if (gmt_file_is_tiled_list (API, Ctrl->I.file, NULL, NULL, NULL)) { /* Must read and stitch the tiles first */
if ((I_data = GMT_Read_Data (API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, GMT_CONTAINER_AND_DATA|GMT_GRID_NEEDS_PAD2, wesn, Ctrl->I.file, NULL)) == NULL) /* Get srtm grid data */
Return (API->error);
if (GMT_Open_VirtualFile (API, GMT_IS_GRID, GMT_IS_SURFACE, GMT_IN|GMT_IS_REFERENCE, I_data, data_file))
Return (API->error);
}
else
strcpy (data_file, Ctrl->I.file);
}
else if (Topo->data) { /* If not NULL it means we have a tiled and blended grid, use as is */
if (GMT_Open_VirtualFile (API, GMT_IS_GRID, GMT_IS_SURFACE, GMT_IN|GMT_IS_REFERENCE, Topo, data_file))
Return (API->error);
}
else
strcpy (data_file, Ctrl->In.file);
/* Prepare the grdgradient arguments using selected -A -N */
gmt_filename_set (data_file); /* Replace any spaces with ASCII 29 */
sprintf (cmd, "%s -G%s -A%s -N%s+a%s -R%.16g/%.16g/%.16g/%.16g --GMT_HISTORY=readonly",
data_file, int_grd, Ctrl->I.azimuth, Ctrl->I.method, Ctrl->I.ambient, wesn[XLO], wesn[XHI], wesn[YLO], wesn[YHI]);
/* Call the grdgradient module */
GMT_Report (API, GMT_MSG_INFORMATION, "Calling grdgradient with args %s\n", cmd);
if (GMT_Call_Module (API, "grdgradient", GMT_MODULE_CMD, cmd))
Return (API->error);
/* Obtain the data from the virtual file */
if ((Intens = GMT_Read_VirtualFile (API, int_grd)) == NULL)
Return (API->error);
i_reg = gmt_change_grdreg (GMT, Intens->header, GMT_GRID_NODE_REG); /* Ensure gridline registration */
}
/* Read data */
GMT_Report (API, GMT_MSG_INFORMATION, "Processing shape grid\n");
if (!read_z && GMT_Read_Data (API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, GMT_DATA_ONLY|GMT_GRID_NEEDS_PAD2, wesn, Ctrl->In.file, Topo) == NULL) { /* Get topo data */
Return (API->error);
}
t_reg = gmt_change_grdreg (GMT, Topo->header, GMT_GRID_NODE_REG); /* Ensure gridline registration */
if (Ctrl->C.active) {
char *dataset_cpt = (Ctrl->G.active && Ctrl->G.n == 1 && !gmt_M_file_is_image (Ctrl->G.file[0])) ? Ctrl->G.file[0] : Ctrl->In.file;
char *cpt = gmt_cpt_default (API, Ctrl->C.file, dataset_cpt, Topo->header);
if ((P = gmt_get_palette (GMT, cpt, GMT_CPT_OPTIONAL, Topo->header->z_min, Topo->header->z_max, Ctrl->C.dz)) == NULL) {
Return (API->error);
}
if (P->is_bw) Ctrl->Q.monochrome = true;
if (P->categorical && !(Ctrl->Q.mode == GRDVIEW_MESH || Ctrl->T.active)) {
GMT_Report (API, GMT_MSG_ERROR, "Categorical data (as implied by CPT) cannot be interpolated and require -T or just -Qm.\n");
Return (GMT_RUNTIME_ERROR);
}
if (cpt) gmt_M_str_free (cpt);
}
get_contours = (Ctrl->Q.mode == GRDVIEW_MESH && Ctrl->W.contour) || (Ctrl->Q.mode == GRDVIEW_SURF && P && P->n_colors > 1);
if (Ctrl->G.active) { /* Draping wanted */
if (Ctrl->G.n == 1 && gmt_M_file_is_image (Ctrl->G.file[0])) {
double inc[2];
/* Want to drape an image on top of surface. Do so by converting the image to r, g, b grids */
struct GMT_IMAGE *I = NULL;