extrap2d.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # This function extrapolates horizontally a (x,y) field using the
  2. # nearest neighbour method.
  3. #
  4. # Usage : python extrap2d.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. (ly0,lx0)=var0_a.shape
  48. mask3d=var0_a.mask
  49. if mask3d.size==1 :
  50. if var0_a.id in ('gsu','gru','gtu','ssu_m'):
  51. varmask='umask'
  52. elif var0_a.id in ('gsv','grv','gtv','ssv_m'):
  53. varmask='vmask'
  54. else:
  55. varmask='tmask'
  56. if (ly0,lx0) == (1021,1442) :
  57. f=cdms.open('/home/vguemas/SCRIP/interpvert/mesh_mask_glorys2v1_lev0.nc')
  58. mask3d=1-f(varmask,squeeze=1)
  59. f.close()
  60. #
  61. f=cdms.open(fileM)
  62. lon=f(varlon,squeeze=1)
  63. lat=f(varlat,squeeze=1)
  64. f.close()
  65. #
  66. f=cdms.open(fileF)
  67. Pfill=f(varF,squeeze=1)
  68. f.close()
  69. #
  70. var4=N.zeros((ly0,lx0))
  71. var4=var4.astype('d')
  72. var4=N.where(mask3d==False,var0_a,var4)
  73. pi=N.pi
  74. coslat=N.cos(lat*pi/180)
  75. coslon=N.cos(lon*pi/180)
  76. sinlat=N.sin(lat*pi/180)
  77. sinlon=N.sin(lon*pi/180)
  78. indexes1=N.where(Pfill[:,:]>=1)
  79. for ind in N.arange(indexes1[0].shape[0]) :
  80. jy=indexes1[0][ind]
  81. jx=indexes1[1][ind]
  82. distance=MV.arccos(coslat[jy,jx]*coslat*(coslon[jy,jx]*coslon+sinlon[jy,jx]*sinlon)+sinlat[jy,jx]*sinlat)
  83. distance=N.where(mask3d[:,:]==False,distance,1e20)
  84. dismin=distance.min()
  85. test=cdms.createVariable(var0_a[:,:],mask=MV.where(distance==dismin,False,True))
  86. var4[jy,jx]=test.mean()
  87. maskout=MV.where(Pfill>0.5,False,mask3d)
  88. var4=cdms.createVariable(var4,id=var0_a.id)
  89. var4=MV.where(maskout<0.5,var4,1e20)
  90. var4.getAxis(0).id='y'
  91. var4.getAxis(1).id='x'
  92. var4.id=var0_a.id
  93. h=cdms.open(fileOUT,'w')
  94. h.write(var4)
  95. h.close()