      SUBROUTINE ANGLES(B,LEN,FMOD,DEL,LAM,BAD)
C
C CALCULATE FIELD ANGLES FROM FIELD COMPONENTS AND MODULUS
C
C INPUT ARRAY OF FIELD COMPONENTS
C OUTPUT FIELD MODULUS, LATITUDINAL AND LONGITUDINAL FIELD ANGLES
C 
      REAL*4 B(3,LEN),FMOD(LEN),DEL(LEN),LAM(LEN)
C
      DO I = 1,LEN
C
C CALCULATE FIELD COMPONENT NORM (MODULUS)
C
       IF (B(1,I).NE.BAD.AND.
     &     B(2,I).NE.BAD.AND.
     &     B(3,I).NE.BAD) THEN
        FMOD(I) = SQRT(B(1,I)**2+B(2,I)**2+B(3,I)**2)
       ELSE
        FMOD(I) = BAD
       END IF
C
C CALCULATE LATITUDINAL FIELD ANGLE
C
       IF (FMOD(I).NE.BAD.AND.
     &     FMOD(I).NE.0.0.AND.
     &     B(3,I).NE.BAD) THEN
        DEL(I) = ASIN(B(3,I)/FMOD(I)) * 
     &   57.2957795D0
       ELSE
        DEL(I) = BAD
       END IF
C
C CALCULATE LONGITUDINAL FIELD ANGLE
C
       IF (B(1,I).NE.BAD.AND.
     &     B(2,I).NE.BAD.AND.
     &     B(1,I).NE.0.0) THEN
        LAM(I) = 180.0 - 
     &   ATAN2(B(2,I),-B(1,I)) * 57.2957795D0
       ELSE IF (B(1,I).NE.BAD.AND.
     &          B(2,I).NE.BAD.AND.
     &          B(1,I).EQ.0.0.AND.
     &          B(2,I).NE.0.0) THEN
        LAM(I) = 90.0
       ELSE IF (B(1,I).NE.BAD.AND.
     &          B(2,I).NE.BAD.AND.
     &          B(1,I).NE.0.0.AND.
     &          B(2,I).EQ.0.0) THEN
        LAM(I) = 180.0
       ELSE
        LAM(I) = BAD
       END IF
C
      END DO
C
      RETURN
      END
