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

*  Purpose:
*     Service FileItemMap requests for the virtual Error field of BinDS

*  Language:
*     Starlink Fortran

*  Invocation:
*     CALL BDI1_MAPERR( 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'

*  Arguments Given:
      INTEGER                   NARG, ARGS(*)

*  Arguments Returned:
      INTEGER                   OARG

*  Status:
      INTEGER 			STATUS             	! Global status

*  Local Variables:
      CHARACTER*(DAT__SZLOC)	CLOC			! New component

      CHARACTER*6		MODE
      CHARACTER*7		TYPE

      INTEGER			NELM			! Number of data items
      INTEGER			PSID			! Items private store
      INTEGER			PTR			! Mapped data address
*.

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

*  Default return value
      OARG = ADI__NULLID

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

*  Ensure objects satisfy mapping requirement
      CALL BDI1_CFIND( ARGS(1), ARGS(2), 'Variance',
     :                 (MODE.EQ.'WRITE'), CLOC, STATUS )

*  Everything ok? 
      IF ( STATUS .EQ. SAI__OK ) THEN

*    Locate the BDI private storage for the item, creating if required
        CALL BDI0_LOCPST( ARGS(1), 'Error', .TRUE., PSID, STATUS )

*    Write mode?
        IF ( MODE .EQ. 'WRITE' ) THEN

*      Simply map for write access        
          CALL BDI1_ARYMAP( CLOC, TYPE, MODE, .TRUE., PSID, PTR,
     :                      NELM, STATUS )

        ELSE

*      Map existing data
          CALL BDI1_ARYMAP( CLOC, TYPE, MODE, .TRUE., PSID, PTR,
     :                      NELM, STATUS )

*      Convert variance to error
          IF ( TYPE .EQ. 'REAL' ) THEN
            CALL BDI1_MAPERR_V2ER( NELM, %VAL(PTR), STATUS )

          ELSE IF ( TYPE .EQ. 'DOUBLE' ) THEN
            CALL BDI1_MAPERR_V2ED( NELM, %VAL(PTR), STATUS )

          ELSE
            STATUS = SAI__ERROR
            CALL MSG_SETC( 'T', TYPE )
            CALL ERR_REP( 'BDI1_MAPERR_1', 'Mapping Error for '/
     :                    /'read/update access and type ^T is '/
     :                    /'not currently supported', STATUS )
          END IF

        END IF

*    Release private storage
        CALL ADI_ERASE( PSID, STATUS )

*    Release object
        CALL DAT_ANNUL( CLOC, STATUS )

      END IF

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

      END



      SUBROUTINE BDI1_MAPERR_V2ER( N, VAR, STATUS )
*+
*  Name:
*     BDI1_MAPERR_V2ER

*  Purpose:
*     Convert REAL variance to error in situ

*  Language:
*     Starlink Fortran

*  Invocation:
*     CALL BDI1_MAPERR_V2ER( N, VAR, STATUS )

*  Description:
*     Convert REAL variance to error in situ. Duff variance values (<0) are
*     converted to zero errors. This is done because generally such bad
*     variance values are ignored due to an accompanying bad quality. We
*     don't want to report errors unnecessarily.

*  Arguments:
*     N = INTEGER (given)
*        Number of data elements
*     VAR[] = REAL (given and returned)
*        Variance on input, error on output
*     STATUS = INTEGER (given)
*        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

*  Arguments Given and Returned:
      REAL			VAR(*)

*  Status:
      INTEGER 			STATUS             	! Global status

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

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

*  Convert data values
      DO I = 1, N
        IF ( VAR(I) .GT. 0.0 ) THEN
          VAR(I) = SQRT(VAR(I))
        ELSE
          VAR(I) = 0.0
        END IF
      END DO

      END



      SUBROUTINE BDI1_MAPERR_V2ED( N, VAR, STATUS )
*+
*  Name:
*     BDI1_MAPERR_V2ED

*  Purpose:
*     Convert DOUBLE PRECISION variance to error in situ

*  Language:
*     Starlink Fortran

*  Invocation:
*     CALL BDI1_MAPERR_V2ED( N, VAR, STATUS )

*  Description:
*     Convert DOUBLE PRECISION variance to error in situ. Duff variance values (<0) are
*     converted to zero errors. This is done because generally such bad
*     variance values are ignored due to an accompanying bad quality. We
*     don't want to report errors unnecessarily.

*  Arguments:
*     N = INTEGER (given)
*        Number of data elements
*     VAR[] = DOUBLE PRECISION (given and returned)
*        Variance on input, error on output
*     STATUS = INTEGER (given)
*        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

*  Arguments Given and Returned:
      DOUBLE PRECISION		VAR(*)

*  Status:
      INTEGER 			STATUS             	! Global status

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

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

*  Convert data values
      DO I = 1, N
        IF ( VAR(I) .GT. 0.0 ) THEN
          VAR(I) = SQRT(VAR(I))
        ELSE
          VAR(I) = 0.0
        END IF
      END DO

      END