Index: /trunk/ippScripts/scripts/sc_prepare_chip.pl
===================================================================
--- /trunk/ippScripts/scripts/sc_prepare_chip.pl	(revision 36595)
+++ /trunk/ippScripts/scripts/sc_prepare_chip.pl	(revision 36596)
@@ -11,4 +11,5 @@
 use DateTime;
 use Data::Dumper;
+use File::Basename;
 
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
@@ -126,5 +127,5 @@
 	$detselect_command2   .= " -iteration $det_iter ";
 	( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	    run(command => $detselect_command2, verbose => $verbose);
+	    run(command => $detselect_command2, verbose => 0);
 	unless ($success) {
 	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
@@ -171,8 +172,12 @@
 # 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 $chip_id = $compEntry->{stage_id};
-    
+    my $exp_id ;
 # Get exposure level information from the chipRun we're working from.
     my $command = "$chiptool -listrun -chip_id $chip_id ";
@@ -197,5 +202,5 @@
 	my $chipEntry = ${ $chipData2 }[0];
 	
-	my $exp_id = $chipEntry->{exp_id};
+	$exp_id = $chipEntry->{exp_id};    
 	my $filter = $chipEntry->{filter};
 	my $altfilt= $filter;
@@ -230,39 +235,6 @@
 	    }
 	}
-
-
-# 	if ($filter eq 'y.00000') {
-# 	    $det_types{'FRINGE'} = '-fringe';
-# 	}
-# 	$filter =~ s/.00000//; # The database doesn't have the trailing zeros, and the query isn't a like.	
-# 	foreach my $det (keys %det_types) {
-# 	    my $detselect_command = "detselect -search ";
-# 	    $detselect_command   .= " -det_type $det ";
-# 	    $detselect_command   .= " -inst $camera " if defined($camera);
-# 	    $detselect_command   .= " -filter $filter " if ($det eq 'FLAT');
-# 	    $detselect_command   .= " -time $dateobs ";
-	    
-# 	    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-# 		run(command => $detselect_command, verbose => $verbose);
-# 	    unless ($success) {
-# 		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-		
-# 		&my_die("Unable to run dettool to obtain detrend information: $det $camera $filter",
-# 			$chip_id,$error_code);
-# 	    }
-	    
-# 	    my $detExp = $mdcParser->parse(join "", @$stdout_buf) or 
-# 		&my_die("Unable to parse detrend information: $det $camera $filter",
-# 			$chip_id,$error_code);
-# 	    my $detExp2= parse_md_list($detExp);
-	    
-# 	    foreach $detExp (@$detExp2) {
-# 		my $det_id = $detExp->{det_id};
-# 		my $iteration = $detExp->{iteration};
-# 		$detrends{$det}{ID}  = $det_id;
-# 		$detrends{$det}{ITER}= $iteration;
-# 	    }
-#	}
-    }
+    }
+
 #
 # Step 3: Iterate over the sub-components
@@ -306,39 +278,4 @@
 	my $ppImage_command = "ppImage -file $remote_uri";
 	$ppImage_command   .= " -burntool $remote_btt ";
-	
-	# Add detrend information to the command line
-# 	foreach my $det (keys %detrends) {
-# 	    my $det_id   = $detrends{$det}{ID};
-# 	    my $det_iter = $detrends{$det}{ITER};
-# 	    my $det_code = $det_types{$det};
-	    
-# 	    if ((($video_cells)&&($det eq 'DARK'))||
-# 		((!$video_cells)&&($det eq 'VIDEODARK'))) {
-# 		next;
-# 	    }
-	    
-# 	    my $detselect_command2 = "detselect -select ";
-# 	    $detselect_command2   .= " -det_id $det_id ";
-# 	    $detselect_command2   .= " -iteration $det_iter ";
-# 	    $detselect_command2   .= " -class_id $class_id ";
-# 	    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-# 		run(command => $detselect_command2, verbose => $verbose);
-# 	    unless ($success) {
-# 		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-		
-# 		&my_die("No valid detrend available for this image: $det $det_id $det_iter $class_id",
-# 			$chip_id,$error_code);
-# 	    }
-	    
-# 	    my $detImfile = $mdcParser->parse(join "", @$stdout_buf) or 
-# 		&my_die("Could not parse detrend information for this image: $det $det_id $det_iter $class_id",
-# 			$chip_id,$error_code);
-# 	    my $detImfile2= parse_md_list($detImfile);
-# 	    foreach $detImfile (@$detImfile2) {
-# 		my $duri = $detImfile->{uri};
-# 		my (undef, $remote_det_uri) = uri_to_outputs($duri);
-# 		$ppImage_command .= " $det_code $remote_det_uri ";
-# 	    }
-# 	}
 	
 	# Add detrend information to the command line
