library(raster)
xtd <- stack()
for (year in 1895:2016){
print(paste("adding ", year))
file = paste(getwd(), "/tdmean/", year, "/", year, ".tif", sep="")
raz = raster(file)
xtd <- stack( xtd , raz )
}
xtd
#save file
outtd = paste(getwd(), "/tdmean/RasterBrick.tif" , sep="")
writeRaster(xtd, filename=outtd, overwrite=TRUE)
outfile <- writeRaster(xtd, filename='xtd.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))
}
TdmeanBrick = array(xtd)
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
tdmeanExtrapolation2050 = raster(nrow=621,ncol=1405)
tdmeanExtrapolation2100 = raster(nrow=621,ncol=1405)
tdmeanExtrapolation2050 <- setExtent(tdmeanExtrapolation2050, reference, keepres=FALSE, snap=FALSE)
tdmeanExtrapolation2100 <- setExtent(tdmeanExtrapolation2100, reference, keepres=FALSE, snap=FALSE)
tdmeanExtrapolation2050
tdmeanExtrapolation2100
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(), "/progresstd.txt", sep=""), append=FALSE)
matt = getValues(TdmeanBrick, 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)
tdmeanExtrapolation2050[x:x,y:y] = output2050
tdmeanExtrapolation2100[x:x,y:y] = output2100
}
}
extrap2050 = paste(getwd(), "/tdmean/Extrapolation2050.tif" , sep="")
writeRaster(tdmeanExtrapolation2050, filename=extrap2050, format="GTiff", overwrite=TRUE)
extrap2100 = paste(getwd(), "/tdmean/Extrapolation2100.tif" , sep="")
writeRaster(tdmeanExtrapolation2100, filename=extrap2100, format="GTiff", overwrite=TRUE)
extrap2100 = paste(getwd(), "/tdmean/Extrapolation2100.tif" , sep="")
writeRaster(scale(tdmeanExtrapolation2100), filename=extrap2100, format="GTiff", overwrite=TRUE)
hist(raster(extrap2100))
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(), "/progresstd.txt", sep=""), append=FALSE)
if (!is.na(raz0[x:x,y:y])){
if (raz0[x:x,y:y] > 0){
out[x:x,y:y] = (arr[x*y]/max)* 255
}
else {
out[x:x,y:y] = 0
}
} else {
out[x:x,y:y] = 0
}
}
}
return(out)
}
tdmeanExtrapolation2050
hist(tdmeanExtrapolation2050)
normed = normalize(tdmeanExtrapolation2050)
normed <- setExtent(normed, reference, keepres=FALSE, snap=FALSE)
extrap2050norm = paste(getwd(), "/tdmean/Extrapolation2050_norm.tif" , sep="")
writeRaster(normed, filename=extrap2050norm,format="GTiff",datatype='INT1U', overwrite=TRUE)
library(raster)
for (year in 2016:2016){
testFile = paste(getwd(), "/tmean/", year, "/", year, ".tif", sep="")
input_name= raster(testFile)
plot(input_name)
input_name
#writeRaster(input_name, output_name,format="GTiff",datatype='INT1U',overwrite=TRUE)
}
year=1990
print(paste("adding ", year))
file = paste(getwd(), "/tmean/", year, "/", year, ".tif", sep="")
extent(raz)
extent(normed)
normed = setExtent(normed, raz, keepres=FALSE, snap=FALSE)
extent(normed)
extrap2050norm = paste(getwd(), "/tdmean/Extrapolation2050_norm.tif" , sep="")
writeRaster(normed, filename=extrap2050norm,format="GTiff",datatype='INT1U', overwrite=TRUE)