Index: trunk/tools/listcmf
===================================================================
--- trunk/tools/listcmf	(revision 34358)
+++ trunk/tools/listcmf	(revision 34359)
@@ -94,4 +94,6 @@
 }
 
+my $flags_col;
+my $flags2_col;
 
 my %extensions;
@@ -225,8 +227,10 @@
     extend_incomplete_lines(\@lines, $total_cols, $ncols_previous);
 } else {
-    print STDERR "no xfit extension found in $cmf\n" unless $xfit_ext;
+    print STDERR "no xfit extension found in $cmf\n" if $verbose;
 }
 
 my $base = basename($cmf) ;
+# data to be output for "missing" sources
+my $null_line = "";
 {
     print $oh "# cmf: $base       FILTER: $filter       ZPT: $zero_point       EXPTIME: $exp_time\n";
@@ -235,17 +239,38 @@
 
     print $oh "IPP_IDET ";
+    # Loop over columns and print out the header line and build $null_line
     for (my $i = 1; $i < scalar @psf_cols; $i++) {
         print $oh " $psf_cols[$i]_$filter";
+        if ($i eq $flags_col) {
+            # for no source set flags to zero
+            $null_line .= '     0';
+        } elsif ($i eq $flags2_col) {
+            # for no source use PM_SOURCE_MODE2_DIFF_WITH_DOUBLE as a "no source" flag
+            # isnan(X_PSF_$filter) also works but this should be faster to process
+            $null_line .= '     2';
+        } elsif ($i > 1) {
+            # for other columns just print NULL which mana converts to nan
+            # Note data for column 1, IPP_IDET == $linenum, is added for the null line below
+            $null_line .= ' NULL';
+        }
     }
     for (my $i = 0; $i < scalar @xsrc_cols; $i++) {
-        print $oh " $xsrc_cols[$i]_$filter"
+        print $oh " $xsrc_cols[$i]_$filter";
+        $null_line .= ' NULL';
     }
     for (my $i = 0; $i < scalar @xfit_cols; $i++) {
         print $oh " $xfit_cols[$i]_$filter";
+        $null_line .= ' NULL';
     }
     print $oh "\n";
 }
 
-foreach my $line (@lines) {
+my $num_lines = scalar @lines;
+for (my $i = 1; $i < $num_lines; $i++) {
+    my $line = $lines[$i];
+    if (!$line) {
+        # Add line for missing source
+        $line = sprintf "%11d %s", $i, $null_line;
+    }
     print $oh "$line\n";
 }
@@ -323,4 +348,12 @@
             # XXX TODO: check the format and insure that it is not a vector
             # because we currently don't support them
+            if ($name eq 'FLAGS') {
+                # print STDERR "found flags in column $col\n";
+                $flags_col = $col;
+            }
+            if ($name eq 'FLAGS2') {
+                # print STDERR "found flags2 in column $col\n";
+                $flags2_col = $col;
+            }
         }
         push @$r_cols, $name;
@@ -336,18 +369,23 @@
         # XXX: we are assuming here that the ids in a psf extension
         # are sequential
-        foreach my $line (@lines) {
+        # foreach my $line (@lines) {
+        my $num_lines = scalar @lines;
+        for (my $i = 0; $i < $num_lines; $i++) {
+            my $line = $lines[$i];
             # id is first column
             chomp $line;
             my ($ipp_idet) = split " ", $line;
-            push @$r_ids, $ipp_idet;
-            push @$r_lines, $line;
+#            push @$r_ids, $ipp_idet;
+#            push @$r_lines, $line;
+            $r_ids->[$ipp_idet] = $ipp_idet;
+            $r_lines->[$ipp_idet] = $line;
         }
     } else {
-        my $num_objects = scalar @$r_lines;
+        my $num_lines = scalar @$r_lines;
         foreach my $line (@lines) {
             chomp $line;
             my ($id, $rest_of_line) = identify_line($line);
-            if ($id > $num_objects) {
-                die "TILT: id $id is greater than num objects\n";
+            if ($id > $num_lines) {
+#                die "TILT: id $id is greater than num objects\n";
             }
             # just append the line to this object's line
@@ -374,4 +412,5 @@
     my $nprevious = shift;
     for (my $i = 0; $i < scalar @$lines; $i++) {
+        next if !$lines->[$i];
         my @words = split " ", $lines->[$i];
         my $nwords = scalar @words;
