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

Enable writing of 3-D netCDF data cubes from greenspline and grdinterpolate #4581

Merged
merged 53 commits into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
ec176c3
Let gmtapi_export_cube call gmt_write_nc_cube
PaulWessel Dec 14, 2020
8d077dd
Properly name the cube functions
PaulWessel Dec 14, 2020
57f218f
add cube arg to gmtnc_grd_info so we can deal with 3-D vars
PaulWessel Dec 14, 2020
53cc3f3
Add netcdf var ID for 3-D time/depth variable
PaulWessel Dec 14, 2020
a1e7b97
Fix wrong number of output levels in grdinterpolate
PaulWessel Dec 14, 2020
66b6d8d
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 14, 2020
302326f
Set cube data type
PaulWessel Dec 14, 2020
4e4bf9b
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 14, 2020
b478f51
Getting close
PaulWessel Dec 15, 2020
98ba949
Update greenspline to write cube command
PaulWessel Dec 15, 2020
99a0fd2
Remember netcdf IDs after reading
PaulWessel Dec 16, 2020
da022e3
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 16, 2020
c052fdc
Got a working solution but z listed before x,y in dimensions list
PaulWessel Dec 16, 2020
79a15bb
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 16, 2020
00051d9
Use UNLIMITED dimension for layers
PaulWessel Dec 16, 2020
0b0195c
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 16, 2020
3fc371b
Fix KEY for greenspline to be U if cube output
PaulWessel Dec 16, 2020
461433f
Remove initial STACK option to read grids directly into cube
PaulWessel Dec 17, 2020
9c58c4c
Document the reformatting of stacked grids to cube
PaulWessel Dec 17, 2020
ff70b62
Update grdinterpolate.c
PaulWessel Dec 17, 2020
01aa600
Update gspline_4.sh
PaulWessel Dec 17, 2020
73a5006
Improve the docs
PaulWessel Dec 17, 2020
95c8b0c
var changes
PaulWessel Dec 17, 2020
cfc8b5f
Fix nc parameter IDs
PaulWessel Dec 17, 2020
f7b44ab
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 17, 2020
9eb4eba
Add gmt_nc_is_cube and fix geometry mistakes
PaulWessel Dec 17, 2020
2e3f491
Add gmt_nc_is_cube function
PaulWessel Dec 17, 2020
dd0f842
Let grdinfo -Q handle cubes
PaulWessel Dec 18, 2020
0a1a981
Fix z subset glitch
PaulWessel Dec 18, 2020
a603220
Update gmt_api.c
PaulWessel Dec 18, 2020
0a16f7d
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 18, 2020
458b81d
Fix reading cubes with selection of variable
PaulWessel Dec 18, 2020
0245547
fix KEYS for grdinfo
PaulWessel Dec 18, 2020
eb62a18
Towards resolving data type info
PaulWessel Dec 18, 2020
1189c63
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 18, 2020
ffdbd11
Fix unit issues for cubes
PaulWessel Dec 19, 2020
a642175
fix chunking array
PaulWessel Dec 19, 2020
9c2e5ea
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 19, 2020
9fe2389
Add tests for 3-D cube writing
PaulWessel Dec 19, 2020
a3cada7
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 19, 2020
7474bae
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 20, 2020
2eb08f8
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 21, 2020
ab4eba0
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 23, 2020
b1bf1b8
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 23, 2020
278f78e
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 24, 2020
26f08ae
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 25, 2020
a9ee450
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 26, 2020
273dd52
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 26, 2020
262ce08
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 27, 2020
55dac40
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 27, 2020
0a37889
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 27, 2020
36c2a82
Merge branch 'master' into nc-3D-writing
PaulWessel Dec 28, 2020
113ff03
Update -D to include z-dimension, optionally set var name
PaulWessel Dec 29, 2020
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
62 changes: 19 additions & 43 deletions src/gmt_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1997,6 +1997,11 @@ GMT_LOCAL int gmtapi_init_grdheader (struct GMT_CTRL *GMT, unsigned int directio
gmtlib_grd_get_units (GMT, header);
gmt_BC_init (GMT, header); /* Initialize grid interpolation and boundary condition parameters */
HH->grdtype = gmtlib_get_grdtype (GMT, direction, header); /* Set grid type (i.e. periodicity for global grids) */
#ifdef DOUBLE_PRECISION_GRID
header->type = GMT_GRID_IS_ND;
#else
header->type = GMT_GRID_IS_NF;
#endif
return (GMT_NOERROR);
}

