Index: /trunk/Ohana/src/perl/src/dt.select
===================================================================
--- /trunk/Ohana/src/perl/src/dt.select	(revision 46)
+++ /trunk/Ohana/src/perl/src/dt.select	(revision 47)
@@ -10,4 +10,8 @@
 $output = $ARGV[4];
 $mode   = $ARGV[5];
+
+$ptolemy = $ENV{'PTOLEMY'};
+print "PTOLEMY: $ptolemy\n";
+system ("cp $ptolemy /h/eugene/ptolemy.config");
 
 unless ($output =~ /CCDNUM/) { die "ERROR: output file needs CCDNUM component\n"; }
@@ -30,4 +34,5 @@
     if ($filter eq "HA")    { $min = 1000; }
     if ($filter eq "HAOFF") { $min = 1000; }
+    # these should come from the filter tables
     # probably need to set these in a more intelligent fashion.
 }
@@ -35,6 +40,14 @@
 # relevant camera parameters
 $Nccd = `cameraconfig -Nccd`; chop ($Nccd);
-$line = `cameraconfig -axes`; chop ($line);
-($Naxis1, $Naxis2) = split (" ", $line);
+if ($?) { die "error in config\n"; }
+
+($Naxis1, $Naxis2) = split (" ", `cameraconfig -axes`);
+if ($?) { die "error in config\n"; }
+
+@ccds = split (" ", `cameraconfig -ccds`);
+if ($?) { die "error in config\n"; }
+
+@ccdn = split (" ", `cameraconfig -ccdn`);
+if ($?) { die "error in config\n"; }
 
 @good = ();
