!=====================================================================
! dcl $B$r;H$C$?%0%i%U=PNO%W%m%0%i%`(B
!    - 
! 2001/12/12
! 2001/12/30 $B;~9o(B t $B$K$*$1$k=PNO$N$_$,07$($k(B
!            $B0[$J$k;~9o$N%G!<%?$O=PNO$5$;$F$$$J$$(B.
!=====================================================================
program graph
  use prm_4                        ! prm $B$O<B?t$OG\@:EY$G;H$($J$$$N$GJL$KMQ0U(B
  use dcl

  implicit none

! 2d-ice-model $B$K$h$C$F=PNO$5$l$?%G!<%?$NG[Ns(B
  real                 :: h(0:im-1)             ! $BI98|(B($B@0?t3J;RE@(B)
  real                 :: u(0:im-1, 0:km)       ! $B?eJ?B.EY(B u($B?eJ?(B, $BH>@0?t(B)
  real                 :: w(0:im-1, 0:km)       ! $B1tD>B.EY(B w($B?eJ?(B, $BH>@0?t(B)

  real                 :: T(0:im-1, 0:km+1)       ! $B29EY(B
  real                 :: Tdisp(0:im-1, 0:kdisp)! $B29EY=PNOMQ$NJQ?t(B

! $B:BI8<4$r@_Dj$9$k$?$a$NJQ?t(B
  real                 :: x(0:im-1)             ! x $B:BI8(B($B=PNO$N:]$KI,MW(B)
  real                 :: z_all(0:im-1,0:km+1)  ! $BA4(B z $B:BI8(B($B=PNO$N:]$KI,MW(B)
  real                 :: z_i(0:km)             ! $B3F(B x $B:BI8$K$*$1$k(B z $B:BI8(B
  real                 :: z(0:kdisp)            ! $B29EY1tD>:BI8(B
  real                 :: zk(0:km)              ! $B&F:BI8(B

  real                 :: uk(0:im-1, 0:km)      ! $B%Y%/%H%k>l$rI=<($5$;$k$?$a$N(B
  real                 :: wk(0:im-1, 0:km)      ! $BCV$-49$($k$?$a$NJQ?t(B


  integer              :: i, k, l               ! do $BJQ?t(B
  integer              :: io                    ! $B@)8fJQ?t(B, $B%0%i%U=PNOJQ?t(B
  

! $B7W;;7k2L$N=PNO%U%!%$%k$r%*!<%W%s(B
  open(8, file='h.txt')
  open(9, file='u.txt')
  open(10, file='w.txt')
  read (8, *) h                               ! $BI98|%G!<%?FI$_9~$_(B
  read (9, *) u                               ! $B?eJ?B.EY%G!<%?FI$_9~$_(B
  read (10,*) w                               ! $B1tD>B.EY%G!<%?FI$_9~$_(B
! $B=PNO%U%!%$%k$r%/%m!<%:(B
  close(8)
  close(9)
  close(10)

! $B29EY%W%m%U%!%$%k$O(B pending
 open(11,file='T-sample.txt')
 read (11, *, iostat=io) T
 close(11)

! $B:BI8CM$N7hDj(B
  !x $B<4(B
  x  = (/(i, i=0, im-1)/) * dx

  !$B&F<4(B
  zk = (/0.0, 0.02, 0.05, 0.10, 0.17, 0.25,0.40, 0.55, 0.70, 0.85, 1.0/)

  ! $B$"$k(B x $B$K$*$1$k(B z $B3J;RE@$N0LCV7hDj(B
  z_all(:, km+1)    = hmax      ! z_all(:, km)$B$OI9>2I=LL(B
                                ! z_all(:, km+1)$B$O6k7ANN0h$N>eC<(B
  do i = 0, im-1
     do k = 0, km
        z_all(i, k) = zk(k) * h(i)
     enddo
  enddo

  ! $B6k7ANN0h$N(B z $B<4(B
  z = (/(k, k = 0, kdisp)/) * hmax/kdisp
  

! $B@~7AJd4V$7$F(B T $BJ,I[$rIA<L(B
! $B6k7ANN0h$N>eC<$NCM(B($BI9>2>e6u$J$N$G(B T_s $B$H$9$k(B)
  T(:, km+1) = T_s
  

  do i = 0, im-1
     if(z_all(i, km) == 0.)then     ! $B?eJ?J}8~$KBP$7$F$NI9>26-3&(B
        Tdisp(i, :) = T_s           
     else
        do l = 0, kdisp             
           do k = 0, km             
              if(z(l) >= h(i))then  ! $B1tD>J}8~$KBP$7$F$NI9>26-3&(B
                 Tdisp(i, l) = T_s                 
                                    ! $BI98|FbIt$NNN0h(B($B@~7AJd4V(B)
              elseif((z(l) >= z_all(i, k)).and.(z_all(i, k+1) >= z(l)))then
                 Tdisp(i, l) = ((z_all(i, k+1) - z(l)) * T(i, k)           &
                      &      + (z(l) - z_all(i, k)) * T(i, k+1))         &
                      &    /(z_all(i, k+1) - z_all(i, k))
              end if
           end do
        end do
     end if
  end do
  

! $B3NG'$N$?$a$N%U%!%$%k=PNO(B

! open(12, file='z_all.txt')      
! do i = 0, im-1
!    do k = 0, km
!       write(12, '(e15.7)') z_all(i, k)
!    end do
! end do
! close(12)

  print *, 'graph is 0(ice-shape) or 1(u, w) or 2(T)'
  read *, io
  if(io == 0 )then

!=====================================================================
! $BI98|%W%m%U%!%$%k:n@.?^(B make_h_profile(xi, h)
!=====================================================================

     call DclOpenGraphics()          ! $B=PNOAuCV$N%*!<%W%s(B  
     call DclNewFrame                ! $B?7$7$$IA2hNN0h$r:n@.$9$k(B
     call DclSetWindow( 0., 1.e-3 * dx*(im-1) , 0., hmax )
     call DclSetViewPort( 0.2, 0.8, 0.4, 0.7 ) 
     call DclSetTransFunction        !
     call DclDrawScaledAxis          ! $B:BI8<4:n@.(B
     call DclDrawLine( 1.e-3 * x, h) ! $B%W%m%U%!%$%k:n@.(B
     call DclCloseGraphics           ! $B=PNOAuCV$N%/%m!<%:(B  

!=====================================================================
! z $B:BI87O$K$*$1$kB.EY>lI=<(%5%V%k!<%A%s(B
! $BJ}?K(B
!   $B!&(B x $B<4$N3F(B grid $B$KBP$7$F<4$r:n@.$9$k(B
!   $B!&<4$KBP1~$9$kG[Ns$r=`Hw(B(im x (km+1) $B$H$$$&G[Ns$,(B im $B8DI,MW(B)
!       - $B<40J30$NG[NsCM$O(B 0 
!=====================================================================

  elseif(io == 1)then
     call DclOpenGraphics()
     call DclNewFrame
     call DclSetWindow( 0., 1.e-3 * dx*(im-1) , 0., hmax + 200.)
     call DclSetViewPort( 0.2, 0.8, 0.4, 0.8)         ! $BIA<LNN0h$N:n@.(B
     call DclSetTransFunction
     call DclDrawScaledAxis
     call DclDrawLine(1.e-3 * x, h)               ! $B;~9o(B t $B$NI98|%W%m%U%!%$%k(B

     
! $B%Y%/%H%k$N%9%1!<%j%s%0%Q%i%a!<%?$r<jF0$G@_Dj(B.
! $B%9%1!<%j%s%0%Q%i%a!<%?$O<4$,$?$/$5$s$"$k$N$G(B
! $B%G%U%)%k%H$G$OI=<($5$;$J$$(B.
! $B$3$3$G$O(B x, y $B$b6&$K$9$Y$F(B 1.e-3 $B$H$7$?(B.
!(scaling factor $B$O(B $B%Q%i%a!<%?JQ99$K$h$C$F?o;~JQ99$9$kI,MW$"$j(B)


     call DclSetParm( 'ENABLE_VECTOR_NORMALIZING', .false. )  
     call DclSetParm('ENABLE_VECTOR_MESSAGE', .false.)       
     call DclSetParm('VECTOR_SCALING_FACTOR_X', 1.e-3)
     call DclSetParm('VECTOR_SCALING_FACTOR_Y', 1.e-3)



     do i =0, im-1
        if(i==0)then
           call DclSetParm('ENABLE_VECTOR_MESSAGE', .true.)
           call DclSetParm('VECTOR_SCALING_FACTOR_X', 1.e-3)
           call DclSetParm('VECTOR_SCALING_FACTOR_Y', 1.e-3)
        else
           call DclSetParm('ENABLE_VECTOR_MESSAGE', .false.)
        end if
        uk = u                ! i $B$K$*$1$k:BI8MQ(B u $BG[Ns$rMQ0U(B
        wk = w                ! i $B$K$*$1$k:BI8MQ(B w $BG[Ns$rMQ0U(B


! $B:BI8MQ$N(B u,w $BG[Ns$K$*$1$k(B i $B0J30$NG[NsMWAG$r(B 0$B$H$9$k(B
        if(i/=0)then          
           uk(:i-1, :) = 0    !  
           wk(:i-1, :) = 0
           uk(i+1:, :) =  0
           wk(i+1:, :) =  0 
        else
           uk(i+1:, :) =  0
           wk(i+1:, :) =  0 
        end if


        z_i(0:km) = z_all(i, 0:km)              ! i $B$K$*$1$k:BI8<4$r:n@.(B
        
        call DclSetYGrid(z_i)                   ! i $B$N(B z $B:BI8<4$r@_Dj(B
        call DclSetXEvenGrid(0., (1.e-3 * dx*(im-1)), im) 
                                                ! x $B:BI8<4$N@_Dj(B
        call DclDrawVectors(uk *s_a, wk * s_a)  ! $BB.EY%Y%/%H%k>l$N:n@.(B
    end do

    call DclCloseGraphics                       ! $B=PNOAuCV$N%/%m!<%:(B  
 

!=====================================================================
! $B29EYEyCM@~I=<(%5%V%k!<%A%s(B
! 
!=====================================================================
 else
    call DclOpenGraphics()
    call DclNewFrame
    call DclSetWindow( 0., 1.e-3 * dx*(im-1) , 0., hmax) 
    call DclSetViewPort( 0.2, 0.8, 0.2, 0.8 )
    call DclSetTransFunction
    call DclDrawScaledAxis
    call DclDrawLine(1.e-3 * x, h)               ! $B;~9o(B t $B$NI98|%W%m%U%!%$%k(B
    
    ! call DclDrawScaledAxis
    call DclSetXEvenGrid(0. , (1.e-3 * dx * (im-1)), im)
    call DclSetYEvenGrid(0. , hmax , kdisp)
    call DclDrawContour( Tdisp )
    
    call DclCloseGraphics
 end if


end program graph
