2015-04-24 236 views
1

從FITS-Table讀取數據,並獲取包含該表的Struct,其中每個標記表示一列。IDL - 結構陣列到結構陣列

有沒有辦法將數組的結構重新格式化爲結構數組? 這樣,數組中的一個Struct代表一個Row?


通過@mgalloy做了一般性的解決方案(見下文):

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 

回答

1

是的,所以你有這樣的事情?

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

創建新表,它定義了一個單行的外觀和複製它的適當次數:

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

你新:

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

然後列在複製表可以按行或列訪問:

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 
+0

再次感謝。我目前正試圖找到一種更通用的方法,因此它不依賴於標籤的名稱(如果甚至可能的話)。 – user3199134

1

你可以做到這一點不知道標記的名稱太(未經測試的代碼):

; 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 
+0

再次感謝,只是一些小的修復後,它沒有問題的作品。 我會將它添加到主註釋中。 – user3199134

0

從mgalloy的解決方案在幫助我。我已經使用修復程序更新了他的代碼,因爲我無法添加註釋。

; 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