Index: /trunk/tools/gpc1_find_images.pl
===================================================================
--- /trunk/tools/gpc1_find_images.pl	(revision 26097)
+++ /trunk/tools/gpc1_find_images.pl	(revision 26098)
@@ -3,5 +3,5 @@
 use DBI;
 use Getopt::Std;
-getopts('hFARCPp:BMDWvx:n:c:w:O:S:',\%opt);
+getopts('hFARCPp:BLMDWIvx:n:c:w:s:O:S:N:',\%opt);
 #getopt('xncwOS:hARCW',\%opt);
 unless (exists($opt{O})) {
@@ -51,4 +51,5 @@
     print "          -c <chip_id>    ...\n";
     print "          -w <warp_id>    ...\n";
+    print "          -s <stack_id>   ...\n";
     print "          -n <exp_name>   ...\n";
     print " \n";
@@ -71,5 +72,6 @@
 		   ) or die "Unable to connect to database $DBI::errstr\n";
 
-$sth_base = "SELECT warp_id,warpRun.workdir,chipRun.chip_id,chipRun.workdir,rawExp.exp_name,rawExp.exp_id,rawExp.workdir,rawExp.dateobs,rawExp.pon_time from warpRun JOIN fakeRun USING(fake_id) JOIN camRun USING(cam_id) JOIN chipRun USING(chip_id) JOIN rawExp USING (exp_id)";
+$sth_base = "SELECT warp_id,warpRun.workdir,chipRun.chip_id,chipRun.workdir,rawExp.exp_name,rawExp.exp_id,rawExp.workdir,rawExp.dateobs,rawExp.pon_time,0,'nostack' from warpRun JOIN fakeRun USING(fake_id) JOIN camRun USING(cam_id) JOIN chipRun USING(chip_id) JOIN rawExp USING (exp_id)";
+
 # What do we have?
 @sth_adds = ();
