Index: trunk/tools/dlapstacks
===================================================================
--- trunk/tools/dlapstacks	(revision 33521)
+++ trunk/tools/dlapstacks	(revision 33522)
@@ -24,28 +24,27 @@
 use IPC::Cmd 0.36 qw( can_run run );
 
-
 my $ipprc = PS::IPP::Config->new();
 my $dbh = getDBHandle();
 
 my ($requested_filter, $no_display, $skycell_id, $verbose, $ra, $dec, $convolved);
-my ($singleframe, $fitToFrame, $deasin, $save_temps);
+my ($singleframe, $fitToFrame, $deasin, $save_temps, $data_group);
 
 my $temproot = '/local/ipp/tmp';
 my $outdir;
 
-
 GetOptions(
     'filter|f=s'    => \$requested_filter,  # restrict to a single filter 
-    'skycell_id|s=s'=> \$skycell_id,        # don't bother with 
+    'skycell_id|s=s'=> \$skycell_id,        # specificly sset skycell_id of interest
     'convolved|c'   => \$convolved,         # display convolved stack images instead of unconvolved
-    'fit'           => \$fitToFrame,
-    'deasin|d'      => \$deasin,
+    'fit'           => \$fitToFrame,        # scale the images to fit the ds9 frame
+    'single'        => \$singleframe,       # set ds9 to singleframe mode
+    'deasin|d'      => \$deasin,            # run ppstamp to remove the ASIN scaling from the stacks
     'label|l=s'     => \$label,             # use a different label (careful!)
-    'tess_id|t=s'   => \$tess_id,           # 
-    'outdir|o=s'    => \$outdir,
-    'no-display|n'  => \$no_display,        # don't run ds9 just print the filenames
-    'single'        => \$singleframe,
-    'save-temps'    => \$save_temps,
-    'verbose|v'     => \$verbose,
+    'data_group=s'  => \$data_group,        # select stacks by data_group
+    'tess_id|t=s'   => \$tess_id,           # set tess_id
+    'outdir|o=s'    => \$outdir,            # if deasin save de-asined images to given directory
+    'no-display|n'  => \$no_display,        # don't run ds9 just print the nebulous pathnames
+    'save-temps'    => \$save_temps,        # save temporary files
+    'verbose|v'     => \$verbose,           # print lots of information
 ) or pod2usage( 2 );
 
@@ -58,4 +57,12 @@
 pod2usage( -msg => 'if ra is suplied <dec> must be supplied as well', -exitval => 2)
     if (defined $ra and ! defined $dec);
+
+if ($skycell_id and !($skycell_id =~ /^skycell/)) {
+    if (length($skycell_id) < 3) {
+        # two character skycell_id NM should probably be 0NM
+        $skycell_id = "0$skycell_id";
+    }
+    $skycell_id = "skycell.$skycell_id";
+}
 
 my $missing_tools;
@@ -78,6 +85,15 @@
 # don't allow MD label unless it's a refstack.
 # Nightly stacks have too many images
-if (($label =~ /MD/) and !(uc($label) =~ /REF/)) {
-    die "cannot use this program for label $label because we will probably too many matches. Talk to Bill\n";
+if (($data_group && ($data_group =~ /MD/)) or ($label =~ /^MD/)) {
+    unless ($data_group or uc($label) =~ /REF/) {
+        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";
+    }
+    # if tess_id hasn't been set to an MD field, guess that it's a V3 stack
+    unless ($tess_id =~ /MD/) {
+        my $tag = $data_group ? $data_group : $label;
+        my $field = substr $tag, 0, 4;
+        $tess_id = "$field.V3";
+        print "Setting tess_id to $tess_id\n";
+    }
 }
 
@@ -100,5 +116,10 @@
 my $query = "SELECT skycell_id, filter, stack_id, quality, state, path_base
     FROM stackRun join stackSumSkyfile USING(stack_id) 
-    WHERE stackRun.state ='full' AND label = '$label' AND skycell_id = ?";
+    WHERE stackRun.state ='full' AND skycell_id = ?";
+if ($data_group) {
+    $query .= " AND data_group = '$data_group'";
+} else {
+    $query .= " AND label = '$label'";
+}
 
 if ($requested_filter) {
@@ -281,2 +302,93 @@
 }
 
+__END__
+
+=pod
+=head1 NAME
+dlapstacks - display stack images from the PS1 first Grand Reprocessing (LAP)
+
+=head1 SYNOPSIS
+    
+    dlapstacks <ra> <decl> [options]
+    dlapstacks --skycell_id <skycell_id> [<ra> <decl>] [options]
+
+
+=head1 DESCRIPTION
+
+Query the gpc1 database to find stacks containing a given RA and DEC or for a specific skycell_id.
+Display the images using ds9. If coordinates are supplied the display is centered there.
+
+=head1 OPTIONS
+
+=over 4
+
+=item * -s, --skycell_id <skycell_id>
+
+The skycell_id to display. Note if skycell_id does not begin with "skycell", "skycell" is prepended to the supplied value before querying the database.
+
+
+=item * <ra>   
+
+Right Ascension. Format: decimal degrees or HH:MM:SS
+Required unless skycell_id is supplied
+
+
+=item * <decl>
+
+Declination. Format: decimal degrees or DD:MM:SS 
+Required unless skycell_id is supplied
+
+=item * -f, --filter <filter>
+
+Only display images for the given filter. The SQL wild card character '%' is appended to single character values.
+
+=item * -c, --convolved
+
+Display convolved stacks. Default is to display unconvolved stack images.
+
+=item * --fit
+
+Instruct ds9 to fit the image to the frame.
+
+=item * --single
+
+Set ds9 to single frame mode
+
+=item * -d, --deasin
+
+Display images tha have had the ASIN scaling removed.
+
+=item *-o, --outdir <outdir>
+
+If --deasin is selected the unscaled images are preserved in the provided directory.
+The directory created if it does not exist.
+
+=item * -l, --label <label>
+
+Chose stacks with the provided label. The default is LAP.ThreePi.20110809.
+NOTE: If the label begins with MD and --data_group is not provided the label must contain "ref" or "REF". This prevents
+users from selecting the hundreds of nightly stack images. 
+
+=item * --data_group <data_group>
+
+Select stacks with the provided data_group. By default stacks are selected by label.
+
+
+=item * -t, --tess_id <tess_id>
+
+Set the tessellation that skycells are selected from. Default RINGS.V3. Note if data_group or label begins with MDnn tess_id is set to MDnn.V3 by
+default.
+
+=item * -n, --no-display
+
+Look up the stack images using the provided parameters but rather than displaying them with ds9 print out some information about the stacks.
+
+=item * --save-temps
+
+For debugging deasin mode do not delete temporary images. Ignored if --deasin is not supplied or if --outdir is supplied.
+
+=item * -v, --verbose
+
+Print information about the progress of the program.
+
+=cut
