Skip to content

Commit

Permalink
working on #7
Browse files Browse the repository at this point in the history
  • Loading branch information
cmichelenstrofer committed Nov 27, 2023
1 parent af58c29 commit edcc17b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 12 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "0.2.0"

[deps]
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
UnitfulAngles = "6fb2a4bd-7999-5318-a3b2-8ad61056cd98"
UnitfulEquivalences = "da9c4bc3-91c8-4f02-8a40-6b990d2a7e0c"

[compat]
Expand Down
83 changes: 75 additions & 8 deletions src/DimensionfulAngles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ julia> cos(45ua"°")
module DimensionfulAngles

# using Base: Base # extend: see `base.jl` for full list of functions extended
using Unitful: Unitful # extend: has_unit_spacing,
using Unitful: minute, promotion, rad, s, 𝐓, 𝐋
using Unitful: Dimension, DimensionlessQuantity, Frequency, FrequencyFreeUnits, Length
using Unitful: MixedUnits, NoDims, NoUnits, Number, Quantity, Time, Unitlike, Unit, Units
using Unitful: Wavenumber
using Unitful: Unitful # extend: has_unit_spacing, uconvert
using Unitful: minute, promotion, rad, rpm, rps, s, sr, 𝐓, 𝐋, °
using Unitful: Dimension, Dimensions, DimensionlessQuantity, FreeUnits, Frequency
using Unitful: FrequencyFreeUnits, Length, MixedUnits, NoDims, NoUnits, Number, Quantity
using Unitful: Time, Unitlike, Unit, Units, Wavenumber
using Unitful: @dimension, @refunit, @derived_dimension, @unit
using Unitful: dimension, register, uconvert, unit, ustrip
using UnitfulEquivalences: Equivalence, @eqrelation
using UnitfulAngles: turn, doubleTurn, halfTurn, quadrant, sextant, octant, clockPosition
using UnitfulAngles: hourAngle, compassPoint, hexacontade, brad, diameterPart, grad
using UnitfulAngles: arcminute, arcsecond
# using UnitfulAngles: mas, μas, pas # UnitfulAngles PR #34

export @ua_str
export θ₀
Expand All @@ -45,7 +49,7 @@ export sexagesimal, show_sexagesimal
# export AngularVelocity, AngularVelocityUnits, AngularVelocityFreeUnits
# export AngularAcceleration, AngularAccelerationUnits, AngularAccelerationFreeUnits
# export AngularPeriod, AngularPeriodUnits, AngularPeriodFreeUnits
# export AngularWavenumber, AngularWavenumberUnits, AngularWavenumberUnits
# export AngularWavenumber, AngularWavenumberUnits, AngularWavenumberFreeUnits
# export AngularWavelength, AngularWavelengthUnits, AngularWavelengthFreeUnits

