Skip to content

Commit

Permalink
Add some i and refactor cells
Browse files Browse the repository at this point in the history
  • Loading branch information
helgee committed Feb 9, 2019
1 parent 3fe69fa commit 451dc92
Show file tree
Hide file tree
Showing 6 changed files with 1,593 additions and 221 deletions.
117 changes: 61 additions & 56 deletions src/cells.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dtype(::Type{SpiceDouble}) = 1
dtype(::Type{SpiceInt}) = 2

mutable struct Cell{T}
# Do not reorder these fields
dtype::SpiceInt
length::SpiceInt
size::SpiceInt
Expand All @@ -22,23 +23,57 @@ mutable struct Cell{T}
end
end

mutable struct SpiceCell{T, N} <: AbstractArray{T, N}
data::Array{T, N}
cell::Cell{T}
itertype(::Type{T}) where {T} = T
itertype(::Type{SpiceChar}) = String

init_data(::Type{T}, size, _) where {T} = Array{T}(undef, CTRLSZ + size)
init_data(::Type{SpiceChar}, size, length) = fill(SpiceChar(0), (length, CTRLSZ + size))

len(::Type{T}, _) where {T} = 0
len(::Type{SpiceChar}, length) = length += 1

dim(::Type{T}) where {T} = 1
dim(::Type{SpiceChar}) = 2

data_ptr(::Type{SpiceChar}, data, length) = pointer(data, CTRLSZ * length + 1)
data_ptr(::Type{T}, data, _) where {T} = pointer(data, CTRLSZ + 1)

mutable struct SpiceCell{S, T, N} <: AbstractArray{T, 1}
data::Array{S, N}
cell::Cell{S}

function SpiceCell{T}(size::Integer, length::Integer=256) where T
length = len(T, length)
cell = Cell{T}(length, size)
data = init_data(T, size, length)
self = new{T, itertype(T), dim(T)}(data, cell)
base = pointer(self.data, 1)
data = pointer(self.data, CTRLSZ + 1)
self.cell.base = pointer(self.data, 1)
self.cell.data = data_ptr(T, self.data, length)
self
end
end

const SpiceCharCell = SpiceCell{SpiceChar,2}
const SpiceDoubleCell = SpiceCell{SpiceDouble,1}
const SpiceIntCell = SpiceCell{SpiceInt,1}
const SpiceDoubleCell = SpiceCell{SpiceDouble}
const SpiceIntCell = SpiceCell{SpiceInt}
const SpiceCharCell = SpiceCell{SpiceChar}

Base.IndexStyle(::SpiceCell) = IndexLinear()
Base.firstindex(cell::SpiceCell) = 1
Base.lastindex(cell::SpiceCell) = cell.cell.card

function show(io::IO, cell::SpiceCell{T,1}) where T
"""
SpiceCharCell(size, length)
Create a SpiceCharCell that can contain up to `size` strings with `length` characters.
"""
SpiceCharCell

function show(io::IO, cell::SpiceCell{T}) where T
print(io, "SpiceCell{$(T.name.name)}($(cell.cell.size))")
end
function show(io::IO, cell::SpiceCell{T,2}) where T
function show(io::IO, cell::SpiceCharCell)
print(io, "SpiceCell{$(T.name.name)}($(cell.cell.size),$(cell.cell.length))")
end

Expand All @@ -51,71 +86,34 @@ function check_ind(cell, inds)
end
end

function getindex(cell::SpiceCell{T}, ind) where T<:Real
function getindex(cell::SpiceCell, ind::Int)
check_ind(cell, ind)
return cell.data[CTRLSZ .+ ind]
cell.data[CTRLSZ .+ ind]
end

function getindex(cell::SpiceCharCell, ind::Int)
check_ind(cell, ind)
str = cell.data[:, CTRLSZ .+ ind]
return String(str[1:findfirst(str .== 0)-1])
end

function getindex(cell::SpiceCharCell, ind)
check_ind(cell, ind)
str = cell.data[:, CTRLSZ .+ ind]
return vec(mapslices(x -> String(x[1:findfirst(x .== 0)-1]), str, dims=1))
end

function SpiceCell{SpiceChar}(size, length)
# We add an extra element for the null-byte terminator.
nlength = length + 1
strc = Cell{SpiceChar}(nlength, size)
data = fill(UInt8(0), (nlength, CTRLSZ + size))
self = SpiceCell(data, strc)
base = pointer(self.data, 1)
data = pointer(self.data, CTRLSZ * nlength + 1)
self.cell.base = base
self.cell.data = data
return self
end

"""
SpiceCharCell(size, length)
Create a SpiceCharCell that can contain up to `size` strings with `length` characters.
"""
SpiceCharCell(size::Int, length::Int) = SpiceCell{SpiceChar}(size, length)

function SpiceCell{T}(size) where T<:Real
strc = Cell{T}(0, size)
data = Vector{T}(undef, CTRLSZ + size)
self = SpiceCell(data, strc)
base = pointer(self.data, 1)
data = pointer(self.data, CTRLSZ + 1)
self.cell.base = base
self.cell.data = data
return self
String(str[1:findfirst(str .== 0)-1])
end

