Index: /trunk/Ohana/src/perl/src/dads.detrend
===================================================================
--- /trunk/Ohana/src/perl/src/dads.detrend	(revision 88)
+++ /trunk/Ohana/src/perl/src/dads.detrend	(revision 89)
@@ -155,21 +155,12 @@
 @demlist = ();
 foreach $ccd (@ccdn) {
-
-    # create output name
-    $name = "$outdir/$base$ccd.fits";
-    @fitlist = (@fitlist, $name);
-
-    # create tmp detrended name
-    $name = "$workdir/$base$ccd.det";
-    @detlist = (@detlist, $name);
-
-    # create tmp demoded name
-    $name = "$workdir/$base$ccd.dem";
-    @demlist = (@demlist, $name);
-
-    # create tmp defringed name
-    $name = "$workdir/$base$ccd.def";
-    @deflist = (@deflist, $name);
-}
+    push @fitlist, "$outdir/$base$ccd.fits";
+    push @detlist, "$workdir/$base$ccd.det";
+    push @demlist, "$workdir/$base$ccd.dem";
+    push @deflist, "$workdir/$base$ccd.def";
+}
+# phu file:
+$phufile = "$outdir/$base.phu";
+goto skipall;
 
 # final output   is    @fitlist
@@ -190,5 +181,6 @@
     if ($status) { &escape ("ERROR: problem running flatten on $input, $ccds[$i]"); }
 }
-
+$status = &vsystem ("flatten.flips $close -quiet $input $phufile phu mef");
+if ($status) { &escape ("ERROR: problem running flatten on $input, phu"); }
 
 # we need files containing detlist, demlist, deflist:
@@ -229,4 +221,32 @@
 }
 
+# update the phu header with the results from ccd # 0
+if ($DO_DEMODE || $DO_DEFRINGE) {
+    $temp = `mktemp /tmp/elixir.XXXXXX`; chop ($temp);
+    system ("fhead $detlist[0] > $temp.1.hdr");
+    system ("fhead $fitlist[0] > $temp.2.hdr");
+    
+    @answer = `diff $temp.1.hdr $temp.2.hdr`;
+    open (FILE, ">$temp.hdr");
+    foreach $line (@answer) {
+	if ($line =~ m|^>\s|) {
+	    ($head) = $line =~ m|^>\s(.*)$|;
+	    print FILE "$head\n";
+	}
+    }
+    close FILE;
+  
+    vsystem ("fits_insert $phufile $temp.hdr");
+    if ($?) { &escape ("ERROR: cannot update phu"); }
+    unlink ("$temp");
+    unlink ("$temp.1.hdr");
+    unlink ("$temp.2.hdr");
+    unlink ("$temp.hdr");
+}
+
+ skipall:
+    $temp = "/tmp/elixir.1yqFQu";
+exit 1;
+
 &goodbye ("SUCCESS: finished with dads.detrend");
 
