
      SUBROUTINE FORMIC (KIND,I,J,IAUX,IC,LENG,B)

*** LOAD UNKNOWN NUMBER VECTOR WITH UNKNOWN NUMBERS

      IMPLICIT DOUBLE PRECISION (A-H,O-Z)
      IMPLICIT INTEGER (I-N)
      DIMENSION IC(*),B(*), LSTA(3)
      DIMENSION LENGS3(24)
      COMMON /STRUCT/ NSTA,NAUX,NUNK,IDIM,NSTAS,NOBS,NCON,NZ,NCD
      DATA LENGS3 / 1,1,1,6,6,6,6,6,6,6,7,6,6,6,2,2,2,4,2,2,1,1,2,2 /

*** KIND = 0 IS AN AUXILIARY PARAMETER CONSTRAINT (I = IAUX)

      IF (KIND.EQ.0) THEN
        IC(1) = IUNAUX(I)
        LENG = 1

*** KIND = 1 KIND = 2 KIND = 3 ARE SIMPLE COORD. CONSTRAINTS (I = ISTA)

      ELSEIF ( KIND.GE.1 .AND. KIND.LE.3     .AND.
     &            I.GE.1 .AND.    I.LE.NSTA ) THEN
        IC(1) = IUNSTA(I,KIND)
        LENG = LENGS3(KIND)

*** KIND = 4 KIND = 5 KIND = 6 ARE DIFFERENTIAL ECF X,Y,Z (GPS)

      ELSEIF ( KIND.GE.4 .AND. KIND.LE.6     .AND.
     &            I.GE.1 .AND.    I.LE.NSTA  .AND.
     &            J.GE.1 .AND.    J.LE.NSTA)  THEN
        LENG = LENGS3(KIND)
        IC(1) = IUNSTA(I,1)
        IC(2) = IUNSTA(I,2)
        IC(3) = IUNSTA(I,3)
        IC(4) = IUNSTA(J,1)
        IC(5) = IUNSTA(J,2)
        IC(6) = IUNSTA(J,3)
        IF (IAUX.GT.0) THEN
          LENG = LENG + 1
          IC(LENG) = IUNAUX(IAUX)
        ENDIF
        IF (NCD.GT.0) THEN
          LSTA(1) = I
          LSTA(2) = J
          CALL ADDCD (IC,LENG,LSTA,2,B,KIND)
        ENDIF

*** KIND = 7 IS MARK TO MARK DISTANCE

      ELSEIF ( KIND.EQ.7 .AND. I.GE.1 .AND. I.LE.NSTA  .AND.
     &                         J.GE.1 .AND. J.LE.NSTA ) THEN
        LENG = LENGS3(KIND)
        IC(1) = IUNSTA(I,1)
        IC(2) = IUNSTA(I,2)
        IC(3) = IUNSTA(I,3)
        IC(4) = IUNSTA(J,1)
        IC(5) = IUNSTA(J,2)
        IC(6) = IUNSTA(J,3)
        IF (IAUX.GT.0) THEN
          LENG = LENG + 1
          IC(LENG) = IUNAUX(IAUX)
        ENDIF
        IF (NCD.GT.0) THEN
          LSTA(1) = I
          LSTA(2) = J
          CALL ADDCD (IC,LENG,LSTA,2,B,KIND)
        ENDIF

***  KIND = 8 IS AN ASTRONOMIC AZIMUTH

      ELSEIF ( KIND.EQ.8 .AND. I.GE.1 .AND. I.LE.NSTA  .AND.
     &                         J.GE.1 .AND. J.LE.NSTA ) THEN
        LENG = LENGS3(KIND)
        IC(1) = IUNSTA(I,1)
        IC(2) = IUNSTA(I,2)
        IC(3) = IUNSTA(I,3)
        IC(4) = IUNSTA(J,1)
        IC(5) = IUNSTA(J,2)
        IC(6) = IUNSTA(J,3)
        IF (NCD.GT.0) THEN
          LSTA(1) = I
          LSTA(2) = J
          CALL ADDCD (IC,LENG,LSTA,2,B,KIND)
        ENDIF

***  KIND = 9 IS A ZENITH DISTANCE

      ELSEIF ( KIND.EQ.9 .AND. I.GE.1 .AND. I.LE.NSTA  .AND.
     &                         J.GE.1 .AND. J.LE.NSTA ) THEN
        LENG = LENGS3(KIND)
        IC(1) = IUNSTA(I,1)
        IC(2) = IUNSTA(I,2)
        IC(3) = IUNSTA(I,3)
        IC(4) = IUNSTA(J,1)
        IC(5) = IUNSTA(J,2)
        IC(6) = IUNSTA(J,3)
        IF (IAUX.GT.0) THEN
          LENG = LENG + 1
          IC(LENG) = IUNAUX(IAUX)
        ENDIF
        IF (NCD.GT.0) THEN
          LSTA(1) = I
          LSTA(2) = J
          CALL ADDCD (IC,LENG,LSTA,2,B,KIND)
        ENDIF

