!+ Subroutine to read a single profile of raw data.
!
! Method:
!	The header is read to determine the contents. These are
!	read and converted to S. I. units.
!
! 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 READ_RAW_PROFILE(IERR
     &   , L_REMOVE_MISSING, MISSING_DATA_FLAG
     &   , N_LEVEL_PROFILE
     &   , N_COLUMN_PROFILE, I_DATA_GROUP, I_DATA_TYPE, PROFILE)
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADERS
      INCLUDE 'incl/def_data_in.icf'
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/gas_list.pcf'
      INCLUDE 'incl/dimensions_spec.ucf'
      INCLUDE 'incl/aerosol_component.pcf'
      INCLUDE 'incl/error.pcf'
      INCLUDE 'incl/input_head.pcf'
      INCLUDE 'incl/input_head.ncf'
      INCLUDE 'incl/dimensions_field.ucf'
!
!     DUMMY ARGUMENTS
      INTEGER	!, INTENT(OUT)
     &     IERR
!             ERROR FLAG
     &   , I_DATA_GROUP(NPD_DATA_COLUMN)
!             GROUPS OF DATA IN PROFILE
     &   , I_DATA_TYPE(NPD_DATA_COLUMN)
!             TYPES OF DATA IN PROFILE
     &   , N_COLUMN_PROFILE
!             NUMBER OF COLUMNS IN PROFILE
     &   , N_LEVEL_PROFILE
!             NUMBER OF LEVELS IN PROFILE
      LOGICAL	!, INTENT(IN)
     &     L_REMOVE_MISSING
!             LOGICAL TO REMOVE MISSING DATA
      REAL*8	!, INTENT(IN)
     &     MISSING_DATA_FLAG
!             MISSING DATA FLAG
      REAL*8	!, INTENT(OUT)
     &     PROFILE(NPD_LAYER+1, NPD_DATA_COLUMN)
!             DATA OF PROFILE
!
!     LOCAL ARGUMENTS.
      CHARACTER
     &     LINE*132
!             INPUT LINE
     &   , WORD(NPD_DATA_COLUMN)*14
!             HEADER WORDS
     &   , TYPE(NPD_DATA_COLUMN)*6
!             TYPE OF DATA
     &   , UNIT(NPD_DATA_COLUMN)*6
!             UNIT OF DATA
     &   , TAB*1
!             TAB CHARACTER
      LOGICAL
     &     L_DISCARD
!             FLAG TO DISCARD LINE
      INTEGER
     &     I
!             LOOP VARIABLE
     &   , J
!             LOOP VARIABLE
     &   , K
!             LOOP VARIABLE
     &   , N_WORD
!             NUMBER OF WORDS IN HEADER LINE
     &   , LENGTH_WORD(NPD_DATA_COLUMN)
!             LENGTH OF EACH HEADER WORD
     &   , LENGTH_TYPE
!             LENGTH OF NAME OF TYPE
     &   , LENGTH_UNIT
!             LENGTH OF NAME OF UNIT
     &   , IOS
!             I/O ERROR FLAG
      REAL*8
     &     CONVERSION_FACTOR(NPD_DATA_COLUMN)
!             CONVERSIONS TO S.I. UNITS
     &   , CONVERSION_OFFSET(NPD_DATA_COLUMN)
!             CONVERSIONS TO S.I. UNITS
!
!     INCLUDE DATA STATEMENTS.
      INCLUDE 'incl/input_head.dcf'
!
      DATA TAB/'	'/
!
!
!     INITIALIZE CHARACTER VARIABLES.
      DO J=1, LEN(LINE)
         LINE(J:J)=' '
      ENDDO
      DO I=1, NPD_DATA_COLUMN
         DO J=1, LEN(TYPE(1))
            TYPE(I)(J:J)=' '
         ENDDO
         DO J=1, LEN(UNIT(1))
            UNIT(I)(J:J)=' '
         ENDDO
      ENDDO
!
!     READ THE HEADER LINE AND SPLIT IT INTO WORDS.
      READ(IU_RAW_IN, '(A)') LINE
      J=0
      N_WORD=0
      DO I=1, NPD_DATA_COLUMN
         LENGTH_WORD(I)=0
      ENDDO
1     J=J+1
      IF ( ( (LINE(J:J).NE.' ').AND.(LINE(J:J).NE.TAB) ).AND.
     &   ( (J.EQ.1).OR.(LINE(J-1:J-1).EQ.' ')
     &     .OR.(LINE(J-1:J-1).EQ.TAB) ) ) THEN
!        A NEW WORD IS FOUND.
         N_WORD=N_WORD+1
      ENDIF
      IF ( (LINE(J:J).NE.' ').AND.(LINE(J:J).NE.TAB) ) THEN
         K=LENGTH_WORD(N_WORD)+1
         LENGTH_WORD(N_WORD)=K
         WORD(N_WORD)(K:K)=LINE(J:J)
      ENDIF
      IF (J.LT.NP_MAX_LENGTH_LINE) GOTO 1
!
      N_COLUMN_PROFILE=N_WORD
!
!     PROCESS EACH WORD TO FIND THE TYPE AND THE UNIT USED.
      DO I=1, N_WORD
         J=0
         LENGTH_TYPE=0
