

C LAST UPDATE: 
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C**************************
C**     LSC_HIST        **
C**************************
C
C **** LSM SERVICE ROUTINE ONLY ****
C
CD-Routine: Output of reference set scaling results - LSC_HIST
C This routine is called by LSC_HIST_RES, LSC_HIST_INT, LSC_HIST_THETA,
C and LSC_HIST_LAMBDA and outputs histogramms of the reference set scaling 
C statistics prpared by those calling routines
CD-end
CD-Fortran:
C
      SUBROUTINE LSC_HIST (IOPT, OUTPUT, NUMBINS, BIN_WIDTH, 
     +                      VAL_MIN, VAL_MAX, RBIN)
CD-end
C
      INCLUDE 'implicit_none.finc'
C
C Purpose: Prepares output histograms of scaling results - LSC_HIST 
C
C Author:  S. Arzt, September 1997
C
C Arguments:
C
      INTEGER IOPT
      INTEGER OUTPUT
      INTEGER NUMBINS
      REAL BIN_WIDTH
      REAL VAL_MIN
      REAL VAL_MAX
      REAL RBIN(*)
C
CD-Parameters:
C  IOPT      i   (R)  Option flag =1 lambda, =2 resolution, 
C                               =3 intensity, = 4 theta
C  OUTPUT    i   (R)  Option flag =1 no. of relections per bin vs IOPT
C                          flag =2 mean IL/IS ratio per bin vs IOPT
C                          flag =3 mean IL per bin vs IOPT
C                          flag =4 mean IS per bin vs IOPT
C                          flag =5 R-factor per bin vs IOPT 
C                          flag =6 log(IL/IS) per bin vs resolution 
C  NUMBINS   i   (R)  Number of bins
C  BIN_WIDTH r   (R)  Bin width
C  VAL_MIN   r   (R)  Minimum soft limits value
C  VAL_MAX   r   (R)  Maximum soft limits value
C  RBIN()    r   (R)  Significant/Measured intensity ratios for NUMBINS bins
CD-end

C
C====== PARAMETERS
C 
      INCLUDE 'limits.finc'
C
C====== GLOBALS:
C
      INCLUDE 'view_handles.finc'
      INCLUDE 'get_soft.finc'
      INCLUDE 'screen_params.finc'
C
C====== FUNCTION TYPES:
C
      INTEGER XDLSTR
      INTEGER LENSTR
C
C====== LOCALS:
C
      INTEGER I
      INTEGER IERR
      INTEGER NDX, NDY, NDEVX, NDEVY
      REAL WIDTHX, WIDTHY
      REAL AX1_DIV
      REAL X1
      REAL X2
      REAL Y
      REAL RBINMAX
      REAL RBINMIN
      REAL XMIN, XMAX, YMIN, YMAX
      CHARACTER*80 TITLE
      CHARACTER*40 OPTCHAR
      CHARACTER*25 QUITNAM

      CHARACTER*25 NAMES(4)
      DATA NAMES /'Plot 1',
     *            'Plot 2',
     *            'Plot 3',
     *            'Plot 4'/
      DATA QUITNAM /'Quit'/
C     LOGICAL MARK_OVER       !Mark value >1.0 on normalised histogram
C     INTEGER I               !Temp/loop variable
C     INTEGER KXROOT          !Root window x position for graphics window
C     INTEGER KYROOT          !Root window y position for graphics window
C     INTEGER IERR            !Error flag
C     REAL AX1_DIV            !Axis 1 divisions width
C     REAL X1                 !Start of histogram box (x axis)
C     REAL X2                 !End of histogram box (x axis)
C     REAL Y                  !Height of histogram box (y axis)
C     REAL ARR_H              !Arrow height 
C     REAL ARR_W              !Arrow half width
C     REAL ARR_X(3)           !Arrow head x coordinates
C     REAL ARR_Y(3)           !Arrow head y coordinates
C     REAL RBINMAX            !Maximum value is RBIN array
C     CHARACTER*80 TITLE      !Graph title string
C
C-------------------------------------------------------------------------------
C
C====== Initialise
C
      YMIN = 0.0
      YMAX = 0.0
      XMIN = 0.0
      XMAX = 0.0
      NDY = 3
      NDX = 2
      NDEVX = 0
      NDEVY = 0
      WIDTHX = 0.0
      WIDTHY = 0.0
      
      CALL XDLF_GRAPH_WIN_DELETE (IVH_GRAPH, 0, IERR)
