How to convert a rotated NetCDF back to a normal lat/lon grid with terra in R?

71 views Asked by At

I have a climate projection file that covers North America but the extent is not in the normal lat/long. I need to convert it to normal lat/lon coordinates (-180 to 180 for lon and -90 to 90 for lat). I was thinking of shifting the extent but I dont think that is right. What is the best way to convert the lat/log to the normal one using terra in R.

data <- terra::rast("snw_NAM-44_CCCma-CanESM2_historical-r1_r1i1p1_CCCma-CanRCM4_r2_mon_195001-195012.nc")
data
#> class       : SpatRaster 
#> dimensions  : 130, 155, 12  (nrow, ncol, nlyr)
#> resolution  : 0.4400001, 0.44  (x, y)
#> extent      : -34.1, 34.1, -28.82, 28.38  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) 
#> source      : snw_NAM-44_CCCma-CanESM2_historical-r1_r1i1p1_CCCma-CanRCM4_r2_mon_195001-195012.nc:snw 
#> varname     : snw (Surface Snow Amount) 
#> names       :  snw_1,  snw_2,  snw_3,  snw_4,  snw_5,  snw_6, ... 
#> unit        : kg m-2, kg m-2, kg m-2, kg m-2, kg m-2, kg m-2, ... 
#> time (days) : 1954-08-08 to 1955-07-08

This is the details of the nc file which has a rotated pole is:

ncdf4::nc_open("snw_NAM-44_CCCma-CanESM2_historical-r1_r1i1p1_CCCma-CanRCM4_r2_mon_195001-195012.nc")
#> File snw_NAM-44_CCCma-CanESM2_historical-r1_r1i1p1_CCCma-CanRCM4_r2_mon_195001-195012.nc (NC_FORMAT_CLASSIC):
#> 
#>      5 variables (excluding dimension variables):
#>         double time_bnds[bnds,time]   
#>         double lon[rlon,rlat]   
#>             long_name: longitude
#>             units: degrees_east
#>         double lat[rlon,rlat]   
#>             long_name: latitude
#>             units: degrees_north
#>         char rotated_pole[]   
#>             grid_mapping_name: rotated_latitude_longitude
#>             grid_north_pole_longitude: 83
#>             grid_north_pole_latitude: 42.5
#>         float snw[rlon,rlat,time]   
#>             long_name: Surface Snow Amount
#>             standard_name: surface_snow_amount
#>             units: kg m-2
#>             cell_methods: time:mean (interval:1200 seconds)
#>             coordinates: lon lat
#>             grid_mapping: rotated_pole
#>             _FillValue: 1.00000002004088e+20
#> 
#>      4 dimensions:
#>         time  Size:12   *** is unlimited *** 
#>             long_name: time
#>             standard_name: time
#>             units: days since 1949-12-01
#>             axis: T
#>             calendar: 365_day
#>             bounds: time_bnds
#>         rlon  Size:155 
#>             long_name: longitude in rotated pole grid
#>             units: degrees
#>             axis: X
#>             standard_name: grid_longitude
#>         rlat  Size:130 
#>             long_name: latitude in rotated pole grid
#>             units: degrees
#>             axis: Y
#>             standard_name: grid_latitude
#>         bnds  Size:2 (no dimvar)
#> 
#>     26 global attributes:
#>         title: CanRCM4 model output prepared for CanSISE Project
#>         institution: CCCma (Canadian Centre for Climate Modelling and Analysis, Victoria, BC, Canada)
#>         institute_id: CCCma
#>         experiment: CanSISE downscaling run driven by CCCma-CanESM2 eia-001
#>         experiment_id: historical-r1
#>         driving_experiment: CCCma-CanESM2, historical-r1, r1i1p1
#>         driving_model_id: CCCma-CanESM2
#>         driving_experiment_name: historical-r1
#>         driving_model_ensemble_member: r1i1p1
#>         realization: 1
#>         initialization_method: 1
#>         physics_version: 1
#>         forcing: GHG,Oz,SA,BC,OC,LU,Vl (GHG includes CO2,CH4,N2O,CFC11,effective CFC12)
#>         project_id: CanSISE
#>         model_id: CCCma-CanRCM4
#>         CORDEX_domain: NAM-44
#>         rcm_version_id: r2
#>         frequency: mon
#>         product: output
#>         CCCma_runid: nam44_v001_eia-001
#>         Conventions: CF-1.4
#>         creation_date: 2016-10-04-T17:30:32Z
#>         contact: [email protected]
#>         references: http://www.cccma.ec.gc.ca/models
#>         history: created: 2016-10-04 17:31:06 by rcm2nc
1

There are 1 answers

7
dimfalk On BEST ANSWER

Glad the gdalwarp idea worked out. Let me provide an answer then so that the crucial hint is not hidden somewhere in the comments.

adm <- geodata::gadm("Canada", path = tempdir())

# gdalwarp
sf::gdal_utils(util = "warp", 
               source = "snw_NAM-44_CCCma-CanESM2_historical-r1_r1i1p1_CCCma-CanRCM4_r2_mon_195001-195012.nc", 
               destination = "r_wgs84.nc", 
               options = c("-t_srs", "EPSG:4326"))

# read warped data
r <- terra::rast("r_wgs84.nc")
r
#> class       : SpatRaster 
#> dimensions  : 113, 262, 12  (nrow, ncol, nlyr)
#> resolution  : 0.5647745, 0.5642747  (x, y)
#> extent      : -170.9646, -22.99372, 12.11698, 75.88002  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) 
#> sources     : r_wgs84.nc:Band1  
#>               r_wgs84.nc:Band2  
#>               r_wgs84.nc:Band3  
#>               ... and 9 more source(s)
#> varnames    : Band1 (Surface Snow Amount) 
#>               Band2 (Surface Snow Amount) 
#>               Band3 (Surface Snow Amount) 
#>               ...
#> names       :  Band1,  Band2,  Band3,  Band4,  Band5,  Band6, ... 
#> unit        : kg m-2, kg m-2, kg m-2, kg m-2, kg m-2, kg m-2, ...

# inspect and verify georeference
terra::plot(r[[1]])
terra::plot(adm, add = TRUE)

Created on 2024-03-26 with reprex v2.1.0