Search code examples
rraster

Open .gz compressed ASCII raster in R


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!


Solution

  • 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