-
Notifications
You must be signed in to change notification settings - Fork 5
/
reorganizedCreateMiddayNDVI.R
279 lines (256 loc) · 9.23 KB
/
reorganizedCreateMiddayNDVI.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env Rscript
#install.packages("/projectnb/dietzelab/kiwheel/NEFI_pheno/PhenologyBayesModeling",repo=NULL)
library("ncdf4")
library(plyr)
library("PhenologyBayesModeling")
library(doParallel)
#detect cores.
n.cores <- 6
#register the cores.
registerDoParallel(cores=n.cores)
##Can only do for same TZ
createNDVI_sub <- function(siteData,orbitVersion,day.time,year){
##Will need to return a vector of the NDVI values
##Create File Paths
ACM.path <- paste("GOES_Data2017/",dir(path="GOES_Data2017",pattern=paste("OR_ABI-L2-ACMC-M3_G16_s",day.time,sep="")),sep="")
filestrC03 <- paste("OR_ABI-L1b-RadC-M3C03_G16_s",day.time,sep="")
#print(filestrC03)
filestrC02 <- paste("OR_ABI-L1b-RadC-M3C02_G16_s",day.time,sep="")
filePathC02 <- paste("GOES_Data2017/",dir(path="GOES_Data2017",pattern=filestrC02),sep="")
filePathC03 <- paste("GOES_Data2017/",dir(path="GOES_Data2017",pattern=filestrC03),sep="")
NDVIs <- numeric()
#print(ACM.path)
#print(filePathC02)
#print(filePathC03)
##Open Files
#print(paste("filePathC02:",filePathC02))
#print(paste("filePathC03:",filePathC03))
if(nchar(filePathC02)>20 && nchar(filePathC03)>20){
if(file.exists(filePathC02) && file.exists(filePathC03) && !dir.exists(filePathC02) && !dir.exists(filePathC03)){
ACM.file <-nc_open(ACM.path)
R2.file <- nc_open(filePathC02)
R3.file <- nc_open(filePathC03)
##Extract Data
R3 <- ncvar_get(R3.file,"Rad")
R2 <- ncvar_get(R2.file,"Rad") #the full R2 dataset
R3.kappa0 <- ncvar_get(R3.file,"kappa0")
R2.kappa0 <- ncvar_get(R2.file,"kappa0")
R3.DQF <- ncvar_get(R3.file,"DQF") #Data Quality Flags
R2.DQF <- ncvar_get(R2.file,"DQF")
R3 <- R3 * R3.kappa0 #done to covert radiance to reflectance
R2 <- R2 * R2.kappa0
clouds <- ncvar_get(ACM.file,"BCM")
clouds.DQF <- ncvar_get(ACM.file,"DQF")
for(i in 1:nrow(siteData)){
##General Site Data
siteName <- as.character(siteData[i,1])
lat <- as.numeric(siteData[i,2])
long <- as.numeric(siteData[i,3])
TZ <- as.numeric(siteData[i,6])
##Determine index values
lat.rd <- lat*2*pi/360
long.rd <- long*2*pi/360
Ind2 <- getDataIndex(getABI_Index(lat.rd,long.rd,orbitVersion=orbitVersion),2,orbitVersion=orbitVersion)
Ind3 <- getDataIndex(getABI_Index(lat.rd,long.rd,orbitVersion=orbitVersion),3,orbitVersion=orbitVersion)
ACM.ind <- getDataIndex(getABI_Index(lat.rd,long.rd,orbitVersion=orbitVersion),"ACM",orbitVersion=orbitVersion)
i2 <- Ind2[1]
j2 <- Ind2[2]
i3 <- Ind3[1]
j3 <- Ind3[2]
if(!is.na(R3.DQF[i3,j3]) && !is.na(R2.DQF[i2,j2]) && !is.na(R2.DQF[i2,j2]) && !is.na(R2.DQF[(i2+1),j2]) && !is.na(R2.DQF[i2,(j2+1)]) && !is.na(R2.DQF[(i2+1),(j2+1)]) && !is.na(clouds[ACM.ind[1],ACM.ind[2]]) && !is.na(clouds.DQF[ACM.ind[1],ACM.ind[2]])){
if(R3.DQF[i3,j3]==0 && R2.DQF[i2,j2]==0 && R2.DQF[i2,j2]==0 && R2.DQF[(i2+1),j2]==0 && R2.DQF[i2,(j2+1)]==0 && R2.DQF[(i2+1),(j2+1)]==0 && clouds[ACM.ind[1],ACM.ind[2]] == 0 && clouds.DQF[ACM.ind[1],ACM.ind[2]] == 0){
R3.val <- R3[i3,j3]
R2.val <- mean(R2[i2,j2],R2[(i2+1),j2],R2[i2,(j2+1)],R2[(i2+1),(j2+1)])
output <- calNDVI(R2.val,R3.val)
}
else{
output <- NA
}
}
else{
output <- NA
}
print(paste("NDVI:",output))
NDVIs <- c(NDVIs,output)
}
}
else{
return(rep(NA,nrow(siteData)))
}
}
else{
return(rep(NA,nrow(siteData)))
}
return(NDVIs)
}
createEmptyFiles <- function(siteData,day,year){
for(i in 1:length(siteData)){
siteName <- as.character(siteData[i,1])
fileName <- paste("GOES_NDVI_Midday",siteName,"_",day,"_",day,"_kappaDQF.csv",sep="")
write.table(NA,file=fileName,sep=",",col.names=FALSE,row.names=FALSE)
}
}
checkFileExists <- function(siteName,day){
##Returns True if the file exists
dat <- read.csv(file=paste(siteName,"_missingMidday_NDVI_Days.csv",sep=""),header=FALSE)
for(i in 1:nrow(dat)){
if(as.numeric(dat[i,1])==as.numeric(day)){
return(FALSE)
}
}
return(TRUE)
}
createMissingFilesList <- function(siteName){
diurnal.files <- dir(pattern=paste("GOES_NDVIMidday",siteName,sep=""))
days <- numeric()
print(paste("Length of diurnal.files:"),length(diurnal.files))
if(length(diurnal.files)!= 0){
for(i in 1:length(diurnal.files)){
#print(diurnal.files[i])
start <- as.numeric(strsplit(diurnal.files[i],"_")[[1]][4])
end <- as.numeric(strsplit(diurnal.files[i],"_")[[1]][5])
#print(start)
#print(end)
dys <- seq(start,end,1)
days <- c(days,dys)
}
}
#sort(days)
#all.days <- c(seq(321,333,1),seq(348,365,1))
all.days <- c(seq(1,333,1),seq(348,365,1))
#all.days <- seq(1,181,1)
missingDays <- numeric()
for(j in 1:length(all.days)){
if((!all.days[j] %in% days)){
missingDays <- c(missingDays,all.days[j])
}
}
#missingDays
#print(length(missingDays))
if(length(missingDays)==0){
missingDays <- c(missingDays,0)
}
write.table(missingDays,file=paste(siteName,"_missingMidday_NDVI_Days.csv",sep=""),sep=",",col.names=FALSE,row.names=FALSE)
}
createNDVI_GOES_LeftoverMAIN <- function(day,siteData,orbitVersion,year,TZ){
#print("Inside Main")
##hours for leftover calculations
print(TZ)
hrs <- as.character(seq(10+as.numeric(TZ),(13+as.numeric(TZ)),1))
print(hrs)
if(as.numeric(day)<10){
day <- paste("00",as.character(day),sep="")
}
else if(as.numeric(day)<100){
day <- paste("0",as.character(day),sep="")
}
else{
day <- as.character(day)
}
##
filestrACM <- paste("OR_ABI-L2-ACMC-M3_G16_s",year,day,sep="")
print(filestrACM)
ACM.files <- character()
for(q in 1:length(hrs)){
if(as.numeric(hrs[q])<10){
hrs[q] <- paste("0",hrs[q],sep="")
}
else if(as.numeric(hrs[q])>23){
hrs[q] <- paste("0",as.character((hrs[q]-24)),sep="")
day <- as.numeric(day)+1
if(as.numeric(day)<10){
day <- paste("00",as.character(day),sep="")
}
else if(as.numeric(day)<100){
day <- paste("0",as.character(day),sep="")
}
else{
day <- as.character(day)
}
}
print(c("hrs[q]",hrs[q]))
newFiles <- intersect(dir(path="GOES_Data2017",pattern=filestrACM),dir(path="GOES_Data2017",pattern=paste("s",year,day,hrs[q],sep="")))
ACM.files <- c(ACM.files,newFiles)
}
print(ACM.files)
if(!dir.exists((paste("GOES_Data2017/",dir(path="GOES_Data2017",pattern=filestrACM),sep="")))){
if(length(ACM.files>1)){
day.time.vals <- character()
NDVI.vals <- matrix(ncol=nrow(siteData),nrow=0)
print(dim(NDVI.vals))
for(j in 1:length(ACM.files)){
##Open Files
day.time <- substr(ACM.files[j],24,34)
day.time.vals <- c(day.time.vals,day.time)
#print(NDVI)
NDVI.vals <- rbind(NDVI.vals,createNDVI_sub(siteData=siteData,orbitVersion=orbitVersion,day.time=day.time) )
#print(NDVI.vals)
}
print(dim(NDVI.vals))
for(i in 1:nrow(siteData)){
siteName <- as.character(siteData[i,1])
fileName <- paste("GOES_NDVI_Midday",siteName,"_",day,"_",day,"_kappaDQF.csv",sep="")
print("Printing NDVI.vals[,i]:")
print(NDVI.vals[,i])
print(dim(NDVI.vals[,i]))
output <- rbind(day.time.vals,NDVI.vals[,i])
write.table(output,file=fileName,sep=",",col.names=FALSE,row.names=FALSE)
}
}
else{
createEmptyFiles(siteData=siteData,day=day)
}
}
else{
createEmptyFiles(siteData=siteData,day=day)
}
}
#########
##For TZ == 5
#siteData <- read.csv("GOES_Paper_Sites.csv",header=TRUE)[c(2,3,4,5,8,16,17,19,20),] ##TZ 5 sites
#siteData <- read.csv("GOES_Paper_Sites.csv",header=TRUE)[c(8,11,16,17,18,19,20),]#[c(2,3,4),]
#siteData <- read.csv("GOES_Paper_Sites.csv",header=TRUE)[c(1,2,3,4,5),]
#siteData <- read.csv("GOES_Paper_Sites.csv",header=TRUE)[c(6,9,10,15),] ##TZ 6 sites
#siteData <- read.csv("GOES_Paper_Sites.csv",header=TRUE)[c(7,13,14),]##TZ 7 sites
siteData <- read.csv("GOES_Paper_Sites.csv",header=TRUE)[c(12),] ##TZ 8
siteData <- read.csv("GOES_Paper_Sites.csv",header=TRUE)[c(21,22),]
TZ <- as.numeric(siteData[1,6])
for(s in 1:nrow(siteData)){
siteName <- as.character(siteData[s,1])
#print(siteName)
createMissingFilesList(siteName)
}
print("Done creating missing day files")
#all.days <- c(seq(321,333,1),seq(348,365,1))
all.days <- c(seq(1,333,1),seq(348,365,1))
#all.days <- seq(158,181)
#year <- 2017
output <-
foreach (d = 1:length(all.days)) %dopar% {
#for(d in 1:length(all.days)){
print(paste("Starting Day:",all.days[d],sep=" "))
iseq <- numeric()
if(all.days[d]<110){#######Need to change for DB
year <- 2018
}
else{
year <- 2017
}
for(s in 1:nrow(siteData)){
siteName <- as.character(siteData[s,1])
#print(siteName)
if(!checkFileExists(siteName = siteName,day=all.days[d])){
iseq <- c(iseq,s)
}
}
print(iseq)
if(all.days[d]<334 && all.days[d]>109){##Need to change for DB
orbitVersion <- "OLD"
}
else{
orbitVersion <- "NEW"
}
if(length(iseq)>0){
createNDVI_GOES_LeftoverMAIN(day=all.days[d],siteData=siteData[iseq,],orbitVersion = orbitVersion,year = year,TZ = TZ)
}
}