rewrite_nemo.sh 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #
  9. set -u
  10. #set -xv
  11. #
  12. # for on each file containing a call to work alloc
  13. for i in $( grep -iRl "^[^\!]*call *wrk_alloc *(" * | grep "90$" )
  14. do
  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. # define the next variable (previous argument)
  120. var=$( echo $line | sed -e "s/.*,\(.*\), *[^0-9a-zA-Z_]*${var}[^0-9a-zA-Z_]*[,)].*/\1/" | sed -e "s/ *//g" )
  121. nn=$(( $nn + 1 ))
  122. done
  123. ll=$(( $ll + 1 ))
  124. done
  125. #delete wrk_alloc line
  126. sed -e "/call *wrk_alloc *(/d" -e "/CALL *wrk_alloc *(/d" \
  127. -e "/call *wrk_dealloc *(/d" -e "/CALL *wrk_dealloc *(/d" \
  128. -e "/USE wrk_nemo/d" -e "/use wrk_nemo/d" $i > tmp$$
  129. mv tmp$$ $i
  130. echo $i done
  131. done
  132. #
  133. # some specific changes...
  134. #
  135. # OPA_SRC/SBC/albedo.F90
  136. sed -e "s/DIMENSION(jpi,jpj,ijpl/DIMENSION(jpi,jpj,SIZE(pt_ice,3)/" OPA_SRC/SBC/albedo.F90 > tmp$$
  137. mv tmp$$ OPA_SRC/SBC/albedo.F90
  138. # see result of
  139. # grep -i "wrk_alloc" $( find . -name "*90" ) | grep "="
  140. #
  141. # LIM_SRC_2/limrhg_2.F90
  142. #./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 )
  143. #./LIM_SRC_2/limrhg_2.F90: CALL wrk_alloc( jpi,jpj+2, zzfrld, zztms, zi1, zi2, zmasst, zpresh, kjstart = 0 )
  144. sed -e "s/DIMENSION(jpi,jpj+2/DIMENSION(jpi,0:jpj+1/" LIM_SRC_2/limrhg_2.F90 > tmp$$
  145. mv tmp$$ LIM_SRC_2/limrhg_2.F90
  146. # LIM_SRC_3/limitd_me.F90
  147. #./LIM_SRC_3/limitd_me.F90: CALL wrk_alloc( jpi,jpj,jpl+2, Gsum, kkstart = -1 )
  148. sed -e "s/DIMENSION(jpi,jpj,jpl+2)/DIMENSION(jpi,jpj,-1:jpl)/" LIM_SRC_3/limitd_me.F90 > tmp$$
  149. mv tmp$$ LIM_SRC_3/limitd_me.F90
  150. # LIM_SRC_3/limitd_th.F90
  151. #./LIM_SRC_3/limitd_th.F90: CALL wrk_alloc( jpi,jpj,jpl+1, zhbnew, kkstart = 0 )
  152. sed -e "s/DIMENSION(jpi,jpj,jpl+1)/DIMENSION(jpi,jpj,0:jpl)/" LIM_SRC_3/limitd_th.F90 > tmp$$
  153. mv tmp$$ LIM_SRC_3/limitd_th.F90
  154. # LIM_SRC_3/limthd_dif.F90
  155. #./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 )
  156. #./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 )
  157. sed -e "s/DIMENSION(kiut,nlay_i+1)/DIMENSION(kiut,0:nlay_i)/" \
  158. -e "s/DIMENSION(kiut,nlay_s+1)/DIMENSION(kiut,0:nlay_s)/" LIM_SRC_3/limthd_dif.F90 > tmp$$
  159. mv tmp$$ LIM_SRC_3/limthd_dif.F90
  160. # LIM_SRC_3/limthd_ent.F90
  161. #./LIM_SRC_3/limthd_ent.F90: CALL wrk_alloc( jpij, nlay_i+3, zqh_cum0, zh_cum0, kjstart = 0 )
  162. #./LIM_SRC_3/limthd_ent.F90: CALL wrk_alloc( jpij, nlay_i+1, zqh_cum1, zh_cum1, kjstart = 0 )
  163. sed -e "s/DIMENSION(jpij,nlay_i+3)/DIMENSION(jpij,0:nlay_i+2)/" \
  164. -e "s/DIMENSION(jpij,nlay_i+1)/DIMENSION(jpij,0:nlay_i)/" LIM_SRC_3/limthd_ent.F90 > tmp$$
  165. mv tmp$$ LIM_SRC_3/limthd_ent.F90
  166. # OPA_SRC/DYN/divcur.F90
  167. #./OPA_SRC/DYN/divcur.F90: CALL wrk_alloc( jpi+4, jpj , zwv, kjstart = -1 )
  168. sed -e "s/DIMENSION(jpi+4,jpj)/DIMENSION(-1:jpi+2,jpj)/" OPA_SRC/DYN/divcur.F90 > tmp$$
  169. mv tmp$$ OPA_SRC/DYN/divcur.F90
  170. # OPA_SRC/LDF/ldfslp.F90
  171. #./OPA_SRC/LDF/ldfslp.F90: CALL wrk_alloc( jpi,jpj,jpk,2, zdxrho , zdyrho, zdzrho, klstart = 0 )
  172. #./OPA_SRC/LDF/ldfslp.F90: CALL wrk_alloc( jpi,jpj, 2,2, zti_mlb, ztj_mlb, kkstart = 0, klstart = 0 )
  173. sed -e "s/DIMENSION(jpi,jpj,jpk,2)/DIMENSION(jpi,jpj,jpk,0:1)/" \
  174. -e "s/DIMENSION(jpi,jpj,2,2)/DIMENSION(jpi,jpj,0:1,0:1)/" OPA_SRC/LDF/ldfslp.F90 > tmp$$
  175. mv tmp$$ OPA_SRC/LDF/ldfslp.F90
  176. # OPA_SRC/ZDF/zdfkpp.F90
  177. #./OPA_SRC/ZDF/zdfkpp.F90: CALL wrk_alloc( jpi,3, zmoek, kjstart = 0 )
  178. sed -e "s/DIMENSION(jpi,3) *::* zmoek/DIMENSION(jpi,0:2) :: zmoek/" OPA_SRC/ZDF/zdfkpp.F90 > tmp$$
  179. mv tmp$$ OPA_SRC/ZDF/zdfkpp.F90
  180. # links
  181. # see result of
  182. # find . -type l
  183. #
  184. # ./LIM_SRC_2/limrhg.F90
  185. cd LIM_SRC_2
  186. ln -sf ../LIM_SRC_3/limrhg.F90 .
  187. cd ..
  188. # ./OOO_SRC/dtadyn.F90
  189. cd OOO_SRC
  190. ln -sf ../OFF_SRC/dtadyn.F90 .
  191. cd ..
  192. # ./OOO_SRC/obs_fbm.F90
  193. cd OOO_SRC
  194. ln -sf ../OPA_SRC/OBS/obs_fbm.F90 .
  195. cd ..