build_windmod_eke_from_u_v.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/usr/bin/env python
  2. #
  3. # L. Brodeau, Dec. 2015
  4. import sys
  5. import numpy as nmp
  6. from netCDF4 import Dataset
  7. import string
  8. from os.path import basename
  9. import barakuda_tool as bt
  10. import barakuda_thermo as bthermo
  11. cv_u='U10M'
  12. cv_v='V10M'
  13. cv_wm ='WIND_MOD_10M' ; # OUTPUT !
  14. cv_ke ='EKE'
  15. if len(sys.argv) < 2 and len(sys.argv) > 3:
  16. print 'Usage: '+sys.argv[0]+' <IN_FILE_U.nc> (Year)'
  17. sys.exit(0)
  18. cf_u = sys.argv[1]
  19. cf_v = string.replace(cf_u, cv_u, cv_v)
  20. cf_wm = basename(string.replace(cf_u, cv_u, cv_wm))
  21. if len(sys.argv) == 3:
  22. cvy = sys.argv[2]
  23. iyear = int(cvy)
  24. #print iyear ; sys.exit(0)
  25. # First need time length:
  26. bt.chck4f(cf_u) ; f_u_in = Dataset(cf_u)
  27. vlon = f_u_in.variables['lon'][:]
  28. cunt_lon = f_u_in.variables['lon'].units
  29. print 'LONGITUDE: ', cunt_lon
  30. # Extracting the longitude 1D array:
  31. vlat = f_u_in.variables['lat'][:]
  32. cunt_lat = f_u_in.variables['lat'].units
  33. print 'LATITUDE: ', cunt_lat
  34. # Extracting time 1D array:
  35. vtime = f_u_in.variables['time'][:] ; cunt_time = f_u_in.variables['time'].units
  36. print 'TIME: ', cunt_time, '\n'
  37. f_u_in.close()
  38. Nt = len(vtime)
  39. print 'Nt = ', Nt
  40. #ii = Nt/31
  41. for jt in range(Nt):
  42. #vtime[jt] = float(iyear) + 365./float(Nt) * float(jt)+0.5
  43. print ' *** jt = ', jt
  44. # U10M
  45. # ~~~~
  46. if jt == 0:
  47. bt.chck4f(cf_u)
  48. f_u_in = Dataset(cf_u)
  49. cunt_u = f_u_in.variables[cv_u].units
  50. xu = f_u_in.variables[cv_u][jt,:,:]
  51. if jt == Nt-1: f_u_in.close()
  52. # V10M
  53. # ~~~
  54. if jt == 0:
  55. bt.chck4f(cf_v)
  56. f_v_in = Dataset(cf_v)
  57. cunt_v = f_v_in.variables[cv_v].units
  58. xv = f_v_in.variables[cv_v][jt,:,:]
  59. if jt == Nt-1: f_v_in.close()
  60. # Checking dimensions
  61. # ~~~~~~~~~~~~~~~~~~~
  62. if jt == 0:
  63. dim_u = xu.shape ; dim_v = xv.shape
  64. if dim_u != dim_v:
  65. print 'Shape problem!!!'; print dim_u , dim_v
  66. print '\n'
  67. [ nj, ni ] = dim_u
  68. print 'ni, nj, nt = ', ni, nj, Nt
  69. xwm = nmp.zeros(nj*ni) ; xwm.shape = dim_u
  70. xke = nmp.zeros(nj*ni) ; xke.shape = dim_u
  71. # Building wm
  72. # ~~~~~~~~~~~
  73. xke = xu*xu + xv*xv
  74. xwm = nmp.sqrt(xke)
  75. xke = 0.5*xke
  76. # Creating output file
  77. # ~~~~~~~~~~~~~~~~~~~~
  78. if jt == 0:
  79. f_out = Dataset(cf_wm, 'w', format='NETCDF3_CLASSIC')
  80. # Dimensions:
  81. f_out.createDimension('lon', ni)
  82. f_out.createDimension('lat', nj)
  83. f_out.createDimension('time', None)
  84. # Variables
  85. id_lon = f_out.createVariable('lon','f4',('lon',))
  86. id_lat = f_out.createVariable('lat','f4',('lat',))
  87. id_tim = f_out.createVariable('time','f4',('time',))
  88. id_wm = f_out.createVariable(cv_wm,'f4',('time','lat','lon',))
  89. id_ke = f_out.createVariable(cv_ke,'f4',('time','lat','lon',))
  90. # Attributes
  91. id_tim.units = cunt_time
  92. #id_lat.long_name = clnm_lat
  93. id_lat.units = cunt_lat
  94. #id_lat.standard_name = csnm_lat
  95. #id_lon.long_name = clnm_lon
  96. id_lon.units = cunt_lon
  97. #id_lon.standard_name = csnm_lon
  98. id_tim.units = cunt_time
  99. #id_wm.long_name = 'Surface specific humidity at 2m, built from U10M, V10M and MSL'
  100. #id_wm.units = 'kg/kg'
  101. #id_wm.code = '133'
  102. #id_wm.table = '128'
  103. f_out.About = 'Created by L. Brodeau using MSL, U10M and V10M corresponding fields'
  104. # Filling variables:
  105. id_lat[:] = vlat[:]
  106. id_lon[:] = vlon[:]
  107. id_tim[jt] = vtime[jt]
  108. id_wm[jt,:,:] = xwm[:,:]
  109. id_ke[jt,:,:] = xke[:,:]
  110. if jt == Nt-1: f_out.close()
  111. print ' *** file '+cf_wm+' created !'
  112. print 'Bye!'