Skip to content

Commit 309b014

Browse files
authored
Let colorbar -L have ability to set axis label and unit (#4980)
* Let colorbar place label when -L is used Since labels and units are set via -B, the use of -L precludes setting these strings. Now, they can be specified via new -S+x+y modifiers. * Update logcpt.sh
1 parent cad2c24 commit 309b014

File tree

5 files changed

+32
-19
lines changed

5 files changed

+32
-19
lines changed

doc/rst/source/colorbar.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Synopsis
2626
[ |-N|\ [**p**\|\ *dpi* ]]
2727
[ |-Q| ]
2828
[ |SYN_OPT-R| ]
29-
[ |-S|\ [**+a**\ *angle*][**+c**\|\ **n**\ ][**+s**] ]
29+
[ |-S|\ [**+a**\ *angle*][**+c**\|\ **n**\ ][**+s**][**+x**\ *label*][**+y**\ *unit*] ]
3030
[ |SYN_OPT-U| ]
3131
[ |SYN_OPT-V| ]
3232
[ |-W|\ *scale* ]

doc/rst/source/colorbar_common.rst_

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ Optional Arguments
134134
.. _-L:
135135

136136
**-L**\ [**i**][*gap*]
137+
137138
Gives equal-sized color rectangles. Default scales rectangles
138-
according to the z-range in the CPT (Also see **-Z**). If set,
139-
any equal interval annotation set with **-B** will be ignored. If
139+
according to the z-range in the CPT (Also see **-Z**). If
140140
*gap* is appended and the CPT is discrete we will center each
141141
annotation on each rectangle, using the lower boundary z-value for
142142
the annotation. If **i** is prepended we annotate the interval range
@@ -173,12 +173,14 @@ Optional Arguments
173173

174174
.. _-S:
175175

176-
**-S**\ [**+a**\ *angle*][**+c**\|\ **n**\ ][**+s**]
176+
**-S**\ [**+a**\ *angle*][**+c**\|\ **n**\ ][**+s**][**+x**\ *label*][**+y**\ *unit*]
177177
Control various aspects of color bar appearance when **-B** is *not* used.
178178
Append **+a** to place annotations at the given *angle* [default is no slanting].
179179
Append **+c** to use custom labels if given in the CPT as annotations.
180180
Append **+n** to use numerical labels [Default].
181181
Append **+s** to skip drawing gridlines separating different color intervals [Default draws gridlines].
182+
If **-L** is used then **-B** cannot be used, hence you may optionally set a bar label via **+x**\ *label*
183+
and any unit (i.e., y-label) via **+y**\ *unit*.
182184

183185
.. _-U:
184186

doc/rst/source/psscale.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Synopsis
2727
[ |-O| ]
2828
[ |-P| ] [ |-Q| ]
2929
[ |SYN_OPT-R| ]
30-
[ |-S|\ [**+a**\ *angle*][**+c**\|\ **n**\ ][**+s**] ]
30+
[ |-S|\ [**+a**\ *angle*][**+c**\|\ **n**\ ][**+s**][**+x**\ *label*][**+y**\ *unit*] ]
3131
[ |SYN_OPT-U| ]
3232
[ |SYN_OPT-V| ]
3333
[ |-W|\ *scale* ]

src/psscale.c

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,12 @@ struct PSSCALE_CTRL {
112112
struct PSSCALE_Q { /* -Q */
113113
bool active;
114114
} Q;
115-
struct PSSCALE_S { /* -S[+c|n][+s][+a<angle>] */
115+
struct PSSCALE_S { /* -S[+c|n][+s][+a<angle>][+x<label>][+y<unit>] */
116116
bool active;
117117
bool skip;
118118
double angle;
119119
unsigned int mode;
120+
char unit[GMT_LEN256], label[GMT_LEN256];
120121
} S;
121122
struct PSSCALE_W { /* -W<scale> */
122123
bool active;
@@ -163,7 +164,7 @@ static int usage (struct GMTAPI_CTRL *API, int level) {
163164
GMT_Message (API, GMT_TIME_NONE, "\t[-D%s[+w<length>[/<width>]][+e[b|f][<length>]][+h|v][+j<justify>][+ma|c|l|u][+n[<txt>]]%s]\n", GMT_XYANCHOR, GMT_OFFSET);
164165
GMT_Message (API, GMT_TIME_NONE, "\t[-F%s]\n", GMT_PANEL);
165166
GMT_Message (API, GMT_TIME_NONE, "\t[-G<zlo>/<zhi>] [-I[<max_intens>|<low_i>/<high_i>] [%s] %s[-L[i][<gap>]] [-M] [-N[p|<dpi>]]\n", GMT_J_OPT, API->K_OPT);
166-
GMT_Message (API, GMT_TIME_NONE, "\t%s%s[-Q] [%s] [-S[+a<angle>][+c|n][+s]] [%s] [%s] [-W<scale>]\n", API->O_OPT, API->P_OPT, GMT_Rgeoz_OPT, GMT_U_OPT, GMT_V_OPT);
167+
GMT_Message (API, GMT_TIME_NONE, "\t%s%s[-Q] [%s] [-S[+a<angle>][+c|n][+s][+x<label>][+y<unit>]] [%s] [%s] [-W<scale>]\n", API->O_OPT, API->P_OPT, GMT_Rgeoz_OPT, GMT_U_OPT, GMT_V_OPT);
167168
GMT_Message (API, GMT_TIME_NONE, "\t[%s] [%s] [-Z<zfile>]\n\t%s[%s] [%s] [%s]\n\n", GMT_X_OPT, GMT_Y_OPT, API->c_OPT, GMT_p_OPT, GMT_t_OPT, GMT_PAR_OPT);
168169

169170
if (level == GMT_SYNOPSIS) return (GMT_MODULE_SYNOPSIS);
@@ -215,6 +216,8 @@ static int usage (struct GMTAPI_CTRL *API, int level) {
215216
GMT_Message (API, GMT_TIME_NONE, "\t Append +c to use any custom labels in the CPT for annotations, if available.\n");
216217
GMT_Message (API, GMT_TIME_NONE, "\t Append +n to use numerical values for annotations [Default].\n");
217218
GMT_Message (API, GMT_TIME_NONE, "\t Append +s to skip drawing gridlines between different color sections [Default draws lines].\n");
219+
GMT_Message (API, GMT_TIME_NONE, "\t Append +x<label> to set a colorbar label [No label].\n");
220+
GMT_Message (API, GMT_TIME_NONE, "\t Append +y to set a colorbar unit [No unit].\n");
218221
GMT_Option (API, "U,V,X");
219222
GMT_Message (API, GMT_TIME_NONE, "\t-Z Give file with colorbar-width (in %s) per color entry.\n",
220223
API->GMT->session.unit_name[API->GMT->current.setting.proj_length_unit]);
@@ -351,6 +354,7 @@ static int parse (struct GMT_CTRL *GMT, struct PSSCALE_CTRL *Ctrl, struct GMT_OP
351354
j = 0;
352355
if (opt->arg[0] == 'i') Ctrl->L.interval = true, j = 1;
353356
if (opt->arg[j]) Ctrl->L.spacing = gmt_M_to_inch (GMT, &opt->arg[j]);
357+
if (c) c[0] = '+'; /* Restore option string */
354358
break;
355359
case 'M':
356360
Ctrl->M.active = true;
@@ -372,16 +376,20 @@ static int parse (struct GMT_CTRL *GMT, struct PSSCALE_CTRL *Ctrl, struct GMT_OP
372376
Ctrl->S.active = true;
373377
Ctrl->S.mode = PSSCALE_ANNOT_NUMERICAL; /* The default */
374378
if (opt->arg[0]) { /* Modern syntax with modifiers */
375-
if ((c = strstr (opt->arg, "+a"))) {
376-
Ctrl->S.mode |= PSSCALE_ANNOT_ANGLED;
377-
Ctrl->S.angle = atof (&c[2]);
379+
if ((c = gmt_first_modifier (GMT, opt->arg, "acnsxy"))) { /* Process any modifiers */
380+
unsigned int pos = 0; /* Reset to start of new word */
381+
while (gmt_getmodopt (GMT, 'S', c, "acnsxy", &pos, txt_a, &n_errors) && n_errors == 0) {
382+
switch (txt_a[0]) {
383+
case 'a': Ctrl->S.angle = atof (&txt_a[1]); Ctrl->S.mode |= PSSCALE_ANNOT_ANGLED; break;
384+
case 'c': Ctrl->S.mode |= PSSCALE_ANNOT_CUSTOM; break;
385+
case 'n': Ctrl->S.mode = PSSCALE_ANNOT_NUMERICAL; break;
386+
case 's': Ctrl->S.skip = true; break;
387+
case 'x': strncpy (Ctrl->S.label, &txt_a[1], GMT_LEN256); break;
388+
case 'y': strncpy (Ctrl->S.unit, &txt_a[1], GMT_LEN256); break;
389+
default: break; /* These are caught in gmt_getmodopt so break is just for Coverity */
390+
}
391+
}
378392
}
379-
if (strstr (opt->arg, "+c"))
380-
Ctrl->S.mode |= PSSCALE_ANNOT_CUSTOM;
381-
if (strstr (opt->arg, "+n")) /* Default, but just in case */
382-
Ctrl->S.mode = PSSCALE_ANNOT_NUMERICAL;
383-
if (strstr (opt->arg, "+s"))
384-
Ctrl->S.skip = true;
385393
}
386394
else /* Backwards compatible -S means -S+s */
387395
Ctrl->S.skip = true;
@@ -1026,7 +1034,10 @@ GMT_LOCAL void psscale_draw_colorbar (struct GMT_CTRL *GMT, struct PSSCALE_CTRL
10261034
strncpy (label, GMT->current.map.frame.axis[GMT_X].label, GMT_LEN256-1);
10271035
strncpy (unit, GMT->current.map.frame.axis[GMT_Y].label, GMT_LEN256-1);
10281036
GMT->current.map.frame.axis[GMT_X].label[0] = GMT->current.map.frame.axis[GMT_Y].label[1] = 0;
1029-
1037+
if (Ctrl->L.active) {
1038+
strncpy (label, Ctrl->S.label, GMT_LEN256-1);
1039+
strncpy (unit, Ctrl->S.unit, GMT_LEN256-1);
1040+
}
10301041
if (flip & PSSCALE_FLIP_ANNOT) { /* Place annotations on the opposite side */
10311042
justify = l_justify = (Ctrl->D.horizontal) ? PSL_BC : PSL_MR;
10321043
y_base = width;

test/makecpt/logcpt.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
ps=logcpt.ps
55
gmt makecpt -T1/1000/2 -Qo -Cjet | gmt psscale -Dx0i/4.5i+w9i/0.2i+jML -P -K -By+l"-Qo" > $ps
66
gmt makecpt -T1/3/0.2 -Qi -Cjet | gmt psscale -Dx2i/4.5i+w9i/0.2i+jML -By+l"-Qi" -O -K --FORMAT_FLOAT_MAP=%.0f >> $ps
7-
gmt makecpt -T1/1000/2 -Qo -Cjet | gmt psscale -Dx4i/4.5i+w9i/0.2i+jML -O -K -L -By+l"-Qo -L" >> $ps
8-
gmt makecpt -T1/3/0.2 -Qi -Cjet | gmt psscale -Dx6i/4.5i+w9i/0.2i+jML -L -By+l"-Qi -L" -O --FORMAT_FLOAT_MAP=%.0f >> $ps
7+
gmt makecpt -T1/1000/2 -Qo -Cjet | gmt psscale -Dx4i/4.5i+w9i/0.2i+jML -O -K -L -S+y"-Qo -L" >> $ps
8+
gmt makecpt -T1/3/0.2 -Qi -Cjet | gmt psscale -Dx6i/4.5i+w9i/0.2i+jML -L -S+y"-Qi -L" -O --FORMAT_FLOAT_MAP=%.0f >> $ps

0 commit comments

Comments
 (0)