| [5848] | 1 | #!/bin/bash |
|---|
| 2 | |
|---|
| [5850] | 3 | . lmdz_env.sh |
|---|
| 4 | |
|---|
| 5 | set -eu |
|---|
| 6 | |
|---|
| 7 | #----------------------------------------------------------------------------- |
|---|
| [5848] | 8 | # |
|---|
| [5850] | 9 | # Script for interpolating monthly ERA* files to the LMDZ grid; |
|---|
| 10 | # it creates the folder structure required by the LMDZ_Setup scripts |
|---|
| [5848] | 11 | # |
|---|
| [5850] | 12 | # NB: for "cleanest" guiding, the 1st step of the next month should be added at the end of each month. |
|---|
| 13 | # If you need such precision, you should use the scripts here - AND adjust "ERADIR" in script_SIMU ! |
|---|
| 14 | # wget http://forge.ipsl.jussieu.fr/igcmg/browser/TOOLS/INTERP_NUDGE |
|---|
| [5848] | 15 | # documented on LMDZpedia (search for "Guidage" ) |
|---|
| 16 | # |
|---|
| [5850] | 17 | #----------------------------------------------------------------------------- |
|---|
| [5848] | 18 | # Requires : netcdf, ferret, nco, cdo |
|---|
| [5850] | 19 | #----------------------------------------------------------------------------- |
|---|
| [5848] | 20 | |
|---|
| [5850] | 21 | #------------------------------------------------ |
|---|
| [5848] | 22 | # User choices |
|---|
| [5850] | 23 | #------------------------------------------------ |
|---|
| [5848] | 24 | # Periode : |
|---|
| 25 | mth_i=200001 |
|---|
| [5850] | 26 | mth_f=200212 |
|---|
| [5848] | 27 | # |
|---|
| 28 | vars="u v ta q" |
|---|
| 29 | vars="u v" |
|---|
| 30 | # |
|---|
| 31 | # Choix des fichiers de guidage ("rea"nalyses) : ERA5, ERAI, OPERA |
|---|
| 32 | rea="ERAI" |
|---|
| 33 | resol_rea=075 |
|---|
| 34 | |
|---|
| [5850] | 35 | #------------------------------------------------ |
|---|
| 36 | |
|---|
| [5848] | 37 | ERA5_PATH="/gpfsstore/rech/psl/rpsl376/ergon/ERA5/" |
|---|
| 38 | ERAI_PATH="/gpfsstore/rech/psl/rpsl376/ergon/ERAI/" |
|---|
| 39 | ERAI_PATH="/lustre/fswork/projects/rech/wfe/rgzi027/LMDZ/pub/3DInputData/ReAnalyses/ERAI" |
|---|
| 40 | #/lustre/fswork/projects/rech/wfe/rgzi027/LMDZ/pub/3DInputData/ReAnalyses/ERAI/NETCDF/GLOBAL_075/4xdaily/AN_PL/2014/ |
|---|
| 41 | |
|---|
| 42 | #@ADS ERA5_PATH="/lus/work/CT1/cad15221/abarral/ERA_TMP/" # /!\ temp for test only ! |
|---|
| 43 | #@ADS ERAI_PATH="" # /!\ temp for tests only! |
|---|
| 44 | #@ADS echo "/!\ Nudging files are not available on Adastra for now !"; exit 1 # comment this line for tests |
|---|
| 45 | #@ADS SCRATCH=$SCRATCHDIR |
|---|
| 46 | |
|---|
| [5850] | 47 | set -u # raise error if path if unset on machine |
|---|
| 48 | echo "Paths: $ERA5_PATH $ERAI_PATH $SCRATCH" |
|---|
| 49 | set +u |
|---|
| 50 | |
|---|
| [5848] | 51 | GRID_DIR=./INIT |
|---|
| 52 | |
|---|
| [5850] | 53 | #----------------------------------------------------------------------------- |
|---|
| [5848] | 54 | #Utilite du block suivant a re-examiner : |
|---|
| [5850] | 55 | #----------------------------------------------------------------------------- |
|---|
| [5848] | 56 | #L'utilisateur a maintenant des choix a faire en plus de mth* : type de guidage, et "rea" |
|---|
| 57 | # Alors c'est plus facile d'editer&modifier le script, puis lancer avec ./era2gcm.sh, que de donner tous les params |
|---|
| 58 | # TODO check w/ Adriana if we can remove those and put them as inline args (above) |
|---|
| 59 | while (($# > 0)) |
|---|
| 60 | do |
|---|
| 61 | case $1 in |
|---|
| 62 | "-h") cat <<........fin |
|---|
| 63 | ./era2gcm.sh [-mthini initial_month] [-mthend final_month] [-grid_dir directory_containing_grille_gcm.nc] |
|---|
| 64 | ........fin |
|---|
| 65 | exit ;; |
|---|
| 66 | "-grid_dir") GRID_DIR=$2 ; shift ; shift ;; |
|---|
| 67 | "-mthini") mth_i=$2 ; shift ; shift ;; |
|---|
| 68 | "-mthend") mth_f=$2 ; shift ; shift ;; |
|---|
| 69 | *) $0 -h ; exit 1 |
|---|
| 70 | esac |
|---|
| 71 | done |
|---|
| 72 | #--Fin du bloc a examiner -------------------------------------------- |
|---|
| 73 | |
|---|
| 74 | tmin=1 |
|---|
| 75 | resol=grilles_gcm.nc |
|---|
| 76 | |
|---|
| 77 | GRID_FI=${GRID_DIR}/${resol} |
|---|
| 78 | if [ ! -f "$GRID_FI" ] ; then |
|---|
| 79 | echo "Le fichier $GRID_DIR/$resol est nécessaire; créer le fichier $GRID_FI avec grilles_gcm_netcdf.e" |
|---|
| 80 | exit 1 |
|---|
| 81 | fi |
|---|
| 82 | mth=$mth_i |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | ##################################################################### |
|---|
| 86 | # LOOP ON MONTHS |
|---|
| 87 | ##################################################################### |
|---|
| 88 | while (( mth <= mth_f )) ; do |
|---|
| 89 | |
|---|
| 90 | echo "mth $mth" |
|---|
| 91 | mois=$(echo "$mth" | cut -c 5-) |
|---|
| 92 | an=$(echo "$mth" | cut -c -4) |
|---|
| 93 | ndays=( 31 28 31 30 31 30 31 31 30 31 30 31 ) |
|---|
| 94 | months=( jan feb mar apr may jun jul aug sep oct nov dec ) |
|---|
| 95 | if [ $(( an % 4 )) = 0 ] ; then ndays[1]=29 ; fi |
|---|
| 96 | imois=$(( ${mois#0} - 1 )) |
|---|
| 97 | month=${months[$imois]} |
|---|
| 98 | nday=${ndays[$imois]} |
|---|
| 99 | tmax=$(( nday * 4 )) |
|---|
| 100 | echo "PARAMETRES CALENDAIRES $imois $month $nday $tmax" |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | iip1=$(ncdump -h "$GRID_FI" | grep lonu | head -1 | awk ' { print $3 } ') |
|---|
| 104 | jjm=$(ncdump -h "$GRID_FI" | grep latv | head -1 | awk ' { print $3 } ') |
|---|
| 105 | (( jjp1 = jjm + 1 )) |
|---|
| 106 | # \rm t2.nc ua.nc va.nc sst.nc u.nc v.nc T.nc ts.nc |
|---|
| 107 | |
|---|
| 108 | ##################################################################### |
|---|
| 109 | # Choix de la periode temporelle |
|---|
| 110 | ##################################################################### |
|---|
| 111 | |
|---|
| 112 | t0="l=$tmin" |
|---|
| 113 | t1tn="l=${tmin}:${tmax}" |
|---|
| 114 | |
|---|
| 115 | ##################################################################### |
|---|
| 116 | # Lien avec les fichiers netcdf contenant les d0 ECMWF |
|---|
| 117 | ##################################################################### |
|---|
| 118 | echo "-------- liens de telechargement a actualiser ----" |
|---|
| 119 | if [ "$rea" = "ERA5" ] ; then |
|---|
| 120 | if [[ $an -ge 2022 ]] ; then |
|---|
| 121 | ANA_DIR="$ERA5_PATH/NETCDF/GLOBAL_025/hourly" |
|---|
| 122 | suf="ap1e5.GLOBAL_025" |
|---|
| 123 | else |
|---|
| 124 | ANA_DIR="$ERA5_PATH/NETCDF/GLOBAL_025/4xdaily" |
|---|
| 125 | suf="aphe5.GLOBAL_025" |
|---|
| 126 | fi |
|---|
| 127 | elif [ "$rea" = "ERAI" ] ; then |
|---|
| 128 | ANA_DIR="$ERAI_PATH/NETCDF/GLOBAL_$resol_rea/4xdaily" |
|---|
| 129 | suf="aphei.GLOBAL_$resol_rea" |
|---|
| 130 | fi |
|---|
| 131 | |
|---|
| 132 | if [ ! -d $ANA_DIR ] ; then echo Directory $ANA_DIR does not exist ; exit 1 ; fi |
|---|
| 133 | |
|---|
| [5850] | 134 | ################################################################################# |
|---|
| [5848] | 135 | # Loop on variables among u, v, ta, q |
|---|
| [5850] | 136 | ################################################################################# |
|---|
| [5848] | 137 | |
|---|
| 138 | for var in $vars ; do |
|---|
| 139 | |
|---|
| 140 | case $var in |
|---|
| 141 | ta) filename=T.nc ;; |
|---|
| 142 | q) filename=hur.nc ;; |
|---|
| 143 | *) filename=$var.nc |
|---|
| 144 | esac |
|---|
| 145 | outd=GUIDE_${rea}/$an/$mois |
|---|
| 146 | out_file=$outd/$filename |
|---|
| 147 | |
|---|
| 148 | if [ ! -f $out_file ] ; then |
|---|
| 149 | mkdir -p $outd |
|---|
| 150 | file_rea="$ANA_DIR/AN_PL/$an/$var.$an$mois.$suf.nc" |
|---|
| 151 | |
|---|
| [5850] | 152 | ############################################################################ |
|---|
| [5848] | 153 | # Automatically downloading reanalysis if needed |
|---|
| [5850] | 154 | ############################################################################ |
|---|
| [5848] | 155 | if [ "$( echo $ERAI_PATH | grep 3DInputData )" != "" ] ; then |
|---|
| 156 | if [ ! -f $file_rea ] ; then |
|---|
| 157 | mkdir -p $( dirname $file_rea ) |
|---|
| 158 | cd $( dirname $file_rea ) |
|---|
| 159 | wget http://lmdz.lmd.jussieu.fr/pub/3DInputData/$( echo $file_rea | sed -e 's/^.*3DInputData//' ) |
|---|
| 160 | cd - |
|---|
| 161 | fi |
|---|
| 162 | fi |
|---|
| [5850] | 163 | ############################################################################ |
|---|
| [5848] | 164 | # Checking the avaibility of reanalysis native file |
|---|
| [5850] | 165 | ############################################################################ |
|---|
| [5848] | 166 | if [ ! -f $file_rea ] ; then echo Reanalysis file $file_rea missing ; exit 1 ; fi |
|---|
| 167 | |
|---|
| [5850] | 168 | ############################################################################ |
|---|
| [5848] | 169 | # Special treatments |
|---|
| [5850] | 170 | ############################################################################ |
|---|
| [5848] | 171 | if [ "$rea" = "ERAI" ] ; then |
|---|
| 172 | # original data in "short" should be changed to "float" for ferret |
|---|
| [5850] | 173 | ncap2 -s $var'=float('$var')' $file_rea -O tmp_in.nc |
|---|
| [5848] | 174 | elif [ "$rea" = "ERA5" -a $an -ge 2022 ] ; then |
|---|
| 175 | # Recent analysis available on a daily basis |
|---|
| [5850] | 176 | cdo selhour,0,6,12,18 $file_rea -O tmp_in.nc |
|---|
| [5848] | 177 | else |
|---|
| [5850] | 178 | ln -sf $file_rea tmp_in.nc |
|---|
| [5848] | 179 | fi |
|---|
| 180 | |
|---|
| [5850] | 181 | ############################################################################ |
|---|
| [5848] | 182 | # Dimensions of horizontal grid |
|---|
| [5850] | 183 | ############################################################################ |
|---|
| [5848] | 184 | case $var in |
|---|
| 185 | v) imjm="i=1:$iip1,j=1:$jjm" ;; |
|---|
| 186 | *) imjm="i=1:$iip1,j=1:$jjp1" |
|---|
| 187 | esac |
|---|
| 188 | |
|---|
| [5850] | 189 | ############################################################################ |
|---|
| [5848] | 190 | # Grid to be used from grilles_gcm.nc |
|---|
| [5850] | 191 | ############################################################################ |
|---|
| [5848] | 192 | case $var in |
|---|
| 193 | u|v) grid=grille_$var ;; |
|---|
| 194 | *) grid=grille_s |
|---|
| 195 | esac |
|---|
| 196 | |
|---|
| [5850] | 197 | ############################################################################ |
|---|
| [5848] | 198 | # Interpolating with ferret |
|---|
| [5850] | 199 | ############################################################################ |
|---|
| [5848] | 200 | cat <<.........eod......... >| tmp.jnl |
|---|
| 201 | ! NB : Augmenter la memoire (plus de 512) peut faire planter |
|---|
| 202 | set memory/size=512 |
|---|
| 203 | use "$GRID_FI" |
|---|
| [5850] | 204 | use tmp_in.nc |
|---|
| [5848] | 205 | define axis/t="1-${month}-${an}:00:00":"${nday}-${month}-${an}:18:00":6/units=hours thour |
|---|
| 206 | ! Pour regrid horizontal on utilise la variable grille_s(lonv,latu) du fichier grilles_gcm.nc |
|---|
| 207 | !! Alors il faut renommer la variable à la fin (ncrename), car le code cherche "UWND", "VWND". |
|---|
| [5850] | 208 | save/clobber/file="tmp_out.nc" $var[d=2,gxy=${grid}[d=1],$imjm,$t0,gt=thour@asn] |
|---|
| 209 | repeat/$t1tn save/file="tmp_out.nc"/append $var[d=2,gxy=${grid}[d=1],$imjm,gt=thour@asn] |
|---|
| [5848] | 210 | .........eod......... |
|---|
| 211 | |
|---|
| [5850] | 212 | set +e |
|---|
| [5848] | 213 | ferret -nojnl <<.........eod......... |
|---|
| 214 | go tmp.jnl |
|---|
| 215 | quit |
|---|
| [5850] | 216 | fi |
|---|
| [5848] | 217 | .........eod......... |
|---|
| [5850] | 218 | if [ $? != 0 ] ; then |
|---|
| 219 | echo Something went wrong when running ferret with script tmp.jnl : |
|---|
| 220 | cat tmp.jnl |
|---|
| 221 | echo On $PWD |
|---|
| 222 | exit 1 |
|---|
| 223 | else |
|---|
| 224 | set -e |
|---|
| 225 | mv tmp_out.nc $out_file |
|---|
| 226 | fi |
|---|
| [5848] | 227 | |
|---|
| [5850] | 228 | ############################################################################ |
|---|
| [5848] | 229 | # Changing variable names |
|---|
| [5850] | 230 | ############################################################################ |
|---|
| [5848] | 231 | case $var in |
|---|
| 232 | u) out_var=UWND ;; |
|---|
| 233 | v) out_var=VWND ;; |
|---|
| 234 | r) out_var=RH ;; |
|---|
| 235 | ta) out_var=AIR |
|---|
| 236 | esac |
|---|
| [5850] | 237 | #Ferret a ecrit $varu en majuscules, l'equivalent en bash est ${varu^^} |
|---|
| 238 | # (Note : inversement, ${varu,,} passe $varu de majuscules en minuscules) |
|---|
| [5848] | 239 | ncrename -v ${var^^},$out_var $out_file |
|---|
| 240 | fi |
|---|
| 241 | done |
|---|
| 242 | |
|---|
| 243 | |
|---|
| [5850] | 244 | ############################################################################ |
|---|
| [5848] | 245 | # Updating month counter |
|---|
| [5850] | 246 | ############################################################################ |
|---|
| [5848] | 247 | echo AN MTH $an $mois |
|---|
| 248 | (( mth = $mth + 1 )) |
|---|
| 249 | if [ $mois = 12 ] ; then |
|---|
| 250 | (( an = $an + 1 )) |
|---|
| 251 | mth=${an}01 |
|---|
| 252 | fi |
|---|
| 253 | |
|---|
| 254 | done |
|---|
| 255 | ln -sf GUIDE_$rea GUIDE |
|---|