      SUBROUTINE NSSORT(NSPOTS,NLINES,NDATEL,DATBUF,SPTARR,LINARR,
     1                  CHAN,NUMFOV)
C
C*********************************************************************
C
C   NAME- NSSORT
C
C   LANGUAGE- FORTRAN 77      TYPE- SUBROUTINE      COMPUTER- CRAY   
C
C   VERSION- 1.0    DATE- 04/01/92   PROGRAMMER- JEFFREY R. JONES(SMSRC)
C   VERSION- 3.0    DATE- 05/06/96   PROGRAMMER- ALEX POZDNYAK (FDC)     
C      FOR REHOSTING TO THE CRAY, CHANGED ALL I*4 DECLARATIONS TO
C      INTEGER AND ALL R*4 DECLARATIONS TO REAL.
C
C   FUNCTION- THIS ROUTINE SORTS A SUBSET OF VALUES IN THE
C             DATA BUFFER.  THE SPOT ARRAY AND LINE ARRAY CORRESPOND
C             TO THE SUBSET OF FIELD OF VIEW'S SPOT NUMBER AND LINE
C             NUMBER RESPECTIVLY.  THE CHANNEL IS THE VALUE THAT THE
C             SUBSET IS TO BE SORTED ON.  THE SORT USED IN THIS ROUTINE
C             IS THE HEAPSORT.
C
C             REFERENCES:
C
C             KNUTH, DONALD E., 1973, SORTING AND SEARCHING, VOL. 3 OF
C             THE ART OF COMPUTER PROGRAMMING (READING, MASS.:
C             ADDISON-WESLEY) PP. 145-149, 153-158
C
C             PRESS, WILLIAM H., FLANNERY, BRIAN P., TEUKOLSKY, SAUL A.,
C             VETTERLING, WILLIAM T. 1986,NUMERICAL RECIPES THE ART OF
C             SCIENTIFIC PROGRAMMING (NEW YORK: CAMBRIDGE UNIVERSITY
C             PRESS) PP. 229-232
C
C   CALLING SEQUENCE PARAMETERS-
C
C     ARGUMENT  TYPE  I/O                   DESCRIPTION
C     --------  ----  ---  ------------------------------------------
C     NSPOTS    INT    I   NUMBER OF SPOTS IN DATBUF.
C
C     NLINES    INT    I   NUMBER OF SCAN LINES IN DATBUF.
C
C     NDATEL    INT    I   NUMBER OF DATA ELEMENTS IN DATBUF.
C
C     DATBUF    REAL   I   EXTENDED DATA BUFFER DIMENSIONED
C                          (NSPOTS,NLINES,NDATEL)
C
C     SPTARR    INT   I/O  THE SPOT NUMBERS OF THE FIELDS OF VIEW TO BE
C                          SORTED.
C
C     LINARR    INT   I/O  THE LINE NUMBERS OF THE FIELDS OF VIEW TO BE
C                          SORTED.
C
C     CHAN      INT    I   THE CHANNEL BY WHICH THE SORT IS TO BE
C                          PERFORMED.
C
C     NUMFOV    INT    I   THE NUMBER OF FIELDS OF VIEW TO BE SORTED.
C
C   LOCAL PARAMETERS-
C
C     ARGUMENT  TYPE  I/O               DESCRIPTION
C     --------  ----  ---  --------------------------------------------
C     IINDEX    INT   N/A  INDEX USED IN SIFTING THE FIELD OF VIEW
C                          SPECIFIED IN TEMLIN AND TEMSPT TO ITS PROPER
C                          LEVEL IN THE HEAP.
C
C     JINDEX    INT   N/A  INDEX USED IN SIFTING THE FIELD OF VIEW
C                          SPECIFIED IN TEMLIN AND TEMSPT TO ITS PROPER
C                          LEVEL IN THE HEAP.
C
C     LINDEX    INT   N/A  INDEX USED IN THE HEAP CREATION PHASE.
C
C     RINDEX    INT   N/A  INDEX USED IN THE HEAP SELECTION PHASE.
C
C     TEMLIN    INT   N/A  TEMPORARY STORAGE FOR SCAN LINE NUMBERS
C                          DURING ORDER CHANGING.
C
C     TEMSPT    INT   N/A  TEMPORARY STORAGE FOR SPOT NUMBERS DURING
C                          ORDER CHANGING.
C
C**********************************************************************
C
      INTEGER    NSPOTS,NLINES,NDATEL,NUMFOV,
     1           SPTARR(NUMFOV),LINARR(NUMFOV),CHAN
      REAL       DATBUF(NSPOTS,NLINES,NDATEL)
