;+
; Project     :	SOHO - CDS
;
; Name        :	WR_VDSWIN_FILE
;
; Purpose     :	Writes out a CDS VDSWIN table to file.
;
; Explanation :	Writes out the VDS window parameters in the format required by the
;               Perl script files for loading onto the CDS.
;               Calculates the VDS windows on the fly from the data extraction and IEF 
;               windows.
;
; Use         : <wr_vdswin_file, vdswin, dexwin_details>
;                 or if version number required
;               <wr_vdswin_file, vdswin, VERSION=version>.
;
; Inputs      : vdswin          = structure array giving information on VDS window tables;
;               dexwin_details  = structure containing data extraction window information 
;                                 from datawin planning database.
;
; Opt. Inputs : flag_master_info : structure giving information on flag master scheme.
;                                  If study flag study then not passed to routine.
;                                  If study not flag study and not flag master then 
;                                  value 0 is passed.
;
; Outputs     : Writes VDS 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, get_vds_win, 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.
;                            Changed to routine get_vds_win.
;               Version 0.2, MKC, 23/3/95
;                            ll_title removed from display.
;               version 0.3, MKC, 21/4/95
;                            Modified argument list.
;               Version 0.4, MKC, 24/5/95
;                            Added check for too many windows.
;                            Added command terminator.
;               Version 0.5, MKC, 23/10/95
;                            Renamed environment variables and used concat_dir.
;               Version 0.6, MKC, 7/11/95
;                            Modified printout.
;                            Changed so that will write out table if already loaded.
;                            Added flag_master_info to argument list.
;               Version 0.7, MKC, 21/11/95
;                            Fixed bug due to table length not being written out.
;               Version 0.8, MKC, 24/11/95
;                            Changed so that CVT file shave .dt extension
;               Version 0.9, MKC, 19/2/96
;                            Added more printout.
;               Version 1.0, MKC, 29/3/96
;                            Modified so that now gets VDS readout mode from CBVCAM parameter in state database
;                            and passes this on to get VDS windows routine.
;
; Version     :	Version 1.0, 29/3/96
;-
;**********************************************************

PRO wr_vdswin_file, vdswin, dexwin_details, flag_master_info, VERSION=version

  ; set version number 

  sw_version = '1.0'

  ; check if only version required

  IF KEYWORD_SET(version) THEN BEGIN

    vdswin.vs = sw_version
    RETURN

  ENDIF

  ; retrieve VDS readout mode from CDHS state database

  ss = cp_get_entry ( 'CBVCAM', [4] )

  vdsmode = ss(0).active

  ; check vds mode

  IF vdsmode LT 1 OR vdsmode GT 7 THEN MESSAGE, 'INVALID VDS READOUT MODE : ' + STRTRIM(vdsmode,2)

  ; check if vdswin loaded already

  IF !debug THEN IF vdswin.ok THEN PRINT, 'VDSWIN table already loaded'

  ; check NIS detector

  IF dexwin_details.detector NE 'N' THEN MESSAGE, 'Not VDS detector'

  ; set up window array [4,n] including IEF flag master windows

  IF N_TAGS(flag_master_info) GT 0 THEN $
    wins = [[dexwin_details.wins.win_def],[flag_master_info.wins.win_def]] ELSE $
    wins = dexwin_details.wins.win_def

  ; get new set of windows
  ; use full quadrature readout for now

  wdws = get_vds_win ( TRANSPOSE ( wins ), vdsmode )

  nws = N_ELEMENTS(wdws)/4

  ; check table length 
  ; CDHS table is [50]INT16
  ; table ID, length, imif header, no.of.windows, window.end and checksum is 6 words
  ; each window takes 4 words
  ; This gives maximum of 44 words or 11 tables.
  ; The VDS itself puts more stringent restriction of 10 tables

  IF nws GT 10 THEN MESSAGE, 'Too many data extraction windows'

  ; need to load vdswin

  ; Output vdswin CVT file 
  ; overwrite any existing file 

  ; get file name

  filename = concat_dir ( '$CDS_CP_CVTFILES_W', 'vdswin_'+STRTRIM(vdswin.cdhsx,1)+'.dt' )

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

  ; open file

  OPENW, unit, filename, /GET_LUN

  ; write out vdswin CVT file

  ; file header info

  get_utc, utc, /ECS

  PRINTF, unit, '# VDSWIN TABLE FILE
  PRINTF, unit, '# Created ', utc
  PRINTF, unit, '# Version ', sw_version
  PRINTF, unit, '# CDHS id ', vdswin.cdhsid
  PRINTF, unit, '# CDHS index ', vdswin.cdhsx
  PRINTF, unit, '# Data extraction window database ID  = ', vdswin.id
  PRINTF, unit, '# Calibration version number          = ', vdswin.version
  PRINTF, unit, '# IEF master ID                       = ', vdswin.iefid
  PRINTF, unit

 ; output dexwin header

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

  ; output flag master stuff

  IF N_TAGS(flag_master_info) GT 0 THEN BEGIN

    PRINTF, unit, '# Study is flag master, IEF ID = ', shorthex ( flag_master_info.ieftab_id )

  ENDIF

  ; output vdswin parameters

  PRINTF, unit, shorthex(vdswin.cdhsid), '    # VDSWIN ID'


  ; output table length : imif header, no. of windows, window end 

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

  ; output IMIF command header 
  ; 4 words per window + VDS command header and terminator

  PRINTF, unit, shorthex( '2200'X OR (2+4*nws) ), $
                '    # IMIF command header'

  ; output VDS command header

  PRINTF, unit, shorthex( '3000'X OR nws ), $
                '    # VDS command header'

  PRINTF, unit

  FOR k = 0, nws-1 DO BEGIN

    PRINTF, unit, '# window ', k
    PRINTF, unit

    PRINTF, unit, shorthex ( '3400'X OR wdws(k,0)), $
                    '    # window xstart :', wdws(k,0)

    PRINTF, unit, shorthex ( '3800'X OR wdws(k,1)), $
                    '    # window ystart :', wdws(k,1)

    PRINTF, unit, shorthex ( '3400'X OR wdws(k,0)+wdws(k,2)-1), $
                    '    # window xstop  :', wdws(k,0)+wdws(k,2)-1

    PRINTF, unit, shorthex ( '3800'X OR wdws(k,1)+wdws(k,3)-1), $
                    '    # window ystop  :', wdws(k,1)+wdws(k,3)-1

    PRINTF, unit

  ENDFOR

  ; output VDS command terminator

  PRINTF, unit, shorthex( '3C00'X ), $
                '    # VDS command terminator'

  PRINTF, unit

  ; close file and free logical unit

  FREE_LUN, unit

END


