pro interp_or, data, st_en_lin, st_img, en_img, qdebug=qdebug, trim_edge=trim_edge
;
;+
;NAME:
;	interp_or
;PURPOSE:
;	To intepolate the gaps caused by observing region strips
;INPUT/OUTPUT:
;	data	- The data cube
;INPUT:
;	st_en_lin 
;OPTIONAL INPUT:
;	st_img	- Starting image number to process
;	en_img	- Ending image number to process
;OPTIONAL KEYWORD INPUT:
;	trim_edge- If set, interpolate and replace the edge pixels
;		   as well as the gaps (because when POLY_2D does the
;		   sub pixel registration, it loses some signal)
;HISTORY:
;	Written 12-Aug-93 by M.Morrison
;	22-Sep-93 (MDM) - Added "st_img" and "en_img"
;	31-Jan-94 (MDM) - Modified to correct error when missing
;			  some PFI strips.
;			- Modified to use the correct lines when using
;			  the /TRIM_EDGE option (it was selecting lines
;			  two lines away, ie: trimming two lines since
;			  the code automatically trims one when selecting
;			  the lines to average)
;			- Corrected a scaling problem
;-
;
siz = size(data)
typ = siz( siz(0) + 1)
nexp = n_elements(st_en_lin(0,*,0))
nx = n_elements(data(*,0,0))
ny = n_elements(data(0,*,0))
n = n_elements(data(0,0,*))
;
off = 0
if (keyword_set(trim_edge)) then off = 1
if (n_elements(st_img) eq 0) then st_img = 0
if (n_elements(en_img) eq 0) then en_img = n-1
;
for iimg=st_img,en_img do begin
    for iexp=0,nexp-2 do begin
	ilin1 = st_en_lin(1, iexp, iimg)<st_en_lin(0,iexp+1,iimg) - off		;end line of the exposure
	ilin2 = st_en_lin(0, iexp+1, iimg) + off	;start line of next exposure
	gap =  ilin2 - ilin1 - 1
	if (keyword_set(qdebug)) then print, gap
	if (st_en_lin(0, iexp, iimg)+st_en_lin(1, iexp, iimg) eq 0) then gap = -1
	if (st_en_lin(0, iexp+1, iimg)+st_en_lin(1, iexp+1, iimg) eq 0) then gap = -1
	;
	if (gap gt 0) then begin
	    for ilin=ilin1+1, ilin2-1 do begin
		iulin1 = ilin1-1+off	;line #1 to be used for interpolation
		iulin2 = ilin2+1-off
		lin1 = data(*, iulin1, iimg)	;don't use the line just on the edge unless /TRIM is set
		lin2 = data(*, iulin2, iimg)
		if (typ eq 1) then lin1 = fix(lin1)	;make it integer since match does not work with byte type
		fraction = (ilin-iulin1) / float( iulin2-iulin1 )
		if (keyword_set(qdebug)) then print, 'ilin, ilin1, ilin2, fraction', ilin, ilin1, ilin2, fraction
		lin = lin1 + (lin2-lin1) * fraction
		data(*, ilin, iimg) = lin
	    end
	end
    end
end
;
end

