| 1 | # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*- | ||||||||||||||||||||
| 2 | # vim:ts=8:sw=2:et:sta:sts=2 | ||||||||||||||||||||
| 3 | ######### | ||||||||||||||||||||
| 4 | # Author: rmp@psyphi.net | ||||||||||||||||||||
| 5 | # Created: 2016-12-31 | ||||||||||||||||||||
| 6 | # | ||||||||||||||||||||
| 7 | package Ham::NOAA::Sunspot; | ||||||||||||||||||||
| 8 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 3 | ▶ | use LWP::UserAgent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 3 | our $DEFAULT_URL = q[http://services.swpc.noaa.gov/text/predicted-sunspot-radio-flux.txt]; | |||||||||||||||||||
| 13 | 3 | our $VERSION = q[0.0.2]; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 2 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 16 | 2 | my ($class, $ref) = @_; | |||||||||||||||||||
| 17 | 2 | ▶ | if(!ref $ref) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 18 | 2 | $ref = {}; | |||||||||||||||||||
| 19 | } | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 2 | bless $ref, $class; | |||||||||||||||||||
| 22 | 2 | return $ref; | |||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 2 | ▶ | sub url { | ||||||||||||||||||
|
Subroutine
url: called CC=3 SCAR=13.3
Pod
documented
| |||||||||||||||||||||
| 26 | 2 | my ($self, $url) = @_; | |||||||||||||||||||
| 27 | 2 | ▶ | if(defined $url) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | 0 | $self->{url} = $url; | |||||||||||||||||||
| 29 | } | ||||||||||||||||||||
| 30 | 2 | ▶ | return $self->{url} || $DEFAULT_URL; | ||||||||||||||||||
|
Condition33%1/3
$self->{'url'} || $Ham::NOAA::Sunspot::DEFAULT_URL
MC/DC0%0/2
$self->{'url'} || $Ham::NOAA::Sunspot::DEFAULT_URL
$self->{'url'}
$Ham::NOAA::Sunspot::DEFAULT_URL
Truth table33%1/3
A || B
A= $self->{'url'}B= $Ham::NOAA::Sunspot::DEFAULT_URL
| |||||||||||||||||||||
| 31 | } | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 2 | ▶ | sub _data { | ||||||||||||||||||
|
Subroutine
_data: called CC=5 SCAR=16.3
| |||||||||||||||||||||
| 34 | 2 | my ($self) = @_; | |||||||||||||||||||
| 35 | 2 | ▶ | if($self->{_data}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | 0 | return $self->{_data}; | |||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 2 | my $ua = LWP::UserAgent->new(); | |||||||||||||||||||
| 40 | 2 | $ua->agent(qq[Ham::NOAA::Sunspot $VERSION]); | |||||||||||||||||||
| 41 | 2 | $ua->env_proxy(); | |||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 2 | my $res = $ua->get($self->url); | |||||||||||||||||||
| 44 | 2 | ▶ | if(!$res->is_success) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | 0 | return; | |||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 2 | my $content = $res->decoded_content; | |||||||||||||||||||
| 49 | 2 | my $lines = [split /[\r\n]+/smx, $content]; | |||||||||||||||||||
| 50 | 2 | my $data = []; | |||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 2 | for my $line (@{$lines}) { | |||||||||||||||||||
| 53 | 124 | ▶ | if($line =~ m{^\s*[#:]}smx) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | 38 | next; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 86 | my ($year, $month, $sunspot_predicted, $sunspot_high, $sunspot_low, $flux_predicted, $flux_high, $flux_low) = $line =~ m{(\S+)}smxg; | |||||||||||||||||||
| 58 | 86 | push @{$data}, { | |||||||||||||||||||
| 59 | year => 0+$year, | ||||||||||||||||||||
| 60 | month => 0+$month, | ||||||||||||||||||||
| 61 | sunspot => { | ||||||||||||||||||||
| 62 | predicted => $sunspot_predicted, | ||||||||||||||||||||
| 63 | high => $sunspot_high, | ||||||||||||||||||||
| 64 | low => $sunspot_low, | ||||||||||||||||||||
| 65 | }, | ||||||||||||||||||||
| 66 | flux => { | ||||||||||||||||||||
| 67 | predicted => $flux_predicted, | ||||||||||||||||||||
| 68 | high => $flux_high, | ||||||||||||||||||||
| 69 | low => $flux_low, | ||||||||||||||||||||
| 70 | }, | ||||||||||||||||||||
| 71 | }; | ||||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 2 | $self->{_data} = $data; | |||||||||||||||||||
| 75 | 2 | return $data; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 2 | ▶ | sub _by_year_month { | ||||||||||||||||||
|
Subroutine
_by_year_month: called CC=3 SCAR=11.2
| |||||||||||||||||||||
| 79 | 2 | my $self = shift; | |||||||||||||||||||
| 80 | 2 | ▶ | if($self->{_year_month}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 0 | return $self->{_year_month}; | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 2 | my $data = $self->_data; | |||||||||||||||||||
| 85 | 2 | for my $i (@{$data}) { | |||||||||||||||||||
| 86 | $self->{_year_month}->{$i->{year}}->{$i->{month}} = { | ||||||||||||||||||||
| 87 | sunspot => $i->{sunspot}, | ||||||||||||||||||||
| 88 | flux => $i->{flux}, | ||||||||||||||||||||
| 89 | 86 | }; | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 2 | return $self->{_year_month}; | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 1 | ▶ | sub sunspot_by_year_month { | ||||||||||||||||||
|
Subroutine
sunspot_by_year_month: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 96 | 1 | my ($self, $year, $month) = @_; | |||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 1 | return $self->_by_year_month->{0+$year}->{0+$month}->{sunspot}; | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 1 | ▶ | sub flux_by_year_month { | ||||||||||||||||||
|
Subroutine
flux_by_year_month: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 102 | 1 | my ($self, $year, $month) = @_; | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 1 | return $self->_by_year_month->{0+$year}->{0+$month}->{flux}; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 3 | 1; | |||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | __END__ | ||||||||||||||||||||