0

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 ;

4
  • 1
    If the NetCDF files are rasters, add the raster package (along with ncdf4) and then use raster::raster or raster::stack to read the data. Then your previous method, using raster::extract will work. You could also try the new terra package. Jun 11, 2021 at 17:28
  • 1
    Is the problem that your netCDFs aren't all on the same grid? Is that what you mean by not being able to "make calculation (integrate)" the files? Its hard to tell what exactly you want to do (and your code won't run anyway, there's a space after pts_ in the extract 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.
    – Spacedman
    Jun 11, 2021 at 21:01
  • @Spacedman, I edited the question, hopefully now id more clear.
    – Amber
    Jun 12, 2021 at 2:09
  • @Jeffrey Evans, I think most likely will be to read and reshape using e.g arrayhelpers?
    – Amber
    Jun 12, 2021 at 2:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.