comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » 2D Histogram error
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
2D Histogram error [message #94293] Fri, 31 March 2017 04:58 Go to next message
mitali1203 is currently offline  mitali1203
Messages: 4
Registered: March 2017
Junior Member
Hello,
I am a newbie to IDL. I am currently working on event files (which are similar to FITS files) in IDL. I have written a code which will sort out event files from a list of directories and then compute detector plane histograms (DPH) (2D Histograms, basically) for each of those event files. In the process, I have also set up a 4-D array (4,25,64,64) and run a 'for' loop to save all DPH's to this array.
However, I keep getting this error "Expression must be an array in this context:H", at the line where I have used 'hist_2d' function. I heard this is because there may not be any events in that particular file; hence IDL does not have any data values to create histogram. I guess I have to put a conditional loop (if..else) in order to detect such null event files. I am getting confused as to how this problem can be solved. It would be of great help if someone can troubleshoot this problem.
Here is the code I have written- The line containing hist_2d function is where the error is-



function compute_dph, infile, outpath


dph=fltarr(4,25,64,64)

for quad=0,3 do begin

data=mrdfits(infile,quad+1, /unsigned,/dscale)

for ebin=0,24 do begin

index=where(data.energy gt 5+ebin*10 and data.energy le 5+(ebin+1)*10)

;result= sxpar(H,'NAXIS2')

;if result = 0 then dph(0,0,0,0)=qdph

;endelse

qdph=hist_2d(data(index).detx,data(index).dety,MAX1=63,MAX2= 63,MIN1=0,MIN2=0)

dph(quad,ebin,*,*)=qdph

;tv,bytscl(rebin(qdph,320,320,/sample))

endfor

endfor


names=strsplit(infile,'/', /EXTRACT)

stem=names(N_ELEMENTS(names)-1)

outfile=strmid(stem,0,srtlen(stem)-3)+'dph'

outfile=outpath+outfile

save,dph,filename=outfile



print,infile,outpath

return,0

end
Re: 2D Histogram error [message #94296 is a reply to message #94293] Fri, 31 March 2017 07:21 Go to previous messageGo to next message
wlandsman is currently offline  wlandsman
Messages: 743
Registered: June 2000
Senior Member
You need at least a 2 elements array to obtain a histogram.

IDL> print,hist_2d(-1,-1)
% HISTOGRAM: Expression must be an array in this context: H.
% Execution halted at: $MAIN$


So don't call hist_2d() unless you have at least 2 elements. --Wayne

if N_elements(index) GE 2 then begin

qdph=hist_2d(data(index).detx,data(index).dety,MAX1=63,MAX2= 63,MIN1=0,MIN2=0)
dph[quad,ebin,*,*]=qdp

endif
Re: 2D Histogram error [message #94297 is a reply to message #94293] Fri, 31 March 2017 07:58 Go to previous messageGo to next message
Markus Schmassmann is currently offline  Markus Schmassmann
Messages: 129
Registered: April 2016
Senior Member
On 03/31/2017 01:58 PM, mitali1203@gmail.com wrote:
> I am a newbie to IDL. I am currently working on event files (which
> are similar to FITS files) in IDL. I have written a code which will sort out
> event files from a list of directories and then compute detector plane
> histograms (DPH) (2D Histograms, basically) for each of those event
> files. In the process, I have also set up a 4-D array (4,25,64,64) and
> run a 'for' loop to save all DPH's to this array.
> However, I keep getting this error "Expression must be an array in
> this context:H", at the line where I have used 'hist_2d' function. I
> heard this is because there may not be any events in that particular
> file; hence IDL does not have any data values to create histogram. I
> guess I have to put a conditional loop (if..else) in order to detect
> such null event files. I am getting confused as to how this problem can
> be solved. It would be of great help if someone can troubleshoot this
> problem.
> Here is the code I have written- The line containing hist_2d
> function is where the error is-
>
> function compute_dph, infile, outpath
>
> dph=fltarr(4,25,64,64)
> for quad=0,3 do begin
> data=mrdfits(infile,quad+1, /unsigned,/dscale)
> for ebin=0,24 do begin
> index=where(data.energy gt 5+ebin*10 and data.energy le 5+(ebin+1)*10)
> qdph=hist_2d(data(index).detx,data(index).dety,MAX1=63,MAX2= 63,MIN1=0,MIN2=0)
> dph(quad,ebin,*,*)=qdph
> endfor
> endfor
>
> names=strsplit(infile,'/', /EXTRACT)
> stem=names(N_ELEMENTS(names)-1)
> outfile=strmid(stem,0,srtlen(stem)-3)+'dph'
> outfile=outpath+outfile
> save,dph,filename=outfile
>
> print,infile,outpath
> return,0
> end
Hi,

the following within the innermost loop should work:

index=where( (data.energy gt 5+ebin*10) and (data.energy le
5+(ebin+1)*10) ,cnt)
if cnt ne 0 then
dph[quad,ebin,*,*]=hist_2d(data(index).detx,data(index).dety ,MAX1=63,MAX2=63,MIN1=0,MIN2=0)


or assuming 0 <= detx,dety <=63 & detx,dety are integers you could put
instead of the inner loop

dat=64L*27L*data.dety+27L*data.detx+(0L>ceil((data.energy-5)/10) <26L)
dat=[reform(dat,n_elements(dat),/overwrite),64L*64L*27L-1L]
dph[0,*,*,*]=( reform(histogram(dat),[27,64,64]) )[1:25,*,*]

The latter I have not debugged.

I hope this helps, Markus
Re: 2D Histogram error [message #94298 is a reply to message #94296] Fri, 31 March 2017 08:14 Go to previous messageGo to next message
Markus Schmassmann is currently offline  Markus Schmassmann
Messages: 129
Registered: April 2016
Senior Member
On 03/31/2017 04:21 PM, wlandsman wrote:
> You need at least a 2 elements array to obtain a histogram.
IDL> hist_2d([1],[2])

doesn't fail
Re: 2D Histogram error [message #94301 is a reply to message #94296] Sat, 01 April 2017 08:50 Go to previous message
mitali1203 is currently offline  mitali1203
Messages: 4
Registered: March 2017
Junior Member
Thanks a lot! This worked. Thank you once again
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: =?UTF-8?B?UmU6INis2K/ZiNmEINin2YXYqtit2KfZhtin2Kog2KfZhNir2KfZhtmI2YrYqSDYp9mE?= عامة المعدل الجديد لعام 2017
Next Topic: estimation matrix elements in a vector

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 07:07:05 PDT 2025

Total time taken to generate the page: 0.00498 seconds