Prepare for git repository
[spider.git] / perl / gen_usdb_data.pl
1 #!/usr/bin/perl
2 #
3 # Something to create my subset of the US call book data,
4 # in my flat file form, either from the main data base or
5 # else the daily updates. 
6 #
7 # You can get the main database from: 
8 #
9 #   http://wireless.fcc.gov/uls/data/complete/l_amat.zip
10 #
11 # The daily data bases are available as a set of seven from here:-
12 #
13 #   http://wireless.fcc.gov/uls/data/daily/l_am_sat.zip 
14 #   http://wireless.fcc.gov/uls/data/daily/l_am_sun.zip 
15 #   http://wireless.fcc.gov/uls/data/daily/l_am_mon.zip 
16 #   http://wireless.fcc.gov/uls/data/daily/l_am_tue.zip 
17 #   http://wireless.fcc.gov/uls/data/daily/l_am_wed.zip 
18 #   http://wireless.fcc.gov/uls/data/daily/l_am_thu.zip 
19 #   http://wireless.fcc.gov/uls/data/daily/l_am_fri.zip
20
21 # this program expects one or more zip files containing the call book
22 # data as arguments.
23 #
24 # Copyright (c) 2002 Dirk Koopman G1TLH
25 #
26 # $Id$
27 #
28
29 use strict;
30
31 # make sure that modules are searched in the order local then perl
32 BEGIN {
33         # root of directory tree for this system
34         use vars qw($root);
35         
36         $root = "/spider"; 
37         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
38
39         unshift @INC, "$root/perl";     # this IS the right way round!
40         unshift @INC, "$root/local";
41 }
42
43 use DXVars;
44 use Archive::Zip qw(:ERROR_CODES);
45 use Archive::Zip::MemberRead;
46 use IO::File;
47 use Compress::Zlib;
48
49 my $blksize = 1024 * 1024;
50
51 STDOUT->autoflush(1);
52
53 my $dbrawfn = "$main::data/usdbraw.gz";
54
55 rename "$dbrawfn.oo", "$dbrawfn.ooo";
56 rename "$dbrawfn.o", "$dbrawfn.oo";
57 rename "$dbrawfn", "$dbrawfn.o";
58 my $gzfh = gzopen($dbrawfn, "wb") or die "Cannot open $dbrawfn $!";
59
60 my $ctycount;
61   
62 foreach my $argv (@ARGV) {
63         my $zip = new Archive::Zip($argv) or die "Cannot open $argv $!\n";
64         print "Doing $argv\n";
65         handleEN($zip, $argv);
66         handleAM($zip, $argv);
67         handleHS($zip, $argv);
68 }
69
70 $gzfh->gzclose;
71
72 exit(0);
73
74 sub handleEN
75 {
76         my ($zip, $argv) = @_;
77         my $mname = "EN.dat";
78         my $ofn = "$main::data/$mname";
79         print "  Handling EN records, unzipping";
80         if ($zip->extractMember($mname, $ofn) == AZ_OK) {
81                 my $fh = new IO::File "$ofn" or die "Cannot open $ofn $!";
82                 if ($fh) {
83                         
84                         print ", storing";
85                         
86                         my $count;
87                         my $buf;
88                         
89                         while (my $l = $fh->getline) {
90                                 $l =~ s/[\r\n]+$//;
91                                 my ($rt,$usi,$ulsfn,$ebfno,$call,$type,$lid,$name,$first,$middle,$last,$suffix,
92                                         $phone,$fax,$email,$street,$city,$state,$zip,$pobox,$attl,$sgin,$frn) = split /\|/, $l;
93
94 #                               print "ERR: $l\n" unless $call && $city && $state;
95
96                                 if ($call && $city && $state) {
97                                         my $rec = uc join '|', $call,$city,$state if $city && $state;
98                                         $buf .= "$rec\n";
99                                         if (length $buf > $blksize) {
100                                                 $gzfh->gzwrite($buf);
101                                                 undef $buf;
102                                         }
103                                         $count++;
104                                 }
105                         }
106                         $gzfh->gzwrite($buf) if length $buf;
107                         print ", $count records\n";
108                         $fh->close;
109                 }
110                 unlink $ofn;
111         } else {
112                 print "EN missing in $argv\n";
113                 return;
114         }
115 }
116
117 sub handleAM
118 {
119
120 }
121
122 sub handleHS
123 {
124
125 }