SUBROUTINE WCI1_ROTA( IPOS, PIXID, FORW, OPOS, STATUS )
*+
*  Name:
*     WCI1_ROTA

*  Purpose:
*     Rotate or derotate a position given a position angle

*  Language:
*     Starlink Fortran

*  Invocation:
*     CALL WCI1_ROTA( IPOS, PIXID, FORW, OPOS, STATUS )

*  Description:
*     The position IPOS is rotated about the origin by an amount
*     determined by the ROTATION data member of PIXID. The sense
*     of the rotation is positive if FORW is true, otherwise the
*     sense is negative. 

*  Arguments:
*     IPOS[2] = DOUBLE (given)
*        Input position
*     PIXID = INTEGER (given)
*        ADI identifier of object containing rotation angle
*     FORW = LOGICAL (given)
*        Rotate if specified true, otherwise derotate
*     OPOS[2] = DOUBLE (returned)
*        The output position
*     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:
*     WCI Subroutine Guide : http://www.sr.bham.ac.uk/asterix-docs/Programmer/Guides/wci.html

*  Keywords:
*     package:wci, usage:private

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

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

*  History:
*     31 Jan 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 'MATH_PAR'
      INCLUDE 'AST_PKG'

*  Arguments Given:
      DOUBLE PRECISION		IPOS(2)
      INTEGER			PIXID
      LOGICAL			FORW

*  Arguments Returned:
      DOUBLE PRECISION		OPOS(2)

*  Status:
      INTEGER 			STATUS             	! Global status

*  External References:
      EXTERNAL			AST_QPKGI
        LOGICAL			AST_QPKGI

*  Local variables:
      DOUBLE PRECISION		CROTA			! Cos(ROTA)
      DOUBLE PRECISION		ROTA			! Rotation angle
      DOUBLE PRECISION		SROTA			! Sin(ROTA)
*.

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

*  Check initialised
      IF ( .NOT. AST_QPKGI( WCI__PKG ) ) CALL WCI1_INIT( STATUS )

*  Extract rotation 
      CALL ADI_CGET0D( PIXID, 'ROTATION', ROTA, STATUS )

*  Null rotation?
      IF ( ROTA .NE. 0D0 ) THEN

*    Find cosine and sine of ROTA
        CROTA = COS(ROTA*MATH__DDTOR)
        SROTA = SIN(ROTA*MATH__DDTOR)

        IF ( FORW ) THEN
          OPOS(1) = CROTA*IPOS(1) + SROTA*IPOS(2)
          OPOS(2) = CROTA*IPOS(2) - SROTA*IPOS(1)

        ELSE
          OPOS(1) = CROTA*IPOS(1) - SROTA*IPOS(2)
          OPOS(2) = CROTA*IPOS(2) + SROTA*IPOS(1)

        END IF

      ELSE
        OPOS(1) = IPOS(1)
        OPOS(2) = IPOS(2)

      END IF

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

      END