I am trying to open and process in R the raster data stored as asc.gz
file: ASCII and .gz compressed, as weather data here. I need to extract the weather data for each location for every year and every month = many values, so I wish to automate the process and not upzipp the files manually.
I have followed the previous suggestions here and here how to open a .gz
compressed file, and then the raster one. But I get errors like:
Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", :
Cannot create a RasterLayer object from this file.
unable to find an inherited method for function ‘raster’ for signature ‘"gzfile"’
How to succesfully open the .gz compressed ASCII file in R?
Until now I have tried (not working):
ras_path = paste(myPath, 'rawData/DeutschWetter/01_Jan', "200501asc.gz", sep = "/")
connect=gzfile(ras_path)
r=raster::raster(connect)
Using the temp
folder:
temp <- tempfile()
unzip(temp)
r=raster::raster(ras_path)
zipd = tempdir()
unzip(temp, exdir=zipd)
myRaster = raster(ras_path)
Trying to read raster as a raw vector:
# create connection to a gz file
con <- gzfile(ras_path, open = "rb")
# read data from this connection into a raw vector
myras.raw <- readBin(con, what = "raw", n = 1e10)
# read this raw vector
myras <- raster(con)
I am using R version 4.1.1 (2021-08-10).
Thanks for help!