| 1 | package Geomag::Kyoto::Dst; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.008; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use LWP::Simple; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Time::Local; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | @Geomag::Kyoto::Dst::FILES = qw(Dstqthism.html Dstqlastm.html); | |||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | $Geomag::Kyoto::Dst::BASE = 'http://swdcdb.kugi.kyoto-u.ac.jp/dstdir/dst1/q/'; | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # Values stored in a hash: $time => $dst_val | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=11 SCAR=40.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | my $class = shift || 'Geomag::Kyoto::Dst'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 'Geomag::Kyoto::Dst'
MC/DC0%0/1
shift() || 'Geomag::Kyoto::Dst'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | my $dst = bless { values => {}}, $class; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | my %args = (@_); | |||||||||||||||||||||||||||||||||||||||||
| 22 | # default if no args is to fetch thism and lastm | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | if (!@_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | return $dst->_parse_base(base=>$Geomag::Kyoto::Dst::BASE, files=> [@Geomag::Kyoto::Dst::FILES]); | |||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # if we have a single file => arg, then parse that file only | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | if (@_ == 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | ▶ | if ($args{file}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | return $dst->_parse_file($args{file}); | |||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | if ($args{url}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | return $dst->_parse_url($args{url}); | |||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | if ($args{files}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | foreach (@{$args{files}}) { | |||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | $dst->_parse_file($_); | |||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | return $dst; | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if (@_ == 4) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | $dst->_except("bad args to new()") unless $args{base} && $args{files}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args{"base"} and $args{"files"}
MC/DC0%0/2
$args{"base"} and $args{"files"}
$args{"base"}
$args{"files"}
Truth table0%0/3
A and B
A= $args{"base"}B= $args{"files"}
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | return $dst->_parse_base(base=>$args{base}, files => $args{files}); | |||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | $dst->_except("bad args to new()"); | |||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 2 | ▶ | sub get_array { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_array: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 2 | my $dst = shift; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 2 | my @vals; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 2 | ▶ | while (my ($time,$val) = each %{$dst->{values}}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 114 | push @vals, [$time, $val]; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | ▶ | if (my $start = $args{start}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | @vals = grep {$_->[0] >= $start} @vals; | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | ▶ | if (my $end = $args{end}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 1 | @vals = grep {$_->[0] <= $end} @vals; | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 2 | @vals = sort {$a->[0] <=> $b->[0]} @vals; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | return \@vals; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub get_hash { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_hash: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | my $dst = shift; | |||||||||||||||||||||||||||||||||||||||||
| 70 | # no limits... | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if (!@_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | return $dst->{values}; | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | my %ret; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | my ($start, $end) = @args{qw(start end)}; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | while(my($time, $val) = each %{$dst->{values}}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if (!$start || $time >= $start) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $start or $time >= $start
MC/DC0%0/2
not $start or $time >= $start
not $start
$time >= $start
Truth table0%0/3
not(A) or B
A= not $startB= $time >= $start
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | if (!$end || $time <= $end) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $end or $time <= $end
MC/DC0%0/2
not $end or $time <= $end
not $end
$time <= $end
Truth table0%0/3
not(A) or B
A= not $endB= $time <= $end
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | $ret{$time} = $val; | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | return \%ret; | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 1 | ▶ | sub _parse_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_file: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 1 | my $dst = shift; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | my $file = shift; | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 1 | ▶ | open(my $fh, "<$file") or $dst->_except("Cannot open $file $!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 1 | my $contents = do {local $/; <$fh>}; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | close($fh); | |||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | return $dst->_parse_scalar($contents); | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub _parse_url { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_url: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | my $dst =shift; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | my $url = shift; | |||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my $contents = get($url); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | $dst->_except("Failed to get $url") unless defined $contents; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | return $dst->_parse_scalar($contents); | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub _parse_base { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_base: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | my $dst = shift; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | my %args = @_; # base, files | |||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | my $base = $args{base}; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | my @files = @{$args{files}}; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | foreach my $file (@files) { | |||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | $dst->_parse_url("$base/$file"); | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | return $dst; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | ▶ | sub _parse_scalar { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_scalar: called CC=7 SCAR=19.7
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 1 | my $dst = shift; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | my $data = shift; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 1 | my @lines = split(/\n|\r\n|\r/, $data); | |||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 1 | my ($mon, $year, $day); | |||||||||||||||||||||||||||||||||||||||||
| 126 | 1 | my @months = qw(january february march | |||||||||||||||||||||||||||||||||||||||||
| 127 | april may june | ||||||||||||||||||||||||||||||||||||||||||
| 128 | july august september | ||||||||||||||||||||||||||||||||||||||||||
| 129 | october november december); | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 1 | my $mnths = join("|", @months); | |||||||||||||||||||||||||||||||||||||||||
| 131 | 1 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||
| 132 | 1 | my %m_num = map {$_ => $i++} @months; | |||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 1 | foreach (@lines) { | |||||||||||||||||||||||||||||||||||||||||
| 135 | 68 | ▶ | /^\s+($mnths)\s+(\d{4})/i && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 1 | $mon = $m_num{lc($1)}; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 1 | $year = +$2; | |||||||||||||||||||||||||||||||||||||||||
| 138 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 139 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 67 | ▶ | if (/^DAY/ && !defined($mon) && !defined($year)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
/^DAY/ and not defined $mon and not defined $year
/^DAY/ and not defined $mon
MC/DC0%0/3
/^DAY/ and not defined $mon and not defined $year
/^DAY/
not defined $mon
not defined $year
Truth table50%2/4
A and not(B) and not(C)
A= /^DAY/B= not defined $monC= not defined $year
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | $dst->_except("Did not find month or year in data"); | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | # optional space, day num, space 8 4 char groups, space, 8 4 char groups, space, 8 4 char groups | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 67 | ▶ | /^ ?(\d{1,2}) ([ 0-9-]{32}) ([ 0-9-]{32}) ([ 0-9-]{32})/ && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 31 | $day = $1; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 31 | $dst->_parse_vals($year, $mon, $day, "$2$3$4"); | |||||||||||||||||||||||||||||||||||||||||
| 147 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 1 | return $dst; | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 31 | ▶ | sub _parse_vals { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_vals: called CC=7 SCAR=20.6
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 31 | my $dst = shift; | |||||||||||||||||||||||||||||||||||||||||
| 154 | 31 | my ($year, $mon, $day, $sval) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 31 | ▶ | $dst->_except("No year, mon or day") unless defined($year) && defined($mon) && defined($day); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $year and defined $mon and defined $day
defined $year and defined $mon
MC/DC0%0/3
defined $year and defined $mon and defined $day
defined $year
defined $mon
defined $day
Truth table25%1/4
A and B and C
A= defined $yearB= defined $monC= defined $day
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | # four character groups | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 31 | ▶ | for (my $hour = 0; $hour < 24; $hour++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 744 | my $val = substr($sval, $hour*4, 4); | |||||||||||||||||||||||||||||||||||||||||
| 160 | # only add valid values | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 744 | ▶ | next if ($val eq "9999"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 57 | ▶ | next if ($val eq " "); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 57 | $dst->_add_val($year, $mon, $day, $hour, $val); | |||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 31 | return $dst; | |||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | # expects values to be valid | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 57 | ▶ | sub _add_val { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_add_val: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | 57 | my($dst, $year, $mon, $day, $hour, $val) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 171 | # only here do we alter into computer time values | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 57 | my $epoch = timegm(0,0,$hour, $day, $mon, $year-1900); | |||||||||||||||||||||||||||||||||||||||||
| 173 | 57 | $dst->{values}->{$epoch} = $val; | |||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | # placeholder for proper exceptions | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | sub _except { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_except: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | die "DST: Exception: $_[0]"; | |||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 183 | __END__ | ||||||||||||||||||||||||||||||||||||||||||