Changeset 24190
- Timestamp:
- May 14, 2009, 4:46:29 PM (17 years ago)
- Location:
- trunk/ippScripts/scripts
- Files:
-
- 2 edited
-
ipp_mops_translate.pl (modified) (9 diffs)
-
ipp_serial_mops.pl (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ippScripts/scripts/ipp_mops_translate.pl
r21201 r24190 15 15 16 16 use constant EXTNAME => 'MOPS_TRANSIENT_DETECTIONS'; # Extension name for output table 17 use constant POSITION_ERROR => 0.2 / 3600; # Error in positions, degrees18 17 use constant ZERO_POINT => 25; # Magnitude zero point 19 use constant MAG_ERROR => 0.1; # Error in magnitudes20 18 use constant OBSERVATORY_CODE => 566; # IAU Observatory Code 21 19 use constant FAKE_LIMITING_MAG => 23.0; # Fake limiting magnitude to report … … 25 23 $extname, # Name of extension containing photometry 26 24 $skycell, # Skycell file with WCS 27 $diff_image_id, # difference image id28 25 $output, # Name of output file 29 26 $save_temps, # Save temporary files? … … 33 30 'input=s' => \$input, 34 31 'extname=s' => \$extname, 35 'skycell=s' => \$skycell,36 32 'output=s' => \$output, 37 'diff_image_id=s' => \$diff_image_id,38 33 'save-temps' => \$save_temps, 39 34 ) or pod2usage( 2 ); 40 35 41 36 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV; 42 pod2usage( -msg => "Required options: --input --extname -- skycell --diff_image_id --output",37 pod2usage( -msg => "Required options: --input --extname --output", 43 38 -exitval => 3) 44 39 unless defined $input 45 40 and defined $extname 46 and defined $skycell47 and defined $diff_image_id,48 41 and defined $output; 49 42 … … 64 57 # Header translation table 65 58 my $headers = { 59 # IPP name => MOPS name, type, comment 66 60 'FPA.RA' => { name => 'RA', type => TSTRING, comment => 'Right ascension of boresight'}, 67 61 'FPA.DEC' => { name => 'DEC', type => TSTRING, comment => 'Declination of boresight' }, 68 'FPA.OBS' => { name => 'FPA_ID', type => TSTRING, comment => 'Exposure identifier' },69 62 'FPA.FILTER' => { name => 'FILTER', type => TSTRING, comment => 'Filter name' }, 70 63 'EXPTIME' => { name => 'EXPTIME', type => TDOUBLE, comment => 'Exposure time' }, … … 72 65 'FPA.ALT' => { name => 'TEL_ALT', type => TDOUBLE, comment => 'Telescope altitude' }, 73 66 'FPA.AZ' => { name => 'TEL_AZ', type => TDOUBLE, comment => 'Telescope azimuth' }, 74 }; 75 76 77 # Read the skycell header for the WCS 78 my $skycellResolved = $ipprc->file_resolve( $skycell ); 79 my $status = 0; 80 my $skyfile_fits = Astro::FITS::CFITSIO::open_file( $skycellResolved, READONLY, $status ); 81 die("failed to open skycell file: $skycellResolved: $status") if $status; 82 83 my $wcsheader; 84 ($wcsheader, $status) = Astro::FITS::CFITSIO::fits_read_header( $skyfile_fits ); 85 die("Unable to read skycell header: $status") if $status; 86 87 # Get the useful header keywords 88 my $naxis1 = $$wcsheader{'NAXIS1'}; 89 my $naxis2; 90 if ($naxis1) { 91 $naxis2 = $$wcsheader{'NAXIS2'} or die("Can't find NAXIS2"); 92 } else { 93 # if the skyfile is compressed then the WCS won't be in the primary header 94 my $hdutype; 95 $skyfile_fits->movrel_hdu(1, $hdutype, $status); 96 die("Unable to movrel_hdu: $status") if $status; 97 98 ($wcsheader, $status) = Astro::FITS::CFITSIO::fits_read_header( $skyfile_fits ); 99 die("Unable to read extension header: $status") if $status; 100 my $xtension = $$wcsheader{'XTENSION'} or die("Can't find XTENSION"); 101 die("XTENSION found: $xtension") if $xtension ne "'BINTABLE'"; 102 $naxis1 = $$wcsheader{'ZNAXIS1'} or die("Can't find ZNAXIS1"); 103 $naxis2 = $$wcsheader{'ZNAXIS2'} or die("Can't find ZNAXIS2"); 104 } 105 106 my $ctype1 = $$wcsheader{'CTYPE1'} or die("Can't find CTYPE1"); 107 my $ctype2 = $$wcsheader{'CTYPE2'} or die("Can't find CTYPE2"); 108 my $cdelt1 = $$wcsheader{'CDELT1'} or die("Can't find CDELT1"); 109 my $cdelt2 = $$wcsheader{'CDELT2'} or die("Can't find CDELT2"); 110 my $crval1 = deg2rad($$wcsheader{'CRVAL1'}) or die("Can't find CRVAL1"); 111 my $crval2 = deg2rad($$wcsheader{'CRVAL2'}) or die("Can't find CRVAL2"); 112 my $crpix1 = $$wcsheader{'CRPIX1'} or die("Can't find CRPIX1"); 113 my $crpix2 = $$wcsheader{'CRPIX2'} or die("Can't find CRPIX2"); 114 my $pc11 = $$wcsheader{'PC001001'} or die("Can't find PC001001"); 115 my $pc12 = $$wcsheader{'PC001002'} or die("Can't find PC001002"); 116 my $pc21 = $$wcsheader{'PC002001'} or die("Can't find PC002001"); 117 my $pc22 = $$wcsheader{'PC002002'} or die("Can't find PC002002"); 118 my $crota1 = $$wcsheader{'CROTA1'}; 119 my $crota2 = $$wcsheader{'CROTA2'}; 120 121 die("Unexpected projection: $ctype1 and $ctype2.") unless $ctype1 =~ /^\'RA---TAN\s*\'$/ and 122 $ctype2 =~ /^\'DEC--TAN\s*\'$/; 123 die("Can't determine size of skycell ($naxis1,$naxis2)") unless $naxis1 > 0 and $naxis2 > 0; 124 die("Can't determine scale of skycell ($cdelt1,$cdelt2)") if not defined $cdelt1 or $cdelt1 == 0 or 125 not defined $cdelt2 or $cdelt2 == 0; 126 die("We don't know how to handle rotations ($crota1,$crota2)") if defined $crota1 or defined $crota2; 127 128 129 # Read the input table 130 my $inputResolved = $ipprc->file_resolve($input); 67 'IMAGEID' => { name => 'DIFFIMID', type => TINT, comment => 'Difference image identifier'}, 68 'FPA.OBS' => { name => 'FPA_ID', type => TSTRING, comment => 'Exposure identifier' }, 69 }; 70 71 72 # Read header 73 my $inputResolved = $ipprc->file_resolve($input); # Resolved filename 74 my $status = 0; # CFITSIO status 131 75 my $inFits = Astro::FITS::CFITSIO::open_file( $inputResolved, READONLY, $status ); # FITS file handle 76 die("failed to open input file: $inputResolved: $status") if $status; 132 77 my $inHeader = $inFits->read_header(); # Header for input 133 78 check_fitsio($status); 79 80 # Plate scales 81 my $cdelt1 = $$inHeader{'CDELT1'} or die("Can't find CDELT1"); 82 my $cdelt2 = $$inHeader{'CDELT2'} or die("Can't find CDELT2"); 83 84 # Read table data 134 85 $inFits->movnam_hdu(BINARY_TBL, $extname, 0, $status) and check_fitsio($status); 135 86 my $numRows; # Number of rows in table 136 87 $inFits->get_num_rows($numRows, $status) and check_fitsio($status); 137 88 138 my ($xCol, $yCol, $magCol, $ensCol); # Column numbers for x and y 139 $inFits->get_colnum(0, 'X_PSF', $xCol, $status) and check_fitsio($status); 140 $inFits->get_colnum(0, 'Y_PSF', $yCol, $status) and check_fitsio($status); 141 $inFits->get_colnum(0, 'PSF_INST_MAG', $magCol, $status) and check_fitsio($status); 142 $inFits->get_colnum(0, 'EXT_NSIGMA', $ensCol, $status) and check_fitsio($status); 143 144 my ($xType, $yType, $magType, $ensType); # Column types 145 $inFits->get_coltype($xCol, $xType, undef, undef, $status) and check_fitsio($status); 146 $inFits->get_coltype($yCol, $yType, undef, undef, $status) and check_fitsio($status); 147 $inFits->get_coltype($magCol, $magType, undef, undef, $status) and check_fitsio($status); 148 $inFits->get_coltype($ensCol, $ensType, undef, undef, $status) and check_fitsio($status); 149 150 my ($x, $y, $mag, $ens); # Sources arrays 151 $inFits->read_col($xType, $xCol, 1, 1, $numRows, 0, $x, undef, $status) and check_fitsio($status); 152 $inFits->read_col($yType, $yCol, 1, 1, $numRows, 0, $y, undef, $status) and check_fitsio($status); 153 $inFits->read_col($magType, $magCol, 1, 1, $numRows, 0, $mag, undef, $status) and check_fitsio($status); 154 $inFits->read_col($ensType, $ensCol, 1, 1, $numRows, 0, $ens, undef, $status) and check_fitsio($status); 89 my $ra = column($inFits, 'RA_PSF', $numRows); # Right Ascension, degrees 90 my $dec = column($inFits, 'DEC_PSF', $numRows); # Declination, degrees 91 my $mag = column($inFits, 'PSF_INST_MAG', $numRows); # Magnitude 92 my $magErr = column($inFits, 'PSF_INST_MAG_SIG', $numRows); # Magnitude error 93 my $ens = column($inFits, 'EXT_NSIGMA', $numRows); # Significance of extension 94 my $xErr = column($inFits, 'X_PSF_SIG', $numRows); # Error in x position, pixels 95 my $yErr = column($inFits, 'Y_PSF_SIG', $numRows); # Error in y position, pixels 96 my $scale = column($inFits, 'PLTSCALE', $numRows); # Plate scale, arcsec/pixel 97 my $angle = column($inFits, 'POSANGLE', $numRows); # Position angle, degrees 155 98 156 99 $inFits->close_file( $status ); 100 101 my ($raErr, $decErr); # Error in ra, dec 102 for (my $i = 0; $i < $numRows; $i++) { 103 my $cosPA = cos($$angle[$i]); 104 my $sinPA = sin($$angle[$i]); 105 106 # XXX Not sure about the transformation here --- check 107 $$raErr[$i] = $$scale[$i] * ($cosPA * $xErr + $sinPA * $yErr) / 3600; 108 $$decErr[$i] = $$scale[$i] * ($sinPA * $xErr - $cosPA * $yErr) / 3600; 109 } 110 157 111 158 112 # Parse the list of columns … … 169 123 # Convert the input data into the output formats 170 124 for (my $i = 0; $i < $numRows; $i++) { 171 my ($ra, $dec) = pixels_to_sky($$x[$i], $$y[$i]); # Sky coordinates172 173 125 push @{$colData{'RA_DEG'}}, $ra; 174 126 push @{$colData{'DEC_DEG'}}, $dec; 175 push @{$colData{'RA_SIG'}}, POSITION_ERROR;176 push @{$colData{'DEC_SIG'}}, POSITION_ERROR;127 push @{$colData{'RA_SIG'}}, $raErr; 128 push @{$colData{'DEC_SIG'}}, $decErr; 177 129 push @{$colData{'FLUX'}}, $$mag[$i] + ZERO_POINT; 178 push @{$colData{'FLUX_SIG'}}, MAG_ERROR;130 push @{$colData{'FLUX_SIG'}}, $magErr; 179 131 push @{$colData{'ANG'}}, 0.0; 180 132 push @{$colData{'ANG_SIG'}}, 0.0; … … 211 163 } 212 164 213 # set DIFFIMID214 # XXX TODO: we could put the diff_image_id in the .cmf file and get it from there215 $outFits->write_key( TLONGLONG, 'DIFFIMID', $diff_image_id, 'Difference image identifier', $status);216 check_fitsio( $status );217 218 165 # Adjust the time from start to mid-point 219 166 { … … 260 207 261 208 209 # Read a column specified by name 210 sub column 211 { 212 my $fits = shift; # FITS file 213 my $name = shift; # Name of column 214 my $rows = shift; # Number of rows 215 my $status = 0; # Status of CFITSIO 216 217 my $num; # Column number 218 $fits->get_colnum(0, $name, $num, $status) and check_fitsio($status); 219 my $type; # Type of column 220 $inFits->get_coltype($num, $type, undef, undef, $status) and check_fitsio($status); 221 my $data; # Array with data 222 $inFits->read_col($type, $num, 1, 1, $rows, 0, $data, undef, $status) and check_fitsio($status); 223 224 return $data; 225 } 226 227 228 262 229 263 230 # From Astro::FITS::CFITSIO demo … … 273 240 } 274 241 275 # Convert pixel coordinates to sky coordinates276 sub pixels_to_sky277 {278 my ($x, $y) = @_; # Coordinates279 280 # Pixel coordinate relative to reference281 $x -= $crpix1;282 $y -= $crpix2;283 284 # Coordinates on tangent plane285 my $xi = $pc11 * ($x) + $pc12 * ($y);286 my $eta = $pc21 * ($x) + $pc22 * ($y);287 $xi *= $cdelt1;288 $eta *= $cdelt2;289 290 # Coordinates on rotated celestial sphere291 my $phi = atan2($eta,$xi) + pi/2;292 my $theta = atan(180 / pi / sqrt($xi**2 + $eta**2));293 294 # Coordinates on celestial sphere295 my $ra = $crval1 + atan2(cos($theta) * sin($phi),296 sin($theta) * cos($crval2) + cos($theta) * sin($crval2) * cos($phi));297 my $dec = asin(sin($theta) * sin($crval2) - cos($theta) * cos($crval2) * cos($phi));298 299 $ra = rad2deg($ra);300 $dec = rad2deg($dec);301 302 $ra += 360 if $ra < 0;303 $dec += 360 if $dec < 0;304 $ra -= 360 if $ra >= 360;305 $dec -= 360 if $dec >= 360;306 307 return ($ra, $dec);308 }309 310 311 242 __END__ 312 243 -
trunk/ippScripts/scripts/ipp_serial_mops.pl
r19717 r24190 71 71 die "Unable to connect to database: $DBI::errstr"; 72 72 73 my $sql = "SELECT exp_id, diff_id, skycell_id, warp_id, diffSkyfile.path_base AS diff_path_base, warpSkyfile.path_base AS warp_path_base FROM diffSkyfile JOIN diffRun using(diff_id) JOIN diffInputSkyfile USING(diff_id,tess_id,skycell_id) JOIN warpRun USING(warp_id,tess_id) JOIN warpSkyfile USING(warp_id,skycell_id,tess_id) JOIN fakeRun using(fake_id) JOIN camRun USING(cam_id) JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id) WHERE diffSkyfile.fault = 0 AND rawExp.camera = \'$camera\'"; 74 $sql .= " AND diffRun.label = \'$label\'" if defined $label; 75 $sql .= ';'; 73 my $where_label = defined $label ? "AND label = '$label'" : ""; # WHERE for label 76 74 77 my $rows = $db->selectall_arrayref( $sql, { Slice => {} } ) or die "Unable to execute SQL: $DBI::errstr"; 78 $db->disconnect; 75 my $sql = " 76 -- Get a list of exposures on which magic may be performed 77 SELECT 78 exp_id, 79 MAX(diffWarps.diff_id) AS diff_id, 80 -- The following trick pulls out the 'inverse' value for the maximum diff_id 81 CONVERT(SUBSTRING_INDEX(GROUP_CONCAT(diffWarps.inverse ORDER BY diffWarps.diff_id), ',', 1), UNSIGNED) AS inverse 82 FROM ( 83 -- Forward diffs 84 SELECT 85 diffRun.diff_id, 86 warp1 AS warp_id, 87 0 AS inverse 88 FROM diffRun 89 JOIN diffInputSkyfile USING(diff_id) 90 WHERE diffInputSkyfile.warp1 IS NOT NULL 91 AND diffRun.exposure = 1 92 $where_label 93 UNION 94 -- Backward diffs 95 SELECT 96 diffRun.diff_id, 97 warp2 AS warp_id, 98 1 AS inverse 99 FROM diffRun 100 JOIN diffInputSkyfile USING(diff_id) 101 WHERE diffInputSkyfile.warp2 IS NOT NULL 102 AND diffRun.exposure = 1 103 AND diffRun.bothways = 1 104 $where_label 105 ) AS diffWarps 106 JOIN warpRun USING(warp_id) 107 JOIN fakeRun USING(fake_id) 108 JOIN camRun USING(cam_id) 109 JOIN chipRun USING(chip_id) 110 WHERE diffRun.state = 'full' 111 AND rawExp.camera = '$camera' 112 GROUP BY exp_id;"; 79 113 80 print "Selected " . scalar @$rows . " rows.\n"; 114 my $diffs = $db->selectall_arrayref( $sql, { Slice => {} } ) or die "Unable to execute SQL: $DBI::errstr"; 115 116 print "Selected " . scalar @$diffs . " rows.\n"; 81 117 82 118 $ipprc->outroot_prepare( $outroot ); … … 84 120 my ($dsFile, $dsName) = tempfile( "$outrootResolved.dslist.XXXX", UNLINK => !$save_temps); 85 121 86 foreach my $row ( @$rows ) { 87 my $exp_id = $row->{exp_id}; 88 my $diff_id = $row->{diff_id}; 89 my $skycell_id = $row->{skycell_id}; 90 my $warp_id = $row->{warp_id}; 91 my $diff_base = $row->{diff_path_base}; 92 my $warp_base = $row->{warp_path_base}; 122 foreach my $diff ( @$diffs ) { 123 my $exp_id = $diff->{exp_id}; 124 my $diff_id = $diff->{diff_id}; 125 my $inverse = $diff->{inverse}; 93 126 94 my $input = $ipprc->filename("PSPHOT.OUT.CMF.MEF", $diff_base); 95 die "Can't find $input\n" unless $ipprc->file_exists($input); 96 $input = $ipprc->file_resolve($input); 127 my $sql = "SELECT * FROM diffSkyfile WHERE diff_id = $diff_id;"; 128 my $skycells = $db->selectall_arrayref( $sql, { Slice => {} } ) or die "Unable to execute SQL: $DBI::errstr"; 97 129 98 my $skycell = $ipprc->filename("SKYCELL.TEMPLATE", $warp_base);99 die "Can't find $skycell\n" unless $ipprc->file_exists($skycell);100 $skycell = $ipprc->file_resolve($skycell);130 foreach my $skycell ( @$skycells ) { 131 my $skycell_id = $diff->{skycell_id}; 132 my $path_base = $diff->{path_base}; 101 133 102 my $output = caturi( $outroot, "mops.$exp_id.$skycell_id.$diff_id.fits" ); 103 $output = $ipprc->file_resolve($output); 134 my $sources = $inverse ? "PPSUB.INVERSE.SOURCES" : "PPSUB.OUTPUT.SOURCES"; 104 135 105 unless ($no_op) { 106 $ipprc->file_prepare($output); 107 my $command = "$mops --input $input --extname SkyChip.psf --skycell $skycell --output $output"; 108 my $success = run( command => $command, verbose => $verbose ); 109 die "Couldn't translate $input\n" unless $success; 136 my $input = $ipprc->filename($sources, $path_base); 137 die "Can't find $input\n" unless $ipprc->file_exists($input); 138 $input = $ipprc->file_resolve($input); 139 140 my $output = caturi( $outroot, "mops.$exp_id.$skycell_id.$diff_id.fits" ); 141 $output = $ipprc->file_resolve($output); 142 143 unless ($no_op) { 144 $ipprc->file_prepare($output); 145 my $command = "$mops --input $input --extname SkyChip.psf --output $output"; 146 my $success = run( command => $command, verbose => $verbose ); 147 die "Couldn't translate $input\n" unless $success; 148 } 149 150 # format: filename|filesize|md5sum|filetype| 151 # note: since we omit filesize and md5sum, dsreg will calculate them 152 print $dsFile "${output}|||ipp-mops|\n"; 110 153 } 111 112 # format: filename|filesize|md5sum|filetype|113 # note: since we omit filesize and md5sum, dsreg will calculate them114 print $dsFile "${output}|||ipp-mops|\n";115 154 } 116 155 close $dsFile; 156 $db->disconnect; 117 157 118 158 # Register new files with the data store
Note:
See TracChangeset
for help on using the changeset viewer.
