
;+
; 
;             NAME : eis_search_rasters_driver
; 
;          PURPOSE : To search the EIS raster database.
;
; CALLING SEQUENCE : eis_search_rasters_driver,wavelengths,slit_index,$
;                         rastertype
; 
;           INPUTS : wavelengths - an array of wavelengths to look for
;                    slit_index  - (0-1", 1-266", 2-2", 3-40")
;                    raster_type - (0 - scan, 1 - SNS)
;
;  OPTIONAL INPUTS : none
; 
;         KEYWORDS : exposure_time_min, exposure_time_max - limit the
;                    range of exposure times (in seconds). Note that
;                    only the minimum exposure time is considered.
;                    Some rasters take multiple exposures with
;                    different exposure times.
;
;                  : data_rate_min, data_rate_max - limit the data
;                    rate (in kilobits/second).  
;
;                  : data_volume_min, data_volume_max - limit the data
;                    volume (in megabits).
;
;                    Note that the actual data rate is difficult to
;                    determine a priori, compression factors can be
;                    highly variable. Compression factors are
;                    determined for the quiet Sun.
;
;                  : compression - the compression index. see
;                    eis_get_compression_factor for details.
;
;                  : raster_height_min, raster_height_max,
;                    raster_width_min, raster_width_max, - limit the
;                    size of the raster. The raster width search
;                    options only work for scanning rasters (not sit
;                    and stare).
; 
;          OUTPUTS : The number of rasters found is returned
;
; OPTIONAL OUTPUTS : rasters - the raster definition
;                    lines   - the line list definition
;                    struct  - some ancillary info (such as the data rate)
;
;    COMMON BLOCKS : none
; 
;          EXAMPLE : IDL> nfound = eis_search_rasters_driver([280.15,0,0])
;                    IDL> print,nfound
;                         23
;
;    SPECIAL CALLS : eis_find_lineraster 
;                    eis_get_compression_factor
;
;          WRITTEN : HPW : 19-JAN-2008, written
;                    HPW : 23-JAN-2008, raster widths and heights added
;                    HPW : 08-AUG-2008, corrected call to eis_get_compression_factor
;                    HPW : 11-AUG-2008, added data volume calculation
; 
;-

function eis_search_rasters_driver,wavelengths,slit_index,raster_type,$
                                   exposure_time_min=exposure_time_min,$
                                   exposure_time_max=exposure_time_max,$
                                   data_rate_min=data_rate_min,$
                                   data_rate_max=data_rate_max,$
                                   data_volume_min=data_volume_min,$
                                   data_volume_max=data_volume_max,$
                                   compression=compression,$
                                   raster_height_min=raster_height_min,$
                                   raster_height_max=raster_height_max,$
                                   raster_width_min=raster_width_min,$
                                   raster_width_max=raster_width_max,$
                                   rasters=rasters,lines=lines,struct=struct,$
                                   test_array=test_array

  default_target = 'quiet sun'

  ;; ----------------------------------------------------------------------
  ;; --- find all rasters with the specified wavelengths

  acronym = eis_find_lineraster(wavelengths,rasters=rasters,lines=lines,$
                                summary=summary)

  if acronym[0] eq '' then return,0 else nrasters = n_elements(acronym) 

  ;; ----------------------------------------------------------------------
  ;; --- collect more information

  struct = {id:                  0L,$
            slit_index:           0,$
            exposure_time:      0.0,$
            raster_type:         0b,$
            data_rate:          0.0,$
            data_volume:        0.0,$
            compression:          0,$
            raster_height:      0.0,$
            raster_width:       0.0}
  struct = replicate(struct,nrasters)

  for i=0,nrasters-1 do begin

    struct[i].id          = rasters[i].id
    struct[i].slit_index  = rasters[i].slitindex
    struct[i].raster_type = rasters[i].rastertype
    struct[i].compression = rasters[i].compression

    ;; --- exposure time (in seconds), look only at the minimum
    exposure_time = rasters[i].exposures[0:rasters[i].nexp-1]/1000.
    struct[i].exposure_time = min(exposure_time)

    ss = struct[i].slit_index
    target = default_target
    compression_factor    = eis_get_compression_factor(rasters[i].compression,ss,target=target)

    ;; --- compute data volume in kilobits
    struct[i].data_volume = compression_factor*float(rasters[i].volume)/1024.

    ;; --- compute data rate in kilobits per second
    struct[i].data_rate   = struct[i].data_volume/(float(rasters[i].duration)/1000.)

    ;; --- convert data volume to megabits
    struct[i].data_volume = struct[i].data_volume/1024.

    struct[i].raster_height = rasters[i].windheight
    struct[i].raster_width  = eis_get_total_raster_width(rasters[i])
    
  endfor

  ;; ----------------------------------------------------------------------
  ;; --- search structure
  
  s = 'slit_index='+trim(slit_index)
  test_array = s

  s = 'raster_type='+trim(raster_type)
  test_array = [test_array,s]

  if keyword_set(exposure_time_min) then begin
    s = 'exposure_time>='+trim(exposure_time_min,'(f10.1)')
    test_array = [test_array,s]
  endif
  
  if keyword_set(exposure_time_max) then begin
    s = 'exposure_time<='+trim(exposure_time_max,'(f10.1)')
    test_array = [test_array,s]
  endif

  if keyword_set(data_rate_min) then begin
    s = 'data_rate>='+trim(data_rate_min,'(f10.1)')
    test_array = [test_array,s]    
  endif    

  if keyword_set(data_rate_max) then begin
    s = 'data_rate<='+trim(data_rate_max,'(f10.1)')
    test_array = [test_array,s]    
  endif    

  if keyword_set(data_volume_min) then begin
    s = 'data_volume>='+trim(data_volume_min,'(f10.1)')
    test_array = [test_array,s]    
  endif    

  if keyword_set(data_volume_max) then begin
    s = 'data_volume<='+trim(data_volume_max,'(f10.1)')
    test_array = [test_array,s]    
  endif    

  if keyword_set(compression) then begin
    s = 'compression='+trim(compression)
    test_array = [test_array,s]    
  endif    

  if keyword_set(raster_height_min) then begin
    s = 'raster_height>='+trim(raster_height_min)
    test_array = [test_array,s]    
  endif

  if keyword_set(raster_height_max) then begin
    s = 'raster_height<='+trim(raster_height_max)
    test_array = [test_array,s]    
  endif

  if raster_type eq 0 then begin
    if keyword_set(raster_width_min) then begin
      s = 'raster_width>='+trim(raster_width_min)
      test_array = [test_array,s]    
    endif
    if keyword_set(raster_width_max) then begin
      s = 'raster_width<='+trim(raster_width_max)
      test_array = [test_array,s]    
    endif
  endif

  hprint,test_array

  match = struct_where(struct,nfound,test_array=test_array,/quiet)

  if nfound gt 0 then begin
    rasters = rasters[match]
    lines   = lines[match]
    struct  = struct[match]
  endif else begin
    rasters = -1
    lines   = -1
    struct  = -1
  endelse

return,nfound
end
