Index: /tags/ipp-20141024/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm
===================================================================
--- /tags/ipp-20141024/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm	(revision 38162)
+++ /tags/ipp-20141024/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm	(revision 38163)
@@ -83,7 +83,9 @@
            ($req_type ne "byskycell");
 
+if (0) {
     if (!$data_group and !$default_data_groups) {
         $default_data_groups = load_data_groups($verbose);
     }
+}
 
     my $dateobs_begin;
@@ -211,7 +213,11 @@
     my $mjd_max   = $row->{MJD_MAX};
     my $data_group = $row->{DATA_GROUP};
+
+    # default_data_groups is a concept far past it's sell by date
+if (0) {
     if (isnull($data_group) and !$default_data_groups) {
         $default_data_groups = load_data_groups($verbose);
     }
+}
 
     my $rownum     = $row->{ROWNUM};
@@ -273,5 +279,17 @@
     my ($release_name, $survey, $default_tess_id) = get_release_info($row);
     if (!$tess_id and $default_tess_id) {
-        $tess_id = $default_tess_id;
+        # Don't want to set default tess_id in this context
+        # byid: no the tess_id is defined by the run
+        # byexp warp: This is the one case where it might make sense to use a default to avoid double lookup.
+        #   this could be avoided if we did a two level lookup in that case. Find the warpRun first then use the 
+        #   run's tess id to find the specific skycell(s).
+        # bydiff: no tess_id defined by the diff
+        # byskycell requires tess_id so this does not apply
+        # note: currently get_release_info only sets default_tess_id for byskycell and bycoord requests
+        if (ref($default_tess_id) eq 'ARRAY') {
+            $tess_id = $default_tess_id->[0];
+        } else {
+            $tess_id = $default_tess_id;
+        }
     }
 
