ece2evt-ifs.sh 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #!/bin/bash
  2. set -eu
  3. # experiment name
  4. expname=$1
  5. # read configuration file
  6. . $2
  7. tmpdir=$tmpdir_root/ifs
  8. mkdir -p $tmpdir
  9. start_yyyymm=${start_yyyy}01
  10. end_yyyymm=${end_yyyy}12
  11. d=$resdir
  12. nprocs=$nprocs_ifs
  13. # frequency of PP output in IFS (needed to normalise accumulated variables)
  14. ifs_pp_freq=$((6*3600))
  15. # area for each ocean gridcell (undef over land)
  16. area_file=/nobackup/rossby17/sm_wyser/ece2esmvaltool/area_a255.nc
  17. # list of requested output variables
  18. var_2d_list="clt pr pr-mmday rlut rsut tas psl hfls hfss tauu tauv zg-500 ta-200 ta-850 ua-200 ua-850 va-200 va-850 snd"
  19. var_1d_list="clt tas pr-mmday pme-mmday rnds rndt"
  20. # create parameter list
  21. param_list=
  22. for var in $var_2d_list $var_1d_list
  23. do
  24. awk -v var=$var -f split_ptab.awk $ifs_par > $tmpdir/ifs-par-$var
  25. eval $(sed -n "/param[[:blank:]]*=/ p" $tmpdir/ifs-par-$var | tr -d [:blank:])
  26. param_list=$param_list" "$param
  27. done
  28. # remove duplicates
  29. param_list=$(echo $param_list | tr '[:blank:]' '\n' | sort | uniq | tr '\n' ' ')
  30. # create new parameter list, add parameters for derived variables
  31. param_list_ifs=
  32. for p in $param_list
  33. do
  34. case $p in
  35. ( 228.128 | 103.228 ) param_list_ifs+=",142.128,143.128" ;;
  36. ( 101.128 ) param_list_ifs+=",142.128,143.128,182.128" ;;
  37. ( 97.128 ) param_list_ifs+=",178.128,212.128" ;;
  38. ( 98.128 ) param_list_ifs+=",179.128" ;;
  39. ( 99.128 ) param_list_ifs+=",178.128,179.128" ;;
  40. ( 100.128 ) param_list_ifs+=",176.128,177.128,146.128,147.128" ;;
  41. ( 102.128 ) param_list_ifs+=",141.128,33.128" ;;
  42. ( * ) param_list_ifs+=",$p" ;;
  43. esac
  44. done
  45. # remove duplicates
  46. param_list_ifs=$(echo $param_list_ifs | tr ',' '\n' | sort | uniq | tr '\n' ',')
  47. # remove leading and trailing comma
  48. param_list_ifs=${param_list_ifs:1:${#param_list_ifs}-2}
  49. # create list with filenames to be included
  50. # NB: the approach with "find" may not be the best possible choice in case of
  51. # very long simulations
  52. rm -f $tmpdir/icmgg_list $tmpdir/icmsh_list
  53. for fname in $(find $d/ifs -regextype posix-extended -regex '.*/ICMGG.*+[0-9]{6}$' | sort )
  54. do
  55. yyyymm=${fname:${#fname}-6:6}
  56. [ $yyyymm -ge $start_yyyymm ] && [ $yyyymm -le $end_yyyymm ] && echo $fname >> $tmpdir/icmgg_list
  57. done
  58. # create similar list for ICMSH files
  59. sed -e's/ICMGG/ICMSH/g' $tmpdir/icmgg_list > $tmpdir/icmsh_list
  60. # loop over all ICMSH files; execute in parallel
  61. icount=0
  62. while read fname
  63. do
  64. (( icount+=1 ))
  65. echo "...now processing $(basename $fname)"
  66. yyyymm=${fname:${#fname}-6:6}
  67. yyyy=${yyyymm:0:4}
  68. mm=${yyyymm:4:2}
  69. # extract variables and compute monthly mean
  70. ( cdo -s splitparam -sp2gpl -setdate,$yyyy-$mm-01 -timmean \
  71. -selparam,130.128,131.128,132.128 -sellevel,20000,85000 \
  72. $fname $tmpdir/x_${yyyymm}_
  73. cdo -s splitparam -sp2gpl -setdate,$yyyy-$mm-01 -timmean \
  74. -selparam,129.128 -sellevel,50000 \
  75. $fname $tmpdir/x_${yyyymm}_ ) &
  76. if (( icount%nprocs == 0 )); then wait; fi
  77. done < $tmpdir/icmsh_list
  78. wait
  79. # loop over all ICMGG files; execute in parallel
  80. icount=0
  81. while read fname
  82. do
  83. (( icount+=1 ))
  84. echo "...now processing $(basename $fname)"
  85. yyyymm=${fname:${#fname}-6:6}
  86. yyyy=${yyyymm:0:4}
  87. mm=${yyyymm:4:2}
  88. # extract variables and compute monthly mean
  89. cdo -s splitparam -setdate,$yyyy-$mm-01 -timmean \
  90. -selparam,$param_list_ifs $fname $tmpdir/x_${yyyymm}_ &
  91. if (( icount%nprocs == 0 )); then wait; fi
  92. done < $tmpdir/icmgg_list
  93. wait
  94. # concatenate all files
  95. icount=0
  96. for param in $(echo $param_list_ifs | tr ',' ' ')
  97. do
  98. (( icount+=1 ))
  99. rm -f $tmpdir/x_${param}.grb
  100. cdo -s cat $tmpdir/x_*_${param}.grb $tmpdir/x_${param}.grb &
  101. if (( icount%nprocs == 0 )); then wait; fi
  102. done
  103. wait
  104. # change units
  105. icount=0
  106. for param in $param_list
  107. do
  108. (( icount+=1 ))
  109. echo "...now processing parameter $param"
  110. ( case $param in
  111. ( 146.128 | 147.128 | 176.128 | 169.128 | 177.128 | 175.128 | 179.128 | 212.128 | 95.128 | 96.128 | 209.128 | 180.128 | 181.128 )
  112. # radiation/heat
  113. cdo_operator="divc,$ifs_pp_freq" ;;
  114. ( 97.128 )
  115. # upwelling SW radiation
  116. cdo -s setparam,97.128 \
  117. -divc,$ifs_pp_freq \
  118. -sub $tmpdir/x_212.128.grb $tmpdir/x_178.128.grb \
  119. $tmpdir/x_97.128.grb
  120. cdo_operator="" ;;
  121. ( 98.128 )
  122. # upwelling LW radiation
  123. cdo -s setparam,98.128 \
  124. -divc,$ifs_pp_freq \
  125. -mulc,-1 $tmpdir/x_179.128.grb $tmpdir/x_98.128.grb
  126. cdo_operator="" ;;
  127. ( 99.128 )
  128. # net TOA radiation
  129. cdo -s setparam,99.128 \
  130. -divc,$ifs_pp_freq \
  131. -add $tmpdir/x_178.128.grb $tmpdir/x_179.128.grb \
  132. $tmpdir/x_99.128.grb
  133. cdo_operator="" ;;
  134. ( 100.128 )
  135. # net SFC energy flux
  136. cdo -s setparam,100.128 \
  137. -divc,$ifs_pp_freq \
  138. -add $tmpdir/x_146.128.grb \
  139. -add $tmpdir/x_147.128.grb \
  140. -add $tmpdir/x_176.128.grb $tmpdir/x_177.128.grb \
  141. $tmpdir/x_100.128.grb
  142. cdo_operator="" ;;
  143. ( 142.128 | 143.128 | 144.128 | 205.128 | 182.128 )
  144. # snow/precipitation/evaporation/runoff
  145. cdo_operator="mulc,1000 -divc,$ifs_pp_freq" ;;
  146. ( 228.128 )
  147. # total precipitation rate in kg m-2 s-1
  148. cdo -s setparam,228.128 \
  149. -mulc,1000 -divc,$ifs_pp_freq \
  150. -add $tmpdir/x_142.128.grb $tmpdir/x_143.128.grb \
  151. $tmpdir/x_228.128.grb
  152. cdo_operator="" ;;
  153. ( 103.128 )
  154. # total precipitation in mm/day
  155. cdo -s setparam,103.228 \
  156. -mulc,86.4e6 -divc,$ifs_pp_freq \
  157. -add $tmpdir/x_142.128.grb $tmpdir/x_143.128.grb \
  158. $tmpdir/x_103.128.grb
  159. cdo_operator="" ;;
  160. ( 101.128 )
  161. # precipitation minus evaporation in mm/day
  162. cdo -s setparam,101.128 \
  163. -mulc,86.4e6 -divc,$ifs_pp_freq \
  164. -add $tmpdir/x_182.128.grb \
  165. -add $tmpdir/x_142.128.grb $tmpdir/x_143.128.grb \
  166. $tmpdir/x_101.128.grb
  167. cdo_operator="" ;;
  168. ( 164.128 )
  169. # cloud cover
  170. cdo_operator="mulc,100" ;;
  171. ( 129.128 )
  172. # geopotential meters
  173. cdo_operator="divc,9.81" ;;
  174. ( 102.128 )
  175. # snow depth (make sure we have snow density (33.128) or an estimate)
  176. [ -f $tmpdir/x_33.128.grb ] \
  177. && div_dens="-div $tmpdir/x_141.128.grb $tmpdir/x_33.128.grb" \
  178. || div_dens="-divc,350 $tmpdir/x_141.128.grb"
  179. cdo -s setparam,102.128 \
  180. -mulc,1000 $div_dens $tmpdir/x_102.128.grb
  181. cdo_operator="" ;;
  182. ( * )
  183. # default
  184. cdo_operator="" ;;
  185. esac
  186. if [ -n "$cdo_operator" ]
  187. then
  188. cdo -s $cdo_operator \
  189. $tmpdir/x_${param}.grb $tmpdir/y_${param}.grb
  190. else
  191. mv $tmpdir/x_${param}.grb $tmpdir/y_${param}.grb
  192. fi ) &
  193. if (( icount%nprocs == 0 )); then wait; fi
  194. done
  195. wait
  196. # split levels (if necessary) and save as compressed netCDF
  197. icount=0
  198. for var in $var_2d_list
  199. do
  200. (( icount+=1 ))
  201. echo "...now saving $var"
  202. ( # find param in unique parameter table
  203. eval $(sed -n "/param[[:blank:]]*=/ p" $tmpdir/ifs-par-$var | tr -d [:blank:])
  204. # level and field type for ESMValTool
  205. case $var in
  206. ( ta-[0-9]* | ua-[0-9]* | va-[0-9]* | zg-[0-9]* )
  207. sellev="-sellevel,$((100*${var##*[a-z,A-Z]-}))"
  208. evt_ftype='T2M' ;;
  209. ( * )
  210. sellev=
  211. evt_ftype='T2Ms' ;;
  212. esac
  213. # name of output file
  214. outfile=$outdir/CMIP5_Amon_historical_${expname}_r1i1p1_${evt_ftype}_${var}_${start_yyyy}-${end_yyyy}.nc
  215. rm -f $outfile
  216. if [ $evt_ftype = 'T2M' ]
  217. then
  218. cdo -s -f nc4c -z zip -R setpartabp,$tmpdir/ifs-par-$var \
  219. -setreftime,$refdate,00:00:00,days \
  220. -invertlat $sellev \
  221. $tmpdir/y_${param}.grb $tmpdir/z_${var}.nc
  222. # remove unused vertical dimension
  223. ncwa -a lev $tmpdir/z_${var}.nc $outfile
  224. else
  225. cdo -s -f nc4c -z zip -R setpartabp,$tmpdir/ifs-par-$var \
  226. -setreftime,$refdate,00:00:00,days \
  227. -invertlat $sellev \
  228. $tmpdir/y_${param}.grb $outfile
  229. fi ) &
  230. if (( icount%nprocs == 0 )); then wait; fi
  231. done
  232. wait
  233. # split levels (if necessary) and save as compressed netCDF
  234. icount=0
  235. for var in $var_1d_list
  236. do
  237. (( icount+=1 ))
  238. echo "...now saving $var"
  239. ( # find param in unique parameter table
  240. eval $(sed -n "/param[[:blank:]]*=/ p" $tmpdir/ifs-par-$var | tr -d [:blank:])
  241. # level and field type for ESMValTool
  242. case $var in
  243. ( rndt )
  244. evt_ftype='T1M' ;;
  245. ( * )
  246. evt_ftype='T1Ms' ;;
  247. esac
  248. # name of output file
  249. outfile=$outdir/CMIP5_Amon_historical_${expname}_r1i1p1_${evt_ftype}_${var}_${start_yyyy}-${end_yyyy}.nc
  250. rm -f $outfile
  251. cdo -s -f nc4c -z zip -R setpartabp,$tmpdir/ifs-par-$var \
  252. -setreftime,$refdate,00:00:00,days \
  253. -fldmean \
  254. $tmpdir/y_${param}.grb $outfile
  255. # ocean only
  256. case $var in
  257. ( rnds | pme-mmday | pr-mmday )
  258. outfile_oce=${outfile/.nc/_ocean.nc}
  259. rm -f $outfile_oce
  260. cdo -s -f nc4c -z zip setpartabp,$tmpdir/ifs-par-$var \
  261. -setreftime,$refdate,00:00:00,days \
  262. -div -fldsum -mul $tmpdir/y_${param}.grb $area_file \
  263. -fldsum $area_file $outfile_oce
  264. esac ) &
  265. if (( icount%nprocs == 0 )); then wait; fi
  266. done
  267. wait
  268. # clean up
  269. rm -r $tmpdir
  270. echo "!!! ece2evt-ifs done !!!"
  271. exit 0