C*********************************************************************
C
C     PROGRAM xzcont_uwt
C
C     x-z平面等値線図作成プログラム(時刻, 条件指定)
C
C     履歴 1997/07/24 中島健介
C          1999/02/04 小高正嗣
C          2000/11/21 小高正嗣; D 論用に書き直し
C
C*********************************************************************
      program xzcont_uwtq
*-----------------------------------------------------
      implicit real*8 ( a-h, o-z )
*-----------------------------------------------------

#include "grid_size_M.f"

      parameter ( nbr3 = 3*((nx+6)*(nz+6)/16+1) )
      parameter ( nxp = 256, nzp= 102)
*-----------------------------------------------------
      integer ibr(nbr3)
*-----------------------------------------------------
      parameter ( day = 86400.0 )
      parameter ( hour = 3600.0 )
      parameter ( PI = 3.141592D0 ) 

      real*8
     \   zgrid ( -2:nzb ),
     \   temp0 ( -2:nzb ),
     \   tpot0 ( -2:nzb ),
     \   dens0 ( -2:nzb ),
     \   ppai0 ( -2:nzb ),
     \   pres0 ( -2:nzb ),
     \   rvap0 ( -2:nzb ),
     \   qvap0 ( -2:nzb )

      real*8
     \     u ( -2:nxb, -2:nzb ),
     \     w ( -2:nxb, -2:nzb ),
     \     tpot ( -2:nxb, -2:nzb ),
     \     qvap ( -2:nxb, -2:nzb )

      real*4
     \    z0p ( 0:nz ), 
     \    p0p ( 0:nz ),
     \    d0p ( 0:nz ),
     \ temp0p ( 0:nz ),
     \ tpot0p ( 0:nz ),
     \  pai0p ( 0:nz ),
     \ rvap0p ( 0:nz ),
     \ qvap0p ( 0:nz )

      real*4 z0pp( 0:nzp )

      real*4 
     \      up ( nx+1,nz+1 ),
     \      wp ( nx+1,nz+1 ),
     \      tpotp ( nxp+1, nzp+1 ),
     \      tpall ( nxp+1, nzp+1 ),
     \      tpave ( nzp+1 ),
     \      tpdev ( nxp+1, nzp+1 ),
     \      qvapp ( nxp+1, nzp+1 )

      real*4
     \      usfc  ( nx+1 ),
     \      xplot ( nx+1 ),
     \      xpos  ( nx+1 ),
     \      usfc0 ( nx+1 )


      character*2 mint
      character*2 lt
      character*2 date
      character*17 header

      character*100 dumy
      character CSGI*1
*-----------------------------------------------------
      
      open (11,file='exparam')
      read (11,*) irun
      read (11,*) time0, nloop1, nloop2, dtime, ntmoni, ntkubu, times
      read (11,*) ylat, als
      close(11)

      call FOPEN ( irun+1 )

      do iz = 1,9
         read(16,'(a100)') dumy
      end do

      do iz = -2, nzb
         read(16,*) ii, zgrid(iz), pres0(iz), dens0(iz), temp0(iz),
     \                     tpot0(iz), ppai0(iz), qvap0(iz), rvap0(iz)
      end do

      do iz = 0, nz
         z0p(iz)   = REAL(zgrid(iz)) / 1000.0
         p0p(iz)   = pres0(iz)
         d0p(iz)   = dens0(iz)
         temp0p(iz)= temp0(iz)
         tpot0p(iz)= tpot0(iz)
         pai0p(iz) = ppai0(iz)
         rvap0p(iz)= rvap0(iz)
         qvap0p(iz)= qvap0(iz)
      end do

      write(6,*) 'data number = ', nloop1
      write(6,*) 'INPUT DATA BEGINING, END, & INTERVAL' 
      write(6,*) 'itb ite interval ?'
      read (*,*) itb, ite, interv
      write(6,*) 'SELECT TEMP. MODE (I)' 
      write(6,*) '1; RAW PTEMP 2; DEVIATION ?' 
      read (5,*) IDN

      call sgpwsn
      read(*,*) iws

      call gropn(-iws)
      call sldiv( 'T', 1, 3 )
      call swlset( 'LALT',  .TRUE. )
      call sglset( 'LFULL', .TRUE. )
      call sglset( 'LCORNER', .FALSE. )
      call udlset( 'LMSG', .FALSE. )
      call uzfact(0.6)


      do it = 1, ite

      read(31) time, ((u(ix,iz),ix=-2,nxb), iz=-2,nzb)
      read(33) time, ((w(ix,iz),ix=-2,nxb), iz=-2,nzb)
      read(34) time, ((tpot(ix,iz),ix=-2,nxb), iz=-2,nzb)
      read(35) time, ((qvap(ix,iz),ix=-2,nxb), iz=-2,nzb)

      write(0,*) 'reading time = ', time

      if ( it .ge. itb   .and.  it .le. ite ) then
         if ( mod( (it-itb), interv ) .eq. 0 ) then

            write(0,*) 'writing time = ', time
            
c*** 時刻変換: 秒から LT へ************************************
            time = time + times
            write(date,'(I2.2)') INT( time / day ) + 1

            time = ( time - INT( time / day ) * day ) / hour 

            if ( INT(time) .ge. 24 ) then
               time = time - 24
            end if

            write(lt, '(I2.2)') INT(time)
            write(mint, '(I2.2)') INT( MOD(time,1)*60.0 )

            header = 'day=' // date // ' ' // 'LT=' // lt // ':' // mint
c**************************************************************

c** 2-grid noize の除去 ***************************************

            do iz = 1,nz+1            
               do ix = 1, nx+1
                  up(ix,iz)    = u(ix-1,iz-1)
