【问题标题】:Processing netcdf file in R with multiple variables在 R 中处理带有多个变量的 netcdf 文件
【发布时间】:2020-03-10 18:36:16
【问题描述】:

编辑

我已经编辑了我的问题,因为我取得了一些进展。

我正在尝试处理本网站中的伯克利地球数据。 http://berkeleyearth.org/data/。获取样本数据:

向下滚动到:

Daily Land(实验性;1880 - 最近)> 平均高温 (TMAX) > 1º x 1º 纬度-经度网格(每十年 200-450 MB)> 1990-1999

此 netCDF 文件描述了 1990-1999 年间每一天的平均 tmax 和异常值,作为经度和纬度的函数。我还有一组不同的纬度,我想提取每日平均 tmax 和异常数据。这就是我要做的事情。

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
    }

我不确定这个错误是什么意思,以及我在这里做错了什么导致了这个错误。

【问题讨论】:

标签: r raster netcdf netcdf4


【解决方案1】:

如果栅格图层由于大小而被保留在磁盘上并且未存储在内存中:

> inMemory(tempClim)
[1] FALSE

然后extract 失败:

> extract(tempClim, spts)
Error in if (x@file@nodatavalue < 0) { : 
  missing value where TRUE/FALSE needed

让 R 读入它并工作:

> extract(readAll(tempClim), spts)
 [1] 23.83663 27.29292 28.61940 24.58966 23.86629 27.96306 27.10444 30.01295
 [9] 22.47180 29.46369 22.79476 23.86629 23.55748 27.75946 22.86436 30.12937

虽然tempClim 不是很大 (180x360),但它来自一个大型结构:

> dim(climtest)
[1] 180 360 365

并且 R 正在推迟从磁盘读取,直到你告诉它。通过包裹在readAll 中,它可以做到这一点。我不知道为什么extract 没有打印出更好的错误消息,但我在这里也有似曾相识的感觉......

【讨论】:

    猜你喜欢
    • 2022-11-19
    • 2016-02-15
    • 2018-05-13
    • 2018-03-25
    • 2021-10-01
    • 2018-05-12
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    相关资源
    最近更新 更多