Hi,

I would like to un-compress a gz or a bz2 file to current working directory.
I am able to open the connection in a variety of ways, but than cannot
access the *.*nc file inside.
Is there a way of accessing the netcdf wiathout uncompressing?
(I am using windows8, R 3.0.3)

An example of what I tried so far is in the end.

Thank you very much in advance,
Any help would be greatly appreciated,
Cheers,
M.


Here it is what I tried:

EXAMPLE:

# File URL: (Example from globcolour):
url2 = c("
ftp://globcolour_data:fgh...@ftp.acri.fr/EURO_4KM/RAN/CHL1/MERGED/GSM/1997/MONTHLY/L3m_19970901-19970930__EURO_4_GSM-SWF_CHL1_MO_00.nc.gz
")
#file name:
fname2="L3m_19970901-19970930__EURO_4_GSM-SWF_CHL1_MO_00.nc.gz"

# This command downloads the file to working directory (note the first is a
large file):
download.file(url2, fname2, method = "auto", quiet = FALSE, mode="wb",
cacheOK = TRUE)

# can we access the url file, without downloading it locally? Does not work.

# using raster:
require(raster)
raster(url2)

# using ncdf:
require(ncdf)
open.nc(url2)


# Alternatives to uncompress manually, from R:

# 1. to access the file contents, we need to open the connection. A tried
several alternatives:
untar(fname2)
# Error in rawToChar(block[seq_len(ns)]) :
#   embedded nul in string:
'CDF\001\0\0\0\0\0\0\0\n\0\0\0\003\0\0\0\003lon\0\0\0\034
\0\0\0\003lat\0\0\0\016\020\0\0\0\004time\0\0\0\001\0\0\0\f\0\0\0\027\0\0\0\vConventions\0\0\0\0\002\0\0\0\006CF-1.0\0\0\0\0\0\005titl'

# 2. Using gzfile:
mycon1 <- gzfile(fname2, open="rb") #rb goes for open binary files
mycon1

require(raster)
raster(mycon1)
# Error in (function (classes, fdef, mtable)  :
#   unable to find an inherited method for function 'raster' for signature
'"gzfile"'
open.ncdf(mycon1)
# Error in open.ncdf(mycon1) : Error in open.ncdf trying to open file 12


# 3. Using gzcon:
mycon1 <- gzcon(url(url2))
raw <- textConnection(readLines(mycon1 ))
> warnings()
# Warning messages:
# 1: line 1 appears to contain an embedded nul
# 2: line 2 appears to contain an embedded nul (...)


# 4. Another alternative, using RCurl:
library(RCurl)
#?getBinaryURL
#Requires Rcompression that "package 'Rcompression' is not available (for R
version 3.0.3 RC)"
#gunzip(fname)
#getBinaryURL(url2)


# Still, if I download the file and unzip using manually to the windows
explorer (using 7zip in this case), I can open and plot it easily:
chl=raster("L3m_19970901-19970930__EURO_4_GSM-SWF_CHL1_MO_00.nc")
plot(chl)

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to