提取 bz2 文件并使用 R 打开 ncdf

extract bz2 file and open ncdf using R

我正在尝试从 ftp 打开一个 bz2 压缩的 netcdf 文件,但无法运行。非常感谢任何帮助。

我试过直接使用以下方式打开它:

url <- 'ftp://podaac-ftp.jpl.nasa.gov/allData/ghrsst/data/L4/GLOB/NCDC/AVHRR_OI/1982/001/19820101-NCDC-L4LRblend-GLOB-v01-fv02_0-AVHRR_OI.nc.bz2'
nc_open(url)

但这不起作用(我得到:Error in R_nc4_open: No such file or directory)。估计是压缩的缘故,所以先下载了文件再解压。

temp <- tempfile()
download.file(url,temp)
nc_open(bzfile(temp, '19820101-NCDC-L4LRblend-GLOB-v01-fv02_0-AVHRR_OI.nc', 'rb'))

但这也不起作用,我同时收到错误和警告:

Error in bzfile(temp, "19820101-NCDC-L4LRblend-GLOB-v01-fv02_0-AVHRR_OI.nc",  : 
  cannot open the connection
In addition: Warning message:
In bzfile(temp, "19820101-NCDC-L4LRblend-GLOB-v01-fv02_0-AVHRR_OI.nc",  :
  cannot open bzip2-ed file '/var/folders/hs/k9t_8wxs2hn48qq4vp_7xmgm0000gn/T//Rtmpv9UIBr/filef5659606aee', probable reason 'Invalid argument'

有什么想法吗?

谢谢

这似乎按照您希望的方式工作:

library(ncdf4)
library(R.utils)

URL <- "ftp://podaac-ftp.jpl.nasa.gov/allData/ghrsst/data/L4/GLOB/NCDC/AVHRR_OI/1982/001/19820101-NCDC-L4LRblend-GLOB-v01-fv02_0-AVHRR_OI.nc.bz2"
bzfil <- basename(URL)
if (!file.exists(bzfil)) download.file(URL, bzfil)

fil <- bunzip2(bzfil, overwrite=TRUE, remove=FALSE)

nc <- nc_open(fil)
summary(nc)

##             Length Class  Mode     
## filename    1      -none- character
## writable    1      -none- logical  
## id          1      -none- numeric  
## safemode    1      -none- logical  
## format      1      -none- character
## is_GMT      1      -none- logical  
## groups      1      -none- list     
## fqgn2Rindex 1      -none- list     
## ndims       1      -none- numeric  
## natts       1      -none- numeric  
## dim         3      -none- list     
## unlimdimid  1      -none- numeric  
## nvars       1      -none- numeric  
## var         4      -none- list