FUNCTION scanLine, lineP

  line = StrTrim(lineP,2)
  nCols = 0L
  len = StrLen( line )
  WHILE len NE 0 DO BEGIN
    p = StrPos( line, ' ' )
    IF p NE -1 THEN BEGIN 
      line = StrTrim( StrMid( line, p+1, len-p+1 ), 2 )
      len = StrLen( line )
    ENDIF  ELSE len = 0
    nCols = nCols+1L
  ENDWHILE
  
  RETURN, nCols

END ; ScanLine
    
PRO Columns_Read, file, x, y, qual, dx, dy, $
	NCOLS = nCols, NLINES = nLines, $
	XNR = xNr, YNR = yNr, QNR = qNr, $
	DXLEFTNR = dxLeftNr,$
	DXRIGHTNR = dxRightNr, DYUPNR = dyUpNr, $
	DYDOWNNR = dyDownNr, $
	INTERACTIVE = interactive, $
	LINESTYLE = lineStyle, USERSYM = userSym, $
	HEADER = header, STATUS = status

;+
; NAME:
; 	Columns_Read
; PURPOSE:
;	Read vectors from a file with a (given) number of
;	columns and a given number of lines.
;	Although the vectors can have every meaning, their names
;	are in the procedure: x, y, qualifiers and errors.
;	Column numbers, lines, etc can be specified with keywords.
;	If not, they are asked in the terminal window.
; CATEGORY:
;	Input/Output
; CALLING SEQUENCE:
;	Columns_Read, file [,  x ], y [,qualifier, dx, dy ]
; INPUT:
;	file: the complete name of the file to be read	
; OUTPUT: 
;	x,y: the values to be plotted
;	qualifier: the values corresponding to a plotting symbol
;		for Wave. If > 100, they are user symbols.
;		The values in the file are not the same,
;		their meaning is described in the users manual.
;	dx, dy: vectors for the construction of error bars.
;		They are 1D if the error bars are symmetrical,
;		2D otherwise.
; KEYWORDS:
;	NCOLS, NLINES: number of columns and lines in the file.
;	XNR, YNR, QNR, DXLEFTNR, DXRIGHTNR,
;	DYUPNR, DYDOWNNR: the number of the concerned
;		column. The first have the number 1.
;	/INTERACTIVE: If present, the columns number etc. 
;		will be asked ion the terminal window. 
;		Otherwise, they are taken from the above
;		defined keywords.
;	/HEADER:  If present, it signals the presence of a header
;		that will be jumped. The number of lines
;		to be jumped are assigned to the keyword.
; 	STATUS: "Done" or "Not Done"
;	LINESTYLE: acoording to !p.lineStyle
;	USERSYM: see list of symbols available.
; SIDE EFFECTS:
;	Open files,  in interactive mode use the terminal
;	window.
;
; MODIFICATION HISTORY:
;	Created by A.Csillaghy in June 1991. ETHZ
;	LINESTYLE and USERSYM added in May 1993, A.Cs
;	Minor modifs in March 95, ACs
;-	

status = "Not Done"

On_IOError, exit

Print,'Read colums out of a file'
Print,''

OpenR, unit, file, /GET_LUN

IF NOT Keyword_Set(INTERACTIVE) THEN BEGIN
  xPresent = Keyword_Set(XNR)
  yPresent = Keyword_Set(YNR)
  qPresent = Keyword_Set(QNR)
  dxlPresent = Keyword_Set(DXLEFTNR)
  dxrPresent = Keyword_Set(DXRIGHTNR)
  dyuPresent = Keyword_Set(DYUPNR)
  dydPresent = Keyword_Set(DYDOWNNR)
  IF xPresent THEN xNr = xNr-1L 
  IF yPresent THEN yNr = yNr-1L 
  IF qPresent THEN qNr = qNr-1L 
  IF dxlPresent THEN dxLeftNr = dxLeftNr-1L 
  IF dxrPresent THEN dxRightNr = dxRightNr-1L 
  IF dyuPresent THEN dyUpNr = dyUpNr-1L 
  IF dydPresent THEN dyDownNr = dyDownNr-1L 
  IF NOT Keyword_Set(header) THEN  header = 0 
  IF NOT Keyword_Set(NCOLS) THEN BEGIN
    Print, 'Number of columns is missing ... '
    RETURN
  ENDIF
  IF NOT Keyword_Set(NLINES) THEN BEGIN
    Print, 'Number of lines is missing ... '
    RETURN
  ENDIF
 
