Changeset 24190 for trunk/ippScripts/scripts/ipp_mops_translate.pl
- Timestamp:
- May 14, 2009, 4:46:29 PM (17 years ago)
- File:
-
- 1 edited
-
trunk/ippScripts/scripts/ipp_mops_translate.pl (modified) (9 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
Note:
See TracChangeset
for help on using the changeset viewer.
