      SUBROUTINE READNRTD(N,TMAX,YR,MO,DA,DOY,HR,U,UF,KMAX,
     &FNMAG,FNSWEPAM,FNKP,FNDST)
      IMPLICIT NONE
      INTEGER N,TMAX,KMAX
      CHARACTER*255 FNMAG,FNSWEPAM,FNKP,FNDST
      INTEGER YR(TMAX),MO(TMAX),DA(TMAX),DOY(TMAX),HR(TMAX)
      REAL*8 U(KMAX,TMAX),UF(KMAX),PI4
      INTEGER KIN,I,J,K,ERRLVL,YR1,MO1,DA1,HR1,NN
C     OFFSETS OF DOY W.R.T. CALENDAR DAYS VS. MONTHS
      CHARACTER*255 FN
      INTEGER FDOM(13)
      DATA FDOM/0,31,59,90,120,151,181,212,243,273,304,334,365/
      KIN=KMAX-4
C     PI*4
      PI4=16._8*DATAN(1._8)
      NN=N
      OPEN(1,FILE=FNMAG,ACTION='READ',STATUS='OLD')
      OPEN(2,FILE=FNSWEPAM,ACTION='READ',STATUS='OLD')
      OPEN(3,FILE=FNKP,ACTION='READ',STATUS='OLD')
      OPEN(4,FILE=FNDST,ACTION='READ',STATUS='OLD')
      READ(1,110)
      READ(2,120)
      N=1
1     READ(1,10,END=3,ERR=100,IOSTAT=ERRLVL)YR(N),MO(N),DA(N),
     &HR(N),(U(K,N),K=4,6),(U(K,N),K=1,3)
      READ(2,20,END=3,ERR=200,IOSTAT=ERRLVL)YR1,MO1,DA1,HR1,
     &U(8,N),U(9,N),U(7,N)
      IF(YR(N).EQ.0)GOTO 3
      IF(HR(N).EQ.0)THEN
      READ(3,30,END=3,ERR=300,IOSTAT=ERRLVL)
     &(U(10,N+3*I),I=0,7),(U(12,N+3*I),I=0,7)
      READ(4,40,END=3,ERR=400,IOSTAT=ERRLVL)(U(11,N+I),I=0,23)
      DO 2 I=0,7
      IF(N+3*I+1.GT.TMAX)THEN
      WRITE(*,*)'MAXIMUM SAMPLE VOLUME REACHED, REMAINING DATA IGNORED'
      WRITE(*,'()')
      GOTO 3
      ENDIF
      U(10,N+3*I+1)=U(10,N+3*I)
      U(12,N+3*I+1)=U(12,N+3*I)
      IF(N+3*I+2.GT.TMAX)THEN
      WRITE(*,*)'MAXIMUM SAMPLE VOLUME REACHED, REMAINING DATA IGNORED'
      WRITE(*,'()')
      GOTO 3
      ENDIF
      U(10,N+3*I+2)=U(10,N+3*I)
2     U(12,N+3*I+2)=U(12,N+3*I)
      ENDIF
      IF(YR(N).NE.YR1)GOTO 900
      IF(MO(N).NE.MO1)GOTO 900
      IF(DA(N).NE.DA1)GOTO 900
      IF(HR(N).NE.HR1)GOTO 900
      IF(N.GT.TMAX)THEN
      WRITE(*,*)'MAXIMUM SAMPLE VOLUME REACHED, REMAINING DATA IGNORED'
      WRITE(*,*)N,TMAX
      WRITE(*,'()')
      GOTO 3
      ENDIF
C     CONVERT MONTH AND DAY TO DOY
      DOY(N)=DA(N)+FDOM(MO(N))
C     CORRECT FOR LEAP YEARS (THIS WILL FAIL IN 2100 AD)
      IF((((YR(N)/4)*4).EQ.YR(N)).AND.(MO(N).GE.3))DOY(N)=DOY(N)+1
C     GENERATE SYNTHETIC INPUTS
C     SEMIDIURNAL VARIATIONS
      U(KIN+1,N)=DSIN(PI4*(HR(N)-2)/24._8)
      U(KIN+2,N)=DCOS(PI4*(HR(N)-2)/24._8)
