Terra包中的rast()不会从NetCDF文件中提取空间信息:[rast]未知范围

mctunoxg  于 2023-03-15  发布在  Etcd
关注(0)|答案(1)|浏览(409)

我一直尝试从terra加载带有rast函数的NetCDF,但收到以下警告消息:

> SM_rast <- rast("Soil_moisture_v3.nc", "soil_moisture") 
Warning message:
[rast] unknown extent

对应的SpatRaster没有crs,分辨率错误,坐标错误:

> SM_rast
class       : SpatRaster 
dimensions  : 2160, 4320, 172  (nrow, ncol, nlyr)
resolution  : 1, 1  (x, y)
extent      : 0, 4320, 0, 2160  (xmin, xmax, ymin, ymax)
coord. ref. :  
source      : Soil_moisture_v3.nc 
names       : Soil_~_v3_1, Soil_~_v3_2, Soil_~_v3_3, Soil_~_v3_4, Soil_~_v3_5, Soil_~_v3_6, ...

您可以下载NetCDF文件here
根据this question的答案,我尝试将drivers="NETCDF"添加到rast()函数中,但它甚至无法读取文件:

> SM_rast <- rast("Soil_moisture_v3.nc", "soil_moisture", drivers="NETCDF") 
Error: [rast] cannot open this file as a SpatRaster: Soil_moisture_v3.nc
In addition: Warning message:
`Soil_moisture_v3.nc' not recognized as a supported file format. (GDAL error 4)

将SpatRaster转换为数据框时,所有包含NA的单元格均丢失,并且坐标列错误:

> df_rast <- terra::as.data.frame(SM_rast[[1]], xy = TRUE)
> summary(df_rast)
       x                y          Soil_moisture_v3_1
 Min.   :   2.5   Min.   : 414.5   Min.   :0.02000   
 1st Qu.:1525.5   1st Qu.: 937.5   1st Qu.:0.07621   
 Median :2403.5   Median :1240.5   Median :0.17518   
 Mean   :2340.6   Mean   :1197.1   Mean   :0.20558   
 3rd Qu.:2841.5   3rd Qu.:1440.5   3rd Qu.:0.30225   
 Max.   :4317.5   Max.   :1960.5   Max.   :0.91196

如果我使用raster包中的brick()执行同样的操作,一切都正常:

> SM_brick <- brick("data-raw/TROPOMI/Soil_moisture_v3.nc", varname = "soil_moisture")
> SM_brick
class      : RasterBrick 
dimensions : 2160, 4320, 9331200, 172  (nrow, ncol, ncell, nlayers)
resolution : 0.0833333, 0.0833333  (x, y)
extent     : -180, 179.9998, -90, 89.99993  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : Soil_moisture_v3.nc 
names      : X2018.02.02, X2018.02.10, X2018.02.18, X2018.02.26, X2018.03.06, X2018.03.14, X2018.03.22, X2018.03.30, X2018.04.07, X2018.04.15, X2018.04.23, X2018.05.01, X2018.05.09, X2018.05.17, X2018.05.25, ... 
Date       : 2018-02-02, 2021-10-24 (min, max)
varname    : soil_moisture 

> df_brick <- terra::as.data.frame(SM_brick[[1]], xy = TRUE)
> summary(df_brick)
       x                    y              X2018.02.02     
 Min.   :-179.95833   Min.   :-89.95834   Min.   :0        
 1st Qu.: -89.97920   1st Qu.:-44.97919   1st Qu.:0        
 Median :  -0.00008   Median : -0.00004   Median :0        
 Mean   :  -0.00008   Mean   : -0.00004   Mean   :0        
 3rd Qu.:  89.97905   3rd Qu.: 44.97911   3rd Qu.:0        
 Max.   : 179.95818   Max.   : 89.95826   Max.   :1        
                                          NA's   :8181031

我仍然需要使用terra,因为它速度更快,并且具有raster中没有的更多有用的功能。

mklgxw1f

mklgxw1f1#

当我这样做的时候,我觉得一切都很好:

library(terra)
# terra 1.7.20
rast("Soil_moisture_v3.nc")
#class       : SpatRaster 
#dimensions  : 2160, 4320, 172  (nrow, ncol, nlyr)
#resolution  : 0.0833333, 0.0833333  (x, y)
#extent      : -180, 179.9998, -90, 89.99993  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#source      : Soil_moisture_v3.nc 
#varname     : soil_moisture 
#names       : soil_~ure_1, soil_~ure_2, soil_~ure_3, soil_~ure_4, soil_~ure_5, soil_~ure_6, ... 
#unit        :       m3/m3,       m3/m3,       m3/m3,       m3/m3,       m3/m3,       m3/m3, ... 
#time (days) : 2018-02-02 to 2021-10-24 

gdal()
#[1] "3.6.2"

g <- gdal(drivers=T)
g[g$name=="netCDF", ]
#     name   type        can   vsi                  long.name
#88 netCDF raster read/write FALSE Network Common Data Format

如果您没有看到这一点,那可能是因为您有另一个版本的terra,和/或GDAL,和/或因为您的操作系统和您安装“terra”的方式。
从你的压缩文件中我可以看到你使用的是Mac。问题可能与CRAN上的二进制版本的GDAL有关。你可以尝试从R-Universe安装,而不是使用

install.packages('terra', repos='https://rspatial.r-universe.dev')

或者,如果不起作用,从源代码安装。请参阅instructions
还有一种变通办法:

library(raster)
# add zero to force the values out of the file.
# that takes a while
b <- brick("Soil_moisture_v3.nc") + 0
r <- rast(b)

相关问题