#!/usr/bin/perl use DBI; use Math::Trig; use Config::IniFiles; use Time::ParseDate; use Log::Log4perl qw(:easy); Log::Log4perl->easy_init($DEBUG); Log::Log4perl->init("./conf/log4perl.conf"); my $logger = get_logger(); $cfg = new Config::IniFiles( -file => "./conf/noaa-modem.ini" ); $buoy_id = $ARGV[0]; $row_id = $ARGV[1]; $ndbc_ftp_site = $cfg->val('NDBC', 'ftp_site'); $ndbc_ftp_dir = $cfg->val('NDBC', 'ftp_dir'); $ndbc_ftp_user = $cfg->val('NDBC', 'ftp_user'); $ndbc_ftp_passwd = $cfg->val('NDBC', 'ftp_password'); $ndbc_sent_file_name = $cfg->val('NDBC', 'sent_file_name'); $ndbc_build_message_bin_dir = $cfg->val('NDBC', 'build_message_bin_dir'); $db_name = $cfg->val('DB', 'db_name'); $db_user = $cfg->val('DB', 'db_user'); $db_passwd = $cfg->val('DB', 'db_passwd'); #my $datafile = "./output/buoydata.txt"; # this file used to send data through BuildMessage my $datafile = "../demodata.txt"; # this file used to send data through BuildMessage $wsunits = "meters/second"; $msg_fmt = "FM64"; # Hash of buoys/stations # buoy, station_id, routing_id, latitude, longitude my %buoys_hash = ( buoy2 => ["FRP2", "41033", "SOVD83 KWBC", "32.26N", "80.42W"], buoy4 => ["CAP2", "41029", "SOVD83 KWBC", "32.80N", "79.62W"], buoy6 => ["SUN2", "41024", "SOVD83 KWBC", "33.83N", "78.48W"], buoy5 => ["CAP3", "41030", "SOVD83 KWBC", "32.50N", "79.32W"], buoy7 => ["SUN3", "41027", "SOVD83 KWBC", "33.31N", "78.15W"] ); if(!exists($buoys_hash{$buoy_id})) { $logger->error("$buoy_id not supported or $buoy_id is invalid."); exit 0; } $id = $buoys_hash{$buoy_id}[0]; $station_id = $buoys_hash{$buoy_id}[1]; $routing_id = $buoys_hash{$buoy_id}[2]; $latitude = $buoys_hash{$buoy_id}[3]; $longitude = $buoys_hash{$buoy_id}[4]; #establish database connection $dbh = DBI->connect ( "dbi:Pg:dbname=$db_name", $db_user, $db_passwd) or $logger->logdie("Couldn't connect to database: " . DBI->errstr); $query=" SELECT to_char(measurement_date,'YYYY/MM/DD HH24:MI'), buoy_lat, buoy_long, adcp_bin1_velocity,adcp_bin1_direction, adcp_bin2_velocity,adcp_bin2_direction, adcp_bin3_velocity,adcp_bin3_direction, adcp_bin4_velocity,adcp_bin4_direction, adcp_bin5_velocity,adcp_bin5_direction, adcp_bin6_velocity,adcp_bin6_direction, adcp_bin7_velocity,adcp_bin7_direction, adcp_bin8_velocity,adcp_bin8_direction, adcp_bin9_velocity,adcp_bin9_direction, adcp_bin10_velocity,adcp_bin10_direction, pcat_temp, pcat_salinity, pcat_pressure FROM $buoy_id where row_id = ?"; $sth=$dbh->prepare($query); $sth->execute($row_id) or $log->logdie("Couldn't execute statement: " . $sth->errstr); if ($sth ->rows == 0) { print "No Data Found.\n\n"; } else { # Read the matching records and print them out while (@db_data = $sth->fetchrow_array()) { $pcat_pressure = $db_data[25]; $adcp_bin1_velocity = $db_data[3]; $adcp_bin1_direction = $db_data[4]; $adcp_bin2_velocity = $db_data[5]; $adcp_bin2_direction = $db_data[6]; $adcp_bin3_velocity = $db_data[7]; $adcp_bin3_direction = $db_data[8]; $adcp_bin4_velocity = $db_data[9]; $adcp_bin4_direction = $db_data[10]; $adcp_bin5_velocity = $db_data[11]; $adcp_bin5_direction = $db_data[12]; $adcp_bin6_velocity = $db_data[13]; $adcp_bin6_direction = $db_data[14]; $adcp_bin7_velocity = $db_data[15]; $adcp_bin7_direction = $db_data[16]; $adcp_bin8_velocity = $db_data[17]; #if ($adcp_bin8_velocity eq '') {$adcp_bin8_velocity = 0}; $adcp_bin8_direction = $db_data[18]; #if ($adcp_bin8_direction eq '') {$adcp_bin8_direction = 0}; $adcp_bin9_velocity = $db_data[19]; #if ($adcp_bin9_velocity eq '') {$adcp_bin9_velocity = 0}; $adcp_bin9_direction = $db_data[20]; #if ($adcp_bin9_direction eq '') {$adcp_bin9_direction = 0}; $adcp_bin10_velocity = $db_data[21]; #if ($adcp_bin10_velocity eq '') {$adcp_bin10_velocity = 0}; $adcp_bin10_direction = $db_data[22]; #if ($adcp_bin10_direction eq '') {$adcp_bin10_direction = 0}; #$UV001 = sprintf("%.3f",cos(deg2rad($adcp_bin1_direction) * (($adcp_bin1_velocity)/10))); $UV001 = sprintf("%.3f",cos(deg2rad($adcp_bin1_direction)) * ($adcp_bin1_velocity)/10); #print "UV001 $adcp_bin1_direction,$adcp_bin1_velocity,$UV001\n"; $UV002 = sprintf("%.3f",cos(deg2rad($adcp_bin2_direction)) * ($adcp_bin2_velocity)/10); $UV003 = sprintf("%.3f",cos(deg2rad($adcp_bin3_direction)) * ($adcp_bin3_velocity)/10); $UV004 = sprintf("%.3f",cos(deg2rad($adcp_bin4_direction)) * ($adcp_bin4_velocity)/10); $UV005 = sprintf("%.3f",cos(deg2rad($adcp_bin5_direction)) * ($adcp_bin5_velocity)/10); $UV006 = sprintf("%.3f",cos(deg2rad($adcp_bin6_direction)) * ($adcp_bin6_velocity)/10); $UV007 = sprintf("%.3f",cos(deg2rad($adcp_bin7_direction)) * ($adcp_bin7_velocity)/10); $UV008 = sprintf("%.3f",cos(deg2rad($adcp_bin8_direction)) * ($adcp_bin8_velocity)/10); $UV009 = sprintf("%.3f",cos(deg2rad($adcp_bin9_direction)) * ($adcp_bin9_velocity)/10); $UV010 = sprintf("%.3f",cos(deg2rad($adcp_bin10_direction)) * ($adcp_bin10_velocity)/10); #print "UV010 $adcp_bin10_direction,$adcp_bin10_velocity,$UV010\n"; $VV001 = sprintf("%.3f",sin(deg2rad($adcp_bin1_direction)) * ($adcp_bin1_velocity)/10); #print "VV001 $adcp_bin1_direction,$adcp_bin1_velocity,$VV001\n"; $VV002 = sprintf("%.3f",sin(deg2rad($adcp_bin2_direction)) * ($adcp_bin2_velocity)/10); $VV003 = sprintf("%.3f",sin(deg2rad($adcp_bin3_direction)) * ($adcp_bin3_velocity)/10); $VV004 = sprintf("%.3f",sin(deg2rad($adcp_bin4_direction)) * ($adcp_bin4_velocity)/10); $VV005 = sprintf("%.3f",sin(deg2rad($adcp_bin5_direction)) * ($adcp_bin5_velocity)/10); $VV006 = sprintf("%.3f",sin(deg2rad($adcp_bin6_direction)) * ($adcp_bin6_velocity)/10); $VV007 = sprintf("%.3f",sin(deg2rad($adcp_bin7_direction)) * ($adcp_bin7_velocity)/10); $VV008 = sprintf("%.3f",sin(deg2rad($adcp_bin8_direction)) * ($adcp_bin8_velocity)/10); $VV009 = sprintf("%.3f",sin(deg2rad($adcp_bin9_direction)) * ($adcp_bin9_velocity)/10); $VV010 = sprintf("%.3f",sin(deg2rad($adcp_bin10_direction)) * ($adcp_bin10_velocity)/10); #print "VV010 $adcp_bin10_direction,$adcp_bin10_velocity,$VV010\n"; #want missing values to be represented as missing not zero if (($UV001 == '0.000') && ($VV001 = '0.000')) { $UV001 = ''; $VV001 = ''; }; if (($UV002 == '0.000') && ($VV002 = '0.000')) { $UV002 = ''; $VV002 = ''; }; if (($UV003 == '0.000') && ($VV003 = '0.000')) { $UV003 = ''; $VV003 = ''; }; if (($UV004 == '0.000') && ($VV004 = '0.000')) { $UV004 = ''; $VV004 = ''; }; if (($UV005 == '0.000') && ($VV005 = '0.000')) { $UV005 = ''; $VV005 = ''; }; if (($UV006 == '0.000') && ($VV006 = '0.000')) { $UV006 = ''; $VV006 = ''; }; if (($UV007 == '0.000') && ($VV007 = '0.000')) { $UV007 = ''; $VV007 = ''; }; if (($UV008 == '0.000') && ($VV008 = '0.000')) { $UV008 = ''; $VV008 = ''; }; if (($UV009 == '0.000') && ($VV009 = '0.000')) { $UV009 = ''; $VV009 = ''; }; if (($UV010 == '0.000') && ($VV010 = '0.000')) { $UV010 = ''; $VV010 = ''; }; $DV001 = 0; $DV002 = 0; $DV003 = 0; $DV004 = 0; $DV005 = 0; $DV006 = 0; $DV007 = 0; $DV008 = 0; $DV009 = 0; $DV010 = 0; #$TSDEP01 = 11.5; if ($id eq 'CAP3') { $bin_start = 2; $bin_width = 1.5; } else { #10m line FRP2, CAP2, SUN $bin_start = 1.5; $bin_width = 1; } my %bin_hash = ( "001" => [$UV001, $VV001, $bin_start], "002" => [$UV002, $VV002, $bin_start + 1*$bin_width], "003" => [$UV003, $VV003, $bin_start + 2*$bin_width], "004" => [$UV004, $VV004, $bin_start + 3*$bin_width], "005" => [$UV005, $VV005, $bin_start + 4*$bin_width], "006" => [$UV006, $VV006, $bin_start + 5*$bin_width], "007" => [$UV007, $VV007, $bin_start + 6*$bin_width], "008" => [$UV008, $VV008, $bin_start + 7*$bin_width], "009" => [$UV009, $VV009, $bin_start + 8*$bin_width], "010" => [$UV010, $VV010, $bin_start + 9*$bin_width] ); # account for tides exposing higher bins to air(erroneous) values @rsorted = reverse(sort(keys (%bin_hash))); LOOP: foreach $key (@rsorted) { if($pcat_pressure < $bin_hash{$key}[2]) { delete $bin_hash{$key}; } else { last LOOP; } } #print "010 :".$bin_hash{"010"}[0].": :".$bin_hash{"010"}[1].":\n"; @sorted = sort(keys (%bin_hash)); #$TSDEP01 = sprintf("%.3f", $pcat_pressure - $bin_hash{ @sorted[scalar(@sorted) - 1] }[2]); $TSDEP01 = sprintf("%.3f", $pcat_pressure); $measurement_date = $db_data[0]; $pcat_temp = sprintf("%.3f", $db_data[23]); $pcat_salinity = sprintf("%.3f", $db_data[24]); my @data = (); push @data, "STATION =$station_id"; push @data, "ROUTING_ID =$routing_id"; push @data, "DATE =$measurement_date"; push @data, "MSGFMT =$msg_fmt"; push @data, "WSUNITS =$wsunits"; push @data, "LAT =$latitude"; push @data, "LON =$longitude"; push @data, "TSDIGIT =7"; push @data, "TSDEPM =1"; push @data, "TSINST =830"; push @data, "TSWTTY =99"; push @data, "TSDEP01 =$TSDEP01"; push @data, "TSTMP01 =$pcat_temp"; push @data, "TSSAL01 =$pcat_salinity"; push @data, "CMDT =4"; push @data, "CMPD =9"; push @data, "CMSM =2"; push @data, "BN01DIST =$bin_start"; push @data, "BNLENGTH =$bin_width"; my $uv1; my $uv2; my $uv3; my $uv4; my $uv5; my $uv6; my $uv7; my $uv8; my $uv9; my $uv10; my $vv1; my $vv2; my $vv3; my $vv4; my $vv5; my $vv6; my $vv7; my $vv8; my $vv9; my $vv10; my $dv1; my $dv2; my $dv3; my $dv4; my $dv5; my $dv6; my $dv7; my $dv8; my $dv9; my $dv10; my $depth; if(scalar(@sorted) > 0) { $popped = pop(@sorted); $depth = $pcat_pressure - $bin_hash{ $popped }[2]; $TSDEP01 = $pcat_pressure - $bin_hash{ @sorted[scalar(@sorted) - 1] }[2]; $uv1 = $bin_hash{ $popped }[0]; $vv1 = $bin_hash{ $popped }[1]; $dv1 = sprintf("%.3f",$depth); } if(scalar(@sorted) > 0) { $popped = pop(@sorted); $depth = $pcat_pressure - $bin_hash{ $popped }[2]; $uv2 = $bin_hash{ $popped }[0]; $vv2 = $bin_hash{ $popped }[1]; $dv2 = sprintf("%.3f", $depth); } if(scalar(@sorted) > 0) { $popped = pop(@sorted); $depth = $pcat_pressure - $bin_hash{ $popped }[2]; $uv3 = $bin_hash{ $popped }[0]; $vv3 = $bin_hash{ $popped }[1]; $dv3 = sprintf("%.3f", $depth); } if(scalar(@sorted) > 0) { $popped = pop(@sorted); $depth = $pcat_pressure - $bin_hash{ $popped }[2]; $uv4 = $bin_hash{ $popped }[0]; $vv4 = $bin_hash{ $popped }[1]; $dv4 = sprintf("%.3f", $depth); } if(scalar(@sorted) > 0) { $popped = pop(@sorted); $depth = $pcat_pressure - $bin_hash{ $popped }[2]; $uv5 = $bin_hash{ $popped }[0]; $vv5 = $bin_hash{ $popped }[1]; $dv5 = sprintf("%.3f", $depth); } if(scalar(@sorted) > 0) { $popped = pop(@sorted); $depth = $pcat_pressure - $bin_hash{ $popped }[2]; $uv6 = $bin_hash{ $popped }[0]; $vv6 = $bin_hash{ $popped }[1]; $dv6 = sprintf("%.3f", $depth); } if(scalar(@sorted) > 0) { $popped = pop(@sorted); $depth = $pcat_pressure - $bin_hash{ $popped }[2]; $uv7 = $bin_hash{ $popped }[0]; $vv7 = $bin_hash{ $popped }[1]; $dv7 = sprintf("%.3f", $depth); } if(scalar(@sorted) > 0) { $popped = pop(@sorted); $depth = $pcat_pressure - $bin_hash{ $popped }[2]; $uv8 = $bin_hash{ $popped }[0]; $vv8 = $bin_hash{ $popped }[1]; $dv8 = sprintf("%.3f", $depth); } if(scalar(@sorted) > 0) { $popped = pop(@sorted); $depth = $pcat_pressure - $bin_hash{ $popped }[2]; $uv9 = $bin_hash{ $popped }[0]; $vv9 = $bin_hash{ $popped }[1]; $dv9 = sprintf("%.3f", $depth); } if(scalar(@sorted) > 0) { $popped = pop(@sorted); $depth = $pcat_pressure - $bin_hash{ $popped }[2]; $uv10 = $bin_hash{ $popped }[0]; $vv10 = $bin_hash{ $popped }[1]; $dv10 = sprintf("%.3f", $depth); } push @data, "UV001 =$uv1"; push @data, "UV002 =$uv2"; push @data, "UV003 =$uv3"; push @data, "UV004 =$uv4"; push @data, "UV005 =$uv5"; push @data, "UV006 =$uv6"; push @data, "UV007 =$uv7"; push @data, "UV008 =$uv8"; push @data, "UV009 =$uv9"; push @data, "UV010 =$uv10"; push @data, "UV011 ="; push @data, "UV012 ="; push @data, "UV013 ="; push @data, "UV014 ="; push @data, "UV015 ="; push @data, "UV016 ="; push @data, "UV017 ="; push @data, "UV018 ="; push @data, "UV019 ="; push @data, "UV020 ="; push @data, "VV001 =$vv1"; push @data, "VV002 =$vv2"; push @data, "VV003 =$vv3"; push @data, "VV004 =$vv4"; push @data, "VV005 =$vv5"; push @data, "VV006 =$vv6"; push @data, "VV007 =$vv7"; push @data, "VV008 =$vv8"; push @data, "VV009 =$vv9"; push @data, "VV010 =$vv10"; push @data, "VV011 ="; push @data, "VV012 ="; push @data, "VV013 ="; push @data, "VV014 ="; push @data, "VV015 ="; push @data, "VV016 ="; push @data, "VV017 ="; push @data, "VV018 ="; push @data, "VV019 ="; push @data, "VV020 ="; push @data, "DV001 =$dv1"; push @data, "DV002 =$dv2"; push @data, "DV003 =$dv3"; push @data, "DV004 =$dv4"; push @data, "DV005 =$dv5"; push @data, "DV006 =$dv6"; push @data, "DV007 =$dv7"; push @data, "DV008 =$dv8"; push @data, "DV009 =$dv9"; push @data, "DV010 =$dv10"; push @data, "DV011 ="; push @data, "DV012 ="; push @data, "DV013 ="; push @data, "DV014 ="; push @data, "DV015 ="; push @data, "DV016 ="; push @data, "DV017 ="; push @data, "DV018 ="; push @data, "DV019 ="; push @data, "DV020 ="; #$now = time(); #my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($now); #$year += 1900; #$mon++; #$mon = sprintf "%02d",$mon; #$mday = sprintf "%02d",$mday; #$hour = sprintf "%02d",$hour; #date format 2004/09/27 05:00 $year = substr($measurement_date,0,4); $mon = substr($measurement_date,5,2); $mday = substr($measurement_date,8,2); $hour = substr($measurement_date,11,2); my $file_date = "$year\_$mon\_$mday\_$hour".'00'; $ndbc_sent_file_name = $station_id."_CAROCOOPS_".$file_date.".dat"; $build_message = "cd $ndbc_build_message_bin_dir; ./BuildMessage > ./perl/data/$ndbc_sent_file_name"; open DATAFILE, ">$datafile" or $log->logdie("Can't open output file! $datafile: $!"); foreach (@data) { #print "\t$_\n"; print DATAFILE "$_\n"; } close DATAFILE; # FORMAT DATA with NDBC program BuildMessage #my $res = system($build_message); my $res = `$build_message`; print "$build_message returned $res.\n"; if ($res){ print STDERR "BuildMessage returned $res.\n"; } } my $ftpcmd = "/usr/local/bin/ncftpput -u $ndbc_ftp_user -p $ndbc_ftp_passwd -r 3 $ndbc_ftp_site $ndbc_ftp_dir"; $logger->debug("Executing command:"."$ftpcmd ./data/$ndbc_sent_file_name"); # FTP my @res = `$ftpcmd ./data/$ndbc_sent_file_name`; $logger->error(@res."\n"); }