1#!  /usr/local/bin/perl
2#  This program reads the witty worm interval files and produces a
3#  cuttlefish configuration file.
4use Getopt::Std;
5use FindBin qw($Bin);
6use lib "$Bin/../lib";
7#use Util;
8
9use strict;
10my ($exicutable) = ($0 =~ /([^\/]+)$/);
11my $usage =
12"usage:$0 [-h] witty-interval-file\n";
13sub Help() {
14    print STDERR $usage;
15print<<EOP;
16    This reads in the witty worm interval files to produce
17    a cuttlefish input file.
18    h - this message
19EOP
20}
21
22my %opts;
23if (!getopts("h",\%opts) && !defined $opts{h}) {
24    print STDERR $usage;
25    exit -1;
26}
27if (defined $opts{h}) {
28    Help();
29    exit(0);
30}
31
32my %loc2id;
33my %time2id2value;
34my %time2total;
35LoadIntervals(@ARGV);
36PrintHeader();
37PrintNodes();
38PrintFrames();
39
40sub LoadIntervals {
41    my @files = @_;
42    my $id_counter = 0;
43my $file_id = 0;
44    foreach my $file (@files) {
45#print "processing file$file\n";
46	my $time;
47	my $linenum = 0;
48	open (IN, "<$file") || die("Unble to open \"$file\":$!");
49	while (<IN>) {
50#print;
51	    $linenum++;
52	    if (/# (\d+)/) {
53		$time = $1;
54	    }
55	    s/#.*//;
56	    next unless (/[^\s]/);
57	    my ($lat, $long, $value) = split /\s+/;
58	    if ($value =~ /^\d+$/) {
59		my $loc = "$lat $long";
60		unless (defined $time) {
61		    chop;
62		    die("$file\[$linenum\] $_\n\tfailed to find time");
63		}
64		my $id = $loc2id{$loc};
65		unless (defined $id) {
66		    $loc2id{$loc} = $id_counter++;
67		    $id = $loc2id{$loc};
68		}
69#print "time:$time id:$id\n";
70		$time2id2value{$time}{$id} = $value;
71		$time2total{$time} += $value;
72	    }
73#last if ($linenum > 10);
74	}
75#last if ($file_id++ > 10);
76    }
77}
78
79sub PrintFrames() {
80    print "# frames ------------------------------------\n";
81    foreach my $time (sort {$a<=>$b} keys %time2id2value) {
82print STDERR "total:$time ",$time2total{$time},"\n";
83	print "object:frame time:$time\n";
84	foreach my $id (sort {$a<=>$b} keys %{$time2id2value{$time}}) {
85	    my $value = $time2id2value{$time}{$id};
86	    print "$id $value\n";
87	}
88    }
89}
90
91sub PrintNodes() {
92    print "# nodes ------------------------------------\n";
93    print "object:nodes\n";
94    foreach my $loc (sort {$loc2id{$a}<=>$loc2id{$b}} keys %loc2id) {
95	my $id = $loc2id{$loc};
96	print "$id $loc\n";
97    }
98}
99
100sub PrintHeader {
101print<<EOP;
102object:global
103output:witty-hosts.gif
104frame-rate:1
105delay:60
106night-color: 0 0 0
107background-color: 75 75 75
108day-color: 200 200 200
109
110# World image ------------------------------------
111object:map
112label: x:20 y:20 font:giant Witty Worm Hosts
113label: x:20 y:30 type:date
114image:images/world-water.jpg
115x: 0
116y: 0
117image-geo: -180 -90 180 90
118width:700
119
120# Histogram -------------------------------------
121object:histogram
122x: 700
123y: 0
124y-axis: number of hosts
125width: 300
126height: 150
127
128# Legend -----------------------------------
129object:legend
130x: 700
131y: 170
132y-axis: number of hosts
133height: 150
134EOP
135}
136