cross_sections.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env python
  2. ##############################################################
  3. # B a r a K u d a
  4. #
  5. # Generate netcdf files of cross-sections
  6. #
  7. # L. Brodeau, 2016
  8. ##############################################################
  9. import sys
  10. import numpy as nmp
  11. from netCDF4 import Dataset
  12. import barakuda_orca as bo
  13. import barakuda_tool as bt
  14. import barakuda_ncio as bnc
  15. venv_needed = {'ORCA','EXP','DIAG_D','i_do_sect','TS_SECTION_FILE','MM_FILE','NN_T','NN_S'}
  16. vdic = bt.check_env_var(sys.argv[0], venv_needed)
  17. i_do_sect = int(vdic['i_do_sect'])
  18. if i_do_sect != 1: print 'ERROR: sys.argv[0] => why are we here when i_do_sect != 1 ???'; sys.exit(0)
  19. f_sections = vdic['TS_SECTION_FILE']
  20. CONFEXP = vdic['ORCA']+'-'+vdic['EXP']
  21. cnexec = sys.argv[0]
  22. na = len(sys.argv)
  23. if na != 3:
  24. print 'Usage : '+cnexec+' <EXP_grid_T.nc> <year>'
  25. sys.exit(0)
  26. cf_in = sys.argv[1]
  27. cyear = sys.argv[2] ; jyear = int(cyear); cyear = '%4.4i'%jyear
  28. cv_t = vdic['NN_T']
  29. cv_s = vdic['NN_S']
  30. print 'Current year is '+cyear+' !\n'
  31. bt.chck4f(vdic['MM_FILE'])
  32. id_mm = Dataset(vdic['MM_FILE'])
  33. rmsk = id_mm.variables['tmask'][0,:,:,:]
  34. xlon = id_mm.variables['glamt'][0,:,:]
  35. xlat = id_mm.variables['gphit'][0,:,:]
  36. id_mm.close()
  37. [ nk, nj, ni ] = rmsk.shape
  38. bt.chck4f(cf_in)
  39. id_in = Dataset(cf_in)
  40. try:
  41. vdepth = id_in.variables['deptht'][:]
  42. except KeyError as e:
  43. vdepth = id_in.variables['olevel'][:]
  44. XT = id_in.variables[cv_t][:,:,:,:]
  45. XS = id_in.variables[cv_s][:,:,:,:]
  46. id_in.close()
  47. [ Nt, nk0, nj0, ni0 ] = XT.shape
  48. if [ nk0, nj0, ni0 ] != [ nk, nj, ni ]: print 'ERROR: ssx_boxes.py => mask and field disagree in shape!'; sys.exit(0)
  49. print 'Nt, nk, nj, ni =', Nt, nk, nj, ni
  50. # Masking:
  51. for jt in range(Nt):
  52. XT[jt,:,:,:] = rmsk[:,:,:]*XT[jt,:,:,:] + (1. - rmsk[:,:,:])*-9999.
  53. XS[jt,:,:,:] = rmsk[:,:,:]*XS[jt,:,:,:] + (1. - rmsk[:,:,:])*-9999.
  54. vtime = nmp.zeros(Nt)
  55. for jt in range(Nt): vtime[jt] = float(jyear) + (float(jt) + 0.5)/float(Nt)
  56. # Getting sections:
  57. vboxes, vlon1, vlat1, vlon2, vlat2 = bt.read_coor(f_sections, ctype='float', lTS_bounds=False)
  58. js = -1
  59. for csname in vboxes:
  60. js = js + 1
  61. print'\n *** '+sys.argv[0]+': treating section '+csname
  62. ( i1, i2, j1, j2 ) = bo.transect_zon_or_med(vlon1[js], vlon2[js], vlat1[js], vlat2[js], xlon, xlat)
  63. print csname+' :'
  64. print '(lon1, lon2, lat1, lat2) =', vlon1[js], vlon2[js], vlat1[js], vlat2[js]
  65. print ' => i1, i2, j1, j2 =', i1, i2, j1, j2
  66. print ''
  67. if i1 > i2: print 'ERROR: cross_sections.py => i1 > i2 !'; sys.exit(0)
  68. if j1 > j2: print 'ERROR: cross_sections.py => j1 > j2 !'; sys.exit(0)
  69. if i1 == i2:
  70. print 'Meridional section!'
  71. caxis = 'y' ; cxn = 'lat'
  72. vaxis = xlat[j1:j2,i1]
  73. imsk = rmsk[:,j1:j2,i1]
  74. ZT = XT[:,:,j1:j2,i1]
  75. ZS = XS[:,:,j1:j2,i1]
  76. if j1 == j2:
  77. print 'Zonal section!'
  78. caxis = 'x'; cxn = 'lon'
  79. vx = xlon[j1,i1:i2] ; vaxis = nmp.zeros(len(vx)) ; vaxis[:] = vx[:]
  80. ivf = nmp.where(vx>180); vaxis[ivf] = vx[ivf] - 360.
  81. imsk = rmsk[:,j1,i1:i2]
  82. ZT = XT[:,:,j1,i1:i2]
  83. ZS = XS[:,:,j1,i1:i2]
  84. cf_out = vdic['DIAG_D']+'/TS_section_'+csname+'.nc'
  85. bnc.wrt_appnd_2dt_series(vaxis, -vdepth, vtime, ZT, cf_out, cv_t,
  86. missing_val=-9999.,
  87. cxdnm=cxn, cydnm='depth', cxvnm=cxn, cyvnm='depth',
  88. cu_t='year', cu_d='deg.C', cln_d='Potential temperature',
  89. xd2=ZS, cvar2=cv_s, cln_d2='Salinity', cun2='PSU')