C LAST UPDATE: 
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C**************************
C**     LDM_SET_CTOF     **
C**************************
C
C
CD-Doc:
C CTOF             SUBROUTINE LDM_SET_CTOF (IPACK, IPLATE, VALUE, 
C                 +                         IERR, ERRSTR)
CD-end
      SUBROUTINE LDM_SET_CTOF (IPACK, IPLATE, VALUE, IERR, ERRSTR)
C
      INCLUDE 'implicit_none.finc'
C
C Purpose: Set value of plate dependent real parameter CTOF
C
C Author:  John W. Campbell, June 1993
C
C Arguments:
C
      INTEGER IPACK
      INTEGER IPLATE
      REAL    VALUE
      INTEGER IERR
      CHARACTER*(*) ERRSTR

C  IPACK     (R)   Pack number, 0=all packs
C  IPLATE    (R)   Plate number, 0=all plates
C  VALUE     (R)   Value for the crystal-film distance (mm) >0.0
C                  If set globally for plates, then SPACING values for
C                  lower plates are added on to successive ctof values
C  IERR      (W)   Error flag =0 OK
C                             =-1 Invalid pack number
C                             =-2 Invalid plate number
C                             =3 Value must be >0.0
C  ERRSTR    (W)   Error description string if error found
C
C====== GLOBALS:
C
      INCLUDE 'ldm_data.finc'
      INCLUDE 'ldm_lrl.finc'
      EXTERNAL LDM_BDAT
C
C====== LOCALS:
C
      INTEGER I
      INTEGER J
      REAL SPC
C
C-------------------------------------------------------------------------------
C
      IERR = 0
      ERRSTR = ' '
      CALL LDM_CHK_SPL (IPACK, IPLATE, IERR, ERRSTR)
      IF (IERR.NE.0) RETURN
      CALL LDM_ERR_R (VALUE, 2, 0.0, 0.0, 3, IERR, ERRSTR)
      IF (IERR.NE.0) RETURN
      IF (IPACK.EQ.0) THEN
         IF (IPLATE.EQ.0) THEN
            DO 100 I = 1, LDM_MAXPK
               SPC = 0.0
               DO 50 J = 1, LDM_MAXPL
                  IF (J.GT.1) SPC = SPC + SPAC(I,J)
                  CTOF(I,J) = VALUE + SPC
                  IS_CTOF(I,J) = 2
                  CH_CTOF(I,J) = CH_MASK
                  ND_CTOF(I,J) = -1000
50             CONTINUE
100         CONTINUE
         ELSE
            DO 150 I = 1, LDM_MAXPK
                CTOF(I,IPLATE) = VALUE
                IS_CTOF(I,IPLATE) = 2
                CH_CTOF(I,IPLATE) = CH_MASK
                ND_CTOF(I,IPLATE) = -1000
150         CONTINUE
         END IF
      ELSE
         IF (IPLATE.EQ.0) THEN
            SPC = 0.0
            DO 200 J = 1, LDM_MAXPL
               IF (J.GT.1) SPC = SPC + SPAC(IPACK,J)
               CTOF(IPACK,J) = VALUE + SPC
               IS_CTOF(IPACK,J) = 2
               CH_CTOF(IPACK,J) = CH_MASK
               ND_CTOF(IPACK,J) = -1000
200         CONTINUE
         ELSE
            CTOF(IPACK,IPLATE) = VALUE
            IS_CTOF(IPACK,IPLATE) = 1
            CH_CTOF(IPACK,IPLATE) = CH_MASK
            ND_CTOF(IPACK,IPLATE) = -1000
         END IF
      END IF
      CALL LDM_CLR_GEN (IPACK, IPLATE, 2)
      RETURN
      END
