Skip to content

Commit

Permalink
Update population data
Browse files Browse the repository at this point in the history
  • Loading branch information
slowe committed Jul 24, 2024
1 parent 5ac85de commit 0cf0ea3
Show file tree
Hide file tree
Showing 3 changed files with 708 additions and 24 deletions.
51 changes: 43 additions & 8 deletions pipelines/society/population.pl
Original file line number Diff line number Diff line change
@@ -1,23 +1,58 @@
#!/usr/bin/perl
# Process population figures

use lib "./../lib/"; # Custom functions
use utf8;
use warnings;
use strict;
use Data::Dumper;
use Cwd qw(abs_path);
my ($basedir, $path);
BEGIN {
# Get the real base directory for this script
($basedir, $path) = abs_path($0) =~ m{(.*/)?([^/]+)$};
}
use lib $basedir."../lib/"; # Custom functions
require "lib.pl";
use OpenInnovations::XLSX;


my $url = "https://data.parliament.uk/resources/constituencystatistics/PowerBIData/ConstituencyDashboards/population_by_age.xlsx";
my $file = $basedir."../../raw-data/population_by_age.xlsx";
my $outfile = $basedir."../../src/themes/society/_data/population_2022.csv";

my $popfile = "../../raw-data/Population_all.csv";
my $outfile = "../../src/_data/sources/society/population-2020.csv";

# Get the remote data
SaveFromURL($url,$file);

my @rows = LoadCSV($popfile);
# Load in the Excel file
my $xlsx = OpenInnovations::XLSX->new()->load($file);

# Load the specific sheet
my $number = $xlsx->loadSheet('Single year of age',{
'header'=>[0],
'startrow'=>1,
'rename'=>sub {
my $str = shift;
if($str eq "con_code"){
return "PCON24CD";
}elsif($str eq "con_name"){
return "PCON24NM";
}
return $str;
}
});

# Get the rows
my @rows = @{$number->{'rows'}};

my ($r,$n,$pcon,$age,$categories,$cat,$i);
my $ages = {};
my $totals = {};

for($r = 0; $r < @rows; $r++){
$pcon = $rows[$r]->{'PCON11CD'};
$age = $rows[$r]->{'Age_year'};
$n = $rows[$r]->{'Age_pop'};
$pcon = $rows[$r]->{'PCON24CD'};
$age = $rows[$r]->{'age'};
$n = $rows[$r]->{'con_number'};
if(!$ages->{$pcon}){ $ages->{$pcon} = {}; }
if(!$ages->{$pcon}{$age}){ $ages->{$pcon}{$age} = 0; }
if(!$totals->{$pcon}){ $totals->{$pcon} = {}; }
Expand Down Expand Up @@ -53,7 +88,7 @@

msg("Saving to <cyan>$outfile<none>\n");
open(FILE,">",$outfile);
print FILE "PCON11CD";
print FILE "PCON24CD";
for($i = 0; $i <= 90; $i++){
print FILE ",".$i;
if($i == 90){ print FILE "+"; }
Expand Down
Loading

0 comments on commit 0cf0ea3

Please sign in to comment.