ec_build_wind_speed.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/env python
  2. #
  3. # L. Brodeau, 2017
  4. #
  5. # Compute curl, aka relative vorticity from 2D vector on a lat-lon spherical
  6. # grid a la ECMWF
  7. #
  8. import sys
  9. import numpy as nmp
  10. from netCDF4 import Dataset
  11. import string
  12. import barakuda_tool as bt
  13. cv_wmod='wspd10m'
  14. if len(sys.argv) != 4:
  15. print 'Usage: '+sys.argv[0]+' <FILE_VEC_V.nc> <name_Vx> <name_Vy>'
  16. sys.exit(0)
  17. cf_Vx = sys.argv[1]
  18. cv_Vx = sys.argv[2]
  19. cv_Vy = sys.argv[3]
  20. cf_Vy = string.replace(cf_Vx, cv_Vx, cv_Vy)
  21. cf_wmod = string.replace(cf_Vx, cv_Vx, cv_wmod)
  22. print " cf_Vx = ", cf_Vx
  23. print " cf_Vy = ", cf_Vy
  24. print " cf_wmod = ", cf_wmod
  25. print "\n"
  26. rmult = 1.
  27. if cv_Vx == 'EWSS':
  28. rmult = 1000.
  29. CUNIT_WMOD= '???'
  30. if cv_Vx == 'U10M':
  31. rmult = 1.
  32. CUNIT_WMOD= 's^-1'
  33. # U
  34. # ~
  35. bt.chck4f(cf_Vy)
  36. f_Vy_in = Dataset(cf_Vy)
  37. # Extracting the longitude and 1D array:
  38. vlon = f_Vy_in.variables['lon'][:]
  39. clnm_lon = f_Vy_in.variables['lon'].long_name ; cunt_lon = f_Vy_in.variables['lon'].units
  40. csnm_lon = f_Vy_in.variables['lon'].standard_name
  41. print 'LONGITUDE: ', clnm_lon, cunt_lon, csnm_lon
  42. # Extracting the longitude 1D array:
  43. vlat = f_Vy_in.variables['lat'][:]
  44. clnm_lat = f_Vy_in.variables['lat'].long_name ; cunt_lat = f_Vy_in.variables['lat'].units
  45. csnm_lat = f_Vy_in.variables['lat'].standard_name
  46. print 'LATGITUDE: ', clnm_lat, cunt_lat, csnm_lat
  47. # Extracting time 1D array:
  48. vtime = f_Vy_in.variables['time'][:] ; cunt_time = f_Vy_in.variables['time'].units
  49. print 'TIME: ', cunt_time, '\n'
  50. # Extracting a variable, ex: "t" the 3D+T field of temperature:
  51. xty0 = f_Vy_in.variables[cv_Vy][0,:,:]
  52. cunt_Vy = f_Vy_in.variables[cv_Vy].units
  53. code_Vy = f_Vy_in.variables[cv_Vy].code
  54. ctab_Vy = f_Vy_in.variables[cv_Vy].table
  55. print cv_Vy+': ', cunt_Vy, code_Vy, ctab_Vy, '\n'
  56. # V
  57. # ~~~
  58. bt.chck4f(cf_Vx)
  59. f_Vx_in = Dataset(cf_Vx)
  60. xtx0 = f_Vx_in.variables[cv_Vx][0,:,:]
  61. cunt_Vx = f_Vx_in.variables[cv_Vx].units
  62. code_Vx = f_Vx_in.variables[cv_Vx].code
  63. ctab_Vx = f_Vx_in.variables[cv_Vx].table
  64. print cv_Vx+': ', cunt_Vx, code_Vx, ctab_Vx, '\n'
  65. # Checking dimensions
  66. # ~~~~~~~~~~~~~~~~~~~
  67. dim_Vy = xty0.shape ; dim_Vx = xtx0.shape
  68. if dim_Vy != dim_Vx:
  69. print 'Shape problem!!!'; print dim_Vy , dim_Vx
  70. print '\n'
  71. Nt = len(vtime)
  72. (nj, ni) = dim_Vy
  73. print 'ni, nj, Nt = ', ni, nj, Nt
  74. # Building Curl
  75. # ~~~~~~~~~~~
  76. xwmod = nmp.zeros(( nj, ni ))
  77. # Creating output file
  78. # ~~~~~~~~~~~~~~~~~~~~
  79. f_out = Dataset(cf_wmod, '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_wmod = f_out.createVariable(cv_wmod,'f4',('time','lat','lon',))
  89. # Attributes
  90. id_tim.units = cunt_time
  91. id_lat.long_name = clnm_lat
  92. id_lat.units = cunt_lat
  93. id_lat.standard_name = csnm_lat
  94. id_lon.long_name = clnm_lon
  95. id_lon.units = cunt_lon
  96. id_lon.standard_name = csnm_lon
  97. id_tim.units = cunt_time
  98. id_wmod.long_name = 'Curl of vector '+cv_Vx+' and '+cv_Vy
  99. id_wmod.units = CUNIT_WMOD
  100. id_wmod.code = '???'
  101. id_wmod.table = '128'
  102. f_out.About = 'Created by Barakuda using '+cv_Vx+' and '+cv_Vy+'.'
  103. # Filling variables:
  104. id_lat[:] = vlat[:]
  105. id_lon[:] = vlon[:]
  106. for jt in range(Nt):
  107. print ' *** jt = ', jt
  108. xty = rmult*f_Vy_in.variables[cv_Vy][jt,:,:]
  109. xtx = rmult*f_Vx_in.variables[cv_Vx][jt,:,:]
  110. xwmod[:,:] = nmp.sqrt(xtx*xtx + xty*xty)
  111. id_tim[jt] = vtime[jt]
  112. id_wmod[jt,:,:] = xwmod[:,:]
  113. f_out.close()
  114. f_Vx_in.close()
  115. f_Vy_in.close()
  116. print 'Bye!'