ENDIF ELSE BEGIN
  header = ''
  Read, 'How many header lines to jump (=0) ? ',header
  IF header EQ '' THEN header = 0
  IF (NOT Keyword_Set(NCOLS)) OR $
	(NOT Keyword_Set(NLINES)) THEN BEGIN
      hdLines = header
      line = '' & constLines = 0L
      WHILE hdLines NE 0 DO BEGIN
         ReadF, unit, line
         hdLines = hdLines-1
      ENDWHILE
      Print, 'Scanning file for number of lines ... '
      WHILE NOT EOF(unit) DO BEGIN
        ReadF, unit, line
        constLines = constLines+1L
      ENDWHILE
      constCols = scanLine( line )
      IF NOT Keyword_Set(NCOLS) THEN $
	Read_Test, 'Number of data columns : ', constCols, nCols
      IF NOT Keyword_Set(NLINES) THEN $
	Read_Test, 'Number of data lines: ', constLines, nLines
      Close, unit
      OpenR, unit, file
    ENDIF
  val = ''
  Print,'Insert the column numbers for each variable.'
  Print,'If a variable is not defined, then press <return>'
  Print,''
  Read,'Number of x column: ',val
  xPresent = val NE ''
  IF xPresent THEN xNr = val-1L
  Read,'Number of y column: ',val
  yPresent = val NE ''
  IF yPresent THEN yNr = val-1L 
  Read,'Number of qualifier column: ',val
  qPresent = val NE ''
  IF qPresent THEN qNr = val-1L 
  Read,'Number of x error left column: ',val
  dxlPresent = val NE ''
  IF dxlPresent THEN dxLeftNr= val-1L 
  Read,'Number of y error up column: ',val
  dyuPresent = val NE ''
  IF dyuPresent THEN dyUpNr = val -1L 
  Read,'Number of x error right column: ',val
  dxrPresent = val NE ''
  IF dxrPresent THEN dxRightNr =  val -1L 
  Read,'Number of y error down column: ',val
  dydPresent = val NE ''
  IF dydPresent THEN dyDownNr =  val -1L 

ENDELSE

IF dxrPresent THEN $
  IF dxLeftNr EQ dxRightNr THEN dxrPresent = 0
IF dydPresent THEN $
  IF dyDownNr EQ dyUpNr THEN dydPresent = 0

IF header NE 0 THEN  $
  FOR i = 0,header-1 DO BEGIN
    line = ''
    ReadF, unit, line
  ENDFOR

zr = FltArr(nCols, nLines)
ReadF, unit, zr
z = Transpose(zr)

IF xPresent THEN BEGIN
  xNr = xNr*nLines
  x = z(xNr:xNr+nLines-1)
ENDIF
IF yPresent THEN BEGIN
  yNr = yNr*nLines
  y = z(yNr:yNr+nLines-1)
ENDIF
IF qPresent THEN BEGIN
  qNr = qNr*nLines
  qual = IntArr(nLines) & lineStyle = 0*qual & userSym = 0*qual
  qual = z( qNr: qNr+nLines-1 )
  QualifTransform, qual, lineStyle, userSym
ENDIF
IF dxlPresent THEN  BEGIN
  dxLeftNr = dxLeftNr*nLines
  dx = z(dxLeftNr:dxLeftNr+nLines-1)
ENDIF
IF dxrPresent THEN BEGIN
   dxRightNr = dxRightNr*nLines
   dxRight = z(dxRightNr:dxRightNr+nLines-1)
   dx = [ [ dx ], [ dxRight ] ]
ENDIF
IF dyuPresent THEN  BEGIN
  dyUpNr = dyUpNr*nLines
  dy = z(dyUpNr:dyUpNr+nLines-1)
ENDIF
IF dydPresent THEN  BEGIN
  dyDownNr = dyDownNr*nLines
  dyDown = z(dyDownNr:dyDownNr+nLines-1)
  dy = [ [ dy ], [dyDown ] ]
ENDIF

Free_LUn, unit
IF xPresent OR yPresent THEN status = "Done"

RETURN

Exit: Print, !Err_String

END