movie_square_zoom_IFS.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 netCDF4 import Dataset
  12. import matplotlib as mpl
  13. mpl.use('Agg')
  14. import matplotlib.pyplot as plt
  15. import matplotlib.colors as colors
  16. import datetime
  17. import barakuda_colmap as bcm
  18. import barakuda_tool as bt
  19. year_ref_ini = 1990
  20. #CTATM = 'T255'
  21. CTATM = 'T1279'
  22. if CTATM == 'T255':
  23. # South Greenland:
  24. #i1 = 412; i2 =486
  25. #j1 = 22 ; j2 = 56
  26. # NAtl:
  27. i1 = 385 ; i2= 540
  28. j1 = 6 ; j2 = 84
  29. #Global T255:
  30. #i1 = 0 ; i2 =511
  31. #j1 = 0 ; j2 = 255
  32. elif CTATM == 'T1279':
  33. #
  34. #Global:
  35. i1 = 0 ; i2 = 2559+1
  36. j1 = 0 ; j2 = 1279+1
  37. # Natl:
  38. ##i1 = 1849 ; i2 = 2525
  39. ##j1 = 97 ; j2 = 508
  40. ##i1 = 1960 ; i2 = 2550; #2680
  41. ##i1 = 1849 ; i2 = 2525
  42. ##j1 = 97 ; j2 = 508
  43. #i1 = 2000 ; i2 = 2590
  44. #j1 = 0 ; j2 = 519
  45. else:
  46. print 'UNKNOW ATMOSPHERE RESOLUTION!'; sys.exit(0)
  47. fig_type='png'
  48. narg = len(sys.argv)
  49. if narg < 4: print 'Usage: '+sys.argv[0]+' <file> <variable> <LSM_file>'; sys.exit(0)
  50. cf_in = sys.argv[1] ; cv_in=sys.argv[2] ; cf_lsm=sys.argv[3]
  51. lsst = False ; lshf = False
  52. if cv_in == 'T2M': lt2m = True
  53. if cv_in == 'SSTK': lsst = True
  54. if cv_in == 'SNHF': lshf = True
  55. if lt2m:
  56. #tmin=-16. ; tmax=28. ; dt = 1.
  57. tmin=-2. ; tmax=28. ; dt = 1.
  58. cpal = 'ncview_nrl'
  59. #cpal = 'jaisnd'
  60. #cpal = '3gauss'
  61. #cpal = 'rainbow2_cmyk'
  62. #cpal = 'rainbow'
  63. #cpal = 'rnb2'
  64. #cpal = 'jaisnc'
  65. #cpal = 'jaisnb'
  66. cfield = 'T2M'
  67. cunit = r'$^{\circ}C$'
  68. cb_jump = 2
  69. if lsst:
  70. tmin=-20. ; tmax=12. ; dt = 1.
  71. cpal = 'sstnw'
  72. cfield = 'SST'
  73. cunit = r'$Boo$'
  74. cb_jump = 2
  75. if lshf:
  76. tmin=-1200. ; tmax=400. ; dt = 25.
  77. #cpal = 'rainbow'
  78. cpal = 'ncview_nrl'
  79. cfield = 'Net Heat Flux'
  80. cunit = r'$W/m^2$'
  81. cb_jump = 4
  82. clsm = 'LSM'
  83. # Need to know dimension:
  84. bt.chck4f(cf_lsm)
  85. id_lsm = Dataset(cf_lsm)
  86. vlon = id_lsm.variables['lon'][:]
  87. vlat = id_lsm.variables['lat'][:]
  88. id_lsm.close()
  89. Ni0 = len(vlon)
  90. Nj0 = len(vlat)
  91. print '\n Dimension of global domain:', Ni0, Nj0
  92. imax=Ni0+1
  93. Ni = i2-i1
  94. Nj = j2-j1
  95. LSM = nmp.zeros((Nj,Ni), dtype=nmp.float)
  96. XIN = nmp.zeros((Nj,Ni))
  97. id_lsm = Dataset(cf_lsm)
  98. if i2 >= imax:
  99. print ' i2 > imax !!! => ', i2, '>', imax
  100. Xall = id_lsm.variables[clsm][0,j1:j2,:]
  101. LSM[:,0:imax-i1] = Xall[:,i1-1:imax]
  102. ii=imax-i1
  103. LSM[:,ii:Ni] = Xall[:,0:i2-imax]
  104. del Xall
  105. else:
  106. LSM[:,:] = id_lsm.variables[clsm][0,j1:j2,i1:i2]
  107. id_lsm.close()
  108. [ nj , ni ] = nmp.shape(LSM)
  109. idx_ocean = nmp.where(LSM[:,:] < 0.5)
  110. LSM[idx_ocean] = nmp.nan
  111. LSM = nmp.flipud(LSM)
  112. params = { 'font.family':'Ubuntu',
  113. 'font.size': int(12),
  114. 'legend.fontsize': int(12),
  115. 'xtick.labelsize': int(12),
  116. 'ytick.labelsize': int(12),
  117. 'axes.labelsize': int(12) }
  118. mpl.rcParams.update(params)
  119. cfont_clb = { 'fontname':'Arial', 'fontweight':'normal', 'fontsize':13 }
  120. cfont_title = { 'fontname':'Ubuntu Mono', 'fontweight':'normal', 'fontsize':18 }
  121. cfont_mail = { 'fontname':'Times New Roman', 'fontweight':'normal', 'fontstyle':'italic', 'fontsize':9, 'color':'0.5' }
  122. # Pal_Sst:
  123. pal_fld = bcm.chose_colmap(cpal)
  124. norm_fld = colors.Normalize(vmin = tmin, vmax = tmax, clip = False)
  125. pal_lsm = bcm.chose_colmap('blk')
  126. norm_lsm = colors.Normalize(vmin = 0, vmax = 1, clip = False)
  127. vc_fld = nmp.arange(tmin, tmax + dt, dt)
  128. pfin = nmp.zeros((nj,ni))
  129. bt.chck4f(cf_in)
  130. id_in = Dataset(cf_in)
  131. vtime = id_in.variables['time'][:]
  132. id_in.close()
  133. del id_in
  134. Nt = len(vtime)
  135. # Size of the figure:
  136. rat_Nj_Ni = float(Nj)/float(Ni) + 0.12
  137. rh = 7.5
  138. rw = rh/rat_Nj_Ni
  139. FSZ = ( rw , rh )
  140. rcorr = rat_Nj_Ni/(float(Nj0)/float(Ni0))
  141. print ' rcorr => ', rcorr
  142. for jt in range(Nt):
  143. print '\n *** Reading record # '+str(jt+1)+' of '+cv_in+' in '+cf_in
  144. id_in = Dataset(cf_in)
  145. if i2 >= imax:
  146. print ' i2 = ', i2
  147. Xall = id_in.variables[cv_in][jt,j1:j2,:]
  148. XIN[:,0:imax-i1] = Xall[:,i1-1:imax]
  149. ii=imax-i1
  150. XIN[:,ii:Ni] = Xall[:,0:i2-imax]
  151. del Xall
  152. else:
  153. XIN[:,:] = id_in.variables[cv_in][jt,j1:j2,i1:i2]
  154. id_in.close()
  155. del id_in
  156. if lsst or lt2m: XIN[:,:] = XIN[:,:] - 273.15
  157. ct = '%3.3i'%(jt+1)
  158. cd = str(datetime.datetime.strptime(str(year_ref_ini)+' '+ct, '%Y %j'))
  159. cdate = cd[:10] ; print ' *** cdate :', cdate
  160. cfig = 'figs/'+cv_in+'_IFS'+'_d'+ct+'.'+fig_type
  161. fig = plt.figure(num = 1, figsize=FSZ, dpi=None, facecolor='w', edgecolor='k')
  162. ax = plt.axes([0.055, 0.05, 0.9, 1.], axisbg = 'k')
  163. cf = plt.imshow(nmp.flipud(XIN), cmap = pal_fld, norm = norm_fld)
  164. plt.axis([ 0, ni, 0, nj])
  165. # Mask
  166. print ' LSM stuff...'
  167. cm = plt.imshow(LSM, cmap = pal_lsm, norm = norm_lsm)
  168. plt.title('IFS: '+cfield+', coupled ORCA12-'+CTATM+', '+cdate, **cfont_title)
  169. ax2 = plt.axes([0.04, 0.08, 0.93, 0.025])
  170. clb = mpl.colorbar.ColorbarBase(ax2, ticks=vc_fld, cmap=pal_fld, norm=norm_fld, orientation='horizontal', extend='both')
  171. #clb = plt.colorbar(cf, ticks=vc_fld, orientation='horizontal', drawedges=False, pad=0.07, shrink=1., aspect=40)
  172. cb_labs = [] ; cpt = 0
  173. for rr in vc_fld:
  174. if cpt % cb_jump == 0:
  175. cb_labs.append(str(int(rr)))
  176. else:
  177. cb_labs.append(' ')
  178. cpt = cpt + 1
  179. clb.ax.set_xticklabels(cb_labs)
  180. clb.set_label(cunit, **cfont_clb)
  181. del cf
  182. ax.annotate('laurent.brodeau@bsc.es', xy=(1, 4), xytext=(480, -85), **cfont_mail)
  183. plt.savefig(cfig, dpi=160, orientation='portrait', transparent=False)
  184. print cfig+' created!\n'
  185. plt.close(1)
  186. del fig, ax, clb, cm