Skip to content

Commit

Permalink
Fixed window(FLPar), does nothing if no year dimension
Browse files Browse the repository at this point in the history
  • Loading branch information
iagomosqueira committed Dec 10, 2024
1 parent 89bfd8a commit 1feee0d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: FLCore
Title: Core Package of FLR, Fisheries Modelling in R
Version: 2.6.20.9311
Version: 2.6.20.9313
Authors@R: c(
person("Iago", "Mosqueira", email = "iago.mosqueira@wur.nl",
role = "cre", comment=c(c(ORCID = "0000-0002-3252-0591"))),
Expand Down
19 changes: 11 additions & 8 deletions R/FLPar.R
Original file line number Diff line number Diff line change
Expand Up @@ -1011,37 +1011,40 @@ setMethod("window", signature(x="FLPar"),
function(x, start=as.numeric(dimnames(x)$year[1]),
end=as.numeric(dimnames(x)$year[length(dimnames(x)$year)]), extend=TRUE,
frequency=1) {
#

# DO nothing if no 'year' in object dimnames
if(!"year" %in% names(x))
stop("window can only be called to objects with a 'year' dimension")
return(x)

# get original min and max
# GET original min and max
yrs <- dimnames(x)$year
min <- as.numeric(yrs[1])
max <- as.numeric(yrs[length(yrs)])
pos <- match("year", names(x))

# if extend=FALSE and end/start ask for it, error
# IF extend=FALSE and end/start ask for it, error
if(!extend && (start < min | end > max))
stop("FLPar to be extended but extend=FALSE")

# if extend is a number, added to end
if(is.numeric(extend))
# IF extend is a number, added to end
if(is.numeric(extend)) {
if (missing(end))
end <- max + extend
else
stop("'extend' is numeric and 'end' provided, don't know what to do")
}

# construct new FLPar
dnames <- dimnames(x)
dnames[[pos]] <- seq(start, end, by=frequency)
res <- do.call(class(x), list(NA, units=units(x), dimnames=dnames))

# add data for matching years
dnames <- dimnames(x)[pos]
dnames <- list(intersect(dimnames(res)[[pos]], dimnames(x)[[pos]]))
names(dnames) <- c('i', 'j', 'k', 'l', 'm', 'n')[pos]

do.call('[<-', c(list(x=res, value=x), dnames))
do.call('[<-', c(list(x=res, value=do.call('[', c(list(x=x), dnames))),
dnames))
}
) # }}}

Expand Down

0 comments on commit 1feee0d

Please sign in to comment.