Project

General

Profile

change values of specific levels

Added by Shizhu Wang almost 6 years ago

Hi, I have a nc file temp.nc, with a variable called temp(lon,lat,depth), where the length of depth is 20.
Now I want to replace the values of the last level with the values of the second last level:
temp(:,:,20) = temp(:,:,19)

How can I do this with CDO?

Thanks.
Shizhu


Replies (2)

RE: change values of specific levels - Added by Karin Meier-Fleischer almost 6 years ago

Hi Shizhu,

I think the fastest way you can do this (if you really want it?) is using NCL because in my opinion you can't do it with CDO.

Here is an ksh-shell script creating and running an NCL example script:

#!/bin/ksh

infile=infile.nc                                #-- name of your input file
outfile=outfile.nc                              #-- name of the output file

cat << EOF > script.ncl
f      = addfile("$infile","r")
temp   = f->temp                                ;-- temp(lon,lat,depth)
ndepth = dimsizes(f->depth)                     ;-- number of depths

temp_new = temp                                 ;-- copy variable and meta data
temp_new(:,:,ndepth-1) = temp(:,:,ndepth-2)     ;-- copy data of next to last plev to last plev

system("rm $outfile")                           ;-- delete file if it exists
g = addfile("$outfile","c")                     ;-- create new file
g->temp_new = temp_new                          ;-- write data to new file
EOF

ncl script.ncl                                  #-- run NCL script

exit

-Karin

RE: change values of specific levels - Added by Shizhu Wang almost 6 years ago

Hi Karin,

Thanks for your suggestion. I know nothing about ncl, but I just made it using nco:

ncap2 -s "temp(19,:,:)=temp(18,:,:)" in.nc out.nc

Two reasons for this:
1. dimensions are reversed: in Matlab or Fortran, it is temp(lon,lat,depth); but in ncdump (i.e., nco), it is temp(depth,lat,lon).
2. every dimension in nco starts from 0. So here temp(19,:,:) in ncap2 is temp(:,:,20) in Matlab or Fortran.

Best,
Shizhu

    (1-2/2)