      SUBROUTINE DISTZ2(INAM,IND,NSTAT)
      INTEGER*2 INAM(90,4),IND(90,5),ITITL(10),IST(90,4)
      INTEGER*2 INDEX(70),ISTA(2)
      DIMENSION DEL(90),AZI(90),D(90,5),AZ(90,5),del1(90)
      DIMENSION STLAT(90),STLONG(90),XLAT(90),XLON(90)
      DO 20 K=1,NSTAT
      REWIND 12
      READ(12,1000)ITITL,EPLAT,EPLONG
c      WRITE(6,1000)ITITL,EPLAT,EPLONG
1000      FORMAT(1X,10A2,22X,2G15.7)
10     READ(12,500,END=900)ISTA,STLAT(K),STLONG(K),GC
C     WRITE(1,500)ISTA,STLAT(K),STLONG(K),GC
500    FORMAT(1X,2A2,2X,14X,F10.5,3X,F10.5,30X,F8.2)
c      WRITE(6,550)ISTA,INAM(K,1),INAM(K,2)
550    FORMAT(1X,2A2,1x,2a2)
      IF(ISTA(1).EQ.INAM(K,1).AND.ISTA(2).EQ.INAM(K,2))GO TO 19
      READ(12,1020)GC
1020    FORMAT(70X,F10.0)
c      WRITE(6,1021)GC
1021     FORMAT(' GC',F10.0)
      GO TO 10
19      READ(12,600)(D(K,J),AZ(K,J),J=1,5)
      WRITE(6,600)(D(K,J),AZ(K,J),J=1,5)
20       CONTINUE
      GO TO 910
600      FORMAT(1X,5(F12.2,F10.2))
      WRITE(6,1005)INAM(K,1),INAM(K,2)
900    CONTINUE
       print*,'stopping in distzs end of data in DIST file'
1005    FORMAT(' STATION',2X,2A2,' NOT FOUND')
      WRITE(6,1005)INAM(K,1),INAM(K,2)
        STOP
910   L=0
      DO 30 K=1,NSTAT
      DO 30 J=1,4
	print*,'k j ind', k, j , ind(k,j)
      IF(IND(K,J).EQ.0)GO TO 30
31      L=L+1
      IST(L,1)=INAM(K,1)
      IST(L,2)=INAM(K,2)
      IST(L,3)=INAM(K,3)
      IST(L,4)=INAM(K,4)
	del1(l)=d(k,1)
      WRITE(6,130)IST(L,1),IST(L,2),INAM(K,1),INAM(K,2)
130      FORMAT(2X,2A2,2X,2A2)
      INDEX(L)=IND(K,J)
      L1=INDEX(L)
      XLAT(L)=STLAT(K)
      XLON(L)=STLONG(K)
      IF(L1.GT.5)GO TO 44
      DEL(L)=D(K,L1)
      AZI(L)=AZ(K,L1)
      GO TO 45
44     CONTINUE
      IF((L1/2)*2.LT.L1)GO TO 47
46    CONTINUE
      DEL(L)=D(K,2)+GC*((L1/2)-1)
      AZI(L)=AZI(2)
      GO TO 45
47     CONTINUE
      DEL(L)=D(K,1)+GC*((L1/2))
      AZI(L)=AZI(1)
45      CONTINUE
      WRITE(6,101)L,XLAT(L),XLON(L),DEL(L),AZI(L),INDEX(L),del1(l)
101     FORMAT(I5,4F8.2,I5,f8.2)
30      CONTINUE
      NSTAT=L
      CALL SORT(IST,DEL,AZI,XLAT,XLON,INDEX,NSTAT,del1)
      WRITE(6,1000)ITITL
      WRITE(11,1001)ITITL,EPLAT,EPLONG
      WRITE(9,1001)ITITL,EPLAT,EPLONG
