	program x11tossrl
	parameter    (nfilemax = 4096)
	character*80 filter,filenm,result(nfilemax)
	logical*4 cli_get_pvalue
	external  cli_get_pvalue
	logical*4 read_x11,write_collect
	external  read_x11,write_collect
c
c ....... recover file info
c
	if(.not.cli_get_pvalue(1,filter))stop 'No data files specified'
	nfile = 0
	call find_files(filter,result,nfile)
	type *,'Converting ...'
	do i=1,nfile
	call strip_version(result(i))
	if( read_x11(result(i)) )then
	type *,result(i)
		if(.not.write_collect(result(i)) )
     *	type *,'Error - Cannot write ',result(i)

	else
	type *,'Error - Cannot read ',result(i)
	end if
	end do
	end
c
c
	logical*4 function read_x11(filenm)
	parameter (nptmax=4096)
	parameter (ncolmx =100)
	logical*4 loop,read_data
	character filenm*80
	character line*648
	character arg(100)*40
	character*78 comments(6)
	character*18 labels(ncolmx)
	real*4    d(ncolmx,nptmax),offsets(ncolmx)
	integer*4 n,ncol
	common /comnt/comments,labels
	common /parms/e0,offsets
	common /data /d,n,ncol
	data loop/.true./
	save
	read_data = .false.
	read_x11  = .false.
c
	open (1,file=filenm,status='old',blank='zero',readonly
     *,form='formatted',recl=10240,err=20)
c
        icnt= 0
	n   = 0
	do while (loop)
	read (1,'(a)',end=10,err=20)line
	call decod(line,arg,ncols)
c
	if(read_data)then
		n = n+1
		do j=1,ncol
		read(arg(j),*,err=1)d(j,n)
		end do	! j
	else
		if(line(1:3).eq.'E0='     )read(arg(2),*)E0
		if(line(1:6).eq.'Gains='  )read(1,'(a)',err=20)comments(1)
c
			if(arg(1)(1:8).eq.'Offsets=')then
			do j=1,ncols-1
			read (arg(j+1),*)offsets(j)
			end do	! j
			end if	! offsets
c
			if(line(1:10).eq.'----------')then
			read (1,'(a)',err=20)line
			call decod(line,arg,ncols)
				do j=1,ncols
				labels(j)=arg(j)
				end do
			read_data = .true.
			ncol      = ncols
			end if	! next is the data ...
c
	end if	! read data
	end do	! loop
c
1	n = n-1	! had a problem reading a data point - drop it
10	read_x11 = .true.
20	close(1)
	return
	end

	logical*4 function write_collect(fname)
     	parameter (nptmax=4096)
	parameter (ncolmx= 100)
	character*80 fname
	character*38 title,tdate,scaler_file,region_file,lowsignal
	character*78 comments(6),m_info
	character*18 labels(ncolmx)
	real*4    d(ncolmx,nptmax),offsets(ncolmx),weights(ncolmx)
	integer*4 n
	common /comnt/comments,labels
	common /parms/e0,offsets
	common /data /d,n,ncol
	logical*4 mafile_write
	external  mafile_write
	data title/'SSRL                                    '/
	data tdate/' '/
	data scaler_file/'scaler_file'/
	data region_file/'region_file'/
	data m_info/' '/
	data lowsignal/' '/
	save
	write_collect = mafile_write(
     *	fname,
     *	title,
     *	tdate,
     *	scaler_file,
     *	region_file,
     *	m_info,
     *	lowsignal,
     *	comments,
     *	ncol,n,
     *	offsets,
     *	weights,
     *	labels,
     *	d)
	return
	end

	logical*4 function mafile_write(fname,
     *					title,
     *					tdate,
     *					scaler_file,
     *			      		region_file,
     *					m_info,
     *					lowsignal,
     *					coms,
     *			       		ncol,npts,
     *					offsets,
     *					weights,
     *					labels,
     *					d)
	parameter (ncolmx= 100)
	parameter (nptmax=4096)
	character*80 fname
	character*38 title ,tdate,scaler_file,region_file
	character*78 m_info
	character*38 lowsignal
	character*78 coms(6)
	character*18 labels(ncolmx)
	integer*4    ncol,npts
	real*4        offsets(ncolmx)
	real*4        weights(ncolmx)
	real*4       d(ncolmx,nptmax)
	mafile_write=.true.
	open (1,file=fname,status='new',blank='zero'
     * ,carriagecontrol='list',form='formatted',recl=10240)
	write(1,'(a)')title
	write(1,'(a)')tdate
	write(1,'(''PTS:'',i11,'' COLS:'',i11)')npts,ncol
	write(1,'(a)')scaler_file
	write(1,'(a)')region_file
	write(1,'(a)')m_info
	write(1,'(a)')lowsignal
	do i=1,6
	write(1,'(a)')coms(i)
	end do
	write(1,'(a)')
	write(1,'(''Weights: '')')
	write(1,'(<ncol>(1x,f14.3))')(weights(j),j=1,ncol)
	write(1,'(''Offsets: '')')
	write(1,'(<ncol>(1x,f14.3))')(offsets(j),j=1,ncol)
	write(1,'(''Data: '')')
	do j=1,ncol
	write(1,'(a)')labels(j)
	end do
	write(1,'(a)')
	do i=1,npts
	write(1,'(<ncol>(1x,f14.3))')(d(j,i)    ,j=1,ncol)
	end do
	close(1)
	end

	subroutine strip_version(file)
	character file*(*)
	n=len(file)
	do i=n,1,-1
		if(file(i:i).eq.';')then
		file(i:n)=' '
		return
		end if
	end do
	return
	end

