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

*  Purpose:
*     Service FileItemUnmap requests from the BDI system for HDS files

*  Language:
*     Starlink Fortran

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

*  Description:
*     Services BDI unmap requests for HDS files. 

*  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'

*  Arguments Given:
      INTEGER                   NARG, ARGS(*)

*  Arguments Returned:
      INTEGER                   OARG

*  Status:
      INTEGER 			STATUS             	! Global status

*  Local Variables:
      CHARACTER*20		ITEM

      INTEGER			PSID			! Private item storage
*.

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

*  Default return value
      OARG = ADI__NULLID

*  Extract the arguments
      CALL ADI_GET0C( ARGS(3), ITEM, STATUS )

*  Locate the BDI private storage for the item
      CALL BDI0_LOCPST( ARGS(1), ITEM, .FALSE., PSID, STATUS )
      IF ( PSID .NE. ADI__NULLID ) THEN 

*    Unmap the item
        CALL BDI1_UNMAP_INT( ARGS(1), ARGS(2), PSID, STATUS )

*    Release the item
        CALL ADI_ERASE( PSID, STATUS )

      END IF

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

      END



      SUBROUTINE BDI1_UNMAP_INT( BDID, HFID, PSID, STATUS )
*+
*  Name:
*     BDI1_UNMAP_INT

*  Purpose:
*     Unmap an item given its private storage location

*  Language:
*     Starlink Fortran

*  Invocation:
*     CALL BDI1_UNMAP_INT( BDID, HFID, PSID, STATUS )

*  Description:
*     Unmap an item given its private storage location. If the data is not
*     directly mapped to the file and the access mode is not read then an
*     optional WriteBack function is invoked.

*  Arguments:
*     BDID = INTEGER (given)
*        ADI identifier to top level BDI object
*     HFID = INTEGER (given)
*        The ADI identifier of the HDS file object
*     PSID = INTEGER (given)
*        ADI identifier to private storage
*     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'

*  Arguments Given:
      INTEGER                   BDID,HFID,PSID

*  Status:
      INTEGER 			STATUS             	! Global status

*  Local Variables:
      CHARACTER*3		MSYS			! Mapping system
      CHARACTER*6		MODE			! Mapping mode

      INTEGER			WB_CB			! WriteBack function

      LOGICAL			THERE			! Object exists?
*.

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

*  Get mapping mode
      CALL ADI_CGET0C( PSID, 'MapSystem', MSYS, STATUS )

*  Get mode
      CALL ADI_CGET0C( PSID, 'Mode', MODE, STATUS )

*  Has a WriteBack function been provided?
      CALL ADI_THERE( PSID, 'WriteBack', THERE, STATUS )
      IF ( THERE ) THEN

*    Get writeback address 
        CALL ADI_CGET0I( PSID, 'WriteBack', WB_CB, STATUS )

*    Invoke the write back
        CALL BDI1_UNMAP_WB( %VAL(WB_CB), BDID, HFID, PSID, STATUS )

*  Issue warning because data mapped for update or write access is not
*  being handled cleanly
      ELSE IF ( MODE .NE. 'READ' ) THEN
        CALL MSG_PRNT( 'WARNING : Altered data cannot be written '/
     :                 /'back to HDS file' )          
      
      END IF     

*  Force map count to zero
      CALL ADI_CPUT0I( PSID, 'MapCount', 0, STATUS )

*  Report any errors
      IF ( STATUS .NE. SAI__OK ) THEN
        CALL AST_REXIT( 'BDI1_UNMAP_INT', STATUS )
      END IF

      END



      SUBROUTINE BDI1_UNMAP_WB( WRITEB, BDID, HFID, PSID, STATUS )
*+
*  Name:
*     BDI1_UNMAP_WB

*  Purpose:
*     Invoke a WriteBack function

*  Language:
*     Starlink Fortran

*  Invocation:
*     CALL BDI1_UNMAP_WB( WRITEB, BDID, HFID, PSID, STATUS )

*  Description:
*     Invokes a WriteBack function to change memory data back to the file
*     representation immediately prior to it being written to file.

*  Arguments:
*     WRITEB = EXTERNAL (given)
*        The WriteBack procedure
*     BDID = INTEGER (given)
*        ADI identifier to top level BDI object
*     HFID = INTEGER (given)
*        The ADI identifier of the HDS file object
*     PSID = INTEGER (given)
*        ADI identifier private storage 
*     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:
      EXTERNAL			WRITEB
      INTEGER                   BDID, HFID, PSID

*  Status:
      INTEGER 			STATUS             	! Global status
*.

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

*  Invoke write back
      CALL WRITEB( BDID, HFID, PSID, STATUS )

      END