How to Extract Variable Names from a Netcdf File in R

How to extract variable names from a netCDF file in R?

If your ncdf object is called nc, then quite simply:

names(nc$var)

With an example, using the dataset downloaded here, for instance (since you didn't provide with one):

nc <- open.ncdf("20130128-ABOM-L4HRfnd-AUS-v01-fv01_0-RAMSSA_09km.nc")
names(nc$var)
[1] "analysed_sst" "analysis_error" "sea_ice_fraction" "mask"

list of all variable names from a NetCDF dataset, in R

I use the ncdf pacakge which I found to be much easier to install than the RNetCDF package across operating systems (notably Linux and MacOS). To get the list of variables you simply:

library(ncdf)
nc = open.ncdf('example.nc')
variables = names(nc[['var']])

Extract certain values out of netCDF

It looks like you are using the ncdf package in R. If you can, I recommend using the updated ncdf4 package, which is based on Unidata's netcdf version 4 library (link).

Back to your problem. I use the ncdf4 package, but I think the ncdf package works the same way. When you call the function get.var.ncdf, you also need to explicitly supply the name of the variable that you want to extract. I think you can get the names of the variables using names(test$var).

So you need to do something like this:

# Open the nc file
test = open.ncdf("rr_0.25deg_reg_1980-1994_v8.0.nc")

# Now get the names of the variables in the nc file
names(test$var)

# Get the data from the first variable listed above
# (May not fit in memory)
data = get.var.ncdf(test,varid=names(test$var)[1])

# If you only want a certain range of data.
# The following will probably not fit in memory either
# data = get.var.ncdf(test,varid=names(test$var)[1])[1:464,1:201,1:365]

For your problem, you would need to replace varid=names(test$var)[1] above with varid='VARIABLE_NAME', where VARIABLE_NAME is the variable you want to extract.

Hope that helps.

EDIT:

I installed the ncdf package on my system, and the above code works for me!

How to extract varname from raster?

One of those maybe?

gsub("(.*)\\\\","", r@file@name, perl=TRUE)
# [1] "consecutive_wet_days_index_50_.nc"

r@file@datanotation
# [1] "FLT4S"

You may examine the structure using str(r).

Or use capture.output hack.

x <- capture.output(r)
trimws(gsub("(.*)\\:","", x[9], perl=TRUE))
# [1] "consecutive_wet_days_index_per_time_period"

Combining .nc files and extracting selected variables

The way I have used ".nc" files with satellite data, in R. Have been reading it in with the "raster" library as a raster file.

library(raster)

r <- raster("yuor_file.nc")
plot(r) # quick plot to see if everything is as it should be

The way I read in my timeseries was with a loop, and in addition I used a function found from this site somewhere, to covert the raster into a sensible r-data frame

stack overflow function, to convert the loaded raster to data frame

gplot_data <- function(x, maxpixels = 50000)  {
x <- raster::sampleRegular(x, maxpixels, asRaster = TRUE)
coords <- raster::xyFromCell(x, seq_len(raster::ncell(x)))
## Extract values
dat <- utils::stack(as.data.frame(raster::getValues(x)))
names(dat) <- c('value', 'variable')

dat <- dplyr::as.tbl(data.frame(coords, dat))

if (!is.null(levels(x))) {
dat <- dplyr::left_join(dat, levels(x)[[1]],
by = c("value" = "ID"))
}
dat
}

Read in one file at a time, convert with function and return data.frame

files<- list.files(folder, pattern= ".nc", full.names = TRUE)

fun <- function(i) {
#read in one file at a time
r <- raster(files[i])

#convert to normal data frame
temp <- gplot_data(r)
temp #output
}
dat <- plyr::rbind.fill(lapply(1:length(files), fun)) #bind each iteration

Here a plot using ggplot2 and ggforce.

ggplot() +
geom_tile(data = dat,
aes(x = x, y = y, fill = value))

Alternatively if you do not know the context of you file, the following, from the "ncdf4" package, will help you inspect it. https://towardsdatascience.com/how-to-crack-open-netcdf-files-in-r-and-extract-data-as-time-series-24107b70dcd

library(ncdf4)
our_nc_data <- nc_open("/your_file.nc")

print(our_nc_data)

# look for the variable names and assign them to vectors that can be bound together in dataframes
lat <- ncvar_get(our_nc_data, "lat") #names of latitude column
lon <- ncvar_get(our_nc_data, "lon") #name of longitude column

time <- ncvar_get(our_nc_data, "time") #the time was called time
tunits <- ncatt_get(our_nc_data, "time", "units")# check units

lswt_array <- ncvar_get(our_nc_data, "analysed_sst") #select the relevant variable, this is temperature named "analysed_sst"

How to Extract Variables from Multiple Aqua Modis netCDF files in R?

You seem to misunderstand what a RasterStack or SpatRaster object is. You printed SST_brick and it clearly shows that it knows about the longitude and latitudes. You do not need to anything else.

With

library(terra)
filenames = list.files('~/Documents/Ocean_ColorSST_2016_2021',pattern='*.nc',full.names=TRUE)

And assuming that dolphins is a matrix or data.frame with variables "longitude" and "latitude", you could do this for one file like this:

SST <- rast(filenames[1], "sst")
e <- extract(SST, dolphins[, c("longitude", "latitude")])

But you can probably do this in one swoop like this

SSTs <- rast(filenames, "sst")
e <- extract(SSTs, dolphins[, c("longitude", "latitude")])

Unable to extract a netcdf variable in R that I see in Panoply

Extracting the variables using the appropriate function ncdf4::ncvar_get seems to work:

df <- ncdf4::nc_open(filename = "MIROC-ES2L_LGM_moclim_tas.nc")
ncdf4::ncvar_get(df, "lat_bnds")

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
[1,] -90.00000 -86.57775 -83.75703 -80.95502 -78.15835 -75.36394 -72.57070 -69.77815 -66.98603 -64.19420 -61.40258 -58.61111 -55.81976
[2,] -86.57775 -83.75703 -80.95502 -78.15835 -75.36394 -72.57070 -69.77815 -66.98603 -64.19420 -61.40258 -58.61111 -55.81976 -53.02849
[,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
[1,] -53.02849 -50.23730 -47.44615 -44.65506 -41.86400 -39.07297 -36.28196 -33.49098 -30.70002 -27.90906 -25.11813 -22.32720 -19.53628
[2,] -50.23730 -47.44615 -44.65506 -41.86400 -39.07297 -36.28196 -33.49098 -30.70002 -27.90906 -25.11813 -22.32720 -19.53628 -16.74537
[,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38] [,39]
[1,] -16.74537 -13.95447 -11.163569 -8.372673 -5.581781 -2.79089 0.00000 2.790890 5.581781 8.372673 11.16357 13.95447 16.74537
[2,] -13.95447 -11.16357 -8.372673 -5.581781 -2.790890 0.00000 2.79089 5.581781 8.372673 11.163569 13.95447 16.74537 19.53628
[,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49] [,50] [,51] [,52] [,53]
[1,] 19.53628 22.32720 25.11813 27.90906 30.70002 33.49098 36.28196 39.07297 41.86400 44.65506 47.44615 50.23730 53.02849 55.81976
[2,] 22.32720 25.11813 27.90906 30.70002 33.49098 36.28196 39.07297 41.86400 44.65506 47.44615 50.23730 53.02849 55.81976 58.61111
[,54] [,55] [,56] [,57] [,58] [,59] [,60] [,61] [,62] [,63] [,64]
[1,] 58.61111 61.40258 64.19420 66.98603 69.77815 72.57070 75.36394 78.15835 80.95502 83.75703 86.57775
[2,] 61.40258 64.19420 66.98603 69.77815 72.57070 75.36394 78.15835 80.95502 83.75703 86.57775 90.00000

ncdf4::ncvar_get(df, "lat")

[1] -87.863799 -85.096527 -82.312913 -79.525607 -76.736900 -73.947515 -71.157752 -68.367756 -65.577607 -62.787352 -59.997020
[12] -57.206632 -54.416200 -51.625734 -48.835241 -46.044727 -43.254195 -40.463648 -37.673090 -34.882521 -32.091944 -29.301360
[23] -26.510769 -23.720174 -20.929574 -18.138971 -15.348365 -12.557756 -9.767146 -6.976534 -4.185921 -1.395307 1.395307
[34] 4.185921 6.976534 9.767146 12.557756 15.348365 18.138971 20.929574 23.720174 26.510769 29.301360 32.091944
[45] 34.882521 37.673090 40.463648 43.254195 46.044727 48.835241 51.625734 54.416200 57.206632 59.997020 62.787352
[56] 65.577607 68.367756 71.157752 73.947515 76.736900 79.525607 82.312913 85.096527 87.863799


Related Topics



Leave a reply



Submit