Expand Down Expand Up @@ -5372,7 +5377,7 @@ GMT_LOCAL struct GMT_CUBE * gmtapi_import_cube (struct GMTAPI_CTRL *API, int obj
unsigned int both_set = (GMT_CONTAINER_ONLY | GMT_DATA_ONLY);
unsigned int method, start_over_method = 0;
double dx, dy, d;
double *level = NULL, z_min, z_max;
double *level = NULL, z_min, z_max, w_range[2] = {0.0, 0.0};
p_func_uint64_t GMT_2D_to_index = NULL;
struct GMT_CUBE *U_obj = NULL, *U_orig = NULL;
struct GMT_GRID *G = NULL;
Expand Down Expand Up @@ -5435,7 +5440,7 @@ GMT_LOCAL struct GMT_CUBE * gmtapi_import_cube (struct GMTAPI_CTRL *API, int obj
strcpy (cube_layer, &nc_z_named[1]); /* Place variable name in cube_layer string */
nc_z_named[0] = '\0'; /* Chop off layer name for now */
}
if (gmt_examine_nc_cube (GMT, the_file, &n_layers, &level)) { /* Learn the basics about the cube */
if (gmt_nc_read_cube_info (GMT, the_file, w_range, &n_layers, &level)) { /* Learn the basics about the cube */
GMT_Report (API, GMT_MSG_ERROR, "gmtapi_import_cube: Unable to examine cube %s.\n", the_file);
return_null (API, GMT_RUNTIME_ERROR);
}
Expand Down Expand Up @@ -5880,14 +5885,13 @@ GMT_LOCAL int gmtapi_export_cube (struct GMTAPI_CTRL *API, int object_ID, unsign
bool done = true;
unsigned int method;
uint64_t row, col, i0, i1, j0, j1, k0, k1, ij, ijp, ij_orig;
uint64_t k, n_layers_used, here = 0, save_n_bands;
uint64_t k, here = 0;
size_t size;
double dx, dy;
p_func_uint64_t GMT_2D_to_index = NULL;
GMT_putfunction api_put_val = NULL;
struct GMTAPI_DATA_OBJECT *S_obj = NULL;
struct GMT_CUBE *U_copy = NULL;
struct GMT_GRID *G = NULL;
struct GMT_MATRIX *M_obj = NULL;
struct GMT_MATRIX_HIDDEN *MH = NULL;
struct GMT_CUBE_HIDDEN *UH = gmt_get_U_hidden (U_obj), *UH2 = NULL;
Expand All @@ -5911,52 +5915,18 @@ GMT_LOCAL int gmtapi_export_cube (struct GMTAPI_CTRL *API, int object_ID, unsign
}
if (mode & GMT_GRID_IS_GEO) gmt_set_geographic (GMT, GMT_OUT); /* From API to tell cube is geographic */
gmtlib_grd_set_units (GMT, U_obj->header); /* Ensure unit strings are set, regardless of destination */

method = gmtapi_set_method (S_obj); /* Get the actual method to use since may be MATRIX or VECTOR masquerading as GRID */
switch (method) {
case GMT_IS_FILE: /* Name of a cube file on disk */
case GMT_IS_FILE: /* Name of a cube file to write to disk */
if (mode & GMT_CONTAINER_ONLY) { /* Update header structure only */
GMT_Report (API, GMT_MSG_INFORMATION, "Updating cube header for file %s not implemented\n", S_obj->filename);
return (gmtlib_report_error (API, GMT_RUNTIME_ERROR));
}
else {
GMT_Report (API, GMT_MSG_INFORMATION, "Writing cube to file %s\n", S_obj->filename);
/* Determine which layers we want to write */
k0 = 0; k1 = U_obj->header->n_bands - 1;
if (S_obj->region && S_obj->wesn[ZHI] > S_obj->wesn[ZLO]) { /* Want to write a subset of layers */
if (gmt_get_active_layers (GMT, U_obj, &(S_obj->wesn[ZLO]), &k0, &k1) == 0) {
gmtlib_report_error (API, GMT_RUNTIME_ERROR);
}
}
n_layers_used = k1 - k0 + 1; /* Total number of layers actually to be read */
if (n_layers_used == 0) {
GMT_Report (API, GMT_MSG_ERROR, "gmtapi_export_cube: No layers selected from GMT_IS_CUBE.\n");
return (gmtlib_report_error (API, GMT_DIM_TOO_SMALL));
}

/* !! Remember that the gmt_nc.c codes will unpad and mess up C->data */
G = gmt_get_grid (GMT); /* Need a dummy grid for writing */
save_n_bands = U_obj->header->n_bands; /* Remember how many bands */
G->header = U_obj->header; /* Use this pointer for now */
G->header->n_bands = 1; /* Grids only have one band */
here = k0 * U_obj->header->size; /* Start position in the cube for layer k0 */
/* Write the layers via a grid */
for (k = k0; k <= k1; k++) { /* For all selected output levels */
if (n_layers_used > 1) /* Create the k'th layer file */
sprintf (file, S_obj->filename, U_obj->z[k]);
else /* Just this one layer grid */
sprintf (file, "%s", S_obj->filename);
G->data = &U_obj->data[here]; /* Point to start of this layer */
GMT_Report (API, GMT_MSG_DEBUG, "gmtapi_export_cube: Layer %" PRIu64 ", offset = %" PRIu64 ".\n", k, here);
if (GMT_Write_Data (API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, GMT_CONTAINER_AND_DATA, S_obj->wesn, file, G) != GMT_NOERROR) {
return (API->error);
}
here += U_obj->header->size; /* Move to next level */
}
U_obj->header->n_bands = save_n_bands; /* Restore number of layers */
/* Wipe and free the temporary grid structure */
G->data = NULL;
G->header = NULL;
gmt_free_grid (GMT, &G, true);
if (gmt_nc_write_cube (GMT, U_obj, S_obj->wesn, S_obj->filename) != GMT_NOERROR)
return (gmtlib_report_error (API, API->error));
done = true;
}
break;
Expand Down Expand Up @@ -12453,7 +12423,13 @@ struct GMT_RESOURCE * GMT_Encode_Options (void *V_API, const char *module_name,
else if (!strncmp (module, "greenspline", 11U) && (opt = GMT_Find_Option (API, 'R', *head))) {
/* Found the -R"domain" option; determine the dimensionality of the output */
unsigned dim = gmtapi_determine_dimension (API, opt->arg);
type = (dim == 2) ? 'G' : 'D';
switch (dim) { /* Determine if output is D, G, or U */
case 1: type = 'D'; break; /* 1-D is a data table */
case 2: type = 'G'; break; /* 2-D is always a grid */
default: /* 3-D, but can be dataset or cube */
type = ((opt = GMT_Find_Option (API, 'G', *head))) ? 'U' : 'D';
break;
}
}
/* 1g. Check if this is the triangulate module, where primary dataset output should be turned off if -G given without -M,N,Q,S */
else if (!strncmp (module, "triangulate", 11U) && (opt = GMT_Find_Option (API, 'G', *head))) {
Expand Down
10 changes: 10 additions & 0 deletions src/gmt_grdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2692,6 +2692,11 @@ struct GMT_GRID *gmt_create_grid (struct GMT_CTRL *GMT) {
G->header = gmt_get_header (GMT);
gmt_grd_init (GMT, G->header, NULL, false); /* Set default values */
GMT_Set_Index (GMT->parent, G->header, GMT_GRID_LAYOUT);
#ifdef DOUBLE_PRECISION_GRID
G->header->type = GMT_GRID_IS_ND;
#else
G->header->type = GMT_GRID_IS_NF;
#endif
GH->alloc_mode = GMT_ALLOC_INTERNALLY; /* Memory can be freed by GMT. */
GH->alloc_level = GMT->hidden.func_level; /* Must be freed at this level. */
GH->id = GMT->parent->unique_var_ID++; /* Give unique identifier */
Expand Down Expand Up @@ -3330,6 +3335,11 @@ struct GMT_CUBE *gmtlib_create_cube (struct GMT_CTRL *GMT) {
GU = gmt_get_U_hidden (C);
C->header = gmt_get_header (GMT);
gmt_grd_init (GMT, C->header, NULL, false); /* Set default values */
#ifdef DOUBLE_PRECISION_GRID
C->header->type = GMT_GRID_IS_ND;
#else
C->header->type = GMT_GRID_IS_NF;
#endif
GMT_Set_Index (GMT->parent, C->header, GMT_GRID_LAYOUT);
GU->alloc_mode = GMT_ALLOC_INTERNALLY; /* Memory can be freed by GMT. */
GU->alloc_level = GMT->hidden.func_level; /* Must be freed at this level. */
Expand Down
1 change: 1 addition & 0 deletions src/gmt_hidden.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ struct GMT_GRID_HEADER_HIDDEN {
int z_id; /* NetCDF: id of z field */
int ncid; /* NetCDF: file ID */
int xy_dim[2]; /* NetCDF: dimension order of x and y; normally {1, 0} */
int xyz_id[3]; /* NetCDF: id of x, y, and z (if cube) field */
size_t t_index[3]; /* NetCDF: index of higher coordinates */
size_t data_offset; /* NetCDF: distance from the beginning of the in-memory grid */
size_t n_alloc; /* Bytes allocated for this grid */
Expand Down
Loading