source: branches/2014/dev_r4650_UKMO14.12_STAND_ALONE_OBSOPER/NEMOGCM/TOOLS/MISCELLANEOUS/rewrite_nemo.sh @ 5600

Last change on this file since 5600 was 5600, checked in by andrewryan, 5 years ago

merged in latest version of trunk alongside changes to SAO_SRC to be compatible with latest OBS

  • Property svn:executable set to *
File size: 9.1 KB
Line 
1#!/bin/bash
2#
3# rsync -av NEMO/ NEMO_no_wrkarrays/
4# cd NEMO_no_wrkarrays/
5# ../TOOLS/MISCELLANEOUS/rewrite_nemo.sh
6# cd ../CONFIG
7# ./makenemo -n ORCA2_LIM3 -s NEMO_no_wrkarrays
8#
9set -u
10#set -xv
11#
12# for on each file containing a call to work alloc
13for i in $( grep -iRl "^[^\!]*call *wrk_alloc *(" * | grep "90$" )
14do
15# create a temporary file that will be easier to process...
16    sed -e "s/dimension/DIMENSION/" -e "s/pointer/POINTER/" $i > tmp$$
17# replace "dimension(...), pointer" to "pointer, dimension(...)"
18    sed -e "s/, *DIMENSION *(\([^)]*\)) *, *POINTER/, POINTER, DIMENSION(\1)/" tmp$$ > tmp2$$
19    mv tmp2$$ tmp$$ 
20# remove all comments in $i
21    sed -e "s/\!.*//" tmp$$ > tmp2$$
22    mv tmp2$$ tmp$$
23# numbers of the lines defining the beginning and the end of each subroutine or functions
24    n1=$( egrep -ic "(^ *subroutine *[0-9a-zA-Z_]|function *[0-9a-zA-Z_][0-9a-zA-Z_]* *\(.*\))" tmp$$ )
25    n2=$( egrep -ic "(^ *end *subroutine|^ *end *function)" tmp$$ )
26    if [ $n1 -ne $n2 ]
27    then
28   echo "error with the definition of subroutine/function blocks in $i" 
29   exit
30    fi
31    linesbegin=$( egrep -in "(^ *subroutine *[0-9a-zA-Z_]|function *[0-9a-zA-Z_][0-9a-zA-Z_]* *\(.*\))" tmp$$ | sed -e "s/:.*//" )
32    linesend=$( egrep -in "(^ *end *subroutine|^ *end *function)" tmp$$ | sed -e "s/:.*//" | sort -rn )
33#
34# number of the lines containing wrk_alloc
35    cnt=$( grep -ci "^[^\!]*call *wrk_alloc *(" tmp$$ )
36# for each of these lines
37    ll=1
38    while [ $ll -le $cnt ]
39    do
40# get the line with its number
41   line=$( grep -in "^[^\!]*call *wrk_alloc *(" tmp$$ | sed -n ${ll}p | sed -e "s/\!.*//" )
42# get its number
43   lline=$( echo $line | sed -e "s/:.*//" )
44# keep only the arument of wrk_alloc between ()
45   line=$( echo $line | sed -e "s/^.*[cC][aA][lL][lL] *[wW][rR][kK]_[aA][lL][lL][oO][cC]//" | sed -e "s/[^(]*\((.*)\).*/\1/" | sed -e "s/, *k[ijkl]start *=[^,]*,/,/" | sed -e "s/, *k[ijkl]start *=.*)/ )/" )
46# find in which subroutine or function is located this call to wrk_alloc: l1 beginning l2: end
47   for lll in $linesbegin
48   do
49       [ $lline -gt $lll ] && l1=$lll
50   done
51   for lll in $linesend
52   do
53       [ $lline -lt $lll ] && l2=$lll
54   done
55   if [ $l2 -le $l1 ]
56   then
57       echo "error in $i in the indentification subroutine/function block containing line $lline"
58       exit
59   fi
60# get the last argument of the call to wrk_alloc (between the last "," and ")"
61   lw=$( echo $line | sed -e "s/.*,\(.*\)).*/\1/" | sed -e "s/ *//g" )
62# get the line containing the definition of this argument and from it get the number of dimension of the argument
63   tst=$( sed -n ${l1},${l2}p tmp$$  | grep -i "POINTER *, *DIMENSION *(.*) *::.*"${lw} | egrep -ic "[^0-9a-zA-Z_]("${lw}" *$|"${lw}" *,)" )
64   if [ $tst -ne 1 ]
65   then
66       echo "error with ${lw} definition in $i" 
67       exit
68   fi
69   dimsz=$( sed -n ${l1},${l2}p tmp$$  | grep -i "POINTER *, *DIMENSION *(.*) *::.*"${lw} | egrep -i "[^0-9a-zA-Z_]("${lw}" *$|"${lw}" *,)" | sed -e "s/.*POINTER *, *DIMENSION *(\(.*\)) *::.*/\1/" | sed -e "s/ //g" | wc -c )
70   dimsz=$(( $dimsz / 2 ))
71# get the number of variables concerned by the call to wrk_alloc (keep anly the , and count them)
72   nvar=$( echo $line | sed -e "s/[^,]//g" | wc -c )
73   nvar=$(( $nvar - $dimsz ))
74# get the définition of the size of the dimensions from the call to wrk_alloc
75   case $dimsz in
76       1) dim=$( echo $line | sed -e "s/( *\([^,]*\),.*)/\1/" | sed -e "s/ //g" ) ;; # keep between ( and the 1st ,
77       2) dim=$( echo $line | sed -e "s/( *\([^,]*,[^,]*\),.*)/\1/" | sed -e "s/ //g" ) ;; # keep between ( and the 2nd ,
78       3) dim=$( echo $line | sed -e "s/( *\([^,]*,[^,]*,[^,]*\),.*)/\1/" | sed -e "s/ //g" ) ;; # keep between ( and the 3rd ,
79       4) dim=$( echo $line | sed -e "s/( *\([^,]*,[^,]*,[^,]*,[^,]*\),.*)/\1/" | sed -e "s/ //g" ) ;; # keep between ( and the 4th ,
80       *) echo "error with the number of dimensions of $lw in $i"; exit ;;
81   esac
82# for each variable, change its definition...
83   nn=1
84   var=$lw # start from the last argument
85   while [ $nn -le $nvar ]
86   do
87# get the line where this variable is defined. 
88       tst=$( sed -n ${l1},${l2}p tmp$$  | grep -ni "POINTER *, *DIMENSION *(.*) *::.*"${var} | egrep -ic "[^0-9a-zA-Z_]("${var}" *$|"${var}" *,)" )
89       if [ $tst -ne 1 ]
90       then
91      echo "error with ${lw} definition in $i" 
92      exit
93       fi
94# get the line number where the variable is defined
95       lvar=$( sed -n ${l1},${l2}p tmp$$  | grep -ni "POINTER *, *DIMENSION *(.*) *::.*"${var} | egrep -i "[^0-9a-zA-Z_]("${var}" *$|"${var}" *,)" | sed -e "s/:.*//" )
96       lvar=$(( $lvar + $l1 - 1 ))
97# get the dimension definition and compare it to ${dim}
98       dim2=$( sed -n -e "${lvar}p" $i | sed -e "s/dimension/DIMENSION/" -e "s/\!.*//" | sed -e "s/.*, *DIMENSION *(\(.*\)).*/\1/" | sed -e "s/ //g" )
99       if [ $( echo $dim2 | wc -c ) -ne $(( $dimsz * 2 )) ] 
100       then
101      if [ "$dim2" != "$dim" ] 
102      then
103          echo "problem in $i in the defininition of variable $var: its size is $dim but we have $dim2"
104          sed -n -e "${lvar}p" $i
105          exit
106      fi
107       fi
108# remove pointer
109       sed -e "${lvar}s/pointer/POINTER/" $i | sed -e "${lvar}s/, *POINTER *,/,/" | sed -e "${lvar}s/, *POINTER */ /" > tmp2$$
110       mv tmp2$$ $i
111# redefine the dimensions
112       case $dimsz in
113      1) sed -e "${lvar}s/\(DIMENSION *(\) *: *)/\1${dim})/" $i > tmp2$$ ;;
114      2) sed -e "${lvar}s/\(DIMENSION *(\) *: *, *: *)/\1${dim})/" $i > tmp2$$ ;;
115      3) sed -e "${lvar}s/\(DIMENSION *(\) *: *, *: *, *: *)/\1${dim})/" $i > tmp2$$ ;;
116      4) sed -e "${lvar}s/\(DIMENSION *(\) *: *, *: *, *: *, *: *)/\1${dim})/" $i > tmp2$$ ;;
117       esac
118       mv tmp2$$ $i
119       
120# define the next variable (previous argument)
121       var=$( echo $line | sed -e "s/.*,\(.*\), *[^0-9a-zA-Z_]*${var}[^0-9a-zA-Z_]*[,)].*/\1/" | sed -e "s/ *//g" )
122       nn=$(( $nn + 1 ))
123   done
124   
125   ll=$(( $ll + 1 ))
126    done
127#delete wrk_alloc line
128    sed -e "/call *wrk_alloc *(/d" -e "/CALL *wrk_alloc *(/d" \
129   -e "/call *wrk_dealloc *(/d" -e "/CALL *wrk_dealloc *(/d" \
130   -e "/USE wrk_nemo/d"  -e "/use wrk_nemo/d" $i > tmp$$
131    mv tmp$$ $i
132   
133    echo $i done
134done
135#
136# some specific changes...
137#
138# OPA_SRC/SBC/albedo.F90
139sed -e "s/DIMENSION(jpi,jpj,ijpl/DIMENSION(jpi,jpj,SIZE(pt_ice,3)/" OPA_SRC/SBC/albedo.F90 > tmp$$
140mv tmp$$ OPA_SRC/SBC/albedo.F90
141# see result of
142#    grep -i "wrk_alloc" $( find . -name "*90" ) | grep "="
143#
144# LIM_SRC_2/limrhg_2.F90
145#./LIM_SRC_2/limrhg_2.F90:      CALL wrk_alloc( jpi,jpj+2, zu0, zv0, zu_n, zv_n, zu_a, zv_a, zviszeta, zviseta, kjstart = 0 )
146#./LIM_SRC_2/limrhg_2.F90:      CALL wrk_alloc( jpi,jpj+2, zzfrld, zztms, zi1, zi2, zmasst, zpresh, kjstart = 0 )
147sed -e "s/DIMENSION(jpi,jpj+2/DIMENSION(jpi,0:jpj+1/" LIM_SRC_2/limrhg_2.F90 > tmp$$
148mv tmp$$ LIM_SRC_2/limrhg_2.F90
149
150# LIM_SRC_3/limitd_me.F90
151#./LIM_SRC_3/limitd_me.F90:      CALL wrk_alloc( jpi,jpj,jpl+2, Gsum, kkstart = -1 )
152sed -e "s/DIMENSION(jpi,jpj,jpl+2)/DIMENSION(jpi,jpj,-1:jpl)/" LIM_SRC_3/limitd_me.F90 > tmp$$
153mv tmp$$ LIM_SRC_3/limitd_me.F90
154
155# LIM_SRC_3/limitd_th.F90
156#./LIM_SRC_3/limitd_th.F90:      CALL wrk_alloc( jpi,jpj,jpl+1, zhbnew, kkstart = 0 )   
157sed -e "s/DIMENSION(jpi,jpj,jpl+1)/DIMENSION(jpi,jpj,0:jpl)/" LIM_SRC_3/limitd_th.F90 > tmp$$
158mv tmp$$ LIM_SRC_3/limitd_th.F90
159
160# LIM_SRC_3/limthd_dif.F90
161#./LIM_SRC_3/limthd_dif.F90:      CALL wrk_alloc( jpij,nlay_i+1, ztcond_i, zradtr_i, zradab_i, zkappa_i, ztib, zeta_i, ztitemp, z_i, zspeche_i, kjstart=0 )
162#./LIM_SRC_3/limthd_dif.F90:      CALL wrk_alloc( jpij,nlay_s+1,           zradtr_s, zradab_s, zkappa_s, ztsb, zeta_s, ztstemp, z_s, kjstart=0 )
163sed -e "s/DIMENSION(kiut,nlay_i+1)/DIMENSION(kiut,0:nlay_i)/" \
164    -e "s/DIMENSION(kiut,nlay_s+1)/DIMENSION(kiut,0:nlay_s)/" LIM_SRC_3/limthd_dif.F90 > tmp$$
165mv tmp$$ LIM_SRC_3/limthd_dif.F90
166
167# LIM_SRC_3/limthd_ent.F90
168#./LIM_SRC_3/limthd_ent.F90:      CALL wrk_alloc( jpij, nlay_i+3, zqh_cum0, zh_cum0, kjstart = 0 )
169#./LIM_SRC_3/limthd_ent.F90:      CALL wrk_alloc( jpij, nlay_i+1, zqh_cum1, zh_cum1, kjstart = 0 )
170sed -e "s/DIMENSION(jpij,nlay_i+3)/DIMENSION(jpij,0:nlay_i+2)/" \
171    -e "s/DIMENSION(jpij,nlay_i+1)/DIMENSION(jpij,0:nlay_i)/" LIM_SRC_3/limthd_ent.F90 > tmp$$
172mv tmp$$ LIM_SRC_3/limthd_ent.F90
173
174# OPA_SRC/DYN/divcur.F90
175#./OPA_SRC/DYN/divcur.F90:      CALL wrk_alloc( jpi+4, jpj  , zwv, kjstart = -1 )
176sed -e "s/DIMENSION(jpi+4,jpj)/DIMENSION(-1:jpi+2,jpj)/" OPA_SRC/DYN/divcur.F90 > tmp$$
177mv tmp$$ OPA_SRC/DYN/divcur.F90
178
179# OPA_SRC/LDF/ldfslp.F90
180#./OPA_SRC/LDF/ldfslp.F90:      CALL wrk_alloc( jpi,jpj,jpk,2, zdxrho , zdyrho, zdzrho,              klstart = 0  )
181#./OPA_SRC/LDF/ldfslp.F90:      CALL wrk_alloc( jpi,jpj,  2,2, zti_mlb, ztj_mlb,        kkstart = 0, klstart = 0  )
182sed -e "s/DIMENSION(jpi,jpj,jpk,2)/DIMENSION(jpi,jpj,jpk,0:1)/" \
183    -e "s/DIMENSION(jpi,jpj,2,2)/DIMENSION(jpi,jpj,0:1,0:1)/" OPA_SRC/LDF/ldfslp.F90 > tmp$$
184mv tmp$$ OPA_SRC/LDF/ldfslp.F90
185# OPA_SRC/ZDF/zdfkpp.F90
186#./OPA_SRC/ZDF/zdfkpp.F90:      CALL wrk_alloc( jpi,3, zmoek, kjstart = 0 )
187sed -e "s/DIMENSION(jpi,3) *::* zmoek/DIMENSION(jpi,0:2) ::   zmoek/" OPA_SRC/ZDF/zdfkpp.F90 > tmp$$
188mv tmp$$ OPA_SRC/ZDF/zdfkpp.F90
189
190# links
191# see result of
192# find . -type l
193#
194# ./LIM_SRC_2/limrhg.F90
195cd LIM_SRC_2
196ln -sf ../LIM_SRC_3/limrhg.F90 .
197cd ..
198
199# ./OOO_SRC/dtadyn.F90
200cd OOO_SRC
201ln -sf ../OFF_SRC/dtadyn.F90 .
202cd ..
203
204# ./OOO_SRC/obs_fbm.F90
205cd OOO_SRC
206ln -sf ../OPA_SRC/OBS/obs_fbm.F90 .
207cd ..
Note: See TracBrowser for help on using the repository browser.