CTITL  PTOUT.FTN - LOCATE POINTS INSIDE OR OUTSIDE AN AREA
C USED TO SEARCH A DATA FILE (LU 4) FOR POINTS INSIDE AND OUTSIDE
C A COORDINATED AREA
C CONTROL INPUT LU  5
C COORDINATE LIST  LU 6
C STATIONS EITHER INSIDE OR OUTSIDE AS REQUESTED ON LU  8
C REQUIRES SUBROUTINE FSTJDN
      DIMENSION X(100),Y(100),YINTVL(100),RSLOPE(100)
      CHARACTER*600 TEXT
C USE AS A BUFFER
      CHARACTER*144 A
C ******************CHANGE HERE TO RECORD LENGTH OF FILE
      LOGICAL INOUT
      INTEGER*2 AOK,IN,OUT,SWATH(100,25)
      INTEGER*4 EAST,NORTH
      LOGICAL WITHIN,IER
      DATA IN/1HI/,OUT/1HO/
C USED TO SET INSIDE OR OUTSIDE
C     OPEN(UNIT=5,FILE='CON:')
C BY DEFAULT LU 5,6 ARE CONSOLE
C OPEN THE CONSOLE AS LU 5
      OPEN(UNIT=4,FILE='/mdata9/airmag2/cmt/el52_94/mtlyell.asc',
     .  READONLY)
C LU 4 IS INPUT DATA SET
      OPEN(UNIT=8,FILE='lyellel.dat')
C LU 8 IS OUTPUT DATA SET
C
C
C FOR CONSOLE INPUT OF AREA COMMENT OUT LU 5 ASSIGNMENT
      OPEN(UNIT=5,FILE='coords.lst',READONLY)
C ALLOW USE OF A FILE OF COORDS AS PER THE SCREEN INPUT
C I OR O
C NO OF COORD PAIRS TO CLOSE AREA
C COORDS IN KM
      REWIND 4
      XMAX=-9.9E+9
      YMAX=XMAX
      XMIN=-XMAX
      YMIN=XMIN
C USE FOR RANGE OF COORDS
    9 WRITE(6,900)
  900 FORMAT(' DATA INSIDE OR OUTSIDE AREA?')
      READ(5,901) AOK
  901 FORMAT(A1)
      IF (AOK .NE. IN .AND. AOK .NE. OUT) GOTO 9
      WITHIN=AOK .EQ. IN
   10 WRITE(6,802)
  802 FORMAT(' NO. OF COORDINATE PAIRS TO CLOSE AREA?'/
     .   ' ENTER 2 TO SPECIFY A RECTANGLE')
      READ(5,*) NCORD
      IF (NCORD .LT. 2) GOTO 10
      IF (NCORD .EQ. 2) THEN
      WRITE(6,808)
  808 FORMAT(' ENTER COORDS (KM) OF TWO DIAGONALLY OPPOSITE CORNERS OF '
     .  'THE RECTANGLE'/' ONE PER LINE IN THE FORM  EAST  NORTH')
      ELSE
      WRITE(6,803)
  803 FORMAT(' ENTER COORDS. (KM) IN CLOCKWISE ORDER'/' EAST   NORTH')
      ENDIF
      DO 15 I=1,NCORD
      READ(5,*) XTMP,YTMP
      XTMP=XTMP*1000.
      YTMP=YTMP*1000.
C CONVERT TO METRES
      XMAX=AMAX1(XMAX,XTMP)
      XMIN=AMIN1(XMIN,XTMP)
      YMAX=AMAX1(YMAX,YTMP)
      YMIN=AMIN1(YMIN,YTMP)
      X(I)=XTMP
      Y(I)=YTMP
   15 CONTINUE
C READ THE COORDS OF THE AREA
      IF (NCORD .GT. 2) THEN
      IF (X(1) .EQ. X(NCORD) .AND. Y(1) .EQ. Y(NCORD)) GOTO 18
      NCORD=NCORD+1
      X(NCORD)=X(1)
      Y(NCORD)=Y(1)
