Skip to content

Instantly share code, notes, and snippets.

@dvictori
Created March 14, 2022 11:43
Show Gist options
  • Save dvictori/b439f19fa1c67707059847442a4e9c3a to your computer and use it in GitHub Desktop.
Save dvictori/b439f19fa1c67707059847442a4e9c3a to your computer and use it in GitHub Desktop.
Rewrite CDS CMIP6 NetCDF files to fix lat_bnds and lon_bnds problem
#!/usr/bin/env Rscript
# NetCDF rewrite - Data downloaded from CDS had some funny dimensions
# CDO did not like it. So rewriting
# usage: rewrite_netcdf input.nc output.nc
library(ncdf4)
# processin arguments ####
args <- commandArgs(trailingOnly = TRUE)
if (length(args) != 2) {
print("usage: rewrite_netcdf input.nc output.nc ")
stop('Provide needed arguments')
}
arq <- args[1]
path <- dirname(args[1])
nome_base <- basename(arq)
variable <- unlist(strsplit(nome_base, '_'))[1]
# opening data and attributes ####
infile <- nc_open(arq)
xsize <- infile$dim$lon$len
ysize <- infile$dim$lat$len
atributos <- ncatt_get(infile, 0)
var_atrib <- ncatt_get(infile, variable)
# creating output file ####
lon <- ncvar_get(infile, 'lon')
lat <- ncvar_get(infile, 'lat')
tvals <- ncvar_get(infile, 'time')
tunits <- ncatt_get(infile, 'time', 'units')
londim <- ncdim_def('lon', 'degrees_east', lon, longname = 'longitude')
latdim <- ncdim_def('lat', 'degrees_north', lat, longname = 'latitude')
timedim <- ncdim_def('time', tunits$value, tvals, longname = 'time')
var_def <- ncvar_def(variable, var_atrib$units, list(londim, latdim, timedim),
var_atrib[['_FillValue']])
outfile <- nc_create(args[2],
list(var_def), force_v4 = TRUE)
# copying data to output files ####
# in chunks to save memory
anos <- ceiling(length(tvals) / 365)
for (i in 1:anos) {
sid <- (i - 1)*365 + 1
if (i == anos) {
# ultimo grupo.
nts <- length(tvals) - (i-1) * 365
} else {
nts <- 365
}
dados <- ncvar_get(infile, variable,
start = c(1, 1, sid),
count = c(xsize, ysize, nts))
ncvar_put(outfile, variable, dados,
start = c(1,1, sid),
count = c(xsize, ysize, nts))
}
# Updating output file attributes ####
ncatt_put(outfile, 'lon', 'axis', 'X')
ncatt_put(outfile, 'lat', 'axis', 'Y')
ncatt_put(outfile, 'time', 'axis', 'T')
atributos$history <- paste(atributos$history, ';', Sys.time(),
'NetCDF rewrite to work with CDO using rewrite_netcdf.R (DCV, Embrapa CNPTIA)')
for (a in names(atributos)) {
ncatt_put(outfile, 0, a, atributos[[a]])
}
var_atrib$history <- paste(var_atrib$history, ';',
Sys.time(),
'NetCDF rewrite to work with CDO using rewrite_netcdf.R (DCV, Embrapa CNPTIA)')
# fillvalue does not work here. remove from attribute list
ncatt_put(outfile, variable, 'missing_value', var_atrib[['missing_value']])
var_atrib[['_FillValue']] <- NULL
var_atrib[['missing_value']] <- NULL
for (a in names(var_atrib)) {
ncatt_put(outfile, variable, a, var_atrib[[a]])
}
nc_close(outfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment