;+
; PROJECT
;           SolarB EIS
;
; NAME
;           EIS_TITLE_UNIQUE
;
; CATEGORY
;           EIS Technical Planning
;           Database
;
; PURPOSE
;           Determine whether a lineList title and acronym already exists.
;           It checks both are unique.
;
; KEYWORDS
;           linelist - entry type
;           raster   -    "   "
;           study    -    "   "
;
; INPUTS
;           title - the new title
;           acron - the new acronym
;
; OUTPUTS
;           a structure containing the tags;
;               unique_title = 0/1 when title   is unique/not
;               exists_title = (string) ID of (first) ll with this title
;               unique_acron = 0/1 when acronym is unique/not
;               exists_acron = (string) ID of (first) ll with this acronym
;
;
; HISTORY
;           V0 Written by: John Rainnie, RAL j.rainnie@rl.ac.uk (Dec 2005)
;           April 2006 - changed to check both title & acroynm
;-
;************************************************************************
FUNCTION eis_title_unique , title , acron               ,                   $
                                    linelist = linelist ,                   $
                                    raster   = raster   ,                   $
                                    study    = study

IF KEYWORD_SET(linelist) THEN db = db_read_linelist_all()
IF KEYWORD_SET(raster)   THEN db = db_read_raster_all()
IF KEYWORD_SET(study)    THEN db = db_read_study_all()

nEntries  = N_ELEMENTS(db[0,*])

;
; TITLE - linelist titles will be unique since they are automatically 
;         generated. Study titles (supplied by the user must also
;         be unique). Raster titles don't have to be unique!
;

unique_title = 1
exists_title = ''

IF KEYWORD_SET(LINELIST) OR KEYWORD_SET(STUDY) THEN BEGIN

    oldTitles    = STRARR(nEntries)
    FOR i        = 0 , nEntries - 1 DO oldTitles[i] = STRTRIM(db[2,i],2)
    in_titles    = WHERE(oldTitles EQ STRTRIM(title , 2))
    unique_title = (in_titles[0] EQ -1) ? 1  : 0
    exists_title = (in_titles[0] EQ -1) ? '' : db[0,in_titles[0]]

ENDIF
;IF KEYWORD_SET(RASTER) THEN BEGIN
;
;    stop
;
;ENDIF

;
; ACRONYM - all lineList, raster and study acronyms must be unique
;
oldAcrons    = STRARR(nEntries)
FOR i        = 0 , nEntries - 1 DO oldAcrons[i] = STRTRIM(db[1,i],2)
in_acrons    = WHERE(oldAcrons EQ STRTRIM(acron , 2))
unique_acron = (in_acrons[0] EQ -1) ? 1  : 0
exists_acron = (in_acrons[0] EQ -1) ? '' : db[0,in_acrons[0]]

RETURN , { unique_title:unique_title , exists_title:exists_title    ,       $
           unique_acron:unique_acron , exists_acron:exists_acron  }

END
