!+ Subroutine to write a SAMSON-field.
!
! Method:
!	Straightforward.
!
! 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 WRITE_SAMSON_FIELD(IERR, L_OFFSET
     &   , FILE_NAME, TITLE
     &   , N_PROFILE, N_LAYER, P, PSTAR, FIELD
     &   )
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER.
      INCLUDE 'incl/dimensions_field.ucf'
      INCLUDE 'incl/samson_parm.ecf'
      INCLUDE 'incl/def_samson_io.icf'
      INCLUDE 'incl/error.pcf'
!
!     DUMMY ARGUMENTS.
      INTEGER	!, INTENT(OUT)
     &     IERR
!             ERROR FLAG
      INTEGER	!, INTENT(IN)
     &     N_LAYER
!             NUMBER OF LAYERS
     &   , N_PROFILE
!             NUMBER OF PROFILES
      CHARACTER	!, INTENT(IN)
     &     FILE_NAME*(*)
!             NAME OF FILE
     &   , TITLE*(*)
!             TITLE OF FILE
      LOGICAL	!, INTENT(IN)
     &     L_OFFSET
!             LOGICAL TO OFFSET FIELDS IN LAYERS
      REAL*8	!, INTENT(IN)
     &     P(0: NPD_LAYER)
!             PRESSURES ON LEVELS
     &   , PSTAR
!             SURFACE PRESSURE
     &   , FIELD(NPD_PROFILE, 0: NPD_LAYER)
!             FIELD TO BE WRITTEN
!
!     LOCAL VARIABLES.
      INTEGER
     &     inthead(P_headlen)
!             SAMSON FILE HEADER
     &   , I
!             LOOP VARIABLE
     &   , L
!             LOOP VARIABLE
      REAL*8
     &     samson_field(-1: NPD_PROFILE, NPD_LAYER+1)
!             SAMSON FIELD
!
!     SUBROUTINES CALLED:
      EXTERNAL
     &     OPEN_SAMSON_OUT, SAMSON_OUT
!
!
!
!     PREPARE SAMSON HEADERS AND THE FIELD.
      inthead(1)=N_PROFILE
      inthead(3)=1
      IF (L_OFFSET) THEN
         inthead(2)=N_LAYER
         DO I=1, N_LAYER
            samson_field(-1, I)=0.0D+00
            samson_field(0, I)=P(I)-PSTAR*samson_field(-1, I)
            DO L=1, N_PROFILE
               samson_field(L, I)=FIELD(L, I)
            ENDDO
         ENDDO
      ELSE
         inthead(2)=N_LAYER+1
         DO I=0, N_LAYER
            samson_field(-1, I+1)=0.0D+00
            samson_field(0, I+1)=P(I)-PSTAR*samson_field(-1, I+1)
            DO L=1, N_PROFILE
               samson_field(L, I+1)=FIELD(L, I)
            ENDDO
         ENDDO
      ENDIF
!
      CALL OPEN_SAMSON_OUT(IERR, IU_SAMSON_OUT, FILE_NAME)
      IF (IERR.EQ.I_ERR_IO) THEN
!        TIDY UP AND MOVE TO NEXT FILE.
         CLOSE(IU_SAMSON_OUT)
         IERR=0
         RETURN
      ENDIF
      CALL SAMSON_OUT(IU_SAMSON_OUT, TITLE, inthead, samson_field)
      CLOSE(IU_SAMSON_OUT)
!
!
      RETURN
      END