"""
SpiceDoubleCell(size)
Create a SpiceDoubleCell that can contain up to `size` elements.
"""
SpiceDoubleCell(size) = SpiceCell{SpiceDouble}(size)
SpiceDoubleCell

"""
SpiceIntCell(size)
Create a SpiceIntCell that can contain up to `size` elements.
"""
SpiceIntCell(size) = SpiceCell{SpiceInt}(size)
SpiceIntCell

for (t, f) in zip((SpiceInt, SpiceDouble), ("appndi_c", "appndd_c"))
@eval begin
function appnd(item, cell::SpiceCell{$t})
function appnd(item, cell::SpiceCell{$t, $t})
c = Ref(cell.cell)
ccall(($f, libcspice), Cvoid, ($t, Ref{Cell{$t}}), item, cell.cell)
handleerror()
Expand All @@ -133,7 +131,7 @@ Append an `item` to the char/double/integer SpiceCell `cell`.
[https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/appndd_c.html](https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/appndd_c.html)
[https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/appndi_c.html](https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/appndi_c.html)
"""
function appnd(item, cell::SpiceCell{SpiceChar})
function appnd(item, cell::SpiceCharCell)
ccall((:appndc_c, libcspice), Cvoid, (Cstring, Ref{Cell{SpiceChar}}), item, cell.cell)
handleerror()
return nothing
Expand All @@ -156,21 +154,21 @@ append!(cell::SpiceCell, collection) = foreach(x -> appnd(x, cell), collection)
"""
card(cell)
Returns the cardinality (number of elements) of a SpiceCell `cell`.
Returns the cardinality (number of elements) of `cell`.
"""
card(cell::SpiceCell) = Int(cell.cell.card)

"""
length(cell)
Returns the cardinality (number of elements) of a SpiceCell `cell`.
Returns the cardinality (number of elements) of `cell`.
"""
length(cell::SpiceCell) = card(cell)

"""
size(cell)
Returns the cardinality (number of elements) of a SpiceCell `cell`.
Returns the cardinality (number of elements) of `cell`.
"""
size(cell::SpiceCell) = (card(cell),)

Expand All @@ -179,8 +177,15 @@ size(cell::SpiceCell) = (card(cell),)
Duplicate the SpiceCell `cell`.
"""
function Base.copy(cell::SpiceCell{T, N}) where {T, N}
function Base.copy(cell::SpiceCell{T}) where {T}
copy = SpiceCell{T}(cell.cell.size)
ccall((:copy_c, libcspice), Cvoid, (Ref{Cell{T}}, Ref{Cell{T}}), cell.cell, copy.cell)
copy
end

function Base.copy(cell::SpiceCharCell)
copy = SpiceCharCell(cell.cell.size, cell.cell.length)
ccall((:copy_c, libcspice), Cvoid, (Ref{Cell{SpiceChar}}, Ref{Cell{SpiceChar}}), cell.cell, copy.cell)
copy
end

38 changes: 34 additions & 4 deletions src/e.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
export
export
edlimb,
et2utc,
etcal,
eul2m,
edterm

"""
edlimb(a, b, c, viewpt)
Find the limb of a triaxial ellipsoid, viewed from a specified point.
### Arguments ###
- `a`: Length of ellipsoid semi-axis lying on the x-axis
- `b`: Length of ellipsoid semi-axis lying on the y-axis
- `c`: Length of ellipsoid semi-axis lying on the z-axis
- `viewpt`: Location of viewing point
### Output ###
Returns the limb of the ellipsoid as seen from the viewing point.
### References ###
- [NAIF Documentation](https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/edlimb_c.html)
"""
function edlimb(a, b, c, viewpt)
length(viewpt) != 3 && throw(ArgumentError("Length of `viewpt` must be 3."))
limb = Ellipse()
ccall((:edlimb_c, libcspice), Cvoid,
(SpiceDouble, SpiceDouble, SpiceDouble, Ptr{SpiceDouble}, Ref{Ellipse}),
a, b, c, viewpt, limb)
handleerror()
limb
end

"""
et2utc(et, format, prec)
Expand All @@ -23,8 +54,7 @@ to Calendar, Day-of-Year, or Julian Date format, UTC.
### Output ###
Returns an output time string equivalent to the input
epoch, in the specified format.
Returns an output time string equivalent to the input epoch, in the specified format.
### References ###
Expand Down Expand Up @@ -128,5 +158,5 @@ function edterm(trmtyp, source, target, et, fixref, obsrvr, npts; abcorr="NONE")
(Cstring, Cstring, Cstring, SpiceDouble, Cstring, Cstring, Cstring, SpiceInt, Ref{SpiceDouble}, Ptr{SpiceDouble}, Ptr{SpiceDouble}),
trmtyp, source, target, et, fixref, abcorr, obsrvr, npts, trgepc, obspos, trmpts)
handleerror()
trgepc[], obspos, trmpts
trgepc[], obspos, trmpts
end
Loading

0 comments on commit 451dc92

Please sign in to comment.