;+
; NAME:
;     FILTERAMF
; PURPOSE:
;     Routine to filter an array of AMF data (obtained from READAMF) so that
;     it contains only data for frequencies contained in the input frequency list.
; CATEGORY:
;     OVRO APC SUPPORT
; CALLING SEQUENCE:
;     amfout = filteramf(f,amf)
; INPUTS:
;     f		Array of frequencies (in GHz) for which to return data from AMF.  The
;             array F can contain more or fewer than the number of frequencies in AMF.
;             If a frequency is not found in AMF, the values for that frequency are NaN.
;     amf   The AMF data as returned by READAMF.
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
; ROUTINES CALLED:
; OUTPUTS:
; COMMENTS:
;     The purpose of this routine is to simplify comparison of data between two
;     AMF files that were taken with two different observing sequences (two different
;     sets of frequencies).
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 17-Jun-2007 by Dale E. Gary
;     18-Jun-2007  DG
;       Changed to allow more frequencies in F than in AMF.
;-
function filteramf,f,amf

   nf = n_elements(f)
   nfin = n_elements(amf[0,*,0,0])
;   if (nf gt nfin) then begin
;      print,'FILTER_AMF: Error, number of frequencies in frequency array larger than number in AMF array.'
;      return,-1
;   endif
   samf = size(amf)
   if (samf[0] eq 4) then begin
      amfout = fltarr(samf[1],nf,samf[3],samf[4])*!values.f_nan
   endif else if (samf[0] eq 3) then begin
      amfout = fltarr(samf[1],nf,samf[3])*!values.f_nan
   endif
   for i = 0, nf-1 do begin
      ifound = where(amf[0,*,0,0] eq f[i],nfound)
      if (nfound eq 1) then begin
         amfout[*,i,*,*] = amf[*,ifound,*,*]
      endif else begin
         amfout[*,i,*,*] = !values.f_nan  ; If frequency does not exist, set to NaN
      endelse
   endfor

return,amfout
end