Project

General

Profile

Conditional operator in CDO

Added by jyoti lodha almost 6 years ago

Hi
When I am exceuting my ksh script I am getting follwing error. As I am having 4 files of DT2.nc(1to4) I want to comapre it with the constant value and then if the conditionn is true I want to multiply it with a another file of RB2.nc and if false I want to multiple it with the timea file and RB2.nc file. How can it be possible. Please help me I am stuck here,please provide me some solution.

for n in $( seq 2 5)
do
i=$(expr $n - 1)
RDinput1=DT2_${i}.nc
RBinput1=RB_${i}.nc

moinput=timea_${i}.nc
for u in $Tmoinput
do
for e in $RDinput1
do
for f in $RBinput1
do

cdo -expr,'tas=(tas>0.197)?$f:$f*$u;' DT2_$i.nc RI3_$i.nc

done
done
done
done

Error: cdo div: Processed 2 values from 2 variables over 2 timesteps ( 0.01s )
cdo sub: Processed 2 values from 2 variables over 2 timesteps ( 0.00s )
cdo sub: Processed 2 values from 2 variables over 2 timesteps ( 0.00s )
cdo sub: Processed 2 values from 2 variables over 2 timesteps ( 0.01s )
cdo sub: Processed 2 values from 2 variables over 2 timesteps ( 0.01s )
Unknown character!
Unknown character!
Unknown character!

cdo expr (Abort): Variable >f< not found!
Unknown character!
Unknown character!
Unknown character!

cdo expr (Abort): Variable >f< not found!
Unknown character!
Unknown character!
Unknown character!

cdo expr (Abort): Variable >f< not found!
Unknown character!
Unknown character!
Unknown character!

cdo expr (Abort): Variable >f< not found!

Waiting for a positive reply.

DT2_4.nc (4.89 KB) DT2_4.nc file for comparsion
DT2_3.nc (5.32 KB) DT2_3.nc file for comparsion
DT2_2.nc (5.1 KB) DT2_2.nc file for comparsion
DT2_1.nc (5.31 KB) DT2_1.nc file for comparsion
RB_4.nc (2.38 KB) RB_4.nc Rb input

Replies (5)

RE: Conditional operator in CDO - Added by Ralf Mueller almost 6 years ago

your call

cdo -expr,'tas=(tas>0.197)?$f:$f*$u;' DT2_$i.nc RI3_$i.nc
uses single qutes. In any shell I know, these indicate unchangable strings. this means, that your variable value $f is not evaluation to its value, but kep as it is, i.e. the string '$f'.

Use double quotes instead (and curly braces for security}

cdo -expr,"tas=(tas>0.197)?${f}:${f}*${u};" DT2_${i}.nc RI3_${i}.nc

hth
ralf

RE: Conditional operator in CDO - Added by jyoti lodha almost 6 years ago

Hi
Thanks a lot. But still it is giving me an error

syntax error!

cdo expr (Abort): No output variable found!
syntax error!

cdo expr (Abort): No output variable found!
syntax error!

cdo expr (Abort): No output variable found!
syntax error!

cdo expr (Abort): No output variable found!

RE: Conditional operator in CDO - Added by Ralf Mueller almost 6 years ago

my version

for n in $( seq 2 5)                               
do                                                 
  echo "n=$n"                                      
  i=$(expr $n - 1)                                 
  echo "i=$i"                                      
  RDinput1=DT2_${i}.nc                             
  RBinput1=RB_${i}.nc                              

  Tmoinput=timea_${i}.nc                           
  for u in $Tmoinput                               
  do                                               
    for e in $RDinput1                             
    do                                             
      for f in $RBinput1                           
      do                                           

        echo cdo -expr,"tas=(tas>0.197)?${f}:${f}*${u};" DT2_${i}.nc RI3_${i}.nc
        cdo -expr,"tas=(tas>0.197)?${f}:${f}*${u};" DT2_${i}.nc RI3_${i}.nc                                                                  

      done                                         
    done                                           
  done                                             
done          

my output

n=2
i=1
cdo -expr,tas=(tas>0.197)?RB_1.nc:RB_1.nc*timea_1.nc; DT2_1.nc RI3_1.nc

cdo expr (Abort): Variable >timea_1.nc< not found!
n=3
i=2
cdo -expr,tas=(tas>0.197)?RB_2.nc:RB_2.nc*timea_2.nc; DT2_2.nc RI3_2.nc

cdo expr (Abort): Variable >timea_2.nc< not found!
n=4
i=3
cdo -expr,tas=(tas>0.197)?RB_3.nc:RB_3.nc*timea_3.nc; DT2_3.nc RI3_3.nc

cdo expr (Abort): Variable >timea_3.nc< not found!
n=5
i=4
cdo -expr,tas=(tas>0.197)?RB_4.nc:RB_4.nc*timea_4.nc; DT2_4.nc RI3_4.nc

cdo expr (Abort): Variable >timea_4.nc< not found!

The expr operator does not some magic uppon files like multiplying them. It can only work on existing data variables from these files. so you have to use variable names instead of filenames.

And another issue: The loopcounter 'e' is not used. so this loop is ether superfluous or it's usage in the CDO call is missing. I guess

RE: Conditional operator in CDO - Added by jyoti lodha almost 6 years ago

Hi

Thanks a lot. I understood what to said. Is there any other command or some other way to do it in CDO.

Like By using if then else statement

if [[ $filename -eq 1000 ]];then
  # commands....
fi

Thanks. If possible please let me know how to solve?

RE: Conditional operator in CDO - Added by Ralf Mueller almost 6 years ago

you can do two things

  1. you call cdo expr with the variable names of the corresponding files (tas or tmp)
  2. you call cdo ifthenelse on the files with a dynamically created mask

I would to the first one, something like:

cdo -expr,tas='(tas>0.197)?tmp:tmp*tas' -merge DT2_4.nc RB_4.nc output.nc

hth
ralf

    (1-5/5)