#======================================================================================================= # Authors: Mohau Mateyisi $ Lerato Mpheshea # # Email: MMateyisi@csir.co.za # # Date: 03rd March, 2019 # # Vesion 02 (trial) # # Property of CSIR NRE # # Disclaimer: Neither the Authers nor and the host # # institution CSIR should be held liabile to any # # loss or damage that may be incured due to # # the use of this script. # # . # #=======================================================================================================
library("raster")
## Loading required package: sp
library("ncdf4")
location.of.ncdfFIles<-"E:\\CCAM-CABLE"
filesIN<-list.files(location.of.ncdfFIles)
fileIN<- filesIN[1]
Get.aggregated.layer <- function(fileIN,dirW){
# Extract data feature from file name
FILEIN.FEATURES <-unlist(strsplit(fileIN,"-"))
PARAMETER <-FILEIN.FEATURES[3]
MODEL <- FILEIN.FEATURES[1]
# upload netcdf file as a data brick
In.data <- brick(paste(dirW,fileIN,sep="\\"),varname = PARAMETER)
# combine input data bricks
T12 <- stack(In.data)
rnlayers=nlayers(T12)
# output template
Out.temp <- raster(In.data)
# The output vector of the three term mean anomaly matrices
Out.vec<-vector("list",1)
stack.seq.aggregate <- function(myvec,na.rm=T){
#**********************************************************************************
# the function takes a vector
# and it returns mean
#**********************************************************************************
my.inpu.vec<-myvec[9:28]
period.mean <- mean(my.inpu.vec)
return(period.mean)
}
#apply the aggregation function to the whole raster stack
Out.temp <- stackApply(T12, indices = rep(1, rnlayers), fun = stack.seq.aggregate, na.rm = FALSE)
# ANOM.DATA output mean anomaly for plotting, stored the output in a single vector
Out.vec[1]<-Out.temp
return(Out.vec)
}
output1<-Get.aggregated.layer(fileIN,location.of.ncdfFIles)
## Warning in `[<-`(`*tmp*`, 1, value = Out.temp): implicit list embedding of S4
## objects is deprecated
library("ggplot2")
PARAMETER<-"rnd24"
location.of.ncdfFIles<-"E:\\CCAM-CABLE"
getRaster.to.dataframe <-function(Input.raster,location.of.ncdfFIles,PARAMETER){
# Convert data for each time period into data file
SPDATA1<- as(Input.raster, "SpatialPixelsDataFrame")
SPDATA_DF1 <- as.data.frame(SPDATA1)
SPDATA_DF1 <- fortify(SPDATA_DF1)
write.csv(SPDATA_DF1,file.path(location.of.ncdfFIles, paste(PARAMETER,"mean","csv",sep = ".")), row.names = F)
return(SPDATA_DF1)
}
output2<- getRaster.to.dataframe(output1[[1]],location.of.ncdfFIles,PARAMETER)