C LAST UPDATE: 
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C**************************
C**     LFN_AUTO_UPDT    **
C**************************
C
C
      SUBROUTINE LFN_AUTO_UPDT (IPACK, IPLATE, IFLAG, KFLAG, UPD)
C
      INCLUDE 'implicit_none.finc'
C
C Purpose: Update refined parameters for other packs/plates if required
C
C ****Service routine for LFN_AUTO_REFN****
C
C Author:  John W. Campbell, October 1994
C
C Arguments:
C
      INTEGER IPACK
      INTEGER IPLATE
      INTEGER IFLAG(*)
      INTEGER KFLAG(*)
      LOGICAL UPD(9)
C 
C IPACK      (R)  Pack number
C IPLATE     (R)  Plate number
C IFLAG      (R)  Parameter refine flags, phis, ctof, x_c, y_c, w_c (see
C                 LFN_RFN)
C KFLAG      (R)  Parameter refine flags for distortion (see LFN_RFN)
C UPD        (R)  Automatic update flags .true. yes, .false. no
C                 UPD(1) Phis
C                 UPD(2) ctof
C                 UPD(3) x_c, y_c
C                 UPD(4) w_c
C                 UPD(5) y_scale
C                 UPD(6) twist, tilt
C                 UPD(7) bulge
C                 UPD(8) roff/toff
C                 UPD(9) spd parameters
C
C====== Parameter statements
C 

C
C====== GLOBALS:
C

C
C====== FUNCTION TYPES:
C
      INTEGER LDM_SPDMAX
C
C====== LOCALS:
C
      INTEGER KPACK
      INTEGER KPLATE
      INTEGER IERR
      INTEGER KERR
      INTEGER LFLAG
      INTEGER ND
      INTEGER DISTOR_TYPE
      INTEGER I
      INTEGER MAX_SPD
      INTEGER IVAL
      REAL VALUE(2)
      REAL VAL
      CHARACTER*2 ERRSTR
      CHARACTER*2 VALSTR

C     INTEGER KPACK          !Pack number or 0 for all packs
C     INTEGER KPLATE         !Plate number or 0 for all pates
C     INTEGER IERR           !Error flag
C     INTEGER KERR           !Error flag
C     INTEGER LFLAG          !Parameter status flag
C     INTEGER ND             !No. of decimal places flag
C     INTEGER DISTOR_TYPE    !Distortion type flag
C     INTEGER I              !Temp/loop variable
C     INTEGER MAX_SPD        !Maximum polynomial coefficient
C     INTEGER IVAL           !Integer parameter value
C     REAL VALUE(2)          !Real parameter values array
C     REAL VAL               !Real parameter value
C     CHARACTER*2 ERRSTR     !Error string (dummy)
C     CHARACTER*2 VALSTR     !Parameter value string (dummy)

C
C-------------------------------------------------------------------------------
C
C
C====== Update LDM
C
      MAX_SPD = LDM_SPDMAX(0)
      IF (IFLAG(1).EQ.1) THEN
         CALL LDM_GET_PHIX (IPACK, VAL, LFLAG, ND, IERR, ERRSTR)
         CALL LFN_AUTO_UPD (IPACK, IPLATE, 1, UPD(1), KPACK, KPLATE)
         CALL LDM_SET_PHIX (KPACK, VAL, KERR, ERRSTR)
      END IF
      IF (IFLAG(2).EQ.1) THEN
         CALL LDM_GET_PHIY (IPACK, VAL, LFLAG, ND, IERR, ERRSTR)
         CALL LFN_AUTO_UPD (IPACK, IPLATE, 1, UPD(1), KPACK, KPLATE)
         CALL LDM_SET_PHIY (KPACK, VAL, KERR, ERRSTR)
      END IF
      IF (IFLAG(3).EQ.1) THEN
         CALL LDM_GET_PHIZ (IPACK, VAL, LFLAG, ND, IERR, ERRSTR)
         CALL LFN_AUTO_UPD (IPACK, IPLATE, 1, UPD(1), KPACK, KPLATE)
         CALL LDM_SET_PHIZ (KPACK, VAL, KERR, ERRSTR)
      END IF
      IF (IFLAG(4).EQ.1) THEN
         CALL LDM_GET_CTOF (IPACK, IPLATE, VAL, LFLAG, ND, IERR, ERRSTR)
         CALL LFN_AUTO_UPD (IPACK, IPLATE, 2, UPD(2), KPACK, KPLATE)
         CALL LDM_SET_CTOF (KPACK, KPLATE, VAL, KERR, ERRSTR)
      END IF
      IF (IFLAG(5).EQ.1) THEN
         CALL LDM_GET_XC (IPACK, IPLATE, VAL, LFLAG, ND, IERR, ERRSTR)        
         CALL LFN_AUTO_UPD (IPACK, IPLATE, 2, UPD(3), KPACK, KPLATE)
         CALL LDM_SET_XC (KPACK, KPLATE, VAL, KERR, ERRSTR)
      END IF
      IF (IFLAG(6).EQ.1) THEN
         CALL LDM_GET_YC (IPACK, IPLATE, VAL, LFLAG, ND, IERR, ERRSTR)        
         CALL LFN_AUTO_UPD (IPACK, IPLATE, 2, UPD(3), KPACK, KPLATE)
         CALL LDM_SET_YC (KPACK, KPLATE, VAL, KERR, ERRSTR)
      END IF
      IF (IFLAG(7).EQ.1) THEN
         CALL LDM_GET_WC (IPACK, IPLATE, VAL, LFLAG, ND, IERR, ERRSTR)        
         CALL LFN_AUTO_UPD (IPACK, IPLATE, 2, UPD(4), KPACK, KPLATE)
         CALL LDM_SET_WC (KPACK, KPLATE, VAL, KERR, ERRSTR)
      END IF

      CALL LDM_GET_DSTOR (VALSTR, DISTOR_TYPE, LFLAG)
      IF (DISTOR_TYPE.EQ.1) THEN
         IF (KFLAG(1).EQ.1) THEN
            CALL LDM_GET_YSCAL (IPACK, IPLATE, VAL, LFLAG, ND, 
     +           IERR, ERRSTR)        
            CALL LFN_AUTO_UPD (IPACK, IPLATE, 2, UPD(5), KPACK, KPLATE)       
            CALL LDM_SET_YSCAL (KPACK, KPLATE, VAL, KERR, ERRSTR)
         END IF
         IF (KFLAG(2).EQ.1) THEN
            CALL LDM_GET_TWIST (IPACK, IPLATE, VAL, LFLAG, ND, 
     +           IERR, ERRSTR)        
            CALL LFN_AUTO_UPD (IPACK, IPLATE, 2, UPD(6), KPACK, KPLATE)
            CALL LDM_SET_TWIST (KPACK, KPLATE, VAL, KERR, ERRSTR)
         END IF
         IF (KFLAG(3).EQ.1) THEN
            CALL LDM_GET_TILT (IPACK, IPLATE, VAL, LFLAG, ND, 
     +           IERR, ERRSTR)        
            CALL LFN_AUTO_UPD (IPACK, IPLATE, 2, UPD(6), KPACK, KPLATE)
            CALL LDM_SET_TILT (KPACK, KPLATE, VAL, KERR, ERRSTR)
         END IF
         IF (KFLAG(4).EQ.1) THEN
            CALL LDM_GET_BULGE (IPACK, IPLATE, VAL, LFLAG, ND, 
     +           IERR, ERRSTR)        
            CALL LFN_AUTO_UPD (IPACK, IPLATE, 2, UPD(7), KPACK, KPLATE)
            CALL LDM_SET_BULGE (KPACK, KPLATE, VAL, KERR, ERRSTR)
         END IF
      ELSE IF (DISTOR_TYPE.EQ.2) THEN
         IF (KFLAG(1).EQ.1) THEN
            CALL LDM_GET_YSCAL (IPACK, IPLATE, VAL, LFLAG, ND, 
     +           IERR, ERRSTR)        
            CALL LFN_AUTO_UPD (IPACK, IPLATE, 2, UPD(5), KPACK, KPLATE)       
            CALL LDM_SET_YSCAL (KPACK, KPLATE, VAL, KERR, ERRSTR)
         END IF
         IF (KFLAG(2).EQ.1) THEN
            CALL LDM_GET_TWIST (IPACK, IPLATE, VAL, LFLAG, ND, 
     +           IERR, ERRSTR)        
            CALL LFN_AUTO_UPD (IPACK, IPLATE, 2, UPD(6), KPACK, KPLATE)
            CALL LDM_SET_TWIST (KPACK, KPLATE, VAL, KERR, ERRSTR)
         END IF
         IF (KFLAG(3).EQ.1) THEN
            CALL LDM_GET_TILT (IPACK, IPLATE, VAL, LFLAG, ND, 
     +           IERR, ERRSTR)        
            CALL LFN_AUTO_UPD (IPACK, IPLATE, 2, UPD(6), KPACK, KPLATE)
            CALL LDM_SET_TILT (KPACK, KPLATE, VAL, KERR, ERRSTR)
         END IF
         IF (KFLAG(4).EQ.1) THEN
            CALL LDM_GET_ROFF (IPACK, IPLATE, VAL, LFLAG, ND, 
     +           IERR, ERRSTR)        
            CALL LFN_AUTO_UPD (IPACK, IPLATE, 2, UPD(8), KPACK, KPLATE)
            CALL LDM_SET_ROFF (KPACK, KPLATE, VAL, KERR, ERRSTR)
         END IF
         IF (KFLAG(5).EQ.1) THEN
            CALL LDM_GET_TOFF (IPACK, IPLATE, VAL, LFLAG, ND, 
     +           IERR, ERRSTR)        
            CALL LFN_AUTO_UPD (IPACK, IPLATE, 2, UPD(8), KPACK, KPLATE)
            CALL LDM_SET_TOFF (KPACK, KPLATE, VAL, KERR, ERRSTR)
         END IF
      ELSE IF (DISTOR_TYPE.EQ.3) THEN
         IF (KFLAG(1).EQ.1) THEN
            CALL LDM_GET_YSCAL (IPACK, IPLATE, VAL, LFLAG, ND, 
     +           IERR, ERRSTR)        
            CALL LFN_AUTO_UPD (IPACK, IPLATE, 2, UPD(5), KPACK, KPLATE)       
            CALL LDM_SET_YSCAL (KPACK, KPLATE, VAL, KERR, ERRSTR)
         END IF
         IF (KFLAG(2).EQ.1) THEN
            CALL LDM_GET_TWIST (IPACK, IPLATE, VAL, LFLAG, ND, 
     +           IERR, ERRSTR)        
            CALL LFN_AUTO_UPD (IPACK, IPLATE, 2, UPD(6), KPACK, KPLATE)
            CALL LDM_SET_TWIST (KPACK, KPLATE, VAL, KERR, ERRSTR)
         END IF
         IF (KFLAG(3).EQ.1) THEN
            CALL LDM_GET_TILT (IPACK, IPLATE, VAL, LFLAG, ND, 
     +           IERR, ERRSTR)        
            CALL LFN_AUTO_UPD (IPACK, IPLATE, 2, UPD(6), KPACK, KPLATE)
            CALL LDM_SET_TILT (KPACK, KPLATE, VAL, KERR, ERRSTR)
         END IF
         IF (KFLAG(4).EQ.1) THEN
            CALL LDM_GET_SPDXY (IPACK, IPLATE, VAL, LFLAG, ND, 
     +           IERR, ERRSTR)        
            CALL LFN_AUTO_UPD (IPACK, IPLATE, 2, UPD(9), 
     +                         KPACK, KPLATE)
            CALL LDM_SET_SPDXY (KPACK, KPLATE, VAL, KERR, ERRSTR)
         END IF
         IF (KFLAG(5).EQ.1) THEN
            CALL LFN_AUTO_UPD (IPACK, IPLATE, 2, UPD(9), 
     +                         KPACK, KPLATE)
            CALL LDM_GET_SPDXN (IPACK, IPLATE, IVAL, LFLAG, 
     +           IERR, ERRSTR)        
            CALL LDM_SET_SPDXN (KPACK, KPLATE, IVAL, KERR, ERRSTR)
            CALL LDM_GET_SXMIN (IPACK, IPLATE, VAL, LFLAG, ND, 
     +           IERR, ERRSTR)        
            CALL LDM_SET_SXMIN (KPACK, KPLATE, VAL, KERR, ERRSTR)
            CALL LDM_GET_SXMAX (IPACK, IPLATE, VAL, LFLAG, ND, 
     +           IERR, ERRSTR)        
            CALL LDM_SET_SXMAX (KPACK, KPLATE, VAL, KERR, ERRSTR)
            DO 100 I = 1, MAX_SPD
               CALL LDM_GET_SPDX (I, IPACK, IPLATE, VALUE, LFLAG, ND,
     +                         KERR, ERRSTR)
               CALL LDM_SET_SPDX (I, KPACK, KPLATE, VALUE,
     +                         KERR, ERRSTR)
