      subroutine datacolorssave(I1,I2,I3,I4)

      implicit none
      integer I1,I2,I3,I4

      if (I4.eq.0) then
        if (I3.eq.1) then
            write(130,'(2I5)') I1,I2
        else
            write(130,'(2I5)') I1,I2
            write(130,*)
        end if
      end if

      if (I4.eq.1) then
        if (I3.eq.1) then
            write(115,'(2I5)') I1,I2
        else
            write(115,'(2I5)') I1,I2
            write(115,*)

        end if
      end if

      if (I4.eq.2) then
        if (I3.eq.1) then
            write(116,'(2I5)') I1,I2
        else
            write(116,'(2I5)') I1,I2
            write(116,*)
        end if
      end if

      if (I4.eq.3) then
        if (I3.eq.1) then
            write(117,'(2I5)') I1,I2
        else
            write(117,'(2I5)') I1,I2
            write(117,*)
        end if
      end if

      if (I4.eq.4) then
        if (I3.eq.1) then
            write(118,'(2I5)') I1,I2
        else
            write(118,'(2I5)') I1,I2
            write(118,*)
        end if
      end if

      return
      end


      subroutine drawdatasave(I1,I2,I3,I4)

      implicit none
      integer I1,I2,I3,I4

      if (I1.LT.0.OR.I2.LT.0.OR.I1.GT.10000.OR.I2.GT.7000) then
      return
      end if

      if (I4.eq.0) then
        if (I3.eq.1) then
            write(130,'(2I5)') I1,I2
        else
            write(130,'(2I5)') I1,I2
            write(130,*)
        end if
      end if

      if (I4.eq.1) then
        if (I3.eq.1) then
            write(115,'(3I5)') I1,I2,I3
        else
            write(115,*)
            write(115,'(3I5)') I1,I2,I3
        end if
      end if

      if (I4.eq.2) then
        if (I3.eq.1) then
            write(116,'(2I5)') I1,I2
        else
            write(116,'(2I5)') I1,I2
            write(116,*)

        end if
      end if

      if (I4.eq.3) then
        if (I3.eq.1) then
            write(117,'(2I5)') I1,I2
        else
            write(117,'(2I5)') I1,I2
            write(117,*)
        end if
      end if

      if (I4.eq.4) then
        if (I3.eq.1) then
            write(118,'(2I5)') I1,I2
        else
            write(118,'(2I5)') I1,I2
            write(118,*)

        end if
      end if

      return
      end


            subroutine drawdatasave3(I1,I2,I3)

      implicit none
      integer I1,I2,I3

      if (I1.LT.0.OR.I2.LT.0.OR.I1.GT.10000.OR.I2.GT.7000) then
      return
      end if

      if (I3.eq.1) then
        write(116,'(2I5)') I1,I2
      else
        write(116,'(2I5)') I1,I2
        write(116,*)
      end if

      return
      end

      subroutine gnuplotlabel(I1,I2,label)

      implicit none
      integer I1,I2
      real X,Y
      character label*20

      X=real(I1)/10000.0
      Y=real(I2)/7000.0

      write(150,'(A19,F6.4,A1,F6.4,A2,A,A)') 'set label at graph ',
     1X,",",Y,' "',label,'"'

      return
      end


      subroutine drawcmdsave

      include 'DATMAI.INC'
      character*150 catcommand

      catcommand="cat "//trim(HOME)//"drawcmd0.txt "
     1//trim(HOME)//"drawcmd3.txt "//"> "//trim(HOME)//"drawcmd.txt"

      call system(catcommand)

      return
      end



      subroutine userinpt2(INPUT,COMMAND,WC)

      implicit none

      character COMMAND*140,INPUT*140,KKDP*3
      character WC*8

      call SELECTKDP(KKDP)

      WC='        '
      write(6,'(A$)') KKDP//'> '
      read(5,'(a$)') COMMAND
      call upper_case(COMMAND)

      if (COMMAND.eq.'') then
        COMMAND=' '
      endif

      INPUT=COMMAND(1:132)

      call PROCES

      return
      end


      subroutine setonecolors

      include 'DATMAI.INC'

      write(150,*) 'plot [0:10000] [0:7000] "'//
     1 trim(HOME)//'plotdata/black.txt" lc rgb "black" lw 0.5 w l'
      write(150,*) 'pause -1'

      return
      end

      subroutine settwocolors

      include 'DATMAI.INC'

      write(150,*) 'plot [0:10000] [0:7000] "'//
     1 trim(HOME)//'plotdata/black.txt" lc rgb "black"
     1 lw 0.5 w l,"'//
     1 trim(HOME)//'plotdata/yellow.txt" lc rgb "dark-yellow"
     1 lw 0.5 w l'
      write(150,*) 'pause -1'

      return
      end

      subroutine settwocolors2

      include 'DATMAI.INC'

      write(150,*) 'plot [0:10000] [0:7000] "'//
     1 trim(HOME)//'plotdata/black.txt" lc rgb "black"
     1 lw 0.5 w l,"'//
     1 trim(HOME)//'plotdata/red.txt" lc rgb "red"
     1 lw 0.5 w l'
      write(150,*) 'pause -1'

      return
      end

      subroutine settwocolors3

      include 'DATMAI.INC'

      write(150,*) 'plot [0:10000] [0:7000] "'//
     1 trim(HOME)//'plotdata/black.txt" lc rgb "black"
     1 lw 0.5 w l,"'//
     1 trim(HOME)//'plotdata/red.txt" lc rgb "red"
     1 pt 7 ps 0.3'
      write(150,*) 'pause -1'

      return
      end

      subroutine setthreecolors

      include 'DATMAI.INC'

      write(150,*) 'plot [0:10000] [0:7000] "'//
     1 trim(HOME)//'plotdata/black.txt" lc rgb "black"
     1 lw 0.5 w l,"'//
     1 trim(HOME)//'plotdata/yellow.txt" lc rgb "dark-yellow"
     1 lw 0.5 w l,"'//
     1 trim(HOME)//'plotdata/magenta.txt" lc rgb "magenta"
     1 lw 0.5 w l'
      write(150,*) 'pause -1'

      return
      end

      subroutine setthreecolors2

      include 'DATMAI.INC'

      write(150,*) 'plot [0:10000] [0:7000] "'//
     1 trim(HOME)//'plotdata/black.txt" lc rgb "black"
     1 lw 0.5 w l,"'//
     1 trim(HOME)//'plotdata/yellow.txt" lc rgb "dark-yellow"
     1 lw 0.5 w l,"'//
     1 trim(HOME)//'plotdata/red.txt" lc rgb "red" lw 0.5 w l'
      write(150,*) 'pause -1'

      return
      end


      subroutine setfourcolors

      include 'DATMAI.INC'

      write(150,*) 'plot [0:10000] [0:7000] "'//
     1 trim(HOME)//'plotdata/black.txt" lc rgb "black"
     1 lw 0.5 w l,"'//
     1 trim(HOME)//'plotdata/yellow.txt" lc rgb "dark-yellow"
     1 lw 0.5 w l,"'//
     1 trim(HOME)//'plotdata/magenta.txt" lc rgb "magenta"
     1 lw 0.5 w l,"'//
     1 trim(HOME)//'plotdata/red.txt" lc rgb "red" lw 0.5 w l'
      write(150,*) 'pause -1'

      return
      end

      SUBROUTINE MAC_EDITOR
        IMPLICIT NONE
        INCLUDE 'DATMAI.INC'
        INCLUDE 'DATMAC.INC'
