extract_daily_forcing.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/bin/bash
  2. # This script creates netcdf files used by sparring to drive LPJG offline from raw IFS output.
  3. #
  4. # Created By Klaus Wyser, some modifications by Etienne Tourigny
  5. set -xuve
  6. # ==============================================================================
  7. # edit these variables
  8. expname=a0l3; npts=88838
  9. #expname=AHIL; npts=35718
  10. y1=1990
  11. y2=1990
  12. y0=1990
  13. srcdir=/gpfs/scratch/bsc32/bsc32051/lpjg/a0l3/in
  14. outdir=/gpfs/scratch/bsc32/bsc32051/lpjg/a0l3/out
  15. #tmpdir=$SNIC_TMP
  16. tmpdir=/gpfs/scratch/bsc32/bsc32051/lpjg/tmp
  17. #nprocs=$SLURM_NPROCS
  18. nprocs=1
  19. # ==============================================================================
  20. mkdir -p $outdir
  21. mkdir -p $tmpdir
  22. cd $tmpdir
  23. # create grib_filter script
  24. # add 169 (ssrd) and 175 (strd) for surface downward radiation
  25. # LPJG currently uses surface net radiation
  26. sstring=
  27. for c in 39 40 41 42 139 167 170 183 236; do
  28. sstring+=" || marsParam is \"$c.128\""
  29. done
  30. sstring=${sstring:4:${#sstring}}
  31. fstring=
  32. for c in 142 143 144 176 177; do
  33. fstring+=" || marsParam is \"$c.128\""
  34. done
  35. fstring=${fstring:4:${#fstring}}
  36. cat > gf << EOT
  37. if ( $sstring ) { write "x_state" ; }
  38. if ( $fstring ) { write "x_flux" ; }
  39. EOT
  40. # first timestep
  41. ym=$((y1-1))12
  42. if (( y1 == $y0 ))
  43. then
  44. grib_filter gf $srcdir/output/ifs/001/ICMGG${expname}+000000
  45. else
  46. grib_filter gf $srcdir/output/ifs/$(printf %03d $((y1-y0)))/ICMGG${expname}+$ym
  47. fi
  48. mv x_state x_state_$ym
  49. cdo shifttime,"-6hour" x_flux x_flux_$ym
  50. rm -f x_flux
  51. njobs=0
  52. for y in $(seq $y1 $y2); do
  53. for m in {1..12}; do
  54. ( (( njobs+=1 ))
  55. ym=$((100*y+m))
  56. mkdir -p $tmpdir/data_$ym
  57. cd $tmpdir/data_$ym
  58. grib_filter ../gf \
  59. $srcdir/output/ifs/$(printf %03d $((y-$((y0-1)))))/ICMGG${expname}+$ym
  60. mv x_state ../x_state_$ym
  61. cdo shifttime,"-6hour" x_flux ../x_flux_$ym
  62. cd -
  63. rm -r $tmpdir/data_$ym ) &
  64. if (( njobs%nprocs == 0 )); then wait; fi
  65. done
  66. done
  67. wait
  68. njobs=0
  69. for y in $(seq $y1 $y2); do
  70. ( (( njobs+=1 ))
  71. cdo cat x_state_$((y-1))12 x_state_$y* y_state_$y
  72. cdo splitcode -daymean -selyear,$y y_state_$y z_${y}_
  73. cdo cat x_flux_$y* y_flux_$y
  74. cdo splitcode -daymean -selyear,$y y_flux_$y* z_${y}_ ) &
  75. if (( njobs%nprocs ==0 )); then wait; fi
  76. done
  77. wait
  78. for f in $(/bin/ls z_*grb ); do
  79. fnew=$(basename $f .grb | cut -d _ -f 3 )
  80. year=$(basename $f .grb | cut -d _ -f 2 )
  81. cdo -f nc4c -z zip setgrid,g${npts}x1 $f $outdir/var${fnew}_${year}_dayavg.nc
  82. done
  83. exit 0