C  GRFMM1B
C
C  J F Schivell  23 Aug 95  -- based on GRFMM1; removed key
C
C  SET SCALE AND
C  PLOT MULTIPLOT VIA CALL TO MULFDR AND ALLOW REPEAT OF PLOT
C  ON DIFFERENT SCALE
C
C  ** PLOTTING MOMENTS FLUX SURFACE CONTOURS** THE SCALE IS MADE
C  "SQUARE" SO THAT (R,Y) SPACE OF THE POLOIDAL PLANE IS NOT
C  DISTORTED
C    RESCALE OPTION ALLOWS USER TO SPECIFY (R,Y) OF LOWER LEFT HAND
C  CORNER OF SQUARE AND LENGTH OF EDGE
C
C  ** MOD DMC 6 JUL 1992 **
C  SQUARE SCALE PLOTTING CAN BE DISABLED BY KLUGE:  SET THE FIRST
C  ELEMENT OF THE INPUT ARRAY ISELF NEGATIVE ***; THE ABSOLUTE
C  VALUE IS USED IN THE CODE LOGIC TO ADDRESS THE CURVE TO BE
C  PLOTTED, AS BEFORE.
C
C  PLOT 1 GRAPH IMMEDIATELY ON CALL WITH ESTABLISHED DEFAULTS
C  THEN GIVE OPPORTUNITY TO USER TO CHANGE SCALING/AXIS DEFAULTS
C  OR JUST ON THE FUNCTION BEING PLOTTED WITHOUT AFFECTING DEFAULTS
C  ON FUTURE CALLS.
C  USE UREAD I/O SUBROUTINE
C
C  AXES SELECTION (LOG/LINEAR FOR EITHER/BOTH/NEITHER AXES)
C  SOFTWARE IN "GAXES.FOR" IS USED
C
C  FEB 1986 - DMC
C   REWRITTEN TO USE STANDARDIZED SCALE SETTING AND POST-PLOT RESCALE
C  MENU OPTIONS
C
      SUBROUTINE GRFMM1B(R,Y,ND1,NC,NNC,Z,NZ,ISELF,NSEL,
     >     RUN,YUN,MNLAB)
C
C  ARGUMENTS -- SAME AS SUBROUTINE MULCDR (MULCDR.FOR) EXCEPT
C   ARGUMENT "IAXSEL" IS DETERMINED HERE
C
      implicit NONE
      integer nd1,nz,nsel,nnc
      REAL R(ND1,NZ),Y(ND1,NZ),Z(NZ)
      INTEGER ISELF(NSEL)
      INTEGER NC(NNC)       ! NO. OF PTS / CONTOUR
      CHARACTER*(*) MNLAB(3)
      CHARACTER*(*) RUN,YUN
c
c  local
c
      integer insel,ilocsc,isel,i,ind,itot,luntrm,lnout,icod
      real zdx,zdy,zd2
      integer, save :: iopcod=0
C
C---------------------------------------------
C
      Integer, Parameter :: jmsurf = 201
      INTEGER IPTR(jmsurf),LPTR(jmsurf),JSELF(jmsurf)
C
      INCLUDE 'PLOT1MMX'
C
      EXTERNAL GRADUM  ! FOR GRAOPT CALL INTERFACE
C
      LOGICAL ISQUARE
C
C---------------------------------------------
C
      integer icounter
C
C-------------------------------------------------------------
C
      icounter=0
C
C---------------------------------------------
C
      INSEL = NSEL
 
      IF(ISELF(1).GT.0) THEN
        ISQUARE=.TRUE.
      ELSE
        ISQUARE=.FALSE.
      ENDIF
C
C  LOCAL DEFAULT OVERRIDE SWITCHES CLEARED:
      ILOCSC=0
C
C  GET DEFAULT AXIS SELECTION
      CALL GAXGDF(ISEL)
C
C  SET UP INDICES FOR SCALE CALCULATION
C
      DO 5 I=1,INSEL
        JSELF(I)=IABS(ISELF(I))
        IND=JSELF(I)
        IPTR(I)=1+(IND-1)*ND1
        LPTR(I)=NC(MIN0(I,NNC))
 5    CONTINUE
C
C-------------------------------------------------------------
C  AUTOMATIC GENERATION OF PLOT
C
 100  CONTINUE
      icounter=icounter+1
C
C  SET AXES
C
      CALL GAXCUR(ISEL)
C
C  SET SCALE
C
      ITOT=ND1*NZ
      IF(ILOCSC.EQ.0) THEN
C  COMPUTE X Y PLOT SCALE (USE GSCALE DEFAULTS)
        CALL GSCSMG(R,Y,ITOT,ITOT,IPTR,1,IPTR,1,LPTR,INSEL)
      ELSE IF(ILOCSC.EQ.1) THEN
C  COMPUTE Y AUTOSCALE ONLY
        CALL GSCSM0(R,Y,ITOT,ITOT,IPTR,1,IPTR,1,
     >                  LPTR,INSEL,XMIN,XMAX,1)
        CALL GSCYDF(1)
      ENDIF
C
      CALL GSCSEP(XMIN,XMAX)
      CALL GSCSEP(YMIN,YMAX)
C
C  MAKE IT A "SQUARE" SCALE
C
      IF(ISQUARE) THEN
        ZDX=XMAX-XMIN
        ZDY=YMAX-YMIN
        ZD2=0.5*ABS(ZDX-ZDY)
        IF(ZDX.GT.ZDY) THEN
          YMIN=YMIN-ZD2
          YMAX=YMAX+ZD2
        ELSE IF(ZDY.GT.ZDX) THEN
          XMIN=XMIN-ZD2
          XMAX=XMAX+ZD2
        ENDIF
      ENDIF
C
      LUNTRM=LNOUT(0)
      CALL GAXCHK(LUNTRM,ISEL,ICOD)
C
      CALL GSCREC  ! RECORD SCALE OF UPCOMING PLOT IN GSCALE COMMON
C
      CALL MULCDRB(
     >   R,Y,ND1,NC,NNC,Z,NZ,JSELF,INSEL,ICOD,
     >   RUN,YUN,MNLAB)
C
      GO TO 200
C
C
C  POST PLOT OPTIONS
C
 200  CONTINUE
C
C  GENERATE MENU AND FETCH USER REPLY
C
      IF(ISQUARE) THEN
        CALL GRAOPT('SQA',ISEL,ILOCSC,IOPCOD,GRADUM)
      ELSE
        CALL GRAOPT('SA',ISEL,ILOCSC,IOPCOD,GRADUM)
      ENDIF
C
C  ACT ON USER REPLY - REPLOT?
C
      IF(IOPCOD.EQ.1) THEN
         GO TO 100
      ELSE IF(IOPCOD.EQ.2) THEN
         RETURN
      ENDIF
C
      CALL UERMSI(' ?GRFMM1 *FATAL BUG* UNANTICIPATED OPERATION CODE',
     >              IOPCOD,1)
      call bad_exit
C
      	END
