#!/usr/bin/perl -w # RWB June 22, 2001 # This is a Perl script read in, sift, and analyze a lot of data from # the USCG VTM system. It will copy data from CD ROM to disk, unzip the # files, extract ferry files (for the Marin, for now), scan for ferry # runs and calculate various quantities. The file sequence (the # example is data from april 25, 2001) is: # h:\2001\04\april2001.zip to hard disk (must be done from Windows) # april2001.zip to 20010425.log (unzip) # 20010425.log to mar10425.log (sortvtm: Marin only) # mar10425.log to mar10425.dat (scanvtm; calcvtm) use strict; my @mname=('january','february','march','april','may','june','july', 'august','september','october','november','december'); my @ndays=(31,28,31,30,31,30,31,31,30,31,30,31); my $iy1=2001; # first year (can be changed interactively) my $im10=1; # first month (ditto) my $iy2=2001; # last year (ditto) my $im20=1; # last month (ditto) my $iday10=1; # first day (ditto) my $iday20=1; # last day (ditto) my $iunzip=0; # flag for unpkzip (ditto) my $isort=0; # flag for sortvtm (ditto) my $iscan=0; # flag for scanvtm (ditto) my $icalc=0; # flag for calcvtm (ditto) my $idellog=0; # flag for dlog (ditto) my $idelzip=0; # flag for dzip (ditto) my ($im,$im1,$im2,$iy,$id); my ($iya,$ima,$imb,$id1,$id2); askval(); # Set parameters interactively. ###### Unzip data and sort out the data for the Marin, by PST days. $im1=$im10; # correct first time around. $im2=$12; # correct until last year. for ($iy=$iy1; $iy <= $iy2; $iy++) { if($iy!=$iy1) {$im1=1;} # Replace special first month. if($iy==$iy2) {$im2=$im20;} # Put in special last month. for($im=$im1; $im <= $im2; $im++) { unpkzip($iy,$im); # Unzip a month's VTM loggings. } $iya=$iy; # One year at a time. $ima=$im1; $imb=$im2; } sortvtm(); # sort out Marin files by PST days. ###### Scan for ferry runs, and carry out calculations; save results ###### in WDSK (IDL) format. $im1=$im10; # correct first time around. $id1=$iday10; # correct first time around. $im2=$12; # correct until last year. for ($iy=$iy1; $iy <= $iy2; $iy++) { if($iy!=$iy1) {$im1=1;} # Replace special first month. if($iy==$iy2) {$im2=$im20;} # Put in special last month. for($im=$im1; $im <= $im2; $im++) { if( $iy!=$iy1||$im!=$im1 ) { $id1=1; } # Replace special first day. $id2=$ndays[$im-1]; if($im==2&&$iy%4==0) { $id2++; } if( $iy==$iy2&&$im==$im20) { $id2=$iday20; } # Put in special last day. for($id=$id1; $id <= $id2; $id++) { scancalc(); # Call IDL to scan and calculate. if( $idellog ne 0) { dlog(); } # Delete log files for all ships. } if( $idelzip ne 0) { dzip(); } # Delete zip file. } } ### END of main script. # Subroutine to prompt for dates and drive letter. sub askval() { print "This PERL script looks for USCG VTM-system data on the CD-ROM\n"; print "drive, and copies it to /d4/Darmat/data/naut/vtm, making\n"; print "subdirectories as needed. Then the file is unziped,\n"; print "sorted, and scanned for ferry runs, and calculations for\n"; print "the ferry runs are carried out.\n"; my $a; print "\$iy1,\$iy2,\$im10,\$im20,\$iday10,\$iday20 = $iy1, $iy2, $im10, $im20, $iday10, $iday20\n"; print "first year: $iy1: "; $a=; chomp($a); if(length($a) gt 0) {$iy1=$a,$iy2=$a}; print "first month: $im10: "; $a=; chomp($a); if(length($a) gt 0) {$im10=$a,$im20=$a}; print "first day: $iday10: "; $a=; chomp($a); if(length($a) gt 0) {$iday10=$a;$iday20=$a;} if($iday10==1) {$iday20=$ndays[$im10-1];} print "last year: $iy2: "; $a=; chomp($a); if(length($a) gt 0) {$iy2=$a;} print "last month: $im20: "; $a=; chomp($a); if(length($a) gt 0) {$im20=$a;} if($iy2!=$iy1||$im20!=$im10) {$iday20=$ndays[$im20-1];} print "last day: $iday20: "; $a=; chomp($a); if(length($a) gt 0) {$iday20=$a}; print "iunzip (0->don't unzip): $iunzip: "; $a=; chomp($a); if(length($a) gt 0) {$iunzip=$a}; print "isort (0->don't sort): $isort: "; $a=; chomp($a); if(length($a) gt 0) {$isort=$a}; print "iscan (0->don't scan): $iscan: "; $a=; chomp($a); if(length($a) gt 0) {$iscan=$a}; print "icalc (0->don't calc): $icalc: "; $a=; chomp($a); if(length($a) gt 0) {$icalc=$a}; print "idellog (0->don't delete): $idellog: "; $a=; chomp($a); if(length($a) gt 0) {$idellog=$a}; print "idelzip (0->don't delete): $idelzip: "; $a=; chomp($a); if(length($a) gt 0) {$idelzip=$a}; print "\$iy1,\$iy2,\$im10,\$im20,\$iday10,\$iday20 = $iy1, $iy2, $im10, $im20, $iday10, $iday20\n"; print "\$iunzip, \$isort, \$iscan, \$icalc, \$idellog, \idelzip = $iunzip, $isort, $iscan, $icalc, $idellog, $idelzip\n"; } ### END of subroutine ask() # Subroutine to unzip a month of logged ship records. sub unpkzip() { my ($infn, $expath); if( !$iunzip ) { return 0; } $infn='/d4/Darmat/data/naut/vtm/'.sprintf("%4d",$iy).'/'. sprintf("%02d",$im).'/'.$mname[$im-1].sprintf("%4d",$iy).'.zip'; $expath=substr($infn,0,32); print "\$infn = $infn\n\$expath = $expath\n"; if( !(-e $infn) ) { return 0; } system("unzip $infn -d $expath"); # Unzip one month of data. return 1; } ### END of sub inpkzip() # Subroutine to spawn the IDL job which sorts out Marin data, # sortvtm.pro. The variables $iya, $ima, and $imb are inherited # from the calling script. Only data from a single year is # processed in one call. sub sortvtm() { if( !$isort ) { return 0; } open( TEMP,'>ptemp1.tmp' ); print TEMP "sortvtm,".sprintf("%d,%d,%d,%d,%d,%d\n",$iy1,$iy2,$im10, $im20,$iday10,$iday20)."exit\n"; close( TEMP ); system("idl ptemp1.tmp"); # Call IDL to sort data. unlink( "ptemp1.tmp" ); return 1; } ### END of sub sortvtm() # Subroutine to spawn the IDL jobs to scan Marin files for ferry runs # and to carry out calculations. Each call to this subroutine handles # one day's data. sub scancalc() { my $stat1=0; my $stat2=0; if( $iscan ) { open( TEMP,'>ptemp1.tmp' ); print TEMP "scanvtm,".sprintf("%d,%d,%d\n",$iya,$ima,$id)."exit\nn"; close( TEMP ); $stat1=system("idl ptemp1.tmp"); # Call IDL to sort data. unlink( "ptemp1.tmp" ); } if( $icalc ) { open( TEMP,'>ptemp1.tmp' ); print TEMP "calcvtm,".sprintf("%d,%d,%d\n",$iya,$ima,$id)."exit\nn"; close( TEMP ); $stat2=system("idl ptemp1.tmp"); # Call IDL to sort data. unlink( "ptemp1.tmp" ); } return $stat1*$stat2; } ### END of sub sortvtm() # Subroutines to delete the large files with information for all boats # in the Bay, after use. The date information $iy, $im and $id are # inherited from the main script, as well as a list of names of months. sub dlog() { my ($filename,$status); $status=0; $filename='/d4/Darmat/data/naut/vtm/'.sprintf("%04d",$iy).'/'. sprintf("%02d",$im).'/'.sprintf("%04d%02d%02d",$iy,$im,$id). '.log'; if(-e $filename) {$status=unlink $filename;} print "Deleting $filename, status = $status\n"; return $status; } # End of sub dlog() sub dzip() { my ($filename,$status); $status=0; $filename='/d4/Darmat/data/naut/vtm/'.sprintf("%04d",$iy).'/'. sprintf("%02d",$im).'/'.$mname[$im-1].sprintf("%04d",$iy). '.zip'; if(-e $filename) {$status=unlink $filename;} print "Deleting $filename, status = $status\n"; return $status; } # End of sub dzip()