!*******************************************************************************
!$RCSfile$
!$Revision$
!$Date$
!*******************************************************************************
INTEGER FUNCTION ParseDataSO()

USE SCIPUFFdriver_fi
USE constants_fd

IMPLICIT NONE

INTEGER ios, irv, i, j
INTEGER alloc_stat
LOGICAL lexist

CHARACTER(128), EXTERNAL :: BuildFileNameAERMOD

INTEGER, EXTERNAL :: ReallocRelList, FindRelName, FindMatlName, WritePrimeFile

ParseDataSO = FAILURE

SELECT CASE( TRIM(carg(ikwrd)) )

  CASE( 'ELEVUNIT' )

    IF( narg > ikwrd )THEN
      IF( TRIM(carg(ikwrd+1) ) == 'FEET' )elevCnv = 0.3048
    END IF

  CASE( 'LOCATION' )

    IF( narg-ikwrd < 4 )THEN
      WRITE(*,'(A)') 'Insufficient SO LOCATION input'
      GOTO 9999
    END IF

    i = new%scnHead%number

    i = i + 1
    IF( i > new%scnHead%max )THEN
      irv = ReallocRelList( 10 )  !Increment by 10
      IF( irv /= SUCCESS )GOTO 9999
    END IF

    new%scnHead%number = i

    relList(i)%relName    = TRIM(carg(ikwrd+1))
    relList(i)%relDisplay = ' '
    relList(i)%status     = 1                   !HS_VALID

    SELECT CASE( TRIM(carg(ikwrd+2)) )
      CASE( 'POINT','STACK' )
        relList(i)%type = HR_STACK
        CALL InitRelStack()

      CASE( 'SCICONT' )
        relList(i)%type = HR_CONT
        CALL InitRelCont()

      CASE( 'SCIINST' )
        relList(i)%type = HR_INST
        CALL InitRelInst()

      CASE( 'VOLUME' )
        relList(i)%type = HR_VOL
        CALL InitRelCont()

      CASE( 'AREA' )
        relList(i)%type = HR_AREA
        CALL InitRelStack()

      CASE( 'AREACIRC' )
        relList(i)%type = HR_AREAC
        CALL InitRelStack()

      CASE( 'AREAPOLY' )
        relList(i)%type = -9999

      CASE DEFAULT
        WRITE(*,'(A)') 'Invalid source type for SCIPUFF'
        GOTO 9999

    END SELECT

    SrcFlag(i) = relList(i)%type  !Save original source type (only relevant for Volume source currently)

    READ(carg(ikwrd+3),*,IOSTAT=ios) relList(i)%xRel
    IF( ios /= 0 )THEN
      WRITE(*,'(A)') 'Error reading SO Xs'
      GOTO 9999
    END IF

    READ(carg(ikwrd+4),*,IOSTAT=ios) relList(i)%yRel
    IF( ios /= 0 )THEN
      WRITE(*,'(A)') 'Error reading SO Ys'
      GOTO 9999
    END IF

    relList(i)%xRel = relList(i)%xRel*xfac
    relList(i)%yRel = relList(i)%yRel*xfac - yoff

    IF( narg-ikwrd > 4 )THEN
      READ(carg(ikwrd+5),*,IOSTAT=ios) relList(i)%zRel !**** not sure if should use this
      IF( ios /= 0 )THEN
        WRITE(*,'(A)') 'Error reading SO Zs'
        GOTO 9999
      END IF
    ELSE
      relList(i)%zRel = 0.
    END IF

    relList(i)%zRel = 0. !*****
    relList(i)%tRel = 0. !*****

    relList(i)%material = TRIM(mtlList(1)%name) !Default to first material

  CASE( 'MATERIAL' )

    IF( narg-ikwrd < 2 )THEN
      WRITE(*,'(A)') 'Insufficient SO MATERIAL input'
      GOTO 9999
    END IF

    i = FindRelName( carg(ikwrd+1) )
    IF( i == 0 )THEN
      WRITE(*,'(A)') 'Invalid SO MATERIAL source id'
      GOTO 9999
    END IF

    j = FindMatlName(TRIM(carg(ikwrd+2)))
    IF( j < 1 )THEN
      WRITE(*,'(A)') 'Invalid SO MATERIAL material id'
      GOTO 9999
    END IF

    relList(i)%material = TRIM(mtlList(j)%name)

  CASE( 'MULTCOMP' )

    IF( new%scnHead%number == 0 )THEN
      WRITE(*,'(A)') 'SO MULTCOMP must follow all SO LOCATION inputs'
      GOTO 9999
    END IF

    nMC = narg - ikwrd

    IF( nMC == 0 )THEN
      WRITE(*,*) 'No specie names specified with SO MULTCOMP keyword'
      GOTO 9999
    END IF

    IF( nMC > MAX_MCR )THEN
      WRITE(*,*) 'Too many MC source species in SO MULTCOMP: ',nMC
      WRITE(*,*) 'Maximum is ',MAX_MCR
      GOTO 9999
    END IF

    IF( ALLOCATED(MCrate) )DEALLOCATE(MCrate,STAT=alloc_stat )

    ALLOCATE( MCname(nMC),MCrate(nMC,new%scnHead%number),STAT=alloc_stat )
    IF( alloc_stat /= 0 )THEN
      WRITE(*,'(A)') 'Error allocating arrays for SO MULTCOMP specie names and rates'
      GOTO 9999
    END IF

    DO i = 1,nMC
       MCname(i) = TRIM(carg(ikwrd+i))
    END DO

    MCrate = 0.

  CASE( 'SRCDURAT','SRCDUR','SRCDURTN' )

    IF( narg-ikwrd < 2 )THEN
      WRITE(*,'(A)') 'Insufficient SO SRCDURAT input'
      GOTO 9999
    END IF

    i = FindRelName( carg(ikwrd+1) )
    IF( i == 0 )THEN
      WRITE(*,'(A)') 'Invalid SO SRCDURAT source id'
      GOTO 9999
    END IF

