How to Find the Sum of Array Elements Using Idl\Gdl Program?

I am using readfits.pro program to read the FITS file which is giving array of struct type. Which program should I use so that I can find the sum of the elements the obtained array?

2

2 Answers

The TOTAL function might be what you need. If your structure has a field "field1" and you want to add up those values from the structures in your array "structArray", this should work:

field1Total = Total(structArray.field1)

10

I can answer for the case of using MRDFITS, which you described in the comments of Dick Jackson's answer,

b=mrdfits('/home/bhuvi/Desktop/data/S60501010021alif4ttagfca‌​l (2).fit',
1,range=[3112,3114]) MRDFITS: Binary table. 1 columns by 3 rows. 

GDL> print,b { 1.61571e-13}{ 1.06133e-13}{ 1.06137e-13}

What I think you are getting is an array of structures. And it looks like each structure has a single field, populated with a single float. To illustrate, I defined an array of those structures, using your values for b, and I arbitrarily named the field "data":

b= [{data:1.61571e-13},{data:1.06133e-13},{data:1.06137e-13}]

I get the same output as you when I print it:

IDL> print, b
{  1.61571e-13}{  1.06133e-13}{  1.06137e-13}

So, I'm pretty sure this is what your data looks like. To check it for yourself, help is your friend.

IDL> help, b
B                     STRUCT   = -> <Anonymous> Array[3]

This tells you that b is an array of structures. You can pass the /structure keyword (/str for short) to get info on makeup of the structures within the array:

IDL> help, b, /str
** Structure <beb6f8>, 1 tags, length=4, data length=4, refs=1:
   DATA            FLOAT       1.61571e-13

This says that the first element of the array b, is a structure with a field called 'data', which points to a float value of 1.61571e-13. Alternately, you could just use help with the individual structures by indexing the array b:

IDL> help, b[0]
** Structure <beb6f8>, 1 tags, length=4, data length=4, refs=2:
   DATA            FLOAT       1.61571e-13
IDL> help, b[1]
** Structure <beb6f8>, 1 tags, length=4, data length=4, refs=2:
   DATA            FLOAT       1.06133e-13
IDL> help, b[2]
** Structure <beb6f8>, 1 tags, length=4, data length=4, refs=2:
   DATA            FLOAT       1.06137e-13

I find arrays of structures to be super useful because you can easily look at an individual structure, or you can easily make an array out of a particular field from all the structures. In other words, to get at your data, simply use the structure.field notation, and you have a vector made of the floats from each of the three structures in the array:

IDL> print, b.data
  1.61571e-13  1.06133e-13  1.06137e-13

Finally, to get the sum, use total() as Dick Jackson suggested:

IDL> print, total(b.data)
  3.73841e-13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest