SUBROUTINE BDI1_MAPLQ( NARG, ARGS, OARG, STATUS )
*+
*  Name:
*     BDI1_MAPLQ

*  Purpose:
*     Service FileItemMap requests for the LogicalQuality field of BinDS

*  Language:
*     Starlink Fortran

*  Invocation:
*     CALL BDI1_MAPLQ( NARG, ARGS, OARG, STATUS )

*  Description:
*     Provides mapping for the 'Error' class member of BinDS derived
*     objects in HDS files. This member is derived from the VARIANCE
*     file object.

*  Arguments:
*     NARG = INTEGER (given)
*        Number of method arguments
*     ARGS(*) = INTEGER (given)
*        ADI identifier of method arguments
*     OARG = INTEGER (returned) 
*        Output data
*     STATUS = INTEGER (given and returned)
*        The global status.

*  Examples:
*     {routine_example_text}
*        {routine_example_description}

*  Pitfalls:
*     {pitfall_description}...

*  Notes:
*     {routine_notes}...

*  Prior Requirements:
*     {routine_prior_requirements}...

*  Side Effects:
*     {routine_side_effects}...

*  Algorithm:
*     {algorithm_description}...

*  Accuracy:
*     {routine_accuracy}

*  Timing:
*     {routine_timing}

*  External Routines Used:
*     {name_of_facility_or_package}:
*        {routine_used}...

*  Implementation Deficiencies:
*     {routine_deficiencies}...

*  References:
*     BDI Subroutine Guide : http://www.sr.bham.ac.uk/asterix-docs/Programmer/Guides/bdi.html

*  Keywords:
*     package:bdi, usage:private

*  Copyright:
*     Copyright (C) University of Birmingham, 1995

*  Authors:
*     DJA: David J. Allan (Jet-X, University of Birmingham)
*     {enter_new_authors_here}

*  History:
*     9 Aug 1995 (DJA):
*        Original version.
*     {enter_changes_here}

*  Bugs:
*     {note_any_bugs_here}

*-
      
*  Type Definitions:
      IMPLICIT NONE              ! No implicit typing

*  Global Constants:
      INCLUDE 'SAE_PAR'          ! Standard SAE constants
      INCLUDE 'ADI_PAR'
      INCLUDE 'DAT_PAR'
      INCLUDE 'QUAL_PAR'

*  Arguments Given:
      INTEGER                   NARG, ARGS(*)

*  Arguments Returned:
      INTEGER                   OARG

*  Status:
      INTEGER 			STATUS             	! Global status

*  Local Variables:
      CHARACTER*(DAT__SZLOC)	MLOC			! Quality mask
      CHARACTER*(DAT__SZLOC)	QLOC			! Quality array

      CHARACTER*6		MODE

      INTEGER			NELM			! Number of data items
      INTEGER			PSID			! Items private store
      INTEGER			PTR			! Mapped data address
      INTEGER			QPTR			! Mapped quality array

      BYTE			MASK			! Quality mask
*.

*  Check inherited global status.
      IF ( STATUS .NE. SAI__OK ) RETURN

*  Default return value
      OARG = ADI__NULLID

*  Extract the arguments
      CALL ADI_GET0C( ARGS(5), MODE, STATUS )

*  Ensure objects satisfy mapping requirement
      IF ( MODE .EQ. 'READ' ) THEN

*    Locate quality object      
        CALL BDI1_CFIND( ARGS(1), ARGS(2), 'Quality', .FALSE., QLOC,
     :                   STATUS )

*    And the mask
        CALL BDI1_CFIND( ARGS(1), ARGS(2), 'QualityMask', .FALSE., 
     :                   MLOC, STATUS )

*    If no quality array, build a dynamic array to mark everything as good
        IF ( QLOC .EQ. DAT__NOLOC ) THEN

*      Get number of data elements
          CALL BDI_GETNEL( ARGS(1), NELM, STATUS )

*      Create dynamic array
          CALL DYN_MAPL( 1, NELM, PTR, STATUS )

*      Fill it with 'good' value
          CALL ARR_INIT1L( .TRUE., NELM, %VAL(PTR), STATUS )

        ELSE 

*      Default mask if not present
          IF ( MLOC .EQ. DAT__NOLOC ) THEN
            MASK = QUAL__MASK
          ELSE
            CALL DAT_GET( MLOC, '_UBYTE', 0, 0, MASK, STATUS )
          END IF

*      Map the quality array 
          CALL DAT_MAPV( QLOC, '_UBYTE', 'READ', QPTR, NELM, STATUS )

*      Create dynamic array
          CALL DYN_MAPL( 1, NELM, PTR, STATUS )

*      Copy bytes into workspace (which is 4 times bigger)
          CALL ARR_COP1B( NELM, %VAL(QPTR), %VAL(PTR), STATUS )

*      Logical AND with the mask
          CALL BIT_AND1UB( NELM, %VAL(PTR), MASK, STATUS )

*      Copy mask bytes to logical values
          CALL BDI1_MAPLQ_BCOP( NELM, %VAL(PTR), %VAL(PTR), STATUS )
 
*      Release the quality array
          CALL DAT_UNMAP( QLOC, STATUS )
          CALL DAT_ANNUL( QLOC, STATUS )

        END IF

*    Release locators
        IF ( MLOC .NE. DAT__NOLOC ) THEN
          CALL DAT_ANNUL( MLOC, STATUS )
        END IF
          
*    Locate the BDI private storage for the item, creating if required
        CALL BDI0_LOCPST( ARGS(1), 'LogicalQuality', .TRUE., PSID, 
     :                    STATUS )

*    If mapping went ok, store the pointer
        CALL BDI1_STOMAP( PSID, .TRUE., DAT__NOLOC, 0, PTR, 'LOGICAL',
     :                    NELM, MODE, STATUS )

*    Release private storage
        CALL ADI_ERASE( PSID, STATUS )

*    If mapping went ok, store the pointer in the return argument
        CALL ADI_NEWV0I( PTR, OARG, STATUS )

      ELSE
        STATUS = SAI__ERROR
        CALL MSG_SETC( 'M', MODE )
        CALL ERR_REP( 'BDI1_MAPLQ_1', 'Mapping LogicalQuality for '/
     :                 /'update/write access is forbidden', STATUS )

      END IF

*  Report any errors
      IF ( STATUS .NE. SAI__OK ) CALL AST_REXIT( 'BDI1_MAPLQ', STATUS )

      END



      SUBROUTINE BDI1_MAPLQ_BCOP( N, BVAL, LVAL, STATUS )
*+
*  Name:
*     BDI1_MAPLQ_BCOP

*  Purpose:
*     Convert masked BYTE values to LOGICAL in situ

*  Language:
*     Starlink Fortran

*  Invocation:
*     CALL BDI1_MAPLQ_BCOP( N, BVAL, LVAL, STATUS )

*  Description:
*     Provides mapping for the 'Error' class member of BinDS derived
*     objects in HDS files. This member is derived from the VARIANCE
*     file object.

*  Arguments:
*     N = INTEGER (given)
*        Number of values to copy
*     BVAL[] = BYTE (given)
*        Byte values 
*     LVAL[] = LOGICAL (returned) 
*        Logical values, true if BVAL is zero, false otherwise
*     STATUS = INTEGER (given and returned)
*        The global status.

*  Examples:
*     {routine_example_text}
*        {routine_example_description}

*  Pitfalls:
*     {pitfall_description}...

*  Notes:
*     {routine_notes}...

*  Prior Requirements:
*     {routine_prior_requirements}...

*  Side Effects:
*     {routine_side_effects}...

*  Algorithm:
*     {algorithm_description}...

*  Accuracy:
*     {routine_accuracy}

*  Timing:
*     {routine_timing}

*  External Routines Used:
*     {name_of_facility_or_package}:
*        {routine_used}...

*  Implementation Deficiencies:
*     {routine_deficiencies}...

*  References:
*     BDI Subroutine Guide : http://www.sr.bham.ac.uk/asterix-docs/Programmer/Guides/bdi.html

*  Keywords:
*     package:bdi, usage:private

*  Copyright:
*     Copyright (C) University of Birmingham, 1995

*  Authors:
*     DJA: David J. Allan (Jet-X, University of Birmingham)
*     {enter_new_authors_here}

*  History:
*     9 Aug 1995 (DJA):
*        Original version.
*     {enter_changes_here}

*  Bugs:
*     {note_any_bugs_here}

*-
      
*  Type Definitions:
      IMPLICIT NONE              ! No implicit typing

*  Global Constants:
      INCLUDE 'SAE_PAR'          ! Standard SAE constants

*  Arguments Given:
      INTEGER                   N
      BYTE			BVAL(*)

*  Arguments Returned:
      LOGICAL			LVAL(*)

*  Status:
      INTEGER 			STATUS             	! Global status

*  Local Variables:
      INTEGER			I			! Loop over values
*.

*  Check inherited global status.
      IF ( STATUS .NE. SAI__OK ) RETURN

*  Loop over array back to front, as its really the same array 
      DO I = N, 1, -1
        LVAL(I) = (BVAL(I).EQ.0)
      END DO

      END