      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

      implicit none

      include 'DATMAI.INC'

      character KKDP*3

      call SELECTKDP(KKDP)

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

      if (INPUT.eq.'') then
        INPUT=' '
      endif
      
      if(ECH.EQ.1) then
        OUTLYNE='> '//trim(INPUT)
        call SHOWIT(1)
      end if
 
      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')

#ifdef LINUX
      write(113,*) 'set terminal wxt font "Courier,5"'
#endif
#ifdef OSX
      write(113,*) 'set terminal aqua font "Courier,9"'
#endif
#ifdef ANDROID
      write(113,*) 'set terminal png font "/data/data/com.droidplot/share/
     &fonts/LiberationMono-Regular.ttf,4"'
      write(113,*) 'set size ratio 0.8 1'
#endif
      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)

#ifdef ANDROID
      call system('/data/data/com.droidplot/bin/droidplot '
     &//trim(HOME)//'PLOTBMP.TXT')
#endif
#ifdef OSX
      call system('/opt/local/bin/gnuplot '//trim(HOME)//'PLOTBMP.TXT')
#endif
#ifdef LINUX
      call system('gnuplot '//trim(HOME)//'PLOTBMP.TXT')
#endif

      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*3)
      common iwidth,iheight,header,image

      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))))

      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) IG
            read(114,rec=irec+1) IR
            read(114,rec=irec+2) IB
            image(irec)=ichar(IG)
            image(irec+1)=ichar(IR)
            image(irec+2)=ichar(IB)
            ABMPDATA24(L)=ichar(IG)
            ABMPDATA24(L+1)=ichar(IR)
            ABMPDATA24(L+2)=ichar(IB)
            L=L+3
            irec=irec+3
        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,I,J
      parameter(Maxwidth=3220,Maxheight=2415)
      integer BMPDATA24(iwidth*iheight*3)
      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
      L=1
 
      do I=1,iheight*3
         do J=1,iwidth
            bmpdataG=char(BMPDATA24(L))
            write(112,rec=irec) bmpdataG
            bmpdataR=char(BMPDATA24(L+1))
            write(112,rec=irec+1) bmpdataR
            bmpdataB=char(BMPDATA24(L+2))
            write(112,rec=irec+2) bmpdataB
            irec=irec+3
            if (L.ge.iheight*iwidth*3-3) exit
            L=L+3
         end do
      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*3)
      common iwidth,iheight

      if (L.ge.iwidth*iheight*3-3) return

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

      return
      end

      SUBROUTINE LOWER_CASE(STRUC)
      IMPLICIT NONE
      CHARACTER STRUC*80
      INTEGER I,J
                      DO I=1,80
              J=ICHAR(STRUC(I:I))
      IF(J.GE.65.AND.J.LE.90)
     1       STRUC(I:I)=CHAR(J+32)
                      END DO
                      RETURN
                      END

