123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #!/bin/bash
- module load R
- set -o errexit
- set -o nounset
- if [ $# == 0 ] ; then
- echo ""
- echo " Usage: gener_perturb_restart.bash NETCDF-file NUMBER-members"
- echo " PURPOSE : "
- echo " Creates NUMBER-members copies of the input NETCDF-file file"
- echo " and adds for each copy a white noise to sea surface"
- echo " temperature (standard deviation: 10^(-4) K)"
- echo " This script can be used to generate ensemble members"
- echo " for coupled simulations"
- echo " ARGUMENTS : "
- echo " NETCDF-file : Restart file of NEMO (NetCDF format). The file"
- echo " must contain a variable named tn"
- echo " NUMBER-members : number of perturbed restarts to generate"
- echo " OUTPUT : "
- echo " NUMBER-members copies of the original files with perturbed"
- echo " variable tn. The file names are the same as the input files"
- echo " but with suffix pertXX where XX = 01, 02, 03, ..."
- echo " EXAMPLE : "
- echo " ./gener_perturb_restart.bash EC04_00046752_restart_oce_0005.nc 5"
- echo " --> will create:"
- echo " EC04_00046752_restart_oce_0005_pert01.nc"
- echo " EC04_00046752_restart_oce_0005_pert02.nc"
- echo " EC04_00046752_restart_oce_0005_pert03.nc"
- echo " EC04_00046752_restart_oce_0005_pert04.nc"
- echo " EC04_00046752_restart_oce_0005_pert05.nc"
- echo " HELPDESK : "
- echo " francois.massonnet@uclouvain.be"
- echo ""
- exit
- fi
- filein=$1
- nmemb=$2
- var=tn
- per=0.0001
-
- cp $filein ${filein}.backup
- for jmemb in `seq 1 $nmemb`
- do
- echo "Doing copy $jmemb out of $nmemb"
- jmemb=$(printf "%02d" $jmemb)
-
- filenew="${filein%.nc}_pert${jmemb}.nc"
- cp $filein $filenew
-
- echo "#!/usr/bin/env Rscript
- library(ncdf4)
- # François Massonnet, 30 Jan 2015
- # Adds a gaussian perturbation at the first level of a 3D field
- # Tested only for NEMO restarts
- #
- # This script should be called by a bash script so that the variable and file names are specified, as well as the perturbation
- varname='$var'
- filein <- '$filenew'
- ex.nc <- nc_open(filein,write=TRUE)
- spert <- $per
- myvar <- ncvar_get(ex.nc, varname)
- myvarpert <- myvar
- for (i in seq(1,dim(myvar)[1])){
- for (j in seq(1,dim(myvar)[2])){
- if (myvar[i,j,1] != 0){
- myvarpert[i,j,1] = myvarpert[i,j,1] + rnorm(1,sd=spert)
- }
- }
- }
- ncvar_put(ex.nc,varname,myvarpert)
- nc_close(ex.nc)" > Rtmpfile.R
- chmod 744 Rtmpfile.R
-
- ./Rtmpfile.R
- rm -f Rtmpfile.R
- done
|