4

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)

ExHunter
  • 307
  • 4
  • 11
  • Could you please show the cdo command that you tried and the error that you got? – Robert Davy Mar 15 '20 at 02:28
  • `cdo merge input_1.nc input_2.nc output.nc` This takes a lot of time because i cant do looping with that code – ExHunter Mar 15 '20 at 04:44
  • Did you try `cdo mergetime` with wildcard, e.g. https://stackoverflow.com/questions/26240747/how-to-convert-daily-to-monthly-netcdf-files – Robert Davy Mar 17 '20 at 00:19
  • if the file names are not sequential but time variable is sequential, is that okay? or have to be sequential? – ExHunter Mar 17 '20 at 06:34
  • I believe they need to be in correct alphanumeric order like in your original example. Why not try it out and see what you get? – Robert Davy Mar 18 '20 at 00:07
  • 2
    @Robert Davy, in fact they don't even need to be in correct alphanumeric order, CDO sorts that for you using the date stamp if you use the command mergetime... They would need to be in correct order if you were instead using the more general "merge" command, which simply takes the files in the order they come. – ClimateUnboxed Apr 28 '20 at 08:41
  • Does this answer your question? [merging of Netcdf files](https://stackoverflow.com/questions/17408587/merging-of-netcdf-files) – ClimateUnboxed Apr 28 '20 at 09:47

1 Answers1

4

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.

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
  • I got an error `simultan.sh: line 1: syntax error near unexpected token ``$'do\r''` 'imultan.sh: line 1:` `for year in {1982..2018} ; do` – ExHunter Apr 30 '20 at 03:52
  • which shell are you using? I added a bash command at the top that you may need to add as your first line. If you prefer to use another shell you can also write the loop using seq like this: for year in `seq 1981 2007` ; do or for year in $(seq 1981 2007) ; do v - In any case, did you try simply doing it in one command first without resorting to the loop? Did that work? – ClimateUnboxed Apr 30 '20 at 05:24
  • you mean the ubuntu shell process that one can now install under windows 10? what happens if you type "seq 10" in the shell window? – ClimateUnboxed May 01 '20 at 10:53