#!/usr/bin/perl
use strict;
use XML::LibXML;
my ($env,$unit_conversion) = @ARGV;
my $unit_suffix;
if ($unit_conversion eq 'en') { $unit_suffix = '_en'; }
#load database and path info
my $xp_env = XML::LibXML->new->parse_file("../environment_xenia_$env.xml");
my $dir_base = $xp_env->findvalue('//path/dir_base');
my $http_base = $xp_env->findvalue('//path/http_base');
my $xp_graph = XML::LibXML->new->parse_file("environment_xenia_graph.xml");
my $xp_latest = XML::LibXML->new->parse_file($dir_base.'latest_obs.xml');
my ($html_body,$html_time);
foreach my $platform ($xp_latest->findnodes('//platform')) {
#print "platform:$platform\n"; #debug
$html_body = '';
$html_time = '';
my $platform_handle = $platform->find('platformHandle');
print "$platform_handle\n";
#my $platform_url = $platform->find('platformURL');
my ($value, $datetime);
foreach my $observation ($platform->findnodes('observationList/observation')) {
if ($html_time eq '') {
my $time = $observation->find('dateTime'); #note this assumes all observations have same dateTime
$time =~ s/T/ /g;
$time = `date --date='$time' '+%B %-d, %l:%M %p %Z'`;
chomp($time);
print "$time\n";
$html_time = "Click on the links below for further graphs and data or here for archived data files
Last update $time
";
}
#get m_type_id and associated display info
my $m_type_id = int($observation->find('m_type_id'));
my $title = $xp_graph->findvalue('//observation_list/observation[@m_type_id="'.$m_type_id.'"]/title');
my $obs_type = $observation->find('type');
my $uom_type = $observation->find('uom');
if ($unit_conversion eq 'en') { #en is for the default english unit conversion
$uom_type = $xp_graph->findvalue('//observation_list/observation[@m_type_id="'.$m_type_id.'"]/standard_uom_en');
}
print " $obs_type:$uom_type\n";
my $standard_uom = $xp_graph->findvalue('//observation_list/observation[@m_type_id="'.$m_type_id.'"]/standard_uom');
my $y_title = $xp_graph->findvalue('//unit_conversion_list/unit_conversion[@id="'.$standard_uom.'_to_'.$uom_type.'"]/y_title');
#apply conversion as needed
my $value = $observation->find('value');
my $conversion_formula = $xp_graph->findvalue('//unit_conversion_list/unit_conversion[@id="'.$standard_uom.'_to_'.$uom_type.'"]/conversion_formula');
#unit conversion using supplied equation(e.g. celcius to fahrenheit)
$conversion_formula =~ s/var1/$value/g;
my $conversion_val = eval $conversion_formula;
#extra display info for winds, etc
my $extra_display = '';
if ($m_type_id == 3) { $extra_display = ' (from '.&conv_degrees_to_compass($conversion_val).')'; }
$html_body .= "$title($y_title) $conversion_val$extra_display
";
#print "$value\n";
} #observation
open (HTML_FILE,">$dir_base\platform/$platform_handle/html/latest$unit_suffix.html");
print HTML_FILE $html_time;
print HTML_FILE $html_body."
";
close (HTML_FILE);
} #platform
exit 0;
#this sub called for wind direction, etc
sub conv_degrees_to_compass{
my $degrees = shift;
if ($degrees eq 'NULL') { return $degrees; }
my @compass = qw(N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW);
$degrees = ($degrees + 22.5) / 22.5;
$degrees -= .5;
my $quad = $degrees % 16;
return $compass[$quad];
}