#!/usr/bin/perl -w

=head1 NAME

translatehdr - Translate the FITS header from the supplied file

=head1 SYNOPSIS

  translatehdr mytest.sdf

=head1 DESCRIPTION

This command reads a FITS header from the supplied file (either a FITS
file or NDF) and writes the translated header information to
standard out.

=head1 ARGUMENTS

=over 4

=item B<--help>

Simple usage information.

=item B<--man>

The manual page.

=item B<--version>

Version number for this command.

=item B<--test-to-fits>

Also output the results of converting the translated header information
back to instrument-specific FITS headers.  This option is primarily
intended for use in testing the operation of the FITS header translation
software.

=back

=cut

use strict;
use warnings;

use Getopt::Long;
use Pod::Usage;

use Astro::FITS::Header;
use Astro::FITS::HdrTrans;


# Options
my ($help, $man, $version, $test_bidi);
my $status = GetOptions("help" => \$help,
                        "man" => \$man,
                        "version" => \$version,
                        "test-to-fits" => \$test_bidi,
                       );

pod2usage(1) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;

if ($version) {
  print "translatehdr - Translate a FITS header to generic form\n";
  print "Version: ", $Astro::FITS::HdrTrans::VERSION, "\n";
  exit;
}

my $file = shift(@ARGV);
die "Must supply a filename\n" unless $file;

# Get the header
my $hdr;
if ($file =~ /\.sdf$/) {
    require Astro::FITS::Header::NDF;
    $hdr = Astro::FITS::Header::NDF->new( File => $file );
} elsif ($file =~ /\.(gsd|dat)$/) {
    # assume GSD??
    require Astro::FITS::Header::GSD;
    $hdr = Astro::FITS::Header::GSD->new( File => $file );
} else {
    require Astro::FITS::Header::CFITSIO;
    $hdr = Astro::FITS::Header::CFITSIO->new( File => $file, ReadOnly => 1 );
}

# tie to a hash
my %header;
tie %header, "Astro::FITS::Header", $hdr;

my %translation = Astro::FITS::HdrTrans::translate_from_FITS( \%header );

for my $k (sort keys %translation) {
    next if $k =~ /^_/;
    my $v = $translation{$k};
    $v = "undef" if !defined $v; # should be trapped by HdrTrans
    $v = join ",\n    ", @$v if 'ARRAY' eq ref $v;
    print "$k => $v\n";
}

if ($test_bidi) {
    print "\n";

    my %retranslation = Astro::FITS::HdrTrans::translate_to_FITS(\%translation);

    for my $k (sort keys %retranslation) {
        my $v = $retranslation{$k};
        $v = 'undef' unless defined $v;
        print "$k => $v\n";
    }
}

=head1 NOTES

Currently an input file containing multiple subheaders is not handled
properly and the headers will not merged. This is true, for example,
with UKIRT .HEADER + .I1, .I2 data.

=head1 AUTHORS

Tim Jenness E<lt>t.jenness@jach.hawaii.eduE<gt>,

=head1 COPYRIGHT

Copyright (C) 2008 Science and Technology Facilities Council.
All Rights Reserved.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful,but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place,Suite 330, Boston, MA  02111-1307, USA

=cut