torrance / Casacore.jl

A high level Julia interface to Casacore
MIT License
9 stars 0 forks source link

Working with string values in columns #2

Closed iniyannatarajan closed 1 year ago

iniyannatarajan commented 1 year ago

While I can easily access the values of a Float64 array such as the DISH_DIAMETER in the ANTENNA subtable table.ANTENNA[:DISH_DIAMETER][:],

trying to access a column with string values throws the following error:

table.ANTENNA[:MOUNT][:]
ERROR: MethodError: Cannot `convert` an object of type Ptr{Nothing} to an object of type Casacore.LibCasacore.String
Closest candidates are:
  convert(::Type{Casacore.LibCasacore.String}, ::Casacore.LibCasacore.StringDereferenced) at ~/.julia/packages/CxxWrap/IdOJa/src/CxxWrap.jl:681
  convert(::Type{Casacore.LibCasacore.String}, ::Casacore.LibCasacore.StringAllocated) at ~/.julia/packages/CxxWrap/IdOJa/src/CxxWrap.jl:680
  convert(::Type{Casacore.LibCasacore.String}, ::T) where T<:Casacore.LibCasacore.String at ~/.julia/packages/CxxWrap/IdOJa/src/CxxWrap.jl:676
  ...
Stacktrace:
 [1] cxxconvert(to_type::Type{CxxWrap.CxxWrapCore.CxxPtr{Casacore.LibCasacore.String}}, x::Ptr{Nothing}, #unused#::Type{CxxWrap.CxxWrapCore.IsCxxType})
   @ CxxWrap.CxxWrapCore ~/.julia/packages/CxxWrap/IdOJa/src/CxxWrap.jl:581
 [2] cconvert(to_type::Type{CxxWrap.CxxWrapCore.CxxPtr{Casacore.LibCasacore.String}}, x::Ptr{Nothing})
   @ CxxWrap.CxxWrapCore ~/.julia/packages/CxxWrap/IdOJa/src/CxxWrap.jl:564
 [3] Casacore.LibCasacore.Vector{Casacore.LibCasacore.String}(arg1::Casacore.LibCasacore.IPositionAllocated, arg2::Ptr{Nothing}, arg3::Casacore.LibCasacore.StorageInitPolicy)
   @ Casacore.LibCasacore ~/.julia/packages/CxxWrap/IdOJa/src/CxxWrap.jl:618
 [4] getindex(c::Casacore.Tables.Column{Casacore.LibCasacore.String, 1, Casacore.LibCasacore.ScalarColumnAllocated{Casacore.LibCasacore.String}}, i::Colon)
   @ Casacore.Tables ~/.julia/packages/Casacore/pZFa6/src/Tables.jl:204
 [5] top-level scope
   @ REPL[14]:1

Am I missing something related to indexing in Julia here? Or is there something about Casacore's own String type I should be watching out for? Thanks!

torrance commented 1 year ago

Good catch! It's currently written to handle tables of primitive types but not yet strings. (In fact, I'm not sure I've seen string columns in my own datasets).

I will take a look at this on Monday and see how they're stored and whether this can be an easy extension of functionality.

david-macmahon commented 1 year ago

I just came across this issue as well, but in my case it was for table.ANTENNA[:NAME][:]. I hope it's an easy fix because this package is very useful for me. Thanks for writing it!

torrance commented 1 year ago

This has been a tricky one. For primitive types, both Julia and Casacore share the same underlying bit representation. This allows for great efficiencies with primitive types where I entirely avoid copying of memory from Casascore to Julia. For strings, this is not the case, and required their very own unique code path.

Are either of @iniyannatarajan or @david-macmahon able to check out whether dev now works for their use cases correctly? To add the dev package from git try: pkg> add https://github.com/torrance/Casacore.jl.

Note that the full test suite won't pass on your machines since casacorecxx_jll hasn't been updated in the registry yet, but you should be able to read/write to String columns. (The registry has been updated, casacorecxx_jl v0.2.1 is now updated)

iniyannatarajan commented 1 year ago

@torrance Thanks for the quick fix! With the new version, I can read in columns with string values as Vectors of Strings.

david-macmahon commented 1 year ago

@torrance I can confirm that reading table.ANTENNA[:NAME][:] returns a Vector of Stings now. Thanks!!!

torrance commented 1 year ago

Thanks both for your feedback! I'll push this out into a v0.2 release shortly.