C REQUIRE THAT AREA IS CLOSED
   18 WRITE(6,800)
  800 FORMAT(' COORDS. ARE')
      WRITE(6,801) (X(I),Y(I),I=1,NCORD)
  801 FORMAT(3(F12.0,F9.0))
      NCORD=NCORD-1
C ROUTINE FOR LOCATING POINTS DOESN'T REQUIRE CLOSURE
      CALL PREPLY(X,Y,NCORD,YINTVL,INTVLS,SWATH,RSLOPE)
C THE INITIAL SET UP
      ELSE
      WRITE(6,810) XMIN,XMAX,YMIN,YMAX
  810 FORMAT(' RECTANGLE FROM',F9.0,'ME TO',F9.0,'ME AND FROM',
     .  F9.0,'MN TO',F9.0,'MN')
      ENDIF
      WRITE(6,807)
  807 FORMAT(//)
      LINE=0
C COUNTER FOR RECORD NUMBER IN FILE
   20 READ(4,804,END=25) A
  804 FORMAT(A144)
C *************************SET TO FILE RECORDLENGTH
      LINE=LINE+1
C COUNT WAY THROUGH FILE
      WRITE(TEXT,804) A
C WRITE TO BUFFER TO ALLOW GETTING COORDS EASILY
C
C
C NOW FIND COORDS OF POINT DEPENDING ON WHETHER INTEGRER OR REALS
      READ(TEXT,805) XTMP,YTMP
  805 FORMAT(45X,2F10.1)
C SET ABOVE FOR REALS WITH RIGHT POSITION AND FIELDS
C     READ(TEXT,805) EAST,NORTH
C 805 FORMAT(24X,I8,I8)
C     XTMP=EAST
C     YTMP=NORTH
C ABOVE 4 LINES FOR INTEGER WITH RIGHT POSITIONS, FIELDS ETC
C
C
C
C
      IF (WITHIN) THEN
      IF (XTMP .LT. XMIN .OR. XTMP .GT. XMAX) GOTO 20
      IF (YTMP .LT. YMIN .OR. YTMP .GT. YMAX) GOTO 20
      ELSE
      IF (XTMP .LT. XMIN .OR. XTMP .GT. XMAX) GOTO 24
      IF (YTMP .LT. YMIN .OR. YTMP .GT. YMAX) GOTO 24
      IF (NCORD .EQ. 2) GOTO 20
      ENDIF
C IF OUTSIDE RANGE AND WANT OUTSIDE THEN PRINT
C NOT POSSIBLY IN AREA
      IF (NCORD .EQ. 2) GOTO 24
C SIMPLE RECTANGLE
      LOC=-1
C DEFAULT TO BE OUTSIDE AREA
      IF (INOUT(XTMP,YTMP,X,Y,YINTVL,INTVLS,SWATH,RSLOPE)) LOC=+1
      IF (LOC .GE. 0 .AND. WITHIN) GOTO 24
C WITHIN AREA AND WANT WITHIN AREA
      IF (LOC .GE. 0 .AND. .NOT. WITHIN) GOTO 20
C WITHIN AREA AND WANTED OUTSIDE AREA
C ALL THAT IS LEFT IS OUTSIDE AREA
C IF IN AREA GO DIRECT TO THE PRINT STATEMENT
      IF (WITHIN) GOTO 20
C NOT INSIDE AREA
   24 WRITE(8,804) A
C     WRITE(6,902) LINE
C 902 FORMAT(I9)
C  24 WRITE(8,902) LINE,A
C 902 FORMAT(I8,A46)
C WRITE RECORD NUMBER
      GOTO 20
   25 CONTINUE
      CLOSE(UNIT=8,STATUS='KEEP')
      STOP
      END
      INCLUDE '/mdata5/pe/fstjdn.f'
