Skip to content
This repository has been archived by the owner on Dec 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #19 from EcoJulia/fix/syntax
Browse files Browse the repository at this point in the history
Update the syntax internally to be more consistent
  • Loading branch information
tpoisot authored Apr 10, 2020
2 parents 17db192 + cf7b051 commit 4cf7818
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 105 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SimpleSDMLayers"
uuid = "2c645270-77db-11e9-22c3-0f302a89c64c"
authors = ["Timothée Poisot <[email protected]>"]
version = "0.2.1"
version = "0.2.2"

[deps]
GDAL = "add2ef01-049f-52c4-9ee2-e494f65e021a"
Expand Down
2 changes: 1 addition & 1 deletion src/SimpleSDMLayers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export coarsen
include(joinpath("recipes", "recipes.jl"))

# HACK but this fixes the export of clip when GBIF or others are loaded
clip(p::T) where {T <: SimpleSDMLayer} = nothing
clip(::T) where {T <: SimpleSDMLayer} = nothing
export clip

function __init__()
Expand Down
17 changes: 9 additions & 8 deletions src/bioclimaticdata/worldclim.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
worldclim(layers::Vector{Int64}; resolution::AbstractString="10", path::AbstractString="assets")
worldclim(layers::Vector{T}; resolution::AbstractString="10", path::AbstractString="assets") where {T <: Integer}
Download and prepare WorldClim 2.0 bioclimatic variables, and returns them as an
array of `SimpleSDMPredictor`s. Layers are called by their number, from 1 to 19.
Expand Down Expand Up @@ -41,10 +41,11 @@ will be much faster.
| 19 | Precipitation of Coldest Quarter |
"""
function worldclim(layers::Vector{Int64}; resolution::AbstractString="10", path::AbstractString="assets")
@assert all(1 .≤ layers .≤ 19)
function worldclim(layers::Vector{T}; resolution::AbstractString="10", path::AbstractString="assets") where {T <: Integer}
all(1 .≤ layers .≤ 19) || throw(ArgumentError("The number of the layers must all be between 1 and 19"))
isdir(path) || mkdir(path)
@assert resolution ["2.5", "5", "10"]
resolution ["2.5", "5", "10"] || throw(ArgumentError("The resolution argument ($(resolution) must be 2.5, 5, or 10"))

codes = [lpad(code, 2, "0") for code in layers]
paths = [joinpath(path, "wc2.0_bio_$(resolution)m_$(code).tif") for code in codes]

Expand Down Expand Up @@ -85,15 +86,15 @@ function worldclim(layers::Vector{Int64}; resolution::AbstractString="10", path:
end

"""
worldclim(layer::Int64; x...)
worldclim(layer::T; x...) where {T <: Integer}
Return a single layer from WorldClim 2.0.
"""
worldclim(layer::Int64; x...) = worldclim([layer]; x...)[1]
worldclim(layer::T; x...) where {T <: Integer} = first(worldclim([layer]; x...))

"""
worldclim(layers::UnitRange{Int64}; x...)
worldclim(layers::UnitRange{T}; x...) where {T <: Integer}
Return a range of layers from WorldClim 2.0.
"""
worldclim(layers::UnitRange{Int64}; x...) = worldclim(collect(layers); x...)
worldclim(layers::UnitRange{T}; x...) where {T <: Integer} = worldclim(collect(layers); x...)
36 changes: 22 additions & 14 deletions src/lib/basics.jl
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
"""
latitudes(p::T) where {T <: SimpleSDMLayer}
latitudes(layer::T) where {T <: SimpleSDMLayer}
Returns an iterator with the latitudes of the SDM layer passed as its argument.
This returns the latitude at the center of each cell in the grid.
"""
function latitudes(p::T) where {T <: SimpleSDMLayer}
grid_size = stride(p; dims=2)
centers = range(p.bottom+grid_size; stop=p.top-grid_size, length=size(p, 1))
function latitudes(layer::T) where {T <: SimpleSDMLayer}
grid_size = stride(layer; dims=2)
centers = range(layer.bottom+grid_size; stop=layer.top-grid_size, length=size(layer, 1))
return centers
end

"""
longitudes(p::T) where {T <: SimpleSDMLayer}
longitudes(layer::T) where {T <: SimpleSDMLayer}
Returns an iterator with the longitudes of the SDM layer passed as its argument.
This returns the longitudes at the center of each cell in the grid.
"""
function longitudes(p::T) where {T <: SimpleSDMLayer}
grid_size = stride(p; dims=1)
centers = range(p.left+grid_size; stop=p.right-grid_size, length=size(p, 2))
function longitudes(layer::T) where {T <: SimpleSDMLayer}
grid_size = stride(layer; dims=1)
centers = range(layer.left+grid_size; stop=layer.right-grid_size, length=size(layer, 2))
return centers
end

function are_compatible(l1::FT, l2::ST) where {FT <: SimpleSDMLayer, ST <: SimpleSDMLayer}
@assert size(l1) == size(l2)
@assert l1.top == l2.top
@assert l1.left == l2.left
@assert l1.bottom == l2.bottom
@assert l1.right == l2.right
"""
_layers_are_compatible(l1::X, l2::Y) where {X <: SimpleSDMLayer, Y <: SimpleSDMLayer}
This returns
"""
function _layers_are_compatible(l1::X, l2::Y) where {X <: SimpleSDMLayer, Y <: SimpleSDMLayer}
size(l1) == size(l2) || throw(ArgumentError("The layers have different sizes"))
l1.top == l2.top || throw(ArgumentError("The layers have different top coordinates"))
l1.left == l2.left || throw(ArgumentError("The layers have different left coordinates"))
l1.bottom == l2.bottom || throw(ArgumentError("The layers have different bottom coordinates"))
l1.right == l2.right || throw(ArgumentError("The layers have different right coordinates"))
end
6 changes: 6 additions & 0 deletions src/lib/geotiff.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"""
geotiff(tiff_file; T::Type=Float64)
The geotiff function reads a geotiff file, and returns it as a matrix of type
`T`.
"""
function geotiff(tiff_file; T::Type=Float64)
# Register GDAL drivers
GDAL.gdalallregister()
Expand Down
Loading

2 comments on commit 4cf7818

@tpoisot
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/12711

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.2 -m "<description of version>" 4cf7818d4fbda762ee3338efcdd88218e41dd155
git push origin v0.2.2

Please sign in to comment.