2

From a FITS-Table I read data, and get a Struct containing the table, where each tag represents a column.

Is there a way to reformat the Struct of Arrays to an Array of Structs? So that one Struct in the Array represents a Row?


General solution made by @mgalloy (see below):

function SoA2AoS, table

  if (table eq !NULL) then return, !NULL

  tnames = tag_names(table)
  new_table = create_struct(tnames[0], (table.(0)[0]))
  for t = 1L, n_tags(table) - 1L do begin
    new_table = create_struct(new_table, tnames[t], (table.(t))[0])
  endfor

  new_table = replicate(new_table, n_elements(table.(0)))

  for t = 0L, n_tags(table) - 1L do begin
    new_table.(t) = table.(t)
  endfor

  return, new_table

end
Matty
  • 63
  • 1
  • 9

3 Answers3

2

Yes, so you have something like this?

IDL> table = { a: findgen(10), b: 2. * findgen(10) }

Create a new table, which defines how a single row looks and replicate it the appropriate number of times:

IDL> new_table = replicate({a: 0.0, b: 0.0}, 10)

Then copy the columns over:

IDL> new_table.a = table.a
IDL> new_table.b = table.b

Your new table can be accessed by row or column:

IDL> print, new_table[5]
{      5.00000      10.0000}
IDL> print, new_table.a
  0.00000      1.00000      2.00000      3.00000      4.00000      5.00000      6.00000
  7.00000      8.00000      9.00000
mgalloy
  • 2,356
  • 1
  • 12
  • 10
  • Thanks a lot again. I'm currently trying to find a way to make it more universal, so it doesn't depend on the names of the tags (if that's even possible). – Matty Apr 25 '15 at 14:49
2

You can do it without knowing the names of the tags too (untested code):

; suppose table is a structure containing arrays
tnames = tag_names(table)
new_table = create_struct(tnames[0], (table.(0))[0]
for t = 1L, n_tags(table) - 1L do begin
  new_table = create_struct(new_table, tnames[t], (table.(t))[0])
endfor
table = replicate(table, n_elements(table.(0)))
for t = 0L, n_tags(table) - 1L do begin
  new_table.(t) = table.(t)
endfor
mgalloy
  • 2,356
  • 1
  • 12
  • 10
  • Thanks again, after just some minor fixes it works without problems. I'll add it to the main-comment. – Matty Apr 29 '15 at 08:43
2

The solution from mgalloy helped me too. I've updated his code with fixes since I can't add a comment.

; suppose table is a structure containing arrays
tnames = tag_names(table)
new_table = create_struct(tnames[0], (table.(0))[0])
for t = 1L, n_tags(table) - 1L do begin
   new_table = create_struct(new_table, tnames[t], (table.(t))[0])
endfor
new_table = replicate(new_table, n_elements(table.(0)))
for t = 0L, n_tags(table) - 1L do begin
   new_table.(t) = table.(t)
endfor
Andy
  • 91
  • 1
  • 4