1

I have several observations over time of a spatial variable (let's say var_A_1985.nc, var_A_2000.nc and var_A_2010.nc) with only one time layer. Would it be possible to efficiently reconstruct the daily values from 01/01/1985 to 01/01/2010 by linearly interpolating these values?

I currently explored two options:

  • Climate Data Operator (CDO): I saw the operators inttime and intyear, but these do not seem to work for my case.

  • R as RasterBrick element manipulation

something like:

library(raster)
library(ncdf4)
setwd(mypath)

var_A_1985 <- raster("var_A_1985.nc")
var_A_2000 <- raster("var_A_2000.nc")

var_A_brick <- brick(var_A_1985, var_A_2000)
brick_2<- brick(lapply(1:n_days, function(x) raster::raster(matrix(NA, nrow(var_A_1985), ncol(var_A_1985)))) #with n_days representing the number of days between 01/01/1985 and 01/01/2000
extremes<- c(1, n_days)

for (i in 1:2) {
    brick_2[[extremes[i]]]=var_A_brick[[1]]
}
var_A_brick_filled <- approxNA(brick_2, method = 'linear')
#and then write this the new netcdf with daily values 

This second method actually works, but it takes forever. Is there a more efficient way to do this?

Nemesi
  • 781
  • 3
  • 13
  • 29

1 Answers1

2

You can do this with CDO as follows:

cdo -inttime,1985-01-01,12:00:00,1day -mergetime var*.nc outfile

Adjust the start time, of course.

Robert Wilson
  • 3,192
  • 11
  • 19
  • Hi Robert, thanks for your reply. Not really, with this command you input just one file (so just one point in time). How do you define the end of the interpolation? – Nemesi Jun 18 '21 at 14:50
  • 1
    Robert's solution merges the files starting with "var" into one file with mergetime, and then applied the interpolation to the output of the mergetime command, and so will have more than one timeslice – ClimateUnboxed Jun 18 '21 at 16:19
  • Thanks Adrian and Robert! Just to understand if I got this correctly: step 1) I create the two var files assigning a date to them (01/01/ year1 and year2); step 2) I merge the two files (cdo -cat); step 3) I run the cdo -inttime as for Robert's suggestion. Is this correct? – Nemesi Jun 21 '21 at 08:32
  • Could you not just try to do this before asking? – Robert Wilson Jun 21 '21 at 09:12
  • Hi Robert, no need to get nasty: I was just trying to understand how this works. Thanks for your suggestion: I'll test it and accept your answer. – Nemesi Jun 21 '21 at 13:03