Skip to content

Commit 980792a

Browse files
committed
Save any default CPT in hidden grid header
This addresses #6167 when we cut a subset from, sa, earth_relief_10m and then no longer know what the CPT should be.
1 parent d8ebeef commit 980792a

11 files changed

+28
-9
lines changed

src/gmt_dev.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ extern "C" {
129129
#include "gmt_common_math.h" /* Shared math functions */
130130
#include "gmt.h" /* All GMT high-level API */
131131
#include "gmt_private.h" /* API declaration needed by libraries */
132+
#include "gmt_constants.h" /* All basic constant definitions */
132133
#include "gmt_hidden.h" /* Hidden bookkeeping structure for API containers */
133134

134135
struct GMT_CTRL; /* forward declaration of GMT_CTRL */
135136

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

138-
#include "gmt_constants.h" /* All basic constant definitions */
139139
#include "gmt_modern.h" /* Modern mode constant definitions */
140140
#include "gmt_macros.h" /* All basic macros definitions */
141141
#include "gmt_dimensions.h" /* Constant definitions created by configure */

src/gmt_hidden.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ struct GMT_GRID_HEADER_HIDDEN {
159159
char name[GMT_GRID_NAME_LEN256]; /* Actual name of the file after any ?<varname> and =<stuff> has been removed */
160160
char varname[GMT_GRID_VARNAME_LEN80];/* NetCDF: variable name */
161161
char *title; /* Title string not limited to GMT_GRID_TITLE_LEN80 characters */
162+
char cpt[GMT_LEN64]; /* Name of default CPT for a remote grid subset written via grdcut */
162163
char *command; /* Command/history string not limited to GMT_GRID_TITLE_LEN80 characters */
163164
char *remark; /* Remark/description string not limited to GMT_GRID_REMARK_LEN160 characters */
164165
int row_order; /* NetCDF: k_nc_start_south if S->N, k_nc_start_north if N->S */

src/gmt_io.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4752,9 +4752,17 @@ int gmtlib_nc_get_att_vtext (struct GMT_CTRL *GMT, int ncid, int varid, char *na
47524752
size_t attlen, trunclen;
47534753
char *att = NULL;
47544754

4755+
if (name == NULL) {
4756+
GMT_Report (GMT->parent, GMT_MSG_ERROR, "Attribute name passed to gmtlib_nc_get_att_vtext is NULL\n");
4757+
return GMT_RUNTIME_ERROR;
4758+
}
4759+
if (text == NULL) {
4760+
GMT_Report (GMT->parent, GMT_MSG_ERROR, "Attribute pointer passed to gmtlib_nc_get_att_vtext is NULL\n");
4761+
return GMT_RUNTIME_ERROR;
4762+
}
47554763
status = nc_inq_attlen (ncid, varid, name, &attlen);
47564764
if (status != NC_NOERR) { /* No such attribute */
4757-
*text = '\0';
4765+
text[0] = '\0';
47584766
return status;
47594767
}
47604768
att = calloc (attlen+1, sizeof (char)); /* Allocate the memory for the full string plus text terminator */
@@ -4782,8 +4790,8 @@ int gmtlib_nc_get_att_vtext (struct GMT_CTRL *GMT, int ncid, int varid, char *na
47824790
strncpy (text, att, trunclen); /* Copy att to text */
47834791
text[trunclen] = '\0'; /* Terminate string */
47844792
}
4785-
else /* Not successful, set ouput string to empty */
4786-
*text = '\0';
4793+
else /* Not successful, set output string to empty */
4794+
text[0] = '\0';
47874795
if (wipe) gmt_M_str_free (att); /* Free since not placed in hidden structure */
47884796
return status;
47894797
}

src/gmt_nc.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,9 @@ GMT_LOCAL int gmtnc_grd_info (struct GMT_CTRL *GMT, struct GMT_GRID_HEADER *head
896896
/* Valid range is already in packed units, so do not convert */
897897
header->z_min = dummy[0], header->z_max = dummy[1];
898898
}
899+
if (gmtlib_nc_get_att_text (GMT, ncid, z_id, "cpt", HH->cpt, GMT_LEN64)) /* Found cpt attribute */
900+
GMT_Report (GMT->parent, GMT_MSG_INFORMATION, "netCDF grid %s has a default CPT %s.\n", HH->cpt);
901+
899902
if (gmt_M_is_dnan (header->z_min) && gmt_M_is_dnan (header->z_max)) {
900903
GMT_Report (GMT->parent, GMT_MSG_INFORMATION, "netCDF grid %s information has zmin = zmax = NaN. Reset to 0/0.\n", HH->name);
901904
header->z_min = header->z_max = 0.0;
@@ -1029,6 +1032,8 @@ GMT_LOCAL int gmtnc_grd_info (struct GMT_CTRL *GMT, struct GMT_GRID_HEADER *head
10291032

10301033
/* Define z variable. Attempt to remove "scale_factor" or "add_offset" when no longer needed */
10311034
gmtnc_put_units (ncid, z_id, header->z_units);
1035+
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 */
1036+
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));
10321037

10331038
if (header->z_scale_factor != 1.0) {
10341039
gmt_M_err_trap (nc_put_att_double (ncid, z_id, "scale_factor", NC_DOUBLE, 1U, &header->z_scale_factor));

src/gmt_private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ struct GMTAPI_CTRL {
209209
char *O_OPT, *K_OPT, *P_OPT, *c_OPT;
210210
/* structure array of remote file information (sorted alphabetically) */
211211
int n_remote_info; /* How many remote server files we know of */
212+
int remote_id; /* Currently used remote ID or -1 */
212213
struct GMT_DATA_INFO *remote_info;
213214
bool server_announced; /* Set to true after we have announced which GMT data server we are using */
214215
struct GMT_COMMON *common_snapshot; /* Holds the latest GMT common option settings after a module completes. */

src/gmt_prototypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ EXTERN_MSC int gmt_find_macro (char *arg, unsigned int n_macros, struct GMT_MATH
659659
EXTERN_MSC int gmt_load_macros (struct GMT_CTRL *GMT, char *mtype, struct GMT_MATH_MACRO **M);
660660
EXTERN_MSC struct GMT_OPTION * gmt_substitute_macros (struct GMT_CTRL *GMT, struct GMT_OPTION *options, char *mfile);
661661
EXTERN_MSC void gmt_free_macros (struct GMT_CTRL *GMT, unsigned int n_macros, struct GMT_MATH_MACRO **M);
662-
EXTERN_MSC char * gmt_cpt_default (struct GMTAPI_CTRL *API, char *cpt, char *file);
662+
EXTERN_MSC char * gmt_cpt_default (struct GMTAPI_CTRL *API, char *cpt, char *file, struct GMT_GRID_HEADER *h);
663663
EXTERN_MSC void gmt_sort_array (struct GMT_CTRL *GMT, void *base, uint64_t n, unsigned int type);
664664
EXTERN_MSC bool gmt_polygon_is_open (struct GMT_CTRL *GMT, double x[], double y[], uint64_t n);
665665
EXTERN_MSC int gmt_polygon_centroid (struct GMT_CTRL *GMT, double *x, double *y, uint64_t n, double *Cx, double *Cy);

src/gmt_remote.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ void gmt_set_unspecified_remote_registration (struct GMTAPI_CTRL *API, char **fi
477477
ext = gmt_chop_ext (infile);
478478
/* If the remote file is found then there is nothing to do */
479479
if ((k_data = gmt_remote_dataset_id (API, infile)) == GMT_NOTSET) goto clean_up;
480+
API->remote_id = k_data;
480481
L = strlen (API->remote_info[k_data].dir) - 1; /* Length of dir minus trailing slash */
481482
strncpy (dir, API->remote_info[k_data].dir, L); dir[L] = '\0'; /* Duplicate dir without slash */
482483
p = strrchr (dir, '/') + 1; /* Start of final subdirectory (skipping over the slash we found) */
@@ -498,6 +499,7 @@ void gmt_set_unspecified_remote_registration (struct GMTAPI_CTRL *API, char **fi
498499
}
499500
gmt_M_str_free (*file_ptr);
500501
*file_ptr = strdup (newfile);
502+
API->remote_id = k_data;
501503
goto clean_up;
502504
}
503505
}

src/gmt_support.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8337,7 +8337,7 @@ struct GMT_PALETTE * gmtlib_read_cpt (struct GMT_CTRL *GMT, void *source, unsign
83378337
return (X);
83388338
}
83398339

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

83508351
if (cpt) return strdup (cpt); /* CPT was already specified */
83518352
if (file == NULL) return NULL; /* No file given, so there */
8353+
if (HH->cpt[0] && HH->cpt[0] != '-') return (strdup (HH->cpt)); /* Found it in the grid header */
83528354
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 */
83538355

83548356
if ((k_data = gmt_remote_dataset_id (API, file)) == GMT_NOTSET) {

src/grd2kml.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ EXTERN_MSC int GMT_grd2kml (void *V_API, int mode, void *args) {
695695
}
696696

697697
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 */
698-
char *cpt = gmt_cpt_default (API, Ctrl->C.file, Ctrl->In.file);
698+
char *cpt = gmt_cpt_default (API, Ctrl->C.file, Ctrl->In.file, G->header);
699699
char cptfile[PATH_MAX] = {""};
700700
struct GMT_PALETTE *P = NULL;
701701
if ((P = gmt_get_palette (GMT, cpt, GMT_CPT_OPTIONAL, G->header->z_min, G->header->z_max, Ctrl->C.dz)) == NULL) {

src/grdimage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,7 @@ EXTERN_MSC int GMT_grdimage (void *V_API, int mode, void *args) {
15811581
has_content = (got_z_grid) ? false : true; /* Images always have content but grids may be all NaN */
15821582
if (got_z_grid) { /* Got a single grid so need to convert z to color via a CPT */
15831583
if (Ctrl->C.active) { /* Read a palette file */
1584-
char *cpt = gmt_cpt_default (API, Ctrl->C.file, Ctrl->In.file);
1584+
char *cpt = gmt_cpt_default (API, Ctrl->C.file, Ctrl->In.file, Grid_orig->header);
15851585
if ((P = gmt_get_palette (GMT, cpt, GMT_CPT_OPTIONAL, Grid_orig->header->z_min, Grid_orig->header->z_max, Ctrl->C.dz)) == NULL) {
15861586
GMT_Report (API, GMT_MSG_ERROR, "Failed to read CPT %s.\n", Ctrl->C.file);
15871587
gmt_free_header (API->GMT, &header_G);

src/grdview.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ EXTERN_MSC int GMT_grdview (void *V_API, int mode, void *args) {
979979

980980
if (Ctrl->C.active) {
981981
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;
982-
char *cpt = gmt_cpt_default (API, Ctrl->C.file, dataset_cpt);
982+
char *cpt = gmt_cpt_default (API, Ctrl->C.file, dataset_cpt, Topo->header);
983983
if ((P = gmt_get_palette (GMT, cpt, GMT_CPT_OPTIONAL, Topo->header->z_min, Topo->header->z_max, Ctrl->C.dz)) == NULL) {
984984
Return (API->error);
985985
}

0 commit comments

Comments
 (0)