cross_sections.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. vdepth = id_in.variables['olevel'][:]
  41. XT = id_in.variables[cv_t][:,:,:,:]
  42. XS = id_in.variables[cv_s][:,:,:,:]
  43. id_in.close()
  44. [ Nt, nk0, nj0, ni0 ] = XT.shape
  45. if [ nk0, nj0, ni0 ] != [ nk, nj, ni ]: print 'ERROR: ssx_boxes.py => mask and field disagree in shape!'; sys.exit(0)
  46. print 'Nt, nk, nj, ni =', Nt, nk, nj, ni
  47. # Masking:
  48. for jt in range(Nt):
  49. XT[jt,:,:,:] = rmsk[:,:,:]*XT[jt,:,:,:] + (1. - rmsk[:,:,:])*-9999.
  50. XS[jt,:,:,:] = rmsk[:,:,:]*XS[jt,:,:,:] + (1. - rmsk[:,:,:])*-9999.
  51. vtime = nmp.zeros(Nt)
  52. for jt in range(Nt): vtime[jt] = float(jyear) + (float(jt) + 0.5)/float(Nt)
  53. # Getting sections:
  54. vboxes, vlon1, vlat1, vlon2, vlat2 = bt.read_coor(f_sections, ctype='float', lTS_bounds=False)
  55. js = -1
  56. for csname in vboxes:
  57. js = js + 1
  58. print'\n *** '+sys.argv[0]+': treating section '+csname
  59. ( i1, i2, j1, j2 ) = bo.transect_zon_or_med(vlon1[js], vlon2[js], vlat1[js], vlat2[js], xlon, xlat)
  60. print csname+' :'
  61. print '(lon1, lon2, lat1, lat2) =', vlon1[js], vlon2[js], vlat1[js], vlat2[js]
  62. print ' => i1, i2, j1, j2 =', i1, i2, j1, j2
  63. print ''
  64. if i1 > i2: print 'ERROR: cross_sections.py => i1 > i2 !'; sys.exit(0)
  65. if j1 > j2: print 'ERROR: cross_sections.py => j1 > j2 !'; sys.exit(0)
  66. if i1 == i2:
  67. print 'Meridional section!'
  68. caxis = 'y' ; cxn = 'lat'
  69. vaxis = xlat[j1:j2,i1]
  70. imsk = rmsk[:,j1:j2,i1]
  71. ZT = XT[:,:,j1:j2,i1]
  72. ZS = XS[:,:,j1:j2,i1]
  73. if j1 == j2:
  74. print 'Zonal section!'
  75. caxis = 'x'; cxn = 'lon'
  76. vx = xlon[j1,i1:i2] ; vaxis = nmp.zeros(len(vx)) ; vaxis[:] = vx[:]
  77. ivf = nmp.where(vx>180); vaxis[ivf] = vx[ivf] - 360.
  78. imsk = rmsk[:,j1,i1:i2]
  79. ZT = XT[:,:,j1,i1:i2]
  80. ZS = XS[:,:,j1,i1:i2]
  81. cf_out = vdic['DIAG_D']+'/TS_section_'+csname+'.nc'
  82. bnc.wrt_appnd_2dt_series(vaxis, -vdepth, vtime, ZT, cf_out, cv_t,
  83. missing_val=-9999.,
  84. cxdnm=cxn, cydnm='depth', cxvnm=cxn, cyvnm='depth',
  85. cu_t='year', cu_d='deg.C', cln_d='Potential temperature',
  86. xd2=ZS, cvar2=cv_s, cln_d2='Salinity', cun2='PSU')