#!/usr/bin/perl use strict; use Getopt::Long; use Census; use TSV; use Data::Dumper; # Need ^C to exit cleanly so we flush cache data to disk $SIG{INT} = sub { exit; }; main(); sub usage { print qq{ Census Data Extraction Tool Usage: cdet --year 2000 datafile.dat Then it will process datafile.dat one line at a time, and show its progress. Output will be put into datafile_out.dat }; exit; } sub main { my $data_year; my $lookup_address; GetOptions("year=i", \$data_year) or die "Error in command line arguments!"; if($data_year != 2000) { print "Error: I don't know how to process year '$data_year'\n"; usage(); } # Get data filename from the commandline my $data_filename = shift @ARGV; my $output_filename = $data_filename; $output_filename =~ s/\.dat//; $output_filename = $data_filename . ".out.dat"; # Open WIDE!!! open my $in, "<", $data_filename or die "Error: $!\n"; open my $out, ">", $output_filename or die "Error: $!\n"; my (@header) = TSV::getRow($in); #print "Header: " . Dumper(\@header) . "\n"; unless(grep { /tract/ } @header) { $lookup_address = 1; } my $rownum = 0; while(my %row = TSV::getRowHash($in, @header)) { $rownum++; my %block_info; if($lookup_address) { my $address = Census::getAddress(%row); %block_info = Census::address_to_block($address); printf "% 30s ", $address; } else { $block_info{tract} = $row{tract}; $block_info{block_group} = $row{block_group}; $block_info{block} = $row{block}; } if(%block_info) { printf "% 7s % 2s % 4s ", $block_info{tract}, $block_info{block_group}, $block_info{block}; my $logrecno = Census::get_logrecno($data_year, $block_info{tract}, $block_info{block_group}, $block_info{block}); printf "% 7s ", $logrecno; $logrecno = Census::get_logrecno($data_year, $block_info{tract}, $block_info{block_group}); printf "% 7s ", $logrecno; } else { print "Block info not found :("; } print "\n"; } }