!**** This must be specified before SRCPARAM *****
    READ(carg(ikwrd+2),*,IOSTAT=ios) relStackData%duration
    relStackData%duration = relStackData%duration*3600.  !Convert to seconds

  CASE( 'SRCPARAM' )

    IF( narg-ikwrd < 2 )THEN
      WRITE(*,'(A)') 'Insufficient SO SRCPARAM input'
      GOTO 9999
    END IF

    i = FindRelName( carg(ikwrd+1) )
    IF( i == 0 )THEN
      WRITE(*,'(A)') 'Invalid SO SRCPARAM source id'
      GOTO 9999
    END IF

    SELECT CASE(  relList(i)%type )

      CASE( HR_STACK,HR_PRIME )

        IF( narg-ikwrd < 6 )THEN
          WRITE(*,'(A)') 'Insufficient SO SRCPARAM POINT input'
          GOTO 9999
        END IF

        READ(carg(ikwrd+2),*,IOSTAT=ios) relStackData%rate
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO POINT emission rate'
          GOTO 9999
        END IF

        READ(carg(ikwrd+3),*,IOSTAT=ios) relList(i)%zRel
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO POINT height'
          GOTO 9999
        END IF

        READ(carg(ikwrd+4),*,IOSTAT=ios) relStackData%exitTemp
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO POINT exit temperature'
          GOTO 9999
        END IF

        READ(carg(ikwrd+5),*,IOSTAT=ios) relStackData%exitVel
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO POINT exit velocity'
          GOTO 9999
        END IF

        READ(carg(ikwrd+6),*,IOSTAT=ios) relStackData%diameter
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO POINT stack diameter'
          GOTO 9999
        END IF

        relStackData%rate       = relStackData%rate * 1.E-3       !Convert to g/s
        relStackData%exitTemp   = relStackData%exitTemp + ABSZERO !Convert to deg-C
        relStackData%activeFrac = 1.0

        IF( relStackData%duration == NOT_SET_R )relStackData%duration = 10.*365.*24.

        relList(i)%relData = TRANSFER(relStackData,relList(i)%relData)

        IF( nMC > 0 )THEN
          IF( narg-ikwrd >= 6+nMC )THEN
            DO j = 1,nMC
              READ(carg(ikwrd+6+j),*,IOSTAT=ios) MCrate(j,i)
              IF( ios /= 0 )THEN
                WRITE(*,'(A)') 'Error reading SO POINT multicomponent specie rate'
                WRITE(*,*) 'Source ',TRIM(carg(ikwrd+1)),' Specie ',MCname(j)
                GOTO 9999
              END IF
            END DO
          ELSE
            DO j = 1,nMC
              CALL get_next_data( lun,line,nch,kwrd,narg,carg,MAXN,lerr )
              IF( lerr )THEN
                WRITE(*,'(A)') 'Error reading multicomponent input following SO SRCPARAM POINT line'
                WRITE(*,'(A)') 'Specie '//MCname(j)
                GOTO 9999
              ELSE IF( narg < 2 )THEN
                WRITE(*,'(A)') 'Invalid multicomponent input following SO SRCPARAM POINT line'
                WRITE(*,'(A)') 'Specie '//MCname(j)
                GOTO 9999
              ELSE IF( TRIM(carg(1)) /= TRIM(MCname(j)) )THEN
                WRITE(*,'(A)') 'Wrong multicomponent specie: ',TRIM(carg(1))
                WRITE(*,'(A)') 'Expecting '//TRIM(MCname(j))
                GOTO 9999
              END IF
              READ(carg(2),*,IOSTAT=ios) MCrate(j,i)
              IF( ios /= 0 )THEN
                WRITE(*,'(A)') 'Error reading multicomponent input following SO SRCPARAM POINT line'
                GOTO 9999
              END IF
            END DO
          END IF
        END IF

      CASE( HR_AREA ) !ARERMOD area source; use stack structure to store area parameters

        IF( narg-ikwrd < 6 )THEN
          WRITE(*,'(A)') 'Insufficient SO SRCPARAM AREA input'
          GOTO 9999
        END IF

        READ(carg(ikwrd+2),*,IOSTAT=ios) relStackData%rate !per unit area
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO AREA emission rate'
          GOTO 9999
        END IF

        READ(carg(ikwrd+3),*,IOSTAT=ios) relList(i)%zRel
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO AREA height'
          GOTO 9999
        END IF

        READ(carg(ikwrd+4),*,IOSTAT=ios) relStackData%exitTemp !X-length
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO AREA X-length'
          GOTO 9999
        END IF

        READ(carg(ikwrd+5),*,IOSTAT=ios) relStackData%exitVel  !Y-length
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO AREA Y-length'
          GOTO 9999
        END IF

        READ(carg(ikwrd+6),*,IOSTAT=ios) relStackData%diameter !Rotation angle
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO AREA angle'
          GOTO 9999
        END IF

        IF( narg-ikwrd > 6 )THEN
          READ(carg(ikwrd+7),*,IOSTAT=ios) relStackData%sigma  !Sigma-z ** Not Used **
          IF( ios /= 0 )THEN
            WRITE(*,'(A)') 'Error reading SO AREA sigma-z'
            GOTO 9999
          END IF
        END IF

        relStackData%rate = relStackData%rate * 1.E-3       !Convert to g/s
        relStackData%activeFrac = 1.0

        IF( relStackData%duration == NOT_SET_R )relStackData%duration = 10.*365.*24.

        relList(i)%relData = TRANSFER(relStackData,relList(i)%relData)

        relList(i)%type = -HR_STACK  !Set type for later processing

      CASE( HR_AREAC ) !ARERMOD circular area source; use stack structure to store area parameters

        IF( narg-ikwrd < 4 )THEN
          WRITE(*,'(A)') 'Insufficient SO SRCPARAM AREACIRC input'
          GOTO 9999
        END IF

        READ(carg(ikwrd+2),*,IOSTAT=ios) relStackData%rate !per unit area
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO AREACIRC emission rate'
          GOTO 9999
        END IF

        READ(carg(ikwrd+3),*,IOSTAT=ios) relList(i)%zRel
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO AREACIRC height'
          GOTO 9999
        END IF

        READ(carg(ikwrd+4),*,IOSTAT=ios) relStackData%exitTemp !Radius
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO AREACIRC radius'
          GOTO 9999
        END IF

        IF( narg-ikwrd > 4 )THEN
          READ(carg(ikwrd+5),*,IOSTAT=ios) relStackData%sigma  !Sigma-z ** Not Used **
          IF( ios /= 0 )THEN
            WRITE(*,'(A)') 'Error reading SO AREA sigma-z'
            GOTO 9999
          END IF
        END IF

        relStackData%rate = relStackData%rate * 1.E-3       !Convert to g/s

