C LAST UPDATE: 1 October 1993
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C**************************
C**     LFN_DETPRO       **
C**************************
C
C
      SUBROUTINE LFN_DETPRO (IPACK, IPLATE, NHALF, IERR, ERRSTR)
C
      INCLUDE 'implicit_none.finc'
C
C PURPOSE: to determine profile region
C
C **** Service Routine Only ****
C
C AUTHOR: Q. Hao 
C
C ARGUMENTS:
C
      INTEGER IPACK
      INTEGER IPLATE 
      INTEGER NHALF
      INTEGER IERR
      CHARACTER*(*) ERRSTR
C
C      INTEGER IPACK	(R)	Pack number
C      INTEGER IPLATE 	(R)	Plate number
C      INTEGER NHALF    (R)     Maximum half width of box
C      INTEGER IERR	(W)     Error flag = 0 OK
C                                   =-1 Invalid pack number
C                                   =-2 Invalid plate number
C                                   = 1 Box size required exceeds current
C                                       program limits
C      CHARACTER*(*) ERRSTR (W) Error string if error found    
C
C====== Parameter statements
C 
      INCLUDE 'saint_limits.finc'
C
C====== GLOBALS:
C
      INCLUDE 'lfn_saint.finc'
C
C====== LOCALS:
C
      INTEGER ND
      INTEGER IFLAG 
      INTEGER IX
      INTEGER IY
      REAL SPOTL
      REAL SPOTW
      REAL BORDER
      REAL MM_RAST_X
      REAL MM_RAST_Y
      REAL AMAJOR
      REAL AMAJOR_1
      REAL AMAJOR_B
      REAL BMINOR
      REAL BMINOR_1
      REAL BMINOR_B
      REAL ELLIP
      REAL ELLIP_1
      REAL ELLIP_B
C
C     INTEGER ND	!Number of decimal places for print
C     INTEGER IFLAG 	!Flag = -1 undefined value
C                            =  0 default value 
C                            =  1 value set explicitly 
C                            =  2 value set globally  
C     INTEGER IX        !Loop counter over X pixel
C     INTEGER IY        !Loop counter over Y pixel
C     REAL SPOTL	!Spot length in mm
C     REAL SPOTW	!Spot width in mm
C     REAL BORDER	!Spot background border width in pixels
C     REAL MM_RAST_X	!Conversion factor for mm to x-rasters
C     REAL MM_RAST_Y	!Conversion factor for mm to y-rasters
C     REAL AMAJOR	!Ellipse major axis (corrected) in rasters
C     REAL AMAJOR_1	!Ellipse major axis (including omit pixel) in rasters 
C     REAL AMAJOR_B	!Ellipse major axis (including background) in rasters 
C     REAL BMINOR	!Ellipse minor axis in rasters
C     REAL BMINOR_1	!Ellipse minor axis (including omit pixel) in rasters
C     REAL BMINOR_B	!Ellipse minor axis (including background) in rasters
C     REAL ELLIP	!Value of ellipse function x**2/a**+y**2/b**2
C     REAL ELLIP_B	!Value of ellipse function x**2/a**+y**2/b**2 (inc 
C                        omit pixel)
C     REAL ELLIP_B	!Value of ellipse function x**2/a**+y**2/b**2 (inc bg)
C
C-------------------------------------------------------------------------------
C
      IERR = 0
      CALL LDM_GET_SPOTL (IPACK, IPLATE, SPOTL, IFLAG, ND, IERR, ERRSTR)
      IF (IERR.NE.0) RETURN
      CALL LDM_GET_SPOTW (IPACK, IPLATE, SPOTW, IFLAG, ND, IERR, ERRSTR)
      CALL LDM_GET_SPOTB (IPACK, IPLATE, BORDER, IFLAG, ND, IERR,ERRSTR)
      IF (BORDER.LT.1.0) BORDER = 1.0
      CALL LDM_MMRAST (IPACK, IPLATE, MM_RAST_X, MM_RAST_Y)
      IF (SPOTW.EQ.0.0) SPOTW = SPOTL
      AMAJOR = SPOTL*MM_RAST_X/2.0
      AMAJOR_1 = AMAJOR + 1.0
      AMAJOR_B = AMAJOR_1 + BORDER
      BMINOR = SPOTW*MM_RAST_X/2.0
      BMINOR_1 = BMINOR + 1.0
      BMINOR_B = BMINOR_1 + BORDER 
      NUM_IN_PROF = 0
      DO 800 IX = -NHALF, NHALF
         DO 700 IY = -NHALF, NHALF
            INDXPR(IX,IY) = 0
            ELLIP = (IX**2/AMAJOR**2) + (IY**2/BMINOR**2)
            ELLIP_1 = (IX**2/AMAJOR_1**2) + (IY**2/BMINOR_1**2)
            ELLIP_B = (IX**2/AMAJOR_B**2) + (IY**2/BMINOR_B**2)
            IF (ELLIP_B.LE.1.0) THEN
               NUM_IN_PROF = NUM_IN_PROF + 1 
C
C====== Set 'SHAPE' mask defining background
C
               SHAPE(NUM_IN_PROF) = 0
               SHAPE_PROF(NUM_IN_PROF) = 0
	       IXPROF(NUM_IN_PROF) = IX
	       IYPROF(NUM_IN_PROF) = IY
               INDXPR(IX,IY) = NUM_IN_PROF
    
               IF (ELLIP_1.LE.1.0) THEN
                  SHAPE(NUM_IN_PROF) = -1
                  SHAPE_PROF(NUM_IN_PROF) = -1
C
C====== Set 'SHAPE' mask defining peak
C 
                  IF (ELLIP.LE.1.0) THEN
                     SHAPE(NUM_IN_PROF) = 10
                     SHAPE_PROF(NUM_IN_PROF) = 10
                  ENDIF
               END IF
            ENDIF
700      CONTINUE
800   CONTINUE
C
      RETURN
      END
