Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. #Initial config
  2. nsample = 0 #Maximum number of non-NA cells to sample. Set to 0 if you want all cells.
  3. stepx_plot=50 #Step of the X plot axis label
  4.  
  5. xlabel="Quantile value (mm/day)"
  6. ylabel="Quantile"
  7. title="Daily precipitation @ 5km resolution (1980-2008)"
  8. plotpath="plots/quantile_5+12+50+80_remap5.pdf"
  9. qprobs=c(900:1000/1000)
  10.  
  11. #Zoom setup
  12. zoom <- c(10, 90, 0.99, 1) #Left, right, bottom, top margins for zoomed area
  13. box <- c(30, 270, 0.905, 0.971) #Left, right, bottom, top margins for the are where to plot the zoom
  14.  
  15. #Definition of file variables
  16. filename <- "/home/clima-archive3/afantini/tesi/learning/obs/EURO4M-APGD/EURO4M_1980-2008.nc"
  17. filename[2] <- "/home/netapp-clima-scratch/afantini/ERAINT_12KM/output/daily/masked/masked_EURO4M_5_PR+TAS_Med_CORDEX_STS.1980-2008_remap_12to5.nc"
  18. filename[3] <- "/home/netapp-clima-scratch/afantini/ERAINT_50KM/output/daily/masked/masked_EURO4M_5_PR+TAS_Med_CORDEX_STS.1980-2008_remap_50to12to5.nc"
  19. filename[4] <- "/home/netapp-clima-scratch/afantini/ERAINT_50KM/output/daily/masked/masked_EURO4M_5_PR+TAS_Med_CORDEX_STS.1980-2008_remap_50to5.nc"
  20. filename[5] <- "/home/clima-archive/afantini/ERAIN/cat/masked/cropped/ERAIN_pr_1980-2008_daily_masked_remap_80to50to12to5_cropped.nc"
  21. filename[6] <- "/home/clima-archive/afantini/ERAIN/cat/masked/cropped/ERAIN_pr_1980-2008_daily_masked_remap_80to5_cropped.nc"
  22.  
  23. name="EURO4M-APGD \n 5km"
  24. name[2]="RegCM4.4 \n 12->5km"
  25. name[3]="RegCM4.4 \n 50->12->5km"
  26. name[4]="RegCM4.4 \n 50->5km"
  27. name[5]="ERA-Interim \n 80->50->12->5km"
  28. name[6]="ERA-Interim \n 80->5km"
  29.  
  30. var="pr"
  31. var[2]="pr"
  32. var[3]="pr"
  33. var[4]="pr"
  34. var[5]="TP"
  35. var[6]="TP"
  36.  
  37. gain=1
  38. gain[2]=86400
  39. gain[3]=86400
  40. gain[4]=86400
  41. gain[5]=86400
  42. gain[6]=86400
  43.  
  44. colors=c("#006600", "#001F5C", "#B26B00", "#FFCC99", "#500000", "#FF3399")
  45.  
  46. # #Load the necessary libraries
  47. library(raster)
  48. m <- NULL
  49. for (i in 1:length(filename) ) {
  50. cat( "\n",format(Sys.time(), "%H:%M:%S" ), " " )
  51. cat("\t\tReading file", i, "of", length(filename),":\n", filename[i], "\n ")
  52. pr <- brick(filename[i], varname=var[i], na.rm=T)
  53.  
  54.  
  55. if (nsample > ncell(pr) || nsample == 0) {
  56. cat("\t\t\tFile read. Calculating quantiles over all", ncell(pr), "cells...")
  57. qs <- quantile(getValues(pr)*gain[i], probs=qprobs, na.rm=T, type=8, names=F)
  58. } else {
  59. cnsample = nsample
  60. cat("\t\t\tFile read. Calculating quantiles over", cnsample, "random cells (out of", ncell(pr),"cells) for each timestep...")
  61. qs <- quantile(sampleRandom(pr, cnsample)*gain[i], probs=qprobs, na.rm=T, type=8, names=F)
  62. }
  63.  
  64. cat(" Done!\n")
  65.  
  66. assign( paste("df", i, sep=""), data.frame(qprobs, qs, rep(name[i], length(qs))) )
  67. m[i] <- max(qs)
  68. gc()
  69. }
  70.  
  71. df_tot <- eval(parse(text=paste("rbind(", paste("df", 1:length(filename), sep="", collapse=", "), ")")))
  72. colnames(df_tot) <- c('p','v','Dataset')
  73.  
  74. library(ggplot2)
  75. library(grid)#For unit()
  76.  
  77.  
  78.  
  79. plotbig <- ggplot(data=df_tot, aes(x=v, y=p, color=Dataset)) +
  80. geom_line(alpha = 0.7, size=.5) +
  81. geom_point(size=.8) +
  82. coord_cartesian(ylim=c(0.9, 1.001), xlim=c(0,max(m)+2)) +
  83. scale_color_manual(values=colors) +
  84. xlab(xlabel) + ylab(ylabel) +
  85. ggtitle(title) +
  86. theme_bw(base_size=12) +
  87. theme(panel.grid.major = element_line(size = .5, color = "grey"), axis.line = element_line(size=.4, color = "black"), legend.position = c(.85,.5),text = element_text(size=14), legend.key.height=unit(3,"line"))
  88.  
  89. plotsmall <- ggplotGrob(
  90. plotbig + coord_cartesian(xlim=c(zoom[1],zoom[2]), ylim=c(zoom[3], zoom[4]+0.0001)) + theme_bw(base_size=10) + theme(legend.position="none", axis.title.x = element_blank(), axis.title.y = element_blank(), title = element_blank(), plot.margin=unit(c(0,1,0,0),"mm"), plot.background = element_rect(colour = "black"))
  91. )
  92.  
  93.  
  94.  
  95.  
  96. pdf(plotpath, useDingbats=FALSE)
  97. print(
  98. plotbig + annotation_custom(grob=plotsmall, xmin = box[1], xmax = box[2], ymin = box[3], ymax= box[4]) +
  99. annotate("rect", xmin = zoom[1], xmax = zoom[2], ymin = zoom[3], ymax = zoom[4], alpha = 0, colour = "black") +
  100. annotate("segment", x = box[1], xend = zoom[1], y = box[4], yend = zoom[3],colour="black") +
  101. annotate("segment", x = box[2], xend = zoom[2], y = box[4], yend = zoom[4],colour="black")
  102. )
  103. dev.off()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement