pro filtprofile,lambda,ftype,fwid,ftnam,prof
; This routine constructs filter profiles on the wavelength interval lambda
; according to one of three types.
;  ftype=0 => boxcar filter, with unit response over entire range of lambda.
;  ftype=1 => gaussian filter centered at(lambda_min + lambda_max)/2., with
;             FWHM = fwid.
;  ftype=2 => arbitrary profile read from ascii file ftnam and interpolated
;             onto array lambda.  In this case, fwid is interpreted as a
;             offset (in stellar magnitudes) such that the magnitudes
;             returned by the filter are the nominal ones (with max transmission
;             equal to unity) + fwid.
;  Format for input file ftnam:  2 columns, giving lambda (nm) and relative
;           transmission (zero to 1.)
;           Lambda values must be monotonically increasing and uniformly spaced
;
; The filter profile is returned in array prof, which has as many elements
; as lambda does.

; get sizes of things
nlam=n_elements(lambda)

; boxcar case
if(ftype eq 0) then begin
  prof=fltarr(nlam)+1.
endif

; gaussian case
if(ftype eq 1) then begin
  lamc=(min(lambda)+max(lambda))/2.
  dif=lambda-lamc
  sigma=fwid/2.355
  arg=dif^2/(2.*sigma^2) < 40.
  prof=exp(-arg)
endif

; arbitrary case.  Read input data
if(ftype eq 2) then begin
  openr,iun,ftnam,/get_lun
  ss=''
  nn=0
  while(not eof(iun)) do begin
    readf,iun,ss
    nn=nn+1
  endwhile
  point_lun,iun,0
  plam=fltarr(nn)
  ppro=fltarr(nn)
  for i=0,nn-1 do begin
    readf,iun,llam,lpro
    plam(i)=llam
    ppro(i)=lpro
  endfor
  close,iun
  free_lun,iun

; interpolate the data onto desired grid, normalize
  lam0=min(lambda)
  l0=min(plam)
  dl=(max(plam)-l0)/(nn-1)
  jj=(lambda-l0)/dl
  prof=interpolate(ppro,jj,missing=0.,cubic=-0.5)
  prof=prof/max(prof)
  prof=prof*10.^(-0.4*fwid)
endif

end