Index: trunk/ippScripts/scripts/skycalibration.pl
===================================================================
--- trunk/ippScripts/scripts/skycalibration.pl	(revision 38115)
+++ trunk/ippScripts/scripts/skycalibration.pl	(revision 38565)
@@ -62,4 +62,5 @@
 
 my ($skycal_id, $stack_id, $camera, $dbname, $threads, $outroot, $path_base, $filter, $singlefilter, $reduction, $verbose, $no_update, $no_op, $redirect, $save_temps);
+my $run_state = 'new';
 GetOptions(
     'skycal_id=s'       => \$skycal_id, # sky calibration run id
@@ -69,4 +70,5 @@
     'threads=s'         => \$threads,   # Number of threads to use
     'outroot=s'         => \$outroot,   # Output root name
+    'run-state=s'       => \$run_state, # 'new' or 'update'
     'path_base=s'       => \$path_base, # path_base of input
     'filter=s'          => \$filter,    # filter for input stack
@@ -112,4 +114,9 @@
 # generate the input 
 my $cmdflags = "";
+
+unless ($run_state eq 'new' or $run_state eq 'update') {
+    &my_die("$run_state is not a valid value for run-state\n", $skycal_id, $PS_EXIT_CONFIG_ERROR);
+}
+my $updateMode = $run_state eq 'update';
 
 my $recipe_psastro  = $ipprc->reduction($reduction, 'STATICSKY_CALIBRATION'); # Recipe to use for psastro
@@ -223,4 +230,5 @@
 # Perform psastro analysis
 my $output_sources_filerule =  'PSASTRO.OUTPUT.CMF';
+my $quality = 0;
 {
     my $command = "$psastro $outroot";
@@ -230,4 +238,5 @@
     $command .= " -recipe PSPHOT   $recipe_psphot";
     $command .= " -F PSASTRO.OUTPUT $output_sources_filerule";
+    $command .= " -Db PSASTRO.SAVE.CFF F" if $updateMode;
     $command .= " -stats $stats";
     # $command .= " -recipe PPSTATS CAMSTATS";
@@ -262,5 +271,4 @@
         $cmdflags .= $compatability_flags unless ($cmdflags =~ /-n_detections/);
 
-        my $quality;
         ($quality) =  $cmdflags =~ /-quality (\d+)/;
         if (!$quality) {
@@ -279,5 +287,5 @@
 
 # Add the result to the database
-{
+if (!$updateMode) {
     my $command = "$staticskytool -skycal_id $skycal_id";
     $command .= " -addskycalresult -path_base $outroot";
@@ -292,5 +300,41 @@
         unless ($success) {
             $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-            my $err_message = "Unable to perform staticskytool -addresult";
+            my $err_message = "Unable to perform $command";
+	    warn($err_message);
+	    exit $error_code;
+        }
+    } else {
+        print "skipping $command\n";
+    }
+} else {
+    if ($quality) {
+        # First update the result
+        my $command = "$staticskytool -updateresult $skycal_id -set_fault 0";
+        $command .= " -set_quality $quality";
+        $command .= " -dbname $dbname" if defined $dbname;
+
+        unless($no_update) {
+            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+                run(command => $command, verbose => $verbose);
+            unless ($success) {
+                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+                my $err_message = "Unable to perform $command";
+                warn($err_message);
+                exit $error_code;
+            }
+        } else {
+            print "skipping $command\n";
+        }
+    }
+    my $command = "$staticskytool -skycal_id $skycal_id";
+    $command .= ' -updaterun -set_state full';
+    $command .= " -dbname $dbname" if defined $dbname;
+
+    unless($no_update) {
+        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+            run(command => $command, verbose => $verbose);
+        unless ($success) {
+            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+            my $err_message = "Unable to perform $command";
 	    warn($err_message);
 	    exit $error_code;
@@ -315,8 +359,12 @@
     if (defined $skycal_id and not $no_update) {
         my $command = "$staticskytool -skycal_id $skycal_id -fault $exit_code";
-	$command .= " -addskycalresult";
-	$command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
-	$command .= " -hostname $host" if defined $host;
-	$command .= " -path_base $outroot" if defined $outroot;
+        if ($updateMode) {
+            $command .= ' -updateskycalresult';
+        } else {
+            $command .= " -addskycalresult";
+            $command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
+            $command .= " -hostname $host" if defined $host;
+            $command .= " -path_base $outroot" if defined $outroot;
+        }
 	$command .= " -dbname $dbname" if defined $dbname;
         run(command => $command, verbose => $verbose);
