PRO MDispWrite, xP, y, q, dx, dy, FILENAME = fileName, $
	LINESTYLE = lineStyle, THICK = thick, $
	COLOR = color, SUBTITLE = subTitle, TITLE = title, $
	XTITLE = xTitle, YTITLE = yTitle, $
	USERSYM = userSym, STATUS = status, $
	INTERACTIVE = interactive, COLS = cols

;+ 
; NAME:
;	MDisp Write
; PURPOSE:
;	Writes MDisp Files
; CALLING SEQUENCE:
;	MDispWrite, [ x, ]  y [, qualif, dx, dy ]
; INPUTS
;	x, y : the vectors (same length, 1D ) containing coordinates
;		of the points to draw
;	qualif: the points qualifiers, also a vector of the same length
;		as x and y. The values correspond to the 
;		values of the system variable  "!psym"
;	dx, dy: the values of the error bars. They are 1D vectors for
;		symmetrical error bars, 2D arrays otherwise.
; KEYWORDS:
;	FILENAME: If not specified, the filename is
;		 "mdisp.mdisp"
;	LINESTYLE: A vector containing the linestyle for the
;		points. Default: solid (see "!p.linestyle" for
;		definition of the  values
;	THICK: The line thickness (Default: 1).
;	COLOR: the color number of the plot (Default: 255).
;	SUBTITLE, TITLE, XTITLE, YTITLE: the plot descriptor
;		strings. Default: the contents of the
;		corresponding ssystem variables.
;	USERSYM: a number of user-defined symbol
;	STATUS: Reports "Done" if the file is written, "Not Done" 
;		otherwise.
; 	/INTERACTIVE: the filename is asked on the terminal.
; 	/COLS: Only data columns are witten, no header. It is then
;		compatible with the ".cols" format.
; SIDE EFFECT:
;	A file is written
;
; MODIFICATION HISTORY:
;	Written in January 1992 by A. Csillaghy,
;		Inst. of Astronomy, ETH, Zurich
;	Lots of modifications in March 1995, ACs
;-

  np = N_Params()
  isCols =  Keyword_Set( COLS )
  
  IF np EQ 0 THEN BEGIN
    Error, 1, 'Mdisp Write'
    RETURN
  ENDIF ELSE IF np EQ 1 THEN y = xP ELSE x = xP

  IF N_Elements(y) EQ 0 THEN BEGIN
    Error, 15, 'MDisp Write'
    RETURN
  ENDIF

  IF  isCols THEN extension = '.cols' $
  ELSE extension = '.mdisp'

  IF Keyword_Set( INTERACTIVE ) THEN BEGIN

    Print, ' ' 
    fileName = ''
    Read, 'Please enter the complete name of the file (mdisp' $
	+ extension +')', fileName 
    IF fileName EQ '' THEN fileName = 'mdisp' + extension
  ENDIF
    
  Print, 'Writing the data in file ' + fileName + '...'
  OpenW, unit, fileName, /GET_LUN

  IF NOT Keyword_Set( USERSYM ) THEN userSym = 0

  IF NOT isCols THEN BEGIN
    IF NOT Keyword_Set( SUBTITLE ) THEN subTitle = !p.subtitle
    IF NOT Keyword_Set( TITLE ) THEN title = !p.title
    IF NOT Keyword_Set( XTITLE ) THEN xTitle = !x.title
    IF NOT Keyword_Set( YTITLE ) THEN yTitle = !y.title
    IF Keyword_Set( COLOR ) THEN $
      IF color NE 255 THEN  $
        Printf, unit, 'COLOR = '+ StrTrim( Fix(color), 2 )
    IF subTitle NE '' THEN Printf, unit, 'SUBTITLE = ' + subTitle
    IF Keyword_Set( THICK ) THEN $
      IF thick NE 0 THEN $
        Printf, unit, 'THICK = ' + StrTrim( thick, 2 )
    IF title NE '' THEN Printf, unit, 'TITLE = ' + title
    IF xTitle NE '' THEN Printf, unit, 'XTITLE = ' + xTitle
    IF yTitle NE '' THEN Printf, unit, 'YTITLE = ' + yTitle
  ENDIF 

  IF N_Elements( x ) NE 0 THEN BEGIN
    Printf, unit, 'XCOL = 1'  
    Printf, unit, 'YCOL = 2'  
    array = [ [ x ], [ y ] ] 
  ENDIF ELSE BEGIN
    Printf, unit, 'YCOL = 1'  
    array = [y]
  ENDELSE
  nEls = N_Elements( y )

  IF N_Elements( q ) NE 0 THEN BEGIN
    equalTest = Total( q ) EQ q(0)*nEls AND $
	Total( userSym ) EQ userSym(0)* nEls 
    IF equalTest AND NOT isCols THEN $
      Printf, unit, 'QUALIFIER = ' + StrTrim( Fix( $
	TransfQualif(q(0), lineStyle(0), userSym(0))), 2 ) $
    ELSE BEGIN
      trq = TransfQualif(q, lineStyle, userSym)
      IF Total( trq ) NE 0 THEN BEGIN
        array = [ [array], [trq] ]
	IF NOT isCols THEN BEGIN
          siz = Size( array )
          Printf, unit, 'QCOL = ',siz(2)
        ENDIF
      ENDIF
    ENDELSE
  ENDIF
  IF np GT 3 THEN BEGIN
    sizDx = Size( dx )
    IF (sizDx(0) NE 0) AND Total( dx ) NE 0 THEN BEGIN
      IF sizDx(0) EQ 1 THEN array = [ [array], [dx] ] $
      ELSE array = [ [array], [dx(*,0)] ]
      IF NOT isCols THEN BEGIN
        siz = Size( array )
        Printf, unit, 'DXLEFTCOL = ',siz(2)
      ENDIF
    ENDIF
  ENDIF
  IF np GT 4 THEN BEGIN
    sizDy = Size( dy )
    IF sizDy(0) NE 0  AND Total( dy ) NE 0 THEN BEGIN
      IF sizDy(0) EQ 1 THEN array = [ [array], [dy] ] $
      ELSE array = [ [array], [dy(*,0)] ]
      IF NOT isCols THEN BEGIN
        siz = Size( array )
        Printf, unit, 'DYUPCOL = ',siz(2)
      ENDIF
      IF sizDx(0) EQ 2 THEN BEGIN
        array = [ [array], [dx(*,1)] ]
        IF NOT isCols THEN BEGIN
          siz = Size( array )
          Printf, unit, 'DXRIGHTCOL = ',siz(2)
        ENDIF
      ENDIF
      IF sizDy(0) EQ 2 THEN BEGIN
        array = [ [array], [dy(*,1)] ]
        IF NOT isCols THEN BEGIN
          siz = Size( array )
          Printf, unit, 'DYDOWNCOL = ',siz(2)
        ENDIF
      ENDIF
    ENDIF
  ENDIF ELSE IF sizDx(0) EQ 2 THEN BEGIN
    array = [ [array], [dx(*,1)] ]
    IF NOT isCols THEN BEGIN
      siz = Size( array )
      Printf, unit, 'DXRIGHTCOL = ' ,siz(2)
    ENDIF
  ENDIF

  Printf, unit, Transpose( array )

  Print, '...done.'
  Free_LUn, unit

END ; mdisp write
  
  



