Skip to content

Commit

Permalink
pretty printing of arrays of ClimArray
Browse files Browse the repository at this point in the history
  • Loading branch information
Datseris committed Mar 18, 2021
1 parent 7fefa66 commit 1fe1023
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/ClimateBase.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module ClimateBase

# TODO: Be sure all exported names have docstrings
include("core/coredefs.jl")
include("core/prettyprint.jl")
include("core/nc_io.jl")
include("core/aggregation.jl")

Expand Down
63 changes: 2 additions & 61 deletions src/core/coredefs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ Time = DimensionalData.Ti
AbDimArray = DimensionalData.AbstractDimArray

export At, Between, Near # Selectors from DimensionalArrays.jl
export hasdim, DimensionalArray, dimnum
export Time, Lon, Lat, dims, Coord, Hei, Pre
export hasdim, dimnum, dims
export Time, Lon, Lat, dims, Coord, Hei, Pre, Ti
export GaussianEqualArea, LonLatGrid, spacestructure
export DimensionalData # for accessing its functions

Expand Down Expand Up @@ -129,62 +129,3 @@ Base.view(A::ClimArray, i::Tuple) = view(A, i...)
# Convenience
Base.ones(A::AbDimArray) = basetypeof(A)(ones(eltype(A), size(A)), dims(A))
Base.zeros(A::AbDimArray) = basetypeof(A)(zeros(eltype(A), size(A)), dims(A))

##########################################################################################
# Pretty printing
##########################################################################################
# Remove reference dims from printing, and show attributes if any
function Base.show(io::IO, A::ClimArray)
summary(io, A)
print(io, "and")
printstyled(io, " data: "; color=:green)
dataA = data(A)
print(io, summary(dataA), "\n")
x = 2length(dims(A)) + attriblength(A.attrib) + 5
custom_show(io, data(A), x)
end

attriblength(d::AbstractDict) = length(d)
attriblength(d) = 0

# Define summary
function Base.summary(io::IO, A::ClimArray)
l = nameof(typeof(A))
printstyled(io, nameof(typeof(A)); color=:blue)
if A.name Symbol("")
print(io, " (named ")
printstyled(io, A.name; color=:blue)
print(io, ")")
end

print(io, " with dimensions:\n")
for d in dims(A)
print(io, " ", d, "\n")
end
if !isnothing(A.attrib)
printstyled(io, "attributes: "; color=:magenta)
show(io, MIME"text/plain"(), A.attrib)
print(io, '\n')
end
end

# Thanks to Michael Abbott for the following function
function custom_show(io::IO, A::AbstractArray{T,0}, x) where T
Base.show(IOContext(io, :compact => true, :limit => true), A)
end
function custom_show(io::IO, A::AbstractArray{T,1}, x) where T
Base.show(IOContext(io, :compact => true, :limit => true, :displaysize => displaysize(io) .- (x, 0)), A)
end
function custom_show(io::IO, A::AbstractArray{T,2}, x) where T
Base.print_matrix(IOContext(io, :compact => true, :limit => true, :displaysize => displaysize(io) .- (x, 0)), A)
end
function custom_show(io::IO, A::AbstractArray{T,N}, x) where {T,N}
o = ones(Int, N-2)
frame = A[:, :, o...]
onestring = join(o, ", ")
println(io, "[:, :, $(onestring)]")
Base.print_matrix(
IOContext(io, :compact => true, :limit=>true, :displaysize => displaysize(io) .- (x, 0)),
frame)
print(io, "\n[and ", prod(size(A,d) for d=3:N) - 1," more slices...]")
end
67 changes: 67 additions & 0 deletions src/core/prettyprint.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# printing of a single clim array
function Base.show(io::IO, ::MIME"text/plain", A::ClimArray)
summary(io, A)
print(io, "and")
printstyled(io, " data: "; color=:green)
dataA = data(A)
print(io, summary(dataA), "\n")
x = 2length(dims(A)) + attriblength(A.attrib) + 5
custom_show(io, data(A), x)
end

attriblength(d::AbstractDict) = length(d)
attriblength(d) = 0

# Define summary
function Base.summary(io::IO, A::ClimArray)
printstyled(io, "ClimArray"; color=:blue)
if A.name Symbol("")
print(io, " (named ")
printstyled(io, A.name; color=:blue)
print(io, ")")
end

print(io, " with dimensions:\n")
for d in dims(A)
print(io, " ", d, "\n")
end
if !isnothing(A.attrib)
printstyled(io, "attributes: "; color=:magenta)
show(io, MIME"text/plain"(), A.attrib)
print(io, '\n')
end
end

# Thanks to Michael Abbott for the following function
function custom_show(io::IO, A::AbstractArray{T,0}, x) where T
Base.show(IOContext(io, :compact => true, :limit => true), A)
end
function custom_show(io::IO, A::AbstractArray{T,1}, x) where T
Base.show(IOContext(io, :compact => true, :limit => true, :displaysize => displaysize(io) .- (x, 0)), A)
end
function custom_show(io::IO, A::AbstractArray{T,2}, x) where T
Base.print_matrix(IOContext(io, :compact => true, :limit => true, :displaysize => displaysize(io) .- (x, 0)), A)
end
function custom_show(io::IO, A::AbstractArray{T,N}, x) where {T,N}
o = ones(Int, N-2)
frame = A[:, :, o...]
onestring = join(o, ", ")
println(io, "[:, :, $(onestring)]")
Base.print_matrix(
IOContext(io, :compact => true, :limit=>true, :displaysize => displaysize(io) .- (x, 0)),
frame)
print(io, "\n[and ", prod(size(A,d) for d=3:N) - 1," more slices...]")
end

# printing of a vector of climarrays
function Base.show(io::IO, A::ClimArray)
printstyled(io, "ClimArray"; color=:blue)
if A.name Symbol("")
print(io, " (named ")
printstyled(io, A.name; color=:blue)
print(io, ")")
end
s = join(string.(size(A)), "×")
d = join([replace(string(basetypeof(d)), "Ti"=>"Time") for d in dims(A)], "×")
print(io, " with $s $d data")
end

0 comments on commit 1fe1023

Please sign in to comment.