;+
; NAME:
;     DB_FILTER
; PURPOSE:
;	Given a Nobeyama AR database, a search criterion, and a range of
;	the parameter, returns indices of those entries where the parameter
;	is within the given range, or outside the range, if
;	/INVERSE keyword is set.
;
; CATEGORY:
; CALLING SEQUENCE:
;
;         index = db_filter(db,criter,range,dbout)
;
; INPUTS:
;     db      the AR database to filter
;     criter  the parameter of search: 'Name', 'Area', 'Type',
;             'Latitude', 'CarrLng','Longitude', 'Polarization', 'Tbr'
;              Only first 4 letters are significant.
;              CRITER is case-insensitive.
;     range   the range of parameter values
;
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
;
;      inverse		if set, entries (or regions) OUTSIDE of given range are
;                   	returned.
;      exhaustive       a name of the variable to receive two-dimensional
;			index of the regions found (1st - number of the entry,
;			2nd - number of the active region)
;      exlusively	if set, only the active regions whose type match
;			exactly to the given criterion are retrived.
;      absolute_value   if set, absolute value of the criterion range is used.
;      follower  	if set, use criterion in follower region data
;      map       	if set, use criterion for map peak data
;      leader    	if set, use criterion in leader region data (default)
;      type    		if criter="Polarization" or "Tbr" specifies which type
;              		of location the polarization and Tbr should
;              		be compared with range.  Possible choices are:
;	       for Criterion='Tbr':
;    	        type       keyword	selection
;               II, IV     map		I or V range at I peak of map
;               VI, VV     map          I or V range at V peak of map
;               II, IV     follower     I or V range at I peak of follower
;               VI, VV     follower     I or V range at V peak of follower
;               II, IV     leader       I or V range at I peak of leader
;               VI, VV     leader       I or V range at V peak of leader
;	       for Criterion='Polarization':
;             type       keyword	selection
;               I          map		Polarization at I peak of map
;               V          map          Polarization at V peak of map
;               I          follower     Polarization at I peak of follower
;               V          follower     Polarization at V peak of follower
;               I          leader       Polarization at I peak of leader
;               V          leader       Polarization at V peak of leader
;
; ROUTINES CALLED:
; OUTPUTS:
;     dbout   a new AR database containing the selected subset
;
; OPTIONAL (KEYWORD) OUTPUT PARAMETERS:
;      error - 1 if no entries found
;
; COMMENTS:
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 24 Nov 1996 by Dale E. Gary
;-

function db_filter, db, criter, range, dbout, inverse=inverse, $
	error=error, exhaustive=exhaustive, absolute_value=absolute_value, $
	type=type, follower=follower, map=map, exclusively=exclusively, $
	leader=leader, array=array

; *********************************************

if n_params() lt 3 then message,'Insufficient arguments'

Criterion=strmid(strcompress(strupcase(criter), /rem),0,4)

if n_elements(Range) gt 1 then RangeS=Range(sort(Range)) else RangeS=Range

	if n_elements(type) le 0 then begin
if Criterion eq 'POLA' then p_type = 'I'
if Criterion eq 'BRIG' then p_type = 'II'
	endif else p_type=strcompress(strupcase(type), /rem)


	CASE Criterion OF

'NAME':	temp=db.region.name

'AREA':	temp=db.region.area

'LONG':	temp=db.region.location.lng

'LATI':	temp=db.region.location.lat

'CARR':	temp=db.region.carrlng

'TYPE':	begin
	Sz=size(Range)
var_type=Sz(Sz(0)+1)

AR_type=['ALPHA', 'BETA', 'GAMMA', 'DELTA']

if var_type ne 7 then RangeS=AR_type(RangeS)
RangeS=strcompress(strupcase(RangeS), /rem)

ind=0

	temp=strcompress(db.region.type, /rem)

	IF keyword_set(exclusively) THEN $
for j=0,n_elements(RangeS)-1 do ind=[ind, where(temp eq RangeS(j))] ELSE $
for j=0,n_elements(RangeS)-1 do ind=[ind, where(strpos(temp, RangeS(j)) ge 0)]

ind=ind(1:*)
ind=ind(sort(ind))

	end

'POLA':	begin

		CASE 1 OF

	keyword_set(follower):	begin
	if p_type eq 'V' then $
	temp=db.region.follower.vpeak.vtb/(db.region.follower.vpeak.itb > 100) else $
	temp=db.region.follower.ipeak.vtb/(db.region.follower.ipeak.itb > 100)

				end

	keyword_set(map):	begin
	if p_type eq 'V' then $
	temp=db.map.vpeak.vtb/(db.map.vpeak.itb > 100) else $
	temp=db.map.ipeak.vtb/(db.map.ipeak.itb > 100)
				end

		ELSE:	begin
	if p_type eq 'V' then $
	temp=db.region.leader.vpeak.vtb/(db.region.leader.vpeak.itb > 100) else $
	temp=db.region.leader.ipeak.vtb/(db.region.leader.ipeak.itb > 100)
			end

		ENDCASE

	end


'BRIG':	begin

		CASE 1 OF

	keyword_set(follower):	begin

			CASE p_type OF
		'IV': temp=db.region.follower.ipeak.vtb
		'VI': temp=db.region.follower.vpeak.itb
		'VV': temp=db.region.follower.vpeak.vtb
			ELSE: temp=db.region.follower.ipeak.itb
			ENDCASE

				end

	keyword_set(map):	begin

			CASE p_type OF
		'IV': temp=db.map.ipeak.vtb
		'VI': temp=db.map.vpeak.itb
		'VV': temp=db.map.vpeak.vtb
			ELSE: temp=db.map.ipeak.itb
			ENDCASE
				end

		ELSE:	begin

			CASE p_type OF
		'IV': temp=db.region.leader.ipeak.vtb
		'VI': temp=db.region.leader.vpeak.itb
		'VV': temp=db.region.leader.vpeak.vtb
			ELSE: temp=db.region.leader.ipeak.itb
			ENDCASE
			end

		ENDCASE

	end


'MAGN':	begin

		CASE 1 OF

	keyword_set(follower):	temp=db.region.follower.kgauss

	keyword_set(map):	temp=db.map.kgauss

		ELSE:		temp=db.region.leader.kgauss

		ENDCASE

	end



ELSE:   temp=db.region.name

	ENDCASE


IF Criterion ne 'TYPE' THEN BEGIN
	if keyword_set(absolute_value) then temp=abs(temp)
	ind=where(temp ge RangeS(0) and temp le RangeS(n_elements(RangeS)-1))
ENDIF

	Length=n_elements(temp)


error=ind(0) lt 0

N_db=n_elements(db)

	if error then begin
if keyword_set(inverse) then Record=lindgen(N_db) else Record=0
exhaustive=[0,0]
goto, Err0
	endif


M=Length/N_db
index=transpose([[ind mod M],[ind/M]])

Record=transpose(index(1,*))
exhaustive=index

	if keyword_set(inverse) then begin
AA=lonarr(Length)
AA(ind)=1
ind=where(AA eq 0)

index=transpose([[ind mod M],[ind/M]])

exhaustive=index
AA=lonarr(N_db)
AA(Record)=1
Record=where(AA eq 0)

	endif

Record=Record(uniq(Record))

Err0:

exhaustive=transpose(exhaustive([1,0],*))


	if n_params() eq 4 then dbout=db(Record > 0)

return, Record


end