C
C====== Plot the histogram
C
      RBINMAX = RBIN(1)
      RBINMIN = RBIN(1)
      DO 140 I = 1, NUMBINS
         IF(RBIN(I).GT.RBINMAX) RBINMAX = RBIN(I)
         IF(RBIN(I).LT.RBINMIN) RBINMIN = RBIN(I)
 140  CONTINUE

      IF (IOPT.EQ.1) THEN
         TITLE = 'ANALYSIS OF DATA AS FUNCTION OF LAMBDA, 1. RANGE'
         OPTCHAR = 'LAMBDA'
         XMIN = VAL_MIN 
         XMAX = VAL_MAX
         NDX = 2
         NDEVX = 10
         WIDTHX = 0.0
      END IF   

      IF (IOPT.EQ.2) THEN
         TITLE = 'ANALYSIS OF DATA AS FUNCTION OF RESOLUTION'
         OPTCHAR = 'SIN**(THETA)/LAMBDA**2'
         XMIN = VAL_MIN 
         XMAX = VAL_MAX
         NDX = 3
         NDEVX = 10
         WIDTHX = 0.0
      END IF   

      IF (IOPT.EQ.3) THEN
         TITLE = 'ANALYSIS OF DATA AS FUNCTION OF INTENSITIES'
         OPTCHAR = 'INTENSITIES'
         XMIN = VAL_MIN 
         XMAX = VAL_MAX
         NDX = 0
         NDEVX = 10
         WIDTHX = 0.0
      END IF   

      IF (IOPT.EQ.4) THEN
         TITLE = 'ANALYSIS OF DATA AS FUNCTION OF THETA'
         OPTCHAR = 'THETA'
         XMIN = VAL_MIN 
         XMAX = VAL_MAX
         NDX = 1
         NDEVX = 10
         WIDTHX = 0.0
      END IF   

      CALL XDLF_GRAPH_WIN_STYLE (IVH_GRAPH, 0, 1, -1, IERR)
      CALL XDLF_GRAPH_WIN_MAP (IVH_GRAPH, 0.0, 1.0, 
     +                         0.0, 1.0, 0.0, 1.0, 0.0, 1.0,
     +                         0, IERR)
      CALL XDLF_GRAPH_WIN_STEXT (IVH_GRAPH, 0.5, 0.95,
     +                           XDLSTR(TITLE), LENSTR(TITLE),
     +                           0.0, .042, 0, 1, 0.0, IERR)

      IF (OUTPUT.EQ.1) THEN
         TITLE = 'No. of Laue reflections per '//
     +            OPTCHAR(1:LENSTR(OPTCHAR))//' bins' 
         YMIN = 0.0
         YMAX = RBINMAX         
         NDY = 0
         NDEVY = 10
         WIDTHY = 0.0
      ELSE IF (OUTPUT.EQ.2) THEN
         TITLE = 'Mean IL/Iref versus '//OPTCHAR(1:LENSTR(OPTCHAR))
     +            //' bins' 
         YMIN = 0.0
         YMAX = RBINMAX
         NDY = 3
         NDEVY = 10
         WIDTHY = 0.0
      ELSE IF (OUTPUT.EQ.3) THEN
         TITLE = 'Mean scaled Laue intensities versus '//
     +            OPTCHAR(1:LENSTR(OPTCHAR))//' bins' 
         YMIN = 0.0
         YMAX = RBINMAX
         NDY = 1
         NDEVY = 10
         WIDTHY = 0.0
      ELSE IF (OUTPUT.EQ.4) THEN
         TITLE = 'Mean scaled reference intensities versus '
     +            //OPTCHAR(1:LENSTR(OPTCHAR))//' bins' 
         YMIN = 0.0
         YMAX = RBINMAX
         NDY = 1
         NDEVY = 10
         WIDTHY = 0.0
      ELSE IF (OUTPUT.EQ.5) THEN
         TITLE = 'R-factor versus  '//OPTCHAR(1:LENSTR(OPTCHAR))
     +            //' bins' 
         YMIN = 0.0
         YMAX = RBINMAX
         NDY = 3
         NDEVY = 10
         WIDTHY = 0.0
      ELSE IF (OUTPUT.EQ.6) THEN
         TITLE = 'log(IL/Iref) versus '//OPTCHAR(1:LENSTR(OPTCHAR))
     +            //' bins' 
         YMIN = RBINMIN - 0.01
         YMAX = RBINMAX
         NDY = 3
         NDEVY = 10
         WIDTHY = 0.0
      END IF

      CALL XDLF_GRAPH_WIN_STEXT (IVH_GRAPH, 0.5, 0.89,
     +                           XDLSTR(TITLE), LENSTR(TITLE),
     +                           0.0, .033, 0, 1, 0.0, IERR)

      IF (IOPT.EQ.1) THEN
         TITLE = 'lambda'
         CALL STR_ADDS (TITLE,'(Angstroms)',1)
      ELSE IF (IOPT.EQ.2) THEN
         TITLE = 'sin**(theta)/lambda**2'
      ELSE IF (IOPT.EQ.3) THEN
         TITLE = 'intensities'      
      END IF

      CALL XDLF_GRAPH_WIN_STEXT (IVH_GRAPH, 0.5, 0.05,
     +                           XDLSTR(TITLE), LENSTR(TITLE),
     +                           0.0, .033, 0, 1, 0.0, IERR)

      CALL XDLF_GRAPH_WIN_MAP (IVH_GRAPH, XMIN, XMAX, 
     +                    YMIN, YMAX, 0.1, 0.9, 0.15, 0.8,
     +                    0, IERR)

      IF (NUMBINS.LE.15) THEN
         CALL XDLF_GRAPH_WIN_AX1 (IVH_GRAPH, XMIN, YMIN, 
     +                         XMAX-XMIN, NDEVX, WIDTHX, 
     +                         NDX, .015, .03, 0, IERR)
      ELSE
         I = (NUMBINS-1)/15 + 1
         AX1_DIV = I*BIN_WIDTH
         CALL XDLF_GRAPH_WIN_AX1 (IVH_GRAPH, XMIN, YMIN, 
     +                         XMAX-XMIN, NDEVX, WIDTHX, 
     +                         NDX, .015, .03, 0, IERR)
      END IF

      CALL XDLF_GRAPH_WIN_AX2 (IVH_GRAPH, XMIN, YMIN, 
     +                YMAX-YMIN, NDEVY, WIDTHY, NDY, .015, .03, 
     +                0, IERR)       
