Index: /tags/ipp-pv3-20140717/ippScripts/scripts/sc_prepare_camera.pl
===================================================================
--- /tags/ipp-pv3-20140717/ippScripts/scripts/sc_prepare_camera.pl	(revision 37290)
+++ /tags/ipp-pv3-20140717/ippScripts/scripts/sc_prepare_camera.pl	(revision 37291)
@@ -1,3 +1,5 @@
 #! /usr/bin/env perl
+
+# generate the input & output files lists and commands for a single camRun
 
 use Carp;
@@ -16,21 +18,13 @@
 use Pod::Usage qw( pod2usage );
 
-
 # Hard coded values
-# my $remote_root   = '/scratch3/watersc1/';  # Far side destination base location
-my $remote_root   = '/lustre/scratch1/turquoise/watersc1/ps1/'; # Far side destination base location
-$remote_root = '/scratch3/watersc1/';
-my $threads       = 2;                      # How many threads are we going to use?
-my $job_cost      = 1700 / 60 / 60;         # Estimate of how long a job runs, in hours.
-my $proc_per_node = 24;                     # processors per node
-my $min_nodes     = 1;                      # smallest allocation to ask for
-my $max_nodes     = 1000;                   # largest allocation to ask for
-my $min_time      = 1;                      # shortest allocation to ask for
-my $max_time      = 8;                      # longest allocation to ask for
-my $remote_hostname= "LANL/Mustang";         # Name of the remote node.
-my @return_component_list = ("DBINFO.EXP", "PSASTRO.CONFIG", "PSASTRO.OUTPUT", "LOG.EXP", "PSASTRO.STATS");
+# my $remote_root   = '/lustre/scratch1/turquoise/watersc1/ps1/'; # Far side destination base location
+my $remote_root   = '/scratch3/watersc1/';  # Far side destination base location
+my $threads_req   = 0;                      # How many threads are we going to use?
+
+my $fail_state = "prep_fail";
+
 # Look for programs we need
 my $missing_tools;
-
 my $remotetool = can_run('remotetool') or (warn "Can't find remotetool" and $missing_tools = 1);
 my $camtool    = can_run('camtool') or (warn "Can't find camtool" and $missing_tools = 1);
@@ -41,7 +35,8 @@
 }
 
-my ($remote_id,$cam_id,,$camera,$dbname,$verbose,$path_base,$no_update);
+my ($remote_id,$cam_id,$camera,$dbname,$verbose,$path_base,$no_update);
 GetOptions(
     'remote_id=s'    => \$remote_id,
+    'cam_d=s'        => \$cam_id,
     'camera|c=s'     => \$camera,
     'dbname|d=s'     => \$dbname,
@@ -52,35 +47,18 @@
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
-pod2usage( -msg => "Required options: --remote_id --camera --dbname --path_base", -exitval => 3) unless
+pod2usage( -msg => "Required options: --remote_id --cam_id --camera --dbname --path_base", -exitval => 3) unless
     defined($remote_id) and
+    defined($cam_id) and
     defined($camera) and
     defined($path_base) and
     defined($dbname);
-my $ipprc = PS::IPP::Config->new( $camera ) or my_die( "Unable to set up", $remote_id);
+
+my $ipprc = PS::IPP::Config->new( $camera ) or &my_die( "Unable to set up", $remote_id, $cam_id, $PS_EXIT_CONFIG_ERROR, $fail_state);
 
 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
 
-#
-# Step 1: Get a list of the components that make up this remoteRun
-
-# SHould this call listrun to ensure we're in state new?
-my $rt_cmd = "$remotetool -listcomponent -remote_id $remote_id";
-$rt_cmd   .= " -dbname $dbname " if defined($dbname);
-
-my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-    run(command => $rt_cmd, verbose => $verbose);
-unless ($success) {
-    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-    &my_die("Unable to run remotetool to determine stage parameters.",
-            $remote_id,$error_code);
-}
-my $compData = $mdcParser->parse(join "", @$stdout_buf) or
-    &my_die("Unable to determine component information.",
-            $remote_id,$PS_EXIT_PROG_ERROR);
-my $compData2= parse_md_list($compData);
-
-#
-# Step 1b: Open output files
-my $uri_command = $path_base . ".cmd";
+my @return_component_list = ("DBINFO.EXP", "PSASTRO.CONFIG", "PSASTRO.OUTPUT", "LOG.EXP", "PSASTRO.STATS");
+
+# STEP 0: Open output files
 my $uri_transfer= $path_base . ".transfer";
 my $uri_check   = $path_base . ".check";
@@ -89,5 +67,4 @@
 my $uri_return  = $path_base . ".return";
 
-my $disk_command = $ipprc->file_resolve($uri_command,1);
 my $disk_transfer= $ipprc->file_resolve($uri_transfer,1);
 my $disk_check   = $ipprc->file_resolve($uri_check,1);
@@ -98,23 +75,16 @@
 my (undef, $remote_config) = uri_convert($uri_config); # Needs to be done after we've created it.
 
-open(TRANSFER, ">$disk_transfer")  || &my_die("Couldn't open file? $disk_transfer",$remote_id,$PS_EXIT_SYS_ERROR);
-open(CHECK,    ">$disk_check")  || &my_die("Couldn't open file? $disk_check",$remote_id,$PS_EXIT_SYS_ERROR);
-open(CONFIG,   ">$disk_config")  || &my_die("Couldn't open file? $disk_config",$remote_id,$PS_EXIT_SYS_ERROR);
-open(GENERATE, ">$disk_generate") || &my_die("Couldn't open file? $disk_generate",$remote_id,$PS_EXIT_SYS_ERROR);
-open(RETURN,   ">$disk_return") || &my_die("Couldn't open file? $disk_return", $remote_id, $PS_EXIT_SYS_ERROR);
+open(TRANSFER, ">$disk_transfer")  || &my_die("Couldn't open file? $disk_transfer",$remote_id, $cam_id, $PS_EXIT_SYS_ERROR, $fail_state);
+open(CHECK,    ">$disk_check")     || &my_die("Couldn't open file? $disk_check",   $remote_id, $cam_id, $PS_EXIT_SYS_ERROR, $fail_state);
+open(CONFIG,   ">$disk_config")    || &my_die("Couldn't open file? $disk_config",  $remote_id, $cam_id, $PS_EXIT_SYS_ERROR, $fail_state);
+open(GENERATE, ">$disk_generate")  || &my_die("Couldn't open file? $disk_generate",$remote_id, $cam_id, $PS_EXIT_SYS_ERROR, $fail_state);
+open(RETURN,   ">$disk_return")    || &my_die("Couldn't open file? $disk_return",  $remote_id, $cam_id, $PS_EXIT_SYS_ERROR, $fail_state);
 my %file_filter = ();
 
-#
-# Step 2: Iterate over all componenets in this remote run.
 my $job_index = 0;
-my @pre_commands = ();
-my @main_commands = ();
-my @post_commands = ();
-
-foreach my $compEntry (@$compData2) {
-    my $cam_id = $compEntry->{stage_id};
-
-# Get exposure level information from the camRun we're working from.
-    
+
+# STEP 1: Get exposure level information for this camRun
+my ($workdir,$exp_tag);
+{    
     my $command = "$camtool -pendingexp -cam_id $cam_id ";
     $command   .= " -dbname $dbname " if defined($dbname);
@@ -125,49 +95,44 @@
         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
         
-        &my_die("Unable to run camtool to determine stage parameters.",
-                $cam_id,$error_code);
-    }
-
-    my $camData = $mdcParser->parse(join "", @$stdout_buf) or
-	&my_die("Unable to determine cam component information.",
-		$cam_id,$PS_EXIT_PROG_ERROR);
-    my $camData2= parse_md_list($camData);
-    my $camEntry = ${ $camData2 }[0];
-    
-    my $workdir = $camEntry->{workdir};
-    my $exp_tag = $camEntry->{exp_tag};
+        &my_die("Unable to run camtool to determine stage parameters.", $remote_id, $cam_id, $error_code, $fail_state);
+    }
+    
+    my $MDlist = $mdcParser->parse(join "", @$stdout_buf) or
+	&my_die("Unable to determine cam component information.", $remote_id, $cam_id, $PS_EXIT_PROG_ERROR, $fail_state);
+    my $metadata = parse_md_list($MDlist);
+    my $camEntry = $metadata->[0];
+    
+    $workdir = $camEntry->{workdir};
+    $exp_tag = $camEntry->{exp_tag};
     unless (defined($workdir)) {
 	while( my ($k, $v) = each %$camEntry ) {
 	    print "key: $k, value: $v.\n";
 	}
-
 	print "%{ $camEntry }\n";
 	die;
     }
-    my $ipp_outroot = "${workdir}/${exp_tag}/${exp_tag}.cm.${cam_id}";
-    my $remote_outroot = uri_local_to_remote($ipp_outroot);
-
-
-#
+}
+
+my $ipp_outroot = "${workdir}/${exp_tag}/${exp_tag}.cm.${cam_id}";
+my $remote_outroot = uri_local_to_remote($ipp_outroot);
+
 # Step 3: Iterate over the sub-components
