Skip to content

Commit 90f8f56

Browse files
committed
Conflicts solved (the best from both changes)
Merge branch 'dev' of https://github.com/KWB-R/lakeRS into dev # Conflicts: # R/dynamic_per_pixel.R
2 parents 4fc0a43 + c91d979 commit 90f8f56

2 files changed

Lines changed: 65 additions & 55 deletions

File tree

R/dynamic_per_pixel.R

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#'
2828
#' @export
2929
#'
30-
#'
3130
dynamic_per_pixel <- function(
3231
ncImage, t_date, years, water_scenes_only = TRUE, days_around_ma = 20,
3332
maxPixels = 1000, threshold = NULL, lakeInfo = c("", ""), maxDataPoints = 5000000
@@ -42,22 +41,31 @@ dynamic_per_pixel <- function(
4241

4342
indexImages <- ncImage$RSindex[yearFilter]
4443
imageDOY <- imageDOY[yearFilter]
45-
d <- dim(indexImages[[1]])
46-
47-
sclImage <- ncImage$SCL[yearFilter]
44+
sclImages <- ncImage$SCL[yearFilter]
4845

49-
cat("Image data is filtered pixel by pixel ... \n")
46+
d <- dim(indexImages[[1]])
5047

51-
if(water_scenes_only){
52-
indexImages <- lapply(seq_along(indexImages), function(i){
53-
indexImages[[i]][sclImage[[i]] != 6] <- NA
54-
indexImages[[i]]
55-
})
56-
} else {
57-
indexImages <- lapply(seq_along(indexImages), function(i){
58-
indexImages[[i]][sclImage[[i]] %in% c(8:11)] <- NA
59-
indexImages[[i]]
60-
})
48+
# rm(ncImage)
49+
# gc()
50+
# if(object.size(ncImage)/1E+09 > 10){
51+
#
52+
# }
53+
54+
cat("Image data is filtered for SCL categories pixel by pixel ... \n")
55+
56+
for(i in seq_along(indexImages)){
57+
indexImages[[i]] <- if(water_scenes_only){
58+
scl_filter(
59+
indexImage = indexImages[[i]],
60+
sclImage = sclImages[[i]],
61+
bands = 6,
62+
invert = TRUE)
63+
} else {
64+
scl_filter(
65+
indexImage = indexImages[[i]],
66+
sclImage = sclImages[[i]],
67+
bands = c(8:11))
68+
}
6169
}
6270

6371
t_doy <- imageDOY[order(imageDOY)]
@@ -81,6 +89,19 @@ dynamic_per_pixel <- function(
8189

8290
valid_values <- apply(ts, 1 , function(p_ts){sum(!is.na(p_ts))})
8391
pixel_selection <- valid_values > threshold
92+
93+
if(sum(pixel_selection) == 0){
94+
return(
95+
list("moving_averages" = NULL,
96+
"raster_location" = data.frame(
97+
"pixel" = NA,
98+
"i_col" = 0,
99+
"i_row" = 0,
100+
"valid_values" = 0)
101+
)
102+
)
103+
}
104+
84105
pixel_selection_i <- which(pixel_selection)
85106
nAvailable <- length(pixel_selection_i)
86107
if(nAvailable > maxPixels){
@@ -119,7 +140,7 @@ dynamic_per_pixel <- function(
119140
t_doy = t_doy,
120141
days_around_ma = days_around_ma
121142
)
122-
143+
123144
cat(paste0("Processing ", nm+1, " matrices ... \n"))
124145
df_out <- lapply(ts_start, function(ts){
125146
cat(" | matrix done")
@@ -156,7 +177,6 @@ dynamic_per_pixel <- function(
156177
)
157178
}
158179

159-
160180
#' Subset of all images to be used for moving averages for each day
161181
#'
162182
#' @param t_doy Numeric vector of the day of the year according to ts_pixel
@@ -166,7 +186,7 @@ dynamic_per_pixel <- function(
166186
#'
167187
images_per_ma <- function(t_doy, days_around_ma){
168188
lapply(1:365, function(doy){
169-
d_range <- doy + c(-20, 20)
189+
d_range <- doy + c(-days_around_ma, days_around_ma)
170190
d_range[d_range < 1] <- d_range[d_range < 1] + 365
171191
d_range[d_range > 365] <- d_range[d_range > 365] - 365
172192

@@ -178,31 +198,18 @@ images_per_ma <- function(t_doy, days_around_ma){
178198
})
179199
}
180200

181-
#' Moving average for a sorted pixel vector with according day of the year time
182-
#' vector
201+
#' Set values NA by SCL
183202
#'
184-
#' @param ts_pixel Data for one pixel of multiple images (ts)
185-
#' @param t_doy Numeric vector of the day of the year according to ts_pixel
186-
#' (same length and order)
187-
#' @param days_around_ma The days around the moving average day (i.e. 10 means
188-
#' 10 days before and ten days after the actual day are used for averaging)
203+
#' @param indexImage,sclImage Index and SCL layer of the image
204+
#' @param bands Numeric vector specifiyng the SCL categories to filtered for
205+
#' @param invert If TRUE the filtering is inverted -> specefied bands are removed
189206
#'
190-
#'
191-
# moving_average <- function(ts_pixel, t_doy, days_around_ma){
192-
# df_out <- data.frame("doy" = 1:365)
193-
#
194-
# df_out$ma <- sapply(df_out$doy, function(doy){
195-
# d_range <- doy + c(-days_around_ma, days_around_ma)
196-
# d_range[d_range < 1] <- d_range[d_range < 1] + 365
197-
# d_range[d_range > 365] <- d_range[d_range > 365] - 365
198-
#
199-
# days_for_ma <- if(d_range[2] > d_range[1]){
200-
# t_doy > d_range[1] & t_doy < d_range[2]
201-
# } else {
202-
# t_doy > d_range[1] | t_doy < d_range[2]
203-
# }
204-
# mean(ts_pixel[days_for_ma], na.rm = TRUE)
205-
# })
206-
# df_out
207-
# }
208-
207+
scl_filter <- function(indexImage, sclImage, bands, invert = FALSE){
208+
if(invert){
209+
indexImage[!(sclImage %in% bands)] <- NA
210+
} else {
211+
indexImage[!(sclImage %in% bands)] <- NA
212+
}
213+
indexImage
214+
}
215+
>>>>>>> c91d979d3f068e2d74c8eac59c874cbb27f57666

R/load_netcdf.R

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,21 @@ load_netcdf <- function(
2525
start_xy = c(10,10), count_xy = c(NA,NA)
2626
){
2727
ncMeta <- ncdump::NetCDF(x = file.path(filePath, fileName))
28+
dimDf <- ncMeta$dimension
29+
xLen <- dimDf$len[dimDf$name == "x"]
30+
yLen <- dimDf$len[dimDf$name == "y"]
2831
if(any(is.na(count_xy))){
2932
all_values <- c("x", "y")[which(is.na(count_xy))]
3033
if("x" %in% all_values){
31-
count_xy[1] <- ncMeta$dimension$len[ncMeta$dimension$name == "x"]
34+
count_xy[1] <- xLen
3235
}
3336
if("y" %in% all_values){
34-
count_xy[2] <- ncMeta$dimension$len[ncMeta$dimension$name == "y"]
37+
count_xy[2] <- yLen
3538
}
3639
}
3740

38-
dimDf <- ncMeta$dimension
39-
to_many_counts <- c(dimDf$len[dimDf$name == "x"], dimDf$len[dimDf$name == "y"]) -
40-
(count_xy + start_xy - 1)
41+
42+
to_many_counts <- c(xLen, yLen) - (count_xy + start_xy - 1)
4143
if(any(to_many_counts < 0)){
4244
count_xy[to_many_counts < 0] <- count_xy[to_many_counts < 0] + to_many_counts[to_many_counts < 0]
4345
}
@@ -56,8 +58,7 @@ load_netcdf <- function(
5658
vars <- availableVars
5759
}
5860

59-
if(count_xy[1] < dimDf$len[dimDf$name == "x"] |
60-
count_xy[2] < dimDf$len[dimDf$name == "y"]){
61+
if(count_xy[1] < xLen |count_xy[2] < yLen){
6162
crop_nc <- TRUE
6263
}
6364

@@ -66,10 +67,12 @@ load_netcdf <- function(
6667
t_id <- dimDf$id[dimDf$name == "t"]
6768

6869
valDf <- ncMeta$dimension_values
70+
initial_x <- valDf$vals[valDf$id == x_id]
71+
initial_y <- valDf$vals[valDf$id == y_id]
6972
cat(paste0("Loading Coordinates and time variables ... \n"))
7073
nc <- list()
71-
nc[["x"]] <- valDf$vals[valDf$id == x_id]
72-
nc[["y"]] <- valDf$vals[valDf$id == y_id]
74+
nc[["x"]] <- valDf$vals[valDf$id == x_id][start_xy[1]:(start_xy + count_xy - 1)[1]]
75+
nc[["y"]] <- valDf$vals[valDf$id == y_id][start_xy[2]:(start_xy + count_xy - 1)[2]]
7376
nc[["t"]] <- valDf$vals[valDf$id == t_id]
7477
nc[["t_date"]] <- as.Date(nc[["t"]], origin = "1990-01-01")
7578

@@ -91,8 +94,8 @@ load_netcdf <- function(
9194
)
9295
if(crop_nc){
9396
bb_crop <- c(
94-
range(nc$x[v_crop[1:2]]),
95-
range(nc$y[v_crop[3:4]])
97+
range(initial_x[v_crop[1:2]]),
98+
range(initial_y[v_crop[3:4]])
9699
)
97100
e <- raster::extent(bb_crop)
98101
nc[[varName]] <- raster::crop(nc[[varName]], e)

0 commit comments

Comments
 (0)