IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 7, 2009, 4:08:25 PM (17 years ago)
Author:
Paul Price
Message:

Merging trunk (r25026) to get up-to-date on old branch.

Location:
branches/pap
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/pap

  • branches/pap/ippScripts/scripts/ipp_mops_translate.pl

    r21201 r25027  
    1515
    1616use constant EXTNAME => 'MOPS_TRANSIENT_DETECTIONS'; # Extension name for output table
    17 use constant POSITION_ERROR => 0.2 / 3600; # Error in positions, degrees
    1817use constant ZERO_POINT => 25;  # Magnitude zero point
    19 use constant MAG_ERROR => 0.1;  # Error in magnitudes
    2018use constant OBSERVATORY_CODE => 566; # IAU Observatory Code
    2119use constant FAKE_LIMITING_MAG => 23.0; # Fake limiting magnitude to report
     
    2523     $extname,                  # Name of extension containing photometry
    2624     $skycell,                  # Skycell file with WCS
    27      $diff_image_id,            # difference image id
    2825     $output,                   # Name of output file
    2926     $save_temps,               # Save temporary files?
     
    3330           'input=s'    => \$input,
    3431           'extname=s'  => \$extname,
    35            'skycell=s'  => \$skycell,
    3632           'output=s'   => \$output,
    37            'diff_image_id=s' => \$diff_image_id,
    3833           'save-temps' => \$save_temps,
    3934) or pod2usage( 2 );
    4035
    4136pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
    42 pod2usage( -msg => "Required options: --input --extname --skycell --diff_image_id --output",
     37pod2usage( -msg => "Required options: --input --extname --output",
    4338           -exitval => 3)
    4439    unless defined $input
    4540    and defined $extname
    46     and defined $skycell
    47     and defined $diff_image_id,
    4841    and defined $output;
    4942
     
    6457# Header translation table
    6558my $headers = {
     59    # IPP name     => MOPS name, type, comment
    6660    'FPA.RA'       => { name => 'RA',       type => TSTRING, comment => 'Right ascension of boresight'},
    6761    'FPA.DEC'      => { name => 'DEC',      type => TSTRING, comment => 'Declination of boresight' },
    68     'FPA.OBS'      => { name => 'FPA_ID',   type => TSTRING, comment => 'Exposure identifier' },
    6962    'FPA.FILTER'   => { name => 'FILTER',   type => TSTRING, comment => 'Filter name' },
    7063    'EXPTIME'      => { name => 'EXPTIME',  type => TDOUBLE, comment => 'Exposure time' },
     
    7265    'FPA.ALT'      => { name => 'TEL_ALT',  type => TDOUBLE, comment => 'Telescope altitude' },
    7366    '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
     73my $inputResolved = $ipprc->file_resolve($input); # Resolved filename
     74my $status = 0;                 # CFITSIO status
    13175my $inFits = Astro::FITS::CFITSIO::open_file( $inputResolved, READONLY, $status ); # FITS file handle
     76die("failed to open input file: $inputResolved: $status") if $status;
    13277my $inHeader = $inFits->read_header(); # Header for input
    13378check_fitsio($status);
     79
     80# Read table data
    13481$inFits->movnam_hdu(BINARY_TBL, $extname, 0, $status) and check_fitsio($status);
    13582my $numRows;                    # Number of rows in table
    13683$inFits->get_num_rows($numRows, $status) and check_fitsio($status);
    13784
    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);
     85my $ra = column($inFits, 'RA_PSF', $numRows); # Right Ascension, degrees
     86my $dec = column($inFits, 'DEC_PSF', $numRows); # Declination, degrees
     87my $mag = column($inFits, 'PSF_INST_MAG', $numRows); # Magnitude
     88my $magErr = column($inFits, 'PSF_INST_MAG_SIG', $numRows); # Magnitude error
     89my $ens = column($inFits, 'EXT_NSIGMA', $numRows); # Significance of extension
     90my $xErr = column($inFits, 'X_PSF_SIG', $numRows); # Error in x position, pixels
     91my $yErr = column($inFits, 'Y_PSF_SIG', $numRows); # Error in y position, pixels
     92my $scale = column($inFits, 'PLTSCALE', $numRows); # Plate scale, arcsec/pixel
     93my $angle = column($inFits, 'POSANGLE', $numRows); # Position angle, degrees
    15594
    15695$inFits->close_file( $status );
     96
     97my ($raErr, $decErr);           # Error in ra, dec
     98for (my $i = 0; $i < $numRows; $i++) {
     99    my $cosPA = cos($$angle[$i]);
     100    my $sinPA = sin($$angle[$i]);
     101
     102    # XXX Not sure about the transformation here --- check
     103    $$raErr[$i] = $$scale[$i] * ($cosPA * $xErr + $sinPA * $yErr) / 3600;
     104    $$decErr[$i] = $$scale[$i] * ($sinPA * $xErr - $cosPA * $yErr) / 3600;
     105}
     106
     107# Plate scales
     108#my $cdelt1 = $$inHeader{'CDELT1'} or die("Can't find CDELT1");
     109#my $cdelt2 = $$inHeader{'CDELT2'} or die("Can't find CDELT2");
     110### XXX WCS wasn't being set in inverse diffs, but it's available elsewhere
     111my $cdelt1 = $$scale[0] / 3600;
     112my $cdelt2 = $$scale[1] / 3600;
    157113
    158114# Parse the list of columns
     
    169125# Convert the input data into the output formats
    170126for (my $i = 0; $i < $numRows; $i++) {
    171     my ($ra, $dec) = pixels_to_sky($$x[$i], $$y[$i]); # Sky coordinates
    172 
    173127    push @{$colData{'RA_DEG'}}, $ra;
    174128    push @{$colData{'DEC_DEG'}}, $dec;
    175     push @{$colData{'RA_SIG'}}, POSITION_ERROR;
    176     push @{$colData{'DEC_SIG'}}, POSITION_ERROR;
     129    push @{$colData{'RA_SIG'}}, $raErr;
     130    push @{$colData{'DEC_SIG'}}, $decErr;
    177131    push @{$colData{'FLUX'}}, $$mag[$i] + ZERO_POINT;
    178     push @{$colData{'FLUX_SIG'}}, MAG_ERROR;
     132    push @{$colData{'FLUX_SIG'}}, $magErr;
    179133    push @{$colData{'ANG'}}, 0.0;
    180134    push @{$colData{'ANG_SIG'}}, 0.0;
     
    211165}
    212166
    213 # set DIFFIMID
    214 # XXX TODO: we could put the diff_image_id in the .cmf file and get it from there
    215 $outFits->write_key( TLONGLONG, 'DIFFIMID', $diff_image_id, 'Difference image identifier', $status);
    216 check_fitsio( $status );
    217 
    218167# Adjust the time from start to mid-point
    219168{
     
    260209
    261210
     211# Read a column specified by name
     212sub column
     213{
     214    my $fits = shift;           # FITS file
     215    my $name = shift;           # Name of column
     216    my $rows = shift;           # Number of rows
     217    my $status = 0;             # Status of CFITSIO
     218
     219    my $num;                    # Column number
     220    $fits->get_colnum(0, $name, $num, $status) and check_fitsio($status);
     221    my $type;                   # Type of column
     222    $inFits->get_coltype($num, $type, undef, undef, $status) and check_fitsio($status);
     223    my $data;                   # Array with data
     224    $inFits->read_col($type, $num, 1, 1, $rows, 0, $data, undef, $status) and check_fitsio($status);
     225
     226    return $data;
     227}
     228
     229
     230
    262231
    263232# From Astro::FITS::CFITSIO demo
     
    273242}
    274243
    275 # Convert pixel coordinates to sky coordinates
    276 sub pixels_to_sky
    277 {
    278     my ($x, $y) = @_;           # Coordinates
    279 
    280     # Pixel coordinate relative to reference
    281     $x -= $crpix1;
    282     $y -= $crpix2;
    283 
    284     # Coordinates on tangent plane
    285     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 sphere
    291     my $phi = atan2($eta,$xi) + pi/2;
    292     my $theta = atan(180 / pi / sqrt($xi**2 + $eta**2));
    293 
    294     # Coordinates on celestial sphere
    295     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 
    311244__END__
    312245
Note: See TracChangeset for help on using the changeset viewer.