Search code examples
rasciirasterterra

.asc conversion to .tif file(raster)


I am not familiar with .asc file and trying to convert it to raster. Extended Reconstructed Sea Surface Temperature (ERSST) dataset was downloaded from ERSST repo

There is some info for the dataset in its README file:

filename convention:

(1) ERSST 

ersst.VERSION.yyyy.asc
yyyy=year

Units: isst=100 degree C
Missing value=-9999

(2) grid information
latitude: -88.0, increase northward per 2 degree, to +88.0
longitude:  0.0, increase eastward per 2 degree, to 358.0

Here is what I have tried:

url <- "https://www.ncei.noaa.gov/pub/data/cmb/ersst/v5/ascii/ersst.v5.1854.asc"
f <- basename(url)
download.file(url, f, mode="wb")

a<- terra::rast(f)
plot(a)

I got warning messages when use a=terra::rast(f),for example, and plot(a):

1: Failed to read scanline 672. (GDAL error 3) 
2: ersst.v5.1854.asc, band 1: IReadBlock failed at X offset 0, Y offset 672: Failed to read scanline 672. (GDAL error 1) 
3: [plot] SpatRaster has no cell values

Not sure if it is something related to the projection system. If it is the case, which projection reference I should use for this global dataset?


Solution

  • The asc data use a non-standard format. It would be easy enough to read if you had to, but in this case you can use the NetCDF files instead; and that is much simpler:

    Download example file

    url <- "https://www.ncei.noaa.gov/pub/data/cmb/ersst/v5/netcdf/ersst.v5.185401.nc"
    f <- basename(url)
    download.file(url, f, mode="wb")
    

    Open it as a SpatRaster

    library(terra)
    x <- rast(f)
    x
    #class       : SpatRaster 
    #dimensions  : 89, 180, 2  (nrow, ncol, nlyr)
    #resolution  : 2, 2  (x, y)
    #extent      : -1, 359, -89, 89  (xmin, xmax, ymin, ymax)
    #coord. ref. : lon/lat WGS 84 
    #sources     : ersst.v5.185401.nc:sst  
    #              ersst.v5.185401.nc:ssta  
    #varnames    : sst (Extended reconstructed sea surface temperature) 
    #              ssta (Extended reconstructed SST anomalies) 
    #names       : sst_lev=0, ssta_lev=0 
    #unit        :  degree_C,   degree_C 
    #time (raw)  : 0 
    

    Note that longitude goes from -1 to 359. You can use use rotate to get more standard longitudes; and you can write it to a GeoTiff at the same time.

    rotate(x, filename="ersst.v5.185401.tif")
    
    #class       : SpatRaster 
    #dimensions  : 89, 180, 2  (nrow, ncol, nlyr)
    #resolution  : 2, 2  (x, y)
    #extent      : -181, 179, -89, 89  (xmin, xmax, ymin, ymax)
    #coord. ref. : lon/lat WGS 84 
    #source      : ersst.v5.185401.tif 
    #names       : sst_lev=0, ssta_lev=0 
    #min values  :  -1.80000,  -4.958618 
    #max values  :  32.09937,   2.676313 
    #time (raw)  : 0 
    

    But note that it still is not what it ought to be (-180, 180). You could use resample to fix that.