source: BOL/LMDZ_Setup/era2gcm.sh @ 5859

Last change on this file since 5859 was 5851, checked in by fhourdin, 7 weeks ago

Improvement era2gcm.sh

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