100         CONTINUE
         END IF
         IF (KFLAG(6).EQ.1) THEN
            CALL LFN_AUTO_UPD (IPACK, IPLATE, 2, UPD(9), 
     +                         KPACK, KPLATE)
            CALL LDM_GET_SPDYN (IPACK, IPLATE, IVAL, LFLAG, 
     +           IERR, ERRSTR)        
            CALL LDM_SET_SPDYN (KPACK, KPLATE, IVAL, KERR, ERRSTR)
            CALL LDM_GET_SYMIN (IPACK, IPLATE, VAL, LFLAG, ND, 
     +           IERR, ERRSTR)        
            CALL LDM_SET_SYMIN (KPACK, KPLATE, VAL, KERR, ERRSTR)
            CALL LDM_GET_SYMAX (IPACK, IPLATE, VAL, LFLAG, ND, 
     +           IERR, ERRSTR)        
            CALL LDM_SET_SYMAX (KPACK, KPLATE, VAL, KERR, ERRSTR)
            DO 110 I = 1, MAX_SPD
               CALL LDM_GET_SPDY (I, IPACK, IPLATE, VALUE, LFLAG, ND,
     +                         KERR, ERRSTR)
               CALL LDM_SET_SPDY (I, KPACK, KPLATE, VALUE,
     +                         KERR, ERRSTR)
110         CONTINUE
         END IF
      END IF
      RETURN
      END
