C     KC$GMCK  ******************************************************
C
C     THIS PROGRAM CHECKS GEOM STATION,GPEL,AND GPELX CARDS
C     FOR GROSS ERRORS AND HAS AN OPTION TO CONVERT THIS DATA
C     TO METERS WHICH IS WRITTEN TO A USER SPECIFIED SYSTEMS
C     FILE.
C
C     TO RUN THIS PROGRAM THE USER MUST ENTER THE FOLLOWING IN
C
C     TSM>T.GEMCK XXXXXXXX YYYYYYYY
C
C            WHERE XXXXXXXX = INPUT FILE NAME
C
C                 YYYYYYYY = OUTPUT FILE NAME
C
C     BOTH FILES ARE IN THE USER AREA IN EDITOR FORMAT (STORED UNN)
C
C     ***************************************************************
C
      DIMENSION STA(6),X(5),Y(5),ELEV(9),XM(5),YM(5),ELEVM(9)
      DIMENSION CARD(80)
      INTEGER STA, SPACE
      INTEGER*8 FLAG,GPELX,STATION,UNIT,METERS,FEET
      DATA GPELX/5HGPELX/,STATION/7HSTATION/,STA/6*0/
      DATA METERS/6HMETERS/,FEET/4HFEET/,GPEL/4HGPEL/
      FTOM(A) = A / 3.280839
C
C*****READ IN STATION SPACING AND UNITS FLAG
C
      IFL=0
      STA=0
      X=0.0
      Y=0.0
      JFL=0
      READ(5,1,ERR=988) SPACE,UNIT
1     FORMAT(I3,1X,A6)
      XYALLOW=.1*SPACE
      ELALLOW = .1
      WRITE(6,2) SPACE
