ssx_boxes.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/usr/bin/env python
  2. # B a r a K u d a
  3. #
  4. # Generate time-series plot of averaged variables on a rectangular box
  5. #
  6. # L. Brodeau, 2014
  7. import sys
  8. import numpy as nmp
  9. from netCDF4 import Dataset
  10. import barakuda_orca as bo
  11. import barakuda_tool as bt
  12. venv_needed = {'ORCA','EXP','DIAG_D','MM_FILE','NN_SST','NN_SSS','FILE_DEF_BOXES'}
  13. vdic = bt.check_env_var(sys.argv[0], venv_needed)
  14. CONFEXP = vdic['ORCA']+'-'+vdic['EXP']
  15. cnexec = sys.argv[0]
  16. na = len(sys.argv)
  17. if na != 3 and na != 5 :
  18. print 'Usage : '+cnexec+' <EXP_grid_T.nc> <year> (<name_sst> <name_sss>) '
  19. sys.exit(0)
  20. cf_in = sys.argv[1]
  21. cyear = sys.argv[2] ; jyear = int(cyear); cyear = '%4.4i'%jyear
  22. cv_sst = vdic['NN_SST']
  23. cv_sss = vdic['NN_SSS']
  24. if na == 5:
  25. cv_sst = sys.argv[3]
  26. cv_sss = sys.argv[4]
  27. print 'Current year is '+cyear+' !\n'
  28. bt.chck4f(vdic['MM_FILE'])
  29. id_mm = Dataset(vdic['MM_FILE'])
  30. rmsk = id_mm.variables['tmask'][0,0,:,:]
  31. rlon = id_mm.variables['glamt'][0,:,:]
  32. rlat = id_mm.variables['gphit'][0,:,:]
  33. Xe1t = id_mm.variables['e1t'][0,:,:]
  34. Xe2t = id_mm.variables['e2t'][0,:,:]
  35. id_mm.close()
  36. [ nj, ni ] = rmsk.shape
  37. bt.chck4f(cf_in)
  38. id_in = Dataset(cf_in)
  39. if cv_sst == 'thetao':
  40. XSST = id_in.variables[cv_sst][:,0,:,:]
  41. else:
  42. XSST = id_in.variables[cv_sst][:,:,:]
  43. if cv_sss == 'so':
  44. XSSS = id_in.variables[cv_sss][:,0,:,:]
  45. else:
  46. XSSS = id_in.variables[cv_sss][:,:,:]
  47. id_in.close()
  48. [ nt, nj0, ni0 ] = XSST.shape
  49. if [ nj0, ni0 ] != [ nj, ni ]: print 'ERROR: ssx_boxes.py => mask and field disagree in shape!'; sys.exit(0)
  50. print 'nt, nj, ni =', nt, nj, ni
  51. vtime = nmp.zeros(nt)
  52. for jt in range(nt): vtime[jt] = float(jyear) + (float(jt) + 0.5)/float(nt)
  53. # First will read name and coordinates of rectangular boxes to treat into file 'FILE_DEF_BOXE'
  54. ##############################################################################################
  55. vboxes, vi1, vj1, vi2, vj2 = bt.read_coor(vdic['FILE_DEF_BOXE'])
  56. nbb = len(vboxes)
  57. print ''
  58. rmean_sst = nmp.zeros((nt,nbb))
  59. rmean_sss = nmp.zeros((nt,nbb))
  60. for jb in range(nbb):
  61. cbox = vboxes[jb] ; print '\n *** Focus on '+cbox+' box'
  62. i1 = vi1[jb]
  63. j1 = vj1[jb]
  64. i2 = vi2[jb]+1
  65. j2 = vj2[jb]+1
  66. nx_b = i2 - i1
  67. ny_b = j2 - j1
  68. Xbox = nmp.zeros(ny_b*nx_b) ; Xbox.shape = [ ny_b, nx_b ]
  69. idx_msk = nmp.where( rmsk[j1:j2,i1:i2] < 0.5 )
  70. for jt in range(nt):
  71. rmean_sst[jt,jb] = nmp.sum(XSST[jt,j1:j2,i1:i2]*Xe1t[j1:j2,i1:i2]*Xe2t[j1:j2,i1:i2]*rmsk[j1:j2,i1:i2]) \
  72. / nmp.sum(Xe1t[j1:j2,i1:i2]*Xe2t[j1:j2,i1:i2]*rmsk[j1:j2,i1:i2])
  73. rmean_sss[jt,jb] = nmp.sum(XSSS[jt,j1:j2,i1:i2]*Xe1t[j1:j2,i1:i2]*Xe2t[j1:j2,i1:i2]*rmsk[j1:j2,i1:i2]) \
  74. / nmp.sum(Xe1t[j1:j2,i1:i2]*Xe2t[j1:j2,i1:i2]*rmsk[j1:j2,i1:i2])
  75. # Saving in netcdf file:
  76. cf_out = vdic['DIAG_D']+'/'+'SSX_'+cbox+'_'+CONFEXP+'.nc'
  77. l_nc_is_new = not os.path.exists(cf_out)
  78. # Creating/Opening output Netcdf file:
  79. if l_nc_is_new:
  80. f_out = Dataset(cf_out, 'w', format='NETCDF3_CLASSIC')
  81. else:
  82. f_out = Dataset(cf_out, 'a', format='NETCDF3_CLASSIC')
  83. if l_nc_is_new:
  84. jrec2write = 0
  85. # Creating Dimensions:
  86. f_out.createDimension('time', None)
  87. f_out.createDimension('y', ny_b)
  88. f_out.createDimension('x', nx_b)
  89. # Creating variables:
  90. id_t = f_out.createVariable('time','f4',('time',)) ; id_t.units = 'year'
  91. id_lat = f_out.createVariable('nav_lat','f4',('y','x',)) ; id_lat.units = 'deg.N'
  92. id_lon = f_out.createVariable('nav_lon','f4',('y','x',)) ; id_lon.units = 'deg.E'
  93. id_v01 = f_out.createVariable(cv_sst+'_sa' ,'f4',('time',))
  94. id_v02 = f_out.createVariable(cv_sss+'_sa' ,'f4',('time',))
  95. id_x01 = f_out.createVariable(cv_sst ,'f4',('time','y','x',), fill_value=-9999.)
  96. id_x02 = f_out.createVariable(cv_sss ,'f4',('time','y','x',), fill_value=-9999.)
  97. # Writing depth vector
  98. id_lat[:,:] = rlat[j1:j2,i1:i2]
  99. id_lon[:,:] = rlon[j1:j2,i1:i2]
  100. for jt in range(nt):
  101. id_t[jt] = vtime[jt]
  102. id_v01[jt] = rmean_sst[jt,jb]
  103. id_v02[jt] = rmean_sss[jt,jb]
  104. Xbox[:,:] = XSST[jt,j1:j2,i1:i2]
  105. Xbox[idx_msk] = -9999.
  106. id_x01[jt,:,:] = Xbox[:,:]
  107. Xbox[:,:] = XSSS[jt,j1:j2,i1:i2]
  108. Xbox[idx_msk] = -9999.
  109. id_x02[jt,:,:] = Xbox[:,:]
  110. f_out.Author = 'Generated with "ssx_boxes.py" of BaraKuda (https://github.com/brodeau/barakuda)'
  111. else:
  112. vt = f_out.variables['time']
  113. jrec2write = len(vt)
  114. v01 = f_out.variables[cv_sst+'_sa']
  115. x01 = f_out.variables[cv_sst]
  116. v02 = f_out.variables[cv_sss+'_sa']
  117. x02 = f_out.variables[cv_sss]
  118. for jt in range(nt):
  119. vt [jrec2write+jt] = vtime[jt]
  120. v01[jrec2write+jt] = rmean_sst[jt,jb]
  121. v02[jrec2write+jt] = rmean_sss[jt,jb]
  122. Xbox[:,:] = XSST[jt,j1:j2,i1:i2] ; Xbox[idx_msk] = -9999.
  123. x01[jrec2write+jt,:,:] = Xbox[:,:]
  124. Xbox[:,:] = XSSS[jt,j1:j2,i1:i2] ; Xbox[idx_msk] = -9999.
  125. x02[jrec2write+jt,:,:] = Xbox[:,:]
  126. f_out.close()
  127. print cf_out+' written!'