r ncdf4“;我找不到所需的var&引用;

r ncdf4“;我找不到所需的var&引用;,r,netcdf,r-raster,ncdf4,R,Netcdf,R Raster,Ncdf4,我收到一个“找不到请求的变量”错误,即使变量名有效-它适用于其他netcdf库-包括NCO、R netcdf4库和Panoply: 此示例使用一个名为: 这与预期的效果一样: 库(ncdf4) nc[1]“变量(或dimvar)名称:crs” #>[1]“文件名:test2.nc” #>.rasterObjectFromCDF(x,type=objecttype,band=band,…)中的警告:NAs #>强迫引入 #>if(band>nbands(r)){中出错:缺少需要TRUE/FALSE

我收到一个“找不到请求的变量”错误,即使变量名有效-它适用于其他netcdf库-包括NCO、R netcdf4库和Panoply:

此示例使用一个名为:

这与预期的效果一样:

库(ncdf4)
nc[1]“变量(或dimvar)名称:crs”
#>[1]“文件名:test2.nc”
#>.rasterObjectFromCDF(x,type=objecttype,band=band,…)中的警告:NAs
#>强迫引入
#>if(band>nbands(r)){中出错:缺少需要TRUE/FALSE的值
于2021年6月11日由(v2.0.0)创建

会话信息
sessioninfo::session_info()
#>-会话信息---------------------------------------------------------------
#>设定值
#>R版4.0.2版(2020-06-22)
#>操作系统Windows 10 x64
#>系统x86_64,mingw32
#>用户界面RTerm
#>语言(英文)
#>核对英语和美国
#>ctype English_United States.1252
#>美国/凤凰城
#>日期2021-06-11
#> 
#>-包裹-------------------------------------------------------------------
#>包*版本日期库源
#>资产负债表0.2.1 2019-03-21[1]起重机(R 4.0.2)
#>后端口1.2.1 2020-12-09[1]起重机(R 4.0.3)
#>cli 2.3.1 2021-02-23[1]起重机(R 4.0.4)
#>代码工具0.2-16 2018-12-24[2]起重机(R 4.0.2)
#>蜡笔1.4.12021-02-08[1]克兰(R4.0.2)
#>文摘0.6.27 2020-10-24[1]CRAN(R 4.0.3)
#>省略号0.3.1 2020-05-15[1]克兰(R 4.0.2)
#>评估0.14 2019-05-28[1]起重机(R 4.0.2)
#>fansi 0.4.2 2021-01-15[1]起重机(R 4.0.3)
#>fs 1.5.0 2020-07-31[1]起重机(R4.0.3)
#>胶水1.4.2 2020-08-27[1]起重机(R 4.0.2)
#>高0.8 2019-03-20[1]克兰(R4.0.2)
#>htmltools 0.5.1.1 2021-01-22[1]起重机(R 4.0.3)
#>克尼特1.31 2021-01-27[1]克兰(R 4.0.3)
#>晶格0.20-412020-04-02[2]起重机(R4.0.2)
#>生命周期1.0.0 2021-02-15[1]起重机(R 4.0.4)
#>magrittr 2.0.1 2020-11-17[1]起重机(R 4.0.3)
#>ncdf4 1.17 2019-10-23[1]起重机(R4.0.0)
#>支柱1.5.1 2021-03-05[1]起重机(R 4.0.4)
#>pkgconfig 2.0.3 2019-09-22[1]起重机(R 4.0.2)
#>purrr 0.3.4 2020-04-17[1]起重机(R 4.0.2)
#>光栅*3.4-5 2020-11-14[1]起重机(R4.0.3)
#>Rcpp 1.0.6 2021-01-15[1]起重机(R 4.0.3)
#>reprex 2.0.0 2021-04-02[1]起重机(R 4.0.5)
#>rgdal 1.5-23 2021-02-03[1]起重机(R 4.0.3)
#>rlang 0.4.10 2020-12-30[1]起重机(R 4.0.3)
#>R标记2.7 2021-02-19[1]起重机(R 4.0.4)
#>会议信息1.1.1 2018-11-05[1]CRAN(R 4.0.2)
#>sp*1.4-5 2021-01-10[1]起重机(R 4.0.3)
#>stringi 1.5.3 2020-09-09[1]起重机(R 4.0.3)
#>纵梁1.4.0 2019-02-10[1]起重机(R4.0.2)
#>样式器1.3.2 2020-02-23[1]起重机(R 4.0.2)
#>tibble 3.1.0 2021-02-25[1]起重机(R 4.0.4)
#>utf8 1.2.1 2021-03-12[1]起重机(R 4.0.5)
#>vctrs 0.3.6 2020-12-17[1]起重机(R 4.0.3)
#>带R 2.4.1 2021-01-26[1]起重机(R 4.0.3)
#>xfun 0.20 2021-01-06[1]起重机(R 4.0.3)
#>yaml 2.2.1 2020-02-01[1]起重机(R 4.0.2)
#> 
#>[1]C:/Users/David/Documents/lib/R
#>[2]C:/Program Files/R/R-4.0.2/library

