      CHARACTER*1 BEL
      CHARACTER*13 FILEIN, FILEOUT
      CHARACTER*8 LABL(6)
      REAL*8  O(6), A(3,6), AT(6,3), ANG(3), DIFF(6), DSEC(6), F(3), 
     1        K(3), OA(6), Q(6,6), QE(3,3), AQ(3,6), WE(3,3), V(6),
     2        QAT(6,3), WORK(100), PI, QDIAG, RAD, WFACT, SSQA, SSQL
      BEL = CHAR(7)
      PI = 3.141592653589793D00
      RAD = 180.D00/PI
      DATA LABL/'ALPHA(1)','ALPHA(2)','ALPHA(3)','LENG 1-2','LENG 2-3',
     1          'LENG 1-3'/
      TYPE 498, BEL
  498 FORMAT (' ENTER NAME OF INPUT FILE', A1)
      ACCEPT 497, FILEIN
  497 FORMAT (A)
      TYPE 499, BEL
  499 FORMAT (' ENTER NAME OF OUTPUT FILE', A1)
      ACCEPT 497, FILEOUT
      TYPE 496, BEL
  496 FORMAT (' ENTER WEIGHT FACTOR', A1)
      ACCEPT*, WFACT
      OPEN (5, FILE=FILEIN, STATUS='OLD', FORM='FORMATTED')
      OPEN (6, FILE=FILEOUT, STATUS='NEW', FORM='FORMATTED')
C *** READ OBSERVED ANGLES AND LENGTHS
      READ (5, *) (O(I), I=1,6)
C *** PRINT INITIAL DATA
      WRITE (6, 500) (LABL(I), I=1,6)
      WRITE (6, 501) (O(I), I=1,6)
C *** STORE OBSERVED ANGLES IN DECIMAL DEGREES
      DO 50 I=1,3
      ANG(I) = O(I)
   50 CONTINUE
C *** CONVERT OBSERVED ANGLES TO RADIANS
      DO 100 I=1,3
      O(I) = O(I)/RAD
  100 CONTINUE
C *** INITIALIZE [A] MATRIX
      DO 150 I=1,3
      DO 150 J=1,6
      A(I,J) = 0.D00
  150 CONTINUE
C *** COMPUTE [A] MATRIX
      A(1,1) = 1.D00 
      A(1,2) = 1.D00
      A(1,3) = 1.D00
      A(2,1) = O(4)*DCOS(O(1))
      A(2,3) = -O(5)*DCOS(O(3))
      A(2,4) = DSIN(O(1))
      A(2,5) = -DSIN(O(3))
      A(3,1) = -O(4)*DSIN(O(1))
      A(3,3) = -O(5)*DSIN(O(3))
      A(3,4) = DCOS(O(1))
      A(3,5) = DCOS(O(3))
      A(3,6) = -1.D00
C *** COMPUTE MATRIX [AT], THE TRANSPOSE OF [A]
      DO 250 I=1,6
      DO 250 J=1,3
      AT(I,J) = A(J,I)
  250 CONTINUE
C *** COMPUTE THE [F] MATRIX
      F(1) = PI - (ANG(1) + ANG(2) + ANG(3))/RAD
      F(2) = O(5)*DSIN(O(3)) - O(4)*DSIN(O(1))
      F(3) = O(6) - O(4)*DCOS(O(1)) - O(5)*DCOS(O(3))
C *** SET UP [Q] MATRIX
      DO 260 I=1,3
  260 Q(I,I) = 1.D00/WFACT
      DO 270 I=4,6
  270 Q(I,I) = 1.D00
C *** COMPUTE [QE] MATRIX = [A][Q][AT] AND [WE] = INV[QE]
      CALL VMULFF (A, Q, 3, 6, 6, 3, 6, AQ, 3, IER1)
      CALL VMULFF (AQ, AT, 3, 6, 3, 3, 6, QE, 3, IER2)
      CALL LINV1F (QE, 3, 3, WE, 0, WORK, IER3)
C *** COMPUTE [K] MATRIX
      CALL VMULFF (WE, F, 3, 3, 1, 3, 3, K, 3, IER4)
C *** COMPUTE [V] MATRIX OF RESIDUALS
      CALL VMULFF (Q, AT, 6, 6, 3, 6, 6, QAT, 6, IER5)
      CALL VMULFF (QAT, K, 6, 3, 1, 6, 3, V, 6, IER6)
C *** COMPUTE ADJUSTED ANGLES AND LENGTHS
      DO 300 I=1,6
      OA(I) = O(I) + V(I)
  300 CONTINUE
C *** CONVERT ADJUSTED ANGLES TO DECIMAL DEGREES
      DO 360 I=1,3
      OA(I) = OA(I)*RAD
  360 CONTINUE
C *** COMPUTE DIFFERENCES
      DO 370 I=1,3
      DIFF(I) = OA(I) - ANG(I)
      DSEC (I) = DIFF(I)*3600.D00
  370 CONTINUE
      DO 375 I=4,6
  375 DIFF(I) = OA(I) - O(I)
C *** COMPUTE SUMS OF SQUARES
      SSQA = DSEC(1)*DSEC(1) + DSEC(2)*DSEC(2) + DSEC(3)*DSEC(3)
      SSQL = (DIFF(4)*DIFF(4) + DIFF(5)*DIFF(5) + DIFF(6)*DIFF(6))*1.D06
C *** PRINT FINAL VALUES
      WRITE (6, 510)
      WRITE (6, 511) (LABL(I), OA(I), ANG(I), DIFF(I), DSEC(I), I=1,3)
      WRITE (6, 518)
      WRITE (6, 512) (LABL(I), OA(I), O(I), DIFF(I), I=4,6)
      WRITE (6, 513) SSQA, SSQL
      WRITE (6, 514) IER1, IER2, IER3, IER4, IER5, IER6
      CLOSE (5)
      CLOSE (6)
      STOP
  500 FORMAT('1ADJUSTMENT OF TRIANGLE USING TRIANGULATION AND TRILATERAT
     1ION', /, ' (METHOD OF OBSERVATIONS ONLY)', //,
     2       ' INPUT DATA (OBSERVED VALUES):',//, 8(2X, A8))
  501 FORMAT (6F10.5)
  510 FORMAT (/, 6X, 'ADJUSTED ANGLES AND LENGTHS', /,
     1        14X, 'ADJUSTED   OBSERVED   DIFF.      "')
  511 FORMAT (3X, A8, 2F11.5, F9.5, F6.2)
  512 FORMAT (3X, A8, F10.4, F11.4, F9.4)
  513 FORMAT ('0SUM OF SQUARES (ANGLES) = ', F8.2, ' SEC**2', /,
     1        ' SUM OF SQUARES (LENGTHS) = ', F7.2, '  MM**2')
  514 FORMAT ('0ERROR CODES:', 6I5)
  515 FORMAT (1P3E15.5)
  516 FORMAT (6E12.4)
  517 FORMAT (3F15.6)
  518 FORMAT (' ')
      END

