;+
; Project     :	SOHO - CDS
;
; Name        :	WR_DEXWIN_FILE
;
; Purpose     :	Writes out a CDS DEXWIN table to file.
;
; Explanation :	Writes out the data extraction window parameters in the format required by 
;               the Perl script files for loading onto the CDS.
;
; Use         : <wr_dexwin_file, dexwin, dexwin_details>
;                 or if version number required
;               <wr_dexwin_file, dexwin, VERSION=version>.
;
; Inputs      : dexwin          = structure array giving information on data extraction
;                                 tables;
;               dexwin_details  = structure containing data extraction window information 
;                                 from datawin planning database.
;
; Opt. Inputs : None.
;
; Outputs     : Writes data extraction window data to file.
;
; Opt. Outputs:	version = software version number if keyword set.
;
; Keywords    : VERSION = Character string containing software version number.
;
; Calls       :	get_utc, shorthex, concat_dir.
;                
; Common      :	None.
;
; Restrictions:	None.
;
; Side effects:	None.
;
; Category    :	Command preparation.
;
; Prev. Hist. :	None.
;
; Written     :	Version 0.00, Martin Carter, RAL, 14/10/94
;
; Modified    :	Version 0.01, Martin Carter, RAL, 28/11/94
;                            Added proforma.
;               Version 0.1, MKC, 2/3/95
;                            Removed .var tag.
;               Version 0.2, MKC, 23/3/95
;                            Removed ll_title from display.
;               Version 0.3, MKC, 21/4/95
;                            Modified argument list.
;               Version 0.4, MKC, 23/5/95
;                            Added check on table length.
;               Version 0.5, MKC, 1/6/95
;                            Tidied up output..
;               Version 0.6, MKC, 23/10/95
;                            Renamed environment variables and used concat_dir.
;               Version 0.7, MKC, 7/11/95
;                            Modified printout.
;                            Changed so that will write out table if already loaded.
;               Version 0.8, MKC, 24/11/95
;                            Changed so that CVT file shave .dt extension
;
; Version     :	Version 0.8, 24/11/95
;-
;**********************************************************

PRO wr_dexwin_file, dexwin, dexwin_details, VERSION=version

  ; set version number 

  sw_version = '0.7'

  ; check if only version required

  IF KEYWORD_SET(version) THEN BEGIN

    dexwin.vs = sw_version
    RETURN

  ENDIF

  ; check if dexwin loaded already

  IF !debug THEN IF dexwin.ok THEN PRINT, 'DEXWIN table already loaded'

  ; need to load dexwin

  ; Output dexwin CVT file 
  ; overwrite any existing file 

  ; get file name

  filename = concat_dir ( '$CDS_CP_CVTFILES_W', 'dexwin_' + STRTRIM(dexwin.cdhsx,1) + '.dt' )

  IF !debug THEN PRINT, 'WRITING FILE : ', filename

  ; open file

  OPENW, unit, filename, /GET_LUN

  ; write out dexwin CVT file

  ; file header info

  get_utc, utc, /ECS

  PRINTF, unit, '# DEXWIN TABLE FILE
  PRINTF, unit, '# Created : ', utc
  PRINTF, unit, '# Version : ', sw_version
  PRINTF, unit, '# CDHS id : ', dexwin.cdhsid
  PRINTF, unit, '# CDHS index : ', dexwin.cdhsx
  PRINTF, unit, '# ID : ', dexwin.id
  PRINTF, unit

  ; output dexwin header

  PRINTF, unit, '# detector  = ', dexwin_details.detector
  PRINTF, unit, '# dw_desc   = ', STRTRIM(dexwin_details.dw_desc)
  PRINTF, unit, '# ll_desc   = ', STRTRIM(dexwin_details.ll_desc)  
  PRINTF, unit

  ; output dexwin parameters

  PRINTF, unit, shorthex(dexwin.cdhsid), '    # CDHS internal Dexwin ID'

  ; get no. of windows

  nws = N_ELEMENTS(dexwin_details.wins)

  ; check which detector

  IF dexwin_details.detector EQ 'G' THEN BEGIN

    ; GIS detector

    ; check table length 
    ; CDHS table is [100]INT16
    ; table ID, length and checksum is 3 words
    ; each window takes 2 words

    IF 2*nws GT 97 THEN MESSAGE, 'Too many GIS data extraction windows'

    PRINTF, unit, shorthex( 2*nws ), $
                  '    # CDHS Table length'

    PRINTF, unit

    FOR k = 0, nws-1 DO BEGIN

      PRINTF, unit, '# window ', k
      PRINTF, unit, '# win_name ', dexwin_details.wins(k).win_name
      PRINTF, unit
      PRINTF, unit, shorthex(dexwin_details.wins(k).win_def(0)), $
                    '    # window start :', dexwin_details.wins(k).win_def(0)

      PRINTF, unit, shorthex(dexwin_details.wins(k).win_def(2)), $
                    '    # window size  :', dexwin_details.wins(k).win_def(2)
      PRINTF, unit

    ENDFOR

  ENDIF ELSE BEGIN

    ; VDS detector

    ; check table length 
    ; CDHS table is [100]INT16
    ; table ID, length and checksum is 3 words
    ; each window takes 4 words

    IF 4*nws GT 97 THEN MESSAGE, 'Too many VDS data extraction windows'

    PRINTF, unit, shorthex( 4*nws ), $
                  '    # Table length'

    PRINTF, unit

    FOR k = 0, nws-1 DO BEGIN

      PRINTF, unit, '# window ', k
      PRINTF, unit, '# win_name ', dexwin_details.wins(k).win_name
      PRINTF, unit
      PRINTF, unit, shorthex(dexwin_details.wins(k).win_def(0)), $
                    '    # window xstart :', dexwin_details.wins(k).win_def(0)

      PRINTF, unit, shorthex(dexwin_details.wins(k).win_def(1)), $
                    '    # window ystart :', dexwin_details.wins(k).win_def(1)

      PRINTF, unit, shorthex(dexwin_details.wins(k).win_def(2)), $
                    '    # window xsize  :', dexwin_details.wins(k).win_def(2)

      PRINTF, unit, shorthex(dexwin_details.wins(k).win_def(3)), $
                    '    # window ysize  :', dexwin_details.wins(k).win_def(3)
      PRINTF, unit

    ENDFOR

  ENDELSE

  ; close file and free logical unit 

  FREE_LUN, unit

 END
