show_global_orca_field.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. import matplotlib as mpl
  13. mpl.use('Agg')
  14. import matplotlib.pyplot as plt
  15. import matplotlib.colors as colors
  16. import matplotlib.font_manager as font_manager
  17. import warnings
  18. warnings.filterwarnings("ignore")
  19. import barakuda_orca as bo
  20. import barakuda_ncio as bnc
  21. import barakuda_colmap as bcm
  22. import barakuda_plot as bp
  23. import barakuda_tool as bt
  24. CONF = 'ORCA12'
  25. ldebug = False
  26. l_add_lon_lat = False
  27. l_draw_lat_lon_only = False
  28. l_force_mask = True
  29. lon_reorg = True
  30. l_show_colorbar = False
  31. ny_exclude_north = 0 ; # nb points to exclude at the north...
  32. ny_exclude_south = 0 ; # '' south...
  33. nx_exclude_west = 0 ; # '' west
  34. if CONF == 'ORCA12':
  35. ny_exclude_north = 350 ; # nb points to exclude at the north...
  36. ny_exclude_south = 150 ; # '' south...
  37. nx_exclude_west = 300 ; # '' west
  38. cpal_fld = 'tap1'
  39. #cpal_fld = 'tap2'
  40. color_text_colorbar = 'k'
  41. color_stff_colorbar = 'k'
  42. #color_continents = '#9C5536'
  43. #color_continents = '#EDD0AB'
  44. color_continents = '0.75'
  45. log_ctrl=0.1 ; # 0 if you want no log involved in the colorscale...
  46. #b: log_ctrl=0.05 ; # 0 if you want no log involved in the colorscale...
  47. ilon_ext = 35 ; # Map extension on the RHS in degrees....
  48. #nx_res = 1600
  49. #nx_res = 1920 ; # number of pixels you want in the final image...
  50. nx_res = -1 ; # trigers full res!!!
  51. rDPI=100.
  52. longitude = 50. ; #
  53. latitude = 0. ; # fixed latitude to view from
  54. #lforce_mask = True
  55. #year_ref_ini = 1990
  56. cf_mm = '/data/gcm_output/NEMO/ORCA12.L75/ORCA12.L75-I/mesh_mask.nc4' ; # NEMO mesh_mask file...
  57. fig_type='png'
  58. if l_draw_lat_lon_only: fig_type='svg'
  59. narg = len(sys.argv)
  60. if narg < 4: print 'Usage: '+sys.argv[0]+' <file> <variable> <# snapshot>'; sys.exit(0)
  61. cf_in = sys.argv[1] ; cv_in=sys.argv[2] ; jt=int(sys.argv[3])-1
  62. if cv_in == 'tap':
  63. cfield = 'TAP'
  64. tmin=0. ; tmax=600. ; dtemp = 10.
  65. #cpal_fld = 'ncview_hotres'
  66. #cpal_fld = 'hot_r'
  67. #cpal_fld = 'tap1' ; log_ctrl=0.1 ; # 0 if you want no log involved in the colorscale...
  68. cunit = '(MW)'
  69. cb_jump = 4
  70. elif cv_in == 'sosstsst':
  71. cfield = 'SST'
  72. log_ctrl=0 ; # 0 if you want no log involved in the colorscale...
  73. tmin=-2. ; tmax=32. ; dtemp = 2.
  74. #cpal_fld = 'ncview_hotres'
  75. #cpal_fld = 'hot_r'
  76. cunit = r'deg.C'
  77. cb_jump = 2
  78. j1=ny_exclude_north
  79. bt.chck4f(cf_in)
  80. bt.chck4f(cf_mm)
  81. id_mm = Dataset(cf_mm)
  82. XLONo = id_mm.variables['glamt'][0,:,:]
  83. (njo,nio) = nmp.shape(XLONo)
  84. j1=ny_exclude_south
  85. j2=njo-ny_exclude_north
  86. del XLONo
  87. XLONo = id_mm.variables['glamt'][0,j1:j2,:]
  88. XLATo = id_mm.variables['gphit'][0,j1:j2,:]
  89. if l_force_mask: XMSKo = id_mm.variables['tmask'][0,0,j1:j2,:]
  90. id_mm.close()
  91. #path = '/home/laurent/.fonts/OSX_conv_LinuX/HelveticaNeue.ttf'
  92. #prop = font_manager.FontProperties(fname=path)
  93. #prop.set_weight = 'light'
  94. #mpl.rcParams['font.family'] = prop.get_name()
  95. #mpl.rcParams['font.weight'] = 'light'
  96. #cfont_clb = { 'fontweight':'ultra-light', 'fontsize':int(13*font_corr), 'color':'white' }
  97. #cfont_title = { 'fontname':'Ubuntu Mono', 'fontweight':'normal', 'fontsize':int(18*font_corr), 'color':'white' }
  98. #cfont_mail = { 'fontname':'Times New Roman', 'fontweight':'normal', 'fontstyle':'italic', 'fontsize':int(10*font_corr), 'color':'0.5'}
  99. # Colormaps for fields:
  100. pal_fld = bcm.chose_colmap(cpal_fld, log_ctrl=log_ctrl)
  101. norm_fld = colors.Normalize(vmin = tmin, vmax = tmax, clip = False)
  102. vc_fld = nmp.arange(tmin, tmax + dtemp, dtemp)
  103. #pal_ice = bcm.chose_colmap(cpal_ice)
  104. #norm_ice = colors.Normalize(vmin = rmin_ice, vmax = 1, clip = False)
  105. #pal_mm = bcm.chose_colmap('blk')
  106. #norm_mm = colors.Normalize(vmin = 0., vmax = 1., clip = False)
  107. print ''
  108. vproj = [ 'ortho' ]
  109. #vproj = [ 'gall', 'mill' ,'merc' ,'cyl' ,'mbtfpq' ,'kav7' ,'moll' ,'eck4' ,'robin' ,'hammer' ]
  110. #vproj = [ 'mbtfpq' ]
  111. #cd = str(datetime.datetime.strptime('1990 '+ct, '%Y %j'))
  112. #cdate = cd[:10] ; print ' *** Date :', cdate
  113. id_fld = Dataset(cf_in)
  114. XFLDo = id_fld.variables[cv_in][jt,j1:j2,:]
  115. id_fld.close()
  116. print " => done!"
  117. if lon_reorg:
  118. XLAT = bo.lon_reorg_orca(XLATo, XLONo, ilon_ext=ilon_ext)
  119. XFLD = bo.lon_reorg_orca(XFLDo, XLONo, ilon_ext=ilon_ext)
  120. XLON = bo.lon_reorg_orca(XLONo, XLONo, ilon_ext=ilon_ext)
  121. if l_force_mask: XMSK = bo.lon_reorg_orca(XMSKo, XLONo, ilon_ext=ilon_ext)
  122. print "shape original XLON =>", nmp.shape(XLONo)
  123. print "shape intermediate XLON =>", nmp.shape(XLON)
  124. #idn = nmp.where( XLONa < 0. )
  125. #XLONa[idn] = XLONa[idn] + 360.
  126. #bnc.write_2d_mask('zshowa.nc', XFLDa, xlon=XLONa, xlat=XLATa, name='tap')
  127. #XLAT = bo.lon_reorg_orca(XLATa, XLONa, v_junc_i_p=355., v_junc_i_m=5.)
  128. #XFLD = bo.lon_reorg_orca(XFLDa, XLONa, v_junc_i_p=355., v_junc_i_m=5.)
  129. #XLON = bo.lon_reorg_orca(XLONa, XLONa, v_junc_i_p=355., v_junc_i_m=5.)
  130. #print "shape new XLON =>", nmp.shape(XLON)
  131. #del XLONa, XLATa, XFLDa
  132. else:
  133. XLAT = XLATo
  134. XFLD = XFLDo
  135. XLON = XLONo
  136. XMSK = XMSKo
  137. del XLATo,XLONo,XFLDo,XMSKo
  138. # Do we remove some RHS points:
  139. XLON = XLON[:,nx_exclude_west:]
  140. XLAT = XLAT[:,nx_exclude_west:]
  141. XMSK = XMSK[:,nx_exclude_west:]
  142. XFLD = XFLD[:,nx_exclude_west:]
  143. (nj,ni) = nmp.shape(XLON)
  144. print "shape final XLON =>", (nj,ni)
  145. if nx_res == -1: nx_res = ni
  146. #lolo: bnc.write_2d_mask('zshow.nc', XFLD, xlon=XLON, xlat=XLAT, name='tap')
  147. # Time for figure...
  148. font_corr = float(nx_res)/1200.
  149. rh = float(nx_res)/rDPI
  150. params = { 'font.family':'Helvetica Neue',
  151. 'font.weight': 'light',
  152. 'font.size': int(14*font_corr),
  153. 'legend.fontsize': int(14*font_corr),
  154. 'xtick.labelsize': int(14*font_corr),
  155. 'ytick.labelsize': int(14*font_corr),
  156. 'axes.labelsize': int(14*font_corr) }
  157. mpl.rcParams.update(params)
  158. # Creating colorbar in a dfferent image:
  159. fig = plt.figure(num = 2, figsize=(rh,rh/18.), dpi=None) #, facecolor='w', edgecolor='0.5')
  160. ax2 = plt.axes([0., 0., 1., 1.], facecolor = None)
  161. ax2 = plt.axes([0.2, 0.5, 0.6, 0.4])
  162. clb = mpl.colorbar.ColorbarBase(ax2, ticks=vc_fld, cmap=pal_fld, norm=norm_fld, orientation='horizontal', extend='both')
  163. cb_labs = [] ; cpt = 0
  164. for rr in vc_fld:
  165. if cpt % cb_jump == 0:
  166. cb_labs.append(str(int(rr)))
  167. else:
  168. cb_labs.append(' ')
  169. cpt = cpt + 1
  170. clb.ax.set_xticklabels(cb_labs)
  171. clb.set_label(cunit, color=color_text_colorbar)
  172. clb.ax.yaxis.set_tick_params(color=color_stff_colorbar) ; # set colorbar tick color
  173. clb.outline.set_edgecolor(color_stff_colorbar) ; # set colorbar edgecolor
  174. plt.setp(plt.getp(clb.ax.axes, 'xticklabels'), color=color_stff_colorbar) ; # set colorbar ticklabels
  175. cbytick_obj = plt.getp(clb.ax.axes, 'xticklabels') #tricky
  176. plt.setp(cbytick_obj, color=color_text_colorbar)
  177. #for l in clb.ax.xaxis.get_ticklabels():
  178. # l.set_family("Fixed")
  179. plt.savefig('colorbar_p'+cpal_fld+'_cc'+color_continents+'.svg', dpi=rDPI, orientation='portrait', transparent=True)
  180. ##################################################
  181. fig = plt.figure(num = 1, figsize=(rh,rh*float(nj)/float(ni)), dpi=None) #, facecolor='r', edgecolor='0.5') #, facecolor='w', edgecolor='0.5')
  182. ax = plt.axes([0., 0., 1., 1.], facecolor = color_continents)
  183. if ldebug: ax = plt.axes([0.1, 0.1, 0.8, 0.8], facecolor = 'w')
  184. if l_add_lon_lat or l_draw_lat_lon_only:
  185. VX = nmp.zeros(ni) ; VY = nmp.zeros(nj)
  186. VX[:] = XLON[100,:]
  187. dlong = abs(VX[11] - VX[10])
  188. VX0 = nmp.arange(nx_exclude_west,ni+nx_exclude_west) # lulu????
  189. VX = VX0*dlong + dlong/2.
  190. print VX,'\n'
  191. VY[:] = XLAT[:,nmp.argmax(XLAT[nj-1,:])]
  192. print VY,'\n'
  193. if not l_draw_lat_lon_only:
  194. cf = plt.pcolormesh(VX, VY, XFLD, cmap = pal_fld, norm = norm_fld)
  195. # Masking:
  196. pal_lsm = bcm.chose_colmap('terre')
  197. pal_lsm = bcm.chose_colmap('land')
  198. norm_lsm = colors.Normalize(vmin = 0., vmax = 1., clip = False)
  199. pmsk = nmp.ma.masked_where(XMSK[:,:] > 0.2, XMSK[:,:]*0.+40.)
  200. cm = plt.pcolormesh(VX, VY, pmsk, cmap = pal_lsm, norm = norm_lsm)
  201. [vvx, vvy, clon, clat] = bp.__name_coor_ticks__(lon_ext=ilon_ext);
  202. plt.yticks(vvy,clat) ; plt.xticks(vvx,clon)
  203. plt.axis([ min(VX), 360.+ilon_ext-2., min(VY), max(VY)])
  204. else:
  205. # Masking:
  206. idx_land = nmp.where(XMSK[:,:] < 0.2)
  207. XFLD[idx_land] = nmp.nan
  208. cf = plt.imshow(XFLD[:,:] , cmap = pal_fld, norm = norm_fld)
  209. plt.axis([ 0, ni, 0, nj])
  210. if l_show_colorbar:
  211. ax2 = plt.axes([0.2, 0.08, 0.6, 0.025])
  212. clb = mpl.colorbar.ColorbarBase(ax2, ticks=vc_fld, cmap=pal_fld, norm=norm_fld, orientation='horizontal', extend='both')
  213. cb_labs = [] ; cpt = 0
  214. for rr in vc_fld:
  215. if cpt % cb_jump == 0:
  216. cb_labs.append(str(int(rr)))
  217. else:
  218. cb_labs.append(' ')
  219. cpt = cpt + 1
  220. clb.ax.set_xticklabels(cb_labs)
  221. clb.set_label(cunit, **cfont_clb)
  222. clb.ax.yaxis.set_tick_params(color=color_stff_colorbar) ; # set colorbar tick color
  223. clb.outline.set_edgecolor(color_stff_colorbar) ; # set colorbar edgecolor
  224. plt.setp(plt.getp(clb.ax.axes, 'xticklabels'), color=color_stff_colorbar) ; # set colorbar ticklabels
  225. #del cf
  226. #ax.annotate('Date: '+cday+' '+chour+':00', xy=(1, 4), xytext=(nx_res-nx_res*0.22, 95), **cfont_title)
  227. #ax.annotate('laurent.brodeau@ocean-next.fr', xy=(1, 4), xytext=(nx_res-nx_res*0.2, 20), **cfont_mail)
  228. plt.savefig('figure01_p'+cpal_fld+'_cc'+color_continents+'.'+fig_type, dpi=rDPI, orientation='portrait') #, transparent=True) ; #facecolor='k')