C
      INTEGER    IINDEX,JINDEX,LINDEX,RINDEX,TEMLIN,TEMSPT
C
C   MAKE SURE MORE THAN ONE FIELD OF VIEW NEEDS TO BE SORTED.
C
      IF (NUMFOV .GT. 1) THEN
C
C      INITIALIZE HEAP INDICES.
C
         LINDEX = (NUMFOV/2) + 1
         RINDEX = NUMFOV
C
   10    IF (LINDEX .GT. 1) THEN
C
C          STILL IN HEAP CREATION PHASE.
C
             LINDEX = LINDEX - 1
C
C          STORE SCAN LINE AND SPOT NUMBERS OF ELEMENT LINDEX IN
C          TEMPORARY STORAGE VARIABLES.
C
             TEMLIN = LINARR(LINDEX)
             TEMSPT = SPTARR(LINDEX)
C
         ELSE
C
C          IN HEAP SELECTION PHASE.
C
C          STORE SCAN LINE AND SPOT NUMBERS OF ELEMENT RINDEX IN
C          TEMPORARY STORAGE VARIABLES.
C
             TEMLIN = LINARR(RINDEX)
             TEMSPT = SPTARR(RINDEX)
C
C          MOVE THE TOP OF THE HEAP TO THE END OF THE ARRAY.
C
             LINARR(RINDEX) = LINARR(1)
             SPTARR(RINDEX) = SPTARR(1)
C
             RINDEX = RINDEX - 1
C
             IF (RINDEX .EQ. 1) THEN
C
                 LINARR(1) = TEMLIN
                 SPTARR(1) = TEMSPT
C
C              SORT COMPLETED.
C
                 RETURN
C
             END IF
C
         END IF
C
C      INITIALIZE SIFTING INDICES.
C
         IINDEX = LINDEX
         JINDEX = LINDEX * 2
C
C      MOVE FIELD OF VIEW SPECIFIED BY TEMLIN AND TEMSPT TO ITS PROPER
C      LEVEL IN THE HEAP.
C
   20    IF (JINDEX .LE. RINDEX) THEN
C
             IF (JINDEX .LT. RINDEX) THEN
C
                 IF (DATBUF(SPTARR(JINDEX),LINARR(JINDEX),CHAN) .LT.
     1             DATBUF(SPTARR(JINDEX+1),LINARR(JINDEX+1),CHAN)) THEN
C
                     JINDEX = JINDEX + 1
C
                 END IF
C
             END IF
C
             IF (DATBUF(TEMSPT,TEMLIN,CHAN) .LT.
     1           DATBUF(SPTARR(JINDEX),LINARR(JINDEX),CHAN)) THEN
C
C              DEMOTE FIELD OF VIEW SPECIFIED BY TEMLIN AND TEMSPT.
C
                 LINARR(IINDEX) = LINARR(JINDEX)
                 SPTARR(IINDEX) = SPTARR(JINDEX)
                 IINDEX = JINDEX
                 JINDEX = JINDEX * 2
C
             ELSE
C
C              THIS IS THE CORRECT LEVEL FOR THE FIELD OF VIEW
C              SPECIFIED BY TEMLIN AND TEMSPT.  SET J TO TERMINATE
C              SIFTING.
C
                 JINDEX = RINDEX + 1
C
             END IF
C
         GOTO 20
         END IF
C
C      PUT THE FIELD OF VIEW SPECIFIED BY TEMLIN AND TEMSPT IN ITS
C      SLOT.
C
         LINARR(IINDEX) = TEMLIN
         SPTARR(IINDEX) = TEMSPT
C
         GOTO 10
C
      END IF
C
      END
