Skip to content

Commit d516e67

Browse files
authored
This PR fixes #8756 that showed both Windows specific and a general errors in grdblend (#8853)
See the issue comments for a description of what was done. Fix #7012
1 parent 1412db0 commit d516e67

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

src/gmt_io.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5699,7 +5699,7 @@ char *gmt_get_filename (struct GMTAPI_CTRL *API, const char* filename, const cha
56995699
/* Need to strip off any valid, trailing modifiers and netCDF specifications that may be part of filename */
57005700
char file[PATH_MAX] = {""}, *c = NULL, *clean_file = NULL;
57015701

5702-
if (strstr (filename, "/=tiled_")) /* Special list with remote tiles, use exactly as is */
5702+
if (strstr(filename, "/=tiled_") || strstr(filename, "\\=tiled_")) /* Special list with remote tiles, use exactly as is */
57035703
strncpy (file, filename, PATH_MAX-1);
57045704
else /* Strip off netCDF3-D grid extensions to make sure we get a valid file name */
57055705
sscanf (filename, "%[^=?]", file);

src/grdblend.c

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ GMT_LOCAL int grdblend_init_blend_job(struct GMT_CTRL *GMT, char **files, unsign
216216
struct GMT_GRID_HEADER_HIDDEN *HH = NULL;
217217
struct GMT_GRID_HEADER_HIDDEN *HHG = NULL;
218218

219-
char *sense[2] = {"normal", "inverse"}, buffer[GMT_BUFSIZ] = {""}, res[4] = {""};
219+
char *sense[2] = {"normal", "inverse"}, buffer[PATH_MAX] = {""}, res[4] = {""};
220220
static char *V_level = GMT_VERBOSE_CODES;
221221
char Iargs[GMT_LEN256] = {""}, Rargs[GMT_LEN256] = {""}, cmd[GMT_LEN256] = {""};
222222
double wesn[4], sub = 0.0;
@@ -439,15 +439,23 @@ GMT_LOCAL int grdblend_init_blend_job(struct GMT_CTRL *GMT, char **files, unsign
439439
/* The goal below is to come up with a wesn that a proper subset of the output grid while still not
440440
* exceeding the bounds of the current tile grid. The wesn below will be compatible with the output grid
441441
* but is not compatible with the input grid. grdsample will make the adjustment */
442+
443+
/* Tinny roundoff differences between wesn and t->wesn could cause jumps of one grid step and being
444+
responsible for what's reported in the forum topic (issue #8576)
445+
https://forum.generic-mapping-tools.org/t/lines-appear-when-the-resolution-is-increased/6011/3
446+
For percaution we limit the adjustment to be within half a grid increment and apply only x|y_min.
447+
*/
442448
k = (unsigned int)rint ((MAX (h->wesn[XLO], t->wesn[XLO]) - h->wesn[XLO]) / h->inc[GMT_X] - h->xy_off);
443449
wesn[XLO] = h->wesn[XLO] + k * h->inc[GMT_X];
444-
while (wesn[XLO] < t->wesn[XLO]) wesn[XLO] += t->inc[GMT_X]; /* Make sure we are not outside this grid */
450+
//while (wesn[XLO] < t->wesn[XLO]) wesn[XLO] += t->inc[GMT_X]; /* Make sure we are not outside this grid */
451+
while ((wesn[XLO] - t->wesn[XLO]) < -t->inc[GMT_X] / 2) wesn[XLO] += t->inc[GMT_X];
445452
k = (unsigned int)rint ((MIN (h->wesn[XHI], t->wesn[XHI]) - h->wesn[XLO]) / h->inc[GMT_X] - h->xy_off);
446453
wesn[XHI] = h->wesn[XLO] + k * h->inc[GMT_X];
447454
while (wesn[XHI] > t->wesn[XHI]) wesn[XHI] -= t->inc[GMT_X]; /* Make sure we are not outside this grid */
448455
k = (unsigned int)rint ((MAX (h->wesn[YLO], t->wesn[YLO]) - h->wesn[YLO]) / h->inc[GMT_Y] - h->xy_off);
449456
wesn[YLO] = h->wesn[YLO] + k * h->inc[GMT_Y];
450-
while (wesn[YLO] < t->wesn[YLO]) wesn[YLO] += t->inc[GMT_Y]; /* Make sure we are not outside this grid */
457+
//while (wesn[YLO] < t->wesn[YLO]) wesn[YLO] += t->inc[GMT_Y]; /* Make sure we are not outside this grid */
458+
while ((wesn[YLO] - t->wesn[YLO]) < -t->inc[GMT_Y] / 2) wesn[YLO] += t->inc[GMT_Y]; /* Make sure we are not outside this grid */
451459
k = (unsigned int)rint ((MIN (h->wesn[YHI], t->wesn[YHI]) - h->wesn[YLO]) / h->inc[GMT_Y] - h->xy_off);
452460
wesn[YHI] = h->wesn[YLO] + k * h->inc[GMT_Y];
453461
while (wesn[YHI] > t->wesn[YHI]) wesn[YHI] -= t->inc[GMT_Y]; /* Make sure we are not outside this grid */
@@ -469,8 +477,13 @@ GMT_LOCAL int grdblend_init_blend_job(struct GMT_CTRL *GMT, char **files, unsign
469477
gmt_filename_set (B[n].file); /* Replace any spaces in filename with ASCII 29 */
470478
GMT->common.V.active = false; /* Since we will parse again below */
471479
if (do_sample & 1) { /* Resampling of the grid into a netcdf grid */
472-
473-
if (gmt_get_tempname (GMT->parent, "grdblend_resampled", ".nc", buffer))
480+
char template[GMT_LEN32] = {""};
481+
/* We need different template name be cause MSVC is shamelessly ignoring what it states in the _mktemp_s man page
482+
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/mktemp-s-wmktemp-s?view=msvc-170*
483+
and returns the same temp name over and over again causing that the sampled grids overwrite each other
484+
*/
485+
sprintf(template, "grdblend_resampled_%d", n);
486+
if (gmt_get_tempname (GMT->parent, template, ".nc", buffer))
474487
return GMT_RUNTIME_ERROR;
475488
snprintf (cmd, GMT_LEN256, "%s %s %s %s -G%s -V%c", B[n].file, res,
476489
Iargs, Rargs, buffer, V_level[GMT->current.setting.verbose]);
@@ -1038,12 +1051,14 @@ EXTERN_MSC int GMT_grdblend (void *V_API, int mode, void *args) {
10381051
/* Last case BLEND_LAST is always true in that we always update z[col] */
10391052
}
10401053
switch (Ctrl->C.sign) { /* Check if sign of input grid should be considered in decision */
1041-
case -1: if (first_grid) {z[col] = blend[k].z[kk]; first_grid = false; continue; break;} /* Must initialize with first grid in case nothing passes */
1042-
else if (blend[k].z[kk] > 0.0) continue; /* Only pick grids value if negative or zero */
1043-
break;
1044-
case +1: if (first_grid) { z[col] = blend[k].z[kk]; first_grid = false; continue; break;} /* Must initialize with first grid in case nothing passes */
1045-
else if (blend[k].z[kk] < 0.0) continue; /* Only pick grids value if positive or zero */
1046-
break;
1054+
case -1:
1055+
if (first_grid) {z[col] = blend[k].z[kk]; first_grid = false; continue; break;} /* Must initialize with first grid in case nothing passes */
1056+
else if (blend[k].z[kk] > 0.0) continue; /* Only pick grids value if negative or zero */
1057+
break;
1058+
case +1:
1059+
if (first_grid) { z[col] = blend[k].z[kk]; first_grid = false; continue; break;} /* Must initialize with first grid in case nothing passes */
1060+
else if (blend[k].z[kk] < 0.0) continue; /* Only pick grids value if positive or zero */
1061+
break;
10471062
default: break; /* Always use the grid value */
10481063

10491064
}

src/grdcut.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,8 @@ EXTERN_MSC int GMT_grdcut (void *V_API, int mode, void *args) {
964964
if (GMT_Read_Data (API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, GMT_DATA_ONLY | add_mode, wesn_new, Ctrl->In.file, G) == NULL) { /* Get subset (unless memory file) */
965965
Return (API->error);
966966
}
967+
if (strstr(Ctrl->In.file, "/=tiled_") || strstr(Ctrl->In.file, "\\=tiled_")) /* Special list with remote tiles, not used anymore */
968+
gmt_remove_file(GMT, Ctrl->In.file);
967969
}
968970
else if (!do_via_gdal) {
969971
if (GMT_Read_Data (API, GMT_IS_IMAGE, GMT_IS_FILE, GMT_IS_SURFACE, GMT_DATA_ONLY, wesn_new, Ctrl->In.file, I) == NULL) { /* Get subset */

0 commit comments

Comments
 (0)