0

I'm accessing ncdf files directly from a website [here][1] into my RMarkdown. When I try to read the file using the nc_open functions as in the code below, I get the error 'Passed a filename that is NOT a string of characters!' Any idea how I can solve this? ps: I even tried uncompressing the files with the gzcon function but the result is the same when I try to read the data. Thanks for your help! Kami

library(httr)
library(ncdf4)
nc<-GET("https://crudata.uea.ac.uk/cru/data/hrg/cru_ts_4.05/cruts.2103051243.v4.05/pre/cru_ts4.05.2011.2020.pre.dat.nc.gz")
cru_nc<-nc_open(nc)
Kami
  • 3
  • 2

2 Answers2

0

Is this a mode="w" Vs mode="wb" issue. I've had this with files before. No experience of ncdf4.

Not sure if you can pass mode="wb" to get but does

file.download(yourUrl, mode="wb")

Work / help


Edit:

Ah. Other thing is you are storing the object as an object (nc) but nc_open wants to open a file.

I think you need to save the object locally (unless nc_open can just take the URL) and then open it? Possibly after unzipping.

CALUM Polwart
  • 497
  • 3
  • 5
0

OK here is the fill answer:

library(httr)
library(ncdf4)
library(R.utils)
url <- "https://crudata.uea.ac.uk/cru/data/hrg/cru_ts_4.05/cruts.2103051243.v4.05/pre/cru_ts4.05.2011.2020.pre.dat.nc.gz"
filename <- "/tmp/file.nc.gz"

# Download the file and store it as a temp file
download.file(url, filename, mode = "wb")

# Unzip the temp file
gunzip(filename)

# The unzipped filename drops the .gz
unzip_filename <- "/tmp/file.nc"

# You can now open the unzipped file with its **filename** rather than the object
cru_nc<-nc_open(unzip_filename)
CALUM Polwart
  • 497
  • 3
  • 5