      module mod_script
        interface mscript
           module  procedure mscriptr,mscripi,mscriptl4,mscriptl1
!     *     ,mscriptr_format
        end interface
        contains
      !***************************************************************
      subroutine mscriptr(oap,file)
      IMPLICIT REAL(8)(A-H,O-Z)
      dimension oap(:,:)
      integer,intent(in),optional :: file
      integer :: ifile
      if(present(file)) then
         ifile=file
      else
         ifile=6
      endif
      if(maxval(oap)>=1000..or.minval(oap)<-100.) then
         call mscriptr2(oap)
         return
      endif
      m=size(oap,dim=1)
      n=size(oap,dim=2)
      nnn=10
      NN=N/nnn
      IF(NN.EQ.0) GOTO 85
      DO 83 KK=1,NN
      N1=nnn*(KK-1)+1
      N2=nnn*KK
      IF(N2.LT.N1) RETURN
      write(ifile,*) ' '
      write(ifile,1024) (I,I=N1,N2)
      DO 83 JJ=1,M
83    write(ifile,1025) JJ,(OAP(JJ,II),II=N1,N2)
      write(ifile,*) ' '
85    N1=nnn*NN+1
      N2=N
      IF(N2.LT.N1) RETURN
      write(ifile,*) ' '
      write(ifile,1024) (I,I=N1,N2)
      write(ifile,*) ' '
      DO 84 JJ=1,M
84    write(ifile,1025) JJ,(OAP(JJ,II),II=N1,N2)
      write(ifile,*) ' '
1024  FORMAT(3X,10(3X,I5))
1025  FORMAT(I5,10F8.4)
      RETURN
      end subroutine
      !***************************************************************
       subroutine mscriptr2(oap,file)
      IMPLICIT REAL(8)(A-H,O-Z)
      dimension oap(:,:)
      integer,intent(in),optional :: file
      integer :: ifile
      if(present(file)) then
         ifile=file
      else
         ifile=6
      endif
      m=size(oap,dim=1)
      n=size(oap,dim=2)
      nnn=5
      NN=N/nnn
      IF(NN.EQ.0) GOTO 85
      DO 83 KK=1,NN
      N1=nnn*(KK-1)+1
      N2=nnn*KK
      IF(N2.LT.N1) RETURN
      write(ifile,*) ' '
      write(ifile,1024) (I,I=N1,N2)
      write(ifile,*) ' '
      DO 83 JJ=1,M
83    write(ifile,1025) JJ,(OAP(JJ,II),II=N1,N2)
      write(ifile,*) ' '
85    N1=nnn*NN+1
      N2=N
      IF(N2.LT.N1) RETURN
      write(ifile,*) ' '
      write(ifile,1024) (I,I=N1,N2)
      write(ifile,*) ' '
      DO 84 JJ=1,M
84    write(ifile,1025) JJ,(OAP(JJ,II),II=N1,N2)
      write(ifile,*) ' '
1024  FORMAT(1X,5(3X,I9))
1025  FORMAT(I5,5F12.6)
      RETURN
      end subroutine
      !***************************************************************
!      subroutine mscriptr_format(oap,iform,file)
!      IMPLICIT REAL(8)(A-H,O-Z)
!      dimension oap(:,:)
!      character(15) format1,format2
!      integer iform
!      integer,intent(in),optional :: file,iform
!      integer :: ifile
!      if(present(file)) then
!         ifile=file
!      else
!         ifile=6
!      endif
!      m=size(oap,dim=1)
!      n=size(oap,dim=2)
!      nnn=80/(4+iform)
!
!      format1='(1X,10I8)'
!      !format1(1:4)='(1X,'
!      if(nnn<10) then
!         write(format1(5:5),'(i1)') nnn
!         l=6
!      else
!         write(format1(5:6),'(i2)') nnn
!         l=7
!      endif
!      if(iform+4<10) then
!         format1(l:l+3)='I8)'
!      else
!         format1(l:l+3)='I9)'
!      endif
!
!      !format2='(I5,10F8.4)'
!      format2(1:7)='(I4,1X,'
!      if(nnn<10) then
!         write(format2(8:8),'(i1)') nnn
!         l=9
!      else
!         write(format2(8:9),'(i2)') nnn
!         l=10
!      endif
!      if(iform+4<10) then
!         format2(l:l+3)='F8.'
!      else
!         format2(l:l+3)='F9.'
!      endif
!      if(iform<10) then
!         write(format2(l+4:l+4),'(i1)') iform
!         format2(l+5:l+5)=')'
!      else
!         write(format2(l+4:l+5),'(i2)') iform
!         format2(l+6:l+6)=')'
!      endif
!
!      NN=N/nnn
!      IF(NN.EQ.0) GOTO 85
!      DO 83 KK=1,NN
!      N1=nnn*(KK-1)+1
!      N2=nnn*KK
!      IF(N2.LT.N1) RETURN
!      write(ifile,*) ' '
!      write(ifile,format1) (I,I=N1,N2)
!      write(ifile,*) ' '
!      DO 83 JJ=1,M
!83    write(ifile,format2) JJ,(OAP(JJ,II),II=N1,N2)
!      write(ifile,*) ' '
!85    N1=nnn*NN+1
!      N2=N
!      IF(N2.LT.N1) RETURN
!      write(ifile,*) ' '
!      write(ifile,format1) (I,I=N1,N2)
!      write(ifile,*) ' '
!      DO 84 JJ=1,M
!84    write(ifile,format2) JJ,(OAP(JJ,II),II=N1,N2)
!      write(ifile,*) ' '
!      RETURN
!      end subroutine
      !***************************************************************
      SUBROUTINE mscripi (mat,iform,file)
      IMPLICIT REAL*8(A-H,O-Z)
      integer,intent(in),optional :: iform
      character*20 f1025,f1024,aa
      DIMENSION mat(:,:)
      integer,intent(in),optional :: file
      integer :: ifile
      if(present(file)) then
         ifile=file
      else
         ifile=6
      endif

      m=size(mat,dim=1)
      n=size(mat,dim=2)

