
C*---------------------------------------------------------------------*
C*              S E R V I C E     R O U T I N E S                      *
C*---------------------------------------------------------------------*

C LAST UPDATE: 
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C**************************
C**     LFN_UNQ_LIMS     **
C**************************
C
C
      SUBROUTINE LFN_UNQ_LIMS(NLAUE, RESMAX, HMIN, HMAX,
     +                        KMIN, KMAX, LMIN, LMAX, IERR)
C
      INCLUDE 'implicit_none.finc'
C
C Purpose: Get index limits for a given resolution range and Laue group
C
C Author:  Based on code from the CCP4 program 'UNIQUE'
C
C Arguments:
C
      INTEGER NLAUE
      REAL RESMAX
      INTEGER HMIN
      INTEGER HMAX
      INTEGER KMIN
      INTEGER KMAX
      INTEGER LMIN
      INTEGER LMAX
      INTEGER IERR
C 
C NLAUE      (R)  Laue group number (e.g. from LDM_ASUSET)
C RESMAX     (R)  Resolution limit in Angstroms
C HMIN       (W)  Minimum 'h' index
C HMAX       (W)  Maximum 'h' index
C KMIN       (W)  Minimum 'k' index
C KMAX       (W)  Maximum 'k' index
C LMIN       (W)  Minimum 'l' index
C LMAX       (W)  Maximum 'l' index
C IERR       (W)  Error flag =0 OK, =1 invalid Laue group
C
C====== GLOBALS:
C
      EXTERNAL LFN_UNQ_BDAT
      INCLUDE 'lfn_unq.finc'
C
C====== FUNCTION TYPES:
C

C
C====== LOCALS:
C
      INTEGER IFLAG
      INTEGER ND
      REAL CELL(6)

C     INTEGER IFLAG       !LDM parameter status flag
C     INTEGER ND          !No. of decimal places flag
C     REAL CELL(6)        !Cell parameters
C
C-------------------------------------------------------------------------------
C
      IERR = 0
      HMIN = 0
      HMAX = 0
      KMIN = 0
      KMAX = 0
      LMIN = 0
      LMAX = 0
C
C====== Get cell parameters
C
      CALL LDM_GET_A (CELL(1),IFLAG,ND)
      CALL LDM_GET_B (CELL(2),IFLAG,ND)
      CALL LDM_GET_C (CELL(3),IFLAG,ND)
      CALL LDM_GET_ALPHA (CELL(4),IFLAG,ND)
      CALL LDM_GET_BETA (CELL(5),IFLAG,ND)
      CALL LDM_GET_GAMMA (CELL(6),IFLAG,ND)
C
C====== Check Laue code
C
      IF (NLAUE.LT.1.OR.NLAUE.GT.16) THEN
         IERR = 1
         RETURN
      END IF
C
C====== Get maximum absolute limits from resolution 
C
      HMAX = INT(CELL(1)/RESMAX)
      KMAX = INT(CELL(2)/RESMAX)
      LMAX = INT(CELL(3)/RESMAX)

      HMIN = 0
      KMIN = 0
      LMIN = 0

      IF (NLAUE.EQ.1) THEN
C
C====== LAUE = 1, 1bar,  hkl:h>=0   0kl:k>=0   00l:l>=0
C
        KMIN = -KMAX
        LMIN = -LMAX
        RETURN
C
C====== LAUE = 2, 1bar,  hkl:k>=0   h0l:l>=0   h00:h>=0
C
      ELSE IF (NLAUE.EQ.2) THEN
        HMIN = -HMAX
        LMIN = -LMAX
        RETURN
C
C====== LAUE = 3, 1bar,  hkl:l>=0   hk0:h>=0   0k0:k>=0
C
      ELSE IF (NLAUE.EQ.3) THEN
        HMIN = -HMAX
        KMIN = -KMAX
        RETURN
C
C====== LAUE = 4, 2/m,   hkl:k>=0, l>=0     hk0:h>=0
C
      ELSE IF (NLAUE.EQ.4) THEN
        HMIN = -HMAX
        RETURN
C
C====== LAUE = 5, 2/m,   hkl:h>=0, l>=0     0kl:k>=0   (2-nd sett.)
C
      ELSE IF (NLAUE.EQ.5) THEN
        KMIN = -KMAX
        RETURN
C
C====== LAUE = 6, mmm,   hkl:h>=0, k>=0, l>=0
C
      ELSE IF (NLAUE.EQ.6) THEN
        RETURN
C
C====== LAUE = 7, 4/m,   hkl:h>=0, k>0, l>=0 with  k>=0 for h=0
C
      ELSE IF (NLAUE.EQ.7) THEN
        RETURN
C
C====== LAUE = 8, 4/mmm,   hkl:h>=0, h>=k>=0, l>=0
C
      ELSE IF (NLAUE.EQ.8) THEN
        RETURN
C
C====== LAUE = 9, 3bar,   hkl:h>=0, k>0  including  00l:l>0
C
      ELSE IF (NLAUE.EQ.9) THEN
        KMAX = HMAX
        LMIN = -LMAX
        KMIN = 1
        RETURN
C
C====== LAUE = 10, 312,    hkl:h>=0, k>=0 with k<=h  and l>=0 if h = 0
C
      ELSE IF (NLAUE.EQ.10) THEN
        KMAX = HMAX
        LMIN = -LMAX
        RETURN
C
C====== LAUE = 11, 321,    hkl:h>=0, k>=0 with k<=h  and l>=0 if h = k
C
      ELSE IF (NLAUE.EQ.11) THEN
        KMAX = HMAX
        LMIN = -LMAX
        RETURN
C
C====== LAUE = 12, 6/m,   hkl:h>=0, k>0, l>=0  with  k>=0 for h=0
C
      ELSE IF (NLAUE.EQ.12) THEN
        KMAX = HMAX
        RETURN
C
C====== LAUE = 13, 6/mmm,   hkl:h>=0, h>=k>=0, l>=0
C
      ELSE IF (NLAUE.EQ.13) THEN
        KMAX = HMAX
        RETURN
C
C====== LAUE = 14, m3,  hkl:h>=0, k>=0, l>=0 with l>=h, k>=h for l=h
C======                                             and  k>h for l>h
C
      ELSE IF (NLAUE.EQ.14) THEN
        RETURN
C
C====== LAUE = 15, m3m,   hkl:k>=l>=h>=0
C
      ELSE IF (NLAUE.EQ.15) THEN
        RETURN
C
C====== LAUE = 16, m3m,   hkl:h>=k>=l>=0
C
      ELSE IF (NLAUE.EQ.16) THEN
        RETURN
      END IF
      RETURN
      END
