编辑答案:
使用已添加到 CDO v1.8.0 及更高版本 ( https://code.zmaw.de/projects/cdo/embedded/index.html#x1-3300002.8.1 )的新 timcumsum 函数,现在可以完成此任务网格字段:
cdo gec,0 precip.nc mask.nc # positive entries are 1, negative are zero
cdo timcumsum mask.nc maskcum.nc # sum the mask in the time direction
# invert the mask so that you have 1 at start, but then zero from 1st +ve val
cdo lec,0.5 maskcum.nc maskinv.nc
# sum this new mask, and add 1, result is timestep when first positive value occurs:
cdo addc,1 -timcumsum maskinc.nc stepfirstpos.nc
我认为这个功能在一条线上完成
cdo addc,1 -timcumsum -lec,0.5 -timcumsum -gec,0 precip.nc stepfirstpos.nc
原始答案:
我花了一年的时间,但我想出了一种方法,将 NCO 和 CDO 结合起来用于单点文件,而不是网格字段:
#!/bin/bash
# code to find date of first positive file entry
file=trmm_per10_pc0_year2000_nc2.nc
# set negative values to missing
cdo -s setrtomiss,-1.e36,0.0 $file set.nc
ntime=`cdo -s ntime set.nc` # number of steps in file
# loop over steps to find first positive value
for i in `seq 1 ${ntime}` ; do
# nco counts from step 0, cdo from 1
ncoi=`expr $i - 1`
# print out the value in step i
op=`ncks -d time,$ncoi -s "%16.10f\n" -H -C -v precip set.nc`
if [[ $op != "_" ]] ; then # not missing
# print the date of timestep i
cdo -s showdate -seltimestep,$i set.nc
rm -f set.nc
exit
fi
done
# no positive value found
rm -f set.nc
echo "All values in $file are negative"