如何计算IDL中数据的数量分布

时间:2018-06-12 12:27:44

标签: idl-programming-language

我有一个由时间和通量组成的数据(4117行x 2列)我想要计算并绘制与图片distribution of brightness variation相同的两个连续数据点对之间亮度变化的数量分布

这是我在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

当我运行代码时,dc的所有值都是4116。我认为我计算dc的方式是错误的。有任何建议以正确的方式做到这一点吗?

1 个答案:

答案 0 :(得分:0)

我很确定问题就在这一行:

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

在IDL中,a < b是&#34; a和b&#34;中的较小者的简写,而a > b同样是&#34; a和b&#34;中的较大者。所以现在,您的IF语句实际上被评估为:

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

并且因为非零浮点数在IDL中评估为TRUE,所以最终会这样:

IF TRUE or TRUE THEN begin

因为IF语句始终为true,所以c总是递增,dc的每个值最终都是4116。

要解决此问题,您需要使用关系运算符LTGT,它们返回TRUE或FALSE。换句话说,您的代码应为:

IF (df(j) LT dff(i+1)) or (df(j) GT dff(i)) THEN begin