      SUBROUTINE YD2YMD(YD,Y,M,D)
C
C     GIVEN A GREGORIAN CALENDAR DATE EXPRESSED IN THE FORM
C     YYYYDDD.DD, THIS ROUTINE RETURNS THE CORRESPONDING
C     YEAR, MONTH, AND DAY (AND FRACTION OF DAY).
C
C     B. KNAPP, 1988-11-14, 2001-04-24 (i*4)
C
C
C     RCS DATA
C     
C     $Header$
C     
C     $Log$
C
C
      IMPLICIT NONE
C
C     INPUT:
      REAL*8 YD
C        (DATE OF THE FORM YYYYDDD.DD)
C
C     OUTPUT:
      INTEGER*4 Y,M
      REAL*8 D
C        (E.G., 1987, 12, 15.5)
C
C     LOCAL:
      REAL*8 JD, YD2
      INTEGER*4 MDAYS(12,2),K
      DATA MDAYS  
     &   /  0, 31, 59, 90,120,151,181,212,243,273,304,334,
     &      0, 31, 60, 91,121,152,182,213,244,274,305,335/
      SAVE MDAYS
C
C     EXTERNAL FUNCTIONS, SUBROUTINES
      EXTERNAL YD2JD, JD2YD
C
C     FIRST CONVERT TO JD AND BACK, TO NORMALIZE TO YEAR AND DAY,
C     WITH DAY IN RANGE 1..NUMBER OF DAYS IN YEAR
      CALL YD2JD( YD, JD )
      CALL JD2YD( JD, YD2 )
C
      Y=INT(YD2/1000.0D0)
      IF (MOD(Y,4).NE.0 .OR. MOD(Y,100).EQ.0 .AND. MOD(Y,400).NE.0) THEN
         K=1
      ELSE
         K=2
      ENDIF
      M=12
      D=MOD(ABS(YD2),1000.0D0)
   10 IF (MDAYS(M,K).LT.D) GOTO 20
         M=M-1
      GOTO 10
   20 D=D-MDAYS(M,K)
      RETURN
      END
