Search code examples
rgdalterra

R terra package error in reading netcdf: No UNIDATA NC_GLOBAL:Conventions attribute


I am trying to read the below netcdf using terra package. The netcdf file contains data for 3 years. When I load the data, it gives me below warning which I never had before.

gdp <- terra::rast(file.path(dir_ls$base, 'v3.nc'))

Warning message:
In new_CppObject_xp(fields$.module, fields$.pointer, ...) :
   GDAL Message 1: No UNIDATA NC_GLOBAL:Conventions attribute

If I check the projection, it shows me NA

crs(gdp)

Even I try to project the data, the projection is still NA

 gdp <- terra::project(gdp, "epsg:4326")
 crs(gdp)
 NA

EDIT

 terra::gdal(lib="")
 gdal    proj    geos 
"2.2.3" "4.9.3" "3.7.1

The gdp file looks like

class       : SpatRaster 
dimensions  : 21600, 43200, 3  (nrow, ncol, nlyr)
resolution  : 0.008333333, 0.008333333  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326) 
source      : GDP_PPP_30arcsec_v3.nc 
varname     : GDP_PPP (Gross Domestic Production (GDP) (PPP)) 
names       :                             GDP_PPP_1,                             GDP_PPP_2,                             GDP_PPP_3 
unit        : constant 2011 international US dollar, constant 2011 
international US dollar, constant 2011 international US dollar

My session info is here:

R version 4.0.5 (2021-03-31)
    Platform: x86_64-pc-linux-gnu (64-bit)
    Running under: Ubuntu 18.04.5 LTS
    
    Matrix products: default
    BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
    LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
    
    locale:
    [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8       
    [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
    [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
    [10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     
    
    other attached packages:
    [1] rgdal_1.5-32      rgeos_0.5-9       raster_3.4-10     sp_1.4-5          geodata_0.4-9    
    [6] sf_0.9-8          terra_1.6-7       forcats_0.5.1     stringr_1.4.0     dplyr_1.0.6      
    [11] purrr_0.3.4       readr_1.4.0       tidyr_1.1.3       tibble_3.1.1      ggplot2_3.3.3    
    [16] tidyverse_1.3.1   data.table_1.14.0
    
    loaded via a namespace (and not attached):
    [1] tinytex_0.31       tidyselect_1.1.1   xfun_0.22          lattice_0.20-44   
    [5] haven_2.4.1        colorspace_2.0-1   vctrs_0.3.8        generics_0.1.0    
    [9] utf8_1.2.1         rlang_0.4.11       e1071_1.7-6        pillar_1.6.0      
    [13] glue_1.4.2         withr_2.4.2        DBI_1.1.1          dbplyr_2.1.1      
    [17] modelr_0.1.8       readxl_1.3.1       lifecycle_1.0.0    munsell_0.5.0     
    [21] gtable_0.3.0       cellranger_1.1.0   rvest_1.0.0        codetools_0.2-18  
    [25] class_7.3-19       fansi_0.4.2        broom_0.7.6        Rcpp_1.0.6        
    [29] KernSmooth_2.23-20 scales_1.1.1       backports_1.2.1    classInt_0.4-3    
    [33] jsonlite_1.7.2     fs_1.5.0           hms_1.0.0          stringi_1.6.1     
    [37] grid_4.0.5         cli_2.5.0          tools_4.0.5        magrittr_2.0.1    
    [41] proxy_0.4-25       crayon_1.4.1       pkgconfig_2.0.3    ellipsis_0.3.2    
    [45] xml2_1.3.2         reprex_2.0.0       lubridate_1.7.10   assertthat_0.2.1  
    [49] httr_1.4.2         rstudioapi_0.13    R6_2.5.0           units_0.7-1       
    [53] compiler_4.0.5    

Solution

  • As you show, gdp clearly has a defined coordinate reference system:

    coord. ref. : lon/lat WGS 84 (EPSG:4326) 
    

    and projecting it to that same crs again makes no sense.

    I do not think that terra would ever report a crs as NA. Since you are not showing your actual code and results, it is hard to say what goes wrong. But one red flag is that you have many packages loaded. If you start with a clean session (without any packages loaded) you should be able to do

    library(terra)
    r <- rast("filename")
    crs(r)
    

    And get a better result. Perhaps you load another package that hides the terra::crs method? In that case, you could do

    terra::crs(r)
    

    Like this:

    library(terra)
    r <- rast("GDP_per_capita_PPP_1990_2015_v2.nc")
    r
    #class       : SpatRaster 
    #dimensions  : 2160, 4320, 26  (nrow, ncol, nlyr)
    #resolution  : 0.08333333, 0.08333334  (x, y)
    #extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
    #coord. ref. : lon/lat WGS 84 
    #source      : GDP_per_capita_PPP_1990_2015_v2.nc 
    #varname     : GDP_per_capita_PPP (Gross Domestic Production (GDP) per capita (PPP)) 
    #names       :                           GDP_p~PPP_1,                           GDP_p~PPP_2,                           GDP_p~PPP_3,                           GDP_p~PPP_4,                           GDP_p~PPP_5,                           GDP_p~PPP_6, ... 
    #unit        : constant 2011 international US dollar, constant 2011 international US dollar, constant 2011 international US dollar, constant 2011 international US dollar, constant 2011 international US dollar, constant 2011 international US dollar, ... 
    
    crs(r, proj=T)
    #[1] "+proj=longlat +datum=WGS84 +no_defs"
    

    None of this has to do with the warning you are getting (I think you can ignore that warning).