Skip to content

Gridpp in C plus plus

Thomas Nipen edited this page Dec 15, 2022 · 14 revisions

Example code

To use gridpp in C++, include the header file and read the API documentation for available functions.

include <gridpp.h>
using gridpp

int main(void) {

    // Define a coarse-scale grid and create some fake temperature data on this grid
    gridpp::vec2 coarse_lats = gridpp::init_vec2(6, 5);
    gridpp::vec2 coarse_lons = gridpp::init_vec2(6, 5);
    gridpp::vec2 coarse_temperature = gridpp::init_vec2(6, 5);
    for(int y = 0; y < 6; y++) {
        for(int x = 0; x < 5; x++) {
            coarse_lats[y][x] = y;
            coarse_lons[y][x] = x;
            coarse_temperature[y][x] = 2 * y * y + x * x
        }
    }
    gridpp::vec2 coarse_grid = gridpp::Grid(lats, lons)

    // Define the desired-fine scale grid
    gridpp::vec2 fine_lats = gridpp::init_vec2(61, 51);
    gridpp::vec2 fine_lons = gridpp::init_vec2(61, 51);
    for(int y = 0; y < 61; y++) {
        for(int x = 0; x < 51; x++) {
            fine_lats[y][x] = y / 10;
            fine_lons[y][x] = x / 10;
        }
    }
    gridpp::vec2 fine_grid = gridpp::Grid(lats, lons)

    // Perform interpolation from the coarse grid to the fine grid
    gridpp::vec2 fine_temperature = gridpp::bilinear(coarse_grid, fine_grid, coarse_temperature)
}

Compilation

Here is how this can be compiled:

g++ example.cpp -l gridpp
Clone this wiki locally