plot_amo.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python
  2. # L. Brodeau, February 2017
  3. # Plot the Atlantic Multidecadal Oscillation out of a time-series of SST
  4. # averaged over the North Atlantic (0-70N)
  5. import sys
  6. from string import replace
  7. import os
  8. from netCDF4 import Dataset
  9. import numpy as nmp
  10. import barakuda_tool as bt
  11. import barakuda_plot as bp
  12. import barakuda_stat as bs
  13. n_run_mean = 11
  14. #n_run_mean = 21
  15. fig_form = os.getenv('FIG_FORM')
  16. if fig_form is None: fig_form = 'png'
  17. if len(sys.argv) != 3:
  18. print 'Usage: '+sys.argv[0]+' <monthly_SST_time_series.nc> <name_sst>'
  19. sys.exit(0)
  20. cf_in = sys.argv[1]
  21. cv_in = sys.argv[2]
  22. bt.chck4f(cf_in)
  23. cname = replace(os.path.basename(cf_in), '.nc', '')
  24. id_in = Dataset(cf_in)
  25. vt_m = id_in.variables['time'][:]
  26. vsst_m = id_in.variables[cv_in][:]
  27. id_in.close()
  28. Nm = len(vsst_m)
  29. # Annual averaging first:
  30. vt, vsst = bt.monthly_2_annual(vt_m, vsst_m)
  31. Nt = len(vt)
  32. n1 = (n_run_mean-1)/2
  33. n2 = -n1 - 1
  34. vtmp = nmp.zeros(Nt)
  35. vtime = nmp.zeros(Nt-n_run_mean)
  36. xplot = nmp.zeros((Nt-n_run_mean,4)) ; # Nt-n_run_mean because X month-running mean
  37. vtime[:] = vt[n1:n2]
  38. if n_run_mean == 11: vtmp = bs.running_mean_11(vsst) ; # 11-month running mean
  39. if n_run_mean == 21: vtmp = bs.running_mean_21(vsst) ; # 21-month running mean
  40. xplot[:,0] = vsst[n1:n2]
  41. xplot[:,1] = vtmp[n1:n2]
  42. (za,zb) = bs.least_sqr_line(vtime[:], xplot[:,1]) ; # Least-square linear trend
  43. xplot[:,2] = za*vtime[:] + zb
  44. xplot[:,3] = xplot[:,1] - xplot[:,2] ; # anomaly for 11-month running mean
  45. ittic = bt.iaxe_tick(Nm/12)
  46. bp.plot("oscillation_index")( vtime, xplot[:,3], ymax=0.3, dy=0.05,
  47. tmin=vt_m[0], tmax=vt_m[-1], dt=ittic,
  48. cfignm=cname, cfig_type=fig_form,
  49. cyunit=r'SST anomaly ($^{\circ}$C)',
  50. ctitle='Atlantic Multidecadal Oscillation ('+str(n_run_mean)+'-year running mean)' )