#!/usr/bin/perl use Time::Local; # reads wd log files and outputs # meteohub raw format # command line argument: month and year in wd log format (eg. 72008) # will read lg, indoor and vantage logs. $mon_yr=$ARGV[0]; $lg_file=$mon_yr . "lg.txt"; $indoor_file=$mon_yr . "indoorlog.txt"; $vantage_file=$mon_yr . "vantagelog.txt"; open (LOG, $lg_file) or die "Can't open $lg_file\n"; while () { $line=$_; # this converts windows newlines to unix format $line =~ s/$1/\n/g if m/(\r\n?|\n\r?)/; # eat all newlines chomp $line; # remove space at the beginning of rows $line =~ s/^\s//g; # skips the first line next if $line =~ m/^day/; ($day,$month,$year,$hour,$minute, $temp,$hum,$dew,$baro,$windsp, $gustsp,$dir,$rainlastmin,$dailyrain,$monthlyrain, $yearlyrain,$heatindex)=split(/\s+/, $line); $year-=1900; $month-=1; ($utc_sec,$utc_min,$utc_hr,$utc_mday,$utc_month,$utc_year)= gmtime(timelocal(0,$minute,$hour,$day,$month,$year)); $utc_year+=1900; $utc_month++; # skip lines that fall to previous mth next if $utc_month < $month+1; $idx=sprintf("%04d%02d%02d%02d%02d", $utc_year, $utc_month, $utc_mday, $utc_hr, $utc_min); $items{$idx}{'th0_temp'}=int($temp*10); $items{$idx}{'th0_hum'}=int($hum); $items{$idx}{'th0_dew'}=addprefixzero(int($dew*10)); $items{$idx}{'thb0_baro'}=int($baro*10); $items{$idx}{'wind0_avg'}=int(knots2mps($windsp)*10); $items{$idx}{'wind0_gust'}=int(knots2mps($gustsp)*10); $items{$idx}{'wind0_dir'}=int($dir); $items{$idx}{'wind0_chill'}="00"; # Fake value! # reset hourly rain rate at the top of the hour if ($hour != $prev_hour) { $hourlyrain=0; } # need to calculate a fake value for alltime rain total # because meteohub uses it to calculate rain amounts. # we don't know the total at the end of previous month # so we need to come up with a value that is smaller # than any value at the end of previous month. # if you are addign log data between actual log data, # this may give you problems. if ($month_total==0) { $month_total=9999; } $prev_hour=$hour; # wd reports rain in minute increments $hourlyrain += int($rainlastmin*10); $month_total += int($rainlastmin*10); if ($hourlyrain > 0) { $rain0_hourrate=addprefixzero($hourlyrain); } else { $rain0_hourrate="0"; } $items{$idx}{'rain0_hourrate'}=$rain0_hourrate; $items{$idx}{'rain0_yesterday'}="0"; # manual says: not supported $items{$idx}{'rain0_alltime'}=$month_total; # see above } close (LOG); open (LOG, $indoor_file) or die "Can't open $indoor_file\n"; while () { $line=$_; # this converts windows newlines to unix format $line =~ s/$1/\n/g if m/(\r\n?|\n\r?)/; # eat all newlines chomp $line; # remove space at the beginning of rows $line =~ s/^\s//g; # skips the first line next if $line =~ m/^day/; ($day,$month,$year,$hour,$minute, $temp,$hum)=split(/\s+/, $line); $year-=1900; $month-=1; ($utc_sec,$utc_min,$utc_hr,$utc_mday,$utc_month,$utc_year)= gmtime(timelocal(0,$minute,$hour,$day,$month,$year)); $utc_year+=1900; $utc_month++; # skip lines that fall to previous mth next if $utc_month < $month+1; $idx=sprintf("%04d%02d%02d%02d%02d", $utc_year, $utc_month, $utc_mday, $utc_hr, $utc_min); $items{$idx}{'thb0_temp'}=int($temp*10); $items{$idx}{'thb0_hum'}=int($hum); } open (LOG, $vantage_file) or die "Can't open $vantage_file\n"; while () { $line=$_; # this converts windows newlines to unix format $line =~ s/$1/\n/g if m/(\r\n?|\n\r?)/; # eat all newlines chomp $line; # remove space at the beginning of rows $line =~ s/^\s//g; # skips the first line next if $line =~ m/^day/; ($day,$month,$year,$hour,$minute, $solar,$uv,$dailyet,$soil_moist,$soil_tmp)=split(/\s+/, $line); $year-=1900; $month-=1; ($utc_sec,$utc_min,$utc_hr,$utc_mday,$utc_month,$utc_year)= gmtime(timelocal(0,$minute,$hour,$day,$month,$year)); $utc_year+=1900; $utc_month++; # skip lines that fall to previous mth next if $utc_month < $month+1; $idx=sprintf("%04d%02d%02d%02d%02d", $utc_year, $utc_month, $utc_mday, $utc_hr, $utc_min); $items{$idx}{'uv0'}=int($uv); } foreach $key (sort keys %items) { print $key . "00 " . "thb0 " . $items{$key}{'thb0_temp'} . " " . $items{$key}{'thb0_hum'} . " " . $items{$key}{'thb0_temp'} . " " . $items{$key}{'thb0_baro'} . " " . "0 " . $items{$key}{'thb0_baro'} . "\n"; print $key . "01 " . "th0 " . $items{$key}{'th0_temp'} . " " . $items{$key}{'th0_hum'} . " " . $items{$key}{'th0_dew'} . "\n"; print $key . "02 " . "wind0 " . $items{$key}{'wind0_dir'} . " " . $items{$key}{'wind0_gust'} . " " . $items{$key}{'wind0_avg'} . " " . $items{$key}{'wind0_chill'} . "\n"; print $key . "03 " . "rain0 " . $items{$key}{'rain0_hourrate'} . " " . $items{$key}{'rain0_yesterday'} . " " . $items{$key}{'rain0_alltime'} . "\n"; print $key . "04 " . "uv0 " . $items{$key}{'uv0'} . "\n"; } sub knots2mps { local($knots)=@_; return $knots*0.5144444; } sub addprefixzero { local($to_add)=@_; local($return); if ($to_add < 0) { $return="-0" . abs(int($to_add)); } else { $return="0" . int($to_add); } return $return; }