
      PROGRAM EXOTFI
C
C This program is meant to provide a quick plot of data written from the
C EZMAP database editor by an "O" command.
C
C Define the error file, the Fortran unit number, the workstation type,
C and the workstation ID to be used in calls to GKS routines.  Use one
C of the following:
C
        PARAMETER (IERF=6,LUNI=2,IWTY=1 ,IWID=1)  !  NCGM
C       PARAMETER (IERF=6,LUNI=2,IWTY=8 ,IWID=1)  !  X Windows
C       PARAMETER (IERF=6,LUNI=2,IWTY=20,IWID=1)  !  PostScript
C       PARAMETER (IERF=6,LUNI=2,IWTY=11,IWID=1)  !  PDF, Portrait
C       PARAMETER (IERF=6,LUNI=2,IWTY=12,IWID=1)  !  PDF, Landscape
C
C Declare arrays required for color fill of areas defined by the data.
C
        PARAMETER (LAMA=2000000,MCRA=100000)
        DIMENSION IAMA(LAMA),XCRA(MCRA),YCRA(MCRA),IAAI(1),IAGI(1)
C
C Declare an array for the line widths to be used for lines of five
C different types.
C
        DIMENSION WDTH(5)
C
C Declare a variable in which to get the name of the user dataset to be
C read.
C
        CHARACTER*64 FNMI
C
C Declare some required temporary character variables.
C
        CHARACTER*1 DUMI
        CHARACTER*100 NAML,NAMR
C
C Declare the color-fill routine for areas EXTERNAL so as to keep the
C compiler from treating it as a real variable.
C
        EXTERNAL COLORA
C
C Define the line widths associated with the different line types.
C
        DATA WDTH / 3. , 3. , 3. , 2. , 1. /
C
C Open GKS.
C
        CALL GOPKS (IERF,0)
        CALL GOPWK (IWID,LUNI,IWTY)
        CALL GACWK (IWID)
C
C Tell GKS to use solid fill.
C
        CALL GSFAIS (1)
C
C Turn off clipping by GKS.
C
        CALL GSCLIP (0)
C
C Define colors to use.
C
        CALL GSCR (IWID,101,.5,.6,.8)
        CALL GSCR (IWID,102,.2,.4,.6)
        CALL GSCR (IWID,103,.2,.6,.4)
        CALL GSCR (IWID,104,.2,.8,.2)
        CALL GSCR (IWID,105,.4,.6,.2)
        CALL GSCR (IWID,106,.6,.4,.2)
        CALL GSCR (IWID,107,.6,.6,.6)
        CALL GSCR (IWID,108,.9,1.,1.)
C
C Allow the user to enter the name of the file to be read.
C
        PRINT * , ' '
        PRINT * , 'Enter the name of the dataset to be examined:'
        READ (*,'(A64)') FNMI
C
C Open the user's input file.
C
        OPEN (11,FILE=FNMI(1:IOLNBC(FNMI)),STATUS='OLD',
     +                                         FORM='FORMATTED',ERR=904)
C
C Skip three records in the input file.
C
        READ (11,'(A1)',END=901) DUMI
        READ (11,'(A1)',END=901) DUMI
        READ (11,'(A1)',END=901) DUMI
C
C Read the values that tell us what values are associated with the left,
C right, top, and bottom edges of the window.  The values of X are the
C minimum and maximum longitudes and the values of Y are the minimum and
C maximum latitudes.
C
        READ (11,'(F12.6)',END=901,ERR=902) XWDL
        READ (11,'(F12.6)',END=901,ERR=902) XWDR
        READ (11,'(F12.6)',END=901,ERR=902) YWDB
        READ (11,'(F12.6)',END=901,ERR=902) YWDT
C
C Call SET to define the mapping of lon/lat pairs to the window.
C
        CALL SET (.01,.99,.01,.99,XWDL,XWDR,YWDB,YWDT,1)
C
C Initialize the area map array.
C
        CALL ARINAM (IAMA,LAMA)
C
C Put the edge of the window into the area map.
C
        CALL PLOTAM (XWDL,YWDB,0,IAMA,1,0,0)
        CALL PLOTAM (XWDR,YWDB,1,IAMA,1,0,0)
        CALL PLOTAM (XWDR,YWDT,1,IAMA,1,0,0)
        CALL PLOTAM (XWDL,YWDT,1,IAMA,1,0,0)
        CALL PLOTAM (XWDL,YWDB,1,IAMA,1,0,0)
C
C Skip the next record of the input file.
C
        READ (11,'(A1)',END=901) DUMI
C
C Initialize the "expected line number" to zero.
C
        LNEX=0
C
C Control returns here for each new line defined by the outline data.
C Skip two records.  Note that, if we get an EOF trying to read the
C first record, it means we've hit the end of the input file.
C
  101   READ (11,'(A1)',END=103) DUMI
        READ (11,'(A1)',END=901) DUMI
