0

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!

maycca
  • 3,848
  • 5
  • 36
  • 67

2 Answers2

1

Using the package R.utils to uncompress the .gz file, you can do something like that:

ras_path <- "<YOUR_PATH>\\grids_germany_monthly_precipitation_188101.asc.gz"

R.utils::gunzip(ras_path, remove = FALSE)

new_ras_path <- gsub("\\.gz$", "", ras_path)

raster::raster(new_ras_path)
#> class      : RasterLayer 
#> dimensions : 866, 654, 566364  (nrow, ncol, ncell)
#> resolution : 1000, 1000  (x, y)
#> extent     : 3280415, 3934415, 5237501, 6103501  (xmin, xmax, ymin, ymax)
#> crs        : NA 
#> source     : grids_germany_monthly_precipitation_188101.asc 
#> names      : grids_germany_monthly_precipitation_188101 
#> values     : -2147483648, 2147483647  (min, max)

Created on 2022-07-14 by the reprex package (v2.0.1)

bretauv
  • 7,756
  • 2
  • 20
  • 57
1

See this link for a general discussion of decompressing gz files.

In this case, you can directly read the files you point at like this (I am using the "terra" package, the replacement of the "raster" package)

library(terra)
url <- "https://opendata.dwd.de/climate_environment/CDC/grids_germany/monthly/precipitation/01_Jan/grids_germany_monthly_precipitation_188101.asc.gz"
x <- rast(paste0("/vsigzip//vsicurl/", url))

Or first download the file

f <- basename(url)
download.file(url, f, mode="wb")
y <- rast(paste0("/vsigzip/", f))

Or first decompress the downloaded gz file (and perhaps remove it with remove=TRUE)

R.utils::gunzip(f, remove=FALSE)
ff <- gsub(".gz$", "", f)
z <- rast(ff)

These ascii files do not store the coordinate reference system, so you need to set it yourself.

crs(z) <- "EPSG:31467"
z
#class       : SpatRaster 
#dimensions  : 866, 654, 1  (nrow, ncol, nlyr)
#resolution  : 1000, 1000  (x, y)
#extent      : 3280415, 3934415, 5237501, 6103501  (xmin, xmax, ymin, ymax)
#coord. ref. : DHDN / 3-degree Gauss-Kruger zone 3 (EPSG:31467) 
#source      : grids_germany_monthly_precipitation_188101.asc 
#name        : grids_germany_monthly_precipitation_188101 
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • This is excellent, thank you so much! also, thank you for including `terra` package, it is way faster than the `raster` one. Is the `"EPSG:31467"` typical for German weather data, or is it just a randomly selected one? – maycca Jul 18 '22 at 03:34
  • I took the coordinate reference system from the metadata (BESCHEIBUNG / DESCRIPTION) file in https://opendata.dwd.de/climate_environment/CDC/grids_germany/monthly:/precipitation/ – Robert Hijmans Jul 18 '22 at 04:15