#/bin/ksh set -evx # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # This script computes daily perturbation from any NEMO forcing variable # ('u10', 'v10', 't2', 'q2', 'qsw', 'qlw', 'snow', 'precip') by picking up # randomly monthly differences between ERAinterim and DFS4.3 forcing fields # and interpolating these to a daily frequency (see more details in Guemas # et al, 2014, Climate Dynamics) # # This script chases a fly with a flame thrower because it has been written # while testing many different options. It works but it should be better # rewritten. # # History : Virginie Guemas - Initial version - 2012 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Arguments # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ lstvars=( 'u10' 'v10' ) # Variables for which to compute and pick up random perturbations lstmbs=( 1 2 3 4 ) # List of members for which to build pertubation files dfs2era='T' # Interpolation from DFS4 to ERA (T) or from ERA to DFS (F) # to build differences and perturbations patherain='/cfu/scratch/vguemas/ERAint/' # Path were to find the ERAinterim forcing files pathdfsin='/cfu/scratch/vguemas/dfs4.3/' # Path were to find the DFS4.3 forcing files year0=1979 # Initial year of the reference period to compute perturbations yearf=2006 # Last year of the reference period to compute perturbations year1=2012 # First year for which to pick and build a pertubation file year2=2014 # Last year for which to pick and build a perturbation file nstep=1460 # Number of time steps for a non-leap year : 1460 or 365 raw='F' # Compute the timestep differences between raw ERA and DFS fields # If the script has already run once and the differences have # been stored, this option can be set to F for the step of picking # up random perturbations. ano='F' # Compute anomalies relative to a monthly-smoothed daily climatology # and the timestep differences between anomalies of ERA and DFS fields # raw=T needs to have run before. If the script has already run once and # the timesteps differences have been stored, this option can be set to F. reorg='F' # Reorganize the differences timestep per timestep all the years together # rather than year per year with all the timesteps. Works on anomalies # if wih is set to 'ano_' and raw differences if wih is set to '_' # Once the computation of differences and their reorganization has been # performed once (raw=T, ano=T, reorg=T), the results are stored in directories # named according to the options set and raw, ano and reorg can be set # to F for the picking up. wih='ano_' # Which types needs to be picked for the perturbations monmean='T' # Compute monthly mean differences and pickup perturbations from monthly mean # differences rather than timestep differences pickup1='T' # Pickup random perturbations for each member, variable, year, either # one per timestep or one per month pickup2='F' # Pickup 2 random perturbations and average them for each member, variable, # year, each timestep # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if [[ $dfs2era == T ]] ; then dif='eraint-dfs4.3' dir0='era-dfs' else dif='dfs4.3-eraint' dir0='dfs-era' fi for var in ${lstvars[@]} ; do if [[ $raw == T ]] ; then for ((year=$year0;year<=$yearf;year++)) ; do cp ${patherain}/${var}_eraint_${year}.nc ${var}_eraint_${year}.nc ncpdq -O -U ${var}_eraint_${year}.nc ${var}_eraint_${year}.nc if [[ $nstep == 1460 ]] ; then case $year in 1980|1984|1988|1992|1996|2000|2004|2008|2012|2016) ncks -O -d time,0,232 ${var}_eraint_${year}.nc ${var}_eraint_${year}_1.nc ncks -O -d time,237, ${var}_eraint_${year}.nc ${var}_eraint_${year}_2.nc ncrcat -O ${var}_eraint_${year}_1.nc ${var}_eraint_${year}_2.nc ${var}_eraint_${year}.nc rm -f ${var}_eraint_${year}_1.nc ${var}_eraint_${year}_2.nc ;; esac fi if [[ $nstep == 365 ]] ; then case $year in 1980|1984|1988|1992|1996|2000|2004|2008|2012|2016) ncks -O -d time,0,58 ${var}_eraint_${year}.nc ${var}_eraint_${year}_1.nc ncks -O -d time,60, ${var}_eraint_${year}.nc ${var}_eraint_${year}_2.nc ncrcat -O ${var}_eraint_${year}_1.nc ${var}_eraint_${year}_2.nc ${var}_eraint_${year}.nc rm -f ${var}_eraint_${year}_1.nc ${var}_eraint_${year}_2.nc ;; esac fi cp ${pathdfsin}/${var}_DFS4.3_${year}.nc ${var}_dfs4.3_${year}.nc ncks -A -v time ${var}_eraint_${year}.nc ${var}_dfs4.3_${year}.nc case $var in 'u10'|'v10'|'t2'|'q2') cdo setgrid,r320x161 ${var}_dfs4.3_${year}.nc ${var}_dfs4.3_${year}_bis.nc ;; 'qsw'|'qlw'|'snow'|'precip') cdo setgrid,r192x94 ${var}_dfs4.3_${year}.nc ${var}_dfs4.3_${year}_bis.nc ;; esac ncks -O -x -v lat_2,lon_2 ${var}_dfs4.3_${year}_bis.nc ${var}_dfs4.3_${year}.nc rm -f ${var}_dfs4.3_${year}_bis.nc if [[ $dfs2era == T ]] ; then cdo remapbic,r240x121 ${var}_dfs4.3_${year}.nc ${var}_dfs4.3_${year}_grideraint.nc cdo invertlat ${var}_dfs4.3_${year}_grideraint.nc ${var}_dfs4.3_${year}_grideraint_bis.nc rm -f ${var}_dfs4.3_${year}.nc ${var}_dfs4.3_${year}_grideraint.nc mv ${var}_dfs4.3_${year}_grideraint_bis.nc ${var}_dfs4.3_gridera_${year}.nc cdo sub ${var}_eraint_${year}.nc ${var}_dfs4.3_gridera_${year}.nc ${var}_eraint-dfs4.3_${year}.nc else case $var in 'u10'|'v10'|'t2'|'q2') cdo remapbic,r320x161 ${var}_eraint_${year}.nc ${var}_eraint_griddfs_${year}.nc ;; 'qsw'|'qlw'|'snow'|'precip') cdo remapbic,r192x94 ${var}_eraint_${year}.nc ${var}_eraint_griddfs_${year}.nc ;; esac rm -f ${var}_eraint_${year}.nc cdo sub ${var}_dfs4.3_${year}.nc ${var}_eraint_griddfs_${year}.nc ${var}_dfs4.3-eraint_${year}.nc fi done fi if [[ $ano == T ]] ; then length=30 if [[ $dfs2era == T ]] ; then ncea -O -n $((${yearf}-${year0}+1)),4,1 ${var}_dfs4.3_gridera_${year0}.nc ${var}_dfs4.3_gridera_${year0}-${yearf}_clim.nc ncea -O -n $((${yearf}-${year0}+1)),4,1 ${var}_eraint_${year0}.nc ${var}_eraint_${year0}-${yearf}_clim.nc ncrcat -O ${var}_dfs4.3_gridera_${year0}-${yearf}_clim.nc ${var}_dfs4.3_gridera_${year0}-${yearf}_clim.nc ${var}_dfs4.3_gridera_${year0}-${yearf}_clim_bis.nc ncrcat -O ${var}_eraint_${year0}-${yearf}_clim.nc ${var}_eraint_${year0}-${yearf}_clim.nc ${var}_eraint_${year0}-${yearf}_clim_bis.nc cdo runmean,${length} ${var}_dfs4.3_gridera_${year0}-${yearf}_clim_bis.nc ${var}_dfs4.3_gridera_${year0}-${yearf}_clim_smoothed.nc ncks -O -d time,$(($nstep+1)),$(($nstep+$length/2)) ${var}_dfs4.3_gridera_${year0}-${yearf}_clim_smoothed.nc ${var}_dfs4.3_gridera_${year0}-${yearf}_clim_smoothed_1.nc ncks -O -d time,$(($length/2+1)),$nstep ${var}_dfs4.3_gridera_${year0}-${yearf}_clim_smoothed.nc ${var}_dfs4.3_gridera_${year0}-${yearf}_clim_smoothed_2.nc ncrcat -O ${var}_dfs4.3_gridera_${year0}-${yearf}_clim_smoothed_1.nc ${var}_dfs4.3_gridera_${year0}-${yearf}_clim_smoothed_2.nc ${var}_dfs4.3_gridera_${year0}-${yearf}_clim.nc rm -f ${var}_dfs4.3_gridera_${year0}-${yearf}_clim_bis.nc ${var}_dfs4.3_gridera_${year0}-${yearf}_clim_smoothed.nc ${var}_dfs4.3_gridera_${year0}-${yearf}_clim_smoothed_1.nc ${var}_dfs4.3_gridera_${year0}-${yearf}_clim_smoothed_2.nc cdo runmean,${length} ${var}_eraint_${year0}-${yearf}_clim_bis.nc ${var}_eraint_${year0}-${yearf}_clim_smoothed.nc ncks -O -d time,$(($nstep+1)),$(($nstep+$length/2)) ${var}_eraint_${year0}-${yearf}_clim_smoothed.nc ${var}_eraint_${year0}-${yearf}_clim_smoothed_1.nc ncks -O -d time,$(($length/2+1)),$nstep ${var}_eraint_${year0}-${yearf}_clim_smoothed.nc ${var}_eraint_${year0}-${yearf}_clim_smoothed_2.nc ncrcat -O ${var}_eraint_${year0}-${yearf}_clim_smoothed_1.nc ${var}_eraint_${year0}-${yearf}_clim_smoothed_2.nc ${var}_eraint_${year0}-${yearf}_clim.nc rm -f ${var}_eraint_${year0}-${yearf}_clim_bis.nc ${var}_eraint_${year0}-${yearf}_clim_smoothed.nc ${var}_eraint_${year0}-${yearf}_clim_smoothed_1.nc ${var}_eraint_${year0}-${yearf}_clim_smoothed_2.nc for ((year=$year0;year<=$yearf;year++)) ; do cdo sub ${var}_eraint_${year}.nc ${var}_eraint_${year0}-${yearf}_clim.nc ${var}_ano_eraint_${year}.nc cdo sub ${var}_dfs4.3_gridera_${year}.nc ${var}_dfs4.3_gridera_${year0}-${yearf}_clim.nc ${var}_ano_dfs4.3_gridera_${year}.nc cdo sub ${var}_ano_eraint_${year}.nc ${var}_ano_dfs4.3_gridera_${year}.nc ${var}_ano_eraint-dfs4.3_${year}.nc rm -f ${var}_eraint_${year}.nc ${var}_dfs4.3_gridera_${year}.nc ${var}_ano_eraint_${year}.nc ${var}_ano_dfs4.3_gridera_${year}.nc done else ncea -O -n $((${yearf}-${year0}+1)),4,1 ${var}_dfs4.3_${year0}.nc ${var}_dfs4.3_${year0}-${yearf}_clim.nc ncea -O -n $((${yearf}-${year0}+1)),4,1 ${var}_eraint_griddfs_${year0}.nc ${var}_eraint_griddfs_${year0}-${yearf}_clim.nc ncrcat -O ${var}_dfs4.3_${year0}-${yearf}_clim.nc ${var}_dfs4.3_${year0}-${yearf}_clim.nc ${var}_dfs4.3_${year0}-${yearf}_clim_bis.nc ncrcat -O ${var}_eraint_griddfs_${year0}-${yearf}_clim.nc ${var}_eraint_griddfs_${year0}-${yearf}_clim.nc ${var}_eraint_griddfs_${year0}-${yearf}_clim_bis.nc cdo runmean,${length} ${var}_dfs4.3_${year0}-${yearf}_clim_bis.nc ${var}_dfs4.3_${year0}-${yearf}_clim_smoothed.nc ncks -O -d time,$(($nstep+1)),$(($nstep+$length/2)) ${var}_dfs4.3_${year0}-${yearf}_clim_smoothed.nc ${var}_dfs4.3_${year0}-${yearf}_clim_smoothed_1.nc ncks -O -d time,$(($length/2+1)),$nstep ${var}_dfs4.3_${year0}-${yearf}_clim_smoothed.nc ${var}_dfs4.3_${year0}-${yearf}_clim_smoothed_2.nc ncrcat -O ${var}_dfs4.3_${year0}-${yearf}_clim_smoothed_1.nc ${var}_dfs4.3_${year0}-${yearf}_clim_smoothed_2.nc ${var}_dfs4.3_${year0}-${yearf}_clim.nc rm -f ${var}_dfs4.3_${year0}-${yearf}_clim_bis.nc ${var}_dfs4.3_${year0}-${yearf}_clim_smoothed.nc ${var}_dfs4.3_${year0}-${yearf}_clim_smoothed_1.nc ${var}_dfs4.3_${year0}-${yearf}_clim_smoothed_2.nc cdo runmean,${length} ${var}_eraint_griddfs_${year0}-${yearf}_clim_bis.nc ${var}_eraint_griddfs_${year0}-${yearf}_clim_smoothed.nc ncks -O -d time,$(($nstep+1)),$(($nstep+$length/2)) ${var}_eraint_griddfs_${year0}-${yearf}_clim_smoothed.nc ${var}_eraint_griddfs_${year0}-${yearf}_clim_smoothed_1.nc ncks -O -d time,$(($length/2+1)),$nstep ${var}_eraint_griddfs_${year0}-${yearf}_clim_smoothed.nc ${var}_eraint_griddfs_${year0}-${yearf}_clim_smoothed_2.nc ncrcat -O ${var}_eraint_griddfs_${year0}-${yearf}_clim_smoothed_1.nc ${var}_eraint_griddfs_${year0}-${yearf}_clim_smoothed_2.nc ${var}_eraint_griddfs_${year0}-${yearf}_clim.nc rm -f ${var}_eraint_griddfs_${year0}-${yearf}_clim_bis.nc ${var}_eraint_griddfs_${year0}-${yearf}_clim_smoothed.nc ${var}_eraint_griddfs_${year0}-${yearf}_clim_smoothed_1.nc ${var}_eraint_griddfs_${year0}-${yearf}_clim_smoothed_2.nc for ((year=$year0;year<=$yearf;year++)) ; do cdo sub ${var}_eraint_griddfs_${year}.nc ${var}_eraint_griddfs_${year0}-${yearf}_clim.nc ${var}_ano_eraint_griddfs_${year}.nc cdo sub ${var}_dfs4.3_${year}.nc ${var}_dfs4.3_${year0}-${yearf}_clim.nc ${var}_ano_dfs4.3_${year}.nc cdo sub ${var}_ano_dfs4.3_${year}.nc ${var}_ano_eraint_griddfs_${year}.nc ${var}_ano_dfs4.3-eraint_${year}.nc rm -f ${var}_eraint_griddfs_${year}.nc ${var}_dfs4.3_${year}.nc ${var}_ano_eraint_griddfs_${year}.nc ${var}_ano_dfs4.3_${year}.nc done fi fi if [[ $reorg == T ]] ; then for ((jt=1;jt<=$nstep;jt++)) ; do ncrcat -O -d time,$((jt-1)),,$nstep -n $((yearf-year0+1)),4,1 ${var}_${wih}${dif}_${year0}.nc ${var}_${wih}${dif}_${year0}-${yearf}_$jt.nc done mkdir -p ${wih}${dir0} mv ${var}_${wih}${dif}_* ${wih}${dir0}/. fi if [[ $raw == T && $ano == F ]] ; then for ((year=$year0;year<=$yearf;year++)) ; do if [[ $dfs2era == T ]] ; then rm -f ${var}_eraint_${year}.nc ${var}_dfs4.3_gridera_${year}.nc else rm -f ${var}_eraint_griddfs_${year}.nc ${var}_dfs4.3_${year}.nc fi done fi if [[ $monmean == T ]] ; then jt0=1 for ((mon=1;mon<=12;mon++)) ; do case $mon in 1|3|5|7|8|10|12) ndays=31 ;; 4|6|9|11) ndays=30 ;; 2) ndays=28 ;; esac lstfiles="" case $nstep in 1460) ntimes=$((ndays*4+jt0-1)) ;; 365) ntimes=$((ndays+jt0-1)) ;; 12) ntimes=1 ;; esac for ((jt=$jt0;jt<=$ntimes;jt++)) ;do lstfiles=$lstfiles" "${wih}${dir0}/${var}_${wih}${dif}_${year0}-${yearf}_$jt.nc done ncea -O $lstfiles ${wih}${dir0}/${var}_${wih}${dif}_${year0}-${yearf}_mon$(printf "%02d" ${mon}).nc case $nstep in 1460) jt0=$((${jt0}+ndays*4)) ;; 365) jt0=$((${jt0}+ndays)) ;; 12) jt0=$((${jt0}+1)) ;; esac done fi done if [[ $pickup1 == T ]] ; then if [[ $monmean == T ]] ; then dirpert=perturb1_${dir0}_mon nstep=12 else dirpert=perturb1_${dir0} fi mkdir -p $dirpert RANGE=$((${yearf}-${year0}+1)) for ((jt=1;jt<=$nstep;jt++)) ; do for memb in ${lstmbs[@]} ; do for ((year=$year1;year<=$year2;year++)) ; do number=$((RANDOM%${RANGE})) for var in ${lstvars[@]} ; do if [[ $monmean == T ]] ; then filein=${var}_${wih}${dif}_${year0}-${yearf}_mon$(printf "%02d" ${jt}).nc else filein=${var}_${wih}${dif}_${year0}-${yearf}_$jt.nc fi ncks -O -d time,$number ${wih}${dir0}/${filein} ${dirpert}/${var}_fc${memb}_${year}_$(printf "%04d" $jt).nc done done done done fi if [[ $pickup2 == T ]] ; then mkdir -p perturb2_${dir0} RANGE=$((${yearf}-${year0}+1)) for ((jt=1;jt<=$nstep;jt++)) ; do for memb in ${lstmbs[@]} ; do for ((year=$year1;year<=$year2;year++)) ; do for ((jlaunch=1;jlaunch<=2;jlaunch++)) ; do number=$((RANDOM%${RANGE})) for var in ${lstvars[@]} ; do ncks -O -d time,$number ${wih}${dir0}/${var}_${wih}${dif}_${year0}-${yearf}_$jt.nc perturb2_${dir0}/${var}_fc${memb}_${year}_$(printf "%04d" $jt)_$jlaunch.nc done done for var in ${lstvars[@]} ; do ncea -O perturb2_${dir0}/${var}_fc${memb}_${year}_$(printf "%04d" $jt)_1.nc perturb2_${dir0}/${var}_fc${memb}_${year}_$(printf "%04d" $jt)_2.nc perturb2_${dir0}/${var}_fc${memb}_${year}_$(printf "%04d" $jt).nc rm -f perturb2_${dir0}/${var}_fc${memb}_${year}_$(printf "%04d" $jt)_1.nc perturb2_${dir0}/${var}_fc${memb}_${year}_$(printf "%04d" $jt)_2.nc done done done done fi if [[ $pickup1 == T || $pickup2 == T ]] ; then lstdirs="" if [[ $pickup1 == T ]] ; then if [[ $monmean == T ]] ; then lstdirs=$lstdirs" "perturb1_${dir0}_mon else lstdirs=$lstdirs" "perturb1_${dir0} fi fi if [[ $pickup2 == T ]] ; then lstdirs=$lstdirs" "perturb2_${dir0} fi for dir in $lstdirs ; do for memb in ${lstmbs[@]} ; do for ((year=$year1;year<=$year2;year++)) ; do for var in ${lstvars[@]} ; do ncrcat -O -n $nstep,4,1 ${dir}/${var}_fc${memb}_${year}_0001.nc ${dir}/${var}_fc${memb}_${year}.nc rm -f ${dir}/${var}_fc${memb}_${year}_*.nc if [[ $monmean == T ]] ; then cdo settaxis,${year}-01-15,12:00,1mon ${dir}/${var}_fc${memb}_${year}.nc ${dir}/${var}_fc${memb}_${year}_bis.nc rm ${dir}/${var}_fc${memb}_${year}.nc mv ${dir}/${var}_fc${memb}_${year}_bis.nc ${dir}/${var}_fc${memb}_${year}.nc fi if [[ $dfs2era == T && $monmean == F ]] ; then case $year in 1980|1984|1988|1992|1996|2000|2004|2008|2012|2016|2020) case $nstep in 1460) ncks -O -d time,0,232 ${dir}/${var}_fc${memb}_${year}.nc ${dir}/${var}_fc${memb}_${year}_1.nc ncks -O -d time,229,232 ${dir}/${var}_fc${memb}_${year}.nc ${dir}/${var}_fc${memb}_${year}_2.nc ncks -O -d time,233, ${dir}/${var}_fc${memb}_${year}.nc ${dir}/${var}_fc${memb}_${year}_3.nc ;; 365) ncks -O -d time,0,58 ${dir}/${var}_fc${memb}_${year}.nc ${dir}/${var}_fc${memb}_${year}_1.nc ncks -O -d time,58 ${dir}/${var}_fc${memb}_${year}.nc ${dir}/${var}_fc${memb}_${year}_2.nc ncks -O -d time,59, ${dir}/${var}_fc${memb}_${year}.nc ${dir}/${var}_fc${memb}_${year}_3.nc ;; esac ncrcat -O ${dir}/${var}_fc${memb}_${year}_1.nc ${dir}/${var}_fc${memb}_${year}_2.nc ${dir}/${var}_fc${memb}_${year}_3.nc ${dir}/${var}_fc${memb}_${year}.nc rm -f ${dir}/${var}_fc${memb}_${year}_1.nc ${dir}/${var}_fc${memb}_${year}_2.nc ${dir}/${var}_fc${memb}_${year}_3.nc esac fi done done done done fi if [[ $monmean == T ]] ; then for memb in ${lstmbs[@]} ; do for var in ${lstvars[@]} ; do ncrcat -O -n $((${year2}-${year1}+1)),4,1 perturb1_${dir0}_mon/${var}_fc${memb}_${year1}.nc perturb1_${dir0}_mon/${var}_fc${memb}_${year1}-${year2}.nc cdo inttime,${year1}-01-15,12:00,6hour perturb1_${dir0}_mon/${var}_fc${memb}_${year1}-${year2}.nc perturb1_${dir0}_mon/${var}_fc${memb}_${year1}-${year2}_6hourly.nc rm -f perturb1_${dir0}_mon/${var}_fc${memb}_${year1}-${year2}.nc cdo splityear perturb1_${dir0}_mon/${var}_fc${memb}_${year1}-${year2}_6hourly.nc perturb1_${dir0}_mon/${var}_fc${memb}_ rm -f perturb1_${dir0}_mon/${var}_fc${memb}_${year1}-${year2}_6hourly.nc if [[ $dfs2era == F ]] ; then lstyears=( 1960 1964 1968 1972 1976 1980 1984 1988 1992 1996 2000 2004 2008 2012 2016 2020 ) for year in ${lstyears[@]} ; do if [[ -e perturb1_${dir0}_mon/${var}_fc${memb}_${year}.nc ]] ; then case $nstep in 1460) ncks -O -d time,0,232 perturb1_${dir0}_mon/${var}_fc${memb}_${year}.nc perturb1_${dir0}_mon/${var}_fc${memb}_${year}_1.nc ncks -O -d time,237, perturb1_${dir0}_mon/${var}_fc${memb}_${year}.nc perturb1_${dir0}_mon/${var}_fc${memb}_${year}_2.nc ;; 365) ncks -O -d time,0,58 perturb1_${dir0}_mon/${var}_fc${memb}_${year}.nc perturb1_${dir0}_mon/${var}_fc${memb}_${year}_1.nc ncks -O -d time,60, perturb1_${dir0}_mon/${var}_fc${memb}_${year}.nc perturb1_${dir0}_mon/${var}_fc${memb}_${year}_2.nc ;; esac ncrcat -O perturb1_${dir0}_mon/${var}_fc${memb}_${year}_1.nc perturb1_${dir0}_mon/${var}_fc${memb}_${year}_2.nc perturb1_${dir0}_mon/${var}_fc${memb}_${year}.nc rm -f perturb1_${dir0}_mon/${var}_fc${memb}_${year}_1.nc perturb1_${dir0}_mon/${var}_fc${memb}_${year}_2.nc fi done fi done done fi