From 118c4142c10c7b4c448a5a81a80160019dba653e Mon Sep 17 00:00:00 2001 From: melt Date: Mon, 22 Jan 2024 11:38:13 +0000 Subject: [PATCH] add comments to describe switching the dims --- Grid.cpp | 10 +++++++++- Grid.hpp | 8 +++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Grid.cpp b/Grid.cpp index 614fa70..87d10d3 100644 --- a/Grid.cpp +++ b/Grid.cpp @@ -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)); @@ -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)); @@ -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) { @@ -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; @@ -159,7 +167,7 @@ Grid::Grid(MPI_Comm comm, const std::string& filename, // create copy of land mask ready to transpose std::vector _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++) { diff --git a/Grid.hpp b/Grid.hpp index c4feba9..ff6b40b 100644 --- a/Grid.hpp +++ b/Grid.hpp @@ -52,7 +52,7 @@ 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 @@ -60,6 +60,12 @@ class LIB_EXPORT Grid * 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)