zgr2_slider.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #!/usr/bin/python
  2. # compute vertical levels using 2 tanh algorithm
  3. import numpy as np
  4. from math import *
  5. import matplotlib.pyplot as plt
  6. from matplotlib.widgets import Slider, Button, RadioButtons
  7. #functions
  8. def findcoef(zacr,zacr2,zkth,zkth2 ) :
  9. alpha1 = zacr * log ( cosh( (1. -zkth ) / zacr ) )
  10. alpha2 = zacr2 * log ( cosh( (1. -zkth2 ) / zacr2 ) )
  11. alpha3 = zacr * log ( cosh( (jpk-zkth ) / zacr ) )
  12. alpha4 = zacr2 * log ( cosh( (jpk-zkth2 ) / zacr2 ) )
  13. beta1 = tanh( (1.5-zkth ) / zacr )
  14. beta2 = tanh( (1.5-zkth2 ) / zacr2 )
  15. beta3 = tanh( (jpk-0.5-zkth ) / zacr )
  16. beta4 = tanh( (jpk-0.5-zkth2 ) / zacr2 )
  17. # declare matrix
  18. A=np.matrix( [[1.,1.,alpha1, alpha2], [1., jpk, alpha3, alpha4 ], [ 0., 1., beta1, beta2],[ 0.,1, beta3, beta4]] )
  19. B=np.matrix( [[h0],[hmax],[dzmin],[dzmax] ] )
  20. # [zsur, za0, za1, za2]=np.linalg.solve(A,B)
  21. return np.linalg.solve(A,B)
  22. def printcoef() : #zacr,zkth,zsur,za0,za1,za2,zkth2,zacr2) :
  23. print "ppsur =", zsur
  24. print "ppaa0 = ", za0
  25. print "ppa1 = ", za1
  26. print "ppkth =", zkth
  27. print "ppacr = ", zacr
  28. print "ldbleranh = ", ldbletan
  29. print "ppa2 = ", za2
  30. print "ppkth2 =", zkth2
  31. print "ppacr2 = ", zacr2
  32. def printall(val) :
  33. printcoef()
  34. jk=0
  35. while ( jk < jpk ) :
  36. print "%4d %9.2f %9.2f %9.3f %9.3f " % (jk+1, gdepw_t[jk], gdept_t[jk],e3w_t[jk],e3t_t[jk])
  37. jk=jk+1
  38. def depfunc( zsur, za0, za1, zkth, zacr, za2, zkth2, zacr2 , ldbletan ) :
  39. zw = np.arange(1 ,jpk+1 )
  40. zt = np.arange(1.5,jpk+1.5)
  41. ztmpw1 = zsur + za0 * zw + za1 * zacr * np.log ( np.cosh( (zw-zkth ) / zacr ) )
  42. if ( ldbletan ) :
  43. ztmpw2 = za2 * zacr2* np.log ( np.cosh( (zw-zkth2) / zacr2 ) )
  44. ztmpw = ztmpw1 + ztmpw2
  45. ztmpt1 = zsur + za0 * zt + za1 * zacr * np.log ( np.cosh( (zt-zkth ) / zacr ) )
  46. if ( ldbletan ) :
  47. ztmpt2 = za2 * zacr2* np.log ( np.cosh( (zt-zkth2) / zacr2 ) )
  48. ztmpt = ztmpt1 + ztmpt2
  49. return ( ztmpw1, ztmpw2, ztmpw, ztmpt1, ztmpt2, ztmpt )
  50. def e3func( za0, za1, zkth, zacr, za2, zkth2, zacr2 , ldbletan ) :
  51. zw = np.arange(1 ,jpk+1 )
  52. zt = np.arange(1.5,jpk+1.5)
  53. ztmpw1 = za0 + za1 * np.tanh( (zw-zkth ) / zacr )
  54. if ( ldbletan ) :
  55. ztmpw2 = za2 * np.tanh( (zw-zkth2) / zacr2 )
  56. ztmpw = ztmpw1 + ztmpw2
  57. ztmpt1 = za0 + za1 * np.tanh( (zt-zkth ) / zacr )
  58. if ( ldbletan ) :
  59. ztmpt2 = za2 * np.tanh( (zt-zkth2) / zacr2 )
  60. ztmpt = ztmpt1 + ztmpt2
  61. return ( ztmpw1, ztmpw2, ztmpw, ztmpt1, ztmpt2, ztmpt )
  62. def update(val):
  63. global gdepw_t, gdept_t, e3w_t, e3t_t
  64. global zkth, zacr, zkth2, zacr2, zsur, za0, za1, za2
  65. zkth = skth.val
  66. zacr = sacr.val
  67. zkth2 = skth2.val
  68. zacr2 = sacr2.val
  69. solution=findcoef(zacr,zacr2,zkth,zkth2)
  70. zsur = solution [0,0]
  71. za0 = solution [1,0]
  72. za1 = solution [2,0]
  73. za2 = solution [3,0]
  74. # printcoef() #zacr,zkth,zsur,za0,za1,za2,zkth2,zacr2)
  75. gdepw_0, gdepw_2, gdepw_t, gdept_0, gdept_2, gdept_t = depfunc( zsur, za0, za1, zkth, zacr, za2, zkth2, zacr2 , ldbletan )
  76. e3w_0, e3w_2, e3w_t, e3t_0, e3t_2, e3t_t = e3func( za0, za1, zkth, zacr, za2, zkth2, zacr2 , ldbletan )
  77. l1.set_xdata(gdepw_t)
  78. l1.set_ydata(e3t_t)
  79. l2.set_ydata(gdepw_t)
  80. l3.set_ydata(e3t_t)
  81. l4.set_ydata(e3t_0)
  82. l5.set_ydata(e3t_2)
  83. fig.canvas.draw_idle()
  84. # fig1.canvas.draw_idle()
  85. # fig2.canvas.draw_idle()
  86. # fig3.canvas.draw_idle()
  87. # fig4.canvas.draw_idle()
  88. def reset(event):
  89. skth.reset()
  90. sacr.reset()
  91. skth2.reset()
  92. sacr2.reset()
  93. update
  94. def finish(event):
  95. print "Ciao"
  96. quit()
  97. #====================================================================================
  98. h0 = 0.
  99. hmax = 6000.
  100. dzmin = 1.
  101. dzmax = 50
  102. jpk = 300
  103. ldbletan = True
  104. # coef to be set before (default value)
  105. zacr = 38.4998412469 # 80 # 7.0*3
  106. zkth = 317.238187329 # 184.2121644 #15.35101370000000*jpk/75.*3
  107. zacr2 = 121.356963487 # 13.0*3
  108. zkth2 = 31.5541059316 # 48.029893720000*jpk/75.
  109. fig, ax = plt.subplots()
  110. plt.subplots_adjust(left=0.05, bottom=0.25)
  111. axcolor = 'lightgoldenrodyellow'
  112. axkth = plt.axes([0.25, 0.16, 0.65, 0.02], facecolor=axcolor)
  113. axacr = plt.axes([0.25, 0.12, 0.65, 0.02], facecolor=axcolor)
  114. axkth2 = plt.axes([0.25, 0.08, 0.65, 0.02], facecolor=axcolor)
  115. axacr2 = plt.axes([0.25, 0.04, 0.65, 0.02], facecolor=axcolor)
  116. skth2 = Slider(axkth2, 'kth2', 0.1, 400.0, valinit=zkth2)
  117. skth = Slider(axkth , 'kth ', 0.1, 400.0, valinit=zkth )
  118. sacr = Slider(axacr, 'acr', 0.1, 150, valinit=zacr )
  119. sacr2 = Slider(axacr2, 'acr2', 0.1, 150.0, valinit=zacr2)
  120. A=np.ones( (4,4) )
  121. B=np.ones( (4,1 ) )
  122. solution=findcoef(zacr,zacr2,zkth,zkth2)
  123. zsur = solution [0,0]
  124. za0 = solution [1,0]
  125. za1 = solution [2,0]
  126. za2 = solution [3,0]
  127. gdepw_0, gdepw_2, gdepw_t, gdept_0, gdept_2, gdept_t = depfunc( zsur, za0, za1, zkth, zacr, za2, zkth2, zacr2 , ldbletan )
  128. e3w_0, e3w_2, e3w_t, e3t_0, e3t_2, e3t_t = e3func( za0, za1, zkth, zacr, za2, zkth2, zacr2 , ldbletan )
  129. printcoef() #zacr,zkth,zsur,za0,za1,za2,zkth2,zacr2)
  130. fig1=plt.subplot(2,2,1)
  131. l1,=plt.plot(gdepw_t, e3t_t, 'g^')
  132. gr1=plt.grid(True)
  133. plt.axis([0, 6000, 0, 60])
  134. fig2=plt.subplot(2,2,2)
  135. l2, =plt.plot(gdepw_t ,'r^')
  136. gr2 =plt.grid(True)
  137. fig3=plt.subplot(2,2,3)
  138. l3,=plt.plot(e3t_t ,'b^')
  139. gr3=plt.grid(True)
  140. fig4=plt.subplot( 2,2,4 )
  141. l4,l5 =plt.plot(e3t_0 ,'b^', e3t_2, 'b+' )
  142. gr4=plt.grid(True)
  143. resetax = plt.axes([0.02, 0.075, 0.1, 0.04])
  144. button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
  145. resetax2 = plt.axes([0.02, 0.025, 0.1, 0.04])
  146. button2 = Button(resetax2, 'Quit', color='red', hovercolor='0.575')
  147. resetax3 = plt.axes([0.02, 0.125, 0.1, 0.04])
  148. button3 = Button(resetax3, 'Print', color='blue', hovercolor='0.575')
  149. button.on_clicked(reset)
  150. button2.on_clicked(finish)
  151. button3.on_clicked(printall)
  152. skth2.on_changed(update)
  153. skth.on_changed(update)
  154. sacr.on_changed(update)
  155. sacr2.on_changed(update)
  156. plt.show()