compare_time_series.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. #!/usr/bin/env python
  2. # B a r a K u d a
  3. #
  4. # Compare time-series between some experiments!
  5. #
  6. # L. Brodeau, 2013
  7. import sys
  8. import os
  9. import numpy as nmp
  10. from netCDF4 import Dataset
  11. import barakuda_ncio as bn
  12. import barakuda_orca as bo
  13. import barakuda_plot as bp
  14. import barakuda_tool as bt
  15. DEFAULT_LEGEND_LOC = 'out'
  16. iamoc = 1
  17. i2dfl = 1
  18. i3dfl = 1
  19. imld = 1
  20. iice = 1
  21. itrsp = 1
  22. ifwf = 1 ; # freshwater fluxes at the surface
  23. venv_needed = {'LIST_EXPS','LIST_CONF','DIAG_DIR','FIG_FORMAT', 'BM_FILE', \
  24. 'NN_SST','NN_SST','NN_SSS','NN_SSH','NN_T','NN_S','NN_MLD', \
  25. 'TRANSPORT_SECTION_FILE','LMOCLAT','i_do_ifs_flx'}
  26. vdic = bt.check_env_var(sys.argv[0], venv_needed)
  27. cd_diag = vdic['DIAG_DIR']
  28. cffig = vdic['FIG_FORMAT']
  29. i_do_ifs_flx = int(vdic['i_do_ifs_flx'])
  30. narg = len(sys.argv)
  31. if narg != 3: print 'Usage: '+sys.argv[0]+' <first_year> <last_year>'; sys.exit(0)
  32. cy1 = sys.argv[1] ; y1 = int(cy1)
  33. cy2 = sys.argv[2] ; y2 = int(cy2)
  34. nb_years = y2 - y1 + 1
  35. clist_exps = vdic['LIST_EXPS'].split()
  36. clist_conf = vdic['LIST_CONF'].split()
  37. clist_confexps = []
  38. je=0
  39. for cexp in clist_exps:
  40. clist_confexps.append(clist_conf[je]+'-'+cexp)
  41. je=je+1
  42. print sys.argv[0]+': will compare following experiments: '; print clist_confexps
  43. print ' ... saved into '+cd_diag+'\n'
  44. nbexp = len(clist_confexps)
  45. ittic = bt.iaxe_tick(nb_years)
  46. Vt = nmp.zeros( nb_years)
  47. Xf = nmp.zeros((nbexp, nb_years))
  48. def test_nb_mnth_rec(nbmn, nbyr, cnd):
  49. print ' *** nb. mnth. records =', nbmn
  50. if nbmn%12 != 0:
  51. print 'ERROR: compare_time_series.py => number of monthly records is not a multile of 12 in the netcdf file! diag = '+cnd
  52. sys.exit(0)
  53. if nbmn/12 > nbyr:
  54. print 'ERROR: compare_time_series.py => too many monthly records in netcdf file! diag = '+cnd
  55. print ' number of expected monthy records =', nbyr*12
  56. print ' number found =', nbmn
  57. sys.exit(0)
  58. return
  59. # Only one column to read:
  60. ##########################
  61. if i2dfl == 1 or i3dfl == 1 or imld == 1 :
  62. list_basin_names, list_basin_lgnms = bo.get_basin_info(vdic['BM_FILE'])
  63. if i2dfl == 1:
  64. vvar = [ vdic['NN_SSH'], vdic['NN_SSS'], vdic['NN_SST'] ]
  65. vname = [ 'SSH' , 'SSS' , 'SST' ]
  66. vunit = [ r'm' , r'PSU' , r'$^{\circ}$C']
  67. jvar=0
  68. for cvar in vvar:
  69. cdiag = 'mean_'+cvar
  70. print '\n Treating '+cdiag
  71. joce = 0
  72. for cocean in list_basin_names :
  73. Xf[:,:] = 0. ; jexp = 0
  74. for confexp in clist_confexps:
  75. cf_in = cd_diag+'/'+confexp+'/'+cdiag+'_'+confexp+'_'+cocean+'.nc'
  76. vt0, vd0 = bn.read_1d_series(cf_in, cvar, cv_t='time', l_return_time=True)
  77. nbm = len(vt0)
  78. test_nb_mnth_rec(nbm, nb_years, cdiag)
  79. VY, FY = bt.monthly_2_annual(vt0, vd0)
  80. Vt[:nbm/12] = VY[:]
  81. Xf[jexp,:nbm/12] = FY[:] ; Xf[jexp,nbm/12:] = -999.
  82. jexp = jexp + 1
  83. bp.plot("1d_multi")(Vt[:], Xf[:,:], clist_exps, cfig_type=cffig,
  84. cfignm=cdiag+'_comparison_'+cocean, dt=ittic,
  85. loc_legend=DEFAULT_LEGEND_LOC, cyunit=vunit[jvar],
  86. ctitle = vname[jvar]+', '+list_basin_lgnms[joce], ymin=0, ymax=0)
  87. joce = joce + 1
  88. jvar = jvar+1
  89. if imld == 1:
  90. cvar = vdic['NN_MLD']
  91. cdiag = 'mean_mld'
  92. vname = [ r'Mixed layer depth' ]
  93. lplot = True
  94. print '\n Treating '+cdiag
  95. Xf[:,:] = 0
  96. joce = 0
  97. for coce in list_basin_names:
  98. jexp = 0
  99. for confexp in clist_confexps:
  100. cf_in = cd_diag+'/'+confexp+'/mean_'+cvar+'_'+confexp+'_'+coce+'.nc'
  101. bt.chck4f(cf_in, script_name='compare_time_series.py')
  102. vt0, vd0 = bn.read_1d_series(cf_in, cvar, cv_t='time', l_return_time=True)
  103. nbm = len(vt0)
  104. test_nb_mnth_rec(nbm, nb_years, cdiag)
  105. VY, FY = bt.monthly_2_annual(vt0, vd0)
  106. Vt[:nbm/12] = VY[:]
  107. Xf[jexp,:nbm/12] = FY[:] ; Xf[jexp,nbm/12:] = -999.
  108. lplot = lplot and lplot
  109. jexp = jexp + 1
  110. if lplot:
  111. bp.plot("1d_multi")(Vt[:], Xf[:,:], clist_exps, cfig_type=cffig,
  112. cfignm=cdiag+'_'+coce+'_comparison', dt=ittic,
  113. loc_legend=DEFAULT_LEGEND_LOC, cyunit='m',
  114. ctitle = 'Mixed layer depth, '+list_basin_lgnms[joce],
  115. ymin=0, ymax=0)
  116. joce = joce+1
  117. # Several columns to read
  118. #=========================
  119. if i3dfl == 1:
  120. vvar = [ vdic['NN_S'], vdic['NN_T'] ]
  121. vname = [ 'Salinity' , 'Potential Temperature' ]
  122. vunit = [ r'PSU' , r'$^{\circ}$C' ]
  123. vdepth_infil = [ '0-bottom', '0-100', '100-1000', '1000-bottom' ] ; # for 3d_thetao and 3d_so
  124. vdepth_range = [ 'All', '0m-100m', '100-1000m', '1000m-bottom' ] ; # for 3d_thetao and 3d_so
  125. jvar=0
  126. for cvar in vvar:
  127. cdiag = '3d_'+cvar
  128. print '\n Treating '+cdiag
  129. joce = 0
  130. for cocean in list_basin_names :
  131. # along the 4 columns of temperature
  132. idepth=0
  133. for cdepth in vdepth_range:
  134. cdif = vdepth_infil[idepth]
  135. Xf[:,:] = 0.
  136. jexp = 0
  137. for confexp in clist_confexps:
  138. cf_in = cd_diag+'/'+confexp+'/'+cdiag+'_'+confexp+'_'+cocean+'.nc'
  139. vt0, vd0 = bn.read_1d_series(cf_in, cvar+'_'+cdif, cv_t='time', l_return_time=True)
  140. nbm = len(vt0)
  141. test_nb_mnth_rec(nbm, nb_years, cdiag)
  142. VY, FY = bt.monthly_2_annual(vt0, vd0)
  143. Vt[:nbm/12] = VY[:]
  144. Xf[jexp,:nbm/12] = FY[:] ; Xf[jexp,nbm/12:] = -999.
  145. jexp = jexp + 1
  146. bp.plot("1d_multi")(Vt[:], Xf[:,:], clist_exps, cfig_type=cffig,
  147. cfignm=cdiag+'_comparison_'+cocean+'_'+cdepth, dt=ittic, loc_legend=DEFAULT_LEGEND_LOC,
  148. cyunit=vunit[jvar], ctitle = vname[jvar]+', '+list_basin_lgnms[joce]+', depth range = '+cdepth, ymin=0, ymax=0)
  149. idepth = idepth + 1
  150. joce = joce + 1
  151. jvar = jvar+1
  152. # Sea-ice
  153. #########
  154. if iice == 1:
  155. vvar = [ 'volu' , 'area' ]
  156. vvnm = [ 'Sea-ice Volume' , 'Sea-ice Extent' ]
  157. vunit = [ r'$10^3km^3$' , r'$10^6$km$^2$' ]
  158. vpole = [ 'Arctic', 'Antarctic' ]
  159. vlab = [ 'ne' , 'se' ]
  160. jvar = -1
  161. for cvar in vvar:
  162. jvar = jvar + 1
  163. vmnth = [ 2 , 8 ]
  164. vname = [ vvnm[jvar]+' in March', vvnm[jvar]+' in September' ]
  165. for jdiag in range(len(vmnth)):
  166. print '\n Treating '+vname[jdiag]
  167. # along the 2 columns of Arcit/Antarctic
  168. ipole = 0
  169. for cpole in vpole:
  170. Xf[:,:] = 0.
  171. jexp = 0
  172. for confexp in clist_confexps:
  173. cf_in = cd_diag+'/'+confexp+'/seaice_diags.nc'
  174. vt0, vd0 = bn.read_1d_series(cf_in, cvar+'_'+vlab[ipole], cv_t='time', l_return_time=True)
  175. nbm = len(vt0)
  176. test_nb_mnth_rec(nbm, nb_years, cdiag)
  177. Vt[:nbm/12] = vt0[vmnth[jdiag]::12]
  178. Xf[jexp,:nbm/12] = vd0[vmnth[jdiag]::12] ; Xf[jexp,nbm/12:] = -999.
  179. jexp = jexp + 1
  180. cdiag = 'seaice_'+cvar
  181. cmnth = '%2.2i'%(vmnth[jdiag]+1)
  182. bp.plot("1d_multi")(Vt, Xf, clist_exps, cfig_type=cffig,
  183. cfignm=cdiag+'_m'+str(cmnth)+'_comparison_'+cpole, dt=ittic, loc_legend=DEFAULT_LEGEND_LOC,
  184. cyunit=vunit[jvar], ctitle = vname[jdiag]+', '+cpole, ymin=0, ymax=0)
  185. ipole = ipole + 1
  186. # Transport through sections
  187. ############################
  188. if itrsp == 1:
  189. print '\nUsing TRANSPORT_SECTION_FILE = '+vdic['TRANSPORT_SECTION_FILE']
  190. list_sections = bt.get_sections_from_file(vdic['TRANSPORT_SECTION_FILE'])
  191. print 'List of sections to treat: ', list_sections
  192. nbsect = len(list_sections)
  193. lok_ice = nmp.zeros(nbsect, dtype=bool)
  194. #vstuf = [ 'volume', 'heat' , 'salt' , 'freshwater' ]
  195. #vunit = [ 'Sv' , 'PW' , 'kt/s' , 'Sv' ]
  196. #ivolu = 0 ; iheat = 1 ; isalt = 2 ; ifrwt = 3
  197. vstuf = [ 'volume', 'heat' , 'freshwater' ]
  198. vunit = [ 'Sv' , 'PW' , 'Sv' ]
  199. ivolu = 0 ; iheat = 1 ; ifrwt = 2
  200. nbtt = len(vstuf)
  201. jexp = 0
  202. for confexp in clist_confexps:
  203. jsect=0
  204. for csect in list_sections:
  205. print '\n Treating transports through '+csect
  206. cf_in = cd_diag+'/'+confexp+'/transport_sect_'+csect+'.nc'
  207. lok = os.path.exists(cf_in)
  208. cf_in_ice = cd_diag+'/'+confexp+'/transport_ice_sect_'+csect+'.nc'
  209. lok_ice[jsect] = os.path.exists(cf_in_ice)
  210. if lok_ice[jsect]: id_in_ice = Dataset(cf_in_ice)
  211. if lok: id_in = Dataset(cf_in)
  212. if jsect == 0:
  213. if jexp == 0:
  214. vyear = nmp.zeros(nb_years)
  215. Xtrsp = nmp.zeros((nbexp,nbsect,nbtt,nb_years))
  216. if lok:
  217. Vt_t = id_in.variables['time'][:]
  218. nbm = len(Vt_t) ; nby = nbm/12
  219. test_nb_mnth_rec(nbm, nb_years, cdiag)
  220. Xtrsp[jexp,jsect,:,:] = -999.
  221. if lok:
  222. vyear[:nby], Xtrsp[jexp,jsect,ivolu,:nby] = bt.monthly_2_annual(Vt_t, id_in.variables['trsp_volu'][:nbm])
  223. vyear[:nby], Xtrsp[jexp,jsect,iheat,:nby] = bt.monthly_2_annual(Vt_t, id_in.variables['trsp_heat'][:nbm])
  224. #vyear[:nby], Xtrsp[jexp,jsect,isalt,:nby] = bt.monthly_2_annual(Vt_t, id_in.variables['trsp_salt'][:nbm])
  225. vyear[:nby], Xtrsp[jexp,jsect,ifrwt,:nby] = bt.monthly_2_annual(Vt_t, id_in.variables['trsp_frwt'][:nbm])
  226. if lok_ice[jsect]:
  227. print ' => adding sea-ice-related contribution to freshwater transport!'
  228. vdum, Xtrsp[jexp,jsect,ifrwt,:nby] = Xtrsp[jexp,jsect,ifrwt,:nby] + bt.monthly_2_annual(Vt_t, id_in_ice.variables['trsp_frwt'][:nbm])
  229. if lok: id_in.close()
  230. if lok_ice[jsect]: id_in_ice.close()
  231. jsect = jsect + 1
  232. jexp = jexp + 1
  233. # All data read!
  234. jsect=0
  235. for csect in list_sections:
  236. jstuff = 0
  237. for cstuf in vstuf:
  238. cex = ''
  239. if cstuf == 'freshwater' and lok_ice[jsect]: cex = ' (liquid+solid)'
  240. bp.plot("1d_multi")(vyear[:], Xtrsp[:,jsect,jstuff,:], clist_exps, cfig_type=cffig,
  241. cfignm='transport_'+cstuf+'_'+csect+'_comparison', dt=ittic, loc_legend=DEFAULT_LEGEND_LOC,
  242. cyunit=vunit[jstuff], ctitle = 'Transport of '+cstuf+cex+' through section '+csect,
  243. ymin=0, ymax=0)
  244. jstuff = jstuff + 1
  245. jsect = jsect+1
  246. # AMOC
  247. if iamoc == 1:
  248. list_lat = vdic['LMOCLAT'].split() ; nblat = len(list_lat)
  249. print '\n AMOC: '+str(nblat)+' latitude bands!'
  250. nbm_prev = 0
  251. jexp = 0
  252. for confexp in clist_confexps:
  253. jl = 0
  254. for clr in list_lat:
  255. [ c1, c2 ] = clr.split('-') ; clat_info = '+'+c1+'N+'+c2+'N'
  256. cf_in = cd_diag+'/'+confexp+'/max_moc_atl_'+clat_info+'.nc' ; bt.chck4f(cf_in, script_name='compare_time_series.py')
  257. id_in = Dataset(cf_in)
  258. if jl == 0:
  259. if jexp == 0:
  260. vyear = nmp.zeros(nb_years)
  261. Xamoc = nmp.zeros((nbexp, nblat, nb_years))
  262. Vt_t = id_in.variables['time'][:] ; nbm = len(Vt_t) ; nby = nbm/12
  263. test_nb_mnth_rec(nbm, nb_years, cdiag)
  264. Xamoc[jexp,jl,:] = -999.
  265. vyear[:nby], Xamoc[jexp,jl,:nby] = bt.monthly_2_annual(Vt_t[:nbm], id_in.variables['moc_atl'][:nbm])
  266. id_in.close()
  267. jl = jl + 1
  268. jexp = jexp + 1
  269. jl = 0
  270. for clr in list_lat:
  271. bp.plot("1d_multi")(vyear[:], Xamoc[:,jl,:], clist_exps, cfig_type=cffig,
  272. cfignm='AMOC_'+clr+'_comparison', loc_legend=DEFAULT_LEGEND_LOC,
  273. dt=ittic, cyunit='Sv', ctitle = 'AMOC ('+clr+')', ymin=0, ymax=0)
  274. jl = jl + 1
  275. # Freshwater Fluxes
  276. if ifwf == 1:
  277. vvar = [ 'EmPmR', 'E', 'R' , 'P' , 'ICalv' ]
  278. vname = [ 'E-P-R', 'Evaporation', 'Runoffs', 'Precip', 'Calving']
  279. vunit = [ r'Sv' , r'Sv', r'Sv' , r'Sv' , r'Sv' ]
  280. jvar=0
  281. for cvar in vvar:
  282. cdiag = cvar
  283. print '\n Treating FWF : '+cdiag
  284. # Testing only on the first experiment if cvar has been written:
  285. id_test = Dataset(cd_diag+'/'+clist_confexps[0]+'/mean_fwf_'+clist_confexps[0]+'_GLO.nc')
  286. list_variables = id_test.variables.keys()
  287. if cvar in list_variables:
  288. jexp=0
  289. for confexp in clist_confexps:
  290. cf_in = cd_diag+'/'+confexp+'/mean_fwf_'+confexp+'_GLO.nc'
  291. vt0, vd0 = bn.read_1d_series(cf_in, cvar, cv_t='time', l_return_time=True)
  292. nbm = len(vt0)
  293. test_nb_mnth_rec(nbm, nb_years, cdiag)
  294. VY, FY = bt.monthly_2_annual(vt0, vd0)
  295. Vt[:nbm/12] = VY[:]
  296. Xf[jexp,:nbm/12] = FY[:] ; Xf[jexp,nbm/12:] = -999.
  297. jexp = jexp + 1
  298. bp.plot("1d_multi")(Vt[:], Xf[:,:], clist_exps, cfig_type=cffig,
  299. cfignm='FWF_'+cdiag+'_comparison', dt=ittic, loc_legend=DEFAULT_LEGEND_LOC,
  300. cyunit=vunit[jvar], ctitle = vname[jvar]+' flux integrated over oceans (NEMO)',
  301. ymin=0, ymax=0)
  302. jvar = jvar+1
  303. if i_do_ifs_flx == 1:
  304. # Checking if there are filef for IFS:
  305. l_fwf_ifs = True ; jexp=0
  306. for confexp in clist_confexps:
  307. cf_in = cd_diag+'/'+confexp+'/mean_fwf_IFS_'+clist_exps[jexp]+'_GLO.nc'
  308. print ' *** Checking for the existence of '+cf_in
  309. if os.path.exists(cf_in):
  310. print " *** IFS FWF files found!"
  311. l_fwf_ifs = True and l_fwf_ifs
  312. jexp=jexp+1
  313. if l_fwf_ifs:
  314. co = ' oceans (IFS)'
  315. cl = ' land (IFS)'
  316. vvar = [ 'flx_e_sv', 'flx_p_sv' , 'flx_emp_sv', 'flx_e_land_sv', 'flx_p_land_sv' , 'flx_emp_land_sv' ]
  317. vname = [ 'E'+co , 'Precip'+co, 'E-P'+co , 'E'+cl , 'Precip'+cl , 'E-P'+cl ]
  318. vunit = [ r'Sv' , r'Sv' , r'Sv' , r'Sv' , r'Sv' , r'Sv' ]
  319. vstit = [ co, co, co, cl, cl, cl ]
  320. jvar=0
  321. for cvar in vvar:
  322. cdiag = cvar
  323. print '\n Treating IFS FWF : '+cdiag
  324. jexp=0
  325. for confexp in clist_confexps:
  326. cf_in = cd_diag+'/'+confexp+'/mean_fwf_IFS_'+clist_exps[jexp]+'_GLO.nc'
  327. vt0, vd0 = bn.read_1d_series(cf_in, cvar, cv_t='time', l_return_time=True)
  328. nbm = len(vt0)
  329. test_nb_mnth_rec(nbm, nb_years, cdiag)
  330. VY, FY = bt.monthly_2_annual(vt0, vd0)
  331. Vt[:nbm/12] = VY[:]
  332. Xf[jexp,:nbm/12] = FY[:] ; Xf[jexp,nbm/12:] = -999.
  333. jexp = jexp + 1
  334. bp.plot("1d_multi")(Vt[:], Xf[:,:], clist_exps, cfig_type=cffig,
  335. cfignm='FWF_'+cdiag+'_IFS_comparison', dt=ittic, loc_legend=DEFAULT_LEGEND_LOC,
  336. cyunit=vunit[jvar], ctitle = vname[jvar]+' flux integrated over'+vstit[jvar], ymin=0, ymax=0)
  337. jvar = jvar+1
  338. print '\n\n'+sys.argv[0]+' done...\n'