*DECK PSPRAI
      SUBROUTINE PSPRAI(MXSEG ,NPTS  ,XYPOS ,CENTER,RCIRC ,
     >                  NSEG  ,IORDER,RADANG)
C
C-------------------------    PSPRAI    -------------------------------
C
C 1- SUBROUTINE STATISTICS:
C     NAME     : PSPRAI
C     USE      : FIND RECTANGULAR/ANNULAR INTERSECTION
C                AND ORDER POINTS FOR PLOTTING
C     MODIFIED : 99-03-15
C     AUTHOR   : G. MARLEAU
C
C 2- PARAMETERS:
C  INPUT
C     MXSEG  : MAXIMUM NUMBER OF SEGMENTS              I
C     NPTS   : NUMBER OF CORNERS                       I
C     XYPOS  : X AND Y POSITION OF CORNERS             R(2,NPTS)
C     CENTER : X AND Y POSITION OF ANNULUS CENTER      R(2)
C     RCIRC  : ANNULUS RADIUS                          R
C  OUTPUT
C     NSEG   : NUMBER OF REGION INTERSECTION           I
C              NUMBER OF SEGMENTS IS NSEG-1
C     IORDER : TYPE OF REGION                          I(MXSEG)
C              = -2 : ARC SEGMENT BEGINS
C              = -1 : ARC SEGMENT ENDS
C              =  0 : CLOSE PATH
C              >  0 : CORNER
C     RADANG : SEGMENTS INTERSECTION POINTS            R(2,MXSEG)
C              WITH RESPECT TO ANNULAR REGION CENTER
C              RADANG(1) = RADIAL POSITION
C              RADANG(2) = ANGULAR POSITION
C
C----------------------------------------------------------------------
C
      IMPLICIT         NONE
      INTEGER          IOUT
      CHARACTER        NAMSBR*6
      REAL             PI
      PARAMETER       (IOUT=6,PI=3.1415926535897932,NAMSBR='PSPRAI')
C----
C  ROUTINE PARAMETERS
C----
      INTEGER          MXSEG,NPTS,NSEG
      INTEGER          IORDER(MXSEG)
      REAL             XYPOS(2,NPTS),CENTER(2),RCIRC,
     >                 RADANG(2,MXSEG)
C----
C  LOCAL PARAMETERS
C----
      INTEGER          IPT,ICUR,INXT
      REAL             XYCUR(2),XYNXT(2),RADCUR,RADNXT,
     >                 XANN,YANN
C----
C  SCAN OVER CORNERS AND SIDES
C----
      NSEG=1
      IORDER(NSEG)=0
      DO 100 IPT=1,NPTS
        ICUR=IPT
        INXT=MOD(IPT,NPTS)+1
        XYCUR(1)=XYPOS(1,ICUR)-CENTER(1)
        XYCUR(2)=XYPOS(2,ICUR)-CENTER(2)
        XYNXT(1)=XYPOS(1,INXT)-CENTER(1)
        XYNXT(2)=XYPOS(2,INXT)-CENTER(2)
        RADCUR=SQRT(XYCUR(1)*XYCUR(1)+XYCUR(2)*XYCUR(2))
        RADNXT=SQRT(XYNXT(1)*XYNXT(1)+XYNXT(2)*XYNXT(2))
C----
C  CHECK IF CURRENT CORNER OF THE RECTANGLE IS LOCATED INSIDE
C  ANNULAR REGIONS
C----
        IF(RADCUR .LE. RCIRC) THEN
          IF(IORDER(NSEG) .NE. ICUR) THEN
C----
C  IT IS LOCATED INSIDE
C  SET IORDER TO IPT TO SPECIFY THIS POINT TO CORRESPOND TO
C  CORNER IPT
C----
            NSEG=NSEG+1
            IORDER(NSEG)=ICUR
            RADANG(1,NSEG)=RADCUR
            IF(RADCUR .EQ. 0.0) THEN
              RADANG(2,NSEG)=0.0
            ELSE
              RADANG(2,NSEG)=ATAN2(XYCUR(2),XYCUR(1))
            ENDIF
          ENDIF
        ENDIF
C----
C  FIND IF ANNULUS INTERSECTS WITH A RECTANGLE SIDE
C----
        XANN=XYCUR(1)
        YANN=XYCUR(2)
        IF(IPT .EQ. 1) THEN
C----
C  BOTTOM SIDE (Y=YANN)
C  INTERSECTION IF  -RCIRC < YANN < RCIRC
C  X-INTERSECTION POINTS SPECIFIED BY
C  XANN(1)=-SQRT(RCIRC*RCIRC-YANN*YANN)
C  XANN(2)= SQRT(RCIRC*RCIRC-YANN*YANN)
C  IF LOCATED BETWEEN XCUR AND XNEXT
C----
          IF(YANN .GE. -RCIRC .AND. YANN .LE. RCIRC) THEN
            XANN=-SQRT(RCIRC*RCIRC-YANN*YANN)
            IF(XANN .GE. XYCUR(1) .AND.
     >         XANN .LE. XYNXT(1)) THEN
              NSEG=NSEG+1
              IORDER(NSEG)=-1
              RADANG(1,NSEG)=RCIRC
              RADANG(2,NSEG)=ATAN2(YANN,XANN)
            ENDIF
            XANN=-XANN
            IF(XANN .GE. XYCUR(1) .AND.
     >         XANN .LE. XYNXT(1)) THEN
              NSEG=NSEG+1
              IORDER(NSEG)=-2
              RADANG(1,NSEG)=RCIRC
              RADANG(2,NSEG)=ATAN2(YANN,XANN)
            ENDIF
          ENDIF
        ELSE IF(IPT .EQ. 2) THEN
