!+ Subroutine to set mapping between height and pressure.
!
! Method:
!	A reference profile is passed to the subroutine to 
!	the atmospheric state. This is checked to see that it
!	contains both pressure and height data. If not an error
!	is deemed to have occurrred. If it does, the reference 
!	state is set.
!
! 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 SET_STATE(IERR
     &   , N_LEVEL_PROFILE, N_COLUMN_PROFILE, I_DATA_TYPE, PROFILE
     &   , L_REFERENCE, N_LEVEL_REF, P_REF, Z_REF
     &   )
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/input_head.pcf'
      INCLUDE 'incl/dimensions_field.ucf'
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/error.pcf'
!
!     DUMMY ARGUMENTS.
      INTEGER	!, INTENT(OUT)
     &     IERR
!             ERROR FLAG
     &   , N_LEVEL_REF
!             NUMBER OF LEVELS IN REFERENCE
      INTEGER	!, INTENT(IN)
     &     N_COLUMN_PROFILE
!             NUMBER OF COLUMNS IN PROFILE
     &   , N_LEVEL_PROFILE
!             NUMBER OF LEVELS IN PROFILE
     &   , I_DATA_TYPE(NPD_DATA_COLUMN)
!             TYPES OF DATA PRESENT
      LOGICAL	!, INTENT(OUT)
     &     L_REFERENCE
!             TRUE IF REFERENCE STATE PRESENT
      REAL*8	!, INTENT(IN)
     &     PROFILE(NPD_LAYER+1, NPD_DATA_COLUMN)
!             PROPOSED REFERENCE PROFILE
      REAL*8	!, INTENT(OUT)
     &     P_REF(NPD_LAYER+1)
!             REFERENCE PRESSURE
     &   , Z_REF(NPD_LAYER+1)
!             REFERENCE HEIGHT
!
!     LOCAL VARIABLES.
      INTEGER
     &     I_COLUMN
!             COLUMN INDEX
     &   , I_COLUMN_PRESSURE
!             COLUMN CONTAINING PRESSURE
     &   , I_COLUMN_HEIGHT
!             COLUMN CONTAINING HEIGHT
     &   , I
!             LOOP VARIABLE
!
!
!     SEARCH FOR THE COLUMNS CONTAINING THE HEIGHT AND THE PRESSURE.
      I_COLUMN_PRESSURE=0
      I_COLUMN_HEIGHT=0
      I_COLUMN=0
1     I_COLUMN=I_COLUMN+1
      IF (I_DATA_TYPE(I_COLUMN).EQ.IP_PRESSURE) THEN
         I_COLUMN_PRESSURE=I_COLUMN
      ELSE IF (I_COLUMN.LT.N_COLUMN_PROFILE) THEN
         GOTO 1
      ELSE
         WRITE(IU_ERR, '(/A)') 
     &      '*** ERROR: PROPOSED REFERENCE PROFILE CONTAINS '
     &      //'NO PRESSURE DATA.'
         IERR=I_ERR_FATAL
         RETURN
      ENDIF
!
      I_COLUMN=0
2     I_COLUMN=I_COLUMN+1
      IF (I_DATA_TYPE(I_COLUMN).EQ.IP_HEIGHT) THEN
         I_COLUMN_HEIGHT=I_COLUMN
      ELSE IF (I_COLUMN.LT.N_COLUMN_PROFILE) THEN
         GOTO 2
      ELSE
         WRITE(IU_ERR, '(/A)') 
     &      '*** ERROR: PROPOSED REFERENCE PROFILE CONTAINS '
     &      //'NO HEIGHT DATA.'
         IERR=I_ERR_FATAL
         RETURN
      ENDIF
!
!     DATA ARE PRESENT: PASS THE RELEVANT VALUES 
!     INTO THE REFERENCE ARRAYS.
      L_REFERENCE=.TRUE.
      N_LEVEL_REF=N_LEVEL_PROFILE
      DO I=1, N_LEVEL_PROFILE
         P_REF(I)=PROFILE(I, I_COLUMN_PRESSURE)
         Z_REF(I)=PROFILE(I, I_COLUMN_HEIGHT)
      ENDDO
!
!
      RETURN
      END
