0

I downloaded some CMIP6 data from Copernicus data service and I believe the NetCDF file came with malformed dimension information. When I try to get the lon or lat dimensions (st_get_dimension_values) it gives me a huge vector with the same number of cells in the file. That is, the coordinates are being repeated.

I'm trying to use st_set_dimensions to fix this. But I can't get it to work.

I'm trying to do this because my end goal is to do a monthly aggregate, which is not working and I suspect it's because of the NetCDF dimensions.

Here goes a reprex

library(stars)
#> Loading required package: abind
#> Loading required package: sf
#> Linking to GEOS 3.10.1, GDAL 3.4.0, PROJ 8.2.0; sf_use_s2() is TRUE

# nc file can be downloaded from
# https://drive.google.com/file/d/1XJKt0aKbo4l_t3wPnHx-uXCgVqWKaKmf/view?usp=sharing
# it's a 90Mb file

arq <- 'pr_day_MIROC6_ssp126_r1i1p1f1_gn_20150101-21001231_v20191016.nc'
dados <- read_ncdf(arq, var = "pr")
#> No projection information found in nc file. 
#>  Coordinate variable units found to be degrees, 
#>  assuming WGS84 Lat/Lon.

dados
#> stars object with 3 dimensions and 1 attribute
#> attribute(s), summary of first 1e+05 cells:
#>                       Min.      1st Qu.       Median         Mean     3rd Qu.
#> pr [kg/m^2/s] 1.536407e-23 3.599575e-06 2.169935e-05 7.688519e-05 9.28465e-05
#>                      Max.
#> pr [kg/m^2/s] 0.002508056
#> dimension(s):
#>      from    to         offset  delta  refsys point
#> lon     1    31             NA     NA  WGS 84 FALSE
#> lat     1    30             NA     NA  WGS 84 FALSE
#> time    1 31411 2015-01-01 UTC 1 days POSIXct    NA
#>                                            values x/y
#> lon   [284.7656,286.1719),...,[326.9531,328.3594) [x]
#> lat  [-35.02015,-33.61934),...,[5.60321,7.004013) [y]
#> time                                         NULL

lons <- st_get_dimension_values(dados, 'lon') 
length(lons)
#> [1] 973741

lats <- st_get_dimension_values(dados, 'lat') 

lons <- lons[1:31]
lats <- lats[1:30]

st_set_dimensions(dados, which = 'lon', values = lons,
                  name = 'lon')
#> Error in st_as_stars.list(unclass(.x), dimensions = d): incorrect length of dimensions values for dimension 2


dados_mes <- aggregate(dados,
                       by = 'month',
                       FUN = sum,
                       na.rm = TRUE)
#> Warning in array(x[[i]], newdims): NAs introduced by coercion to integer range
#> Error in array(x[[i]], newdims): negative length vectors are not allowed

Created on 2022-01-07 by the reprex package (v2.0.1)

1 Answer 1

0

Use the ncdf4 package for reading NetCDF files - it lets you get the variables and dimensions and then you have control. Some of the spatial packages seem to make assumptions about NetCDFs that aren't true.

Here's how to get your data (specifically, the pr variable) into a 3d array of lat-long-time:

> pr = nc_open("pr_day_MIROC6_ssp126_r1i1p1f1_gn_20150101-21001231_v20191016.nc")
> prv = ncvar_get(pr,"pr")
> dim(prv)
[1]    31    30 31411

The lat-long grid coords can be gotten from the pr object:

> pr$dim$lat$vals
 [1] -34.3187701 -32.9180072 -31.5172438 -30.1164799 -28.7157157 -27.3149511
 [7] -25.9141862 -24.5134209 -23.1126554 -21.7118896 -20.3111235 -18.9103573
[13] -17.5095908 -16.1088242 -14.7080574 -13.3072904 -11.9065233 -10.5057561
[19]  -9.1049889  -7.7042215  -6.3034540  -4.9026865  -3.5019190  -2.1011514
[25]  -0.7003838   0.7003838   2.1011514   3.5019190   4.9026865   6.3034540
> pr$dim$lon$vals
 [1] -74.53125 -73.12500 -71.71875 -70.31250 -68.90625 -67.50000 -66.09375
 [8] -64.68750 -63.28125 -61.87500 -60.46875 -59.06250 -57.65625 -56.25000
[15] -54.84375 -53.43750 -52.03125 -50.62500 -49.21875 -47.81250 -46.40625
[22] -45.00000 -43.59375 -42.18750 -40.78125 -39.37500 -37.96875 -36.56250
[29] -35.15625 -33.75000 -32.34375

And similarly for the time points, which appear to be in interesting units:

> pr$dim$time$len
[1] 31411
> pr$dim$time$units
[1] "days since 1850-01-01"
> pr$dim$time$val[1:10]
 [1] 60265.5 60266.5 60267.5 60268.5 60269.5 60270.5 60271.5 60272.5 60273.5
[10] 60274.5
2
  • Thanks. That's exactly what I ended up doing. I was having trouble processing this data in CDO. So I recreated the NetCDF file using ncdf4 package. Now, CDO is happy (and so am I). I initially tried to use the other packages (raster, terra, stars) because for me it's a bit more intuitive than ncdf4.
    – Daniel
    Jan 10, 2022 at 12:52
  • Yes, if the NetCDF is structured so that the spatial packages can intuit the structure correctly, I'd advise using them! Its possible there's an option we've not got right or something, but luckily we've got ncdf4 to save us.
    – Spacedman
    Jan 10, 2022 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.