postprocess.bash 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/bin/bash
  2. #SBATCH -n 1
  3. #SBATCH -t 12:00:00
  4. #SBATCH -J post_perturbation
  5. #SBATCH -o slurm_post-%j.out
  6. #SBATCH -e slurm_post-%j.err
  7. set -o nounset
  8. set -o errexit
  9. set -x
  10. # -----------------------------------------------------------------------------
  11. # Author - F. Massonnet
  12. # Purpose- Process perturbations of DFS5.2 forcing and adds them
  13. # to the actual forcing.
  14. # What the script does:
  15. # 1) Interpolate from daily to 3hourly
  16. # 2) Add 29 February if necessary
  17. # 3) Add to the true forcing
  18. # -----------------------------------------------------------------------------
  19. # Which variable of the DFS5.2 forcing set has to be perturbed?
  20. # Possibilities: t2, q2, u10, v10, qlw, qsw, snow or precip
  21. var=qsw
  22. alpha=1.0 # VERY IMPORTANT: strength of the perturbation. 1 = same as interannual variability. 0.5 = half of it.
  23. # First and end years for which a perturbation has to be created.
  24. yearb=1973
  25. yeare=1973
  26. yearbp=1979 # First and end years defining the reference period on which
  27. yearep=2015 # the perturbations were created (must match those in preprocess.bash and in mkpert.R)
  28. nmb=1 # members to loop over. nmb = first member; nme = last one.
  29. # Usually the first member ("fc0") is the true forcing so it should *NOT* be perturbed.
  30. # Hence put nmb=1 and nme=25 if you want 25 perturbed forcings
  31. nme=25
  32. workdir=/esnas/scratch/$USER/TMP/TMP_${var}_26904/ # Where all perturbations are recorded
  33. # This folder was defined during the execution of preprocess.bash
  34. outtag=DFS5.2 # Name of the forcing after perturbations are created
  35. # Can be the same (DFS5.2) or modified (perturbed-DFS5.2 for instance)
  36. outdir=/esarchive/releases/fg/ocean/$outtag/
  37. cd $workdir
  38. echo "Workdir is $workdir"
  39. case ${var} in
  40. t2)
  41. min=100.0 # Min and max values allowed
  42. max=400.0
  43. freq=3hour # Frequency of availability
  44. ntim=2920 # Number of time steps in a year
  45. fvar=${var} # Name of the variable in the NetCDF
  46. ;;
  47. q2)
  48. min=0.0
  49. max=0.1
  50. freq=3hour
  51. ntim=2920
  52. fvar=${var}
  53. ;;
  54. u10)
  55. min=-100.0
  56. max=100.0
  57. freq=3hour
  58. ntim=2920
  59. fvar=${var}
  60. ;;
  61. v10)
  62. min=-100.0
  63. max=100.0
  64. freq=3hour
  65. ntim=2920
  66. fvar=${var}
  67. ;;
  68. qsw)
  69. min=0.0
  70. max=1000.0
  71. freq=1day
  72. ntim=365
  73. fvar=radsw
  74. ;;
  75. qlw)
  76. min=0.0
  77. max=1000.0
  78. freq=1day
  79. ntim=365
  80. fvar=radlw
  81. ;;
  82. snow)
  83. min=0.0
  84. max=0.001
  85. freq=1day
  86. ntim=365
  87. fvar=${var}
  88. ;;
  89. precip)
  90. min=0.0
  91. max=0.01
  92. freq=1day
  93. ntim=365
  94. fvar=${var}
  95. ;;
  96. *)
  97. echo "Variable $var unknown"
  98. exit
  99. esac
  100. for year in `seq ${yearb} ${yeare}`
  101. do
  102. for mm in `seq $nmb $nme`
  103. do
  104. if [ $mm == 0 ]
  105. then
  106. echo "WARNING!!!!"
  107. echo "Usually, at BSC, member 0 is booked to point towards the true forcing"
  108. echo "By creating a perturbed forcing named fc00, and then copying to the source"
  109. echo "directory, you are going to erase a symbolic link that points to the true forcing"
  110. echo "and thereby erase the true forcing!!"
  111. echo "Since this is highly dangerous, this script is aborting."
  112. echo "Contact francois.massonnet@bsc.es for further questions"
  113. exit
  114. fi
  115. m=$(printf "%02d" $mm)
  116. # There is a small trick here. If we have 3 days of data and ask for 3-hourly interpolation, we will have only 17 points and not 24.
  117. # We have in fact (ndays - 1) * 8 + 1. So the trick is to append the last day to the data twice and remove the last time frame.
  118. #
  119. # Extract the last time frame
  120. ncks -F -O -d time,365,365 pert_${var}_DFS5.2_${year}_fc${m}_ref${yearbp}-${yearep}.nc tmp.${year}.${m}.nc
  121. # Append it
  122. ncrcat -F -O pert_${var}_DFS5.2_${year}_fc${m}_ref${yearbp}-${yearep}.nc tmp.${year}.${m}.nc pert_${var}_DFS5.2_${year}_fc${m}.nc.1
  123. # Set time axis
  124. cdo settaxis,${year}-01-01,00:00,1day pert_${var}_DFS5.2_${year}_fc${m}.nc.1 pert_${var}_DFS5.2_${year}_fc${m}.nc.2
  125. # Interpolate in time
  126. cdo inttime,${year}-01-01,00:00,${freq} pert_${var}_DFS5.2_${year}_fc${m}.nc.2 pert_${var}_DFS5.2_${year}_fc${m}.nc.3
  127. # Remove the last time frame
  128. ncks -F -O -d time,1,${ntim} pert_${var}_DFS5.2_${year}_fc${m}.nc.3 pert_${var}_DFS5.2_${year}_fc${m}.nc.4
  129. # Add the desired fraction "alpha" of the perturbation to the true forcing
  130. cdo add -mulc,${alpha} pert_${var}_DFS5.2_${year}_fc${m}.nc.4 ${var}_DFS5.2_${year}.nc ${var}_fc${m}_DFS5.2_${year}.nc.0
  131. # Physical bounds
  132. cdo setrtoc,-10000000000,${min},${min} ${var}_fc${m}_DFS5.2_${year}.nc.0 ${var}_fc${m}_DFS5.2_${year}.nc.1
  133. cdo setrtoc,${max},10000000000,${max} ${var}_fc${m}_DFS5.2_${year}.nc.1 ${var}_fc${m}_DFS5.2_${year}.nc.2
  134. # Set time units to allow nice reading
  135. cdo settunits,years ${var}_fc${m}_DFS5.2_${year}.nc.2 ${outdir}/${var}_fc${m}_${outtag}_${year}.nc
  136. # Add description in the header
  137. ncatted -O -a description,${fvar},a,c,"Perturbed version of DFS5.2 variable ${fvar} for year ${year} (member fc${m}). Strength of perturbation is ${alpha} times the year-to-year differences estimated over the ${yearbp}-${yearep} reference period. For more details: francois.massonnet@bsc.es" ${outdir}/${var}_fc${m}_${outtag}_${year}.nc
  138. chmod 777 ${outdir}/${var}_fc${m}_${outtag}_${year}.nc
  139. rm -f tmp.${year}.${m}.nc pert_${var}_DFS5.2_${year}_fc${m}.nc.? ${var}_fc${m}_DFS5.2_${year}.nc.?
  140. done
  141. done
  142. echo "SCRIPT POSTPROCESS.BASH FINISHED"