Git Product home page Git Product logo

Comments (4)

ZedThree avatar ZedThree commented on July 24, 2024

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.

pf4d avatar pf4d commented on July 24, 2024

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.

pf4d avatar pf4d commented on July 24, 2024

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.

ZedThree avatar ZedThree commented on July 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.