Skip to content

Get data from a variable based on a range in associated coordinate variables #143

@lhmarsden

Description

@lhmarsden

Firstly, apologies if what I am asking for is already possible...

When loading in data from a NetCDF file, it is often useful to be able to select only a subset of the data. Using RNetCDF, I can do this:

netcdf_file <- 'https://opendap1.nodc.no/opendap/physics/point/cruise/nansen_legacy-single_profile/NMDC_Nansen-Legacy_PR_CT_58US_2021708/CTD_station_P1_NLEG01-1_-_Nansen_Legacy_Cruise_-_2021_Joint_Cruise_2-1.nc'
data <- open.nc(netcdf_file)

var.get.nc(data, "TEMP", c(5), c(3))

This give me 3 values from the TEMP variable starting with the 5th value in the array.

However, often I want to be able to extract data based on some coordinate range. For example, working with the data shown above, I might want to retrieve the data between 10 and 20 metres depth.

In python xarray, it is possible to select a subset of the whole xarray object using sel
https://docs.xarray.dev/en/latest/generated/xarray.DataArray.sel.html

This has a number of useful features, and even allows you to select the nearest data to the coordinate that you have provided as an argument.

I think something similar would be very helpful in RNetCDF, and I haven't seen how to do this in the documentation - though I might have missed something.

I am currently writing a tutorial series showing people how to work with NetCDF files using R, and I would like to use RNetCDF throughout this course. This is something that I would like to include in this.

For reference, here is my equivalent course to teach people how to work with NetCDF files in Python, predominantly using xarray:
https://lhmarsden.github.io/NetCDF_in_Python_from_beginner_to_pro/intro.html

A course in R will follow the same structure with most of the same chapters.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions