HdrTrans/Base.pm

filter: Statementstmt 88.2% 256 / 290 Branchbran 66.0% 37 / 56 Conditioncond 46.6% 14 / 30 MC/DCmcdc 20.0% 4 / 20 Subroutinesub 97.6% 127 / 130 Podpod 18.8% 22 / 117 totaltotal 71.5% 460 / 643 CC 49 SCAR 6.1
CC
49
Cov
82%
via_subheader
42.0
translate_from_FITS
12.1
can_translate
7.1
CRAP 1.8
« HdrTrans/UKIRTNew.pm ↑ Summary HdrTrans/CURVE.pm »
1 package Astro::FITS::HdrTrans::Base;
2  
3 =head1 NAME
4  
5 Astro::FITS::HdrTrans::Base - Base class for header translation
6  
7 =head1 SYNOPSIS
8  
9 use base qw/ Astro::FITS::HdrTrans::Base /;
10  
11 %generic = Astro::FITS::HdrTrans::Base->translate_from_FITS( \%fits );
12 %fits = Astro::FITS::HdrTrans::Base->translate_to_FITS( \%gen );
13  
14 =head1 DESCRIPTION
15  
16 This is the header translation base class. Not to be confused with
17 C<Astro::FITS::HdrTrans> itself, which is a high level abstraction
18 class. In general users should use C<Astro::FITS::HdrTrans>
19 for initiating header translations unless they know what they are
20 doing. Also C<Astro::FITS::HdrTrans> is the only public interface
21 to the header translation.
22  
23 =cut
24  
25 71 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 55 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 52 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 52 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 52 ▶use Math::Trig qw/ deg2rad /;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 52 ▶use Astro::FITS::HdrTrans (); # for the generic header list
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 52 our $VERSION = "1.66";
34  
35 =head1 PUBLIC METHODS
36  
37 All methods in this class are CLASS METHODS. No state is retained
38 outside of the hash argument.
39  
40 =over 4
41  
42 =item B<translate_from_FITS>
43  
44 Do the header translation from FITS for the specified class.
45  
46 %generic = $class->translate_to_FITS( \%fitshdr,
47 prefix => $prefix,
48 frameset => $wcs,
49 );
50  
51 Prefix is attached to the keys in the returned hash if it
52 is defined. The frameset is an optional Starlink::AST object.
53  
54 If a translation results in an undefined value (for example, if the
55 headers can represent both imaging and spectroscopy there may be no
56 requirement for a DISPERSION header), the result is not stored in the
57 translated hash.
58  
59 A list of failed translations is available in the _UNDEFINED_TRANSLATIONS
60 key in the generic hash. This points to a reference to an array of all
61 the failed generic translations.
62  
63 The class used for the translation is stored in the key _TRANSLATION_CLASS.
64 This can then be used to reverse the translation without having to
65 re-scan the headers.
66  
67 =cut
68  
69 20 ▶sub translate_from_FITS {
Subroutine translate_from_FITS: called CC=11 SCAR=24.9
Pod documented
70 20 my $class = shift;
71 20 my $FITS = shift;
72 20 my %opts = @_;
73  
74 20 my $prefix = '';
75 20 ▶ if ( exists( $opts{prefix} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"prefix"} and defined $opts{"prefix"}) 1 19 20
Condition66%2/3
exists $opts{"prefix"} and defined $opts{"prefix"}
!ll&&!rl&&r
0191
MC/DC50%1/2
exists $opts{"prefix"} and defined $opts{"prefix"}
exists $opts{"prefix"} defined $opts{"prefix"}
Truth table66%2/3
A and B
A= exists $opts{"prefix"}B= defined $opts{"prefix"}
ABresult
0X0
100
111
76 defined( $opts{prefix} ) ) {
77 1 $prefix = $opts{prefix};
78 }
79  
80 20 my $frameset;
81 20 ▶ if ( exists( $opts{frameset} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"frameset"} and defined $opts{"frameset"}) 0 20 20
Condition33%1/3
exists $opts{"frameset"} and defined $opts{"frameset"}
!ll&&!rl&&r
0200
MC/DC0%0/2
exists $opts{"frameset"} and defined $opts{"frameset"}
exists $opts{"frameset"} defined $opts{"frameset"}
Truth table33%1/3
A and B
A= exists $opts{"frameset"}B= defined $opts{"frameset"}
ABresult
0X0
100
111
82 defined( $opts{frameset} ) ) {
83 0 $frameset = $opts{frameset};
84 }
85  
86 20 ▶ croak "translate_from_FITS: Not a hash reference!"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $FITS and ref $FITS eq "HASH" 0 20 20
Condition33%1/3
ref $FITS and ref $FITS eq "HASH"
!ll&&!rl&&r
0020
MC/DC0%0/2
ref $FITS and ref $FITS eq "HASH"
ref $FITS ref $FITS eq "HASH"
Truth table33%1/3
A and B
A= ref $FITSB= ref $FITS eq "HASH"
ABresult
0X0
100
111
87 unless (ref($FITS) && ref($FITS) eq 'HASH');
88  
89 # Now we need to loop over the known generic headers
90 # which we obtain from Astro::FITS::HdrTrans
91 20 my @GEN = Astro::FITS::HdrTrans->generic_headers;
92  
93 20 my %generic;
94 my @failed;
95 20 for my $g (@GEN) {
96 2920 my $method = "to_$g";
97 2920 ▶ if ($class->can( $method )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can($method)) 1164 1756 2920
98 1164 my $result = $class->$method( $FITS, $frameset );
99 1164 ▶ if (defined $result) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result) { } 901 263 1164
100 901 $generic{"$prefix$g"} = $result;
101 } else {
102 263 push(@failed, $g);
103 }
104 }
105 }
106  
107 # store the failed translations (if we had any)
108 20 ▶ $generic{_UNDEFINED_TRANSLATIONS} = \@failed if @failed;
Branch: 1 branch
BranchTrueFalseTotal
if @failed 20 0 20
109  
110 # store the translation class
111 20 $generic{_TRANSLATION_CLASS} = $class;
112  
113 20 return %generic;
114 }
115  
116 =item B<translate_to_FITS>
117  
118 Do the header translation from generic headers to FITS
119 for the specified class.
120  
121 %fits = $class->translate_to_FITS( \%generic );
122  
123 =cut
124  
125 15 ▶sub translate_to_FITS {
Subroutine translate_to_FITS: called CC=5 SCAR=16.4
Pod documented
126 15 my $class = shift;
127 15 my $generic = shift;
128  
129 15 ▶ croak "translate_to_FITS: Not a hash reference!"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $generic and ref $generic eq "HASH" 0 15 15
Condition33%1/3
ref $generic and ref $generic eq "HASH"
!ll&&!rl&&r
0015
MC/DC0%0/2
ref $generic and ref $generic eq "HASH"
ref $generic ref $generic eq "HASH"
Truth table33%1/3
A and B
A= ref $genericB= ref $generic eq "HASH"
ABresult
0X0
100
111
130 unless (ref($generic) && ref($generic) eq 'HASH');
131  
132 # Now we need to loop over the known generic headers
133 # which we obtain from Astro::FITS::HdrTrans
134 15 my @GEN = Astro::FITS::HdrTrans->generic_headers;
135  
136 15 my %FITS;
137 15 for my $g (@GEN) {
138 2190 my $method = "from_$g";
139 2190 ▶ if ($class->can( $method )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can($method)) 782 1408 2190
140 782 %FITS = (%FITS,$class->$method( $generic ));
141 }
142  
143 }
144  
145 15 return %FITS;
146 }
147  
148 =back
149  
150 =head1 PROTECTED METHODS
151  
152 These methods are available to translation subclasses and should
153 not be used by external classes.
154  
155 =over 4
156  
157 =item B<can_translate>
158  
159 Returns true if the supplied headers can be handled by this class.
160  
161 $cando = $class->can_translate( \%hdrs );
162  
163 The base class version of this method returns true if either the C<INSTRUME>
164 or C<INSTRUMENT> key exist and match the value returned by the
165 C<this_instrument> method. Comparisons are case-insensitive and can use
166 regular expressions on instrument name if provided by the base class.
167  
168  
169 =cut
170  
171 527 ▶sub can_translate {
Subroutine can_translate: called CC=7 SCAR=19.6
Pod documented
172 527 my $class = shift;
173 527 my $headers = shift;
174  
175 # get the reference instrument string
176 527 my $ref = $class->this_instrument();
177 527 ▶ return 0 unless defined $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref 0 527 527
178  
179 # For consistency in subsequent algorithm convert
180 # a string to a pattern match object
181 527 ▶ if (not ref($ref)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $ref) 243 284 527
182 243 $ref = quotemeta($ref);
183 243 $ref = qr/^$ref$/i;
184 }
185  
186 # check against the FITS and Generic versions.
187 527 my $inst;
188 527 for my $k (qw/ INSTRUME INSTRUMENT /) {
189 540 ▶ if (exists $headers->{$k} && defined $headers->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $headers->{$k} and defined $headers->{$k}) 527 13 540
Condition66%2/3
exists $headers->{$k} and defined $headers->{$k}
!ll&&!rl&&r
130527
MC/DC50%1/2
exists $headers->{$k} and defined $headers->{$k}
exists $headers->{$k} defined $headers->{$k}
Truth table66%2/3
A and B
A= exists $headers->{$k}B= defined $headers->{$k}
ABresult
0X0
100
111
190 527 $inst = $headers->{$k};
191 527 last;
192 }
193 }
194  
195 # no recognizable instrument
196 527 ▶ return 0 unless defined $inst;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $inst 0 527 527
197  
198 # Now do the test
199 527 return ( $inst =~ $ref );
200 }
201  
202 =item B<this_instrument>
203  
204 Name of the instrument that can be translated by this class.
205 Defaults to an empty string. The method must be subclassed.
206  
207 $inst = $class->this_instrument();
208  
209 Can return a regular expresion object (C<qr>).
210  
211 =cut
212  
213 0 ▶sub this_instrument {
Subroutine this_instrument: not called CC=1 SCAR=6.9
Pod documented
214 0 return "";
215 }
216  
217 =item B<valid_class>
218  
219 Historically this method was used to determine whether this class can
220 handle the supplied FITS headers. The headers can be either in
221 generic form or in FITS form.
222  
223 $isvalid = $class->valid_class( \%fits );
224  
225 The base class always returns false. This is a backwards compatibility
226 method to prevent mixing of translation modules from earlier release
227 of C<Astro::FITS::HdrTrans> with the current object-oriented version.
228 See the C<can_translate> method for the new interface.
229  
230 =cut
231  
232 0 ▶sub valid_class {
Subroutine valid_class: not called CC=1 SCAR=6.9
Pod documented
233 0 return 0;
234 }
235  
236 =item B<_generate_lookup_methods>
237  
238 We generate the unit and constant mapping methods automatically from a
239 lookup table.
240  
241 Astro::FITS::HdrTrans::UKIRT->_generate_lookup_methods( \%const, \%unit);
242  
243 This method generates all the simple internal methods. Expects two arguments,
244 both references to hashes. The first is a reference to a hash with
245 constant mapping from FITS to generic (and no reverse mapping), the
246 second is a reference to a hash with unit mappings (both from and to
247 methods are created). The methods are placed into the package given
248 by the class supplied to the method.
249  
250 Astro::FITS::HdrTrans::UKIRT->_generate_lookup_methods( \%const, \%unit, \%null);
251  
252 Additionally, an optional third argument can be used to indicate
253 methods that should be null translations. This is a reference to an array
254 of generic keywords and should be used in the rare cases when a base
255 class implementation should be nullified. This will result in undefined
256 values in the generic hash but no value in the generic to FITS mapping.
257  
258 A fourth optional argument can specify those unit mappings that should
259 use the final entry in a subheader (if a subheader is present). Mainly
260 associated with END events such as AIRMASS_END or ELEVATION_END.
261  
262 Astro::FITS::HdrTrans::UKIRT->_generate_lookup_methods( \%const, \%unit,
263 \%null, \%endobs);
264  
265 These methods will have the standard interface of
266  
267 $generic = $class->_to_GENERIC_NAME( \%fits );
268 %fits = $class->_from_GENERIC_NAME( \%generic );
269  
270 Generic unit map translations use the via_subheader() method in scalar
271 context and so will retrieve the first sub header value if the keyword
272 is not present in the primary header.
273  
274 =cut
275  
276 512 ▶sub _generate_lookup_methods {
Subroutine _generate_lookup_methods: called CC=7 SCAR=19.5
277 512 my $class = shift;
278 512 my $const = shift;
279 512 my $unit = shift;
280 512 my $null = shift;
281 512 my $endobs = shift;
282  
283 # Have to go into a different package
284 512 my $p = "{\n package $class;\n";
285 512 my $ep = "\n}"; # close the scope
286  
287 # Loop over the keys to the unit mapping hash
288 # The keys are the GENERIC name
289 512 for my $key (keys %$unit) {
290  
291 # Get the original FITS header name
292 4876 my $fhdr = $unit->{$key};
293  
294 # print "Processing $key and $ohdr and $fhdr\n";
295  
296 # First generate the code to generate Generic headers
297 4876 my $subname = "to_$key";
298 4876 my $sub = qq/ $p sub $subname { scalar \$_[0]->via_subheader_undef_check(\$_[1],\"$fhdr\"); } $ep /;
299 0 ▶ eval "$sub";
Subroutine to_AIRMASS_END: called CC=1 SCAR=0.0
Subroutine to_AIRMASS_START: called CC=1 SCAR=0.0
Subroutine to_AMBIENT_TEMPERATURE: called CC=1 SCAR=0.0
Subroutine to_AZIMUTH_END: called CC=1 SCAR=0.0
Subroutine to_AZIMUTH_START: called CC=1 SCAR=0.0
Subroutine to_BACKEND: called CC=1 SCAR=0.0
Subroutine to_BANDWIDTH_MODE: called CC=1 SCAR=0.0
Subroutine to_CHOP_ANGLE: called CC=1 SCAR=0.0
Subroutine to_CHOP_COORDINATE_SYSTEM: called CC=1 SCAR=0.0
Subroutine to_CHOP_FREQUENCY: called CC=1 SCAR=0.0
Subroutine to_CHOP_THROW: called CC=1 SCAR=0.0
Subroutine to_DATA_UNITS: called CC=1 SCAR=0.0
Subroutine to_DEC_SCALE: called CC=1 SCAR=0.0
Subroutine to_DR_RECIPE: called CC=1 SCAR=0.0
Subroutine to_ELEVATION_END: called CC=1 SCAR=0.0
Subroutine to_ELEVATION_START: called CC=1 SCAR=0.0
Subroutine to_FILENAME: called CC=1 SCAR=0.0
Subroutine to_FRONTEND: called CC=1 SCAR=0.0
Subroutine to_HUMIDITY: called CC=1 SCAR=0.0
Subroutine to_INSTRUMENT: called CC=1 SCAR=0.0
Subroutine to_LATITUDE: called CC=1 SCAR=0.0
Subroutine to_LONGITUDE: called CC=1 SCAR=0.0
Subroutine to_MSBID: called CC=1 SCAR=0.0
Subroutine to_MSB_TRANSACTION_ID: called CC=1 SCAR=0.0
Subroutine to_NUMBER_OF_CYCLES: called CC=1 SCAR=0.0
Subroutine to_OBJECT: called CC=1 SCAR=0.0
Subroutine to_OBSERVATION_NUMBER: called CC=1 SCAR=0.0
Subroutine to_OBSERVED_SIDEBAND: called CC=1 SCAR=0.0
Subroutine to_PROJECT: called CC=1 SCAR=0.0
Subroutine to_RA_SCALE: called CC=1 SCAR=0.0
Subroutine to_SCAN_PATTERN: called CC=1 SCAR=0.0
Subroutine to_SHIFT_TYPE: called CC=1 SCAR=0.0
Subroutine to_SIDEBAND_MODE: called CC=1 SCAR=0.0
Subroutine to_SPECIES: called CC=1 SCAR=0.0
Subroutine to_SPECTRUM_NUMBER: called CC=1 SCAR=0.0
Subroutine to_STANDARD: called CC=1 SCAR=0.0
Subroutine to_SUBSYSTEM_NUMBER: called CC=1 SCAR=0.0
Subroutine to_SWITCH_MODE: called CC=1 SCAR=0.0
Subroutine to_TAI_UTC_CORRECTION: called CC=1 SCAR=0.0
Subroutine to_TELESCOPE: called CC=1 SCAR=0.0
Subroutine to_TRACKING_SIDEBAND: called CC=1 SCAR=0.0
Subroutine to_UT1_UTC_CORRECTION: called CC=1 SCAR=0.0
Subroutine to_VELOCITY_TYPE: called CC=1 SCAR=0.0
Subroutine to_WIND_BLIND: called CC=1 SCAR=0.0
Subroutine to_X_APERTURE: called CC=1 SCAR=0.0
Subroutine to_X_BASE: called CC=1 SCAR=0.0
Subroutine to_X_REFERENCE_PIXEL: called CC=1 SCAR=0.0
Subroutine to_Y_APERTURE: called CC=1 SCAR=0.0
Subroutine to_Y_BASE: called CC=1 SCAR=0.0
Subroutine to_Y_REFERENCE_PIXEL: called CC=1 SCAR=0.0
Subroutine to_CONFIGURATION_INDEX: called CC=1 SCAR=0.0
Subroutine to_DEC_BASE: called CC=1 SCAR=0.0
Subroutine to_DEC_BASE: called CC=1 SCAR=0.0
Subroutine to_DEC_SCALE: called CC=1 SCAR=0.0
Subroutine to_DEC_TELESCOPE_OFFSET: called CC=1 SCAR=0.0
Subroutine to_DETECTOR_INDEX: not called CC=1 SCAR=0.0
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod documented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod documented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod documented
Pod documented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod documented
Pod undocumented
Pod undocumented
Pod documented
Pod documented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
300 #print "Sub: $sub\n";
301  
302 # Now the from
303 4876 $subname = "from_$key";
304 4876 $sub = qq/ $p sub $subname { (\"$fhdr\", \$_[1]->{\"$key\"}); } $ep/;
305 4876 ▶ eval "$sub";
Subroutine from_AIRMASS_END: called CC=1 SCAR=0.0
Subroutine from_AIRMASS_START: called CC=1 SCAR=0.0
Subroutine from_AMBIENT_TEMPERATURE: called CC=1 SCAR=0.0
Subroutine from_AZIMUTH_END: called CC=1 SCAR=0.0
Subroutine from_AZIMUTH_START: called CC=1 SCAR=0.0
Subroutine from_BACKEND: called CC=1 SCAR=0.0
Subroutine from_BANDWIDTH_MODE: called CC=1 SCAR=0.0
Subroutine from_CHOP_ANGLE: called CC=1 SCAR=0.0
Subroutine from_CHOP_COORDINATE_SYSTEM: called CC=1 SCAR=0.0
Subroutine from_CHOP_FREQUENCY: called CC=1 SCAR=0.0
Subroutine from_CHOP_THROW: called CC=1 SCAR=0.0
Subroutine from_DATA_UNITS: called CC=1 SCAR=0.0
Subroutine from_DEC_SCALE: called CC=1 SCAR=0.0
Subroutine from_DR_RECIPE: called CC=1 SCAR=0.0
Subroutine from_ELEVATION_END: called CC=1 SCAR=0.0
Subroutine from_ELEVATION_START: called CC=1 SCAR=0.0
Subroutine from_FILENAME: called CC=1 SCAR=0.0
Subroutine from_FRONTEND: called CC=1 SCAR=0.0
Subroutine from_HUMIDITY: called CC=1 SCAR=0.0
Subroutine from_INSTRUMENT: called CC=1 SCAR=0.0
Subroutine from_LATITUDE: called CC=1 SCAR=0.0
Subroutine from_LONGITUDE: called CC=1 SCAR=0.0
Subroutine from_MSBID: called CC=1 SCAR=0.0
Subroutine from_MSB_TRANSACTION_ID: called CC=1 SCAR=0.0
Subroutine from_NUMBER_OF_CYCLES: called CC=1 SCAR=0.0
Subroutine from_OBJECT: called CC=1 SCAR=0.0
Subroutine from_OBSERVATION_NUMBER: called CC=1 SCAR=0.0
Subroutine from_OBSERVED_SIDEBAND: called CC=1 SCAR=0.0
Subroutine from_PROJECT: called CC=1 SCAR=0.0
Subroutine from_RA_SCALE: called CC=1 SCAR=0.0
Subroutine from_SCAN_PATTERN: called CC=1 SCAR=0.0
Subroutine from_SHIFT_TYPE: called CC=1 SCAR=0.0
Subroutine from_SIDEBAND_MODE: called CC=1 SCAR=0.0
Subroutine from_SPECIES: called CC=1 SCAR=0.0
Subroutine from_SPECTRUM_NUMBER: called CC=1 SCAR=0.0
Subroutine from_STANDARD: called CC=1 SCAR=0.0
Subroutine from_SUBSYSTEM_NUMBER: called CC=1 SCAR=0.0
Subroutine from_SWITCH_MODE: called CC=1 SCAR=0.0
Subroutine from_TAI_UTC_CORRECTION: called CC=1 SCAR=0.0
Subroutine from_TELESCOPE: called CC=1 SCAR=0.0
Subroutine from_TRACKING_SIDEBAND: called CC=1 SCAR=0.0
Subroutine from_UT1_UTC_CORRECTION: called CC=1 SCAR=0.0
Subroutine from_VELOCITY_TYPE: called CC=1 SCAR=0.0
Subroutine from_WIND_BLIND: called CC=1 SCAR=0.0
Subroutine from_X_APERTURE: called CC=1 SCAR=0.0
Subroutine from_X_BASE: called CC=1 SCAR=0.0
Subroutine from_X_REFERENCE_PIXEL: called CC=1 SCAR=0.0
Subroutine from_Y_APERTURE: called CC=1 SCAR=0.0
Subroutine from_Y_BASE: called CC=1 SCAR=0.0
Subroutine from_Y_REFERENCE_PIXEL: called CC=1 SCAR=0.0
Pod documented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod documented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod documented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod documented
Pod undocumented
Pod undocumented
306 #print "Sub: $sub\n";
307  
308 }
309  
310 # and the CONSTANT mappings (only to_GENERIC_NAME)
311 512 for my $key (keys %$const) {
312 793 my $subname = "to_$key";
313 793 my $val = $const->{$key};
314 # A method so no gain in using a null prototype
315 793 my $sub = qq/ $p sub $subname { \"$val\" } $ep /;
316 793 ▶ eval "$sub";
Subroutine to_INST_DHS: called CC=1 SCAR=0.0
Pod documented
317 }
318  
319 # the null mappings
320 512 ▶ if (defined $null) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $null) 272 240 512
321 272 for my $key (@$null) {
322 # to generic
323 62 my $subname = "to_$key";
324 62 my $sub = qq/ $p sub $subname { } $ep /;
325 62 eval "$sub";
326  
327 # to generic
328 62 $subname = "from_$key";
329 62 $sub = qq/ $p sub $subname { return (); } $ep /;
330 62 eval "$sub";
331 }
332 }
333  
334 # the mappings that are unit mappings but from the end of a subheader
335 # group (eg ELEVATION_END)
336 512 ▶ if (defined $endobs) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $endobs) 30 482 512
337 30 for my $key (keys %$endobs) {
338  
339 # Get the original FITS header name
340 60 my $fhdr = $endobs->{$key};
341  
342 # print "Processing $key and $ohdr and $fhdr\n";
343  
344 # First generate the code to generate Generic headers
345 60 my $subname = "to_$key";
346 60 my $sub = qq/ $p sub $subname {
347 my \@allresults = \$_[0]->via_subheader_undef_check(\$_[1],\"$fhdr\");
348 return \$allresults[-1];
349 } $ep /;
350 60 eval "$sub";
351 #print "Sub: $sub\n";
352  
353 # Now the from
354 60 $subname = "from_$key";
355 60 $sub = qq/ $p sub $subname { (\"$fhdr\", \$_[1]->{\"$key\"}); } $ep/;
356 60 eval "$sub";
357 #print "Sub: $sub\n";
358  
359 }
360 }
361  
362 }
363  
364 =item B<nint>
365  
366 Return the nearest integer to a supplied floating point
367 value. 0.5 is rounded up.
368  
369 $int = Astro::FITS::HdrTrans->nint( $value );
370  
371 =cut
372  
373 37 ▶sub nint {
Subroutine nint: called CC=2 SCAR=7.0
Pod documented
374 37 my $class = shift;
375 24 my $value = shift;
376  
377 20 ▶ if ($value >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value >= 0) { } 8 0 8
378 15 return (int($value + 0.5));
379 } else {
380 18 return (int($value - 0.5));
381 }
382 }
383  
384 =item B<_parse_iso_date>
385  
386 Converts a UT date in form YYYY-MM-DDTHH:MM:SS.sss into a date
387 object (Time::Piece).
388  
389 $object = $trans->_parse_iso_date( $date );
390  
391 =cut
392  
393 81 ▶sub _parse_iso_date {
Subroutine _parse_iso_date: called CC=2 SCAR=7.0
394 81 my $self = shift;
395 74 my $datestr = shift;
396 76 my $return;
397 67 ▶ if (defined $datestr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $datestr) 65 0 65
398 # Not part of standard but we can deal with it
399 65 $datestr =~ s/Z//g;
400 # Time::Piece can not do fractional seconds. Should switch to DateTime
401 65 $datestr =~ s/\.\d+$//;
402 # parse
403 65 $return = Time::Piece->strptime( $datestr, "%Y-%m-%dT%T" );
404 }
405 65 return $return;
406 }
407  
408 =item B<_parse_yyyymmdd_date>
409  
410 Converts a UT date in format YYYYMMDD into a date object.
411  
412 $ojbect = $trans->_parse_yyyymmdd_date( $date, $sep );
413  
414 Where $sep is the separator string and can be an empty string.
415 This allows 20090215, 2009-02-15 and 2009:02:15 to be parsed
416 by the same routine by using '', '-' and ':' respectively.
417  
418 =cut
419  
420 20 ▶sub _parse_yyyymmdd_date {
Subroutine _parse_yyyymmdd_date: called CC=3 SCAR=11.0
421 8 my $self = shift;
422 8 my $datestr = shift;
423 8 my $sep = shift;
424 8 ▶ $sep = '' unless defined $sep;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sep 0 8 8
425  
426 # OSX Leopard has a completely broken strptime that can not
427 # handle %Y%m%d. We need to change the string to make it
428 # into a parseable form (or switch to DateTime).
429 8 ▶ if (!$sep) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sep) 1 7 8
430 1 $sep = "-";
431 1 $datestr = join($sep, substr($datestr,0,4),
432 substr($datestr,4,2),
433 substr($datestr,6));
434 }
435  
436 8 return Time::Piece->strptime( $datestr,join($sep,'%Y','%m','%d') );
437 }
438  
439 =item B<_add_seconds>
440  
441 Add the supplied number of seconds to the supplied time object
442 and return a new object.
443  
444 $new = $trans->_add_seconds( $base, $delta );
445  
446 =cut
447  
448 13 ▶sub _add_seconds {
Subroutine _add_seconds: called CC=1 SCAR=6.9
449 0 my $self = shift;
450 0 my $base = shift;
451 0 my $delta = shift;
452 0 return ($base + Time::Seconds->new( $delta ) );
453 }
454  
455 =item B<_utdate_to_object>
456  
457 Converts a UT date in YYYYMMDD format to a date object at midnight.
458  
459 $obj = $trans->_utdate_to_object( $YYYYMMDD );
460  
461 =cut
462  
463 14 ▶sub _utdate_to_object {
Subroutine _utdate_to_object: called CC=1 SCAR=6.9
464 0 my $self = shift;
465 0 my $utdate = shift;
466 0 my $year = substr($utdate, 0, 4);
467 0 my $month= substr($utdate, 4, 2);
468 0 my $day = substr($utdate, 6, 2);
469 0 my $basedate = $self->_parse_iso_date( $year."-".$month ."-".$day.
470 "T00:00:00");
471 0 return $basedate;
472 }
473  
474 =item B<cosdeg>
475  
476 Return the cosine of the angle. The angle must be in degrees.
477  
478 =cut
479  
480 12 ▶sub cosdeg {
Subroutine cosdeg: called CC=1 SCAR=6.9
Pod documented
481 0 my $self = shift;
482 0 my $deg = shift;
483 0 cos( deg2rad($deg) );
484 }
485  
486 =item B<sindeg>
487  
488 Return the sine of the angle. The angle must be in degrees.
489  
490 =cut
491  
492 7 ▶sub sindeg {
Subroutine sindeg: called CC=1 SCAR=6.9
Pod documented
493 0 my $self = shift;
494 0 my $deg = shift;
495 0 sin( deg2rad($deg) );
496 }
497  
498 =item B<via_subheader>
499  
500 For the supplied FITS header item, first check the primary header
501 for existence, then check SUBHEADERS, then check "In" named subheaders.
502  
503 In scalar context returns the first value that matches.
504  
505 $value = $trans->via_subheader( $FITS_headers, $keyword );
506  
507 In list context returns all the available values in order.
508  
509 @values = $trans->via_subheader( $FITS_headers, $keyword );
510  
511 =cut
512  
513 980 ▶sub via_subheader {
Subroutine via_subheader: called CC=13 SCAR=37.4
Pod documented
514 975 my $self = shift;
515 975 my $FITS_headers = shift;
516 975 my $keyword = shift;
517  
518 975 my @values;
519 975 ▶ if (exists $FITS_headers->{$keyword}
Branch: 3 branches
BranchTrueFalseTotal
if (exists $FITS_headers->{$keyword} and defined $FITS_headers->{$keyword}) { } 717 258 975
elsif ($FITS_headers->{'SUBHEADERS'} and exists $FITS_headers->{'SUBHEADERS'}[0]{$keyword}) { } 0 258 258
elsif (exists $FITS_headers->{'I1'} and exists $FITS_headers->{'I1'}{$keyword}) { } 0 516 516
Condition100%3/3
exists $FITS_headers->{$keyword} and defined $FITS_headers->{$keyword}
!ll&&!rl&&r
24810717
MC/DC100%2/2
exists $FITS_headers->{$keyword} and defined $FITS_headers->{$keyword}
exists $FITS_headers->{$keyword} defined $FITS_headers->{$keyword}
Truth table100%3/3
A and B
A= exists $FITS_headers->{$keyword}B= defined $FITS_headers->{$keyword}
ABresult
0X0
100
111
520 && defined $FITS_headers->{$keyword}) {
521  
522 717 ▶ if ( ref( $FITS_headers->{$keyword} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $FITS_headers->{$keyword} eq 'ARRAY') { } 0 717 717
523 0 @values = @{$FITS_headers->{$keyword}};
524 } else {
525 717 push (@values,$FITS_headers->{$keyword});
526 }
527 ▶ } elsif ( $FITS_headers->{SUBHEADERS}
Condition66%2/3
$FITS_headers->{"SUBHEADERS"} and exists $FITS_headers->{"SUBHEADERS"}[0]{$keyword}
!ll&&!rl&&r
1031550
MC/DC0%0/2
$FITS_headers->{"SUBHEADERS"} and exists $FITS_headers->{"SUBHEADERS"}[0]{$keyword}
$FITS_headers->{"SUBHEADERS"} exists $FITS_headers->{"SUBHEADERS"}[0]{$keyword}
Truth table66%2/3
A and B
A= $FITS_headers->{"SUBHEADERS"}B= exists $FITS_headers->{"SUBHEADERS"}[0]{$keyword}
ABresult
0X0
100
111
528 && exists $FITS_headers->{SUBHEADERS}->[0]->{$keyword}) {
529 0 my @subs = @{$FITS_headers->{SUBHEADERS}};
530 0 for my $s (@subs) {
531 0 ▶ if (exists $s->{$keyword} && defined $s->{$keyword}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $s->{$keyword} and defined $s->{$keyword}) 0 0 0
Condition0%0/3
exists $s->{$keyword} and defined $s->{$keyword}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $s->{$keyword} and defined $s->{$keyword}
exists $s->{$keyword} defined $s->{$keyword}
Truth table0%0/3
A and B
A= exists $s->{$keyword}B= defined $s->{$keyword}
ABresult
0X0
100
111
532 0 push(@values, $s->{$keyword});
533 }
534 }
535 ▶ } elsif (exists $FITS_headers->{I1}
Condition33%1/3
exists $FITS_headers->{"I1"} and exists $FITS_headers->{"I1"}{$keyword}
!ll&&!rl&&r
25800
MC/DC0%0/2
exists $FITS_headers->{"I1"} and exists $FITS_headers->{"I1"}{$keyword}
exists $FITS_headers->{"I1"} exists $FITS_headers->{"I1"}{$keyword}
Truth table33%1/3
A and B
A= exists $FITS_headers->{"I1"}B= exists $FITS_headers->{"I1"}{$keyword}
ABresult
0X0
100
111
536 && exists $FITS_headers->{I1}->{$keyword}) {
537 # need to find out how many In we have
538 0 my $i = 1;
539 0 ▶ while (exists $FITS_headers->{"I$i"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$FITS_headers{"I$i"}) 0 0 0
540 0 push(@values, $FITS_headers->{"I$i"}->{$keyword});
541 0 $i++;
542 }
543 }
544  
545 975 ▶ return (wantarray ? @values : $values[0] );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 975 0 975
546 }
547  
548 =item B<via_subheader_undef_check>
549  
550 Version of via_subheader that removes undefined values from the list before
551 returning the answer. Useful for SCUBA-2 where the first dark may not include
552 the TCS information.
553  
554 Same interface as via_subheader.
555  
556 =cut
557  
558 904 ▶sub via_subheader_undef_check {
Subroutine via_subheader_undef_check: called CC=2 SCAR=6.9
Pod documented
559 901 my $self = shift;
560 901 my @values = $self->via_subheader( @_ );
561  
562 # completely filter out undefs
563 901 @values = grep { defined $_ } @values;
564 901 ▶ return (wantarray ? @values : $values[0] );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 38 863 901
565 }
566  
567 =back
568  
569 =head1 PROTECTED IMPORTS
570  
571 Not all translation methods warrant a full blown inheritance. For
572 cases where one or two translation routines should be imported
573 (e.g. reading DATE-OBS FITS standard headers without importing the
574 additional FITS methods) a special import routine can be used when
575 using the class.
576  
577 use Astro::FITS::HdrTrans::FITS qw/ ROTATION /;
578  
579 This will load the from_ROTATION and to_ROTATION methods into
580 the namespace.
581  
582 =cut
583  
584 36 ▶sub import {
Subroutine import: called CC=6 SCAR=18.8
585 35 my $class = shift;
586  
587 # this is where we are going to install the methods
588 35 my $callpkg = caller();
589  
590 # Prepend the from_ and to_ prefixes
591 35 for my $key (@_) {
592 # The key can be fully specified with from_ and to_ already
593 # In that case we do not want to loop over from_ and to_
594 20 my @directions = qw/ from_ to_ /;
595 20 ▶ if ($key =~ /^from_/ || $key =~ /^to_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^from_/ or $key =~ /^to_/) 0 20 20
Condition33%1/3
$key =~ /^from_/ or $key =~ /^to_/
l!l&&r!l&&!r
0020
MC/DC0%0/2
$key =~ /^from_/ or $key =~ /^to_/
$key =~ /^from_/ $key =~ /^to_/
Truth table33%1/3
A or B
A= $key =~ /^from_/B= $key =~ /^to_/
ABresult
1X1
011
000
596 0 @directions = ( '' ); # empty prefix
597 }
598  
599 20 for my $dir (@directions) {
600 40 my $method = $dir . $key;
601 #print "Importing method $method\n";
602 52 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
603  
604 40 ▶ if (!defined *{"$class\::$method"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined *{"${class}::$method";}) 0 40 40
605 0 croak "Method $method is not available for export from class $class";
606 }
607  
608 # assign it
609 40 *{"$callpkg\::$method"} = \&{"$class\::$method"};
610 }
611 }
612  
613 }
614  
615 =head1 WRITING A TRANSLATION CLASS
616  
617 In order to create a translation class for a new instrument it is
618 first necessary to work out the different types of translations that
619 are required; whether they are unit mappings (a simple change of
620 keyword but no change in value), constant mappings (a constant is
621 returned independently of the FITS header), mappings that already
622 exist in another class or complex mappings that have to be explicitly
623 coded.
624  
625 All translation classes must ultimately inherit from
626 C<Astro::FITS::HdrTrans::Base>.
627  
628 The first step in creation of a class is to handle the "can this class
629 translate the supplied headers" query that will be requested from
630 the C<Astro::FITS::HdrTrans> package. If the instrument name is present
631 in the standard "INSTRUME" FITS header then this can be achieved simply
632 by writing a C<this_instrument> method in the subclass that will return
633 the name of the instrument that can be translated. If a more complex
634 decision is required it will be necessary to subclass the C<can_translate>
635 method. This takes the headers that are to be translated (either in FITS
636 or generic form since the method is queried for either direction) and
637 returns a boolean indicating whether the class can be used.
638  
639 Once the class can declare it's translation instrument the next
640 step is to write the actual translation methods themselves. If any
641 unit- or constant-mappings are required they can be setup by defining
642 the %UNIT_MAP and %CONST_MAP (the names are unimportant) hashes
643 and calling the base class automated method constructor:
644  
645 __PACKAGE__->_generate_lookup_methods( \%CONST_MAP, \%UNIT_MAP );
646  
647 If your translations are very similar to an existing set of translations
648 you can inherit from that class instead of C<Astro::FITS::HdrTrans::Base>.
649 Multiple inheritance is supported if, for example, you need to
650 inherit from both the standard FITS translations (eg for DATE-OBS
651 processing) and from a more telescope-specific set of translations.
652  
653 If inheritance causes some erroneous mappings to leak through it is
654 possible to disable a specific mapping by specifying a @NULL_MAP
655 array to the method generation. This is an array of generic keywords.
656  
657 __PACKAGE__->_generate_lookup_methods( \%CONST_MAP, \%UNIT_MAP,
658 \@NULL_MAP );
659  
660 If a subset of translation methods are required from another class
661 but there is no desire to inherit the full set of methods then it
662 is possible to import specific translation methods from other classes.
663  
664 use Astro::FITS::HdrTrans::FITS qw/ UTSTART UTEND /;
665  
666 would import just the DATE-OBS and DATE-END handling functions from
667 the FITS class. Note that both the from- and to- translations will
668 be imported.
669  
670 At some point you may want to write your own more complex translations.
671 To do this you must write to- and from- methods. The API for all
672 the from_FITS translations is identical:
673  
674 $translation = $class->to_GENERIC_KEYWORD( \%fits_headers );
675  
676 ie given a reference to a hash of FITS headers (which can be
677 a tied C<Astro::FITS::Header> object), return a scalar value which
678 is the translated value.
679  
680 To convert from generic to FITS the interface is:
681  
682 %fits_subset = $class->from_GENERIC_KEYWORD( \%generic_header );
683  
684 ie multiple FITS keywords and values can be returned since in some
685 cases a single generic keyword is obtained by combining information
686 from multiple FITS headers.
687  
688 Finally, if this translation module is to be part of the
689 C<Astro::FITS::HdrTrans> distribution the default list of translation
690 classes must be updated in C<Astro::FITS::HdrTrans>. If this is to be
691 a runtime plugin, then the list of classes can be expanded at
692 runtime. For example, it should be possible for
693 C<Astro::FITS::HdrTrans::MyNewInst> to automatically append itself to
694 the list of known classes if the module is explicitly loaded by the
695 user (rather than dynamically loaded to test the headers).
696  
697 Some generic keywords actually return scalar objects. Any new instruments
698 must consistently return compatible objects. For example, UTDATE,
699 UTSTART and UTEND return (currently) Time::Piece objects.
700  
701 =head1 SEE ALSO
702  
703 C<Astro::FITS::HdrTrans>
704  
705 =head1 AUTHOR
706  
707 Tim Jenness E<lt>t.jenness@jach.hawaii.eduE<gt>
708  
709 =head1 COPYRIGHT
710  
711 Copyright (C) 2003-2005 Particle Physics and Astronomy Research Council.
712 All Rights Reserved.
713  
714 This program is free software; you can redistribute it and/or modify it under
715 the terms of the GNU General Public License as published by the Free Software
716 Foundation; either version 2 of the License, or (at your option) any later
717 version.
718  
719 This program is distributed in the hope that it will be useful,but WITHOUT ANY
720 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
721 PARTICULAR PURPOSE. See the GNU General Public License for more details.
722  
723 You should have received a copy of the GNU General Public License along with
724 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
725 Place,Suite 330, Boston, MA 02111-1307, USA
726  
727 =cut
728  
729 52 1;
« HdrTrans/UKIRTNew.pm ↑ Summary HdrTrans/CURVE.pm »