      subroutine datacolorssave(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,*)
            write(130,'(2I5)') I1,I2
        end if
      end if

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

        end if
      end if

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

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

      if (I4.eq.4) then
        if (I3.eq.1) then
            write(118,'(2I5)') I1,I2
        else
            write(118,*)
            write(118,'(2I5)') I1,I2
        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,*)
            write(130,'(2I5)') I1,I2
        end if
      end if

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

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

        end if
      end if

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

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

        end if
      end if

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

        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,*)
        write(116,'(2I5)') I1,I2
      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(I)
      implicit none

      include 'DATMAI.INC'

      character KKDP*3
      integer I

      call SELECTKDP(KKDP)

      write(6,'(A$)') KKDP//'> '

!      call disphistory(KKDP,I,INPUT) !If you use histrory, remove ! this line
      read(5,'(a$)') INPUT           !If you use history, put ! top of this line

      WC='        '

      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 disphistory(KKDP,I,INPUT0)
      implicit none

      include 'DATMAI.INC'

      character KKDP*3,HISTORY*15,INPUT0*15
      CHARACTER :: CR = CHAR(13)
      integer KEY0,KEY1,KEY2,I,HISTNO
      logical f_exist

      KEY0=0
      KEY1=0
      KEY2=0

      call SYS_KEYSET(1)

      HISTNO=I
      HISTORY=""

      if (I.eq.1) HISTNO=1

 !INPUT must be charactor

      do while (KEY0.lt.32)

         call SYS_KEYIN(KEY0)

         if (KEY0.eq.27) then
            call SYS_KEYIN(KEY1)
            call SYS_KEYIN(KEY2)

            if ((KEY2.eq.67).or.(KEY2.eq.68)) continue

            if (KEY2.eq.65) then !Up Arrow
               HISTNO=HISTNO-1
               if (HISTNO.le.0) HISTNO=I-1
               if (I.eq.1) HISTNO=1
            endif
            if (KEY2.eq.66) then !Down Arrow
               HISTNO=HISTNO+1
               if (HISTNO.ge.I) HISTNO=1
               if (I.eq.1) HISTNO=I
            endif

            inquire(file=trim(HOME)//'HISTORY.DAT', exist=f_exist)

            if (f_exist.eqv..true.) then
            open(170,file=trim(HOME)//'HISTORY.DAT',status='old',
     &access='direct',recl=15,form='formatted')
            else
            open(170,file=trim(HOME)//'HISTORY.DAT',status='new',
     &access='direct',recl=15,form='formatted')
            write(170,'(A15)',rec=1) "NO INPUT"
            endif

            read(170,'(A15)',rec=HISTNO) HISTORY
            close(170)

            write(6,'(A$)') CR//'               '
            write(6,'(A$)') CR//KKDP//'> '//trim(HISTORY)

         endif

         if (KEY0.eq.10) then
            call SYS_KEYSET(0)
            write(6,'(A)') CR//KKDP//'> '//HISTORY
            INPUT=HISTORY
            return
         end if

         if (KEY0.eq.127) then
            call SYS_KEYSET(0)
            write(6,'(A$)') CR//'               '
            write(6,'(A)') CR//KKDP//'> '
            INPUT=""
            return
         end if

      end do

      call SYS_KEYSET(0)

      write(6,'(A$)') CR//KKDP//'> '//char(KEY0)
      read(5,'(a$)') INPUT0
      INPUT=char(KEY0)//INPUT0

      open(170,file=trim(HOME)//'HISTORY.DAT',
     &access='direct',recl=15,form='formatted')
 
      if (INPUT.ne."") then
         write(170,'(A15)',rec=I) INPUT
         I=I+1
      endif

      close(170)

      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 setonecolors2

      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/breakblack.txt" with lines lt 0
     1 lc rgb "black" lw 2'

      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'
      write(113,*) 'set font "/data/data/com.droidplot/
     1share/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),cval1
      character*80 BMPFILE
      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*3
        do j=1,iwidth
            read(114,rec=irec) cval1
            ABMPDATA24(L)=ichar(cval1)
            L=L+1
            irec=irec+1
        end do
      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(3)=ichar(header(23))+256*(ichar(header(24))
     &+256*(ichar(header(25))+256*ichar(header(26))))
      info(2)=ichar(header(19))+256*(ichar(header(20))
     &+256*(ichar(header(21))+256*ichar(header(22))))

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

      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 saveeps(GRFILN)

      include 'DATMAI.INC'
      CHARACTER GRFILN*12

#ifdef ANDROID
      call system('sed -e "s/png/postscript eps/g"
     & -e "s/pause -1//g" '
     &//trim(HOME)//'drawcmd.txt > '//trim(HOME)//'epsout.txt')
      call system('/data/data/com.droidplot/bin/droidplot '
     &//trim(HOME)//'epsout.txt > '
     &//trim(USERHOME)//'/'//trim(GRFILN))
#endif

#ifdef OSX
      call system('sed -e "s/aqua/eps/g" -e "s/pause -1//g" '
     &//trim(HOME)//'drawcmd.txt > '//trim(HOME)//'epsout.txt')
      call system('/opt/local/bin/gnuplot '//trim(HOME)//'epsout.txt > '
     &//trim(USERHOME)//'/'//trim(GRFILN))
#endif

#ifdef LINUX
      call system('sed -e "s/wxt/eps/g" -e "s/pause -1//g" '
     &//trim(HOME)//'drawcmd.txt > '//trim(HOME)//'epsout.txt')
      call system('/usr/bin/gnuplot '//trim(HOME)//'epsout.txt > '
     &//trim(USERHOME)//'/'//trim(GRFILN))
#endif
      return
      end


      subroutine savejpg(GRFILN)

      include 'DATMAI.INC'
      CHARACTER GRFILN*12

#ifdef ANDROID
      call system('sed -e "s/png/jpeg/g" -e "s/pause -1//g" '
     &//trim(HOME)//'drawcmd.txt > '//trim(HOME)//'jpgout.txt')
      call system('/data/data/com.droidplot/bin/droidplot '
     &//trim(HOME)//'jpgout.txt > '//trim(USERHOME)//'/'
     &//trim(GRFILN))
#endif

#ifdef OSX
      call system('sed -e "s/aqua/jpeg/g" -e "s/pause -1//g" '
     &//trim(HOME)//'drawcmd.txt > '//trim(HOME)//'jpgout.txt')
      call system('/opt/local/bin/gnuplot '//trim(HOME)//'jpgout.txt > '
     &//trim(USERHOME)//'/'//trim(GRFILN))
#endif

#ifdef LINUX
      call system('sed -e "s/wxt/jpeg/g" -e "s/pause -1//g" '
     &//trim(HOME)//'drawcmd.txt > '//trim(HOME)//'jpgout.txt')
      call system('/usr/bin/gnuplot '//trim(HOME)//'jpgout.txt > '
     &//trim(USERHOME)//'/'//trim(GRFILN))
#endif
      return
      end


      subroutine del_col_files

      include 'DATMAI.INC'

      call system('rm '//trim(HOME)//'plotdata/*.txt')
      call system('touch '//trim(HOME)//'plotdata/black.txt')
      call system('touch '//trim(HOME)//'plotdata/breakblack.txt')
      call system('touch '//trim(HOME)//'plotdata/red.txt')
      call system('touch '//trim(HOME)//'plotdata/yellow.txt')
      call system('touch '//trim(HOME)//'plotdata/magenta.txt')
      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

