Skip to content

Commit

Permalink
Add ydn2md function
Browse files Browse the repository at this point in the history
  • Loading branch information
giordano committed Mar 22, 2016
1 parent 7b2c2b6 commit cc56a8a
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,6 @@ Missing in AstroLib.jl
* `xy2ad`
* `xyad`
* `xyxy`
* `ydn2md`
* `zang`
* `zbrent`
* `zenpos`
Expand Down
47 changes: 47 additions & 0 deletions docs/utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,53 @@ Code of this function is based on IDL Astronomy User's Library.

--------------

ydn2md
~~~~~~

.. function:: ydn2md(year, day) -> date

Purpose
'''''''

Convert from year and day number of year to a date.

Explanation
'''''''''''

Returns the date corresponding to the ``day`` of ``year``.

Arguments
'''''''''

- ``year``: the year, as a scalar integer.
- ``day``: the day of ``year``, as an integer. It is can be either a
scalar or array of integers.

Output
''''''

The date, of ``Date`` type, of :math:`\text{day} - 1` days after January
1st of ``year``.

Example
'''''''

Find the date of the 60th and 234th days of the year 2016.

.. code:: julia
julia> ydn2md(2016, [60, 234])
2-element Array{Date,1}:
2016-02-29
2016-08-21
Note
''''

``ymd2dn`` converts from a date to day of the year.

--------------

ymd2dn
~~~~~~

Expand Down
3 changes: 3 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,8 @@ export vactoair
include("xyz.jl")
export xyz

include("ydn2md.jl")
export ydn2md

include("ymd2dn.jl")
export ymd2dn
51 changes: 51 additions & 0 deletions src/ydn2md.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# This file is a part of AstroLib.jl. License is MIT "Expat".
# Copyright (C) 2016 Mosè Giordano.

"""
ydn2md(year, day) -> date
### Purpose ###
Convert from year and day number of year to a date.
### Explanation ###
Returns the date corresponding to the `day` of `year`.
### Arguments ###
* `year`: the year, as a scalar integer.
* `day`: the day of `year`, as an integer. It is can be either a scalar or
array of integers.
### Output ###
The date, of `Date` type, of \$\text{day} - 1\$ days after January 1st of
`year`.
### Example ###
Find the date of the 60th and 234th days of the year 2016.
``` julia
julia> ydn2md(2016, [60, 234])
2-element Array{Date,1}:
2016-02-29
2016-08-21
```
### Note ###
`ymd2dn` converts from a date to day of the year.
"""
function ydn2md(year::Integer, day::Integer)
return Dates.firstdayofyear(Date(year)) + Dates.Day(day - 1)
end

function ydn2md{D<:Integer}(year::Integer, days::AbstractArray{D})
dates = similar(days, Date)
for d in eachindex(days)
dates[d] = ydn2md(year, days[d])
end
return dates
end
5 changes: 5 additions & 0 deletions test/utils-tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,10 @@ let
@test_approx_eq vz [0.003606857607574784]
end

# Test ydn2md.
@test ydn2md(2016, [60, 234]) == [Date(2016, 02, 29), Date(2016, 08, 21)]
@test ymd2dn(ydn2md(2016, 60)) == 60

# Test ymd2dn
@test ymd2dn([Date(2015,3,5), Date(2016,3,5)]) == [64, 65]
@test ydn2md(2016, ymd2dn(Date(2016, 09, 16))) == Date(2016, 09, 16)

0 comments on commit cc56a8a

Please sign in to comment.