prepare_movies.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #!/usr/bin/env python
  2. # B a r a K u d a
  3. #
  4. # Prepare 2D maps (monthly) that will later become a GIF animation!
  5. # NEMO output and observations needed
  6. #
  7. # L. Brodeau, november 2016
  8. import sys
  9. import os
  10. import numpy as nmp
  11. from string import replace
  12. from netCDF4 import Dataset
  13. import barakuda_tool as bt
  14. import barakuda_plot as bp
  15. venv_needed = {'ORCA','EXP','DIAG_D','MM_FILE','NN_SST','NN_T','NN_SSS','NN_S','NN_MLD',
  16. 'FILE_ICE_SUFFIX','NN_ICEF',
  17. 'NM_TS_OBS','F_T_OBS_3D_12','F_S_OBS_3D_12','F_SST_OBS_12','NN_SST_OBS','NN_T_OBS','NN_S_OBS'}
  18. # 'NM_IC_OBS'
  19. vdic = bt.check_env_var(sys.argv[0], venv_needed)
  20. CONFEXP = vdic['ORCA']+'-'+vdic['EXP']
  21. tmin=-4. ; tmax=-tmin ; dtemp = 0.25
  22. smin=-1.4 ; smax=-smin ; dsali = 0.1
  23. mmin=0. ; mmax=1500. ; dmld = 50.
  24. fig_type='png'
  25. cn_obs_ts = vdic['NM_TS_OBS']
  26. #cn_obs_ic = vdic['NM_TS_OBS']
  27. narg = len(sys.argv)
  28. if narg < 4:
  29. print 'Usage: '+sys.argv[0]+' <NEMO file (1 year, monthyly)> <year> <var>'
  30. print ' with var one of "sst", "sss", "ice", "mld"'
  31. sys.exit(0)
  32. cf_T = sys.argv[1]
  33. cy = sys.argv[2] ; jy=int(cy)
  34. cvar = sys.argv[3]
  35. cf_ice = replace(cf_T, 'grid_T', vdic['FILE_ICE_SUFFIX'])
  36. print ' *** file to read '+vdic['NN_ICEF']+' from: '+cf_ice+'\n'
  37. if not cvar in ['sst','sss','ice','mld']:
  38. print 'ERROR (prepare_movies.py): variable '+cvar+' not supported yet!'
  39. sys.exit(0)
  40. path_fig = 'movies'
  41. os.system("mkdir -p "+path_fig)
  42. # 3D climatology :
  43. # ------------
  44. # Salinity
  45. if cvar == 'sss':
  46. bt.chck4f(vdic['F_S_OBS_3D_12'])
  47. id_clim = Dataset(vdic['F_S_OBS_3D_12'])
  48. Vclim = id_clim.variables[vdic['NN_S_OBS']][:,0,:,:]; print '(has ',Vclim.shape[0],' time snapshots)\n'
  49. id_clim.close()
  50. # 2D SST obs :
  51. if cvar == 'sst':
  52. cv_sst_obs = vdic['NN_SST_OBS']
  53. bt.chck4f(vdic['F_SST_OBS_12'])
  54. id_clim_sst = Dataset(vdic['F_SST_OBS_12'])
  55. nb_dim = len(id_clim_sst.variables[cv_sst_obs].dimensions)
  56. if nb_dim == 3:
  57. Vclim = id_clim_sst.variables[cv_sst_obs][:,:,:]; print '(has ',Vclim.shape[0],' time snapshots)\n'
  58. elif nb_dim == 4:
  59. Vclim = id_clim_sst.variables[cv_sst_obs][:,0,:,:]; print '(has ',Vclim.shape[0],' time snapshots)\n'
  60. else:
  61. print 'ERROR (prepare_movies.py): shape of '+cv_sst_obs+' in '+vdic['F_SST_OBS_12']+' is problematic!'
  62. sys.exit(0)
  63. id_clim_sst.close()
  64. # Sea-ice concentration :
  65. # => no clim used!
  66. if cvar in ['sss','sst']: ( nmn , nj0 , ni0 ) = Vclim.shape
  67. # Getting land-sea mask and coordinates:
  68. bt.chck4f(vdic['MM_FILE'])
  69. id_mask = Dataset(vdic['MM_FILE'])
  70. xlon = id_mask.variables['nav_lon'][:,:]
  71. xlat = id_mask.variables['nav_lat'][:,:]
  72. imask = id_mask.variables['tmask'][0,0,:,:]
  73. id_mask.close()
  74. # Getting NEMO variables:
  75. # -----------------------
  76. cf_in = cf_T
  77. if cvar == 'ice' and cf_ice != cf_T:
  78. cf_in = cf_ice
  79. bt.chck4f(cf_in)
  80. id_in = Dataset(cf_in)
  81. #list_var = id_in.variables.keys()
  82. if cvar == 'sst':
  83. if vdic['NN_SST'] == 'thetao' or vdic['NN_SST'] == 'votemper' : #lolo:bad !!! should check shape!!!
  84. Vnemo = id_in.variables[vdic['NN_SST']][:,0,:,:]
  85. else:
  86. Vnemo = id_in.variables[vdic['NN_SST']][:,:,:]
  87. cv = 'dsst'
  88. if cvar == 'sss':
  89. if vdic['NN_SSS'] == 'so' or vdic['NN_SSS'] == 'vosaline' : #lolo:bad !!! should check shape!!!
  90. Vnemo = id_in.variables[vdic['NN_SSS']][:,0,:,:]
  91. else:
  92. Vnemo = id_in.variables[vdic['NN_SSS']][:,:,:]
  93. cv = 'dsss'
  94. if cvar == 'ice':
  95. if vdic['NN_ICEF'] == 'X':
  96. print 'ERROR (prepare_movies.py): you set "X" (missing) as the name for ice concentration in your conf file!'; sys.exit(0)
  97. Vnemo = id_in.variables[vdic['NN_ICEF']][:,:,:]
  98. Vnemo = nmp.multiply(Vnemo,100)
  99. if cvar == 'mld':
  100. if vdic['NN_MLD'] == 'X':
  101. print 'ERROR (prepare_movies.py): you set "X" (missing) as the name for MLD in your conf file!'; sys.exit(0)
  102. Vnemo = id_in.variables[vdic['NN_MLD']][:,:,:]
  103. id_in.close()
  104. [ nt, nj, ni ] = Vnemo.shape
  105. if nt != 12:
  106. print 'ERROR (prepare_movies.py): we expect 12 montly records in NEMO grid_T file!'
  107. sys.exit(0)
  108. if (cvar in ['sss','sst']) and (nj != nj0 or ni != ni0):
  109. print 'ERROR (prepare_movies.py): NEMO file and clim do no agree in shape for '+cvar+'!'
  110. print ' clim => '+str(ni0)+', '+str(nj0)+', ('+vdic['F_T_OBS_3D_12']+')'
  111. print ' NEMO => '+str(ni)+', '+str(nj)
  112. sys.exit(0)
  113. if cvar in ['sss','sst','mld']:
  114. # Creating 1D long. and lat.:
  115. ji_lat0 = nmp.argmax(xlat[nj-1,:])
  116. vlon = nmp.zeros(ni) ; vlon[:] = xlon[20,:]
  117. vlat = nmp.zeros(nj) ; vlat[:] = xlat[:,ji_lat0]
  118. if cvar == 'ice':
  119. # Extraoplating sea values over continents:
  120. bt.drown(Vnemo[:,:,:], imask, k_ew=2, nb_max_inc=10, nb_smooth=10)
  121. lpix = False
  122. if vdic['ORCA'][:5] == 'ORCA0': lpix = True
  123. for jt in range(nt):
  124. cm = "%02d" % (jt+1)
  125. cdate = cy+cm
  126. cdatet = cy+'/'+cm
  127. if cvar == 'sst':
  128. bp.plot("2d")(vlon, vlat, Vnemo[jt,:,:] - Vclim[jt,:,:],
  129. imask[:,:], tmin, tmax, dtemp,
  130. corca=vdic['ORCA'], lkcont=False, cpal='RdBu_r',
  131. cfignm=path_fig+'/'+cv+'_'+cdate,
  132. cbunit='K', cfig_type=fig_type, lat_min=-77., lat_max=75.,
  133. ctitle='SST (NEMO - "'+cn_obs_ts+'"), '+CONFEXP+' ('+cdatet+')',
  134. lforce_lim=True, i_cb_subsamp=2, lpix=lpix)
  135. if cvar == 'sss':
  136. bp.plot("2d")(vlon, vlat, Vnemo[jt,:,:] - Vclim[jt,:,:],
  137. imask[:,:], smin, smax, dsali,
  138. corca=vdic['ORCA'], lkcont=False, cpal='PiYG_r',
  139. cfignm=path_fig+'/'+cv+'_'+cdate,
  140. cbunit='PSU', cfig_type=fig_type, lat_min=-77., lat_max=75.,
  141. ctitle='SSS (NEMO - "'+cn_obs_ts+'"), '+CONFEXP+' ('+cdatet+')',
  142. lforce_lim=True, i_cb_subsamp=2, lpix=lpix)
  143. if cvar == 'mld':
  144. bp.plot("2d")(vlon, vlat, Vnemo[jt,:,:], imask[:,:], mmin, mmax, dmld,
  145. corca=vdic['ORCA'], lkcont=False, cpal='ncview_nrl',
  146. cfignm=path_fig+'/'+cvar+'_'+cdate,
  147. cbunit='m', cfig_type=fig_type, lat_min=-77., lat_max=75.,
  148. ctitle='Mixed-Layer depth, '+CONFEXP+' ('+cdatet+')',
  149. lforce_lim=True, i_cb_subsamp=2, lpix=lpix)
  150. if cvar == 'ice':
  151. # Extraoplating sea values on continents:
  152. bt.drown(Vnemo[jt,:,:], imask, k_ew=2, nb_max_inc=10, nb_smooth=10)
  153. # ICE north:
  154. cv = "icen"
  155. bp.plot("nproj")('npol2', 0., 100., 10., xlon, xlat, Vnemo[jt,:,:],
  156. cfignm=path_fig+'/'+cv+'_'+cdate, cpal='ice', cbunit='(%)',
  157. ctitle='Ice concentration, '+CONFEXP+' ('+cdatet+')',
  158. lkcont=True, cfig_type=fig_type, lforce_lim=True)
  159. cv = "ices"
  160. bp.plot("nproj")('spstere', 0., 100., 10., xlon, xlat, Vnemo[jt,:,:],
  161. cfignm=path_fig+'/'+cv+'_'+cdate, cpal='ice', cbunit='(%)',
  162. ctitle='Ice concentration, '+CONFEXP+' ('+cdatet+')',
  163. lkcont=True, cfig_type=fig_type, lforce_lim=True)
  164. print '\n *** EXITING prepare_movies.py for year '+cy+', var ='+cvar+' !\n'