C----
C  RIGHT SIDE (X=XANN)
C  INTERSECTION IF  -RCIRC < XANN < RCIRC
C  Y-INTERSECTION POINTS SPECIFIED BY
C  YANN(1)=-SQRT(RCIRC*RCIRC-XANN*XANN)
C  YANN(2)= SQRT(RCIRC*RCIRC-XANN*XANN)
C  IF LOCATED BETWEEN YCUR AND YNEXT
C----
          IF(XANN .GE. -RCIRC .AND. XANN .LE. RCIRC) THEN
            YANN=-SQRT(RCIRC*RCIRC-XANN*XANN)
            IF(YANN .GE. XYCUR(2) .AND.
     >         YANN .LE. XYNXT(2)) THEN
              NSEG=NSEG+1
              IORDER(NSEG)=-1
              RADANG(1,NSEG)=RCIRC
              RADANG(2,NSEG)=ATAN2(YANN,XANN)
            ENDIF
            YANN=-YANN
            IF(YANN .GE. XYCUR(2) .AND.
     >         YANN .LE. XYNXT(2)) THEN
              NSEG=NSEG+1
              IORDER(NSEG)=-2
              RADANG(1,NSEG)=RCIRC
              RADANG(2,NSEG)=ATAN2(YANN,XANN)
            ENDIF
          ENDIF
        ELSE IF(IPT .EQ. 3) THEN
C----
C  TOP SIDE (Y=YANN)
C  INTERSECTION IF  -RCIRC < YANN < RCIRC
C  X-INTERSECTION POINTS SPECIFIED BY
C  XANN(1)= SQRT(RCIRC*RCIRC-YANN*YANN)
C  XANN(2)=-SQRT(RCIRC*RCIRC-YANN*YANN)
C  IF LOCATED BETWEEN XNEXT AND XCUR
C----
          IF(YANN .GE. -RCIRC .AND. YANN .LE. RCIRC) THEN
            XANN=SQRT(RCIRC*RCIRC-YANN*YANN)
            IF(XANN .GE. XYNXT(1) .AND.
     >         XANN .LE. XYCUR(1)) THEN
              NSEG=NSEG+1
              IORDER(NSEG)=-1
              RADANG(1,NSEG)=RCIRC
              RADANG(2,NSEG)=ATAN2(YANN,XANN)
            ENDIF
            XANN=-XANN
            IF(XANN .GE. XYNXT(1) .AND.
     >         XANN .LE. XYCUR(1)) THEN
              NSEG=NSEG+1
              IORDER(NSEG)=-2
              RADANG(1,NSEG)=RCIRC
              RADANG(2,NSEG)=ATAN2(YANN,XANN)
            ENDIF
          ENDIF
        ELSE IF(IPT .EQ. 4) THEN
C----
C  LEFT SIDE (X=XANN)
C  INTERSECTION IF  -RCIRC < XANN < RCIRC
C  Y-INTERSECTION POINTS SPECIFIED BY
C  YANN(1)= SQRT(RCIRC*RCIRC-XANN*XANN)
C  YANN(2)=-SQRT(RCIRC*RCIRC-XANN*XANN)
C  IF LOCATED BETWEEN YNEXT AND YCUR
C----
          IF(XANN .GE. -RCIRC .AND. XANN .LE. RCIRC) THEN
            YANN= SQRT(RCIRC*RCIRC-XANN*XANN)
            IF(YANN .GE. XYNXT(2) .AND.
     >         YANN .LE. XYCUR(2)) THEN
              NSEG=NSEG+1
              IORDER(NSEG)=-1
              RADANG(1,NSEG)=RCIRC
              RADANG(2,NSEG)=ATAN2(YANN,XANN)
            ENDIF
            YANN=-YANN
            IF(YANN .GE. XYNXT(2) .AND.
     >         YANN .LE. XYCUR(2)) THEN
              NSEG=NSEG+1
              IORDER(NSEG)=-2
              RADANG(1,NSEG)=RCIRC
              RADANG(2,NSEG)=ATAN2(YANN,XANN)
            ENDIF
          ENDIF
        ENDIF
C----
C  CHECK IF NEXT CORNER OF THE RECTANGLE IS LOCATED INSIDE
C  ANNULAR REGIONS
C----
        IF(RADNXT .LE. RCIRC) THEN
C----
C  IT IS LOCATED INSIDE
C  SET IORDER TO IPT TO SPECIFY THIS POINT TO CORRESPOND TO
C  CORNER IPT
C----
          NSEG=NSEG+1
          IORDER(NSEG) =INXT
          RADANG(1,NSEG)=RADNXT
          IF(RADNXT .EQ. 0.0) THEN
            RADANG(2,NSEG)=0.0
          ELSE
            RADANG(2,NSEG)=ATAN2(XYNXT(2),XYNXT(1))
          ENDIF
        ENDIF
 100  CONTINUE
C----
C  STORE LAST SEGMENT ALSO AT FIRST POSITION
C  FOR CYCLIC TRACKING
C----
      IF(NSEG .EQ. 1) THEN
        NSEG=2
        IORDER(1)=-2
        RADANG(1,1)=RCIRC
        RADANG(2,1)=0.0
        IORDER(2)=-1
        RADANG(1,2)=RCIRC
        RADANG(2,2)=2.0*PI
      ELSE
        IORDER(1) =IORDER(NSEG)
        RADANG(1,1)=RADANG(1,NSEG)
        RADANG(2,1)=RADANG(2,NSEG)
      ENDIF
      RETURN
      END