"""
Expand Down Expand Up @@ -139,11 +143,74 @@ julia> 2.1ua"rad" / θ₀
"""
const θ₀ = (1 // 1)radᵃ

# Convert to/from Unitful (SI)
function _convert_angles(x::Quantity, input_angle_unit::Units, input_dim::Dimensions,

Check warning on line 147 in src/DimensionfulAngles.jl

View check run for this annotation

Codecov / codecov/patch

src/DimensionfulAngles.jl#L147

Added line #L147 was not covered by tests
output_angle_unit::Units, angle_units::NTuple{N, Units} where N,
)
# convert all units in `angle_units` to common `input_angle_unit` unit.
# cannot use `upreferred` because of `FixedUnits` and `ContextUnits`
AngularUnits = [

Check warning on line 152 in src/DimensionfulAngles.jl

View check run for this annotation

Codecov / codecov/patch

src/DimensionfulAngles.jl#L152

Added line #L152 was not covered by tests
typeof(typeof(angle_unit).parameters[1][1]) for angle_unit in angle_units
]

power = 0//1
input_units = 1
for iunit in typeof(x).parameters[3].parameters[1]
ipower = iunit.power
if typeof(iunit) AngularUnits
power += ipower
input_units *= uconvert(

Check warning on line 162 in src/DimensionfulAngles.jl

View check run for this annotation

Codecov / codecov/patch

src/DimensionfulAngles.jl#L156-L162

Added lines #L156 - L162 were not covered by tests
input_angle_unit^ipower,
1Unitful.FreeUnits{(iunit,), input_dim^ipower, nothing}()
)
else
input_units *= (

Check warning on line 167 in src/DimensionfulAngles.jl

View check run for this annotation

Codecov / codecov/patch

src/DimensionfulAngles.jl#L167

Added line #L167 was not covered by tests
1Unitful.FreeUnits{(iunit,), typeof(iunit).parameters[2]^ipower, nothing}()
)
end
end
input_units = unit(input_units)
x = uconvert(input_units, x)

Check warning on line 173 in src/DimensionfulAngles.jl

View check run for this annotation

Codecov / codecov/patch

src/DimensionfulAngles.jl#L172-L173

Added lines #L172 - L173 were not covered by tests
# convert to output units/dimensions
return x*input_angle_unit^-power*output_angle_unit^power

Check warning on line 175 in src/DimensionfulAngles.jl

View check run for this annotation

Codecov / codecov/patch

src/DimensionfulAngles.jl#L175

Added line #L175 was not covered by tests
end

Unitful.uconvert(s::Symbol, x::Quantity) = Unitful.uconvert(Val{s}(), x)

Check warning on line 178 in src/DimensionfulAngles.jl

View check run for this annotation

Codecov / codecov/patch

src/DimensionfulAngles.jl#L178

Added line #L178 was not covered by tests

function Unitful.uconvert(s::Val{:Unitful}, x::Quantity)
angle_units = (

Check warning on line 181 in src/DimensionfulAngles.jl

View check run for this annotation

Codecov / codecov/patch

src/DimensionfulAngles.jl#L180-L181

Added lines #L180 - L181 were not covered by tests
radᵃ, °ᵃ, turnᵃ, doubleTurnᵃ, halfTurnᵃ, quadrantᵃ, sextantᵃ, octantᵃ,
clockPositionᵃ, hourAngleᵃ, compassPointᵃ, hexacontadeᵃ, bradᵃ, diameterPartᵃ,
gradᵃ, arcminuteᵃ, arcsecondᵃ, asᵃ, ʰᵃ, ᵐᵃ, ˢᵃ,
)
x = _convert_angles(x, radᵃ, 𝐀, rad, angle_units)

Check warning on line 186 in src/DimensionfulAngles.jl

View check run for this annotation

Codecov / codecov/patch

src/DimensionfulAngles.jl#L186

Added line #L186 was not covered by tests
# derived units that contain angles.
x = _convert_angles(x, srᵃ, 𝐀^2, sr, (srᵃ,))
x = _convert_angles(x, rpsᵃ, 𝐀*𝐓^-1, rps, (rpsᵃ,))
x = _convert_angles(x, rpmᵃ, 𝐀*𝐓^-1, rpm, (rpmᵃ,))
return x

Check warning on line 191 in src/DimensionfulAngles.jl

View check run for this annotation

Codecov / codecov/patch

src/DimensionfulAngles.jl#L188-L191

Added lines #L188 - L191 were not covered by tests
end

function Unitful.uconvert(s::Val{:DimensionfulAngles}, x::Quantity)
angle_units = (

Check warning on line 195 in src/DimensionfulAngles.jl

View check run for this annotation

Codecov / codecov/patch

src/DimensionfulAngles.jl#L194-L195

Added lines #L194 - L195 were not covered by tests
rad, °, turn, doubleTurn, halfTurn, quadrant, sextant, octant, clockPosition,
hourAngle, compassPoint, hexacontade, brad, diameterPart, grad, arcminute,
arcsecond,
# TODO: mas, μas, pas # UnitfulAngles PR #34
)
x = _convert_angles(x, rad, NoDims, radᵃ, angle_units)

Check warning on line 201 in src/DimensionfulAngles.jl

View check run for this annotation

Codecov / codecov/patch

src/DimensionfulAngles.jl#L201

Added line #L201 was not covered by tests
# derived units that contain angles.
x = _convert_angles(x, sr, NoDims, srᵃ, (sr,))
x = _convert_angles(x, rps, NoDims, rpsᵃ, (rps,))
x = _convert_angles(x, rpm, NoDims, rpmᵃ, (rpm,))
return x

Check warning on line 206 in src/DimensionfulAngles.jl

View check run for this annotation

Codecov / codecov/patch

src/DimensionfulAngles.jl#L203-L206

Added lines #L203 - L206 were not covered by tests
end

# Other functionalities.
include("units.jl") # Other units of angle.
include("base.jl") # Extend Base functions for units of angle.
include("uamacro.jl") # String macro for using dimensionful units.
include("derived.jl") # Units and functionalities for derived dimensions.
include("uamacro.jl") # String macro for using dimensionful units.
include("base.jl") # Extend Base functions for units of angle.
include("defaults.jl") # Submodule to flood workspace with unit types.

# Register new units and dimensions with Unitful.jl.
Expand Down
2 changes: 1 addition & 1 deletion src/derived.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Dimension: 𝐀 𝐓⁻¹.
See also [`DimensionfulAngles.radᵃ`](@ref).
"""
@unit rpmᵃ "rps" RevolutionsPerMinuteᵃ (1turnᵃ/minute) false
@unit rpmᵃ "rpm" RevolutionsPerMinuteᵃ (1turnᵃ/minute) false

# Angular wavenumber, angular wavelength, angular period
@derived_dimension AngularWavenumber (𝐀*𝐋^-1) true
Expand Down
8 changes: 5 additions & 3 deletions src/units.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ Unitful.has_unit_spacing(u::Units{(Unit{:Arcsecondᵃ, 𝐀}(0, 1 // 1),), 𝐀}
@unit diameterPartᵃ "diameterPart" DiameterPartᵃ (1radᵃ//60) false

# Turn
@doc __unit_docstr("turn", "turn", "2π rad", "rad",
"Equivalent to a full cycle, revolution, or rotation.")
@unit turnᵃ "τ" Turnᵃ (2π*radᵃ) false
@doc __unit_docstr(
"turn", "turn", "2π rad", "rad",
"Equivalent to a full cycle, revolution, or rotation."
)
@unit turnᵃ "τ" Turnᵃ (360°ᵃ) false

# Based on the turn
@doc __unit_docstr("doubleTurn", "double turn", "2 turn", "turn")
Expand Down

0 comments on commit edcc17b

Please sign in to comment.