0

I'm trying to open a bz2 compressed netcdf file from ftp, but can't get it to work. Any help is much appreciated.

I've tried opening it directly using:

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)

but that doesn't work (I get: Error in R_nc4_open: No such file or directory). I guess because of the compression, so I tried downloading the file first and decompress it.

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

but that doesn't work either and I get both an error and a warning:

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'

any ideas?

Thanks

Lukas
  • 655
  • 8
  • 20

1 Answers1

2

This seems to work the way you want it to:

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 
hrbrmstr
  • 77,368
  • 11
  • 139
  • 205