Assuming two netCDF files (nc4 in my case), each of one variable with 3 dimensions: latitude, longitude and time (year). They are of different time spans (e.g. 1700-2005 and 2005-2100 - they overlap at 2005). How can I concatenate them to obtain one nc4 file spanning 1700-2100?
Simply trying the NCO operator
ncrcat file1.cn4 file2.nc4 result.nc4
gives a file that spans [1700-2005, 1700-1795]. When running the line it warns: nco_cln_clc_dff<><> failed to initialize UDUnits2 library
. According to this post, I should not worry.
I also used appending as described in the NCO manual ncrcat --rec_apn file2.nc4 file1.cn4
. I get a resulting time span [2005-2310,2005-2100] and a warning: ncrcat: WARNING Intra-file non-monotonicity. Record coordinate “time” does not monotonically decrease between input file file2.nc4 record indices: 94,95 output file1.nc4 record indices 400,401 ...
(the indices are less important here as I get the warning for every one of them)
Note that I could concatenate without the time span issue with CDO on Linux: cdo mergetime file1.cn4 file2.nc4 result.nc4
(or cdo -z zip_3 mergetime file1.cn4 file2.nc4 result.nc4
to get a level 3 of compression). Note also that I had to use export SKIP_SAME_TIME=1
before calling cdo mergetime
in order to deal with the overlapping 2005 year (will take only the first occurrence).
With R I tried the following:
library(ncdf4)
library(ncdf.tools)
ncFile1 <- nc_open("C://file1.nc4")
nc1 <-ncvar_get(ncFile1)
ncFile2 <- nc_open("C://file2.nc4")
nc2 <-ncvar_get(ncFile2)
transNcdfMerge(c(nc1, nc2), target.name = "my_test.nc4")
I let it run for almost 2 hours and then I stopped the run. Since I have to do this for hundreds of nc4 files, I can't wait that long. Didn't yell any error while running but I am not sure if the code is correct.
I use Windows 7, 64 bit, 8Gb RAM. And for R - "R version 3.3.0 (2016-05-03)"
Update: printing some metadata of the two files with R
1) file1.nc4
ncdf4::print(file1.nc4)
1 variables (excluding dimension variables):
float prop_crop[lon,lat,time] (Chunking: [720,360,1]) (Compression: shuffle,level 3)
units: percent
_FillValue: -9999
long_name: Proportion of landcover in crops
missing_value: -9999
3 dimensions:
lat Size:360
units: degrees_north
long_name: Latitude
standard_name: latitude
lon Size:720
units: degrees_east
long_name: Longitude
standard_name: longitude
time Size:306 *** is unlimited ***
long_name: Time
standard_name: time
calendar: proleptic_gregorian
units: years since 1700-01-01 00:00:00
2) file2.nc4
ncdf4::print(file2.nc4)
1 variables (excluding dimension variables):
float prop_crop[lon,lat,time] (Chunking: [720,360,1]) (Compression: shuffle,level 3)
units: percent
_FillValue: -9999
long_name: Proportion of landcover in crops
missing_value: -9999
3 dimensions:
lat Size:360
units: degrees_north
long_name: Latitude
standard_name: latitude
lon Size:720
units: degrees_east
long_name: Longitude
standard_name: longitude
time Size:96 *** is unlimited ***
units: years since 2005-01-01 00:00:00
long_name: Time
standard_name: time
calendar: proleptic_gregorian
Hope it helps