@@ -86,4 +88,11 @@
 if (exists($opt{w})) {
     push @sth_adds, "warpRun.warp_id = $opt{w}";
+}
+if (exists($opt{s})) {
+    if ($opt{s} != 0) {
+	push @sth_adds, "stackRun.stack_id = $opt{s}";
+    }
+    $sth_base =~ s/0,"nostack"/stack_id,stackSumSkyfile.uri/;
+    $sth_base .= ' JOIN stackInputSkyfile USING (warp_id) JOIN stackSumSkyfile USING (stack_id) JOIN stackRun USING (stack_id)';
 }
 if ($#sth_adds != -1) {
@@ -97,9 +106,24 @@
 $dr = $db->selectall_arrayref( $sth );
 
+
+%uniq = ();
+$found = 0;
 foreach $rr (@{ $dr }) {
     ($warp_id, $warp_workdir, $chip_id, $chip_workdir, 
-     $exp_name, $exp_id, $exp_workdir, $exp_dateobs, $exp_pontime) = @{ $rr };
+     $exp_name, $exp_id, $exp_workdir, $exp_dateobs, $exp_pontime,
+     $stack_id, $stack_sumfile_uri) = @{ $rr };
+    if ($stack_id == 0) {
+	$stack_sumfile_uri = '';
+    }
 #    print (join "\t", @{ $rr }) . "\n";
-    $exp_pontime = 0; # To stop warnings, but I want this around.
+#    $exp_pontime = 0; # To stop warnings, but I want this around.
+    if (exists($opt{I})) {
+	$stack_id = 0; # Ditto.
+    }
+    if (exists($opt{N})) {
+	if ($found > $opt{N}) {
+	    last;
+	}
+    }
     if (exists($opt{P})) {
 	$sth_previous = "SELECT exp_name from rawExp where dateobs < '$exp_dateobs' order by dateobs desc limit 1";
@@ -127,7 +151,47 @@
 #	exit(0);
     }
-    
-
-
+
+    %mapping = ();
+    if ((exists($opt{C})&&(exists($opt{S})))||
+	(exists($opt{W})&&(exists($opt{O})))||
+	exists($opt{I})) {
+	$sth_mapping = "SELECT skycell_id,class_id from warpSkyCellMap WHERE warp_id = $warp_id";
+	$mr = $db->selectall_arrayref( $sth_mapping );
+	foreach $mrr (@{ $mr }) {
+	    my ($skycell,$class) = @{ $mrr };
+	    my $raw_class = $class;
+	    $raw_class =~ s/xy/ota/i;
+	    push @{ $mapping{$skycell} }, $class;
+	    push @{ $mapping{$skycell} }, $raw_class;
+	    push @{ $mapping{$class} }, $skycell;
+	    push @{ $mapping{$raw_class} }, $skycell;
+	}
+    }
+    
+    if (exists($opt{I})) {
+	if ($warp_id eq '') {
+	    $warp_id = 'NULL';
+	}
+	if ($stack_id eq '') {
+	    $stack_id = 'NULL';
+	}
+	$line = "$exp_name $exp_id $exp_dateobs $exp_pontime $chip_id $warp_id\t";
+	if (exists($opt{O})) {
+	    foreach $sky (@{ $mapping{$opt{O}} }) {
+		$line .= "$sky\t";
+	    }
+	}
+	if (exists($opt{S})) {
+	    foreach $sky (@{ $mapping{$opt{S}} }) {
+		$line .= "$sky\t";
+	    }
+	}
+	$line .= "\n";
+	unless (exists($uniq{$line})) {
+	    print $line;
+	    $uniq{$line} = 1;
+	}
+	next;
+    }
     @scan = ();
     if (exists($opt{R})||exists($opt{B})) {
@@ -135,9 +199,7 @@
 	while (<NLS>) {
 	    chomp;
-	    if (($_ =~ /${opt{O}}/)&&((($opt{A})) || ($_ =~ /fits/))) {
-		if (exists($opt{B})) {
-		    $_ =~ s/.fits$/.burn.tbl/;
-		}
+	    if (is_valid_raw($_)) {
 		push @scan, $_;
+		$found++;
 	    }
 	}
@@ -148,13 +210,7 @@
 	while (<NLS>) {
 	    chomp;
-	    if (($_ =~ /${exp_id}.ch/)&&($_ =~ /$chip_id/)&&($_ =~ /$opt{O}/)&&
-		((($opt{A})) || (($_ =~ /fits/) &&
-				 ($opt{M} || ($_ !~ /mk.fits/)) &&
-				 ($_ !~ /wt.fits/)))) {
-		if ($opt{D} && ($_ !~ /SR/)) {
-		    next;
-		}
+	    if (is_valid_chip($_)) {
 		push @scan, $_;
-
+		$found++;
 	    }
 	}
@@ -165,16 +221,15 @@
 	while (<NLS>) {
 	    chomp;
-#	    print "$_\n";
-	    if (($_ =~ /wrp.${warp_id}.$opt{S}/) && ((($opt{A})) || (($_ =~ /fits/) &&
-								     ($opt{M} || ($_ !~ /mask/)) &&
-								     ($_ !~ /wt/)))) {
-		if ($opt{D} && ($_ !~ /SR/)) {
-		    next;
-		}
+	    if (is_valid_warp($_)) {
 		push @scan, $_;
-	    }
-	}
-    }
-    %uniq = ();
+		$found++;
+	    }
+	}
+    }
+    if (exists($opt{s})) {
+	push @scan, $stack_sumfile_uri;
+	$found++;
+    }
+
     foreach $f (@scan) {
 	unless (exists($uniq{$f})) {
@@ -190,2 +245,161 @@
     }
 }
+
+sub is_mapped {
+    my ($key,$map_string) = @_;
+    
+    unless (exists($mapping{$map_string})) {
+	return(0);
+    }
+    
+    foreach $v (@{ $mapping{$map_string} }) {
+	if ($key =~ /$v/) {
+	    return(1);
+	}
+    }
+    return(0);
+}
+
+sub is_valid_raw {
+    my $key = shift;
+
+    if ($opt{A}) {
+	return(1);
+    }
+    elsif ($opt{L}) {
+	if ($key =~ /log/) {
+	    return(1);
+	}
+	else {
+	    return(0);
+	}
+    }
+    elsif ($opt{B}) {
+	if ($key =~ /burn.tbl/) {
+	    return(1);
+	}
+	else {
+	    return(0);
+	}
+    }
+    elsif ($key !~ /fits/) {
+	return(0);
+    }
+    else {
+	if (($opt{O} ne '')&&($key !~ /$opt{O}/)) {
+	    return(0); # Not the requested OTA
+	}
+	if (($opt{S} ne '')&& !(is_mapped($key,$opt{S}))) {
+	    return(0); # Not the remapped skycell requested
+	}
+	if ($key =~ /burn.fits/) {
+	    return(0);
+	}
+	return(1);
+    }
+}
+    
+sub is_valid_chip {
+    my $key = shift;
+    
+    
+    if ($opt{D} && ($key !~ /SR/)) {
+	return(0); # Not destreaked when required
+    }
+    elsif ($key !~ /${exp_id}.ch/) {
+	return(0); # Not a chip from this exposure
+    }
+    elsif ($key !~ /${chip_id}/) {
+	return(0); # Not from this chip_id
+    }
+
+    if (($opt{O} ne '')&&($key !~ /$opt{O}/)) {
+	return(0); # Not the requested OTA
+    }
+    if (($opt{S} ne '')&& !(is_mapped($key,$opt{S}))) {
+	return(0); # Not the remapped skycell requested
+    }
+	    
+    if ($opt{A}) {
+	return(1); # You asked for all, 
+    }
+    elsif ($opt{L}) {
+	if ($key =~ /log/) {
+	    return(1);
+	}
+	else {
+	    return(0);
+	}
+    }
+    elsif ($key !~ /fits/) {
+	return(0); # Not a fits
+    }
+    elsif ($key =~ /wt.fits/) {
+	return(0); # No handler for weight right now.
+    }
+    elsif ($key =~ /mk/) {
+	if ($opt{M}) {
+	    return(1); # Requested mask
+	}
+	else {
+	    return(0);
+	}
+    }
+    elsif ($key =~ /ch.fits/) {
+	return(1); # Should be just the chip
+    }
+    return(0);
+
+}
+
+sub is_valid_warp {
+    my $key = shift;
+    
+    if ($opt{D} && ($key !~ /SR/)) {
+	return(0); # Not destreaked when required
+    }
+    elsif ($key !~ /wrp.${warp_id}/) {
+	return(0); # Not a warp from this warp_id
+    }
+    if (($opt{S} ne '')&&($key !~ /$opt{S}/)) {
+	return(0); # Not the requested skycell
+    }
+    if (($opt{O} ne '')&& !(is_mapped($key,$opt{O}))) {
+	return(0); # Not the remapped OTA requested
+    }
+	    
+    if ($opt{A}) {
+	return(1); # You asked for all, 
+    }
+    elsif ($opt{L}) {
+	if ($key =~ /log/) {
+	    return(1);
+	}
+	else {
+	    return(0);
+	}
+    }
+    elsif ($key !~ /fits/) {
+	return(0); # Not a fits
+    }
+    elsif ($key =~ /wt.fits/) {
+	return(0); # No handler for weight right now.
+    }
+    elsif ($key =~ /mask/) {
+	if ($opt{M}) {
+	    return(1); # Requested mask
+	}
+	else {
+	    return(0);
+	}
+    }
+    else {
+	return(1); # Should be just the warp.
+    }
+    return(0);
+}
+	
+
+
+    
+    
