-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18_getSoilDrainStats.R
More file actions
229 lines (164 loc) · 7.5 KB
/
Copy path18_getSoilDrainStats.R
File metadata and controls
229 lines (164 loc) · 7.5 KB
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
library(tidyverse)
library(sp)
library(raster)
library(rgdal)
library(rgeos)
library(rgdal)
getSD_Stats <- function(FFP_file) {
setwd("/Users/benjaminglass/Desktop/HF21/00_Datasets/FFP_data/EMS_FFP_lines_17-20")
file <- read.csv(FFP_file,stringsAsFactors=FALSE)
#create dataframe
results <- data.frame(year=NA,decDay=NA,SD_mean=NA,SD_std=NA)
year <- substr(FFP_file,14,15)
#print(year)
for (decDay in unique(file$dec.day)) {
print(decDay)
#create a variable that has just the decDay that is being looped over right now
FFP <- file[file$dec.day == decDay, ]
FFP <- FFP[complete.cases(FFP), ]
#get stats for the DecProp.tif
#stats25 <- extractStats(FFP,decDay,.25,"DecidiousProportionMegaPlot.tif","/Users/benjaminglass/Desktop/HF21/00_Datasets/00-spatial-deliverables/Megaplot_rasters")
stats50 <- extractStats_SD(FFP,decDay,.5,"HARV_dtmCrop.tif","/Users/benjaminglass/Desktop")
#stats75 <- extractStats(FFP,decDay,.75,"DecidiousProportionMegaPlot.tif","/Users/benjaminglass/Desktop/HF21/00_Datasets/00-spatial-deliverables/Megaplot_rasters")
# if (is.null(stats25[1]) || is.null(stats50[1] || is.null(stats75[1]))) {
# new_row <- c(year,decDay,NA,NA)
# results <- rbind(results,new_row)
# } else {
#
# weighted_mean <- (stats25[1]*.6)+(stats50[1]*.3)+(stats75[1]*.1)
# weighted_std <- (stats25[2]*.6)+(stats50[2]*.3)+(stats75[2]*.1)
#
# new_row <- c(year,decDay,weighted_mean,weighted_std)
#print(stats50)
new_row <- c(year,decDay,stats50[1],stats50[2])
results <- rbind(results,new_row)
}
return(results)
}
setwd("/Users/benjaminglass/Desktop/HF21/00_Datasets/misc")
# returns spatial stats for a single FFP decDay
extractStats_SD <- function(FFP_file,dayVar,rVar,rasterImage,path) {
#filters by dec.day and r
FFP_filtered <- FFP_file %>%
filter(r==rVar,
dec.day == dayVar) %>%
mutate(easting = xr+732275,
northing=yr+4713368)
# check to see if the resulting filtered data frame actually has rows in it
if (nrow(FFP_filtered)==0) {
#if the df is empty, that means there is no coordinate data to create a polygon from, and thus we just return Na,Na to the parent script
print(paste0("ERROR: NO DATA FOR ",rVar," AND ",dayVar," IN FFP FILE."))
return(c(NA,NA))
# if there is data, then we move forward
} else {
#define coords as coordinates of all the vector points in FFP filtered
coords = cbind(FFP_filtered[5],FFP_filtered[6])
#print(coords)
#create spatial polygon with all FFP filtered vector points (vector points -> polygon)
p = Polygon(coords)
ps = Polygons(list(p),1)
sps = SpatialPolygons(list(ps),proj4string = CRS("+proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
#PLOTTING PRINT CMD BELOW
# =====================
#plot(sps)
#set working directory to where outputs are
setwd(path)
#create raster stack from landsat8 imagery
rast <- raster(rasterImage)
rast[rast==0] <- NA
#print(rast)
#print(rast)
#mask landsat imagery by FFP spatial polygon
masked <- mask(rast,sps)
#plot(masked)
#print(masked)
#print(min(values(masked)))
#print(all.equal(rast,masked))
if (all(is.na(values(masked)))) {
#print(paste0("ERROR: NO SPATIAL OVERLAP BETWEEN FFP FILE AND ",rasterImage,"."))
return(c(NA,NA))
} else {
#crop masked raster by the outline of the polygon
raster <- crop(masked,sps)
#plot(raster)
#plot(masked)
# ========= DUMMY RASTER TEST =============
# this uses a blank "dummy" raster that is much larger than the megaplot raster layer to
# test whether the FFP polygon adequetely covers the megaplot raster layer. This calculates
# the pixels in the dummy raster that has been masked by the FFP polygon (representing the
# full amount of pixels in the FFP under normal conditions). Then we compare this to the
# amount of pixels in the megaplot raster masked by FFP (which may have fewer pixels if the
# footprint is off to the side). If this "percentValuePixels" is below a certain threshold value
# then we discard the results.
setwd("/Users/benjaminglass/Desktop/HF21/00_Datasets/00-spatial-deliverables")
dummyRaster <- raster("dummyRaster.tif")
dummyCrop <- crop(mask(dummyRaster,sps),sps)
dummyTotal <- ncell(dummyCrop)
total <- ncell(raster)
percentValuePixels <- total/dummyTotal
#plot(masked)
#plot(dummyCrop)
#print(paste0(dummyTotal," ",total," PVP: ",percentValuePixels))
threshold <- 0
if (percentValuePixels <= threshold ) {
#print(paste0("ERROR: SPATIAL OVERLAP WITH ",rasterImage," IS ",percentValuePixels,"."))
#print(paste0("THRESHOLD VALUE IS ",threshold,"."))
#return(c(NA,NA))
return(c(cellStats(raster, stat='mean', na.rm=TRUE),
cellStats(raster, stat='sd', na.rm=TRUE)))
} else {
#return mean and std statistics about the cropped raster
return(c(cellStats(raster, stat='mean', na.rm=TRUE),
cellStats(raster, stat='sd', na.rm=TRUE)))
}
}
# ======================================
# ALTERNATE CODE: we can use the extract function also, but it does take longer than cellStats
# ======================================
# print(c(extract(rast,sps,method='simple',buffer=NULL,fun=mean,na.rm=TRUE),
# (extract(rast,sps,method='simple',buffer=NULL,fun=sd,na.rm=TRUE))))
# return( c(extract(rast,sps,method='simple',buffer=NULL,fun=mean,na.rm=TRUE),
# (extract(rast,sps,method='simple',buffer=NULL,fun=sd,na.rm=TRUE))
# )
# )
#
}
}
#read in source files
setwd("/Users/benjaminglass/HF21-Scripts")
source("10_getMGP_stats.R")
setwd("/Users/benjaminglass/HF21-Scripts")
source("12_getLIDAR_stats.R")
setwd("/Users/benjaminglass/HF21-Scripts")
source("13_getTCT_stats.R")
setwd("/Users/benjaminglass/Desktop/HF21/00_Datasets/FFP_data/EMS_FFP_lines_17-20")
#get all FFP output files for EMS tower
temp = list.files(pattern="*.csv")
#create master results file (which will be our final product)
masterResults_all <- data.frame(year=NA,
decDay=NA,
SD_mean = NA,
SD_std = NA)
doneFiles <- data.frame(file=NA)
#variable to count files completed for debugging
iter <- 0
#===================== FOR LOOP ==========================
#looping over all files in temp
for (currentFile in temp) {
iter <- iter+1
#printing
print("============================================================")
print(paste0("FFP FILE ",iter," OF ",length(temp),": ",currentFile))
print("============================================================")
#get year of FFP file
year <- substr(currentFile,14,15)
#currentFile <- "FFP.hr.lines.00.3.csv"
# FIRST SET OF FUNCTIONS FOR MEGAPLOT
print("--------------------------------------DTM")
results <- getSD_Stats(currentFile)
print(paste0(currentFile,":----------------------------------------------------------------------------DONE"))
masterResults_all <- rbind(masterResults_all,results)
doneFile <- (currentFile)
doneFiles <- rbind(doneFiles,doneFile)
}
write.csv(masterResults_all,"/Users/benjaminglass/Desktop/HF21/00_Datasets/all_results_0714_dtm_results.csv", row.names = FALSE)