@@ -393,5 +411,11 @@
     my $release_args;
     if (($req_type eq 'byexp' and $stage ne 'raw') or ($req_type eq 'byskycell' and $stage ne 'diff')) {
-        if ($release_name or $survey) {
+        # we use releasetool for lookups if we get here and have a survey or release specfied and
+        # the survey is not 'bypass' 
+        # That is a hook to allow sophisticated users to bypass the release mechanisms. Useful for
+        # MOPS which is interested primarily in nightly data and wants to get their data immediately
+        # without regard to timeleness of the relexp tables. 
+        # This also alows us to defer loading release tables for gpc2
+        if ($release_name or ($survey and $survey ne 'bypass')) {  
             $use_releasetool = 1;
             if ($release_name) {
@@ -554,5 +578,10 @@
         # $cmf_name    = "PSPHOT.OUTPUT";    # this puts .fpa. in the name
         $psf_name    = "PPSTACK.TARGET.PSF";
+        # XXX TODO should we filter stacks so that only one per skycell/filter combintation is returned for
+        # most requests. Currently unless a release is provided all that match are returned.
+        # Actually maybe that is the right answer. User's simply need to specify a release value
+        # if that is all that they want.
     } else {
+        # this should have been caught before we get here.
         die "Unknown IMG_TYPE supplied: $stage";
     }
@@ -590,4 +619,5 @@
         $command .= " -dateobs_end $dateobs_end" if $dateobs_end;
     } elsif ($req_type ne "byid") {
+        # stacks
         $command .= " -mjd_obs_begin $mjd_min" if $mjd_min;
         $command .= " -mjd_obs_end $mjd_max" if $mjd_max;
@@ -599,5 +629,6 @@
         $command .= " -data_group $data_group";
     } elsif (!$use_releasetool and $req_type eq 'byskycell') {
-        # XXX: Why am I using default data_groups only for byskycell requests?
+        # Default data_groups are used by bycoord in the camera run lookup and
+        # here for byskycell.
         $command .= $default_data_groups;
     }
@@ -773,5 +804,11 @@
         my ($release_name, $survey, $default_tess_id) = get_release_info($row);
         if (!$tess_id and $default_tess_id) {
-            $tess_id = $default_tess_id;
+            if (ref($default_tess_id) eq 'ARRAY') {
+                # XXX: handle the case where default_tess_id is an array
+                # For now just take the first entry (RINGS.V3)
+                $tess_id = $default_tess_id->[0];
+            } else {
+                $tess_id = $default_tess_id;
+            }
         }
 
@@ -1061,12 +1098,19 @@
     
 
+    my @tess_ids;
     my ($release_name, $survey, $default_tess_id) = get_release_info($row);
-    if (!$tess_id and $default_tess_id) {
-        $tess_id = $default_tess_id;
+    if ($tess_id) {
+        push @tess_ids, $tess_id;
+    } elsif ($default_tess_id) {
+        if (ref($default_tess_id) eq 'ARRAY') {
+            @tess_ids = @$default_tess_id;
+        } else {
+            push @tess_ids, $default_tess_id;
+        }
     }
 
     my $rowList = [$row];
 
-    my $results = ();
+    my $results = [];
     if (($stage eq "raw") or ($stage eq "chip")) {
 
@@ -1081,5 +1125,8 @@
                 next if $component and ($chip->{component} ne $component);
                 my $these_results = lookup($ipprc, $rowList, $imagedb, "byid", $stage, $chip->{id},
-                    $tess_id, $chip->{component}, $need_magic, 
+                    # lookup doesn't need tess_id in this context
+                    # $tess_id,
+                    undef, 
+                    $chip->{component}, $need_magic, 
                     $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $mjd_min, $mjd_max,
                     $fwhm_min, $fwhm_max,
@@ -1097,21 +1144,24 @@
                                               and ($stage ne "stack") and ($stage ne "diff");
 
-        my $skycells = lookup_skycell_by_coords($ipprc, $tess_id, $component, $ra, $dec, $verbose);
-
-        if (!$skycells or scalar @$skycells == 0) {
+        foreach my $tess_id (@tess_ids) {
+            my $skycells = lookup_skycell_by_coords($ipprc, $tess_id, $component, $ra, $dec, $verbose);
+
+            if ($skycells and scalar @$skycells != 0) {
+                # XXX: We are not applying the fwhm cuts unless we use releasetool
+                foreach my $skycell (@$skycells) {
+                    my $these_results = lookup($ipprc, $rowList, $imagedb, "byskycell", $stage, undef,
+                        $skycell->{tess_id}, $skycell->{component}, $need_magic, 
+                        $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $mjd_min, $mjd_max, 
+                        $fwhm_min, $fwhm_max,
+                        undef, 0, $release_name, $survey,
+                        $verbose);
+
+                    next if !$these_results;
+                    push @$results, @$these_results;
+                }
+            }
+        }
+        if (scalar @$results == 0) {
             setErrorCodes($rowList, $PSTAMP_NO_IMAGE_MATCH);
-        } else {
-            # XXX: We are not applying the fwhm cuts unless we use releasetool
-            foreach my $skycell (@$skycells) {
-                my $these_results = lookup($ipprc, $rowList, $imagedb, "byskycell", $stage, undef,
-                    $skycell->{tess_id}, $skycell->{component}, $need_magic, 
-                    $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $mjd_min, $mjd_max, 
-                    $fwhm_min, $fwhm_max,
-                    undef, 0, $release_name, $survey,
-                    $verbose);
-
-                next if !$these_results;
-                push @$results, @$these_results;
-            }
         }
     }
@@ -1976,4 +2026,6 @@
 }
 
+# get user supplied release paramters and the asssociated default_tess_id.
+# If release parameters are not supplied set defaults.
 sub get_release_info {
     my $row = shift;
@@ -1985,19 +2037,85 @@
         # no release check for survey
         $survey = isnull($row->{SURVEY_NAME}) ? "" : $row->{SURVEY_NAME};
-    } else {
-        # if release is given leave survey empty
-        $survey = "";
-    }
-
-    my $tess_id;
-    if ($release_name) {
-        # hack to optimize bycoord and byskycell lookups for well known releases
-        # XXX: todo query database and return a list for the survey or release
-        if ($release_name eq '3PI.PV1') {
-            $tess_id = 'RINGS.V3';
-        }
-    }
-
-    return ($release_name, $survey, $tess_id);
+    }
+
+    # XXX: From here down this function is currently very PS1 specific and depends on 
+    # conventions being used for release names.
+    # The comments following the lines  marked 'XXX to improve' give suggestions on
+    # how these hacks can be cleaned up. 
+
+    my $tess_id = $row->{TESS_ID};
+    if (!$release_name and !$survey) { 
+        # no release or survey. If user does not supply
+        # a data group use a default survey
+        my $req_type = $row->{REQ_TYPE};
+        if ($req_type eq "bycoord" or $req_type eq 'byskycell') {
+            # USING 3PI for bycoord and byskycell requests
+            # XXX to improve
+            # get default survey from project
+            my $data_group = $row->{DATA_GROUP};
+            if (isnull($data_group) and isnull($tess_id)) {
+                print "No release information supplied for this row. Defaulting to 3PI survey\n";
+                $survey = '3PI';
+            }
+        }
+    }
+
+    # if survey was not supplied, guess it from the release_name. This is used below
+    # to choose the default_tess_id
+    if (!$survey) {
+        if ($release_name) {
+            # XXX: to improve 
+            # get survey from ippRelease(release_name) in database. Would need to cache
+            # such results
+            if ($release_name =~ "^3PI") {
+                $survey = '3PI';
+            } elsif ($release_name =~ "^MD") {
+                $survey = substr($release_name, 0, 4);
+            } elsif ($release_name =~ "^SSS") {
+                $survey = 'SSS';
+            } elsif ($release_name =~ "^SAS") {
+                $survey = 'SAS';
+            } else {
+                # we don't know
+                print "Can't guess survey for $release_name\n";
+            }
+        } elsif (!isnull($tess_id)) {
+            # finally if it is a MD tessellation use the MD field as the survey
+            # (This will also work for M31 and STS when that data is released.
+            if ($tess_id =~ "^MD") {
+                $survey = substr($tess_id, 0, 4);
+            }
+        }
+    }
+
+    # choose default tess_id based on the survey
+
+    # Note: Setting a default_tess_id effectively disables lookup bycoord requests
+    # for previous tessellations unless the user supplies the tess_id (which is
+    # applied by the caller to this function).
+    # bills has decided that this is desirable behaviour since most everything has
+    # been processed on a V3 tess.
+    my $default_tess_id;
+    if ($survey) {
+        # XXX: to improve
+        # Put default tess_id in survey table.
+        # note users will need to supply tessellation to get at 3pi skycells
+        # in the cnp tessellation. Hmm. Maybe we need to be clever and look
+        # at declination or perhaps allow default_tess_id to be an array.
+        if ($survey eq '3PI') {
+            $default_tess_id = ['RINGS.V3', 'CNP.V3'];
+        } elsif ($survey eq 'SSS' or $survey eq 'SAS') {
+            $default_tess_id = 'RINGS.V3';
+        } elsif ($survey =~ 'MD') {
+            $default_tess_id = $survey . '.V3';
+        }
+    }
+            
+    # leave the returned survey blank if release_name was supplied
+    # XXX: Why? Answer: this is how this function always behaved and I'm not sure if the
+    # callers depend on that behavior.
+    $survey = "" if $release_name;
+
+    return ($release_name, $survey, $default_tess_id);
 }
 
@@ -2007,4 +2125,5 @@
     if (!isnull($run_type)) {
         $run_type = lc($run_type);
+        # XXX: these diff_mode values should be defined somewhere else.
         if ($run_type eq 'warp_warp') {
             $diff_mode = 1;
