1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
#------------------------------------------------------------------------------
# File: FITS.pm
#
# Description: Read Flexible Image Transport System metadata
#
# Revisions: 2018/03/07 - P. Harvey Created
#
# References: 1) https://fits.gsfc.nasa.gov/fits_standard.html
#------------------------------------------------------------------------------
package Image::ExifTool::FITS;
use strict;
use vars qw($VERSION);
use Image::ExifTool qw(:DataAccess :Utils);
$VERSION = '1.00';
# FITS tags (ref 1)
%Image::ExifTool::FITS::Main = (
GROUPS => { 2 => 'Image' },
NOTES => q{
This table lists some standard Flexible Image Transport System (FITS) tags,
but ExifTool will extract any other tags found. See
L<https://fits.gsfc.nasa.gov/fits_standard.html> for the specification.
},
TELESCOP => 'Telescope',
BACKGRND => 'Background',
INSTRUME => 'Instrument',
OBJECT => 'Object',
OBSERVER => 'Observer',
DATE => { Name => 'CreateDate', Groups => { 2 => 'Time' } },
AUTHOR => { Name => 'Author', Groups => { 2 => 'Author' } },
REFERENC => 'Reference',
'DATE-OBS'=> { Name => 'ObservationDate', Groups => { 2 => 'Time' } },
'TIME-OBS'=> { Name => 'ObservationTime', Groups => { 2 => 'Time' } },
'DATE-END'=> { Name => 'ObservationDateEnd', Groups => { 2 => 'Time' } },
'TIME-END'=> { Name => 'ObservationTimeEnd', Groups => { 2 => 'Time' } },
);
#------------------------------------------------------------------------------
# Read information in a FITS document
# Inputs: 0) ExifTool ref, 1) dirInfo ref
# Returns: 1 on success, 0 if this wasn't a valid FITS file
sub ProcessFITS($$)
{
my ($et, $dirInfo) = @_;
my $raf = $$dirInfo{RAF};
my ($buff, $tag, $continue);
return 0 unless $raf->Read($buff, 80) == 80 and $buff =~ /^SIMPLE = {20}T/;
$et->SetFileType();
my $tagTablePtr = GetTagTable('Image::ExifTool::FITS::Main');
for (;;) {
$raf->Read($buff, 80) == 80 or $et->Warn('Truncated FITS header'), last;
my $key = substr($buff, 0, 8);
$key =~ s/ +$//; # remove trailing space from key
if ($key eq 'CONTINUE') {
defined $continue or $et->WarnOnce('Unexpected FITS CONTINUE keyword'), next;
} else {
if (defined $continue) {
# the previous value wasn't continued, so store with the trailing '&'
$et->HandleTag($tagTablePtr, $tag, $continue . '&');
undef $continue;
}
last if $key eq 'END';
# make sure the key is valid
$key =~ /^[-_A-Z0-9]*$/ or $et->Warn('Format error in FITS header'), last;
next unless substr($buff,8,2) eq '= '; # ignore comment lines
# save tag name (avoiding potential conflict with ExifTool variables)
$tag = $Image::ExifTool::specialTags{$key} ? "_$key" : $key;
# add to tag table if necessary
unless ($$tagTablePtr{$tag}) {
my $name = ucfirst lc $tag; # make tag name lower case with leading capital
$name =~ s/_(.)/\U$1/g; # remove all '_' and capitalize subsequent letter
AddTagToTable($tagTablePtr, $tag, { Name => $name });
}
}
my $val = substr($buff, 10);
# parse quoted values
if ($val =~ /^'(.*?)'(.*)/) {
($val, $buff) = ($1, $2);
while ($buff =~ /^('.*?)'(.*)/) { # handle escaped quotes
$val .= $1;
$buff = $2;
}
$val =~ s/ +$//; # remove trailing spaces
if (defined $continue) {
$val = $continue . $val;
undef $continue;
}
# check for possible continuation, removing trailing '&'
$val =~ s/\&$// and $continue = $val, next;
} elsif (defined $continue) {
$et->WarnOnce('Invalid FITS CONTINUE value');
next;
} else {
$val =~ s/ *(\/.*)?$//; # remove trailing spaces and comment
next unless length $val; # ignore undefined values
$val =~ s/^ +//; # remove leading spaces
# re-format floating-point values to use 'e'
$val =~ tr/DE/e/ if $val =~ /^[+-]?(?=\d|\.\d)\d*(\.\d*)?([ED]([+-]?\d+))?$/;
}
$et->HandleTag($tagTablePtr, $tag, $val);
}
return 1;
}
1; # end
__END__
=head1 NAME
Image::ExifTool::FITS - Read Flexible Image Transport System metadata
=head1 SYNOPSIS
This module is used by Image::ExifTool
=head1 DESCRIPTION
This module contains definitions required by Image::ExifTool to read meta
information from FITS (Flexible Image Transport System) images.
=head1 AUTHOR
Copyright 2003-2021, Phil Harvey (philharvey66 at gmail.com)
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=head1 REFERENCES
=over 4
=item L<https://fits.gsfc.nasa.gov/fits_standard.html>
=back
=head1 SEE ALSO
L<Image::ExifTool::TagNames/FITS Tags>,
L<Image::ExifTool(3pm)|Image::ExifTool>
=cut
|