      SUBROUTINE TOLGH( X, Y, Z, N, E, U, ISN, JSN, B)

*** CONVERT RESIDUALS TO LGH

      IMPLICIT DOUBLE PRECISION (A-H, O-Z)
      DIMENSION B(*)
      DOUBLE PRECISION X, Y, Z, N, E, U
      SAVE GLA1, GLO1, GLA2, GLO2
      SAVE SB, CB, SL, CL
      SAVE ISNX, JSNX
      DATA ISNX /0/, JSNX /0/

      IF (ISN .NE. ISNX  .OR.  JSN .NE. JSNX) THEN
        IF (ISN .NE. ISNX) THEN
          CALL GETGLA (GLA1, ISN, B)
          CALL GETGLO (GLO1, ISN, B)
          ISNX = ISN
        ENDIF
        IF (JSN .NE. JSNX) THEN
          CALL GETGLA (GLA2, JSN, B)
          CALL GETGLO (GLO2, JSN, B)
          JSNX = JSN
        ENDIF

*** COMPUTE MEAN ORIENTATION

        GLA = (GLA1 + GLA2)/2.D0
        GLO = (GLO1 + GLO2)/2.D0

        SB = DSIN(GLA)
        CB = DCOS(GLA)
        SL = DSIN(GLO)
        CL = DCOS(GLO)
      ENDIF

      N = -SB*CL*X - SB*SL*Y + CB*Z
      E = -   SL*X +    CL*Y
      U =  CB*CL*X + CB*SL*Y + SB*Z

      RETURN
      END
