C***********************************************************************
C     THIS SUBROUTINE IS DESIGNED TO CALL ROUTINES TO READ IN THE DATA
C     AND GIVE THE DESCRIPTIVE OUTPUT WHICH HAS BEEN CHOSEN ON THE
C     BASIS OF THE CONTROL CARDS.  THIS ROUTINE IS USED FOR UNGROUPED
C     DATA ANALYSIS.
C
C     SUBROUTINES CALLED - READ,SKIP,SORT,CRSPLT,PRINT,HEADER,MEAN
C                          UNIVAR,HIST
C***********************************************************************
      SUBROUTINE UNGRPD
C***********************************************************************
C     DECLARATIONS
C***********************************************************************
      INCLUDE 'PARMTR.INC'
      LOGICAL BOTHD, PEST, SEST, GRP, POOL, DESC, DEF, CUTP, TRUNC, HELP
      LOGICAL LIST, PLOTS, HST, CDFPLT, WARN, SK
      REAL MESANG, DATA(MAXOBJ,5)
      INTEGER ERROR1(50), ERROR2(50), SYSIN, CNT, STATUS, FILPOS
C***********************************************************************
C     COMMON STATEMENTS
C***********************************************************************
      COMMON /MEASUR/ PD(MAXOBJ), SD(MAXOBJ), MESANG(MAXOBJ),
     1 COMANG(MAXOBJ), SINA(MAXOBJ)
      COMMON /NUM/ XL(MAXLIN), WIDTH, N, CNT, CONV(3), VARN, IDF, WARN
      COMMON /IND/ IC, II, IREP, STATUS
      COMMON /OPTION/ GRP, POOL, PEST, SEST, DESC, DEF, CUTP, TRUNC,
     1 HELP
      COMMON /DESOPT/ LIST, PLOTS, HST, CDFPLT
      COMMON /STATS/ SM(10), CORMAT(5,5)
      COMMON /INTER/ KCUT, CUT(MAXCEL), FREQ(MAXCEL), NCUT, NKC(5),
     1   NK(5), RFREQ(5,MAXCEL), CCUT(5,MAXCEL)
      COMMON /FILE/ SYSIN, SK, FILPOS
C***********************************************************************
C     EQUIVALENCE STATEMENTS
C***********************************************************************
      EQUIVALENCE (DATA(1,1),PD(1))
C***********************************************************************
C     READ IN THE DATA AND MAKE DISTANCE CONVERSIONS.  N IS THE SAMPLE
C     SIZE.
C***********************************************************************
      CALL READ (N)
      DO 10 I=1,N
         PD(I)=PD(I)*CONV(1)
   10    SD(I)=SD(I)*CONV(1)
C***********************************************************************
C     NEXT CHECK FOR ERRORS IN THE DATA. MAKE SURE THE ANGLE IS BETWEEN
C     0 AND 90 DEGREES , PERPENDICULAR DISTANCE IS GREATER THAN OR EQUAL
C     TO ZERO, SIGHTING DISTANCE IS GREATER THAN ZERO AND THAT SIGHTING
C     DISTANCE IS GREATER THAN OR EQUAL TO PERPENDICULAR DISTANCE.
C***********************************************************************
      BOTHD=.FALSE.
      IF ((STATUS.EQ.1).OR.(STATUS.EQ.4).OR.(STATUS.EQ.5)) BOTHD=.TRUE.
      IER1=0
      IER2=0
      IF (STATUS.LT.4) GO TO 30
      DO 20 I=1,N
         IF ((MESANG(I).GE.0.0).AND.(MESANG(I).LE.90.)) GO TO 20
         IER1=IER1+1
         ERROR1(IER1)=I
         IF (IER1.GT.50) GO TO 30
   20 CONTINUE
   30 IF ((.NOT.BOTHD).AND.(STATUS.NE.2)) GO TO 50
      DO 40 I=1,N
         IF ((SD(I).GT.0.0).AND.(SD(I).GE.PD(I)).AND.(PD(I).GE.0.0))
     1      GO TO 40
         IER2=IER2+1
         ERROR2(IER2)=I
         IF (IER2.GT.50) GO TO 50
   40    CONTINUE
