Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

Processing netcdf file in R with multiple variables

$
0
0

EDIT

I have edited my question since I made some progress.

I am trying to process the berkely earth data which is present in this website. http://berkeleyearth.org/data/. To get a sample data:

Scroll down to:

Daily Land (Experimental; 1880 - Recent) >Average High Temperature (TMAX) >1º x 1º Latitude-Longitude Grid (each decade 200-450 MB) >1990-1999

This netCDF file describes contains mean tmax and anomaly for each day rom 1990-1999 as a function of longitude and latitude. I also have a different set of lat lon for which I want to extract the daily mean tmax and anomaly data. This is how I am going about it.

dat <- structure(list(locatioID = paste0('ID', 1:16), lon = c(73.73, 86, 73.45, 86.41, 85.36, 81.95, 82.57, 75.66, 82.03, 81.73, 85.66, 85.31, 81.03, 81.70, 87.03, 73.38), lat = c(24.59, 20.08, 22.61, 23.33, 23.99, 19.09, 18.85, 15.25, 26.78, 16.63, 25.98, 23.28, 24.5, 21.23, 25.08, 21.11)), 
             row.names = c(1L, 3L, 5L, 8L, 11L, 14L, 17L, 18L, 19L, 21L, 
                           23L, 26L, 29L, 32L, 33L, 35L), class = "data.frame")

xy <- dat[,2:3]
spts <- SpatialPoints(xy, proj4string=CRS("+proj=longlat +datum=WGS84"))

# read the netcdf file 
 ncname <- file.path(dirList$inputDir, 'climate','tmax','Complete_TMAX_Daily_LatLong1_1990.nc')
 ncin <- nc_open(ncname)
 print(ncin)

 # first I find the length of the datapoints in the netcdf 
 lengthvar <- ncin$var[[1]][['varsize']]
 # there are 3652 days in the netcdf

 # define the variables I need 
 dname1 <- 'temperature' # anomaly 
 dname2 <- 'climatology' # climatology 

 # load as a stack separately for climatology and anomaly 
  climtest <- brick(ncname, varname = 'climatology')
  anotest <- brick(ncname, varname = 'temperature')

# Now iterate through each day and extract the climatology and anomaly   

 for(i in 1:lengthvar){

    tempClim <- climtest[[i]]
    tempAno <- anotest[[i]]

    # Extract data by spatial point
     temp2 <- extract(tempClim, spts)
     temp2 <- extract(tempAno, spts)

      # Error in if (x@file@nodatavalue < 0) { : missing value where TRUE/FALSE needed
    }

I am not sure what does this error mean and what I am doing wrong here leading to this error.


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>