diff --git a/R/CDORemap.R b/R/CDORemap.R index 7d6ff39157809f7b28709d5bc4207766e2d620b8..f3909baefe83817c6e2a2779f854ccbe82a10638 100644 --- a/R/CDORemap.R +++ b/R/CDORemap.R @@ -42,11 +42,24 @@ CDORemap <- function(data_array = NULL, lons, lats, grid, method, return_array <- FALSE if (length(dim(lons)) == 1) { array_dims <- c(length(lats), length(lons)) - names(array_dims) <- c('lat', 'lon') + new_lon_dim_name <- 'lon' + new_lat_dim_name <- 'lat' } else { array_dims <- dim(lons) - names(array_dims) <- c('j', 'i') + new_lon_dim_name <- 'i' + new_lat_dim_name <- 'j' } + if (!is.null(names(dim(lons)))) { + if (any(known_lon_names %in% names(dim(lons)))) { + new_lon_dim_name <- known_lon_names[which(known_lon_names %in% names(dim(lons)))[1]] + } + } + if (!is.null(names(dim(lats)))) { + if (any(known_lat_names %in% names(dim(lats)))) { + new_lat_dim_name <- known_lat_names[which(known_lat_names %in% names(dim(lats)))[1]] + } + } + names(array_dims) <- c(new_lat_dim_name, new_lon_dim_name) data_array <- array(NA, array_dims) } if (!(is.logical(data_array) || is.numeric(data_array)) || !is.array(data_array)) { @@ -79,16 +92,7 @@ CDORemap <- function(data_array = NULL, lons, lats, grid, method, } } else { if (!(lon_dim %in% names(dim(lons)))) { - if (!return_array) { - if (any(names(dim(lons)) %in% known_lon_names)) { - lon_dim <- names(dim(lons))[which(names(dim(lons)) %in% known_lon_names)] - names(dim(data_array))[2] <- lon_dim - } else { - stop("Parameter 'lon' must have a longitude dimension.") - } - } else { - stop("Parameter 'lon' must have the same longitude dimension name as the 'data_array'.") - } + stop("Parameter 'lon' must have the same longitude dimension name as the 'data_array'.") } if (length(dim(lons)) > 1 && !(lat_dim %in% names(dim(lons)))) { stop("Parameter 'lon' must have the same latitude dimension name as the 'data_array'.") @@ -102,16 +106,7 @@ CDORemap <- function(data_array = NULL, lons, lats, grid, method, } } else { if (!(lat_dim %in% names(dim(lats)))) { - if (!return_array) { - if (any(names(dim(lats)) %in% known_lat_names)) { - lat_dim <- names(dim(lats))[which(names(dim(lats)) %in% known_lat_names)] - names(dim(data_array))[1] <- lat_dim - } else { - stop("Parameter 'lat' must have a latitude dimension.") - } - } else { - stop("Parameter 'lat' must have the same latitude dimension name as the 'data_array'.") - } + stop("Parameter 'lat' must have the same latitude dimension name as the 'data_array'.") } if (length(dim(lats)) > 1 && !(lon_dim %in% names(dim(lats)))) { stop("Parameter 'lat' must have the same longitude dimension name as the 'data_array'.") @@ -223,7 +218,8 @@ CDORemap <- function(data_array = NULL, lons, lats, grid, method, tmp_lon <- Subset(lons, lat_dim, min_pos[which(names(dim(lons)) == lat_dim)], drop = 'selected') } i <- 1:length(tmp_lon) - lon_model <- lm(tmp_lon ~ poly(i, 3)) + degree <- min(3, length(i) - 1) + lon_model <- lm(tmp_lon ~ poly(i, degree)) lon_extremes <- c(NA, NA) left_is_min <- FALSE right_is_max <- FALSE @@ -281,7 +277,8 @@ CDORemap <- function(data_array = NULL, lons, lats, grid, method, tmp_lat <- Subset(lats, lon_dim, min_pos[which(names(dim(lats)) == lon_dim)], drop = 'selected') } i <- 1:length(tmp_lat) - lat_model <- lm(tmp_lat ~ poly(i, 3)) + degree <- min(3, length(i) - 1) + lat_model <- lm(tmp_lat ~ poly(i, degree)) lat_extremes <- c(NA, NA) if (which.min(tmp_lat) == 1) { prev_lat <- predict(lat_model, data.frame(i = 0))