	function width_interpolate,index,x,y,ymax

index0=index(0)	&	index1=index(n_elements(index)-1)

index01=index0-1 > 0
index11=index1+1 < (n_elements(y)-1)

ymin0=y(index0)	&	ymin1=y(index01)
ymax0=y(index1)	&	ymax1=y(index11)

IF ymin1 eq ymin0 then x0=(x(index0)+x(index01))/2. else $
x0=x(index01)+(0.5*ymax-ymin1)*(x(index01)-x(index0))/(ymin1-ymin0)
IF ymax1 eq ymax0 then x1=(x(index1)+x(index11))/2. else $
x1=x(index1)+(ymax0-0.5*ymax)*(x(index11)-x(index1))/(ymax0-ymax1)

return,[x0,x1]
	end


	function fwhm,x,y,x_peak=x_peak,i_peak=i_peak, $
		full=full,error=error,follow=follow

;+	Function FWHM returns value of full width at half maximum
;	(FWHM) for input curve with respect to zero level. If there
;	is only one input argument, then FWHM is calculated in
;	subscript values. If two arguments are given, then the first
;	(X) is interpreted as abscissae and second (Y) as the curve
;	to measure. In this latter case FWHM is calculated in X values.
;		When no keyword parameter is specified, FWHM is
;	calculated for the area around the highest value of the input
;	curve.
;		Keyword parameters:
;		I_PEAK - subscript of the input array corresponding
;	to the area of the peak of interest (I_PEAK is not
;	necessary subscript of a maximum peak value).
;		X_PEAK - any abscissa corresponding to the area of
;	the peak of interest (X_PEAK is not necessary abscissa of
;	a maximum peak value).
;		FULL - if this keyword is set and non-zero, then full
;	width FWHM is returned with no respect to existence and values
;	of any local minimums.
;		ERROR - specifies name of the variable to place error
;	message "No maximum" if it is the case.
;-

WIDGET_CONTROL,/HOUR

error=''

x_save=x

	if n_params() lt 2 then begin
y=x	&	x=findgen(n_elements(y))
	endif

	CASE 1 OF

n_elements(x_peak) gt 0:	N_peak=(where(x ge x_peak))(0)
n_elements(i_peak) gt 0:	N_peak=i_peak

	ELSE:		ymax=max(y,N_peak)

	ENDCASE

	if N_peak(0) lt 0 then begin
print,'N_peak(0)=',N_peak(0)
error='No maximum'
goto,exit
	endif

peak=select_peak(y,N_peak)
ymax=max(y(peak(0):peak(1)),N_peak)
N_peak=N_peak+peak(0)
;PRINT,'N_peak=',N_peak

	IF keyword_set(follow) THEN BEGIN

j=N_peak
while (y(j) ge ymax/2.) and (j ge 1) do j=j-1
if j eq 0 then jl=0 else jl=j+1

j=N_peak
while (y(j) ge ymax/2.) and (j le n_elements(y)-2) do j=j+1
if j ne (n_elements(y)-1) then j=j-1

index=[jl,j]
;print,'index=',index
	goto, A1

	ENDIF


index=where(y gt ymax/2.)

	if n_elements(index) eq 1 then if index(0) eq -1 then begin
width=x(n_elements(x)-1)-x(0)
;print,'Nindex=',n_elements(index),'index(0)=',index(0)
error='No maximum'
goto,exit
	endif

	IF not keyword_set(full) THEN BEGIN

split_array,index,first_subscript=Sf,last_subs=Sl,number=n

for j=0,n-1 do if N_peak ge Sf(j) and N_peak le Sl(j) then $
	index=indgen(Sl(j)-Sf(j)+1)+Sf(j)
;print,'indexfull=',index
	ENDIF

A1:

xw=width_interpolate(index,x,y,ymax)
;print,'xw=',xw
width=(xw(1)-xw(0))

exit:
if error ne '' then print,error
x=x_save
return,width
end
