I am trying to read 100 netcdf files using the package ncdf4 of R.
library(ncdf4)
require(ncdump)
library(reshape2)
require(arrayhelpers)
flist <- list.files(ncpath, pattern = "tempp.nc",recursive=TRUE,
full.names=TRUE)
pts <- read.table(ncpath/pts.dat,sep="\t",head=F)
I can read the variables for each file .nc, however I have tried to go through all the files and read, for examples, longitude, latitude and the temperature value. From this, I can generate a table with all values.
I haven't managed to get every variable from the netcdf in a recursive way. This means, extracting longitude, latitude, and temperature to integrate all the 100 files in a dataframe.
If I had raster files I can do the following and works to extract values from specific points' file ('pts.dat').
extracted=list()
for(i in seq_along(flist)) {
extracted[[i]] = extract(raster(flist[i]), pts, method="simple") }
Thus, I am searching for something similar to the above 'extracted' to go through the 100 temperature files that look like this:
The digital elevation model used: float dem(grid_lat, grid_lon) ; dem:long_name = "Grid_dem" ; dem:standard_name = "depth" ; dem:units = "meters" ; dem:missing_value = -9999.f ; dem:_FillValue = -9999.f ;
The resulted temperature value: float temp(lat, lon) ; temp:long_name = "Temperature" ; temp:units = "meters" ; temp:missing_value = -9999.f ; temp:_FillValue = -9999.f ;
raster::raster
orraster::stack
to read the data. Then your previous method, usingraster::extract
will work. You could also try the new terra package.pts_
in theextract
call). If you can edit to create code that runs and maybe show us metadata for two or three sample netCDFs, and describe in more detail what you are trying to calculate, we will be better placed to help you.