Skip to content

Make the -J question-mark for subplots and insets optional #4758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions doc/rst/source/inset.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ Description
The **begin** directive of **inset** defines the dimension and placement of the inset canvas. It
records the current region and projection so that we may return to the initial
plot environment when the inset is completed. The user may select any plot region
and projection once plotting in the inset, but if the first command uses
? as scale or width then we adjust the scale or width to fill the inset as best
and projection once plotting in the inset, but if the first command uses a projection
that leaves off the scale or width then we supply a scale or width to fill the inset as best
as possible, given the inset size and margins (if selected).


Expand Down Expand Up @@ -129,7 +129,7 @@ To make a simple basemap plot called inset.pdf that demonstrates the inset modul
gmt begin inset pdf
gmt basemap -R0/40/20/60 -JM6.5i -Bafg -B+glightgreen
gmt inset begin -DjTR+w2.5i+o0.2i -F+gpink+p0.5p -M0.25i
gmt basemap -Rg -JA20/20/2i -Bafg
gmt basemap -Rg -JA20/20/ -Bafg
gmt text -F+f18p+cTR+tINSET -Dj-0.15i -N
gmt inset end
gmt text -F+f18p+cBL+tMAP -Dj0.2i
Expand Down
6 changes: 3 additions & 3 deletions doc/rst/source/subplot.rst
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ Synopsis (set mode)
**gmt subplot set** [ *row,col*\|\ *index* ] [ **-A**\ *fixedlabel*] [ **-C**\ *side*\ *clearance* ] [ |SYN_OPT-V| ]

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

Optional Arguments
------------------
Expand Down
2 changes: 1 addition & 1 deletion doc/scripts/GMT_inset.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ gmt begin GMT_inset
# Bottom map of Australia
gmt coast -R110E/170E/44S/9S -JM6i -B -BWSne -Wfaint -N2/1p -EAU+gbisque -Gbrown -Sazure1 -Da -Xc --FORMAT_GEO_MAP=dddF
gmt inset begin -DjTR+w1.5i+o0.15i -F+gwhite+p1p+s -M0.05i
gmt coast -Rg -JG120/30S/1.4i -Da -Gbrown -A5000 -Bg -Wfaint -EAU+gbisque
gmt coast -Rg -JG120/30S/ -Da -Gbrown -A5000 -Bg -Wfaint -EAU+gbisque
gmt inset end
gmt end show
79 changes: 74 additions & 5 deletions src/gmt_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -13805,6 +13805,70 @@ GMT_LOCAL int gmtinit_set_last_dimensions (struct GMTAPI_CTRL *API) {
return (GMT_NOERROR);
}

GMT_LOCAL bool gmtinit_replace_missing_with_questionmark (struct GMTAPI_CTRL *API, char *arg, char *newarg) {
/* If this -J argument is like the '?' args (but with the '?' missing) and having trailing /, or missing an only arg, then
* we append or insert the missing ? so that the gmtinit_build_new_J_option can work as is.
* We assume newarg is completely blank. We only get here if there is no ? in arg. */
size_t o = 0, i = 0, L = strlen (arg) - 1; /* Index of last character in arg (we know arg has at least length 1) */

/* Category 1 projections: Always has slashes and need to end in /? */
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 */
if (arg[L] == '/') /* User followed instructions and left a trailing / */
sprintf (newarg, "%s?", arg);
else {
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);
return false;
}
}
else if ((strchr ("pP", arg[0]) && !strncmp (&arg[1], "oly", 3U)) || strchr ("hHiIjJkKmMnNqQrRvVwWyYuU", arg[0])) { /* These may or may not have a trailing slash */
if (arg[L] == '/') { /* Multiple argument so left a trailing / */
sprintf (newarg, "%s?", arg);
}
else { /* Used defaults so here we have things like -JM, -Jkf, or -Jpoly; otherwise it is -JM15c etc (i.e., with argument) */
if (L == 0 || (L == 1 && strchr ("kK", arg[0]) && strchr ("fs", arg[1]))) /* No argument, just append ? */
sprintf (newarg, "%s?", arg);
else {
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);
return false;
}
}
}
else if (strchr ("xX", arg[0])) { /* Cartesian projection, must worry about separate x and y settings if a slash is found */
/* 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.
* But don't touch things like -Jx|X[+|-]<number> */
newarg[o++] = arg[i++]; /* This is x or X */
if (arg[i] && strchr ("-+", arg[i])) newarg[o++] = arg[i++]; /* Placing an optional sign (-Jx has no modifiers) */
if (isdigit (arg[i]) || arg[i] == '.') { /* Got -Jx-.5, -JX3c, -JX-2 or similar, do nothing */
GMT_Report (API, GMT_MSG_DEBUG, "gmtinit_replace_missing_with_questionmark: -J%s assumed to be a complete Cartesian projection\n", arg);
return false;
}
/* Here we must insert or append one or two ? */
newarg[o++] = '?'; /* Insert the first ?-mark */
if (strchr (arg, '/')) { /* slash[0] == '/' means we got separate x and y scale args for linear[d]/log/power axes */
while (arg[i] != '/') newarg[o++] = arg[i++]; /* Copying any linear[d]/log etc args for x-axis until the slash */
newarg[o++] = arg[i++]; /* This is the / */
if (arg[i] && strchr ("-+", arg[i])) newarg[o++] = arg[i++]; /* Placing the optional second sign */
newarg[o++] = '?'; /* Insert the second ?-mark */
while (arg[i]) newarg[o++] = arg[i++]; /* Copying any linear[d]/log etc args for y-axis until the end */
}
else { /* Just a single scale/width. With ? already appended, check for the log,power,degree args */
while (arg[i]) newarg[o++] = arg[i++]; /* Copying any linear[d]/log etc args until the end */
}
}
else if (strchr ("pP", arg[0])) { /* Polar (cylindrical) projection, must insert missing ?-mark if no scale/width given */
newarg[o++] = arg[i++]; /* This is p or P */
if (isdigit (arg[i]) || arg[i] == '.') { /* Got -Jp.5, -JP15c or similar (there are no signs here */
GMT_Report (API, GMT_MSG_DEBUG, "gmtinit_replace_missing_with_questionmark: -J%s assumed to be a complete Polar projection\n", arg);
return false;
}
newarg[o++] = '?'; /* Insert the ?-mark */
while (arg[i]) newarg[o++] = arg[i++]; /* Copying any polar modifiers until the end */
}
GMT_Report (API, GMT_MSG_DEBUG, "Modern mode: First converted -J%s to -J%s.\n", arg, newarg);

