Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit 901559a

Browse files
authoredAug 21, 2024
Update README.md to add example for reading gridded NetCDF data in R
1 parent ee5c730 commit 901559a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
 

‎q_and_a/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,31 @@ There could be cases in which you've shared sensitive information during a proje
1010

1111
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.
1212

13+
### How do I read in gridded NetCDF files in R?
14+
15+
An example, for 2D data:
16+
17+
```
18+
# Load library
19+
library(ncdf4)
20+
21+
# Define the filepath
22+
file <- "/path/to/file.nc"
23+
24+
# Open the netCDF file for reading
25+
ncin <- nc_open(file)
26+
27+
# Get coordinates
28+
X <- ncvar_get(ncin, "xcoordinatename")
29+
Y <- ncvar_get(ncin, "ycoordinatename")
30+
31+
# Get data
32+
bla.array <- ncvar_get(ncin, "variablename")
33+
34+
# Get units
35+
dunits <- ncatt_get(ncin, variablename, "units")
36+
37+
# Set coordinates of data array (might have to switch 1 and 2 around)
38+
dimnames(bla.array)[[1]] <- X
39+
dimnames(bla.array)[[2]] <- Y
40+
```

0 commit comments

Comments
 (0)
This repository has been archived.