ece2evt-nemo.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/bin/bash
  2. set -eu
  3. # experiemnt name
  4. expname=$1
  5. # read configuration file
  6. . $2
  7. tmpdir=$tmpdir_root/nemo
  8. mkdir -p $tmpdir
  9. start_yyyymmdd=${start_yyyy}0101
  10. end_yyymmdd=${end_yyyy}1231
  11. d=$resdir
  12. nprocs=$nprocs_nemo
  13. # land-sea mask for nemo
  14. lsm_file=/nobackup/rossby17/sm_wyser/ece2esmvaltool/lsm_orca1.nc
  15. # area for each ocean gridcell (undef over land)
  16. area_file=/nobackup/rossby17/sm_wyser/ece2esmvaltool/area_orca1.nc
  17. # variable names to be processed
  18. var_2d_list="tos sic"
  19. var_1d_list="tos sos zos heatc saltc sienh siesh sivolnh sivolsh"
  20. var_list=
  21. for var in $var_2d_list $var_1d_list
  22. do
  23. awk -v var=$var -f split_ptab.awk $nemo_par > $tmpdir/nemo-par-$var
  24. eval $(sed -n "/name[[:blank:]]*=/ p" $tmpdir/nemo-par-$var | tr -d [:blank:])
  25. var_list=$var_list" "$name
  26. done
  27. # remove duplicates
  28. var_list=$(echo $var_list | tr '[:blank:]' '\n' | sort | uniq | tr '\n' ' ')
  29. # create list with filenames to be included
  30. # NB: the approach with "find" may not be the best possible choice in case of
  31. # very long simulations
  32. rm -f $tmpdir/fname_list
  33. for fname in $(find $d/nemo -regextype posix-extended -regex '.*_1m_.*(grid_T|icemod).nc$' | sort )
  34. do
  35. f=$(basename $fname)
  36. fstart=$(echo $f | cut -d _ -f 3)
  37. fend=$(echo $f | cut -d _ -f 4)
  38. [ $fend -ge $start_yyyymmdd ] && [ $fstart -le $end_yyymmdd ] && echo $fname >> $tmpdir/fname_list
  39. done
  40. # loop over all filenames; execute in parallel
  41. icount=0
  42. while read fname
  43. do
  44. (( icount+=1 ))
  45. echo "...now processing $(basename $fname)"
  46. iiii=$(printf %04d $icount)
  47. # cdo on bi has problems with pipes and compressed netcdf files
  48. # therefore save temporary files with -f nc (maybe only on bi?)
  49. cdo -s -f nc splitvar -ifthen $lsm_file \
  50. -selvar,$(echo $var_list | tr -s '[:blank:]' ',') \
  51. $fname $tmpdir/x_${iiii}_ &
  52. if (( icount%nprocs == 0 )); then wait; fi
  53. done < $tmpdir/fname_list
  54. wait
  55. # concatenate files
  56. icount=0
  57. for var in $var_list
  58. do
  59. (( icount+=1 ))
  60. echo "...concatenating $var"
  61. ( rm -f $tmpdir/y_${var}.nc
  62. cdo -s cat $tmpdir/x_*_$var.nc $tmpdir/y_${var}.nc
  63. # change time variable
  64. ncrename -O \
  65. -d time_counter,time -v time_counter,time \
  66. $tmpdir/y_${var}.nc $tmpdir/z_${var}.nc ) &
  67. if (( icount%nprocs == 0 )); then wait; fi
  68. done
  69. wait
  70. # save files
  71. icount=0
  72. for out_name in $var_2d_list
  73. do
  74. (( icount+=1 ))
  75. echo "...now processing $out_name"
  76. ( # get variable name from $nemo_par
  77. var=$(grep ' name ' $tmpdir/nemo-par-$out_name | cut -d = -f 2 | tr -d '[:blank:]')
  78. # convert units (this may become superfluos once UDUNITS2 support
  79. # has been activated when compiling cdo
  80. case $out_name in
  81. ( tos ) unit_conv='-addc,273.15' ;;
  82. ( sic ) unit_conv='-mulc,100' ;;
  83. ( * ) unit_conv= ;;
  84. esac
  85. outfile=$outdir/CMIP5_Omon_historical_${expname}_r1i1p1_T2Ms_${out_name}_${start_yyyy}-${end_yyyy}.nc
  86. rm -f $outfile
  87. cdo -s -f nc4c -z zip setpartabn,$tmpdir/nemo-par-$out_name \
  88. -setreftime,$refdate,00:00:00,days \
  89. -invertlat \
  90. $unit_conv -remapdis,n128 \
  91. $tmpdir/z_${var}.nc $outfile ) &
  92. if (( icount%nprocs == 0 )); then wait; fi
  93. done
  94. wait
  95. icount=0
  96. for out_name in $var_1d_list
  97. do
  98. (( icount+=1 ))
  99. echo "...now processing $out_name"
  100. ( # get variable name from $nemo_par
  101. var=$(grep ' name ' $tmpdir/nemo-par-$out_name | cut -d = -f 2 | tr -d '[:blank:]')
  102. # convert units (this may become superfluos once UDUNITS2 support
  103. # has been activated when compiling cdo
  104. case $out_name in
  105. ( tos ) unit_conv='-addc,273.15' ;;
  106. ( sie* ) unit_conv='-divc,1e12' ;;
  107. ( sivol* ) unit_conv='-divc,1e12' ;;
  108. ( * ) unit_conv= ;;
  109. esac
  110. # threshold for sea-ice extent
  111. case $out_name in
  112. ( sie* ) ext_thresh='-gtc,.15' ;;
  113. ( * ) ext_thresh= ;;
  114. esac
  115. outfile=$outdir/CMIP5_Omon_historical_${expname}_r1i1p1_T1Ms_${out_name}_${start_yyyy}-${end_yyyy}.nc
  116. rm -f $outfile
  117. case $out_name in
  118. ( sienh | sivolnh )
  119. cdo -s -f nc4c -z zip setpartabn,$tmpdir/nemo-par-$out_name \
  120. -setreftime,$refdate,00:00:00,days $unit_conv \
  121. -fldsum -maskindexbox,1,362,147,292 \
  122. -mul $ext_thresh $tmpdir/z_${var}.nc $area_file \
  123. $outfile ;;
  124. ( siesh | sivolsh )
  125. cdo -s -f nc4c -z zip setpartabn,$tmpdir/nemo-par-$out_name \
  126. -setreftime,$refdate,00:00:00,days $unit_conv \
  127. -fldsum -maskindexbox,1,362,1,146 \
  128. -mul $ext_thresh $tmpdir/z_${var}.nc $area_file \
  129. $outfile ;;
  130. ( * )
  131. cdo -s -f nc4c -z zip setpartabn,$tmpdir/nemo-par-$out_name \
  132. -setreftime,$refdate,00:00:00,days $unit_conv \
  133. -div -fldsum -mul $area_file $tmpdir/z_${var}.nc \
  134. -fldsum $area_file $outfile ;;
  135. esac ) &
  136. if (( icount%nprocs == 0 )); then wait; fi
  137. done
  138. wait
  139. # clean up
  140. rm -r $tmpdir
  141. echo "!!! ece2evt-nemo done !!!"
  142. exit 0