C
C Read the value of the line number.
C
        READ (11,'(20X,I6)',END=901,ERR=902) LNRD
C
C Bump the value of the expected line number and make sure it matches
C the value just read from the file.  If not, jump to an error exit.
C
        LNEX=LNEX+1
        IF (LNEX.NE.LNRD) GO TO 903
C
C Skip the next record of the input file.
C
        READ (11,'(A1)',END=901) DUMI
C
C Read the value of the line type.
C
        READ (11,'(20X,I6)',END=901,ERR=902) ILTY
C
C Read the values of the left and right area identifiers, the left and
C right area types, and the left and right suggested color indices.
C
        READ (11,'(20X,I6)',END=901,ERR=902) IAIL
        READ (11,'(20X,I6)',END=901,ERR=902) IAIR
        READ (11,'(20X,I6)',END=901,ERR=902) IATL
        READ (11,'(20X,I6)',END=901,ERR=902) IATR
        READ (11,'(20X,I6)',END=901,ERR=902) ISCL
        READ (11,'(20X,I6)',END=901,ERR=902) ISCR
C
C Skip the next three records of the input file.
C
        READ (11,'(A1)',END=901) DUMI
        READ (11,'(A1)',END=901) DUMI
        READ (11,'(A1)',END=901) DUMI
C
C Read the values of the names of the areas to the left and the right.
C
        READ (11,'(A100)',END=901,ERR=902) NAML
        READ (11,'(A100)',END=901,ERR=902) NAMR
C
C Skip the next three records of the input file.
C
        READ (11,'(A1)',END=901) DUMI
        READ (11,'(A1)',END=901) DUMI
        READ (11,'(A1)',END=901) DUMI
C
C Initialize the "pen" parameter to produce a "pen-up" move.
C
        IPEN=0
C
C Control returns here to read each new pair of longitude and latitude
C values defining the position of a point on the line.
C
  102   READ (11,'(2F12.6)',END=901,ERR=902) PLON,PLAT
C
C If the point is not at (0,0), extend the line segment being put into
C the area map and jump back for the next point of the line; otherwise,
C drop through.
C
        IF (PLON.NE.0..OR.PLAT.NE.0.) THEN
          CALL PLOTAM (PLON,PLAT,IPEN,IAMA,1,ISCL,ISCR)
          IPEN=1
          GO TO 102
        END IF
C
C Jump back to read the next line.
C
        GO TO 101
C
C The end of the input file has been reached; use the contents of the
C area-map array to color the areas defined.
C
  103   CALL ARSCAM (IAMA,XCRA,YCRA,MCRA,IAAI,IAGI,1,COLORA)
C
C Rewind the input file.  We'll read it again to draw lines on top of
C the colored background.
C
        REWIND 11
C
C Skip three records in the input file.
C
        READ (11,'(A1)',END=901) DUMI
        READ (11,'(A1)',END=901) DUMI
        READ (11,'(A1)',END=901) DUMI
C
C Read the values that tell us what values are associated with the left,
C right, top, and bottom edges of the window.  The values of X are the
C minimum and maximum longitudes and the values of Y are the minimum and
C maximum latitudes.
C
        READ (11,'(F12.6)',END=901,ERR=902) XWDL
        READ (11,'(F12.6)',END=901,ERR=902) XWDR
        READ (11,'(F12.6)',END=901,ERR=902) YWDB
        READ (11,'(F12.6)',END=901,ERR=902) YWDT
C
C Call SET to define the mapping of lon/lat pairs to the window.
C
        CALL SET (.01,.99,.01,.99,XWDL,XWDR,YWDB,YWDT,1)
C
C Skip the next record of the input file.
C
        READ (11,'(A1)',END=901) DUMI
C
C Initialize the "expected line number" to zero.
C
        LNEX=0
C
C Control returns here for each new line defined by the outline data.
C Skip two records.  Note that, if we get an EOF trying to read the
C first record, it means we've hit the end of the input file.
C
  201   READ (11,'(A1)',END=203) DUMI
        READ (11,'(A1)',END=901) DUMI
C
C Read the value of the line number.
C
        READ (11,'(20X,I6)',END=901,ERR=902) LNRD
C
C Bump the value of the expected line number and make sure it matches
C the value just read from the file.  If not, jump to an error exit.
C
        LNEX=LNEX+1
        IF (LNEX.NE.LNRD) GO TO 903
C
C Skip the next record of the input file.
C
        READ (11,'(A1)',END=901) DUMI
C
C Read the value of the line type.
C
        READ (11,'(20X,I6)',END=901,ERR=902) ILTY
C
C Set the current line width to the value appropriate to the line type.
C
        CALL GSLWSC (WDTH(MAX(1,MIN(5,ILTY))))
