Skip to content

Commit 2789263

Browse files
authored
Make the -J question-mark for subplots and insets optional (#4758)
* Allow the ?-mark for unknow size to be optional Given the issues with csh discussed in #3916, this PR makes the questionmark optional. It even allows for a lack of final slash. * Update gmt_init.c * Implement the more conservative scheme when ? is not present Require a trailing / if at least one another argument, or otherwise have no numbers after the projection code. Adjusted the demonstration inset script and docs. added complement script that tests -JX without ?.
1 parent 20e279d commit 2789263

File tree

8 files changed

+109
-22
lines changed

8 files changed

+109
-22
lines changed

doc/rst/source/inset.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ Description
3434
The **begin** directive of **inset** defines the dimension and placement of the inset canvas. It
3535
records the current region and projection so that we may return to the initial
3636
plot environment when the inset is completed. The user may select any plot region
37-
and projection once plotting in the inset, but if the first command uses
38-
? as scale or width then we adjust the scale or width to fill the inset as best
37+
and projection once plotting in the inset, but if the first command uses a projection
38+
that leaves off the scale or width then we supply a scale or width to fill the inset as best
3939
as possible, given the inset size and margins (if selected).
4040

4141

@@ -129,7 +129,7 @@ To make a simple basemap plot called inset.pdf that demonstrates the inset modul
129129
gmt begin inset pdf
130130
gmt basemap -R0/40/20/60 -JM6.5i -Bafg -B+glightgreen
131131
gmt inset begin -DjTR+w2.5i+o0.2i -F+gpink+p0.5p -M0.25i
132-
gmt basemap -Rg -JA20/20/2i -Bafg
132+
gmt basemap -Rg -JA20/20/ -Bafg
133133
gmt text -F+f18p+cTR+tINSET -Dj-0.15i -N
134134
gmt inset end
135135
gmt text -F+f18p+cBL+tMAP -Dj0.2i

doc/rst/source/subplot.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,10 @@ Synopsis (set mode)
180180
**gmt subplot set** [ *row,col*\|\ *index* ] [ **-A**\ *fixedlabel*] [ **-C**\ *side*\ *clearance* ] [ |SYN_OPT-V| ]
181181

182182
Before you start plotting you must first select the active subplot.
183-
**Note**: If any **-J** option is passed with **?** as scale or width when plotting subplots,
184-
then the dimensions of the map are automatically determined by the subplot size and your region.
183+
**Note**: If any **-J** option is passed *without* the scale or width when you first are plotting
184+
inside the subplot, then the scale of the map is automatically determined by the subplot size and your region.
185185
For Cartesian plots: If you want the scale to apply *equally* to both dimensions
186-
then you must specify **-Jx** [The default **-JX** will fill the subplot by using unequal scales].
186+
then you must specify **-Jx** [The default projection of **-JX** will fill the subplot by using unequal scales].
187187

188188
Optional Arguments
189189
------------------

doc/scripts/GMT_inset.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ gmt begin GMT_inset
66
# Bottom map of Australia
77
gmt coast -R110E/170E/44S/9S -JM6i -B -BWSne -Wfaint -N2/1p -EAU+gbisque -Gbrown -Sazure1 -Da -Xc --FORMAT_GEO_MAP=dddF
88
gmt inset begin -DjTR+w1.5i+o0.15i -F+gwhite+p1p+s -M0.05i
9-
gmt coast -Rg -JG120/30S/1.4i -Da -Gbrown -A5000 -Bg -Wfaint -EAU+gbisque
9+
gmt coast -Rg -JG120/30S/ -Da -Gbrown -A5000 -Bg -Wfaint -EAU+gbisque
1010
gmt inset end
1111
gmt end show

src/gmt_init.c

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13805,6 +13805,70 @@ GMT_LOCAL int gmtinit_set_last_dimensions (struct GMTAPI_CTRL *API) {
1380513805
return (GMT_NOERROR);
1380613806
}
1380713807

13808+
GMT_LOCAL bool gmtinit_replace_missing_with_questionmark (struct GMTAPI_CTRL *API, char *arg, char *newarg) {
13809+
/* If this -J argument is like the '?' args (but with the '?' missing) and having trailing /, or missing an only arg, then
13810+
* we append or insert the missing ? so that the gmtinit_build_new_J_option can work as is.
13811+
* We assume newarg is completely blank. We only get here if there is no ? in arg. */
13812+
size_t o = 0, i = 0, L = strlen (arg) - 1; /* Index of last character in arg (we know arg has at least length 1) */
13813+
13814+
/* Category 1 projections: Always has slashes and need to end in /? */
13815+
if ((strchr ("cC", arg[0]) && !strncmp (&arg[1], "yl_stere", 8U)) || strchr ("aAbBcCdDeEfFgGlLoOsStT", arg[0])) { /* These projection all must end in / and if no ? then append it */
13816+
if (arg[L] == '/') /* User followed instructions and left a trailing / */
13817+
sprintf (newarg, "%s?", arg);
13818+
else {
13819+
GMT_Report (API, GMT_MSG_DEBUG, "gmtinit_replace_missing_with_questionmark: -J%s has no trailing slash. Assumed to be a complete geographic projection\n", arg);
13820+
return false;
13821+
}
13822+
}
13823+
else if ((strchr ("pP", arg[0]) && !strncmp (&arg[1], "oly", 3U)) || strchr ("hHiIjJkKmMnNqQrRvVwWyYuU", arg[0])) { /* These may or may not have a trailing slash */
13824+
if (arg[L] == '/') { /* Multiple argument so left a trailing / */
13825+
sprintf (newarg, "%s?", arg);
13826+
}
13827+
else { /* Used defaults so here we have things like -JM, -Jkf, or -Jpoly; otherwise it is -JM15c etc (i.e., with argument) */
13828+
if (L == 0 || (L == 1 && strchr ("kK", arg[0]) && strchr ("fs", arg[1]))) /* No argument, just append ? */
13829+
sprintf (newarg, "%s?", arg);
13830+
else {
13831+
GMT_Report (API, GMT_MSG_DEBUG, "gmtinit_replace_missing_with_questionmark: -J%s has no trailing slash. Assumed to be a complete geographic projection\n", arg);
13832+
return false;
13833+
}
13834+
}
13835+
}
13836+
else if (strchr ("xX", arg[0])) { /* Cartesian projection, must worry about separate x and y settings if a slash is found */
13837+
/* Look for Cartesian -Jx|X[-][d|l|p<pow>][/[-][d|l|p<pow>]] which needs one or two ?-marks to be inserted for the two dummy Cartesian scales.
13838+
* But don't touch things like -Jx|X[+|-]<number> */
13839+
newarg[o++] = arg[i++]; /* This is x or X */
13840+
if (arg[i] && strchr ("-+", arg[i])) newarg[o++] = arg[i++]; /* Placing an optional sign (-Jx has no modifiers) */
13841+
if (isdigit (arg[i]) || arg[i] == '.') { /* Got -Jx-.5, -JX3c, -JX-2 or similar, do nothing */
13842+
GMT_Report (API, GMT_MSG_DEBUG, "gmtinit_replace_missing_with_questionmark: -J%s assumed to be a complete Cartesian projection\n", arg);
13843+
return false;
13844+
}
13845+
/* Here we must insert or append one or two ? */
13846+
newarg[o++] = '?'; /* Insert the first ?-mark */
13847+
if (strchr (arg, '/')) { /* slash[0] == '/' means we got separate x and y scale args for linear[d]/log/power axes */
13848+
while (arg[i] != '/') newarg[o++] = arg[i++]; /* Copying any linear[d]/log etc args for x-axis until the slash */
13849+
newarg[o++] = arg[i++]; /* This is the / */
13850+
if (arg[i] && strchr ("-+", arg[i])) newarg[o++] = arg[i++]; /* Placing the optional second sign */
13851+
newarg[o++] = '?'; /* Insert the second ?-mark */
13852+
while (arg[i]) newarg[o++] = arg[i++]; /* Copying any linear[d]/log etc args for y-axis until the end */
13853+
}
13854+
else { /* Just a single scale/width. With ? already appended, check for the log,power,degree args */
13855+
while (arg[i]) newarg[o++] = arg[i++]; /* Copying any linear[d]/log etc args until the end */
13856+
}
13857+
}
13858+
else if (strchr ("pP", arg[0])) { /* Polar (cylindrical) projection, must insert missing ?-mark if no scale/width given */
13859+
newarg[o++] = arg[i++]; /* This is p or P */
13860+
if (isdigit (arg[i]) || arg[i] == '.') { /* Got -Jp.5, -JP15c or similar (there are no signs here */
13861+
GMT_Report (API, GMT_MSG_DEBUG, "gmtinit_replace_missing_with_questionmark: -J%s assumed to be a complete Polar projection\n", arg);
13862+
return false;
13863+
}
13864+
newarg[o++] = '?'; /* Insert the ?-mark */
13865+
while (arg[i]) newarg[o++] = arg[i++]; /* Copying any polar modifiers until the end */
13866+
}
13867+
GMT_Report (API, GMT_MSG_DEBUG, "Modern mode: First converted -J%s to -J%s.\n", arg, newarg);
13868+
13869+
return true; /* yes, we made changes */
13870+
}
13871+
1380813872
GMT_LOCAL bool gmtinit_build_new_J_option (struct GMTAPI_CTRL *API, struct GMT_OPTION *opt_J, struct GMT_SUBPLOT *P, struct GMT_INSET *I, bool is_psrose) {
1380913873
/* Look for Cartesian -Jx|X[-]?[d|l|p<pow>][/[-]?[d|l|p<pow>]] which needs one or two ?-marks to be replaced with dummy Cartesian scales.
1381013874
* Otherwise, -J<code>? or -J<code><arg>/<arg>/.../? which needs only one ?-mark to be replaced with dummy map scale. */
@@ -13814,12 +13878,17 @@ GMT_LOCAL bool gmtinit_build_new_J_option (struct GMTAPI_CTRL *API, struct GMT_O
1381413878
int Iyscl = 1;
1381513879

1381613880
if (opt_J == NULL) return false; /* No -J option to update */
13817-
if ((c = strchr (opt_J->arg, '?')) == NULL) return false; /* No questionmark in the argument to update */
13818-
strncpy (oldarg, opt_J->arg, GMT_LEN128-1);
13881+
if (opt_J->arg == NULL || opt_J->arg[0] == '\0') return false; /* No argument to update */
13882+
if (strchr (opt_J->arg, '?')) /* Found ?, we must go to work, make a copy of oldargs */
13883+
strncpy (oldarg, opt_J->arg, GMT_LEN128-1);
13884+
else if (!gmtinit_replace_missing_with_questionmark (API, opt_J->arg, oldarg)) /* Not an argument we should update */
13885+
/* If an argument with nothing instead of ? then we insert ? so the rest of the function can work */
13886+
return false;
1381913887

13888+
c = strchr (oldarg, '?'); /* Pointer to questionmark in the argument */
1382013889
/* Here, c[0] is the first question mark (there may be one or two) */
13821-
if (strchr ("xX", opt_J->arg[0])) { /* Cartesian projection, must worry about separate x and y settings if a slash is found */
13822-
slash = strchr (opt_J->arg, '/'); /* slash[0] == '/' means we got separate x and y scale args for linear[d]/log/power axes */
13890+
if (strchr ("xX", oldarg[0])) { /* Cartesian projection, must worry about separate x and y settings if a slash is found */
13891+
slash = strchr (oldarg, '/'); /* slash[0] == '/' means we got separate x and y scale args for linear[d]/log/power axes */
1382313892
if (slash && slash[1] == '-') { /* While any negative x-scale will automatically be included, for y we just make sure we scale by -1 if a hyphen is found after the slash */
1382413893
if (P) P->dir[GMT_Y] = -1; else if (I) Iyscl = -1; /* Only use P or I if defined */
1382513894
}
@@ -13849,7 +13918,7 @@ GMT_LOCAL bool gmtinit_build_new_J_option (struct GMTAPI_CTRL *API, struct GMT_O
1384913918
snprintf (sclX, GMT_LEN64, "%gi", I->w);
1385013919
}
1385113920
arg[0] = c[0] = '\0'; /* Chop off everything from first ? to end */
13852-
snprintf (arg, GMT_LEN128, "%s%s", opt_J->arg, sclX); /* Build new -J<arg> from initial J arg, then replace first ? with sclX */
13921+
snprintf (arg, GMT_LEN128, "%s%s", oldarg, sclX); /* Build new -J<arg> from initial J arg, then replace first ? with sclX */
1385313922
c[0] = '?'; /* Put back the ? we removed */
1385413923
if (c[1] == 'l') /* Must add the log character after the scale */
1385513924
strcat (arg, "l");

src/inset.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static int usage (struct GMTAPI_CTRL *API, int level) {
8585
const char *name = gmt_show_name_and_purpose (API, THIS_MODULE_LIB, THIS_MODULE_CLASSIC_NAME, THIS_MODULE_PURPOSE);
8686
if (level == GMT_MODULE_PURPOSE) return (GMT_NOERROR);
8787
GMT_Message (API, GMT_TIME_NONE, "usage: %s begin -D%s |\n\t-D%s\n", name, GMT_INSET_A, GMT_INSET_B);
88-
GMT_Message (API, GMT_TIME_NONE, "\t[-F%s] [-M<margins>] [-N] [%s] [%s]\n\n", GMT_PANEL, GMT_V_OPT, GMT_PAR_OPT);
88+
GMT_Message (API, GMT_TIME_NONE, "\t[-F%s]\n\t[-M<margins>] [-N] [%s] [%s]\n\n", GMT_PANEL, GMT_V_OPT, GMT_PAR_OPT);
8989
GMT_Message (API, GMT_TIME_NONE, "usage: %s end [%s]\n\n", name, GMT_V_OPT);
9090

9191
if (level == GMT_SYNOPSIS) return (GMT_MODULE_SYNOPSIS);

test/subplot/linpanels.sh

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
#!/usr/bin/env bash
2-
# Test various -J perturbations
2+
# Test various -J perturbations without the ? marks
33
gmt begin linpanels ps
44
gmt set MAP_FRAME_TYPE plain
55
gmt subplot begin 3x3 -Fs5c/7c -A1 -M6p -R0/100/0/80 -BWSen
66
gmt basemap -c
7-
gmt basemap -c -JX?
8-
gmt basemap -c -JX?/?
7+
gmt basemap -c -JX
8+
gmt basemap -c -JX/
99

10-
gmt basemap -c -JX-?/?
11-
gmt basemap -c -JX?/-?
12-
gmt basemap -c -JX-?/-?
10+
gmt basemap -c -JX-/
11+
gmt basemap -c -JX/-
12+
gmt basemap -c -JX-/-
1313

14-
gmt basemap -c -JX?d/?
15-
gmt basemap -c -JX?/?d
16-
gmt basemap -c -JX?d/?d
14+
gmt basemap -c -JXd/
15+
gmt basemap -c -JX/d
16+
gmt basemap -c -JXd/d
1717
gmt subplot end
1818
gmt end show

test/subplot/linpanelsnew.ps

42.9 KB
Binary file not shown.

test/subplot/linpanelsnew.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
# Test various -J perturbations
3+
gmt begin linpanelsnew ps
4+
gmt set MAP_FRAME_TYPE plain
5+
gmt subplot begin 3x3 -Fs5c/7c -A1 -M6p -R0/100/0/80 -BWSen
6+
gmt basemap -c
7+
gmt basemap -c -JX?
8+
gmt basemap -c -JX?/?
9+
10+
gmt basemap -c -JX-?/?
11+
gmt basemap -c -JX?/-?
12+
gmt basemap -c -JX-?/-?
13+
14+
gmt basemap -c -JX?d/?
15+
gmt basemap -c -JX?/?d
16+
gmt basemap -c -JX?d/?d
17+
gmt subplot end
18+
gmt end show

0 commit comments

Comments
 (0)