;+
; Project     :	SOHO - CDS
;
; Name        :	FIND_CDS_STUDY
;
; Purpose     :	find a CDS study in database
;
; Explanation :	searches on TITLE, OBS_PROG, and SV_DESC fields
;
; Use         :	FIND_CDS_STUDY,STUDIES
;
; Inputs      :	None.
;
; Opt. Inputs :	None.
;
; Outputs     :	STUDIES = study definitions found
;
; Opt. Outputs:	None.
;
; Keywords    :	TITLE = string title to search on
;               OBS_PROG = obs_prog to search on
;               SV_DESC = study variation description to search on
;               ERR = any error messages
;               NFOUND= number of entries found
;               STUDY_ID = study_id to search
;               FUND_ONLY = only look at Fundamental part
;               USE_TITLE = treat STUDY_ID as TITLE_ID
;               RELIST = relist fundamental studies
;
; Calls       :	LIST_F_STUDY, LIST_V_STUDY
;
; Common      :	FIND_CDS_STUDY
;
; Restrictions:	None.
;
; Side effects:	None.
;
; Category    :	Planning, Database.
;
; Prev. Hist. :	None.
;
; Written     :	Dominic Zarro (ARC)
;
; Version     :	Version 1.0, 8 May 1995
;-

pro find_cds_study,studies,title_name=title,sv_desc=sv_desc,obs_prog=obs_prog,$
 err=err,study_id=study_id,fund_only=fund_only,$
 title_id=title_id,nfound=nfound,det=det,relist=relist

common find_cds_study,fstudies,sav_sdb

err=''
nfound=0

if n_params() ne 1 then begin
 err='syntax --> FIND_CDS_STUDY,STUDIES,[TITLE=TITLE,OBS_PROG=OBS_PROG...]'
 message,err & return
endif

;-- get fundamental studies

;-- check if database changed

if datatype(sav_sdb) ne 'STR' then sav_sdb=getenv('ZDBASE')
cur_db=getenv('ZDBASE')
if cur_db ne sav_sdb then begin
 message,'ZDBASE changed!!!',/contin
 delvarx,fstudies & sav_sdb=cur_db
endif

if keyword_set(relist) then delvarx,fstudies
if (datatype(fstudies) ne 'STC') then list_f_study,fstudies,nfound,err=err
if err ne '' then begin
 message,err,/contin & delvarx,studies & return
endif

;-- now do search

;-- start with STUDY_ID

sorder=sort(fstudies.study_id)
studies=fstudies(sorder) & nfound=n_elements(studies)
if n_elements(study_id) ne 0 then begin
 clook=where(studies.study_id eq study_id,nfound)
 if nfound gt 0 then studies=studies(clook) else begin
  err='No studies with matching STUDY_ID - '+num2str(study_id)
  message,err,/contin & delvarx,studies & return
 endelse
endif

;-- search on TITLE_ID

if n_elements(title_id) ne 0 then begin
 clook=where(studies.title_id eq title_id,nfound)
 if nfound gt 0 then studies=studies(clook) else begin
  err='No studies with matching TITLE_ID - '+num2str(title_id)
  message,err,/contin & delvarx,studies & return
 endelse
endif

;-- search on TITLE

if datatype(title) eq 'STR' then begin
 title=strupcase(strtrim(title,2))
 if title ne '' then begin
  clook=where(strpos(strupcase(studies.title),title) gt -1,nfound)
  if nfound gt 0 then studies=studies(clook) else begin
   err='No studies with matching TITLE: '+title
   if trim(obs_prog) ne '' then err=err+' / ACRONYM: '+obs_prog
   if trim(sv_desc) ne '' then err=err+' / SV_DESC: '+sv_desc
   message,err,/contin & delvarx,studies & return
  endelse
 endif
endif
 
;-- now search OBS_PROG

if datatype(obs_prog) eq 'STR' then begin
 obs_prog=strupcase(strtrim(obs_prog,2))
 if obs_prog ne '' then begin
  clook=where(strpos(strupcase(studies.obs_prog),obs_prog) gt -1,nfound)
  if nfound gt 0 then studies=studies(clook) else begin
   err='No studies with matching ACRONYM: '+obs_prog
   if trim(title) ne '' then err=err+' / TITLE: '+title
   if trim(sv_desc) ne '' then err=err+' / SV_DESC: '+sv_desc
   message,err,/contin & delvarx,studies & return
  endelse
 endif
endif

;-- find all study variations

if keyword_set(fund_only) then return

for i=0,n_elements(studies)-1 do begin
 list_v_study,studies(i).title_id,vstudy,nfound,/use_tit
 if nfound gt 0 then vstudies=concat_struct(vstudies,vstudy)
endfor

if not exist(vstudies) then begin
 err='No study variations found'
 message,err,/contin & delvarx,studies & return
endif

;-- now search SV_DESC

studies=vstudies & nfound=n_elements(studies)
if datatype(sv_desc) eq 'STR' then begin
 sv_desc=strupcase(strtrim(sv_desc,2))
 if sv_desc ne '' then begin
  clook=where(strpos(strupcase(studies.sv_desc),sv_desc) gt -1,nfound)
  if nfound gt 0 then studies=studies(clook) else begin
   err='No study variations with matching SV_DESC: '+sv_desc
   if trim(obs_prog) ne '' then err=err+' / ACRONYM: '+obs_prog
   if trim(title) ne '' then err=err+' / TITLE: '+title
   delvarx,studies
   message,err,/contin & return
  endelse
 endif
endif

;-- now search on detector

if (datatype(det) eq 'STR') and exist(studies) then begin
 det=strmid(strupcase(strtrim(det,2)),0,1)
 message,'searching for '+det,/cont
 if (det eq 'N') or (det eq 'G') then begin
  for i=0,n_elements(studies) do begin
   get_study_par,study=studies,dets=dets
   clook=where(dets eq det,count)
   all_det=(count eq n_elements(dets))
   if all_det then dstudies=concat_struct(dstudies,studies)
  endfor
  if exist(dstudies) then studies=dstudies else begin
   err='No studies with matching detector - '+det
   message,err,/contin & delvarx,studies & return
  endelse
 endif
endif
return & end