@@ -45,10 +58,7 @@
 if ($type eq "FLAT") {
     # convert given filter name to list of possible names:
-    print STDERR "filtnames $filter -all\n";
-    @filtlist = `filtnames $filter -all`;
-    foreach $name (@filtlist) {
-	chop $name;
-	$line = "-type flat -filter $name -mode $mode -trange $start $stop";
-	push @filtline, $line;
+    @filtlist = split (" ", `filtnames $filter -all`);
+    foreach $filt (@filtlist) {
+	push @filtline, "-type flat -filter $filt -mode $mode -trange $start $stop";
     }
 } 
@@ -63,5 +73,5 @@
 @list = ();
 foreach $line (@filtline) {
-    @tlist = `imsearch -ccd 0 $line`;
+    @tlist = `imsearch -ccd $ccds[0] $line`;
     @list = (@list, @tlist);
 }
@@ -72,5 +82,4 @@
 foreach $frame (@list) {
     @words = split (" ",$frame);
-    print "$words[5]\n";
 
     # skip domeflat images
@@ -92,17 +101,21 @@
 	@answer = <$words[4]/*.fits>;
 	if (@answer != $Nccd) { print STDERR "$words[5] rejected: missing images\n"; next FRAME; }
+    } else {
+	# base file should exist:
+	if (! -e "$words[4]/$words[5]") { print STDERR "file $words[4]/$words[5] missing, skipping\n"; next FRAME; }
     }
 
     # images have to have the correct dimensions
     if ($mode eq "SPLIT") {
-	$answer = `echo $words[4]/$words[5] | fields NAXIS1 NAXIS2`;
+	($tmp, $Nx, $Ny) = split (" ", `echo $words[4]/$words[5] | fields NAXIS1 NAXIS2`);
     } else {
-	$answer = `echo $words[4]/$words[5] | fields -x 0 NAXIS1 NAXIS2`;
+	($tmp, $Nx, $Ny) = split (" ", `echo $words[4]/$words[5] | fields -x 0 NAXIS1 NAXIS2`);
     }
-    ($tmp, $Nx, $Ny) = split (" ", $answer);
-    if (($Nx != $Naxis1) || ($Ny != $Naxis2)) { print STDERR "$words[5] rejected: wrong dimensions\n"; next FRAME; }
+    if ($?) { print STDERR "failure to find NAXIS1, NAXIS2 in file $words[4]/$words[5], skipping\n"; next FRAME; }
+    if ($Nx != $Naxis1) { print STDERR "$words[5] rejected: wrong dimensions\n"; next FRAME; }
+    if ($Ny != $Naxis2) { print STDERR "$words[5] rejected: wrong dimensions\n"; next FRAME; }
     
-    # search for Nccd images with same name
-    if ($mode eq "split") { 
+    # must have Nccd entries in db:
+    if ($mode eq "SPLIT") { 
 	@answer = split ("/", $words[4]); 
 	$match = $answer[-1]; 
@@ -110,5 +123,5 @@
 	$match = $words[5]; 
     }
-    @list2 = `imsearch -seq -name $match`;
+    @list2 = `imsearch -name $match`;
     if (@list2 != $Nccd) { print STDERR "$words[5] rejected: missing entries in image database\n"; next FRAME; }
 
@@ -119,42 +132,46 @@
 	    @words2 = split (" ", $chip);
 	    
-	    # test for acceptable flux range
-	    if ($words2[11] < $min) { $range = 0; print STDERR "$words[5] rejected: signal too low: $words2[11] vs $min\n"; next FRAME; }
-	    if ($words2[11] > $max) { $range = 0; print STDERR "$words[5] rejected: signal too high: $words2[11] vs $max\n"; next FRAME; }
+	    # test for acceptable counts range
+	    if ($words2[11] < $min) { print STDERR "$words[5] rejected: signal too low: $words2[11] vs $min\n"; next FRAME; }
+	    if ($words2[11] > $max) { print STDERR "$words[5] rejected: signal too high: $words2[11] vs $max\n"; next FRAME; }
 	    
-	    # test for acceptable rate
-	    if (($words2[11] / $words2[7]) < $minrate) { 
-	      $rate = 0; 
-	      $F = $words2[11] / $words2[7];
-	      print STDERR "$words[5] rejected: flux too low: $F vs $minrate\n"; next FRAME; }
+	    # test for acceptable flux
+	    $Flux = $words2[11] / $words2[7];
+	    if ($Flux < $minrate) { print STDERR "$words[5] rejected: flux too low: $Flux vs $minrate\n"; next FRAME; }
 	}
     }
 
-    # add good images to the output lists
+    # add good images to the output lists (use CCD N for output filenames, CCD ID for [extname] in mef files)
+    # entries are not guaranteed to be in CCD N sequence
+
     foreach $line (@list2) {
 	@words2 = split (" ", $line);
-	$tmp = sprintf "chip%02d", $words2[3];
-	@$tmp = (@$tmp, "$words2[4]/$words2[5]");
+	$ccdid  = $words2[3];
+	if ($mode eq "MEF") {
+	    @$ccdid = (@$ccdid, "$words2[4]/$words2[5]\[$ccdid]");
+	} else {
+	    @$ccdid = (@$ccdid, "$words2[4]/$words2[5]");
+	}
     }
-    if (($type eq "BIAS") && (@$tmp > 30)) { last FRAME; }
-    if (($type eq "DARK") && (@$tmp > 30)) { last FRAME; }
-    if (($type eq "FLAT") && (@$tmp > 50)) { last FRAME; }
+    if (($type eq "BIAS") && (@$ccdid > 30)) { last FRAME; }
+    if (($type eq "DARK") && (@$ccdid > 30)) { last FRAME; }
+    if (($type eq "FLAT") && (@$ccdid > 50)) { last FRAME; }
 }
 
-# write output to Nccd files 
+# write output to Nccd list files 
 for ($i = 0; $i < $Nccd; $i++) {
+    $ccd   = $ccdn[$i];
+    $ccdid = $ccds[$i];
+
     $name = $output;
-    $ccd = sprintf "%02d", $i;
     $name =~ s/CCDNUM/$ccd/;
-    $tmp = sprintf "chip%02d", $i;
 
     open (LIST, ">$name");
-    foreach $line (@$tmp) {
+    foreach $line (@$ccdid) {
 	print LIST "$line\n";
     } 
     close (LIST);
 }
-print "\n";
-print STDERR "SUCCESS\n";
+print STDERR "SUCCESS\n\n";
 exit 0;
 
