IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 19, 2012, 5:24:19 PM (14 years ago)
Author:
mhuber
Message:

merging latest r34040 trunk changes to branch

Location:
branches/meh_branches/ppstack_test
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/meh_branches/ppstack_test

  • branches/meh_branches/ppstack_test/tools/dlapstacks

    r33415 r34041  
    44use warnings;
    55
    6 # given an RA and DEC lookup find the LAP stack images
    7 
    8 # XXX: consider making these options these could be made options I guess
    9 
     6# given an RA and DEC or a skycell_id find the LAP stack images containing them
     7# and display using ds9
     8
     9# XXX: consider accepting a string of parameters to be passed to ds9
     10# add --fit set ds9 to fit to frame
     11# XXX if label is supplied by the user and it is like MD%
     12# guess at the tess_id
     13# allow mask and variance images to be displayed as well
     14
     15my $label   = 'LAP.ThreePi.20110809';
    1016my $tess_id = 'RINGS.V3';
    11 my $label = 'LAP.ThreePi.20110809';
    1217
    1318use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
     
    1520use DBI;
    1621use PS::IPP::Config 1.01 qw( :standard );
    17 
    18 my $ipprc =  PS::IPP::Config->new();
     22use File::Temp qw( tempfile tempdir );
     23use File::Basename;
     24use IPC::Cmd 0.36 qw( can_run run );
     25
     26my $ipprc = PS::IPP::Config->new();
    1927my $dbh = getDBHandle();
    2028
    21 
    2229my ($requested_filter, $no_display, $skycell_id, $verbose, $ra, $dec, $convolved);
    23 
     30my ($singleframe, $fitToFrame, $deasin, $save_temps, $data_group);
     31
     32# XXX: get this from the ipprc
     33my $temproot = '/local/ipp/tmp';
     34my $outdir;
    2435
    2536GetOptions(
    26     'filter|f=s'    => \$requested_filter,
    27     'convolved'     => \$convolved,
    28     'no-display|n'  => \$no_display,
    29     'skycell_id=s'  => \$skycell_id,
    30     'verbose|v'     => \$verbose,
     37    'filter|f=s'    => \$requested_filter,  # restrict to a single filter
     38    'skycell_id|s=s'=> \$skycell_id,        # specificly sset skycell_id of interest
     39    'convolved|c'   => \$convolved,         # display convolved stack images instead of unconvolved
     40    'fit'           => \$fitToFrame,        # scale the images to fit the ds9 frame
     41    'single'        => \$singleframe,       # set ds9 to singleframe mode
     42    'deasin|d'      => \$deasin,            # run ppstamp to remove the ASIN scaling from the stacks
     43    'label|l=s'     => \$label,             # use a different label (careful!)
     44    'data_group=s'  => \$data_group,        # select stacks by data_group
     45    'tess_id|t=s'   => \$tess_id,           # set tess_id
     46    'outdir|o=s'    => \$outdir,            # if deasin save de-asined images to given directory
     47    'no-display|n'  => \$no_display,        # don't run ds9 just print the nebulous pathnames
     48    'save-temps'    => \$save_temps,        # save temporary files
     49    'verbose|v'     => \$verbose,           # print lots of information
    3150) or pod2usage( 2 );
    3251
     
    3655$ra = shift;
    3756$dec = shift;
     57
     58pod2usage( -msg => 'if ra is suplied <dec> must be supplied as well', -exitval => 2)
     59    if (defined $ra and ! defined $dec);
     60
     61if ($skycell_id and !($skycell_id =~ /^skycell/)) {
     62    if (length($skycell_id) < 3) {
     63        # two character skycell_id NM should probably be 0NM
     64        $skycell_id = "0$skycell_id";
     65    }
     66    $skycell_id = "skycell.$skycell_id";
     67}
     68
     69my $missing_tools;
     70my $ppstamp;
     71my $ds9;
     72if ($deasin) {
     73    $ppstamp = can_run('ppstamp') or (warn "Can't find ppstamp") and $missing_tools = 1;
     74}
     75if (!$no_display) {
     76    $ds9 = can_run('ds9') or (warn "Can't find ds9") and $missing_tools = 1;
     77}
     78if ($missing_tools) {
     79    # warn("Can't find required tools.");
     80    exit($PS_EXIT_CONFIG_ERROR);
     81}
    3882
    3983my $extension;
     
    4488}
    4589
    46 # run whichimage to find a list of skycells containing the given coordinates
    47 my $whichimage_output = `whichimage --listchipcoords --tess_id $tess_id $ra $dec`;
    48 
    49 print $whichimage_output if $verbose;
    50 
    51 my @results = split "\n", $whichimage_output;
    52 
    53 die "whichimage returned no output\n" if scalar @results == 0;
    54 
    55 my $query = "SELECT skycell_id, filter, stack_id, quality, path_base
     90# don't allow MD label unless it's a refstack.
     91# Nightly stacks have too many images
     92if (($data_group && ($data_group =~ /MD/)) or ($label =~ /^MD/)) {
     93    unless ($data_group or uc($label) =~ /REF/) {
     94        die "cannot use this program for label $label without data_group because that will most likely select nightly stacks and we will match an unreasonably large number of matches. Talk to Bill\n";
     95    }
     96    # if tess_id hasn't been set to an MD field, guess that it's a V3 stack
     97    unless ($tess_id =~ /MD/) {
     98        my $tag = $data_group ? $data_group : $label;
     99        my $field = substr $tag, 0, 4;
     100        $tess_id = "$field.V3";
     101        print "Setting tess_id to $tess_id\n";
     102    }
     103}
     104
     105my $whichimage_output;
     106if (!$skycell_id or (defined $ra and defined $dec)) {
     107    # run whichimage to find a list of skycells containing the given coordinates and the
     108    # image coordinates of the requested sky coordinates
     109    $whichimage_output = `whichimage --listchipcoords --tess_id $tess_id $ra $dec`;
     110
     111    print "Output from whichimage:\n$whichimage_output\n" if $verbose;
     112
     113    my @results = split "\n", $whichimage_output;
     114
     115    die "whichimage returned no output\n" if scalar @results == 0;
     116} else {
     117    # we were supplied a skycell create a fake whichimage_output that can be parsed below
     118    $whichimage_output = "0 0 dummy $skycell_id -1 -1\n";
     119}
     120
     121my $query = "SELECT skycell_id, filter, stack_id, quality, state, path_base
    56122    FROM stackRun join stackSumSkyfile USING(stack_id)
    57     WHERE stackRun.state ='full' AND label = '$label' AND skycell_id = ?";
     123    WHERE stackRun.state ='full' AND skycell_id = ?";
     124if ($data_group) {
     125    $query .= " AND data_group = '$data_group'";
     126} else {
     127    $query .= " AND label = '$label'";
     128}
    58129
    59130if ($requested_filter) {
     
    69140
    70141# @files will contain the list of skycells found in order of skycell_id, filter.
    71 # These will be displayed in this order so that the image frames are in order of increasing bandpass
     142# The images will be displayed in this order so that the image frames are in order of increasing bandpass
    72143my @files = ();
    73144
     
    81152foreach (split "\n", $whichimage_output) {
    82153    chomp;
     154
    83155    my ($radeg, $decdeg, undef, $skycell, $x, $y) = split " ";
    84     print "$skycell $radeg $decdeg\n" if $verbose;
     156
     157    print "skycell: $skycell RA (deg): $radeg DEC (deg): $decdeg (X,Y): ($x, $y)\n" if $verbose;
     158
    85159    $stmt->execute($skycell) or die $stmt->errstr;
    86160    my $files_found = 0;
    87161    while (my $f = $stmt->fetchrow_hashref()) {
    88162        next if $f->{quality};
     163        next if $f->{state} ne 'full';
     164
    89165        my $filter = $f->{filter};
    90166        my $stack_id = $f->{stack_id};
    91167        my $path_base = $f->{path_base};
     168
    92169        # save the skycell coordinates so we can pan efficiently below
    93170        $f->{X} = $x;
    94171        $f->{Y} = $y;
    95172
    96         # Find where this filter goes in the array
     173        # Find where this filter goes in the array of files
    97174        my $i = $index{$filter};
    98175        if (!defined $i) {
     
    100177            next;
    101178        }
    102         # if multiple skycells match the coordinates bump the index
     179        # if multiple skycells match the coordinates bump the index by $num_filters
    103180        $i += $skycells * $num_filters;
    104181        $files[$i] = $f;
     
    112189
    113190if (!$total_files) {
    114     print "no files found\n";
     191    print "no matching stacks found\n";
    115192    exit 1;
    116193}
     
    118195
    119196if (!$no_display) {
    120     # estimate of image size for the case where we have 5 frames
    121     my $imagesize = 680;
    122     # my $panargs = "-pan to $ra $dec wcs fk5";
     197    my $imagesize;
     198    if ($singleframe) {
     199        $imagesize = 1024;
     200    } else {
     201        # estimate of image size for the case where we have 5 frames
     202        $imagesize = 680;
     203    }
     204    my $tempdir;
     205    if (!$outdir) {
     206        $tempdir = tempdir ("$temproot/dlapstacks.temp.XXXX", CLEANUP => !$save_temps);
     207    }
     208    if ($deasin) {
     209        if ($outdir) {
     210            print "Saving deasined files to $outdir\n" if $verbose;
     211            # user specified output directory save the files there
     212            if (!-d $outdir) {
     213                mkdir $outdir or die "failed to mkdir $outdir\n";
     214            }
     215        } else {
     216            # otherwise use a temporary directory
     217            $outdir = $tempdir;
     218            print "deasined files will be saved to $outdir\n" if $save_temps;
     219        }
     220    }
     221    # Build the ds9 argument list
    123222    my @args;
     223    my $panargs = "";
    124224    foreach my $f (@files) {
    125225        next if ! $f;
    126         my $filename = $f->{path_base} . ".$extension";
    127         my $resolved = $ipprc->file_resolve($filename);
    128         next if !$resolved;
    129         my $panargs = "-pan to $f->{X} $f->{Y}";
    130 
     226        my $pathname = $f->{path_base} . ".$extension";
     227        my $filename = $ipprc->file_resolve($pathname);
     228        next if !$filename;
     229
     230        # prefix file name with filter
     231        # this will either be the ppstamp output filename or we
     232        # will put a symlink to it in tempdir
     233        my $base = basename($pathname);
     234        my $filt = substr $f->{filter}, 0, 1;
     235        $base = "$filt.$base";
     236        if ($deasin) {
     237            # run ppstamp to undo the asin scaling. The output from ppstamp is linear F32 images
     238
     239            # drop the .fits for the output path for ppstamp
     240            my $destbase = $base;
     241            $destbase =~ s/\.fits//g;
     242
     243
     244            my $command = "$ppstamp -wholefile -file $filename $outdir/$destbase";
     245            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     246                run(command => $command, verbose => $verbose);
     247            unless ($success) {
     248                print STDERR join "", @$stderr_buf;
     249                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     250                die("Unable to perform ppstamp: $error_code\n");
     251            }
     252
     253            # new filename for ds9
     254            $filename = "$outdir/$base";
     255        } else {
     256            my $nebname = $filename;
     257            $filename = "$tempdir/$base";
     258            symlink $nebname, $filename or die "failed to symlink $nebname";
     259        }
     260
     261        if ($f->{X} > 0) {
     262            $panargs = "-pan to $f->{X} $f->{Y}";
     263        } else {
     264            $panargs = "";
     265        }
     266
     267        # The following is an attempt put the filter name in the image using a region with text a label.
    131268        my $xlabel = $f->{X} + $imagesize / 2;
    132269        my $ylabel = $f->{Y} + $imagesize / 2;
    133         # The following is an attempt put the filter name in the image using a region with text a label.
     270
    134271        # I couldn't make this work. The circle would appear but without the text
    135272        # my $label = " -regions command  'circle($xlabel,$ylabel,1)' # text={ $f->{filter} }";
     
    140277        # $panargs .= $label;
    141278
    142         push @args, ($resolved, $panargs);
    143     }
    144     my $ds9cmd = "ds9 -geom 1024x1024 @args";
     279        # I used to pan each frame but it turns out to be quicker to wait until the last image is
     280        # loaded. pan thatone and then matching the wcs for the frames.
     281        # Also that allows V0 and V3 skycells to have east to the left orientation
     282        # push @args, ($filename, $panargs);
     283        push @args, $filename;
     284    }
     285    my $ds9cmd = "$ds9 -geom 1024x1024 @args $panargs";
     286    $ds9cmd .= " -single" if $singleframe;
     287    $ds9cmd .= " -zoom to fit" if $fitToFrame;
     288    $ds9cmd .= " -match frames wcs";
     289    $ds9cmd .= " -frame first" if $singleframe;
    145290    print "$ds9cmd\n" if $verbose;
    146291    system $ds9cmd;
    147292} else {
     293    # no-display option Just print out some information
     294    print "\n" if $verbose;
     295    print "Skycell ID        stack_id filter   path_base\n";
     296    print "------------------------------------------------------------------------------------------\n";
    148297    foreach my $f (@files) {
    149298        next if !$f;
    150         print "$f->{skycell_id} $f->{stack_id} $f->{filter} $f->{path_base}\n";
     299        print "$f->{skycell_id}  $f->{stack_id}   $f->{filter} $f->{path_base}\n";
    151300    }
    152301}
     
    173322}
    174323
     324__END__
     325
     326=pod
     327=head1 NAME
     328dlapstacks - display stack images from the PS1 first Grand Reprocessing (LAP)
     329
     330=head1 SYNOPSIS
     331   
     332    dlapstacks <ra> <decl> [options]
     333    dlapstacks --skycell_id <skycell_id> [<ra> <decl>] [options]
     334
     335
     336=head1 DESCRIPTION
     337
     338Query the gpc1 database to find stacks containing a given RA and DEC or for a specific skycell_id.
     339Display the images using ds9. If coordinates are supplied the display is centered there.
     340
     341=head1 OPTIONS
     342
     343=over 4
     344
     345=item * -s, --skycell_id <skycell_id>
     346
     347The skycell_id to display. Note if skycell_id does not begin with "skycell", "skycell" is prepended to the supplied value before querying the database.
     348
     349
     350=item * <ra>   
     351
     352Right Ascension. Format: decimal degrees or HH:MM:SS
     353Required unless skycell_id is supplied
     354
     355
     356=item * <decl>
     357
     358Declination. Format: decimal degrees or DD:MM:SS
     359Required unless skycell_id is supplied
     360
     361=item * -f, --filter <filter>
     362
     363Only display images for the given filter. The SQL wild card character '%' is appended to single character values.
     364
     365=item * -c, --convolved
     366
     367Display convolved stacks. Default is to display unconvolved stack images.
     368
     369=item * --fit
     370
     371Instruct ds9 to fit the image to the frame.
     372
     373=item * --single
     374
     375Set ds9 to single frame mode
     376
     377=item * -d, --deasin
     378
     379Display images tha have had the ASIN scaling removed.
     380
     381=item *-o, --outdir <outdir>
     382
     383If --deasin is selected the unscaled images are preserved in the provided directory.
     384The directory created if it does not exist.
     385
     386=item * -l, --label <label>
     387
     388Chose stacks with the provided label. The default is LAP.ThreePi.20110809.
     389NOTE: If the label begins with MD and --data_group is not provided the label must contain "ref" or "REF". This prevents
     390users from selecting the hundreds of nightly stack images.
     391
     392=item * --data_group <data_group>
     393
     394Select stacks with the provided data_group. By default stacks are selected by label.
     395
     396
     397=item * -t, --tess_id <tess_id>
     398
     399Set the tessellation that skycells are selected from. Default is RINGS.V3
     400unless a label or data group that begins with MDnm is supplied. In that case the default is
     401MDnm.V3
     402
     403=item * -n, --no-display
     404
     405Look up the stack images using the provided parameters but rather than displaying them with ds9 print out some information about the stacks.
     406
     407=item * --save-temps
     408
     409If --deasin is supplied and --outdir is not supplied do not delete temporary images. (For debugging).
     410
     411=item * -v, --verbose
     412
     413Print information about the progress of the program.
     414
     415=cut
Note: See TracChangeset for help on using the changeset viewer.