Merge pull request #31 from MakeNEnjoy/robin-uvgrid-filepath

feat: made uvgrid take a path as argument
This commit is contained in:
Djairo 2024-05-14 12:10:48 +00:00 committed by GitHub
commit 813d586d7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 95 additions and 85 deletions

View File

@ -8,14 +8,14 @@
using namespace std; using namespace std;
UVGrid::UVGrid() { UVGrid::UVGrid(string path) {
auto us = readHydrodynamicU(); auto us = readHydrodynamicU(path);
auto vs = readHydrodynamicV(); auto vs = readHydrodynamicV(path);
if (us.size() != vs.size()) { if (us.size() != vs.size()) {
throw domain_error(sizeError2); throw domain_error(sizeError2);
} }
tie(times, lats, lons) = readGrid(); tie(times, lats, lons) = readGrid(path);
timeSize = times.size(); timeSize = times.size();
latSize = lats.size(); latSize = lats.size();

View File

@ -6,84 +6,89 @@
class UVGrid { class UVGrid {
private: private:
/** /**
* 1D data vector of all the us and vs * 1D data vector of all the us and vs
*/ */
std::vector<Vel> uvData; std::vector<Vel> uvData;
public: public:
UVGrid(); /**
* Constructs the UVGrid.
* @param path The data path where the constructor expects to find
* the files hydrodynamic_U.h5, hydrodynamic_V.h5, and grid.h5
*/
UVGrid(std::string path);
/** /**
* The matrix has shape (timeSize, latSize, lonSize) * The matrix has shape (timeSize, latSize, lonSize)
*/ */
size_t timeSize; size_t timeSize;
size_t latSize; size_t latSize;
size_t lonSize; size_t lonSize;
/** /**
* Assuming grid is a regular grid, gives the longitudinal spacing of grid. * Assuming grid is a regular grid, gives the longitudinal spacing of grid.
* @return longitudinal spacing * @return longitudinal spacing
*/ */
double lonStep() const; double lonStep() const;
/** /**
* Assuming grid is a regular grid, gives the latitudinal spacing of grid. * Assuming grid is a regular grid, gives the latitudinal spacing of grid.
* @return latitudinal spacing * @return latitudinal spacing
*/ */
double latStep() const; double latStep() const;
/** /**
* Assuming grid is a regular grid, gives the time spacing of grid. * Assuming grid is a regular grid, gives the time spacing of grid.
* @return time spacing * @return time spacing
*/ */
int timeStep() const; int timeStep() const;
/** /**
* Returns the lowest longitudinal value of grid. * Returns the lowest longitudinal value of grid.
* @return minimum longitude * @return minimum longitude
*/ */
double lonMin() const; double lonMin() const;
/** /**
* Returns the highest longitudinal value of grid. * Returns the highest longitudinal value of grid.
* @return maximum longitude * @return maximum longitude
*/ */
double lonMax() const; double lonMax() const;
/** /**
* Returns the lowest latitudinal value of grid. * Returns the lowest latitudinal value of grid.
* @return minimum latitude * @return minimum latitude
*/ */
double latMin() const; double latMin() const;
/** /**
* Returns the higehst latitudinal value of grid. * Returns the higehst latitudinal value of grid.
* @return maximum latitude * @return maximum latitude
*/ */
double latMax() const; double latMax() const;
/** /**
* times, lats, lons are vector of length timeSize, latSize, lonSize respectively. * times, lats, lons are vector of length timeSize, latSize, lonSize respectively.
* The maintain the following invariant: * The maintain the following invariant:
* grid[timeIndex,latIndex,lonIndex] gives the u,v at the point with latitude at lats[latIndex], * grid[timeIndex,latIndex,lonIndex] gives the u,v at the point with latitude at lats[latIndex],
* with longitude at lons[lonIndex], and with time at times[timeIndex]. * with longitude at lons[lonIndex], and with time at times[timeIndex].
*/ */
std::vector<int> times; std::vector<int> times;
std::vector<double> lats; std::vector<double> lats;
std::vector<double> lons; std::vector<double> lons;
/** /**
* The 3D index into the data. The array is sized by [8761][67][116] * The 3D index into the data. The array is sized by [8761][67][116]
* @return Velocity at that index * @return Velocity at that index
*/ */
const Vel& operator[](size_t timeIndex, size_t latIndex, size_t lonIndex) const; const Vel &operator[](size_t timeIndex, size_t latIndex, size_t lonIndex) const;
/** /**
* Streams a slice at timeIndex t of the matrix to the outstream given by os * Streams a slice at timeIndex t of the matrix to the outstream given by os
* @param os outstream * @param os outstream
* @param t index with which to slice matrix * @param t index with which to slice matrix
*/ */
void streamSlice(std::ostream &os, size_t t); void streamSlice(std::ostream &os, size_t t);
}; };
#endif //UVGRID_H #endif //UVGRID_H

View File

@ -7,44 +7,48 @@
using namespace std; using namespace std;
using namespace netCDF; using namespace netCDF;
template <typename T> template<typename T>
vector<T> getVarVector(const NcVar &var) { vector<T> getVarVector(const NcVar &var) {
int length = 1; int length = 1;
for (NcDim dim : var.getDims()) { for (NcDim dim: var.getDims()) {
length *= dim.getSize(); length *= dim.getSize();
} }
vector<T> vec(length); vector<T> vec(length);
var.getVar(vec.data()); var.getVar(vec.data());
return vec; return vec;
} }
vector<double> readHydrodynamicU() { vector<double> readHydrodynamicU(string path) {
// Vs and Us flipped cause the files are named incorrectly // Vs and Us flipped cause the files are named incorrectly
netCDF::NcFile data("../../../../data/hydrodynamic_V.h5", netCDF::NcFile::read); string fileName = "hydrodynamic_V.h5";
multimap< string, NcVar > vars = data.getVars(); netCDF::NcFile data(path + '/' + fileName, netCDF::NcFile::read);
return getVarVector<double>(vars.find("vo")->second); multimap<string, NcVar> vars = data.getVars();
return getVarVector<double>(vars.find("vo")->second);
} }
vector<double> readHydrodynamicV() { vector<double> readHydrodynamicV(string path) {
// Vs and Us flipped cause the files are named incorrectly // Vs and Us flipped cause the files are named incorrectly
netCDF::NcFile data("../../../../data/hydrodynamic_U.h5", netCDF::NcFile::read); string fileName = "hydrodynamic_U.h5";
netCDF::NcFile data(path + '/' + fileName, netCDF::NcFile::read);
multimap< string, NcVar > vars = data.getVars(); multimap<string, NcVar> vars = data.getVars();
return getVarVector<double>(vars.find("uo")->second); return getVarVector<double>(vars.find("uo")->second);
} }
tuple<vector<int>, vector<double>, vector<double>> readGrid() { tuple<vector<int>, vector<double>, vector<double>> readGrid(string path) {
netCDF::NcFile data("../../../../data/grid.h5", netCDF::NcFile::read); string fileName = "grid.h5";
multimap< string, NcVar > vars = data.getVars(); netCDF::NcFile data(path + '/' + fileName, netCDF::NcFile::read);
vector<int> time = getVarVector<int>(vars.find("times")->second); multimap<string, NcVar> vars = data.getVars();
vector<double> longitude = getVarVector<double>(vars.find("longitude")->second); vector<int> time = getVarVector<int>(vars.find("times")->second);
vector<double> latitude = getVarVector<double>(vars.find("latitude")->second); vector<double> longitude = getVarVector<double>(vars.find("longitude")->second);
vector<double> latitude = getVarVector<double>(vars.find("latitude")->second);
return {time, latitude, longitude}; return {time, latitude, longitude};
} }

View File

@ -5,18 +5,18 @@
* reads the file hydrodynamic_U.h5 * reads the file hydrodynamic_U.h5
* @return the data vector of us * @return the data vector of us
*/ */
std::vector<double> readHydrodynamicU(); std::vector<double> readHydrodynamicU(std::string path);
/** /**
* reads the file hydrodynamic_V.h5 * reads the file hydrodynamic_V.h5
* @return the data vector of vs * @return the data vector of vs
*/ */
std::vector<double> readHydrodynamicV(); std::vector<double> readHydrodynamicV(std::string path);
/** /**
* Reads the file grid.h5 * Reads the file grid.h5
* @return a tuple of (times, latitude, longitude) * @return a tuple of (times, latitude, longitude)
*/ */
std::tuple<std::vector<int>, std::vector<double>, std::vector<double>> readGrid(); std::tuple<std::vector<int>, std::vector<double>, std::vector<double>> readGrid(std::string path);
#endif //READDATA_H #endif //READDATA_H

View File

@ -23,16 +23,17 @@ using namespace std;
int main() { int main() {
cout << "Reading data..." << endl; cout << "Reading data..." << endl;
shared_ptr<UVGrid> uvGrid = make_shared<UVGrid>(); string dataPath = "../../../../data";
shared_ptr<UVGrid> uvGrid = make_shared<UVGrid>(dataPath);
auto kernelRK4 = make_unique<RK4AdvectionKernel>(uvGrid); auto kernelRK4 = make_unique<RK4AdvectionKernel>(uvGrid);
auto kernelRK4BoundaryChecked = make_unique<SnapBoundaryConditionKernel>(std::move(kernelRK4), uvGrid); auto kernelRK4BoundaryChecked = make_unique<SnapBoundaryConditionKernel>(std::move(kernelRK4), uvGrid);
cout << "Starting vtk..." << endl; cout << "Starting vtk..." << endl;
auto l = new LGlyphLayer(uvGrid, std::move(kernelRK4BoundaryChecked)); auto l = new LGlyphLayer(uvGrid, std::move(kernelRK4BoundaryChecked));
l->spoofPoints(); // l->spoofPoints();
unique_ptr<Program> program = make_unique<Program>(DT); unique_ptr<Program> program = make_unique<Program>(DT);
program->addLayer(new BackgroundImage("../../../../data/map_661-661.png")); program->addLayer(new BackgroundImage(dataPath + "/map_661-661.png"));
program->addLayer(new EGlyphLayer(uvGrid)); program->addLayer(new EGlyphLayer(uvGrid));
program->addLayer(l); program->addLayer(l);