0

I'm completely new to coding and have almost no idea what I'm doing. Currently I'm trying to find the radius of each pixel from the center of a galaxy in a fits file. I was told to try this by creating a blank array the same size as the fits file and I'm trying to use a for loop for each x value and another for each y. So far, I have the array and have attempted to create the for loops.

xcenter =249.8
ycenter =250.0

d=fltarr(500,500)

for i=0,499 do begin
    d=d(i-xcenter,*)
endfor

for j=0,499 do begin
    d=d(j-ycenter,*)
endfor

I know this look awful and I obviously have no idea what I'm doing. So if anyone can offer any help at all I'd be grateful.

DavidH
  • 415
  • 4
  • 21
LexieStark
  • 11
  • 3

1 Answers1

2

Let's look at a simpler version first. Suppose you have 10 points on a line, and you want to calculate the distance of each point from some xref. IDL supports vector math, so you can do this:

xref = 5.4
x = dindgen(10)
distance = abs(x - xref)

I have used the DINDGEN command here, and you can look up the help. Now in a 2d case, you want 2-d arrays: one will be a 500 * 500 array containing the X coordinate of each pixel, the other containing the Y coordinate. So the arrays will have to be of the form,

0 1 2 3 ...
0 1 2 3 ...
0 1 2 3 ...

and

0 0 0 0 ...
1 1 1 1 ...
2 2 2 2 ...

We can generate them using the # operator. Note that IDL counts from 0 by default.

just_one = replicate(1d, 500) ; contains 1 1 1 1 ...
one_500 = dindgen(500)        ; contains 0.0 1.0 2.0 ...
x = just_one # one_500
y = one_500 # just_one

Now you can calculate the distance as usual, d = sqrt(xx + yy), but using vector math again:

distance = sqrt( (x - xref) ^ 2 + (y - yref) ^ 2 )

This is a 500x500 array, which contains the distance of each pixel from your xref, yref points.

VBB
  • 1,305
  • 7
  • 17