Search code examples
rmapsrasternetcdf

Raster plot of Netcdf climate data is rotated in R


I'm new to working with NetCDF files and I haven't been able to find the answer to my question elsewhere.

Daily precip data for year 2015 (from Gridmet): https://www.northwestknowledge.net/metdata/data/pr_2015.nc

My question: Maps are displaying with lat on the x axis and long on the y axis. How do I flip these axes? Futhermore, it also appears that values for latitude are inverted. (see linked map below)

    library(raster)
    library(ncdf4)
    nc15 <- nc_open("C:\\Users\\vsteen\\Desktop\\BorealToad\\Climate\\pr_2015.nc")        
    b <- brick("C:\\Users\\vsteen\\Desktop\\BorealToad\\Climate\\pr_2015.nc",varname="precipitation_amount")
    plot(b[[3]])



    print(nc15)
 1 variables (excluding dimension variables):
    float precipitation_amount[lat,lon,day]   
        units: mm
        description: Daily Accumulated Precipitation
        _FillValue: -32767
        esri_pe_string: GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]]
        coordinates: lon lat
        cell_methods: time: sum(interval: 24 hours)
        missing_value: -32767

 3 dimensions:
    lon  Size:1386
        units: degrees_east
        description: longitude
    lat  Size:585
        units: degrees_north
        description: latitude
    day  Size:365
        units: days since 1900-01-01 00:00:00
        calendar: gregorian
        description: days since 1900-01-01

9 global attributes:
    author: John Abatzoglou - University of Idaho, jabatzoglou@uidaho.edu
    date: 20 September 2016
    note1: The projection information for this file is: GCS WGS 1984.
    note2: Citation: Abatzoglou, J.T., 2013, Development of gridded surface meteorological data for ecological applications and modeling, International Journal of Climatology, DOI: 10.1002/joc.3413
    last_permanent_slice: 365
    last_early_slice: 365
    note3: Data in slices after last_permanent_slice (1-based) are considered provisional and subject to change with subsequent updates
    note4: Data in slices after last_early_slice (1-based) are considered early and subject to change with subsequent updates
    note5: Days correspond approximately to calendar days ending at midnight, Mountain Standard Time (7 UTC the next calendar day)

    str(nc15$dim)

    List of 3
    $ lon:List of 10
    ..$ name         : chr "lon"
    ..$ len          : int 1386
    ..$ unlim        : logi FALSE
    ..$ group_index  : int 1
    ..$ group_id     : int 65536
    ..$ id           : int 0
    ..$ dimvarid     :List of 5
    .. ..$ id         : int 0
    .. ..$ group_index: int 1
    .. ..$ group_id   : int 65536
    .. ..$ list_index : num -1
    .. ..$ isdimvar   : logi TRUE
    .. ..- attr(*, "class")= chr "ncid4"
    ..$ units        : chr "degrees_east"
    ..$ vals         : num [1:1386(1d)] -125 -125 -125 -125 -125 ...
    ..$ create_dimvar: logi TRUE
    ..- attr(*, "class")= chr "ncdim4"
   $ lat:List of 10
    ..$ name         : chr "lat"
    ..$ len          : int 585
    ..$ unlim        : logi FALSE
    ..$ group_index  : int 1
    ..$ group_id     : int 65536
    ..$ id           : int 1
    ..$ dimvarid     :List of 5
    .. ..$ id         : int 1
    .. ..$ group_index: int 1
    .. ..$ group_id   : int 65536
    .. ..$ list_index : num -1
    .. ..$ isdimvar   : logi TRUE
    .. ..- attr(*, "class")= chr "ncid4"
    ..$ units        : chr "degrees_north"
    ..$ vals         : num [1:585(1d)] 49.4 49.4 49.3 49.3 49.2 ...
    ..$ create_dimvar: logi TRUE
    ..- attr(*, "class")= chr "ncdim4"
   $ day:List of 11
    ..$ name         : chr "day"
    ..$ len          : int 365
    ..$ unlim        : logi FALSE
    ..$ group_index  : int 1
    ..$ group_id     : int 65536
    ..$ id           : int 2
    ..$ dimvarid     :List of 5
    .. ..$ id         : int 2
    .. ..$ group_index: int 1
    .. ..$ group_id   : int 65536
    .. ..$ list_index : num -1
    .. ..$ isdimvar   : logi TRUE
    .. ..- attr(*, "class")= chr "ncid4"
    ..$ units        : chr "days since 1900-01-01 00:00:00"
    ..$ calendar     : chr "gregorian"
    ..$ vals         : num [1:365(1d)] 42003 42004 42005 42006 42007 ...
    ..$ create_dimvar: logi TRUE
    ..- attr(*, "class")= chr "ncdim4"
  >

Thanks in advance for any help. It will be much appreciated!

Rotated U.S. precipitation map


Solution

  • you can use the combination of transpose and flip from raster package:

    s <- stack("pr_2015.nc", varname="precipitation_amount")
    
    s2 <- t(flip(s, direction='y' ))