我正在使用readfits.pro程序来读取给出结构类型数组的 FITS 文件。我应该使用哪个程序才能找到获得的数组元素的总和?
2 回答
TOTAL 函数可能是您需要的。如果您的结构有一个字段“field1”并且您想从数组“structArray”中的结构中添加这些值,这应该可以:
field1Total = Total(structArray.field1)
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/S60501010021alif4ttagfcal (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