Skip to content
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

Check for error if CPT has non-zero hinge #8160

Merged
merged 2 commits into from
Dec 8, 2023
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
3 changes: 2 additions & 1 deletion doc/rst/source/reference/features.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1386,7 +1386,7 @@ are identified as such via the special comment

| ``# HARD_HINGE``

and all hard hinges occur at data value *z = 0* (but you can change this value by
and all hard hinges must occur at data value *z = 0* (but you can change this value by
adding **+h**\ *value* to the name of the CPT).
Other CPTs may instead have a *soft* hinge which indicates a natural hinge or transition
point in the CPT itself, unrelated to any natural data set *per se*. These CPTs
Expand All @@ -1397,6 +1397,7 @@ are flagged by the special comment
CPTs with soft hinges behave as regular (non-hinge) CPTs *unless* the user activates then by
appending **+h**\ [*hinge*] to the CPT name. This modifier will convert the soft
hinge into a hard hinge at the user-specified data value *hinge* [which defaults to 0].
As for hard hinges, soft hinges must occur at data value *z = 0* in the CPT.
Note that if your specified data range *excludes* an activated soft or hard hinge then we
only perform color sampling from the *half* of the CPT that pertains to the data range.
All dynamic CPTs will need to be stretched to the user's preferred range, and there
Expand Down
2 changes: 1 addition & 1 deletion src/gmt_prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ EXTERN_MSC bool gmt_x_out_of_bounds (struct GMT_CTRL *GMT, int *i, struct GMT_GR
EXTERN_MSC bool gmt_row_col_out_of_bounds (struct GMT_CTRL *GMT, double *in, struct GMT_GRID_HEADER *h, openmp_int *row, openmp_int *col);
EXTERN_MSC int gmt_list_cpt (struct GMT_CTRL *GMT, char option);
EXTERN_MSC void gmt_scale_cpt (struct GMT_CTRL *GMT, struct GMT_PALETTE *P, double scale);
EXTERN_MSC void gmt_stretch_cpt (struct GMT_CTRL *GMT, struct GMT_PALETTE *P, double z_low, double z_high);
EXTERN_MSC unsigned int gmt_stretch_cpt (struct GMT_CTRL *GMT, struct GMT_PALETTE *P, double z_low, double z_high);
EXTERN_MSC struct GMT_PALETTE * gmt_sample_cpt (struct GMT_CTRL *GMT, struct GMT_PALETTE *Pin, double z[], int nz, bool continuous, bool reverse, bool log_mode, bool no_inter);
EXTERN_MSC void gmt_invert_cpt (struct GMT_CTRL *GMT, struct GMT_PALETTE *P);
EXTERN_MSC void gmt_undo_log10 (struct GMT_CTRL *GMT, struct GMT_PALETTE *P);
Expand Down
16 changes: 9 additions & 7 deletions src/gmt_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -1468,8 +1468,8 @@ GMT_LOCAL int gmtsupport_find_cpt_hinge (struct GMT_CTRL *GMT, struct GMT_PALETT
GMT_Report (GMT->parent, GMT_MSG_DEBUG, "Found CPT hinge at z' = 0 for slice k = %u!\n", k);
return (int)k;
}
GMT_Report (GMT->parent, GMT_MSG_WARNING, "Hinge requested but no hinge at z' = 0 was found in this CPT\n");
return GMT_NOTSET;
GMT_Report (GMT->parent, GMT_MSG_ERROR, "Hinge requested but no hinge at z' = 0 was found in this CPT\n");
return GMT_PARSE_ERROR;
}

/*! . */
Expand Down Expand Up @@ -9041,7 +9041,7 @@ struct GMT_PALETTE *gmt_get_palette (struct GMT_CTRL *GMT, char *file, enum GMT_
}
PH = gmt_get_C_hidden (P);
PH->auto_scale = 1; /* Flag for colorbar to supply -Baf if not given */
gmt_stretch_cpt (GMT, P, zmin, zmax);
if ((gmt_stretch_cpt (GMT, P, zmin, zmax)) == GMT_PARSE_ERROR) return (NULL);
}
else if (file) { /* Gave a CPT file */
GMT_Report (GMT->parent, GMT_MSG_DEBUG, "CPT argument %s understood to be a regular CPT table\n", file);
Expand Down Expand Up @@ -9098,6 +9098,7 @@ GMT_LOCAL int gmtsupport_validate_cpt (struct GMT_CTRL *GMT, struct GMT_PALETTE
if (!P->has_hinge) return GMT_NOTSET; /* Not our concern here */
/* Claims to have a hinge */
ks = gmtsupport_find_cpt_hinge (GMT, P); /* Get hinge slice (or -1 if no hinge found) */
if (ks == GMT_PARSE_ERROR) return (GMT_PARSE_ERROR);
if (ks == GMT_NOTSET) { /* Must be a rogue CPT - ignore the hinge setting */
GMT_Report (GMT->parent, GMT_MSG_WARNING, "gmt_stretch_cpt: CPT says it has a hinge but none is actually found? - ignored.\n");
P->has_hinge = 0;
Expand All @@ -9123,7 +9124,7 @@ GMT_LOCAL int gmtsupport_validate_cpt (struct GMT_CTRL *GMT, struct GMT_PALETTE
}

/*! . */
void gmt_stretch_cpt (struct GMT_CTRL *GMT, struct GMT_PALETTE *P, double z_low, double z_high) {
unsigned int gmt_stretch_cpt (struct GMT_CTRL *GMT, struct GMT_PALETTE *P, double z_low, double z_high) {
/* Replace CPT z-values with new ones linearly scaled from z_low to z_high. If these are
* zero then we substitute the CPT's default range instead (if available).
* Note: If P has a hinge then its value is expected to be in the final user data values.
Expand All @@ -9138,11 +9139,11 @@ void gmt_stretch_cpt (struct GMT_CTRL *GMT, struct GMT_PALETTE *P, double z_low,
if (z_low == z_high) { /* Range information not given, rely on CPT RANGE setting */
if (P->has_range == 0) {
GMT_Report (GMT->parent, GMT_MSG_DEBUG, "gmt_stretch_cpt: Passed z_low == z_high but CPT has no explicit range. No changes made\n");
return;
return GMT_NOERROR;
}
z_low = P->minmax[0]; z_high = P->minmax[1];
}
ks = gmtsupport_validate_cpt (GMT, P, &z_low, &z_high); /* Deal with any issure related to hinges */
if ((ks = gmtsupport_validate_cpt (GMT, P, &z_low, &z_high)) == GMT_PARSE_ERROR) return (GMT_PARSE_ERROR); /* Deal with any issue related to hinges */

z_min = P->data[0].z_low;
z_start = z_low;
Expand All @@ -9160,6 +9161,7 @@ void gmt_stretch_cpt (struct GMT_CTRL *GMT, struct GMT_PALETTE *P, double z_low,
P->data[is].z_high = z_start + (P->data[is].z_high - z_min) * scale;
P->data[is].i_dz /= scale;
}
return (GMT_NOERROR);
}

/*! . */
Expand Down Expand Up @@ -9743,7 +9745,7 @@ struct GMT_PALETTE * gmt_truncate_cpt (struct GMT_CTRL *GMT, struct GMT_PALETTE
* expand it to its natural z-range before we can truncate since z_low/z_high are in user units */

if (P->has_range) {
gmt_stretch_cpt (GMT, P, 0.0, 0.0); /* Stretch to its natural range first */
if ((gmt_stretch_cpt (GMT, P, 0.0, 0.0)) == GMT_PARSE_ERROR) return NULL; /* Stretch to its natural range first */
P->has_range = 0;
}

Expand Down
3 changes: 2 additions & 1 deletion src/grd2cpt.c
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,8 @@ EXTERN_MSC int GMT_grd2cpt (void *V_API, int mode, void *args) {

if (Ctrl->E.active && Ctrl->E.levels == 0) { /* Use existing CPT structure, just linearly change z */
if ((Pout = GMT_Duplicate_Data (API, GMT_IS_PALETTE, GMT_DUPLICATE_ALLOC, Pin)) == NULL) return (API->error);
gmt_stretch_cpt (GMT, Pout, Ctrl->L.min, Ctrl->L.max);
if ((gmt_stretch_cpt (GMT, Pout, Ctrl->L.min, Ctrl->L.max)) == GMT_PARSE_ERROR)
Return (GMT_RUNTIME_ERROR);
if (Ctrl->I.mode & GMT_CPT_C_REVERSE)
gmt_invert_cpt (GMT, Pout); /* Also flip the colors */
cpt_flags = 0;
Expand Down
4 changes: 3 additions & 1 deletion src/makecpt.c
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,9 @@ EXTERN_MSC int GMT_makecpt (void *V_API, int mode, void *args) {
}
if (!Ctrl->T.interpolate) { /* Just copy what was in the CPT but stretch to given range min/max */
if ((Pout = GMT_Duplicate_Data (API, GMT_IS_PALETTE, GMT_DUPLICATE_ALLOC, Pin)) == NULL) return (API->error);
gmt_stretch_cpt (GMT, Pout, Ctrl->T.T.min, Ctrl->T.T.max); /* Stretch to given range or use natural range if 0/0 */
/* Stretch to given range or use natural range if 0/0 */
if ((gmt_stretch_cpt (GMT, Pout, Ctrl->T.T.min, Ctrl->T.T.max)) == GMT_PARSE_ERROR)
Return (GMT_RUNTIME_ERROR);
if (Ctrl->I.mode & GMT_CPT_C_REVERSE) /* Also flip the colors */
gmt_invert_cpt (GMT, Pout);
if (Ctrl->Q.mode == 1)
Expand Down
6 changes: 4 additions & 2 deletions src/psscale.c
Original file line number Diff line number Diff line change
Expand Up @@ -1922,8 +1922,10 @@ EXTERN_MSC int GMT_psscale (void *V_API, int mode, void *args) {
Ctrl->D.emode &= 4; /* This removes any 1,2,3 of selected but leaves 4 for nan */
}

if (P->has_range) /* Convert from normalized to default CPT z-range */
gmt_stretch_cpt (GMT, P, 0.0, 0.0);
if (P->has_range) { /* Convert from normalized to default CPT z-range */
if ((gmt_stretch_cpt (GMT, P, 0.0, 0.0)) == GMT_PARSE_ERROR)
Return (GMT_RUNTIME_ERROR);
}

if (P->categorical && (Ctrl->D.emode & 1 || Ctrl->D.emode & 2)) {
GMT_Report (API, GMT_MSG_WARNING, "Option -D: Cannot select back/fore-ground extender for categorical CPT\n");
Expand Down
Loading