| 1 | package Weather::MOSMIX; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use Moo 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use DBI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use DBD::SQLite 1.56; # some virtual table bugfixes | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Weather::MOSMIX::Weathercodes 'mosmix_weathercode'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Storable 'dclone'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Time::Piece; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Encode 'encode', 'decode'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 NAME | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | Weather::MOSMIX - Reader for MOSMIX weather forecast files | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =cut | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 3 | with 'MooX::Role::DBIConnection'; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 3 | our $TIMESTAMP = '%Y-%m-%dT%H:%M:%S'; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | has 'json' => ( | ||||||||||||||||||||||||||||
| 28 | is => 'lazy', | ||||||||||||||||||||||||||||
| 29 | 0 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 30 | 0 | JSON->new() | |||||||||||||||||||||||||||
| 31 | }, | ||||||||||||||||||||||||||||
| 32 | 3 | ); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Convert an array into an SQLite virtual table | ||||||||||||||||||||||||||||
| 35 | # This should move into its own module/role, maybe | ||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub as_dbh( $table_name, $rows, $colnames=[keys %{ $rows->[0]}]) { | ||||||||||||||||||||||||||
|
Subroutine
as_dbh: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | 0 | my $dbh = DBI->connect('dbi:SQLite:dbname=:memory:',undef,undef,{AutoCommit => 1, RaiseError => 1,PrintError => 0}); | |||||||||||||||||||||||||||
| 38 | 0 | $dbh->sqlite_create_module(perl => "DBD::SQLite::VirtualTable::PerlData"); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | $colnames = join ",", @$colnames; | |||||||||||||||||||||||||||
| 41 | 0 | our $table_000; | |||||||||||||||||||||||||||
| 42 | 0 | local $table_000 = $rows; | |||||||||||||||||||||||||||
| 43 | 0 | my $tablevar = __PACKAGE__ . '::table_000'; | |||||||||||||||||||||||||||
| 44 | 0 | my $sql = qq(CREATE VIRTUAL TABLE "$table_name" USING perl($colnames, hashrefs="$tablevar");); | |||||||||||||||||||||||||||
| 45 | 0 | $dbh->do($sql); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | return $dbh | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub forecast( $self, %options ) { | ||||||||||||||||||||||||||
|
Subroutine
forecast: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 51 | 0 | my $cos_lat_sq = cos( $options{ latitude } ) ^ 2; | |||||||||||||||||||||||||||
| 52 | my $res = | ||||||||||||||||||||||||||||
| 53 | 0 | $self->dbh->selectall_arrayref(<<'SQL', { Slice => {}}, $options{latitude}, $options{latitude}, $options{longitude},$options{longitude}, $cos_lat_sq); | |||||||||||||||||||||||||||
| 54 | select *, | ||||||||||||||||||||||||||||
| 55 | ((l.latitude - ?)*(l.latitude - ?)) | ||||||||||||||||||||||||||||
| 56 | + ((l.longitude - ?)*(l.longitude - ?)*?) as distance | ||||||||||||||||||||||||||||
| 57 | from forecast_location l | ||||||||||||||||||||||||||||
| 58 | join forecast f on l.name = f.name | ||||||||||||||||||||||||||||
| 59 | order by distance asc, expiry desc | ||||||||||||||||||||||||||||
| 60 | limit 1 | ||||||||||||||||||||||||||||
| 61 | SQL | ||||||||||||||||||||||||||||
| 62 | 0 | for (@$res) { | |||||||||||||||||||||||||||
| 63 | $_->{forecasts} = $self->json->decode($_->{forecasts}) | ||||||||||||||||||||||||||||
| 64 | 0 | }; | |||||||||||||||||||||||||||
| 65 | 0 | $res->[0] | |||||||||||||||||||||||||||
| 66 | }; | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub forecast_dbh( $self, %options ) { | ||||||||||||||||||||||||||
|
Subroutine
forecast_dbh: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 69 | 0 | my $res = $self->forecast( %options ); | |||||||||||||||||||||||||||
| 70 | # Convert from UTC to CET. This happens to work because my machines | ||||||||||||||||||||||||||||
| 71 | # are located within CET ... | ||||||||||||||||||||||||||||
| 72 | 0 | my $time = Time::Piece->strptime( $res->{issuetime}, $TIMESTAMP.'Z' ); | |||||||||||||||||||||||||||
| 73 | 0 | $time = $time->_mktime( $time->epoch, 1 ); | |||||||||||||||||||||||||||
| 74 | 0 | $time->tzoffset(2*3600); # at least until October ... | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | #for my $i ($offset..$offset+$count-1) { | ||||||||||||||||||||||||||||
| 77 | # my $c = $weathercode->{values}->[ $i ]; | ||||||||||||||||||||||||||||
| 78 | # if( length $c ) { | ||||||||||||||||||||||||||||
| 79 | # my $v = sprintf '%02d', 0+$c; | ||||||||||||||||||||||||||||
| 80 | # push @{ $weather }, { | ||||||||||||||||||||||||||||
| 81 | # timestamp => $time->new(), | ||||||||||||||||||||||||||||
| 82 | # description => mosmix_weathercode($v), | ||||||||||||||||||||||||||||
| 83 | # }; | ||||||||||||||||||||||||||||
| 84 | # $time += 3600; | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # zip ww and TTT to AoH | ||||||||||||||||||||||||||||
| 87 | 0 | my $hour_ofs; | |||||||||||||||||||||||||||
| 88 | my @rows = map { | ||||||||||||||||||||||||||||
| 89 | 0 | my $ts = $time->new; | |||||||||||||||||||||||||||
| 90 | 0 | $time += 3600; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | my $res = +{ | ||||||||||||||||||||||||||||
| 93 | $res->{forecasts}->[0]->{type} => $res->{forecasts}->[0]->{values}->[$_], | ||||||||||||||||||||||||||||
| 94 | $res->{forecasts}->[1]->{type} => $res->{forecasts}->[1]->{values}->[$_], | ||||||||||||||||||||||||||||
| 95 | timestamp => $ts->strftime($TIMESTAMP), | ||||||||||||||||||||||||||||
| 96 | date => $ts->strftime('%Y-%m-%d'), | ||||||||||||||||||||||||||||
| 97 | hour => $ts->strftime('%H'), | ||||||||||||||||||||||||||||
| 98 | hour_ofs => $hour_ofs++, | ||||||||||||||||||||||||||||
| 99 | weekday => $ts->strftime('%a'), | ||||||||||||||||||||||||||||
| 100 | description => $res->{description}, | ||||||||||||||||||||||||||||
| 101 | issuetime => $res->{issuetime} | ||||||||||||||||||||||||||||
| 102 | 0 | }; | |||||||||||||||||||||||||||
| 103 | 0 | my $descr = mosmix_weathercode($res->{ww}, 'emoji'); | |||||||||||||||||||||||||||
| 104 | 0 | $res->{emoji} = encode('UTF-8', $descr); | |||||||||||||||||||||||||||
| 105 | 0 | ▶ | length $res->{TTT} ? $res : () | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | } 0..$#{$res->{forecasts}->[0]->{values}}; | |||||||||||||||||||||||||||
| 107 | 0 | return as_dbh( 'forecast', \@rows ) | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | sub format_forecast_range_concise { | ||||||||||||||||||||||||||
|
Subroutine
format_forecast_range_concise: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 111 | 0 | my( $ts, $temp, $weathercode, $offset, $count ) = @_; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | my ($min, $max) = (1000,0); | |||||||||||||||||||||||||||
| 114 | 0 | for my $f (grep { length $_ } @{ $temp->{values} }[$offset..$offset+$count-1]) { | |||||||||||||||||||||||||||
| 115 | 0 | ▶ | if( $f < $min ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | $min = $f; | |||||||||||||||||||||||||||
| 117 | }; | ||||||||||||||||||||||||||||
| 118 | 0 | ▶ | if( $f > $max ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | $max = $f; | |||||||||||||||||||||||||||
| 120 | }; | ||||||||||||||||||||||||||||
| 121 | }; | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | $max -= 273.15; | |||||||||||||||||||||||||||
| 124 | 0 | $min -= 273.15; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | my $weather = []; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | my %forecast = ( | |||||||||||||||||||||||||||
| 129 | date => $ts->new(), | ||||||||||||||||||||||||||||
| 130 | weather => $weather, | ||||||||||||||||||||||||||||
| 131 | min => $min, | ||||||||||||||||||||||||||||
| 132 | max => $max, | ||||||||||||||||||||||||||||
| 133 | ); | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | my $time = $ts->new(); | |||||||||||||||||||||||||||
| 136 | 0 | $time->tzoffset(2*3600); # at least until October ... | |||||||||||||||||||||||||||
| 137 | # Do the min/max for the 4 6-hour windows | ||||||||||||||||||||||||||||
| 138 | # Find the "representative" weather code for each window | ||||||||||||||||||||||||||||
| 139 | # This should be done in SQL instead of hacking Perl code for this | ||||||||||||||||||||||||||||
| 140 | 0 | my %count; | |||||||||||||||||||||||||||
| 141 | # with range as ( | ||||||||||||||||||||||||||||
| 142 | # select * from perldata | ||||||||||||||||||||||||||||
| 143 | # where rownum() between ? and ? | ||||||||||||||||||||||||||||
| 144 | # ) | ||||||||||||||||||||||||||||
| 145 | # select min(temp) as mintemp over () | ||||||||||||||||||||||||||||
| 146 | # , max(temp) as maxtemp over () | ||||||||||||||||||||||||||||
| 147 | # from range | ||||||||||||||||||||||||||||
| 148 | 0 | for my $i ($offset..$offset+$count-1) { | |||||||||||||||||||||||||||
| 149 | 0 | my $c = $weathercode->{values}->[ $i ]; | |||||||||||||||||||||||||||
| 150 | 0 | ▶ | if( length $c ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | my $v = sprintf '%02d', 0+$c; | |||||||||||||||||||||||||||
| 152 | 0 | $count{ $v }++; | |||||||||||||||||||||||||||
| 153 | 0 | $time += 3600; | |||||||||||||||||||||||||||
| 154 | }; | ||||||||||||||||||||||||||||
| 155 | }; | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # Use the prevalent weather ... | ||||||||||||||||||||||||||||
| 158 | 0 | my ($prevalent_weather) = (sort { $count{$a} cmp $count{$a} } keys %count )[0]; | |||||||||||||||||||||||||||
| 159 | 0 | push @{ $weather }, { | |||||||||||||||||||||||||||
| 160 | timestamp => $ts->new(), | ||||||||||||||||||||||||||||
| 161 | description => mosmix_weathercode($prevalent_weather), | ||||||||||||||||||||||||||||
| 162 | }; | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | return \%forecast | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | ▶ | sub format_forecast_day_concise( $ts, $temp, $weathercode, $offset, $count ) { | ||||||||||||||||||||||||||
|
Subroutine
format_forecast_day_concise: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | # with range as ( | ||||||||||||||||||||||||||||
| 170 | # select * from perldata | ||||||||||||||||||||||||||||
| 171 | # ) | ||||||||||||||||||||||||||||
| 172 | # , minmax as ( | ||||||||||||||||||||||||||||
| 173 | # select min(temp) as mintemp over (partition by offset / 6) | ||||||||||||||||||||||||||||
| 174 | # , max(temp) as maxtemp over (partition by offset / 6) | ||||||||||||||||||||||||||||
| 175 | # , weathercode | ||||||||||||||||||||||||||||
| 176 | # select | ||||||||||||||||||||||||||||
| 177 | # lead(min,0) lead(max,0), lead(weathercode,0) | ||||||||||||||||||||||||||||
| 178 | # lead(min,1) lead(max,1), lead(weathercode,1) | ||||||||||||||||||||||||||||
| 179 | # lead(min,2) lead(max,2), lead(weathercode,2) | ||||||||||||||||||||||||||||
| 180 | # lead(min,3) lead(max,3), lead(weathercode,3) | ||||||||||||||||||||||||||||
| 181 | # from range | ||||||||||||||||||||||||||||
| 182 | 0 | my @res; | |||||||||||||||||||||||||||
| 183 | 0 | for (1..4) { | |||||||||||||||||||||||||||
| 184 | 0 | push @res, format_forecast_range_concise( $ts, $temp, $weathercode, $offset, 6 ); | |||||||||||||||||||||||||||
| 185 | 0 | $offset += 6; | |||||||||||||||||||||||||||
| 186 | }; | ||||||||||||||||||||||||||||
| 187 | # put all of the information into a single line: | ||||||||||||||||||||||||||||
| 188 | # location / ts / 3-9 / 10-15 / 16-21 / 22-2 | ||||||||||||||||||||||||||||
| 189 | # location / ts / min/max/w / min/max/w / min/max/w / min/max/w | ||||||||||||||||||||||||||||
| 190 | }; | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | ▶ | sub format_forecast_day { | ||||||||||||||||||||||||||
|
Subroutine
format_forecast_day: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 193 | 0 | my( $self, $ts, $temp, $weathercode, $offset, $count ) = @_; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 0 | my ($min, $max) = (1000,0); | |||||||||||||||||||||||||||
| 196 | 0 | for my $f (grep { length $_ } @{ $temp->{values} }[$offset..$offset+$count-1]) { | |||||||||||||||||||||||||||
| 197 | 0 | ▶ | if( $f < $min ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 0 | $min = $f; | |||||||||||||||||||||||||||
| 199 | }; | ||||||||||||||||||||||||||||
| 200 | 0 | ▶ | if( $f > $max ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 0 | $max = $f; | |||||||||||||||||||||||||||
| 202 | }; | ||||||||||||||||||||||||||||
| 203 | }; | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 0 | $max -= 273.15; | |||||||||||||||||||||||||||
| 206 | 0 | $min -= 273.15; | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 0 | my $weather = []; | |||||||||||||||||||||||||||
| 209 | 0 | my %forecast = ( | |||||||||||||||||||||||||||
| 210 | date => $ts->new(), | ||||||||||||||||||||||||||||
| 211 | weather => $weather, | ||||||||||||||||||||||||||||
| 212 | min => $min, | ||||||||||||||||||||||||||||
| 213 | max => $max, | ||||||||||||||||||||||||||||
| 214 | ); | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 0 | my $time = $ts->new(); | |||||||||||||||||||||||||||
| 217 | 0 | $time->tzoffset(2*3600); # at least until October ... | |||||||||||||||||||||||||||
| 218 | 0 | for my $i ($offset..$offset+$count-1) { | |||||||||||||||||||||||||||
| 219 | 0 | my $c = $weathercode->{values}->[ $i ]; | |||||||||||||||||||||||||||
| 220 | 0 | ▶ | if( length $c ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | 0 | my $v = sprintf '%02d', 0+$c; | |||||||||||||||||||||||||||
| 222 | 0 | push @{ $weather }, { | |||||||||||||||||||||||||||
| 223 | timestamp => $time->new(), | ||||||||||||||||||||||||||||
| 224 | description => mosmix_weathercode($v), | ||||||||||||||||||||||||||||
| 225 | }; | ||||||||||||||||||||||||||||
| 226 | 0 | $time += 3600; | |||||||||||||||||||||||||||
| 227 | }; | ||||||||||||||||||||||||||||
| 228 | }; | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 0 | return \%forecast, | |||||||||||||||||||||||||||
| 231 | }; | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 0 | ▶ | sub format_forecast_dbh( $self, $dbh, $interval, $offset=0 ) { | ||||||||||||||||||||||||||
|
Subroutine
format_forecast_dbh: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 234 | # We need some offset for the first set, which will not contain the full | ||||||||||||||||||||||||||||
| 235 | # six (or whatever) hours | ||||||||||||||||||||||||||||
| 236 | 0 | my $sql = <<SQL; | |||||||||||||||||||||||||||
| 237 | with | ||||||||||||||||||||||||||||
| 238 | partitioned as ( | ||||||||||||||||||||||||||||
| 239 | select | ||||||||||||||||||||||||||||
| 240 | round((hour_ofs*1.0)/$interval -0.5) as part | ||||||||||||||||||||||||||||
| 241 | , (hour_ofs*1.0)/$interval as weather_partition | ||||||||||||||||||||||||||||
| 242 | , $interval as size | ||||||||||||||||||||||||||||
| 243 | , * | ||||||||||||||||||||||||||||
| 244 | from forecast | ||||||||||||||||||||||||||||
| 245 | ) | ||||||||||||||||||||||||||||
| 246 | , minmax as ( | ||||||||||||||||||||||||||||
| 247 | select | ||||||||||||||||||||||||||||
| 248 | max(TTT) over (partition by part) as maxtemp | ||||||||||||||||||||||||||||
| 249 | , min(TTT) over (partition by part) as mintemp | ||||||||||||||||||||||||||||
| 250 | , row_number() over (partition by part order by timestamp) as row | ||||||||||||||||||||||||||||
| 251 | --, min(weather_partition) over (partition by part) as use_this | ||||||||||||||||||||||||||||
| 252 | -- , date | ||||||||||||||||||||||||||||
| 253 | -- , timestamp -- TZ-adjusted | ||||||||||||||||||||||||||||
| 254 | , * | ||||||||||||||||||||||||||||
| 255 | from partitioned | ||||||||||||||||||||||||||||
| 256 | ) | ||||||||||||||||||||||||||||
| 257 | select | ||||||||||||||||||||||||||||
| 258 | 'active' as status | ||||||||||||||||||||||||||||
| 259 | , * | ||||||||||||||||||||||||||||
| 260 | from minmax | ||||||||||||||||||||||||||||
| 261 | where row = 1 | ||||||||||||||||||||||||||||
| 262 | order by timestamp | ||||||||||||||||||||||||||||
| 263 | SQL | ||||||||||||||||||||||||||||
| 264 | 0 | my $res = $dbh->selectall_arrayref($sql, { Slice => {} }); | |||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | # Now, add dummy data for slots we don't have | ||||||||||||||||||||||||||||
| 267 | # this would likely be parts of the day that have already passed | ||||||||||||||||||||||||||||
| 268 | 0 | ▶ | while( $res->[0]->{hour} > $offset+$interval) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | 0 | my $new = dclone( $res->[0]); | |||||||||||||||||||||||||||
| 270 | 0 | $new->{hour}-= $interval; | |||||||||||||||||||||||||||
| 271 | 0 | $new->{status} = 'past'; | |||||||||||||||||||||||||||
| 272 | 0 | unshift @$res, $new; | |||||||||||||||||||||||||||
| 273 | }; | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 0 | for( @$res ) { | |||||||||||||||||||||||||||
| 276 | 0 | ▶ | if( exists $_->{emoji}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | 0 | $_->{emoji} = decode('UTF-8', $_->{emoji}); | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | # fix up the data we have | ||||||||||||||||||||||||||||
| 282 | # $time->tzoffset(2*3600); # at least until October ... | ||||||||||||||||||||||||||||
| 283 | #for my $i ($offset..$offset+$count-1) { | ||||||||||||||||||||||||||||
| 284 | # my $c = $weathercode->{values}->[ $i ]; | ||||||||||||||||||||||||||||
| 285 | # if( length $c ) { | ||||||||||||||||||||||||||||
| 286 | # my $v = sprintf '%02d', 0+$c; | ||||||||||||||||||||||||||||
| 287 | # push @{ $weather }, { | ||||||||||||||||||||||||||||
| 288 | # timestamp => $time->new(), | ||||||||||||||||||||||||||||
| 289 | # description => mosmix_weathercode($v), | ||||||||||||||||||||||||||||
| 290 | # }; | ||||||||||||||||||||||||||||
| 291 | # $time += 3600; | ||||||||||||||||||||||||||||
| 292 | # }; | ||||||||||||||||||||||||||||
| 293 | #}; | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 0 | return $res; | |||||||||||||||||||||||||||
| 296 | }; | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 0 | ▶ | sub format_forecast( $self, $f ) { | ||||||||||||||||||||||||||
|
Subroutine
format_forecast: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 299 | 0 | my $loc = $f->{description}; | |||||||||||||||||||||||||||
| 300 | 0 | (my $temp) = grep{ $_->{type} eq 'TTT' } @{ $f->{forecasts}}; | |||||||||||||||||||||||||||
| 301 | 0 | (my $weathercode) = grep{ $_->{type} eq 'ww' } @{ $f->{forecasts}}; | |||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | # Convert from UTC to CET. This happens to work because my machines | ||||||||||||||||||||||||||||
| 304 | # are located within CET ... | ||||||||||||||||||||||||||||
| 305 | 0 | my $time = Time::Piece->strptime( $f->{issuetime}, '%Y-%m-%dT%H:%M:%SZ' ); | |||||||||||||||||||||||||||
| 306 | 0 | $time = $time->_mktime( $time->epoch, 1 ); | |||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | # Find where today ends, and add a linebreak, resp. move to the next array ... | ||||||||||||||||||||||||||||
| 309 | 0 | my @forecasts; | |||||||||||||||||||||||||||
| 310 | 0 | my %weather = ( | |||||||||||||||||||||||||||
| 311 | #today => $weath, | ||||||||||||||||||||||||||||
| 312 | #tomorrow => [], | ||||||||||||||||||||||||||||
| 313 | #tomnext => [], | ||||||||||||||||||||||||||||
| 314 | days => \@forecasts, | ||||||||||||||||||||||||||||
| 315 | ); | ||||||||||||||||||||||||||||
| 316 | 0 | my %sequence = ( | |||||||||||||||||||||||||||
| 317 | today => 'tomorrow', | ||||||||||||||||||||||||||||
| 318 | tomorrow => 'tomnext', | ||||||||||||||||||||||||||||
| 319 | ); | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 0 | my $count = 0; | |||||||||||||||||||||||||||
| 322 | 0 | my $today = $time->truncate(to => 'day'); | |||||||||||||||||||||||||||
| 323 | 0 | my $start = $time->new(); | |||||||||||||||||||||||||||
| 324 | 0 | my $offset = $start->hour; | |||||||||||||||||||||||||||
| 325 | 0 | my $slot = 'today'; | |||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 0 | ▶ | while( $offset < @{$weathercode->{values}} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 328 | 0 | $time += 3600; | |||||||||||||||||||||||||||
| 329 | 0 | $count++; | |||||||||||||||||||||||||||
| 330 | 0 | ▶ | if( $time->truncate( to => 'day' ) != $today ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 331 | 0 | push @forecasts, $self->format_forecast_day( $start, $temp, $weathercode, $offset, $count ); | |||||||||||||||||||||||||||
| 332 | 0 | $offset += $count; | |||||||||||||||||||||||||||
| 333 | 0 | $count = 0; | |||||||||||||||||||||||||||
| 334 | 0 | ▶ | if( defined $slot ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 335 | #print "$slot ($today) -> $sequence{ $slot } ($time)\n"; | ||||||||||||||||||||||||||||
| 336 | 0 | $slot = $sequence{ $slot }; | |||||||||||||||||||||||||||
| 337 | }; | ||||||||||||||||||||||||||||
| 338 | 0 | $today = $time->truncate( to => 'day' ); | |||||||||||||||||||||||||||
| 339 | 0 | $start = $today; | |||||||||||||||||||||||||||
| 340 | }; | ||||||||||||||||||||||||||||
| 341 | }; | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | 0 | $weather{ today } = $forecasts[0]; | |||||||||||||||||||||||||||
| 344 | 0 | $weather{ tomorrow } = $forecasts[1]; | |||||||||||||||||||||||||||
| 345 | 0 | $weather{ tomnext } = $forecasts[2]; | |||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | return { | ||||||||||||||||||||||||||||
| 348 | issuetime => $f->{issuetime}, | ||||||||||||||||||||||||||||
| 349 | 0 | location => $loc, | |||||||||||||||||||||||||||
| 350 | weather => \%weather, | ||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 0 | ▶ | sub formatted_forecast( $self, %options ) { | ||||||||||||||||||||||||||
|
Subroutine
formatted_forecast: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 355 | 0 | my $f = $self->forecast( %options ); | |||||||||||||||||||||||||||
| 356 | 0 | $self->format_forecast( $f ) | |||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | =head2 C<< $mosmix->locations >> | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | Lists all locations with their names and longitude/latitude. If a longitude | ||||||||||||||||||||||||||||
| 362 | / latitude pair is passed in, the list is ordered by the distance from | ||||||||||||||||||||||||||||
| 363 | that position. | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | =cut | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 0 | ▶ | sub locations( $self, %options ) { | ||||||||||||||||||||||||||
|
Subroutine
locations: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 368 | 0 | my $order_by = 'description asc'; | |||||||||||||||||||||||||||
| 369 | 0 | ▶ | if( exists $options{ longitude } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $options{"longitude"} and exists $options{"latitude"}
MC/DC0%0/2
exists $options{"longitude"} and exists $options{"latitude"}
exists $options{"longitude"}
exists $options{"latitude"}
Truth table0%0/3
A and B
A= exists $options{"longitude"}B= exists $options{"latitude"}
| |||||||||||||||||||||||||||||
| 370 | and exists $options{ latitude } ) { | ||||||||||||||||||||||||||||
| 371 | 0 | $order_by = 'distance asc'; | |||||||||||||||||||||||||||
| 372 | } else { | ||||||||||||||||||||||||||||
| 373 | 0 | $options{ latitude } = 0; | |||||||||||||||||||||||||||
| 374 | }; | ||||||||||||||||||||||||||||
| 375 | 0 | my $cos_lat_sq = cos( $options{ latitude } ) ^ 2; | |||||||||||||||||||||||||||
| 376 | my $res = | ||||||||||||||||||||||||||||
| 377 | 0 | $self->dbh->selectall_arrayref(<<SQL, { Slice => {}}, $options{latitude}, $options{latitude}, $options{longitude},$options{longitude}, $cos_lat_sq); | |||||||||||||||||||||||||||
| 378 | select | ||||||||||||||||||||||||||||
| 379 | description | ||||||||||||||||||||||||||||
| 380 | , latitude | ||||||||||||||||||||||||||||
| 381 | , longitude | ||||||||||||||||||||||||||||
| 382 | , ((l.latitude - ?)*(l.latitude - ?)) | ||||||||||||||||||||||||||||
| 383 | + ((l.longitude - ?)*(l.longitude - ?)*?) as distance | ||||||||||||||||||||||||||||
| 384 | from forecast_location l | ||||||||||||||||||||||||||||
| 385 | order by $order_by | ||||||||||||||||||||||||||||
| 386 | SQL | ||||||||||||||||||||||||||||
| 387 | 0 | $res | |||||||||||||||||||||||||||
| 388 | }; | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | =head1 SETUP | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | =over 4 | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | =item 1 | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | Install the module | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | =item 2 | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | Create a directory for the database | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | mkdir ~/weather ; cd ~/weather | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | =item 3 | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | Create the database | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | mosmix-import.pl --create | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | =item 4 | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | Set up a cron job to fetch the MOSMIX forecast | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | 01 6,12,18,0 * * * cd /home/corion/weather; mosmix-import.pl | ||||||||||||||||||||||||||||
| 415 | 15 6,12,18,0 * * * cd /home/corion/weather; mosmix-purge.pl | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | =item 5 | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | Query the current forecast | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | cd /home/corion/weather; mosmix-query.pl | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | =back | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | German Weather Service | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | L<https://opendata.dwd.de/weather/> | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | L<https://opendata.dwd.de/weather/local_forecasts/mos/MOSMIX_S/all_stations/kml/> | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | Other Weather APIs | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | L<Weather::YR> - API of the L<https://yr.no> weather service | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | L<https://openweathermap.org/api> - international, signup required | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | L<https://www.weatherbit.io/api> - international, signup required | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | L<https://developer.accuweather.com/> - international, signup required | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | L<https://darksky.net/dev> - paid, international, signup required | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | L<http://api.weather2020.com/> - international, signup required | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | Overview of Open Data | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | L<https://index.okfn.org/place/de/weather/> | ||||||||||||||||||||||||||||
| 450 | L<https://index.okfn.org/place/us/weather/> | ||||||||||||||||||||||||||||
| 451 | L<https://index.okfn.org/place/lv/weather/> | ||||||||||||||||||||||||||||
| 452 | L<https://index.okfn.org/place/cy/weather/> | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | Cyprus forecast | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | L<http://www.moa.gov.cy/moa/ms/ms.nsf/DMLforecast_general_gr/DMLforecast_general_gr?opendocument> | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | =head2 Icons | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | L<https://github.com/zagortenay333/Tempestacons> | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | L<https://thenounproject.com/search/?q=weather> | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | L<https://undraw.co/search> | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | L<https://coreui.io/icons/> | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | =cut | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | 3 | 1; | |||||||||||||||||||||||||||
| 471 | =head1 REPOSITORY | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | The public repository of this module is | ||||||||||||||||||||||||||||
| 474 | L<https://github.com/Corion/weather-mosmix>. | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | The public support forum of this module is L<https://perlmonks.org/>. | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | =head1 BUG TRACKER | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | Please report bugs in this module via the RT CPAN bug queue at | ||||||||||||||||||||||||||||
| 483 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=Weather-MOSMIX> | ||||||||||||||||||||||||||||
| 484 | or via mail to L<www-Weather-MOSMIX@rt.cpan.org|mailto:Weather-MOSMIX@rt.cpan.org>. | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | Max Maischein C<corion@cpan.org> | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | =head1 COPYRIGHT (c) | ||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | Copyright 2019-2020 by Max Maischein C<corion@cpan.org>. | ||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | This module is released under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | =cut | ||||||||||||||||||||||||||||