#!/usr/local/bin/perl # Update cnss catalog for a month # # Assume imported raw catalog has been created by catgen. # # Steps: # # 1. For each network file: # Create catalog entry (and add netid to the entry), # and append to input monthly catalog. # Ensure that date field contains no zeros (done in catgen). # catgen # # 2. Sort monthly input catalog and split into regions. # sortcat | splitcat # # 3. For each regional file: # Sort input catalog and run seldupe on each subgroup causing that # network's solution to be chosen over any other duplicate events. # seldupe -p n # # 4. For each regional file: # Run seldupe on each subgroup causing the higher magnitude # solution to be chosen over any other duplicate events. # seldupe -p l # # 5. Cat all regional files together and sort by time. # cat * | sortcat # # 6. Run seldupe over the final file, causing the higher magnitude # solution to be chosen over any other duplicate events. # seldupe -p l # # Network control file will contain: # Netid polygon_file # Last entry may contain no polygon file, which implies that it # receives any events not contains in any previous polygon file. ######################################################################## # Global initialization. ######################################################################## $cnssdir = $ENV{"CNSSDIR"}; die "Environment variable CNSSDIR not set" if ($cnssdir eq ""); $cnsscatdir = $ENV{"CNSSCATDIR"}; die "Environment variable CNSSCATDIR not set" if ($cnsscatdir eq ""); $cnssworkdir = $ENV{"CNSSWORKDIR"}; die "Environment variable CNSSWORKDIR not set" if ($cnssworkdir eq ""); $cnssphasedir = $ENV{"CNSSPHASEDIR"}; die "Environment variable CNSSPHASEDIR not set" if ($cnssphasedir eq ""); $cnssmaint = "$cnssdir/maintenance"; $cnssregions = "$cnssmaint/regions.cnss"; # Lockfile MUST be the same as the lockfile for the auto-import procedure. # When invoked by the auto-import procedure, ignore the lockfile, # since the parent already has the lockfile locked. $lockdir = "/home/ftp/dc/import.cnss"; $lockfile = "LOCKFILE.CNSS"; $lockpath = "$lockdir/$lockfile"; $LOCK_SH = 1; $LOCK_EX = 2; $LOCK_NB = 4; $LOCK_UN = 8; $DELTA_D = 100; $DELTA_T = 16; $CATGEN = "$cnssmaint/catgen -s"; # Use -N netid to add network code. $SORTCAT = "$cnssmaint/sortcat"; $SPLITCAT = "$cnssmaint/splitcat"; $SELDUPE = "$cnssmaint/seldupe -n -D $DELTA_D -T $DELTA_T"; # Assume netid code added by catgen. $CAT = "/usr/bin/cat"; $ZCAT = "/usr/ucb/zcat"; $COMPRESS = "/usr/ucb/compress"; ######################################################################## # print_syntax - print syntax and exit. ######################################################################## sub print_syntax { local($cmdname) = @_; $cmdname = $1 if ($cmdname =~ m|^.*/([^/]+)$|); printf ( "$cmdname - Generate CNSS catalog for specified month. Syntax: $cmdname [-h] dates where: -h Prints this help message. -r regionsfile Specifies the name of the regions file. Default regions file is $cnssregions. -d n Set debug flag. 1 = print cmds. -L Do no use the lockfile to determine exclusive access. date Date(s) for which the CNSS catalog should be updated. Date is specified as yyyy.mm "); exit(0); } ######################################################################## # Main program ######################################################################## $DEBUG_CMD = 1; { local($date,$year,$month); select(STDERR); $| = 1; select(STDOUT); $| = 1; require "getopts.pl"; &Getopts ('hLr:d:'); &print_syntax($0) if ($opt_h); $debug = ($opt_d == "") ? 0 : $opt_d; $cnssregions = $opt_r if ($opt_r ne ""); # Open a non-blocking lockfile to prevent multiple copies of this program # from simultaneously running on this system. if (! $opt_L) { open (LOCK, "> $lockpath") || die ("unable to open lock file: $lockpath"); die ("unable to lock lockfile: $lockpath") if (flock (LOCK, $LOCK_EX | $LOCK_NB) != 1); } foreach $date (@ARGV) { ($year,$month) = split(/[\.\/]/,$date); $year = ($year >= 100) ? $year : ($year >= 50) ? 1900+$year : 2000+$year; $month = sprintf ("%02d",$month); $status = &update_cnss_cat ($year, $month); print "CNSS catalog created for $year.$month\n" if ($status == 0); print "**** Error $status creating CNSS catalog for $year.$month" if ($status); # sleep (1); } if (! $opt_L) { flock (LOCK, $LOCK_UN); close (LOCK); unlink ($lockpath); } } ######################################################################## # update_cnss_cat: # Update the CNSS catalog for the specified month. ######################################################################## sub update_cnss_cat { local($year,$month) = @_; local($line,$net,@nets,$cmd,$i,$in,$out); local($phasedir) = sprintf ("%s/%04d",$cnssphasedir,$year); local($workdir) = sprintf ("%s",$cnssworkdir); local($catdir) = sprintf ("%s/%04d",$cnsscatdir,$year); local(@workfiles); mkdir($phasedir,0777) || die "Unable to make $phasedir" if (! -d "$phasedir"); mkdir($workdir,0777) || die "Unable to make $workdir" if (! -d "$workdir"); mkdir($catdir,0777) || die "Unable to make $catdir" if (! -d "$catdir"); # Remove all files from the work directory. opendir(DIR,"$workdir") || die "Unable to open $workdir"; unlink(grep(($_="$workdir/$_") && 1,readdir(DIR))); closedir(DIR); # Read in the region names. open(REGIONS, $cnssregions) || die "Unable to open regions file $cnssregions"; while ($line = ) { next if ($line =~ m/^\s*\#/); ($net) = $line =~ /\s*(\S+)/; push(@nets,$net); } # Assume that there is at least 1 file for this month. opendir(DIR,"$phasedir") || die "Unable to opendir $phasedir"; @files = grep(/^\w*\.$year\.$month\.cnss/,readdir(DIR)); closedir(DIR); foreach $file (@files) { ($net,$compressed) = $file =~ /^(\w+)\.\d+\.\d+\.cnss(\.Z)?$/; if ($compressed ne "") { $cmd = "$ZCAT $phasedir/$file | $CATGEN -d $debug -N $net >>$workdir/xx"; } else { $cmd = "$CAT $phasedir/$file | $CATGEN -d $debug -N $net >>$workdir/xx"; } print (STDERR $cmd, "\n") if ($debug & $DEBUG_CMD); system("$cmd") && die "Error $? merging initial data for $year.$month"; } $cmd = "$SORTCAT < $workdir/xx | $SPLITCAT -r $cnssregions"; print (STDERR $cmd, "\n") if ($debug & $DEBUG_CMD); system("$cmd") && die "Error $? splitting data for $year.$month"; # unlink("$workdir/xx"); # For each network region, perform duplicate event removal. for ($i=0; $i<=$#nets; $i++) { $net = $nets[$i]; $in = "$workdir/region.0.$net"; $out = "$workdir/region.1.$net"; $cmd = "$SELDUPE -p n -N $net -d $debug < $in > $out"; print (STDERR $cmd, "\n") if ($debug & $DEBUG_CMD); die "Error $? running $cmd" if (system($cmd)); $in = $out; $out = "$workdir/region.2.$net"; $cmd = "$SELDUPE -p l -d $debug < $in > $out"; print (STDERR $cmd, "\n") if ($debug & $DEBUG_CMD); die "Error $? running $cmd" if (system($cmd)); } # Merge, sort, and rerun seldupe on final catalog. $out = "$cnsscatdir/$year/$year.$month.cnss"; $cmd = "$CAT $workdir/*.2.* | $SORTCAT | $SELDUPE -s -p l -d $debug | $COMPRESS > $out.new.Z"; print (STDERR $cmd, "\n") if ($debug & $DEBUG_CMD); system($cmd); die "Error $? running $cmd" if($?); rename ("$out.new.Z", "$out.Z") || die "Error $! renaming new catalog $out"; return (0); }