Changeset 28806 for trunk/pstamp/scripts/detect_query_read
- Timestamp:
- Jul 30, 2010, 12:41:45 PM (16 years ago)
- File:
-
- 1 edited
-
trunk/pstamp/scripts/detect_query_read (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/pstamp/scripts/detect_query_read
r28777 r28806 20 20 use Math::Trig; 21 21 use Data::Dumper; 22 use IPC::Cmd 0.36 qw( can_run run ); 22 23 23 24 use constant EXTNAME => 'MOPS_DETECTABILITY_QUERY'; # Extension name for table … … 31 32 $output, # Name of output table 32 33 $save_temps, # Save temporary files? 34 $dbname, # needed to do camtool lookups. 33 35 ); 36 37 my $regtool = can_run('regtool') or (die "Can't find regtool"); 38 my $camtool = can_run('camtool') or (die "Can't find regtool"); 39 34 40 35 41 GetOptions( 36 42 'input|i=s' => \$input, 37 43 'output|o=s' => \$output, 44 'dbname=s' => \$dbname, 38 45 'nolabel|l' => \$no_print_label, 39 46 'noheader|h' => \$no_print_header, … … 65 72 66 73 # The keywords found in the header 67 # my $header = {68 # 'EXTVER' => { name => 'EXTVER',69 # writetype => TSTRING,70 # comment => 'Extension version',71 # value => undef72 # },73 # 'EXTNAME' => { name => 'EXTNAME',74 # writetype => TSTRING,75 # comment => 'name of this binary table extension',76 # value => undef77 # },78 # 'QUERY_ID' => { name => 'QUERY_ID',79 # writetype => TSTRING,80 # comment => 'MOPS Query ID for this batch query',81 # value => undef82 # },83 # 'FPA_ID' => { name => 'FPA_ID',84 # writetype => TSTRING,85 # comment => 'orginal FPA_ID used at ingest',86 # value => undef87 # },88 # 'MJD_OBS' => { name => 'MJD-OBS',89 # writetype => TDOUBLE,90 # comment => 'starting time of the exposure, MJD',91 # value => undef92 # },93 # 'FILTER' => { name => 'FILTER',94 # writetype => TSTRING,95 # comment => 'effective filter use for the exposure',96 # value => undef97 # },98 # 'OBSCODE' => { name => 'OBSCODE',99 # writetype => TSTRING,100 # comment => 'site identifier (MPC observatory code)',101 # value => undef102 # },103 # 'STAGE' => {104 # name => 'STAGE',105 # writetype => TSTRING,106 # comment => 'processing stage to examine',107 # value => undef108 # }109 # };110 74 111 75 my $parse_error = 0; 112 # Parse the header to determine what we expect to find. 76 # Parse the header to determine what we have to work with. 77 # Clean up empty space and remove unneeded single quotes. 113 78 foreach my $header_key (keys %{ $inHeader }) { 114 79 $inHeader->{$header_key} =~ s/\s+//g; 115 80 $inHeader->{$header_key} =~ s/\'//g; 116 81 } 117 my ($EXTVER,$headerEXTNAME,$QUERY_ID,$FPA_ID,$MJD_OBS,$FILTER,$OBSCODE,$STAGE) = 118 ($inHeader->{EXTVER},$inHeader->{EXTNAME},$inHeader->{QUERY_ID},$inHeader->{FPA_ID}, 119 $inHeader->{'MJD-OBS'},$inHeader->{FILTER},$inHeader->{OBSCODE},$inHeader->{STAGE}); 82 my ($EXTVER,$headerEXTNAME,$QUERY_ID, 83 $FPA_ID,$MJD_OBS,$FILTER, 84 $OBSCODE,$STAGE) = 85 ($inHeader->{EXTVER},$inHeader->{EXTNAME},$inHeader->{QUERY_ID}, 86 $inHeader->{FPA_ID},$inHeader->{'MJD-OBS'},$inHeader->{FILTER}, 87 $inHeader->{OBSCODE},$inHeader->{STAGE}); 88 120 89 unless(defined($EXTVER) && defined($headerEXTNAME) && 121 90 (($EXTVER == 1)||($EXTVER == 2)) && 122 ($headerEXTNAME eq EXTNAME)) { 91 ($headerEXTNAME eq EXTNAME)&& 92 (defined($QUERY_ID))) { 123 93 $parse_error = 1; 124 94 } … … 143 113 # apparent magnitude 144 114 { name => 'MAG', type => 'D', writetype => TDOUBLE, version => 1 }, 145 # v2 query_id: MOPS query ID for this batch query115 # v2 query_id: needs to be here. 146 116 { name => 'QUERY_ID', type => '20A', writetype => TSTRING, version => 2, default => $QUERY_ID }, 147 117 # v2 fpa_id: original FPA_ID used at ingest … … 172 142 173 143 if ($col->{version} > $EXTVER) { 174 @{ $colData{$col->{name}} } = map { $col->{default} } (0 .. $numRows );144 @{ $colData{$col->{name}} } = map { $col->{default} } (0 .. $numRows - 1); 175 145 next; 176 146 } 177 $inFits->get_colnum(0, $col->{name}, $col_num, $status) and check_fitsio($status); 178 $inFits->get_coltype($col_num, $col_type, undef, undef, $status) and check_fitsio($status); 179 $inFits->read_col($col_type, $col_num, 1, 1, $numRows, 0, $col_data, undef, $status) 180 and check_fitsio($status); 181 $colData{$col->{name}} = $col_data; 182 if ($col->{name} eq 'MJD-OBS') { 183 print @{ $col_data } . "\n"; 147 else { 148 if (defined($col->{default})) { 149 @{ $colData{$col->{name}} } = map { $col->{default} } (0 .. $numRows - 1); 150 next; 151 } 152 } 153 154 $inFits->get_colnum(0, $col->{name}, $col_num, $status); 155 if ($status == 0) { 156 $inFits->get_coltype($col_num, $col_type, undef, undef, $status) and check_fitsio($status); 157 $inFits->read_col($col_type, $col_num, 1, 1, $numRows, 0, $col_data, undef, $status) 158 and check_fitsio($status); 159 $colData{$col->{name}} = $col_data; 160 if ($col->{name} eq 'QUERY_ID') { 161 print @{ $col_data } . "\n"; 162 } 163 164 } 165 elsif ($status == 219) { 166 @{ $colData{$col->{name}} } = map { "Not Set" } (0 .. $numRows - 1); 167 $status = 0; 168 } 169 } 170 171 # Set things that aren't set. 172 for (my $i = 0; $i < $numRows; $i++) { 173 # Simple stuff first. 174 my %known_filters = (); 175 if ($colData{STAGE}[$i] eq "Not Set") { 176 $colData{STAGE}[$i] = 'diff'; 177 } 178 if ($colData{OBSCODE}[$i] eq "Not Set") { 179 $colData{OBSCODE}[$i] = 566; 180 } 181 # Define filter and MJD from FPA_ID 182 if (($colData{FILTER}[$i] eq "Not Set")&&($colData{FPA_ID}[$i] ne "Not Set")) { 183 if (exists($known_filters{$colData{FPA_ID}[$i]})) { 184 $colData{FILTER}[$i] = $known_filters{$colData{FPA_ID}[$i]}; 185 } 186 else { 187 my $cmd = "$regtool -processedexp -dbname $dbname -exp_name $colData{FPA_ID}[$i]"; 188 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 189 run(command => $cmd, verbose => 0); 190 unless ($success) { 191 # This is a problem, because I'm not sure how we handle a failure to read something. 192 # We need to return a $PSTAMP_INVALID_REQUEST, I think, but if we can't read it, 193 # we can't send that response back. 194 die("Unable to perform $cmd error code: $error_code"); 195 } 196 foreach my $entry (split /\n/, (join "", @$full_buf)) { 197 $entry =~ s/^\s+//; 198 my @line = split /\s+/, $entry; 199 if (scalar(@line) != 3) { 200 next; 201 } 202 my ($key,$type,$value) = @line; 203 # print "$entry => $key / $type / $value \n"; 204 if ($key =~ /filter/i) { 205 $value =~ s/.00000//; 206 $colData{FILTER}[$i] = $value; 207 $known_filters{$colData{FPA_ID}[$i]} = $value; 208 } 209 } 210 } 211 } 212 if (($colData{'MJD-OBS'}[$i] eq "Not Set")&&($colData{FPA_ID}[$i] ne "Not Set")) { 213 # HACK! 214 my $mjd = $colData{FPA_ID}[$i]; 215 $mjd =~ s/o(....)g.*/$1/; 216 $mjd += 50000; 217 $colData{'MJD-OBS'}[$i] = $mjd; 218 } 219 if (($colData{FPA_ID}[$i] eq "Not Set")&&(($colData{'FILTER'}[$i] ne "Not Set")&& 220 ($colData{'MJD-OBS'}[$i] ne "Not Set"))) { 221 my $dateobs_begin = mjd_to_dateobs($colData{'MJD-OBS'}[$i]); 222 my $dateobs_end = mjd_to_dateobs($colData{'MJD-OBS'}[$i] + 1); 223 my $ra = $colData{'RA1_DEG'}[$i]; 224 my $dec = $colData{'DEC1_DEG'}[$i]; 225 my $filter = $colData{'FILTER'}[$i] . ".00000"; 226 my $cmd = "$camtool -processedexp -dbname $dbname -filter $filter "; 227 $cmd .= " -dateobs_begin $dateobs_begin -dateobs_end $dateobs_end "; 228 $cmd .= " -ra $ra -decl $dec -limit 1"; 229 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 230 run(command => $cmd, verbose => 0); 231 unless ($success) { 232 # This is a problem, because I'm not sure how we handle a failure to read something. 233 # We need to return a $PSTAMP_INVALID_REQUEST, I think, but if we can't read it, 234 # we can't send that response back. 235 die("Unable to perform $cmd error code: $error_code"); 236 } 237 foreach my $entry (split /\n/, (join "", @$full_buf)) { 238 $entry =~ s/^\s+//; 239 my @line = split /\s+/, $entry; 240 if (scalar(@line) != 3) { 241 next; 242 } 243 my ($key,$type,$value) = @line; 244 # print "$entry => $key / $type / $value \n"; 245 if ($key =~ /exp_name/i) { 246 $colData{FPA_ID}[$i] = $value; 247 } 248 } 184 249 } 185 250 } … … 189 254 foreach my $colName (@unique_fields) { 190 255 my %counter = (); 256 my $i = 0; 191 257 foreach my $row (@{ $colData{$colName} }) { 192 258 $counter{$row} = 1; … … 232 298 my $msg; # Message to output 233 299 Astro::FITS::CFITSIO::fits_get_errstatus( $status , $msg ); 234 die "CFITSIO error: $ msg\n";300 die "CFITSIO error: $status => $msg\n"; 235 301 } 236 302 } … … 252 318 } 253 319 320 # Stolen from PStamp/Job.pm. Thanks, Bill. 321 sub mjd_to_dateobs { 322 my $mjd = shift; 323 324 my $ticks = ($mjd - 40587.0) * 86400; 325 326 my ($sec, $min, $hr, $day, $mon, $year) = gmtime($ticks); 327 328 return sprintf "'%4d-%02d-%02dT%02d:%02d:%02dZ'", $year+1900, $mon+1, $day, $hr, $min, $sec; 329 } 330 254 331 255 332
Note:
See TracChangeset
for help on using the changeset viewer.
