test_transects.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env python
  2. # B a r a K u d a
  3. #
  4. # L. Brodeau, 2017]
  5. import sys
  6. import numpy as nmp
  7. import string
  8. import os
  9. from netCDF4 import Dataset
  10. import barakuda_tool as bt
  11. import barakuda_orca as bo
  12. import barakuda_ncio as bn
  13. #cf_tf = '/home/laurent/DEV/barakuda/data/TS_sections.dat'
  14. narg = len(sys.argv)
  15. if narg != 3:
  16. print 'Usage: '+sys.argv[0]+' <TS_transect_file> <mesh_mask>'; sys.exit(0)
  17. cf_tf = sys.argv[1]
  18. cf_mm = sys.argv[2]
  19. # Opening mesh_mask:
  20. f_mm = Dataset(cf_mm)
  21. Xlon = f_mm.variables['glamt'][0,:,:]
  22. Xlat = f_mm.variables['gphit'][0,:,:]
  23. mask = f_mm.variables['tmask'][0,0,:,:]
  24. f_mm.close()
  25. (nj,ni) = nmp.shape(Xlon)
  26. nmask = nmp.zeros((nj,ni))
  27. # Getting sections:
  28. vboxes, vlon1, vlat1, vlon2, vlat2 = bt.read_coor(cf_tf, ctype='float', lTS_bounds=False)
  29. js = -1
  30. for csname in vboxes:
  31. js = js + 1
  32. print'\n *** '+sys.argv[0]+': treating section '+csname
  33. nmask[:,:] = mask[:,:]
  34. ( i1, i2, j1, j2 ) = bo.transect_zon_or_med(vlon1[js], vlon2[js], vlat1[js], vlat2[js], Xlon, Xlat)
  35. print csname+' :'
  36. print '(lon1, lon2, lat1, lat2) =', vlon1[js], vlon2[js], vlat1[js], vlat2[js]
  37. print ' => i1, i2, j1, j2 =', i1, i2, j1, j2
  38. # Just for info:
  39. lat1_o = Xlat[j1,i1]
  40. lat2_o = Xlat[j2,i2]
  41. lon1_o = Xlon[j1,i1]
  42. lon2_o = Xlon[j2,i2]
  43. # Mid point:
  44. im = (i1+i2)/2
  45. jm = (j1+j2)/2
  46. lat_m = Xlat[jm,im]
  47. lon_m = Xlon[jm,im]
  48. if lon1_o > 180.: lon1_o = lon1_o - 360.
  49. if lon2_o > 180.: lon2_o = lon2_o - 360.
  50. if lon_m > 180.: lon_m = lon_m - 360.
  51. print ' => ORCA p1 , p2 =>' , lon1_o, lon2_o, lat1_o, lat2_o
  52. print ' => ORCA mid point =>' , lon_m, lat_m
  53. print ''
  54. if i1 > i2: print 'ERROR: cross_sections.py => i1 > i2 !'; sys.exit(0)
  55. if j1 > j2: print 'ERROR: cross_sections.py => j1 > j2 !'; sys.exit(0)
  56. ip = 0
  57. if i1 == i2:
  58. print 'Meridional section!'
  59. ip = 1
  60. jp=0
  61. if j1 == j2:
  62. print 'Zonal section!'
  63. jp=1
  64. nmask[j1:j2+jp,i1:i2+ip] = -1
  65. #bn.write_2d_mask('mask_'+csname+'.nc', nmask, xlon=Xlon, xlat=Xlat)
  66. bn.write_2d_mask('mask_'+csname+'.nc', nmask)