Search code examples
rr-rasternetcdf4terra

R terra/raster: uploading a netcdf file alters resolution


I have netcdf files (GPM IMERG precipitation) which are supposed to be at0.1x0.1 resolution like in the red box at the bottom here:

enter image description here

Well the pixel size is not exactly 0.1 on the image, but at least its height and width are the same.

However when uploading the netcdf via terra:: or raster:: the resolution slightly changes:

class       : SpatRaster 
dimensions  : 185, 97, 1  (nrow, ncol, nlyr)
resolution  : 0.09999998, 0.1000001  (x, y)
extent      : 34.2, 43.9, 62.3, 80.80002  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 
source      : _(1).nc:precipitationCal 
varname     : precipitationCal (Daily accumulated precipitation (combined microwave-IR) estimate) 
name        : precipitationCal 
unit        :               mm 
time (days) : 2000-06-04 

Any solution on how to transform the rasters in R back to their original resolution would be welcomed. Btw, does this has to do with rounding coordinates of the extent in R packages?


Solution

  • Can you point to that file? From what I can see, either the extent or the resolution shown on the website is wrong. Based on these numbers, it should be:

    print((43.9000038 - 34.20000855) / 97, digits=10)
    #[1] 0.09999995103
    

    Perhaps for the website the resolution was computed using the center of the cells, so that you get

    print((43.95 - 34.15) / 97, digits=10)
    #[1] 0.1010309582
    

    It is hard to be sure without the file. But I would put my money on the website being wrong.


    With your file I also see this

    f <- "__1_.nc"
    r <- rast(f)
    res(r)
    #[1] 0.09999998 0.10000007
    

    But that is correct in the sense that that does reflect the x and y coordinates written to the file. The x and y coordinates for the cells that are provided are not strictly regular (but nearly so). For example, here are the first and last three y coordinates:

    62.3500022888184 62.4500083923340 62.5499992370605 ...  
    80.5500030517578 80.6500091552734 80.7500152587891
    

    From the file alone I cannot say whether these small decimals are intentional or stem from sloppiness, I assume the latter and in that case it indeed seems reasonable to round the extent:

    ext(r) <- round(ext(r), 4)
    r
    #class       : SpatRaster 
    #dimensions  : 185, 97, 1  (nrow, ncol, nlyr)
    #resolution  : 0.1, 0.1  (x, y)
    #extent      : 34.2, 43.9, 62.3, 80.8  (xmin, xmax, ymin, ymax)
    #coord. ref. : lon/lat WGS 84 
    #source      : __1_.nc:precipitationCal 
    #varname     : precipitationCal (Daily accumulated precipitation #(combined microwave-IR) estimate) 
    #name        : precipitationCal 
    #unit        :               mm 
    #time (days) : 2000-06-04