C
        RESOLVEIT=.TRUE.
        call system('vi MAC_EDIT.DAT')

        RETURN
      END


      subroutine OSCHECK(I)

      implicit none
      integer I

!      if(system('ver > /dev/null 2>&1').eq.0) I=1        !Windows
      if(system('uname > /dev/null 2>&1').eq.0) I=2      !Linux(unix)
      if(system('hostinfo> /dev/null 2>&1').eq.0) I=3   !MacOSX

      return
      end

      subroutine plotbmp(BMPFILE)

      include 'DATMAI.INC'
      character*80 BMPFILE
      logical BMPEXIST

      open(113,file=trim(HOME)//'PLOTBMP.TXT')

      write(113,*) 'set terminal wxt font "Courier,5"'
      write(113,*) 'set noborder'
      write(113,*) 'set nokey'
      write(113,*) 'set notics'
      write(113,*) 'plot "'//trim(BMPFILE)//'" binary
     1array=(320,240) skip=54 format="%uint8" using 3:2:1
     2with rgbimage'
      write(113,*) 'pause -1'
      close(113)

      call system('gnuplot '//trim(HOME)//'PLOTBMP.TXT')

      inquire(file=trim(BMPFILE),exist=BMPEXIST)

      if (.not.BMPEXIST) then

        WRITE(OUTLYNE,*) 'ERROR OPENING ',TRIM(BMPFILE)
        CALL SHOWIT(1)
        CALL MACFAL
        RETURN

      END IF
      return
      end


      subroutine loadbmp(BMPFILE,ABMPDATA24)

!     BMP reading routine obtained from
!     http://www.rhinocerus.net/forum/lang-fortran/93927-read-bmp-into-fortran.html

      include 'DATMAI.INC'
      integer Maxwidth,Maxheight,irec,iwidth,iheight,i,j,L,ipad
      parameter(Maxwidth=3220,Maxheight=2415)
      character header(54),IR,IG,IB
      character*80 BMPFILE
      integer image(Maxheight*Maxwidth)
      integer ABMPDATA24(iwidth*iheight)
      common iwidth,iheight,header,image

      write(6,*) iwidth,iheight

      open(114,file=trim(BMPFILE),form='unformatted',
     &access='direct',recl=1)

      do irec=1,54
        read(114,rec=irec) header(irec)
      end do

      if(ichar(header(11)).ne.54.or.ichar(header(29)).ne .
     &24.or.ichar(header(31)).ne.0) then
      print*,'sorry, can not handle this file'
      end if

      ! get image height and width
      iheight=ichar(header(23))+256*(ichar(header(24))
     &+256*(ichar(header(25))+256*ichar(header(26))))
      iwidth=ichar(header(19))+256*(ichar(header(20))
     &+256*(ichar(header(21))+256*ichar(header(22))))

!      print*,'iheight,iwidth=',iheight,iwidth

      ipad=(Maxwidth-iwidth)*3-((Maxwidth-iwidth)*3/4)*4
      irec=54
      L=1
      do i=1,iheight
        do j=1,iwidth
            read(114,rec=irec) IR
            image(irec)=ichar(IR)
            ABMPDATA24(L)=ichar(IR)
            L=L+1
            irec=irec+1
        end do
        irec=irec+ipad
      end do

      close(114)

      return
      end


      subroutine savebmp(BMPFILE,BMPDATA24)

      include 'DATMAI.INC'
      integer Maxwidth,Maxheight,iheight,iwidth,irec,L
      parameter(Maxwidth=3220,Maxheight=2415)
      integer BMPDATA24(iwidth*iheight)
      character header(54),bmpdataR,bmpdataG,bmpdataB
      character*80 BMPFILE
      common iwidth,iheight,header

      open(112,file=trim(BMPFILE),form='unformatted',
     &access='direct',recl=1)

      do irec=1,54
        write(112,rec=irec) header(irec)
      end do

      irec=55

      do L=1,iheight*iwidth
            bmpdataR=char(BMPDATA24(L))
            write(112,rec=irec) bmpdataR
            irec=irec+1
      end do

      close(112)
      return
      end

      subroutine bmpinfo(BMPFILE,info)

      include 'DATMAI.INC'
      integer irec,iwidth,iheight
      integer info(3)
      character header(54)
      character*80 BMPFILE
      common iwidth,iheight,header

      open(114,file=trim(BMPFILE),form='unformatted',
     &access='direct',recl=1)

      do irec=1,54
        read(114,rec=irec) header(irec)
      end do

      if(ichar(header(11)).ne.54.or.ichar(header(29)).ne .
     &24.or.ichar(header(31)).ne.0) then
      print*,'sorry, can not handle this file'
      end if

      ! get image height and width
      info(2)=ichar(header(23))+256*(ichar(header(24))
     &+256*(ichar(header(25))+256*ichar(header(26))))
      info(3)=ichar(header(19))+256*(ichar(header(20))
     &+256*(ichar(header(21))+256*ichar(header(22))))

      iwidth=info(2)
      iheight=info(3)

      close(114)

      return
      end



      subroutine RGBsplit(ABMPDATA24,L,IR,IB,IG)

      include 'DATMAI.INC'
      integer IR,IB,IG,L,ABMPDATA24
      dimension ABMPDATA24(iwidth*iheight)
      common iwidth,iheight

            IG=int(ABMPDATA24(L))
            IR=int(ABMPDATA24(L+1))
            IB=int(ABMPDATA24(L+2))

      return
      end
