Skip to content

Commit 4d95950

Browse files
committed
1 parent f3abb30 commit 4d95950

6 files changed

Lines changed: 109 additions & 158 deletions

File tree

analysis/1-pressure.R

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,17 @@ if (debug) {
134134

135135
# Query timeserie of pressure based on these path
136136
pressure_timeserie <- geopressure_ts_path(path, pam_short$pressure, include_flight = c(0, 1))
137+
pressure_ts_bind <- do.call("rbind", pressure_timeserie) %>%
138+
filter(!is.na(sta_id))
137139

138140
# Test 3 ----
139141
p <- ggplot() +
140142
geom_line(data = pam$pressure, aes(x = date, y = obs), colour = "grey") +
141143
geom_point(data = subset(pam$pressure, isoutlier), aes(x = date, y = obs), colour = "black") +
142-
geom_line(data = pressure_na, aes(x = date, y = obs, color = factor(sta_id)), size = 0.5) +
143-
geom_line(data = do.call("rbind", pressure_timeserie) %>% filter(!is.na(sta_id)), aes(x = date, y = pressure0, col = factor(sta_id)), linetype = 2) +
144+
geom_line(data = pam$pressure %>%
145+
mutate(obs = ifelse(isoutlier | sta_id == 0, NA, obs)),
146+
aes(x = date, y = obs, color = factor(sta_id)), size = 0.5) +
147+
geom_line(data = pressure_ts_bind %>% filter(sta_id > 0), aes(x = date, y = pressure0, col = factor(sta_id)), linetype = 2) +
144148
theme_bw() +
145149
scale_colour_manual(values = col) +
146150
scale_y_continuous(name = "Pressure(hPa)")
@@ -149,16 +153,19 @@ if (debug) {
149153

150154

151155
# Test 4 ----
152-
par(mfrow = c(5, 6), mar = c(1, 1, 3, 1))
153-
for (i_r in seq_len(length(pressure_timeserie))) {
154-
if (!is.null(pressure_timeserie[[i_r]])) {
155-
i_s <- unique(pressure_timeserie[[i_r]]$sta_id)
156-
df3 <- merge(pressure_timeserie[[i_r]], subset(pam$pressure, !isoutlier & sta_id == i_s), by = "date")
157-
df3$error <- df3$pressure0 - df3$obs.x
158-
hist(df3$error, main = i_s, xlab = "", ylab = "")
159-
abline(v = 0, col = "red")
160-
}
161-
}
156+
# You might also want to adjust the value of s in `geopressure_prob_map()` based on the SD value
157+
# of these histogram gpr$prob_map_s
158+
pam$pressure %>%
159+
left_join(pressure_ts_bind %>% dplyr::select(c("date","pressure0")), by="date") %>%
160+
mutate(diff=ifelse(is.na(pressure0), 0, obs-pressure0)) %>%
161+
filter(sta_id > 0 & !isoutlier) %>%
162+
group_by(sta_id) %>%
163+
mutate(sta_id = paste0(sta_id, " (SD=",round(sd(diff),2)," ; N=",n(),")")) %>%
164+
ggplot( aes(x=diff)) +
165+
geom_histogram(aes(y=(..count..)/tapply(..count..,..PANEL..,sum)[..PANEL..]), binwidth=.4) +
166+
facet_wrap(~sta_id) +
167+
scale_x_continuous(name = "Pressure Geolocator - best match ERA5 (hPa)") +
168+
scale_y_continuous(name = "Normalized histogram")
162169

163170
# Map the most likely position
164171
sta_duration <- unlist(lapply(pressure_prob, function(x) {

analysis/2-light.R

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -130,38 +130,43 @@ if (debug) {
130130
hist(z, freq = F)
131131
lines(fit_z, col = "red")
132132

133-
# Compare with pressure
134-
load(paste0("data/1_pressure/", gdl, "_pressure_prob.Rdata"))
135-
dur <- unlist(lapply(pressure_prob, function(x) difftime(metadata(x)$temporal_extent[2], metadata(x)$temporal_extent[1], units = "days")))
136-
long_id <- which(dur > 5)
137-
138-
par(mfrow = c(2, 3))
139-
for (i_s in long_id) {
140-
twl_fl <- twl %>%
141-
filter(!deleted) %>%
142-
filter(twilight > pressure_timeserie[[i_s]]$date[1] & twilight < tail(pressure_timeserie[[i_s]]$date, 1))
143-
sun <- solar(twl_fl$twilight)
144-
z_i <- refracted(zenith(sun, pressure_timeserie[[i_s]]$lon[1], pressure_timeserie[[i_s]]$lat[1]))
145-
hist(z_i, freq = F, main = paste0("sta_id=", i_s, " n=", nrow(twl_fl)))
146-
lines(fit_z, col = "red")
147-
}
133+
# Compute twilight at the best location of pressure
134+
path <- geopressure_map2path(pressure_prob)
135+
twl_path <- left_join(twl, path) %>%
136+
mutate(
137+
twilight = twilight(twilight,
138+
lon = lon, lat = lat, rise = rise, zenith = 96)
139+
) %>%
140+
filter(!is.na(twilight))
148141

149-
# Light comparison
142+
# Twilight comparison with pressure based position (probably not correct for short stationay period)
150143
lightImage(
151144
tagdata = raw_geolight,
152145
offset = gpr$shift_k / 60 / 60
153146
)
154147
tsimagePoints(twl$twilight,
155-
offset = gpr$shift_k / 60 / 60, pch = 16, cex = 1.2,
156-
col = ifelse(twl$deleted, "grey20", ifelse(twl$rise, "firebrick", "cornflowerblue"))
148+
offset = -gpr$shift_k / 60 / 60, pch = 16, cex = 1.2,
149+
col = ifelse(twl$deleted, "grey20", ifelse(twl$rise, "firebrick", "cornflowerblue"))
150+
)
151+
tsimageDeploymentLines(twl_path$twilight,
152+
lon = twl_path$lon, twl_path$lat,
153+
offset = -gpr$shift_k / 60 / 60, lwd = 3, col = adjustcolor("orange", alpha.f = 0.5)
157154
)
158-
for (i_s in long_id) {
155+
156+
# Check histogram of zenith angle for each stationary period based on pressure.
157+
# Filter long stationary period
158+
sta_id_long <- pam$sta %>%
159+
filter(difftime(end,start,units = "days")>5) %>%
160+
.$sta_id
161+
162+
par(mfrow = c(2, 3))
163+
for (i_s in sta_id_long) {
159164
twl_fl <- twl %>%
160-
filter(twilight > pressure_timeserie[[i_s]]$date[1] & twilight < tail(pressure_timeserie[[i_s]]$date, 1))
161-
tsimageDeploymentLines(twl_fl$twilight,
162-
lon = pressure_timeserie[[i_s]]$lon[1], pressure_timeserie[[i_s]]$lat[1],
163-
offset = gpr$shift_k / 60 / 60, lwd = 3, col = adjustcolor("orange", alpha.f = 0.5)
164-
)
165+
filter(!deleted) %>%
166+
filter(sta_id==i_s)
167+
z_i <- refracted(zenith(solar(twl_fl$twilight), path$lon[path$sta_id==i_s], path$lat[path$sta_id==i_s]))
168+
hist(z_i, freq = F, main = paste0("sta_id=", i_s, " n=", nrow(twl_fl)))
169+
lines(fit_z, col = "red")
165170
}
166171
}
167172

@@ -236,7 +241,8 @@ if (debug) {
236241
}
237242

238243
# Save ----
239-
save(twl,
244+
save(
245+
twl,
240246
light_prob,
241247
z,
242248
fit_z,

analysis/3-static.R

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ tmp[!is.na(tmp)] <- 0
8484
tmp[lat_calib_id, lon_calib_id] <- 1
8585
values(static_prob[[1]]) <- tmp
8686

87-
if (!is.na(gpr$calib_2_start)) {
87+
if (!is.na(gpr$calib_2_start) & abs(difftime(gpr$calib_2_end, gpr$crop_end, units = "days"))<3) {
8888
if (!is.na(gpr$calib_2_lat)) {
8989
lon_calib_id <- which.min(abs(gpr$calib_2_lon - lon))
9090
lat_calib_id <- which.min(abs(gpr$calib_2_lat - lat))
@@ -101,22 +101,67 @@ if (!is.na(gpr$calib_2_start)) {
101101
path <- geopressure_map2path(static_prob)
102102
static_timeserie <- geopressure_ts_path(path, pam$pressure)
103103

104-
105104
if (debug) {
106105
# GeopressureViz
107-
geopressureviz <- list(
106+
path_modified <- geopressureviz(
108107
pam = pam,
109108
static_prob = static_prob,
110109
pressure_prob = pressure_prob,
111110
light_prob = light_prob,
112111
pressure_timeserie = static_timeserie
113112
)
114-
save(geopressureviz, file = "~/geopressureviz.RData")
115-
116-
appDir <- system.file("geopressureviz", package = "GeoPressureR")
117-
shiny::runApp(appDir, launch.browser = getOption("browser"))
118113

114+
# Pressure
115+
path_modified_timeserie <- geopressure_ts_path(path_modified, pam$pressure)
116+
117+
path_modified_ts_bind <- do.call("rbind", path_modified_timeserie) %>%
118+
filter(!is.na(sta_id))
119+
120+
# To make the labeling easier, you can replace pam$pressure$obs by the difference between
121+
# pam$pressure$obs and pressure_timeserie$pressure0 to see the anomalies.
122+
# Because trainset_read does not read the actual value of obs, but simply the label, it won't
123+
# impact your code
124+
pam_diff <- pam
125+
pam_diff$pressure <- pam_diff$pressure %>%
126+
left_join(path_modified_ts_bind %>% dplyr::select(c("date","pressure0")), by="date") %>%
127+
rename(obs_ref = pressure0)
128+
129+
trainset_write(pam_diff, "data/1_pressure/labels/", filename = paste0(pam$id, "_act_pres"))
130+
131+
# We can automatically extract some outlier based on s value
132+
# pam_diff_pressure <- pam_diff_pressure %>%
133+
# mutate( isoutlier = ifelse(!isoutlier&abs(diff)>4*gpr$prob_map_s&sta_id>0, TRUE, isoutlier))
134+
# message(sum(pam_diff_pressure$isoutlier)-sum(pam$pressure$isoutlier), " new outlier automatically added")
135+
twl_path <- left_join(twl, path_modified) %>%
136+
mutate(
137+
twilight = twilight(twilight,
138+
lon = lon, lat = lat, rise = rise, zenith = 96)
139+
) %>%
140+
filter(!is.na(twilight))
141+
142+
write.csv(
143+
df <- rbind(
144+
data.frame(
145+
series = ifelse(twl$rise, "Rise", "Set"),
146+
timestamp = strftime(twl$twilight, "%Y-%m-%dT00:00:00Z", tz = "UTC"),
147+
value = (as.numeric(format(twl$twilight, "%H")) * 60 + as.numeric(format(twl$twilight, "%M"))
148+
+ gpr$shift_k / 60 + 60 * 12) %% (60 * 24),
149+
label = ifelse(is.na(twl$delete), "", ifelse(twl$delete, "Delete", ""))
150+
),
151+
data.frame(
152+
series = ifelse(twl_path$rise, "Set_ref", "Rise_ref"),
153+
timestamp = strftime(twl_path$twilight, "%Y-%m-%dT00:00:00Z", tz = "UTC"),
154+
value = (as.numeric(format(twl_path$twilight, "%H")) * 60 + as.numeric(format(twl_path$twilight, "%M"))
155+
+ gpr$shift_k / 60 + 60 * 12) %% (60 * 24),
156+
label = ""
157+
)
158+
),
159+
paste0("data/2_light/labels/", gpr$gdl_id, "_light.csv"),
160+
row.names = FALSE
161+
)
162+
}
119163

164+
if (debug) {
120165
# Check 1
121166
static_prob_n <- lapply(static_prob, function(x) {
122167
probt <- raster::as.matrix(x)

analysis/4-basic-graph.R

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ grl$p <- grl$ps * flight_prob(grl$gs, method = "gamma", shape = 7, scale = 7, lo
2525

2626

2727

28-
29-
30-
31-
32-
33-
3428
# Marginal map ----
3529
static_prob_marginal <- graph_marginal(grl)
3630

@@ -58,72 +52,15 @@ path_sim <- graph_simulation(grl, nj = nj)
5852

5953

6054
if (debug) {
61-
62-
# Rapid visual check
63-
sta_duration <- unlist(lapply(static_prob_marginal, function(x) {
64-
as.numeric(difftime(metadata(x)$temporal_extent[2], metadata(x)$temporal_extent[1], units = "days"))
65-
}))
66-
67-
m <- leaflet(width = "100%") %>%
68-
addProviderTiles(providers$Stamen.TerrainBackground) %>%
69-
addFullscreenControl()
70-
for (i in seq_len(nj)) {
71-
m <- m %>%
72-
addPolylines(lng = path_sim$lon[i, ], lat = path_sim$lat[i, ], opacity = 0.7, weight = 1, color = "#808080") %>%
73-
addCircles(lng = path_sim$lon[i, ], lat = path_sim$lat[i, ], opacity = 1, weight = 1, color = "#000")
74-
}
75-
76-
m %>%
77-
addPolylines(lng = shortest_path$lon, lat = shortest_path$lat, opacity = 1, color = "#808080", weight = 3) %>%
78-
addCircles(lng = shortest_path$lon, lat = shortest_path$lat, opacity = 1, color = "#000", weight = sta_duration^(0.3) * 10)
79-
80-
81-
# Light comparison
8255
load(paste0("data/2_light/", gdl, "_light_prob.Rdata"))
83-
raw_geolight <- pam$light %>%
84-
transmute(
85-
Date = date,
86-
Light = obs
87-
)
88-
lightImage(
89-
tagdata = raw_geolight,
90-
offset = gpr$shift_k / 60 / 60
91-
)
92-
tsimagePoints(twl$twilight,
93-
offset = -gpr$shift_k / 60 / 60, pch = 16, cex = 1.2,
94-
col = ifelse(twl$deleted, "grey20", ifelse(twl$rise, "firebrick", "cornflowerblue"))
95-
)
96-
for (ts in shortest_path_timeserie) {
97-
twl_fl <- twl %>%
98-
filter(twilight > ts$date[1] & twilight < tail(ts$date, 1))
99-
tsimageDeploymentLines(twl_fl$twilight,
100-
lon = ts$lon[1], ts$lat[1],
101-
offset = gpr$shift_k / 60 / 60, lwd = 3, col = adjustcolor("orange", alpha.f = 0.5)
102-
)
103-
}
104-
105-
# In depth analysis with GeoPressureViz
106-
load(paste0("data/1_pressure/", gdl, "_pressure_prob.Rdata"))
107-
sta_marginal <- unlist(lapply(static_prob_marginal, function(x) raster::metadata(x)$sta_id))
108-
sta_pres <- unlist(lapply(pressure_prob, function(x) raster::metadata(x)$sta_id))
109-
sta_light <- unlist(lapply(light_prob, function(x) raster::metadata(x)$sta_id))
110-
pressure_prob <- pressure_prob[sta_pres %in% sta_marginal]
111-
light_prob <- light_prob[sta_light %in% sta_marginal]
112-
113-
114-
geopressureviz <- list(
56+
geopressureviz(
11557
pam = pam,
11658
static_prob = static_prob,
11759
static_prob_marginal = static_prob_marginal,
11860
pressure_prob = pressure_prob,
11961
light_prob = light_prob,
12062
pressure_timeserie = shortest_path_timeserie
12163
)
122-
save(geopressureviz, file = "~/geopressureviz.RData")
123-
124-
shiny::runApp(system.file("geopressureviz", package = "GeoPressureR"),
125-
launch.browser = getOption("browser")
126-
)
12764
}
12865

12966

analysis/5-3-wind-graph_analyse.R

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,32 +56,14 @@ path_sim <- graph_simulation(grl, nj = nj)
5656

5757

5858
if (debug) {
59-
60-
# In depth analysis with GeoPressureViz
61-
load(paste0("data/1_pressure/", gdl, "_pressure_prob.Rdata"))
62-
load(paste0("data/2_light/", gdl, "_light_prob.Rdata"))
63-
load(paste0("data/3_static/", gdl, "_static_prob.Rdata"))
64-
65-
sta_marginal <- unlist(lapply(static_prob_marginal, function(x) raster::metadata(x)$sta_id))
66-
sta_pres <- unlist(lapply(pressure_prob, function(x) raster::metadata(x)$sta_id))
67-
sta_light <- unlist(lapply(light_prob, function(x) raster::metadata(x)$sta_id))
68-
pressure_prob <- pressure_prob[sta_pres %in% sta_marginal]
69-
light_prob <- light_prob[sta_light %in% sta_marginal]
70-
71-
72-
geopressureviz <- list(
59+
geopressureviz(
7360
pam = pam,
7461
static_prob = static_prob,
7562
static_prob_marginal = static_prob_marginal,
7663
pressure_prob = pressure_prob,
7764
light_prob = light_prob,
7865
pressure_timeserie = shortest_path_timeserie
7966
)
80-
save(geopressureviz, file = "~/geopressureviz.RData")
81-
82-
shiny::runApp(system.file("geopressureviz", package = "GeoPressureR"),
83-
launch.browser = getOption("browser")
84-
)
8567
}
8668

8769

0 commit comments

Comments
 (0)