C***********************************************************************
C     IF ERRORS OCCUR THEN SET OPTIONS FOR DATA LISTING.
C***********************************************************************
   50 IF ((IER1.EQ.0).AND.(IER2.EQ.0)) GO TO 60
      DESC=.TRUE.
      LIST=.TRUE.
      GO TO 110
C***********************************************************************
C     COMPUTE DATA WHICH HAS NOT BEEN COLLECTED AND STORE IT.
C***********************************************************************
   60 IF (STATUS.LT.4) GO TO 90
      DO 70 I=1,N
   70    SINA(I)=SIN(MESANG(I)*.017453293)
      IF (STATUS.EQ.4) GO TO 90
      IF (STATUS.NE.5) GO TO 110
      DO 80 I=1,N
   80    PD(I)=SD(I)*SINA(I)
      GO TO 110
   90 IF (.NOT.BOTHD) GO TO 110
      DO 100 I=1,N
      TEMP=PD(I)/SD(I)
      IF (STATUS.EQ.1) SINA(I)=TEMP
      TEMP=(SD(I)**2.)-(PD(I)**2.)
      IF(TEMP.NE.0.) GO TO 95
      COMANG(I)=90.
      GO TO 100
   95 TEMP=PD(I)/SQRT(TEMP)
      COMANG(I)=ATAN(TEMP)*57.29577951
      IF (COMANG(I).GT.90.) COMANG(I)=90.
  100 CONTINUE
C***********************************************************************
C     CALCULATE MEANS, STANDARD DEVIATIONS, AND STANDARD ERRORS.
C***********************************************************************
  110 CALL MEAN (N)
C***********************************************************************
C     IMPLEMENT ANY DESCRIPTIVE OPTIONS WHICH HAVE BEEN REQUESTED
C     WRITE OUT AN EXPLANATION OF THE DESCRIPTIVE OPTIONS CHOSEN.
C***********************************************************************
      IF (.NOT.DESC) GO TO 170
      CALL HEADER (1)
      WRITE (6,290)
      CALL HEADER (2)
      WRITE (6,310)
      IF (LIST) WRITE (6,320)
      IF (LIST.AND.HELP) WRITE (6,370)
      IF (.NOT.PLOTS) GO TO 120
      WRITE (6,300)
      IF (HELP) WRITE (6,380)
      IF (BOTHD.AND.HELP) WRITE (6,330)
      IF (HELP.AND.STATUS.EQ.4) WRITE (6,340)
  120 IF ((.NOT.HST).AND.(.NOT.CDFPLT)) GO TO 130
      WRITE (6,490)
      IF (HELP) WRITE (6,410)
      IF (HST) WRITE (6,350)
      IF ((HST.AND.(.NOT.CUTP)).AND.HELP) WRITE (6,390)
      IF (HST.AND.CUTP.AND.HELP) WRITE (6,400)
      IF (CDFPLT) WRITE (6,360)
      IF (CDFPLT.AND.HELP) WRITE (6,420)
C***********************************************************************
C     DATA LISTING AND SUMMARY STATISTICS
C     IF ANY ERRORS HAVE OCCURRED IN THE DATA WRITE THEM OUT AT THE END
C     OF THE LIST.
C***********************************************************************
  130 IF (.NOT.LIST) GO TO 160
      CALL HEADER (1)
      WRITE (6,430)
      CALL HEADER (2)
      CALL PRINT
      IF (STATUS.EQ.1) WRITE (6,440)
      IF (IER1.EQ.0) GO TO 140
      WRITE (6,450) (ERROR1(I),I=1,IER1)
      IF (IER1.EQ.50) WRITE (6,460)
  140 IF (IER2.EQ.0) GO TO 150
      WRITE (6,470) (ERROR2(I),I=1,IER2)
      IF (IER2.EQ.50) WRITE (6,460)
  150 IF ((IER1.EQ.0).AND.(IER2.EQ.0)) GO TO 160
      WRITE (6,480)
      IF (IC.NE.-2) CALL SKIP
      IF (IC.EQ.-2) SK=.TRUE.
      RETURN
C***********************************************************************
C     CROSSPLOTS OF VARIABLES
C***********************************************************************
  160 IF (PLOTS) CALL CRSPLT
