!=================================================================
!  一次元 CO2 大気放射対流平衡計算プログラム; 
!  DCL による描画(鉛直温度/温位分布)
!
!  2000/09/30  小高正嗣: v3.0 にあわせて書き換え
!=================================================================
      program tprof
!-----------------------------------------------------------------      
      implicit real*8 ( a-h,o-z )
!-----------------------------------------------------------------      
#include "grid_size_M.f"
!-----------------------------------------------------------------      
      parameter ( NZP = NZ )
      parameter ( DAY    = 86400.0D0)
      parameter ( HOUR   = 3600.0D0 ) 
!-----------------------------------------------------------------      
      dimension
     \   TEMP0(-2:NZB) , TPOT0(-2:NZB),
     \   CDTURB(-2:NZB), ZGRID(-2:NZB), QVAL1(-2:NZB),
     \   PRES0(-2:NZB) , DENS0(-2:NZB),
     \   PPAI0(-2:NZB) , QVAP0(-2:NZB), RVAP0(-2:NZB)

      real*4
     \   ZGRID_p(0:NZP),
     \   QVAL1_p(0:NZP) 

      real*4
     \   ZMIN , ZMAX

      real*4 POS

      character*14 dheader
      character*18 pheader
      character*100 model

      character*5 CLAT
      character*5 CLS
      character*2 timel
      character*2 date
      character*2 CSGI

!-----------------------------------------------------------------      

      open(10,file='exparam')
      read(10,*) IRUN
      read(10,*) TIME0, NLOOP1, NLOOP2, DTIME, TIMES
      read(10,*) YLAT, ALS
      read(10,*) model

      call FOPEN ( irun+1 )

      do IZ = -2, NZB
         READ(16,*) ITW, ZGRID(IZ), PRES0(IZ), DENS0(IZ),
     \                   TEMP0(IZ), TPOT0(IZ), PPAI0(IZ), 
     \                   QVAP0(IZ), RVAP0(IZ)
      end do

      do iz = 0, nzp
         ZGRID_p(iz)= real( ZGRID(iz) )/ 1000.0
      end do

      ZMIN    = ZGRID_p(0)
      ZMAX    = ZGRID_p(NZP)

      write(6,*) 'data number = ', nloop1
      write(6,*) 'INPUT DATA BEGINING, END, & INTERVAL' 
      write(6,*) 'itb ite interval ?'
      read (*,*) itb, ite, interv

      CALL sgpwsn
      read(*,*) iws
      CALL gropn(iws)
      CALL sglset( 'LCORNER', .FALSE. )
      CALL sglset( 'LCHAR', .TRUE. )

      CALL grfrm
      CALL grswnd( 160.0, 260.0, ZMIN, ZMAX )   
      CALL grsvpt( 0.2, 0.8, 0.2, 0.8)                     
      CALL grstrn( 1 )
      CALL grstrf
      CALL ussttl( 'Temperature', '[K]', '', 'Z[km]' )
      CALL usdaxs

      POS = 0.8

      do it = 1, ite

         read(32) TIME, ( QVAL1(IZ), 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
               write(date, '(I2.2)') INT( time/DAY ) + 1

c*** 時刻変換: 秒から LT へ************************************
               time = MOD( time, DAY ) + TIMES
               time = time / HOUR

               if ( time .gt. 24 ) then
                  time = time - 24
               end if
c**************************************************************

               write(timel,'(I2.2)') INT(time)
               write(CLS,  '(F5.1)') ALS
               write(CLAT, '(F5.1)') YLAT

               pheader = 'Ls=' // CLS // ' ' // CSGI(172) // '=' // CLAT
               dheader = 'day' // date // ' LT=' // timel // ':00'
    
               do iz = 0, NZP
                  QVAL1_p(iz) = real( QVAL1(iz) )
               end do

c               CALL sgrset( 'FFCT', POS )
c               CALL sgsplc( timel )
               CALL uulinz( NZP+1, QVAL1_p, ZGRID_p, 1, 3  )
               POS = POS + 0.05
            end if
         end if

      end do

!      CALL uxsttl( 'T', dheader, 1. )      
      CALL uxsttl( 'T', pheader, 1. )      
      CALL uxsttl( 'T', model, 1. )      

      CALL grcls                                                        

      end

