File Coverage

blib/lib/Astro/FITS/HdrTrans/JAC.pm
Criterion Covered Total %
statement 47 65 72.3
branch 6 18 33.3
condition 4 15 26.6
subroutine 11 12 91.6
pod 2 2 100.0
total 70 112 62.5


line stmt bran cond sub pod time code
1             package Astro::FITS::HdrTrans::JAC;
2              
3             =head1 NAME
4              
5             Astro::FITS::HdrTrans::JAC - Base class for translation of Joint
6             Astronomy Centre instruments.
7              
8             =head1 SYNOPSIS
9              
10             use Astro::FITS::HdrTrans::JAC;
11              
12             =head1 DESCRIPTION
13              
14             This class provides a generic set of translations that are common to
15             instrumentation from the Joint Astronomy Centre. It should not be used
16             directly for translation of instrument FITS headers.
17              
18             =cut
19              
20 27     27   19367390 use 5.006;
  27         143  
21 27     27   267 use warnings;
  27         109  
  27         1680  
22 27     27   246 use strict;
  27         145  
  27         953  
23 27     27   174 use Carp;
  27         75  
  27         2624  
24              
25 27     27   19867 use DateTime;
  27         9975922  
  27         1397  
26 27     27   257 use DateTime::TimeZone;
  27         81  
  27         1838  
27             # Cache UTC definition
28             our $UTC = DateTime::TimeZone->new( name => 'UTC' );
29              
30             # Inherit from the Base translation class and not HdrTrans itself
31             # (which is just a class-less wrapper).
32              
33 27     27   185 use base qw/ Astro::FITS::HdrTrans::FITS /;
  27         79  
  27         20940  
34              
35             our $VERSION = "1.66";
36              
37             # in each class we have three sets of data.
38             # - constant mappings
39             # - unit mappings
40             # - complex mappings
41              
42             # For a constant mapping, there is no FITS header, just a generic
43             # header that is constant.
44             my %CONST_MAP = (
45             );
46              
47             # Unit mapping implies that the value propagates directly
48             # to the output with only a keyword name change.
49              
50             my %UNIT_MAP = (
51             MSBID => 'MSBID',
52             MSB_TRANSACTION_ID => 'MSBTID',
53             SHIFT_TYPE => "OPER_SFT",
54             );
55              
56             # Create the translation methods.
57             __PACKAGE__->_generate_lookup_methods( \%CONST_MAP, \%UNIT_MAP );
58              
59             =head1 METHODS
60              
61             =over 4
62              
63             =item B<translate_from_FITS>
64              
65             This routine overrides the base class implementation to enable the
66             caches to be cleared and for the location of the DATE-OBS/DATE-END field to
67             be found so that base class implementations will work correctly.
68              
69             This means that some conversion methods (in particular those using time in
70             a base class) may not work properly outside the context of a full translation
71             unless they have been subclassed locally.
72              
73             =cut
74              
75             sub translate_from_FITS {
76 19     19 1 54 my $class = shift;
77 19         51 my $headers = shift;
78              
79             # sort out DATE-OBS and DATE-END
80 19         217 $class->_fix_dates( $headers );
81              
82             # Go to the base class
83 19         166 return $class->SUPER::translate_from_FITS( $headers, @_ );
84             }
85              
86             =back
87              
88             =head1 COMPLEX CONVERSIONS
89              
90             These methods are more complicated than a simple mapping. We have to
91             provide both from- and to-FITS conversions All these routines are
92             methods and the to_ routines all take a reference to a hash and return
93             the translated value (a many-to-one mapping). The from_ methods take
94             a reference to a generic hash and return a translated hash (sometimes
95             these are many-to-many).
96              
97             =over 4
98              
99             =item B<to_OBSERVATION_ID>
100              
101             Converts the C<OBSID> header directly into the C<OBSERVATION_ID>
102             generic header, or if that header does not exist, converts the
103             C<INSTRUME>, C<RUNNR>, and C<DATE-OBS> headers into C<OBSERVATION_ID>.
104              
105             The form of the observation ID string is documented in
106             JSA/ANA/001 (http://docs.jach.hawaii.edu/JCMT/JSA/ANA/001/jsa_ana_001.pdf).
107              
108             =cut
109              
110             sub to_OBSERVATION_ID {
111 18     18 1 48 my $self = shift;
112 18         103 my $FITS_headers = shift;
113              
114 18         41 my $return;
115 18 100 66     137 if ( exists( $FITS_headers->{'OBSID'} ) &&
116             defined( $FITS_headers->{'OBSID'} ) ) {
117 3         343 $return = $FITS_headers->{'OBSID'};
118             } else {
119              
120 15         818 my $instrume = $self->to_INSTRUMENT( $FITS_headers );
121 15         608 my $obsnum = $self->to_OBSERVATION_NUMBER( $FITS_headers );
122 15         131 my $dateobs = $self->to_UTSTART( $FITS_headers );
123              
124 15 50 33     167 if ( defined $dateobs && defined $obsnum && defined $instrume ) {
      33        
125 15         104 my $datetime = $dateobs->datetime;
126 15         1507 $datetime =~ s/-//g;
127 15         67 $datetime =~ s/://g;
128              
129 15         580 $return = join '_', (lc $instrume), $obsnum, $datetime;
130             }
131             }
132              
133 18         303 return $return;
134              
135             }
136              
137             =item B<_fix_dates>
138              
139             Sort out DATE-OBS and DATE-END in cases where they are not available directly.
140             This is mainly an issue with database retrievals where the date format is not
141             FITS compliant.
142              
143             Astro::FITS::HdrTrans::JAC->_fix_dates( \%headers );
144              
145             =cut
146              
147             sub _fix_dates {
148 45     45   141 my ( $class, $FITS_headers ) = @_;
149              
150             # DATE-OBS can be from DATE_OBS
151             # For compatability with Sybase database, also accept LONGDATEOBS LONGDATE
152 45         219 __PACKAGE__->_try_dates( $FITS_headers, 'DATE-OBS', qw/ DATE_OBS LONGDATEOBS LONGDATE / );
153              
154             # DATE-END can be from DATE_END
155             # For compatability with Sybase database, also accept LONGDATEEND
156 45         161 __PACKAGE__->_try_dates( $FITS_headers, 'DATE-END', qw/ DATE_END LONGDATEEND / );
157              
158 45         117 return;
159             }
160              
161             # helper routine for _fix_dates
162             sub _try_dates {
163 90     90   314 my ( $class, $FITS_headers, $outkey, @tests ) = @_;
164              
165 90 100       379 if (!exists $FITS_headers->{$outkey}) {
166 16         174 for my $key (@tests) {
167 40 50       282 if ( exists( $FITS_headers->{$key} ) ) {
168 0         0 my $date = _convert_mysql_date( $FITS_headers->{$key} );
169 0 0       0 if( defined( $date ) ) {
170 0         0 $FITS_headers->{$outkey} = $date->datetime;
171 0         0 last;
172             }
173             }
174             }
175             }
176 90         2572 return;
177             }
178              
179             # Convert MySQL date string to DateTime object.
180             # For compatability, also accepts Sybase date strings.
181             sub _convert_mysql_date {
182 0     0     my $date = shift;
183              
184 0           $date =~ s/\s*$//;
185              
186 0 0         if ($date =~ /\s*(\d\d\d\d)-(\d\d)-(\d\d)\s+(\d{1,2}):(\d\d):(\d\d)/) {
    0          
187              
188 0           my $return = DateTime->new( year => $1,
189             month => $2,
190             day => $3,
191             hour => $4,
192             minute => $5,
193             second => $6,
194             time_zone => $UTC,
195             );
196 0           return $return;
197              
198             } elsif ($date =~ /\s*(\w+)\s+(\d{1,2})\s+(\d{4})\s+(\d{1,2}):(\d\d):(\d\d)(?::\d\d\d)?(AM|PM)/) {
199              
200 0           my $hour = $4;
201 0 0 0       if (uc($7) eq 'AM' && $hour == 12) {
    0 0        
202 0           $hour = 0;
203             } elsif ( uc($7) eq 'PM' && $hour < 12 ) {
204 0           $hour += 12;
205             }
206              
207 0           my %mon_lookup = ( 'Jan' => 1,
208             'Feb' => 2,
209             'Mar' => 3,
210             'Apr' => 4,
211             'May' => 5,
212             'Jun' => 6,
213             'Jul' => 7,
214             'Aug' => 8,
215             'Sep' => 9,
216             'Oct' => 10,
217             'Nov' => 11,
218             'Dec' => 12 );
219 0           my $month = $mon_lookup{$1};
220              
221 0           my $return = DateTime->new( year => $3,
222             month => $month,
223             day => $2,
224             hour => $hour,
225             minute => $5,
226             second => $6,
227             time_zone => $UTC,
228             );
229 0           return $return;
230              
231             } else {
232 0           return undef;
233             }
234             }
235              
236             =back
237              
238             =head1 SEE ALSO
239              
240             C<Astro::FITS::HdrTrans>, C<Astro::FITS::HdrTrans::Base>.
241              
242             =head1 AUTHOR
243              
244             Brad Cavanagh E<lt>b.cavanagh@jach.hawaii.eduE<gt>.
245              
246             =head1 COPYRIGHT
247              
248             Copyright (C) 2007 Science and Technology Facilities Council.
249             Copyright (C) 2006 Particle Physics and Astronomy Research Council.
250             All Rights Reserved.
251              
252             This program is free software; you can redistribute it and/or modify
253             it under the terms of the GNU General Public License as published by
254             the Free Software Foundation; either Version 2 of the License, or (at
255             your option) any later version.
256              
257             This program is distributed in the hope that it will be useful,but
258             WITHOUT ANY WARRANTY; without even the implied warranty of
259             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
260             General Public License for more details.
261              
262             You should have received a copy of the GNU General Public License
263             along with this program; if not, write to the Free Software
264             Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
265             USA.
266              
267             =cut
268              
269             1;