Changeset 6

Show
Ignore:
Timestamp:
02/04/09 20:43:22 (3 years ago)
Author:
apribyl
Message:

version with concatenated graphs and some other improvements in display

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • weatherstats/weatherstats

    r5 r6  
    99# WeatherStats 
    1010# 
    11 # version:      0.4 
     11# version:      0.5 
    1212# 
    1313# author:       Adam Pribyl, covex@lowlevel.cz 
     
    2121sub help { 
    2222    print "usage: \n"; 
    23     print "     weatherstats            reads out the data from the weather station and stores them in round robin database\n"; 
    24     print "     weatherstats -g         reads out the data and generates graphs\n"; 
     23    print "     weatherstats -a <altitude>      reads out the data from the weather station and stores them in round robin database\n"; 
     24    print "     weatherstats -a <altitude> -g   reads out the data and generates graphs\n"; 
    2525    exit(0); 
    2626} 
     
    4646        my $rrdbn = $_[0]; 
    4747        my $value = $_[1]; 
    48         my $wname = $_[2]; 
     48        my $scale = $_[2]; 
     49        my $wname = $_[3]; 
     50        my $value_type = $rrdbn; 
    4951 
    5052        $tm = time; 
    5153 
     54        $min = "U"; 
     55        $max = "U"; #unlimited 
     56        if ($rrdbn =~ /temperature/) { 
     57            $min = "-60"; 
     58            $max = "60"; 
     59            $value_type =~ s/temperature//g; 
     60            $value_type =~ s/_//g; 
     61            push(@{$temperature{$value_type}}, $wname); 
     62            push(@{$temperature{$value_type}}, $value); 
     63            push(@{$temperature{$value_type}}, $scale); 
     64            $rrdbn = "temperature_".$value_type; 
     65        } elsif ($rrdbn eq "wind_speed" or $rrdbn eq "wind_gust") { 
     66            $min = "0"; 
     67            $max = "100"; #m/s > 330km/h 
     68            $value_type =~ s/wind//g; 
     69            $value_type =~ s/_//g; 
     70            push(@{$wind{$value_type}}, $wname); 
     71            push(@{$wind{$value_type}}, $value); 
     72            push(@{$wind{$value_type}}, $scale); 
     73            $rrdbn = "wind_".$value_type; 
     74        } elsif ($rrdbn eq "wind_direction") { 
     75            $min = "0"; 
     76            $max = "15"; 
     77            $value_type =~ s/wind//g; 
     78            $value_type =~ s/_//g; 
     79            push(@{$wind{$value_type}}, $wname); 
     80            push(@{$wind{$value_type}}, $value); 
     81            push(@{$wind{$value_type}}, $scale); 
     82            $rrdbn = "wind_".$value_type; 
     83        } elsif ($rrdbn =~ /humidity/) { 
     84            $min = "0"; 
     85            $max = "100"; #% 
     86            $value_type =~ s/humidity//g; 
     87            $value_type =~ s/_//g; 
     88            push(@{$humidity{$value_type}}, $wname); 
     89            push(@{$humidity{$value_type}}, $value); 
     90            push(@{$humidity{$value_type}}, $scale.$scale); 
     91            $rrdbn = "humidity_".$value_type; 
     92        } elsif ($rrdbn eq "rain_1h") { 
     93            $min = "0"; 
     94            $max = "100"; #mm/h 
     95            $value_type =~ s/rain//g; 
     96            $value_type =~ s/_//g; 
     97            push(@{$rain{$value_type}}, $wname); 
     98            push(@{$rain{$value_type}}, $value); 
     99            push(@{$rain{$value_type}}, $scale); 
     100            $rrdbn = "rain_".$value_type; 
     101        } elsif ($rrdbn eq "rain_total") { 
     102            $value_type =~ s/rain//g; 
     103            $value_type =~ s/_//g; 
     104            push(@{$rain{$value_type}}, $wname); 
     105            push(@{$rain{$value_type}}, $value); 
     106            push(@{$rain{$value_type}}, $scale); 
     107            $rrdbn = "rain_".$value_type; 
     108        } elsif ($rrdbn =~ /pressure/) { 
     109            $min = "800"; 
     110            $max = "1200"; 
     111            $value_type =~ s/pressure//g; 
     112            $value_type =~ s/_//g; 
     113            push(@{$pressure{$value_type}}, $wname); 
     114            push(@{$pressure{$value_type}}, $value); 
     115            push(@{$pressure{$value_type}}, $scale); 
     116            $rrdbn = "pressure_".$value_type; 
     117        } 
     118 
    52119        if ( !( -e $rrdbp.$rrdbn.".rrd") ) { 
    53                 $min = "U"; 
    54                 $max = "U"; #unlimited 
    55                 if ($wname =~ /temperature/) { 
    56                     $min = "-60"; 
    57                     $max = "60"; 
    58                 } elsif ($wname eq "wind_chill") { 
    59                     $min = "-60"; 
    60                     $max = "60"; 
    61                 } elsif ($wname eq "wind_speed" or $wname eq "wind_gust") { 
    62                     $min = "0"; 
    63                     $max = "100"; #m/s > 330km/h 
    64                 } elsif ($wname eq "wind_direction") { 
    65                     $min = "0"; 
    66                     $max = "15"; 
    67                 } elsif ($wname =~ /humidity/) { 
    68                     $min = "0"; 
    69                     $max = "100"; #% 
    70                 } elsif ($wname eq "rain_1h") { 
    71                     $min = "0"; 
    72                     $max = "100"; #mm/h 
    73                 } elsif ($wname eq "air_pressure") { 
    74                     $min = "800"; 
    75                     $max = "1200"; 
    76                 } 
    77                  
    78120                # every 120s (2min), 600s maximum interval between feeding data 
    79121                # 1. AVERAGE, every 1st value, store 720 (30*24) values (1day) 
     
    82124                # same for MAX values 
    83125                &callsys($rrdtoolp." create ".$rrdbp.$rrdbn.".rrd --start $tm -s 120 \\ 
    84                             DS:$rrdbn:GAUGE:600:".$min.":".$max." \\ 
     126                            DS:".$value_type.":GAUGE:600:".$min.":".$max." \\ 
    85127                            RRA:AVERAGE:0.5:1:720 \\ 
    86128                            RRA:AVERAGE:0.5:30:720 \\ 
    87129                            RRA:AVERAGE:0.5:720:1825 \\ 
     130                            RRA:MAX:0.5:1:720 \\ 
     131                            RRA:MAX:0.5:30:720 \\ 
     132                            RRA:MAX:0.5:720:1825 \\ 
    88133                "); 
    89134#               print "Database $rrdbn created.\n"; 
     
    91136        } else { 
    92137                &callsys($rrdtoolp." update ".$rrdbp.$rrdbn.".rrd ".$tm.":".$value); 
    93                 if ($graphs == 1) { 
    94                         #86400 = 24h 
    95                         $img = $wwwdir.$rrdbn."_24h.gif"; 
    96                         &callsys($rrdtoolp." graph ".$img." --start -86400 -i \\ 
    97                             --vertical-label \"$wname $scale\" \\ 
    98                             DEF:in=".$rrdbp.$rrdbn.".rrd:$rrdbn:AVERAGE \\ 
    99                             LINE1:in#00FF00:\"$rrdbn\"  \\ 
    100                             GPRINT:in:MAX:\"max %6.2lf %s\" \\ 
    101                             GPRINT:in:MIN:\"min %6.2lf %s\" \\ 
    102                             GPRINT:in:AVERAGE:\"avg %6.2lf %s\" \\ 
    103                         "); 
    104                         #2592000 = 30*86400 
    105                         $img = $wwwdir.$rrdbn."_30d.gif"; 
    106                         &callsys($rrdtoolp." graph ".$img." --start -2592000 -i\\ 
    107                             --vertical-label \"$wname\" \\ 
    108                             DEF:in=".$rrdbp.$rrdbn.".rrd:$rrdbn:AVERAGE \\ 
    109                             LINE1:in#00FF00:\"$rrdbn\"  \\ 
    110                             GPRINT:in:MAX:\"max %6.2lf %s\" \\ 
    111                             GPRINT:in:MIN:\"min %6.2lf %s\" \\ 
    112                             GPRINT:in:AVERAGE:\"avg %6.2lf %s\" \\ 
    113                         "); 
    114                         #365*86400 
    115                         $img = $wwwdir.$rrdbn."_1y.gif"; 
    116                         &callsys($rrdtoolp." graph ".$img." --start -31536000 -i\\ 
    117                             --vertical-label \"$wname\" \\ 
    118                             DEF:in=".$rrdbp.$rrdbn.".rrd:$rrdbn:AVERAGE \\ 
    119                             LINE1:in#00FF00:\"$rrdbn\"  \\ 
    120                             GPRINT:in:MAX:\"max %6.2lf %s\" \\ 
    121                             GPRINT:in:MIN:\"min %6.2lf %s\" \\ 
    122                             GPRINT:in:AVERAGE:\"avg %6.2lf %s\" \\ 
    123                         "); 
    124                         #365*5*86400 
    125                         $img = $wwwdir.$rrdbn."_5y.gif"; 
    126                         &callsys($rrdtoolp." graph ".$img." --start -157680000 -i\\ 
    127                             --vertical-label \"$wname\" \\ 
    128                             DEF:in=".$rrdbp.$rrdbn.".rrd:$rrdbn:AVERAGE \\ 
    129                             LINE1:in#00FF00:\"$rrdbn\"  \\ 
    130                             GPRINT:in:MAX:\"max %6.2lf %s\" \\ 
    131                             GPRINT:in:MIN:\"min %6.2lf %s\" \\ 
    132                             GPRINT:in:AVERAGE:\"avg %6.2lf %s\" \\ 
    133                         "); 
     138        }; 
     139}; 
     140 
     141 
     142sub create_graphs_for { 
     143        my %hasharray = %{$_[0]}; 
     144        my $wname = $_[1]; 
     145        my %gtimes = ( 
     146                "24h" => 86400, 
     147                "30d" => 2592000, 
     148                "1y"  => 31536000, 
     149                "5y"  => 157680000  
     150        ); 
     151        my @color = ( "#dc0000", "#dc00dc", "#5800dc", "#009bdc", "#00dc87" ); 
     152        my $coloridx = 0; 
     153        #                       N          NNE          NE      ENE         E           ESE      SE         SSE         S         SSW           SW      WSW             W       WNW       NW            NNw 
     154        my @direction_cols = ( "#ff0000", "#ff6600", "#ffcc00", "#ccff00", "#66ff00", "#00ff00", "#00ff66", "#00ffcc", "#00ccff", "#0066ff", "#0000ff", "#6600ff", "#cc00ff", "#ff00cc", "#ff0066", "#ff0000"); 
     155 
     156        foreach $gtime (keys %gtimes) { 
     157                $arguments = ""; 
     158                $coloridx = 0; 
     159                $img = $wwwdir.$wname."_".$gtime.".gif"; 
     160                $cmd_arguments = $rrdtoolp." graph ".$img." --start -".$gtimes{$gtime}." --width 550 --height 150 --vertical-label \"".ucfirst($wname)."\" --watermark \"WeatherStats\" \\"; #jak udelat scale?? --vertical-label".$scale\" \\"; 
     161 
     162                # compile the graphs arguments 
     163                if ($wname eq "temperature") { 
     164                        foreach $atribute (keys %hasharray) { 
     165                                if ($atribute =~ /out/) { 
     166                                        $scale = ${$hasharray{$atribute}}[2]; 
     167                                        $arguments = $arguments."DEF:".$atribute."=".$rrdbp.$wname."_".$atribute.".rrd:".$atribute.":AVERAGE "; 
     168                                        $arguments = $arguments."LINE1:".$atribute.$color[$coloridx].":\"".${$hasharray{$atribute}}[0]."\" "; 
     169                                        $arguments = $arguments."GPRINT:".$atribute.":MIN:\"Min\\: %4.2lf \" "; 
     170                                        $arguments = $arguments."GPRINT:".$atribute.":AVERAGE:\"Avg\\: %4.2lf \" "; 
     171                                        $arguments = $arguments."GPRINT:".$atribute.":MAX:\"Max\\: %4.2lf ".$scale."\\j\" "; 
     172                                        $coloridx++; 
     173                                } 
     174                        } 
     175                        $arguments = $arguments."HRULE:0#474747"; 
     176                        $cmd_arguments_tmp = $cmd_arguments; 
     177                        $cmd_arguments_tmp =~ s/$gtime/out_$gtime/g; 
     178                        &callsys($cmd_arguments_tmp.$arguments); 
     179                        $arguments = ""; 
     180                        $coloridx = 0; 
     181                        foreach $atribute (keys %hasharray) { 
     182                                $scale = ${$hasharray{$atribute}}[2]; 
     183                                $arguments = $arguments."DEF:".$atribute."=".$rrdbp.$wname."_".$atribute.".rrd:".$atribute.":AVERAGE "; 
     184                                $arguments = $arguments."LINE1:".$atribute.$color[$coloridx].":\"".${$hasharray{$atribute}}[0]."\" "; 
     185                                $arguments = $arguments."GPRINT:".$atribute.":MIN:\"Min\\: %4.2lf \" "; 
     186                                $arguments = $arguments."GPRINT:".$atribute.":AVERAGE:\"Avg\\: %4.2lf \" "; 
     187                                $arguments = $arguments."GPRINT:".$atribute.":MAX:\"Max\\: %4.2lf ".$scale."\\j\" "; 
     188                                $coloridx++; 
     189                        } 
     190                        $arguments = $arguments."HRULE:0#474747"; 
     191                } elsif ($wname eq "wind") { 
     192                        foreach $atribute (keys %hasharray) { 
     193                                if ($atribute eq "direction") { 
     194                                        $scale = ${$hasharray{$atribute}}[2]; 
     195                                        $arguments = $arguments."DEF:".$atribute."=".$rrdbp.$wname."_".$atribute.".rrd:".$atribute.":MAX "; 
     196                                        $arguments = $arguments."CDEF:North=".$atribute.",0,GE,-4,* "; 
     197                                        $arguments = $arguments."CDEF:NNE=".$atribute.",1,GE,-4,* "; 
     198                                        $arguments = $arguments."CDEF:NE=".$atribute.",2,GE,-4,* "; 
     199                                        $arguments = $arguments."CDEF:ENE=".$atribute.",3,GE,-4,* "; 
     200                                        $arguments = $arguments."CDEF:East=".$atribute.",4,GE,-4,* "; 
     201                                        $arguments = $arguments."CDEF:ESE=".$atribute.",5,GE,-4,* "; 
     202                                        $arguments = $arguments."CDEF:SE=".$atribute.",6,GE,-4,* "; 
     203                                        $arguments = $arguments."CDEF:SSE=".$atribute.",7,GE,-4,* "; 
     204                                        $arguments = $arguments."CDEF:South=".$atribute.",8,GE,-4,* "; 
     205                                        $arguments = $arguments."CDEF:SSW=".$atribute.",9,GE,-4,* "; 
     206                                        $arguments = $arguments."CDEF:SW=".$atribute.",10,GE,-4,* "; 
     207                                        $arguments = $arguments."CDEF:WSW=".$atribute.",11,GE,-4,* "; 
     208                                        $arguments = $arguments."CDEF:West=".$atribute.",12,GE,-4,* "; 
     209                                        $arguments = $arguments."CDEF:WNW=".$atribute.",13,GE,-4,* "; 
     210                                        $arguments = $arguments."CDEF:NW=".$atribute.",14,GE,-4,* "; 
     211                                        $arguments = $arguments."CDEF:NNW=".$atribute.",15,GE,-4,* "; 
     212                                        $arguments = $arguments."AREA:North".$direction_cols[0].":N "; 
     213                                        $arguments = $arguments."AREA:NNE".$direction_cols[1].": "; 
     214                                        $arguments = $arguments."AREA:NE".$direction_cols[2].":NE "; 
     215                                        $arguments = $arguments."AREA:ENE".$direction_cols[3].": "; 
     216                                        $arguments = $arguments."AREA:East".$direction_cols[4].":E "; 
     217                                        $arguments = $arguments."AREA:ESE".$direction_cols[5].": "; 
     218                                        $arguments = $arguments."AREA:SE".$direction_cols[6].":SE "; 
     219                                        $arguments = $arguments."AREA:SSE".$direction_cols[7].": "; 
     220                                        $arguments = $arguments."AREA:South".$direction_cols[8].":S "; 
     221                                        $arguments = $arguments."AREA:SSW".$direction_cols[9].": "; 
     222                                        $arguments = $arguments."AREA:SW".$direction_cols[10].":SW "; 
     223                                        $arguments = $arguments."AREA:WSW".$direction_cols[11].": "; 
     224                                        $arguments = $arguments."AREA:West".$direction_cols[12].":W "; 
     225                                        $arguments = $arguments."AREA:WNW".$direction_cols[13].": "; 
     226                                        $arguments = $arguments."AREA:NW".$direction_cols[14].":\"NW\\l\" "; 
     227                                        $arguments = $arguments."AREA:NNW".$direction_cols[15].": "; 
     228                                } else { 
     229                                        $scale = ${$hasharray{$atribute}}[2]; 
     230                                        $arguments = $arguments."DEF:".$atribute."=".$rrdbp.$wname."_".$atribute.".rrd:".$atribute.":MAX "; 
     231                                        $arguments = $arguments."LINE1:".$atribute.$color[$coloridx].":\"".${$hasharray{$atribute}}[0]."\" "; 
     232                                        $arguments = $arguments."GPRINT:".$atribute.":MIN:\"Min\\: %4.2lf \" "; 
     233                                        $arguments = $arguments."GPRINT:".$atribute.":AVERAGE:\"Avg\\: %4.2lf \" "; 
     234                                        $arguments = $arguments."GPRINT:".$atribute.":MAX:\"Max\\: %4.2lf ".$scale."\\j\" "; 
     235                                        $coloridx++; 
     236                                } 
     237                        } 
     238                } elsif ($wname eq "rain") { 
     239                        foreach $atribute (keys %hasharray) { 
     240                                if ($atribute eq "1h") { 
     241                                        $scale = ${$hasharray{$atribute}}[2]; 
     242                                        $arguments = $arguments."DEF:".$atribute."=".$rrdbp.$wname."_".$atribute.".rrd:".$atribute.":AVERAGE "; 
     243                                        $arguments = $arguments."LINE1:".$atribute.$color[$coloridx].":\"".${$hasharray{$atribute}}[0]."\" "; 
     244                                        $arguments = $arguments."GPRINT:".$atribute.":MIN:\"Min\\: %4.2lf \" "; 
     245                                        $arguments = $arguments."GPRINT:".$atribute.":AVERAGE:\"Avg\\: %4.2lf \" "; 
     246                                        $arguments = $arguments."GPRINT:".$atribute.":MAX:\"Max\\: %4.2lf ".$scale."\\j\" "; 
     247                                        $coloridx++; 
     248                                        $cmd_arguments_tmp = $cmd_arguments; 
     249                                        $cmd_arguments_tmp =~ s/$gtime/1h_$gtime/g; 
     250                                        &callsys($cmd_arguments_tmp.$arguments); 
     251                                        $arguments = ""; 
     252                                } else { 
     253                                        $scale = ${$hasharray{$atribute}}[2]; 
     254                                        $arguments = $arguments."DEF:".$atribute."=".$rrdbp.$wname."_".$atribute.".rrd:".$atribute.":AVERAGE "; 
     255                                        $arguments = $arguments."LINE1:".$atribute.$color[$coloridx].":\"".${$hasharray{$atribute}}[0]."\" "; 
     256                                        $arguments = $arguments."GPRINT:".$atribute.":MIN:\"Min\\: %4.2lf \" "; 
     257                                        $arguments = $arguments."GPRINT:".$atribute.":AVERAGE:\"Avg\\: %4.2lf \" "; 
     258                                        $arguments = $arguments."GPRINT:".$atribute.":MAX:\"Max\\: %4.2lf ".$scale."\\j\" "; 
     259                                        $coloridx++; 
     260                                } 
     261                        } 
     262                } else { 
     263                        foreach $atribute (keys %hasharray) { 
     264                                $scale = ${$hasharray{$atribute}}[2]; 
     265                                $arguments = $arguments."DEF:".$atribute."=".$rrdbp.$wname."_".$atribute.".rrd:".$atribute.":AVERAGE "; 
     266                                $arguments = $arguments."LINE1:".$atribute.$color[$coloridx].":\"".${$hasharray{$atribute}}[0]."\" "; 
     267                                $arguments = $arguments."GPRINT:".$atribute.":MIN:\"Min\\: %4.2lf \" "; 
     268                                $arguments = $arguments."GPRINT:".$atribute.":AVERAGE:\"Avg\\: %4.2lf \" "; 
     269                                $arguments = $arguments."GPRINT:".$atribute.":MAX:\"Max\\: %4.2lf ".$scale."\\j\" "; 
     270                                $coloridx++; 
     271                        } 
    134272                } 
    135         } 
    136 } 
    137  
     273                &callsys($cmd_arguments.$arguments); 
     274        } 
     275} 
     276 
     277sub create_graphs { 
     278        &create_graphs_for(\%temperature, "temperature"); 
     279        &create_graphs_for(\%humidity, "humidity"); 
     280        &create_graphs_for(\%wind, "wind"); 
     281        &create_graphs_for(\%rain, "rain"); 
     282        &create_graphs_for(\%pressure, "pressure"); 
     283} 
    138284 
    139285sub print_header { 
     
    144290        print INDEX "<h1>Weather statistic</h1>\n"; 
    145291        $date = `date`; 
    146         print INDEX "<p>Generated: ".$date."</p>"; 
     292        print INDEX "<p>Generated: ".$date."</p>\n"; 
    147293} 
    148294 
    149295sub print_footer { 
     296        print INDEX "<div id=\"credits\">WeatherStats - <a href=\"http://meteolinger.lowlevel.cz/\">http://meteolinger.lowlevel.cz/</a></div>\n"; 
    150297        print INDEX "</body></html>"; 
    151298        close INDEX; 
     
    158305} 
    159306 
    160 sub print_body { 
    161         my $rrdbn = $_[0]; 
    162         my $value = $_[1]; 
    163         my $wname = $_[2]; 
    164         my $scale = $_[3]; 
    165         print INDEX "<div id=\"$rrdbn\" class=\"wname\"><a href=".$rrdbn.".html>".$wname."</a></br>\n"; 
    166         printf INDEX "<span class=\"wvalue\"><a href=\"%s.html\">%.1f</span><span class=\"wscale\">&nbsp;$scale</span></a><br />\n", $rrdbn, $value; 
    167         print INDEX "<a href=".$rrdbn.".html><img src=\"".$rrdbn."_24h.gif\" width=\"165\" height=\"54\"/></a></div>\n"; 
     307sub print_body_line { 
     308        my @atribute = @_; 
     309        printf INDEX "<div>\n<div class=\"wname\"><a href=\"$atribute[0].html\">$atribute[1]</a></div> 
     310<span class=\"wvalue\"><a href=\"$atribute[0].html\">%.1f</a></span> 
     311<span class=\"wscale\"><a href=\"$atribute[0].html\">%s</a></span>\n</div>\n", $atribute[3], $atribute[4]; 
    168312} 
    169313 
    170314sub print_page { 
    171         my $rrdbn = $_[0]; 
    172         my $wname = $_[1]; 
    173         open ( PAGE, ">$wwwdir".$rrdbn."\.html") || die "ERROR: WeatherStats: Coould not create page $wwwdir $rrdbn"; 
     315        my $page = $_[0]; 
     316        open ( PAGE, ">$wwwdir".$page."\.html") || die "ERROR: WeatherStats: Could not create page $wwwdir $rrdbn"; 
    174317        print PAGE "<html>\n<head><title>Weather statistic from TOPCOM 265 NE</title></head>\n<body>\n"; 
    175         print PAGE "<h1>$wname</h1>\n"; 
    176         print PAGE "<img src=\"".$rrdbn."_24h.gif\"/></br>\n"; 
    177         print PAGE "<img src=\"".$rrdbn."_30d.gif\"/></br>\n"; 
    178         print PAGE "<img src=\"".$rrdbn."_1y.gif\"/></br>\n"; 
    179         print PAGE "<img src=\"".$rrdbn."_5y.gif\"/></br>\n"; 
     318        print PAGE "<h1>".ucfirst($page)."</h1>\n"; 
     319        print PAGE "<img src=\"".$page."_24h.gif\"/><br />\n"; 
     320        print PAGE "<img src=\"".$page."_30d.gif\"/><br />\n"; 
     321        print PAGE "<img src=\"".$page."_1y.gif\"/><br />\n"; 
     322        print PAGE "<img src=\"".$page."_5y.gif\"/><br />\n"; 
    180323        print PAGE "</body></html>"; 
    181324        close PAGE 
    182325} 
    183326 
    184  
     327sub print_body_begin { 
     328        my $wtype = $_[0]; 
     329 
     330        print INDEX "<div id=\"".$wtype."\">\n"; 
     331        print INDEX "<span class=\"wimg\"><a href=\"".$wtype.".html\"><img src=\"".$wtype."_24h.gif\" width=\"164\" /></a></span><br />\n"; 
     332        print INDEX "<a class=\"mname\" href=\"".$wtype.".html\">".ucfirst($wtype)."</a><br/>\n"; 
     333} 
     334 
     335sub print_body { 
     336 
     337 
     338        $wtype = "temperature"; 
     339        &print_body_begin($wtype); 
     340        $wtype = "temperature_out"; 
     341        &print_body_begin($wtype); 
     342        print INDEX "</div>\n"; 
     343        &print_page($wtype); 
     344        $wtype = "temperature"; 
     345        foreach $atribute (keys %temperature) { 
     346                &print_body_line($wtype,$atribute, ${$temperature{$atribute}}[0], ${$temperature{$atribute}}[1], ${$temperature{$atribute}}[2]); 
     347        } 
     348        print INDEX "</div>\n"; 
     349        &print_page($wtype); 
     350 
     351        $wtype = "humidity"; 
     352        &print_body_begin($wtype); 
     353        foreach $atribute (keys %humidity) { 
     354                &print_body_line($wtype, $atribute, ${$humidity{$atribute}}[0], ${$humidity{$atribute}}[1], ${$humidity{$atribute}}[2]); 
     355        } 
     356        print INDEX "</div>\n"; 
     357        &print_page($wtype); 
     358 
     359        $wtype = "wind"; 
     360        &print_body_begin($wtype); 
     361        foreach $atribute (keys %wind) { 
     362                &print_body_line($wtype, $atribute, ${$wind{$atribute}}[0], ${$wind{$atribute}}[1], ${$wind{$atribute}}[2]); 
     363        } 
     364        print INDEX "</div>\n"; 
     365        &print_page($wtype); 
     366 
     367        $wtype = "rain"; 
     368        &print_body_begin($wtype); 
     369        $wtype = "rain_1h"; 
     370        &print_body_begin($wtype); 
     371        print INDEX "</div>\n"; 
     372        &print_page($wtype); 
     373        $wtype = "rain"; 
     374        foreach $atribute (keys %rain) { 
     375                &print_body_line($wtype, $atribute, ${$rain{$atribute}}[0], ${$rain{$atribute}}[1], ${$rain{$atribute}}[2]); 
     376        } 
     377        print INDEX "</div>\n"; 
     378        &print_page($wtype); 
     379 
     380        $wtype = "pressure"; 
     381        &print_body_begin($wtype); 
     382        foreach $atribute (keys %pressure) { 
     383                &print_body_line("$wtype", $atribute, ${$pressure{$atribute}}[0], ${$pressure{$atribute}}[1], ${$pressure{$atribute}}[2]); 
     384        } 
     385        print INDEX "</div>\n"; 
     386        &print_page($wtype); 
     387} 
     388 
     389 
     390 
     391################# 
    185392##### MAIN ###### 
     393################# 
    186394 
    187395@wwsr = `./wwsr -y`; 
    188 #@wwsr = `cat wwsr2.out`; 
     396#@wwsr = `cat wwsr6.out`; 
     397#@wwsr = `tail -14 /var/www/html/wwss/log`; 
    189398 
    190399$wwwdir = "/var/www/html/wwss/"; 
     400#$wwwdir = "wwss/"; 
    191401$en_dbg = 0; 
    192402$rrdtoolp = "/usr/bin/rrdtool"; 
    193403$rrdbp = "/var/www/html/wwss/"; 
     404#$rrdbp = "wwss/"; 
    194405 
    195406$graphs = 0; 
     
    197408# What options are defined on command line? 
    198409if ( defined($ARGV[0]) ) { 
    199         foreach $argmnt (@ARGV) { 
     410        for ($i = 0; $i < @ARGV; $i++) { 
     411                $argmnt = $ARGV[$i]; 
    200412                if ($argmnt eq "-g") { 
    201413                        $graphs = 1; 
    202                 } else { 
    203                         &help; 
    204414                } 
    205         } 
    206 } 
    207  
    208 &print_header; 
     415                if ($argmnt eq "-a") { 
     416                        if (defined ($ARGV[$i+1] and length($ARGV[$i+1] == 1)) ) { 
     417                                $altitude = $ARGV[$i+1]; 
     418                                $i++; 
     419                        } else {  
     420                                die "ERROR: Unknown or missing argument to -a" 
     421                        } 
     422                } 
     423        } 
     424} else { 
     425        &help; 
     426}; 
     427 
    209428 
    210429open (LOG, ">>$wwwdir"."log") || die "ERROR: no log"; 
     
    228447} 
    229448 
     449 
     450# define main hashes for value groups 
     451%temperature = (); 
     452%pressure = (); 
     453%humidity = (); 
     454%wind = (); 
     455%rain = (); 
     456 
     457# store all values that wwsr returns 
    230458foreach $line (@wwsr) { 
    231459        chomp $line; 
     
    239467                $rrdbn = lc($rrdbn); 
    240468                 
    241                 if ($rrdbn eq "outdoor_temperature") { 
    242                         $tout = $value; 
    243                 } 
    244                 if ($rrdbn eq "wind_speed") { 
    245                         $rrdbnt = "wind_chill"; 
    246                         $wname = "Wind chill"; 
    247                         # windchill calculation is only valid for wind speed > 4.5km/h ~ 1.25m/s 
    248                         if ($value > 1.25) {  
    249                                 $ws = $value**0.16; 
    250                                 #Wind chill 
    251                                 $twc = 13.12+(0.6215*$tout)-(11.37*($ws))+((0.3965*$tout)*($ws)); 
    252                         } else { 
    253                                 $twc = $tout; 
    254                         } 
    255                         &insertdb($rrdbnt,$twc,$wname); #store also wind chill 
    256                         &print_page($rrdbnt,$wname); 
    257                         &print_body($rrdbnt,$twc,$wname,"C"); 
    258                 } 
    259  
    260                 &insertdb($rrdbn,$value,$aline[0]); 
    261                 &print_page($rrdbn,$aline[0]); 
    262                 &print_body($rrdbn,$value,$aline[0],$scale); 
    263         } 
    264 } 
    265  
     469                if ($rrdbn eq "interval") { next }; 
     470 
     471                &insertdb($rrdbn,$value,$scale,$aline[0]); 
     472#               &print_page($rrdbn,$aline[0]); 
     473#               &print_body($rrdbn,$value,$aline[0],$scale); 
     474        } 
     475}; 
     476 
     477# additional calculate values 
     478$rrdbnt = "temperature_feel-out"; 
     479$wname = "Feels like temperature"; 
     480$value = ${$wind{'speed'}}[1]; 
     481$tout = ${$temperature{'outdoor'}}[1]; 
     482# windchill calculation is only valid for wind speed > 4.5km/h ~ 1.25m/s 
     483if ($value > 1.25) {  
     484        $ws = $value**0.16; 
     485        #Wind chill 
     486        $twc = 13.12+(0.6215*$tout)-(11.37*($ws))+((0.3965*$tout)*($ws)); 
     487} else { 
     488        $twc = $tout; 
     489} 
     490&insertdb($rrdbnt,$twc,"C",$wname); 
     491 
     492$rrdbnt = "pressure_air-sea"; 
     493$wname = "Air pressure sea"; 
     494$value = ${$pressure{'air'}}[1]; 
     495$scale = ${$pressure{'air'}}[2]; 
     496# aaMADIS calculation 
     497$k1 = 0.190284; # discrepency with calculated k1 probably because Smithsonian used less precise gas constant and gravity values 
     498$k2 = 8.4184960528E-5; # (standardLapseRate / standardTempK) * (Power(standardSLP, k1) 
     499$aps = ((($value-0.3)**$k1)+($k2*$altitude))**(1/$k1); # Power(Power(pressureHPa - 0.3, k1) + (k2 * elevationM), 1/k1); 
     500&insertdb($rrdbnt,$aps,$scale,$wname); 
     501 
     502$rrdbnt = "temperature_dew-out"; 
     503$wname = "Due point temperature"; 
     504$value = ${$humidity{'outdoor'}}[1]; 
     505$tdp = $tout-((100-$value)/5); # aproximation 
     506&insertdb($rrdbnt,$tdp,"C",$wname); 
     507 
     508&print_header; 
     509&print_body; 
    266510&print_footer; 
     511 
     512if ($graphs == 1) { 
     513        &create_graphs; 
     514} 
     515 
     516