#include #include #include #include "readdata.h" using namespace std; using namespace netCDF; template vector getVarVector(const NcVar &var) { int length = 1; for (NcDim dim : var.getDims()) { length *= dim.getSize(); } vector vec(length); var.getVar(vec.data()); return vec; } /** * Read a 3D matrix from a NetCDF variable. * Reads data into a contiguous 1D data vector. * Returns a pair of the size of the matrix (in the form of an extent) with the data vector. * * Inteded usage of this function involves using the two returned values * to create an mdspan: * * auto arr = mdspan(vec.data(), size); */ template pair, std::dextents> get3DMat(const NcVar &var) { if(var.getDimCount() != 3) { throw invalid_argument("Variable is not 3D"); } int timeLength = var.getDim(0).getSize(); int latLength = var.getDim(1).getSize(); int longLength = var.getDim(2).getSize(); vector vec(timeLength*latLength*longLength); var.getVar(vec.data()); auto arr = std::mdspan(vec.data(), timeLength, latLength, longLength); return {vec, arr.extents()}; } pair, std::dextents> readHydrodynamicU() { netCDF::NcFile data("../../../../data/hydrodynamic_U.h5", netCDF::NcFile::read); multimap< string, NcVar > vars = data.getVars(); return get3DMat(vars.find("uo")->second); } tuple, vector, vector> readGrid() { netCDF::NcFile data("../../../../data/grid.h5", netCDF::NcFile::read); multimap< string, NcVar > vars = data.getVars(); vector time = getVarVector(vars.find("times")->second); vector longitude = getVarVector(vars.find("longitude")->second); vector latitude = getVarVector(vars.find("latitude")->second); return {time, latitude, longitude}; }