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)