#!perl
# Testing AST dependancies

use strict;

use Test::More;
BEGIN { 
 eval "use Starlink::AST;";
 if ($@) {
   plan skip_all => "Starlink::AST not available.";
   exit;
 } else {
   plan tests => 51;
 }
}

# load modules
require_ok( "Astro::FITS::Header" );
require_ok( "Astro::FITS::Header::AST" );
require_ok( "Astro::FITS::Header::Item" );


# T E S T   H A R N E S S --------------------------------------------------

# GET_WCS() in ASTRO::FITS::HEADER
# --------------------------------

# read header from DATA block
my @raw = <DATA>;
chomp(@raw);

# build header array
my $header = new Astro::FITS::Header( Cards => \@raw );

# test the header
for my $i (0 .. $#raw) {
  my $card = $header->item($i);
  ok( "$card", $raw[$i]);  
}

# grab the WCS
my $wcsinfo = $header->get_wcs();
isa_ok( $wcsinfo, "Starlink::AST::FrameSet" );

# Mappings
# --------

# define some arrays (and references) to hold out inital coordinates
my ( @x, @y );
my $xpixel = \@x;
my $ypixel = \@y;

# FORWARD MAPPING
$x[0] = 0;
$y[0] = 0;;
$x[1] = 114;
$y[1] = 128;

my ( $xworld, $yworld) = $wcsinfo->Tran2( $xpixel, $ypixel, 1 );
is( $$xworld[0], 4.5, "Forward mapping of lower bound X co-ordinate" ); 
is( $$yworld[0], -0.5, "Forward mapping of lower bound Y co-ordinate" );
is( $$xworld[1], 118.5, "Forward mapping of upper bound X co-ordinate" ); 
is( $$yworld[1], 127.5, "Forward mapping of upper bound Y co-ordinate" );

# REVERSE MAPPING
$x[0] = 4.5;
$y[0] = -0.5;
$x[1] = 118.5;
$y[1] = 127.5;
($xworld, $yworld) = $wcsinfo->Tran2( $xpixel, $ypixel, 0 );
is( $$xworld[0], 0, "Reverse mapping of lower bound X co-ordinate" ); 
is( $$yworld[0], 0, "Reverse mapping of lower bound Y co-ordinate" );
is( $$xworld[1], 114, "Reverse mapping of upper bound X co-ordinate" ); 
is( $$yworld[1], 128, "Reverse mapping of upper bound Y co-ordinate" );

# new ASTRO::FITS::HEADER::AST()
# ------------------------------

my $ast_header = new Astro::FITS::Header::AST( FrameSet => $wcsinfo );

# test the header
is( $ast_header->sizeof(), 9, "Size of returned header" );

exit;

__DATA__
SIMPLE  =                    T / file does conform to FITS standard             
BITPIX  =                  -32 / number of bits per data pixel                  
NAXIS   =                    2 / number of data axes                            
NAXIS1  =                  114 / length of data axis 1                          
NAXIS2  =                  128 / length of data axis 2                          
CRPIX1  =                 57.0 / Reference pixel on axis 1                      
CRPIX2  =                 64.0 / Reference pixel on axis 2                      
CRVAL1  =                 61.5 / Value at ref. pixel on axis 1                  
CRVAL2  =                 63.5 / Value at ref. pixel on axis 2                  
CTYPE1  = 'CCD_REG1'           / Quantity represented by axis 1                 
CTYPE2  = 'CCD_REG2'           / Quantity represented by axis 2                 
CD1_1   =                  1.0 / Transformation matrix element                  
CD2_2   =                  1.0 / Transformation matrix element                  
EXTEND  =                    T / FITS dataset may contain extensions            
COMMENT   FITS (Flexible Image Transport System) format is defined in 'Astronomy
COMMENT   and Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H 
LBOUND1 =                    6 / Pixel origin along axis 1                      
LBOUND2 =                    1 / Pixel origin along axis 2                      
OBJECT  = 'Output from TRANNDF'/ Title of the dataset                           
DATE    = '2004-02-22T22:02:27'/ file creation date (YYYY-MM-DDThh:mm:ss UT)    
ORIGIN  = 'Starlink Project, U.K.'/ Origin of this FITS file                    
BSCALE  =                  1.0 / True_value = BSCALE * FITS_value + BZERO       
BZERO   =                  0.0 / True_value = BSCALE * FITS_value + BZERO       
HDUCLAS1= 'NDF     '           / Starlink NDF (hierarchical n-dim format)       
HDUCLAS2= 'DATA    '           / Array component subclass                       
CCDXIMSI=                  114                                                  
CCDXIMST=                    6                                                  
CCDXSIZE=                  128                                                  
CCDYIMSI=                  128                                                  
CCDYIMST=                    1                                                  
CCDYSIZE=                  128                                                  
GAIN    =                    1                                                  
READNOIS=                 10.0                                                  
PFMFNAME= 'B                 '                                                  
OBSTYPE = 'TARGET            '                                                  
TELESCOP= 'CCDPACK SPECIAL   '                                                  
ISEQ    =                    1                                                  
END                                                                             
