#!/afs/isis/pkg/isis/bin/perl -w # Last modified: Time-stamp: <2004-10-13 08:29:21 haines> # # Abstract: script to process data, run NDBC encoder, and push data to NDBC. # # Usage: % proc2ndbc.pl [-d] # # Author: Sara Haines (2004-10) # modified by Sara Haines to work with NC-COOS data and processing steps # if debugging requested print messages to STDOUT if (grep /[debug|DEBUG|d]/, @ARGV) { $debug = 1; } # set the group system("newgrp seacoos"); ###################################################################### # Set all necessary paths and libraries ###################################################################### # # on nemo $data_dir = "/seacoos/data/nc-coos/latest_ndbc"; $log_dir = "/opt/local/seacoos/log"; $bin_dir = "/opt/local/seacoos/bin"; $matl = "/opt/local/seacoos/matlab"; $bp = "/afs/isis/pkg/matlab/bin"; # test on pitot # $haines_dir = "/afs/isis.unc.edu/depts/marine/workspace/haines"; # $work_dir = $haines_dir ."/nc-coos/dev_proc2ndbc"; # $data_dir = $work_dir. "/test_data"; # $bin_dir = $work_dir ."/bin"; # $log_dir = $work_dir ."/log"; # $matl = $work_dir; # $bp = "/afs/isis/pkg/matlab/bin"; use Net::FTP; use Date::Manip; # print Date and Time and version of perl + patchlevel /1000 $now=&UnixDate(&ParseDate("today"), "%Y:%m:%d %H:%M:%S"); if ($debug) { print "\n==== Starting: $now ==== Perl Version: $]\n"; } # name card file with current date and time in GMT $date = &ParseDate("today"); $tz = &Date_TimeZone($date); $date = &Date_ConvTZ($date, $tz, "GMT"); $filename_date = &UnixDate($date, "%Y%m%d_%H%M"); # get current directory $cwd = `pwd`; # start stop-watch for cummulative times $starttime = time; # %ENV is a hash (associative array) containg environment variables. # set environment variable MATUSER for use with MATLAB startup.m file $ENV{MATUSER} = "none"; if ($debug) { print "MATUSER should be set to $ENV{MATUSER}\n"; } # set environment variable DISPLAY for use with MATLAB under script in tcsh $ENV{DISPLAY} = "nemo.isis.unc.edu:0.0"; if ($debug) { print "DISPLAY should be set to $ENV{DISPLAY}\n"; } chdir "$matl/proc2ndbc"; # process raw data, be sure to include -nojvm for MATLAB v6.1 system("$bp/matlab -nojvm -nosplash < $matl/proc2ndbc/run_proc2ndbc.m >> $log_dir/mat_proc2ndbc.log") && die "run_proc2ndbc failed"; ######################## # (2) generate NDBC card images ######################## chdir "$data_dir"; @locallist = (glob("$data_dir/ndbc-input*CMAN")); # print "$#locallist\n"; # print "@locallist\n"; if ($#locallist>=0) { $out_file1 = "$data_dir/nccoos_${filename_date}_CMAN"; print "Writing ... $out_file1\n"; open OUT,">$out_file1"; # read the file and encode. for (@locallist) { # print "$_\n"; $line = $_; print " ... encoding $line \n"; system("$bin_dir/BuildMessage $line \>\> $out_file1"); rename($line, $line."_sent"); } close OUT; } else { print "No encoded CMAN file created.\n"; } # @locallist = (glob("$data_dir/ndbc-input*FM64")); # $out_file2 = "nccoos_${filename_date}_FM64"; # clean up "sent" files if more than 12 @locallist = (glob("$data_dir/ndbc-input*CMAN_sent")); $numfiles = $#locallist; $filelimit = 12; # if 13 or more if ($numfiles>$filelimit) { @localremove = @locallist[0..$numfiles-$filelimit]; print @localremove . "\n"; unlink @localremove; } else { print "No *CMAN_sent files to clean up\n"; } ######################## # (3) push file to NDBC ######################## if (-e $out_file1) { # FTP files if any data encoded print "Start ftp.\n"; $ftp = Net::FTP->new("ndbc1.ndbc.noaa.gov"); $ftp->login("uncdata","beattigers"); $ftp->put("$out_file1"); # $ftp->put("$out_file2"); $ftp->quit; print "Done ftp.\n"; } else { print "No encoded files ftp'd.\n"; } # clean up encoded files if more than 12 @locallist = (glob("$data_dir/nccoos*CMAN")); $numfiles = $#locallist; $filelimit = 12; # if 13 or more if ($numfiles>$filelimit) { @localremove = @locallist[0..$numfiles-$filelimit]; print @localremove . "\n"; unlink @localremove; } else { print "No encoded files to clean up\n"; } # return to original directory on local computer chdir "$cwd"; # stop stop-watch $cummtime = time - $starttime; if ($debug) { print "Total script time = $cummtime (seconds).\n"; }