!------ Set parameters for equivalent rectangle area source

        relStackData%exitTemp = SQRTPI*relStackData%exitTemp  !X-length
        relStackData%exitVel  = relStackData%exitTemp         !Y-length
        relStackData%diameter = 0.                            !Rotation
        relStackData%activeFrac = 1.0

        IF( relStackData%duration == NOT_SET_R )relStackData%duration = 10.*365.*24.

        relList(i)%relData = TRANSFER(relStackData,relList(i)%relData)

        relList(i)%type = -HR_STACK  !Set type for later processing

      CASE( HR_INST )

        IF( narg-ikwrd < 6 )THEN
          WRITE(*,'(A)') 'Insufficient SO SRCPARAM SCIINT input'
          GOTO 9999
        END IF

        READ(carg(ikwrd+2),*,IOSTAT=ios) relInstData%mass
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO SCIINT mass'
          GOTO 9999
        END IF

        READ(carg(ikwrd+3),*,IOSTAT=ios) relList(i)%zRel
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO SCIINT height'
          GOTO 9999
        END IF

        READ(carg(ikwrd+4),*,IOSTAT=ios) relInstData%buoyancy
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO SCIINT buoyancy'
          GOTO 9999
        END IF

        READ(carg(ikwrd+5),*,IOSTAT=ios) relInstData%momentum
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO SCIINT vertical momentum'
          GOTO 9999
        END IF

        READ(carg(ikwrd+6),*,IOSTAT=ios) relInstData%sigX
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO SCIINT X-spread'
          GOTO 9999
        END IF

        IF( narg > ikwrd+7 )THEN

          READ(carg(ikwrd+7),*,IOSTAT=ios) relInstData%sigY
          IF( ios /= 0 )THEN
            WRITE(*,'(A)') 'Error reading SO SCIINT Y-spread'
            GOTO 9999
          END IF

          READ(carg(ikwrd+8),*,IOSTAT=ios) relInstData%sigZ
          IF( ios /= 0 )THEN
            WRITE(*,'(A)') 'Error reading SO SCIINT vertical spread'
            GOTO 9999
          END IF

        ELSE

          relInstData%sigy = relInstData%sigX
          relInstData%sigZ = relInstData%sigX

        END IF

        relInstData%activeFrac = 1.0

        relList(i)%relData = TRANSFER(relInstData,relList(i)%relData)

      CASE( HR_CONT )

        IF( narg-ikwrd < 6 )THEN
          WRITE(*,'(A)') 'Insufficient SO SRCPARAM SCICONT input'
          GOTO 9999
        END IF

        READ(carg(ikwrd+2),*,IOSTAT=ios) relContData%rate
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO SCICONT emission rate'
          GOTO 9999
        END IF

        READ(carg(ikwrd+3),*,IOSTAT=ios) relList(i)%zRel
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO SCICONT height'
          GOTO 9999
        END IF

        READ(carg(ikwrd+4),*,IOSTAT=ios) relContData%buoyancy
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO SCICONT buoyancy'
          GOTO 9999
        END IF

        READ(carg(ikwrd+5),*,IOSTAT=ios) relContData%momentum
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO SCICONT vertical momentum'
          GOTO 9999
        END IF

        READ(carg(ikwrd+6),*,IOSTAT=ios) relContData%sigY
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO SCICONT lateral spread'
          GOTO 9999
        END IF

        IF( narg > ikwrd+6 )THEN
          READ(carg(ikwrd+7),*,IOSTAT=ios) relContData%sigZ
          IF( ios /= 0 )THEN
            WRITE(*,'(A)') 'Error reading SO SCICONT vertical spread'
            GOTO 9999
          END IF
        ELSE
          relContData%sigZ = relContData%sigY
        END IF

        IF( relContData%duration == NOT_SET_R )relContData%duration = 10.*365.*24.

        relContData%activeFrac = 1.0

        relList(i)%relData = TRANSFER(relContData,relList(i)%relData)

      CASE( HR_VOL )  !AERMOD volume source

        IF( narg-ikwrd < 5 )THEN
          WRITE(*,'(A)') 'Insufficient SO VOLUME input'
          GOTO 9999
        END IF

        READ(carg(ikwrd+2),*,IOSTAT=ios) relContData%rate
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO VOLUME emission rate'
          GOTO 9999
        END IF

        READ(carg(ikwrd+3),*,IOSTAT=ios) relList(i)%zRel
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO VOLUME height'
          GOTO 9999
        END IF

        READ(carg(ikwrd+4),*,IOSTAT=ios) relContData%sigY
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO VOLUME lateral spread'
          GOTO 9999
        END IF

        READ(carg(ikwrd+5),*,IOSTAT=ios) relContData%sigZ
        IF( ios /= 0 )THEN
          WRITE(*,'(A)') 'Error reading SO VOLUME vertical spread'
          GOTO 9999
        END IF

        relContData%rate = relContData%rate * 1.E-3       !Convert to g/s

        relContData%buoyancy = 0.  !AERMOD volumes sources are passive
        relContData%momentum = 0.
        relContData%activeFrac = 1.0

        IF( relContData%duration == NOT_SET_R )relContData%duration = 10.*365.*24.

        relList(i)%relData = TRANSFER(relContData,relList(i)%relData)

        relList(i)%type = HR_CONT  !Set to standard SCIPUFF continuous release type

      CASE DEFAULT
        WRITE(*,'(A)') 'Invalid source'
        WRITE(*,*) 'Only POINT, AREA, AREAC, VOLUME, SCICONT and SCIINST recognized'
        GOTO 9999

    END SELECT

  CASE( 'BUILDHGT' )
    IF( narg-ikwrd < 2 )THEN
      WRITE(*,'(A)') 'Insufficient SO BUILDHGT input'
      GOTO 9999
    END IF

    i = WritePrimeFile()
    IF( i < SUCCESS )GOTO 9999

  CASE( 'BUILDLEN' )
    IF( narg-ikwrd < 2 )THEN
      WRITE(*,'(A)') 'Insufficient SO BUILDLEN input'
      GOTO 9999
    END IF

    i = WritePrimeFile()
    IF( i < SUCCESS )GOTO 9999

  CASE( 'BUILDWID' )
    IF( narg-ikwrd < 2 )THEN
      WRITE(*,'(A)') 'Insufficient SO BUILDWID input'
      GOTO 9999
    END IF

    i = WritePrimeFile()
    IF( i < SUCCESS )GOTO 9999

  CASE( 'XBADJ'    )
    IF( narg-ikwrd < 2 )THEN
      WRITE(*,'(A)') 'Insufficient SO XBADJ input'
      GOTO 9999
    END IF

    i = WritePrimeFile()
    IF( i < SUCCESS )GOTO 9999

  CASE( 'YBADJ'    )
    IF( narg-ikwrd < 2 )THEN
      WRITE(*,'(A)') 'Insufficient SO YBADJ input'
      GOTO 9999
    END IF

    i = WritePrimeFile()
    IF( i < SUCCESS )GOTO 9999

