Index: branches/eam_branches/ipp-20130419/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm
===================================================================
--- branches/eam_branches/ipp-20130419/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm	(revision 35548)
+++ branches/eam_branches/ipp-20130419/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm	(revision 35549)
@@ -51,4 +51,7 @@
 
 my $default_data_groups = "";
+
+# old locate_images() function. Still used by dqueryparse
+# Postage stamp server now uses locate_images_for_row()
 
 sub locate_images {
@@ -172,4 +175,5 @@
     my $results = lookup($ipprc, $rowList, $imagedb, $req_type, $stage, $id, $tess_id, $component,
         $need_magic, $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $mjd_min, $mjd_max, 
+        0, 0,   # fwhm cuts are not applied here
         undef, undef, undef, $verbose);
 
@@ -217,7 +221,17 @@
     if (!iszero($mjd_min)) {
         $dateobs_begin = mjd_to_dateobs($mjd_min);
+        if (!$dateobs_begin) {
+            $row->{error_code} = $PSTAMP_INVALID_REQUEST;
+            print STDERR "Invalid MJD_MIN: $mjd_min\n";
+            return undef;
+        }
     }
     if (!iszero($mjd_max)) {
         $dateobs_end = mjd_to_dateobs($mjd_max);
+        if (!$dateobs_begin) {
+            $row->{error_code} = $PSTAMP_INVALID_REQUEST;
+            print STDERR "Invalid MJD_MAX: $mjd_max\n";
+            return undef;
+        }
     }
     if (isnull($tess_id)) {
@@ -250,5 +264,8 @@
     }
 
-    my ($release_name, $survey) = get_release_info($row);
+    my ($release_name, $survey, $default_tess_id) = get_release_info($row);
+    if (!$tess_id and $default_tess_id) {
+        $tess_id = $default_tess_id;
+    }
 
     if ($req_type eq "bydiff") {
@@ -304,4 +321,5 @@
     my $results = lookup($ipprc, $rowList, $imagedb, $req_type, $stage, $id, $tess_id, $component,
         0, $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $mjd_min, $mjd_max,
+        getValOrZero($row->{FWHM_MIN}), getValOrZero($row->{FWHM_MAX}),
         undef, $release_name, $survey,
         $verbose);
@@ -330,4 +348,6 @@
     my $mjd_min = shift;
     my $mjd_max = shift;
+    my $fwhm_min = shift;
+    my $fwhm_max = shift;
     my $selectedAstrom = shift;
     my $release_name = shift;
@@ -365,7 +385,11 @@
     my $release_args;
     if (($req_type eq 'byexp' and $stage ne 'raw') or ($req_type eq 'byskycell' and $stage ne 'diff')) {
-        unless (isnull($release_name) and isnull($survey)) {
+        if ($release_name or $survey) {
             $use_releasetool = 1;
-            $release_args = " -release_name $release_name" if $release_name;
+            if ($release_name) {
+                $release_args = " -release_name $release_name";
+            } else {
+                $release_args = " -priority_order";
+            }
             $release_args .= " -surveyName $survey" if $survey;
         }
@@ -402,5 +426,5 @@
         # choose the chips containing the center pixel by calling selectComponets() below
         if ($use_releasetool) {
-            $command = "$releasetool -dbname $imagedb -priority_order -listrelexp $release_args";
+            $command = "$releasetool -dbname $imagedb -listrelexp $release_args";
             $choose_components = 1;
         } else {
@@ -433,5 +457,6 @@
     } elsif ($stage eq 'warp') {
         if ($use_releasetool) {
-            $command = "$releasetool -dbname $imagedb -priority_order -listrelexp $release_args";
+            $command = "$releasetool -dbname $imagedb -listrelexp $release_args";
+            $skycell_id = $component;
             $choose_components = 1;
         } else {
@@ -444,5 +469,5 @@
                 }
             } else {
-                $command = " -listrun";
+                $command .= " -listrun";
                 $choose_components = 1;
             }
@@ -456,5 +481,5 @@
         $base_name    = "path_base"; # name of the field for the warptool output
     } elsif ($stage eq "diff") {
-        # XXX: is this path ever used for diff images anymore?
+
         if ($component or $use_imfile_id or !$skycenter) {
             $command = "$difftool -diffskyfile -pstamp_order -dbname $imagedb";
@@ -475,4 +500,6 @@
         $psf_name    = "PSPHOT.PSF.SKY.SAVE";
         $base_name   = "path_base";
+        my $diff_mode = getDiffMode($row->{RUN_TYPE});
+        $command .= " -diff_mode $diff_mode" if $diff_mode;
     } elsif ($stage eq "stack") {
         $skycell_id = $component;
@@ -480,4 +507,12 @@
             $command = "$releasetool -listrelstack -dbname $imagedb $release_args";
             $base_name = 'stack_path_base';
+            my $stack_type = $row->{RUN_TYPE};
+            if (!isnull($stack_type)) {
+                if (lc($stack_type) eq 'notnightly') {
+                    $command .= " -stack_type deep -stack_type reference";
+                } else {
+                    $command .= " -stack_type $stack_type";
+                }
+            }
         } else {
             $command = "$stacktool -sumskyfile -dbname $imagedb";
@@ -539,6 +574,6 @@
         $command .= " -dateobs_end $dateobs_end" if $dateobs_end;
     } elsif ($req_type ne "byid") {
-        $command .= " -mjd_obs_begin $mjd_min" if $mjd_min;
-        $command .= " -mjd_obs_end $mjd_max" if $mjd_max;
+        $command .= " -mjd_min $mjd_min" if $mjd_min;
+        $command .= " -mjd_max $mjd_max" if $mjd_max;
     }
 
@@ -552,4 +587,10 @@
     }
 
+    if ($use_releasetool) {
+        # fwhm arguments are currently only supported when using releasetool for lookups
+        $command .= " -fwhm_min $fwhm_min" if $fwhm_min;
+        $command .= " -fwhm_max $fwhm_max" if $fwhm_max;
+    }
+
     my $images = runToolAndParse($command, $verbose);
 
@@ -568,5 +609,5 @@
         # match the coords in the rows to the components in the runs
         # returns an actual list of images
-        if ($skycenter) {
+        if ($skycenter and $req_type ne 'byskycell') {
             $images = selectComponents($ipprc, $imagedb, $req_type, $stage, $rowList, $images, $verbose);
         } else {
@@ -644,4 +685,6 @@
                 $out->{data_state} = $out->{state} = $out->{stack_state};
             } else {
+                # XXX: Consider looking up skycal results even if we are
+                # not using releasetool
                 $out->{data_state} = $out->{state};
             }
@@ -655,5 +698,12 @@
             if ($selectedAstrom) {
                 $out->{astrom} = $selectedAstrom;
-                $out->{cam_path_base} = basename($selectedAstrom);
+                $out->{cam_path_base} = $selectedAstrom;
+                if ($selectedAstrom =~ /\.smf$/) {
+                    $out->{cam_path_base} =~ s/\.smf$//;
+                } elsif ($selectedAstrom =~ /\.cmf$/) {
+                    $out->{cam_path_base} =~ s/\.cmf$//;
+                } else {
+                    die ("ERROR: don't know how to extract cam_path_base from $selectedAstrom\n");
+                }
             } elsif (! defined $out->{astrom}) {
                 if (! find_astrometry($ipprc, $imagedb, $out, $verbose)) {
@@ -697,5 +747,5 @@
 
     my $command = "$difftool -dbname $imagedb -pstamp_order";
-    
+
     my $choose_components = 0;
     if ($byid) {
@@ -713,4 +763,7 @@
     }
 
+    my $diff_mode = getDiffMode($rowList->[0]->{RUN_TYPE});
+    $command .= " -diff_mode $diff_mode" if $diff_mode;
+    
     my $output = runToolAndParse($command, $verbose);
 
@@ -742,4 +795,6 @@
         }
 
+        # XXX: The logic below is somewhat messed up. See XXX: ... below
+        #
         # The standard way to do a diff is warp - stack 
         # so we interpret the requested image in that way
@@ -756,4 +811,9 @@
         }
         my $stack_id;
+        if ($image->{diff_mode} == 2) {
+            # XXX: .. stack_id wasn't getting set
+            $stack_id = $stack2;
+            $image->{stack_id} = $stack_id;
+        }
         if ($stack1 and $stack2) {
             # we have a stack - stack diff (well it might be stack - warp....)
@@ -850,15 +910,21 @@
     my $ra         = shift;
     my $dec        = shift;
-    my $dateobs_begin  = shift;
+    my $dateobs_begin  = shift; # these are used for single frame lookups
     my $dateobs_end    = shift;
     my $filter     = shift;
     my $data_group = shift;
     my $option_mask = shift;
-    my $mjd_min = shift;
-    my $mjd_max = shift;
-    my $astrom = shift;
+    my $mjd_min = shift;        # these are used for stack lookups
+    my $mjd_max = shift;        # these are used for stack lookups
     my $verbose    = shift;
 
-    my ($release_name, $survey) = get_release_info($row);
+    my $fwhm_min = getValOrZero($row->{FWHM_MIN});
+    my $fwhm_max = getValOrZero($row->{FWHM_MAX});
+    
+
+    my ($release_name, $survey, $default_tess_id) = get_release_info($row);
+    if (!$tess_id and $default_tess_id) {
+        $tess_id = $default_tess_id;
+    }
 
     my $rowList = [$row];
@@ -868,5 +934,6 @@
 
         my $chips = lookup_by_cam_id_and_coords($ipprc, $imagedb, $stage,
-            $ra, $dec, $need_magic, $dateobs_begin, $dateobs_end, $filter, $data_group, $verbose, $release_name, $survey, $row->{FWHM_MIN}, $row->{FWHM_MAX});
+            $ra, $dec, $need_magic, $dateobs_begin, $dateobs_end, $filter, $data_group, $verbose,
+            $release_name, $survey, $fwhm_min, $fwhm_max);
 
         if (!$chips or scalar @$chips == 0) {
@@ -878,4 +945,5 @@
                     $tess_id, $chip->{component}, $need_magic, 
                     $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $mjd_min, $mjd_max,
+                    $fwhm_min, $fwhm_max,
                     $chip->{astrom}, undef, undef,
                     $verbose);
@@ -896,8 +964,10 @@
             setErrorCodes($rowList, $PSTAMP_NO_IMAGE_MATCH);
         } else {
+            # XXX: We are not applying the fwhm cuts
             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, $release_name, $survey,
                     $verbose);
@@ -939,8 +1009,8 @@
     if ($release_name or $survey) {
         $command = "$releasetool -dbname $imagedb -listrelexp";
-        if (!isnull($survey)) {
-            $command .= " -survey_name $survey";
-        }
-        if (!isnull($release_name)) {
+        if ($survey) {
+            $command .= " -surveyName $survey";
+        }
+        if ($release_name) {
             $command .= " -release_name $release_name";
         } else {
@@ -1268,4 +1338,9 @@
     my $mjd = shift;
 
+    if ($mjd < 54000) {
+        print STDERR "MJD value too small minimum is 54000\n";
+        return undef; 
+    }
+
     my $ticks = ($mjd - 40587.0) * 86400;
 
@@ -1292,4 +1367,9 @@
     my $val = shift;
     return (!defined($val) or ($val == 0));
+}
+
+sub getValOrZero {
+    my $val = shift;
+    return iszero($val) ? 0.0 : $val;
 }
 
@@ -1525,5 +1605,6 @@
     }
     
-    if (($req_type eq "byid") or ($req_type eq "byexp")) {
+    # XXX: Why did I make this restriction? I added byskycell
+    if (($req_type eq "byid") or ($req_type eq "byexp") or ($req_type eq 'byskycell')) {
         my ($last_tess_id, $tess_dir_abs, $astrom_file) = ("", "", "");
         foreach my $run (@$runList) {
@@ -1757,11 +1838,40 @@
         $release_name = "";
         # no release check for survey
-        $survey = $row->{SURVEY_NAME};
+        $survey = isnull($row->{SURVEY_NAME}) ? "" : $row->{SURVEY_NAME};
     } else {
+        # if release is given leave survey empty
         $survey = "";
     }
 
-    return ($release_name, $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);
+}
+
+sub getDiffMode {
+    my $run_type = shift;
+    my $diff_mode;
+    if (!isnull($run_type)) {
+        $run_type = lc($run_type);
+        if ($run_type eq 'warp_warp') {
+            $diff_mode = 1;
+        } elsif ($run_type eq 'warp_stack') {
+            $diff_mode = 2;
+        } elsif ($run_type eq 'stack_stack') {
+            $diff_mode = 4;
+        } else {
+            print STDERR "Ingnoring unrecognized diff RUN_TYPE : $run_type\n";
+        }
+    }
+    return $diff_mode;
+}
+        
 
 sub my_die
Index: branches/eam_branches/ipp-20130419/PS-IPP-PStamp/lib/PS/IPP/PStamp/RequestFile.pm
===================================================================
--- branches/eam_branches/ipp-20130419/PS-IPP-PStamp/lib/PS/IPP/PStamp/RequestFile.pm	(revision 35548)
+++ branches/eam_branches/ipp-20130419/PS-IPP-PStamp/lib/PS/IPP/PStamp/RequestFile.pm	(revision 35549)
@@ -44,4 +44,5 @@
                     $PSTAMP_DUP_REQUEST
                     $PSTAMP_INVALID_REQUEST
+                    $PSTAMP_UNKNOWN_PROJECT
                     $PSTAMP_UNKNOWN_PRODUCT
                     $PSTAMP_NO_IMAGE_MATCH
@@ -52,5 +53,4 @@
                     $PSTAMP_NO_OVERLAP
                     $PSTAMP_NOT_AUTHORIZED
-                    $PSTAMP_UNKNOWN_PROJECT
                     );
 our %EXPORT_TAGS = (standard => [@EXPORT_OK]);
@@ -81,4 +81,6 @@
 our $PSTAMP_USE_IMFILE_ID      = 16384;
 our $PSTAMP_NO_WAIT_FOR_UPDATE = 32768;
+
+# these bits will be repurposed
 our $PSTAMP_REQUEST_UNCENSORED = 0x10000;
 our $PSTAMP_REQUIRE_UNCENSORED = 0x20000;
@@ -94,13 +96,13 @@
 our $PSTAMP_DUP_REQUEST      = 20;
 our $PSTAMP_INVALID_REQUEST  = 21;
-our $PSTAMP_UNKNOWN_PRODUCT  = 22;
+our $PSTAMP_UNKNOWN_PROJECT  = 22;
+our $PSTAMP_UNKNOWN_PRODUCT  = 22;  #this error code was mis-named it is left for compatabiliyt
 our $PSTAMP_NO_IMAGE_MATCH   = 23;
 our $PSTAMP_NOT_DESTREAKED   = 24;
 our $PSTAMP_NOT_AVAILABLE    = 25;
-our $PSTAMP_GONE             = 26;
+our $PSTAMP_GONE             = 26;  # this value is used in ippTools
 our $PSTAMP_NO_JOBS_QUEUED   = 27;
 our $PSTAMP_NO_OVERLAP       = 28;
 our $PSTAMP_NOT_AUTHORIZED   = 29;
-our $PSTAMP_UNKNOWN_PROJECT  = 22;
 
 
@@ -133,5 +135,5 @@
 PSTAMP_DUP_REQUEST
 PSTAMP_INVALID_REQUEST
-PSTAMP_UNKNOWN_PRODUCT
+PSTAMP_UNKNOWN_PROJECT
 PSTAMP_NO_IMAGE_MATCH
 PSTAMP_NOT_DESTREAKED
@@ -140,4 +142,5 @@
 PSTAMP_NO_JOBS_QUEUED
 PSTAMP_NO_OVERLAP
+PSTAMP_NOT_AUTHORIZED
 PSTAMP_NOT_AUTHORIZED
 );
