
CD-Section: Convenience and Supplementary LDM Routines
C This section includes a number of useful routines for decoding axis order
C strings, getting mm to raster conversion factors, determining an image
C file name (using template or explicit definition as appropriate). Routines
C are also listed in this section for returning the spatial distortion 
C polynomial coefficient limit and for converting strings to integer or
C real numbers.
CD-end

C LAST UPDATE: 
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C**************************
C**     LDM_AXORD        **
C**************************
C
CD-Routine: Interpret Axis Order String - LDM_AXORD
C This routine decodes an axis order string as described in the parameter
C list.
CD-end
CD-Fortran:
      SUBROUTINE LDM_AXORD (VALSTR, IORD, ISWAP, IERR, ERRSTR)
CD-end
      INCLUDE 'implicit_none.finc'
C
C Purpose: Decode axis order string
C
C Author:  John W. Campbell, June 1993
C
C Arguments:
C
      CHARACTER*(*) VALSTR
      INTEGER IORD
      INTEGER ISWAP
      INTEGER IERR
      CHARACTER*(*) ERRSTR

CD-Parameters:
C VALSTR   c (R)   The string containing the axis order code string to
C                  be interpreted.
C
C                  The string defines the axis order in terms of x and y 
C                  (the detector xd, yd axes). The slower moving axis is
C                  given first followed by the faster moving axis. Minus 
C                  signs are used when the order of an axis is reversed. 
C                  The code 's' if present indicates that byte swapping is 
C                  to be done and the code 'n' indicates that no byte 
C                  swapping is to be done (byte swapping only relevant for 
C                  image plate data). Characters, other than 'x', 'y', 
C                  '-', '+', 's' and 'n', or their upper case equivalents, 
C                  are ignored. 
C                  Some examples follow and the returned flags are indicated:
C
C                  xy                     IORD = 1, ISWAP = 0
C                  +x+y                   IORD = 1, ISWAP = 0
C                  -xy                    IORD = 3, ISWAP = 0
C                  x-y s                  IORD = 2, ISWAP = 2
C                  -Y, +X, n              IORD = 7, ISWAP = 1
C                  S                      IORD = 0, ISWAP = 2
C                  +yd, +xd               IORD = 4, ISWAP = 0
C
C                  Invalid strings: +x-x      (Same axis defined twice)
C                                   +x+y-x    (More than two axes defined)
C                                   xysn      (Byte swapping and no byte 
C                                              swapping both defined)
C
C IORD     i (W)   Returns the axis order as a number from 1 - 8
C                  0 if none specified (or syntax error)
C                  1   +xd slow   +yd fast
C                  2   +xd slow   -yd fast
C                  3   -xd slow   +yd fast
C                  4   -xd slow   -yd fast
C                  5   +yd slow   +xd fast
C                  6   +yd slow   -xd fast
C                  7   -yd slow   +xd fast
C                  8   -yd slow   -xd fast
C ISWAP    i (W)   Returns byte swap type   =1 do not swap bytes
C                                           =2 swap bytes
C                                           =0 not specified (or syntax
C                                              error)
C IERR     i (W)   Error flag 0 = OK
C                             1 = Only one axis defined
C                             2 = Both axes the same
C                             3 = More than two axes defined
C                             4 = byte swap and no byte swap both given
C ERRSTR   c (W)   Error message string (up to 80 chars)
CD-end
C
C====== GLOBALS:
C

C
C====== FUNCTION TYPES:
C
      INTEGER LENSTR
C
C====== LOCALS:
C
      INTEGER SAXIS
      INTEGER SSIGN
      INTEGER FAXIS
      INTEGER FSIGN 
      INTEGER N
      INTEGER LL
      INTEGER L  

C     INTEGER SAXIS      !Slow axis 1=x, 2=y
C     INTEGER SSIGN      !Sign of slow axis 1='+', -1='-'
C     INTEGER FAXIS      !Fast axis 1=x, 2=y
C     INTEGER FSIGN      !Sign of fast axis 1='+', -1='-'
C     INTEGER N          !No. of axis definitions found
C     INTEGER LL         !Significant length of input string
C     INTEGER L          !Loop counter through input string
C
C-------------------------------------------------------------------------------
C
C====== Initialisations
C
      IERR = 0
      ERRSTR = ' '
      IORD = 0
      IF (VALSTR.EQ.' ') RETURN
      LL = LENSTR(VALSTR)
      SAXIS = 0
      SSIGN = 1
      FAXIS = 0
      FSIGN = 1
      ISWAP = 0
      IORD = 0
      N = 1
C
C====== Decode the string
C
      DO 100 L = 1, LL
         IF (VALSTR(L:L).EQ.' ') GO TO 100
         IF (VALSTR(L:L).EQ.'-') THEN
            IF (N.EQ.1) THEN
               SSIGN = -1
            ELSE
               FSIGN = -1
            END IF
         ELSE IF (VALSTR(L:L).EQ.'+') THEN
            IF (N.EQ.1) THEN
               SSIGN = 1
            ELSE
               FSIGN = 1
            END IF
         ELSE IF (VALSTR(L:L).EQ.'x'.OR.VALSTR(L:L).EQ.'X') THEN
            IF (N.EQ.1) THEN
               SAXIS = 1
               N = 2
            ELSE
               IF (N.EQ.3) THEN
                  IERR = 3
                  ERRSTR = 'Invalid order - more than two axes defined'
                  GO TO 200
               END IF
               FAXIS = 1
               N = 3
            END IF
         ELSE IF (VALSTR(L:L).EQ.'y'.OR.VALSTR(L:L).EQ.'Y') THEN
            IF (N.EQ.1) THEN
               SAXIS = 2
               N = 2
            ELSE
               IF (N.EQ.3) THEN
                  IERR = 3
                  ERRSTR = 'Invalid order - more than two axes defined'
                  GO TO 200
               END IF
               FAXIS = 2
               N = 3
            END IF
         ELSE IF (VALSTR(L:L).EQ.'s'.OR.VALSTR(L:L).EQ.'S') THEN
            IF (ISWAP.GT.0) THEN
               IERR = 4
               ERRSTR = 'Invalid code - byte & no byte swap defined'
               GO TO 200
            END IF
            ISWAP = 2
         ELSE IF (VALSTR(L:L).EQ.'n'.OR.VALSTR(L:L).EQ.'N') THEN
            IF (ISWAP.GT.0) THEN
               IERR = 4
               ERRSTR = 'Invalid code - byte & no byte swap defined'
               GO TO 200
            END IF
            ISWAP = 1
         END IF
100   CONTINUE
      IF (SAXIS.EQ.0.AND.FAXIS.EQ.0) THEN
         RETURN
      END IF
      IF (SAXIS.EQ.0.OR.FAXIS.EQ.0) THEN
         IERR = 1
         ERRSTR = 'Invalid order - only one axis defined'
         GO TO 200
      END IF
      IF (SAXIS.EQ.FAXIS) THEN
         IERR = 2
         ERRSTR = 'Invalid order - both axes the same'
         GO TO 200
      END IF
      IORD = 4*SAXIS - 3
      IF (SSIGN.LT.0) IORD = IORD + 2
      IF (FSIGN.LT.0) IORD = IORD + 1
      RETURN
C
C====== Error conditions
C
200   ERRSTR = 'Invalid axis order string'
      IORD = 0
      ISWAP = 0
      RETURN
      END
