!+ Subroutine to sort the elements of a profile in increasing pressure.
!
! Method:
!	The profile is searched to find the pressure or height on
!	which to sort. A shell sorting algorithm is called to find
!	the correct order. Using the key thus defined the columns
!	os the profile are re-ordered.
!
! Current Owner of Code: J. M. Edwards
!
! History:
!	Version		Date			Comment
!	1.0		12-04-95		First Version under RCS
!						(J. M. Edwards)
!
! Description of Code:
!   FORTRAN 77  with extensions listed in documentation.
!
!- ---------------------------------------------------------------------
      SUBROUTINE SORT_RAW_PROFILE(IERR
     &   , N_LEVEL_PROFILE, N_COLUMN_PROFILE, I_DATA_TYPE, I_DATA_GROUP
     &   , PROFILE
     &   )
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/dimensions_field.ucf'
      INCLUDE 'incl/input_head.pcf'
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/error.pcf'
!
!     DUMMY ARGUMENTS.
      INTEGER	!, INTENT(OUT)
     &     IERR
!             ERROR FLAG
      INTEGER	!, INTENT(IN)
     &     N_LEVEL_PROFILE
!             NUMBER OF LEVELS
     &   , N_COLUMN_PROFILE
!             NUMBER OF COLUMNS
     &   , I_DATA_TYPE(NPD_DATA_COLUMN)
!             TYPES OF DATA IN COLUMNS
     &   , I_DATA_GROUP(NPD_DATA_COLUMN)
!             GROUPS OF TYPES OF DATA IN COLUMNS
      REAL*8	!, INTENT(INOUT)
     &     PROFILE(NPD_LAYER+1, NPD_DATA_COLUMN)
!             PROFILE OF DATA
!
!     LOCAL VARIABLES.
      INTEGER
     &     I_COLUMN
!             COLUMN BEING SEARCHED
     &   , I_COLUMN_PRESSURE
!             COLUMN CONTAINING PRESSURE
     &   , I_COLUMN_HEIGHT
!             COLUMN CONTAINING HEIGHT
     &   , INDEX_LEVEL(NPD_LAYER+1)
!             INDEX FOR SORTING
     &   , I
!             LOOP VARIABLE
     &   , J
!             LOOP VARIABLE
      REAL*8
     &     SORT_KEY(NPD_LAYER+1)
!             SORTING KEY
     &   , X(NPD_LAYER+1)
!             TEMPORARY VARIABLE
!
!     SUBROUTINES CALLED:
      EXTERNAL
     &     SHELL_SORT
!
!
!
!     PROCEED THROUGH THE PROFILE TO FIND THE PRESSURE OR THE HEIGHT.
      I_COLUMN_PRESSURE=0
      I_COLUMN=0
1     I_COLUMN=I_COLUMN+1
      IF ( (I_DATA_TYPE(I_COLUMN).EQ.IP_PRESSURE).AND.
     &     (I_DATA_GROUP(I_COLUMN).EQ.IP_PHYSICAL_DATA) ) THEN
         I_COLUMN_PRESSURE=I_COLUMN
!        THE SORTING KEY IS THE PRESSURE.
         DO I=1, N_LEVEL_PROFILE
            SORT_KEY(I)=PROFILE(I, I_COLUMN_PRESSURE)
            INDEX_LEVEL(I)=I
         ENDDO
      ELSE IF (I_COLUMN.LT.N_COLUMN_PROFILE) THEN
         GOTO 1
      ENDIF
!
      IF (I_COLUMN_PRESSURE.EQ.0) THEN
!        THERE IS NO PRESSURE IN THIS PROFILE: SEARCH FOR THE HEIGHT.
         I_COLUMN_HEIGHT=0
         I_COLUMN=0
2        I_COLUMN=I_COLUMN+1
         IF ( (I_DATA_TYPE(I_COLUMN).EQ.IP_HEIGHT).AND.
     &        (I_DATA_GROUP(I_COLUMN).EQ.IP_PHYSICAL_DATA) ) THEN
            I_COLUMN_HEIGHT=I_COLUMN
            DO I=1, N_LEVEL_PROFILE
               SORT_KEY(I)=-PROFILE(I, I_COLUMN_HEIGHT)
               INDEX_LEVEL(I)=I
            ENDDO
         ELSE IF (I_COLUMN.LT.N_COLUMN_PROFILE) THEN
            GOTO 2
         ELSE
            WRITE(IU_ERR, '(/A)') 
     &         '*** ERROR: THIS PROFILE CONTAINS NEITHER '
     &         //'PRESSURE NOR HEIGHT DATA.'
            IERR=I_ERR_FATAL
            RETURN
         ENDIF
      ENDIF
!
!     SORT THE ARRAY INDEX_LEVEL IN ORDER OF ASCENDING VALUE OF SORT_KEY.
      CALL SHELL_SORT(N_LEVEL_PROFILE, INDEX_LEVEL, SORT_KEY)
!
!     PASS THROUGH THE PROFILE RE-ORDERING EACH COLUMN AS REQUIRED BY
!     THE INDEX.
      DO J=1, N_COLUMN_PROFILE
         DO I=1, N_LEVEL_PROFILE
            X(I)=PROFILE(I, J)
         ENDDO
         DO I=1, N_LEVEL_PROFILE
            PROFILE(I, J)=X(INDEX_LEVEL(I))
         ENDDO
      ENDDO
!
!
      RETURN
      END
