Project

General

Profile

Compute Autocorrelation (lag correlation) in cdo

Added by Lyndon Mark Olaguera over 7 years ago

Hi all,

I would like to perform autocorrelation (lag 1) of a netcdf file.
Is this possible in cdo?
I tried to use the fldcor and timcor functions, but I dont know how to implement this.
Would it be correct if I just delete a time step in the netcdf file and correlate it with the original netcdf file?

Ill appreciate any help


Replies (5)

RE: Compute Autocorrelation (lag correlation) in cdo - Added by Sebastian Bathiany over 7 years ago

Hi Lyndon,

I just had the same problem.
timcor seems to do what you want if you copy your time series and shift it by the required lag, then use timcor on the original and shifted time series.

There is one caveat I struggled with:
The cdo command does not seem to recognize the date. So the "t" in the cdo documentation does actually not stand for absolute time, but for the number of the record in the file. So it is not sufficient to just relabel the dates, you have to remove a record entry.
I did it the following way:

noftimesteps=`cdo showtime dataset.nc |wc -w`        #counts time steps (this and the next command depend on the shell you are using)
(( noftimesteps2 = ${noftimesteps} - 1 )) #number of time steps minus 1 (because my lag is one month and the data are monthly)
cdo shifttime,1month dataset.nc dataset_shift.nc
cdo seltimestep,2/${noftimesteps} dataset_shift.nc dataset_shift1.nc
cdo seltimestep,1/${noftimesteps2} dataset.nc dataset_shift2.nc
cdo timcor dataset_shift1.nc dataset_shift2.nc dataset_AC.nc

I hope that this may help anyone.

Sebastian

RE: Compute Autocorrelation (lag correlation) in cdo - Added by Lyndon Mark Olaguera over 7 years ago

Dear Sebastian,

Deleting the timestep before using timcor also worked for my case.
Your solution is more elegant than my method.
Many many thanks for the help.

RE: Compute Autocorrelation (lag correlation) in cdo - Added by Ralf Mueller over 7 years ago

Instead of

noftimesteps=`cdo showtime dataset.nc |wc -w`
you could use the ntime operator. see here for more

RE: Compute Autocorrelation (lag correlation) in cdo - Added by Ralf Mueller over 7 years ago

Instead of selecting the timesteps you want, you could delete the ones, you don't want with the delete operator:

ram@luthien:~ cdo -infov -for,1,10                                                                                                                                                                                                                           [10:30:10|16-12-02]
cdo infon: Started child process "for,1,10 (pipe1.1)".
cdo(2) for: Set default filetype to GRIB
    -1 :       Date     Time   Level Gridsize    Miss :     Minimum        Mean     Maximum : Parameter name
     1 : 0001-01-01 00:00:00       0        1       0 :                  1.0000             : for           
     2 : 0001-01-02 00:00:00       0        1       0 :                  2.0000             : for           
     3 : 0001-01-03 00:00:00       0        1       0 :                  3.0000             : for           
     4 : 0001-01-04 00:00:00       0        1       0 :                  4.0000             : for           
     5 : 0001-01-05 00:00:00       0        1       0 :                  5.0000             : for           
     6 : 0001-01-06 00:00:00       0        1       0 :                  6.0000             : for           
     7 : 0001-01-07 00:00:00       0        1       0 :                  7.0000             : for           
     8 : 0001-01-08 00:00:00       0        1       0 :                  8.0000             : for           
     9 : 0001-01-09 00:00:00       0        1       0 :                  9.0000             : for           
    10 : 0001-01-10 00:00:00       0        1       0 :                  10.000             : for           
cdo(2) for:  ( 0.00s )
cdo infon: Processed 10 values from 1 variable over 10 timesteps ( 0.00s )
ram@luthien:~ cdo -infov -delete,timestep=1 -for,1,10                                                                                                                                           [10:30:39|16-12-02]
cdo infon: Started child process "delete,timestep=1 -for,1,10 (pipe1.1)".
cdo(2) delete: Started child process "for,1,10 (pipe2.1)".
cdo(3) for: Set default filetype to GRIB
    -1 :       Date     Time   Level Gridsize    Miss :     Minimum        Mean     Maximum : Parameter name
     1 : 0001-01-02 00:00:00       0        1       0 :                  2.0000             : for           
     2 : 0001-01-03 00:00:00       0        1       0 :                  3.0000             : for           
     3 : 0001-01-04 00:00:00       0        1       0 :                  4.0000             : for           
     4 : 0001-01-05 00:00:00       0        1       0 :                  5.0000             : for           
     5 : 0001-01-06 00:00:00       0        1       0 :                  6.0000             : for           
     6 : 0001-01-07 00:00:00       0        1       0 :                  7.0000             : for           
     7 : 0001-01-08 00:00:00       0        1       0 :                  8.0000             : for           
     8 : 0001-01-09 00:00:00       0        1       0 :                  9.0000             : for           
     9 : 0001-01-10 00:00:00       0        1       0 :                  10.000             : for           
cdo(3) for:  ( 0.00s )
cdo(2) delete: Processed 9 values from 1 variable over 10 timesteps ( 0.00s )
cdo infon: Processed 9 values from 1 variable over 9 timesteps ( 0.00s )
ram@luthien:~ cdo -infov -delete,timestep=1/3 -for,1,10                                                                                                                                         [10:30:48|16-12-02]
cdo infon: Started child process "delete,timestep=1/3 -for,1,10 (pipe1.1)".
cdo(2) delete: Started child process "for,1,10 (pipe2.1)".
cdo(3) for: Set default filetype to GRIB
    -1 :       Date     Time   Level Gridsize    Miss :     Minimum        Mean     Maximum : Parameter name
     1 : 0001-01-04 00:00:00       0        1       0 :                  4.0000             : for           
     2 : 0001-01-05 00:00:00       0        1       0 :                  5.0000             : for           
     3 : 0001-01-06 00:00:00       0        1       0 :                  6.0000             : for           
     4 : 0001-01-07 00:00:00       0        1       0 :                  7.0000             : for           
     5 : 0001-01-08 00:00:00       0        1       0 :                  8.0000             : for           
     6 : 0001-01-09 00:00:00       0        1       0 :                  9.0000             : for           
     7 : 0001-01-10 00:00:00       0        1       0 :                  10.000             : for           
cdo(3) for:  ( 0.00s )
cdo(2) delete: Processed 7 values from 1 variable over 10 timesteps ( 0.00s )
cdo infon: Processed 7 values from 1 variable over 7 timesteps ( 0.00s )

hth
ralf

RE: Compute Autocorrelation (lag correlation) in cdo - Added by Noor Shazwani over 6 years ago

noftimesteps=`cdo showtime dataset.nc |wc -w` #counts time steps (this and the next command depend on the shell you are using)
(( noftimesteps2 = ${noftimesteps} - 1 )) #number of time steps minus 1 (because my lag is one month and the data are monthly)
cdo shifttime,1month dataset.nc dataset_shift.nc
cdo seltimestep,2/${noftimesteps} dataset_shift.nc dataset_shift1.nc
cdo seltimestep,1/${noftimesteps2} dataset.nc dataset_shift2.nc

The codes above for monthly data. How about daily data? Do I need to minus one? I already try it but it shows invalid arithmetic operator. Maybe because of my data is in daily data. Do anyone know how to solve it? Thanks in advance.

    (1-5/5)