So it is clearly a problem for the cell type String and the dimension (1,).
Looking at the function given by the error ERROR: DimensionMismatch: bad number of dimensions, we see that code (I removed some useless parts for clarity):
# Check dimensions. The number of rows is adjusted automatically.
cell_dims = read_tdim(hdu, num)
if (cell_ndims = length(cell_dims)) == 1 && Base.first(cell_dims) == 1
cell_ndims = 0
end
vals_ndims = ndims(vals)
if eltype(vals) <: AbstractString
off = 1 # the first dimension is to index characters and does not count here
else
off = 0 # all leading dimensions must be identical
end
n = cell_ndims - off # number of dimension to compare
((vals_ndims == n)|(vals_ndims == n+1)) || throw(DimensionMismatch("bad number of dimensions"))
for i in 1:n
vals_dims[i] == cell_dims[i+off] || throw(DimensionMismatch("incompatible dimension"))
end
Now imagine we run that code, I add the values of the variables after each instruction:
julia> cell_dims = read_tdim(hdu, num)
(1,) # we set dimension (1,) for our column, which means "scalar", CFITSIO will give us (1,)
julia> if (cell_ndims = length(cell_dims)) == 1 && Base.first(cell_dims) == 1
cell_ndims = 0
end
0
julia> vals_ndims = ndims(vals)
1 # indeed our input values `["a"]` has one dimension
julia> if eltype(vals) <: AbstractString
off = 1 # the first dimension is to index characters and does not count here
else
off = 0 # all leading dimensions must be identical
end
1
julia> n = cell_ndims - off # number of dimension to compare
-1
julia> ((vals_ndims == n)|(vals_ndims == n+1)) || throw(DimensionMismatch("bad number of dimensions"))
ERROR: DimensionMismatch: bad number of dimensions
# indeed `vals_ndims` is `1` whereas `n` is `-1`
We can see that the two concepts, "not comparing the first dimension for String vals", and "(1,) means a scalar" are wrong when used together.
What is wrong
I create a FitsTableHDU and I want a column of type String with maximum string length 1. I cannot write to such a column, it fails.
What I expected
No failure
How to reproduce
Investigation
It works for columns of type
Int
:It works for dimensions
(2,)
:It works for dimensions
(1,1)
(yes):So it is clearly a problem for the cell type
String
and the dimension(1,)
.Looking at the function given by the error
ERROR: DimensionMismatch: bad number of dimensions
, we see that code (I removed some useless parts for clarity):Now imagine we run that code, I add the values of the variables after each instruction:
We can see that the two concepts, "not comparing the first dimension for String vals", and "(1,) means a scalar" are wrong when used together.
Here is a list of cases when cell type is
Int
:Now with cell type
String
:How to resolve
I suggest that when cell type is String and dimensions is (1,) we put
n
to0
.You can check on the previous two tables that it is correct