Skip to content

Commit

Permalink
Merge pull request #38 from NERC-CEH/q_and_a_netcdf_R
Browse files Browse the repository at this point in the history
Added example for reading gridded NetCDF data in R to the Q and A
  • Loading branch information
metazool authored Aug 28, 2024
2 parents 585e61d + 901559a commit 18f360a
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions q_and_a/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,31 @@ There could be cases in which you've shared sensitive information during a proje

If, for whatever good reason, you want to publish your project without its history then the "simplest" route is to make a local copy of it without the .git directory that stores its graph of changes, and create a new repository using that copy.

### How do I read in gridded NetCDF files in R?

An example, for 2D data:

```
# Load library
library(ncdf4)
# Define the filepath
file <- "/path/to/file.nc"
# Open the netCDF file for reading
ncin <- nc_open(file)
# Get coordinates
X <- ncvar_get(ncin, "xcoordinatename")
Y <- ncvar_get(ncin, "ycoordinatename")
# Get data
bla.array <- ncvar_get(ncin, "variablename")
# Get units
dunits <- ncatt_get(ncin, variablename, "units")
# Set coordinates of data array (might have to switch 1 and 2 around)
dimnames(bla.array)[[1]] <- X
dimnames(bla.array)[[2]] <- Y
```

0 comments on commit 18f360a

Please sign in to comment.