Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. library(raster)
  2. library(rasterVis)
  3. library(maptools)
  4. library(maps)
  5. library(ncdf4)
  6. library(ncdf.tools)
  7. library(chron)
  8. library(lattice)
  9. library(RColorBrewer)
  10.  
  11. setwd("D:/...")
  12.  
  13.  
  14. ncdf <- nc_open("2013FullYear.nc")
  15.  
  16. lon <- ncvar_get(ncdf,"Longitude")
  17. nlon <- dim(lon)
  18. head(lon)
  19.  
  20. lat <- ncvar_get(ncdf,"Latitude")
  21. nlat <- dim(lat)
  22. head(lat)
  23.  
  24. # Create a vector for Variable week
  25. t <- ncvar_get(ncdf,"Step")
  26. t
  27.  
  28. # Variable of interest in the NetCDF file
  29. dname <- "Weekly Growth Index"
  30.  
  31. # Array with
  32. tmp_array <- ncvar_get(ncdf,dname)
  33.  
  34. m <- 1
  35. tmp_slice <- tmp_array[,,m]
  36.  
  37. # Create list with matrices for each time slot
  38. tmp_stack <- vector("list",length(t))
  39.  
  40. for (i in 1:length(t)) {
  41. tmp_stack[[i]] <- tmp_array[,,i]
  42. }
  43.  
  44. # Create list for 4 seasons:
  45. tmp_stack_1 <- vector("list",length(t)/4)
  46. for (i in 1:13) {
  47. tmp_stack_1[[i]] <- tmp_array[,,i]
  48. }
  49.  
  50. tmp_stack_2 <- vector("list",length(t)/4)
  51. for (i in 14:26) {
  52. for (j in 1:13){
  53. tmp_stack_2[[j]] <- tmp_array[,,i]
  54. }
  55.  
  56. }
  57.  
  58. tmp_stack_3 <- vector("list",length(t)/4)
  59. for (i in 27:39) {
  60. for (j in 1:13){
  61. tmp_stack_3[[j]] <- tmp_array[,,i]
  62. }
  63.  
  64. }
  65.  
  66. tmp_stack_4 <- vector("list",length(t)/4)
  67. for (i in 40:52) {
  68. for (j in 1:13){
  69. tmp_stack_4[[j]] <- tmp_array[,,i]
  70. }
  71.  
  72. }
  73.  
  74. # Summarize the seasons:
  75. tmps_stack_avg1 <- Reduce("+",tmp_stack_1)/length(tmp_stack_1)
  76. tmps_stack_avg2 <- Reduce("+",tmp_stack_2)/length(tmp_stack_2)
  77. tmps_stack_avg3 <- Reduce("+",tmp_stack_3)/length(tmp_stack_3)
  78. tmps_stack_avg4 <- Reduce("+",tmp_stack_4)/length(tmp_stack_4)
  79.  
  80. tmps_stack_max1 <- apply(simplify2array(tmp_stack_1), 1:2, max)
  81. tmps_stack_max2 <- apply(simplify2array(tmp_stack_2), 1:2, max)
  82. tmps_stack_max3 <- apply(simplify2array(tmp_stack_3), 1:2, max)
  83. tmps_stack_max4 <- apply(simplify2array(tmp_stack_4), 1:2, max)
  84.  
  85.  
  86.  
  87. # Piece of code that gives nice map that looks right:
  88. grid <- expand.grid(lon=lon, lat=lat)
  89. cutpts <- seq(0,1,0.1)
  90. levelplot(tmps_stack_max1 ~ lon * lat, data=grid, at=cutpts, cuts=11, pretty=T,
  91. col.regions=(rev(brewer.pal(10,"RdBu"))))
  92.  
  93. # Convert to raster attempt 1 - gives strange result!
  94. raster1 <- raster(
  95. tmps_stack_max1,
  96. xmn=min(lon),xmx=max(lon),
  97. ymn=min(lat),ymx=max(lat),
  98. #crs=CRS(+proj=longlat +datum=WGS84)
  99. crs=CRS("+proj=cea +lat_0=0 +lon_0=0")
  100. )
  101.  
  102. plot(raster1)
  103.  
  104. #### Convert to raster... again wrong output!
  105. r <- raster(tmps_stack_max1, xmn=min(lon), xmx=max(lon), ymn=min(lat), ymx=max(lat))
  106.  
  107. s <- as(r, 'SpatialGridDataFrame')
  108. spplot(s)
  109. writeGDAL(s, "SGDF.tif")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement