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