0

The IDL code I'm trying to write in Python calculates a background noise from a series of values in function of its standard deviation. Here's the code:

; (INPUT)
; data = 1-D array of intensities
; (OUTPUT)
; bck,sig = background and fluctuations (1 sig level)
; ndata = number of values upon which bck,sig are computed
; POSITIVE = retains only data values > 0
; NSIG = number of sigmas

  if not(keyword_set(NSIG)) then nsig=3.    
  if keyword_set(POSITIVE) then begin
    test=where(data gt 0.)
    if test(0) ne -1 then data2=data(test) else begin
      bck=0 & sig=-1 & ndata=0 & goto,out
    endelse
  endif else data2=data
  if n_elements(data2) gt 1 then begin
    bck=mean(data2,/double) & sig=stddev(data2,/double)
  endif else begin
    bck=data2(0) & sig=0 & ndata=1 & goto,out
  endelse
  ndata=1

loop:
  test=where(abs(data2-bck) lt nsig*sig)
  if n_elements(test) eq 1 then goto,out
  ndata=n_elements(test)
  moy=mean(data2(test),/double)
  sig=stddev(data2(test),/double)
  if moy eq bck then goto,out
  bck=moy
  goto,loop

out:
  return
end

The heart of the code is the loop, and here is my attempt to replicate it:

def bg(array):
    temp = []
    for i in range(len(array)):
        if array[i]-np.mean(array) < 3*np.std(array):
            temp.append(array[i])
    avg=mean(temp)
    return avg

Is this correct? Is the original code only finding the mean of those values that are below 3*std?

What does the 1 in this line actually means? if n_elements(test) eq 1 then goto,out

veda905
  • 782
  • 2
  • 12
  • 32
Ricardo Fumachi
  • 79
  • 1
  • 11

1 Answers1

0

It looks like that is what the code is doing. It will only calculate the mean of the points that are under nsig.

if n_elements(test) eq 1 then goto,out is saying that if there is only one element in test then go to the out line which will then execute a return.

In general, goto statements should be avoided in IDL, they have a tendency to make the code very messy and hard to debug.

veda905
  • 782
  • 2
  • 12
  • 32