implicit none
 real :: m(100,100),a1,a2,a3,a4,a5,a6,ms(100,100,4),d(100,100),deg(100,100),pi
 real :: c,fai,h,gat,ce,faie,he,gate,kh,rad(100,100),fair,faier,f(100,100),fe(100,100)
 integer :: i,j,k,tate,yoko
 character(1000) :: filename
 character(1000) :: filename2


pi=asin(1.d0)*2.d0

  print *,"開くファイルを指定(拡張子まで)"
   read '(A)',filename
  print *,"新規(拡張子まで)"
   read '(A)',filename2

  print *,"区分けの数の入力"
  print *,"縦"
   read *,tate
  print *,"横"
   read *,yoko

  print *,"降雨時の土質定数等を入力"
  print *,"土の粘着力(kpa)"
   read *,c
  print *,"土の内部摩擦力(deg)"
   read *,fai
          fair=fai*pi/180
  print *,"表土層厚(m)"
   read *,h
  print *,"土の単位体積重量(kN/m^3)"
   read *,gat
   
  print *,"地震時の土質定数等を入力"
  print *,"土の粘着力(kpa)"
   read *,ce
  print *,"土の内部摩擦力(deg)"
   read *,faie
          faier=faie*pi/180
  print *,"表土層厚(m)"
   read *,he
  print *,"土の単位体積重量(kN/m^3)"
   read *,gate
  print *,"水平震度"
   read *,kh


 open(11,file = filename)
  do i=1,tate
   read(11,*),a1,a2,a3,a4,a5,a6

    m(i,1)=a1
    m(i,2)=a2
    m(i,3)=a3
    m(i,4)=a4
    m(i,5)=a5
    m(i,6)=a6
  end do

 close(11)
  do i=1,tate-2
   do j=1,yoko-2
   
    ms(i,j,1)=abs( m(i+1,j+1)-m(i,j+1) )
    ms(i,j,2)=abs( m(i+1,j+1)-m(i+1,j) )
    ms(i,j,3)=abs( m(i+1,j+1)-m(i+2,j+1) )
    ms(i,j,4)=abs( m(i+1,j+1)-m(i+1,j+2) )
    
     d(i,j)=0
     do k=1,4
     
      if( d(i,j) <= ms(i,j,k) ) then
       d(i,j) = ms(i,j,k)
      end if
     end do
     
     rad(i,j) = (atan(d(i,j)/50))
     deg(i,j) = rad(i,j)*180/pi
       f(i,j) = ( tan(fair)/tan(rad(i,j)) )+( c/(gat*h*cos(rad(i,j))*sin(rad(i,j))) )
      fe(i,j) = ( (cos(rad(i,j))-(kh*sin(rad(i,j))))/(sin(rad(i,j))+(kh*cos(rad(i,j)))) )*tan(faier)&
      +( ce/( gate*he*cos(rad(i,j))*(sin(rad(i,j))+( kh*cos( rad(i,j) ) )) )) 
     
   end do
  end do
  
  open(12,file = filename2,status = 'replace')
   do i=1,4
    do j=1,4
     write(12,*),deg(i,j), ',', f(i,j), ',', fe(i,j)
    end do   
   end do

  close(12)
  

end


 