C
C Read the values of the left and right area identifiers, the left and
C right area types, and the left and right suggested color indices.
C
        READ (11,'(20X,I6)',END=901,ERR=902) IAIL
        READ (11,'(20X,I6)',END=901,ERR=902) IAIR
        READ (11,'(20X,I6)',END=901,ERR=902) IATL
        READ (11,'(20X,I6)',END=901,ERR=902) IATR
        READ (11,'(20X,I6)',END=901,ERR=902) ISCL
        READ (11,'(20X,I6)',END=901,ERR=902) ISCR
C
C Skip the next three records of the input file.
C
        READ (11,'(A1)',END=901) DUMI
        READ (11,'(A1)',END=901) DUMI
        READ (11,'(A1)',END=901) DUMI
C
C Read the values of the names of the areas to the left and the right.
C
        READ (11,'(A100)',END=901,ERR=902) NAML
        READ (11,'(A100)',END=901,ERR=902) NAMR
C
C Skip the next three records of the input file.
C
        READ (11,'(A1)',END=901) DUMI
        READ (11,'(A1)',END=901) DUMI
        READ (11,'(A1)',END=901) DUMI
C
C Initialize the "pen" parameter to produce a "pen-up" move.
C
        IPEN=0
C
C Control returns here to read each new pair of longitude and latitude
C values defining the position of a point on the line.
C
  202   READ (11,'(2F12.6)',END=901,ERR=902) PLON,PLAT
C
C If the point is not at (0,0), plot it and jump back for the next
C point of the line; otherwise, flush the buffers and drop through.
C
        IF (PLON.NE.0..OR.PLAT.NE.0.) THEN
          CALL PLOTIF (CUFX(PLON),CUFY(PLAT),IPEN)
          IPEN=1
          GO TO 202
        ELSE
          CALL SFLUSH
        END IF
C
C Jump back to read the next line.
C
        GO TO 201
C
C The end of the input file has been reached; advance the frame.
C
  203   CALL FRAME
C
C Close GKS.
C
        CALL GDAWK (IWID)
        CALL GCLWK (IWID)
        CALL GCLKS
C
C Done.
C
        STOP
C
C Error exits.
C
  901   PRINT * , 'STOP ON UNEXPECTED EOF'
        STOP
C
  902   PRINT * , 'STOP ON READ ERROR'
        STOP
C
  903   PRINT * , 'STOP ON UNEXPECTED LINE NUMBER READ'
        STOP
C
  904   PRINT * , 'STOP ON ERROR OPENING INPUT FILE'
        STOP
C
      END



      SUBROUTINE PLOTAM (XCRD,YCRD,IPEN,IAMA,IGRP,IAIL,IAIR)
C
C This subroutine allows us to put the outlines read from the user's
C data file into an area map one point at a time.  (XCRD,YCRD) is the
C point to be put into the area map.  IPEN is 0 if a new line is being
C started and 1 if a line is being continued.  IAMA is the area map
C array.  IGRP is the group identifier.  IAIL is the identifier of the
C area to the left.  IAIR is the identifier of the area to the right.
C
C Dimension a local array to be used in the calls to AREDAM.  Each time
C we call it, we give it the two endpoints of a little line segment.
C
        DIMENSION XCRA(2),YCRA(2)
        SAVE XCRA,YCRA
C
C Do it.
C
        IF (IPEN.EQ.0) THEN
          XCRA(2)=XCRD
          YCRA(2)=YCRD
        ELSE
          XCRA(1)=XCRA(2)
          YCRA(1)=YCRA(2)
          XCRA(2)=XCRD
          YCRA(2)=YCRD
          CALL AREDAM (IAMA,XCRA,YCRA,2,IGRP,IAIL,IAIR)
        END IF
C
C Done.
C
        RETURN
C
      END



      SUBROUTINE COLORA (XCRA,YCRA,NCRA,IAAI,IAGI,NGPS)
C
C This routine is called by the routine ARSCAM (in the package AREAS).
C Each such call defines a particular area to be filled, providing the
C point coordinates of the area's outline (in XCRA and YCRA) and the
C area-identifier information (in IAAI and IAGI) necessary to decide
C what color to use.
C
        DIMENSION XCRA(NCRA),YCRA(NCRA),IAAI(NGPS),IAGI(NGPS)
C
C Do it.
C
        IF (NCRA.GT.1) THEN
          IF (NGPS.EQ.1.AND.IAGI(1).EQ.1.AND.
     +        IAAI(1).GE.1.AND.IAAI(1).LE.8) THEN
            CALL GSFACI (100+IAAI(1))
            CALL GFA (NCRA-1,XCRA,YCRA)
          END IF
        END IF
C
C Done.
C
        RETURN
C
      END



      INTEGER FUNCTION IOLNBC (CHRS)
C
        CHARACTER*(*) CHRS
C
C The value of MPILNB(CHRS) is the index of the last non-blank in the
C character string CHRS.
C
        DO 101 I=LEN(CHRS),1,-1
          IF (CHRS(I:I).NE.' ') THEN
            IOLNBC=I
            RETURN
          END IF
  101   CONTINUE
C
        IOLNBC=1
C
        RETURN
C
      END
