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
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# '
3840dynamic_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 ,
0 commit comments