library(maptools)
require(RCurl)
#library(utils)
#URL ftp://prism.nacse.org/ daily monthly
baseurl = "https://aqs.epa.gov/aqsweb/airdata/"
for (year in 1980:2017){
url = gsub(" ", "", paste(baseurl,"/annual_aqi_by_county_",year,".zip"))
file = gsub(" ", "", paste("./epa/", year, ".zip"))
print(paste("downloading",url, " to DEST", file))
download.file(url, gsub(" ", "", paste(getwd(), "/epa/", year, ".zip", sep = "")))
}
#area <- readShapePoly("ne_10m_parks_and_protected_lands_area.shp")
url = "ftp://ftp.cpc.ncep.noaa.gov/GIS/us_temp/tmax/"
filenames = getURL(url, ftp.use.epsv = FALSE, dirlistonly = TRUE)
filenames <- strsplit(filenames, "\r\n")
filenames = unlist(filenames)
for (filename in filenames) {
print ("downloading ")
download.file(paste(url, filename, sep = ""), gsub(" ", "", paste(getwd(), "/noaatmax/", filename, sep = "")))
}
library(curl)
url = "ftp://ftp.cpc.ncep.noaa.gov/GIS/us_temp/tmax/"
h = new_handle(dirlistonly=TRUE)
con = curl(url, "r", h)
tbl = read.table(con, stringsAsFactors=TRUE, fill=TRUE)
close(con)
head(tbl)
for (filename in tbl) {
print ("downloading ")
download.file(paste(url, filename, sep = ""), gsub(" ", "", paste(getwd(), "/noaatmax/", filename, sep = "")))
}
getwd()
zipF <- paste(getwd(), "/tmean/", sep = "")
outDir <- paste(getwd(), "/tmean/", sep = "")
for (year in 1895:2016){
unzip(paste(zipF, year, ".zip", sep = ""), exdir=paste(outDir, year, sep=""))
}
library(raster)
for (year in 1895:2016){
testFile = paste(getwd(), "/tmean/", year, "/PRISM_tmean_stable_4kmM2_", year, "_bil.bil", sep="")
input_name= raster(testFile)
output_name= paste(getwd(), "/tmean/", year, "/", year, ".tif", sep="")
writeRaster(input_name, output_name,format="GTiff",datatype='INT1U',overwrite=TRUE)
}
library(raster)
for (year in 1895:2016){
testFile = paste(getwd(), "/tdmean/", year, "/PRISM_tdmean_stable_4kmM1_", year, "_bil.bil", sep="")
input_name= raster(testFile)
output_name= paste(getwd(), "/tdmean/", year, "/", year, ".tif", sep="")
writeRaster(input_name, output_name,format="GTiff",datatype='INT1U',overwrite=TRUE)
}
library(raster)
for (year in 1895:1980){
testFile = paste(getwd(), "/precip/", year, "/PRISM_ppt_stable_4kmM2_", year, "_bil.bil", sep="")
input_name= raster(testFile)
output_name= paste(getwd(), "/precip/", year, "/", year, ".tif", sep="")
writeRaster(input_name, output_name,format="GTiff",datatype='INT1U',overwrite=TRUE)
}
#switch to M3
for (year in 1981:2016){
testFile = paste(getwd(), "/precip/", year, "/PRISM_ppt_stable_4kmM3_", year, "_bil.bil", sep="")
input_name= raster(testFile)
output_name= paste(getwd(), "/precip/", year, "/", year, ".tif", sep="")
writeRaster(input_name, output_name,format="GTiff",datatype='INT1U',overwrite=TRUE)
}
library(raster)
#raz = raster(paste(getwd(), "/tmean/2003/2003/2003.tif", sep=""))
#raz
#nrow(raz)
#ncol(raz)
#matr = as.matrix(raz)
#matr[0,0]
xtm <- stack()
for (year in 1895:2016){
print(paste("adding ", year))
file = paste(getwd(), "/tmean/", year, "/", year, ".tif", sep="")
raz = raster(file)
xtm <- stack( xtm , raz )
}
xtm
#tmean first
outtm = paste(getwd(), "/tmean/RasterBrick.tif" , sep="")
writeRaster(xtm, filename=outtm, options="INTERLEAVE=BAND", overwrite=TRUE)
arr = array(xtm[[1:122]])
length(arr)
system.time(arr[500*600])
arr[500*600]
#now tdmean
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)
#now precip
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
#save file
#outpr = paste(getwd(), "/precip/RasterBrick.tif" , sep="")
#writeRaster(xpr, filename=outpr, overwrite=TRUE)
library(raster)
for (year in 1980:2016){
testFile = paste(getwd(), "/epa/", year, "/annual_aqi_by_county_", year, ".csv", sep="")
input_name= raster(testFile)
output_name= paste(getwd(), "/epa/", year, "/", year, ".tif", sep="")
writeRaster(input_name, output_name,format="GTiff",datatype='INT1U',overwrite=TRUE)
}
library(rgdal)
library(raster)
year = 1990
rawobj <- read.csv(paste(getwd(), "/epa/", year, "/annual_aqi_by_county_", year, ".csv", sep=""), stringsAsFactors = FALSE)
# look at the data structure
rawobj
nrow(rawobj)
options(geonamesUsername="tbraun96")
library(geonames)
res <- GNsearch(name_equals="Calhoun", q="Alabama", country="US", maxRows=1)
res
year=1980
options(geonamesUsername="tbraun96")
library(geonames)
colX <- matrix(ncol=1,nrow=867)
colY <- matrix(ncol=1,nrow=867)
rawobj <- read.csv(paste(getwd(), "/epa/", year, "/annual_aqi_by_county_", year, ".csv", sep=""), stringsAsFactors = FALSE)
colX[1:867] = "hello"
colX[3:3] = "yep"
rawobj[25:25,1:1]
for (idx in 1:867){
res <- GNsearch(name_equals=rawobj[idx:idx, 2:2], q=rawobj[idx:idx,1:1], country="US", maxRows=1)
if (length(res) != 0){
colX[idx:idx] = res$lat
colY[idx:idx] = res$lng
print(paste("For ", rawobj[idx:idx, 2:2], ",", rawobj[idx:idx,1:1], " we have lat/long of", res$lat, ",", res$lng, sep=""))
}
else {
print(paste("SKIPPING ", rawobj[idx:idx, 2:2], ",", rawobj[idx:idx,1:1]))
colX[idx:idx] = 0
colY[idx:idx] = 0
}
}
tworow <- cbind(colX[1:nrow(rawobj)],colY[1:nrow(rawobj)])
rawobj = cbind(rawobj, tworow)
rawobj
tworow <- cbind(colX[1:nrow(rawobj)],colY[1:nrow(rawobj)])
rawobj = cbind(rawobj, tworow)
rawobj
rawobj
year = 2000
rawobj2 <- read.csv(paste(getwd(), "/epa/", year, "/annual_aqi_by_county_", year, ".csv", sep=""), stringsAsFactors = FALSE)
# look at the data structure
rawobj2
library(rgdal)
library(raster)
rbrickref = raster(paste(getwd(), "/precip/RasterBrick.tif" , sep=""))
crs(rbrickref)
colnames(rawobj) <- NULL
rawobj
outf <- SpatialPointsDataFrame(as.matrix(rawobj[,22:23]), rawobj)
outf <- crs("+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs")
#Optimized index-value retriever fxn
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))
}
outfile <- writeRaster(xtm, filename='xtm.tif', format="GTiff", overwrite=TRUE,options=c("COMPRESS=LZW"))
print("done saving")
#PrecipBrick <- raster(paste(getwd(), "/precip/RasterBrick.tif" , sep=""))
#TdmeanBrick <- raster(paste(getwd(), "/tdmean/RasterBrick.tif" , sep=""))
#TmeanBrick = xtm
TmeanBrick = array(xtm)
print("Entering loop()")
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
tmeanExtrapolation2050 = raster(nrow=621,ncol=1405)
tmeanExtrapolation2100 = raster(nrow=621,ncol=1405)
tmeanExtrapolation2050 <- setExtent(tmeanExtrapolation2050, reference, keepres=FALSE, snap=FALSE)
tmeanExtrapolation2100 <- setExtent(tmeanExtrapolation2100, reference, keepres=FALSE, snap=FALSE)
tmeanExtrapolation2050
tmeanExtrapolation2100
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(), "/progress.txt", sep=""), append=FALSE)
matt = getValues(TmeanBrick, 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)
tmeanExtrapolation2050[x:x,y:y] = output2050
tmeanExtrapolation2100[x:x,y:y] = output2100
}
}
print("done")
extrap2050 = paste(getwd(), "/tmean/Extrapolation2050.tif" , sep="")
writeRaster(tmeanExtrapolation2050, filename=extrap2050, format="GTiff", overwrite=TRUE)
extrap2100 = paste(getwd(), "/tmean/Extrapolation2100.tif" , sep="")
writeRaster(tmeanExtrapolation2100, filename=extrap2100, format="GTiff", overwrite=TRUE)
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(), "/progress.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)
}
print("Entering loop")
tmeanExtrapolation2050
hist(tmeanExtrapolation2050)
normed = normalize(tmeanExtrapolation2050)
normed <- setExtent(normed, reference, keepres=FALSE, snap=FALSE)
extrap2050norm = paste(getwd(), "/tmean/Extrapolation2050_norm.tif" , sep="")
writeRaster(normed, filename=extrap2050norm,format="GTiff",datatype='INT1U', overwrite=TRUE)
extrap2100 = paste(getwd(), "/tmean/Extrapolation2100.tif" , sep="")
writeRaster(scale(tmeanExtrapolation2100), filename=extrap2100,format="GTiff", overwrite=TRUE)
hist(raster(extrap2100))
plot(raster(extrap2100))
PrecipBrick = array(xpr[[92:122]])
#tmeanMatrix gives values versus time for a single cell
precipExtrapolation2050 = raster(nrow=621,ncol=1405)
precipExtrapolation2100 = raster(nrow=621,ncol=1405)
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(), "/progress.txt", sep=""), append=FALSE)
matt = getValues(PrecipBrick, 30, x, y)
t = lm(formula = matt ~ c(1:30))
output2050 = as.numeric(t$coefficients[1]) + (as.numeric(t$coefficients[1])*64)
output2100 = as.numeric(t$coefficients[1]) + (as.numeric(t$coefficients[1])*114)
precipExtrapolation2050[x:x,y:y] = output2050
precipExtrapolation2100[x:x,y:y] = output2100
}
}
extrap2050 = paste(getwd(), "/precip/Extrapolation2050.tif" , sep="")
writeRaster(precipExtrapolation2050, filename=extrap2050, options="INTERLEAVE=BAND", overwrite=TRUE)
extrap2100 = paste(getwd(), "/precip/Extrapolation2100.tif" , sep="")
writeRaster(precipExtrapolation2100, filename=extrap2100, options="INTERLEAVE=BAND", overwrite=TRUE)
system.time(PrecipBrick)
TdmeanBrick = array(xtd[[92:122]])
#tmeanMatrix gives values versus time for a single cell
tdmeanExtrapolation2050 = raster(nrow=621,ncol=1405)
tdmeanExtrapolation2100 = raster(nrow=621,ncol=1405)
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(), "/progress.txt", sep=""), append=FALSE)
matt = getValues(TdmeanBrick, 30, x, y)
t = lm(formula = matt ~ c(1:30))
output2050 = as.numeric(t$coefficients[1]) + (as.numeric(t$coefficients[1])*64)
output2100 = as.numeric(t$coefficients[1]) + (as.numeric(t$coefficients[1])*114)
tdmeanExtrapolation2050[x:x,y:y] = output2050
tdmeanExtrapolation2100[x:x,y:y] = output2100
}
}
extrap2050 = paste(getwd(), "/tdmean/Extrapolation2050.tif" , sep="")
writeRaster(tdmeanExtrapolation2050, filename=extrap2050, options="INTERLEAVE=BAND", overwrite=TRUE)
extrap2100 = paste(getwd(), "/tdmean/Extrapolation2100.tif" , sep="")
writeRaster(tdmeanExtrapolation2100, filename=extrap2100, options="INTERLEAVE=BAND", overwrite=TRUE)
library(raster)
year=1990
print(paste("adding ", year))
file = paste(getwd(), "/tmean/", year, "/", year, ".tif", sep="")
reference = raster(file)
#I want to normalize the rasters first
normPrecipExtrapolation2050 = raster(paste(getwd(), "/precip/Extrapolation2050.tif" , sep=""))
normPrecipExtrapolation2100 = raster(paste(getwd(), "/precip/Extrapolation2100.tif" , sep=""))
normPrecipExtrapolation2050 <- setExtent(normPrecipExtrapolation2050, reference, keepres=FALSE, snap=FALSE)
normPrecipExtrapolation2100 <- setExtent(normPrecipExtrapolation2100, reference, keepres=FALSE, snap=FALSE)
normTmeanExtrapolation2050 = raster(paste(getwd(), "/tmean/Extrapolation2050.tif" , sep=""))
normTmeanExtrapolation2100 = raster(paste(getwd(), "/tmean/Extrapolation2100.tif" , sep=""))
normTmeanExtrapolation2050 <- setExtent(normTmeanExtrapolation2050, reference, keepres=FALSE, snap=FALSE)
normTmeanExtrapolation2100 <- setExtent(normTmeanExtrapolation2100, reference, keepres=FALSE, snap=FALSE)
normTdmeanExtrapolation2050 = raster(paste(getwd(), "/tdmean/Extrapolation2050.tif" , sep=""))
normTdmeanExtrapolation2100 = raster(paste(getwd(), "/tdmean/Extrapolation2100.tif" , sep=""))
normTdmeanExtrapolation2050 <- setExtent(normTdmeanExtrapolation2050, reference, keepres=FALSE, snap=FALSE)
normTdmeanExtrapolation2100 <- setExtent(normTdmeanExtrapolation2100, reference, keepres=FALSE, snap=FALSE)
#extrap2050 = paste(getwd(), "/tmean/Extrapolation2050.tif" , sep="")
#writeRaster(scale(tmeanExtrapolation2050) +4, filename=extrap2050, format="GTiff",datatype='INT1U', overwrite=TRUE)
hist(normTmeanExtrapolation2050)
print("Done loading and normalizing rasters")
normPrecipExtrapolation2100
arrNormPrecipExtrapolation2050 = scale(array(normPrecipExtrapolation2050))
arrNormTmeanExtrapolation2050 = scale(array(normTmeanExtrapolation2050))
arrNormTdmeanExtrapolation2050 = scale(array(normTdmeanExtrapolation2050))
arrNormPrecipExtrapolation2100 = scale(array(normPrecipExtrapolation2100))
arrNormTmeanExtrapolation2100 = scale(array(normTmeanExtrapolation2100))
arrNormTdmeanExtrapolation2100 = scale(array(normTdmeanExtrapolation2100))
weightTMean = .5
weightPrecip = .4
weightTdmean = .1
doing2100 = 1
SustainabilityIndex2050 = raster(nrow=621,ncol=1405)
SustainabilityIndex2100 = raster(nrow=621,ncol=1405)
SustainabilityIndex2050 <- setExtent(SustainabilityIndex2050, reference, keepres=FALSE, snap=FALSE)
SustainabilityIndex2100 <- setExtent(SustainabilityIndex2100, reference, keepres=FALSE, snap=FALSE)
arrNormPrecipExtrapolation2050 = array(normPrecipExtrapolation2050)
arrNormTmeanExtrapolation2050 = array(normTmeanExtrapolation2050)
arrNormTdmeanExtrapolation2050 = array(normTdmeanExtrapolation2050)
arrNormPrecipExtrapolation2100 = array(normPrecipExtrapolation2100)
arrNormTmeanExtrapolation2100 = array(normTmeanExtrapolation2100)
arrNormTdmeanExtrapolation2100 = array(normTdmeanExtrapolation2100)
if (doing2100 == 0){
for (x in 1:621){
for(y in 1:1405){
#Rainfall is good, Heat is bad. Score Total: SI(tdmean,precip,tmean) = .4*precip + .4*(1/tmean) + .2*(1/tdmean)
index = ((x-1)*621) + y
tmeanVal = arrNormTmeanExtrapolation2050[index]
tdmeanVal = arrNormTdmeanExtrapolation2050[index]
if (!is.finite(tmeanVal) && is.finite(tmeanVal)){
SI2050 = (weightPrecip*arrNormPrecipExtrapolation2050[index]*1000/255) - 0 - (weightTdmean*(tdmeanVal*1000/41))
}
else if (is.finite(tmeanVal) && !is.finite(tmeanVal)){
SI2050 = (weightPrecip*arrNormPrecipExtrapolation2050[index]*1000/255) - (weightTMean*(tmeanVal*1000/60)) - 0
}
else if (!is.finite(tmeanVal) && !is.finite(tmeanVal)){
SI2050 = (weightPrecip*arrNormPrecipExtrapolation2050[index]*1000/255) - 0 - 0
}
else {
SI2050 = (weightPrecip*arrNormPrecipExtrapolation2050[index]*1000/255) - (weightTMean*(tmeanVal*1000/60)) - (weightTdmean*(tdmeanVal*1000/41))
}
#SI2100 = (weightPrecip*normPrecipExtrapolation2100[x:x,y:y]) + (weightTMean*(1/normTmeanExtrapolation2100[x:x,y:y])) + (weightTdmean*(1/normTdmeanExtrapolation2100[x:x, y:y]))
#SI2050[!is.finite(SI2050)] <- 0
SustainabilityIndex2050[x:x,y:y] = SI2050
#SustainabilityIndex2100[x:x,y:y] = SI2100
data = paste("At [", x, ",", y, "] | Completed: ", ((x)/(621))*100, "%", sep="")
write(data, file=paste(getwd(), "/progress.txt", sep=""), append=FALSE)
}
}
}
if (doing2100 == 1){
for (x in 1:621){
for(y in 1:1405){
#Rainfall is good, Heat is bad. Score Total: SI(tdmean,precip,tmean) = .4*precip + .4*(1/tmean) + .2*(1/tdmean)
index = ((x-1)*621) + y
tmeanVal = arrNormTmeanExtrapolation2100[index]
tdmeanVal = arrNormTdmeanExtrapolation2100[index]
if (!is.finite(tmeanVal) && is.finite(tmeanVal)){
SI2100 = (weightPrecip*arrNormPrecipExtrapolation2100[index]*1000/255) - 0 - (weightTdmean*(tdmeanVal*1000/41))
}
else if (is.finite(tmeanVal) && !is.finite(tmeanVal)){
SI2100 = (weightPrecip*arrNormPrecipExtrapolation2100[index]*1000/255) - (weightTMean*(tmeanVal*1000/60)) - 0
}
else if (!is.finite(tmeanVal) && !is.finite(tmeanVal)){
SI2100 = (weightPrecip*arrNormPrecipExtrapolation2100[index]*1000/255) - 0 - 0
}
else {
SI2100 = (weightPrecip*arrNormPrecipExtrapolation2100[index]*1000/255) - (weightTMean*(tmeanVal*1000/60)) - (weightTdmean*(tdmeanVal*1000/41))
}
#SI2050[!is.finite(SI2050)] <- 0
#SustainabilityIndex2050[x:x,y:y] = SI2050
SustainabilityIndex2100[x:x,y:y] = SI2100
data = paste("At [", x, ",", y, "] | Completed: ", ((x)/(621))*100, "%", sep="")
write(data, file=paste(getwd(), "/progress.txt", sep=""), append=FALSE)
}
}
}
extrap2100 = paste(getwd(), "/US_Sustainability_SurvivalIndex_2100.tif", sep="")
writeRaster(SustainabilityIndex2100, filename=extrap2100, format="GTiff", overwrite=TRUE)
#v = raster(paste(getwd(), "/US_Sustainability_SurvivalIndex_2100.tif", sep=""))
#hist(SustainabilityIndex2050)
#hist(scale(SustainabilityIndex2050))
hist(SustainabilityIndex2100)
convertToNA <- function (raz){
out = raster(raz)
for (x in 1:nrow(raz)){
for(y in 1:ncol(raz)){
data = paste("At [", x, ",", y, "] | Completed: ", ((x)/(621))*100, "%", sep="")
write(data, file=paste(getwd(), "/progress.txt", sep=""), append=FALSE)
if (raz[x:x,y:y] == 0){
out[x:x,y:y] = NA
} else {
out[x:x, y:y] = raz[x:x,y:y]
}
}
}
return(out)
}
partsX = floor(621/20)
partsY = floor(1405/40)
tmp = convertToNA(SustainabilityIndex2050)
tmp
data_matrix <- rasterToPoints(tmp)
head(data_matrix)
obj <- kmeans(data_matrix, 5)
plot(data_matrix, col = obj$cluster)
#Do the same thing as above except normalize once again
extrap2050 = paste(getwd(), "/US_Sustainability_SurvivalIndex_2050_norm.tif" , sep="")
writeRaster(normImage(SustainabilityIndex2050, norm=TRUE), filename=extrap2050, options="INTERLEAVE=BAND", overwrite=TRUE)
extrap2100 = paste(getwd(), "/US_Sustainability_SurvivalIndex_2100_norm.tif" , sep="")
writeRaster(normImage(SustainabilityIndex2100, norm=TRUE), filename=extrap2100, options="INTERLEAVE=BAND", overwrite=TRUE)