Skip to content

Commit

Permalink
Fix bug and improve documentation in define_domain()
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-MET committed Feb 2, 2024
1 parent 5ea4f8c commit d1eb4c4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Imports:
cli,
dplyr (>= 1.1),
glue,
lifecycle,
magrittr,
matrixStats,
meteogrid,
Expand Down
32 changes: 21 additions & 11 deletions R/geo_transform.R
Original file line number Diff line number Diff line change
Expand Up @@ -1307,22 +1307,21 @@ tranverse_mercator <- function(ref_lon, ref_lat, tilt) {
#'
#' This function is used to define a new domain with a regular grid. At a
#' minimum, the projection, geographic location of the centre of the domain and
#' number of horizontal resolution of the grid points must be provided.
#' number and horizontal resolution of the grid points must be provided.
#'
#' @param proj The projection of the domain. See details.
#' @param centre_lon The longitude of the centre of the domain in decimal
#' degrees.
#' @param centre_lat The latitude of the centre of the domain in decimal
#' degrees.
#' @param proj The projection of the domain. This can be the name of the
#' projection or a projection string.
#' @param centre_lon,centre_lat The longitude and latitude of the centre of the
#' domain in decimal degrees.
#' @param nxny The number of grid points in the x and y directions. Should be a
#' vector of length 2 with the number of grid points in the x direction first.
#' If only 1 value is given it is assumed to be the same in both directions.
#' @param dxdy The horizontal resolution of the grid in the x and y directions.
#' For lat-lon projections this should be in decimal degrees, otherwise should
#' be in metres. Should be a vector of length 2 with the resolution in the x
#' direction first.
#' @param ref_lon The reference longitude of the projection if needed.
#' @param ref_lat The reference latitude of the projection if needed.
#' @param ref_lon,ref_lat The reference longitude and latitude of the projection
#' if relevant to the projection. Ignored if `proj` is a projection string.
#' @param exey If defining a grid with an extension zone, a vector length 2 with
#' the number of grid points in the x and y directions of the extension zone.
#' If only 1 value is given it is assumed to be the same in both directions.
Expand All @@ -1340,6 +1339,13 @@ tranverse_mercator <- function(ref_lon, ref_lat, tilt) {
#'
#' dd <- define_domain(0, 0, c(360, 180), 1, "latlong") # Whole earth
#' plot(dd)
#'
#' # Pass the projection as a proj string
#' dd <- define_domain(
#' 10, 60, 1000, 2500,
#' proj = "+proj=lcc +lon_0=15 +lat_0=63.3 +lat_1=63.3 +lat_2=63.3 +R=6371000"
#' )
#' plot(dd)
define_domain <- function(
centre_lon,
centre_lat,
Expand All @@ -1359,9 +1365,13 @@ define_domain <- function(
) {

is_proj_str <- FALSE
if (substring(proj, 1, 6) == "+proj=") {
is_proj_str <- TRUE
} else {
projTry <- try(match.arg(proj), silent = TRUE)
if (inherits(projTry, "try-error")) {
if (length(proj) == 1 && substring(proj, 1, 6) == "+proj=") {
is_proj_str <- TRUE
}
}
if (!is_proj_str) {
proj <- match.arg(proj)
proj <- switch(
proj,
Expand Down
24 changes: 14 additions & 10 deletions man/define_domain.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d1eb4c4

Please sign in to comment.