! adaptation du format a la taille des nombres a ecrire
      ll=1
      if(n>9.or.m>9) ll=2
      if(n>99.or.m>99) ll=3
      do i=1,n
         do j=1,m
            write(aa,*) mat(j,i)
            ll=max(ll,len_trim(aa))
         enddo
      enddo
      ll=ll+4
      jform=80/ll

! Format:
      if(present(iform)) then
         jform=iform
      endif
      nnn=80/jform
      if(jform.lt.10) then
         write(f1024,'(''(4X,'',i2,''i'',i1,'')'')') nnn,jform
         write(f1025,'(''(I4,'',i2,''i'',i1,'')'')') nnn,jform
      else
         write(f1024,'(''(4X,'',i2,''i'',i2,'')'')') nnn,jform
         write(f1025,'(''(I4,'',i2,''i'',i2,'')'')') nnn,jform
      endif
!
      NN=N/nnn
      IF(NN.EQ.0) GOTO 85
      DO 83 KK=1,NN
      N1=nnn*(KK-1)+1
      N2=nnn*KK
      IF(N2.LT.N1) RETURN
      write(ifile,*) ' '
      write(ifile,f1024) (I,I=N1,N2)
      DO 83 JJ=1,M
83    write(ifile,f1025) JJ,(mat(JJ,II),II=N1,N2)
85    N1=nnn*NN+1
      N2=N
      IF(N2.LT.N1) RETURN
      write(ifile,f1024) (I,I=N1,N2)
      DO 84 JJ=1,M
84    write(ifile,f1025) JJ,(mat(JJ,II),II=N1,N2)
1024  FORMAT(4X,10(6X,I2))
1025  FORMAT(3X,I2,10i8)
      RETURN
      END subroutine
      !***************************************************************
       subroutine mscriptl4(yy,file)
      IMPLICIT REAL(8)(A-H,O-Z)
      logical yy(:,:)
      integer,intent(in),optional :: file
      integer :: ifile
      if(present(file)) then
         ifile=file
      else
         ifile=6
      endif
      m=size(yy,dim=1)
      n=size(yy,dim=2)
      nnn=10
      NN=N/nnn
      IF(NN.EQ.0) GOTO 85
      DO 83 KK=1,NN
      N1=nnn*(KK-1)+1
      N2=nnn*KK
      IF(N2.LT.N1) RETURN
      write(ifile,*) ' '
      write(ifile,1024) (I,I=N1,N2)
      write(ifile,*) ' '
      DO 83 JJ=1,M
83    write(ifile,1025) JJ,(yy(JJ,II),II=N1,N2)
      write(ifile,*) ' '
85    N1=nnn*NN+1
      N2=N
      IF(N2.LT.N1) RETURN
      write(ifile,*) ' '
      write(ifile,1024) (I,I=N1,N2)
      write(ifile,*) ' '
      DO 84 JJ=1,M
84    write(ifile,1025) JJ,(yy(JJ,II),II=N1,N2)
      write(ifile,*) ' '
1024  FORMAT(1X,10(3X,I5))
1025  FORMAT(I5,10l8)
      RETURN
      end subroutine
      !***************************************************************
       subroutine mscriptl1(yy,file)
      IMPLICIT REAL(8)(A-H,O-Z)
      logical(1) yy(:,:)
      integer,intent(in),optional :: file
      integer :: ifile
      if(present(file)) then
         ifile=file
      else
         ifile=6
      endif
      m=size(yy,dim=1)
      n=size(yy,dim=2)
      nnn=10
      NN=N/nnn
      IF(NN.EQ.0) GOTO 85
      DO 83 KK=1,NN
      N1=nnn*(KK-1)+1
      N2=nnn*KK
      IF(N2.LT.N1) RETURN
      write(ifile,*) ' '
      write(ifile,1024) (I,I=N1,N2)
      write(ifile,*) ' '
      DO 83 JJ=1,M
83    write(ifile,1025) JJ,(yy(JJ,II),II=N1,N2)
      write(ifile,*) ' '
85    N1=nnn*NN+1
      N2=N
      IF(N2.LT.N1) RETURN
      write(ifile,*) ' '
      write(ifile,1024) (I,I=N1,N2)
      write(ifile,*) ' '
      DO 84 JJ=1,M
84    write(ifile,1025) JJ,(yy(JJ,II),II=N1,N2)
      write(ifile,*) ' '
1024  FORMAT(1X,10(3X,I5))
1025  FORMAT(I5,10l8)
      RETURN
      end subroutine
      !***************************************************************
      end module