*** KIND = 10 IS HORIZONTAL ANGLE

      ELSEIF ( KIND.EQ.10 .AND. I.GE.1 .AND.    I.LE.NSTA  .AND.
     &                          J.GE.1 .AND.    J.LE.NSTA  .AND.
     &                       IAUX.GE.1 .AND. IAUX.LE.NSTA ) THEN
        K = IAUX
        LENG = 6            
        IC(1) = IUNSTA(I,1)
        IC(2) = IUNSTA(I,2)
        IC(3) = IUNSTA(J,1)
        IC(4) = IUNSTA(J,2)
        IC(5) = IUNSTA(K,1)
        IC(6) = IUNSTA(K,2)
        IF (NCD.GT.0) THEN
          LSTA(1) = I
          LSTA(2) = J
          LSTA(3) = K
          CALL ADDCD (IC,LENG,LSTA,3,B,KIND)
        ENDIF

***  KIND = 11 IS A HORIZONTAL DIRECTION

      ELSEIF ( KIND.EQ.11 .AND. I.GE.1 .AND. I.LE.NSTA  .AND.
     &                          J.GE.1 .AND. J.LE.NSTA ) THEN
        LENG = LENGS3(KIND)
        IC(1) = IUNSTA(I,1)
        IC(2) = IUNSTA(I,2)
        IC(3) = IUNSTA(I,3)
        IC(4) = IUNSTA(J,1)
        IC(5) = IUNSTA(J,2)
        IC(6) = IUNSTA(J,3)
        IC(7) = IUNROT(IAUX)
        IF (NCD.GT.0) THEN
          LSTA(1) = I
          LSTA(2) = J
          CALL ADDCD (IC,LENG,LSTA,2,B,KIND)
        ENDIF

***  KIND = 12 IS A CONSTRAINED AZIMUTH

      ELSEIF ( KIND.EQ.12 .AND. I.GE.1 .AND. I.LE.NSTA  .AND.
     &                          J.GE.1 .AND. J.LE.NSTA ) THEN
        LENG = LENGS3(KIND)
        IC(1) = IUNSTA(I,1)
        IC(2) = IUNSTA(I,2)
        IC(3) = IUNSTA(I,3)
        IC(4) = IUNSTA(J,1)
        IC(5) = IUNSTA(J,2)
        IC(6) = IUNSTA(J,3)

*** KIND = 13 IS A CONSTRAINED MARK TO MARK DISTANCE

      ELSEIF ( KIND.EQ.13 .AND. I.GE.1 .AND. I.LE.NSTA  .AND.
     &                          J.GE.1 .AND. J.LE.NSTA ) THEN
        LENG = LENGS3(KIND)
        IC(1) = IUNSTA(I,1)
        IC(2) = IUNSTA(I,2)
        IC(3) = IUNSTA(I,3)
        IC(4) = IUNSTA(J,1)
        IC(5) = IUNSTA(J,2)
        IC(6) = IUNSTA(J,3)

***  KIND = 14 IS A CONSTRAINED ZENITH DISTANCE

      ELSEIF ( KIND.EQ.14 .AND. I.GE.1 .AND. I.LE.NSTA  .AND.
     &                          J.GE.1 .AND. J.LE.NSTA ) THEN
        LENG = LENGS3(KIND)
        IC(1) = IUNSTA(I,1)
        IC(2) = IUNSTA(I,2)
        IC(3) = IUNSTA(I,3)
        IC(4) = IUNSTA(J,1)
        IC(5) = IUNSTA(J,2)
        IC(6) = IUNSTA(J,3)

*** KIND = 15 KIND = 16 KIND = 17 ARE CONSTRAINED HEIGHT DIFFERENCES

      ELSEIF ( KIND.GE.15 .AND. KIND.LE.17    .AND.
     &            I.GE.1  .AND.    I.LE.NSTA  .AND.
     &            J.GE.1  .AND.    J.LE.NSTA ) THEN
        LENG = 2           
        IC(1) = IUNSTA(I,3)
        IC(2) = IUNSTA(J,3)

***  KIND = 18 IS A CONSTRAINED MINIMAL DISTANCE

      ELSEIF ( KIND.EQ.18 .AND. I.GE.1 .AND. I.LE.NSTA  .AND.
     &                          J.GE.1 .AND. J.LE.NSTA ) THEN
        LENG = LENGS3(KIND)
        IC(1) = IUNSTA(I,1)
        IC(2) = IUNSTA(I,2)
        IC(3) = IUNSTA(J,1)
        IC(4) = IUNSTA(J,2)
      ELSE
        WRITE (6,1) KIND,I,J
    1   FORMAT ('0ERROR - ILLEGAL VALUES IN FORMIC = ',3I10)
        CALL ABORT2
      ENDIF

      RETURN
      END


      SUBROUTINE ADDCD (IC,LENG,LSTA,LCNT,B,KIND)

      IMPLICIT DOUBLE PRECISION (A-H,O-Z)
      IMPLICIT INTEGER (I-N)
      DIMENSION IC(*), LSTA(*), B(*)

      IF (KIND.EQ.10) THEN
        N = 2
      ELSE
        N = 3
      ENDIF

      DO 40 LGRD = 1, LCNT
        CALL GETGLA(GLA, LSTA(LGRD), B)
        CALL GETGLO(GLO, LSTA(LGRD), B)

        CALL GRDPOS(GLO, GLA, IGRID, JGRID)

        DO 30 IGRD = IGRID, (IGRID + 1)
          DO 20 JGRD = JGRID, (JGRID + 1)
            DO 10 IVEC = 1, N
              LENG = LENG + 1   
              IC(LENG) = IUNGRD(IGRD, JGRD, IVEC)
   10       CONTINUE
   20     CONTINUE
   30   CONTINUE
   40 CONTINUE

      RETURN
      END

      
