Index: trunk/pstamp/scripts/detect_query_create
===================================================================
--- trunk/pstamp/scripts/detect_query_create	(revision 28043)
+++ trunk/pstamp/scripts/detect_query_create	(revision 28806)
@@ -17,4 +17,5 @@
      $query_id, 
      $nostage,
+     $version,
      );
 
@@ -22,5 +23,6 @@
 	   'input|i=s'    => \$input,
 	   'output|o=s'   => \$output,
-	   'query_id|q=s'  => \$query_id,
+	   'query_id|q=s' => \$query_id,
+           'version|v=s'  => \$version,
            'nostage'      => \$nostage,
 ) or pod2usage( 2 );
@@ -30,5 +32,20 @@
            -exitval => 3) unless defined $input and defined $output;
 
-# The header kewords
+# Read what we've been given
+my $in;
+if ($input eq '-') {
+    $in = \*STDIN;
+} else {
+    open $in, "<$input" or die "cannot open $input for reading";
+}
+my %colData;
+my %headerData;
+my $numRows = read_data_for_table($in,'\s+', \%colData, \%headerData); 
+if (!$numRows) {
+    print STDERR "no data in $input\n";
+    exit 1;
+}
+
+# The header keywords
 my $header = [
         { name =>  'QUERY_ID', 
@@ -62,16 +79,53 @@
                     value => undef
         },
-       { name =>  'STAGE',
+        { name =>  'STAGE',
                    writetype => TSTRING,
                    comment => 'processing stage to examine',
                    value => undef
-       }
+        }
 ];
-unless(defined($nostage)) {
-    push @{ $header },        { name =>  'STAGE',
-				writetype => TSTRING,
-				comment => 'processing stage to examine',
-				value => undef
-    };
+
+# Validate header.
+
+if (defined($query_id)) {
+    $headerData{QUERY_ID} = $query_id;
+}
+unless (exists($headerData{QUERY_ID})) {
+    die "No QUERY_ID specified for header.";
+}
+
+unless (exists($headerData{EXTVER})) {
+    die "No EXTVER specified for header.";
+}
+if ($headerData{EXTVER} == 1) {
+    unless (exists($headerData{STAGE})) {
+	warn "No STAGE value specified in header. Assuming default value of 'diff'";
+	$headerData{STAGE} = 'diff';
+    }
+    foreach my $entry_ref (@{ $header }) {
+	my $name = $entry_ref->{name};
+	unless (exists($headerData{$name})) {
+	    die "Required header value $name not specified (try EXTVER=2?).";
+	}
+	$entry_ref->{value} = $headerData{$name};
+    }
+}    
+elsif ($headerData{EXTVER} == 2) {
+    unless (exists($headerData{STAGE})) {
+	warn "No STAGE value specified in header. Assuming default value of 'diff'";
+	$headerData{STAGE} = 'diff';
+    }
+    my $tmp_header;
+    foreach my $entry_ref (@{ $header }) {
+	my $name = $entry_ref->{name};
+	if (exists($headerData{$name})) {
+	    $entry_ref->{value} = $headerData{$name};
+	    push @{ $tmp_header }, $entry_ref;
+	}
+    }
+    $header = $tmp_header;
+}
+else {
+    die "Unknown EXTVER = $headerData{EXTVER}.";
 }
 
@@ -91,32 +145,51 @@
         { name => 'MAG',      type => 'D',   writetype => TDOUBLE },
 ];