C
C====== Plot histogram boxes
C

      IF (OUTPUT.EQ.6) THEN
         CALL XDLF_GRAPH_WIN_LINE (IVH_GRAPH, XMIN, 0.0, 
     +                                        XMAX, 0.0, IERR)
      END IF
      IF (OUTPUT.EQ.2) THEN
         CALL XDLF_GRAPH_WIN_STYLE (IVH_GRAPH, 3, 1, -1, IERR)

         CALL XDLF_GRAPH_WIN_LINE (IVH_GRAPH, XMIN, 1.0, 
     +                                        XMAX, 1.0, IERR)
         CALL XDLF_GRAPH_WIN_STYLE (IVH_GRAPH, 0, 1, -1, IERR)
      END IF

      DO 100 I = 1, NUMBINS
         X1 = XMIN + (I-1)*BIN_WIDTH
         X2 = XMIN + I*BIN_WIDTH
         Y = RBIN(I)
         IF (OUTPUT.EQ.6) THEN
            CALL XDLF_GRAPH_WIN_LINE (IVH_GRAPH,X1,0.0,X1,Y,IERR)
            CALL XDLF_GRAPH_WIN_LINE (IVH_GRAPH,X1,Y,X2,Y,IERR)
            CALL XDLF_GRAPH_WIN_LINE (IVH_GRAPH,X2,Y,X2,0.0,IERR)
         ELSE
            CALL XDLF_GRAPH_WIN_LINE (IVH_GRAPH,X1,YMIN,X1,Y,IERR)
            CALL XDLF_GRAPH_WIN_LINE (IVH_GRAPH,X1,Y,X2,Y,IERR)
            CALL XDLF_GRAPH_WIN_LINE (IVH_GRAPH,X2,Y,X2,YMIN,IERR)
         END IF
100   CONTINUE

      RETURN

      END