@@ -359,5 +296,5 @@
 	my $remote_outroot = uri_local_to_remote($ipp_outroot);
 	$ppImage_command .= " $remote_outroot ";
-	print STDERR "$remote_outroot $ipp_outroot\n";
+	print STDERR "$remote_outroot $ipp_outroot $class_id\n";
 	# Complete reduction information.
 	$reduction = 'DEFAULT' unless defined $reduction;
@@ -369,12 +306,49 @@
 	$ppImage_command .= " -recipe PPSTATS CHIPSTATS -stats ${remote_outroot}.${class_id}.stats ";
 	$ppImage_command .= " -threads $threads ";
-	$ppImage_command .= " -chip_imfile_id $chip_imfile_id ";
+	$ppImage_command .= " -image_id $chip_imfile_id ";
 	$ppImage_command .= " -tracedest ${remote_outroot}.${class_id}.trace ";
 	$ppImage_command .= " -log ${remote_outroot}.${class_id}.log ";
-	
-	print CONFIG "$ppImage_command\n";
+
+	push @main_commands, $ppImage_command;
+
+	# Calculate pre and post commands
+	my $remote_outroot_dir = dirname($remote_outroot);
+	push @pre_commands, "mkdir -p $remote_outroot_dir";
+	
+	my $post_commandA = "chiptool -addprocessedimfile -exp_id $exp_id -chip_id $chip_id -class_id $class_id ";
+	$post_commandA   .= " -uri ${ipp_outroot}.ch.${class_id}.ch.fits -path_base $ipp_outroot ";
+	$post_commandA   .= " -magicked 0 -hostname REMOTE -dtime_script 0 ";
+	$post_commandA   .= " -dbname $dbname " if defined $dbname;
+
+	my $post_commandB = "echo -n $post_commandA > ${remote_outroot}.${class_id}.dbinfo && ";
+	my $post_commandC = "ppStatsFromMetadata ${remote_outroot}.${class_id}.stats - CHIP_IMFILE >> ${remote_outroot}.${class_id}.dbinfo";
+	
+	push @post_commands, "$post_commandB $post_commandC";
+
 	$job_index++;
     }
 }
+
+# Actually put the commands into the output file.
+my %unique_cmds = ();
+foreach my $cmd (@pre_commands) {
+    unless(exists($unique_cmds{$cmd})) {
+	print CONFIG $cmd . "\n";
+	$unique_cmds{$cmd} = 1;
+    }
+}
+foreach my $cmd (@main_commands) {
+    unless(exists($unique_cmds{$cmd})) {
+	print CONFIG $cmd . "\n";
+	$unique_cmds{$cmd} = 1;
+    }
+}
+foreach my $cmd (@post_commands) {
+    unless(exists($unique_cmds{$cmd})) {
+	print CONFIG $cmd . "\n";
+	$unique_cmds{$cmd} = 1;
+    }
+}
+
 close(CONFIG);
 close(TRANSFER);
@@ -406,5 +380,4 @@
 print COMMAND "#!/bin/tcsh\n";
 print COMMAND "##### Moab controll lines\n";
-#print COMMAND "#MSUB -l nodes=60:ppn=2,walltime=1:00:00\n"; ## CHECK RESOURCES
 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";
@@ -412,10 +385,7 @@
 print COMMAND "#MSUB -o ${remote_root}chip.${remote_id}.out\n";
 print COMMAND "date\n";
-#print COMMAND 'id=$SLURM_JOB_ID' . "\n";
-#print COMMAND 'SLURM_NNODES=$SLURM_JOB_NUM_NODES' . "\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 "srun -n2 -l --multi-prog $remote_config\n";
-print COMMAND "validate_processing.pl ${remote_root}chip.${remote_id}.out\n";
+#print COMMAND "validate_processing.pl ${remote_root}chip.${remote_id}.out\n";
 print COMMAND "date\n";
 close(COMMAND);
