Index: /trunk/Ohana/src/perl/src/flatten.flips
===================================================================
--- /trunk/Ohana/src/perl/src/flatten.flips	(revision 109)
+++ /trunk/Ohana/src/perl/src/flatten.flips	(revision 110)
@@ -2,5 +2,5 @@
 
 # grab the command line options
-$use_fringe = 0;
+$defringe = 0;
 $use_mask = 1;
 $fast = 0;
@@ -8,4 +8,5 @@
 $close = "";
 $config = "";
+$preserve = 0;
 @tARGV = ();
 for (; @ARGV > 0; ) {
@@ -22,4 +23,8 @@
         shift; shift; shift; next;
     }
+    if ($ARGV[0] eq "-preserve") {
+        $preserve = 1;
+        shift; next;
+    }
     if ($ARGV[0] eq "-fast") {
         $fast = 1;
@@ -35,5 +40,5 @@
     }
     if ($ARGV[0] eq "-defringe") {
-	$use_fringe = 1;
+	$defringe = 1;
         shift; next;
     }
@@ -55,6 +60,12 @@
 $mode   = uppercase ($ARGV[3]);
 
+$chipname = "-chipname";
 $infile = $input;
 if ($mode eq "MEF") { $infile = "$input\[$ccd]"; }
+if ($ccd eq "phu") {
+    $infile = "$input"; 
+    $ccd = "ccd00";
+    $chipname = "";
+}
 
 # remove old file if it exists
@@ -63,12 +74,12 @@
 
 # find the appropriate detrend images (need to fix output name for MEF - option?)
-$flat=`detsearch $config -chipname -quiet -image $input $ccd $mode -type flat $close`; chop ($flat);
+$flat=`detsearch $config $chipname -quiet -image $input $ccd $mode -type flat $close`; chop ($flat);
 if ($?) { die "ERROR: can't get flat for image\n"; }
 
 if (!$fast) {
-    $bias=`detsearch $config -chipname -quiet -image $input $ccd $mode -type bias $close`; chop ($bias);
+    $bias=`detsearch $config $chipname -quiet -image $input $ccd $mode -type bias $close`; chop ($bias);
     if ($?) { die "ERROR: can't get bias for image\n"; }
 
-    $dark=`detsearch $config -chipname -quiet -image $input $ccd $mode -type dark $close`; chop ($dark);
+    $dark=`detsearch $config $chipname -quiet -image $input $ccd $mode -type dark $close`; chop ($dark);
     if ($?) { die "ERROR: can't get dark for image\n"; }
 }
@@ -77,5 +88,5 @@
 $mask = "none";
 if ($use_mask) {
-    $mask=`detsearch $config -chipname -quiet -image $input $ccd $mode -type mask $close`; chop ($mask);
+    $mask=`detsearch $config $chipname -quiet -image $input $ccd $mode -type mask $close`; chop ($mask);
     if ($?) { die "ERROR: can't get mask for image\n"; }
 }
@@ -89,7 +100,5 @@
 $fringe = "none";
 $frpts  = "none";
-if ($use_fringe) {
-
-    die "ERROR: -fringe not yet available in flatten.flips\n";
+if ($defringe) {
 
     $filterword  = `gconfig $config FILTER-KEYWORD`;    chop ($filterword);
@@ -107,19 +116,10 @@
     
     # check for DEFRINGE, DEMODE 
-    $use_fringe = 0;
+    $defringe = 0;
     for ($i = 0; $i < @detypes; $i++) {
 	if ($detypes[$i] eq "fringe") {
-	    if ($exptime >= $cutoffs[$i]) { $use_fringe = 1; }
+	    if ($exptime >= $cutoffs[$i]) { $defringe = 1; }
 	    last;
 	}
-    }
-
-    # grab necessary detrend files
-    if ($use_fringe) {
-	$fringe = `detsearch $config -chipname -quiet -image $input $ccd $mode -type fringe $close`; chop ($fringe);
-	if ($?) { die "ERROR: can't get fringe for image\n"; }
-
-	$frpts  = `detsearch $config -chipname -quiet -image $input $ccd $mode -type frpts $close`;
-	if ($?) { die "ERROR: can't get fringe points for image\n"; }
     }
 }
@@ -131,5 +131,4 @@
     ($junk, $t1) = split (" ", `echo $input    | fields $exptimeword`);
     ($name, $t2) = split (" ", `echo $darkfile | fields $exptimeword`);
-    print STDERR "darktime: $input, $t1; $darkfile, $t2\n";
     $darkfactor = `darktime.$darkword $t1 $t2`; chop ($darkfactor);
     if ($?) { die "ERROR: failure to get darkfactor $t1 $t2\n"; }
@@ -178,7 +177,7 @@
     }
 
-    # header blocks
-    $line =~ s|DOHEADER|0|;
-    $line =~ s|NBLOCKS|0|;
+    # header blocks - we need an extra block for elixir keywords
+    $line =~ s|DOHEADER|1|;
+    $line =~ s|NBLOCKS|5|;
 }
 
@@ -193,5 +192,29 @@
 $stat = $?;
 
-unlink $temp; 
+if ($preserve) { 
+    print STDERR "imred input file: $temp\n"; 
+} else {
+    unlink $temp; 
+}
+
+if ($stat) {
+    if (! $quiet) { print STDOUT "ERROR: failure running flatten.flips\n"; }
+    exit 1;
+}
+
+if ($defringe) {
+    system ("mv $output $output.det");
+    if ($?) { 
+	print STDERR "failure to save temporary output file\n";
+	exit 1;
+    }
+
+    system ("defringe.ccd -quiet $close $output.dat $output $ccd SPLIT");
+    if ($?) { 
+	print STDERR "failure in defringe\n";
+	$stat = 1;
+    }
+    unlink "$output.dat";
+}    
 
 if (! $quiet) {
@@ -202,5 +225,11 @@
     }
 }
-exit $stat;
+
+# always return 1, don't return just the exit status
+if ($stat) {
+    exit 1;
+} else {
+    exit 0;
+}
 
 sub vsystem {
