;+
; Project     :	SOHO - CDS
;
; Name        :	GET_DEXWIN_ID()
;
; Purpose     :	Returns an internal CDHS ID for a table.
;
; Explanation : Interrogates the id database to find whether 
;               table has already been used. If already used then returns the CDHS ID assigned to
;               it else gives the table a new ID and enters it into the database.
;
; Use         : <get_dexwin_id, initial_table>
;
; Inputs      : initial_table = structure containing table information.
;               Input tags used :
;                    table.vs      = character string indicating the software version
;                                    of the code used to create the table.
;                    table.id      = integer planning database ID for the table.
;                    table.version = integer giving VDS calibration version
;
; Opt. Inputs : None.
;
; Outputs     : Structure array containing table information with CDHS ID and table OK
;               entries set.
;                    table.cdhsid = the internal CDHS ID used to reference the table
;                    table.ok     = an integer flag indicating whether table has already 
;                                   been loaded onto the CDS.
;
; Opt. Outputs:	None.
;
; Keywords    : None.
;
; Calls       :	dbopen, db_info, dbbuild, dbfind, dbext, dbclose,
;               get_utc, rem_dup.
;                
; Common      :	None.
;
; Restrictions:	None.
;
; Side effects:	None.
;
; Category    :	Command preparation.
;
; Prev. Hist. :	Adapted from get_raster_id.
;
; Written     :	Version 0.0, Martin Carter, RAL, 7/11/95
;
; Modified    :	Version 0.1, MKC, 19/2/96
;                            Modified to use VDS calibration version number.
;                          0.2, MKC, 27/4/98
;                            Current implementation of alternative studies allows 2 versions of
;                            a table to be active on CDHS. Changed checks to reflect this.
;
; Version     :	Version 0.2, 27/4/98
;-
;**********************************************************

FUNCTION get_dexwin_id, initial_table

  ; check if table defined and if not return new CDHS ID

  ; get local copy of table

  table = initial_table

  ; define first cdhs ID 
  ; NB First 2 tables are reserved or modified on power up
  ;     Tables start at 0.
  ; The dexwin ID of table 0 is 0xFFFF.
  ; The dexwin ID of table 1 is 0xFFFE.

  ; Start the CDHS Ids so that they keep track with the table index.

  first_id = 2  

  ; set priviledge to update database

  !PRIV = 2

  ; open ID database for updating

  dbopen, 'ddexwin_id', 1

  ; check if table exists already

  ; check database has some entries
  ; else dbfind will flag error

  nentries = db_info ( 'entries' )

  IF nentries(0) EQ 0 THEN BEGIN

    ; database is empty

    ; table does not exist

    table.ok = 0

    ; set first entry

    table.cdhsid = first_id

    ; Get current utc time in string format

    get_utc, timenow, /ECS

    ; create new entry in database

    dbbuild, timenow, table.vs, table.cdhsid, table.id, table.version, /SILENT

  ENDIF ELSE BEGIN

    ; set up search criteria

    search = 'ID=' + STRING ( table.id ) 

    list = dbfind ( search, /SILENT)

    ; list is array of entries in database
    ; if no entries then list contains a zero
    ; valid entries start at 1 

    IF list(0) GE 1 THEN BEGIN

      ; refine search criteria

      search = 'SW_VERSION=' + table.vs + $
               ',VERSION=' + STRING ( table.version )

      list = dbfind ( search, list, /SILENT)

    ENDIF

    ; does table not exist

    IF ( list(0) LT 1 ) THEN BEGIN

      ; table does not exist, create new table ID entry

      table.ok = 0

      ; get next CDHS ID

      ; extract last  cdhs ID s

      dbext, [ LONG ( nentries ) ], 'CDHS_ID', cdhsids   

      table.cdhsid = cdhsids(0) + 1   
  
      IF table.cdhsid EQ 32768 THEN MESSAGE, 'MAXIMUM CDHSID REACHED'

      ; Get current utc time in string format

      get_utc, timenow, /ECS

      ; create new entry in database

      dbbuild, timenow, table.vs, table.cdhsid, table.id, table.version, /SILENT

    ENDIF ELSE BEGIN

      ; table does exist, extract table entry

      table.ok = 1

      ; remove any duplicates

      list = list ( rem_dup ( list ) )
    
      ; list may have 2 entries

      IF N_ELEMENTS ( list ) GT 2 THEN $
        MESSAGE, 'Invalid number of entries : ' + STRTRIM ( N_ELEMENTS(list), 1 )

      ; get table entry

      dbext, list(0), 'CDHS_ID', cdhsids
 
      ; set ID

      table.cdhsid = cdhsids(0)

    ENDELSE

  ENDELSE

  ; close table ID database

  dbclose, 'ddexwin_id'

  ; reset privildeges

  !PRIV = 1  

  ; return results

  RETURN, table

END


