	subroutine file_convert(new_file_type,length_fixed)
	implicit integer (a-z)

	parameter fixed		= 0,
     1		  segmented	= 1,
     2		  variable	= 2

	logical fixed_length

	character*10 record_type,new_record_type,control
	external std_rfa_useropen,rms$_eof

	character*63 file_name
	
	common /files/ file_name

 	logical overflow
	common /too_large/ overflow,large_length,iaddress
!
! leave off rest of common that is associated with /buf/
!
	byte buffer(32768)
	common /buf/ buffer

	integer*2 size
	data size/32768/

	character*1 blank
	data blank /' '/

	logical write_error
	common /error/ write_error

	fixed_length=.false.

	call check_file

	open(unit=3,name=file_name,type='old',
     1		form='unformatted',readonly,useropen=std_rfa_useropen,
     2		err=9100,shared)
	inquire(3,recl=record_size)
	inquire(3,recordtype=record_type)
	if (record_type.eq.'FIXED') fixed_length=.TRUE.
	close(unit=3)
!
! change record size to get correct number of longwords
!
	if (.not. fixed_length) record_size=record_size+1
!
! added reopen of file because of large fixed length records
!
	open(unit=3,name=file_name,type='old',
     1		form='unformatted',readonly,useropen=std_rfa_useropen,
     2		recl=record_size,recordtype=record_type,shared)

	if (new_file_type.eq.fixed) then
		new_record_type='FIXED'
		control='NONE'
		if (length_fixed.le.0 .or. length_fixed.gt.8191) then
			type *,length_fixed,' is an invalid record',
     1					    ' length'
			return
		 endif
		record_size=length_fixed
		len4=length_fixed*4
	 endif
	
	if (new_file_type.eq.segmented) then
		new_record_type='SEGMENTED'
		control='NONE'
		len4=0
	 endif

	if (new_file_type.eq.variable) then
		new_record_type='VARIABLE'
		control='LIST'
		record_size=8191		! max record size (lngwrds)
		len4=0
	 endif

	inquire(unit=3,name=file_name)
	bracket=index(file_name,']')
	period=index(file_name(bracket:len(file_name)),'.')+bracket-1
	file_name=file_name(1:period)//'CVT'

	open(unit=4,type='new',name=file_name,useropen=std_rfa_useropen,
     1		recordtype=new_record_type,recl=record_size,
     2		form='unformatted',carriagecontrol=control)

	irecords=0
	istat=1

	do while (istat.ne.%loc(rms$_eof))

		call get_record(3,buffer,size,length_total,
     1				istat,fixed_length)

		if (istat.eq.%loc(rms$_eof)) goto 9000

		if (overflow) then
			if (new_record_type.eq.'FIXED' .and.
     1				large_length.gt.len4) then
				type *,'truncated record ',irecords+1,
     1				       ' lost',large_length-len4,'bytes'
				large_length=len4
			 endif
			call put_record(4,%val(iaddress),size,
     1				large_length,fixed_length)
			if (write_error) type *,'error writing record',
     1						irecords+1
			istat1=lib$free_vm(large_length,iaddress)
			if (.not. istat1) then
				type 111
 111				format(/' Error free virtual memory',
     1					' in convert'/)
				iaddress=0
				large_length=0
			 endif
		 else
			if (new_record_type.eq.'FIXED' .and.
     1				length_total.gt.len4) then
				type *,'truncated record ',irecords+1,
     1				       ' lost',length_total-len4,'bytes'
				length_total=len4
			 endif
			call put_record(4,buffer,size,length_total,
     1					fixed_length)
			if (write_error) type *,'error writing record',
     1						irecords+1
		 endif

		if (.not. write_error) irecords=irecords+1
	 enddo

 9000	continue
	inquire(unit=4,name=file_name)
	iblank=index(file_name,blank)-1
	type 10,file_name(1:iblank),irecords
 10	format(/' Created:  ',a,i6,' record(s)'/)

	close(unit=3)
	close(unit=4)

	return

 9100	type *,'unable to open file for conversion'
	return
	end
