Skip to content

Commit

Permalink
Merge pull request #3 from JuliaAstro/ml/formats
Browse files Browse the repository at this point in the history
  • Loading branch information
mileslucas committed Apr 24, 2021
2 parents 506fa5d + 78a39f5 commit 545ba32
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ String representations of angles in both "degree:arcmin:arcsec" and "hour:min:s
#### dms formats

```julia
"[+-]xx:xx:xx.x[NS]"
"[+-]xx xx xx.x[NS]"
"[+-]xxdxxmxx.xs[NS]"
"[+-]xx°xx'xx.x\"[NS]"
"[+-]xx°xx′xx.x″[NS]" # \prime, \pprime
"[+-]xx:xx:xx.x[NESW]"
"[+-]xx xx xx.x[NESW]"
"[+-]xxdxxmxx.xs[NESW]"
"[+-]xx°xx'xx.x\"[NESW]"
"[+-]xx°xx′xx.x″[NESW]" # \prime, \pprime
```

#### hms formats

```julia
"[+-]xx:xx:xx.x[EW]"
"[+-]xx xx xx.x[EW]"
"[+-]xxhxxmxx.xs[EW]"
"[+-]xxhxx'xx.x\"[EW]"
"[+-]xx°xx′xx.x″[EW]"
"[+-]xx:xx:xx.x[NESW]"
"[+-]xx xx xx.x[NESW]"
"[+-]xxhxxmxx.xs[NESW]"
"[+-]xxhxx'xx.x\"[NESW]"
"[+-]xx°xx′xx.x″[NESW]"
```

the simplest way to convert is to use the `@dms_str` and `@hms_str` macros, which allows you to choose the output angle type
Expand Down
25 changes: 10 additions & 15 deletions src/parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ deg_delims = "[°d:\\s]" # only for dms
ha_delims = "[h:\\s]" # only for hms
min_delims = "['m:′\\s]" # shared
sec_delims = "[\"″s\\s]" # shared
dms_dirs = "(N|S)" # positive first
hms_dirs = "(E|W)" # positive first
dirs = "(N|E|S|W)" # positive first
# the trailing ()? groups are optional, so only leading digit is required
const dms_re = Regex("$first$deg_delims?($num)?$min_delims?($num)?$sec_delims?$dms_dirs?")
const hms_re = Regex("$first$ha_delims?($num)?$min_delims?($num)?$sec_delims?$hms_dirs?")
const dms_re = Regex("$first$deg_delims?($num)?$min_delims?($num)?$sec_delims?$dirs?")
const hms_re = Regex("$first$ha_delims?($num)?$min_delims?($num)?$sec_delims?$dirs?")

"""
parse_dms(input)
Parses a string input in "deg:arcmin:arcsec" format to the tuple `(degrees, arcminutes, arcseconds)`. The following delimiters will all work and can be mixed together (the last delimiter is optional):
```
"[+-]xx[°d: ]xx['′m: ]xx[\\\"″s][NS]"
"[+-]xx[°d: ]xx['′m: ]xx[\\\"″s][NESW]"
```
if the direction ("N" or "S") is provided, "S" is considered negative (and "-1:0:0S" is 1 degree North)
if the direction is provided, "S" and "E" are considered negative (and "-1:0:0S" is 1 degree North)
"""
function parse_dms(input)
m = match(dms_re, strip(input))
Expand All @@ -34,10 +33,8 @@ function parse_dms(input)
else
sec = 0.0
end
if m.captures[4] !== nothing
if m.captures[4] == "S"
deg = -deg
end
if m.captures[4] == "S" || m.captures[4] == "W"
deg = -deg
end
return deg, min, sec
end
Expand All @@ -49,7 +46,7 @@ Parses a string input in "ha:min:sec" format to the tuple `(hours, minutes, seco
```
"[+-]xx[h ]xx['′m: ]xx[\\\"″s][EW]"
```
if the direction ("E" or "W") is provided, "W" is considered negative (and "-1:0:0W" is 1 degree East)
if the direction is provided, "S" and "E" are considered negative (and "-1:0:0W" is 1 degree East)
"""
function parse_hms(input)
m = match(hms_re, strip(input))
Expand All @@ -65,10 +62,8 @@ function parse_hms(input)
else
sec = 0.0
end
if m.captures[4] !== nothing
if m.captures[4] == "W"
ha = -ha
end
if m.captures[4] == "S" || m.captures[4] == "W"
ha = -ha
end
return ha, min, sec
end
Expand Down
7 changes: 7 additions & 0 deletions test/parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,16 @@ end
@test parse_dms("1:0:0N") == (1.0, 0.0, 0.0)
@test parse_dms("1:0:0S") == (-1.0, 0.0, 0.0)
@test parse_dms("-1:0:0S") == (1.0, 0.0, 0.0)
@test parse_dms("1:0:0E") == (1.0, 0.0, 0.0)
@test parse_dms("1:0:0W") == (-1.0, 0.0, 0.0)
@test parse_dms("-1:0:0W") == (1.0, 0.0, 0.0)

@test parse_hms("1:0:0E") == (1.0, 0.0, 0.0)
@test parse_hms("1:0:0W") == (-1.0, 0.0, 0.0)
@test parse_hms("-1:0:0W") == (1.0, 0.0, 0.0)
@test parse_dms("1:0:0N") == (1.0, 0.0, 0.0)
@test parse_hms("1:0:0S") == (-1.0, 0.0, 0.0)
@test parse_dms("-1:0:0S") == (1.0, 0.0, 0.0)
end

@testset "astropy examples" begin
Expand Down

0 comments on commit 545ba32

Please sign in to comment.