C****************************************************************
C********       Sample program of using pgplot       ************
C********                 by MORIWAKI                ************
C********                        1998.7              ************
C****************************************************************
        IMPLICIT REAL(A-H,O-Z)
C
        COMMON/FLAG/ IFLAG1
C------------------------------------------------------------
C  IFLAG1 =1 : OUTPOT PS FILES
C  IFLAG1 =0 : DISPLAY XWINDOW
        IFLAG1 = 0
C
      CALL OPEN_FILE
C
      CALL DRIVER
C
      CALL CLOSE_FILE
C
      STOP
      END
C-------------------------------------------------------------
C
C***************************************************
      SUBROUTINE OPEN_FILE
C***************************************************
        IMPLICIT REAL(A-H,O-Z)
C----------------------------------------------------------
C-- Open downloaded file.
        OPEN(10,FILE='../data/Rad-010507.PRN'
     1                      ,STATUS='OLD',FORM='FORMATTED')
      RETURN
      END
C***************************************************
      SUBROUTINE CLOSE_FILE
C***************************************************
        IMPLICIT REAL(A-H,O-Z)
C----------------------------------------------------------
C-- Close files.
       CLOSE(10)
       CLOSE(15)
      RETURN
      END
C***************************************************
      SUBROUTINE DRIVER
C***************************************************
        IMPLICIT REAL(A-H,O-Z)
        PARAMETER(MMAX=24)
        COMMON/AVE/NT(MMAX)
     1            ,RSDN(MMAX),RSUP(MMAX),RLDN(MMAX),RLUP(MMAX)
C
        CHARACTER*2 YEAR
        CHARACTER*3 DATE
        CHARACTER*2 TIME
C----------------------------------------------------------
      YEAR='01'
      DATE='123'
      TIME='06'
C
C
       DO 50 M=1,MMAX
c
c-- Read Rad data
      READ(10,*,END=80) NID,NDATE,NTIME,RSD,RSU,RLD,RLU
C
C-- Input data to array variables of each time set.
      NT(M)  = NTIME
      RSDN(M) = RSD
      RSUP(M) = RSU
      RLDN(M) = RLD
      RLUP(M) = RLU
C
  50   CONTINUE
C
C -- Call visualization of diurnal variation. 
       CALL HOUR_GRAPH(DATE,YEAR)
c
c
C
C 
  80   CONTINUE
      WRITE(*,*) 'File is ended.'
       CLOSE(10)
      RETURN
      END
C***************************************************
      SUBROUTINE HOUR_GRAPH(DATE,YEAR)
C***************************************************
        IMPLICIT REAL(A-H,O-Z)
        PARAMETER(MMAX=24)
        COMMON/AVE/NT(MMAX)
     1            ,RSDN(MMAX),RSUP(MMAX),RLDN(MMAX),RLUP(MMAX)
        DIMENSION X(24)
        INTEGER  pgopen
        CHARACTER*2 YEAR
        CHARACTER*3 DATE
        COMMON/FLAG/ IFLAG1
C------------------------------------------------
C Compute numbers to be plotted.
        DO 210 L=1,24
          X(L) = L-1
  210     CONTINUE
C
C    ! Open device & check
      IF (IFLAG1.EQ.0) CALL pgopen('/XWINDOW')
      IF (IFLAG1.EQ.1) CALL pgopen
     1('../data/RADAVE-'//YEAR//'-'//DATE//'.ps/PS')
C
C    ! Set font size
      CALL PGSCH(1.5)
C!   ! Partition Prot Device
      CALL PGSUBP( 2, 2)
C
C    ! Define plot region 1
      HXMAX=FLOAT(24)
      VXMAX=RSDN(1)
      VXMIN=RSDN(1)
      DO I=1,24
       VXMAX=MAX(VXMAX,RSDN(I))
       VXMIN=MIN(VXMIN,RSDN(I))
      ENDDO
      VXMAX=ANINT(VXMAX)+50.0
      VXMIN=ANINT(VXMIN)-50.0
      CALL pgenv(0.0, HXMAX, VXMIN, 1000.0, 0, -2)
      CALL pgbox('ABCNTS', 6.0, 6, 'ABCNTS', 0.0, 0)
C      CALL pgenv(0.0, HXMAX, VXMIN, 1000.0, 0, 0)
C      CALL pgenv(0.0, HXMAX, VXMIN, VXMAX, 0, 0)
C    ! Plot label & title
      CALL pglab("Time (hour)", " (W/m2)", DATE)
C    ! Plot symbol
      CALL pgline(24, X, RSDN)
      CALL pgpt(24, X, RSDN, 4)
      CALL pgline(24, X, RSUP)
      CALL pgpt(24, X, RSUP, 18)
      CALL pgline(24, X, RLDN)
      CALL pgpt(24, X, RLDN, 7)
      CALL pgline(24, X, RLUP)
      CALL pgpt(24, X, RLUP, -3)
C
C
C    ! Close device
      CALL pgclos
C
C
C
      RETURN
      END
C
