Comments (4)
You can use std::vector::data
to get a pointer to the underlying storage:
// Create a vector of type T with the appropriate size reserved.
std::vector<T> value(dims[0].getSize());
// Assuming `var` is your `NcVar`
var.getVar(value.data());
You could even wrap this up in a helper template function to have something that feels more like idiomatic C++:
/// Return variable data as a flat 1D array.
template <typename T>
std::vector<T> get_variable(const NcVar& var) {
auto dims = var.getDims();
// Get the product of all the dimension sizes
auto total_size = std::accumulate(dims.begin(), dims.end(), 1,
[](const NcDim& dim, std::size_t size) {
return dim.getSize() * size;
});
std::vector<T> values(total_size);
var.getVar(values.data());
return values;
}
// To call our helper, we need to specify the type.
// Note that this does no checking you've given the correct type!
auto data = get_variable<double>(var);
This is just an example that I've not checked actually compiles. For real production use, you might not want a flat 1D array, and you almost certainly want to do some error checking, especially of the types!
from netcdf-cxx4.
Thank you for the quick answer. Do you know how this could be done with two-dimensional data (a vector<vector<T>>
)? I assume I can pull each row one at a time from the NcVar
using one of the NcVar::getVar
methods and populate each vector
with those elements.
Ideally I would like to put each row from the NcVar
into a single "flat" vector
.
Thanks again!
from netcdf-cxx4.
Wow, that was a silly question, you already answered it. I never looked at lambda functions, it took a minute to look though your code to realize that var::getVar
loaded the data as flattened (meaning I suppose that netCDF-4
stores the data as flat arrays?).
Thank you.
from netcdf-cxx4.
NcVar::getVar
has an overload set that takes start
and count
vectors which you can use to pull out some N-D chunk from the file. The version I showed does indeed just read the whole N-D variable as a flat array. How they're actually stored on disk is a bit more complicated, but luckily we don't need to care about that.
In our code, we actually have a set of classes that wrap 2D and 3D arrays, storing them internally as 1D, and providing indexing operators for access. This is much better for data locality than using vector<vector<T>>
for example.
from netcdf-cxx4.
Related Issues (20)
- Get attributes in the original order
- compile failure ncVar.cpp:630:11 error: 'nc_def_var_filter' was not declared in this scope (on static build) HOT 1
- header file is right? HOT 2
- Erroneous condition always evaluating true HOT 2
- Minimum required C++ standard?
- 2 * field not copied in copy constructor ? HOT 1
- Does netcdf-cxx4 support parallel IO? HOT 1
- netCDF::exceptions::NcHdfErr after calling getVar too often HOT 1
- CMake uninstall target conflicts with other previously registered uninstall targets
- Inconsistent constructor signatures
- HDF 1.14 + netcdf-c 4.9 Support? HOT 3
- Cmake modernization
- Testing: cxx4_test_filter (10/18) - Failed HOT 2
- [feature-request] `netcdfcpp_meta.h` file HOT 1
- updating a variable in a netcdf file
- adding a variable to an existing nc file
- Please clarify H5Free HOT 4
- cfxyuygtrdecdfrtyjuhgdszdxfghyjohtgzwaqer2aQWE345R3Q3ER24W3S2sdsfgtp#
- cfxyuygtrdecdfrtyjuhgdszdxfghyjohtgzwaqer2aQWE345R3Q3ER24W3S2sdsfgtp#4715
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from netcdf-cxx4.