Index: /trunk/ippScripts/scripts/skycalibration.pl
===================================================================
--- /trunk/ippScripts/scripts/skycalibration.pl	(revision 34791)
+++ /trunk/ippScripts/scripts/skycalibration.pl	(revision 34792)
@@ -134,60 +134,63 @@
 # First check the expected file name where the stack_id is the FILE_ID
 my $file;
-if (!$singlefilter) {
+# if (!$singlefilter) {
+if (1) {
+
     $file = $ipprc->filename('PSPHOT.STACK.OUTPUT', $path_base, $stack_id);
     if (! $ipprc->file_resolve($file)) {
-        # XXX: Beginning of section that can be removed eventually
-
-        # no file with the expected name found
-        # assume that the input is from an early staticsky run that did not use stack_id as the FILE_ID
-        # but instead used FILE_ID = [0 .. num_filters-1]
         print "\nfailed to resolve $file\n";
-        if (!$filter) {
-            &my_die("filter not supplied unable to identify appropriate staticsky input.", $skycal_id, $PS_EXIT_SYS_ERROR);
-        }
-        print "Trying old style FILE_ID\n";
-        my $max_filters = 5;
-        for (my $i=0; $i < $max_filters; $i++) {
-            my $file_id = sprintf "%03d", $i;
-            $file  = $ipprc->filename('PSPHOT.STACK.OUTPUT', $path_base, $file_id);
-            my $resolved = $ipprc->file_resolve($file);
-            if (!$resolved) {
-                # we fail here assumming that if file_id N doesn't exist, neither to N+1
-                print "\nfailed to resolve $file\n\n";
-                &my_die("unable to identify appropriate staticsky input.", $skycal_id, $PS_EXIT_SYS_ERROR);
+        # file with proper file rule not found. Try older systems
+        if ($singlefilter) {
+            # input is from a single filter static sky run which used psphot instead of psphotStack
+            # The file rule is different for psphot
+            $file = $ipprc->filename('PSPHOT.OUT.CMF.MEF', $path_base);
+            &my_die("Unable to find input for $stack_id $filter", $skycal_id, $PS_EXIT_SYS_ERROR) 
+                unless $ipprc->file_exists($file);
+        } else {
+            # XXX: Beginning of section that can be removed eventually
+            # no file with the expected name found
+            # assume that the input is from an early multifilter staticsky run that did not use stack_id as the FILE_ID
+            # but instead used FILE_ID = [0 .. num_filters-1]
+            if (!$filter) {
+                &my_die("filter not supplied unable to identify appropriate staticsky input.", $skycal_id, $PS_EXIT_SYS_ERROR);
             }
-            # Check the filter id for this file
-            my $command = "$fhead $resolved | grep FILTERID";
-            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-                run(command => $command, verbose => 0);
-            unless ($success) {
-                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-                &my_die("Unable to perform $command: $error_code", $skycal_id, $PS_EXIT_SYS_ERROR);
+            print "Trying old style FILE_ID\n";
+            my $max_filters = 5;
+            for (my $i=0; $i < $max_filters; $i++) {
+                my $file_id = sprintf "%03d", $i;
+                $file  = $ipprc->filename('PSPHOT.STACK.OUTPUT', $path_base, $file_id);
+                my $resolved = $ipprc->file_resolve($file);
+                if (!$resolved) {
+                    # we fail here assumming that if file_id N doesn't exist, neither to N+1
+                    print "\nfailed to resolve $file\n\n";
+                    &my_die("unable to identify appropriate staticsky input.", $skycal_id, $PS_EXIT_SYS_ERROR);
+                }
+                # Check the filter id for this file
+                my $command = "$fhead $resolved | grep FILTERID";
+                my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+                    run(command => $command, verbose => 0);
+                unless ($success) {
+                    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+                    &my_die("Unable to perform $command: $error_code", $skycal_id, $PS_EXIT_SYS_ERROR);
+                }
+                # Expected output: HIERARCH FPA.FILTERID = 'r.00000 ' / Filter used (parsed, abstract name)
+                my ($undef, undef, undef, $filt) = split " ", join("", @$stdout_buf);
+                my $this_filter = substr $filt, 1, 7;
+
+                # if it matches we're done
+                last if $this_filter eq $filter;
+
+                # nope loop around to try the next one
+                print "Input file for $filter is not $file ($this_filter)\n";
+                $file = undef;
             }
-            # Expected output: HIERARCH FPA.FILTERID = 'r.00000 ' / Filter used (parsed, abstract name)
-            my ($undef, undef, undef, $filt) = split " ", join("", @$stdout_buf);
-            my $this_filter = substr $filt, 1, 7;
-
-            # if it matches we're done
-            last if $this_filter eq $filter;
-
-            # nope loop around to try the next one
-            print "Input file for $filter is not $file ($this_filter)\n";
-            $file = undef;
-        }
+        }
+        # XXX: End of section that can be removed eventually
+    }
+    if ($file) {
         print "\nInput file for $stack_id filter: $filter: $file\n";
-
-        # XXX: End of section that can be removed eventually
-    }
-} else {
-    # input is from a single filter static sky run use a different file rule
-    # XXX: can't we just make staticsky.pl use the same file rule?
-    $file = $ipprc->filename('PSPHOT.OUT.CMF.MEF', $path_base);
-    &my_die("Unable to find input for $stack_id $filter", $skycal_id, $PS_EXIT_SYS_ERROR) 
-        unless $ipprc->file_exists($file);
-}
-
-if (!$file) {
-    &my_die("Unable to find input for $stack_id $filter", $skycal_id, $PS_EXIT_SYS_ERROR);
+    } else {
+        &my_die("Unable to find input file for stack_id $stack_id.", $skycal_id, $PS_EXIT_SYS_ERROR);
+    }
 }
 
@@ -197,9 +200,11 @@
 }
 
-# read the input header to find the number of detections and the number of detections
-# with extended model
+# XXX: The following is for compatability with psphotStack cmfs created prior to adding NDET
+# read the input header to find the number of detections
+my $compatability_flags = "";
 my $n_detections = 0;
-my $n_extended = 0;
 {
+    # NSTARS is buggy. It only counts sources with a model, and counts them twice.
+    # We want the actual number of sources (real + matched). Use the length of the psf table
     my $command = "echo $resolved | $fields -n SkyChip.psf NAXIS2";
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -211,11 +216,6 @@
         unless defined $n_detections;
 
-    $command = "echo $resolved | $fields -n SkyChip.hdr NDET_EXT";
-    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-            run(command => $command, verbose => 0);
-    (undef, $n_extended)  = split " ", join "", @$stdout_buf;
-    chomp $n_extended;
-    &my_die("Unable to find number of extended objects from $file: ", $skycal_id, $PS_EXIT_SYS_ERROR)
-        unless defined $n_extended;
+    $compatability_flags .= " -n_detections $n_detections";
+
 }
 
@@ -257,4 +257,7 @@
         }
 
+        # if ppStats didn't get n_detections use the compatability version extracted above
+        $cmdflags .= $compatability_flags unless ($cmdflags =~ /-n_detections/);
+
         my $quality;
         ($quality) =  $cmdflags =~ /-quality (\d+)/;
@@ -276,6 +279,4 @@
 {
     my $command = "$staticskytool -skycal_id $skycal_id";
-    $command .= " -n_detections $n_detections";
-    $command .= " -n_extended $n_extended";
     $command .= " -addskycalresult -path_base $outroot";
     $command .= " $cmdflags";
