plot_timeseries.R 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/env Rscript
  2. library(s2dverification)
  3. args <- commandArgs(TRUE)
  4. comptrend <- T
  5. compcor <- T
  6. comprms <- T
  7. compspread <- T
  8. plotano <- T
  9. var <- args[1] # sie/sia/siv/tos/tas/prlr/ohc/lohc/mohc/uohc/amoc
  10. pole <- args[2] # N/S only for sia/sie
  11. nltimemax <- 124 # number of leadtimes max in the experiments (in months)
  12. nltimeout <- 60 # number of leadtimes to postprocess(in months)
  13. lstexpid <- c('i00k', 'b02p') # list of ids
  14. mon0 <- 11 # initial month
  15. year0 <- 1960 # first start date
  16. yearf <- 2005 # last start date
  17. intsdate <- 5 # interval between start dates
  18. runmeanlen <- 12 # length of the window for running mean (in months)
  19. obs <- switch(var, 'sia' = c('HadISST'), 'sie' = c('HadISST'),
  20. 'tas' = c('NCEP', 'ERA40'), 'tos' = c('ERSST', 'HadISST'),
  21. 'prlr' = c('CRU', 'GPCC'), 'ohc' = c('NEMOVAR_S4'),
  22. 'mohc' = c('NEMOVAR_S4'), 'uohc' = c('NEMOVAR_S4'),
  23. 'lohc' = c('NEMOVAR_S4'), 'amoc' = c('NEMOVAR_S4'),
  24. 'siv' = 'PIOMAS')
  25. toptitle2 <- switch(var, 'sia' = "sea ice area", 'sie' = "sea ice extent",
  26. 'siv' = "sea ice volume", 'tas' = "global T2m",
  27. 'tos' = "global SST (60S-65N)",
  28. 'prlr' = 'land precipitation (60S-65N)',
  29. 'ohc' = "global ocean heat content",
  30. 'lohc' = 'global 800m-bottom ocean heat content',
  31. 'mohc' = 'global 350m-800m ocean heat content',
  32. 'uohc' = 'global 0-350m ocean heat content',
  33. 'amoc' = 'Atlantic Overturning Streamfunction (40-55N, 1-2km)'
  34. )
  35. ytitle1 <- switch(var, 'sia' = "Millions km2", 'sie' = "Millions km2",
  36. 'siv' = 'Thousands km3', 'tas' = 'K', 'tos' = 'K',
  37. 'prlr' = 'mm/day', 'ohc' = '10e22 Joules',
  38. 'lohc' = '10e22 Joules', 'mohc' = '10e22 Joules',
  39. 'uohc' = '10e22 Joules', 'amoc' = 'Sv')
  40. syears <- seq(year0, yearf, intsdate)
  41. imon2 <- paste("0", as.character(mon0), sep = "")
  42. sdates <- paste(as.character(syears), substr(imon2, nchar(imon2) - 1,
  43. nchar(imon2)), '01', sep = "")
  44. toptitle1 <- paste(switch(pole, 'N' = "Arctic", 'S' = "Antarctic", ""),
  45. toptitle2)
  46. savename <- paste(var, switch(pole, 'N' = paste('_', pole, sep = ''),
  47. 'S' = paste('_', pole, sep = ''), ''), sep = '')
  48. for (expid in lstexpid ) {
  49. savename <- paste(savename, '_', expid, sep = '')
  50. }
  51. if (file.exists(paste(savename, '.sav', sep = ''))) {
  52. load(paste(savename, '.sav', sep = ''))
  53. } else {
  54. if (var == 'prlr' | var == 'tos' ) {
  55. fnc <- open.ncdf('/home/vguemas/analysis_hui/constant_files/ecearth_mm_lsm.nc'
  56. )
  57. mask <- get.var.ncdf(fnc, 'LSM')
  58. close.ncdf(fnc)
  59. if (var == 'prlr') {
  60. fnc <- open.ncdf('/cfu/data/dwd/gpcc_combined1x1_v4/constant_fields/land_sea_mask.nc'
  61. )
  62. mask_gpcc <- get.var.ncdf(fnc, 'lsm')
  63. close.ncdf(fnc)
  64. fnc <- open.ncdf('/cfu/data/cru/mask_cru_land.nc')
  65. mask_cru <- get.var.ncdf(fnc, 'pre')
  66. close.ncdf(fnc)
  67. fnc <- open.ncdf('/cfu/data/noaa/gpcp_v2.2/constant_fields/land_sea_mask.nc'
  68. )
  69. mask_gpcp <- get.var.ncdf(fnc, 'LSM')
  70. close.ncdf(fnc)
  71. lstmaskobs <- list(mask_cru, mask_gpcc)
  72. } else {
  73. mask <- 1 - mask
  74. lstmaskobs <- list(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  75. NULL)
  76. }
  77. lstmask <- list()
  78. for (iexp in 1:length(lstexpid)) {
  79. lstmask[[iexp]] <- mask
  80. }
  81. } else {
  82. lstmask <- list(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  83. NULL, NULL, NULL, NULL, NULL)
  84. lstmaskobs <- list(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  85. NULL)
  86. }
  87. latbnd <- switch(var, 'tos' = c(-60, 65), 'prlr' = c(-60, 65), c(-90, 90))
  88. varname <- switch(var, 'sia' = paste(var, pole, sep = ''), 'sie' = paste(var,
  89. pole, sep = ''), 'siv' = paste(var, pole, sep = ''),
  90. 'ohc' = 'heatc', 'uohc' = '0-315_heatc', 'mohc' = '373-657_heatc',
  91. 'lohc' = '800-5350_heatc', 'amoc' = 'moc_40N55N_1-2km', var)
  92. if (is.na(match('b02p', lstexpid)) == TRUE) {
  93. lstload <- lstexpid
  94. } else {
  95. lstload <- lstexpid[-match('b02p', lstexpid)]
  96. }
  97. toto1 <- Load(varname, lstload, obs, sdates, latmin = latbnd[1],
  98. latmax = latbnd[2], nleadtime = nltimemax,
  99. leadtimemax = nltimeout, maskmod = lstmask,
  100. maskobs = lstmaskobs)
  101. if (is.na(match('b02p', lstexpid)) == FALSE) {
  102. toto1bis <- Load(varname, 'b02p', obs = NULL, '19501101',
  103. latmin = latbnd[1], latmax = latbnd[2], maskmod = lstmask,
  104. maskobs = lstmaskobs)
  105. toto1ter <- Histo2Hindcast(toto1bis$mod, '19501101', sdates,
  106. nleadtimesout = nltimeout)
  107. toto1beta <- array(dim = c(dim(toto1$mod)[1] + dim(toto1ter)[1],
  108. max(dim(toto1$mod)[2], dim(toto1ter)[2]),
  109. dim(toto1$mod)[3:4]))
  110. toto1beta[1:dim(toto1$mod)[1], 1:dim(toto1$mod)[2], , ] <- toto1$mod
  111. toto1beta[(dim(toto1$mod)[1] + 1):(dim(toto1$mod)[1] + dim(toto1ter)[1]),
  112. 1:dim(toto1ter)[2], , ] <- toto1ter
  113. toto1$mod <- toto1beta
  114. lstexpid <- c(lstload, 'b02p')
  115. }
  116. if (var == 'prlr') {
  117. toto1$mod <- toto1$mod * 1000 * 3600 * 24
  118. toto1$obs <- toto1$obs * 1000 * 3600 * 24
  119. }
  120. if (var == 'ohc' | var == 'lohc' | var == 'mohc' | var == 'uohc') {
  121. toto1$mod <- toto1$mod / 1e22
  122. toto1$obs <- toto1$obs / 1e22
  123. }
  124. if (var == 'sia' | var=='sie' | var=='siv') {
  125. toto1$mod <- toto1$mod/1000
  126. if (var == 'siv') {
  127. toto1$obs <- toto1$obs/1000
  128. }
  129. }
  130. save(toto1, file = paste(savename, '.sav', sep = ''))
  131. }
  132. toto2a <- Clim(toto1$mod, toto1$obs, memb = F)
  133. toto2b_ano_exp <- Ano(toto1$mod, toto2a$clim_exp)
  134. toto2b_ano_obs <- Ano(toto1$obs, toto2a$clim_obs)
  135. toto3 <- Smoothing(toto2b_ano_exp, runmeanlen, 4)
  136. toto4 <- Smoothing(toto2b_ano_obs, runmeanlen, 4)
  137. suf <- switch(pole, 'N' = paste('_', pole, sep = ''), 'S' = paste('_', pole,
  138. sep = ''), '')
  139. PlotAno(toto1$mod, toto1$obs, sdates, toptitle = paste(lstexpid, toptitle1),
  140. ytitle = c(ytitle1, ytitle1, ytitle1), legends = obs, biglab = F,
  141. fileout = paste(var, '_', lstexpid, suf, '.eps', sep = ''))
  142. PlotAno(Smoothing(toto1$mod, runmeanlen, 4),
  143. Smoothing(toto1$obs, runmeanlen, 4), sdates,
  144. toptitle = paste("smoothed", lstexpid, toptitle1),
  145. ytitle = c(ytitle1, ytitle1, ytitle1), legends = obs, biglab = F,
  146. fileout = paste(var, '_', lstexpid, suf, '_smoothed.eps', sep = ''))
  147. if (plotano) {
  148. PlotAno(toto3, toto4, sdates, toptitle = paste("smoothed", lstexpid,
  149. toptitle1, "anomalies"), ytitle = c(ytitle1, ytitle1, ytitle1),
  150. legends = obs, biglab = F, fileout = paste(var, '_', lstexpid,suf,
  151. '_ano.eps', sep = ''))
  152. PlotClim(toto2a$clim_exp, toto2a$clim_obs, toptitle = paste(switch(pole,
  153. 'N' = "Arctic", 'S' = "Antarctic", ""), toptitle2, "climatologies"),
  154. ytitle = ytitle1, monini = mon0, listexp = lstexpid, listobs = obs,
  155. biglab = F, fileout = paste(savename, '_clim.eps', sep = ''))
  156. }
  157. if (compspread) {
  158. toto5 <- toto3 - InsertDim(Mean1Dim(toto3, 2, narm = T), 2, dim(toto3)[2])
  159. toto6 <- Spread(toto5, c(2, 3))
  160. PlotVsLTime(toto6$iqr, toptitle = paste("InterQuartile Range", toptitle1),
  161. ytitle = ytitle1, monini = mon0, listexp = lstexpid, biglab = F,
  162. fileout = paste("IQR_", savename, ".eps", sep = ''))
  163. PlotVsLTime(toto6$maxmin, toptitle = paste("Maximum-Minimum for", toptitle1),
  164. ytitle = ytitle1, monini = mon0, listexp = lstexpid, biglab = F,
  165. fileout = paste("MaxMin_", savename, ".eps", sep = ''))
  166. PlotVsLTime(toto6$sd, toptitle = paste("Standard Deviation for", toptitle1),
  167. ytitle = ytitle1, monini = mon0, listexp = lstexpid, biglab = F,
  168. fileout = paste("SD_", savename, ".eps", sep = ''))
  169. PlotVsLTime(toto6$mad, toptitle = paste("Median Absolute Deviation for",
  170. toptitle1), ytitle = ytitle1, monini = mon0, listexp = lstexpid,
  171. biglab = F, fileout = paste("Mad_", savename, ".eps", sep = ''))
  172. }
  173. if (compcor) {
  174. cor <- Corr(Mean1Dim(toto3, 2), Mean1Dim(toto4, 2), 1, 2, compROW = 3,
  175. limits = c(ceiling((runmeanlen + 1) / 2),
  176. nltimeout - floor(runmeanlen / 2)))
  177. PlotVsLTime(cor, toptitle = paste("Correlations for", toptitle1),
  178. ytitle = "correlation", monini = mon0, limits = c(-1, 2),
  179. listexp = lstexpid, listobs = obs, biglab = F,
  180. hlines = c(-1, 0, 1), fileout = paste("cor_", savename, ".eps",
  181. sep = ''))
  182. }
  183. if (comprms) {
  184. rms <- RMS(Mean1Dim(toto3, 2), Mean1Dim(toto4, 2), 1, 2, compROW = 3,
  185. limits = c(ceiling((runmeanlen + 1) / 2),
  186. nltimeout - floor(runmeanlen / 2)))
  187. PlotVsLTime(rms, toptitle = paste("RMSE for", toptitle1), ytitle = ytitle1,
  188. monini = mon0, listexp = lstexpid, listobs = obs, biglab = F,
  189. fileout = paste("rms_", savename, ".eps", sep = ""))
  190. }
  191. if (comptrend) {
  192. trends <- Consist_Trend(Mean1Dim(toto3, 2), Mean1Dim(toto4, 2), intsdate / 12)
  193. PlotVsLTime(trends$trend, toptitle = paste("Trend for", toptitle1),
  194. ytitle = paste(ytitle1, "/ year"), monini = mon0,
  195. listexp = c(lstexpid, obs), biglab = F, fileout = paste("trend_",
  196. savename, ".eps", sep = ""))
  197. }
  198. rm(list = ls())
  199. quit()