movie_ifs_globe_ts.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #!/usr/bin/env python
  2. # B a r a K u d a
  3. #
  4. #
  5. # L. Brodeau, 2017
  6. import sys
  7. import os
  8. from string import replace
  9. import numpy as nmp
  10. from netCDF4 import Dataset
  11. from mpl_toolkits.basemap import Basemap
  12. #from mpl_toolkits.basemap import shiftgrid
  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. import datetime
  20. import barakuda_colmap as bcm
  21. import barakuda_tool as bt
  22. nspd = 4 ; # number of snapshots per day, 6 hourly => nspd = 4
  23. long_start = 0 ; # longitude to start the movie from...
  24. latitude = 15. ; # fixed latitude to view from
  25. lforce_mask = True
  26. year_ref_ini = 1990
  27. jt0 = 0
  28. jtN = 0
  29. #CTATM = 'T255'
  30. CTATM = 'T1279'
  31. cbox = 'Pac'
  32. fig_type='png'
  33. narg = len(sys.argv)
  34. if narg < 6:
  35. print 'Usage: '+sys.argv[0]+' <file> <variable> <LSM_file> <first record> <last record>'
  36. print ' => records in C convention! first = 0 !'
  37. sys.exit(0)
  38. cf_in = sys.argv[1] ; cv_in=sys.argv[2] ; cf_lsm=sys.argv[3]
  39. jt0 = int(sys.argv[4])
  40. jtN = int(sys.argv[5])
  41. #if ((jt0+1) % nspd) != 0: print 'ERROR: please give a jt0 which is a multiple of '+str(nspd)+' (C-conv!) !!!'; sys.exit(0)
  42. if (jt0 % nspd) != 0: print 'ERROR: please give a jt0 which is a multiple of '+str(nspd)+' (C-conv!) !!!'; sys.exit(0)
  43. roffset = 0.
  44. if cv_in == 'T2M':
  45. cfield = 'T2M'
  46. tmin=-15. ; tmax=40. ; dtemp = 5.
  47. roffset = -273.15
  48. cpal_fld = 'ncview_nrl'
  49. cunit = r'$[^{\circ}C]$'
  50. cb_jump = 1
  51. if cv_in == 'CURL':
  52. cfield = 'ROT(U10M)'
  53. tmin=-0.1 ; tmax=0.1 ; dtemp = 0.025
  54. cpal_fld = 'ncview_blue_red'
  55. cunit = r'$[s^{-1}]$'
  56. cb_jump = 1
  57. if cv_in == 'wspd10m':
  58. cfield = 'Wind@10m'
  59. tmin=0. ; tmax=28. ; dtemp = 2.
  60. #cpal_fld = 'gist_stern'
  61. #cpal_fld = 'gnuplot'
  62. cpal_fld = 'CMRmap'
  63. cunit = r'$[s^{-1}]$'
  64. cb_jump = 1
  65. if cv_in == 'sosstsst':
  66. cfield = 'SST'
  67. tmin=-2. ; tmax=28. ; dtemp = 1.
  68. cpal_fld = 'ncview_nrl'
  69. cunit = r'$^{\circ}C$'
  70. cb_jump = 2
  71. elif cv_in == 'somxl010':
  72. cfield == 'MLD'
  73. tmin=50. ; tmax=1500. ; dtemp = 50.
  74. cpal_fld = 'viridis_r'
  75. #bt.chck4f(cf_ice)
  76. bt.chck4f(cf_in)
  77. id_fld = Dataset(cf_in)
  78. vtime = id_fld.variables['time'][:]
  79. id_fld.close()
  80. Nt = len(vtime)
  81. if jtN > Nt: print 'ERROR: your jtN > Nt !!!'; sys.exit(0)
  82. if jtN == 0: jtN = Nt
  83. bt.chck4f(cf_lsm)
  84. id_lsm = Dataset(cf_lsm)
  85. #if lforce_mask:
  86. Xlsm = id_lsm.variables['LSM'][0,:,:]
  87. vlon = id_lsm.variables['lon'][:]
  88. vlat = id_lsm.variables['lat'][:]
  89. id_lsm.close()
  90. (nj,ni) = nmp.shape(Xlsm)
  91. # 2D coor...
  92. XLON = nmp.zeros((nj,ni+1))
  93. for jj in range(nj): XLON[jj,:ni] = vlon[:]
  94. XLON[:,ni] = XLON[:,0]
  95. XLAT = nmp.zeros((nj,ni+1))
  96. for ji in range(ni): XLAT[:,ji] = vlat[:]
  97. XLAT[:,ni] = XLAT[:,0]
  98. XMSK = nmp.zeros((nj,ni+1))
  99. XMSK[:,:ni] = Xlsm[:,:]
  100. XMSK[:,ni] = XMSK[:,0]
  101. #pmsk = nmp.ma.masked_where(XMSK[:,:] > 0.2, XMSK[:,:]*0.+40.)
  102. #idx_oce = nmp.where(XMSK[:,:] > 0.5)
  103. XFLD = nmp.zeros((nj,ni+1))
  104. params = { 'font.family':'Ubuntu',
  105. 'font.size': int(12),
  106. 'legend.fontsize': int(12),
  107. 'xtick.labelsize': int(12),
  108. 'ytick.labelsize': int(12),
  109. 'axes.labelsize': int(12) }
  110. mpl.rcParams.update(params)
  111. cfont_clb = { 'fontname':'Arial', 'fontweight':'normal', 'fontsize':13, 'color':'white' }
  112. cfont_title = { 'fontname':'Ubuntu Mono', 'fontweight':'normal', 'fontsize':18, 'color':'white' }
  113. cfont_mail = { 'fontname':'Times New Roman', 'fontweight':'normal', 'fontstyle':'italic', 'fontsize':10, 'color':'0.5'}
  114. # Colormaps for fields:
  115. pal_fld = bcm.chose_colmap(cpal_fld)
  116. norm_fld = colors.Normalize(vmin = tmin, vmax = tmax, clip = False)
  117. #pal_ice = bcm.chose_colmap(cpal_ice)
  118. #norm_ice = colors.Normalize(vmin = rmin_ice, vmax = 1, clip = False)
  119. pal_lsm = bcm.chose_colmap('blk')
  120. norm_lsm = colors.Normalize(vmin = 0., vmax = 1., clip = False)
  121. vc_fld = nmp.arange(tmin, tmax + dtemp, dtemp)
  122. print ''
  123. rh = 7.5
  124. jrot = -1 + jt0
  125. print '\n jt0, jtN =>', jt0, jtN, '\n'
  126. jday = jt0/nspd
  127. jsday = -1
  128. for jt in range(jt0,jtN):
  129. jsday = jsday + 1
  130. if jsday == nspd: jsday = 0
  131. cjt = '%4.4i'%(jt) ; # record! not day!
  132. if jt % nspd == 0:
  133. jday = jday + 1
  134. cday = '%3.3i'%(jday)
  135. cd = str(datetime.datetime.strptime('1990 '+cday, '%Y %j'))
  136. cdate = cd[:10]
  137. print '\n *** Date :', cdate
  138. print " *** Reading record #"+str(cjt)+" of "+cv_in+" in "+cf_in
  139. id_fld = Dataset(cf_in)
  140. XFLD[:,:ni] = id_fld.variables[cv_in][jt,:,:]
  141. id_fld.close()
  142. XFLD[:,ni] = XFLD[:,0]
  143. print " => done!"
  144. jrot = jrot+1
  145. rot = (long_start + (0.5/float(nspd)*float(jrot)))%360.
  146. rot = -rot
  147. cfig = 'figs/'+cv_in+'_IFS'+'_d'+cday+'_'+str(jsday)+'.'+fig_type
  148. print ' *** reference longitude =', rot
  149. fig = plt.figure(num = 1, figsize=(rh,1.167*rh), dpi=None, facecolor='b', edgecolor='k')
  150. ax = plt.axes([0.005, 0.05, 0.99, 0.99], axisbg = 'k')
  151. plt.title('Atmosphere (IFS@'+CTATM+' cpl ORCA12): '+cfield+', '+cdate, **cfont_title)
  152. print ' *** Creating new projection'
  153. carte = Basemap(projection='ortho', lat_0=latitude, lon_0=rot, resolution='h')
  154. x0,y0 = carte(XLON,XLAT)
  155. #if lforce_mask: XFLD = nmp.ma.masked_where(XMSK[:,:] < 0.5, XFLD[:,:])
  156. print ' *** Ploting on map...'
  157. cf = carte.pcolor(x0, y0, XFLD+roffset, cmap=pal_fld, norm=norm_fld)
  158. cc = carte.contour(x0, y0, XMSK, [ 0.5 ], colors='k', linewidths=1.)
  159. #plt.annotate('L. Brodeau, brodeau@gmail.com', xy=(0.7, 0.1), xycoords='figure fraction', **cfont_mail)
  160. plt.annotate('laurent.brodeau@bsc.es', xy=(0.7, 0.1), xycoords='figure fraction', **cfont_mail)
  161. # Colorbar:
  162. ax2 = plt.axes([0.005, 0.06, 0.99, 0.025])
  163. clb = mpl.colorbar.ColorbarBase(ax2, ticks=vc_fld, cmap=pal_fld, norm=norm_fld, orientation='horizontal', extend='both')
  164. if cb_jump > 1:
  165. cb_labs = [] ; cpt = 0
  166. for rr in vc_fld:
  167. if cpt % cb_jump == 0:
  168. cb_labs.append(str(int(rr)))
  169. else:
  170. cb_labs.append(' ')
  171. cpt = cpt + 1
  172. clb.ax.set_xticklabels(cb_labs)
  173. clb.set_label(cunit, **cfont_clb)
  174. clb.ax.yaxis.set_tick_params(color='white') ; # set colorbar tick color
  175. clb.outline.set_edgecolor('white') ; # set colorbar edgecolor
  176. plt.setp(plt.getp(clb.ax.axes, 'xticklabels'), color='white') ; # set colorbar ticklabels
  177. print ' *** Saving figure...'
  178. plt.savefig(cfig, dpi=160, orientation='portrait', facecolor='#06051F')
  179. print ' => '+cfig+' created!\n'
  180. plt.close(1)
  181. del cf, carte, x0, y0