interp_vert.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # This function interpolates vertically a (x,y,z) field using the
  2. # conservative method.
  3. #
  4. # Usage : python interp_vert.py <input file> <input variable name>
  5. # <input grid description file> <input level thickness in grid
  6. # file> <input mask file> <input mask variable> <output grid
  7. # description file> <output level thickness in grid file>
  8. # <output level depth in grid file> <output file>
  9. #
  10. # History : Virginie Guemas - Initial version adpated
  11. # from PhD tools - 2012
  12. # Cleaning - March 2014
  13. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  14. #import cdms,regrid,sys,string
  15. import cdms2 as cdms
  16. import sys,string
  17. #import MLab,MV,MA,Numeric as N
  18. from cdms2 import MV
  19. import numpy as N
  20. from numpy import ma
  21. #import vcs,time
  22. #x=vcs.init()
  23. #
  24. # 1. Input arguments
  25. # ===================
  26. #
  27. # Input file and var names :
  28. # ---------------------------
  29. fileIN=sys.argv[1]
  30. varIN=sys.argv[2]
  31. #
  32. # Input grid file :
  33. # ------------------
  34. fileIG=sys.argv[3]
  35. varIT=sys.argv[4]
  36. #
  37. # Input mask file :
  38. # ------------------
  39. fileM=sys.argv[5]
  40. varM=sys.argv[6]
  41. #
  42. # Output grid file :
  43. # -------------------
  44. fileOG=sys.argv[7]
  45. varOT=sys.argv[8]
  46. varOD=sys.argv[9]
  47. #
  48. # Output file name :
  49. # -------------------
  50. fileOUT=sys.argv[10]
  51. #
  52. # 2. Get the input files
  53. # =======================
  54. #
  55. f=cdms.open(fileIN)
  56. var0_a=f(varIN,squeeze=1)
  57. f.close()
  58. if var0_a.rank() != 3 :
  59. print "3d input file please !"
  60. dims_var0=var0_a.getOrder()
  61. if dims_var0[0] == 'x' :
  62. lx0=var0_a.shape[0]
  63. if dims_var0[1] == 'y' :
  64. ly0=var0_a.shape[1]
  65. lz0=var0_a.shape[2]
  66. else :
  67. ly0=var0_a.shape[2]
  68. lz0=var0_a.shape[1]
  69. else :
  70. if dims_var0[0] == 'y' :
  71. ly0=var0_a.shape[0]
  72. if dims_var0[1] == 'x' :
  73. lx0=var0_a.shape[1]
  74. lz0=var0_a.shape[2]
  75. else :
  76. lx0=var0_a.shape[2]
  77. lz0=var0_a.shape[1]
  78. else :
  79. lz0=var0_a.shape[0]
  80. if dims_var0[1] == 'x' :
  81. lx0=var0_a.shape[1]
  82. ly0=var0_a.shape[2]
  83. else :
  84. lx0=var0_a.shape[2]
  85. ly0=var0_a.shape[1]
  86. #
  87. var0=N.zeros((lz0,ly0,lx0))
  88. var0=var0.astype('d')
  89. #
  90. if dims_var0[0] == 'x' :
  91. if dims_var0[1] == 'y' :
  92. for jk in N.arange(lz0) :
  93. var0[jk,:,:]=N.transpose(var0_a[:,:,jk])
  94. else :
  95. for ji in N.arange(lx0) :
  96. var0[:,:,ji]=var0_a[ji,:,:]
  97. else :
  98. if dims_var0[0] == 'y' :
  99. if dims_var0[1] == 'x' :
  100. for jk in N.arange(lz0) :
  101. var0[jk,:,:]=var0_a[:,:,jk]
  102. else :
  103. for jk in N.arange(lz0) :
  104. var0[jk,:,:]=var0_a[:,jk,:]
  105. else :
  106. if dims_var0[1] == 'x' :
  107. for ji in N.arange(lx0) :
  108. var0[:,:,ji]=var0_a[:,ji,:]
  109. else :
  110. var0[:,:,:]=var0_a[:,:,:]
  111. var0=ma.masked_array(var0,mask=None,fill_value=None)
  112. #
  113. f=cdms.open(fileIG)
  114. thick_in=f(varIT,squeeze=1)
  115. f.close()
  116. #
  117. f=cdms.open(fileM)
  118. mask=f(varM,squeeze=1)
  119. f.close()
  120. if mask.rank() != 2 :
  121. print "2d input mask please !"
  122. dims_mask=mask.getOrder()
  123. if dims_mask[0] == 'x' :
  124. mask=N.transpose(mask)
  125. if mask.shape[1] != lx0 or mask.shape[0] != ly0 :
  126. print "same dims for mask and input file please !"
  127. #
  128. f=cdms.open(fileOG)
  129. thick_out=f(varOT,squeeze=1)
  130. depth_out=f(varOD,squeeze=1)
  131. f.close()
  132. lz1=thick_out.shape[0]
  133. if depth_out.shape[0] != lz1 :
  134. print "same dims for depth and thickness please !"
  135. #
  136. # 2. 2D to 3D mask file
  137. # =======================
  138. #
  139. mask_in=N.zeros((lz0,ly0,lx0))
  140. mask_in=mask_in.astype('d')
  141. for i in N.arange(lx0) :
  142. for j in N.arange(ly0) :
  143. if mask[j,i] > 0 :
  144. down=mask[j,i]
  145. for k in N.arange(down) :
  146. mask_in[k,j,i]=1.
  147. # 3. Vertical interpolation
  148. # ==========================
  149. #
  150. var1=N.zeros((lz1,ly0,lx0),'d')
  151. thick_bis=N.zeros((ly0,lx0),'d')
  152. thick_jk1=N.zeros((ly0,lx0),'d')
  153. mask_out=N.zeros((lz1,ly0,lx0),'d')
  154. #
  155. jk0=0
  156. for jk1 in N.arange(lz1) :
  157. while ( (jk0 <= (lz0-1)) & (N.add.reduce(thick_in[0:(jk0+1)]) <= N.add.reduce(thick_out[0:(jk1+1)])) ) :
  158. thick_bis=N.multiply(N.minimum(thick_in[jk0],N.add.reduce(thick_in[0:(jk0+1)])-N.add.reduce(thick_out[0:jk1])),mask_in[jk0,:,:])
  159. var1[jk1,:,:]=var1[jk1,:,:]+N.multiply(var0[jk0,:,:],thick_bis)
  160. thick_jk1=thick_jk1+thick_bis
  161. jk0=jk0+1
  162. if ( jk0 <= (lz0-1)) :
  163. thick_bis=N.multiply(N.minimum(thick_out[jk1],N.add.reduce(thick_out[0:(jk1+1)])-N.add.reduce(thick_in[0:jk0])),mask_in[jk0,:,:])
  164. var1[jk1,:,:]=var1[jk1,:,:]+N.multiply(var0[jk0,:,:],thick_bis)
  165. thick_jk1=thick_jk1+thick_bis
  166. mask_out[jk1,:,:]=N.where(thick_jk1>0.01,1.,0.)
  167. thick_jk1=N.where(thick_jk1==0.,1.,thick_jk1)
  168. var1[jk1,:,:]=var1[jk1,:,:]/thick_jk1
  169. thick_jk1[:,:]=0
  170. lon=cdms.createAxis(N.arange(lx0),id='x')
  171. lat=cdms.createAxis(N.arange(ly0),id='y')
  172. depth=cdms.createAxis(depth_out,id='z')
  173. var2=cdms.createVariable(var1,id=var0_a.id)
  174. var2.getAxis(0)[:]=depth_out
  175. var2.getAxis(0).id='z'
  176. var2.getAxis(1).id='y'
  177. var2.getAxis(2).id='x'
  178. var3=MV.where(mask_out>0.,var1,1e20)
  179. var3.id=var0_a.id
  180. var3.getAxis(1).id='y'
  181. var3.getAxis(2).id='x'
  182. var3.getAxis(0).id='z'
  183. var3.getAxis(0)[:]=depth_out
  184. g=cdms.open(fileOUT,'w')
  185. g.write(var3)
  186. g.close()