source: BOL/LMDZ_Setup/era2gcm.sh

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

Improvement era2gcm.sh

  • Property svn:executable set to *
File size: 9.1 KB
Line 
1#!/bin/bash
2
3. lmdz_env.sh
4
5set -eu
6
7#-----------------------------------------------------------------------------
8#
9#  Script for interpolating monthly ERA* files to the LMDZ grid;
10#  it creates the folder structure required by the LMDZ_Setup scripts
11#
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
15#      documented on LMDZpedia (search for "Guidage" )
16#
17#-----------------------------------------------------------------------------
18# Requires : netcdf, ferret, nco, cdo
19#-----------------------------------------------------------------------------
20
21#------------------------------------------------
22# User choices
23#------------------------------------------------
24# Periode :
25mth_i=200001
26mth_f=200212
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
35# Location of shared files
36ERA_PATH="/gpfsstore/rech/psl/rpsl376/ergon/$rea/"
37
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"
43
44GRID_DIR=./INIT
45
46#-----------------------------------------------------------------------------
47#Utilite du block suivant a re-examiner :
48#-----------------------------------------------------------------------------
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
114      ANA_DIR="$ERA_PATH/NETCDF/GLOBAL_025/hourly"
115      suf="ap1e5.GLOBAL_025"
116     else
117      ANA_DIR="$ERA_PATH/NETCDF/GLOBAL_025/4xdaily"
118      suf="aphe5.GLOBAL_025"
119     fi
120   elif [ "$rea" = "ERAI" ] ; then
121      ANA_DIR="$ERA_PATH/NETCDF/GLOBAL_$resol_rea/4xdaily"
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
127   #################################################################################
128   # Loop on variables among u, v, ta, q
129   #################################################################################
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
145        ############################################################################
146        # Automatically downloading reanalysis if needed
147        ############################################################################
148        if [ "$( echo $ERA_PATH | grep 3DInputData )" != "" ] ; then
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
156         ############################################################################
157         # Checking the avaibility of reanalysis native file
158         ############################################################################
159         if [ ! -f $file_rea ] ; then echo Reanalysis file $file_rea missing ; exit 1 ; fi
160
161         ############################################################################
162         # Special treatments
163         ############################################################################
164         if [ "$rea" = "ERAI" ] ; then
165             # original data in "short" should be changed to "float" for ferret
166             ncap2 -s $var'=float('$var')' $file_rea -O tmp_in.nc
167         elif [ "$rea" = "ERA5" -a $an -ge 2022 ] ; then
168             # Recent analysis available on a daily basis
169             cdo selhour,0,6,12,18 $file_rea -O tmp_in.nc
170         else
171             ln -sf $file_rea tmp_in.nc
172         fi
173
174         ############################################################################
175         # Dimensions of horizontal grid
176         ############################################################################
177         case $var in
178            v) imjm="i=1:$iip1,j=1:$jjm" ;;
179            *) imjm="i=1:$iip1,j=1:$jjp1"
180         esac
181
182         ############################################################################
183         # Grid to be used from grilles_gcm.nc
184         ############################################################################
185         case $var in
186            u|v) grid=grille_$var ;;
187            *) grid=grille_s
188         esac
189
190         ############################################################################
191         # Interpolating with ferret
192         ############################################################################
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"
197         use tmp_in.nc
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".
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]
203.........eod.........
204
205         set +e
206         ferret -nojnl <<.........eod.........
207         go tmp.jnl
208         quit
209         fi
210.........eod.........
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
220
221        ############################################################################
222        # Changing variable names
223        ############################################################################
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
230         #Ferret a ecrit $varu en majuscules, l'equivalent en bash est ${varu^^}
231         #   (Note : inversement, ${varu,,} passe $varu de majuscules en minuscules)
232         ncrename -v ${var^^},$out_var $out_file
233     fi
234   done
235
236
237   ############################################################################
238   # Updating month counter
239   ############################################################################
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.