      subroutine FOPEN(funit,fname,rw_flag)

c**********************************************************************
c**** DESCRIPTION: Open a file for either reading or writing       ****
c**** USAGE:       On input:                                       ****
c****              funit          The file unit number             ****
c****               (integer)                                      ****
c****              fname          The file name                    ****
c****               (char*80)                                      ****
c****              rw_flag        Flag set to 'r' to read and      ****
c****               (char*1)      to 'w' to write                  ****
c****              On output:                                      ****
c**** DEPENDENCIES:LEN_TO_CHAR                                     ****
c**** AUTHORS:     R Porter                                        ****
c**** CHANGE_LOG:  Written 13/08/99 (tested 17/08/99)              ****
c**********************************************************************
c                                                     column 72 here -->
      implicit none

      character*80     fname
      character*1      rw_flag,ans

      logical          fexist

      integer          funit,LEN_TO_CHAR

      if (funit.lt.7) then
        write (*,'(/'' Error in FOPEN: funit less than 7'')')
        stop
      endif

 10   inquire(file=fname,exist=fexist)
      if (rw_flag.eq.'r') then
        if (fexist) then
          open(funit,file=fname,form='formatted',status='old')
          rewind(funit)
        else
          write (*,'(/'' Error in FOPEN: cannot open file '',a)')
     +               fname(1:LEN_TO_CHAR(fname,' '))
          write (*,'('' Do you want to try again? (y/n) : ''$)')
          read (*,'(a)') ans
          if (ans.eq.'y'.or.ans.eq.'Y') goto 10
          stop
        endif
      else if (rw_flag.eq.'w') then
        if (fexist) then
          write(*,'(/'' The file '',a,'' already exists.'')')
     +                   fname(1:LEN_TO_CHAR(fname,' '))
          write(*,'('' Do you want to overwrite it? (y/n) : ''$)')
          read(*,'(a)') ans
          if (ans.ne.'n'.or.ans.ne.'N') then
            open(funit,file=fname,form='formatted',status='old')
            rewind(funit)
          else
            write(*,'('' Do you want to choose a new'',
     +                '' filename? (y/n) :''$)')
            read (*,'(a)') ans
            if (ans.ne.'n'.or.ans.ne.'N') then
              write(*,'('' Enter new filename : ''$)')
              read (*,'(a)') fname
              goto 10
            endif
            stop
          endif
        else
          open(funit,file=fname,form='formatted',status='new')
        endif
      else
        write (*,'('' Error in FOPEN: rw_flag not "r" or "w"'')')
        pause
      endif

      return

      end

      integer function LEN_TO_CHAR(string,ch)

c**********************************************************************
c**** DESCRIPTION: Finds the length of a string to first occurence ****
c****              of a character                                  ****
c**** USAGE:       On input:                                       ****
c****              string         A string                         ****
c****               (char*(*))                                     ****
c****              ch             A character                      ****
c****               (char*1)                                       ****
c****              On output:                                      ****
c****              LEN_TO_CHAR:   Returns length of string to first****
c****               (integer)     occurrence of character ch       ****
c**** DEPENDENCIES:                                                ****
c**** AUTHORS:     R Porter                                        ****
c**** CHANGE_LOG:  Written 13/08/99                                ****
c**********************************************************************
c                                                     column 72 here -->
      implicit none

      character*(*)   string
      character*1     ch

      integer         i

      i = 1
      do 10 while (string(i:i).ne.ch.and.i.lt.len(string))
        i = i+1
 10   continue

      LEN_TO_CHAR = i

      return

      end
