Index: /trunk/Ohana/src/perl/src/defringe
===================================================================
--- /trunk/Ohana/src/perl/src/defringe	(revision 45)
+++ /trunk/Ohana/src/perl/src/defringe	(revision 45)
@@ -0,0 +1,161 @@
+#!/usr/bin/env perl
+
+# grab the command line options
+$close = "";
+@tARGV = ();
+for (; @ARGV > 0; ) {
+    if ($ARGV[0] eq "-close") {
+	$close = "-close";
+        shift; next;
+    }
+    @tARGV = (@tARGV, $ARGV[0]);
+    shift;
+}
+@ARGV = @tARGV;
+
+if (@ARGV != 2) { &escape ("ERROR: USAGE: defringe (inlist) (outlist) [-close]"); }
+
+# load config info
+$confdir = `gconfig CONFDIR`; chop $confdir;
+if ($?) { &escape ("ERROR: config failure"); }
+$script = "$confdir/mana/fringe.pro";
+
+# various lists
+@input  = split (" ", `cat $ARGV[0]`);
+@output = split (" ", `cat $ARGV[1]`);
+@ccds   = split (" ", `cameraconfig -ccds`);
+$Nccd   = @ccds;
+
+# find fringe-point file
+$frpts  = `detsearch -quiet -image $input[0] $ccds[0] split -type frpts`; chop $frpts;
+if ($?) { &escape ("ERROR: can't get fringe points for image"); }
+
+# temporary files
+$frstats = mktemp ("defringe");
+$imstats = mktemp ("defringe");
+$fitpars = mktemp ("defringe");
+
+# prepare for defringing:
+# 1) measure input image fringe stats (imstats)
+# 2) extract master fringe frame stats (frstats)
+# 3) create output directory if needed
+# 4) delete output files if present
+
+open (FILE, ">$frstats");
+for ($i = 0; $i < $Nccd; $i++) {
+    # measure fringe amplitude of input image
+    vsystem ("getfringe $input[$i] $ccds[$i]  $frpts 1 >> $imstats");
+    if ($?) { die "ERROR: failure to measure fringe amplitude\n"; }
+
+    # find fringe master frames
+    $fringe[$i] = `detsearch -quiet $close -image $input[$i] $ccds[$i] split -type fringe`; chop $fringe[$i];
+    if ($?) { &escape ("ERROR: can't find fringe master for $input[$i]"); }
+
+    # get fringe master stats
+    ($name, $C0, $C1, $SKY, $RNG) = split (" ", `echo $fringe[$i] | fields FRNG_C0 FRNG_C1 FRNG_SKY FRNG_RNG`);
+    print FILE "$C0 $C1 $SKY $RNG\n";
+
+    # check on output directory
+    &ckdir ($output[$i]);
+
+    if (-e $output[$i]) { unlink $output[$i]; }
+}
+close (FILE);
+
+# convert image stats to optimal image parameters
+vsystem ("fr.frstats -applyfit $imstats $frstats $fitpars");
+@fitpars = `cat $fitpars`;
+
+# load flips parameter file
+$paramfile = `gconfig FLIPS_PARAM_DEFRINGE`; chop $paramfile;
+open (FILE, "$paramfile");
+@rawpars = <FILE>;
+close (FILE);
+
+$infile  = mktemp ("\@defringe");
+$parfile = mktemp ("defringe");
+for ($i = 0; $i < $Nccd; $i++) {
+
+    # create input list file
+    ($s0, $s1, $RF, $ds) = split (" ", $fitpars[$i]);
+    open (FILE, ">$infile");
+    print FILE "$input[$i]  $s0 1.0 $ds\n";
+    print FILE "$fringe[$i] $s1 $RF $ds\n";
+    close (FILE);
+
+    # create input par file
+    @params = @rawpars;
+    foreach $line (@params) {
+	$line =~ s|LISTNAME|$infile|;
+	$line =~ s|FILENAME|$output[$i]|;
+	$line =~ s|COMMENT|Elixir Defringing|;
+    }
+    open (FILE, ">$parfile");
+    foreach $line (@params) { print FILE "$line"; }
+    close (FILE);
+
+    vsystem ("/data/elixir2/elixir/flips2/imcombred $parfile");
+    if ($?) { &escape ("ERROR: failure running imcombred"); }
+}
+
+exit 0;
+
+foreach $name (@temp) { unlink ($name); }
+print STDERR "SUCCESS\n";
+exit 0;
+
+########
+
+sub ckdir {
+
+    my ($file) = $_[0];
+    my ($dir, @words);
+
+    @words = split ("/", $file);
+    pop (@words);
+    $dir = join ("/", @words);
+    if ($dir eq "") { $dir = "."; }
+    if (! -e $dir) {
+	system ("mkdir -p $dir");
+	if ($?) { die "ERROR: can't create output directory $dir\n"; }
+    }
+}
+
+sub loadlist {
+
+    my($command) = $_[0];
+    my(@list, $line);
+
+    @list = `$command`;
+
+    foreach $line (@list) { chop ($line); }
+
+    return @list;
+}
+
+sub vsystem {
+    print STDERR "@_\n";
+    $status = system ("@_");
+    $status;
+}
+
+@temp = ();
+sub mktemp {
+
+    my ($base) = $_[0];
+    my ($name);
+    $name = `mktemp /tmp/$base.XXXXXX`;
+    chop $name;
+
+    push @temp, $name;
+    return $name;
+}
+
+sub escape {
+    $message = $_[0];
+    foreach $name (@temp) {
+	unlink ($name);
+    }
+    die "$message\n";
+}
+
Index: /trunk/Ohana/src/perl/src/detflips
===================================================================
--- /trunk/Ohana/src/perl/src/detflips	(revision 45)
+++ /trunk/Ohana/src/perl/src/detflips	(revision 45)
@@ -0,0 +1,63 @@
+#!/usr/bin/env perl
+
+if (@ARGV != 3) { die "ERROR: USAGE: detflips (inlist) (output) (type)\n" }
+
+$inlist = $ARGV[0];
+$output = $ARGV[1];
+$type   = "\U$ARGV[2]\E";
+
+# count lines in $inlist, adjust statmode (FLAT only)
+($nlines) = split (" ", `wc -l $inlist`);
+$statmode = 0;
+if ($nlines < 6) { $statmode = 1; }
+if ($nlines == 0) { die "ERROR: no files in source list, skipping\n"; }
+
+# delete old output file 
+if (-e $output) { unlink "$output"; }
+
+$temp1 = `mktemp /tmp/detflips.XXXXXX`; chop $temp1;
+$temp2 = `mktemp /tmp/\@detflips.XXXXXX`; chop $temp2;
+
+if ($type eq "BIAS")   { $paramkey = "FLIPS_PARAM_DARK";   $detype = "bias";    }
+if ($type eq "DARK")   { $paramkey = "FLIPS_PARAM_DARK";   $detype = "dark";    }
+if ($type eq "FLAT")   { $paramkey = "FLIPS_PARAM_FLAT";   $detype = "skyflat"; }
+if ($type eq "FRINGE") { $paramkey = "FLIPS_PARAM_FRINGE"; $detype = "fringe";  }
+
+if ($paramkey eq "")  { 
+    print STDERR "ERROR: invalid detrend type $type\n";
+    exit 1;
+}
+
+$paramfile = `gconfig $paramkey`; chop $paramfile;
+open (FILE, "$paramfile");
+@params = <FILE>;
+close (FILE);
+
+print STDERR "using: $paramfile\n";
+
+foreach $line (@params) {
+    $line =~ s|LISTNAME|$temp2|;
+    $line =~ s|FILENAME|$output|;
+    $line =~ s|COMMENT|$type|;
+    $line =~ s|DETRENDTYPE|$detype|;
+    if ($type eq "FLAT") { $line =~ s|STATMODE|$statmode|; }
+}
+
+open (FILE, ">$temp1");
+print FILE "@params\n";
+close $temp1;
+
+system ("cp $inlist $temp2");
+system ("/data/elixir2/elixir/flips2/imcombred $temp1");
+$stat = $?;
+
+# unlink ($temp1);
+# unlink ($temp2);
+
+if ($stat) {
+    print STDERR "ERROR: failure in detflips\n";
+} else {
+    print STDERR "SUCCESS\n";
+}
+
+exit $stat;
Index: /trunk/Ohana/src/perl/src/flatten.flips
===================================================================
--- /trunk/Ohana/src/perl/src/flatten.flips	(revision 45)
+++ /trunk/Ohana/src/perl/src/flatten.flips	(revision 45)
@@ -0,0 +1,230 @@
+#!/usr/bin/env perl
+
+# grab the command line options
+$use_fringe = 0;
+$use_mask = 1;
+$fast = 0;
+$quiet = 0;
+$close = "";
+@tARGV = ();
+for (; @ARGV > 0; ) {
+    if ($ARGV[0] eq "-fast") {
+        $fast = 1;
+        shift; next;
+    }
+    if ($ARGV[0] eq "-quiet") {
+	$quiet = 1;
+        shift; next;
+    }
+    if ($ARGV[0] eq "-close") {
+	$close = "-close";
+        shift; next;
+    }
+    if ($ARGV[0] eq "-defringe") {
+	$use_fringe = 1;
+        shift; next;
+    }
+    if ($ARGV[0] eq "-nomask") {
+	$use_mask = 0;
+        shift; next;
+    }
+    @tARGV = (@tARGV, $ARGV[0]);
+    shift;
+}
+@ARGV = @tARGV;
+
+if (@ARGV != 4) { &usage; }
+
+# command line arguments
+$input  = $ARGV[0];
+$output = $ARGV[1];
+$ccd    = $ARGV[2];
+$mode   = uppercase ($ARGV[3]);
+
+$infile = $input;
+if ($mode eq "MEF") { $infile = "$input\[$ccd]"; }
+
+# remove old file if it exists
+if (-e $output) { unlink "$output"; }
+ckpathname ($output);
+
+# find the appropriate detrend images (need to fix output name for MEF - option?)
+$flat=`detsearch -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 -chipname -quiet -image $input $ccd $mode -type bias $close`; chop ($bias);
+    if ($?) { die "ERROR: can't get bias for image\n"; }
+
+    $dark=`detsearch -chipname -quiet -image $input $ccd $mode -type dark $close`; chop ($dark);
+    if ($?) { die "ERROR: can't get dark for image\n"; }
+}
+
+# find the appropriate mask image
+$mask = "none";
+if ($use_mask) {
+    $mask=`detsearch -chipname -quiet -image $input $ccd $mode -type mask $close`; chop ($mask);
+    if ($?) { die "ERROR: can't get mask for image\n"; }
+}
+
+# abstracted keywords
+$ccdkeyword  = `gconfig -q CCDNUM-KEYWORD`;    chop ($ccdkeyword);
+$darkword    = `gconfig -q DARKTIME-FUNCTION`; chop ($darkword);
+$exptimeword = `gconfig -q EXPTIME-KEYWORD`;   chop ($exptimeword);
+
+# find the appropriate fringe frame & coords file
+$fringe = "none";
+$frpts  = "none";
+if ($use_fringe) {
+
+    die "ERROR: -fringe not yet available in flatten.flips\n";
+
+    $filterword  = `gconfig -q FILTER-KEYWORD`;    chop ($filterword);
+
+    # check the FILTER and EXPTIME, include fringe correction?:
+    ($junk, $exptime, $filter) = `echo $input | fields $exptimeword $filterword`;
+    $filter = uppercase ($filter);
+
+    # recipe file defines detrend types & cutoff exptime values
+    $recipefile = `gconfig DETREND_RECIPES`; chop $recipefile;
+    ($detypes, $cutoffs) = split (" ", `gconfig -c $recipefile $filter`);
+    if ($?) { die "ERROR: failure to get detrend recipes\n"; }
+    @detypes = split (",", $detypes);
+    @cutoffs = split (",", $cutoffs);
+    
+    # check for DEFRINGE, DEMODE 
+    $use_fringe = 0;
+    for ($i = 0; $i < @detypes; $i++) {
+	if ($detypes[$i] eq "fringe") {
+	    if ($exptime >= $cutoffs[$i]) { $use_fringe = 1; }
+	    last;
+	}
+    }
+
+    # grab necessary detrend files
+    if ($use_fringe) {
+	$fringe = `detsearch -chipname -quiet -image $input $ccd $mode -type fringe $close`; chop ($fringe);
+	if ($?) { die "ERROR: can't get fringe for image\n"; }
+
+	$frpts  = `detsearch -chipname -quiet -image $input $ccd $mode -type frpts $close`;
+	if ($?) { die "ERROR: can't get fringe points for image\n"; }
+    }
+}
+
+if (! $fast) {
+    # determine scale factor for dark image (use MEF rootname if EXTNAME is given)
+    $darkfile = $dark;
+    $darkfile =~ s|\[.*\]||;
+    ($junk, $t1) = split (" ", `echo $input    | fields $exptimeword`);
+    ($name, $t2) = split (" ", `echo $darkfile | fields $exptimeword`);
+    $darkfactor = `darktime.$darkword $t1 $t2`; chop ($darkfactor);
+    if ($?) { die "ERROR: failure to get darkfactor $t1 $t2\n"; }
+}
+
+# load FLIPS parameter file, fill in missing words:
+$paramfile = `gconfig FLIPS_IMRED_PARAM`; chop $paramfile;
+if ($?) { die "ERROR: missing FLIPS_IMRED_PARAM in config\n"; }
+open (FILE, "$paramfile");
+@params = <FILE>;
+close (FILE);
+
+foreach $line (@params) {
+    # input / output file names
+    $line =~ s|INFILE|$infile|;
+    $line =~ s|OUTFILE|$output|;
+    $line =~ s|COMMENT|Elixir / Flips2 flatten.flips|;
+
+    # overscan correction
+    $line =~ s|DOOVER|1|;
+
+    if ($fast) {
+	$line =~ s|DOBIAS|0|;
+	$line =~ s|DODARK|0|;
+    } else {
+	# bias corrections
+	$line =~ s|DOBIAS|1|;
+	$line =~ s|BIASNAME|$bias|;
+
+	# dark corrections
+	$line =~ s|DODARK|1|;
+	$line =~ s|DARK_FACT|$darkfactor|;
+	$line =~ s|DARKNAME|$dark|;
+    }
+
+    # flat corrections
+    $line =~ s|DOFLAT|1|;
+    $line =~ s|FLATNAME|$flat|;
+
+    # mask corrections
+    if ($mask eq "none") {
+	$line =~ s|DOMASK|0|;
+    } else {
+	$line =~ s|DOMASK|1|;
+	$line =~ s|MASKNAME|$mask|;
+    }
+
+    # header blocks
+    $line =~ s|DOHEADER|0|;
+    $line =~ s|NBLOCKS|0|;
+}
+
+$temp = `mktemp /tmp/flatten.XXXXXX`; chop $temp;
+open (FILE, ">$temp");
+foreach $line (@params) {
+    print FILE "$line";
+}
+close (FILE);
+
+system ("/data/elixir2/elixir/flips2/imred $temp");
+$stat = $?;
+
+unlink $temp; 
+
+if (! $quiet) {
+    if ($stat) { 
+	print STDOUT "ERROR: failure running flatten.flips\n"; 
+    } else {
+	print STDOUT "SUCCESS\n"; 
+    }
+}
+exit $stat;
+
+sub vsystem {
+    print STDERR "@_\n";
+    $status = system ("@_");
+    $status;
+}
+
+sub goodbye {
+    die "@_\n";
+}
+
+sub usage {
+    print STDERR "USAGE: flatten.flips (input) (output) CCD MODE [-fast] [-close] [-quiet]\n";
+    exit 2;
+}
+
+sub uppercase {
+    return "\U$_[0]\E";
+}
+
+# given a filename, check that its path exists, create it if not
+sub ckpathname {
+    my ($file) = $_[0];
+    my (@words, $dir);
+
+    @words = split ("/", $file);
+    pop (@words);
+    $dir = join ("/", @words);
+
+    if ($dir eq "") { $dir = "."; }
+
+    if (! -d $dir) {
+	system ("mkdir -p $dir");
+	if ($?) {
+	    print STDERR "ERROR: can't make directory component for $file\n";
+	    exit 1;
+	}
+    }
+}
+
Index: /trunk/Ohana/src/perl/src/fr.defringe
===================================================================
--- /trunk/Ohana/src/perl/src/fr.defringe	(revision 45)
+++ /trunk/Ohana/src/perl/src/fr.defringe	(revision 45)
@@ -0,0 +1,125 @@
+#!/usr/bin/env perl
+
+if (@ARGV != 5) { &escape ("ERROR: USAGE: defringe (imlist) (imstats) (frlist) (frstats) (outlist)"); }
+
+$imlist  = $ARGV[0];
+$imstats = $ARGV[1];
+$frlist  = $ARGV[2];
+$frstats = $ARGV[3];
+$output  = $ARGV[4];
+
+# load config info
+$confdir = `gconfig CONFDIR`; chop $confdir;
+if ($?) { &escape ("ERROR: config failure"); }
+
+# various lists
+@input  = split (" ", `cat $imlist`);
+@output = split (" ", `cat $output`);
+@ccds   = split (" ", `cameraconfig -ccds`);
+@ccdn   = split (" ", `cameraconfig -ccdn`);
+$Nccd   = @ccds;
+
+# output directories must exist
+for ($i = 0; $i < $Nccd; $i++) { &ckdir ($output[$i]); }
+
+# convert image stats to optimal image parameters
+$fitpars = &mktemp ("defringe");
+vsystem ("fr.frstats -applyfit $imstats $frstats $fitpars");
+@fitpars = `cat $fitpars`; foreach $line (@fitpars) { chop $line; }
+@fringe  = `cat $frlist`; foreach $line (@fringe) { chop $line; }
+
+# load flips parameter file
+$paramfile = `gconfig FLIPS_PARAM_DEFRINGE`; chop $paramfile;
+open (FILE, "$paramfile");
+@rawpars = <FILE>;
+close (FILE);
+
+$infile  = &mktemp ("\@defringe");
+$parfile = &mktemp ("defringe");
+for ($i = 0; $i < $Nccd; $i++) {
+
+    # create input list file
+    ($s0, $s1, $RF, $ds) = split (" ", $fitpars[$i]);
+    open (FILE, ">$infile");
+    print FILE "$input[$i]  $s0 1.0 $ds\n";
+    print FILE "$fringe[$i] $s1 $RF $ds\n";
+    close (FILE);
+
+    print STDOUT "$fitpars[$i]\n";
+    print STDOUT "$input[$i]  $s0 1.0 $ds\n";
+    print STDOUT "$fringe[$i] $s1 $RF $ds\n";
+
+    # create input par file
+    @params = @rawpars;
+    foreach $line (@params) {
+	$line =~ s|LISTNAME|$infile|;
+	$line =~ s|FILENAME|$output[$i]|;
+	$line =~ s|COMMENT|Elixir Defringing|;
+    }
+    open (FILE, ">$parfile");
+    foreach $line (@params) { print FILE "$line"; }
+    close (FILE);
+
+    if (-e $output[$i]) { unlink $output[$i]; }
+    vsystem ("/data/elixir2/elixir/flips2/imcombred $parfile");
+    if ($?) { &escape ("ERROR: failure running imcombred"); }
+}
+
+foreach $name (@temp) { unlink ($name); }
+print STDERR "SUCCESS\n";
+exit 0;
+
+########################################################
+
+sub ckdir {
+
+    my ($file) = $_[0];
+    my ($dir, @words);
+
+    @words = split ("/", $file);
+    pop (@words);
+    $dir = join ("/", @words);
+    if ($dir eq "") { $dir = "."; }
+    if (! -e $dir) {
+	system ("mkdir -p $dir");
+	if ($?) { die "ERROR: can't create output directory $dir\n"; }
+    }
+}
+
+sub loadlist {
+
+    my($command) = $_[0];
+    my(@list, $line);
+
+    @list = `$command`;
+
+    foreach $line (@list) { chop ($line); }
+
+    return @list;
+}
+
+sub vsystem {
+    print STDERR "@_\n";
+    $status = system ("@_");
+    $status;
+}
+
+@temp = ();
+sub mktemp {
+
+    my ($base) = $_[0];
+    my ($name);
+    $name = `mktemp /tmp/$base.XXXXXX`;
+    chop $name;
+
+    push @temp, $name;
+    return $name;
+}
+
+sub escape {
+    $message = $_[0];
+    foreach $name (@temp) {
+	unlink ($name);
+    }
+    die "$message\n";
+}
Index: /trunk/Ohana/src/perl/src/fr.smooth
===================================================================
--- /trunk/Ohana/src/perl/src/fr.smooth	(revision 45)
+++ /trunk/Ohana/src/perl/src/fr.smooth	(revision 45)
@@ -0,0 +1,104 @@
+#!/usr/bin/env perl
+
+# this program converts the binned fringe image to an unbinned image,
+# adds necessary header information.  it could be used to smooth the 
+# image based on the filter and chip, if needed
+
+$version = 2.0;
+
+if (@ARGV != 8) { &escape ("ERROR: USAGE: fr.smooth (infile) (filter) (CCD) (unbin) (start) (stop) (frstats) (outfile)"); }
+
+$infile  = $ARGV[0];  # input image file
+$filter  = $ARGV[1];  # filter for image
+$ccd     = $ARGV[2];  # ccd number
+$binning = $ARGV[3];  # binning of input image
+$start   = $ARGV[4];  # valid time range start
+$stop    = $ARGV[5];  # valid time range stop
+$frstats = $ARGV[6];  # fringe statistics file
+$outfile = $ARGV[7];  # output image file
+
+@ccds   = split (" ", `cameraconfig -ccds`);
+@ccdn   = split (" ", `cameraconfig -ccdn`);
+$Nccd   = @ccds;
+
+@frstats = `cat $frstats`;
+for ($i = 0; $i < @frstats; $i++) {
+    if ($frstats[$i] =~ m|^\s*\#|) { splice @frstats, $i, 1; $i--; }
+}
+($C0, $C1, $Fs, $Fr) = split (" ", $frstats[$ccd]);
+
+$mask = `detsearch -ccd $ccds[$ccd] -type mask -time $start -select -quiet`; chop $mask;
+if ($?) { &escape ("ERROR: can't find mask for image $1"); }
+
+# unbin and insert header keywords
+open (MANA, "|mana --norc");
+print MANA "macro go\n";
+print MANA " rd a $infile\n";
+print MANA " rebin a A -$binning\n";
+print MANA " rd mask $mask\n";
+print MANA " keyword A NAXIS1 nx1\n";
+print MANA " keyword A NAXIS2 ny1\n";
+print MANA " keyword mask NAXIS1 nx2\n";
+print MANA " keyword mask NAXIS2 ny2\n";
+print MANA " delete a\n";
+print MANA " extract A a 0 0 \$nx1 \$ny1 0 0 \$nx2 \$ny2\n";
+print MANA " set b = a*mask + $Fs*not(mask)\n";
+
+# add keywords for fringe amplitude, fit, etc
+print MANA " keyword b FRNG_C0  -wf $C0\n";
+print MANA " keyword b FRNG_C1  -wf $C1\n";
+print MANA " keyword b FRNG_SKY -wf $Fs\n";
+print MANA " keyword b FRNG_RNG -wf $Fr\n";
+print MANA " keyword b FRNG_REF -wd 11\n";
+print MANA " keyword b TVSTART  -w $start\n";
+print MANA " keyword b TVSTOP   -w $stop\n";
+
+# add comments for the new keywords
+print MANA " keyword b FRNG_C0  -wc \"Fringe Cross Correlation linear fit term\"\n";
+print MANA " keyword b FRNG_C1  -wc \"Fringe Cross Correlation linear fit term\"\n";
+print MANA " keyword b FRNG_SKY -wc \"Fringe Frame sky level\" \n";
+print MANA " keyword b FRNG_RNG -wc \"Fringe Frame fringe strength\"\n";
+print MANA " keyword b FRNG_REF -wc \"Fringe Cross Correlation reference chip\"\n";
+print MANA " keyword b TVSTART  -wc \"data validity start time\"\n";
+print MANA " keyword b TVSTOP   -wc \"data validity stop time\"\n";
+print MANA " keyword b OBSTYPE  -w  \"FRINGE\"\n";
+print MANA " keyword b EXPTYPE  -w  \"FRINGE\"\n";
+
+print MANA " keyword b COMMENT  -ws \"Fringe (NN) = C0 + C1 * Fringe (REF)\"\n";
+print MANA " keyword b COMMENT  -ws \"Elixir:fr.smooth version $version\"\n";
+print MANA " keyword b COMMENT  -ws \"Elixir Fringe Frame\"\n";
+
+print MANA " wd b $outfile\n";
+print MANA " exit 0\n";
+
+print MANA "end\n";
+print MANA "go\n";
+print MANA "exit 1\n";
+close (MANA);
+if ($?) { &escape ("ERROR: failure running mana"); }
+
+print STDERR "SUCCESS\n";
+exit 0;
+
+####################################################
+
+@temp = ();
+sub mktemp {
+
+    my ($base) = $_[0];
+    my ($name);
+    $name = `mktemp /tmp/$base.XXXXXX`;
+    chop $name;
+
+    push @temp, $name;
+    return $name;
+}
+
+sub escape {
+    $message = $_[0];
+    foreach $name (@temp) {
+	unlink ($name);
+    }
+    die "$message\n";
+}
+
Index: /trunk/Ohana/src/perl/src/getfringe
===================================================================
--- /trunk/Ohana/src/perl/src/getfringe	(revision 45)
+++ /trunk/Ohana/src/perl/src/getfringe	(revision 45)
@@ -0,0 +1,117 @@
+#!/usr/bin/env perl
+
+if (@ARGV != 4) { &escape ("USAGE: getfringe (input) (ccd) (frpts) (bin)"); }
+
+# assign the command line arguments
+$input   = $ARGV[0];
+$ccd     = $ARGV[1];
+$frpts   = $ARGV[2];
+$binning = $ARGV[3];
+
+# temporary files for flips input/output
+$temp1   = mktemp ("\@getfringe");
+$temp2   = mktemp ("getfringe");
+$temp3   = mktemp ("getfringe");
+
+# create input list file
+@coords = `gtfringetable $frpts $ccd $binning`;
+if ($?) { &escape ("failure to extract fringe point table"); }
+open (FILE, ">$temp1");
+foreach $line (@coords) {
+    chop ($line);
+    print FILE "$input $line\n";
+}
+close (FILE);
+
+# box size for stats:
+$dx = 1 + 2*int(0.5*25/$binning);
+
+# find input parameter file
+$paramfile = `gconfig FLIPS_IMSTAT_PARAM`; chop $paramfile;
+if ($?) { &escape ("params not in config system"); }
+
+# fill in missing keywords
+open (FILE, "$paramfile");
+@params = <FILE>;
+close (FILE);
+
+foreach $line (@params) {
+    chop ($line);
+    $line =~ s|INFILE|$temp1|;
+    $line =~ s|SIZEX|$dx|;
+    $line =~ s|SIZEY|$dx|;
+    $line =~ s|OUTFILE|$temp3|;
+}
+
+# save new parameter file
+open (FILE, ">$temp2");
+foreach $line (@params) {
+    print FILE "$line\n";
+}
+close $temp2;
+
+# run imstat (change to flips2 version)
+system ("/data/elixir2/elixir/flips2/imstat_binary $temp2 > /dev/null");
+if ($?) { &escape ("failure in imstat"); }
+
+# accumulate sky, fringe
+open (FILE, "$temp3");
+@stats = <FILE>;
+@sky    = ();
+@fringe = ();
+for ($i = 0; $i < @stats; $i++) {
+    ($name, $max) = split (" ", $stats[$i]);
+    $i ++;
+    ($name, $min) = split (" ", $stats[$i]);
+
+    $fringe = $max - $min;
+    push @sky, $min;
+    push @fringe, $fringe;
+}
+
+# calculate fringe sigma:
+$F1 = $F2 = 0;
+foreach $fringe (@fringe) { 
+    $F1 += $fringe;
+    $F2 += $fringe*$fringe;
+}
+$N = @fringe;
+$F1 = $F1 / $N;
+$dfringe = sqrt ($F2/$N - $F1*$F1);
+
+# calculate fringe & sky medians
+@fringe = sort @fringe;
+$fringe = $fringe[int(0.5*$N)];
+@sky = sort @sky;
+$sky = $sky[int(0.5*$N)];
+
+# write out results 
+print STDOUT "$input $sky $fringe $dfringe\n";
+
+# cleanup
+unlink $temp1;
+unlink $temp2;
+unlink $temp3;
+exit 0;
+
+########
+
+@temp = ();
+sub mktemp {
+
+    my ($base) = $_[0];
+    my ($name);
+    $name = `mktemp /tmp/$base.XXXXXX`;
+    chop $name;
+
+    push @temp, $name;
+    return $name;
+}
+
+sub escape {
+    $message = $_[0];
+    foreach $name (@temp) {
+	unlink ($name);
+    }
+    die "$message\n";
+}
Index: /trunk/Ohana/src/perl/src/mefheadunstrip
===================================================================
--- /trunk/Ohana/src/perl/src/mefheadunstrip	(revision 45)
+++ /trunk/Ohana/src/perl/src/mefheadunstrip	(revision 45)
@@ -0,0 +1,88 @@
+#!/usr/bin/env perl
+
+$xtline = "XTENSION= 'IMAGE   '           / Image extension ";
+$rwline = "SIMPLE  =                    T / Standard FITS   ";
+
+# input file is PHU: foo.fits.hdr
+# expect NEXTEND files: fooNN.fits.hdr
+
+if (@ARGV != 2) { die "ERROR: USAGE: mefheadunstrip (input) (output)\n" }
+$input  = $ARGV[0];
+$output = $ARGV[1];
+($root) = $input =~ m|(\S*).fits.hdr|;
+
+# blank line for padding:
+$blank = "";
+for ($i = 0; $i < 80; $i++) { $blank = $blank . " "; }
+
+if (-e $output) { die "remove existing target\n"; }
+
+open (OUT, ">$output");
+
+# load primary header
+@head = &loadheader ($input);
+foreach $line (@head) {
+    print OUT "$line";
+}
+
+# find N extensions
+$Nextend = 0;
+foreach $line (@head) {
+    if ($line =~ m|^NEXTEND =\s+\d+|) {
+	($Nextend) = $line =~ m|^NEXTEND =\s+(\d+)|;
+	last;
+    }
+}
+if (!$Nextend) { die "ERROR: no extensions\n"; }
+
+# load extension headers
+for ($i = 0; $i < $Nextend; $i++) {
+    $file = sprintf "%s%02d.fits.hdr", $root, $i;
+    print STDERR "$file\n";
+    
+    @head = &loadheader ($file);
+    foreach $line (@head) {
+	if ($line =~ s|^$rwline|$xtline|) {
+	    print STDERR "fixed SIMPLE\n";
+	}
+    }
+    foreach $line (@head) {
+	print OUT "$line";
+    }
+}
+
+close (OUT);
+exit 0;
+
+# load, strip, pad, fix NAXIS:
+sub loadheader {
+
+    my ($file) = $_[0];
+    my (@input, $i);
+
+    open (FILE, "$file");
+    @input = <FILE>;
+    close (FILE);
+    
+    # strip return chars
+    foreach $line (@input) { chop $line; }
+
+    # pad end of block
+    $N = @input;
+    if ($N % 36) {
+	$Nxtra = 36 - ($N % 36);
+	for ($i = 0; $i < $Nxtra; $i++) {
+	    @input = (@input, $blank);
+	}
+    }
+
+    # NAXIS = 2 -> NAXIS = 0
+    foreach $line (@input) {
+	if ($line =~ s|^NAXIS   =                    2|NAXIS   =                    0|) {
+	    print STDERR "fixed NAXIS\n";
+	}
+    }
+    
+    return (@input);
+}
+