c                  wp(ix,iz)    = w(ix-1,iz-1)
                  wp(ix,iz)    = ( w(ix-2,iz-1) +
     \                 2.0*w(ix-1,iz-1) + w(ix,iz-1))/4.0
               end do
            end do

            do iz = 1,nzp+1            
               do ix = 1, nxp+1
                  qvapp(ix,iz) = qvap(2*(ix-1),2*(iz-1))
c                  tpotp(ix,iz) = tpot(ix*2-1,iz*2-1)
                  tpotp(ix,iz) = ( tpot(2*(ix-1)-1,2*(iz-1)) +
     \        2.0*tpot(2*(ix-1),2*(iz-1)) + tpot(2*ix-1,2*(iz-1)))/4.0
c                  tpall(ix,iz) = tpot(ix-1,iz-1) + tpot0(iz*2-1)
                  tpall(ix,iz) = tpotp(ix,iz) + tpot0(2*(iz-1))

               end do
               z0pp(iz) = z0p( 2*(iz-1) )
            end do
            z0pp(nzp+1) = z0p(nz)
*
*
c**************************************************************

            do iz = 1, nzp+1
               tpave(iz) = 0.
               do ix = 1,nxp+1
                  tpave(iz) = tpave(iz) + tpall(ix,iz) /(nxp+1)
               end do
            end do

            do iz = 1,nzp+1
               do ix = 1, nxp+1
                  tpdev(ix,iz) = tpall(ix,iz) - tpave(iz) 
               end do
            end do

            call grfrm
            call grswnd ( 0.0, 51.2, 0.0, 20.0 )
            call grsvpt ( 0.2, 0.9, 0.1, 0.35 )
            call grstrn (1)
            call uspfit
            call grstrf
            call udpset ( 'rsizet', 0.01 )
            call ussttl ( 'W[m/s]', 'X[km]', '', 'Z[km]' )
            call usdaxs
            call uwsgya ( z0p, nz+1 )
            call uxsttl ( 'T', header, 1. )
            call udpset ( 'ICYCLE', 5 )
            call udgcla ( -50.0, 50.0, 5.0 )
            call udcntz ( wp, nx+1, nx+1, nz+1, ibr, nbr3)
*
*
            call grfrm
            call grswnd ( 0.0, 51.2, 0.0, 20.0 )
            call grsvpt ( 0.2, 0.9, 0.17, 0.42 )
            call grstrn (1)
            call uspfit
            call grstrf
            call udpset ( 'rsizet', 0.01 )
            call ussttl ( '', '', '', 'Z[km]' )
            call uzlset ( 'LABELXB', .FALSE. )
            call usdaxs
            call uwsgya ( z0p, nz+1 )
            call uxsttl ( 'T', header, 1. )
            call udpset ( 'ICYCLE', 5 )
            call udgcla ( -50.0, 50.0, 5.0 )
            call udcntz ( up, nx+1, nx+1, nz+1, ibr, nbr3)

            do ix = 1, nx+1
               usfc(ix) = ( u(ix,0) + 
     \                         2.0* u(ix-1,0) + u(ix+2,0) )/4.0
               xpos(ix) = 0.1 * REAL( ix - 1 )
               usfc0(ix) = 0.0
            end do

            call grfig
            call grswnd ( 0.0, 51.2, -30.0, 30.0 )
            call grsvpt ( 0.2, 0.9, 0.08, 0.17 )
            call grstrn (1)
            call uspfit
            call grstrf
            call udpset ( 'rsizet', 0.01 )
            call uzlset ( 'LABELXB', .TRUE. )
            call uyaxdv ( 'L', 10., 20. )
            call uyaxdv ( 'R', 10., 20. )
            call uxaxdv ( 'B', 1., 5. )
            call uxaxdv ( 'T', 1., 5. )
            call uxsttl ( 'B', '(X[km])', 1. )
            call uxsttl ( 'B', 'U[m/s]', 0. )
            call uulinz ( nx+1, xpos, usfc, 1, 3 )
            call uulinz ( nx+1, xpos, usfc0, 1, 1 )
*
*
            call grfrm
            call grswnd ( 0.0, 51.2, 0.0, 20.0 )
c            call grsvpt ( 0.2, 0.9, 0.15, 0.4 )
            call grsvpt ( 0.2, 0.9, 0.13, 0.38 )
            call grstrn (1)
            call uspfit
            call grstrf
            call udpset ( 'rsizet', 0.01 )
            call uwsgya ( z0pp, nzp+1 )

            if ( IDN .eq. 1 ) then 

            call ussttl ( CSGI(135)//'[K]', 'X[km]', '', 'Z[km]' )
            call udpset ( 'ICYCLE', 5 )

            call usdaxs
            call uxsttl ( 'T', header, 1. )
            call udlset ( 'LABEL', .TRUE. )
            call udgcla ( 220.0, 250.0, 2.5 )
            call udcntz ( tpall, nxp+1, nxp+1, nzp+1, ibr, nbr3)
            call udgcla ( 250.0, 350.0, 5.0 )
            call udcntz ( tpall, nxp+1, nxp+1, nzp+1, ibr, nbr3)

            else

            call ussttl ( CSGI(131)
     \           //CSGI(135)//'[K]', 'X[km]', '', 'Z[km]' )
            call uestlv ( -20., 0., 603)
            call uetone ( tpdev, nxp+1, nxp+1, nzp+1 )
            call ueitlv
            call usdaxs
            call uxsttl ( 'T', header, 1. )
            call udgcla ( -20., 20., 1. )
            call udcntz ( tpdev, nxp+1, nxp+1, nzp+1, ibr, nbr3)


            end if
*
*
         end if
      end if

      end do

      call grcls

      call FCLOSE

      end
