C
C LAST UPDATE:
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C******************************
C**     LFN_GET_PROFILE      **
C******************************
C
CD-Routine: Get Spot Profile - LFN_GET_PROFILE
C This routine is used to return the details of a spot profile calculated
C by a profile determining pass of the routine LFN_SAINT.
CD-end
CD-Fortran:
      SUBROUTINE LFN_GET_PROFILE (IPROF, INDX_PROF, MAX_HALF, 
     +                            IMASK, IXPR, IYPR, PRF, MAX_PROF,
     +                            IPK, IPL, NP_HALF, NUMP, NUMBIN,
     +                            IERR, ERRSTR)
CD-end
      INCLUDE 'implicit_none.finc'
C
C PURPOSE: Get profile details from a profile calculated by LFN_SAINT
C 
C AUTHOR: J.W. Campbell 
C
C ARGUMENTS:
C
      INTEGER MAX_HALF
      INTEGER MAX_PROF
      INTEGER IPROF
      INTEGER INDX_PROF(-MAX_HALF:MAX_HALF,-MAX_HALF:MAX_HALF)
      INTEGER IMASK(MAX_PROF)
      INTEGER IXPR(MAX_PROF)
      INTEGER IYPR(MAX_PROF)
      REAL PRF(MAX_PROF)
      INTEGER IPK
      INTEGER IPL
      INTEGER NP_HALF
      INTEGER NUMP
      INTEGER NUMBIN
      INTEGER IERR
      CHARACTER*(*) ERRSTR
CD-Parameters:
C   IPROF               i (R)  Profile no. (must be in range of those 
C                              calculated)
C   INDX_PROF(-MAX_HALF:MAX_HALF,-MAX_HALF:MAX_HALF)
C                       i (W)  Indices to pixels in profile arrays 
C                              (0 for pixels not used). First dimension 
C                              is along 'x' and 2'nd is along 'y'
C   MAX_HALF            i  (R) Dimension parameter for INDX_PROF
C   IMASK(MAX_PROF)     i  (W) Pixel use 0/1 background, -1 omitted 1 pixel
C                              border round peak, -2 = omitted pixel from
C                              spot overlap, -3 = omitted pixel from
C                              overloaded pixel, >=10 peak pixel
C   IXPR(MAX_PROF)      i  (W) 'x' offset of pixel from box centre
C   IYPR(MAX_PROF)      i  (W) 'y' offset of pixel from box centre
C   PRF(MAX_PROF)       r  (W) Profile values
C   MAX_PROF            i  (R) Maximum dimension of profile arrays passed
C   IPK                 i  (W) Pack no. used in profiles calculation
C   IPL                 i  (W) Plate no. used in profiles calculation
C   NP_HALF             i  (W) Half size of box used in profiles 
C                              calculation (box 2*NP_HALF+1 square)
C   NUMP                i  (W) No. of pixels used in profiles
C   NUMBIN              i  (W) No. of profile bins
C   IERR                i  (W) Error flag =0 OK
C                                =1 Requested bin out of range
C                                =2 Profile arrays too small
C                                -1 Index array too small (central portion
C                                   will be filled) - non-fatal
C   ERRSTR              c  (W) Error message string (max 80 chars) set 
C                              if IERR is non-zero
CD-end
C
C====== Parameter statements
C 
      INCLUDE 'saint_limits.finc'
C
C====== GLOBALS:
C
      INCLUDE 'lfn_saint.finc'
C
C====== LOCALS:
C
      INTEGER I
      INTEGER IX
      INTEGER IY
      INTEGER IHALF

C     INTEGER I       !Temp/loop variable
C     INTEGER IX      !Loop over 'x' pixels (offsets)
C     INTEGER IY      !Loop over 'y' pixels (offsets)
C     INTEGER IHALF   !Half size of index array

C
C------------------------------------------------------------------------------- 
C
      IERR = 0
      ERRSTR = ' '
C
C====== Error checks
C
      IF (IPROF.LT.1.OR.IPROF.GT.NPROF_BIN) THEN
         IERR = 1
         ERRSTR = 'Profile was not calculated for bin'
         CALL STR_ADDI (ERRSTR, IPROF, 1)
         RETURN
      END IF
      IF (NUM_IN_PROF.GT.MAX_IN_PROF) THEN
         IERR = 2
         ERRSTR = 'Profile arrays not large enought to return data'
         RETURN
      END IF
C
C====== Check index array - treat as non-fatal even if too small
C
      IF (NPR_HALF.GT.MAX_HALF) THEN
         IERR = -1
         ERRSTR = 'Index array too small for complete data'
      END IF
C
C====== Return data
C
      IPK = IPROF_PK
      IPL = IPROF_PL
      NP_HALF = NPR_HALF
      NUMP = NUM_IN_PROF
      NUMBIN = NPROF_BIN
      IHALF = NPR_HALF
      IF (IHALF.GT.MAX_HALF) IHALF = MAX_HALF
      DO 20 IX = -IHALF, IHALF
         DO 10 IY = -IHALF, IHALF
            INDX_PROF(IX,IY) = INDXPR(IX,IY)
 10      CONTINUE
 20   CONTINUE
      DO 30 I = 1, NUM_IN_PROF
         IMASK(I) = SHAPE_PROF(I)
         IXPR(I) = IXPROF(I)
         IYPR(I) = IYPROF(I)
         PRF(I) = PROF(I,IPROF)
 30   CONTINUE
      RETURN
      END
