set -x
  inputfile=$1
 outputfile='sortedInput.nc'

min=$(cdo -outputkey,value,nohead -fldmin $inputfile)

cdo -gtc,$((min - 1)) $inputfile mask

cdo -f nc -reducegrid,mask $inputfile unstructuredInput

# create an orderes list of location indices, use options of 'sort' to control the order
cells=$(cdo -outputkey,value,xind,nohead unstructuredInput | sort -rn | rev | cut -d ' ' -f 2 | rev | xargs echo | tr ' ' ,)

# selecta (all) grid locations in the order of the data values
cdo -selgridcell,$cells unstructuredInput $outputfile
