extrap.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # This function extrapolates horizontally each level of a (x,y,z)
  2. # field using the nearest neighbour method.
  3. #
  4. # Usage : python extrap.py <input file> <input variable name>
  5. # <grid description file> <2d longitude name in grid file>
  6. # <2d latitude name in grid file> <mask 1=extrapolate>
  7. # <mask variable name> <output file>
  8. #
  9. # History : Virginie Guemas - Initial version - 2012
  10. # Virginie Guemas - Masking the outputs - March 2014
  11. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  12. import cdms2 as cdms
  13. import sys,string
  14. from cdms2 import MV
  15. import numpy as N
  16. from numpy import ma
  17. #
  18. # 1. Input arguments
  19. # ===================
  20. #
  21. # Input file and var names :
  22. # ---------------------------
  23. fileIN=sys.argv[1]
  24. varIN=sys.argv[2]
  25. #
  26. # Meshmask file :
  27. # ----------------
  28. fileM=sys.argv[3]
  29. varlon=sys.argv[4]
  30. varlat=sys.argv[5]
  31. #
  32. # Location of points to fill :
  33. # ----------------------------
  34. fileF=sys.argv[6]
  35. varF=sys.argv[7]
  36. #
  37. # Output file name :
  38. # -------------------
  39. fileOUT=sys.argv[8]
  40. #
  41. # 2. Get the input files
  42. # =======================
  43. #
  44. f=cdms.open(fileIN)
  45. var0_a=f(varIN,squeeze=1)
  46. f.close()
  47. mask3d=var0_a.mask
  48. (lz1,ly0,lx0)=var0_a.shape
  49. #
  50. f=cdms.open(fileM)
  51. lon=f(varlon,squeeze=1)
  52. lat=f(varlat,squeeze=1)
  53. f.close()
  54. #
  55. f=cdms.open(fileF)
  56. Pfill=f(varF,squeeze=1)
  57. f.close()
  58. #
  59. var4=N.zeros((lz1,ly0,lx0))
  60. var4=var4.astype('d')
  61. var4=N.where(mask3d==False,var0_a,var4)
  62. pi=N.pi
  63. coslat=N.cos(lat*pi/180)
  64. coslon=N.cos(lon*pi/180)
  65. sinlat=N.sin(lat*pi/180)
  66. sinlon=N.sin(lon*pi/180)
  67. indexes1=N.where(N.sum(Pfill[:,:,:],0)>=1)
  68. for ind in N.arange(indexes1[0].shape[0]) :
  69. jy=indexes1[0][ind]
  70. jx=indexes1[1][ind]
  71. distance=MV.arccos(coslat[jy,jx]*coslat*(coslon[jy,jx]*coslon+sinlon[jy,jx]*sinlon)+sinlat[jy,jx]*sinlat)
  72. indexes2=N.where(Pfill[:,jy,jx]>=1)
  73. for ind2 in N.arange(indexes2[0].shape[0]) :
  74. jz=indexes2[0][ind2]
  75. if mask3d[jz,:,:].mean() < 1 :
  76. distance=N.where(mask3d[jz,:,:]==False,distance,1e20)
  77. dismin=distance.min()
  78. test=cdms.createVariable(var0_a[jz,:,:],mask=MV.where(distance==dismin,False,True))
  79. var4[jz,jy,jx]=test.mean()
  80. maskout=MV.where(Pfill>0.5,False,mask3d)
  81. var4=cdms.createVariable(var4,id=var0_a.id)
  82. var4=MV.where(maskout<0.5,var4,1e20)
  83. var4.getAxis(0).id='z'
  84. var4.getAxis(0)[:]=var0_a.getAxis(0)[:]
  85. var4.getAxis(1).id='y'
  86. var4.getAxis(2).id='x'
  87. var4.id=var0_a.id
  88. h=cdms.open(fileOUT,'w')
  89. h.write(var4)
  90. h.close()