2        J=J+1
         IF (WORD(I)(J:J).NE.'(') THEN
            TYPE(I)(J:J)=WORD(I)(J:J)
            LENGTH_TYPE=LENGTH_TYPE+1
            GOTO 2
         ENDIF
!        NOW FIND THE UNIT.
         K=0
         LENGTH_UNIT=0
3        J=J+1
         K=K+1
         IF (WORD(I)(J:J).NE.')' ) THEN
            UNIT(I)(K:K)=WORD(I)(J:J)
            LENGTH_UNIT=LENGTH_UNIT+1
            GOTO 3
         ENDIF
!
!        CHECK THAT EACH TYPE IS VALID AND FIND THE CONVERSION FACTOR
!        TO S.I. UNITS.
         K=0
4        K=K+1
         IF (TYPE(I).EQ.HEADER_PHYS(K)) THEN
!           COLUMN CONTAINS PHYSICAL DATA.
            I_DATA_GROUP(I)=IP_PHYSICAL_DATA
            I_DATA_TYPE(I)=K
         ELSE IF (K.LT.NPD_PHYS_TYPE) THEN
            GOTO 4
         ELSE
!           THE COLUMN DOES NOT CONTAIN PHYSICAL DATA SO CHECK GASEOUS DATA.
            K=0
5           K=K+1
            IF (TYPE(I).EQ.HEADER_GAS(K)) THEN
!              COLUMN CONTAINS PHYSICAL DATA.
               I_DATA_GROUP(I)=IP_GASEOUS_DATA
               I_DATA_TYPE(I)=K
            ELSE IF (K.LT.NPD_GASES) THEN
               GOTO 5
            ELSE
!              THE COLUMN DOES NOT CONTAIN GASEOUS DATA EITHER
!              SO WE CHECK AEROSOL DATA.
               K=0
6              K=K+1
               IF (TYPE(I).EQ.HEADER_AEROSOL(K)) THEN
!                 COLUMN CONTAINS PHYSICAL DATA.
                  I_DATA_GROUP(I)=IP_AEROSOL_DATA
                  I_DATA_TYPE(I)=K
               ELSE IF (K.LT.NPD_AEROSOL_COMPONENT) THEN
                  GOTO 6
               ELSE
                  WRITE(IU_ERR, '(/, 2A)') 
     &               '*** ERROR: UNDEFINED TYPE: ', TYPE(I)
                  IERR=I_ERR_FATAL
                  RETURN
               ENDIF
            ENDIF
         ENDIF
!
!        SET THE CONVERSION FACTOR AND OFFSET
         K=0
7        K=K+1
         IF (UNIT(I).EQ.NAME_UNIT(K)) THEN
            CONVERSION_FACTOR(I)=FACTOR_UNIT(K)
            CONVERSION_OFFSET(I)=OFFSET_UNIT(K)
         ELSE IF (K.LT.NPD_UNIT) THEN
            GOTO 7
         ELSE
            WRITE(IU_ERR, '(/, 2A)') 
     &         '*** ERROR: UNKNOWN UNIT: ', UNIT(I)
            IERR=I_ERR_FATAL
            RETURN
         ENDIF
!
      ENDDO
!
!     READ IN THE PROFILE, CONVERTING THE VALUES TO S.I: UNITS.
      J=0
8     READ(IU_RAW_IN, '(A)') LINE
      IF (LINE(1:4).EQ.'*END') THEN
         N_LEVEL_PROFILE=J
         RETURN
      ELSE IF (J.LT.NPD_LAYER+1) THEN
         J=J+1
         BACKSPACE(IU_RAW_IN)
         READ(IU_RAW_IN, *, IOSTAT=IOS) (PROFILE(J, I), I=1, N_WORD)
         IF (IOS.NE.0) THEN
            WRITE(IU_ERR, '(/A)')
     &         '*** ERROR: MISSING *END DIRECTIVE.'
            IERR=I_ERR_FATAL
            RETURN
         ENDIF
!
!        CHECK FOR MISSING DATA AND SKIP THIS LINE IF IT IS FOUND AND
!        TO BE REMOVED.
         IF (L_REMOVE_MISSING) THEN
            L_DISCARD=.FALSE.
            DO I=1, N_WORD
               IF (ABS(PROFILE(J, I)-MISSING_DATA_FLAG).LT.
     &            1.0D-02*ABS(MISSING_DATA_FLAG)) L_DISCARD=.TRUE.
            ENDDO
            IF (L_DISCARD) THEN
               J=J-1
               GOTO 8
            ENDIF
         ENDIF
!        CARRY OUT ANY REQUIRED CONVERSIONS.
         DO I=1, N_WORD
            PROFILE(J, I)=PROFILE(J, I)*CONVERSION_FACTOR(I)
     &         +CONVERSION_OFFSET(I)
         ENDDO
         GOTO 8
      ELSE
         WRITE(IU_ERR, '(/A)')
     &      '*** ERROR: PROFILE IS TOO LARGE: INCREASE NPD_LAYER AND '
     &      //'RECOMPILE.'
         IERR=I_ERR_FATAL
         RETURN
      ENDIF
!
!
      END