return true; /* yes, we made changes */
}

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) {
/* 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.
* Otherwise, -J<code>? or -J<code><arg>/<arg>/.../? which needs only one ?-mark to be replaced with dummy map scale. */
Expand All @@ -13814,12 +13878,17 @@ GMT_LOCAL bool gmtinit_build_new_J_option (struct GMTAPI_CTRL *API, struct GMT_O
int Iyscl = 1;

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

c = strchr (oldarg, '?'); /* Pointer to questionmark in the argument */
/* Here, c[0] is the first question mark (there may be one or two) */
if (strchr ("xX", opt_J->arg[0])) { /* Cartesian projection, must worry about separate x and y settings if a slash is found */
slash = strchr (opt_J->arg, '/'); /* slash[0] == '/' means we got separate x and y scale args for linear[d]/log/power axes */
if (strchr ("xX", oldarg[0])) { /* Cartesian projection, must worry about separate x and y settings if a slash is found */
slash = strchr (oldarg, '/'); /* slash[0] == '/' means we got separate x and y scale args for linear[d]/log/power axes */
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 */
if (P) P->dir[GMT_Y] = -1; else if (I) Iyscl = -1; /* Only use P or I if defined */
}
Expand Down Expand Up @@ -13849,7 +13918,7 @@ GMT_LOCAL bool gmtinit_build_new_J_option (struct GMTAPI_CTRL *API, struct GMT_O
snprintf (sclX, GMT_LEN64, "%gi", I->w);
}
arg[0] = c[0] = '\0'; /* Chop off everything from first ? to end */
snprintf (arg, GMT_LEN128, "%s%s", opt_J->arg, sclX); /* Build new -J<arg> from initial J arg, then replace first ? with sclX */
snprintf (arg, GMT_LEN128, "%s%s", oldarg, sclX); /* Build new -J<arg> from initial J arg, then replace first ? with sclX */
c[0] = '?'; /* Put back the ? we removed */
if (c[1] == 'l') /* Must add the log character after the scale */
strcat (arg, "l");
Expand Down
2 changes: 1 addition & 1 deletion src/inset.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static int usage (struct GMTAPI_CTRL *API, int level) {
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_Message (API, GMT_TIME_NONE, "usage: %s begin -D%s |\n\t-D%s\n", name, GMT_INSET_A, GMT_INSET_B);
GMT_Message (API, GMT_TIME_NONE, "\t[-F%s] [-M<margins>] [-N] [%s] [%s]\n\n", GMT_PANEL, GMT_V_OPT, GMT_PAR_OPT);
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);
GMT_Message (API, GMT_TIME_NONE, "usage: %s end [%s]\n\n", name, GMT_V_OPT);

if (level == GMT_SYNOPSIS) return (GMT_MODULE_SYNOPSIS);
Expand Down
18 changes: 9 additions & 9 deletions test/subplot/linpanels.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#!/usr/bin/env bash
# Test various -J perturbations
# Test various -J perturbations without the ? marks
gmt begin linpanels ps
gmt set MAP_FRAME_TYPE plain
gmt subplot begin 3x3 -Fs5c/7c -A1 -M6p -R0/100/0/80 -BWSen
gmt basemap -c
gmt basemap -c -JX?
gmt basemap -c -JX?/?
gmt basemap -c -JX
gmt basemap -c -JX/

gmt basemap -c -JX-?/?
gmt basemap -c -JX?/-?
gmt basemap -c -JX-?/-?
gmt basemap -c -JX-/
gmt basemap -c -JX/-
gmt basemap -c -JX-/-

gmt basemap -c -JX?d/?
gmt basemap -c -JX?/?d
gmt basemap -c -JX?d/?d
gmt basemap -c -JXd/
gmt basemap -c -JX/d
gmt basemap -c -JXd/d
gmt subplot end
gmt end show
Binary file added test/subplot/linpanelsnew.ps
Binary file not shown.
18 changes: 18 additions & 0 deletions test/subplot/linpanelsnew.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
# Test various -J perturbations
gmt begin linpanelsnew ps
gmt set MAP_FRAME_TYPE plain
gmt subplot begin 3x3 -Fs5c/7c -A1 -M6p -R0/100/0/80 -BWSen
gmt basemap -c
gmt basemap -c -JX?
gmt basemap -c -JX?/?

gmt basemap -c -JX-?/?
gmt basemap -c -JX?/-?
gmt basemap -c -JX-?/-?

gmt basemap -c -JX?d/?
gmt basemap -c -JX?/?d
gmt basemap -c -JX?d/?d
gmt subplot end
gmt end show