Error in resampling with Rasters.jl

Yeah, this is a hard thing that could be easier.

First NetCDF projections are a mess. NCDatasets.jl does not load the native CF standards projection, probably because its arcane and pretty hard to implement. The “crs / spatial_ref” fields you are retrieving is not a CF standard so I haven’t written anything specific to get it automatically (but we easily could if this is common enough, please make a github issue). The dimension variables are often projected to EPSG(4326) already even if the underlying projection is different, making things even harder to get right when you cant automatically detect the crs.

Second, NCDatasets.jl uses missing as the missing value by default (See https://github.com/Alexander-Barth/NCDatasets.jl/issues/132), and GDAL (that does the resampling in this case) cannot handle missing. One day I will automate replacing the missing values, but for now you need to use replace_missing manually on your Raster.

Last, because netcdf is by default a Mapped projection that can handle the variable having a different projection to the underlying data, you need to specify both crs and mappedcrs are your well known text values. (I should make that optional seeing your dimension variables here are actually the raw geotransforn).

The good news is Rasters.jl automates more than you think, so you can shorten that code a bit. This works for me:

using Rasters, NCDatasets
fn = download("https://www.globsnow.info/swe/archive_v3.0/L3B_monthly_biascorrected_SWE/NetCDF4/201805_northern_hemisphere_monthly_biascorrected_swe_0.25grid.nc", "201805_northern_hemisphere_monthly_biascorrected_swe_0.25grid.nc")
# Get the crs with NCDatasets, and wrap it in `WellKnownText`:
wkt = WellKnownText(Dataset(fn)["crs"].attrib["spatial_ref"])
# Choose the :swe layer from the netcdf file
swe = Raster(fn; name=:swe, crs=wkt, mappedcrs=wkt)
# Use typemin(Int32) as the missing value.
swe = replace_missing(swe, typemin(Int32))
# And resample to EPSG(4326)
resampled = resample(swe, 1; crs=EPSG(4326))
2 Likes