IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 42362


Ignore:
Timestamp:
Feb 4, 2023, 7:37:56 AM (3 years ago)
Author:
eugene
Message:

merge from trunk: fixes to nightly_science.pl

Location:
branches/eam_branches/ipp-20220316/ippScripts/scripts
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20220316/ippScripts/scripts/camera_exp.pl

    r42170 r42362  
    185185
    186186    # Determine if FWHM is too large to bother continuing.
    187     my $maxFWHM = metadataLookupF32($recipeData, 'PSASTRO.MAX.ALLOWED.FWHM');
     187    my $maxFWHM = metadataLookupF32($recipeData, 'PSASTRO.MAX.PROCESS.FWHM');
    188188    if ($maxFWHM) {
    189189        my $expFWHM;
  • branches/eam_branches/ipp-20220316/ippScripts/scripts/nightly_science.pl

    r41994 r42362  
    142142my %unique_filter_hash = ();
    143143
     144#set up the camera config
     145my $ipprcam = PS::IPP::Config->new( $camera ) or my_die( "Unable to set up camera", $PS_EXIT_CONFIG_ERROR ); # IPP camera configuration
     146
    144147# Grab the configuration data.
    145148my $conf_cmd = "$ppConfigDump -camera $camera -dump-recipe NIGHTLY_SCIENCE -";
     
    155158foreach my $entry (@{ $metadata }) {
    156159    if (@{ $entry }{name} eq 'CLEAN_MODES') {
    157         my @mode_data = @{ ${ $entry }{value} };
    158         my $this_mode = '';
    159         foreach my $mentry (@mode_data) {
    160             if (${ $mentry }{name} eq 'MODE') {
    161                 $this_mode = ${ $mentry }{value};
    162             }
    163             elsif (${ $mentry }{name} eq 'COMMAND') {
    164                 $clean_config{$this_mode}{COMMAND} = ${ $mentry }{value};
    165             }
    166             elsif (${ $mentry }{name} eq 'RETENTION_TIME') {
    167                 $clean_config{$this_mode}{RETENTION_TIME} = ${ $mentry }{value};
    168             }
    169         }
     160        my @mode_data = @{ ${ $entry }{value} };
     161        my $this_mode = '';
     162        foreach my $mentry (@mode_data) {
     163            if (${ $mentry }{name} eq 'MODE') {
     164                $this_mode = ${ $mentry }{value};
     165            }
     166            elsif (${ $mentry }{name} eq 'COMMAND') {
     167                $clean_config{$this_mode}{COMMAND} = ${ $mentry }{value};
     168            }
     169            elsif (${ $mentry }{name} eq 'RETENTION_TIME') {
     170                $clean_config{$this_mode}{RETENTION_TIME} = ${ $mentry }{value};
     171            }
     172        }
    170173    }
    171174    elsif (${ $entry }{name} eq 'FILTERS') {
    172         $unique_filter_hash{ ${ $entry }{value} } = 1;
     175        $unique_filter_hash{ ${ $entry }{value} } = 1;
    173176#        push @filter_list, ${ $entry }{value};
    174177    }
     
    177180    }
    178181    elsif (${ $entry }{name} eq 'MACRO_DEFINITIONS') {
    179         my @macro_data = @{ ${ $entry }{value} };
    180         my $this_mode = '';
    181         foreach my $mentry (@macro_data) {
    182             if (${ $mentry }{name} eq 'PROC_MODE') {
    183                 $this_mode = ${ $mentry }{value};
    184             }
    185             elsif (${ $mentry }{name} eq 'MACRO') {
    186                 $macro_config{$this_mode} = ${ $mentry }{value};
    187             }
    188         }
     182        my @macro_data = @{ ${ $entry }{value} };
     183        my $this_mode = '';
     184        foreach my $mentry (@macro_data) {
     185            if (${ $mentry }{name} eq 'PROC_MODE') {
     186                $this_mode = ${ $mentry }{value};
     187            }
     188            elsif (${ $mentry }{name} eq 'MACRO') {
     189                $macro_config{$this_mode} = ${ $mentry }{value};
     190            }
     191        }
    189192    }
    190193    elsif (${ $entry }{name} eq 'TARGETS') {
     
    194197            if (${ $tentry }{name} eq 'NAME') {
    195198                $this_target = ${ $tentry }{value};
    196                 $science_config{$this_target}{DISTRIBUTION} = $this_target; # Set the default dist_group
    197             }
    198             else {
    199                 $science_config{$this_target}{ ${ $tentry }{name} } = ${ $tentry }{value};
    200             }
     199                $science_config{$this_target}{DISTRIBUTION} = $this_target; # Set the default dist_group
     200            }
     201            else {
     202                $science_config{$this_target}{ ${ $tentry }{name} } = ${ $tentry }{value};
     203            }
    201204        }
    202205    }
    203206    elsif (${ $entry }{name} eq 'DETRENDS') {
    204         my @detrend_data = @{ ${ $entry }{value} };
    205         my $this_detrend = '';
    206         foreach my $dentry (@detrend_data) {
    207             if (${ $dentry }{name} eq 'NAME') {
    208                 $this_detrend = ${ $dentry }{value};
    209             }
    210             else {
    211                 $detrend_config{$this_detrend}{ ${ $dentry }{name} } = ${ $dentry }{value};
    212             }
    213         }
    214     }           
     207        my @detrend_data = @{ ${ $entry }{value} };
     208        my $this_detrend = '';
     209        foreach my $dentry (@detrend_data) {
     210            if (${ $dentry }{name} eq 'NAME') {
     211                $this_detrend = ${ $dentry }{value};
     212            }
     213            else {
     214                $detrend_config{$this_detrend}{ ${ $dentry }{name} } = ${ $dentry }{value};
     215            }
     216        }
     217    }           
    215218    elsif (${ $entry }{name} eq 'END_OF_NIGHT') {
    216         my @eon_data = @{ ${ $entry }{value} };
    217         my $this_eon = '';
    218         foreach my $tentry (@eon_data) {
    219             if (${ $tentry }{name} eq 'NAME') {
    220                 $this_eon = ${ $tentry }{value};
    221             }
    222             else {
    223                 $eon_config{$this_eon}{ ${ $tentry }{name} } = ${ $tentry }{value};
    224             }
    225         }
     219        my @eon_data = @{ ${ $entry }{value} };
     220        my $this_eon = '';
     221        foreach my $tentry (@eon_data) {
     222            if (${ $tentry }{name} eq 'NAME') {
     223                $this_eon = ${ $tentry }{value};
     224            }
     225            else {
     226                $eon_config{$this_eon}{ ${ $tentry }{name} } = ${ $tentry }{value};
     227            }
     228        }
    226229    }
    227230}
     
    231234    my $time;
    232235    if ($date =~ / /) {
    233         ($date,$time) = split / /, $date;
     236        ($date,$time) = split / /, $date;
    234237    }
    235238    elsif ($date =~ /T/) {
    236         ($date,$time) = split /T/, $date;
     239        ($date,$time) = split /T/, $date;
    237240    }
    238241    my ($year,$month,$day) = split /-/, $date;
    239242    if (defined($time)) {
    240         my ($hour,$min,$sec) = split /\:/, $time; #/;
    241         $datetime = DateTime->new( year => $year,
    242                                    month => $month,
    243                                    day => $day,
    244                                    hour => $hour,
    245                                    min => $min,
    246                                    second => $sec,
    247                                    time_zone => 'UTC');
     243        my ($hour,$min,$sec) = split /\:/, $time; #/;
     244        $datetime = DateTime->new( year => $year,
     245                                   month => $month,
     246                                   day => $day,
     247                                   hour => $hour,
     248                                   min => $min,
     249                                   second => $sec,
     250                                   time_zone => 'UTC');
    248251    }
    249252    else {
    250         $datetime = DateTime->now(time_zone => 'UTC');
    251         $datetime->set_year($year);
    252         $datetime->set_day($day);
    253         $datetime->set_month($month);
    254     }
    255 }    
     253        $datetime = DateTime->now(time_zone => 'UTC');
     254        $datetime->set_year($year);
     255        $datetime->set_day($day);
     256        $datetime->set_month($month);
     257    }
     258}   
    256259else {
    257260    $datetime = DateTime->now(time_zone => 'UTC'); # time_zone   => 'Pacific/Honolulu');
     
    263266    foreach my $t (keys %science_config) {
    264267        if ($t ne $this_target_only) {
    265             undef($science_config{$t});
     268            undef($science_config{$t});
    266269        }
    267270    }
     
    282285    foreach my $t (keys %clean_config) {
    283286        if ($t ne $this_mode_only) {
    284             undef($clean_config{$t});
     287            undef($clean_config{$t});
    285288        }
    286289    }
     
    295298
    296299if (defined($desdiffdt) == 0) {
    297         $desdiffdt = 1.5;
     300        $desdiffdt = 1.5;
    298301}
    299302
    300303#Build in a safety for desdiffdt
    301304if ((defined($desdiffdt) == 1) && ($desdiffdt <= 0.5)) {
    302         $desdiffdt = 0.5;
     305        $desdiffdt = 0.5;
    303306}
    304307
     
    312315    my $v;
    313316    foreach $z (sort (keys %science_config)) {
    314         foreach $v (keys %{ $science_config{$z} }) {
    315             print "SCIENCE: $z $v $science_config{$z}{$v}\n";
    316         }
     317        foreach $v (keys %{ $science_config{$z} }) {
     318            print "SCIENCE: $z $v $science_config{$z}{$v}\n";
     319        }
    317320    }
    318321    foreach $z (sort (keys %detrend_config)) {
    319         foreach $v (keys %{ $detrend_config{$z} }) {
    320             print "DETREND: $z $v $detrend_config{$z}{$v}\n";
    321         }
     322        foreach $v (keys %{ $detrend_config{$z} }) {
     323            print "DETREND: $z $v $detrend_config{$z}{$v}\n";
     324        }
    322325    }
    323326    foreach $z (keys (%clean_config)) {
    324         foreach $v (keys %{ $clean_config{$z} }) {
    325             print "CLEAN: $z $v $clean_config{$z}{$v}\n";
    326         }
     327        foreach $v (keys %{ $clean_config{$z} }) {
     328            print "CLEAN: $z $v $clean_config{$z}{$v}\n";
     329        }
    327330    }
    328331    foreach $z (@filter_list) {
    329         print "FILTER: $z\n";
     332        print "FILTER: $z\n";
    330333    }
    331334    foreach $z (keys (%macro_config)) {
    332         print "MACROS: $z $macro_config{$z}\n";
     335        print "MACROS: $z $macro_config{$z}\n";
    333336    }
    334337}
     
    397400    &execute_stacks($date,$metadata_out{nsObservingState});
    398401    if ($metadata_out{nsStackState} eq 'QUEUESTACKING') {
    399         $metadata_out{nsStackState} = 'STACKING_POSSIBLE_ERROR';
     402        $metadata_out{nsStackState} = 'STACKING_POSSIBLE_ERROR';
    400403    }
    401404    return_metadata($date);
     
    416419    &execute_diffs($date,$metadata_out{nsObservingState});
    417420    if ($metadata_out{nsDiffState} eq 'QUEUESTACKING') {
    418         $metadata_out{nsDiffState} = 'STACKING_POSSIBLE_ERROR';
     421        $metadata_out{nsDiffState} = 'STACKING_POSSIBLE_ERROR';
    419422    }
    420423    return_metadata($date);
     
    481484        my ($exp_name,$registered,$dateobs,$imfiles,$summit_fault,
    482485            $download_state,$download_count,$new_state,$exp_id,$exp_type) = @{ $row_ref };
    483         $Nsummit_imfiles += $imfiles;
    484         $Ndownload_imfiles += $download_count;
     486        $Nsummit_imfiles += $imfiles;
     487        $Ndownload_imfiles += $download_count;
    485488        $Nsummit_exps++;
    486489        if ($summit_fault) {
    487             print STDERR "check_summit_copy: $date $exp_name has summit_fault $summit_fault";
     490            print STDERR "check_summit_copy: $date $exp_name has summit_fault $summit_fault";
    488491            if (($exp_type ne 'OBJECT')||($exp_name =~ /^c.*/)) {
    489492                print STDERR " (but I don't care).\n";
     
    526529    }
    527530    elsif ($Ndownload_imfiles != $Nsummit_imfiles) {
    528         print STDERR "Not done downloading from the summit for $date (Summit: $Nsummit_imfiles, Downloaded: $Ndownload_imfiles)\n";
    529         $metadata_out{nsRegState} = 'NEW';
     531        print STDERR "Not done downloading from the summit for $date (Summit: $Nsummit_imfiles, Downloaded: $Ndownload_imfiles)\n";
     532        $metadata_out{nsRegState} = 'NEW';
    530533    }
    531534    else {
     
    615618    }
    616619    if (defined($reduction)) {
    617         $cmd .= " -set_reduction $reduction ";
     620        $cmd .= " -set_reduction $reduction ";
    618621    }
    619622    $cmd .= " $select ";
     
    674677
    675678    foreach my $target (sort (keys %science_config)) {
    676         my ($Nexposures,$Nimfiles,$Nburntooled,$Nalready) = check_chip_status($date,$target);
    677         print "BTSTATS: $date $target $Nexposures $Nimfiles $Nburntooled $Nalready\n";
     679        my ($Nexposures,$Nimfiles,$Nburntooled,$Nalready) = check_chip_status($date,$target);
     680        print "BTSTATS: $date $target $Nexposures $Nimfiles $Nburntooled $Nalready\n";
    678681    }
    679682}
     
    695698    $cmd .= " -set_label dqstats.nightlyscience ";
    696699    if ($debug == 1) {
    697         $cmd .= ' -pretend ';
     700        $cmd .= ' -pretend ';
    698701    }
    699702    print STDERR "$cmd\n";
     
    703706sub pre_dqstats_queue {
    704707    my $date = shift;
    705     
     708   
    706709    my $command = construct_dqstats_cmd($date) . ' -pretend ';
    707710    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     
    711714        &my_die("Unable to perform dqstatstool: $error_code",1,1,,$date, $PS_EXIT_SYS_ERROR);
    712715    }
    713     
     716   
    714717    my @input_exposures = split /\n/, (join '', @$stdout_buf);
    715718
     
    735738    my ($Nexposures,$Nchips,$Ncams) = pre_dqstats_queue($date);
    736739    if ($Nexposures == 0) {
    737         print STDERR "execute_dqstats: No exposures on date $date.\n";
     740        print STDERR "execute_dqstats: No exposures on date $date.\n";
    738741    }
    739742    elsif ($Ncams != $Nchips) {
    740         print STDERR "execute_dqstats: Not done processing data through camera stage.\n";
     743        print STDERR "execute_dqstats: Not done processing data through camera stage.\n";
    741744    }
    742745    else {
    743         unless(defined($pretend)) {
    744             dqstats_queue($date);
    745         }
    746         if (defined($pretend)) {
    747             add_to_macro_list('check_dqstats',1,$date);
    748         }
    749         else {
    750             add_to_macro_list('queue_dqstats',1,$date);
    751         }
     746        unless(defined($pretend)) {
     747            dqstats_queue($date);
     748        }
     749        if (defined($pretend)) {
     750            add_to_macro_list('check_dqstats',1,$date);
     751        }
     752        else {
     753            add_to_macro_list('queue_dqstats',1,$date);
     754        }
    752755    }
    753756}   
     
    761764
    762765    my ($label,$workdir,$filter,$exp_type,$det_type,$ref_det_id,$ref_iter,$maxN) = get_dettool_parameters($date,$target);
    763     
     766   
    764767    my $select = "-select_dateobs_begin ${date}T00:00:00 -select_dateobs_end ${date}T23:59:59 ";
    765768    my $use_limits = " -use_begin ${date}T00:00:00 -use_end ${date}T23:59:59 ";
     
    778781    $cmd .= " $use_limits ";
    779782    if ($maxN > 0) {
    780         $cmd .= " -random_subset -random_limit $maxN ";
     783        $cmd .= " -random_subset -random_limit $maxN ";
    781784    }
    782785    if ($debug == 1) {
    783         $cmd .= ' -pretend ';
     786        $cmd .= ' -pretend ';
    784787    }
    785788    print STDERR "$cmd\n";
    786789    return($cmd);
    787 }    
     790}   
    788791
    789792sub verify_uniqueness_detverify {
     
    792795
    793796    my ($label,$workdir,$filter,$exp_type,$det_type,$ref_det_id,$ref_iter,$maxN) = get_dettool_parameters($date,$target);
    794     
     797   
    795798    my $db = init_gpc_db();
    796799    my $sth = "SELECT * FROM detRun WHERE workdir = '$workdir' AND ref_det_id = $ref_det_id AND ref_iter = $ref_iter";
     
    803806    my $date = shift;
    804807    my $target = shift;
    805     
     808   
    806809    my $command = construct_dettool_cmd($date,$target) . ' -pretend ';
    807810    my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    808         run ( command => $command, verbose => $verbose);
     811        run ( command => $command, verbose => $verbose);
    809812    unless ($success) {
    810         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    811         &my_die("Unable to perform dettool: $error_code", 0,0, $date, $PS_EXIT_SYS_ERROR);
    812     }
    813     
     813        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     814        &my_die("Unable to perform dettool: $error_code", 0,0, $date, $PS_EXIT_SYS_ERROR);
     815    }
     816   
    814817    my @input_exposures = split /\n/, (join '', @$stdout_buf);
    815818    return($#input_exposures + 1);
     
    819822    my $date = shift;
    820823    my $target = shift;
    821     
     824   
    822825    my $command = construct_dettool_cmd($date,$target);
    823826    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    824         run ( command => $command, verbose => $verbose);
     827        run ( command => $command, verbose => $verbose);
    825828    unless ($success) {
    826         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    827         &my_die("Unable to perform chiptool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
     829        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     830        &my_die("Unable to perform chiptool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
    828831    }
    829832    $metadata_out{nsDetState} = 'DETREND_QUEUED';
     
    836839    my $exposures = 0;
    837840    foreach my $target (sort (keys %detrend_config)) {
    838         my ($Nexposures) = pre_detrend_queue($date,$target);
    839         if ($Nexposures == 0) {
    840             print STDERR "execute_detrends: Target $target on $date had no exposures.\n";
    841             next;
    842         }
    843         $exposures++;
    844         unless(defined($pretend)) {
    845             detrend_queue($date,$target);
    846         }
    847         if (defined($pretend)) {
    848             add_to_macro_list('check_detrends',1,$date,$target,"dettool");
    849         }
    850         else {
    851             add_to_macro_list('queue_detrends',1,$date,$target,"dettool");
    852         }
     841        my ($Nexposures) = pre_detrend_queue($date,$target);
     842        if ($Nexposures == 0) {
     843            print STDERR "execute_detrends: Target $target on $date had no exposures.\n";
     844            next;
     845        }
     846        $exposures++;
     847        unless(defined($pretend)) {
     848            detrend_queue($date,$target);
     849        }
     850        if (defined($pretend)) {
     851            add_to_macro_list('check_detrends',1,$date,$target,"dettool");
     852        }
     853        else {
     854            add_to_macro_list('queue_detrends',1,$date,$target,"dettool");
     855        }
    853856    }
    854857    if ($exposures == 0) {
    855         $metadata_out{nsDetState} = 'DETREND_DROP';
     858        $metadata_out{nsDetState} = 'DETREND_DROP';
    856859    }
    857860    if (($metadata_out{nsDetState} eq 'CHECKDETRENDS') && ($exposures > 0)) {
    858         $metadata_out{nsDetState} eq 'QUEUEDETRENDS';
     861        $metadata_out{nsDetState} eq 'QUEUEDETRENDS';
    859862    }
    860863}
     
    882885#     $cmd .= " $select ";
    883886#     if ($debug == 1) {
    884 #       $cmd .= ' -pretend ';
     887#       $cmd .= ' -pretend ';
    885888#     }
    886889#     print STDERR "$cmd\n";
     
    895898
    896899    my $dt = DateTime->new(year => $year, month => $month, day => $day,
    897                            hour => 0, minute => 0, second => 0, nanosecond => 0,
    898                            time_zone => 'Pacific/Honolulu');
     900                           hour => 0, minute => 0, second => 0, nanosecond => 0,
     901                           time_zone => 'Pacific/Honolulu');
    899902    do {
    900         $dt->add(days => 1);
    901         my $ymd = $dt->ymd;
    902         my $md_cmd = "moondata $ymd 0 0";
    903         my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    904             run ( command => $md_cmd, verbose => $verbose);
    905         unless ($success) {
    906             $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    907             &my_die("Unable to perform moondata: $error_code", 0,0, $date, $PS_EXIT_SYS_ERROR);
    908         }
    909         my @result = split /\s+/,(join "\n", @$stdout_buf);
    910         if (abs($result[6]) <= 0.6) {
    911             $dateobs_end = $ymd;
    912         }
     903        $dt->add(days => 1);
     904        my $ymd = $dt->ymd;
     905        my $md_cmd = "moondata $ymd 0 0";
     906        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     907            run ( command => $md_cmd, verbose => $verbose);
     908        unless ($success) {
     909            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     910            &my_die("Unable to perform moondata: $error_code", 0,0, $date, $PS_EXIT_SYS_ERROR);
     911        }
     912        my @result = split /\s+/,(join "\n", @$stdout_buf);
     913        if (abs($result[6]) <= 0.6) {
     914            $dateobs_end = $ymd;
     915        }
    913916    } while (!defined($dateobs_end));
    914     
     917   
    915918    do {
    916         $dt->subtract(days => 1);
    917         my $ymd = $dt->ymd;
    918         my $md_cmd = "moondata $ymd 0 0";
    919         my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    920             run ( command => $md_cmd, verbose => $verbose);
    921         unless ($success) {
    922             $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    923             &my_die("Unable to perform moondata: $error_code", 0,0, $date, $PS_EXIT_SYS_ERROR);
    924         }
    925         my @result = split /\s+/,(join "", @$stdout_buf);
    926         if (abs($result[6]) <= 0.6) {
    927             $dateobs_begin = $ymd;
    928         }
     919        $dt->subtract(days => 1);
     920        my $ymd = $dt->ymd;
     921        my $md_cmd = "moondata $ymd 0 0";
     922        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     923            run ( command => $md_cmd, verbose => $verbose);
     924        unless ($success) {
     925            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     926            &my_die("Unable to perform moondata: $error_code", 0,0, $date, $PS_EXIT_SYS_ERROR);
     927        }
     928        my @result = split /\s+/,(join "", @$stdout_buf);
     929        if (abs($result[6]) <= 0.6) {
     930            $dateobs_begin = $ymd;
     931        }
    929932    } while (!defined($dateobs_begin));
    930        
     933       
    931934    return($dateobs_begin,$dateobs_end);
    932935}
    933936
    934 # sub pre_sweetspot_queue { 
     937# sub pre_sweetspot_queue {
    935938#     my $date = shift;
    936939#     my $command = construct_sweetspot_cmd($date) . ' -pretend ';
    937940#     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    938 #       run ( command => $command, verbose => $verbose );
     941#       run ( command => $command, verbose => $verbose );
    939942#     unless ($success) {
    940943#         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     
    943946#     my @stacks = split /\n/, (join '', @$stdout_buf);
    944947#     my $Nstacks = $#stacks + 1;
    945         
     948       
    946949#     return(1,$Nstacks);
    947950# }
     
    951954#     my $command = construct_sweetspot_cmd($date);
    952955#     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    953 #       run ( command => $command, verbose => $verbose );
     956#       run ( command => $command, verbose => $verbose );
    954957#     unless ($success) {
    955958#         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     
    958961#     my @stacks = split /\n/, (join '', @$stdout_buf);
    959962#     my $Nstacks = $#stacks + 1;
    960     
     963   
    961964#     return(1,$Nstacks);
    962965# }
    963966
    964 # sub execute_sweetspot {    
     967# sub execute_sweetspot {   
    965968#     my $date = shift;
    966969#     my $pretend = shift;
    967     
     970   
    968971#     my ($is_lunation_date,$Nstacks) = pre_sweetspot_queue($date);
    969972#     if ($Nstacks == 0) {
    970 #       print STDERR "execute_sweetspot: No new stacks to make ($Nstacks)\n";
    971 #       $metadata_out{nsSSState} = 'SS_EMPTY';
    972 #       return();
     973#       print STDERR "execute_sweetspot: No new stacks to make ($Nstacks)\n";
     974#       $metadata_out{nsSSState} = 'SS_EMPTY';
     975#       return();
    973976#     }
    974977#     if ($Nstacks < 10) {
    975 #       print STDERR "execute_sweetspot: Too few new stacks to make ($Nstacks)\n";
    976 #       $metadata_out{nsSSState} = 'SS_FEW';
    977 #       return();
     978#       print STDERR "execute_sweetspot: Too few new stacks to make ($Nstacks)\n";
     979#       $metadata_out{nsSSState} = 'SS_FEW';
     980#       return();
    978981#     }
    979982#     if ($is_lunation_date == 0) {
    980 #       print STDERR "execute_sweetspot: Invalid lunation date. ($date $is_lunation_date)\n";
    981 #       $metadata_out{nsSSState} = 'SS_ERROR';
    982 #       return();
     983#       print STDERR "execute_sweetspot: Invalid lunation date. ($date $is_lunation_date)\n";
     984#       $metadata_out{nsSSState} = 'SS_ERROR';
     985#       return();
    983986#     }
    984987#     $metadata_out{nsSSState} = 'SS_QUEUE';
    985988#     $metadata_out{nsSweetSpot} = $Nstacks;
    986989#     unless(defined($pretend)) {
    987 #       $metadata_out{nsSSState} = 'SS_DONE';
    988 #       sweetspot_queue($date);
     990#       $metadata_out{nsSSState} = 'SS_DONE';
     991#       sweetspot_queue($date);
    989992#     }
    990993#     if (defined($pretend)) {
    991 #       add_to_macro_list('check_sweetspot',1,$date);
     994#       add_to_macro_list('check_sweetspot',1,$date);
    992995#     }
    993996#     else {
    994 #       add_to_macro_list('queue_sweetspot',1,$date);
     997#       add_to_macro_list('queue_sweetspot',1,$date);
    995998#     }
    996999# }
     
    10201023    $cmd .= " -select_good_frac_min 0.05";
    10211024    if (defined($reduction)) {
    1022         $cmd .= " -set_reduction $reduction ";
     1025        $cmd .= " -set_reduction $reduction ";
    10231026    }
    10241027    else {
    1025         $cmd .= " -set_reduction NIGHTLY_STACK ";
     1028        $cmd .= " -set_reduction NIGHTLY_STACK ";
    10261029    }
    10271030    if (defined($science_config{$target}{ADDITIONAL_STACK_LABEL})) {
    1028         # Grab list of skycells
    1029         my $skycell_select = '';
    1030         {
    1031             my %skycells = ();
    1032             my $warpcmd = "$warptool -warped -label $label -data_group $data_group  -dbname $dbname";
    1033             my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) =
    1034                 run(command => $warpcmd, verbose => $verbose);
    1035             unless($success) {
    1036                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1037                 &my_die("Unabel to perform warptool -warped to determine skycell_ids: $error_code",$target);
    1038             }
    1039             my $runs = $mdcParser->parse_list(join "", @$stdout_buf) or
    1040                 &my_die("Unabel to parse warptool -warped to determine skycell_ids: $error_code",$target);
    1041             if ($#{ $runs } != -1) {
    1042                 for my $warp (@$runs) {
    1043                     $skycells{$warp->{skycell_id}} = 1;
    1044                 }
    1045                 foreach my $skycell (sort (keys (%skycells))) {
    1046                     $skycell_select .= " -select_skycell_id $skycell ";
    1047                 }
    1048                 $cmd .= " -select_label $science_config{$target}{ADDITIONAL_STACK_LABEL} $skycell_select ";
    1049             }
    1050         }
     1031        # Grab list of skycells
     1032        my $skycell_select = '';
     1033        {
     1034            my %skycells = ();
     1035            my $warpcmd = "$warptool -warped -label $label -data_group $data_group  -dbname $dbname";
     1036            my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) =
     1037                run(command => $warpcmd, verbose => $verbose);
     1038            unless($success) {
     1039                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1040                &my_die("Unabel to perform warptool -warped to determine skycell_ids: $error_code",$target);
     1041            }
     1042            my $runs = $mdcParser->parse_list(join "", @$stdout_buf) or
     1043                &my_die("Unabel to parse warptool -warped to determine skycell_ids: $error_code",$target);
     1044            if ($#{ $runs } != -1) {
     1045                for my $warp (@$runs) {
     1046                    $skycells{$warp->{skycell_id}} = 1;
     1047                }
     1048                foreach my $skycell (sort (keys (%skycells))) {
     1049                    $skycell_select .= " -select_skycell_id $skycell ";
     1050                }
     1051                $cmd .= " -select_label $science_config{$target}{ADDITIONAL_STACK_LABEL} $skycell_select ";
     1052            }
     1053        }
    10511054    }
    10521055    $cmd .= " $select ";
     
    10901093    my $where_possibly_faulted = $where . " AND ( " ;
    10911094    foreach my $acceptable_quality (@unrecoverable_quality) {
    1092         $where_possibly_faulted .= " quality = $acceptable_quality OR ";
     1095        $where_possibly_faulted .= " quality = $acceptable_quality OR ";
    10931096    }
    10941097    $where_possibly_faulted .= " 0 )";
     
    11191122        $Nalready += $trash[0];
    11201123    }
    1121     
     1124   
    11221125    my $minimum_required_warp;
    11231126    if ($observing_state eq 'OBSERVING') {
    1124         $minimum_required_warp = $science_config{$target}{MIN_STACK};
     1127        $minimum_required_warp = $science_config{$target}{MIN_STACK};
    11251128    }
    11261129    unless (defined($minimum_required_warp)) {
    1127         $minimum_required_warp = 0;
     1130        $minimum_required_warp = 0;
    11281131    }
    11291132
     
    11431146        &my_die("Unable to perform stacktool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
    11441147    }
    1145     
    1146     
     1148   
     1149   
    11471150    return(0);
    11481151}
     
    11581161    my $is_registering;
    11591162    if ($metadata_out{nsRegistrationState} eq 'REGISTERED') {
    1160         $is_registering = 0;
     1163        $is_registering = 0;
    11611164    }
    11621165    else {
    1163         $is_registering = 1;
     1166        $is_registering = 1;
    11641167    }
    11651168
    11661169    foreach my $target (sort (keys %science_config)) {
    1167         if ($science_config{$target}{STACKABLE} == 1) {
    1168             foreach my $filter (@filter_list) {
     1170        if ($science_config{$target}{STACKABLE} == 1) {
     1171            foreach my $filter (@filter_list) {
    11691172                my ($Nexposures,$NprocChips,$NprocWarps,$NrequiredWarps,$Nalready) = pre_stack_queue($date,$observing_state,$target,$filter);
    11701173                if ((!defined($force_stack_count))&&($NprocChips != $NprocWarps)) { # This makes me sad. :(
    1171                     if ($debug == 1) {
    1172                         print STDERR "execute_stacks: Target $target on $date is not fully processed. ($NprocChips $NprocWarps)\n";
    1173                     }
    1174                     $is_processing = 1;
     1174                    if ($debug == 1) {
     1175                        print STDERR "execute_stacks: Target $target on $date is not fully processed. ($NprocChips $NprocWarps)\n";
     1176                    }
     1177                    $is_processing = 1;
    11751178                    $metadata_out{nsStackState} = 'FORCETOWARP';
    11761179                    next;
    11771180                }
    11781181                if ($Nexposures == 0) {
    1179                     if ($debug == 1) {
    1180                         print STDERR "execute_stacks: Target $target in filter $filter on $date has no exposures.\n";
    1181                     }
     1182                    if ($debug == 1) {
     1183                        print STDERR "execute_stacks: Target $target in filter $filter on $date has no exposures.\n";
     1184                    }
    11821185                    next;
    11831186                }
    1184                 if ($NprocWarps < $NrequiredWarps) {
    1185                     if ($debug == 1) {
    1186                         print STDERR "execute_stacks: Target $target in filter $filter on $date has too few warps to begin stacking. ($NprocWarps $NrequiredWarps)\n";
    1187                     }
    1188                     next;
    1189                 }
    1190                 $Npotential++;
     1187                if ($NprocWarps < $NrequiredWarps) {
     1188                    if ($debug == 1) {
     1189                        print STDERR "execute_stacks: Target $target in filter $filter on $date has too few warps to begin stacking. ($NprocWarps $NrequiredWarps)\n";
     1190                    }
     1191                    next;
     1192                }
     1193                $Npotential++;
    11911194                if ($Nalready != 0) {
    1192                     $Nqueued++;
    1193                     if ($debug == 1) {
    1194                         print STDERR "execute_stacks: Not queueing $target in filter $filter on $date due to already existing stacks.\n";
    1195                     }
    1196                     unless ($metadata_out{nsStackState} eq 'FORCETOWARP') {
    1197                         $metadata_out{nsStackState} = 'STACKING';
    1198                     }
     1195                    $Nqueued++;
     1196                    if ($debug == 1) {
     1197                        print STDERR "execute_stacks: Not queueing $target in filter $filter on $date due to already existing stacks.\n";
     1198                    }
     1199                    unless ($metadata_out{nsStackState} eq 'FORCETOWARP') {
     1200                        $metadata_out{nsStackState} = 'STACKING';
     1201                    }
    11991202                    next;
    12001203                }
     
    12031206                }
    12041207                unless(defined($pretend)) {
    1205                     if ($debug == 1) {
    1206                         print STDERR "execute_stacks: Target $target in filter $filter on $date has exposures and will be queued.\n";
    1207                     }
     1208                    if ($debug == 1) {
     1209                        print STDERR "execute_stacks: Target $target in filter $filter on $date has exposures and will be queued.\n";
     1210                    }
    12081211                    stack_queue($date,$target,$filter);
    1209                     $Nqueued ++;
    1210                 }
    1211                 if (defined($pretend)) {
    1212                     add_to_macro_list('check_stacks',$science_config{$target}{STACKABLE},$date,$target,$filter);
    1213                 }
    1214                 else {
    1215                     add_to_macro_list('queue_stacks',$science_config{$target}{STACKABLE},$date,$target,$filter);
    1216                 }
    1217             }
    1218         }
    1219         else {
    1220             if ($debug == 1) {
    1221                 print STDERR "execute_stacks: Target $target is not auto-stackable.\n";
    1222             }
     1212                    $Nqueued ++;
     1213                }
     1214                if (defined($pretend)) {
     1215                    add_to_macro_list('check_stacks',$science_config{$target}{STACKABLE},$date,$target,$filter);
     1216                }
     1217                else {
     1218                    add_to_macro_list('queue_stacks',$science_config{$target}{STACKABLE},$date,$target,$filter);
     1219                }
     1220            }
     1221        }
     1222        else {
     1223            if ($debug == 1) {
     1224                print STDERR "execute_stacks: Target $target is not auto-stackable.\n";
     1225            }
    12231226        }
    12241227    }
     
    12261229    $metadata_out{nsStackQueued}    = $Nqueued;
    12271230    if (($Npotential == $Nqueued)&&($metadata_out{nsObservingState} eq 'END_OF_NIGHT')&&($is_processing == 0)&&($is_registering == 0)) {
    1228         $metadata_out{nsStackState} = 'FINISHED_STACKS';
    1229     }   
     1231        $metadata_out{nsStackState} = 'FINISHED_STACKS';
     1232    }   
    12301233}
    12311234
     
    12461249    $stack_done_sth .= " AND ((state = 'full') OR (state = 'new' && fault != 0)) ";
    12471250    if ($debug == 1) {
    1248         print STDERR "post_stack_queue: database queries:\n";
    1249         print STDERR "$stack_full_sth\n";
    1250         print STDERR "$stack_done_sth\n";
     1251        print STDERR "post_stack_queue: database queries:\n";
     1252        print STDERR "$stack_full_sth\n";
     1253        print STDERR "$stack_done_sth\n";
    12511254    }
    12521255    my $stack_full_ref = $db->selectall_arrayref( $stack_full_sth );
     
    12591262    my $date = shift;
    12601263    my $pretend = shift;
    1261     
     1264   
    12621265    foreach my $target (sort (keys %science_config)) {
    12631266        if ($science_config{$target}{STACKABLE} == 1) {
    1264             my ($Nstacks, $Nattempted) = post_stack_queue($date,$target);
    1265             if ($debug == 1) {
    1266                 print STDERR "confirm_stacks: Target $target on $date has $Nattempted attempts on $Nstacks.\n";
    1267             }
    1268             if ($Nstacks != $Nattempted) {
    1269                 if ($debug == 1) {
    1270                     print STDERR "confirm_stacks: Target $target on $date is not done stacking. $Nstacks $Nattempted\n";
    1271                 }
    1272                 if ($metadata_out{nsStackState} eq 'CONFIRM_STACKING') {
    1273                     $metadata_out{nsStackState} = 'STACKING';
    1274                 }
    1275                 next;
    1276             }
    1277             if ($Nstacks == 0) {
    1278                 if ($debug == 1) {
    1279                     print STDERR "confirm_stacks: Target $target on $date has no stacks. Skipping.\n";
    1280                 }
    1281                 next;
    1282             }
    1283             if ($metadata_out{nsStackState} eq 'CONFIRM_STACKING') {
    1284                 if (defined($pretend)) {
    1285                     add_to_macro_list('check_confirm_stacks',$science_config{$target}{STACKABLE},$date,$target);
    1286                 }
    1287                 else {
    1288                     add_to_macro_list('confirm_stacks',$science_config{$target}{STACKABLE},$date,$target);
    1289                 }
    1290             }
    1291         }
    1292     }
    1293 }          
     1267            my ($Nstacks, $Nattempted) = post_stack_queue($date,$target);
     1268            if ($debug == 1) {
     1269                print STDERR "confirm_stacks: Target $target on $date has $Nattempted attempts on $Nstacks.\n";
     1270            }
     1271            if ($Nstacks != $Nattempted) {
     1272                if ($debug == 1) {
     1273                    print STDERR "confirm_stacks: Target $target on $date is not done stacking. $Nstacks $Nattempted\n";
     1274                }
     1275                if ($metadata_out{nsStackState} eq 'CONFIRM_STACKING') {
     1276                    $metadata_out{nsStackState} = 'STACKING';
     1277                }
     1278                next;
     1279            }
     1280            if ($Nstacks == 0) {
     1281                if ($debug == 1) {
     1282                    print STDERR "confirm_stacks: Target $target on $date has no stacks. Skipping.\n";
     1283                }
     1284                next;
     1285            }
     1286            if ($metadata_out{nsStackState} eq 'CONFIRM_STACKING') {
     1287                if (defined($pretend)) {
     1288                    add_to_macro_list('check_confirm_stacks',$science_config{$target}{STACKABLE},$date,$target);
     1289                }
     1290                else {
     1291                    add_to_macro_list('confirm_stacks',$science_config{$target}{STACKABLE},$date,$target);
     1292                }
     1293            }
     1294        }
     1295    }
     1296}          
    12941297
    12951298#
     
    13081311
    13091312    my $count = 0;
    1310     
     1313   
    13111314    my $sth = "SELECT DISTINCT diff_id from diffRun JOIN diffInputSkyfile USING(diff_id) where data_group = '$data_group' AND warp1 = $warp_id_1 AND warp2 = $warp_id_2";
    13121315    my $data_ref = $db->selectall_arrayref( $sth );
     
    13311334
    13321335    my $count = 0;
    1333     
     1336   
    13341337    my $sth = "SELECT DISTINCT diff_id from diffRun JOIN diffInputSkyfile USING(diff_id) where (label = '$label' OR label = 'goto_cleaned') AND warp1 = $warp_id_1 AND warp2 = $warp_id_2";
    13351338    my $data_ref = $db->selectall_arrayref( $sth );
     
    13491352    my $filter = shift;
    13501353    my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
    1351     
     1354   
    13521355    my $db = init_gpc_db();
    13531356
     
    13581361    my $where_possibly_faulted = $where . " AND ( " ;
    13591362    foreach my $acceptable_quality (@unrecoverable_quality) {
    1360         $where_possibly_faulted .= " quality = $acceptable_quality OR ";
     1363        $where_possibly_faulted .= " quality = $acceptable_quality OR ";
    13611364    }
    13621365    $where_possibly_faulted .= " 0 )";
     
    13831386    my $is_registering;
    13841387    if ($metadata_out{nsRegistrationState} eq 'REGISTERED') {
    1385         $is_registering = 0;
     1388        $is_registering = 0;
    13861389    }
    13871390    else {
    1388         $is_registering = 1;
     1391        $is_registering = 1;
    13891392    }
    13901393
    13911394    foreach my $target (sort (keys %science_config)) {
    1392         if ($science_config{$target}{DIFFABLE} == 1) {
    1393             foreach my $filter (@filter_list) {
    1394                 $Npotential ++;
    1395                 my ($NprocChips,$NprocWarps) = pre_diff_queue($date,$observing_state,$target,$filter);
    1396                 if ((!defined($force_diff_count))&&($NprocChips != $NprocWarps)) {
    1397                     if ($debug == 1) {
    1398                         print STDERR "execute_diffs: Target $target in filter $filter on $date is not fully processed. ($NprocChips $NprocWarps)\n";
    1399                     }
    1400                     $is_processing = 1;
    1401                 }
     1395        if ($science_config{$target}{DIFFABLE} == 1) {
     1396
     1397            my $config_reduction = "DEFAULT";
     1398            if($science_config{$target}{REDUCTION}) {$config_reduction=$science_config{$target}{REDUCTION};}
     1399            print STDERR "execute_diffs: Trying target $target on $date $camera $science_config{$target}{REDUCTION} $config_reduction\n";
     1400
     1401            my $recipe_psastro = $ipprcam->reduction($config_reduction, 'PSASTRO'); # Recipe to use
     1402            &my_die("Unrecognised PSASTRO recipe", $PS_EXIT_CONFIG_ERROR) unless defined $recipe_psastro;
     1403
     1404            my $recipeData;
     1405            {
     1406                # Get the PSASTRO recipe
     1407                my $command = "$ppConfigDump -camera $camera -recipe PSASTRO $recipe_psastro -dump-recipe PSASTRO -";
     1408                my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1409                    run(command => $command);
     1410                unless ($success) {
     1411                    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1412                    &my_die("Unable to perform ppConfigDump: $error_code", $PS_EXIT_CONFIG_ERROR);
     1413                }
     1414                $recipeData = $mdcParser->parse(join "", @$stdout_buf) or
     1415                    &my_die("Unable to parse metadata config doc", $PS_EXIT_CONFIG_ERROR);
     1416            }
     1417
     1418            my $maxFWHM = metadataLookupF32($recipeData, 'PSASTRO.MAX.ALLOWED.FWHM');
     1419            print STDERR "execute_diffs: Max FWHM= $maxFWHM for $target\n";
     1420
     1421            foreach my $filter (@filter_list) {
     1422                $Npotential ++;
     1423                my ($NprocChips,$NprocWarps) = pre_diff_queue($date,$observing_state,$target,$filter);
     1424                if ((!defined($force_diff_count))&&($NprocChips != $NprocWarps)) {
     1425                    if ($debug == 1) {
     1426                        print STDERR "execute_diffs: Target $target in filter $filter on $date is not fully processed. ($NprocChips $NprocWarps)\n";
     1427                    }
     1428                    $is_processing = 1;
     1429                }
    14021430                if ($NprocChips == 0) {
    1403                     $Nnoexp ++;
    1404                     if ($debug == 1) {
    1405                         print STDERR "execute_diffs: Target $target in filter $filter on $date has no exposures.\n";
    1406                     }
     1431                    $Nnoexp ++;
     1432                    if ($debug == 1) {
     1433                        print STDERR "execute_diffs: Target $target in filter $filter on $date has no exposures.\n";
     1434                    }
    14071435                    next;
    14081436                }
     
    14101438                    $metadata_out{nsDiffState} = 'QUEUEDIFFS';
    14111439                }
    1412                 if ($debug == 1) {
    1413                     print STDERR "execute_diffs: Target $target in filter $filter on $date has exposures and will be queued.\n";
    1414                 }
    1415                 diff_queue($date,$target,$filter,$pretend);
    1416 
    1417                 #Queue up desperate diffs if the conditions are right for them  
    1418                 if ((defined($science_config{$target}{DESPERATE_DIFFS})) && ($science_config{$target}{DESPERATE_DIFFS} == 1)) {
    1419                     desperate_diff_singles($date,$target,$filter,$pretend);
    1420                 }
    1421 
    1422                 if (defined($pretend)) {
    1423                     add_to_macro_list('check_diffs',$science_config{$target}{DIFFABLE},$date,$target,$filter);
    1424                 }
    1425                 else {
    1426                     add_to_macro_list('queue_diffs',$science_config{$target}{DIFFABLE},$date,$target,$filter);
    1427                 }
    1428             }
    1429         }
    1430         else {
    1431             if ($debug == 1) {
    1432                 print STDERR "execute_diffs: Target $target is not auto-diffable.\n";
    1433             }
     1440                if ($debug == 1) {
     1441                    print STDERR "execute_diffs: Target $target in filter $filter on $date has exposures and will be queued.\n";
     1442                }
     1443                diff_queue($date,$target,$filter,$maxFWHM,$pretend);
     1444
     1445                #Queue up desperate diffs if the conditions are right for them
     1446                if ((defined($science_config{$target}{DESPERATE_DIFFS})) && ($science_config{$target}{DESPERATE_DIFFS} == 1)) {
     1447                    desperate_diff_singles($date,$target,$filter,$maxFWHM,$pretend);
     1448                }
     1449
     1450                if (defined($pretend)) {
     1451                    add_to_macro_list('check_diffs',$science_config{$target}{DIFFABLE},$date,$target,$filter);
     1452                }
     1453                else {
     1454                    add_to_macro_list('queue_diffs',$science_config{$target}{DIFFABLE},$date,$target,$filter);
     1455                }
     1456            }
     1457        }
     1458        else {
     1459            if ($debug == 1) {
     1460                print STDERR "execute_diffs: Target $target is not auto-diffable.\n";
     1461            }
    14341462        }
    14351463    }
    14361464    if ($debug == 1) {
    1437         print "$metadata_out{nsObservingState} $metadata_out{nsDiffPotential} $metadata_out{nsDiffQueued} $is_processing $is_registering\n";
     1465        print "$metadata_out{nsObservingState} $metadata_out{nsDiffPotential} $metadata_out{nsDiffQueued} $is_processing $is_registering\n";
    14381466    }
    14391467
    14401468    if ($metadata_out{nsObservingState} eq 'END_OF_NIGHT') {
    1441         if ($is_processing == 1) {
    1442             $metadata_out{nsDiffState} = 'DIFFING';
    1443         }
    1444         elsif ($is_registering == 0) {
    1445             if ($Npotential == $Nnoexp) {
    1446                 $metadata_out{nsDiffState} = 'FINISHED_DIFFS';
    1447             }
    1448             elsif ($metadata_out{nsDiffPotential} == $metadata_out{nsDiffQueued}) {
    1449                 $metadata_out{nsDiffState} = 'FINISHED_DIFFS';
    1450             }
    1451         }
    1452         else {
    1453             $metadata_out{nsDiffState} = 'DIFFING';
    1454         }
     1469        if ($is_processing == 1) {
     1470            $metadata_out{nsDiffState} = 'DIFFING';
     1471        }
     1472        elsif ($is_registering == 0) {
     1473            if ($Npotential == $Nnoexp) {
     1474                $metadata_out{nsDiffState} = 'FINISHED_DIFFS';
     1475            }
     1476            elsif ($metadata_out{nsDiffPotential} == $metadata_out{nsDiffQueued}) {
     1477                $metadata_out{nsDiffState} = 'FINISHED_DIFFS';
     1478            }
     1479        }
     1480        else {
     1481            $metadata_out{nsDiffState} = 'DIFFING';
     1482        }
    14551483    }
    14561484    else {
    1457         $metadata_out{nsDiffState} = 'DIFFING';
     1485        $metadata_out{nsDiffState} = 'DIFFING';
    14581486    }
    14591487    if ($metadata_out{nsDiffState} eq 'FINISHED_DIFFS') {
    1460         foreach my $target (sort (keys %science_config)) {
    1461             # CZW: We should only arrive at this point once, so we can construct a diff report and mail it now.
    1462             if ((defined($science_config{$target}{DESPERATE_DIFFS})) && ($science_config{$target}{DESPERATE_DIFFS} == 1)) {
    1463                 foreach my $filter (@filter_list) {
    1464                     desperate_diff_queue($date,$target,$filter,$pretend);
    1465                 }
    1466             }
    1467 
    1468             if ((defined($science_config{$target}{SELF_WSDIFFS})) && ($science_config{$target}{SELF_WSDIFFS} == 1)) {
    1469                 foreach my $filter (@filter_list) {
    1470                     # This one needs to return a state to see if we need to wait on stacking before checking again.
    1471                     $metadata_out{nsDiffState} = self_WS_diff_queue($date,$target,$filter,$pretend);
    1472                 }
    1473             }
    1474         }
     1488        foreach my $target (sort (keys %science_config)) {
     1489
     1490            my $config_reduction = "DEFAULT";
     1491            if($science_config{$target}{REDUCTION}) {$config_reduction=$science_config{$target}{REDUCTION};}
     1492            print STDERR "execute_diffs: Trying target $target on $date $camera $science_config{$target}{REDUCTION} $config_reduction\n";
     1493
     1494            my $recipe_psastro = $ipprcam->reduction($config_reduction, 'PSASTRO'); # Recipe to use
     1495            &my_die("Unrecognised PSASTRO recipe", $PS_EXIT_CONFIG_ERROR) unless defined $recipe_psastro;
     1496
     1497            my $recipeData;
     1498            {
     1499                # Get the PSASTRO recipe
     1500                my $command = "$ppConfigDump -camera $camera -recipe PSASTRO $recipe_psastro -dump-recipe PSASTRO -";
     1501                my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1502                    run(command => $command);
     1503                unless ($success) {
     1504                    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1505                    &my_die("Unable to perform ppConfigDump: $error_code", $PS_EXIT_CONFIG_ERROR);
     1506                }
     1507                $recipeData = $mdcParser->parse(join "", @$stdout_buf) or
     1508                    &my_die("Unable to parse metadata config doc", $PS_EXIT_CONFIG_ERROR);
     1509            }
     1510
     1511            my $maxFWHM = metadataLookupF32($recipeData, 'PSASTRO.MAX.ALLOWED.FWHM');
     1512
     1513            # CZW: We should only arrive at this point once, so we can construct a diff report and mail it now.
     1514            if ((defined($science_config{$target}{DESPERATE_DIFFS})) && ($science_config{$target}{DESPERATE_DIFFS} == 1)) {
     1515                foreach my $filter (@filter_list) {
     1516                    desperate_diff_queue($date,$target,$filter,$maxFWHM,$pretend);
     1517                }
     1518            }
     1519
     1520            if ((defined($science_config{$target}{SELF_WSDIFFS})) && ($science_config{$target}{SELF_WSDIFFS} == 1)) {
     1521                foreach my $filter (@filter_list) {
     1522                    # This one needs to return a state to see if we need to wait on stacking before checking again.
     1523                    $metadata_out{nsDiffState} = self_WS_diff_queue($date,$target,$filter,$pretend);
     1524                }
     1525            }
     1526        }
    14751527    }
    14761528
    14771529#     if (($Npotential == $Nnoexp)&&($metadata_out{nsObservingState} eq 'END_OF_NIGHT')&&($is_processing == 0)) {
    1478 #       $metadata_out{nsDiffState} = 'FINISHED_DIFFS';
    1479 #     } 
     1530#       $metadata_out{nsDiffState} = 'FINISHED_DIFFS';
     1531#     }
    14801532
    14811533#     if ($is_processing == 1) {
    1482 #       $metadata_out{nsDiffState} = 'DIFFING';
     1534#       $metadata_out{nsDiffState} = 'DIFFING';
    14831535#     }
    14841536}
     
    14881540    my $target = shift;
    14891541    my $filter = shift;
     1542    my $maxFWHM = shift;
    14901543    my $pretend = shift;
    14911544    my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
     
    15001553    my $Npotential = 0;
    15011554    my $Nqueued = 0;
    1502     
     1555   
    15031556    foreach my $object_row (@{ $object_ref }) {
    1504         my $this_object = shift @{ $object_row };       
    1505         my $this_chunk = shift @{ $object_row };
    1506         my $input_sth = "select exp_id,warp_id,dateobs,rawExp.comment,warpRun.state AS warp_state,camProcessedExp.quality FROM ";
    1507         $input_sth .=   " rawExp LEFT JOIN chipRun USING (exp_id) LEFT JOIN camRun USING (chip_id) LEFT JOIN camProcessedExp USING(cam_id) LEFT JOIN fakeRun USING (cam_id) LEFT JOIN warpRun USING (fake_id) ";
    1508         $input_sth .=   " WHERE chipRun.label = '$label' AND chipRun.data_group = '$data_group' AND rawExp.filter = '$filter' AND rawExp.object = '$this_object' ";
    1509         $input_sth .=   " AND substr(rawExp.comment, 1, position(' ' in rawExp.comment)) = '$this_chunk' ORDER BY dateobs ";
    1510 
    1511         my $warps = $db->selectall_arrayref( $input_sth );
    1512 
    1513         # Each comment should only appear once. Therefore, if we see it more than once, we assume the first is extra.
    1514         my %comment_hash = ();
    1515         foreach my $this_warp (@{ $warps }) {
    1516             my $this_comment = ${ $this_warp }[3];
    1517             my $this_exp_id  = ${ $this_warp }[0];
    1518             my $this_quality = ${ $this_warp }[5];
    1519             my $this_state   = ${ $this_warp }[4];
    1520             if (($this_quality != 0) || ($this_state eq 'drop') ) {
    1521                 print STDERR "diff_queue: excluding $this_exp_id for $this_object due to non-zero cam.quality $this_quality or state $this_state\n";
    1522             }
    1523             else {
    1524                 $comment_hash{$this_comment} = $this_exp_id;
    1525             }
    1526         }
    1527         # Exclude any warps that are not stored in the comment_hash.
    1528         my @keep_warps = ();
    1529         foreach my $this_warp (@{ $warps }) {
    1530             my $this_comment = ${ $this_warp }[3];
    1531             my $this_exp_id  = ${ $this_warp }[0];
    1532             if ((exists($comment_hash{$this_comment}))&&
    1533                 ($comment_hash{$this_comment} == $this_exp_id)) {
    1534                 push @keep_warps, $this_warp;
    1535             }
    1536             else {
    1537                 print STDERR "diff_queue: excluding $this_exp_id for $this_object due to not being an accepted comment string $this_comment\n";
    1538             }
    1539         }
    1540         @{ $warps } = @keep_warps;
    1541 
    1542         # Exclude the last entry if we do not have an even number of warps.
    1543         if (($#{ $warps } + 1) % 2 != 0) {
    1544             print STDERR "diff_queue: Number of input warps to make diffs is not even for target $target and object $this_object! $#{ $warps } ";
    1545             if ($#{ $warps} + 1 == 1) {
    1546                 print STDERR ": I can do no diffs with only one exposure.\n";
    1547                 next;
    1548             }
    1549             else {
    1550                 my $rejected_warp = pop @{ $warps };
    1551                 my $rejected_exp_id = ${ $rejected_warp }[0];
    1552                 print STDERR ": Rejecting ${rejected_exp_id} to force visit count.\n";
    1553             }
    1554         }
    1555        
    1556         while ($#{ $warps } > -1) {
    1557             # The array is sorted in pairs of input/template.
    1558             my $input_warp = shift @{ $warps };
    1559             my $template_warp = shift @{ $warps };
    1560 
    1561             my $input_exp_id = ${ $input_warp }[0];
    1562             my $input_comment = ${ $input_warp }[3];
    1563 
    1564             my $template_exp_id = ${ $template_warp }[0];
    1565             my $template_comment = ${ $template_warp }[3];
    1566 
    1567             my $input_warp_id = ${ $input_warp }[1];
    1568             my $template_warp_id = ${ $template_warp }[1];
    1569 
    1570             my $input_warp_state = ${ $input_warp }[4];
    1571             my $template_warp_state = ${ $template_warp }[4];
    1572 
    1573             my $input_warp_camQuality = ${ $input_warp }[5];
    1574             my $template_warp_camQuality = ${ $template_warp }[5];
    1575 
    1576             $Npotential++;
    1577 
    1578             unless (defined($input_warp_id) && defined($template_warp_id) &&
    1579                     ($input_warp_state eq 'full')&&($template_warp_state eq 'full')) {
    1580                 print STDERR "Diff for this $date $target $input_exp_id $template_exp_id not fully processed ($input_warp_state $template_warp_state) ($input_warp_camQuality $template_warp_camQuality)\n";
    1581                 if (($input_warp_camQuality == 4007)||($template_warp_camQuality == 4007)) {
    1582                     # This should now never be reached.
    1583                     # CZW: Trigger backup plan here?  Or simply set up framework?
    1584                     print STDERR "  ...but this is due to a camera stage astrometry quality\n";
    1585                     $Npotential--;
    1586                 }
    1587                 next;
    1588             }
    1589 
    1590             if (verify_uniqueness_diff($input_warp_id,$template_warp_id,$date,$target) != 0) {
    1591                 $Nqueued++;
    1592                 print STDERR "Diffs already queued for this $date $target $input_exp_id $template_exp_id ($input_warp_id $template_warp_id) $this_object $input_comment $template_comment\n";
    1593                 next;
    1594             }
    1595             else {
    1596                 print STDERR "Preparing to diff $date $target $input_exp_id $template_exp_id ($input_warp_id $template_warp_id) $this_object $input_comment $template_comment\n";
    1597             }
     1557        my $this_object = shift @{ $object_row };       
     1558        my $this_chunk = shift @{ $object_row };
     1559        my $input_sth = "select exp_id,warp_id,dateobs,rawExp.comment,warpRun.state AS warp_state,camProcessedExp.quality,camProcessedExp.fwhm_major,chipRun.state,camRun.state FROM ";
     1560        $input_sth .=   " rawExp LEFT JOIN chipRun USING (exp_id) LEFT JOIN camRun USING (chip_id) LEFT JOIN camProcessedExp USING(cam_id) LEFT JOIN fakeRun USING (cam_id) LEFT JOIN warpRun USING (fake_id) ";
     1561        $input_sth .=   " WHERE chipRun.label = '$label' AND chipRun.data_group = '$data_group' AND rawExp.filter = '$filter' AND rawExp.object = '$this_object' ";
     1562        $input_sth .=   " AND substr(rawExp.comment, 1, position(' ' in rawExp.comment)) = '$this_chunk' ORDER BY rawExp.comment,dateobs ";
     1563
     1564        my $warps = $db->selectall_arrayref( $input_sth );
     1565
     1566        # Each comment should only appear once. Therefore, if we see it more than once, we assume the first is extra.
     1567        my %comment_hash = ();
     1568        my $Nbad = 0;
     1569        foreach my $this_warp (@{ $warps }) {
     1570            my $this_comment = ${ $this_warp }[3];
     1571            my $this_exp_id  = ${ $this_warp }[0];
     1572            my $this_quality = ${ $this_warp }[5];
     1573            my $this_state   = ${ $this_warp }[4];
     1574            my $this_warp_id  = ${ $this_warp }[1];
     1575            my $this_fwhm   = ${ $this_warp }[6];
     1576            my $chip_state   = ${ $this_warp }[7];
     1577            my $cam_state   = ${ $this_warp }[8];
    15981578           
    1599             my $cmd = "$difftool -dbname $dbname  -definewarpwarp  ";
    1600             $cmd .= "-input_label $label  -template_label $label -good_frac 0.1 ";
    1601             $cmd .= "-backwards "; # Needed because difftool assumes a different date sorting.
    1602             $cmd .= "-set_workdir $workdir  -set_dist_group $dist_group  -set_data_group $data_group ";
    1603             $cmd .= " -simple  -set_label $label -exp_id $input_exp_id -template_exp_id $template_exp_id ";
    1604 #               $cmd .= " -pretend ";
    1605             if (defined($reduction)) {
    1606                 $cmd .= " -set_reduction $reduction ";
    1607             }
    1608 
    1609             if (defined($pretend)) {
    1610                 $cmd .= ' -pretend ';
    1611             }
    1612             if ($debug == 1) {
    1613                 $cmd .= ' -pretend ';
    1614                 print STDERR "diff_queue: $cmd\n";
    1615                 print STDERR " $input_warp_id $template_warp_id\n";
    1616             }
    1617            
    1618             if (($debug == 0)&&(!defined($pretend))) {
    1619                 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1620                     run ( command => $cmd, verbose => $verbose );
    1621                 unless ($success) {
    1622                     $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1623                     &my_die("Unable to perform difftool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
    1624                 }
    1625                 $Nqueued++;
    1626             }
    1627         }
     1579            #find exposures for this object that have not been fully processed
     1580            if (($this_warp_id eq 'NULL') && (($this_quality == 0) || ($this_quality eq 'NULL')) ) {
     1581                $Nbad += 1;
     1582            }
     1583            if (($chip_state ne 'full') || ($cam_state ne 'full') ) {
     1584                $Nbad += 1;
     1585            }
     1586
     1587            if (($this_quality != 0) || ($this_state eq 'drop') || ($this_fwhm > $maxFWHM)) {
     1588                print STDERR "diff_queue: excluding $this_exp_id for $this_object due to non-zero cam.quality $this_quality or state $this_state or FWHM $this_fwhm\n";
     1589            }
     1590            else {
     1591                $comment_hash{$this_comment} = $this_exp_id;
     1592            }
     1593        }
     1594
     1595        #kick object out of diff consideration if it has exposures not fully processed
     1596        if ($Nbad > 0) {
     1597            print STDERR "diff_queue: excluding $this_object from making diffs due to not being fully processed to warp stage\n";
     1598            next;
     1599        }
     1600
     1601        # Exclude any warps that are not stored in the comment_hash.
     1602        my @keep_warps = ();
     1603        foreach my $this_warp (@{ $warps }) {
     1604            my $this_comment = ${ $this_warp }[3];
     1605            my $this_exp_id  = ${ $this_warp }[0];
     1606            if ((exists($comment_hash{$this_comment}))&&
     1607                ($comment_hash{$this_comment} == $this_exp_id)) {
     1608                push @keep_warps, $this_warp;
     1609            }
     1610            else {
     1611                print STDERR "diff_queue: excluding $this_exp_id for $this_object due to not being an accepted comment string $this_comment\n";
     1612            }
     1613        }
     1614        @{ $warps } = @keep_warps;
     1615
     1616        # Exclude the last entry if we do not have an even number of warps.
     1617        if (($#{ $warps } + 1) % 2 != 0) {
     1618            print STDERR "diff_queue: Number of input warps to make diffs is not even for target $target and object $this_object! $#{ $warps } ";
     1619            if ($#{ $warps} + 1 == 1) {
     1620                print STDERR ": I can do no diffs with only one exposure.\n";
     1621                next;
     1622            }
     1623            else {
     1624                my $rejected_warp = pop @{ $warps };
     1625                my $rejected_exp_id = ${ $rejected_warp }[0];
     1626                print STDERR ": Rejecting ${rejected_exp_id} to force visit count.\n";
     1627            }
     1628        }
     1629       
     1630        while ($#{ $warps } > -1) {
     1631            # The array is sorted in pairs of input/template.
     1632            my $input_warp = shift @{ $warps };
     1633            my $template_warp = shift @{ $warps };
     1634
     1635            my $input_exp_id = ${ $input_warp }[0];
     1636            my $input_comment = ${ $input_warp }[3];
     1637
     1638            my $template_exp_id = ${ $template_warp }[0];
     1639            my $template_comment = ${ $template_warp }[3];
     1640
     1641            my $input_warp_id = ${ $input_warp }[1];
     1642            my $template_warp_id = ${ $template_warp }[1];
     1643
     1644            my $input_warp_state = ${ $input_warp }[4];
     1645            my $template_warp_state = ${ $template_warp }[4];
     1646
     1647            my $input_warp_camQuality = ${ $input_warp }[5];
     1648            my $template_warp_camQuality = ${ $template_warp }[5];
     1649
     1650            $Npotential++;
     1651
     1652            unless (defined($input_warp_id) && defined($template_warp_id) &&
     1653                    ($input_warp_state eq 'full')&&($template_warp_state eq 'full')) {
     1654                print STDERR "Diff for this $date $target $input_exp_id $template_exp_id not fully processed ($input_warp_state $template_warp_state) ($input_warp_camQuality $template_warp_camQuality)\n";
     1655                if (($input_warp_camQuality == 4007)||($template_warp_camQuality == 4007)) {
     1656                    # This should now never be reached.
     1657                    # CZW: Trigger backup plan here?  Or simply set up framework?
     1658                    print STDERR "  ...but this is due to a camera stage astrometry quality\n";
     1659                    $Npotential--;
     1660                }
     1661                next;
     1662            }
     1663
     1664            if (verify_uniqueness_diff($input_warp_id,$template_warp_id,$date,$target) != 0) {
     1665                $Nqueued++;
     1666                print STDERR "Diffs already queued for this $date $target $input_exp_id $template_exp_id ($input_warp_id $template_warp_id) $this_object $input_comment $template_comment\n";
     1667                next;
     1668            }
     1669            else {
     1670                print STDERR "Preparing to diff $date $target $input_exp_id $template_exp_id ($input_warp_id $template_warp_id) $this_object $input_comment $template_comment\n";
     1671            }
     1672           
     1673            my $cmd = "$difftool -dbname $dbname  -definewarpwarp  ";
     1674            $cmd .= "-input_label $label  -template_label $label -good_frac 0.1 ";
     1675            $cmd .= "-backwards "; # Needed because difftool assumes a different date sorting.
     1676            $cmd .= "-set_workdir $workdir  -set_dist_group $dist_group  -set_data_group $data_group ";
     1677            $cmd .= " -simple  -set_label $label -exp_id $input_exp_id -template_exp_id $template_exp_id ";
     1678#               $cmd .= " -pretend ";
     1679            if (defined($reduction)) {
     1680                $cmd .= " -set_reduction $reduction ";
     1681            }
     1682
     1683            if (defined($pretend)) {
     1684                $cmd .= ' -pretend ';
     1685            }
     1686            if ($debug == 1) {
     1687                $cmd .= ' -pretend ';
     1688                print STDERR "diff_queue: $cmd\n";
     1689                print STDERR " $input_warp_id $template_warp_id\n";
     1690            }
     1691           
     1692            if (($debug == 0)&&(!defined($pretend))) {
     1693                my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1694                    run ( command => $cmd, verbose => $verbose );
     1695                unless ($success) {
     1696                    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1697                    &my_die("Unable to perform difftool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
     1698                }
     1699                $Nqueued++;
     1700            }
     1701        }
    16281702    }
    16291703    $metadata_out{nsDiffPotential} += $Npotential;
    16301704    $metadata_out{nsDiffQueued}    += $Nqueued;
    16311705#      if (($metadata_out{nsDiffPotential} == $metadata_out{nsDiffQueued})&&($metadata_out{nsObservingState} eq 'END_OF_NIGHT')) {
    1632      $metadata_out{nsDiffState} = 'FINISHED_DIFFS';
    1633 #      }       
     1706     $metadata_out{nsDiffState} = 'FINISHED_DIFFS';
     1707#      }       
    16341708
    16351709}
     
    16391713    my $target = shift;
    16401714    my $filter = shift;
     1715    my $maxFWHM = shift;
    16411716    my $pretend = shift;
    16421717    my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
     
    16551730    my $Nqueued = 0;
    16561731
    1657     #Before considering making diffs, we should check if there are no issues with summit download and/or registration. 
     1732    #Before considering making diffs, we should check if there are no issues with summit download and/or registration.
    16581733    #summitExp has limited information, so see if the number of exps in rawExp and summitExp match up before most recent dateobs of chunk
    16591734    #be careful here, since dateobs is not quite completely the same in both tables. Therefore, allow numbers to be off by 1
     
    16731748        $rawdate=~/^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/;
    16741749        my $date1=DateTime->new(year=>$1,month=>$2,day=>$3,hour=>$4,minute=>$5,second=>$6,time_zone=>"UTC");
    1675   
     1750 
    16761751        $summitdate=~/^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/;
    16771752        my $date2=DateTime->new(year=>$1,month=>$2,day=>$3,hour=>$4,minute=>$5,second=>$6,time_zone=>"UTC");
     
    16861761    if (($summittimediff > 10.)||($noff >10)) {
    16871762        if ($debug == 1) {
    1688         print STDERR "desp_diff_singles: No desperate diffs will be attempted, since the number of exps at the summit and in rawExp do not match ($nsummitexps $nrawexps), or the timestamps are off ($summittimediff)\n";
    1689         }
    1690     }
    1691     else {    
     1763        print STDERR "desp_diff_singles: No desperate diffs will be attempted, since the number of exps at the summit and in rawExp do not match ($nsummitexps $nrawexps), or the timestamps are off ($summittimediff)\n";
     1764        }
     1765    }
     1766    else {   
    16921767        foreach my $object_row (@{ $object_ref }) {
    1693             my $this_object = shift @{ $object_row };
    1694             my $this_chunk = shift @{ $object_row };
    1695        
    1696             my $input_sth = "select exp_id,warp_id,dateobs,rawExp.comment,warpRun.state AS warp_state,camProcessedExp.quality,substr(comment, 1, position(' ' in comment)) FROM ";
    1697             $input_sth .=   " rawExp LEFT JOIN chipRun USING (exp_id) LEFT JOIN camRun USING (chip_id) LEFT JOIN camProcessedExp USING(cam_id) LEFT JOIN fakeRun USING (cam_id) LEFT JOIN warpRun USING (fake_id) ";
    1698             $input_sth .=   " WHERE chipRun.label = '$label' AND chipRun.data_group = '$data_group' AND rawExp.filter = '$filter' AND rawExp.object = '$this_object' ";
    1699             $input_sth .=   " AND substr(rawExp.comment, 1, position(' ' in rawExp.comment)) = '$this_chunk' ORDER BY dateobs ";
    1700 
    1701             my $warps = $db->selectall_arrayref( $input_sth );
    1702 
    1703             # Each comment should only appear once. Therefore, if we see it more than once, we assume the first is extra.
    1704             my %comment_hash = ();
    1705             foreach my $this_warp (@{ $warps }) {
    1706                 my $this_comment = ${ $this_warp }[3];
    1707                 my $this_exp_id  = ${ $this_warp }[0];
    1708                 my $this_quality = ${ $this_warp }[5];
    1709                 my $this_state   = ${ $this_warp }[4];
    1710                 if (($this_quality != 0) || ($this_state eq 'drop') ) {
    1711                     print STDERR "desp_diff_singles: excluding $this_exp_id for $this_object due to non-zero cam.quality $this_quality or state $this_state\n";
    1712                 }
    1713                 else {
    1714                     $comment_hash{$this_comment} = $this_exp_id;
    1715                 }
    1716             }
    1717             # Exclude any warps that are not stored in the comment_hash.
    1718             my @keep_warps = ();
    1719             foreach my $this_warp (@{ $warps }) {
    1720                 my $this_comment = ${ $this_warp }[3];
    1721                 my $this_exp_id  = ${ $this_warp }[0];
    1722                 if ((exists($comment_hash{$this_comment}))&&
    1723                     ($comment_hash{$this_comment} == $this_exp_id)) {
    1724                     push @keep_warps, $this_warp;
     1768            my $this_object = shift @{ $object_row };
     1769            my $this_chunk = shift @{ $object_row };
     1770       
     1771            my $input_sth = "select exp_id,warp_id,dateobs,rawExp.comment,warpRun.state AS warp_state,camProcessedExp.quality,camProcessedExp.fwhm_major,chipRun.state,camRun.state FROM ";
     1772            $input_sth .=   " rawExp LEFT JOIN chipRun USING (exp_id) LEFT JOIN camRun USING (chip_id) LEFT JOIN camProcessedExp USING(cam_id) LEFT JOIN fakeRun USING (cam_id) LEFT JOIN warpRun USING (fake_id) ";
     1773            $input_sth .=   " WHERE chipRun.label = '$label' AND chipRun.data_group = '$data_group' AND rawExp.filter = '$filter' AND rawExp.object = '$this_object' ";
     1774            $input_sth .=   " AND substr(rawExp.comment, 1, position(' ' in rawExp.comment)) = '$this_chunk' ORDER BY rawExp.comment,dateobs ";
     1775
     1776            my $warps = $db->selectall_arrayref( $input_sth );
     1777            my $warpsBQ = $db->selectall_arrayref( $input_sth );;
     1778
     1779            # Each comment should only appear once. Therefore, if we see it more than once, we assume the first is extra.
     1780            my %comment_hash = ();
     1781            my %comment_hash_good = ();
     1782            my $Nbad = 0;
     1783            foreach my $this_warp (@{ $warps }) {
     1784                my $this_comment = ${ $this_warp }[3];
     1785                my $this_exp_id  = ${ $this_warp }[0];
     1786                my $this_quality = ${ $this_warp }[5];
     1787                my $this_state   = ${ $this_warp }[4];
     1788                my $this_warp_id  = ${ $this_warp }[1];
     1789                my $this_fwhm   = ${ $this_warp }[6];
     1790                my $chip_state   = ${ $this_warp }[7];
     1791                my $cam_state   = ${ $this_warp }[8];
     1792
     1793                #find exposures for this object that have not been fully processed
     1794                if (($this_warp_id eq 'NULL') && (($this_quality == 0) || ($this_quality eq 'NULL')) ) {
     1795                    $Nbad += 1;
     1796                }
     1797                if (($chip_state ne 'full') || ($cam_state ne 'full') ) {
     1798                    $Nbad += 1;
     1799                }
     1800
     1801                if (($this_quality != 0) || ($this_state eq 'drop') || ($this_fwhm > $maxFWHM)) {
     1802                    print STDERR "desp_diff_singles: excluding $this_exp_id for $this_object due to non-zero cam.quality $this_quality or state $this_state or FWHM $this_fwhm\n";
     1803                }
     1804                else {
     1805                    $comment_hash_good{$this_comment} = $this_exp_id;
     1806                }
     1807               
     1808                #also save the entries which are ok, but not best quality
     1809                if (($this_quality == 0) && ($this_state ne 'drop') ) {
     1810                    $comment_hash{$this_comment} = $this_exp_id;
     1811                }
     1812            }
     1813
     1814            #kick object out of diff consideration if it has exposures not fully processed
     1815            if ($Nbad > 0) {
     1816                print STDERR "desp_diff_singles: excluding $this_object from making diffs due to not being fully processed to warp stage\n";
     1817                next;
     1818            }
     1819
     1820            # Exclude any warps that are not stored in the comment_hash_good.
     1821            my @keep_warps = ();
     1822            foreach my $this_warp (@{ $warps }) {
     1823                my $this_comment = ${ $this_warp }[3];
     1824                my $this_exp_id  = ${ $this_warp }[0];
     1825                if ((exists($comment_hash_good{$this_comment}))&&
     1826                    ($comment_hash_good{$this_comment} == $this_exp_id)) {
     1827                    push @keep_warps, $this_warp;
    17251828                    $this_date  = ${ $this_warp }[2];
    17261829                    $chunk_name  = ${ $this_warp }[6];
    1727                 }
    1728                 else {
    1729                     print STDERR "desp_diff_singles: excluding $this_exp_id for $this_object due to being rejected $this_comment\n";
    1730                 }
    1731             }
    1732             @{ $warps } = @keep_warps;
    1733 
     1830                }
     1831                else {
     1832                    print STDERR "desp_diff_singles: excluding $this_exp_id for $this_object due to being rejected $this_comment\n";
     1833                }
     1834            }
     1835            @{ $warps } = @keep_warps;
     1836            my $nwarps = ($#{ $warps } + 1);
     1837           
    17341838            #find the time of the most recent exposure in that chunk
    1735             my $input_chunk = "select max(dateobs) FROM rawExp WHERE substr(comment, 1, position(' ' in comment)) = '$chunk_name' AND filter = '$filter' ";
    1736             my $chunk = $db->selectall_arrayref( $input_chunk );
    1737             foreach my $this_chunk (@{ $chunk }) {
    1738                 $this_date = ${ $this_chunk }[0];
     1839            my $input_chunk = "select max(dateobs) FROM rawExp WHERE substr(comment, 1, position(' ' in comment)) = '$chunk_name' AND filter = '$filter' ";
     1840            my $chunk = $db->selectall_arrayref( $input_chunk );
     1841            foreach my $this_chunk (@{ $chunk }) {
     1842                $this_date = ${ $this_chunk }[0];
    17391843            }
    17401844 
    17411845            #compute and store some stats for potential on-the-fly desperate diffs
    1742             my $nwarps = ($#{ $warps } + 1);
    17431846            my $timediff = 0;
    17441847            my $now=DateTime->now;
     
    17521855                }
    17531856            }
    1754 
    1755             #Consider the special case of on-the-fly desperate diffs
    1756             #The conditions are: uneven nr of warps greater than 3, and the most recent observations is more than $desdiffdt hours old
    1757             if (($nwarps % 2 != 0) && ($nwarps >= 3) && ($timediff < $desdiffdt)) {
    1758                 print STDERR "desp_diff_singles: There are potential desperate diffs to be done, but the time criterium is not met.\n";
    1759                 next;
    1760             }
    1761             if (($nwarps % 2 == 0) || ($nwarps == 1) || ($timediff < $desdiffdt)) {
    1762                 next;
    1763             }
    1764 
    1765             # We are attempting to do the missing diffs, so reverse the list of retained warps.
    1766             # Good objects with all visits will be skipped due to the duplicate check.
    1767             # Bad objects will have the earliest visit rejected, and the visits repaired in a way that will produce all desired pairs.
    1768             @{ $warps } = reverse @keep_warps;
    1769 
    1770             # Exclude the last entry if we do not have an even number of warps.
    1771             if (($#{ $warps } + 1) % 2 != 0) {
    1772                 print STDERR "desp_diff_singles: Number of input warps to make diffs is not even for target $target and object $this_object!";
    1773                 my $rejected_warp = pop @{ $warps };
    1774                 my $rejected_exp_id = ${ $rejected_warp }[0];
    1775                 print STDERR ": Rejecting ${rejected_exp_id} to force visit count.\n";
    1776             }
    1777        
    1778             while ($#{ $warps } > -1) {
    1779                 # The array is sorted in pairs of input/template.
    1780                 my $template_warp = shift @{ $warps };
    1781                 my $input_warp = shift @{ $warps };
    1782    
    1783                 my $input_exp_id = ${ $input_warp }[0];
    1784                 my $input_comment = ${ $input_warp }[3];
    1785 
    1786                 my $template_exp_id = ${ $template_warp }[0];
    1787                 my $template_comment = ${ $template_warp }[3];
    1788 
    1789                 my $input_warp_id = ${ $input_warp }[1];
    1790                 my $template_warp_id = ${ $template_warp }[1];
    1791 
    1792                 my $input_warp_state = ${ $input_warp }[4];
    1793                 my $template_warp_state = ${ $template_warp }[4];
    1794 
    1795                 my $input_warp_camQuality = ${ $input_warp }[5];
    1796                 my $template_warp_camQuality = ${ $template_warp }[5];
    1797 
    1798                 $Npotential++;
    1799 
    1800                 unless (defined($input_warp_id) && defined($template_warp_id) &&
    1801                     ($input_warp_state eq 'full')&&($template_warp_state eq 'full')) {
    1802                     print STDERR "Desp diff for this $date $target $input_exp_id $template_exp_id not fully processed ($input_warp_state $template_warp_state) ($input_warp_camQuality $template_warp_camQuality)\n";
    1803                     if (($input_warp_camQuality == 4007)||($template_warp_camQuality == 4007)) {
    1804                         # This should now never be reached.
    1805                         # CZW: Trigger backup plan here?  Or simply set up framework?
    1806                         print STDERR "  ...but this is due to a camera stage astrometry quality\n";
    1807                         $Npotential--;
    1808                     }
    1809                     next;
    1810                 }
    1811 
    1812                 if (verify_uniqueness_diff($input_warp_id,$template_warp_id,$date,$target) != 0) {
    1813                     $Nqueued++;
    1814                     print STDERR "Desp diffs already queued for this $date $target $input_exp_id $template_exp_id ($input_warp_id $template_warp_id) $this_object $input_comment $template_comment\n";
    1815                     next;
    1816                 }
    1817                 else {
    1818                     print STDERR "Preparing to diff $date $target $input_exp_id $template_exp_id ($input_warp_id $template_warp_id) $this_object $input_comment $template_comment\n";
    1819                 }
    1820            
    1821                 my $cmd = "$difftool -dbname $dbname  -definewarpwarp  ";
    1822                 $cmd .= "-input_label $label  -template_label $label -good_frac 0.1 ";
    1823                 $cmd .= "-backwards "; # Needed because difftool assumes a different date sorting.
    1824                 $cmd .= "-rerun "; # Needed because we may have some diffs that already use some of the exposures
    1825                 $cmd .= "-set_workdir $workdir  -set_dist_group $dist_group  -set_data_group $data_group ";
    1826                 $cmd .= " -simple  -set_label $label -exp_id $input_exp_id -template_exp_id $template_exp_id ";
    1827                 if (defined($reduction)) {
    1828                     $cmd .= " -set_reduction $reduction ";
    1829                 }
    1830        
    1831                 if (defined($pretend)) {
    1832                     $cmd .= ' -pretend ';
    1833                 }
    1834                 if ($debug == 1) {
    1835                     $cmd .= ' -pretend ';
    1836                     print STDERR "desp_diff_singles: $cmd\n";
    1837                     print STDERR " $input_warp_id $template_warp_id\n";
    1838                 }
    1839            
    1840                 if (($debug == 0)&&(!defined($pretend))) {
    1841                     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1842                         run ( command => $cmd, verbose => $verbose );
    1843                     unless ($success) {
    1844                         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1845                         &my_die("Unable to perform difftool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
    1846                     }
    1847                     $Nqueued++;
    1848                 }
    1849             }
     1857           
     1858            ################################
     1859            #We reach a junction here, where having more than 3 (but uneven) warps will result in desperate diffs
     1860            #Conversely, less than 3 good quality warps might result in dirty diffs
     1861            ################################
     1862            if($nwarps >= 3) {     
     1863                #Consider the special case of on-the-fly desperate diffs
     1864                #The conditions are: uneven nr of warps greater than 3, and the most recent observations is more than $desdiffdt hours old
     1865                if (($nwarps % 2 != 0) && ($nwarps >= 3) && ($timediff < $desdiffdt)) {
     1866                    print STDERR "desp_diff_singles: There are potential desperate diffs to be done, but the time criterium is not met.\n";
     1867                    next;
     1868                }
     1869                if (($nwarps % 2 == 0) || ($nwarps == 1) || ($timediff < $desdiffdt)) {
     1870                    next;
     1871                }
     1872
     1873                # We are attempting to do the missing diffs, so reverse the list of retained warps.
     1874                # Good objects with all visits will be skipped due to the duplicate check.
     1875                # Bad objects will have the earliest visit rejected, and the visits repaired in a way that will produce all desired pairs.
     1876                @{ $warps } = reverse @keep_warps;
     1877
     1878                # Exclude the last entry if we do not have an even number of warps.
     1879                if (($#{ $warps } + 1) % 2 != 0) {
     1880                    print STDERR "desp_diff_singles: Number of input warps to make diffs is not even for target $target and object $this_object!";
     1881                    my $rejected_warp = pop @{ $warps };
     1882                    my $rejected_exp_id = ${ $rejected_warp }[0];
     1883                    print STDERR ": Rejecting ${rejected_exp_id} to force visit count.\n";
     1884                }
     1885       
     1886                while ($#{ $warps } > -1) {
     1887                    # The array is sorted in pairs of input/template.
     1888                    my $template_warp = shift @{ $warps };
     1889                    my $input_warp = shift @{ $warps };
     1890   
     1891                    my $input_exp_id = ${ $input_warp }[0];
     1892                    my $input_comment = ${ $input_warp }[3];
     1893
     1894                    my $template_exp_id = ${ $template_warp }[0];
     1895                    my $template_comment = ${ $template_warp }[3];
     1896
     1897                    my $input_warp_id = ${ $input_warp }[1];
     1898                    my $template_warp_id = ${ $template_warp }[1];
     1899
     1900                    my $input_warp_state = ${ $input_warp }[4];
     1901                    my $template_warp_state = ${ $template_warp }[4];
     1902
     1903                    my $input_warp_camQuality = ${ $input_warp }[5];
     1904                    my $template_warp_camQuality = ${ $template_warp }[5];
     1905
     1906                    $Npotential++;
     1907
     1908                    unless (defined($input_warp_id) && defined($template_warp_id) &&
     1909                        ($input_warp_state eq 'full')&&($template_warp_state eq 'full')) {
     1910                        print STDERR "Desp diff for this $date $target $input_exp_id $template_exp_id not fully processed ($input_warp_state $template_warp_state) ($input_warp_camQuality $template_warp_camQuality)\n";
     1911                        if (($input_warp_camQuality == 4007)||($template_warp_camQuality == 4007)) {
     1912                            # This should now never be reached.
     1913                            # CZW: Trigger backup plan here?  Or simply set up framework?
     1914                            print STDERR "  ...but this is due to a camera stage astrometry quality\n";
     1915                            $Npotential--;
     1916                        }
     1917                        next;
     1918                    }
     1919
     1920                    if (verify_uniqueness_diff($input_warp_id,$template_warp_id,$date,$target) != 0) {
     1921                        $Nqueued++;
     1922                        print STDERR "Desp diffs already queued for this $date $target $input_exp_id $template_exp_id ($input_warp_id $template_warp_id) $this_object $input_comment $template_comment\n";
     1923                        next;
     1924                    }
     1925                    else {
     1926                        print STDERR "Preparing to single desperate diff $date $target $input_exp_id $template_exp_id ($input_warp_id $template_warp_id) $this_object $input_comment $template_comment\n";
     1927                    }
     1928           
     1929                    my $cmd = "$difftool -dbname $dbname  -definewarpwarp  ";
     1930                    $cmd .= "-input_label $label  -template_label $label -good_frac 0.1 ";
     1931                    $cmd .= "-backwards "; # Needed because difftool assumes a different date sorting.
     1932                    $cmd .= "-rerun "; # Needed because we may have some diffs that already use some of the exposures
     1933                    $cmd .= "-set_workdir $workdir  -set_dist_group $dist_group  -set_data_group $data_group ";
     1934                    $cmd .= " -simple  -set_label $label -exp_id $input_exp_id -template_exp_id $template_exp_id ";
     1935                    if (defined($reduction)) {
     1936                        $cmd .= " -set_reduction $reduction ";
     1937                    }
     1938       
     1939                    if (defined($pretend)) {
     1940                        $cmd .= ' -pretend ';
     1941                    }
     1942                    if ($debug == 1) {
     1943                        $cmd .= ' -pretend ';
     1944                        print STDERR "desp_diff_singles: $cmd\n";
     1945                        print STDERR " $input_warp_id $template_warp_id\n";
     1946                    }
     1947           
     1948                    if (($debug == 0)&&(!defined($pretend))) {
     1949                        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1950                            run ( command => $cmd, verbose => $verbose );
     1951                        unless ($success) {
     1952                            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1953                            &my_die("Unable to perform difftool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
     1954                        }
     1955                        $Nqueued++;
     1956                    }
     1957                }           
     1958           
     1959            } else {       
     1960                print STDERR "desp_diff_singles: There is a possibility for dirty diffs to be made\n";     
     1961                #Consider the case of on-the-fly dirty desperate diffs
     1962                #The conditions are: uneven nr of bad quality warps greater than 3, and the most recent observations is more than $desdiffdt hours old
     1963
     1964#                # Exclude any warps that are not stored in the more general comment_hash.
     1965#                my @keep_warps = ();
     1966#                foreach my $this_warp (@{ $warpsBQ }) {
     1967#                    my $this_comment = ${ $this_warp }[3];
     1968#                    my $this_exp_id  = ${ $this_warp }[0];
     1969#                    if ((exists($comment_hash{$this_comment}))&&
     1970#                        ($comment_hash{$this_comment} == $this_exp_id)) {
     1971#                        push @keep_warps, $this_warp;
     1972#                        $this_date  = ${ $this_warp }[2];
     1973#                        $chunk_name  = ${ $this_warp }[6];
     1974#                    }
     1975#                    else {
     1976#                        print STDERR "desp_diff_singles: excluding $this_exp_id for $this_object due to being rejected $this_comment\n";
     1977#                    }
     1978#                }
     1979#                @{ $warpsBQ } = @keep_warps;
     1980#
     1981#                if ($nwarps >= 2) {
     1982#                    #one regular diff has already been made. So, need only one more. Strip a good entry from the array             
     1983#                    my $nrem = 0;
     1984#                    my @keep_warps = ();
     1985#                    foreach my $this_warp (@{ $warpsBQ }) {
     1986#                        my $this_comment = ${ $this_warp }[3];
     1987#                        my $this_exp_id  = ${ $this_warp }[0];
     1988#                        my $this_fwhm   = ${ $this_warp }[7];
     1989#                        if (($nrem <1) && ($this_fwhm <= $maxFWHM)) {
     1990#                            $nrem++;
     1991#                            print STDERR "desp_diff_singles: excluding $this_exp_id for $this_object from dirty desp diffs $this_comment\n";
     1992#                        } else {
     1993#                            push @keep_warps, $this_warp;                       
     1994#                        }
     1995#                    }
     1996#                    @{ $warpsBQ } = @keep_warps;
     1997#                }
     1998#
     1999#                # We are attempting to do the missing diffs, so reverse the list of retained warps.
     2000#                # Good objects with all visits will be skipped due to the duplicate check.
     2001#                # Bad objects will have the earliest visit rejected, and the visits repaired in a way that will produce all desired pairs.
     2002#                @{ $warpsBQ } = reverse @{ $warpsBQ };
     2003#
     2004#                # Exclude the last entry if we do not have an even number of warps.
     2005#                if (($#{ $warpsBQ } + 1) % 2 != 0) {
     2006#                    print STDERR "desp_diff_singles: Number of input warps to make dirty diffs is not even for target $target and object $this_object!";
     2007#                    my $rejected_warp = pop @{ $warpsBQ };
     2008#                    my $rejected_exp_id = ${ $rejected_warp }[0];
     2009#                    print STDERR ": Rejecting ${rejected_exp_id} to force visit count.\n";
     2010#                }
     2011#                my $nwarpsBQ = ($#{$warpsBQ} + 1);
     2012#       
     2013#       
     2014#                if (($nwarpsBQ >= 2) && ($timediff < $desdiffdt)) {
     2015#                    print STDERR "desp_diff_singles: There are potential dirty desperate diffs to be done, but the time criterium is not met.\n";
     2016#                    next;
     2017#                }
     2018#                if (($nwarpsBQ <= 1) || ($timediff < $desdiffdt)) {
     2019#                    next;
     2020#                }
     2021#       
     2022#                while ($#{ $warpsBQ } > -1) {
     2023#                    # The array is sorted in pairs of input/template.
     2024#                    my $template_warp = shift @{ $warpsBQ };
     2025#                    my $input_warp = shift @{ $warpsBQ };
     2026#   
     2027#                    my $input_exp_id = ${ $input_warp }[0];
     2028#                    my $input_comment = ${ $input_warp }[3];
     2029#
     2030#                    my $template_exp_id = ${ $template_warp }[0];
     2031#                    my $template_comment = ${ $template_warp }[3];
     2032#
     2033#                    my $input_warp_id = ${ $input_warp }[1];
     2034#                    my $template_warp_id = ${ $template_warp }[1];
     2035#
     2036#                    my $input_warp_state = ${ $input_warp }[4];
     2037#                    my $template_warp_state = ${ $template_warp }[4];
     2038#
     2039#                    my $input_warp_camQuality = ${ $input_warp }[5];
     2040#                    my $template_warp_camQuality = ${ $template_warp }[5];
     2041#
     2042#                    $Npotential++;
     2043#
     2044#                    unless (defined($input_warp_id) && defined($template_warp_id) &&
     2045#                        ($input_warp_state eq 'full')&&($template_warp_state eq 'full')) {
     2046#                        print STDERR "Desp diff for this $date $target $input_exp_id $template_exp_id not fully processed ($input_warp_state $template_warp_state) ($input_warp_camQuality $template_warp_camQuality)\n";
     2047#                        if (($input_warp_camQuality == 4007)||($template_warp_camQuality == 4007)) {
     2048#                            # This should now never be reached.
     2049#                            # CZW: Trigger backup plan here?  Or simply set up framework?
     2050#                            print STDERR "  ...but this is due to a camera stage astrometry quality\n";
     2051#                            $Npotential--;
     2052#                        }
     2053#                        next;
     2054#                    }
     2055#
     2056#                    if (verify_uniqueness_diff($input_warp_id,$template_warp_id,$date,$target) != 0) {
     2057#                        $Nqueued++;
     2058#                        print STDERR "Desp diffs already queued for this $date $target $input_exp_id $template_exp_id ($input_warp_id $template_warp_id) $this_object $input_comment $template_comment\n";
     2059#                        next;
     2060#                    }
     2061#                    else {
     2062#                        print STDERR "Preparing to dirty diff $date $target $input_exp_id $template_exp_id ($input_warp_id $template_warp_id) $this_object $input_comment $template_comment\n";
     2063#                    }
     2064#           
     2065#                    my $cmd = "$difftool -dbname $dbname  -definewarpwarp  ";
     2066#                    $cmd .= "-input_label $label  -template_label $label -good_frac 0.1 ";
     2067#                    $cmd .= "-backwards "; # Needed because difftool assumes a different date sorting.
     2068#                    $cmd .= "-rerun "; # Needed because we may have some diffs that already use some of the exposures
     2069#                    $cmd .= "-set_workdir $workdir  -set_dist_group $dist_group  -set_data_group $data_group ";
     2070#                    $cmd .= " -simple  -set_label $label -exp_id $input_exp_id -template_exp_id $template_exp_id ";
     2071#                    if (defined($reduction)) {
     2072#                        $cmd .= " -set_reduction $reduction ";
     2073#                    }
     2074#       
     2075#                    if (defined($pretend)) {
     2076#                        $cmd .= ' -pretend ';
     2077#                    }
     2078#                    if ($debug == 1) {
     2079#                        $cmd .= ' -pretend ';
     2080#                        print STDERR "desp_diff_singles: $cmd\n";
     2081#                        print STDERR " $input_warp_id $template_warp_id\n";
     2082#                    }
     2083#           
     2084#                    if (($debug == 0)&&(!defined($pretend))) {
     2085#                        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     2086#                            run ( command => $cmd, verbose => $verbose );
     2087#                        unless ($success) {
     2088#                            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     2089#                            &my_die("Unable to perform difftool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
     2090#                        }
     2091#                        $Nqueued++;
     2092#                    }
     2093#                }
     2094           
     2095           
     2096           
     2097            }
    18502098        }
    18512099    }
     
    18582106    my $target = shift;
    18592107    my $filter = shift;
     2108    my $maxFWHM = shift;
    18602109    my $pretend = shift;
    18612110    my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
     
    18722121    my $Nqueued = 0;
    18732122
    1874     #Before considering making diffs, we should check if there are no issues with summit download and/or registration. 
     2123    #Before considering making diffs, we should check if there are no issues with summit download and/or registration.
    18752124    #summitExp has limited information, so see if the number of exps in rawExp and summitExp match up before most recent dateobs of chunk
    18762125    #be careful here, since dateobs is not quite completely the same in both tables. Therefore, allow numbers to be off by 1
     
    18902139        $rawdate=~/^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/;
    18912140        my $date1=DateTime->new(year=>$1,month=>$2,day=>$3,hour=>$4,minute=>$5,second=>$6,time_zone=>"UTC");
    1892   
     2141 
    18932142        $summitdate=~/^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/;
    18942143        my $date2=DateTime->new(year=>$1,month=>$2,day=>$3,hour=>$4,minute=>$5,second=>$6,time_zone=>"UTC");
     
    19032152    if (($summittimediff > 10.)||($noff >10)) {
    19042153        if ($debug == 1) {
    1905         print STDERR "desp_diff_queue: No desperate diffs will be attempted, since the number of exps at the summit and in rawExp do not match ($nsummitexps $nrawexps), or the timestamps are off ($summittimediff)\n";
     2154        print STDERR "desp_diff_queue: No desperate diffs will be attempted, since the number of exps at the summit and in rawExp do not match ($nsummitexps $nrawexps), or the timestamps are off ($summittimediff)\n";
    19062155        }
    19072156    }
    19082157    else {
    19092158        foreach my $object_row (@{ $object_ref }) {
    1910             my $this_object = shift @{ $object_row };
    1911             my $this_chunk = shift @{ $object_row };
    1912        
    1913             my $input_sth = "select exp_id,warp_id,dateobs,rawExp.comment,warpRun.state AS warp_state,camProcessedExp.quality FROM ";
    1914             $input_sth .=   " rawExp LEFT JOIN chipRun USING (exp_id) LEFT JOIN camRun USING (chip_id) LEFT JOIN camProcessedExp USING(cam_id) LEFT JOIN fakeRun USING (cam_id) LEFT JOIN warpRun USING (fake_id) ";
    1915             $input_sth .=   " WHERE chipRun.label = '$label' AND chipRun.data_group = '$data_group' AND rawExp.filter = '$filter' AND rawExp.object = '$this_object' ";
    1916             $input_sth .=   " AND substr(rawExp.comment, 1, position(' ' in rawExp.comment)) = '$this_chunk' ORDER BY dateobs ";
    1917 
    1918             my $warps = $db->selectall_arrayref( $input_sth );
    1919 
    1920             # Each comment should only appear once. Therefore, if we see it more than once, we assume the first is extra.
    1921             my %comment_hash = ();
    1922             foreach my $this_warp (@{ $warps }) {
    1923                 my $this_comment = ${ $this_warp }[3];
    1924                 my $this_exp_id  = ${ $this_warp }[0];
    1925                 my $this_quality = ${ $this_warp }[5];
    1926                 my $this_state   = ${ $this_warp }[4];
    1927                 if (($this_quality != 0) || ($this_state eq 'drop') ) {
    1928                     print STDERR "desp_diff_queue: excluding $this_exp_id for $this_object due to non-zero cam.quality $this_quality or state $this_state\n";
    1929                 }
    1930                 else {
    1931                     $comment_hash{$this_comment} = $this_exp_id;
    1932                 }
    1933             }
    1934             # Exclude any warps that are not stored in the comment_hash.
    1935             my @keep_warps = ();
    1936             foreach my $this_warp (@{ $warps }) {
    1937                 my $this_comment = ${ $this_warp }[3];
    1938                 my $this_exp_id  = ${ $this_warp }[0];
    1939                 if ((exists($comment_hash{$this_comment}))&&
    1940                     ($comment_hash{$this_comment} == $this_exp_id)) {
    1941                     push @keep_warps, $this_warp;
    1942                 }
    1943                 else {
    1944                     print STDERR "desp_diff_queue: excluding $this_exp_id for $this_object due to not being an accepted comment string $this_comment\n";
    1945                 }
    1946             }
    1947             # We are attempting to do the missing diffs, so reverse the list of retained warps.
    1948             # Good objects with all visits will be skipped due to the duplicate check.
    1949             # Bad objects will have the earliest visit rejected, and the visits repaired in a way that will produce all desired pairs.
    1950             @{ $warps } = reverse @keep_warps;
    1951                    
    1952             # Exclude the last entry if we do not have an even number of warps.
    1953             if (($#{ $warps } + 1) % 2 != 0) {
    1954                 print STDERR "desp_diff_queue: Number of input warps to make diffs is not even for target $target and object $this_object! $#{ $warps } ";
    1955                 if ($#{ $warps} + 1 == 1) {
    1956                     print STDERR ": I can do no diffs with only one exposure.\n";
    1957                     next;
    1958                 }
    1959                 else {
    1960                     my $rejected_warp = pop @{ $warps };
    1961                     my $rejected_exp_id = ${ $rejected_warp }[0];
    1962                     print STDERR ": Rejecting ${rejected_exp_id} to force visit count.\n";
    1963                 }
    1964             }
    1965        
    1966             while ($#{ $warps } > -1) {
    1967                 # In this mode, the array is sorted in pairs of template/input, counter to the standard queue.
    1968                 my $template_warp = shift @{ $warps };
    1969                 my $input_warp = shift @{ $warps };
    1970 
    1971                 my $input_exp_id = ${ $input_warp }[0];
    1972                 my $input_comment = ${ $input_warp }[3];
    1973 
    1974                 my $template_exp_id = ${ $template_warp }[0];
    1975                 my $template_comment = ${ $template_warp }[3];
    1976 
    1977                 my $input_warp_id = ${ $input_warp }[1];
    1978                 my $template_warp_id = ${ $template_warp }[1];
    1979 
    1980                 my $input_warp_state = ${ $input_warp }[4];
    1981                 my $template_warp_state = ${ $template_warp }[4];
    1982 
    1983                 my $input_warp_camQuality = ${ $input_warp }[5];
    1984                 my $template_warp_camQuality = ${ $template_warp }[5];
    1985 
    1986                 $Npotential++;
    1987 
    1988                 unless (defined($input_warp_id) && defined($template_warp_id) &&
    1989                         ($input_warp_state eq 'full')&&($template_warp_state eq 'full')) {
    1990                     print STDERR "Diff for this $date $target $input_exp_id $template_exp_id not fully processed ($input_warp_state $template_warp_state) ($input_warp_camQuality $template_warp_camQuality)\n";
    1991                     if (($input_warp_camQuality == 4007)||($template_warp_camQuality == 4007)) {
    1992                         # This should now never be reached.
    1993                         # CZW: Trigger backup plan here?  Or simply set up framework?
    1994                         print STDERR "  ...but this is due to a camera stage astrometry quality\n";
    1995                         $Npotential--;
    1996                     }
    1997                     next;
    1998                 }
    1999 
    2000                 if (verify_uniqueness_diff($input_warp_id,$template_warp_id,$date,$target) != 0) {
    2001                     $Nqueued++;
    2002                     print STDERR "Diffs already queued for this $date $target $input_exp_id $template_exp_id ($input_warp_id $template_warp_id) $this_object $input_comment $template_comment\n";
    2003                     next;
    2004                 }
    2005                 else {
    2006                     print STDERR "Preparing to diff $date $target $input_exp_id $template_exp_id ($input_warp_id $template_warp_id) $this_object $input_comment $template_comment\n";
    2007                 }
     2159            my $this_object = shift @{ $object_row };
     2160            my $this_chunk = shift @{ $object_row };
     2161       
     2162            my $input_sth = "select exp_id,warp_id,dateobs,rawExp.comment,warpRun.state AS warp_state,camProcessedExp.quality,camProcessedExp.fwhm_major,chipRun.state,camRun.state FROM ";
     2163            $input_sth .=   " rawExp LEFT JOIN chipRun USING (exp_id) LEFT JOIN camRun USING (chip_id) LEFT JOIN camProcessedExp USING(cam_id) LEFT JOIN fakeRun USING (cam_id) LEFT JOIN warpRun USING (fake_id) ";
     2164            $input_sth .=   " WHERE chipRun.label = '$label' AND chipRun.data_group = '$data_group' AND rawExp.filter = '$filter' AND rawExp.object = '$this_object' ";
     2165            $input_sth .=   " AND substr(rawExp.comment, 1, position(' ' in rawExp.comment)) = '$this_chunk' ORDER BY rawExp.comment,dateobs ";
     2166
     2167            my $warps = $db->selectall_arrayref( $input_sth );
     2168            my $warpsBQ = $db->selectall_arrayref( $input_sth );;
     2169
     2170            # Each comment should only appear once. Therefore, if we see it more than once, we assume the first is extra.
     2171            my %comment_hash = ();
     2172            my %comment_hash_good = ();
     2173            my $Nbad = 0;
     2174            foreach my $this_warp (@{ $warps }) {
     2175                my $this_comment = ${ $this_warp }[3];
     2176                my $this_exp_id  = ${ $this_warp }[0];
     2177                my $this_quality = ${ $this_warp }[5];
     2178                my $this_state   = ${ $this_warp }[4];
     2179                my $this_warp_id  = ${ $this_warp }[1];
     2180                my $this_fwhm    = ${ $this_warp }[6];
     2181                my $chip_state   = ${ $this_warp }[7];
     2182                my $cam_state   = ${ $this_warp }[8];
     2183
     2184                #find exposures for this object that have not been fully processed
     2185                if (($this_warp_id eq 'NULL') && (($this_quality == 0) || ($this_quality eq 'NULL')) ) {
     2186                    $Nbad += 1;
     2187                }
     2188                if (($chip_state ne 'full') || ($cam_state ne 'full') ) {
     2189                    $Nbad += 1;
     2190                }
     2191
     2192                if (($this_quality != 0) || ($this_state eq 'drop') || ($this_fwhm > $maxFWHM)) {
     2193                    print STDERR "desp_diff_queue: excluding $this_exp_id for $this_object due to non-zero cam.quality $this_quality or state $this_state or FWHM $this_fwhm\n";
     2194                }
     2195                else {
     2196                    $comment_hash_good{$this_comment} = $this_exp_id;
     2197                }
     2198               
     2199                #also save the entries which are ok, but not best quality
     2200                if (($this_quality == 0) && ($this_state ne 'drop') ) {
     2201                    $comment_hash{$this_comment} = $this_exp_id;
     2202                }
     2203            }
     2204
     2205            #kick object out of diff consideration if it has exposures not fully processed
     2206            if ($Nbad > 0) {
     2207                print STDERR "desp_diff_queue: excluding $this_object from making diffs due to not being fully processed to warp stage\n";
     2208                next;
     2209            }
     2210
     2211            # Exclude any warps that are not stored in the comment_hash_good.
     2212            my @keep_warps = ();
     2213            foreach my $this_warp (@{ $warps }) {
     2214                my $this_comment = ${ $this_warp }[3];
     2215                my $this_exp_id  = ${ $this_warp }[0];
     2216                if ((exists($comment_hash_good{$this_comment}))&&
     2217                    ($comment_hash_good{$this_comment} == $this_exp_id)) {
     2218                    push @keep_warps, $this_warp;
     2219                    my $this_date  = ${ $this_warp }[2];
     2220                    my $chunk_name  = ${ $this_warp }[6];
     2221                }
     2222                else {
     2223                    print STDERR "desp_diff_queue: excluding $this_exp_id for $this_object due to being rejected $this_comment\n";
     2224                }
     2225            }
     2226            @{ $warps } = @keep_warps;
     2227            my $nwarps = ($#{ $warps } + 1);
     2228
     2229            ################################
     2230            #We reach a junction here, where having more than 3 (but uneven) warps will result in desperate diffs
     2231            #Conversely, less than 3 warps might result in dirty diffs
     2232            ################################
     2233            if($nwarps >= 3) {     
     2234                # We are attempting to do the missing diffs, so reverse the list of retained warps.
     2235                # Good objects with all visits will be skipped due to the duplicate check.
     2236                # Bad objects will have the earliest visit rejected, and the visits repaired in a way that will produce all desired pairs.
     2237                @{ $warps } = reverse @keep_warps;
     2238
     2239                # Exclude the last entry if we do not have an even number of warps.
     2240                if (($#{ $warps } + 1) % 2 != 0) {
     2241                    print STDERR "desp_diff_queue: Number of input warps to make diffs is not even for target $target and object $this_object! $#{ $warps } ";
     2242                    if ($#{ $warps} + 1 == 1) {
     2243                        print STDERR ": I can do no diffs with only one exposure.\n";
     2244                        next;
     2245                    }
     2246                    else {
     2247                        my $rejected_warp = pop @{ $warps };
     2248                        my $rejected_exp_id = ${ $rejected_warp }[0];
     2249                        print STDERR ": Rejecting ${rejected_exp_id} to force visit count.\n";
     2250                    }
     2251                }
     2252       
     2253                while ($#{ $warps } > -1) {
     2254                    # The array is sorted in pairs of input/template.
     2255                    my $template_warp = shift @{ $warps };
     2256                    my $input_warp = shift @{ $warps };
     2257   
     2258                    my $input_exp_id = ${ $input_warp }[0];
     2259                    my $input_comment = ${ $input_warp }[3];
     2260
     2261                    my $template_exp_id = ${ $template_warp }[0];
     2262                    my $template_comment = ${ $template_warp }[3];
     2263
     2264                    my $input_warp_id = ${ $input_warp }[1];
     2265                    my $template_warp_id = ${ $template_warp }[1];
     2266
     2267                    my $input_warp_state = ${ $input_warp }[4];
     2268                    my $template_warp_state = ${ $template_warp }[4];
     2269
     2270                    my $input_warp_camQuality = ${ $input_warp }[5];
     2271                    my $template_warp_camQuality = ${ $template_warp }[5];
     2272
     2273                    $Npotential++;
     2274
     2275                    unless (defined($input_warp_id) && defined($template_warp_id) &&
     2276                        ($input_warp_state eq 'full')&&($template_warp_state eq 'full')) {
     2277                        print STDERR "Desp diff for this $date $target $input_exp_id $template_exp_id not fully processed ($input_warp_state $template_warp_state) ($input_warp_camQuality $template_warp_camQuality)\n";
     2278                        if (($input_warp_camQuality == 4007)||($template_warp_camQuality == 4007)) {
     2279                            # This should now never be reached.
     2280                            # CZW: Trigger backup plan here?  Or simply set up framework?
     2281                            print STDERR "  ...but this is due to a camera stage astrometry quality\n";
     2282                            $Npotential--;
     2283                        }
     2284                        next;
     2285                    }
     2286
     2287                    if (verify_uniqueness_diff($input_warp_id,$template_warp_id,$date,$target) != 0) {
     2288                        $Nqueued++;
     2289                        print STDERR "Desp diffs already queued for this $date $target $input_exp_id $template_exp_id ($input_warp_id $template_warp_id) $this_object $input_comment $template_comment\n";
     2290                        next;
     2291                    }
     2292                    else {
     2293                        print STDERR "Preparing to desperate diff $date $target $input_exp_id $template_exp_id ($input_warp_id $template_warp_id) $this_object $input_comment $template_comment\n";
     2294                    }
     2295           
     2296                    my $cmd = "$difftool -dbname $dbname  -definewarpwarp  ";
     2297                    $cmd .= "-input_label $label  -template_label $label -good_frac 0.1 ";
     2298                    $cmd .= "-backwards "; # Needed because difftool assumes a different date sorting.
     2299                    $cmd .= "-rerun "; # Needed because we may have some diffs that already use some of the exposures
     2300                    $cmd .= "-set_workdir $workdir  -set_dist_group $dist_group  -set_data_group $data_group ";
     2301                    $cmd .= " -simple  -set_label $label -exp_id $input_exp_id -template_exp_id $template_exp_id ";
     2302                    if (defined($reduction)) {
     2303                        $cmd .= " -set_reduction $reduction ";
     2304                    }
     2305       
     2306                    if (defined($pretend)) {
     2307                        $cmd .= ' -pretend ';
     2308                    }
     2309                    if ($debug == 1) {
     2310                        $cmd .= ' -pretend ';
     2311                        print STDERR "desp_diff_queue: $cmd\n";
     2312                        print STDERR " $input_warp_id $template_warp_id\n";
     2313                    }
     2314           
     2315                    if (($debug == 0)&&(!defined($pretend))) {
     2316                        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     2317                            run ( command => $cmd, verbose => $verbose );
     2318                        unless ($success) {
     2319                            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     2320                            &my_die("Unable to perform difftool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
     2321                        }
     2322                        $Nqueued++;
     2323                    }
     2324                }           
     2325           
     2326            } else {       
     2327                print STDERR "desp_diff_queue: There is a possibility for dirty diffs to be made\n";
     2328                #Consider the case of on-the-fly dirty desperate diffs
     2329                #The conditions are: uneven nr of bad quality warps greater than 3, and the most recent observations is more than $desdiffdt hours old
     2330
     2331#                # Exclude any warps that are not stored in the more general comment_hash.
     2332#                my @keep_warps = ();
     2333#                foreach my $this_warp (@{ $warpsBQ }) {
     2334#                    my $this_comment = ${ $this_warp }[3];
     2335#                    my $this_exp_id  = ${ $this_warp }[0];
     2336#                    if ((exists($comment_hash{$this_comment}))&&
     2337#                        ($comment_hash{$this_comment} == $this_exp_id)) {
     2338#                        push @keep_warps, $this_warp;
     2339#                        my $this_date  = ${ $this_warp }[2];
     2340#                        my $chunk_name  = ${ $this_warp }[6];
     2341#                    }
     2342#                    else {
     2343#                        print STDERR "desp_diff_queue: excluding $this_exp_id for $this_object due to being rejected $this_comment\n";
     2344#                    }
     2345#                }
     2346#                @{ $warpsBQ } = @keep_warps;
     2347#
     2348#                if ($nwarps >= 2) {
     2349#                    #one regular diff has already been made. So, need only one more. Strip a good entry from the array             
     2350#                    my $nrem = 0;
     2351#                    my @keep_warps = ();
     2352#                    foreach my $this_warp (@{ $warpsBQ }) {
     2353#                        my $this_comment = ${ $this_warp }[3];
     2354#                        my $this_exp_id  = ${ $this_warp }[0];
     2355#                        my $this_fwhm   = ${ $this_warp }[7];
     2356#                        if (($nrem <1) && ($this_fwhm <= $maxFWHM)) {
     2357#                            $nrem++;
     2358#                            print STDERR "desp_diff_queue: excluding $this_exp_id for $this_object from dirty desp diffs $this_comment\n";
     2359#                        } else {
     2360#                            push @keep_warps, $this_warp;                       
     2361#                        }
     2362#                    }
     2363#                    @{ $warpsBQ } = @keep_warps;
     2364#                }
     2365#
     2366#                # We are attempting to do the missing diffs, so reverse the list of retained warps.
     2367#                # Good objects with all visits will be skipped due to the duplicate check.
     2368#                # Bad objects will have the earliest visit rejected, and the visits repaired in a way that will produce all desired pairs.
     2369#                @{ $warpsBQ } = reverse @{ $warpsBQ };
     2370#
     2371#                # Exclude the last entry if we do not have an even number of warps.
     2372#                if (($#{ $warpsBQ } + 1) % 2 != 0) {
     2373#                    print STDERR "desp_diff_queue: Number of input warps to make dirty diffs is not even for target $target and object $this_object!";
     2374#                    my $rejected_warp = pop @{ $warpsBQ };
     2375#                    my $rejected_exp_id = ${ $rejected_warp }[0];
     2376#                    print STDERR ": Rejecting ${rejected_exp_id} to force visit count.\n";
     2377#                }
     2378#                my $nwarpsBQ = ($#{$warpsBQ} + 1);
     2379#       
     2380#               
     2381#                while ($#{ $warpsBQ } > -1) {
     2382#                    # The array is sorted in pairs of input/template.
     2383#                    my $template_warp = shift @{ $warpsBQ };
     2384#                    my $input_warp = shift @{ $warpsBQ };
     2385#   
     2386#                    my $input_exp_id = ${ $input_warp }[0];
     2387#                    my $input_comment = ${ $input_warp }[3];
     2388#
     2389#                    my $template_exp_id = ${ $template_warp }[0];
     2390#                    my $template_comment = ${ $template_warp }[3];
     2391#
     2392#                    my $input_warp_id = ${ $input_warp }[1];
     2393#                    my $template_warp_id = ${ $template_warp }[1];
     2394#
     2395#                    my $input_warp_state = ${ $input_warp }[4];
     2396#                    my $template_warp_state = ${ $template_warp }[4];
     2397#
     2398#                    my $input_warp_camQuality = ${ $input_warp }[5];
     2399#                    my $template_warp_camQuality = ${ $template_warp }[5];
     2400#
     2401#                    $Npotential++;
     2402#
     2403#                    unless (defined($input_warp_id) && defined($template_warp_id) &&
     2404#                        ($input_warp_state eq 'full')&&($template_warp_state eq 'full')) {
     2405#                        print STDERR "Desp diff for this $date $target $input_exp_id $template_exp_id not fully processed ($input_warp_state $template_warp_state) ($input_warp_camQuality $template_warp_camQuality)\n";
     2406#                        if (($input_warp_camQuality == 4007)||($template_warp_camQuality == 4007)) {
     2407#                            # This should now never be reached.
     2408#                            # CZW: Trigger backup plan here?  Or simply set up framework?
     2409#                            print STDERR "  ...but this is due to a camera stage astrometry quality\n";
     2410#                            $Npotential--;
     2411#                        }
     2412#                        next;
     2413#                    }
     2414#
     2415#                    if (verify_uniqueness_diff($input_warp_id,$template_warp_id,$date,$target) != 0) {
     2416#                        $Nqueued++;
     2417#                        print STDERR "Desp diffs already queued for this $date $target $input_exp_id $template_exp_id ($input_warp_id $template_warp_id) $this_object $input_comment $template_comment\n";
     2418#                        next;
     2419#                    }
     2420#                    else {
     2421#                        print STDERR "Preparing to dirty diff $date $target $input_exp_id $template_exp_id ($input_warp_id $template_warp_id) $this_object $input_comment $template_comment\n";
     2422#                    }
     2423#           
     2424#                    my $cmd = "$difftool -dbname $dbname  -definewarpwarp  ";
     2425#                    $cmd .= "-input_label $label  -template_label $label -good_frac 0.1 ";
     2426#                    $cmd .= "-backwards "; # Needed because difftool assumes a different date sorting.
     2427#                    $cmd .= "-rerun "; # Needed because we may have some diffs that already use some of the exposures
     2428#                    $cmd .= "-set_workdir $workdir  -set_dist_group $dist_group  -set_data_group $data_group ";
     2429#                    $cmd .= " -simple  -set_label $label -exp_id $input_exp_id -template_exp_id $template_exp_id ";
     2430#                    if (defined($reduction)) {
     2431#                        $cmd .= " -set_reduction $reduction ";
     2432#                    }
     2433#       
     2434#                    if (defined($pretend)) {
     2435#                        $cmd .= ' -pretend ';
     2436#                    }
     2437#                    if ($debug == 1) {
     2438#                        $cmd .= ' -pretend ';
     2439#                        print STDERR "desp_diff_queue: $cmd\n";
     2440#                        print STDERR " $input_warp_id $template_warp_id\n";
     2441#                    }
     2442#           
     2443#                    if (($debug == 0)&&(!defined($pretend))) {
     2444#                        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     2445#                            run ( command => $cmd, verbose => $verbose );
     2446#                        unless ($success) {
     2447#                            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     2448#                            &my_die("Unable to perform difftool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
     2449#                        }
     2450#                        $Nqueued++;
     2451#                    }
     2452#                }
    20082453           
    2009                 my $cmd = "$difftool -dbname $dbname  -definewarpwarp  ";
    2010                 $cmd .= "-input_label $label  -template_label $label -good_frac 0.1 ";
    2011                 $cmd .= "-backwards "; # Needed because difftool assumes a different date sorting.
    2012                 $cmd .= "-rerun "; # Needed because we may have some diffs that already use some of the exposures
    2013                 $cmd .= "-set_workdir $workdir  -set_dist_group $dist_group  -set_data_group $data_group ";
    2014                 $cmd .= " -simple  -set_label $label -exp_id $input_exp_id -template_exp_id $template_exp_id ";
    2015 #                   $cmd .= " -pretend ";
    2016                 if (defined($reduction)) {
    2017                     $cmd .= " -set_reduction $reduction ";
    2018                 }
    2019 
    2020                 if (defined($pretend)) {
    2021                     $cmd .= ' -pretend ';
    2022                 }
    2023                 if ($debug == 1) {
    2024                     $cmd .= ' -pretend ';
    2025                     print STDERR "desp_diff_queue: $cmd\n";
    2026                     print STDERR " $input_warp_id $template_warp_id\n";
    2027                 }
    20282454           
    2029                 if (($debug == 0)&&(!defined($pretend))) {
    2030                     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    2031                         run ( command => $cmd, verbose => $verbose );
    2032                     unless ($success) {
    2033                         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    2034                         &my_die("Unable to perform difftool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
    2035                     }
    2036                     $Nqueued++;
    2037                 }
    2038             }
     2455           
     2456             }
    20392457        }
    20402458    }
    20412459    $metadata_out{nsDiffPotential} += $Npotential;
    20422460    $metadata_out{nsDiffQueued}    += $Nqueued;
    2043 #      if (($metadata_out{nsDiffPotential} == $metadata_out{nsDiffQueued})&&($metadata_out{nsObservingState} eq 'END_OF_NIGHT')) {
    2044 #       $metadata_out{nsDiffState} = 'FINISHED_DIFFS';
    2045 #      }       
    2046 
    20472461}
    20482462
     
    20642478    my $Npotential = 0;
    20652479    my $Nqueued = 0;
    2066     
     2480   
    20672481    foreach my $object_row (@{ $object_ref }) {
    2068         my $this_object = shift @{ $object_row };
    2069 #       my $input_sth = "select exp_id,warp_id,dateobs,rawExp.comment from warpRun JOIN fakeRun USING(fake_id) JOIN camRun USING(cam_id) JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id) ";
    2070 #       $input_sth .= " WHERE warpRun.state = 'full' AND warpRun.label = '$label' AND warpRun.data_group = '$data_group' AND rawExp.filter = '$filter' AND rawExp.object = '$this_object' ";
    2071 #       $input_sth .= " ORDER BY dateobs ";
    2072        
    2073         my $input_sth = "select exp_id,warp_id,dateobs,rawExp.comment,warpRun.state AS warp_state FROM ";
    2074         $input_sth .=   " rawExp LEFT JOIN chipRun USING (exp_id) LEFT JOIN camRun USING (chip_id) LEFT JOIN fakeRun USING (cam_id) LEFT JOIN warpRun USING (fake_id) ";
    2075         $input_sth .=   " WHERE warpRun.label = '$label' AND warpRun.data_group = '$data_group' AND rawExp.filter = '$filter' AND rawExp.object = '$this_object' ";
    2076         $input_sth .=   " ORDER BY dateobs ";
    2077 
    2078         my $warps = $db->selectall_arrayref( $input_sth );
    2079 
    2080         # Each comment should only appear once. Therefore, if we see it more than once, we assume the first is extra.
    2081         my %comment_hash = ();
    2082         foreach my $this_warp (@{ $warps }) {
    2083             my $this_comment = ${ $this_warp }[3];
    2084             my $this_exp_id  = ${ $this_warp }[0];
    2085             $comment_hash{$this_comment} = $this_exp_id;
    2086         }
    2087        
    2088         if (($#{ $warps } + 1) % 2 != 0) {
    2089             print STDERR "md_diff_queue: Number of input warps to make diffs is not even for target $target and object $this_object! $#{ $warps } ";
    2090             if ($#{ $warps} + 1 == 1) {
    2091                 print STDERR ": I can do no diffs with only one exposure.\n";
    2092                 next;
    2093             }
    2094             else {
    2095                 print STDERR ": I should declare an exposure to be faulty.\n";
    2096                 my @keep_warps = ();
    2097 #               print "@{ $warps }\n";
    2098                 foreach my $this_warp (@{ $warps }) {
    2099                     my $this_comment = ${ $this_warp }[3];
    2100                     my $this_exp_id  = ${ $this_warp }[0];
    2101                     if ($comment_hash{$this_comment} == $this_exp_id) {
    2102                         push @keep_warps, $this_warp;
    2103                     }
    2104                     else {
    2105                         print STDERR "md_diff_queue: excluding $this_exp_id for $this_object\n";
    2106                     }
    2107                 }
    2108                 @{ $warps } = @keep_warps;
    2109 #               print "@{ $warps }\n";
    2110             }
    2111         }
    2112        
    2113         while ($#{ $warps } > -1) {
    2114             my $input_warp = shift @{ $warps };
    2115             my $input_exp_id = ${ $input_warp }[0];
    2116             my $input_comment = ${ $input_warp }[3];
    2117 
    2118            
    2119             my $template_warp = shift @{ $warps };
    2120 
    2121             my $template_exp_id = ${ $template_warp }[0];
    2122            
    2123             my $input_warp_id = ${ $input_warp }[1];
    2124             my $template_warp_id = ${ $template_warp }[1];
    2125 
    2126             my $input_warp_state = ${ $input_warp }[4];
    2127             my $template_warp_state = ${ $template_warp }[4];
    2128            
    2129             unless(defined($template_warp)&& defined($template_exp_id)) {
    2130                 print STDERR "md_diff received an undef! $input_exp_id $input_comment $this_object T: $template_warp V: @$template_warp\n";
    2131                 next;
    2132             }
    2133             $Npotential++;
    2134            
    2135             unless (defined($input_warp_id) && defined($template_warp_id) &&
    2136                     ($input_warp_state eq 'full')&&($template_warp_state eq 'full')) {
    2137                 print STDERR "md_Diff for this $date $target $input_exp_id ($input_warp_id $input_warp_state) $template_exp_id ($template_warp_id $template_warp_state) not fully processed\n";
    2138                 next;
    2139             }
    2140 
    2141             if (multi_date_verify_uniqueness_diff($input_warp_id,$template_warp_id,$date,$target) != 0) {
    2142                 $Nqueued++;
    2143                 print STDERR "md_Diffs already queued for this $date $target $input_exp_id $template_exp_id ($input_warp_id $template_warp_id) $this_object $input_comment\n";
    2144                 next;
    2145             }
    2146 
    2147             my $new_data_group = "${data_group}.multi";
    2148             my $cmd = "$difftool -dbname $dbname  -definewarpwarp ";
    2149             $cmd .= "-input_label $label  -template_label $label -good_frac 0.1 ";
    2150             $cmd .= "-backwards "; # Needed because difftool assumes a different date sorting.
    2151             $cmd .= "-set_workdir $workdir  -set_dist_group $dist_group  -set_data_group $new_data_group ";
    2152             $cmd .= " -simple  -set_label $label -exp_id $input_exp_id -template_exp_id $template_exp_id ";
    2153             if (defined($reduction)) {
    2154                 $cmd .= " -set_reduction $reduction ";
    2155             }
    2156 
    2157 #               $cmd .= " -pretend ";
    2158             if (defined($pretend)) {
    2159                 $cmd .= ' -pretend ';
    2160             }
    2161             if ($debug == 1) {
    2162                 $cmd .= ' -pretend ';
    2163                 print STDERR "md_Diffs would like to queue for this $date $target $input_exp_id $template_exp_id ($input_warp_id $template_warp_id) $this_object $input_comment\n";
    2164                 print STDERR "md_diff_queue: $cmd\n";
    2165                 print STDERR " $input_warp_id $template_warp_id\n";
    2166             }
    2167            
    2168             if (($debug == 0)&&(!defined($pretend))) {
    2169                 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    2170                     run ( command => $cmd, verbose => $verbose );
    2171                 unless ($success) {
    2172                     $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    2173                     &my_die("Unable to perform difftool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
    2174                 }
    2175                 $Nqueued++;
    2176             }
    2177         }
     2482        my $this_object = shift @{ $object_row };
     2483#       my $input_sth = "select exp_id,warp_id,dateobs,rawExp.comment from warpRun JOIN fakeRun USING(fake_id) JOIN camRun USING(cam_id) JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id) ";
     2484#       $input_sth .= " WHERE warpRun.state = 'full' AND warpRun.label = '$label' AND warpRun.data_group = '$data_group' AND rawExp.filter = '$filter' AND rawExp.object = '$this_object' ";
     2485#       $input_sth .= " ORDER BY dateobs ";
     2486       
     2487        my $input_sth = "select exp_id,warp_id,dateobs,rawExp.comment,warpRun.state AS warp_state FROM ";
     2488        $input_sth .=   " rawExp LEFT JOIN chipRun USING (exp_id) LEFT JOIN camRun USING (chip_id) LEFT JOIN fakeRun USING (cam_id) LEFT JOIN warpRun USING (fake_id) ";
     2489        $input_sth .=   " WHERE warpRun.label = '$label' AND warpRun.data_group = '$data_group' AND rawExp.filter = '$filter' AND rawExp.object = '$this_object' ";
     2490        $input_sth .=   " ORDER BY dateobs ";
     2491
     2492        my $warps = $db->selectall_arrayref( $input_sth );
     2493
     2494        # Each comment should only appear once. Therefore, if we see it more than once, we assume the first is extra.
     2495        my %comment_hash = ();
     2496        foreach my $this_warp (@{ $warps }) {
     2497            my $this_comment = ${ $this_warp }[3];
     2498            my $this_exp_id  = ${ $this_warp }[0];
     2499            $comment_hash{$this_comment} = $this_exp_id;
     2500        }
     2501       
     2502        if (($#{ $warps } + 1) % 2 != 0) {
     2503            print STDERR "md_diff_queue: Number of input warps to make diffs is not even for target $target and object $this_object! $#{ $warps } ";
     2504            if ($#{ $warps} + 1 == 1) {
     2505                print STDERR ": I can do no diffs with only one exposure.\n";
     2506                next;
     2507            }
     2508            else {
     2509                print STDERR ": I should declare an exposure to be faulty.\n";
     2510                my @keep_warps = ();
     2511#               print "@{ $warps }\n";
     2512                foreach my $this_warp (@{ $warps }) {
     2513                    my $this_comment = ${ $this_warp }[3];
     2514                    my $this_exp_id  = ${ $this_warp }[0];
     2515                    if ($comment_hash{$this_comment} == $this_exp_id) {
     2516                        push @keep_warps, $this_warp;
     2517                    }
     2518                    else {
     2519                        print STDERR "md_diff_queue: excluding $this_exp_id for $this_object\n";
     2520                    }
     2521                }
     2522                @{ $warps } = @keep_warps;
     2523#               print "@{ $warps }\n";
     2524            }
     2525        }
     2526       
     2527        while ($#{ $warps } > -1) {
     2528            my $input_warp = shift @{ $warps };
     2529            my $input_exp_id = ${ $input_warp }[0];
     2530            my $input_comment = ${ $input_warp }[3];
     2531
     2532          
     2533            my $template_warp = shift @{ $warps };
     2534
     2535            my $template_exp_id = ${ $template_warp }[0];
     2536          
     2537            my $input_warp_id = ${ $input_warp }[1];
     2538            my $template_warp_id = ${ $template_warp }[1];
     2539
     2540            my $input_warp_state = ${ $input_warp }[4];
     2541            my $template_warp_state = ${ $template_warp }[4];
     2542          
     2543            unless(defined($template_warp)&& defined($template_exp_id)) {
     2544                print STDERR "md_diff received an undef! $input_exp_id $input_comment $this_object T: $template_warp V: @$template_warp\n";
     2545                next;
     2546            }
     2547            $Npotential++;
     2548          
     2549            unless (defined($input_warp_id) && defined($template_warp_id) &&
     2550                    ($input_warp_state eq 'full')&&($template_warp_state eq 'full')) {
     2551                print STDERR "md_Diff for this $date $target $input_exp_id ($input_warp_id $input_warp_state) $template_exp_id ($template_warp_id $template_warp_state) not fully processed\n";
     2552                next;
     2553            }
     2554
     2555            if (multi_date_verify_uniqueness_diff($input_warp_id,$template_warp_id,$date,$target) != 0) {
     2556                $Nqueued++;
     2557                print STDERR "md_Diffs already queued for this $date $target $input_exp_id $template_exp_id ($input_warp_id $template_warp_id) $this_object $input_comment\n";
     2558                next;
     2559            }
     2560
     2561            my $new_data_group = "${data_group}.multi";
     2562            my $cmd = "$difftool -dbname $dbname  -definewarpwarp ";
     2563            $cmd .= "-input_label $label  -template_label $label -good_frac 0.1 ";
     2564            $cmd .= "-backwards "; # Needed because difftool assumes a different date sorting.
     2565            $cmd .= "-set_workdir $workdir  -set_dist_group $dist_group  -set_data_group $new_data_group ";
     2566            $cmd .= " -simple  -set_label $label -exp_id $input_exp_id -template_exp_id $template_exp_id ";
     2567            if (defined($reduction)) {
     2568                $cmd .= " -set_reduction $reduction ";
     2569            }
     2570
     2571#               $cmd .= " -pretend ";
     2572            if (defined($pretend)) {
     2573                $cmd .= ' -pretend ';
     2574            }
     2575            if ($debug == 1) {
     2576                $cmd .= ' -pretend ';
     2577                print STDERR "md_Diffs would like to queue for this $date $target $input_exp_id $template_exp_id ($input_warp_id $template_warp_id) $this_object $input_comment\n";
     2578                print STDERR "md_diff_queue: $cmd\n";
     2579                print STDERR " $input_warp_id $template_warp_id\n";
     2580            }
     2581          
     2582            if (($debug == 0)&&(!defined($pretend))) {
     2583                my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     2584                    run ( command => $cmd, verbose => $verbose );
     2585                unless ($success) {
     2586                    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     2587                    &my_die("Unable to perform difftool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
     2588                }
     2589                $Nqueued++;
     2590            }
     2591        }
    21782592    }
    21792593    $metadata_out{nsDiffPotential} += $Npotential;
    21802594    $metadata_out{nsDiffQueued}    += $Nqueued;
    21812595#      if (($metadata_out{nsDiffPotential} == $metadata_out{nsDiffQueued})&&($metadata_out{nsObservingState} eq 'END_OF_NIGHT')) {
    2182      $metadata_out{nsDiffState} = 'FINISHED_DIFFS';
    2183 #      }       
     2596     $metadata_out{nsDiffState} = 'FINISHED_DIFFS';
     2597#      }       
    21842598
    21852599}
     
    22002614
    22012615
    2202 # Get a list of exposures that could be diffed    
     2616# Get a list of exposures that could be diffed   
    22032617    my $new_data_group = "${data_group}.offnight";
    22042618    my $new_dist_group = "${dist_group}.offnight";
     
    22102624    $check_cmd .= " -dateobs_begin ${lunation_start}T00:00:00 -dateobs_end ${lunation_end}T23:59:59 -distance 1.5 ";
    22112625    if (defined($reduction)) {
    2212         $check_cmd .= " -set_reduction $reduction ";
     2626        $check_cmd .= " -set_reduction $reduction ";
    22132627    }
    22142628
    22152629    my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    2216         run ( command => $check_cmd, verbose => $verbose );
     2630        run ( command => $check_cmd, verbose => $verbose );
    22172631    unless ($success) {
    2218         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    2219         &my_die("Unable to perform difftool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
    2220     }
    2221     
     2632        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     2633        &my_die("Unable to perform difftool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
     2634    }
     2635   
    22222636    # Parse results
    22232637    my $diffs = $mdcParser->parse_list(join "", @$stdout_buf) or
    2224         &my_die("Unable to parse metadata from difftool -definewarpwarp", 0, 0, $date, $PS_EXIT_PROG_ERROR);
     2638        &my_die("Unable to parse metadata from difftool -definewarpwarp", 0, 0, $date, $PS_EXIT_PROG_ERROR);
    22252639    foreach my $diff (@$diffs) {
    2226         unless (multi_date_verify_uniqueness_diff($diff->{input_warp_id},$diff->{template_warp_id},$date,$target)) {
    2227             # If we don't already have a diff with these inputs, make a diff with these inputs.
    2228             my $cmd = "$difftool -dbname $dbname -definewarpwarp -good_frac 0.1 ";
    2229             $cmd   .= "-input_label $label -template_label $label ";
    2230             $cmd   .= "-warp_id $diff->{input_warp_id} -template_warp_id $diff->{template_warp_id} ";
    2231             $cmd .= "-set_workdir $workdir  -set_dist_group $new_dist_group  -set_data_group $new_data_group ";
    2232             $cmd .= " -mintimediff 40000 ";
    2233             $cmd .= " -simple  -rerun -set_label $label -filter $filter ";
    2234             $cmd .= " -dateobs_begin ${lunation_start}T00:00:00 -dateobs_end ${lunation_end}T23:59:59 -distance 1.5 ";
    2235             if (defined($reduction)) {
    2236                 $cmd .= " -set_reduction $reduction ";
    2237             }
    2238 
    2239             if (defined($pretend)) {
    2240                 $cmd .= ' -pretend ';
    2241             }
    2242             print STDERR "ON_diffs wants to run this command: $cmd\n";
    2243            
    2244             if (($debug == 0)&&(!defined($pretend))) {
    2245                 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    2246                     run ( command => $cmd, verbose => $verbose );
    2247                 unless ($success) {
    2248                     $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    2249                     &my_die("Unable to perform difftool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
    2250                 }
    2251             }
    2252         }
     2640        unless (multi_date_verify_uniqueness_diff($diff->{input_warp_id},$diff->{template_warp_id},$date,$target)) {
     2641            # If we don't already have a diff with these inputs, make a diff with these inputs.
     2642            my $cmd = "$difftool -dbname $dbname -definewarpwarp -good_frac 0.1 ";
     2643            $cmd   .= "-input_label $label -template_label $label ";
     2644            $cmd   .= "-warp_id $diff->{input_warp_id} -template_warp_id $diff->{template_warp_id} ";
     2645            $cmd .= "-set_workdir $workdir  -set_dist_group $new_dist_group  -set_data_group $new_data_group ";
     2646            $cmd .= " -mintimediff 40000 ";
     2647            $cmd .= " -simple  -rerun -set_label $label -filter $filter ";
     2648            $cmd .= " -dateobs_begin ${lunation_start}T00:00:00 -dateobs_end ${lunation_end}T23:59:59 -distance 1.5 ";
     2649            if (defined($reduction)) {
     2650                $cmd .= " -set_reduction $reduction ";
     2651            }
     2652
     2653            if (defined($pretend)) {
     2654                $cmd .= ' -pretend ';
     2655            }
     2656            print STDERR "ON_diffs wants to run this command: $cmd\n";
     2657          
     2658            if (($debug == 0)&&(!defined($pretend))) {
     2659                my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     2660                    run ( command => $cmd, verbose => $verbose );
     2661                unless ($success) {
     2662                    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     2663                    &my_die("Unable to perform difftool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
     2664                }
     2665            }
     2666        }
    22532667    }
    22542668}
     
    22642678    my ($stacks_to_finish,$stacks_total) = check_stack_count($label);
    22652679    if (($stacks_to_finish == 0)&&($stacks_total != 0)) {
    2266         my $cmd = "$difftool -dbname $dbname -definewarpstack ";
    2267         $cmd .= " -good_frac 0.2 ";
    2268         $cmd .= " -warp_label $label -stack_label $label -set_label $label ";
    2269         $cmd .= " -set_workdir $workdir -available -set_reduction WARPSTACK -set_dist_group $dist_group ";
    2270         $cmd .= " -rerun ";
    2271     }
    2272 }          
     2680        my $cmd = "$difftool -dbname $dbname -definewarpstack ";
     2681        $cmd .= " -good_frac 0.2 ";
     2682        $cmd .= " -warp_label $label -stack_label $label -set_label $label ";
     2683        $cmd .= " -set_workdir $workdir -available -set_reduction WARPSTACK -set_dist_group $dist_group ";
     2684        $cmd .= " -rerun ";
     2685    }
     2686}          
    22732687
    22742688sub check_stack_count {
     
    23012715    my $retention_time;
    23022716    if (exists($science_config{$target}{$mode})) {
    2303         $retention_time = $science_config{$target}{$mode};
     2717        $retention_time = $science_config{$target}{$mode};
    23042718    }
    23052719    else {
    2306         $retention_time = $clean_config{$mode}{RETENTION_TIME};
     2720        $retention_time = $clean_config{$mode}{RETENTION_TIME};
    23072721    }
    23082722    if ($retention_time <= 0) {
    2309         return("no clean","true");
     2723        return("no clean","true");
    23102724    }
    23112725
    23122726    my ($year,$month,$day) = split /-/,$date;
    23132727    my $dt = DateTime->new(year => $year, month => $month, day => $day,
    2314                            hour => 0, minute => 0, second => 0, nanosecond => 0,
    2315                            time_zone => 'Pacific/Honolulu');
    2316        
     2728                           hour => 0, minute => 0, second => 0, nanosecond => 0,
     2729                           time_zone => 'Pacific/Honolulu');
     2730       
    23172731    $dt->subtract(days => $retention_time);
    23182732    my $cleaning_date = $dt->ymd;
    23192733
    23202734    my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($cleaning_date,$target);
    2321     
     2735   
    23222736    $command =~ s/\@DBNAME\@/$dbname/;
    23232737    $command =~ s/\@LABEL\@/$label/;
     
    23302744    $command =~ s/\@REDUCTION\@/$reduction/;
    23312745    $command =~ s/\@CURRENT_DATE\@/$cleaning_date/;
    2332     
     2746   
    23332747    if ($debug == 1) {
    23342748        $command .= ' -pretend ';
     
    23422756
    23432757    foreach my $mode (sort (keys %clean_config)) {
    2344         foreach my $target (sort (keys %science_config)) {
    2345             if (exists($science_config{$target}{NOCLEAN})) {
    2346                 next;
    2347             }
    2348             my ($cleaning_date,$command) = construct_cleantool_args($date,$target,$mode);
    2349 
    2350             if ($cleaning_date eq 'no clean') {
    2351                 next;
    2352             }           
    2353             print STDERR "$command\n";
    2354             if (!(defined($pretend) || $debug == 1)) {
     2758        foreach my $target (sort (keys %science_config)) {
     2759            if (exists($science_config{$target}{NOCLEAN})) {
     2760                next;
     2761            }
     2762            my ($cleaning_date,$command) = construct_cleantool_args($date,$target,$mode);
     2763
     2764            if ($cleaning_date eq 'no clean') {
     2765                next;
     2766            }           
     2767            print STDERR "$command\n";
     2768            if (!(defined($pretend) || $debug == 1)) {
    23552769#           print STDERR "BEAR IS DRIVING!?\n";
    2356                 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    2357                     run ( command => $command, verbose => $verbose );
    2358                 unless ($success) {
    2359                     $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    2360                     &my_die("Unable to perform cleantool ($command): $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
    2361                 }
    2362                 add_to_macro_list('clean_old',1,$date,$target,$mode);
    2363             }
    2364             if ($mode eq 'DIFF') {
    2365                 my $WS_command = $command;
    2366                 $WS_command =~ s/$target/${target}.WS/;
    2367                 print STDERR "$WS_command\n";
    2368                 if (!(defined($pretend) || $debug == 1)) {
    2369                    
    2370                     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    2371                         run ( command => $WS_command, verbose => $verbose );
    2372                     unless ($success) {
    2373                         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    2374                         &my_die("Unable to perform cleantool ($WS_command): $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
    2375                     }
    2376                 }
    2377             }
    2378 
    2379         }
     2770                my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     2771                    run ( command => $command, verbose => $verbose );
     2772                unless ($success) {
     2773                    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     2774                    &my_die("Unable to perform cleantool ($command): $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
     2775                }
     2776                add_to_macro_list('clean_old',1,$date,$target,$mode);
     2777            }
     2778            if ($mode eq 'DIFF') {
     2779                my $WS_command = $command;
     2780                $WS_command =~ s/$target/${target}.WS/;
     2781                print STDERR "$WS_command\n";
     2782                if (!(defined($pretend) || $debug == 1)) {
     2783                  
     2784                    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     2785                        run ( command => $WS_command, verbose => $verbose );
     2786                    unless ($success) {
     2787                        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     2788                        &my_die("Unable to perform cleantool ($WS_command): $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
     2789                    }
     2790                }
     2791            }
     2792
     2793        }
    23802794    }
    23812795    return(0);
     
    23892803    my $date = shift;
    23902804    my $eon_dt = DateTime->new( year   => $datetime->year,
    2391                                 month  => $datetime->month,
    2392                                 day    => $datetime->day,
    2393                                 hour   => 17, # 7,
    2394                                 minute => 30,
    2395                                 second => 0,
    2396                                 time_zone => 'UTC');
    2397     
     2805                                month  => $datetime->month,
     2806                                day    => $datetime->day,
     2807                                hour   => 17, # 7,
     2808                                minute => 30,
     2809                                second => 0,
     2810                                time_zone => 'UTC');
     2811   
    23982812    foreach my $eon (keys %eon_config) {
    2399         my $command = "$regtool -processedexp -simple ";
    2400         $command .= " -dbname $dbname ";
    2401         $command .= " -dateobs_begin ${date}T00:00:00 -dateobs_end ${date}T23:59:59 ";
    2402         $command .= " -object $eon_config{$eon}{OBJECT} " if defined($eon_config{$eon}{OBJECT});
    2403         $command .= " -obs_mode $eon_config{$eon}{OBSMODE} " if defined($eon_config{$eon}{OBSMODE});
    2404         $command .= " -exp_type $eon_config{$eon}{EXPTYPE} " if defined($eon_config{$eon}{EXPTYPE});
    2405         $command .= " -comment $eon_config{$eon}{COMMENT} " if defined($eon_config{$eon}{COMMENT});
    2406 
    2407         my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    2408             run ( command => $command, verbose => $verbose );
    2409         unless ($success) {
    2410             $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    2411             &my_die("Unable to perform regtool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
    2412         }
    2413         my @eon_exposures = split /\n/, (join '', @$stdout_buf);
    2414         if ($#eon_exposures >= 0) {
    2415             return("END_OF_NIGHT");
    2416         }
    2417     }   
     2813        my $command = "$regtool -processedexp -simple ";
     2814        $command .= " -dbname $dbname ";
     2815        $command .= " -dateobs_begin ${date}T00:00:00 -dateobs_end ${date}T23:59:59 ";
     2816        $command .= " -object $eon_config{$eon}{OBJECT} " if defined($eon_config{$eon}{OBJECT});
     2817        $command .= " -obs_mode $eon_config{$eon}{OBSMODE} " if defined($eon_config{$eon}{OBSMODE});
     2818        $command .= " -exp_type $eon_config{$eon}{EXPTYPE} " if defined($eon_config{$eon}{EXPTYPE});
     2819        $command .= " -comment $eon_config{$eon}{COMMENT} " if defined($eon_config{$eon}{COMMENT});
     2820
     2821        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     2822            run ( command => $command, verbose => $verbose );
     2823        unless ($success) {
     2824            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     2825            &my_die("Unable to perform regtool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
     2826        }
     2827        my @eon_exposures = split /\n/, (join '', @$stdout_buf);
     2828        if ($#eon_exposures >= 0) {
     2829            return("END_OF_NIGHT");
     2830        }
     2831    }   
    24182832    if ($force_registration) {
    2419         return("END_OF_NIGHT");
     2833        return("END_OF_NIGHT");
    24202834    }
    24212835#    print "$now $eon_dt " . DateTime->compare($now,$eon_dt) . "\n";
    24222836    if (DateTime->compare($now,$eon_dt) < 1) {
    2423         return("OBSERVING");
     2837        return("OBSERVING");
    24242838    }
    24252839    else {
    2426         return("END_OF_NIGHT");
     2840        return("END_OF_NIGHT");
    24272841    }
    24282842}
     
    24332847
    24342848    foreach my $eon (keys %eon_config) {
    2435         my $command = "$regtool -processedexp -simple ";
    2436         $command .= " -dbname $dbname ";
    2437         $command .= " -dateobs_begin ${date}T00:00:00 -dateobs_end ${date}T23:59:59 ";
    2438         $command .= " -object $eon_config{$eon}{OBJECT} " if defined($eon_config{$eon}{OBJECT});
    2439         $command .= " -obs_mode $eon_config{$eon}{OBSMODE} " if defined($eon_config{$eon}{OBSMODE});
    2440         $command .= " -exp_type $eon_config{$eon}{EXPTYPE} " if defined($eon_config{$eon}{EXPTYPE});
    2441         $command .= " -comment $eon_config{$eon}{COMMENT} " if defined($eon_config{$eon}{COMMENT});
    2442 
    2443         my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    2444             run ( command => $command, verbose => $verbose );
    2445         unless ($success) {
    2446             $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    2447             &my_die("Unable to perform regtool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
    2448         }
    2449         my @eon_exposures = split /\n/, (join '', @$stdout_buf);
    2450         if ($#eon_exposures >= 0) {
    2451             return("REGISTERED");
    2452         }
    2453     }   
     2849        my $command = "$regtool -processedexp -simple ";
     2850        $command .= " -dbname $dbname ";
     2851        $command .= " -dateobs_begin ${date}T00:00:00 -dateobs_end ${date}T23:59:59 ";
     2852        $command .= " -object $eon_config{$eon}{OBJECT} " if defined($eon_config{$eon}{OBJECT});
     2853        $command .= " -obs_mode $eon_config{$eon}{OBSMODE} " if defined($eon_config{$eon}{OBSMODE});
     2854        $command .= " -exp_type $eon_config{$eon}{EXPTYPE} " if defined($eon_config{$eon}{EXPTYPE});
     2855        $command .= " -comment $eon_config{$eon}{COMMENT} " if defined($eon_config{$eon}{COMMENT});
     2856
     2857        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     2858            run ( command => $command, verbose => $verbose );
     2859        unless ($success) {
     2860            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     2861            &my_die("Unable to perform regtool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
     2862        }
     2863        my @eon_exposures = split /\n/, (join '', @$stdout_buf);
     2864        if ($#eon_exposures >= 0) {
     2865            return("REGISTERED");
     2866        }
     2867    }   
    24542868    return("NOT_REGISTERED");
    24552869}
    24562870
    2457     
     2871   
    24582872
    24592873sub get_tool_parameters {
     
    24892903    my $internal_filter;
    24902904    if (defined($det_filter)) {
    2491         $internal_filter = $det_filter; $internal_filter =~ s/\..*//;
    2492         $internal_filter = '.' . $internal_filter;
     2905        $internal_filter = $det_filter; $internal_filter =~ s/\..*//;
     2906        $internal_filter = '.' . $internal_filter;
    24932907    }
    24942908    else {
    2495         $internal_filter = '';
     2909        $internal_filter = '';
    24962910    }
    24972911    unless (defined($maxN)) {
    2498         $maxN = 0;
     2912        $maxN = 0;
    24992913    }
    25002914    my $lc_type = lc($exp_type);
     
    25122926
    25132927    if ((defined($macro_config{$proc_mode}))&&($do_or_do_not)) {
    2514         unless (defined($metadata_out{N_MACROS})) {
    2515             $metadata_out{N_MACROS} = 0;
    2516         }
    2517         my $N = $metadata_out{N_MACROS};
    2518         $metadata_out{"ns${N}Macro"} = $macro_config{$proc_mode};
    2519         if ($debug == 1) {
    2520             print STDERR "WORKING ON A MACRO: ns${N}Macro $proc_mode $macro_config{$proc_mode}\n";
    2521         }
    2522         if (defined($date)&&(defined($target))) {
    2523 
    2524             my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction)
    2525                 = get_tool_parameters($date,$target);
    2526             $metadata_out{"ns${N}Macro"} =~ s/\@LABEL\@/$label/;
    2527             $metadata_out{"ns${N}Macro"} =~ s/\@WORKDIR\@/$workdir/;
    2528             $metadata_out{"ns${N}Macro"} =~ s/\@OBSMODE\@/$obs_mode/;
    2529             $metadata_out{"ns${N}Macro"} =~ s/\@OBJECT\@/$object/;
    2530             $metadata_out{"ns${N}Macro"} =~ s/\@COMMENT\@/$comment/;
    2531             $metadata_out{"ns${N}Macro"} =~ s/\@TESS_ID\@/$tess_id/;
    2532             $metadata_out{"ns${N}Macro"} =~ s/\@DIST_GROUP\@/$dist_group/;
    2533             $metadata_out{"ns${N}Macro"} =~ s/\@DATA_GROUP\@/$data_group/;
    2534             $metadata_out{"ns${N}Macro"} =~ s/\@REDUCTION\@/$reduction/;
    2535         }
    2536         if (defined($mode)) {
    2537             $metadata_out{"ns${N}Macro"} =~ s/\@EXTRA\@/$mode/;
    2538         }
    2539         if (defined($date)) {
    2540             $metadata_out{"ns${N}Macro"} =~ s/\@DATE\@/$date/;
    2541         }
    2542         if ($debug == 1) {
    2543             print STDERR "DONE WITH A MACRO: ns${N}Macro $proc_mode $macro_config{$proc_mode}\n";
    2544         }
    2545         $metadata_out{N_MACROS} ++;
     2928        unless (defined($metadata_out{N_MACROS})) {
     2929            $metadata_out{N_MACROS} = 0;
     2930        }
     2931        my $N = $metadata_out{N_MACROS};
     2932        $metadata_out{"ns${N}Macro"} = $macro_config{$proc_mode};
     2933        if ($debug == 1) {
     2934            print STDERR "WORKING ON A MACRO: ns${N}Macro $proc_mode $macro_config{$proc_mode}\n";
     2935        }
     2936        if (defined($date)&&(defined($target))) {
     2937
     2938            my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction)
     2939                = get_tool_parameters($date,$target);
     2940            $metadata_out{"ns${N}Macro"} =~ s/\@LABEL\@/$label/;
     2941            $metadata_out{"ns${N}Macro"} =~ s/\@WORKDIR\@/$workdir/;
     2942            $metadata_out{"ns${N}Macro"} =~ s/\@OBSMODE\@/$obs_mode/;
     2943            $metadata_out{"ns${N}Macro"} =~ s/\@OBJECT\@/$object/;
     2944            $metadata_out{"ns${N}Macro"} =~ s/\@COMMENT\@/$comment/;
     2945            $metadata_out{"ns${N}Macro"} =~ s/\@TESS_ID\@/$tess_id/;
     2946            $metadata_out{"ns${N}Macro"} =~ s/\@DIST_GROUP\@/$dist_group/;
     2947            $metadata_out{"ns${N}Macro"} =~ s/\@DATA_GROUP\@/$data_group/;
     2948            $metadata_out{"ns${N}Macro"} =~ s/\@REDUCTION\@/$reduction/;
     2949        }
     2950        if (defined($mode)) {
     2951            $metadata_out{"ns${N}Macro"} =~ s/\@EXTRA\@/$mode/;
     2952        }
     2953        if (defined($date)) {
     2954            $metadata_out{"ns${N}Macro"} =~ s/\@DATE\@/$date/;
     2955        }
     2956        if ($debug == 1) {
     2957            print STDERR "DONE WITH A MACRO: ns${N}Macro $proc_mode $macro_config{$proc_mode}\n";
     2958        }
     2959        $metadata_out{N_MACROS} ++;
    25462960    }
    25472961}
  • branches/eam_branches/ipp-20220316/ippScripts/scripts/rawcheck.pl

  • branches/eam_branches/ipp-20220316/ippScripts/scripts/warp_overlap.pl

Note: See TracChangeset for help on using the changeset viewer.