Skip to content

Commit 030e843

Browse files
Malte ZamzowMalte Zamzow
authored andcommitted
Bug Fixing
1 parent 128f331 commit 030e843

9 files changed

Lines changed: 86 additions & 21 deletions

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ URL: https://github.com/KWB-R/lakeRS
1313
BugReports: https://github.com/KWB-R/lakeRS/issues
1414
Encoding: UTF-8
1515
Roxygen: list(markdown = TRUE)
16-
RoxygenNote: 7.3.2
16+
RoxygenNote: 7.3.3
1717
Suggests:
1818
covr,
1919
sf

R/dynamic_per_pixel.R

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ dynamic_per_pixel <- function(
9090

9191
ts_start <- ts[pixel_selection_i,]
9292
rm(ts)
93+
equal_size_nRow <- ceiling(maxDataPoints / dim(ts_start)[2])
94+
nm <- ceiling(dim(ts_start)[1]/equal_size_nRow)
9395
if(prod(dim(ts_start)) > maxDataPoints){
94-
equal_size_nRow <- ceiling(maxDataPoints / dim(ts_start)[2])
95-
nm <- ceiling(dim(ts_start)[1]/equal_size_nRow)
9696
cat(paste0(paste0(
9797
"Due to large matrix size, data is splitted into ",
9898
nm, " small matrices ... \n")
@@ -108,7 +108,9 @@ dynamic_per_pixel <- function(
108108
ts_start <- ts_parts
109109
rm(ts_parts)
110110
gc()
111-
}
111+
} else {
112+
ts_start <- list(ts_start)
113+
}
112114

113115
cat(paste0(
114116
"Calculate moving averages of ", nAvailable, " pixel timesseries ... \n")
@@ -128,9 +130,14 @@ dynamic_per_pixel <- function(
128130
df_out[,-grep(pattern = "doy", colnames(df_out))[-1]]
129131
})
130132
cat(" | all matrixes done \n")
133+
134+
if(length(df_out) > 1L){
135+
df_out <- do.call(cbind, df_out)
136+
df_out <- df_out[,-grep(pattern = "doy", colnames(df_out))[-1]]
137+
} else {
138+
df_out <- df_out[[1]]
139+
}
131140

132-
df_out <- do.call(cbind, df_out)
133-
df_out <- df_out[,-grep(pattern = "doy", colnames(df_out))[-1]]
134141
colnames(df_out)[2:ncol(df_out)] <- paste0("p_", 1:(ncol(df_out)-1))
135142

136143
i_col <- ceiling(pixel_selection_i/ d[1])

R/load_netcdf.R

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#' @param vars A character defining the variables of the netcdf to be loaded.
77
#' The default is B02, B05 and SCL which are needed for NDTrI calculation.
88
#' If "all" is specified, all available variables are loaded.
9-
#' @start_xy,count_xy The first value and the length of the from netCDF
9+
#' @param start_xy,count_xy The first value and the length of the from netCDF
1010
#' extracted location values in x and y direciton. If count_xy is c(NA,NA), all
1111
#' values are loaded. If it is NA for only one axis all values in this direction
1212
#' are loaded. These arguments can be used for very large files, where
@@ -17,12 +17,12 @@
1717
#' the data cube.
1818
#'
1919
#' @importFrom ncdump NetCDF
20-
#' @importFrom raster raster crs brick
20+
#' @importFrom raster raster crs brick extend crop
2121
#'
2222
#' @export
2323
load_netcdf <- function(
2424
filePath, fileName = "openEO.nc", vars = c("B02", "B05", "SCL"),
25-
start_xy = c(1,1), count_xy = c(NA,NA)
25+
start_xy = c(10,10), count_xy = c(NA,NA)
2626
){
2727
ncMeta <- ncdump::NetCDF(x = file.path(filePath, fileName))
2828
if(any(is.na(count_xy))){
@@ -34,21 +34,42 @@ load_netcdf <- function(
3434
count_xy[2] <- ncMeta$dimension$len[ncMeta$dimension$name == "y"]
3535
}
3636
}
37+
38+
to_many_counts <- c(dimDf$len[dimDf$name == "x"], dimDf$len[dimDf$name == "y"]) -
39+
(count_xy + start_xy - 1)
40+
if(any(to_many_counts < 0)){
41+
count_xy[to_many_counts < 0] <- count_xy[to_many_counts < 0] + to_many_counts[to_many_counts < 0]
42+
}
43+
44+
crop_nc <- FALSE
45+
v_crop <- c(
46+
"xmin" = start_xy[1],
47+
"xmax" = start_xy[1] + count_xy[1] - 1,
48+
"ymin" = start_xy[2],
49+
"ymax" = start_xy[2] + count_xy[2] - 1
50+
)
51+
3752
availableVars <- ncMeta$variable$name
3853
availableVars <- availableVars[availableVars != "crs"]
3954
if("all" %in% vars){
4055
vars <- availableVars
4156
}
57+
4258
dimDf <- ncMeta$dimension
59+
if(count_xy[1] < dimDf$len[dimDf$name == "x"] |
60+
count_xy[2] < dimDf$len[dimDf$name == "y"]){
61+
crop_nc <- TRUE
62+
}
63+
4364
x_id <- dimDf$id[dimDf$name == "x"]
4465
y_id <- dimDf$id[dimDf$name == "y"]
4566
t_id <- dimDf$id[dimDf$name == "t"]
4667

4768
valDf <- ncMeta$dimension_values
4869
cat(paste0("Loading Coordinates and time variables ... \n"))
4970
nc <- list()
50-
nc[["x"]] <- valDf$vals[valDf$id == x_id][start_xy[1]:(start_xy + count_xy - 1)[1]]
51-
nc[["y"]] <- valDf$vals[valDf$id == y_id][start_xy[2]:(start_xy + count_xy - 1)[2]]
71+
nc[["x"]] <- valDf$vals[valDf$id == x_id]
72+
nc[["y"]] <- valDf$vals[valDf$id == y_id]
5273
nc[["t"]] <- valDf$vals[valDf$id == t_id]
5374
nc[["t_date"]] <- as.Date(nc[["t"]], origin = "1990-01-01")
5475

@@ -68,7 +89,15 @@ load_netcdf <- function(
6889
x = file.path(filePath, fileName),
6990
varname = varName
7091
)
71-
}
92+
if(crop_nc){
93+
bb_crop <- c(
94+
range(nc$x[v_crop[1:2]]),
95+
range(nc$y[v_crop[3:4]])
96+
)
97+
e <- raster::extent(bb_crop)
98+
nc[[varName]] <- raster::crop(nc[[varName]], e)
99+
}
100+
}
72101
nc[["crs"]] <- crsInfo
73102
} else {
74103
nc <- NULL

lakeRS.Rproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,3 @@ LaTeX: pdfLaTeX
1616
BuildType: Package
1717
PackageUseDevtools: Yes
1818
PackageInstallArgs: --no-multiarch --with-keep.source
19-
PackageRoxygenize: rd,collate,namespace

man/best_nk.Rd

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

man/dynamic_per_pixel.Rd

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

man/load_netcdf.Rd

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/pixel_clusters.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plot_dynamic.Rd

Lines changed: 14 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)