C     SEMIANNUAL VARIATIONS
      U(KIN+3,N)=DSIN(PI4*(DOY(N)-80)/365.24_8)
      U(KIN+4,N)=DCOS(PI4*(DOY(N)-80)/365.24_8)
C     DO NOT ADD CARRINGTON AND SCHWABE CYCLES HERE - USE R OR F10.7
      N=N+1
      GOTO 1
3     N=N-1
      CLOSE(1)
      CLOSE(2)
      CLOSE(3)
      CLOSE(4)
      DO 5 I=N+1,TMAX
      DO 4 K=1,KIN
4     U(K,I)=UF(K)
      J=0
      HR(I)=HR(I-1)+1
      DA(I)=DA(I-1)
      DOY(I)=DOY(I-1)
      MO(I)=MO(I-1)
      YR(I)=YR(I-1)
      IF(HR(I).GT.23)THEN
      HR(I)=HR(I)-24
      DA(I)=DA(I)+1
      DOY(I)=DOY(I)+1
      ENDIF
      IF((((YR(I)/4)*4).EQ.YR(I)).AND.(DOY(I).GE.60))J=1
      IF(DOY(I).GT.(FDOM(MO(I)+1)+J))THEN
      MO(I)=MO(I)+1
      DA(I)=DOY(I)-FDOM(MO(I))-J
      ENDIF
      IF(MO(I).GT.12)THEN
      YR(I)=YR(I)+1
      MO(I)=MO(I)-12
      DOY(I)=DOY(I)-365-J
      ENDIF
C     SEMIDIURNAL VARIATIONS
      U(KIN+1,N)=DSIN(PI4*(HR(I)-2)/24._8)
      U(KIN+2,N)=DCOS(PI4*(HR(I)-2)/24._8)
C     SEMIANNUAL VARIATIONS
      U(KIN+3,N)=DSIN(PI4*(DOY(I)-80)/365.24_8)
      U(KIN+4,N)=DCOS(PI4*(DOY(I)-80)/365.24_8)
5     CONTINUE
      RETURN
10    FORMAT(I4,2(1X,I2),2X,I2,2X,2(3X,5X),4X,1X,6(2X,F6.1))
20    FORMAT(I4,2(1X,I2),2X,I2,2X,2(3X,5X),4X,1X,2(4X,F7.1),4X,E9.2)
30    FORMAT(12X,8F2.0,3X,8F3.0,F7.2)
40    FORMAT(20X,24F4.0)
110   FORMAT(3(/))
120   FORMAT(3(/))
C     ERROR HANDLING (TO BE REPLACED BY SOMETHING MORE SOPHISTICATED)
100   FN=FNMAG
      GOTO 1000
200   FN=FNSWEPAM
      GOTO 1000
300   FN=FNKP
      GOTO 1000
400   FN=FNDST
      GOTO 1000
900   WRITE(*,*)'DATE/TIME IN MAG AND SWEPAM DATA DO NOT MATCH'
      STOP
      RETURN
1000  IF(ERRLVL.EQ.-1)THEN
      WRITE(*,'(1X,"END OF FILE ENCOUNTERED ",
     &"WHILE READING THE INPUT FILE",/,1X,A255,/,1X,"AT LINE # ",I8)')
     &FN,N
      ELSEIF(ERRLVL.EQ.-2)THEN
      WRITE(*,'(1X,"END OF RECORD ENCOUNTERED ",
     &"WHILE READING THE INPUT FILE",/,1X,A255,/,1X,"AT LINE # ",I8)')
     &FN,N
      ELSEIF(ERRLVL.EQ.0)THEN
      WRITE(*,'(1X,"SOMETHING STRANGE HAPPENED ",
     &"WHILE READING THE INPUT FILE",/,1X,A255,/,1X,"AT LINE # ",I8)')
     &FN,N
      ELSE
      WRITE(*,'(1X,"A RUN-TIME ERROR F",I4," WAS ENCOUNTERED ",
     &"WHILE READING THE INPUT FILE",/,1X,A255,/,1X,"AT LINE # ",I8)')
     &ERRLVL,FN,N
      ENDIF
      STOP
      RETURN
      END