Skip to content

Commit

Permalink
add comments to describe switching the dims
Browse files Browse the repository at this point in the history
  • Loading branch information
TomMelt committed Jan 22, 2024
1 parent 45fc990 commit 118c414
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 9 additions & 1 deletion Grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ Grid::Grid(MPI_Comm comm, const std::string& filename,
if (ret != NC_NOERR)
data_nc_id = nc_id;
int dim0_nc_id, dim1_nc_id;
// I have switched the order of the dimensions here to reflect the change of
// indices in nextsim-dg
NC_CHECK(nc_inq_dimid(data_nc_id, dim1_name.c_str(), &dim0_nc_id));
NC_CHECK(nc_inq_dimid(data_nc_id, dim0_name.c_str(), &dim1_nc_id));

Expand All @@ -79,6 +81,8 @@ Grid::Grid(MPI_Comm comm, const std::string& filename,
// Retrieve the extent of each dimension of interest. The dimensions of
// interest are the spatial dimensions of the grid. These are named "x" and
// "y" by default.
// I have switched the order of the dimensions here to reflect the change of
// indices in nextsim-dg
size_t tmp_0, tmp_1;
NC_CHECK(nc_inq_dimlen(data_nc_id, dim1_nc_id, &tmp_0));
NC_CHECK(nc_inq_dimlen(data_nc_id, dim0_nc_id, &tmp_1));
Expand Down Expand Up @@ -138,6 +142,8 @@ Grid::Grid(MPI_Comm comm, const std::string& filename,
int dim_id[NDIMS];
char dim_name[NDIMS][128];
NC_CHECK(nc_inq_vardimid(data_nc_id, mask_nc_id, &dim_id[0]));
// I have switched the order of the dimensions here to reflect the change of
// indices in nextsim-dg
NC_CHECK(nc_inq_dimname(data_nc_id, dim_id[1], &dim_name[0][0]));
NC_CHECK(nc_inq_dimname(data_nc_id, dim_id[0], &dim_name[1][0]));
if (dim_name[0] != dim0_name || dim_name[1] != dim1_name) {
Expand All @@ -147,6 +153,8 @@ Grid::Grid(MPI_Comm comm, const std::string& filename,

_land_mask.resize(_num_objects);
size_t start[NDIMS], count[NDIMS];
// I have switched the order of the dimensions here to reflect the change of
// indices in nextsim-dg
// Coordinate of first element
start[1] = _global_0;
start[0] = _global_1;
Expand All @@ -159,7 +167,7 @@ Grid::Grid(MPI_Comm comm, const std::string& filename,
// create copy of land mask ready to transpose
std::vector<int> _land_mask_copy(_land_mask);

// transpose _land_mask
// transpose _land_mask to reflect the change of indices in nextsim-dg
int index = 0;
for (size_t j = 0; j < count[1]; j++) {
for (size_t i = 0; i < count[0]; i++) {
Expand Down
8 changes: 7 additions & 1 deletion Grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,20 @@ class LIB_EXPORT Grid
* dim1) dimensions of the Grid class, where dim1 is defined as the fastest
* increasing dimension. For example, if the dimensions of interest in the
* netCDF file are named x and y and the variables are dimensioned as (y, x),
* then the y dimension will be mapped to dim0 and x to dim1 of the Grid
* then the y dimension will be mapped to dim1 and x to dim0 of the Grid
* class, since the netCDF C/C++ convention is that the last dimension in the
* CDL notation is the fastest increasing dimension. The default dimension
* names for the netCDF file are "x" for dim0 and "y" for dim1, and the
* default name for the land mask variable is "mask". the We assume that all
* variables defined in the netCDF file follow the same convention in terms of
* dimension ordering.
*
* The code was originally written with the assumption that nextsim-dg would
* store arrays in (x,y) order. However, the indices have since been swapped
* and now nextsim-dg uses (y,x) order. To account for the switch of indices
* the grid input netcdf file is now transposed when it is read-in and the x
* and y dims are swapped in Grid.cpp.
*
* @param comm MPI communicator.
* @param filename Grid file in netCDF format.
* @param dim0_name Name of 1st grid dimension in netCDF file (optional)
Expand Down

0 comments on commit 118c414

Please sign in to comment.