-
-    my $reg_command = "$camtool -pendingimfile -cam_id $cam_id ";
-    $reg_command   .= " -dbname $dbname " if defined($dbname);
+{
+    my $command = "$camtool -pendingimfile -cam_id $cam_id ";
+    $command   .= " -dbname $dbname " if defined($dbname);
 
     ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $reg_command, verbose => 0);
+	run(command => $command, verbose => 0);
     unless ($success) {
 	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-        &my_die("Unable to run camtool -pendingimfile ",
-                $cam_id,$error_code);
-    }
-    
+        &my_die("Unable to run camtool -pendingimfile ", $remote_id, $cam_id, $error_code, $fail_state);
+    }
 
     # We don't actually care about the input cam data other than to know which mask files to instantiate.
-    my $inpData = $mdcParser->parse(join "", @$stdout_buf) or
-	&my_die("Unable to determine cam component information.",
-		$cam_id, $PS_EXIT_PROG_ERROR);
-    my $inpData2=parse_md_list($inpData);
-    my $chipProto= ${ $inpData2 }[0];
+    my $MDlist = $mdcParser->parse(join "", @$stdout_buf) or
+	&my_die("Unable to determine cam component information.", $remote_id, $cam_id, $PS_EXIT_PROG_ERROR, $fail_state);
+
+    my $metadata = parse_md_list($MDlist);
+    my $chipProto = $metadata->[0];
     my $chip_path = $chipProto->{path_base};
     my $remote_chip_path = uri_local_to_remote( $chip_path );
@@ -180,9 +145,10 @@
     my $psastro_command = " psastro -list ${remote_outroot}.cmflist ";
     $psastro_command   .= " -masklist ${remote_outroot}.masklist ${remote_outroot} ";
-#    $psastro_command   .= " -refmasklist ${remote_outroot}.masklist ${remote_outroot} "; # This is used to do edge calculations.  It should probably be smart enough to know that it can just use the masks I just specified, but it's not.
+#   $psastro_command   .= " -refmasklist ${remote_outroot}.masklist ${remote_outroot} "; 
+# This is used to do edge calculations.  It should probably be smart enough to know that it can just use the masks I just specified, but it's not.
     # Uncomment when remote psastro supports this
     $psastro_command   .= " -kh-correct /turquoise/usr/projects/ps1/watersc1/references/khcorrect.20140606.v0.fits ";
     $psastro_command   .= " -refmasklist /turquoise/usr/projects/ps1/watersc1/references/gpc1.refmask.list ";
-#    $psastro_command   .= " -astrommodel /turquoise/usr/projects/ps1/watersc1/references/gpc1.20080909.asm ";
+#   $psastro_command   .= " -astrommodel /turquoise/usr/projects/ps1/watersc1/references/gpc1.20080909.asm ";
     $psastro_command   .= " -astrommodel /turquoise/usr/projects/ps1/watersc1/references/gpc1.20140505.asm ";
     $psastro_command   .= " -recipe PSASTRO $recipe_psastro ";
@@ -192,5 +158,6 @@
     
     my $camtool_post_cmd = "camtool -cam_id $cam_id -addprocessedexp -uri UNKNOWN ";
-    $camtool_post_cmd  .=  " -dbname $dbname " if defined $dbname;                                                                                                            $camtool_post_cmd  .=  " -path_base $ipp_outroot -hostname $remote_hostname -dtime_script 0 ";
+    $camtool_post_cmd  .=  " -dbname $dbname " if defined $dbname; 
+    $camtool_post_cmd  .=  " -path_base $ipp_outroot -hostname $remote_hostname -dtime_script 0 ";
     my $post_cmd_echo = " echo -n \"$camtool_post_cmd\" > ${remote_outroot}.dbinfo ";
     my $post_cmd_SfM  = " ppStatsFromMetadata ${remote_outroot}.stats - CAMERA_EXP_FPA >> ${remote_outroot}.dbinfo ";
@@ -206,5 +173,5 @@
 	print CONFIG " && mkdir -p ${remote_root}/tmp/${remote_outroot_dir} && ln -sf $remote_disk ${remote_root}/tmp/${ipp_disk} && touch $remote_disk ";
     }