2     FORMAT(1X,'STATION SPACING = ',I3,' FEET',//,
     /1X,'ERROR ALLOWANCES:',/,
     /1X,'            10% OF STATION SPACING ON XY COORDINATES',/,
     /1X,'            10% GRADE ON ELEVATIONS      ',/,
     /1X,'IMPORTANT!  THIS PROGRAM ONLY CHECKS FOR GROSS ERRORS',/,
     /'             AND IS NOT A SUBSTITUTE FOR CAREFUL CHECKING',/,
     /1X,'            BY COMPETENT WORK-STUDY STUDENTS',//)
C
C*****READ DATA FLAG
C
3     READ(5,4,END=900,ERR=977) FLAG
      WRITE(6,4) FLAG
4     FORMAT(2X,A8)
      IF (UNIT.EQ.FEET) GO TO 5
      WRITE(6,499)
499   FORMAT(1X,'THE DATA HAS BEEN CONVERTED TO METERS AND ',
     /'WRITTEN TO',/,' YOUR SPECIFIED SYSTEM FILE. PLEASE CHECK ',
     /'TO SEE THAT THE',/,' FILE IS CORRECT AND EDIT ANY STRAY ',
     /'ZEROS AT THE END',/,' OF THE DATA SET.')
      ENCODE(80,400,CARD) FLAG
400   FORMAT(2X,A8,70X)
C     CALL STATUS(8,ISTAT,N)
C     IF (ISTAT.EQ.1) GO TO 910
      CALL BUFFEROUT(8,0,CARD,80)
410   CALL STATUS(8,ISTAT,N)
      IF (ISTAT.EQ.4) GO TO 920
      IF (ISTAT.NE.2) GO TO 410
5     IF (FLAG.EQ.STATION) GO TO 100
      IF (FLAG.EQ.GPELX) GO TO 200
      IF (FLAG.EQ.GPEL) GO TO 300
      WRITE(6,6)
6     FORMAT(1X,'INCORRECT FLAG: MUST BE 2X,STATION,GPELX OR GPEL')
      GO TO 999
C
C     STATION CHECK
C
C***** STAION CARD CHECK LOOP
C
100   READ(5,101,ERR=966) (STA(I),X(I),Y(I),I=2,5)
101   FORMAT(4(I4,2F6.0,1X))
      WRITE(6,102) (STA(I),X(I),Y(I),I=2,5)
102   FORMAT(4X,4(2X,I4,2(',',F10.0)))
      DO 1002 I=1,4
      IF (IFL.EQ.0.AND.I.EQ.1) GO TO 1002
      IF (STA(I+1).EQ.0) JFL=1
      IF (JFL.EQ.1) GO TO 104
      HYP=SQRT((X(I+1)-X(I))**2+(Y(I+1)-Y(I))**2)
      OFF=ABS(HYP/(STA(I+1)-STA(I))-SPACE)
      IF (OFF.LT.XYALLOW) GO TO 1002
      WRITE (6,1001) HYP,STA(I),STA(I+1)
 1001 FORMAT (1X,'DISTANCE = ',F10.3,' BETWEEN STATIONS ',I4,'-',I4)
      WRITE(6,103) SPACE
103   FORMAT('+',51X,'CHECK: SPACING NOT NEAR ',I3,' FEET')
 1002 CONTINUE
104   IF (UNIT.EQ.FEET) GO TO 109
C
C***** STATION CARD: METERS CONVERSION
C
      XM=0.0
      YM=0.0
      DO 105  I=2,5
      IF (STA(I).EQ.0) GO TO 105
      XM(I)=FTOM(X(I))
      YM(I)=FTOM(Y(I))
  105 CONTINUE
      WRITE(6,108) (STA(I),XM(I),YM(I),I=2,5)
108   FORMAT(1X,'M. ',4(2X,I4,2(',',F10.0)))
      ENCODE(80,401,CARD) (STA(I),XM(I),YM(I),I=2,3)
401   FORMAT(4X,2(2X,I4,2(',',F10.0)),20X)
      CALL BUFFEROUT(8,0,CARD,80)
420   CALL STATUS(8,ISTAT,N)
      IF (ISTAT.EQ.4) GO TO 930
      IF (ISTAT.NE.2) GO TO 420
      ENCODE(80,401,CARD) (STA(I),XM(I),YM(I),I=4,5)
      CALL BUFFEROUT(8,0,CARD,80)
430   CALL STATUS(8,ISTAT,N)
      IF (ISTAT.EQ.4) GO TO 930
      IF (ISTAT.NE.2) GO TO 430
C
C     RESET FOR NEXT CARD
C
  109 IF (JFL.EQ.1) GO TO 3
      X(1)=X(5)
      Y(1)=Y(5)
      STA(1)=STA(5)
      IFL=1
      GO TO 100
C
C     GPELX CHECK
C
C***** GPELX CARD CHECK LOOP
C
  200 STA=0
      ELEV=0.0
      IFL=0
      JFL=0
2000  READ(5,201,ERR=955) (STA(I),ELEV(I), I=2,6)
201   FORMAT(5(I4,F6.1,2X))
      WRITE(6,202) (STA(I),ELEV(I), I=2,6)
202   FORMAT(3X,5(2X,I4,' + ',F7.1))
      DO 220  I=1,5
      IF (IFL.EQ.0.AND.I.EQ.1) GO TO 220
      IF (STA(I+1).EQ.0) JFL=1
      IF (JFL.EQ.1) GO TO 2500
      J=I+1
      GRADE = ABS((ELEV(J)-ELEV(I))/(SPACE*(STA(J)-STA(I))))
      IF (GRADE.LT.ELALLOW) GO TO 220
      WRITE(6,204) STA(I),STA(J)
204   FORMAT(21X,'GRADE > 10% BETWEEN STATIONS ',I4,'-',I4)
220   CONTINUE
 2500 IF (UNIT.EQ.FEET) GO TO 225
C
C***** GPELX CARD METERS CONVERSION
C
      DO 221  I=2,6
221   ELEVM(I) = FTOM(ELEV(I))
      WRITE(6,222) (STA(I),ELEVM(I), I=2,6)
222   FORMAT(1X,'M.',5(2X,I4,' + ',F7.1))
      ENCODE(80,402,CARD) (STA(I),ELEVM(I), I=2,6)
402   FORMAT(1X,5(1X,I4,' + ',F6.1),9X)
      CALL BUFFEROUT(8,0,CARD,80)
440   CALL STATUS(8,ISTAT,N)
      IF (ISTAT.EQ.4) GO TO 940
      IF (ISTAT.NE.2) GO TO 440
C
C     RESET FOR NEXT CARD
C
  225 IF (JFL.EQ.1) GO TO 900
      ELEV(1)=ELEV(6)
      STA(1)=STA(6)
      IFL=1
      GO TO 2000
C
C     GPEL CHECK
C
C*****FIRST GPEL CARD CHECK LOOP
300   READ(5,301,ERR=955) ( ELEV(I), I=1,8)
301   FORMAT(4X,8(1X,F7.1))
      WRITE(6,301) ( ELEV(I), I=1,8)
      ELEV(9)=ELEV(8)
      DO 320  I=1,7
      J=I+1
      GRADE = (ELEV(J)-ELEV(I))/SPACE
      IF (GRADE.LT.ELALLOW) GO TO 319
      IF (J.EQ.8) GO TO 309
      IF (J.EQ.7) GO TO 311
      IF (J.EQ.6) GO TO 313
      IF (J.EQ.5) GO TO 315
      IF (J-3) 303,305,307
303   WRITE(6,304)
304   FORMAT(13X,'GRD>10%')
      GO TO 319
305   WRITE(6,306)
306   FORMAT(21X,'GRD>10%')
      GO TO 319
307   WRITE(6,308)
308   FORMAT(29X,'GRD>10%')
      GO TO 319
309   WRITE(6,310)
310   FORMAT(61X,'GRD>10%')
      GO TO 319
311   WRITE(6,312)
312   FORMAT(53X,'GRD>10%')
      GO TO 319
313   WRITE(6,314)
314   FORMAT(45X,'GRD>10%')
      GO TO 319
315   WRITE(6,316)
316   FORMAT(37X,'GRD>10%')
319   CONTINUE
320   CONTINUE
      IF (UNIT.EQ.FEET) GO TO 325
C*****FIRST GPEL CARD METERS CONVERSION
      DO 321  I=1,8
321   ELEVM(I) = FTOM(ELEV(I))
      WRITE(6,322) ( ELEVM(I), I=1,8)
322   FORMAT(1X,'M. ',8(1X,F7.1))
      ENCODE(80,502,CARD) ( ELEVM(I), I=1,8)
502   FORMAT(4X,8(1X,F7.1),12X)
      CALL BUFFEROUT(8,0,CARD,80)
540   CALL STATUS(8,ISTAT,N)
      IF (ISTAT.EQ.4) GO TO 940
      IF (ISTAT.NE.2) GO TO 540
C*****MAIN GPEL CARDS CHECK LOOP
325   STA(1) = STA(9)
      ELEV(1) = ELEV(9)
      DO 326  I=2,9
      STA(I) = 0
326   ELEV(I) = 0.0
      READ(5,301,END=900,ERR=955) ( ELEV(I), I=2,9)
      WRITE(6,301) ( ELEV(I), I=2,9)
      DO 350  I=1,8
      J=I+1
      IF (STA(J).EQ.0) GO TO 360
      GRADE = (ELEV(J)-ELEV(I))/SPACE
      IF (GRADE.LT.ELALLOW) GO TO 340
      IF (J.EQ.9) GO TO 331
      IF (J.EQ.8) GO TO 332
      IF (J.EQ.7) GO TO 333
      IF (J.EQ.6) GO TO 334
      IF (J.EQ.5) GO TO 335
      IF (J-3) 336,338,339
331   WRITE(6,310)
      GO TO 340
332   WRITE(6,312)
      GO TO 340
333   WRITE(6,314)
      GO TO 340
334   WRITE(6,316)
      GO TO 340
335   WRITE(6,308)
      GO TO 340
336   WRITE(6,337)
337   FORMAT(5X,'GRD>10%')
      GO TO 340
338   WRITE(6,304)
      GO TO 340
339   WRITE(6,306)
340   CONTINUE
350   CONTINUE
      IF (UNIT.EQ.FEET) GO TO 325
C*****MAIN GPEL CARDS METERS CONVERSION
      DO 351  I=2,9
351   ELEVM(I) = FTOM(ELEV(I))
      WRITE(6,322) (ELEVM(I), I=2,9)
      ENCODE(80,402,CARD) (ELEVM(I), I=2,9)
      CALL BUFFEROUT(8,0,CARD,80)
460   CALL STATUS(8,ISTAT,N)
      IF (ISTAT.EQ.4) GO TO 940
      IF (ISTAT.NE.2) GO TO 460
      GO TO 325
C*****LAST GPEL CARD METERS CONVERSION
360   IF (UNIT.EQ.FEET) GO TO 900
      DO 361  I=2,9
361   ELEVM(I) = FTOM(ELEV(I))
      WRITE(6,322) (ELEVM(I), I=2,9)
      ENCODE(80,402,CARD) (ELEVM(I), I=2,9)
      CALL BUFFEROUT(8,0,CARD,80)
461   CALL STATUS(8,ISTAT,N)
      IF (ISTAT.EQ.4) GO TO 940
      IF (ISTAT.NE.2) GO TO 461
900   STOP
C*****ERROR STATMENTS
910   WRITE(6,915)
915   FORMAT(' BOMB:  METERS FILE NOT READY FOR INPUT')
      GO TO 999
920   WRITE(6,925)
925   FORMAT(' BOMB DURING BUFFEROUT OF FLAG')
      GO TO 999
930   WRITE(6,935)
935   FORMAT(' BOMB DURING BUFFEROUT OF X,Y COORD.')
      GO TO 999
940   WRITE(6,945)
945   FORMAT(' BOMB DURING BUFFEROUT OF ELEV')
      GO TO 999
955   WRITE(6,956)
956   FORMAT(1X,'READ ERROR: STA,ELEVATION')
      GO TO 999
966   WRITE(6,967)
967   FORMAT(1X,'READ ERROR: STA,X,Y')
      GO TO 999
977   WRITE(6,978)
978   FORMAT(1X,'READ ERROR: FLAG')
      GO TO 999
988   WRITE(6,989)
989   FORMAT(1X,'INCORRECT STATION SPACING SPECIFIED')
999   STOP ERROR
      END
