1

I am getting a 'could not find the requested var' error, even though the variable name is valid - it works with other netcdf libraries - including NCO, the R netcdf4 library, and Panoply:

This example uses a file called test2.nc:

This works as expected:

library(ncdf4)
nc <- nc_open('test2.nc')
v <- ncvar_get(nc, 'biomass')
image(v)
nc_close(nc)

This does not work:

library(raster)
r <- raster('test2.nc', 'biomass')
#> Loading required namespace: ncdf4
#> [1] "vobjtovarid4: error #F: I could not find the requsted var (or dimvar) in the file!"
#> [1] "var (or dimvar) name: crs"
#> [1] "file name: test2.nc"
#> Warning in .rasterObjectFromCDF(x, type = objecttype, band = band, ...): NAs
#> introduced by coercion
#> Error in if (band > nbands(r)) {: missing value where TRUE/FALSE needed

Created on 2021-06-11 by the reprex package (v2.0.0)

Session info
sessioninfo::session_info()
#> - Session info ---------------------------------------------------------------
#>  setting  value                       
#>  version  R version 4.0.2 (2020-06-22)
#>  os       Windows 10 x64              
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  English_United States.1252  
#>  ctype    English_United States.1252  
#>  tz       America/Phoenix             
#>  date     2021-06-11                  
#> 
#> - Packages -------------------------------------------------------------------
#>  package     * version date       lib source        
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.2)
#>  backports     1.2.1   2020-12-09 [1] CRAN (R 4.0.3)
#>  cli           2.3.1   2021-02-23 [1] CRAN (R 4.0.4)
#>  codetools     0.2-16  2018-12-24 [2] CRAN (R 4.0.2)
#>  crayon        1.4.1   2021-02-08 [1] CRAN (R 4.0.2)
#>  digest        0.6.27  2020-10-24 [1] CRAN (R 4.0.3)
#>  ellipsis      0.3.1   2020-05-15 [1] CRAN (R 4.0.2)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.2)
#>  fansi         0.4.2   2021-01-15 [1] CRAN (R 4.0.3)
#>  fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.3)
#>  glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.2)
#>  highr         0.8     2019-03-20 [1] CRAN (R 4.0.2)
#>  htmltools     0.5.1.1 2021-01-22 [1] CRAN (R 4.0.3)
#>  knitr         1.31    2021-01-27 [1] CRAN (R 4.0.3)
#>  lattice       0.20-41 2020-04-02 [2] CRAN (R 4.0.2)
#>  lifecycle     1.0.0   2021-02-15 [1] CRAN (R 4.0.4)
#>  magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.0.3)
#>  ncdf4         1.17    2019-10-23 [1] CRAN (R 4.0.0)
#>  pillar        1.5.1   2021-03-05 [1] CRAN (R 4.0.4)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.0.2)
#>  purrr         0.3.4   2020-04-17 [1] CRAN (R 4.0.2)
#>  raster      * 3.4-5   2020-11-14 [1] CRAN (R 4.0.3)
#>  Rcpp          1.0.6   2021-01-15 [1] CRAN (R 4.0.3)
#>  reprex        2.0.0   2021-04-02 [1] CRAN (R 4.0.5)
#>  rgdal         1.5-23  2021-02-03 [1] CRAN (R 4.0.3)
#>  rlang         0.4.10  2020-12-30 [1] CRAN (R 4.0.3)
#>  rmarkdown     2.7     2021-02-19 [1] CRAN (R 4.0.4)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.2)
#>  sp          * 1.4-5   2021-01-10 [1] CRAN (R 4.0.3)
#>  stringi       1.5.3   2020-09-09 [1] CRAN (R 4.0.3)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.0.2)
#>  styler        1.3.2   2020-02-23 [1] CRAN (R 4.0.2)
#>  tibble        3.1.0   2021-02-25 [1] CRAN (R 4.0.4)
#>  utf8          1.2.1   2021-03-12 [1] CRAN (R 4.0.5)
#>  vctrs         0.3.6   2020-12-17 [1] CRAN (R 4.0.3)
#>  withr         2.4.1   2021-01-26 [1] CRAN (R 4.0.3)
#>  xfun          0.20    2021-01-06 [1] CRAN (R 4.0.3)
#>  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.2)
#> 
#> [1] C:/Users/David/Documents/lib/R
#> [2] C:/Program Files/R/R-4.0.2/library
David LeBauer
  • 31,011
  • 31
  • 115
  • 189
  • This appears to be a data problem. Have you checked if the file is cf complaint? Uploading here is likely to point towards the problem https://cfconventions.org/compliance-checker.html – Robert Wilson Jun 12 '21 at 14:18

3 Answers3

3

You can try the rast function from the terra package. It creates a SpatRaster object. You can convert the object to a raster later using the raster function. However, most of the functionalities you want to use in the raster package can probably be achieved by using the terra package. So just stay in the SpatRaster class is probably fine.

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 
www
  • 38,575
  • 12
  • 48
  • 84
2

With the raster package, you have to use the varname argument like this

raster("test2.nc", varname="biomass")

But since the example file only has a single variable, you can just do:

raster("test2.nc")

With terra, as shown by @www, you can do

rast("test2.nc")

or

rast("test2.nc", "biomass")

If there are multiple sub-datasets you might do

sds("test2.nc")

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
0

Try this (but do it before you close the connection).

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()
IRTFM
  • 258,963
  • 21
  • 364
  • 487