Skip to content

Commit 81d8588

Browse files
committed
Merge branch 'dev' of https://github.com/KWB-R/lakeRS into dev
2 parents dc55201 + a0fe5cf commit 81d8588

6 files changed

Lines changed: 93 additions & 50 deletions

File tree

R/dynamic_per_pixel.R

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
#' @param threshold The mininum number of valid values for one pixel to be
1616
#' processed (the higher the days around moving averages are, the lower this
1717
#' threshold can be)
18-
#' @param maxPixels Maximum number of pixels (randomly chosen from available pixels).
19-
#' If Inf, all pixels will be analysed.
18+
#' @param maxPixels If the number is below 1 it will be interpreted as a
19+
#' proportion of all valid Pixels. Otherwise, maxPixels defines the number of
20+
#' pixels used to calculate the overall dynamic.If it is smaller as the number of
21+
#' valid pixels (revided by the threshold value), pixels will drawn in decreasing
22+
#' order from most valid to lowest valid. If Inf, all pixels will be analysed.
23+
#' MaxPixels is ignored if pixelFilter is defined.
2024
#' @param pixelFilter The ID of pixels to be used (The ID of a pixel is its
2125
#' its number in the the original matrix of the netcdf).
2226
#' @param maxDataPoints The number of datapoints to be part of one matrix
@@ -31,8 +35,6 @@
3135
#' are available. If the number of images is not sufficient the days_around_ma
3236
#' need to be increased and the results will become smoother.
3337
#'
34-
#'
35-
#'
3638
#' @export
3739
#'
3840
dynamic_per_pixel <- function(
@@ -55,18 +57,19 @@ dynamic_per_pixel <- function(
5557

5658
cat("Image data is filtered for SCL categories pixel by pixel ... \n")
5759
for(i in seq_along(indexList)){
58-
indexList[[i]] <- if(water_scenes_only){
59-
scl_filter(
60-
indexImage = indexList[[i]],
61-
sclImage = sclList[[i]],
62-
bands = 6,
63-
invert = TRUE)
64-
} else {
65-
scl_filter(
66-
indexImage = indexList[[i]],
67-
sclImage = sclList[[i]],
68-
bands = c(8:11))
69-
}
60+
indexList[[i]] <-
61+
if(water_scenes_only){
62+
scl_filter(
63+
indexImage = indexList[[i]],
64+
sclImage = sclList[[i]],
65+
bands = 6,
66+
invert = TRUE)
67+
} else {
68+
scl_filter(
69+
indexImage = indexList[[i]],
70+
sclImage = sclList[[i]],
71+
bands = c(8:11))
72+
}
7073
}
7174

7275
imageDOY <- as.numeric(format(imageIndex$t_date, "%j"))
@@ -78,19 +81,16 @@ dynamic_per_pixel <- function(
7881
if(!is.null(pixelFilter)){
7982
pixel_selection_i <- pixelFilter
8083
nAvailable <- np <- length(pixelFilter)
81-
maxPixels <- Inf
8284
filteredIndexList <- lapply(indexList, function(x){x[pixelFilter]})
8385
ts_select <- matrix(
8486
data = unlist(filteredIndexList),
8587
nrow = np,
8688
ncol = length(filteredIndexList)
8789
)
8890
} else {
89-
ts <- matrix(
90-
data = unlist(indexList),
91-
nrow = d[1] * d[2],
92-
ncol = length(indexList)
93-
)
91+
ts <- sapply(indexList, function(x){
92+
unlist(x)
93+
})
9494
valid_values <- apply(ts, 1 , function(p_ts){sum(!is.na(p_ts))})
9595
med_values <- median(valid_values[valid_values > 0])
9696
if(is.null(threshold)){
@@ -99,7 +99,17 @@ dynamic_per_pixel <- function(
9999
if(is.null(days_around_ma)){
100100
days_around_ma <- ceiling(365 / med_values * 3)
101101
}
102-
pixel_selection <- valid_values > threshold
102+
pixel_selection <- valid_values >= threshold
103+
if(maxPixels <= 1){
104+
maxPixels <- round(sum(pixel_selection) * maxPixels)
105+
}
106+
pixel_selection_i <-
107+
if(maxPixels < sum(pixel_selection)){
108+
order(valid_values, decreasing = TRUE)[1:maxPixels]
109+
} else {
110+
which(pixel_selection)
111+
}
112+
103113
if(sum(pixel_selection) == 0){
104114
return(
105115
list("moving_averages" = NULL,
@@ -111,12 +121,6 @@ dynamic_per_pixel <- function(
111121
)
112122
)
113123
}
114-
pixel_selection_i <- which(pixel_selection)
115-
nAvailable <- length(pixel_selection_i)
116-
if(nAvailable > maxPixels){
117-
pixel_selection_i <- sample(pixel_selection_i, maxPixels)
118-
nAvailable <- length(pixel_selection_i)
119-
}
120124
ts_select <- ts[pixel_selection_i,]
121125
rm(ts)
122126
}
@@ -146,7 +150,7 @@ dynamic_per_pixel <- function(
146150
}
147151

148152
cat(paste0(
149-
"Calculate moving averages of ", nAvailable, " pixel timesseries ... \n")
153+
"Calculate moving averages of ", length(pixel_selection_i), " pixel timesseries ... \n")
150154
)
151155
ipa <- images_per_ma(
152156
t_doy = t_doy,

R/load_BandLayer.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
load_BandLayer <- function(
1616
nc, band, monthFilter = NULL, yearFilter = NULL
1717
){
18+
cat(paste("Loading values of Band", band, "... \n"))
1819
if(!is.null(monthFilter)){
1920
mf <- as.integer(format(nc$t_date, "%m")) %in% as.integer(monthFilter)
2021
if(sum(mf) > 0){

R/ndi_per_image.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,13 @@ ndi_per_image <- function(
3939
# nc$SpRast[[IN]] <-
4040
# (nc$SpRast[[AN]] - nc$SpRast[[BN]]) / (nc$SpRast[[AN]] + nc$SpRast[[BN]])
4141
# }
42-
cat(paste("Loading values of Band", bandNames[1], "... \n"))
4342
At <- load_BandLayer(
4443
nc = nc,
4544
band = bandNames[1],
4645
monthFilter = monthFilter,
4746
yearFilter = year
4847
)
4948

50-
cat(paste("Loading values of Band", bandNames[2], "... \n"))
5149
Bt <- load_BandLayer(
5250
nc = nc,
5351
band = bandNames[2],

inst/extdata/scripts/example.R

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,35 @@ lakeRS::download_openEO_job(
5959
filePath <- "C:/Users/mzamzo/Documents/tmp/pcl/01_data/input/ndtri_openeo/Koerbaer Teich_lakeRS-example_2020-01-01_2021-01-01"
6060
filePath <- "C:/Users/mzamzo/Documents/tmp/pcl/01_data/input/ndtri_openeo/TrichonisLake_DS1_20190101-20200101"
6161

62+
# further input
63+
lakeName <-
64+
"Lake Trichonis"
65+
# "Körbaer Teich"
66+
lakeID <-
67+
"DS1"
68+
# "800015388119"
69+
indexName <-
70+
"NDTrI"
71+
#"NDCI"
72+
# "NDTI"
73+
# "NDSSI"
74+
75+
indexBands <-
76+
# c("B05", "B02")
77+
# c("B05", "B03")
78+
# c("B04", "B03")
79+
# c("B02", "B08")
80+
81+
#
6282
# open netcdf connection
6383
nc <- lakeRS::open_netcdf(filePath = filePath)
6484

6585
# Index per image
6686
range(nc$t_date)
6787
imageIndex <- lakeRS::ndi_per_image(
6888
nc = nc,
69-
year = 2020,
70-
bandNames = c("B05", "B02")
89+
year = 2019,
90+
bandNames = indexBands
7191
)
7292

7393
# yearly dynamic
@@ -83,18 +103,26 @@ imageIndex <- lakeRS::ndi_per_image(
83103
v_averageList = list(indexDynamic$lakeDynamic$q_0.5),
84104
df_q50List = list(indexDynamic$lakeDynamic[,c("q_0.25", "q_0.75")]),
85105
df_q95List = list(indexDynamic$lakeDynamic[,c("q_0.025", "q_0.975")]),
86-
lakeName = "Körbaer Teich",
87-
ylab = "NDTrI per timestep")
106+
lakeName = lakeName,
107+
ylab = paste0(indexName, " per timestep"))
108+
109+
lakeRS::plot_dynamic(
110+
v_averageList = list(indexDynamic$lakeDynamic$q_0.5),
111+
df_q50List = list(indexDynamic$lakeDynamic[,c("q_0.25", "q_0.75")]),
112+
df_q95List = NULL,
113+
lakeName = lakeName,
114+
ylab = paste0(indexName, " per timestep"))
88115

89116

90117
lakeRS::best_nk(pixelDynamic = indexDynamic$pixelDynamics)
118+
k <- 4
91119

92120
pClusters <- lakeRS::pixel_clusters(
93121
pixelDynamic = indexDynamic$pixelDynamics,
94122
nc = nc, correlate_first = TRUE,
95-
k = 3)
123+
k = k)
96124

97-
DynamicStatList <- lapply(1:3, function(CLUSTER){
125+
DynamicStatList <- lapply(1:k, function(CLUSTER){
98126
lakeRS::dynamic_per_pixel(
99127
imageIndex = imageIndex,
100128
nc = nc,
@@ -109,13 +137,11 @@ imageIndex <- lakeRS::ndi_per_image(
109137
lakeRS::plot_dynamic(
110138
v_averageList = lapply(DynamicStatList, function(x){x$q_0.5}),
111139
df_q50List = lapply(DynamicStatList, function(x){data.frame(x$q_0.25, x$q_0.75)}),
112-
lakeName = "Körbaer Teich",
113-
TSnames = c("C1", "C2", "C3")
140+
lakeName = lakeName,
141+
TSnames = paste0("C", 1:k)
114142
)
115143

116-
DynamicStatList = DynamicStatList,
117-
lakeName = "Körbaer Teich",
118-
smallBandOnly = TRUE)
144+
119145
}
120146

121147
# Temporal aggregation
@@ -140,7 +166,7 @@ if(FALSE){
140166
# spatial aggregation
141167
lakeIndex <- lakeRS::seasonal_index_per_lake(
142168
seasonIndex = seasonIndex,
143-
lakeName = "Koerbaer_teich",
169+
lakeName = lakeName,
144170
lakeID = "800015388119"
145171
)
146172

@@ -151,8 +177,8 @@ yearlyLakes <- lakeRS::combine_years_lakeData(lakeIndexList = list(lakeIndex))
151177
if(FALSE){
152178
lakeRS::plot_lake_index_histogram(
153179
lakeIndex = lakeIndex,
154-
lakeName = "Koerbaer_teich",
155-
indexName = "NDTrI"
180+
lakeName = lakeName,
181+
indexName = indexName
156182
)
157183

158184
# some plot as above
@@ -188,11 +214,23 @@ classData <- lakeRS::discreteClassAssessment(
188214
classColors = lakeRS::tenClassColors$color,
189215
plotLegend = FALSE)
190216
}
191-
classData <- lakeRS::numericAssessment(
217+
218+
numData <- lakeRS::numericAssessment(
192219
yearly_spread = yearlyPixels$indexTable
193220
)
194221

195222

223+
224+
225+
226+
227+
228+
229+
230+
231+
# -------------------------------------------------------
232+
# old
233+
196234
xScene <- lakeRS::nc_scene_per_image(
197235
nc = nc,
198236
scene = "water"

inst/extdata/scripts/example_manyYears.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ system.time(
55
nc <- lakeRS::open_netcdf(filePath = filePath)
66
)
77

8-
98
# Dynmaic
109
DynamicList <- list()
1110
years <- 2018:2025

man/dynamic_per_pixel.Rd

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)