C***********************************************************************
C     SORT THE DATA FOR FUTURE USE IN CDF PLOTS, HISTOGRAMS, ESTIMATORS
C     AND TRUNCATION OF THE PERPENDICULAR DISTANCES.
C***********************************************************************
  170 CALL SORT (PD,N)
      CALL SORT (SD,N)
      CALL SORT (MESANG,N)
      CALL SORT (COMANG,N)
      CALL SORT (SINA,N)
C***********************************************************************
C     DETERMINE THE NUMBER OF OBSERVATIONS TO BE USED FOR ESTIMATION
C     AND DESCRIPTION OF PERPENDICULAR DISTANCES IF A WIDTH HAS BEEN
C     SPECIFIED.
C***********************************************************************
      CNT=N
      IF (TRUNC) GO TO 180
      WIDTH=PD(N)
      GO TO 190
  180 IF (PD(CNT).LE.WIDTH) GO TO 190
      CNT=CNT-1
      GO TO 180
C***********************************************************************
C     IF CUT POINTS FOR PERPENDICULAR DISTANCES WERE NOT SPECIFIED THEN
C     CALCULATE CUT POINTS FOR 2*SQRT(CNT)/3,SQRT(CNT),4*SQRT(CNT)/3
C     INTERVALS. THUS NCUT=3.
C***********************************************************************
  190 IF (.NOT. CUTP) THEN
         KKCUT=MIN0((MAXCEL-1)*3/4,IFIX(SQRT(FLOAT(CNT))+.5))
         DO 210 I=1,3
            NKC(I)=MIN0(MAXCEL,KKCUT*(I+1)/3)
            IF (NKC(I).LT.1) NKC(I)=1
            DO 200 J=1,NKC(I)
  200          CCUT(I,J)=FLOAT(J)*WIDTH/FLOAT(NKC(I))
  210       CONTINUE
         NCUT=3
      ELSE
C***********************************************************************
C     IF ANY CUT POINTS ARE GREATER THAN THE WIDTH EITHER SPECIFIED OR
C     THE LAST ORDER STATISTIC THEN ONLY USE THE NUMBER OF CUT POINTS
C     SUCH THAT THE LAST CUT POINT IS LESS THAN OR EQUAL TO THE WIDTH.
C***********************************************************************
         DO 240 I=1,NCUT
            DO 230 J=1,NK(I)
               IF (CCUT(I,J).LE.WIDTH) GO TO 230
               NKC(I)=J-1
               GO TO 240
  230          CONTINUE
  240       CONTINUE
      ENDIF
C***********************************************************************
C     FOR STORED CUT POINTS CALCULATE THE NUMBERS OF OBSERVATIONS IN THE
C     INTERVALS AND STORE THEM
C***********************************************************************
      DO 280 I=1,NCUT
         KCUT=NKC(I)
         DO 260 J=1,KCUT
  260       CUT(J)=CCUT(I,J)
         CALL HIST (PD,CNT,CUT,KCUT,FREQ)
         DO 270 J=1,KCUT
  270       RFREQ(I,J)=FREQ(J)
  280    CONTINUE
C***********************************************************************
C     HISTOGRAMS AND/OR CDF PLOTS
C***********************************************************************
      IF ((HST.OR.CDFPLT).AND.DESC) CALL UNIVAR
C**********************************************************************
C     FORMAT STATEMENTS
C***********************************************************************
      RETURN