您可以尝试使用
terra
软件包中的
rast
功能。它创建一个
光栅
对象。您可以稍后使用
光栅
功能将该对象转换为
光栅
对象。但是,您想要在
光栅
软件包中使用的大多数功能可能都可以通过使用
terra
包。所以只需呆在
类中就可以了

library(terra)
library(ncdf4)
library(raster)

r <- rast("test2.nc")
r
# class       : SpatRaster 
# dimensions  : 72, 120, 1  (nrow, ncol, nlyr)
# resolution  : 0.04166667, 0.04166667  (x, y)
# extent      : -115, -110, 30, 33  (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +datum=WGS84 +no_defs 
# source      : test2.nc 
# varname     : biomass (biomass) 
# name        :     biomass 
# unit        : Mg ha-1 y-1 
r2 <- raster(r)
r2
# class      : RasterLayer 
# dimensions : 72, 120, 8640  (nrow, ncol, ncell)
# resolution : 0.04166667, 0.04166667  (x, y)
# extent     : -115, -110, 30, 33  (xmin, xmax, ymin, ymax)
# crs        : +proj=longlat +datum=WGS84 +no_defs 
# source     : test2.nc 
# names      : biomass 
# zvar       : biomass 
图书馆(terra)
图书馆(ncdf4)
图书馆(光栅)
r尝试此操作(但在关闭连接之前进行此操作)


nc对于
raster
包,必须像这样使用
varname
参数

光栅(“test2.nc”,varname=“biomal”)

但由于示例文件只有一个变量,因此您可以执行以下操作:

光栅(“test2.nc”)

使用
terra
,如@www所示,您可以

rast(“test2.nc”)

rast(“test2.nc”、“biomative”)

如果有多个子数据集,您可以这样做


sds(“test2.nc”)

这似乎是一个数据问题。您是否检查了该文件是否为cf投诉?在此处上载可能会指向该问题
nc <- nc_open('test2.nc')
biomass.in2 <- ncvar_get(nc, "biomass")
dim(biomass.in2)
[1] 120  72

r <- raster(biomass.in2)
#-----------------
> r
class      : RasterLayer 
dimensions : 120, 72, 8640  (nrow, ncol, ncell)
resolution : 0.01388889, 0.008333333  (x, y)
extent     : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
crs        : NA 
source     : memory
names      : layer 
values     : 0, 7.65493  (min, max)

> summary(r@data@values)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
  0.000   0.000   1.844   2.142   3.865   7.655    1425 
> str(r)
Formal class 'RasterLayer' [package "raster"] with 12 slots
  ..@ file    :Formal class '.RasterFile' [package "raster"] with 13 slots
  .. .. ..@ name        : chr ""
  .. .. ..@ datanotation: chr "FLT4S"
  .. .. ..@ byteorder   : chr "little"
  .. .. ..@ nodatavalue : num -Inf
  .. .. ..@ NAchanged   : logi FALSE
  .. .. ..@ nbands      : int 1
  .. .. ..@ bandorder   : chr "BIL"
  .. .. ..@ offset      : int 0
  .. .. ..@ toptobottom : logi TRUE
  .. .. ..@ blockrows   : int 0
  .. .. ..@ blockcols   : int 0
  .. .. ..@ driver      : chr ""
  .. .. ..@ open        : logi FALSE
  ..@ data    :Formal class '.SingleLayerData' [package "raster"] with 13 slots
  .. .. ..@ values    : num [1:8640] 0 0 0 0 0 0 0 0 0 0 ...
  .. .. ..@ offset    : num 0
  .. .. ..@ gain      : num 1
  .. .. ..@ inmemory  : logi TRUE
  .. .. ..@ fromdisk  : logi FALSE
  .. .. ..@ isfactor  : logi FALSE
  .. .. ..@ attributes: list()
  .. .. ..@ haveminmax: logi TRUE
  .. .. ..@ min       : num 0
  .. .. ..@ max       : num 7.65
  .. .. ..@ band      : int 1
  .. .. ..@ unit      : chr ""
  .. .. ..@ names     : chr ""
  ..@ legend  :Formal class '.RasterLegend' [package "raster"] with 5 slots
  .. .. ..@ type      : chr(0) 
  .. .. ..@ values    : logi(0) 
  .. .. ..@ color     : logi(0) 
  .. .. ..@ names     : logi(0) 
  .. .. ..@ colortable: logi(0) 
  ..@ title   : chr(0) 
  ..@ extent  :Formal class 'Extent' [package "raster"] with 4 slots
  .. .. ..@ xmin: num 0
  .. .. ..@ xmax: num 1
  .. .. ..@ ymin: num 0
  .. .. ..@ ymax: num 1
  ..@ rotated : logi FALSE
  ..@ rotation:Formal class '.Rotation' [package "raster"] with 2 slots
  .. .. ..@ geotrans: num(0) 
  .. .. ..@ transfun:function ()  
  ..@ ncols   : int 72
  ..@ nrows   : int 120
  ..@ crs     :Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr NA
  ..@ history : list()
  ..@ z       : list()