build_q2_from_d2_slp.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/env python
  2. #
  3. # L. Brodeau, Feb.2001
  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_d2='D2M'
  12. cv_p0='MSL'
  13. cv_q2='Q2M' ; # OUTPUT !
  14. if len(sys.argv) != 2:
  15. print 'Usage: '+sys.argv[0]+' <IN_FILE_D2.nc>'
  16. sys.exit(0)
  17. cf_d2 = sys.argv[1]
  18. cf_p0 = string.replace(cf_d2, cv_d2, cv_p0)
  19. cf_q2 = basename(string.replace(cf_d2, cv_d2, cv_q2))
  20. # First need time length:
  21. bt.chck4f(cf_d2) ; f_d2_in = Dataset(cf_d2)
  22. vlon = f_d2_in.variables['lon'][:]
  23. cunt_lon = f_d2_in.variables['lon'].units
  24. print 'LONGITUDE: ', cunt_lon
  25. # Extracting the longitude 1D array:
  26. vlat = f_d2_in.variables['lat'][:]
  27. cunt_lat = f_d2_in.variables['lat'].units
  28. print 'LATITUDE: ', cunt_lat
  29. # Extracting time 1D array:
  30. vtime = f_d2_in.variables['time'][:] ; cunt_time = f_d2_in.variables['time'].units
  31. print 'TIME: ', cunt_time, '\n'
  32. f_d2_in.close()
  33. Nt = len(vtime)
  34. print 'Nt = ', Nt
  35. for jt in range(Nt):
  36. print ' *** jt = ', jt
  37. # D2M
  38. # ~~~
  39. if jt == 0:
  40. bt.chck4f(cf_d2)
  41. f_d2_in = Dataset(cf_d2)
  42. cunt_d2 = f_d2_in.variables[cv_d2].units
  43. xd2 = f_d2_in.variables[cv_d2][jt,:,:]
  44. if jt == Nt-1: f_d2_in.close()
  45. # MSL
  46. # ~~~
  47. if jt == 0:
  48. bt.chck4f(cf_p0)
  49. f_p0_in = Dataset(cf_p0)
  50. cunt_p0 = f_p0_in.variables[cv_p0].units
  51. xp0 = f_p0_in.variables[cv_p0][jt,:,:]
  52. if jt == Nt-1: f_p0_in.close()
  53. # Checking dimensions
  54. # ~~~~~~~~~~~~~~~~~~~
  55. if jt == 0:
  56. dim_d2 = xd2.shape ; dim_p0 = xp0.shape
  57. if dim_d2 != dim_p0:
  58. print 'Shape problem!!!'; print dim_d2 , dim_p0
  59. print '\n'
  60. [ nj, ni ] = dim_d2
  61. print 'ni, nj, nt = ', ni, nj, Nt
  62. xq2 = nmp.zeros(nj*ni) ; xq2.shape = dim_d2
  63. # Building q2
  64. # ~~~~~~~~~~~
  65. xq2 = bthermo.qa_e_p(bthermo.e_sat(xd2), xp0)
  66. # Creating output file
  67. # ~~~~~~~~~~~~~~~~~~~~
  68. if jt == 0:
  69. f_out = Dataset(cf_q2, 'w', format='NETCDF3_CLASSIC')
  70. # Dimensions:
  71. f_out.createDimension('lon', ni)
  72. f_out.createDimension('lat', nj)
  73. f_out.createDimension('time', None)
  74. # Variables
  75. id_lon = f_out.createVariable('lon','f4',('lon',))
  76. id_lat = f_out.createVariable('lat','f4',('lat',))
  77. id_tim = f_out.createVariable('time','f4',('time',))
  78. id_q2 = f_out.createVariable(cv_q2,'f4',('time','lat','lon',))
  79. # Attributes
  80. id_tim.units = cunt_time
  81. #id_lat.long_name = clnm_lat
  82. id_lat.units = cunt_lat
  83. #id_lat.standard_name = csnm_lat
  84. #id_lon.long_name = clnm_lon
  85. id_lon.units = cunt_lon
  86. #id_lon.standard_name = csnm_lon
  87. id_tim.units = cunt_time
  88. id_q2.long_name = 'Surface specific humidity at 2m, built from D2M and MSL'
  89. id_q2.units = 'kg/kg'
  90. id_q2.code = '133'
  91. id_q2.table = '128'
  92. f_out.About = 'Created by L. Brodeau using MSL and D2M corresponding fields'
  93. # Filling variables:
  94. id_lat[:] = vlat[:]
  95. id_lon[:] = vlon[:]
  96. id_tim[jt] = vtime[jt]
  97. id_q2[jt,:,:] = xq2[:,:]
  98. if jt == Nt-1: f_out.close()
  99. print 'Bye!'