0

I have to create a fits file using the data from two IDL structures. This is not the basic problem.

My problem is that first I have to create a variable that contains the two structures. To create this I used a for loop that will write at each step a new row of my variable. The problem is that I cannot add the new row at the next step, it overwrite it so at the end my fits file instead of having, I don't know, 10000 rows, it has only one row.

This is what I also tried

for jj=0,h[1]-1 do begin

  test[*,jj] = [sme.wave[jj], sme.smod[jj]]
  print,test
endfor

but the * wildcard is messing up everything because now inside test I have the number corresponding to jj, not the values of sme.wave and sme.smod.

I hope that someone can understand what I asked and that can help me! thank you in advance!

Chiara

bonsaiviking
  • 5,825
  • 1
  • 20
  • 35

1 Answers1

0

Assuming your "sme.wave" and "sme.smod" structure fields contain 1-D arrays with the same number of elements as there are rows in "test", then your code should work. For example, I tried this and got the following output:

IDL> test = intarr(2, 10)  ; all zeros
IDL> sme = {wave:indgen(10), smod:indgen(10)*2}
IDL> for jj=0, 9 do test[*,jj] = [sme.wave[jj], sme.smod[jj]]
IDL> print, test
       0       0
       1       2
       2       4
       3       6
       4       8
       5      10
       6      12
       7      14
       8      16
       9      18

However, for better speed optimization, you should instead do the following and take advantage of IDL's multi-threaded array operations. Looping is typically much slower than something like the following:

IDL> test = intarr(2, 10)  ; all zeros
IDL> sme = {wave:indgen(10), smod:indgen(10)*2}
IDL> test[0,*] = sme.wave
IDL> test[1,*] = sme.smod
IDL> print, test
   0       0
   1       2
   2       4
   3       6
   4       8
   5      10
   6      12
   7      14
   8      16
   9      18

Further, if you don't know what the size of "test" is ahead of time, and you want to append to the variable, i.e. add a row, then you can do this:

IDL> test = []
IDL> sme = {wave:Indgen(10), smod:Indgen(10)*2}
IDL> for jj=0, 9 do test = [[test], [sme.wave[jj], sme.smod[jj]]]
IDL> Print, test
   0       0
   1       2
   2       4
   3       6
   4       8
   5      10
   6      12
   7      14
   8      16
   9      18
spacemanjosh
  • 641
  • 1
  • 5
  • 14