Search code examples
rnetcdfnetcdf4cdo-climate

How to merge/ combine time variable in multiple netcdf


I have a lot of Sea Surface Temperature NetCDF files with the same lat and lon dimensions, but different time variables. I want to try to combine it into 1 NetCDF file by combining the time variables because the time variables in each netcdf file are sequential

is there a more effective way? because in CDO (Climate Data Operators) I can't do looping

The following is an example of the file name that I use

sstdas_19810101.nc
sstdas_19810102.nc
sstdas_19810103.nc
sstdas_19810201.nc
sstdas_19810202.nc
sstdas_19810203.nc
...
sstdas_20171203.nc

with pattern sstdas_(year)(month)(dekad)


Solution

  • You can use the mergetime argument with CDO:

    cdo mergetime sstdas_*.nc merged_file.nc
    

    note that CDO has all files open at once for this operation, and on some systems there is a limit to the number of open file concurrently, in which case CDO will throw an error. In that case you will need to loop, which you can do with CDO using a bash loop, I would suggest trying one year at a time and then merging those:

    #!/bin/bash
    for year in {1981..2007} ; do
      cdo mergetime sstdas_${year}??.nc sstdas_${year}.nc
    done 
    cdo mergetime sstdas_????.nc sstdas_wholeseries.nc
    

    Note I use the single letter wildcard "?" to be stricter and avoid any mixing of daily and yearly files with the two merges.