1

I have a data consist of time and flux (4117 rows x 2 columns) I want to calculate and plot the number distribution of brightness variation between all pairs of two consecutive data points same as the picture distribution of brightness variation

This is the code I used in idl

nx=4117
t=fltarr(nx)
f=fltarr(nx)
df=fltarr(nx-1)
dt=fltarr(nx-1)
n=4116
dff=fltarr(n)
dc=fltarr(n-1)

data=read_table('data.dat')
;print,data(0,*) ;this is t (time)
;print,data(1,*) ;this is f (flux)

; Plot the light curve

window,0
plot,data(0,*)/data(0,0),data(1,*)/data(1,0),yrange=[0.93,1.1],ystyle=1 

; calculate the flux difference (dff)

for i=0,nx-2 do begin
df(i)=data(1,i+1)/data(1,0) - data(1,i)/data(1,0)
dt(i)=data(0,i+1)/data(0,0) - data(0,i)/data(0,0)
endfor

for i=0,n-1 do dff(i)=min(df)+i*(max(df)-min(df))/float(n-1.0)

print,dff

; calculate the number distribution (dc), I want the counter to reset to zero after every point and start to count again

for i=0,n-2 do begin
c=0.0
for j=0,nx-2 do begin
IF (df(j) < dff(i+1)) or (df(j) > dff(i)) THEN begin
c=c+1
dc(i)=c
endif
endfor

print, dc(i)
endfor

end

when I run the code all the value of dc is 4116 . I think the way I calculated dc is wrong. Any suggestion to do this in proper way?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
A.KT
  • 11
  • 4

1 Answers1

0

I'm pretty sure the problem is this line:

IF (df(j) < dff(i+1)) or (df(j) > dff(i)) THEN begin

In IDL, a < b is shorthand for "the lesser of a and b", and a > b is likewise "the greater of a and b". So right now, your IF statement is actually evaluated as:

IF (df(j) or dff(i+1), whichever is less) OR (df(j) or dff(i), whichever is more) THEN begin

and since non-zero floats evaluate as TRUE in IDL, it's ultimately this:

IF TRUE or TRUE THEN begin

Because the IF statement is always true, c is always incremented, and every value of dc ends up being 4116.

To fix this, you want to use the relational operators LT and GT, which return TRUE or FALSE. In other words, your code should read:

IF (df(j) LT dff(i+1)) or (df(j) GT dff(i)) THEN begin
Steve G
  • 182
  • 9