Skip to content

Commit

Permalink
Save any default CPT in hidden grid header
Browse files Browse the repository at this point in the history
This addresses #6167 when we cut a subset from, sa, earth_relief_10m and then no longer know what the CPT should be.
  • Loading branch information
PaulWessel committed Dec 31, 2021
1 parent d8ebeef commit 980792a
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/gmt_dev.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ extern "C" {
#include "gmt_common_math.h" /* Shared math functions */
#include "gmt.h" /* All GMT high-level API */
#include "gmt_private.h" /* API declaration needed by libraries */
#include "gmt_constants.h" /* All basic constant definitions */
#include "gmt_hidden.h" /* Hidden bookkeeping structure for API containers */

struct GMT_CTRL; /* forward declaration of GMT_CTRL */

#include "gmt_notposix.h" /* Non-POSIX extensions */

#include "gmt_constants.h" /* All basic constant definitions */
#include "gmt_modern.h" /* Modern mode constant definitions */
#include "gmt_macros.h" /* All basic macros definitions */
#include "gmt_dimensions.h" /* Constant definitions created by configure */
Expand Down
1 change: 1 addition & 0 deletions src/gmt_hidden.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ struct GMT_GRID_HEADER_HIDDEN {
char name[GMT_GRID_NAME_LEN256]; /* Actual name of the file after any ?<varname> and =<stuff> has been removed */
char varname[GMT_GRID_VARNAME_LEN80];/* NetCDF: variable name */
char *title; /* Title string not limited to GMT_GRID_TITLE_LEN80 characters */
char cpt[GMT_LEN64]; /* Name of default CPT for a remote grid subset written via grdcut */
char *command; /* Command/history string not limited to GMT_GRID_TITLE_LEN80 characters */
char *remark; /* Remark/description string not limited to GMT_GRID_REMARK_LEN160 characters */
int row_order; /* NetCDF: k_nc_start_south if S->N, k_nc_start_north if N->S */
Expand Down
14 changes: 11 additions & 3 deletions src/gmt_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -4752,9 +4752,17 @@ int gmtlib_nc_get_att_vtext (struct GMT_CTRL *GMT, int ncid, int varid, char *na
size_t attlen, trunclen;
char *att = NULL;

if (name == NULL) {
GMT_Report (GMT->parent, GMT_MSG_ERROR, "Attribute name passed to gmtlib_nc_get_att_vtext is NULL\n");
return GMT_RUNTIME_ERROR;
}
if (text == NULL) {
GMT_Report (GMT->parent, GMT_MSG_ERROR, "Attribute pointer passed to gmtlib_nc_get_att_vtext is NULL\n");
return GMT_RUNTIME_ERROR;
}
status = nc_inq_attlen (ncid, varid, name, &attlen);
if (status != NC_NOERR) { /* No such attribute */
*text = '\0';
text[0] = '\0';
return status;
}
att = calloc (attlen+1, sizeof (char)); /* Allocate the memory for the full string plus text terminator */
Expand Down Expand Up @@ -4782,8 +4790,8 @@ int gmtlib_nc_get_att_vtext (struct GMT_CTRL *GMT, int ncid, int varid, char *na
strncpy (text, att, trunclen); /* Copy att to text */
text[trunclen] = '\0'; /* Terminate string */
}
else /* Not successful, set ouput string to empty */
*text = '\0';
else /* Not successful, set output string to empty */
text[0] = '\0';
if (wipe) gmt_M_str_free (att); /* Free since not placed in hidden structure */
return status;
}
Expand Down
5 changes: 5 additions & 0 deletions src/gmt_nc.c
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,9 @@ GMT_LOCAL int gmtnc_grd_info (struct GMT_CTRL *GMT, struct GMT_GRID_HEADER *head
/* Valid range is already in packed units, so do not convert */
header->z_min = dummy[0], header->z_max = dummy[1];
}
if (gmtlib_nc_get_att_text (GMT, ncid, z_id, "cpt", HH->cpt, GMT_LEN64)) /* Found cpt attribute */
GMT_Report (GMT->parent, GMT_MSG_INFORMATION, "netCDF grid %s has a default CPT %s.\n", HH->cpt);

if (gmt_M_is_dnan (header->z_min) && gmt_M_is_dnan (header->z_max)) {
GMT_Report (GMT->parent, GMT_MSG_INFORMATION, "netCDF grid %s information has zmin = zmax = NaN. Reset to 0/0.\n", HH->name);
header->z_min = header->z_max = 0.0;
Expand Down Expand Up @@ -1029,6 +1032,8 @@ GMT_LOCAL int gmtnc_grd_info (struct GMT_CTRL *GMT, struct GMT_GRID_HEADER *head

/* Define z variable. Attempt to remove "scale_factor" or "add_offset" when no longer needed */
gmtnc_put_units (ncid, z_id, header->z_units);
if (GMT->parent->remote_info && GMT->parent->remote_id != GMT_NOTSET && GMT->parent->remote_info[GMT->parent->remote_id].CPT[0] != '-') /* Subset of remote grid with default CPT, save name as an attribute */
gmt_M_err_trap (nc_put_att_text (ncid, z_id, "cpt", strlen (GMT->parent->remote_info[GMT->parent->remote_id].CPT), GMT->parent->remote_info[GMT->parent->remote_id].CPT));

if (header->z_scale_factor != 1.0) {
gmt_M_err_trap (nc_put_att_double (ncid, z_id, "scale_factor", NC_DOUBLE, 1U, &header->z_scale_factor));
Expand Down
1 change: 1 addition & 0 deletions src/gmt_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ struct GMTAPI_CTRL {
char *O_OPT, *K_OPT, *P_OPT, *c_OPT;
/* structure array of remote file information (sorted alphabetically) */
int n_remote_info; /* How many remote server files we know of */
int remote_id; /* Currently used remote ID or -1 */
struct GMT_DATA_INFO *remote_info;
bool server_announced; /* Set to true after we have announced which GMT data server we are using */
struct GMT_COMMON *common_snapshot; /* Holds the latest GMT common option settings after a module completes. */
Expand Down
2 changes: 1 addition & 1 deletion src/gmt_prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ EXTERN_MSC int gmt_find_macro (char *arg, unsigned int n_macros, struct GMT_MATH
EXTERN_MSC int gmt_load_macros (struct GMT_CTRL *GMT, char *mtype, struct GMT_MATH_MACRO **M);
EXTERN_MSC struct GMT_OPTION * gmt_substitute_macros (struct GMT_CTRL *GMT, struct GMT_OPTION *options, char *mfile);
EXTERN_MSC void gmt_free_macros (struct GMT_CTRL *GMT, unsigned int n_macros, struct GMT_MATH_MACRO **M);
EXTERN_MSC char * gmt_cpt_default (struct GMTAPI_CTRL *API, char *cpt, char *file);
EXTERN_MSC char * gmt_cpt_default (struct GMTAPI_CTRL *API, char *cpt, char *file, struct GMT_GRID_HEADER *h);
EXTERN_MSC void gmt_sort_array (struct GMT_CTRL *GMT, void *base, uint64_t n, unsigned int type);
EXTERN_MSC bool gmt_polygon_is_open (struct GMT_CTRL *GMT, double x[], double y[], uint64_t n);
EXTERN_MSC int gmt_polygon_centroid (struct GMT_CTRL *GMT, double *x, double *y, uint64_t n, double *Cx, double *Cy);
Expand Down
2 changes: 2 additions & 0 deletions src/gmt_remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ void gmt_set_unspecified_remote_registration (struct GMTAPI_CTRL *API, char **fi
ext = gmt_chop_ext (infile);
/* If the remote file is found then there is nothing to do */
if ((k_data = gmt_remote_dataset_id (API, infile)) == GMT_NOTSET) goto clean_up;
API->remote_id = k_data;
L = strlen (API->remote_info[k_data].dir) - 1; /* Length of dir minus trailing slash */
strncpy (dir, API->remote_info[k_data].dir, L); dir[L] = '\0'; /* Duplicate dir without slash */
p = strrchr (dir, '/') + 1; /* Start of final subdirectory (skipping over the slash we found) */
Expand All @@ -498,6 +499,7 @@ void gmt_set_unspecified_remote_registration (struct GMTAPI_CTRL *API, char **fi
}
gmt_M_str_free (*file_ptr);
*file_ptr = strdup (newfile);
API->remote_id = k_data;
goto clean_up;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/gmt_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -8337,7 +8337,7 @@ struct GMT_PALETTE * gmtlib_read_cpt (struct GMT_CTRL *GMT, void *source, unsign
return (X);
}

char * gmt_cpt_default (struct GMTAPI_CTRL *API, char *cpt, char *file) {
char * gmt_cpt_default (struct GMTAPI_CTRL *API, char *cpt, char *file, struct GMT_GRID_HEADER *h) {
/* Return which type of default CPT this data set should use.
* If cpt is specified then that is what we will use. If not, then
* we determine if file is a remote data set, and if it is and has a
Expand All @@ -8346,9 +8346,11 @@ char * gmt_cpt_default (struct GMTAPI_CTRL *API, char *cpt, char *file) {
int k_data;
static char *srtm_cpt = "srtm";
char *curr_cpt = NULL;
struct GMT_GRID_HEADER_HIDDEN *HH = gmt_get_H_hidden (h);

if (cpt) return strdup (cpt); /* CPT was already specified */
if (file == NULL) return NULL; /* No file given, so there */
if (HH->cpt[0] && HH->cpt[0] != '-') return (strdup (HH->cpt)); /* Found it in the grid header */
if (API->GMT->current.setting.run_mode == GMT_MODERN && (curr_cpt = gmt_get_current_item (API->GMT, "cpt", false))) return curr_cpt; /* Use current CPT */

if ((k_data = gmt_remote_dataset_id (API, file)) == GMT_NOTSET) {
Expand Down
2 changes: 1 addition & 1 deletion src/grd2kml.c
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ EXTERN_MSC int GMT_grd2kml (void *V_API, int mode, void *args) {
}

if (!Ctrl->C.active || gmt_is_cpt_master (GMT, Ctrl->C.file)) { /* If no cpt given or just a master then we must compute a scaled one from the full-size grid and use it throughout */
char *cpt = gmt_cpt_default (API, Ctrl->C.file, Ctrl->In.file);
char *cpt = gmt_cpt_default (API, Ctrl->C.file, Ctrl->In.file, G->header);
char cptfile[PATH_MAX] = {""};
struct GMT_PALETTE *P = NULL;
if ((P = gmt_get_palette (GMT, cpt, GMT_CPT_OPTIONAL, G->header->z_min, G->header->z_max, Ctrl->C.dz)) == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion src/grdimage.c
Original file line number Diff line number Diff line change
Expand Up @@ -1581,7 +1581,7 @@ EXTERN_MSC int GMT_grdimage (void *V_API, int mode, void *args) {
has_content = (got_z_grid) ? false : true; /* Images always have content but grids may be all NaN */
if (got_z_grid) { /* Got a single grid so need to convert z to color via a CPT */
if (Ctrl->C.active) { /* Read a palette file */
char *cpt = gmt_cpt_default (API, Ctrl->C.file, Ctrl->In.file);
char *cpt = gmt_cpt_default (API, Ctrl->C.file, Ctrl->In.file, Grid_orig->header);
if ((P = gmt_get_palette (GMT, cpt, GMT_CPT_OPTIONAL, Grid_orig->header->z_min, Grid_orig->header->z_max, Ctrl->C.dz)) == NULL) {
GMT_Report (API, GMT_MSG_ERROR, "Failed to read CPT %s.\n", Ctrl->C.file);
gmt_free_header (API->GMT, &header_G);
Expand Down
2 changes: 1 addition & 1 deletion src/grdview.c
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ EXTERN_MSC int GMT_grdview (void *V_API, int mode, void *args) {

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);
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);
}
Expand Down

0 comments on commit 980792a

Please sign in to comment.