#!/usr/local/bin/perl # @(#)retrieve 1.13 12/14/05 USGS # # 2009/02/13 PNL: added start/end time printouts use strict; use FindBin; use lib "$FindBin::Bin/../lib"; use ShakeConfig; use lib "$shake_perl_lib"; use Shake::Opt; use Shake::Die; use Shake::Version; use Shake::LoadConfig; use File::Basename qw(basename); ####################################################################### # Global variables ####################################################################### my @progs = (); # List of programs to run my $path = "$shake_home/bin"; # Path to executables my $arglist = "@ARGV"; # save the arguments for entry # into the database ####################################################################### # End global variables ####################################################################### ####################################################################### # Stuff to handle command line options and program documentation: ####################################################################### my $desc = 'Program to extract event and ground-motion data from data ' . 'sources and generate XML files for input to other ShakeMap ' . 'programs. This program is a wrapper to execute the programs ' . "as described in retrieve.conf."; my $flgs = [{ FLAG => 'event', ARG => 'event_id', TYPE => 's', REQ => 'y', DESC => 'Specifies the id of the event to process'}, { FLAG => 'scenario', DESC => 'Tells the system that this is a scenario, which ' . 'means that this program will do nothing but ' . 'insert a row in the flags database. This flag ' . 'is usually not necessary because the program ' . 'will detect the "_se" part of the event name.'}, { FLAG => 'forcerun', DESC => 'Override out-of-sequence and lock errors generated by ' . 'Version.pm.'}, { FLAG => 'verbose', DESC => 'Print more informational messages to stderr.'}, { FLAG => 'help', DESC => 'Print program documentation and quit.'} ]; my $options = Opt::setOptions($flgs) or Die "Error in setOptions"; if (defined $options->{'help'}) { Opt::printDoc($desc); exit 0; } defined $options->{'event'} or Die "Must specify an event with -event flag"; my $evid = $options->{'event'}; my $verbose = defined $options->{'verbose'} ? 1 : 0; my $forcerun = defined $options->{'forcerun'} ? 1 : 0; my $scenario = defined $options->{'scenario'} ? 1 : 0; $scenario = 1 if ($evid =~ /_(se|SE)$/i); Die "Unknown argument(s): @ARGV" if (@ARGV); ####################################################################### # End of command line option stuff ####################################################################### exit main(); 0; sub main { my $prog = basename($0); #---------------------------------------------------------------------- # Check that we can run this program, and set a lock # $loc_db_conf and $loc_db_dirs are defined in the ShakeConfig module #---------------------------------------------------------------------- my $sv = new Version($evid, $loc_db_conf, $loc_db_dirs) or Die "Could not create Version object"; $sv->runProg($prog, $forcerun) or Die "Problems in Shake::Version->runProg, quitting."; #---------------------------------------------------------------------- # Scenarios don't use this program #---------------------------------------------------------------------- if ($scenario) { $sv->saveFlags($prog, $arglist) or Die "Error: Couldn't save flags"; return 0; } #---------------------------------------------------------------------- # Do the configuration #---------------------------------------------------------------------- my $cfile = "retrieve.conf"; unshift @$config_dirs, "$shake_home/data/$evid/config"; my $cfg = new LoadConfig($cfile, $config_dirs, \&Print) or Die "Couldn't open config file $cfile"; $cfg->parse( { 'program' => \&program } ) == 0 or Die "Error in config file, quitting..."; #---------------------------------------------------------------------- # Run each program in turn, check for errors #---------------------------------------------------------------------- my $dt = mydate(); Print "----- retrieve started at $dt -----"; foreach my $prog ( @progs ) { my $call = "$path/$prog -event $evid"; Print "Running '$call'" if ($verbose); system($call) == 0 or Die "Error in $prog"; } $sv->saveFlags($prog, $arglist) or Die "Error: Couldn't save flags"; $dt = mydate(); Print "----- retrieve finished at $dt -----"; return 0; } ####################################################################### # Configuration subroutines ####################################################################### sub program { my $prog = shift; #---------------------------------------------------------------------- # Make sure the programs are available #---------------------------------------------------------------------- -e "$path/$prog" or return "Can't find $prog in $path"; push @progs, $prog; return undef; } ####################################################################### # End configuration subroutines ####################################################################### sub mydate { my ($sec, $min, $hr, $day, $mon, $yr) = localtime(); return sprintf('%02d/%02d/%4d %02d:%02d:%02d', $mon + 1, $day, $yr + 1900, $hr, $min, $sec); }