-
Notifications
You must be signed in to change notification settings - Fork 2
/
Step 5. SDCI.R
103 lines (92 loc) · 2.8 KB
/
Step 5. SDCI.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
##Calculates SDCI based off of MODIS, CHIRPS, and AVHRR data that has been clipped to the shapefile
#install.packages("ncdf4")
require(ncdf4)
library(ncdf4)
#install.packages("sp")
library(sp)
#install.packages("raster")
library(raster)
#install.packages("rgdal")
library(rgdal)
#install.packages("maptools")
library(maptools)
gpclibPermit()
#install.packages("gdalUtils")
library(gdalUtils)
#directory <- '/Users/akennedy/SDCI2015/'
#setwd(directory)
AVHRR <- '/Users/akennedy/Desktop/AVHRRElSa'
filesAVHRR <- list.files(path = AVHRR, full.names=T, recursive = T, pattern = 'AVHRR.2017...')
#file name is AVHRR.2000_07.tif
list(filesAVHRR)
SDCI_A.list <- list()
for (i in 1:length(filesAVHRR)) {
r <- raster(filesAVHRR[i])
Amax <- maxValue(r)
Amin <- minValue(r)
scaleAnum <- r - Amin
scaleAden <- Amax - Amin
scaleA <- scaleAnum / scaleAden
SDCI_A <- scaleA * .25
SDCI_A.list[i] <- SDCI_A
}
#####################################
## Creating list of DATE strings ##
#####################################
CHIRPS <- '/Users/akennedy/Desktop/CHIRPSElSa'
filesCHIRPS <- list.files(path = CHIRPS, full.names=T, recursive = T, pattern = 'CHIRPS.2017...')
#file name is CHIRPS.2017.07.tif
dates.list <- list()
for (i in 1:length(filesCHIRPS)) {
x <- paste0(basename(filesCHIRPS[i]))
#print(x)
date <- substr(x, start = 8, stop = 14)
#print(date)
dates.list[i] <- date
}
print(dates.list)
CHIRPS <- '/Users/akennedy/Desktop/CHIRPSElSa'
filesCHIRPS <- list.files(path = CHIRPS, full.names=T, recursive = T, pattern = 'CHIRPS.2017...')
#file name is CHIRPS.2017.07.tif
list(filesCHIRPS)
SDCI_R.list <- list()
for (i in 1:length(filesCHIRPS)) {
r <- raster(filesCHIRPS[i])
Rmax <- maxValue(r)
Rmin <- minValue(r)
scaleRnum <- r - Rmin
scaleRden <- Rmax - Rmin
scaleR <- scaleRnum / scaleRden
SDCI_R <- scaleR * .5
SDCI_R.list[i] <- SDCI_R
}
print(SDCI_R.list)
MODIS <- '/Users/akennedy/Desktop/MODISElSa'
filesMODIS <- list.files(path = MODIS, full.names=T, recursive = F, pattern = 'MOD11C3.A2017...')
list(filesMODIS)
SDCI_MODIS.list <- list()
for (i in 1:length(filesMODIS)) {
r <- raster(filesMODIS[i])
Mmax <- maxValue(r)
Mmin <- minValue(r)
scaleMODISnum <- Mmax - r
scaleMODISden <- Mmax - Mmin
scaleMODIS <- scaleMODISnum / scaleMODISden
SDCI_MODIS <- scaleMODIS * .25
SDCI_MODIS.list[i] <- SDCI_MODIS
}
#dir.create('/Users/akennedy/Desktop/SDCIElSa')
dir.create('/Users/akennedy/Desktop/SDCIElSa/2017')
output_dir <- '/Users/akennedy/Desktop/SDCIElSa/2017'
setwd(output_dir)
for (i in 1:length(SDCI_A.list)) {
A <- SDCI_A.list[[i]]
R <- SDCI_R.list[[i]]
MODIS <- SDCI_MODIS.list[[i]]
SDCI <- A + R + MODIS
#x <- dates.list[i]
#print(x)
newName <- paste0('SDCI.',dates.list[[i]],'.tif')
print(newName)
rf <- writeRaster(x=SDCI, filename=newName, format = "GTiff", overwrite=T)
}