flux_int_basins.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/usr/bin/env python
  2. #
  3. # B a r a K u d a
  4. #
  5. # Spatially integrates surface fluxes (on NEMO grid) on all the 2D regions
  6. # defined in the basin_mask file
  7. #
  8. # L. Brodeau, March 2017
  9. #
  10. import sys
  11. import os
  12. import numpy as nmp
  13. from netCDF4 import Dataset
  14. from string import replace
  15. import barakuda_tool as bt
  16. import barakuda_orca as bo
  17. import barakuda_ncio as bnc
  18. venv_needed = {'ORCA','EXP','DIAG_D','MM_FILE','BM_FILE','FILE_FLX_SUFFIX','TSTAMP',
  19. 'NEMO_SAVED_FILES','NN_RNF','NN_FWF','NN_P','NN_CLV','NN_E'}
  20. vdic = bt.check_env_var(sys.argv[0], venv_needed)
  21. cdir_out = vdic['DIAG_D']+'/flux_int_basins'
  22. os.system('mkdir -p '+cdir_out)
  23. cFgrid = vdic['FILE_FLX_SUFFIX'] ; # suffix of files containing surface fluxes!
  24. CONFEXP = vdic['ORCA']+'-'+vdic['EXP']
  25. if len(sys.argv) != 3:
  26. print 'Usage : sys.argv[1] <ORCA1_EXP_'+cFgrid+'.nc> <year>'; sys.exit(0)
  27. cnexec = sys.argv[0]
  28. cf_flx_in = sys.argv[1]
  29. cyear = sys.argv[2] ; jyear = int(cyear); cyear = '%4.4i'%jyear
  30. print 'Current year is '+cyear+' !\n'
  31. # Checking if the land-sea mask file is here:
  32. for cf in [vdic['MM_FILE'], vdic['BM_FILE']]:
  33. if not os.path.exists(cf):
  34. print 'Mask file '+cf+' not found'; sys.exit(0)
  35. # Reading the grid metrics:
  36. id_mm = Dataset(vdic['MM_FILE'])
  37. list_variables = id_mm.variables.keys()
  38. mask_glo = id_mm.variables['tmask'][0,0,:,:]
  39. xe1t = id_mm.variables['e1t'][0,:,:]
  40. xe2t = id_mm.variables['e2t'][0,:,:]
  41. id_mm.close()
  42. (nj, ni) = mask_glo.shape
  43. # Grid cell area:
  44. Xa = nmp.zeros((nj, ni))
  45. Xa[:,:] = 1.E-6*xe1t[:,:]*xe2t[:,:]*mask_glo[:,:]
  46. del xe1t, xe2t
  47. print ' *** Global ocean surface =', 1.E-6*nmp.sum(Xa[:-2,:-2]), '10^6 km^2\n'
  48. print 'Opening different basin masks in file '+vdic['BM_FILE']
  49. list_basin_names, list_basin_lgnms = bo.get_basin_info(vdic['BM_FILE'])
  50. nb_basins = len(list_basin_names) ; # Global is #0, it is included!
  51. mask = nmp.zeros((nb_basins,nj,ni))
  52. msk_tmp = nmp.zeros((nj,ni))
  53. # Geting basin masks and their zooming indices:
  54. i1 = nmp.zeros(nb_basins, dtype=nmp.int)
  55. i2 = nmp.zeros(nb_basins, dtype=nmp.int)
  56. j1 = nmp.zeros(nb_basins, dtype=nmp.int)
  57. j2 = nmp.zeros(nb_basins, dtype=nmp.int)
  58. mask[0,:,:] = mask_glo[:,:] ; # global
  59. id_bm = Dataset(vdic['BM_FILE'])
  60. for jb in range(nb_basins) :
  61. if jb > 0:
  62. msk_tmp[:,:] = id_bm.variables['tmask'+list_basin_names[jb]][:,:]
  63. mask[jb,:,:] = msk_tmp[:,:]*mask_glo[:,:]
  64. # Decrasing the domain size if possible:
  65. (i1[jb],j1[jb],i2[jb],j2[jb]) = bo.shrink_domain(mask[jb,:,:])
  66. #(vjj , vji) = nmp.where(mask[jb,:,:]>0.5)
  67. #j1[jb] = max( nmp.min(vjj)-2 , 0 )
  68. #i1[jb] = max( nmp.min(vji)-2 , 0 )
  69. #j2[jb] = min( nmp.max(vjj)+2 , nj-1 ) + 1
  70. #i2[jb] = min( nmp.max(vji)+2 , ni-1 ) + 1
  71. #if (i1[jb],i2[jb]) == (0,ni): i2[jb] = i2[jb]-2 ; # Mind east-west periodicity overlap of 2 points...
  72. id_bm.close()
  73. del mask_glo, msk_tmp
  74. # Getting list of variables available in input NEMO file:
  75. id_in = Dataset(cf_flx_in)
  76. list_variables = id_in.variables.keys()
  77. id_in.close()
  78. # Fresh water fluxes (expected in kg/m2/s == mm/s!):
  79. rmult = 1.E-3 ; # to Sverdrup!
  80. cunit='Sv'
  81. vvar = [ vdic['NN_FWF'], vdic['NN_E'], vdic['NN_P'], vdic['NN_RNF'], vdic['NN_CLV'] ]
  82. vnnm = [ 'EmPmR' , 'E' , 'P' , 'R' , 'ICalv' ]
  83. nbv = len(vvar)
  84. # Array contening results for freshwater fluxes:
  85. v_fwf_m = nmp.zeros((12,nb_basins,nbv))
  86. jv = 0
  87. for cvar in vvar:
  88. if cvar == 'X' or (not cvar in list_variables):
  89. print '\n WARNING ('+cnexec+'): skipping '+vnnm[jv]+' = "'+cvar+'"!'
  90. if cvar != 'X': print ' => because not found in '+cf_flx_in+' !\n'
  91. else:
  92. print '\n +++ '+cnexec+' => treating '+vnnm[jv]+' ('+cvar+') !'
  93. print ' ==> variable '+cvar
  94. id_in = Dataset(cf_flx_in)
  95. Xd_m = id_in.variables[cvar][:,:,:]
  96. cu = id_in.variables[cvar].units
  97. id_in.close()
  98. (Nt,nj0,ni0) = nmp.shape(Xd_m)
  99. print ' ==> variable '+cvar+' read ! ('+cu+') =>', (Nt,nj0,ni0)
  100. if Nt != 12: print 'ERROR: '+cnexec+' => only treating monthly data so far...'; sys.exit(0)
  101. if (nj0,ni0) != (nj,ni): print 'ERROR: '+cnexec+' => Field and metrics do not agree in size!'; sys.exit(0)
  102. Xd_m_x_A = nmp.zeros((Nt,nj,ni))
  103. for jt in range(Nt):
  104. Xd_m_x_A[jt,:,:] = Xd_m[jt,:,:]*Xa[:,:]
  105. jb = 0
  106. for cbasin in list_basin_names[:]:
  107. colnm = list_basin_lgnms[jb]
  108. print ' ===> '+cvar+' in basin '+cbasin+' ('+colnm+')'
  109. if (i1[jb],j1[jb],i2[jb],j2[jb]) != (0,0,ni,nj): print ' ===> zooming on i1,j1 -> i2,j2:', i1[jb],j1[jb],'->',i2[jb],j2[jb]
  110. for jt in range(Nt):
  111. v_fwf_m[jt,jb,jv] = nmp.sum( rmult*Xd_m_x_A[jt,j1[jb]:j2[jb],i1[jb]:i2[jb]] * mask[jb,j1[jb]:j2[jb],i1[jb]:i2[jb]] )
  112. jb = jb + 1 ; # next basin!
  113. print '\n +++ '+cnexec+' => Done with 2D-summing of variable '+cvar+'!\n'
  114. del Xd_m
  115. jv = jv + 1 ; # next variable!
  116. # Time to write netcdf file:
  117. vtime = nmp.zeros(12)
  118. for jt in range(12): vtime[jt] = float(jyear) + (float(jt)+0.5)*1./float(12)
  119. #print ' * Calendar: ', vtime[:]
  120. c1 = '2D-integral of '
  121. jb = 0
  122. for cbasin in list_basin_names[:]:
  123. c2 = ') on region '+list_basin_lgnms[jb]
  124. cf_out = cdir_out+'/fwf_int_'+CONFEXP+'_'+cbasin+'.nc'
  125. bnc.wrt_appnd_1d_series(vtime, v_fwf_m[:,jb,0], cf_out, vnnm[0],
  126. cu_t='year', cu_d=cunit, cln_d =c1+vnnm[0]+' ('+vvar[0]+c2,
  127. vd2=v_fwf_m[:,jb,1], cvar2=vnnm[1], cln_d2=c1+vnnm[1]+' ('+vvar[1]+c2,
  128. vd3=v_fwf_m[:,jb,2], cvar3=vnnm[2], cln_d3=c1+vnnm[2]+' ('+vvar[2]+c2,
  129. vd4=v_fwf_m[:,jb,3], cvar4=vnnm[3], cln_d4=c1+vnnm[3]+' ('+vvar[3]+c2,
  130. vd5=v_fwf_m[:,jb,4], cvar5=vnnm[4], cln_d5=c1+vnnm[4]+' ('+vvar[4]+c2)
  131. jb = jb + 1
  132. print '\n *** EXITING '+cnexec+' for year '+cyear+' and variable '+cvar+'!\n'