movie_nemo_section.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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, May 2018
  8. import sys
  9. import os
  10. from string import replace
  11. import numpy as nmp
  12. from netCDF4 import Dataset
  13. import matplotlib as mpl
  14. mpl.use('Agg')
  15. import matplotlib.pyplot as plt
  16. import matplotlib.colors as colors
  17. import warnings
  18. warnings.filterwarnings("ignore")
  19. from calendar import isleap
  20. import datetime
  21. import barakuda_colmap as bcm
  22. import barakuda_tool as bt
  23. vmn = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
  24. vml = [ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
  25. color_top = 'white'
  26. #color_top = 'k'
  27. #jt0 = 248
  28. jt0 = 0
  29. j2=0
  30. k2=0
  31. l_show_cb = True
  32. l_show_date = True
  33. l_log_field = False
  34. l_pow_field = False
  35. l_annotate_name = False
  36. narg = len(sys.argv)
  37. if narg < 6: print 'Usage: '+sys.argv[0]+' <NEMOCONF> <file> <variable> <LSM_file> <YYYYMMDD (start)>'; sys.exit(0)
  38. CNEMO = sys.argv[1]
  39. cf_in = sys.argv[2] ; cv_in=sys.argv[3]
  40. cf_lsm=sys.argv[4] ; cf_date0=sys.argv[5]
  41. if CNEMO == 'eNATL60':
  42. Nk0 = 300
  43. Nj0 = 4729-1
  44. l_show_cb = False
  45. l_show_date = True
  46. cdt = '1h'
  47. #cbox = 'FullMed' ; j1=5400 ; k1=1530 ; j2=Nj0 ; k2=3310 ; rfact_zoom = 0.79 ; vcb = [0.5, 0.875, 0.485, 0.02] ; font_rat = 2.*rfact_zoom ; l_annotate_name=False
  48. cbox = 'ALL' ; j1=0 ; k1=0 ; j2=Nj0 ; k2=Nk0 ; rfact_zoom = 0.3047 ; vcb = [0.59, 0.1, 0.38, 0.018] ; font_rat = 8.*rfact_zoom
  49. #cbox = 'Portrait' ; j1=2760 ; k1=1000 ; j2=4870 ; k2=4000 ; rfact_zoom = 1. ; vcb = [0.59, 0.1, 0.38, 0.018] ; font_rat = 1.*rfact_zoom ; l_annotate_name=False; l_show_date=False
  50. x_date = 1900 ; y_date = 20 ; # where to put the date
  51. if CNEMO == 'NATL60':
  52. Nk0 = 300
  53. Nj0 = 3454-1
  54. #l_pow_field = True ; pow_field = 1.5
  55. l_show_cb = False
  56. l_show_date = False
  57. cdt = '1h'
  58. #cbox = 'zoom1' ; j1 = 1800 ; k1 = 950 ; j2 = j1+1920 ; k2 = k1+1080 ; rfact_zoom = 1. ; vcb = [0.5, 0.875, 0.485, 0.02] ; font_rat = 8.*rfact_zoom ; l_show_lsm = False
  59. #cbox = 'zoom1' ; j1 = 1800 ; k1 = 950 ; j2 = j1+2560 ; k2 = k1+1440 ; rfact_zoom = 1. ; vcb = [0.5, 0.875, 0.485, 0.02] ; font_rat = 8.*rfact_zoom
  60. cbox = 'ALL' ; j1=0 ; k1=0 ; j2=Nj0 ; k2=Nk0 ; rfact_zoom = 1. ; vcb = [0.59, 0.1, 0.38, 0.018] ; font_rat = 4.*rfact_zoom
  61. x_date = 350 ; y_date = 7 ; # where to put the date
  62. if CNEMO == 'NANUK025':
  63. cdt = '3h'; cbox = 'ALL' ; j1 = 0 ; k1 = 0 ; j2 = 492 ; k2 = 614 ; rfact_zoom = 2. ; vcb = [0.5, 0.875, 0.485, 0.02] ; font_rat = 8.*rfact_zoom
  64. x_date = 350 ; y_date = 7 ; # where to put the date
  65. print '\n rfact_zoom = ', rfact_zoom
  66. print ' font_rat = ', font_rat, '\n'
  67. nx_res = j2-j1
  68. ny_res = k2-k1
  69. print ' *** nx_res, ny_res =', nx_res, ny_res
  70. print ' j1,j2,k1,k2 =>', j1,j2,k1,k2
  71. yx_ratio = float(ny_res)/float(nx_res)
  72. nxr = int(rfact_zoom*nx_res) ; # widt image (in pixels)
  73. nyr = int(rfact_zoom*ny_res) ; # height image (in pixels)
  74. dpi = 110
  75. rh = round(float(nxr)/float(dpi),3) ; # width of figure as for figure...
  76. fig_type='png'
  77. cyr0=cf_date0[0:4]
  78. cmn0=cf_date0[4:6]
  79. cdd0=cf_date0[6:8]
  80. l_3d_field = False
  81. if cv_in in ['sosstsst','tos']:
  82. cfield = 'SST'
  83. tmin=0. ; tmax=30. ; df = 2. ; cpal_fld = 'ncview_nrl'
  84. #tmin=0. ; tmax=32. ; df = 2. ; cpal_fld = 'viridis'
  85. #tmin=4. ; tmax=20. ; df = 1. ; cpal_fld = 'PuBu'
  86. cunit = r'SST ($^{\circ}$C)'
  87. cb_jump = 2
  88. l_show_cb = True
  89. if cv_in == 'sossheig':
  90. cfield = 'SSH'
  91. #tmin=-0.5 ; tmax=0.5 ; df = 0.05
  92. #tmin=-1.2 ; tmax=2.3 ; df = 0.05 ; l_apply_lap = True
  93. #cpal_fld = 'ncview_jaisnc'
  94. #cpal_fld = 'PuBu'
  95. #cpal_fld = 'RdBu'
  96. #cpal_fld = 'BrBG'
  97. #
  98. #cpal_fld = 'on3' ; tmin=-1.2 ; tmax=2.3 ; df = 0.05 ; l_apply_lap = True
  99. cpal_fld = 'on2' ; tmin=-1.2 ; tmax=1.2 ; df = 0.05
  100. #cpal_fld = 'coolwarm' ; tmin=-1. ; tmax=1. ; df = 0.05 ; l_apply_lap = True
  101. #cpal_fld = 'RdBu_r' ; tmin=-0.9 ; tmax=-tmin ; df = 0.05 ; l_apply_lap = True
  102. #cpal_fld = 'gray_r' ; tmin=-0.3 ; tmax=0.3 ; df = 0.05 ; l_apply_lap = True
  103. #cpal_fld = 'bone_r' ; tmin=-0.9 ; tmax=-tmin ; df = 0.05 ; l_apply_lap = True ; l_pow_field = True ; pow_field = 2.
  104. cunit = r'SSH (m)'
  105. cb_jump = 1
  106. if cv_in == 'socurloverf':
  107. cfield = 'RV'
  108. cpal_fld = 'on2' ; tmin=-1. ; tmax=1. ; df = 0.05
  109. cunit = ''
  110. cb_jump = 1
  111. if cv_in == 'vozocrtx':
  112. cfield = 'U'
  113. #cpal_fld = 'on2'
  114. cpal_fld = 'RdBu'
  115. tmin=-0.25 ; tmax=0.25 ; df = 0.05
  116. cunit = ''
  117. cb_jump = 1
  118. else:
  119. print 'ERROR: we do not know cv!'
  120. sys.exit(0)
  121. bt.chck4f(cf_lsm)
  122. bt.chck4f(cf_in)
  123. #id_fld = Dataset(cf_in)
  124. #vtime = id_fld.variables['time_counter'][:]
  125. #id_fld.close()
  126. #Nt = len(vtime)
  127. cv_lsm = 'tmask'
  128. if cv_in == 'vozocrtx': cv_lsm = 'umask'
  129. bt.chck4f(cf_lsm)
  130. print '\n '+cv_lsm+' !!!'
  131. id_lsm = Dataset(cf_lsm)
  132. nb_dim = len(id_lsm.variables[cv_lsm].dimensions)
  133. print ' The mesh_mask has '+str(nb_dim)+' dimmensions!'
  134. if nb_dim==4: XMSK = id_lsm.variables[cv_lsm][0,k1:k2,j1:j2,0]
  135. if nb_dim==3: XMSK = id_lsm.variables[cv_lsm][k1:k2,j1:j2,0]
  136. if nb_dim==2: XMSK = id_lsm.variables[cv_lsm][k1:k2,j1:j2]
  137. (nk,nj) = nmp.shape(XMSK)
  138. #XE1T2 = id_lsm.variables['e1t'][0,k1:k2,j1:j2]
  139. #XE2T2 = id_lsm.variables['e2t'][0,k1:k2,j1:j2]
  140. #(nk,nj) = nmp.shape(XE1T2)
  141. #XE1T2 = XE1T2*XE1T2
  142. #XE2T2 = XE2T2*XE2T2
  143. id_lsm.close()
  144. print 'Shape Arrays => nj,nk =', nj,nk
  145. id_fld = Dataset(cf_in)
  146. Nt = len(id_fld.variables[cv_in][:,0,0])
  147. id_fld.close()
  148. print ' *** Nt = ', Nt
  149. print 'Done!\n'
  150. pmsk = nmp.ma.masked_where(XMSK[:,:] > 0.2, XMSK[:,:]*0.+40.)
  151. #font_rat
  152. #params = { 'font.family':'Ubuntu',
  153. params = { 'font.family':'Helvetica Neue',
  154. 'font.weight': 'normal',
  155. 'font.size': int(9.*font_rat),
  156. 'legend.fontsize': int(9.*font_rat),
  157. 'xtick.labelsize': int(9.*font_rat),
  158. 'ytick.labelsize': int(9.*font_rat),
  159. 'axes.labelsize': int(9.*font_rat) }
  160. mpl.rcParams.update(params)
  161. cfont_clb = { 'fontname':'Helvetica Neue', 'fontweight':'medium', 'fontsize':int(8.*font_rat), 'color':'w'}
  162. cfont_date = { 'fontname':'Ubuntu Mono', 'fontweight':'normal', 'fontsize':int(12.*font_rat), 'color':'w' }
  163. cfont_mail = { 'fontname':'Times New Roman', 'fontweight':'normal', 'fontstyle':'italic', 'fontsize':int(14.*font_rat), 'color':'0.8'}
  164. cfont_titl = { 'fontname':'Helvetica Neue', 'fontweight':'light', 'fontsize':int(30.*font_rat), 'color':'w' }
  165. # Colormaps for fields:
  166. pal_fld = bcm.chose_colmap(cpal_fld)
  167. if l_log_field:
  168. norm_fld = colors.LogNorm( vmin = tmin, vmax = tmax, clip = False)
  169. if l_pow_field:
  170. norm_fld = colors.PowerNorm(gamma=pow_field, vmin = tmin, vmax = tmax, clip = False)
  171. else:
  172. norm_fld = colors.Normalize(vmin = tmin, vmax = tmax, clip = False)
  173. pal_lsm = bcm.chose_colmap('land_dark')
  174. norm_lsm = colors.Normalize(vmin = 0., vmax = 1., clip = False)
  175. if cdt == '3h':
  176. dt = 3
  177. elif cdt == '1h':
  178. dt = 1
  179. else:
  180. print 'ERROR: unknown dt!'
  181. print ' *** Dimension image:', rh*float(dpi), rh*yx_ratio*float(dpi),'\n'
  182. ntpd = 24/dt
  183. vm = vmn
  184. if isleap(int(cyr0)): vm = vml
  185. #print ' year is ', vm, nmp.sum(vm)
  186. jd = int(cdd0) - 1
  187. jm = int(cmn0)
  188. for jt in range(jt0,Nt):
  189. jh = (jt*dt)%24
  190. jdc = (jt*dt)/24 + 1
  191. if jt%ntpd == 0: jd = jd + 1
  192. if jd == vm[jm-1]+1 and (jt)%ntpd == 0 :
  193. jd = 1
  194. jm = jm + 1
  195. ch = '%2.2i'%(jh)
  196. #cdc= '%3.3i'%(jdc)
  197. cd = '%3.3i'%(jd)
  198. cm = '%2.2i'%(jm)
  199. #print '\n\n *** jt, ch, cd, cm =>', jt, ch, cd, cm
  200. ct = str(datetime.datetime.strptime(cyr0+'-'+cm+'-'+cd+' '+ch, '%Y-%m-%j %H'))
  201. ct=ct[:5]+cm+ct[7:] #lolo bug !!! need to do that to get the month and not "01"
  202. print ' ct = ', ct
  203. cday = ct[:10] ; print ' *** cday :', cday
  204. chour = ct[11:13] ; print ' *** chour :', chour
  205. cfig = 'figs/'+cv_in+'_NEMO_'+CNEMO+'_'+cbox+'_'+cday+'_'+chour+'_'+cpal_fld+'.'+fig_type
  206. fig = plt.figure(num = 1, figsize=(rh,rh*yx_ratio*3), dpi=None, facecolor='w', edgecolor='0.5')
  207. #ax = plt.axes([0.065, 0.05, 0.9, 1.], facecolor = '0.5')
  208. ax = plt.axes([0., 0., 1., 1.], facecolor = '0.5')
  209. vc_fld = nmp.arange(tmin, tmax + df, df)
  210. print "Reading record #"+str(jt)+" of "+cv_in+" in "+cf_in
  211. id_fld = Dataset(cf_in)
  212. XFLD = id_fld.variables[cv_in][jt,k1:k2,j1:j2,0]
  213. id_fld.close()
  214. print "Done!"
  215. #if not l_show_lsm and jt == jt0: ( nk , nj ) = nmp.shape(XFLD)
  216. print ' *** dimension of array => ', nj, nk
  217. print "Ploting"
  218. cf = plt.imshow(XFLD[:,:], cmap = pal_fld, norm = norm_fld, interpolation='none')
  219. del XFLD
  220. print "Done!"
  221. #cm = plt.imshow(nmp.flipud(pmsk), cmap = pal_lsm, norm = norm_lsm, interpolation='none')
  222. #plt.axis([ 0, nj, 0, nk])
  223. #plt.title('NEMO: '+cfield+', coupled '+CNEMO+', '+cday+' '+chour+':00', **cfont_title)
  224. if l_show_cb:
  225. color_top='w'
  226. ax2 = plt.axes(vcb)
  227. clb = mpl.colorbar.ColorbarBase(ax2, ticks=vc_fld, cmap=pal_fld, norm=norm_fld, orientation='horizontal', extend='both')
  228. if cb_jump > 1:
  229. cb_labs = [] ; cpt = 0
  230. for rr in vc_fld:
  231. if cpt % cb_jump == 0:
  232. if df >= 1.: cb_labs.append(str(int(rr)))
  233. if df < 1.: cb_labs.append(str(rr))
  234. else:
  235. cb_labs.append(' ')
  236. cpt = cpt + 1
  237. clb.ax.set_xticklabels(cb_labs, **cfont_clb)
  238. clb.set_label(cunit, **cfont_clb)
  239. clb.ax.yaxis.set_tick_params(color=color_top) ; # set colorbar tick color
  240. clb.outline.set_edgecolor(color_top) ; # set colorbar edgecolor
  241. plt.setp(plt.getp(clb.ax.axes, 'xticklabels'), color=color_top) ; # set colorbar ticklabels
  242. del cf
  243. if l_show_date:
  244. xl = float(x_date)/rfact_zoom
  245. yl = float(y_date)/rfact_zoom
  246. ax.annotate('Date: '+cday+' '+chour+':00', xy=(1, 4), xytext=(xl,yl), **cfont_date)
  247. #ax.annotate('laurent.brodeau@ocean-next.fr', xy=(1, 4), xytext=(xl+150, 20), **cfont_mail)
  248. if l_annotate_name:
  249. xl = float(nxr)/20./rfact_zoom
  250. yl = float(nyr)/1.33/rfact_zoom
  251. ax.annotate(CNEMO, xy=(1, 4), xytext=(xl, yl), **cfont_titl)
  252. plt.savefig(cfig, dpi=dpi, orientation='portrait', facecolor='k')
  253. print cfig+' created!\n'
  254. plt.close(1)
  255. del cm, fig, ax
  256. if l_show_cb: del clb