Project

General

Profile

Conditional ifthen ifthenelse

Added by Theodoros Gkousarov over 10 years ago

Is it possible to combine if statements in one line so that can be executed at the same time?

for example:

If value in 1st timestep of file1.nc is greater to 0, then extract the value of the first timestep of file1.nc to file out.nc
do this until out.nc gets an accumulated value of 660.

at the same time if value in file1.nc is greater than 0 and less than 660 in out.nc
then take the values from files2.nc and create a file out2.nc which stops accumulating only when value in out.nc becomes 660.


Replies (1)

RE: Conditional ifthen ifthenelse - Added by Jaison-Thomas Ambadan over 10 years ago

I don't think you can do all these in one step. As far as I know there is no cumulative sum operator, which would have made it easier to do the job. Anyway, [If I understood your question correctly,] it can be done using a smalls script like:

#!/bin/sh
#------------------------------------------------------------------------------
#
# set the threshold value
 thold=660.0
#
# select all values above zero for all timesteps
  cdo -mul input_file.nc -gtc,0.0 input_file.nc values_gt0.nc
#
# create a mask file with threshold value (single time-step) 
  cdo -f nc -seltimestep,1 -const,${thold},values_gt0.nc threshold_${thold}.nc
#
# store the total timesteps of the input file
  tsteps=$(cdo ntime input_file.nc)
#
# Now loop through all time steps, calculate the cumulative sum, apply the threshold and mask it
  for I in $(seq 1 1 $tsteps); do
   #
    cdo -mul -timsum -seltimestep,1/$I values_gt0.nc -ge -timsum -seltimestep,1/$I values_gt0.nc threshold_${thold}.nc values_above_threshold_until_step_$I.nc
    cdo -mul -timsum -seltimestep,1/$I values_gt0.nc -lt -timsum -seltimestep,1/$I values_gt0.nc threshold_${thold}.nc values_below_threshold_until_steps_$I.nc
   #
  done
#
# merge all time steps 
 cdo -mergetime values_above_threshold_*.nc above.nc
 cdo -mergetime values_below_threshold_*.nc below.nc
#
# remove temporary files
 \rm -f values*.nc
#
 exit 

The "above.nc" and "below.nc" should give the above and below values if the cumulative sum is above and below the threshold, respectively. I'm not sure if this is what you want.

Cheers,
J

    (1-1/1)