Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more flexible + better benchmarking + Julia 1.x compliant #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions src/ten.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@ ten(d::Real, m::Real=0, s::Real=0) =

# TODO: improve performance, if possible. There are a couple of slow tests to
# make parsing of the string work.
function ten(strng::AbstractString)
# Convert strings into numbers, empty strings into 0s.
# Replace in the string multiple spaces or colons with a single space, strip leading
# whitespaces, and split the resulting string using the space as separator.
tmp = map(x-> x=="" ? 0.0 : parse(Float64, x),
split(lstrip(replace(strng, r"(\:| )+" => s" ")), " "))
# Concatenate "tmp" with 3 zeros, so that "angle" has at least 3 elements
# also with an empty string in input.
angle = vcat(tmp, zeros(Float64, 3))::Vector{Float64}
ten(angle[1], angle[2], angle[3])
function ten(coordinate::AbstractString)
lowercase(coordinate) == "missing" && return missing
coord_strip = replace(uppercase(coordinate), r"[NSEW]" => "")
split_coord = parse.(Float32, split(coord_strip, r"\s|:"))
split_coord[2] /=60.0
if length(split_coord) == 3
split_coord[3] /=3600.0
end
conv = sum(abs.(split_coord))
# N + E are positive | S + W are negative
if split_coord[1] < 0 || occursin(r"[SW]", uppercase(coordinate))
# negative
return conv * -1
else
# positive
return conv
end
end

ten(itr) = ten(itr...)
Expand All @@ -45,6 +52,12 @@ iterable like `(deg, min, sec)` or `[deg, min, sec]` is accepted as argument.

If minutes and seconds are not specified they default to zero.

### Formatting requirements
- Coordinates as a `String` separated by spaces (`"11 43 41"`) or colons (`"11:43:41"`)
- Must use negative sign (`"-11 43.52"`) or single-letter cardinal direction (`"11 43.52W"`)
- Missing data should be coded as the string `"missing"` (can be accomplished with `replace!()`)
- Can mix colons and spaces (although it's bad practice

### Output ###

The decimal conversion of the sexagesimal numbers provided is returned.
Expand Down