Skip to content

Commit

Permalink
Update and rename MCWD.R to RasterMCWD.R
Browse files Browse the repository at this point in the history
  • Loading branch information
celsohlsj authored Jun 26, 2021
1 parent 0e90a0c commit 3d3dded
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions MCWD.R → RasterMCWD.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
## MCWD (Maximum Cumulative Water Deficit) Script ##
## MCWD (Maximun Cumulative Water Deficit) Script ##
# Reference: https://agupubs.onlinelibrary.wiley.com/doi/10.1029/2006GL028946 #

# Library
library(raster)
library(snow)

setwd("Working-Directory") # Directory with Monthly Rainfall Rasters
setwd("directory-here") # Directory with Monthly Rainfall Rasters

# List of Monthly Rainfall Rasters
month.rainfall = list.files("./", pattern = '.tif$', full.names = T)
Expand Down Expand Up @@ -31,17 +33,19 @@ mcwd.f = function(x){
return(result)
}


# Applying the Function
cwd = calc(wd, fun = mcwd.f)

# Applying the Function (Parallel)
beginCluster(n=10) #Change according to the number of core on your computer
cwd = clusterR(wd, calc, args=list(fun=mcwd.f))
endCluster()
writeRaster(cwd, "./MCWD/CWD_1981_2020.tif") # Saving the Monthly CWD

# Determining the Annual MCDW
ano = 2006 # Start Year of the Temporal Series
for (i in seq(1,132,12)) { # Replace 132 by the Total Months of the Time Series
ano = 1981 # Start Year of the Temporal Series
for (i in seq(1,Y_Y,12)) { # Replace "Y_Y" by the Total Months of the Time Series
cwd.a = cwd[[i:(i+11)]]
mcwd.a = min(cwd.a)

j = 121188*(1980-ano)
mcwd.a[mcwd.a == j] = NA
# Saving the Annual MCWD
print(paste0(ano, " - ", Sys.time()))
writeRaster(mcwd.a, paste0("./MCWD/MCWD_", ano, ".tif")) # Create a Folder Called "MCWD" in your Working Directory
Expand Down

0 comments on commit 3d3dded

Please sign in to comment.