-    foreach my $chipInfo (@{ $inpData2 }) {
+    foreach my $chipInfo (@{ $metadata }) {
 	my $filename = $ipprc->filename("PSASTRO.OUTPUT.MASK",$ipp_outroot,$chipInfo->{class_id});
 	my ($ipp_disk, $remote_disk) = uri_to_outputs_for_return( $filename);
@@ -220,48 +187,14 @@
 close(GENERATE);
 
-#
-# Construct the moab command last, so we can use the job_index counter to estimate resources.  Somehow.
-my $proc_need = $job_index * $threads;       # how many total processors do we need?
-my $node_need = $proc_need / $proc_per_node; # this equals how many nodes?
-my $time_need = $job_index * $job_cost;      # How many seconds will this take?
-
-my $fill_factor = 0.8;  # This is the factor of how much of the time allocation we'd like to fill
-my ($time_req,$node_req);
-if ($node_need * $job_cost < $fill_factor * $min_nodes * $min_time) {
-    $time_req = $min_time;
-    $node_req = $min_nodes;
-}
-elsif ($node_need * $job_cost > $fill_factor * $max_nodes * $max_time) {
-    $time_req = $max_time;
-    $node_req = $max_nodes;
-    print STDERR "You've requested the construction of a bundle that appears to need $node_need nodes and $job_cost time per job.  This exceeds the max limits ($max_nodes, $max_time).  Using those max values instead.  Good luck.\n";
-}
-else {
-    $time_req = int(($node_need * $job_cost) / ($fill_factor * $max_nodes)) + 1;
-    $node_req = int(($node_need * $job_cost) / ($fill_factor * $time_req)) + 1;
-}
-$time_req += 1; # Safety addition.
-
-open(COMMAND,  ">$disk_command") || &my_die("Couldn't open file? $disk_command",$remote_id,$PS_EXIT_SYS_ERROR);
-print COMMAND "#!/bin/tcsh\n";
-print COMMAND "##### Moab controll lines\n";
-print COMMAND "#MSUB -l nodes=${node_req}:ppn=${proc_per_node},walltime=${time_req}:00:00\n"; ## CHECK RESOURCES
-print COMMAND "#MSUB -j oe\n";
-print COMMAND "#MSUB -V\n";
-print COMMAND "#MSUB -o ${remote_root}cam.${remote_id}.out\n";
-print COMMAND "date\n";
-print COMMAND 'srun -n $SLURM_JOB_NUM_NODES -m cyclic -l /bin/hostname | sort -n | awk \'{printf "%s\n", $2}\' > hosts.${SLURM_JOB_ID}' . "\n";
-print COMMAND "${remote_root}/stask_chip.py $remote_config " . 'hosts.${SLURM_JOB_ID} 1' . "\n";
-print COMMAND "date\n";
-close(COMMAND);
-
 unless($no_update) {
-    my $command = "remotetool -updaterun -remote_id $remote_id ";
-    $command .= " -set_state pending ";
-    $command .= " -dbname $dbname " if defined $dbname;
-
+    my $command = "remotetool -updatecomponent -remote_id $remote_id -stage_id $cam_id ";
+    $command   .= " -set_jobs $job_index";
+    $command   .= " -set_path_base $path_base";
+    $command   .= " -set_state prep_done";
+    $command   .= " -dbname $dbname " if defined $dbname;
     system($command);
 }
 
+exit (0);
 
 ## Common SC routines
@@ -273,5 +206,5 @@
     
     unless(defined($ipp_disk)) {
-        my_die();
+        &my_die( "Unable to generate file for $neb_uri ", $remote_id, $cam_id, $PS_EXIT_SYS_ERROR, $fail_state);
     }
 
@@ -289,5 +222,5 @@
     
     unless(defined($ipp_disk)) {
-        my_die();
+        &my_die( "Unable to generate file for $neb_uri ", $remote_id, $cam_id, $PS_EXIT_SYS_ERROR, $fail_state);
     }
 
@@ -345,5 +278,6 @@
 sub my_die { # exit with status; my_die(message,stage_id,exit_code,exit_status);
     my $msg = shift;
-    my $id  = shift;
+    my $remote_id  = shift;
+    my $stage_id  = shift;
     my $exit_code = shift;
     my $exit_state = shift;
@@ -353,7 +287,6 @@
     carp($msg);
     
-    if (defined $id and not $no_update) {
-        my $command = "remotetool -updaterun -remote_id $id";
-        $command .= " -fault $exit_code " if defined $exit_code;
+    if (defined $remote_id and defined $stage_id and not $no_update) {
+        my $command = "remotetool -updatecomponent -remote_id $id -stage_id $stage_id";
         $command .= " -set_state $exit_state " if defined $exit_state;
         $command .= " -dbname $dbname " if defined $dbname;
Index: /tags/ipp-pv3-20140717/ippScripts/scripts/sc_prepare_chip.pl
===================================================================
--- /tags/ipp-pv3-20140717/ippScripts/scripts/sc_prepare_chip.pl	(revision 37290)
+++ /tags/ipp-pv3-20140717/ippScripts/scripts/sc_prepare_chip.pl	(revision 37291)
@@ -58,5 +58,5 @@
     defined($dbname);
 
-my $ipprc = PS::IPP::Config->new( $camera ) or my_die( "Unable to set up", $remote_id, $chip_id, $PS_EXIT_CONFIG_ERROR, $fail_state);
+my $ipprc = PS::IPP::Config->new( $camera ) or &my_die( "Unable to set up", $remote_id, $chip_id, $PS_EXIT_CONFIG_ERROR, $fail_state);
 
 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
@@ -304,5 +304,4 @@
     system($command);
 }
-
 exit (0);
 
@@ -313,5 +312,5 @@
 
     unless(defined($ipp_disk)) {
-        my_die( "Unable to generate file for $neb_uri ", $remote_id, $chip_id, $PS_EXIT_SYS_ERROR, $fail_state);
+        &my_die( "Unable to generate file for $neb_uri ", $remote_id, $chip_id, $PS_EXIT_SYS_ERROR, $fail_state);
     }
 
@@ -329,5 +328,5 @@
 
     unless(defined($ipp_disk)) {
-        my_die( "Unable to generate file for $neb_uri ", $remote_id, $chip_id, $PS_EXIT_SYS_ERROR, $fail_state);
+        &my_die( "Unable to generate file for $neb_uri ", $remote_id, $chip_id, $PS_EXIT_SYS_ERROR, $fail_state);
     }
 
Index: /tags/ipp-pv3-20140717/ippScripts/scripts/sc_prepare_run.pl
===================================================================
--- /tags/ipp-pv3-20140717/ippScripts/scripts/sc_prepare_run.pl	(revision 37290)
+++ /tags/ipp-pv3-20140717/ippScripts/scripts/sc_prepare_run.pl	(revision 37291)
@@ -26,7 +26,14 @@
 my $remote_raw    = "${remote_root}/raw/";  # Directory to find raw data in.
 
-# value for chip:
 my $threads       = 2;                      # How many threads are we going to use?
-my $job_cost      = 150 / 60 / 60;          # Estimate of how long a job runs, in hours.
+
+my $fail_state = "prep_fail";
+
+# Estimate of how long a job runs, in hours (depends on stage)
+my %job_cost = ();
+$job_cost{"camera"} = 1700 / 60 / 60; 
+$job_cost{"chip"}   = 150 / 60 / 60;
+$job_cost{"warp"}   = 110 / 60 / 60;
+$job_cost{"stack"}  = 1500 / 60 / 60;
 
 my $proc_per_node = 24;                     # processors per node
@@ -35,4 +42,5 @@
 my $min_time      = 1;                      # shortest allocation to ask for
 my $max_time      = 8;                      # longest allocation to ask for
+my $remote_hostname= "LANL/Mustang";         # Name of the remote node.
 
 # We need to ensure we only ever try to transfer a file once.
@@ -41,6 +49,4 @@
 # Look for programs we need
 my $missing_tools;
-my $chiptool = can_run('chiptool') or (warn "Can't find chiptool" and $missing_tools = 1);
-my $detselect= can_run('detselect') or (warn "Can't find detselect" and $missing_tools = 1);
 my $remotetool = can_run('remotetool') or (warn "Can't find remotetool" and $missing_tools = 1);
 
@@ -51,21 +57,24 @@
 
 # Options
-my ($exp_id,$exp_name,$remote_id,$chip_id,$detrends,$camera,$dbname,$verbose,$path_base,$no_update);
+my ($remote_id,$stage,$camera,$path_base,$dbname,$verbose,$no_update);
 GetOptions(
     'remote_id=s'    => \$remote_id,
+    'stage=s'        => \$stage,
     'camera|c=s'     => \$camera,
+    'path_base=s'    => \$path_base,
     'dbname|d=s'     => \$dbname,
-    'path_base=s'    => \$path_base,
+    'verbose'        => \$verbose,
     'no_update'      => \$no_update,
-    'verbose'        => \$verbose,
     ) or pod2usage( 2 );
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
-pod2usage( -msg => "Required options: --remote_id --camera --dbname --path_base", -exitval => 3) unless
+pod2usage( -msg => "Required options: --remote_id --stage --camera --dbname --path_base", -exitval => 3) unless
     defined($remote_id) and
+    defined($stage) and
     defined($camera) and
     defined($path_base) and
     defined($dbname);
-my $ipprc = PS::IPP::Config->new( $camera ) or my_die( "Unable to set up", $exp_id);
+
+my $ipprc = PS::IPP::Config->new( $camera ) or &my_die( "Unable to set up", $remote_id, $PS_EXIT_CONFIG_ERROR, $fail_state);
 
 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
@@ -82,8 +91,8 @@
         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
 
-        &my_die("Unable to run chiptool to determine stage parameters.", $remote_id, $error_code);
+        &my_die("Unable to run remotetool to determine stage parameters.", $remote_id, $error_code, $fail_state);
     }
     my $MDlist = $mdcParser->parse(join "", @$stdout_buf) or
-        &my_die("Unable to determine component information.", $remote_id,$PS_EXIT_PROG_ERROR);
+        &my_die("Unable to determine component information.", $remote_id, $PS_EXIT_PROG_ERROR, $fail_state);
     $compData = parse_md_list($MDlist);
 }
@@ -112,9 +121,9 @@
 my (undef, $remote_config) = uri_convert($uri_config); # Needs to be done after we've created it.
 
-open(TRANSFER, ">$disk_transfer")  || &my_die("Couldn't open file? $disk_transfer",$chip_id,$PS_EXIT_SYS_ERROR);
-open(CHECK,    ">$disk_check")     || &my_die("Couldn't open file? $disk_check",   $chip_id,$PS_EXIT_SYS_ERROR);
-open(CONFIG,   ">$disk_config")    || &my_die("Couldn't open file? $disk_config",  $chip_id,$PS_EXIT_SYS_ERROR);
-open(GENERATE, ">$disk_generate")  || &my_die("Couldn't open file? $disk_generate",$chip_id,$PS_EXIT_SYS_ERROR);
-open(RETURN,   ">$disk_return")    || &my_die("Couldn't open file? $disk_return",  $chip_id,$PS_EXIT_SYS_ERROR);
+open(TRANSFER, ">$disk_transfer")  || &my_die("Couldn't open file? $disk_transfer",$remote_id, $PS_EXIT_SYS_ERROR, $fail_state);
+open(CHECK,    ">$disk_check")     || &my_die("Couldn't open file? $disk_check",   $remote_id, $PS_EXIT_SYS_ERROR, $fail_state);
+open(CONFIG,   ">$disk_config")    || &my_die("Couldn't open file? $disk_config",  $remote_id, $PS_EXIT_SYS_ERROR, $fail_state);
+open(GENERATE, ">$disk_generate")  || &my_die("Couldn't open file? $disk_generate",$remote_id, $PS_EXIT_SYS_ERROR, $fail_state);
+open(RETURN,   ">$disk_return")    || &my_die("Couldn't open file? $disk_return",  $remote_id, $PS_EXIT_SYS_ERROR, $fail_state);
 
 # Step 2: Iterate over all componenets in this remote run.
@@ -194,24 +203,30 @@
 my $proc_need = $job_index * $threads;       # how many total processors do we need?
 my $node_need = $proc_need / $proc_per_node; # this equals how many nodes?
-my $time_need = $job_index * $job_cost;      # How many seconds will this take?
+my $time_need = $job_index * $job_cost{$stage};      # How many seconds will this take?
 
 my $fill_factor = 0.8;  # This is the factor of how much of the time allocation we'd like to fill
 my ($time_req,$node_req);
-if ($node_need * $job_cost < $fill_factor * $min_nodes * $min_time) {
+if ($node_need * $job_cost{$stage} < $fill_factor * $min_nodes * $min_time) {
     $time_req = $min_time;
     $node_req = $min_nodes;
 }
-elsif ($node_need * $job_cost > $fill_factor * $max_nodes * $max_time) {
+elsif ($node_need * $job_cost{$stage} > $fill_factor * $max_nodes * $max_time) {
     $time_req = $max_time;
     $node_req = $max_nodes;
-    print STDERR "You've requested the construction of a bundle that appears to need $node_need nodes and $job_cost time per job.  This exceeds the max limits ($max_nodes, $max_time).  Using those max values instead.  Good luck.\n";
+    print STDERR "You've requested the construction of a bundle that appears to need $node_need nodes and $job_cost{$stage} time per job.  This exceeds the max limits ($max_nodes, $max_time).  Using those max values instead.  Good luck.\n";
 }
 else {
-    $time_req = int(($node_need * $job_cost) / ($fill_factor * $max_nodes)) + 1;
-    $node_req = int(($node_need * $job_cost) / ($fill_factor * $time_req)) + 1;
-}
-$time_req += 1; # Safety addition.
-
-open(COMMAND,  ">$disk_command") || &my_die("Couldn't open file? $disk_command",$chip_id,$PS_EXIT_SYS_ERROR);
+    $time_req = int(($node_need * $job_cost{$stage}) / ($fill_factor * $max_nodes)) + 1;
+    $node_req = int(($node_need * $job_cost{$stage}) / ($fill_factor * $time_req)) + 1;
+}
+
+if ($stage eq "stack") {
+    $time_req += 2;
+    $node_req *= 3;
+} else {
+    $time_req += 1; # Safety addition.
+}
+
+open(COMMAND,  ">$disk_command") || &my_die("Couldn't open file? $disk_command", $remote_id, $PS_EXIT_SYS_ERROR, $fail_state);
 print COMMAND "#!/bin/tcsh\n";
 print COMMAND "##### Moab controll lines\n";
@@ -219,5 +234,5 @@
 print COMMAND "#MSUB -j oe\n";
 print COMMAND "#MSUB -V\n";
-print COMMAND "#MSUB -o ${remote_root}chip.${remote_id}.out\n";
+print COMMAND "#MSUB -o ${remote_root}/${stage}.${remote_id}.out\n";
 print COMMAND "date\n";
 print COMMAND 'srun -n $SLURM_JOB_NUM_NODES -m cyclic -l /bin/hostname | sort -n | awk \'{printf "%s\n", $2}\' > hosts.${SLURM_JOB_ID}' . "\n";
@@ -246,5 +261,5 @@
 
     unless(defined($ipp_disk)) {
-        my_die();
+        &my_die( "Unable to generate file for $neb_uri ", $remote_id, $PS_EXIT_SYS_ERROR, $fail_state);
     }
 
@@ -258,5 +273,5 @@
 sub my_die {
     my $msg = shift;
-    my $id  = shift;
+    my $remote_id  = shift;
     my $exit_code = shift;
     my $exit_state = shift;
@@ -266,6 +281,6 @@
     carp($msg);
 
-    if (defined $id and not $no_update) {
-        my $command = "remotetool -updaterun -remote_id $id";
+    if (defined $remote_id and not $no_update) {
+        my $command = "remotetool -updaterun -remote_id $remote_id";
         $command .= " -fault $exit_code " if defined $exit_code;
         $command .= " -set_state $exit_state " if defined $exit_state;
Index: /tags/ipp-pv3-20140717/ippScripts/scripts/sc_prepare_stack.pl
===================================================================
--- /tags/ipp-pv3-20140717/ippScripts/scripts/sc_prepare_stack.pl	(revision 37290)
+++ /tags/ipp-pv3-20140717/ippScripts/scripts/sc_prepare_stack.pl	(revision 37291)
@@ -1,3 +1,5 @@
 #! /usr/bin/env perl
+
+# generate the input & output files lists and commands for a single stackRun
 
 use Carp;
@@ -16,27 +18,15 @@
 use Pod::Usage qw( pod2usage );
 
-
 # Hard coded values
-#my $remote_root   = '/scratch3/watersc1/';  # Far side destination base location
-my $remote_root   = '/lustre/scratch1/turquoise/watersc1/ps1/'; # Far side destination base location
-$remote_root = '/scratch3/watersc1/';
-my $threads       = 2;                      # How many threads are we going to use?
-my $job_cost      = 1500 / 60 / 60;           # Estimate of how long a job runs, in hours.
-my $proc_per_node = 24;                     # processors per node
-my $min_nodes     = 1;                      # smallest allocation to ask for
-my $max_nodes     = 1000;                   # largest allocation to ask for
-my $min_time      = 1;                      # shortest allocation to ask for
-my $max_time      = 8;                      # longest allocation to ask for
-my $remote_hostname   = "LANL/Mustang";         # Name of the remote node.
-my @return_component_list = ("DBINFO.EXP", "PPSTACK.UNCONV","PPSTACK.UNCONV.MASK","PPSTACK.UNCONV.VARIANCE",
-			     "PPSTACK.UNCONV.EXP","PPSTACK.UNCONV.EXPWT","PPSTACK.UNCONV.EXPNUM","PSPHOT.OUTPUT",
-			     "LOG.EXP", "PSASTRO.STATS",
-			     "PPSTACK.CONFIG","PPSTACK.TARGET.PSF","PSPHOT.STACK.PSF.SAVE");
-#,"PPSTACK.CONV.KERNEL");
+# my $remote_root   = '/lustre/scratch1/turquoise/watersc1/ps1/'; # Far side destination base location
+my $remote_root   = '/scratch3/watersc1/';  # Far side destination base location
+my $threads_req   = 4;                      # How many threads are we going to use?
+
+my $fail_state = "prep_fail";
+
 # Look for programs we need
 my $missing_tools;
-
 my $remotetool = can_run('remotetool') or (warn "Can't find remotetool" and $missing_tools = 1);
-my $stacktool    = can_run('stacktool') or (warn "Can't find warptool" and $missing_tools = 1);
+my $stacktool    = can_run('stacktool') or (warn "Can't find stacktool" and $missing_tools = 1);
 
 if ($missing_tools) {
@@ -48,4 +38,5 @@
 GetOptions(
     'remote_id=s'    => \$remote_id,
+    'stack_id=s'     => \$stack_id,
     'camera|c=s'     => \$camera,
     'dbname|d=s'     => \$dbname,
@@ -56,35 +47,22 @@
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
-pod2usage( -msg => "Required options: --remote_id --camera --dbname --path_base", -exitval => 3) unless
+pod2usage( -msg => "Required options: --remote_id --stack_id --camera --dbname --path_base", -exitval => 3) unless
     defined($remote_id) and
+    defined($stack_id) and
     defined($camera) and
     defined($path_base) and
     defined($dbname);
-my $ipprc = PS::IPP::Config->new( $camera ) or my_die( "Unable to set up", $remote_id);
+
+my $ipprc = PS::IPP::Config->new( $camera ) or &my_die( "Unable to set up", $remote_id, $stack_id, $PS_EXIT_CONFIG_ERROR, $fail_state);
 
 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
 
-#
-# Step 1: Get a list of the components that make up this remoteRun
-
-# SHould this call listrun to ensure we're in state new?
-my $rt_cmd = "$remotetool -listcomponent -remote_id $remote_id";
-$rt_cmd   .= " -dbname $dbname " if defined($dbname);
-
-my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-    run(command => $rt_cmd, verbose => $verbose);
-unless ($success) {
-    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-    &my_die("Unable to run remotetool to determine stage parameters.",
-            $remote_id,$error_code);
-}
-my $compData = $mdcParser->parse(join "", @$stdout_buf) or
-    &my_die("Unable to determine component information.",
-            $remote_id,$PS_EXIT_PROG_ERROR);
-my $compData2= parse_md_list($compData);
-
-#
-# Step 1b: Open output files
-my $uri_command = $path_base . ".cmd";
+my @return_component_list = ("DBINFO.EXP", "PPSTACK.UNCONV","PPSTACK.UNCONV.MASK","PPSTACK.UNCONV.VARIANCE",
+			     "PPSTACK.UNCONV.EXP","PPSTACK.UNCONV.EXPWT","PPSTACK.UNCONV.EXPNUM","PSPHOT.OUTPUT",
+			     "LOG.EXP", "PSASTRO.STATS",
+			     "PPSTACK.CONFIG","PPSTACK.TARGET.PSF","PSPHOT.STACK.PSF.SAVE");
+#,"PPSTACK.CONV.KERNEL");
+
+# STEP 0: Open output files
 my $uri_transfer= $path_base . ".transfer";
 my $uri_check   = $path_base . ".check";
@@ -93,5 +71,4 @@
 my $uri_return  = $path_base . ".return";
 
-my $disk_command = $ipprc->file_resolve($uri_command,1);
 my $disk_transfer= $ipprc->file_resolve($uri_transfer,1);
 my $disk_check   = $ipprc->file_resolve($uri_check,1);
@@ -102,49 +79,43 @@
 my (undef, $remote_config) = uri_convert($uri_config); # Needs to be done after we've created it.
 
-open(TRANSFER, ">$disk_transfer")  || &my_die("Couldn't open file? $disk_transfer",$remote_id,$PS_EXIT_SYS_ERROR);
-open(CHECK,    ">$disk_check")  || &my_die("Couldn't open file? $disk_check",$remote_id,$PS_EXIT_SYS_ERROR);
-open(CONFIG,   ">$disk_config")  || &my_die("Couldn't open file? $disk_config",$remote_id,$PS_EXIT_SYS_ERROR);
-open(GENERATE, ">$disk_generate") || &my_die("Couldn't open file? $disk_generate",$remote_id,$PS_EXIT_SYS_ERROR);
-open(RETURN,   ">$disk_return") || &my_die("Couldn't open file? $disk_return", $remote_id, $PS_EXIT_SYS_ERROR);
-
-
-#
-# Step 2: Iterate over all componenets in this remote run.
+open(TRANSFER, ">$disk_transfer")  || &my_die("Couldn't open file? $disk_transfer",$remote_id, $stack_id, $PS_EXIT_SYS_ERROR, $fail_state);
+open(CHECK,    ">$disk_check")     || &my_die("Couldn't open file? $disk_check",   $remote_id, $stack_id, $PS_EXIT_SYS_ERROR, $fail_state);
+open(CONFIG,   ">$disk_config")    || &my_die("Couldn't open file? $disk_config",  $remote_id, $stack_id, $PS_EXIT_SYS_ERROR, $fail_state);
+open(GENERATE, ">$disk_generate")  || &my_die("Couldn't open file? $disk_generate",$remote_id, $stack_id, $PS_EXIT_SYS_ERROR, $fail_state);
+open(RETURN,   ">$disk_return")    || &my_die("Couldn't open file? $disk_return",  $remote_id, $stack_id, $PS_EXIT_SYS_ERROR, $fail_state);
+
 my $job_index = 0;
-my @pre_commands = ();
-my @main_commands = ();
-my @post_commands = ();
-my %file_filter = ();
-
-
-foreach my $compEntry (@$compData2) {
-    my $stack_id = $compEntry->{stage_id};
-
-# Get exposure level information from the stackRun we're working from.
-    my $run_command = "$stacktool -tosum -stack_id $stack_id";
-    $run_command .= " -dbname $dbname " if defined($dbname);
+
+# STEP 1: Get exposure level information from the stackRun we're working from.
+my ($reduction, $workdir, $tess_id, $skycell_id);
+{
+    my $command = "$stacktool -tosum -stack_id $stack_id";
+    $command .= " -dbname $dbname " if defined($dbname);
     ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $run_command, verbose => 0);
+	run(command => $command, verbose => 0);
     unless ($success) {
 	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to run stacktool -tosum ",
-		$stack_id,$error_code);
-    }
-
-    my $stackData = $mdcParser->parse(join "", @$stdout_buf) or
-	&my_die("Unable to determine stack component information.",
-		$stack_id,$PS_EXIT_PROG_ERROR);
-    my $stackData2= parse_md_list($stackData);
-
-    my $stack = ${ $stackData2 }[0];
-    my $reduction = $stack->{reduction};
+	&my_die("Unable to run stacktool -tosum ", $remote_id, $stack_id, $error_code, $fail_state);
+    }
+
+    my $MDlist = $mdcParser->parse(join "", @$stdout_buf) or
+	&my_die("Unable to determine stack component information.", $remote_id, $stack_id, $PS_EXIT_PROG_ERROR, $fail_state);
+    my $metadata = parse_md_list($MDlist);
+
+    my $stack = $metadata->[0];
+
+    $reduction = $stack->{reduction};
     $reduction = 'DEFAULT' unless defined($reduction);
-    my $workdir   = $stack->{workdir};
-    my $tess_id   = $stack->{tess_id};
-    my $skycell_id= $stack->{skycell_id};
-
-    my $ipp_outroot    = "${workdir}/${tess_id}/${skycell_id}/${tess_id}.${skycell_id}.stk.${stack_id}";    
-    my $remote_outroot = uri_local_to_remote($ipp_outroot);    
-    my $remote_outdir  = uri_local_to_remote("${workdir}/${tess_id}/${skycell_id}");
+    $workdir   = $stack->{workdir};
+    $tess_id   = $stack->{tess_id};
+    $skycell_id= $stack->{skycell_id};
+}
+my $ipp_outroot    = "${workdir}/${tess_id}/${skycell_id}/${tess_id}.${skycell_id}.stk.${stack_id}";    
+my $remote_outroot = uri_local_to_remote($ipp_outroot);    
+my $remote_outdir  = uri_local_to_remote("${workdir}/${tess_id}/${skycell_id}");
+
+# STEP 2: Get the list of input warps
+my ($warpData);
+{
     # This actually returns all the individual stack/skyfiles that comprise this run.  Because consistency.
     my $command = "$stacktool -inputskyfile -stack_id $stack_id ";
@@ -154,17 +125,16 @@
     unless ($success) {
 	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to run stacktool -inputskyfile ",
-		$stack_id,$error_code);
-    }
-
-    my $warpData = $mdcParser->parse(join "", @$stdout_buf) or
-	&my_die("Unable to determine stack component information.",
-		$stack_id,$PS_EXIT_PROG_ERROR);
-    my $warpData2= parse_md_list($warpData);
-
-#
-# Step 3: Iterate over the sub-components
+	&my_die("Unable to run stacktool -inputskyfile ", $remote_id, $stack_id, $error_code, $fail_state);
+    }
+
+    my $MDlist = $mdcParser->parse(join "", @$stdout_buf) or
+	&my_die("Unable to determine stack component information.", $remote_id, $stack_id, $PS_EXIT_PROG_ERROR, $fail_state);
+    my $warpData = parse_md_list($MDlist);
+}
+
+# STEP 3: Iterate over the sub-components
+{
     my $warp_path_base_string = "";
-    foreach my $warpEntry ( @{ $warpData2 } ) {
+    foreach my $warpEntry ( @{ $warpData } ) {
 	my $warp_path_base = $warpEntry->{path_base};
 	my $remote_path_base = uri_local_to_remote($warp_path_base);
@@ -191,5 +161,5 @@
     $ppstack_command    .= " -R PPSTACK.UNCONV.VARIANCE FITS.TYPE COMP_STACK";
     $ppstack_command    .= " -tracedest ${remote_outroot}.trace -log ${remote_outroot}.log ";
-    $ppstack_command    .= " -threads 4 ";
+    $ppstack_command    .= " -threads $threads_req ";
     $ppstack_command    .= " -dumpconfig ${remote_outroot}.mdc ";
     $ppstack_command    .= " -stack_id $stack_id -skycell_id $skycell_id -tess_id $tess_id ";
@@ -212,8 +182,4 @@
 	print CONFIG " && mkdir -p ${remote_root}/tmp/${remote_outroot_dir} && ln -sf $remote_disk ${remote_root}/tmp/${ipp_disk} && touch $remote_disk ";
     }
-
-#     my $cleanup_cmd   = " (rm -f /scratch3/watersc1/sys_temp/${tess_id}.${skycell_id}.stk.${stack_id}.*conv.*.fits || true) ";    
-#     print CONFIG " && ${cleanup_cmd} \n";
-
     print CONFIG "\n";
 }
@@ -224,49 +190,13 @@
 close(GENERATE);
 
-#
-# Construct the moab command last, so we can use the job_index counter to estimate resources.  Somehow.
-my $proc_need = $job_index * $threads;       # how many total processors do we need?
-my $node_need = $proc_need / $proc_per_node; # this equals how many nodes?
-my $time_need = $job_index * $job_cost;      # How many seconds will this take?
-
-my $fill_factor = 0.8;  # This is the factor of how much of the time allocation we'd like to fill
-my ($time_req,$node_req);
-if ($node_need * $job_cost < $fill_factor * $min_nodes * $min_time) {
-    $time_req = $min_time;
-    $node_req = $min_nodes;
-}
-elsif ($node_need * $job_cost > $fill_factor * $max_nodes * $max_time) {
-    $time_req = $max_time;
-    $node_req = $max_nodes;
-    print STDERR "You've requested the construction of a bundle that appears to need $node_need nodes and $job_cost time per job.  This exceeds the max limits ($max_nodes, $max_time).  Using those max values instead.  Good luck.\n";
-}
-else {
-    $time_req = int(($node_need * $job_cost) / ($fill_factor * $max_nodes)) + 1;
-    $node_req = int(($node_need * $job_cost) / ($fill_factor * $time_req)) + 1;
-}
-
-$time_req += 2;
-$node_req *= 3;
-open(COMMAND,  ">$disk_command") || &my_die("Couldn't open file? $disk_command",$remote_id,$PS_EXIT_SYS_ERROR);
-print COMMAND "#!/bin/tcsh\n";
-print COMMAND "##### Moab controll lines\n";
-print COMMAND "#MSUB -l nodes=${node_req}:ppn=${proc_per_node},walltime=${time_req}:00:00\n"; ## CHECK RESOURCES
-print COMMAND "#MSUB -j oe\n";
-print COMMAND "#MSUB -V\n";
-print COMMAND "#MSUB -o ${remote_root}stack.${remote_id}.out\n";
-print COMMAND "date\n";
-print COMMAND 'srun -n $SLURM_JOB_NUM_NODES -m cyclic -l /bin/hostname | sort -n | awk \'{printf "%s\n", $2}\' > hosts.${SLURM_JOB_ID}' . "\n";
-print COMMAND "${remote_root}/stask_chip.py $remote_config " . 'hosts.${SLURM_JOB_ID} 3' . "\n";
-print COMMAND "date\n";
-close(COMMAND);
-
 unless($no_update) {
-    my $command = "remotetool -updaterun -remote_id $remote_id ";
-    $command .= " -set_state pending ";
+    my $command = "remotetool -updatecomponent -remote_id $remote_id -stage_id $stack_id ";
+    $command .= " -set_jobs $job_index";
+    $command .= " -set_path_base $path_base";
+    $command .= " -set_state prep_done";
     $command .= " -dbname $dbname " if defined $dbname;
 
     system($command);
 }
-
 
 ## Common SC routines
@@ -278,5 +208,5 @@
     
     unless(defined($ipp_disk)) {
-        my_die();
+        &my_die( "Unable to generate file for $neb_uri ", $remote_id, $stack_id, $PS_EXIT_SYS_ERROR, $fail_state);
     }
 
@@ -294,5 +224,5 @@
     
     unless(defined($ipp_disk)) {
-        my_die();
+        &my_die( "Unable to generate file for $neb_uri ", $remote_id, $stack_id, $PS_EXIT_SYS_ERROR, $fail_state);
     }
 
@@ -350,5 +280,6 @@
 sub my_die { # exit with status; my_die(message,stage_id,exit_code,exit_status);
     my $msg = shift;
-    my $id  = shift;
+    my $remote_id  = shift;
+    my $stage_id  = shift;
     my $exit_code = shift;
     my $exit_state = shift;
@@ -358,7 +289,6 @@
     carp($msg);
     
-    if (defined $id and not $no_update) {
-        my $command = "remotetool -updaterun -remote_id $id";
-        $command .= " -fault $exit_code " if defined $exit_code;
+    if (defined $remote_id and defined $stage_id and not $no_update) {
+        my $command = "remotetool -updatecomponent -remote_id $remote_id -stage_id $stage_id";
         $command .= " -set_state $exit_state " if defined $exit_state;
         $command .= " -dbname $dbname " if defined $dbname;
Index: /tags/ipp-pv3-20140717/ippScripts/scripts/sc_prepare_warp.pl
===================================================================
--- /tags/ipp-pv3-20140717/ippScripts/scripts/sc_prepare_warp.pl	(revision 37290)
+++ /tags/ipp-pv3-20140717/ippScripts/scripts/sc_prepare_warp.pl	(revision 37291)
@@ -1,3 +1,5 @@
 #! /usr/bin/env perl
+
+# generate the input & output files lists and commands for a single warpRun
 
 use Carp;
@@ -16,21 +18,13 @@
 use Pod::Usage qw( pod2usage );
 
-
 # Hard coded values
-# my $remote_root   = '/scratch3/watersc1/';  
-my $remote_root = '/lustre/scratch1/turquoise/watersc1/ps1/'; # Far side destination base location
-$remote_root = '/scratch3/watersc1/';
-my $threads       = 2;                      # How many threads are we going to use?
-my $job_cost      = 110 / 60 / 60;          # Estimate of how long a job runs, in hours.
-my $proc_per_node = 24;                     # processors per node
-my $min_nodes     = 1;                      # smallest allocation to ask for
-my $max_nodes     = 1000;                   # largest allocation to ask for
-my $min_time      = 1;                      # shortest allocation to ask for
-my $max_time      = 8;                      # longest allocation to ask for
-my $remote_hostname= "LANL/Mustang";         # Name of the remote node.
-my @return_component_list = ("DBINFO.EXP", "PSWARP.CONFIG", "PSWARP.OUTPUT", "PSWARP.OUTPUT.MASK", "PSWARP.OUTPUT.VARIANCE", "PSWARP.OUTPUT.SOURCES","PSPHOT.PSF.SKY.SAVE","LOG.EXP");
+# my $remote_root = '/lustre/scratch1/turquoise/watersc1/ps1/'; # Far side destination base location
+my $remote_root   = '/scratch3/watersc1/';  
+my $threads_req   = 4;                      # How many threads are we going to use?
+
+my $fail_state = "prep_fail";
+
 # Look for programs we need
 my $missing_tools;
-
 my $remotetool = can_run('remotetool') or (warn "Can't find remotetool" and $missing_tools = 1);
 my $warptool    = can_run('warptool') or (warn "Can't find warptool" and $missing_tools = 1);
@@ -41,7 +35,8 @@
 }
 
-my ($remote_id,$warp_id,,$camera,$dbname,$verbose,$path_base,$no_update);
+my ($remote_id,$warp_id,$camera,$dbname,$verbose,$path_base,$no_update);
 GetOptions(
     'remote_id=s'    => \$remote_id,
+    'warp_id=s'      => \$warp_id,
     'camera|c=s'     => \$camera,
     'dbname|d=s'     => \$dbname,
@@ -52,35 +47,18 @@
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
-pod2usage( -msg => "Required options: --remote_id --camera --dbname --path_base", -exitval => 3) unless
+pod2usage( -msg => "Required options: --remote_id -warp_id --camera --dbname --path_base", -exitval => 3) unless
     defined($remote_id) and
+    defined($warp_id) and
     defined($camera) and
     defined($path_base) and
     defined($dbname);
-my $ipprc = PS::IPP::Config->new( $camera ) or my_die( "Unable to set up", $warp_id);
+
+my $ipprc = PS::IPP::Config->new( $camera ) or my_die( "Unable to set up", $remote_id, $warp_id, $PS_EXIT_CONFIG_ERROR, $fail_state);
 
 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
 
-#
-# Step 1: Get a list of the components that make up this remoteRun
-
-# SHould this call listrun to ensure we're in state new?
-my $rt_cmd = "$remotetool -listcomponent -remote_id $remote_id";
-$rt_cmd   .= " -dbname $dbname " if defined($dbname);
-
-my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-    run(command => $rt_cmd, verbose => $verbose);
-unless ($success) {
-    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-    &my_die("Unable to run remotetool to determine stage parameters.",
-            $remote_id,$error_code);
-}
-my $compData = $mdcParser->parse(join "", @$stdout_buf) or
-    &my_die("Unable to determine component information.",
-            $remote_id,$PS_EXIT_PROG_ERROR);
-my $compData2= parse_md_list($compData);
-
-#
-# Step 1b: Open output files
-my $uri_command = $path_base . ".cmd";
+my @return_component_list = ("DBINFO.EXP", "PSWARP.CONFIG", "PSWARP.OUTPUT", "PSWARP.OUTPUT.MASK", "PSWARP.OUTPUT.VARIANCE", "PSWARP.OUTPUT.SOURCES","PSPHOT.PSF.SKY.SAVE","LOG.EXP");
+
+# STEP 0: Open output files
 my $uri_transfer= $path_base . ".transfer";
 my $uri_check   = $path_base . ".check";
@@ -89,5 +67,4 @@
 my $uri_return  = $path_base . ".return";
 
-my $disk_command = $ipprc->file_resolve($uri_command,1);
 my $disk_transfer= $ipprc->file_resolve($uri_transfer,1);
 my $disk_check   = $ipprc->file_resolve($uri_check,1);
@@ -98,24 +75,15 @@
 my (undef, $remote_config) = uri_convert($uri_config); # Needs to be done after we've created it.
 
-open(TRANSFER, ">$disk_transfer")  || &my_die("Couldn't open file? $disk_transfer",$remote_id,$PS_EXIT_SYS_ERROR);
-open(CHECK,    ">$disk_check")  || &my_die("Couldn't open file? $disk_check",$remote_id,$PS_EXIT_SYS_ERROR);
-open(CONFIG,   ">$disk_config")  || &my_die("Couldn't open file? $disk_config",$remote_id,$PS_EXIT_SYS_ERROR);
-open(GENERATE, ">$disk_generate") || &my_die("Couldn't open file? $disk_generate",$remote_id,$PS_EXIT_SYS_ERROR);
-open(RETURN,   ">$disk_return") || &my_die("Couldn't open file? $disk_return", $remote_id, $PS_EXIT_SYS_ERROR);
-
-
-#
-# Step 2: Iterate over all componenets in this remote run.
+open(TRANSFER, ">$disk_transfer")  || &my_die("Couldn't open file? $disk_transfer",$remote_id, $warp_id, $PS_EXIT_SYS_ERROR, $fail_state);
+open(CHECK,    ">$disk_check")     || &my_die("Couldn't open file? $disk_check",   $remote_id, $warp_id, $PS_EXIT_SYS_ERROR, $fail_state);
+open(CONFIG,   ">$disk_config")    || &my_die("Couldn't open file? $disk_config",  $remote_id, $warp_id, $PS_EXIT_SYS_ERROR, $fail_state);
+open(GENERATE, ">$disk_generate")  || &my_die("Couldn't open file? $disk_generate",$remote_id, $warp_id, $PS_EXIT_SYS_ERROR, $fail_state);
+open(RETURN,   ">$disk_return")    || &my_die("Couldn't open file? $disk_return",  $remote_id, $warp_id, $PS_EXIT_SYS_ERROR, $fail_state);
+
 my $job_index = 0;
-my @pre_commands = ();
-my @main_commands = ();
-my @post_commands = ();
-
-my %file_filter = ();
-
-foreach my $compEntry (@$compData2) {
-    my $warp_id = $compEntry->{stage_id};
-
-# Get exposure level information from the warpRun we're working from.
+
+# STEP 1: Get exposure level information from the warpRun we're working from.
+my ($warpData);
+{
     # This actually returns all the individual warp/skyfiles that comprise this run.  Because consistency.
     my $command = "$warptool -towarped -warp_id $warp_id ";
@@ -125,15 +93,14 @@
     unless ($success) {
 	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to run warptool -pendingimfile ",
-		$warp_id,$error_code);
-    }
-    my $warpData = $mdcParser->parse(join "", @$stdout_buf) or
-	&my_die("Unable to determine warp component information.",
-		$warp_id,$PS_EXIT_PROG_ERROR);
-    my $warpData2= parse_md_list($warpData);
-
-#
-# Step 3: Iterate over the sub-components
-    foreach my $warpEntry ( @{ $warpData2 } ) {
+	&my_die("Unable to run warptool -pendingimfile ", $remote_id, $warp_id, $error_code, $fail_state);
+    }
+    my $MDlist = $mdcParser->parse(join "", @$stdout_buf) or
+	&my_die("Unable to determine warp component information.", $remote_id, $warp_id, $PS_EXIT_PROG_ERROR, $fail_state);
+    $warpData = parse_md_list($MDlist);
+}
+
+# STEP 2: Iterate over the sub-components
+{
+    foreach my $warpEntry ( @{ $warpData } ) {
 	my $workdir = $warpEntry->{workdir};
 	my $exp_tag = $warpEntry->{exp_tag};
@@ -146,19 +113,17 @@
 	my $remote_outroot = uri_local_to_remote($ipp_outroot);
 
-	my $reg_command = "$warptool -scmap -warp_id $warp_id -skycell_id ${skycell_id}";
-	$reg_command   .= " -dbname $dbname " if defined($dbname);
+	my $command = "$warptool -scmap -warp_id $warp_id -skycell_id ${skycell_id}";
+	$command   .= " -dbname $dbname " if defined($dbname);
 	
 	( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	    run(command => $reg_command, verbose => 0);
+	    run(command => $command, verbose => 0);
 	unless ($success) {
 	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	    &my_die("Unable to run warptool -pendingimfile: $reg_command ",
-		    $warp_id,$error_code);
+	    &my_die("Unable to run warptool -pendingimfile: $command ", $remote_id, $warp_id, $error_code, $fail_state);
 	}
     
-	my $inpData = $mdcParser->parse(join "", @$stdout_buf) or
-	    &my_die("Unable to determine warp component information.",
-		    $warp_id, $PS_EXIT_PROG_ERROR);
-	my $inpData2=parse_md_list($inpData);
+	my $MDlist = $mdcParser->parse(join "", @$stdout_buf) or
+	    &my_die("Unable to determine warp component information.", $remote_id, $warp_id, $PS_EXIT_PROG_ERROR, $fail_state);
+	my $inData = parse_md_list($MDlist);
 	
 	my $pre_cmd_ims = "ls -1 ";
@@ -167,5 +132,5 @@
 	my $pre_cmd_astrom = "";
 
-	foreach my $imfile (@{ $inpData2 }) {
+	foreach my $imfile (@{ $inData2 }) {
 	    my $image = $ipprc->filename("PPIMAGE.CHIP", $imfile->{chip_path_base}, $imfile->{class_id});
 	    my $mask  = $ipprc->filename("PSASTRO.OUTPUT.MASK", $imfile->{cam_path_base}, $imfile->{class_id});
@@ -202,5 +167,5 @@
 	$pswarp_command    .= " -recipe PSWARP $recipe_pswarp ";
 	$pswarp_command    .= " -tracedest ${remote_outroot}.trace -log ${remote_outroot}.log ";
-	$pswarp_command    .= " -threads 4 "; # -image_id ${image_id} -source_id ${source_id} ";
+	$pswarp_command    .= " -threads $threads_req "; # -image_id ${image_id} -source_id ${source_id} ";
 	$pswarp_command    .= " -recipe PPSTATS WARPSTATS ";
 	$pswarp_command    .= " -dumpconfig ${remote_outroot}.pswarp.mdc -stats ${remote_outroot}.stats ";
@@ -225,5 +190,4 @@
 	    print CONFIG " && mkdir -p ${remote_root}/tmp/${remote_outroot_dir} && ln -sf $remote_disk ${remote_root}/tmp/${ipp_disk} && touch $remote_disk ";
 	}
-
 	print CONFIG "\n";
 #	die();
@@ -236,48 +200,14 @@
 close(GENERATE);
 
-#
-# Construct the moab command last, so we can use the job_index counter to estimate resources.  Somehow.
-my $proc_need = $job_index * $threads;       # how many total processors do we need?
-my $node_need = $proc_need / $proc_per_node; # this equals how many nodes?
-my $time_need = $job_index * $job_cost;      # How many seconds will this take?
-
-my $fill_factor = 0.8;  # This is the factor of how much of the time allocation we'd like to fill
-my ($time_req,$node_req);
-if ($node_need * $job_cost < $fill_factor * $min_nodes * $min_time) {
-    $time_req = $min_time;
-    $node_req = $min_nodes;
-}
-elsif ($node_need * $job_cost > $fill_factor * $max_nodes * $max_time) {
-    $time_req = $max_time;
-    $node_req = $max_nodes;
-    print STDERR "You've requested the construction of a bundle that appears to need $node_need nodes and $job_cost time per job.  This exceeds the max limits ($max_nodes, $max_time).  Using those max values instead.  Good luck.\n";
-}
-else {
-    $time_req = int(($node_need * $job_cost) / ($fill_factor * $max_nodes)) + 1;
-    $node_req = int(($node_need * $job_cost) / ($fill_factor * $time_req)) + 1;
-}
-$time_req += 1; # Safety addition.
-
-open(COMMAND,  ">$disk_command") || &my_die("Couldn't open file? $disk_command",$remote_id,$PS_EXIT_SYS_ERROR);
-print COMMAND "#!/bin/tcsh\n";
-print COMMAND "##### Moab controll lines\n";
-print COMMAND "#MSUB -l nodes=${node_req}:ppn=${proc_per_node},walltime=${time_req}:00:00\n"; ## CHECK RESOURCES
-print COMMAND "#MSUB -j oe\n";
-print COMMAND "#MSUB -V\n";
-print COMMAND "#MSUB -o ${remote_root}warp.${remote_id}.out\n";
-print COMMAND "date\n";
-print COMMAND 'srun -n $SLURM_JOB_NUM_NODES -m cyclic -l /bin/hostname | sort -n | awk \'{printf "%s\n", $2}\' > hosts.${SLURM_JOB_ID}' . "\n";
-print COMMAND "${remote_root}/stask_chip.py $remote_config " . 'hosts.${SLURM_JOB_ID} 1' . "\n";
-print COMMAND "date\n";
-close(COMMAND);
-
 unless($no_update) {
-    my $command = "remotetool -updaterun -remote_id $remote_id ";
-    $command .= " -set_state pending ";
+    my $command = "remotetool -updatecomponent -remote_id $remote_id -stage_id $warp_id ";
+    $command .= " -set_jobs $job_index";
+    $command .= " -set_path_base $path_base";
+    $command .= " -set_state prep_done";
     $command .= " -dbname $dbname " if defined $dbname;
 
     system($command);
 }
-
+exit (0);
 
 ## Common SC routines
@@ -289,5 +219,5 @@
     
     unless(defined($ipp_disk)) {
-        my_die();
+        &my_die( "Unable to generate file for $neb_uri ", $remote_id, $warp_id, $PS_EXIT_SYS_ERROR, $fail_state);
     }
 
@@ -305,5 +235,5 @@
     
     unless(defined($ipp_disk)) {
-        my_die();
+        &my_die( "Unable to generate file for $neb_uri ", $remote_id, $warp_id, $PS_EXIT_SYS_ERROR, $fail_state);
     }
 
@@ -361,5 +291,6 @@
 sub my_die { # exit with status; my_die(message,stage_id,exit_code,exit_status);
     my $msg = shift;
-    my $id  = shift;
+    my $remote_id  = shift;
+    my $stage_id  = shift;
     my $exit_code = shift;
     my $exit_state = shift;
@@ -369,7 +300,6 @@
     carp($msg);
     
-    if (defined $id and not $no_update) {
-        my $command = "remotetool -updaterun -remote_id $id";
-        $command .= " -fault $exit_code " if defined $exit_code;
+    if (defined $remote_id and defined $stage_id and not $no_update) {
+        my $command = "remotetool -updatecomponent -remote_id $remote_id -stage_id $stage_id";
         $command .= " -set_state $exit_state " if defined $exit_state;
         $command .= " -dbname $dbname " if defined $dbname;
