library(raster)
xpr <- stack()
for (year in 1895:2016){
print(paste("adding ", year))
file = paste(getwd(), "/precip/", year, "/", year, ".tif", sep="")
raz = raster(file)
xpr <- stack( xpr , raz )
}
xpr
outfile <- writeRaster(xpr, filename='xpr.tif', format="GTiff", overwrite=TRUE,options=c("COMPRESS=LZW"))
print("done saving")
arrLenPer = 872505 #621x1405
getValues <- function(arr, size, xCoord, yCoord){
pts = matrix(nrow=1,ncol=size)
for(year in 1:size){
pts[year] = arr[((year - 1)*arrLenPer) + (((xCoord-1)*621) + yCoord)]
pts[year] = replace(pts[year], is.na(pts[year]), "0")
}
return(c(pts))
}
PrecipBrick = array(xpr)
year=1990
print(paste("adding ", year))
file = paste(getwd(), "/tmean/", year, "/", year, ".tif", sep="")
reference = raster(file)
#tmeanMatrix gives values versus time for a single cell
precipExtrapolation2050 = raster(nrow=621,ncol=1405)
precipExtrapolation2100 = raster(nrow=621,ncol=1405)
precipExtrapolation2050 <- setExtent(precipExtrapolation2050, reference, keepres=FALSE, snap=FALSE)
precipExtrapolation2100 <- setExtent(precipExtrapolation2100, reference, keepres=FALSE, snap=FALSE)
precipExtrapolation2050
precipExtrapolation2100
for (x in 1:621){
for(y in 1:1405){
data = paste("At [", x, ",", y, "] | Completed: ", ((x)/(621))*100, "%", sep="")
write(data, file=paste(getwd(), "/progressprecip.txt", sep=""), append=FALSE)
matt = getValues(PrecipBrick, 122, x, y)
t = lm(formula = matt ~ c(1:122))
output2050 = as.numeric(t$coefficients[1]) + (as.numeric(t$coefficients[2])*155)
output2100 = as.numeric(t$coefficients[1]) + (as.numeric(t$coefficients[2])*205)
precipExtrapolation2050[x:x,y:y] = output2050
precipExtrapolation2100[x:x,y:y] = output2100
}
}
extrap2050 = paste(getwd(), "/precip/Extrapolation2050.tif" , sep="")
writeRaster(precipExtrapolation2050, filename=extrap2050, format="GTiff", overwrite=TRUE)
extrap2100 = paste(getwd(), "/precip/Extrapolation2100.tif" , sep="")
writeRaster(precipExtrapolation2100, filename=extrap2100, format="GTiff", overwrite=TRUE)
extrap2100 = paste(getwd(), "/precip/Extrapolation2100.tif" , sep="")
writeRaster(scale(precipExtrapolation2100), filename=extrap2100, format="GTiff", overwrite=TRUE)
hist(raster(paste(getwd(), "/precip/Extrapolation2100.tif" , sep="")))
plot(raster(extrap2100))
year=1990
print(paste("adding ", year))
file = paste(getwd(), "/tmean/", year, "/", year, ".tif", sep="")
reference = raster(file)
normalize <- function (raz0){
out = raster(raz0)
max = maxValue(raz0)
arr = array(raz0)
crs(out) <- crs(raz0)
for (x in 1:nrow(raz0)){
for (y in 1:ncol(raz0)){
data = paste("At [", x, ",", y, "]", sep="")
write(data, file=paste(getwd(), "/progressprecip.txt", sep=""), append=FALSE)
if (raz0[x:x,y:y] > 0){
out[x:x,y:y] = (arr[x*y]/max)* 255
}
else {
out[x:x,y:y] = 0
}
}
}
return(out)
}
precipExtrapolation2050
hist(precipExtrapolation2050)
normed = normalize(precipExtrapolation2050)
hist(normed)
plot(normed)
normed <- setExtent(normed, reference, keepres=FALSE, snap=FALSE)
extrap2050norm = paste(getwd(), "/precip/Extrapolation2050_norm.tif" , sep="")
writeRaster(normed, filename=extrap2050norm,format="GTiff",datatype='INT1U', overwrite=TRUE)