1001     FORMAT(10A2,2F8.2)
C     WRITE(1,1500)IST(1,1),IST(1,2)
      WRITE(11,1500)(IST(K,1),IST(K,2),IST(K,3),IST(K,4),XLAT(K),
     *XLON(K),DEL(K),AZI(K),INDEX(K),del1(k),K=1,NSTAT)
1500      FORMAT(2A2,2A1,4F10.2,1X,I2,f8.1)
      RETURN
      END
      SUBROUTINE SORT(IST,DEL,AZI,XLAT,XLON,INDEX,NSTAT,del1)
      DIMENSION DEL(1),AZI(1),del1(1),dd(90),
     1D(90),AZ(90),XLAT(1),XLON(1),SLAT(90),SLON(90)
      INTEGER*2 IST(90,4),INDEX(1),INAM(90,4),IND(90)
      IF(AZI(1).LT.0.)AZI(1)=AZI(1)+360.
      AZ1=AZI(1)
      AZ(1)=AZ1
      D(1)=DEL(1)
	dd(1)=del1(1)
      IND(1)=INDEX(1)
      INAM(1,1)=IST(1,1)
      INAM(1,2)=IST(1,2)
      INAM(1,3)=IST(1,3)
      INAM(1,4)=IST(1,4)
      SLAT(1)=XLAT(1)
      SLON(1)=XLON(1)
      NDAT=1
      DO 80 I=1,NSTAT
      IF(I.EQ.1)GO TO 80
81      IF(AZI(I).LT.0.)AZI(I)=AZI(I)+360.
      IF(AZI(I).LT.AZ1)GO TO 70
      NDAT=NDAT+1
      INAM(NDAT,1)=IST(I,1)
      INAM(NDAT,2)=IST(I,2)
      INAM(NDAT,3)=IST(I,3)
      INAM(NDAT,4)=IST(I,4)
C      WRITE(1,150)NDAT,I,INAM(NDAT,1),INAM(NDAT,2)
150      FORMAT(' NDAT I INAM',2I5,2X,2A2)
      D(NDAT)=DEL(I)
      AZ(NDAT)=AZI(I)
      SLAT(NDAT)=XLAT(I)
      SLON(NDAT)=XLON(I)
      IND(NDAT)=INDEX(I)
	dd(ndat)=del1(i)
      AZ1=AZI(I)
      GO TO 80
70         DO 75 K=1,NDAT
      IF(AZI(I).GT.AZ(K))GO TO 75
71         DO 76 L=K,NDAT
      L1=NDAT+K-L
      L2=L1+1
      INAM(L2,1)=INAM(L1,1)
      INAM(L2,2)=INAM(L1,2)
      INAM(L2,3)=INAM(L1,3)
      INAM(L2,4)=INAM(L1,4)
      D(L2)=D(L1)
      AZ(L2)=AZ(L1)
      IND(L2)=IND(L1)
      SLAT(L2)=SLAT(L1)
      SLON(L2)=SLON(L1)
	dd(l2)=dd(l1)
76      CONTINUE
      INAM(K,1)=IST(I,1)
      INAM(K,2)=IST(I,2)
      INAM(K,3)=IST(I,3)
      INAM(K,4)=IST(I,4)
      D(K)=DEL(I)
	dd(k)=del1(i)
      AZ(K)=AZI(I)
      IND(K)=INDEX(I)
      SLAT(K)=XLAT(I)
      SLON(K)=XLON(I)
      NDAT=NDAT+1
      GO TO 80
75      CONTINUE
80      CONTINUE
      DO 60 I=1,NSTAT
      IST(I,1)=INAM(I,1)
      IST(I,2)=INAM(I,2)
      IST(I,3)=INAM(I,3)
      IST(I,4)=INAM(I,4)
C     WRITE(1,103)IST(I,1),IST(I,2)
103      FORMAT(2X,2A2)
      DEL(I)=D(I)
	del1(i)=dd(i)
      AZI(I)=AZ(I)
      INDEX(I)=IND(I)
      XLAT(I)=SLAT(I)
      XLON(I)=SLON(I)
60      CONTINUE
      RETURN
      END
