-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredondo_snow_depth_qaqc.R
155 lines (117 loc) · 5.93 KB
/
redondo_snow_depth_qaqc.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
# jack Tarricone
# feb 24 2022
# reondo jemez snowdepth data
# with influence from Dr. Keith's script
library(zoo)
library(lubridate)
library(dplyr)
library(ggplot2);theme_set(theme_classic(12))
# read back in the proper data!!!
depth_data <-read.csv("/Users/jacktarricone/ch1_jemez_data/climate_station_data/wrcc_depth_formatted.csv")
depth_data$date_time <-mdy_hm(depth_data$date_time)
depth_data$date <-as.Date(depth_data$date)
###############################################################################################
#4: Run the second QC
#4a: Remove values outside ±2 standard deviations (± 5 cm per 15 min)
#4b: Think about using more complex routine that identifies spikes
#based on rate of change and length (as in Lehning et al., 2002)
#this might be more accurate
#5 cm seems to be about the max of real snowfall events
del_thresh = 1.5 # 5mm
depth_data$delta_depth <- c(0, diff(depth_data$redondo_snow_depth_cm))
depth_data$depth_lvl_2 <- ifelse(depth_data$delta_depth > del_thresh |
depth_data$delta_depth < (-1 * del_thresh) |
is.na(depth_data$delta_depth),
NA,
depth_data$redondo_snow_depth_cm)
# test plot of first filter of depth data
ggplot(depth_data) +
geom_point(aes(x = date_time, y = redondo_snow_depth_cm), size = .2)
ggplot(depth_data) +
geom_point(aes(x = date_time, y = depth_lvl_2), size = .1)
# filter off the bottom NaNs for interpolation funciton
# doesn't work with NaNs at ennd
depth_data <-filter(depth_data, X <= 1099)
# linearly interpolate between points to fill in NaNs
depth_data <-data.frame(X = seq(depth_data$X[1], depth_data$X[nrow(depth_data)], by = 1)) %>%
full_join(depth_data, by = "X") %>%
mutate(sd_interp = na.approx(depth_data$depth_lvl_2))
# test plot
ggplot(depth_data) +
# geom_line(aes(x = date_time, y = depth_lvl_2, col = "blue"))+
geom_line(aes(x = date_time, y = sd_interp), size = .3)
### filter again on the new data
del_thresh = 1 # 5mm
depth_data$delta_sd_interp <- c(0, diff(depth_data$sd_interp))
depth_data$depth_lvl_3 <- ifelse(depth_data$delta_sd_interp > del_thresh |
depth_data$delta_sd_interp < (-1 * del_thresh) |
is.na(depth_data$delta_sd_interp),
NA,
depth_data$sd_interp)
# linearly interpolate between interpolated data, lol
depth_data <-data.frame(X = seq(depth_data$X[1], depth_data$X[nrow(depth_data)], by = 1)) %>%
full_join(depth_data, by = "X") %>%
mutate(sd_interp_v2 = na.approx(depth_lvl_3))
# test plot with new data
ggplot(depth_data) +
# geom_line(aes(x = date_time, y = redondo_snow_depth_cm))+
# geom_point(aes(x = date_time, y = sd_interp, col = "red"), size = .3) +
geom_line(aes(x = date_time, y = sd_interp_v2, col = "blue"), size = .3)
# write.csv(depth_data, "/Users/jacktarricone/ch1_jemez_data/climate_station_data/vg/snow_depth_qaqcv1.csv" )
#### third filter!
### filter again on the new data
del_thresh = .5 # 5mm
depth_data$delta_sd_interp_v2 <- c(0, diff(depth_data$sd_interp_v2))
depth_data$depth_lvl_4 <- ifelse(depth_data$delta_sd_interp_v2 > del_thresh |
depth_data$delta_sd_interp_v2 < (-1 * del_thresh) |
is.na(depth_data$delta_sd_interp_v2),
NA,
depth_data$sd_interp_v2)
# linearly interpolate between interpolated data, lol
depth_data <-filter(depth_data, X <= 1066)
depth_data <-data.frame(X = seq(depth_data$X[1], depth_data$X[nrow(depth_data)], by = 1)) %>%
full_join(depth_data, by = "X") %>%
mutate(sd_interp_v3 = na.approx(depth_lvl_4))
# test plot with new data
ggplot(depth_data) +
# geom_line(aes(x = date_time, y = redondo_snow_depth_cm))+
geom_point(aes(x = date_time, y = sd_interp_v2, col = "red"), size = .3) +
geom_line(aes(x = date_time, y = sd_interp_v3, col = "blue"), size = .3)
######################
# compute rolling mean
######################
depth_data$sd_interp_v3_smooth3 <- rollmeanr(depth_data$sd_interp_v3, 3, fill = NA)
depth_data$sd_interp_v3_smooth10 <- rollmeanr(depth_data$sd_interp_v3, 10, fill = NA)
depth_data$sd_interp_v3_smooth20 <- rollmeanr(depth_data$sd_interp_v3, 30, fill = NA)
# test plot look good
ggplot(depth_data) +
geom_point(aes(x = date_time, y = sd_interp_v2, col = "red"), size = .3) +
geom_line(aes(x = date_time, y = sd_interp_v3_smooth20 ))
write.csv(depth_data, "/Users/jacktarricone/ch1_jemez_data/climate_station_data/redondo/redondo_snow_depth_qaqc_v2.csv" )
#### plot for ryan
insar <-filter(depth_data, date >= "2020-02-01" & date <="2020-02-26")
flight1 <-as.numeric(insar$date_time[58]) # row for correct date time
flight2 <-as.numeric(insar$date_time[226])
flight3 <-as.numeric(insar$date_time[394])
lims <- as.POSIXct(strptime(c("2020-02-01 00:00:00", "2020-02-27 00:00:00"),
format = "%Y-%m-%d %H:%M:%S"))
ggplot(insar) +
geom_point(aes(x = date_time, y = depth_lvl_3), col = "green", size = .3) +
geom_line(aes(x = date_time, y = sd_interp_v3_smooth10))+
geom_vline(xintercept = flight1, linetype=3, col = "red", alpha = .7) +
geom_vline(xintercept = flight2, linetype=3, col = "red", alpha = .7) +
geom_vline(xintercept = flight3, linetype=3, col = "red", alpha = .7) +
annotate("text", label = "Flight 1 (2/12)", x = insar$date_time[270], y = 700, col = "red") +
annotate("text", label = "Flight 2 (2/19)", x = insar$date_time[437], y = 700, col = "red") +
annotate("text", label = "Flight 3 (2/19)", x = insar$date_time[605], y = 700, col = "red") +
scale_x_datetime(limits = lims,
breaks = "1 week") +
labs(title = "HQ Met Snow Depth 2/1/20 - 2/27/20",
y = "Snow Depth [mm]",
x = "Date")
setwd("/Users/jacktarricone/ch1_jemez_data/plots")
ggsave("hq_met_sd_v2.png",
width = 8,
height = 5,
units = "in",
dpi = 300)