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?
-
add what you have tried so far – Abdul Jun 13 '17 at 06:00
-
I have tried checksum32.pro and tsum.pro but both are giving error that array of struct type cannot be added. – Arya Stark Jun 13 '17 at 06:07
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)

- 71
- 4
-
Again there is "struct elements cannot be added in this context" error – Arya Stark Jun 14 '17 at 08:05
-
I don't know your variable names, but in this context, what do you get from `HELP, structArray, /STRUCTURE` and `HELP, structArray.field1`? Second issue: looking at readfits.pro, it should return a numeric array, not an array of struct type. Please show a little code, at least your call to READFITS. – Dick Jackson Jun 14 '17 at 14:40
-
`GDL> 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}` – Arya Stark Jun 15 '17 at 07:59
-
`GDL> sum1=total(b) % $MAIN$: Struct expression not allowed in this context: B % Execution halted at: $MAIN$ ` – Arya Stark Jun 15 '17 at 08:01
-
-
`GDL> a=readfits('/home/bhuvanahebbar/Desktop/data/S60501010021alif4ttagfcal (2).fit',exten_no=1,startr=3112,numr=2) % READFITS: Now reading 4 by 2 array GDL> print,a 42 53 233 180 41 238 253 136` – Arya Stark Jun 15 '17 at 08:06
-
`GDL> help,b.field1 % Tag name: FIELD1 is undefined for STRUCT. % Execution halted at: $MAIN$` – Arya Stark Jun 15 '17 at 08:22
-
You may want to study how structures, fields and arrays work until you are really comfortable with them. See IDL examples in online help for Arrays of Structures, or ask someone. Following my first comment, your array of structures (`structArray`) that came from MRDFITS would be `b`, so what do you get from `HELP, b, /STRUCTURE`? It will list at least one field name, so then try `HELP, b._thatFieldName_` and I think `fieldTotal = Total(b._thatFieldName_)` should work to add the three numbers you had read. – Dick Jackson Jun 17 '17 at 01:01
-
I don't know whether your FITS file requires other parameters to your READFITS or MRDFITS calls to get the data correctly—you may need to check that. It looks like you got a Byte array of size (4, 2) from when READFITS read two rows into `a`, and each set of four bytes do in fact make up the Float values (`IDL> print,float([180b, 233b, 53b, 42b], 0, 1)` gives `1.61571e-013`). Are you expecting Float values in that range? I bet that `fieldTotal = Total(Float(a, 0, 2))` (because there are 2 rows in `a`) would give the total of those two numbers you read in. – Dick Jackson Jun 17 '17 at 01:02
-
Also, may I add that you gave incorrect details in your original question ("readfits.pro" does not give structure result, "mrdfits.pro" does). You will get better help if you are more careful with your questions. – Dick Jackson Jun 17 '17 at 01:02
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

- 182
- 9