gener_perturb_nemo_restart.bash 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/bin/bash
  2. #
  3. # -- Author : François Massonnet, francois.massonnet@ic3.cat
  4. # -- Date : 30 Jan 2015
  5. # -- At : IC3, Barcelona
  6. # -- Modified : 19 Jan 2016, omar.bellprat@bsc.es
  7. # November 2017, francois.massonnet@uclouvain.be
  8. # -- Purpose: Generation of an arbitrary number of NEMO oceanic restarts that are copies of a reference, plus a perturbation
  9. #
  10. # -- Method : The reference file is duplicated in this script, then read by a R script. The perturbation is introduced and finally written to this latter file.
  11. module load R
  12. set -o errexit
  13. set -o nounset
  14. #set -x
  15. if [ $# == 0 ] ; then
  16. echo ""
  17. echo " Usage: gener_perturb_restart.bash NETCDF-file NUMBER-members"
  18. echo " PURPOSE : "
  19. echo " Creates NUMBER-members copies of the input NETCDF-file file"
  20. echo " and adds for each copy a white noise to sea surface"
  21. echo " temperature (standard deviation: 10^(-4) K)"
  22. echo " This script can be used to generate ensemble members"
  23. echo " for coupled simulations"
  24. echo " ARGUMENTS : "
  25. echo " NETCDF-file : Restart file of NEMO (NetCDF format). The file"
  26. echo " must contain a variable named tn"
  27. echo " NUMBER-members : number of perturbed restarts to generate"
  28. echo " OUTPUT : "
  29. echo " NUMBER-members copies of the original files with perturbed"
  30. echo " variable tn. The file names are the same as the input files"
  31. echo " but with suffix pertXX where XX = 01, 02, 03, ..."
  32. echo " EXAMPLE : "
  33. echo " ./gener_perturb_restart.bash EC04_00046752_restart_oce_0005.nc 5"
  34. echo " --> will create:"
  35. echo " EC04_00046752_restart_oce_0005_pert01.nc"
  36. echo " EC04_00046752_restart_oce_0005_pert02.nc"
  37. echo " EC04_00046752_restart_oce_0005_pert03.nc"
  38. echo " EC04_00046752_restart_oce_0005_pert04.nc"
  39. echo " EC04_00046752_restart_oce_0005_pert05.nc"
  40. echo " HELPDESK : "
  41. echo " francois.massonnet@uclouvain.be"
  42. echo ""
  43. exit
  44. fi
  45. filein=$1
  46. nmemb=$2
  47. # ---------------------------------------------------------
  48. var=tn # Variable to be perturbed
  49. per=0.0001 # Standard deviation of gaussian perturbation to be applied,
  50. # in units of the variable (for tn: in K for example)
  51. cp $filein ${filein}.backup # Do a backup
  52. for jmemb in `seq 1 $nmemb`
  53. do
  54. echo "Doing copy $jmemb out of $nmemb"
  55. jmemb=$(printf "%02d" $jmemb)
  56. # 1. Make a copy of the original file, with the new name
  57. filenew="${filein%.nc}_pert${jmemb}.nc"
  58. cp $filein $filenew
  59. # 2. Prepare the R script
  60. echo "#!/usr/bin/env Rscript
  61. library(ncdf4)
  62. # François Massonnet, 30 Jan 2015
  63. # Adds a gaussian perturbation at the first level of a 3D field
  64. # Tested only for NEMO restarts
  65. #
  66. # This script should be called by a bash script so that the variable and file names are specified, as well as the perturbation
  67. varname='$var'
  68. filein <- '$filenew'
  69. ex.nc <- nc_open(filein,write=TRUE)
  70. spert <- $per
  71. myvar <- ncvar_get(ex.nc, varname)
  72. myvarpert <- myvar
  73. for (i in seq(1,dim(myvar)[1])){
  74. for (j in seq(1,dim(myvar)[2])){
  75. if (myvar[i,j,1] != 0){
  76. myvarpert[i,j,1] = myvarpert[i,j,1] + rnorm(1,sd=spert)
  77. }
  78. }
  79. }
  80. ncvar_put(ex.nc,varname,myvarpert)
  81. nc_close(ex.nc)" > Rtmpfile.R
  82. chmod 744 Rtmpfile.R
  83. # 3. Run the R script, that produces the new NetCDF
  84. ./Rtmpfile.R
  85. rm -f Rtmpfile.R
  86. done