ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c    SDP data    by Pek
c                 Modified by MORIWAKI
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
               IMPLICIT REAL*4 (A-H,O-Z)
          PARAMETER(NMAX=57305,MMAX=150,IY60=30)
C  NMAX: Number of the SDP observation site times 365. 
C  MMAX: Number of the observation site correspond to METPV. 
C  IY60: Number of the averaging years. 
          DIMENSION  KNUB(NMAX,30),KN(NMAX,30)
     &              ,KYEAR(NMAX,30),KMON(NMAX,30)
     &              ,KDN(NMAX,30),KDAY(NMAX,30)
     &              ,KDAT(NMAX,30,8),FFX(MMAX,365,IY60,8)
     &              ,INUB(MMAX),XLAT(MMAX),XLON(MMAX),IZX(MMAX)
     &              ,IKAZU(30),ISTA(MMAX),BAVER(NMAX,8)
     &              ,KDAT1(MMAX,365,IY60,8)
     &              ,DATSUM(MMAX,365,8)
     &              ,DATAVE(MMAX,365,8)
C
          CHARACTER FILEN*11,IYER(30)*4
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c Open site information file.
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
          OPEN(20,FILE='LATLONG',STATUS='OLD',FORM='FORMATTED')
          DO M=1,MMAX
             READ(20,80) ISTA(M),INUB(M),XLAT(M),XLON(M),IZX(M)
          ENDDO
          CLOSE(20,STATUS='KEEP')
c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c Open information file about number of data each year.
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      OPEN(UNIT=10,FILE='IYER,NMAX.txt',STATUS='OLD',FORM='FORMATTED')
           DO I=1,IY60
             READ(10,100)IYER(I),IKAZU(I)
             WRITE(*,*) IYER(I),IKAZU(I)
          ENDDO
 100      FORMAT(A4,I6)
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
       WRITE(*,*) 'Reading SDP data'
C  INPUT INITIAL VALUE
C
           DO 500    IY = 1,IY60
           DO 500     J = 1,8
           DO 500     M = 1,MMAX
           DO 500 IDATE = 1,365
C
           KDAT1(M,IDATE,IY,J) = -999.
  500     CONTINUE
C
C*******************
C   Read Data
C     J indicates the time stamp of data.
c
           DO IY=1,IY60
              FILEN='SDP'//IYER(IY)//'.TXT'
              WRITE(*,*) FILEN
            OPEN(200+IY,FILE=FILEN,STATUS='OLD')
            DO N=1,IKAZU(IY)
c
ccccccccccccccccccccccccccccccccccccccccccccccccc
c  N : number throught the date and site
c  IY: year
c  J : time stamp
c  KDAT : Humidity
ccccccccccccccccccccccccccccccccccccccccccccccccc
c
              READ(200+IY,52) KNUB(N,IY),KN(N,IY),KYEAR(N,IY)
     &         ,KMON(N,IY), KDAY(N,IY),KDN(N,IY),(KDAT(N,IY,J),J=1,8)
              IDAY=0
              DO M=1,MMAX
                IF((KNUB(N,IY).EQ.ISTA(M)).AND.(KDN(N,IY).NE.29)) THEN
                  if(KMON(N,IY).eq.1)  IDATE = KDAY(N,IY)
                  if(KMON(N,IY).eq.2)  IDATE = KDAY(N,IY) + 31
                  if(KMON(N,IY).eq.3)  IDATE = KDAY(N,IY) + 59
                  if(KMON(N,IY).eq.4)  IDATE = KDAY(N,IY) + 90
                  if(KMON(N,IY).eq.5)  IDATE = KDAY(N,IY) +120
                  if(KMON(N,IY).eq.6)  IDATE = KDAY(N,IY) +151
                  if(KMON(N,IY).eq.7)  IDATE = KDAY(N,IY) +181
                  if(KMON(N,IY).eq.8)  IDATE = KDAY(N,IY) +212
                  if(KMON(N,IY).eq.9)  IDATE = KDAY(N,IY) +243
                  if(KMON(N,IY).eq.10) IDATE = KDAY(N,IY) +273
                  if(KMON(N,IY).eq.11) IDATE = KDAY(N,IY) +304
                  if(KMON(N,IY).eq.12) IDATE = KDAY(N,IY) +334
                  IDAY=IDAY+1
C
C**** transform N to M and IDATE
c  N : number throught the date and site
c  M : number of site
c  IDATE : number of date
C
                  DO J=1,8
                   KDAT1(M,IDATE,IY,J) = KDAT(N,IY,J)
                  ENDDO
C****
                ENDIF
              ENDDO
            ENDDO
            CLOSE(200+IY)
          ENDDO
 51          FORMAT(I3,I1,10x,I4,3I2,24(21x,I4,31x),152x)
 52          FORMAT(I3,I1,10x,I4,3I2,8(21x,I4,18x),152x)
 60          FORMAT(2F12.3)
 70          FORMAT(I7,2F12.3,F13.1)
 80          FORMAT(I3,A5,2F10.4,I5)
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
C
        OPEN(2000,FILE='TESTRUN7',STATUS='NEW',FORM='FORMATTED')
c       write(*,*) 'OK1'
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
C  INITIAL VALUE
C
C
           DO 510     J = 1,8
           DO 510     M = 1,MMAX
           DO 510 IDATE = 1,365
C
           DATSUM(M,IDATE,J) = 0.
           DATAVE(M,IDATE,J) = 0.
  510     CONTINUE
C
C*******************
       WRITE(*,*) 'Averaging data'
C
C
       DO 520     J = 1,8
       DO 520     M = 1,MMAX
       DO 520 IDATE = 1,365
         IAVE = 0 
         DO 525 IY=1,IY60
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c If the 1st order of data is not 8, the data is errornormal. c
c When the data is error, input -999.                         c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
          IRMK=INT(MOD(KDAT1(M,IDATE,IY,J),10))
          IF ((IRMK.NE.8).OR.(KDAT1(M,IDATE,IY,J).EQ.-999)) GOTO 700
          FFX(M,IDATE,IY,J)=FLOAT(KDAT1(M,IDATE,IY,J)-IRMK)/10.
          DATSUM(M,IDATE,J)  = DATSUM(M,IDATE,J) + FFX(M,IDATE,IY,J)
          IAVE = IAVE + 1
 700      CONTINUE
 525    CONTINUE
C
        IF(IAVE.EQ.0) THEN
          DATAVE(M,IDATE,J) = -999
          GOTO 800
        ENDIF
C
        DATAVE(M,IDATE,J) = DATSUM(M,IDATE,J)/IAVE
 800    CONTINUE  
C
 520  CONTINUE
C
C*******************************************
C  DATA OUTPUT
       WRITE(*,*) 'Writing data'
c
c
           DO 530     M = 1,MMAX
           DO 530 IDATE = 1,365
c
              WRITE(2000,777) (DATAVE(M,IDATE,J),J=1,8)
 530      CONTINUE 
 777      FORMAT(8F10.3)
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
          STOP
          END

