;+
; NAME:
;       BSC_VALID
; PURPOSE:
;       Extract Valid BCS Bins from user specified channels.
;       Returned data will have the valid wavelength bins selected.
;
; CALLING SEQUENCE:
;       BSC_VALID,BSC_INDEX,BSC_DATA,CHAN=CHAN,SS=SS
;       BSC_VALID,BSC_INDEX,BSC_DATA,NINDEX,NDATA,CHAN=CHAN
;       BSC_VALID,BSC_INDEX,BSC_DATA,NINDEX,NDATA,CHAN=CHAN,/NOVALID
; INPUTS:
;       BSC_INDEX    - BSC index structure
;       BSC_DATA     - BSC data structure
; OUTPUTS:
;       VALID_INDEX  - BSC index structure updated for valid bins
;       VALID_DATA   - BSC data structure  updated for valid bins
; KEYWORDS:
;       CHAN (in)    - BCS channel (def is all)
;       SS   (out)   - indicies matching requested channel
;	NOVALID (in) - if set, do not try to extract valid data
; PROCEDURE:
;       Simple use of WHERE
; HISTORY:
;       11-Sep-93, D. Zarro (ARC) and J. Lemen (LPARL), Written.
;       10-May-94, Zarro, fixed potential bugs in SS and NBIN usage
;       15-Jul-94, Zarro, checked if curvature correction is applied.
;                         (if not, then extract all bins)
;       1-Sep-94, Zarro, fixed potential bug with UNIQ
;-

pro bsc_valid,bsc_index,bsc_data,nindex,ndata,chan=chan_struct,$
              ss=ss,novalid=novalid

on_error,1

ok=bsc_check(bsc_index,bsc_data)
if not ok then 		$
 message,'usage --> BSC_VALID,BSC_INDEX,BSC_DATA,NINDEX,NDATA,CHAN=CHAN,SS=SS

if n_elements(chan_struct) eq 0 then begin
   ss=indgen(n_elements(bsc_index))
endif else begin
   chan_uniq = gt_bsc_chan(chan_struct,/uniq)   	; Get uniq user supplied channels
   chan = gt_bsc_chan(bsc_index)        	  	; Get BSC channels
   ss = -1
   for i=0, n_elements(chan_uniq)-1 do ss = [ss,where(chan eq chan_uniq(i))]
endelse
chk=where(ss ne -1,count)
if count eq 0 then message,'requested channel(s) not present in BSC_DATA'
ss=ss(chk)

nindex = bsc_index(ss)
ndata  = bsc_data(ss)

tags=tag_names(nindex.bsc)
vtag=where(tags eq 'VALID_ANS',count)

if (count eq 0) or keyword_set(novalid) then return

; --------------------------------------------------------
; Find all cases curvature correction has been applied and valid has not 
; update index and data structures
; --------------------------------------------------------


ii = where((nindex.bsc.valid_ans eq 0) and (nindex.bsc.curve_ans eq 2), count)		; Find all non-corrected cases
if count gt 0 then begin
   message,'extracting valid wavelength bins',/info

   ntags = n_elements(tag_names(ndata(0)))		; Get the number of tags

   uchans = gt_bsc_chan(nindex(ii),/uniq)               ; Get the unique channel numbers
   chans = gt_bsc_chan(nindex(ii))		        ; Get all the channel numbers

   modes = nindex(ii).bsc.modeid			; Get all the modes

   if n_elements(modes) eq 1 then modes=[modes]
   umodes = modes(uniq(modes,sort([modes])))       	; Get all the unique modes

   for i=0,n_elements(umodes)-1 do begin		; Loop over unique modes
       for j=0,n_elements(uchans)-1 do begin		; Loop over channels

           s1 = where((modes eq umodes(i)) and (chans eq uchans(j)),ncount)	; Find all cases for this channel
           if ncount gt 0 then begin
		bincal = gt_bsc_bincal(uchans(j),umodes(i))     	
		nindex(ii(s1)).bsc.valid_ans = 1				; Set the valid_ans switch

		nindex(ii(s1)).bsc.nstart = bincal.nstart			; Set start bin
		nindex(ii(s1)).bsc.nend   = bincal.nend				; Set stop bin
                nbin=bincal.nend-bincal.nstart+1
		nindex(ii(s1)).bsc.nbin   = nbin
		for k=0,ntags-1 do begin				; Shift data so that 1st valid bin is (0:*)
		   for m=0,n_elements(s1)-1 do begin				; Loop over tags in data structure
		     temp = ndata(ii(s1(m))).(k) * 0      	; Set up temporary variable
		     temp(0) = (ndata(ii(s1(m))).(k))(bincal.nstart:bincal.nend)
		     ndata(ii(s1(m))).(k) = temp
		   endfor						; m=0,n_elements(s1)-1
		endfor							; k=0,ntags-1
           endif							; ncount gt 0
       endfor       
   endfor
endif							; count gt 0 then begin


;-- record version number of BCS CALFIL used

tags=tag_names(nindex.bsc)
look=where('CALFIL_VERS' eq tags,vcount)
bincal=gt_bsc_bincal(nindex(0))
if vcount gt 0 then nindex.bsc.calfil_vers=bincal.version

return & end

