blib/lib/Astro/DSS/JPEG.pm

filter: Statementstmt 100% 73 / 73 Branchbran 100% 26 / 26 Conditioncond 100% 27 / 27 MC/DCmcdc 100% 15 / 15 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 100% 156 / 156 CC 26 SCAR 18.1
CC
26
Cov
100%
_process_options
12.0
get_image
6.0
new
4.0
CRAP 6.1
↑ Summary
1 package Astro::DSS::JPEG;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use List::Util 'min';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Astro::Coord::Precession qw/precess read_coordinates/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =encoding utf8
13  
14 =head1 NAME
15  
16 Astro::DSS::JPEG - Download color JPEG images from the Digitized Sky Survey
17  
18 =head1 VERSION
19  
20 Version 0.03
21  
22 =cut
23  
24 2 our $VERSION = '0.03';
25  
26 =head1 SYNOPSIS
27  
28 use Astro::DSS::JPEG;
29  
30 my $dss = Astro::DSS::JPEG->new();
31  
32 #get an object (Triangulum galaxy M33) by name at default 1000x1000,
33 #getting coordinates and frame size automatically from SIMBAD
34 my $image = $dss->get_image(target => 'M33');
35  
36 #Save an image directly to file manually specifying coordinates, frame of
37 #90x90 arcmin, 2048x2048 pixels
38 $dss->get_image(
39 ra => '01 33 50.904',
40 dec => '+30 39 35.79',
41 angular_size => 90,
42 pixel_size => 2048,
43 filename => $filename
44 );
45  
46 #In one go, save Andromeda Galaxy to andromeda.jpg
47 Astro::DSS::JPEG->new->get_image(target=>'Andromeda Galaxy', filename=>'andromeda.jpg');
48  
49 =head1 DESCRIPTION
50  
51 Astro::DSS::JPEG downloads JPEG images for any location in the sky from the L<Digitized Sky Survey (DSS)|https://archive.stsci.edu/dss/>.
52 It is meant to be a simple stand alone module to access a fast JPEG-only API that
53 provides color composites made from the blue and red DSS surveys.
54 In comparison, there is an old/not updated L<Astro::DSS> module that would provide
55 access to the slow FITS/GIF interface of the separate DSS1/DSS2 surveys.
56  
57 Optionally, L<SIMBAD|http://simbad.u-strasbg.fr/simbad/> is used if you'd like to use
58 an object name/id instead of coordinates.
59  
60 =head1 CONSTRUCTOR METHODS
61  
62 =head2 C<new>
63  
64 my $dss = Astro::DSS::JPEG->new(
65 dss_url => 'http://gsss.stsci.edu/webservices/dssjpg/dss.svc/GetImage', # Optional
66 simbad_url => 'http://simbad.u-strasbg.fr/simbad/sim-id', # Optional
67 ua => $ua # Optional
68 );
69
70 All parameters are optional. The constructor by default creates an L<LWP::UserAgent>,
71 but you can pass your own with C<ua>, while the URL to the L<STScI|https://archive.stsci.edu/dss/copyright.html>
72 JPEG endpoint and the L<SIMBAD|http://simbad.u-strasbg.fr/simbad/> simple identifier query
73 interface can be redefined (e.g. if they change in the future) from the above shown defaults.
74  
75 =head1 METHODS
76  
77 =head2 C<get_image>
78  
79 my $res = $dss->get_image(
80 target => $target, # Not used if RA, Dec provided
81 ra => $ra, # Right Ascension (in hours of angle)
82 dec => $dec, # Declination (in degrees of angle)
83 epoch => $epoch, # Optional. Epoch for coordinates (otherwise J2000 assumed)
84 angular_size => 30, # Optional. Frame angular size in arcmin
85 pixel_size => 1000, # Optional. Frame size in pixels
86 filename => $filename # Optional. Filename to save image to
87 );
88  
89 Fetches an image either resolving the name through L<SIMBAD|http://simbad.u-strasbg.fr/simbad/>,
90 or using RA & Dec when they are defined. If you pass a C<filename>, the JPEG image will
91 be saved to that and the function will return an L<HTTP::Response> object, otherwise the
92 image data will be returned as a response.
93  
94 The parameters to pass:
95  
96 =over 4
97
98 =item * C<ra>
99  
100 Right ascension, from 0-24h of angle. The option is flexible, it will accept a single
101 decimal number - e.g. C<2.5> or a string with hours, minutes, secs like C<'2 30 00'> or
102 C<'2h30m30s'> etc. Single/double quotes and single/double prime symbols are accepted
103 for denoting minute, second in place of a single space which also works.
104 Uses C<Astro::Coord::Precession::read_coordinates>.
105  
106 =item * C<dec>
107  
108 Declination, from -90 to 90 degrees of angle. The option is flexible, it will accept
109 a single decimal number - e.g. C<54.5> or a string with degrees, minutes, secs like
110 C<'+54 30 00'> or C<'54°30m30s'> etc. Single/double quotes and single/double prime symbols
111 are accepted for denoting minute, second in place of a single space which also works.
112 Uses C<Astro::Coord::Precession::read_coordinates>.
113  
114 =item * C<epoch>
115  
116 DSS uses J2000.0 coordinates, but you can enter custom epoch coordinates by specifying
117 it and it will be converted with L<Astro::Coord::Precession>.
118  
119 =item * C<target>
120  
121 Do a L<SIMBAD|http://simbad.u-strasbg.fr/simbad/> lookup to get the C<target> by name
122 (e.g. 'NGC598'). Will be disregarded if C<ra> and C<dec> are defined. If you have not
123 defined an C<angular_size> and L<SIMBAD|http://simbad.u-strasbg.fr/simbad/> has one
124 defined, it will be used for the image request with an extra 50% for padding.
125  
126 =item * C<angular_size>
127  
128 Define the frame angular size in arcminutes, either as a single number (square) or
129 a comma separated string for a rectangle. Default is 0.5 degrees (equivalent to passing
130 C<30> or C<'30,30'> or even C<'30x30'>). The aspect ratio will be overriden by C<pixel_size>.
131 If you care about framing, you should definitely define C<angular_size>, as the default
132 won't be appropriate for many targets and L<SIMBAD|http://simbad.u-strasbg.fr/simbad/> often
133 does not have the info (or even has an inappropriate value). Also, large C<angular_size>
134 values can make the response slow (or even error out).
135  
136 =item * C<pixel_size>
137  
138 Define the frame size in pixels, either as a single number (square) or a comma separated
139 string for a rectangle. Default is 1000 pixels (equivalent to passing C<1000> or
140 C<'1000,1000'> or even C<'1000x1000'>). Max possible is C<'4096,4096'> total size,
141 or 1 pixel / arcsecond resolution (e.g. 30*60 = 1800 pixels for 30 arcminutes), which
142 is the full resolution DSS plates were scanned at.
143  
144 =back
145  
146 =head1 NOTES
147  
148 Some artifacts can be seen at the borders of separate "stripes" of the survey and
149 also the particular JPEG endpoint used sometimes can leave the corners as plain black
150 squares (depending on the selected frame size, as it is to do with the way it does
151 segmentation), so if you want to make sure you have a frame with no corner gaps,
152 request some more angular size than you want and crop.
153  
154 Note that the module test suite won't actually fetch data from either DSS or SIMBAD.
155 This is mainly to ensure it will not fail even if the DSS & SIMBAD endpoints change,
156 as you can still use the module by passing the updated urls to the constructor. It
157 also avoids unneeded strain to those free services.
158  
159 =cut
160  
161  
162 3 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
163 3 my ($class, %opts) = @_;
164  
165 3 my $self = {};
166 3 bless($self, $class);
167  
168 $self->{dss_url} = $opts{dss_url}
169 3 ▶ || 'http://gsss.stsci.edu/webservices/dssjpg/dss.svc/GetImage';
Condition100%2/2
$opts{'dss_url'} || 'http://gsss.stsci.edu/webservices/dssjpg/dss.svc/GetImage'
l!l
12
MC/DC100%1/1
$opts{'dss_url'} || 'http://gsss.stsci.edu/webservices/dssjpg/dss.svc/GetImage'
$opts{'dss_url'}
Truth table100%2/2
A
A= $opts{'dss_url'}
Aresult
11
00
170 $self->{simbad_url} = $opts{simbad_url}
171 3 ▶ || 'http://simbad.u-strasbg.fr/simbad/sim-id';
Condition100%2/2
$opts{'simbad_url'} || 'http://simbad.u-strasbg.fr/simbad/sim-id'
l!l
12
MC/DC100%1/1
$opts{'simbad_url'} || 'http://simbad.u-strasbg.fr/simbad/sim-id'
$opts{'simbad_url'}
Truth table100%2/2
A
A= $opts{'simbad_url'}
Aresult
11
00
172 $self->{ua} = $opts{ua}
173 3 ▶ || LWP::UserAgent->new(
Condition100%3/3
$opts{'ua'} || 'LWP::UserAgent'->new('agent', "perl/Astro::DSS::JPEG/$Astro::DSS::JPEG::VERSION")
l!l&&r!l&&!r
111
MC/DC100%2/2
$opts{'ua'} || 'LWP::UserAgent'->new('agent', "perl/Astro::DSS::JPEG/$Astro::DSS::JPEG::VERSION")
$opts{'ua'} 'LWP::UserAgent'->new('agent', "perl/Astro::DSS::JPEG/$Astro::DSS::JPEG::VERSION")
Truth table100%3/3
A || B
A= $opts{'ua'}B= 'LWP::UserAgent'->new('agent', "perl/Astro::DSS::JPEG/$Astro::DSS::JPEG::VERSION")
ABresult
1X1
011
000
174 agent => "perl/Astro::DSS::JPEG/$VERSION",
175 );
176  
177 3 return $self;
178 }
179  
180 5 ▶sub get_image {
Subroutine get_image: called CC=6 SCAR=17.9
Pod documented
181 5 my ($self, %opts) = @_;
182 5 ▶ if ($opts{target} && (! defined $opts{ra} || ! defined $opts{dec})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"target"} and not defined $opts{"ra"} && defined $opts{"dec"}) 3 2 5
Condition100%6/6
$opts{"target"} and not defined $opts{"ra"} && defined $opts{"dec"}
!ll&&!rl&&r
113
defined $opts{'ra'} && defined $opts{'dec'}
!ll&&!rl&&r
121
MC/DC100%3/3
$opts{"target"} and not defined $opts{"ra"} && defined $opts{"dec"}
$opts{"target"} defined $opts{'ra'} defined $opts{'dec'}
Truth table100%4/4
A and not(B && C)
A= $opts{"target"}B= defined $opts{'ra'}C= defined $opts{'dec'}
ABCresult
0XX0
1110
10X1
1101
183 3 my $simbad = $self->_get_simbad(%opts);
184 1 %opts = (
185 %opts,
186 %$simbad
187 );
188 }
189  
190 3 my $res = $self->_get_dss(_process_options(%opts));
191 3 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 2 1 3
192 2 ▶ return $res if $opts{filename};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"filename"} 1 1 2
193 1 return $res->decoded_content;
194 } else {
195 1 die "*ERROR* Could not access DSS at: ".$res->request->uri
196 ."\nHTTP Response:\n".$res->status_line;
197 }
198 }
199  
200 3 ▶sub _get_dss {
Subroutine _get_dss: called CC=2 SCAR=6.9
201 3 my ($self, %opts) = @_;
202  
203 my @request = (
204 $self->{dss_url} . sprintf(
205 "?POS=%f,%f&SIZE=%f,%f&ISIZE=%.0f,%.0f",
206 $opts{ra} * 15, # Convert to degrees
207 $opts{dec},
208 $opts{angular_size} / 60, # Convert to degrees
209 $opts{angular_size_y} / 60, # Convert to degrees
210 $opts{pixel_size},
211 $opts{pixel_size_y}
212 )
213 3 );
214 3 ▶ push @request, (':content_file' => $opts{filename}) if $opts{filename};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"filename"} 1 2 3
215 3 return $self->{ua}->get(@request);
216 }
217  
218 18 ▶sub _convert_coordinates {
Subroutine _convert_coordinates: called CC=3 SCAR=11.0
219 18 my %opts = @_;
220 10 ($opts{ra}, $opts{dec}) = @{read_coordinates([@opts{'ra','dec'}])}
221 18 ▶ if defined $opts{ra} && defined $opts{dec};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"ra"} and defined $opts{"dec"} 10 8 18
Condition100%3/3
defined $opts{"ra"} and defined $opts{"dec"}
!ll&&!rl&&r
7110
MC/DC100%2/2
defined $opts{"ra"} and defined $opts{"dec"}
defined $opts{"ra"} defined $opts{"dec"}
Truth table100%3/3
A and B
A= defined $opts{"ra"}B= defined $opts{"dec"}
ABresult
0X0
100
111
222 18 return %opts;
223 }
224  
225 10 ▶sub _process_options {
Subroutine _process_options: called CC=12 SCAR=24.8
226 10 my %opts = _convert_coordinates(@_);
227  
228 1 ($opts{ra}, $opts{dec}) = @{precess([@opts{'ra','dec'}], $opts{epoch},2000)}
229 10 ▶ if $opts{epoch} && $opts{epoch} != 2000;
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"epoch"} and $opts{"epoch"} != 2000 1 9 10
Condition100%3/3
$opts{"epoch"} and $opts{"epoch"} != 2000
!ll&&!rl&&r
811
MC/DC100%2/2
$opts{"epoch"} and $opts{"epoch"} != 2000
$opts{"epoch"} $opts{"epoch"} != 2000
Truth table100%3/3
A and B
A= $opts{"epoch"}B= $opts{"epoch"} != 2000
ABresult
0X0
100
111
230  
231 10 ▶ $opts{angular_size} ||= 30; # 30' Default angular size
Condition100%2/2
$opts{"angular_size"} ||= 30
l!l
55
MC/DC100%1/1
$opts{"angular_size"} ||= 30
$opts{"angular_size"}
Truth table100%2/2
A
A= $opts{"angular_size"}
Aresult
11
00
232 10 ▶ $opts{pixel_size} ||= 1000;
Condition100%2/2
$opts{"pixel_size"} ||= 1000
l!l
46
MC/DC100%1/1
$opts{"pixel_size"} ||= 1000
$opts{"pixel_size"}
Truth table100%2/2
A
A= $opts{"pixel_size"}
Aresult
11
00
233  
234 10 ▶ if ($opts{angular_size} =~ /(\S+)\s*[,xX]\s*(\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"angular_size"} =~ /(\S+)\s*[,xX]\s*(\S+)/) 4 6 10
235 4 ▶ $opts{angular_size} = $1 || 30;
Condition100%2/2
$1 || 30
l!l
31
MC/DC100%1/1
$1 || 30
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
236 4 $opts{angular_size_y} = $2;
237 }
238 10 ▶ if ($opts{pixel_size} =~ /(\S+)\s*[,xX]\s*(\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"pixel_size"} =~ /(\S+)\s*[,xX]\s*(\S+)/) 3 7 10
239 3 ▶ $opts{pixel_size} = $1 || 1000;
Condition100%2/2
$1 || 1000
l!l
21
MC/DC100%1/1
$1 || 1000
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
240 3 $opts{pixel_size_y} = $2;
241 }
242  
243 10 ▶ $opts{angular_size_y} = $opts{angular_size} unless $opts{angular_size_y};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"angular_size_y"} 7 3 10
244 $opts{pixel_size_y} = $opts{pixel_size}*$opts{angular_size_y}/$opts{angular_size}
245 10 ▶ unless $opts{pixel_size_y};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"pixel_size_y"} 8 2 10
246  
247 $opts{"pixel$_"} = min 4096, $opts{"pixel$_"}, $opts{"angular$_"}*60
248 10 for qw/_size _size_y/;
249  
250 10 return %opts;
251 }
252  
253 5 ▶sub _get_simbad {
Subroutine _get_simbad: called CC=4 SCAR=13.9
254 5 my ($self, %opts) = @_;
255  
256 my $res = $self->{ua}->get(
257 $self->{simbad_url}."?output.format=ASCII&Ident=".$opts{target}
258 5 );
259 5 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 3 2 5
260 3 my $simbad = $res->decoded_content;
261 3 ▶ if ($simbad =~ /\(ICRS,ep=J2000,eq=2000\): (\S+ \S+ \S+)\s+(\S+ \S+ \S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($simbad =~ /\(ICRS,ep=J2000,eq=2000\): (\S+ \S+ \S+)\s+(\S+ \S+ \S+)/) { } 2 1 3
262 2 $opts{ra} = $1;
263 2 $opts{dec} = $2;
264 } else {
265 1 die "*ERROR* Could not parse SIMBAD output:\n$simbad";
266 }
267 2 ▶ if ($simbad =~ /Angular size:\s*([0-9.]+)/) { # Get only largest dimension
Branch: 1 branch
BranchTrueFalseTotal
if ($simbad =~ /Angular size:\s*([0-9.]+)/) 1 1 2
268 1 $opts{angular_size} = min($1 * 1.5, 600); # Get 50% extra for framing, max out at 10 degrees
269 }
270 } else {
271 die "*ERROR* Could not access SIMBAD at: "
272 .$self->{simbad_url}."?output.format=ASCII&Ident=".$opts{target}
273 2 ."\nHTTP response:\n".$res->status_line;
274 }
275  
276 2 return \%opts;
277 }
278  
279 =head1 AUTHOR
280  
281 Dimitrios Kechagias, C<< <dkechag at cpan.org> >>
282  
283  
284 =head1 BUGS
285  
286 Please report any bugs or feature requests to C<bug-astro-dss-jpeg at rt.cpan.org>, or through
287 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Astro-DSS-JPEG>.
288 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
289  
290 You could also submit issues or even pull requests to the
291 github repo (see below).
292  
293  
294 =head1 GIT
295  
296 L<https://github.com/dkechag/Astro-DSS-JPEG>
297  
298  
299 =head1 ACKNOWLEDGEMENTS
300  
301 The DSS images are downloaded using a public api of the L<Digitized Sky Survey|https://archive.stsci.edu/dss/>
302 provided by the L<Space Telescope Science Institute|http://www.stsci.edu>.
303  
304 Targets by name/id are resolved using the L<SIMBAD Astronomical Database|http://simbad.u-strasbg.fr/simbad/>.
305  
306  
307 =head1 LICENSE AND COPYRIGHT
308  
309 This software is copyright (c) 2020 by Dimitrios Kechagias.
310  
311 This is free software; you can redistribute it and/or modify it under
312 the same terms as the Perl 5 programming language system itself.
313  
314 For the images you download with this module, please see the L<STScI site|https://archive.stsci.edu/dss/copyright.html>
315 for the full copyright info.
316  
317  
318 =cut
319  
320 2 1;
↑ Summary