C
  290 FORMAT (///10X,60('*')/10X,'*',58X,'*'/10X,'*',
     1 21X,'Data Description',21X,'*'/10X,'*',58X,'*'/10X,60('*'))
  300 FORMAT (/3X,'Crossplots of variables'/3X,23('-'))
  310 FORMAT (//'0The data description section of the program',
     1' is designed to provide basic'/
     2' information about the data in the form of summary statistics,',
     3' crossplots'/
     4' and illustrations of the empirical distributions',
     5' of the variables,'/
     6' all of which are options in the program.  The descriptive',
     7' options (listed'/
     8' in the order of their appearance in the output) chosen are:')
  320 FORMAT (/' Data listing and summary statistics'/1X,35('-'))
  330 FORMAT (' Crossplot of sighting distance and the sine of',
     1' the angle is given.'/
     2' These variables are assumed uncorrelated in the generalized',
     3' HAYNE estimator.'/
     4' The plot facilitates visual examination of any correlation.')
  340 FORMAT (' Crossplot of computed angle and measured angle is',
     1' given.  This'/
     2' allows a quick examination for outliers to determine if there',
     3' are any measurement or entry errors.')
  350 FORMAT (/' Histograms'/1X,10('-'))
  360 FORMAT (/' Cummulative distribution function (CDF) plots'/
     1 1X,44('-'))
  370 FORMAT (' Data which have been collected and computed are',
     1' listed.  Following the'/
     2' data listing sample means, standard deviations, standard',
     3' errors and a'/
     4' correlation matrix are given.  Any errors associated with',
     5' data are'/
     6' listed at the end.  If errors occur then processing is',
     7' discontinued.')
  380 FORMAT (' Crossplots are given to facilitate examination of'/
     1' correlation between variables.')
  390 FORMAT (' Histograms (bar graphs) of all the appropriate',
     1' variables will be given.'/
     2' Three histograms with different numbers of intervals',
     3' are given for each'/
     4' variable.  The appearance of a histogram is often deceiving',
     5' depending on the'/
     6' number of intervals.  Several sets of intervals help',
     7' illustrate this problem.')
  400 FORMAT (' Histograms (bar graphs) of all the appropriate',
     1' variables will be given.'/
     2' Three histograms with different numbers of intervals',
     3' are given for each'/
     4' variable, except perpendicular distance for which the user',
     5' specified one or'/
     6' more sets of intervals.  The appearance of a histogram is',
     7' often deceiving'/
     8' depending on the number of intervals.  Several sets of',
     9' intervals help'/
     A' illustrate this problem.')
  410 FORMAT (' Histograms, discrete approximations to the shape',
     1' of the probability density'/
     2' function (PDF) and a plot of the sample cumulative',
     3' distribution function (CDF)'/
     4' are often needed to provide some insight graphically',
     5' about how a variable'/
     6' is distributed.  This is useful in understanding why tests',
     7' of the fit of a'/
     8' distribution fail in determining if there is a bias in the',
     9' data collection'/
     A' procedure (i.e., rounding errors).  The sample of measure',
     B'ments for each'/
     C' variable is sorted (order statistics) and printed out when',
     D' either histograms'/
     E' and/or CDF plots are requested.')
  420 FORMAT (' A sample CDF of each variable measured or computed',
     1' is given so they'/
     2' can be compared to theoretical distribution (i.e., uniform',
     3' CDF vs. empirical'/
     4' CDF of the sine of the angle).  A CDF is not structured',
     5' around intervals like'/
     6' histograms, so the CDF is unique.  However, they are not as',
     7' intuitively easy'/
     8' to understand as histograms.')
  430 FORMAT (//10X,60('*')/10X,'*',58X,'*'/10X,'*',12X,
     1 'Data Listing and Summary Statistics',11X,'*'/10X,'*',58X,
     2 '*'/10X,60('*'))
  440 FORMAT ('0(**) - Indicates for this data set that the sine of',
     1' the angle is'/
     2' computed as perpendicular distance/sighting distance rather',
     3' than the sine'/
     4' of the measured angle.')
  450 FORMAT ('0The following data points have errors in which the',
     1' measured angle'/
     2' is not in the interval (0,90).'/1X,25I5/1X,25I5)
  460 FORMAT ('0There may be more errors in the data.  A limit of',
     1' 50 was reached.')
  470 FORMAT ('0The following data points have errors in which the',
     1' sighting distance'/
     2' is less than or equal to zero, perpendicular distance is less',
     3' than zero, or the'/
     4' sighting distance is less than the perpendicular distance.'/
     5 1X,25I5/1X,25I5)
  480 FORMAT('0The computed angle and sine have not been calculated',
     1' because of errors in the data.'/
     2' *** Processing is discontinued so errors may be corrected.')
  490 FORMAT (/' Illustrations of Empirical Distributions'/
     1 1X,40('-'))
      END