!  CASE( 'AREAVERT' )
    !Ignore

!  CASE( 'URBANSRC' )
    !Ignore

!  CASE( 'EMISFACT' )
    !Ignore

!  CASE( 'EMISUNIT' )
    !Ignore

!  CASE( 'CONCUNIT' )
    !Ignore

!  CASE( 'DEPOUNIT' )
    !Ignore

!  CASE( 'PARTDIAM' )
    !Ignore

!  CASE( 'MASSFRAX' )
    !Ignore

!  CASE( 'PARTDENS' )
    !Ignore

!  CASE( 'METHOD_2' )
    !Ignore

!  CASE( 'GASDEPOS' )
    !Ignore

!  CASE( 'NO2RATIO' )
    !Ignore

  CASE( 'HOUREMIS' )

    IF( narg-ikwrd < 1 )THEN
      WRITE(*,'(A)') 'Insufficient SO HOUREMIS input'
      GOTO 9999
    END IF

    BACKSPACE(lun,IOSTAT=ios)

!------ Re-read line "as is" since case matters for path and file names on Linux
!       and to handle files names with spaces

    CALL get_next_data_NO_CUPPER( lun,line,nch,kwrd,narg,carg,MAXN,lerr )

    emiFile = TRIM(BuildFileNameAERMOD(carg(ikwrd+1),line,path_in)) !If no path included, input file path used

    INQUIRE(FILE=TRIM(emiFile),EXIST=lexist)
    IF( .NOT.lexist )THEN
      WRITE(*,'(A)') 'Hourly emission file does not exist'
      WRITE(*,'(A)') 'File name = '//TRIM(emiFile)
    END IF

!  CASE( 'INCLUDED' )
    !Ignore

!  CASE( 'OLMGROUP' )
    !Ignore

!  CASE( 'PSDGROUP' )
    !Ignore

!  CASE( 'SRCGROUP' )
    !Ignore

  CASE( 'AREASRCN' )

    IF( narg > ikwrd )THEN
      READ(carg(ikwrd+1),*,IOSTAT=ios) nAreaSrc
      nAreaSrc = MAX(nAreaSrc,1)
    END IF

  CASE( 'FINISHED' )

  CASE DEFAULT
    WRITE(*,'(A)') 'Ignored keyword for SO pathway: '//TRIM(carg(ikwrd))
!    WRITE(*,'(A)') 'Invalid keyword for SO pathway: '//TRIM(carg(ikwrd))
!    GOTO 9999

END SELECT

ParseDataSO = SUCCESS

9999 CONTINUE

RETURN
END

!==============================================================================

INTEGER FUNCTION WritePrimeFile()

USE SCIPUFFdriver_fi

IMPLICIT NONE

INTEGER i, ios

INTEGER, EXTERNAL :: FindRelName

WritePrimeFile = FAILURE

IF( .NOT.lPrimeFile )THEN

  primeFile = TRIM(prjname)//'.pri'
  OPEN(UNIT=lun_pri,FILE=TRIM(primeFile),STATUS='UNKNOWN',ACTION='WRITE',IOSTAT=ios)
  IF( ios /= 0 )THEN
    WRITE(*,'(A)') 'Error opening PRIME input file: '//TRIM(primeFile)
    GOTO 9999
  END IF
  lPrimeFile = .TRUE.

END IF

i = FindRelName( carg(ikwrd+1) )
IF( i == 0 )THEN
  WRITE(*,'(A)') 'Invalid SO SRCPARAM source id'
  GOTO 9999
END IF

IF( .NOT.(relList(i)%type /= HR_STACK .OR. &
          relList(i)%type /= HR_PRIME) )THEN
  WRITE(*,*) 'Only POINT sources can be associated with buildings'
  GOTO 9999
END IF

IF( lRelPrime(i) .eqv. .FALSE. )THEN
  relList(i)%type = HR_PRIME
  relList(i)%relDisplay = TRIM(primeFile)
  lRelPrime(i) = .TRUE.
END IF

IF( ikwrd == 1 )THEN
  WRITE(lun_pri,FMT='(A)',IOSTAT=ios) 'SO '//TRIM(line)
ELSE
  WRITE(lun_pri,FMT='(A)',IOSTAT=ios) TRIM(line)
END IF
IF( ios /= 0 )THEN
  WRITE(*,'(A)') 'Error writing to PRIME file'
  GOTO 9999
END IF

WritePrimeFile = SUCCESS

9999 CONTINUE

RETURN
END

!==============================================================================

INTEGER FUNCTION ReallocRelList( inc )

USE SCIPUFFdriver_fi

IMPLICIT NONE

INTEGER, INTENT( IN ) :: inc

INTEGER alloc_stat, i

TYPE( releaseT ), DIMENSION(:), ALLOCATABLE :: tmpList
LOGICAL,          DIMENSION(:), ALLOCATABLE :: tmplRelPrime
REAL,             DIMENSION(:), ALLOCATABLE :: tmpSrcFlag

ReallocRelList = FAILURE

!------ Allocate temporary list

ALLOCATE( tmpList(new%scnHead%max),tmplRelPrime(new%scnHead%max),tmpSrcFlag(new%scnHead%max),STAT=alloc_stat )
IF( alloc_stat /= 0 )THEN
  WRITE(*,'(A)') 'Error allocating temporary release array'
  GOTO 9999
END IF

!------ Fill it with current releases

DO i = 1,new%scnHead%max
  tmpList(i)      = relList(i)
  tmplRelPrime(i) = lRelPrime(i)
  tmpSrcFlag(i)   = SrcFlag(i)
END DO

!------ Deallocate old list; reallocate with larger size

DEALLOCATE( relList,lRelPrime,SrcFlag,STAT=alloc_stat )

ALLOCATE( relList(new%scnHead%max+inc),lRelPrime(new%scnHead%max+inc),SrcFlag(new%scnHead%max+inc),STAT=alloc_stat )
IF( alloc_stat /= 0 )THEN
  WRITE(*,'(A)') 'Error re-allocating new release array'
  GOTO 9999
END IF

!------ Copy current releases back; increment new%scnHead%max

DO i = 1,new%scnHead%max
  relList(i)   = tmpList(i)
  lRelPrime(i) = tmplRelPrime(i)
  SrcFlag(i)   = tmpSrcFlag(i)
END DO

lRelPrime(new%scnHead%max+1:new%scnHead%max+inc) = .FALSE.

new%scnHead%max = new%scnHead%max + inc

!------ Deallocate temporary list

DEALLOCATE( tmpList,tmplRelPrime,tmpSrcFlag,STAT=alloc_stat )

ReallocRelList = SUCCESS

9999 CONTINUE

RETURN
END

!==============================================================================

SUBROUTINE InitRelStack()

USE SCIPUFFdriver_fi

IMPLICIT NONE

relStackData%distribution = 1         !Default for gas materials
relStackData%rate         = NOT_SET_R
relStackData%duration     = NOT_SET_R
relStackData%diameter     = NOT_SET_R
relStackData%MMD          = NOT_SET_R
relStackData%sigma        = NOT_SET_R
relStackData%exitVel      = NOT_SET_R
relStackData%exitTemp     = NOT_SET_R
relStackData%dryFrac      = NOT_SET_R
relStackData%padding      = NOT_SET_I

RETURN
END

!==============================================================================

SUBROUTINE InitRelCont()

USE SCIPUFFdriver_fi

IMPLICIT NONE

relContData%distribution = 1         !Default for gas materials
relContData%rate         = NOT_SET_R
relContData%duration     = NOT_SET_R
relContData%sigY         = NOT_SET_R
relContData%sigZ         = NOT_SET_R
relContData%MMD          = NOT_SET_R
relContData%sigma        = NOT_SET_R
relContData%momentum     = NOT_SET_R
relContData%buoyancy     = NOT_SET_R
relContData%dryFrac      = NOT_SET_R
relContData%padding      = NOT_SET_I

RETURN
END

!==============================================================================

SUBROUTINE InitRelInst()

USE SCIPUFFdriver_fi

IMPLICIT NONE

relInstData%distribution = 1         !Default for gas materials
relInstData%mass         = NOT_SET_R
relInstData%sigX         = NOT_SET_R
relInstData%sigY         = NOT_SET_R
relInstData%sigZ         = NOT_SET_R
relInstData%MMD          = NOT_SET_R
relInstData%sigma        = NOT_SET_R
relInstData%momentum     = NOT_SET_R
relInstData%buoyancy     = NOT_SET_R
relInstData%dryFrac      = NOT_SET_R
relInstData%nRandom      = NOT_SET_I
relInstData%ranSpread    = NOT_SET_I
relInstData%padding      = NOT_SET_I

RETURN
END
