| 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 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Image::ExifTool qw(:DataAccess :Utils); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | $VERSION = '1.02'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # FITS tags (ref 1) | ||||||||||||||||||||||||||||
| 20 | 1 | %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 => { Name => 'Comment', PrintConv => '$val =~ s/^ +//; $val', | ||||||||||||||||||||||||||||
| 40 | Notes => 'leading spaces are removed if L<PrintConv|../ExifTool.html#PrintConv> is enabled' }, | ||||||||||||||||||||||||||||
| 41 | HISTORY => { Name => 'History', PrintConv => '$val =~ s/^ +//; $val', | ||||||||||||||||||||||||||||
| 42 | Notes => 'leading spaces are removed if L<PrintConv|../ExifTool.html#PrintConv> is enabled' }, | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 46 | # Read information in a FITS document | ||||||||||||||||||||||||||||
| 47 | # Inputs: 0) ExifTool ref, 1) dirInfo ref | ||||||||||||||||||||||||||||
| 48 | # Returns: 1 on success, 0 if this wasn't a valid FITS file | ||||||||||||||||||||||||||||
| 49 | sub ProcessFITS($$) | ||||||||||||||||||||||||||||
| 50 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
ProcessFITS: called CC=21 SCAR=34.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 51 | 1 | my ($et, $dirInfo) = @_; | |||||||||||||||||||||||||||
| 52 | 1 | my $raf = $$dirInfo{RAF}; | |||||||||||||||||||||||||||
| 53 | 1 | my ($buff, $tag, $continue); | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1 | ▶ | return 0 unless $raf->Read($buff, 80) == 80 and $buff =~ /^SIMPLE = {20}T/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$raf->Read($buff, 80) == 80 and $buff =~ /^SIMPLE = {20}T/
MC/DC0%0/2
$raf->Read($buff, 80) == 80 and $buff =~ /^SIMPLE = {20}T/
$raf->Read($buff, 80) == 80
$buff =~ /^SIMPLE = {20}T/
Truth table33%1/3
A and B
A= $raf->Read($buff, 80) == 80B= $buff =~ /^SIMPLE = {20}T/
| |||||||||||||||||||||||||||||
| 56 | 1 | $et->SetFileType(); | |||||||||||||||||||||||||||
| 57 | 1 | my $tagTablePtr = GetTagTable('Image::ExifTool::FITS::Main'); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | for (;;) { | |||||||||||||||||||||||||||
| 60 | 37 | ▶ | $raf->Read($buff, 80) == 80 or $et->Warn('Truncated FITS header'), last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 37 | my $key = substr($buff, 0, 8); | |||||||||||||||||||||||||||
| 62 | 37 | $key =~ s/ +$//; # remove trailing space from key | |||||||||||||||||||||||||||
| 63 | 37 | ▶ | if ($key eq 'CONTINUE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | defined $continue or $et->Warn('Unexpected FITS CONTINUE keyword'), next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | } else { | ||||||||||||||||||||||||||||
| 66 | 37 | ▶ | if (defined $continue) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | # the previous value wasn't continued, so store with the trailing '&' | ||||||||||||||||||||||||||||
| 68 | 0 | $et->HandleTag($tagTablePtr, $tag, $continue . '&'); | |||||||||||||||||||||||||||
| 69 | 0 | undef $continue; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 37 | ▶ | last if $key eq 'END'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | # make sure the key is valid | ||||||||||||||||||||||||||||
| 73 | 36 | ▶ | $key =~ /^[-_A-Z0-9]*$/ or $et->Warn('Format error in FITS header'), last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 36 | ▶ | if ($key eq 'COMMENT' or $key eq 'HISTORY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$key eq "COMMENT" or $key eq "HISTORY"
MC/DC50%1/2
$key eq "COMMENT" or $key eq "HISTORY"
$key eq "COMMENT"
$key eq "HISTORY"
Truth table66%2/3
A or B
A= $key eq "COMMENT"B= $key eq "HISTORY"
| |||||||||||||||||||||||||||||
| 75 | 6 | my $val = substr($buff, 8); # comments start in column 9 | |||||||||||||||||||||||||||
| 76 | 6 | $val =~ s/ +$//; # remove trailing spaces | |||||||||||||||||||||||||||
| 77 | 6 | $et->HandleTag($tagTablePtr, $key, $val); | |||||||||||||||||||||||||||
| 78 | 6 | next; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | # ignore other lines that aren't tags | ||||||||||||||||||||||||||||
| 81 | 30 | ▶ | next unless substr($buff,8,2) eq '= '; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | # save tag name (avoiding potential conflict with ExifTool variables) | ||||||||||||||||||||||||||||
| 83 | 30 | ▶ | $tag = $Image::ExifTool::specialTags{$key} ? "_$key" : $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | # add to tag table if necessary | ||||||||||||||||||||||||||||
| 85 | 30 | ▶ | unless ($$tagTablePtr{$tag}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 23 | my $name = ucfirst lc $tag; # make tag name lower case with leading capital | |||||||||||||||||||||||||||
| 87 | 23 | $name =~ s/_(.)/\U$1/g; # remove all '_' and capitalize subsequent letter | |||||||||||||||||||||||||||
| 88 | 23 | AddTagToTable($tagTablePtr, $tag, { Name => $name }); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 30 | my $val = substr($buff, 10); | |||||||||||||||||||||||||||
| 92 | # parse quoted values | ||||||||||||||||||||||||||||
| 93 | 30 | ▶ | if ($val =~ /^'(.*?)'(.*)/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 94 | 18 | ($val, $buff) = ($1, $2); | |||||||||||||||||||||||||||
| 95 | 18 | ▶ | while ($buff =~ /^('.*?)'(.*)/) { # handle escaped quotes | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | $val .= $1; | |||||||||||||||||||||||||||
| 97 | 0 | $buff = $2; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 18 | $val =~ s/ +$//; # remove trailing spaces | |||||||||||||||||||||||||||
| 100 | 18 | ▶ | if (defined $continue) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | $val = $continue . $val; | |||||||||||||||||||||||||||
| 102 | 0 | undef $continue; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | # check for possible continuation, removing trailing '&' | ||||||||||||||||||||||||||||
| 105 | 18 | ▶ | $val =~ s/\&$// and $continue = $val, next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | } elsif (defined $continue) { | ||||||||||||||||||||||||||||
| 107 | 0 | $et->Warn('Invalid FITS CONTINUE value'); | |||||||||||||||||||||||||||
| 108 | 0 | next; | |||||||||||||||||||||||||||
| 109 | } else { | ||||||||||||||||||||||||||||
| 110 | 12 | $val =~ s/ *(\/.*)?$//; # remove trailing spaces and comment | |||||||||||||||||||||||||||
| 111 | 12 | ▶ | next unless length $val; # ignore undefined values | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 12 | $val =~ s/^ +//; # remove leading spaces | |||||||||||||||||||||||||||
| 113 | # re-format floating-point values to use 'e' | ||||||||||||||||||||||||||||
| 114 | 12 | ▶ | $val =~ tr/DE/e/ if $val =~ /^[+-]?(?=\d|\.\d)\d*(\.\d*)?([ED]([+-]?\d+))?$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 30 | $et->HandleTag($tagTablePtr, $tag, $val); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 1 | return 1; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 1 | 1; # end | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||