-
-my $in;
-if ($input eq '-') {
-    $in = \*STDIN;
-} else {
-    open $in, "<$input" or die "cannot open $input for reading";
-}
-
-my @colData;
-foreach (@$columns) {
-    push @colData, [];
-}
-
-
-my $numRows = read_data_for_table($in,'\s+', \@colData, $header); 
-if (!$numRows) {
-    print STDERR "no data in $input\n";
-    exit 1;
-}
-
-# overwrite the QUERY_ID value from the input file with the command
-# line argument
-
-if ($query_id) {
-    $header->[0]->{value} = $query_id;
-}
-
-my $status = make_fits_table($output, EXTNAME, $numRows, \@colData, $columns, $header);
+my $columns_v2 = [
+    { name => 'FPA_ID', type => '20A', writetype => TSTRING },
+    { name => 'MJD-OBS', type => 'D', writetype => TDOUBLE },
+    { name => 'FILTER', type => '20A', writetype => TSTRING },
+    { name => 'OBSCODE', type => '20A', writetype => TSTRING },
+    { name => 'STAGE', type => '20A', writetype => TSTRING }
+    ];
+# Validate the data.
+if ($headerData{EXTVER} == 1) {
+    foreach my $entry_ref (@{ $columns }) {
+	my $name = $entry_ref->{name};
+	unless (exists($colData{$name})) {
+	    die "Required data column $name not found (try EXTVER=2?).";
+	}
+    }
+}
+if ($headerData{EXTVER} == 2) {
+    my $tmp_columns;
+    foreach my $entry_ref (@{ $columns }) {
+	my $name = $entry_ref->{name};
+	if (exists($colData{$name})) {
+	    push @{ $tmp_columns }, $entry_ref;
+	}
+    }
+    foreach my $entry_ref (@{ $columns_v2 }) {
+	my $name = $entry_ref->{name};
+	if (exists($colData{$name})) {
+	    push @{ $tmp_columns }, $entry_ref;
+	}
+    }
+    $columns = $tmp_columns;
+}	    
+
+# Construct the array of arrays
+my @colDataAoA = ();
+foreach my $col_def (@{ $columns }) {
+    my $name = $col_def->{name};
+    push @colDataAoA, $colData{$name};
+}
+
+# foreach (@$columns) {
+#     push @colData, [];
+# }
+
+
+
+my $status = make_fits_table($output, EXTNAME, $numRows, \@colDataAoA, $columns, $header);
 
 exit $status;
@@ -230,45 +303,49 @@
 
     my $line_num = 0;
-
-    # read data for header if any data is expected
-    if ($header) {
-        my $nhead = @$header;
-        while (my $line = <$in>) {
-            $line_num++;
-            chomp $line;
-            next if !$line;             # skip blank lines
-            next if ($line =~ /^#/);    # skip comment lines
-            my @vals = split /$sep/, $line;
-            my $nvals = @vals;
-            die "number of header columns in input $nvals does not equal expected number of header words $nhead"
-                    if (@vals != @$header);
-
-            for (my $i=0; $i < @$header; $i++) {
-                $header->[$i]->{value} = $vals[$i];
-            }
-
-            last; # only one header line
-        }
-    }
-
-    my $row_num = 0;
-    my $ncols = @$colData;
-    while (my $line = <$in>) {
-        chomp $line;
-        $line_num++;
-        next if !$line;             # skip blank lines
-        next if ($line =~ /^#/);    # skip comment lines
-
-        my @vals = split /$sep/, $line;
-        my $nvals = @vals;
-        die "number of columns $nvals in input does not equal expected number of header "
-                . " words $ncols on line $line_num" if ($nvals != $ncols);
-
-        for (my $col = 0; $col < @$colData; $col++) {
-            $colData->[$col]->[$row_num] = $vals[$col];
-        }
-        $row_num++;
-    }
-
+    my @keywords = ();
+    my $row_num;
+    while (<$in>) {
+	chomp;
+	if ($line_num == 0) {
+	    # Parse header information keywords
+	    $_ =~ s/#//g;
+	    @keywords = split /\s+/;
+	    if ($keywords[0] eq '') {
+		shift(@keywords);
+	    }
+	}
+	elsif ($line_num == 1) {
+	    # Parse header information values
+	    my @values = split /\s+/;
+	    if ($#values != $#keywords) {
+		die "Number of header columns in input does not equal expected number of header words";
+	    }
+	    for (my $i = 0; $i <= $#values; $i++) {
+		$header->{$keywords[$i]} = $values[$i];
+	    }		
+	}
+	elsif ($line_num == 2) {
+	    # Parse table information keywords, dumping old keywords
+	    $_ =~ s/#//g;
+	    @keywords = split /\s+/;
+	    if ($keywords[0] eq '') {
+		shift(@keywords);
+	    }
+	}
+	else {
+	    # Parse table information values
+	    unless ($_ =~ /^#/) {
+		my @values = split /\s+/;
+		if ($#values != $#keywords) {
+		    die "Number of header columns in input does not equal expected number of header words";
+		}
+		for (my $i = 0; $i <= $#values; $i++) {
+		    push @{ $colData{$keywords[$i]} }, $values[$i];
+		    $row_num = $#{ $colData{$keywords[$i] } } + 1;
+		}		
+	    }
+	}
+	$line_num++;
+    }
     # we return the number of rows read
     return $row_num;
