vertextrap.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # This function extrapolates vertically a (x,y,z) field extending
  2. # linearly the vertical slope of the profile.
  3. #
  4. # Usage : python vertextrap.py <input file> <input variable name>
  5. # <input grid description file> <input depth in grid file>
  6. # <output file>
  7. #
  8. # History : Virginie Guemas - Initial version - March 2014
  9. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. import cdms2 as cdms
  11. import sys,string
  12. from cdms2 import MV
  13. import numpy as N
  14. from numpy import ma
  15. #
  16. # 1. Input arguments
  17. # ===================
  18. #
  19. # Input file and var names :
  20. # ---------------------------
  21. fileIN=sys.argv[1]
  22. varIN=sys.argv[2]
  23. #
  24. # Meshmask file :
  25. # ----------------
  26. fileM=sys.argv[3]
  27. varD=sys.argv[4]
  28. #
  29. # Output file name :
  30. # -------------------
  31. fileOUT=sys.argv[5]
  32. #
  33. # 2. Get the input files
  34. # =======================
  35. #
  36. f=cdms.open(fileIN)
  37. var0_a=f(varIN,squeeze=1)
  38. f.close()
  39. mask3d=var0_a.mask
  40. (lz0,ly0,lx0)=var0_a.shape
  41. #
  42. f=cdms.open(fileM)
  43. dep=f(varD,squeeze=1)
  44. f.close()
  45. #
  46. varout=N.zeros((lz0,ly0,lx0))
  47. varout=varout.astype('d')
  48. varout=N.where(mask3d==False,var0_a,varout)
  49. maskout=N.zeros((lz0,ly0,lx0))
  50. for jz in N.arange(lz0) :
  51. if mask3d[jz,:,:].mean() == 1 :
  52. ratio=(dep[jz]-dep[jz-1])/(dep[jz-1]-dep[jz-2])
  53. varout[jz,:,:]=varout[jz-1,:,:]+(varout[jz-1,:,:]-varout[jz-2,:,:])*ratio
  54. maskout[jz,:,:]=maskout[jz-1,:,:]
  55. else :
  56. maskout[jz,:,:]=mask3d[jz,:,:]
  57. varout=cdms.createVariable(varout,id=var0_a.id)
  58. varout=MV.where(maskout<0.5,varout,1e20)
  59. varout.getAxis(0)[:]=var0_a.getAxis(0)[:]
  60. varout.getAxis(0).id='z'
  61. varout.getAxis(1).id='y'
  62. varout.getAxis(2).id='x'
  63. varout.id=var0_a.id
  64. h=cdms.open(fileOUT,'w')
  65. h.write(varout)
  66. h.close()