IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Dec 10, 2010, 2:36:07 PM (15 years ago)
Author:
watersc1
Message:

Needs to run a test, but the development should be finished on nightly science, v2.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/czw_branch/20101203/ippScripts/scripts/nightly_science.pl

    r30014 r30017  
    4343# Grab options
    4444my ( $date, $datetime, $camera, $dbname, $logfile, $verbose, $manual);
    45 my ( $help, $isburning, $force_stack_count, $test_mode, $this_target_only, $this_filter_only, $this_mode_only, $check_mode);
     45my ( $help, $isburning, $force_stack_count, $force_diff_count, $test_mode, $this_target_only, $this_filter_only, $this_mode_only, $check_mode);
    4646my ( $registration_status, $burntool_status, $observing_status);
    4747my ( $check_registration, $define_burntool, $queue_burntool, $check_chips, $queue_chips);
     
    6060    'test_mode'            => \$test_mode,
    6161    'force_stack_count'    => \$force_stack_count,
     62    'force_diff_count'    => \$force_diff_count,
    6263    'this_target_only=s'   => \$this_target_only,
    6364    'this_filter_only=s'   => \$this_filter_only,
     
    8889           --verbose
    8990           --force_stack_count    Force the chip/warp counts.
     91           --force_diff_count    Force the chip/warp counts.
    9092           --this_target_only     Process only a single target.
    9193           --this_filter_only     Process only a single filter.
     
    232234        $datetime = DateTime->now(time_zone => 'UTC');
    233235        $datetime->set_year($year);
    234         $datetime->set_month($year);
    235         $datetime->set_day($year);
     236        $datetime->set_month($month);
     237        $datetime->set_day($day);
    236238    }
    237239}   
     
    275277}
    276278
     279$metadata_out{nsObservingState} = &get_observing_state($date);
    277280#
    278281# Mode selection
    279282################################################################################
     283
    280284if (defined($test_mode)) {
    281285    $debug = 1;
     
    304308    }
    305309}
     310if (defined($observing_status) || defined($test_mode)) {
     311    $metadata_out{nsState} = 'CHECK_OBSERVING_STATUS';
     312    return_metadata($date);
     313    unless (defined($test_mode)) { exit(0); }
     314}
     315
    306316if (defined($registration_status) || defined($test_mode)) {
    307317    &check_summit_copy($date);
     
    314324    unless (defined($test_mode)) { exit(0); }
    315325}
    316 if (defined($observing_status) || defined($test_mode)) {
    317     $metadata_out{nsState} = &get_observing_state($date);
    318     return_metadata($date);
    319     unless (defined($test_mode)) { exit(0); }
    320 }
    321326
    322327if (defined($check_dqstats) || defined($test_mode)) {
     
    330335    unless (defined($test_mode)) { exit(0); }
    331336}
    332 exit(10);
     337
    333338if (defined($check_detrends) || defined($test_mode)) {
    334     $metadata_out{nsState} = 'CHECKDETRENDS';
     339    $metadata_out{nsDetState} = 'CHECKDETRENDS';
    335340    &execute_detrends($date,"pretend");
    336341    return_metadata($date);
     
    338343}
    339344if (defined($queue_detrends)) {
    340     $metadata_out{nsState} = 'QUEUEDETRENDS';
     345    $metadata_out{nsDetState} = 'QUEUEDETRENDS';
    341346    &execute_detrends($date);
    342347    return_metadata($date);
     
    344349}
    345350
     351if (defined($clean_old) || defined($test_mode)) {
     352    if (defined($test_mode)) {
     353        &execute_cleans($date,"pretend");
     354    }
     355    else {
     356        &execute_cleans($date);
     357    }
     358    unless (defined($test_mode)) { exit(0); }
     359}
     360
    346361if (defined($check_stacks) || defined($test_mode)) {
    347     $metadata_out{nsState} = 'TOWARP';
    348     &execute_stacks($date,"pretend");
    349     if ($metadata_out{nsState} eq 'FORCETOWARP') {
    350         $metadata_out{nsState} = 'TOWARP';
     362    $metadata_out{nsStackState} = 'TOWARP';
     363    &execute_stacks($date,$metadata_out{nsObservingState},"pretend");
     364    if ($metadata_out{nsStackState} eq 'FORCETOWARP') {
     365        $metadata_out{nsStackState} = 'TOWARP';
    351366    }
    352367    return_metadata($date);
     
    354369}
    355370if (defined($queue_stacks)) {
    356     $metadata_out{nsState} = 'STACKING';
    357     &execute_stacks($date);
    358     if ($metadata_out{nsState} eq 'QUEUESTACKING') {
    359         $metadata_out{nsState} = 'STACKING_POSSIBLE_ERROR';
     371    $metadata_out{nsStackState} = 'STACKING';
     372    &execute_stacks($date,$metadata_out{nsObservingState});
     373    if ($metadata_out{nsStackState} eq 'QUEUESTACKING') {
     374        $metadata_out{nsStackState} = 'STACKING_POSSIBLE_ERROR';
    360375    }
    361376    return_metadata($date);
     
    364379
    365380if (defined($check_diffs) || defined($test_mode)) {
    366     $metadata_out{nsState} = 'TOWARP';
    367     &execute_diffs($date,"pretend");
    368     if ($metadata_out{nsState} eq 'FORCETOWARP') {
    369         $metadata_out{nsState} = 'TOWARP';
     381    $metadata_out{nsDiffState} = 'TOWARP';
     382    &execute_diffs($date,$metadata_out{nsObservingState},"pretend");
     383    if ($metadata_out{nsDiffState} eq 'FORCETOWARP') {
     384        $metadata_out{nsDiffState} = 'TOWARP';
    370385    }
    371386    return_metadata($date);
     
    373388}
    374389if (defined($queue_diffs)) {
    375     $metadata_out{nsState} = 'STACKING';
    376     &execute_diffs($date);
    377     if ($metadata_out{nsState} eq 'QUEUESTACKING') {
    378         $metadata_out{nsState} = 'STACKING_POSSIBLE_ERROR';
     390    $metadata_out{nsDiffState} = 'DIFFING';
     391    &execute_diffs($date,$metadata_out{nsObservingState});
     392    if ($metadata_out{nsDiffState} eq 'QUEUESTACKING') {
     393        $metadata_out{nsDiffState} = 'STACKING_POSSIBLE_ERROR';
    379394    }
    380395    return_metadata($date);
    381396    exit(0);
    382397}
    383 
     398exit(10);
    384399if (defined($check_confirm_stacks) || defined($test_mode)) {
    385     $metadata_out{nsState} = 'CONFIRM_STACKING';
     400    $metadata_out{nsStackState} = 'CONFIRM_STACKING';
    386401    &confirm_stacks($date,"pretend");
    387402    return_metadata($date);
     
    389404}
    390405if (defined($confirm_stacks)) {
    391     $metadata_out{nsState} = 'CONFIRM_STACKING';
     406    $metadata_out{nsStackState} = 'CONFIRM_STACKING';
    392407    &confirm_stacks($date);
    393408    return_metadata($date);
     
    396411
    397412if (defined($check_sweetspot) || defined($test_mode) || defined($check_mode)) {
    398     $metadata_out{nsState} = 'CHECKSWEETSPOT';
     413    $metadata_out{nsSSState} = 'CHECKSWEETSPOT';
    399414    &execute_sweetspot($date,"pretend");
    400415    return_metadata($date);
     
    402417}
    403418if (defined($queue_sweetspot)) {
    404     $metadata_out{nsState} = 'CHECKSWEETSPOT';
     419    $metadata_out{nsSSState} = 'CHECKSWEETSPOT';
    405420    &execute_sweetspot($date);
    406421    return_metadata($date);
     
    408423}
    409424
    410 if (defined($clean_old) || defined($test_mode)) {
    411     if (defined($test_mode)) {
    412         &execute_cleans($date,"pretend");
    413     }
    414     else {
    415         &execute_cleans($date);
    416     }
    417     unless (defined($test_mode)) { exit(0); }
    418 }
    419425exit(0);
    420426#
     
    488494    if ($Nsummit_exps == 0) {
    489495        print STDERR "No exposures were found on the summit for $date.\n";
    490         $metadata_out{nsState} = 'DROP';
     496        $metadata_out{nsRegState} = 'DROP';
    491497    }
    492498    elsif ($Nfaults != 0) {
    493499        print STDERR "There were faults while downloading the exposures for $date.\n";
    494         $metadata_out{nsState} = 'NEW';
     500        $metadata_out{nsRegState} = 'NEW';
    495501    }
    496502    elsif ($Ndownload_imfiles != $Nsummit_imfiles) {
    497503        print STDERR "Not done downloading from the summit for $date (Summit: $Nsummit_imfiles, Downloaded: $Ndownload_imfiles)\n";
    498         $metadata_out{nsState} = 'NEW';
     504        $metadata_out{nsRegState} = 'NEW';
    499505    }
    500506    else {
    501507        print STDERR "Summit copy and Registration have succeeded for $date.\n";
    502         $metadata_out{nsState} = 'REGISTERED';
     508        $metadata_out{nsRegState} = 'REGISTERED';
    503509    }
    504510
     
    721727    }
    722728}   
    723 # #
    724 # # Detrend verification
    725 # ################################################################################
    726 
    727 # sub construct_dettool_cmd {
    728 #     my $date = shift;
    729 #     my $target = shift;
    730 
    731 #     my ($label,$workdir,$filter,$exp_type,$det_type,$ref_det_id,$ref_iter,$maxN) = get_dettool_parameters($date,$target);
    732    
    733 #     my $select = "-select_dateobs_begin ${date}T00:00:00 -select_dateobs_end ${date}T23:59:59 ";
    734 #     my $use_limits = " -use_begin ${date}T00:00:00 -use_end ${date}T23:59:59 ";
    735 #     $date =~ s/-//g;
    736 
    737 #     my $cmd = "$dettool";
    738 # #    $cmd .= " -pretend ";
    739 #     $cmd .= " -simple -dbname $dbname -definebyquery -det_type $det_type ";
    740 #     $cmd .= " -mode verify -ref_det_id $ref_det_id -ref_iter $ref_iter ";
    741 #     $cmd .= " $select ";
    742 #     $cmd .= " -inst $camera ";
    743 #     $cmd .= " -select_exp_type $exp_type ";
    744 #     $cmd .= " -select_filter $filter " if defined($filter);
    745 #     $cmd .= " -workdir $workdir ";
    746 #     $cmd .= " -label $label ";
    747 #     $cmd .= " $use_limits ";
    748 #     if ($maxN > 0) {
    749 #       $cmd .= " -random_subset -random_limit $maxN ";
    750 #     }
    751 #     if ($debug == 1) {
    752 #       $cmd .= ' -pretend ';
    753 #     }
    754 #     print STDERR "$cmd\n";
    755 #     return($cmd);
    756 # }   
    757 
    758 # sub verify_uniqueness_detverify {
    759 #     my $date = shift;
    760 #     my $target = shift;
    761 
    762 #     my ($label,$workdir,$filter,$exp_type,$det_type,$ref_det_id,$ref_iter,$maxN) = get_dettool_parameters($date,$target);
    763    
    764 #     my $db = init_gpc_db();
    765 #     my $sth = "SELECT * FROM detRun WHERE workdir = '$workdir' AND ref_det_id = $ref_det_id AND ref_iter = $ref_iter";
    766 #     my $data_ref = $db->selectall_arrayref( $sth );
    767 
    768 #     return($#{ $data_ref } + 1);
    769 # }
    770 
    771 # sub pre_detrend_queue {
    772 #     my $date = shift;
    773 #     my $target = shift;
    774    
    775 #     my $command = construct_dettool_cmd($date,$target) . ' -pretend ';
    776 #     my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    777 #       run ( command => $command, verbose => $verbose);
    778 #     unless ($success) {
    779 #       $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    780 #       &my_die("Unable to perform dettool: $error_code", 0,0, $date, $PS_EXIT_SYS_ERROR);
    781 #     }
    782    
    783 #     my @input_exposures = split /\n/, (join '', @$stdout_buf);
    784 #     return($#input_exposures + 1);
    785 # }
    786 
    787 # sub detrend_queue {
    788 #     my $date = shift;
    789 #     my $target = shift;
    790    
    791 #     my $command = construct_dettool_cmd($date,$target);
    792 #     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    793 #       run ( command => $command, verbose => $verbose);
    794 #     unless ($success) {
    795 #       $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    796 #       &my_die("Unable to perform chiptool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
    797 #     }
    798 #     $metadata_out{nsState} = 'DETREND_QUEUED';
    799 #     return(0);
    800 
    801 # }
    802 # sub execute_detrends {
    803 #     my $date = shift;
    804 #     my $pretend = shift;
    805 #     my $exposures = 0;
    806 #     foreach my $target (@detrend_list) {
    807 #       my ($Nexposures) = pre_detrend_queue($date,$target);
    808 #       if ($Nexposures == 0) {
    809 #           print STDERR "execute_detrends: Target $target on $date had no exposures.\n";
    810 #           next;
    811 #       }
    812 #       $exposures++;
    813 #       unless(defined($pretend)) {
    814 #           detrend_queue($date,$target);
    815 #       }
    816 #       if (defined($pretend)) {
    817 #           add_to_macro_list('check_detrends',1,$date,$target,"dettool");
    818 #       }
    819 #       else {
    820 #           add_to_macro_list('queue_detrends',1,$date,$target,"dettool");
    821 #       }
    822 #     }
    823 #     if ($exposures == 0) {
    824 #       $metadata_out{nsState} = 'DETREND_DROP';
    825 #     }
    826 #     if (($metadata_out{nsState} eq 'CHECKDETRENDS') && ($exposures > 0)) {
    827 #       $metadata_out{nsState} eq 'QUEUEDETRENDS';
    828 #     }
    829 # }
     729#
     730# Detrend verification
     731################################################################################
     732
     733sub construct_dettool_cmd {
     734    my $date = shift;
     735    my $target = shift;
     736
     737    my ($label,$workdir,$filter,$exp_type,$det_type,$ref_det_id,$ref_iter,$maxN) = get_dettool_parameters($date,$target);
     738   
     739    my $select = "-select_dateobs_begin ${date}T00:00:00 -select_dateobs_end ${date}T23:59:59 ";
     740    my $use_limits = " -use_begin ${date}T00:00:00 -use_end ${date}T23:59:59 ";
     741    $date =~ s/-//g;
     742
     743    my $cmd = "$dettool";
     744#    $cmd .= " -pretend ";
     745    $cmd .= " -simple -dbname $dbname -definebyquery -det_type $det_type ";
     746    $cmd .= " -mode verify -ref_det_id $ref_det_id -ref_iter $ref_iter ";
     747    $cmd .= " $select ";
     748    $cmd .= " -inst $camera ";
     749    $cmd .= " -select_exp_type $exp_type ";
     750    $cmd .= " -select_filter $filter " if defined($filter);
     751    $cmd .= " -workdir $workdir ";
     752    $cmd .= " -label $label ";
     753    $cmd .= " $use_limits ";
     754    if ($maxN > 0) {
     755        $cmd .= " -random_subset -random_limit $maxN ";
     756    }
     757    if ($debug == 1) {
     758        $cmd .= ' -pretend ';
     759    }
     760    print STDERR "$cmd\n";
     761    return($cmd);
     762}   
     763
     764sub verify_uniqueness_detverify {
     765    my $date = shift;
     766    my $target = shift;
     767
     768    my ($label,$workdir,$filter,$exp_type,$det_type,$ref_det_id,$ref_iter,$maxN) = get_dettool_parameters($date,$target);
     769   
     770    my $db = init_gpc_db();
     771    my $sth = "SELECT * FROM detRun WHERE workdir = '$workdir' AND ref_det_id = $ref_det_id AND ref_iter = $ref_iter";
     772    my $data_ref = $db->selectall_arrayref( $sth );
     773
     774    return($#{ $data_ref } + 1);
     775}
     776
     777sub pre_detrend_queue {
     778    my $date = shift;
     779    my $target = shift;
     780   
     781    my $command = construct_dettool_cmd($date,$target) . ' -pretend ';
     782    my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     783        run ( command => $command, verbose => $verbose);
     784    unless ($success) {
     785        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     786        &my_die("Unable to perform dettool: $error_code", 0,0, $date, $PS_EXIT_SYS_ERROR);
     787    }
     788   
     789    my @input_exposures = split /\n/, (join '', @$stdout_buf);
     790    return($#input_exposures + 1);
     791}
     792
     793sub detrend_queue {
     794    my $date = shift;
     795    my $target = shift;
     796   
     797    my $command = construct_dettool_cmd($date,$target);
     798    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     799        run ( command => $command, verbose => $verbose);
     800    unless ($success) {
     801        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     802        &my_die("Unable to perform chiptool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
     803    }
     804    $metadata_out{nsDetState} = 'DETREND_QUEUED';
     805    return(0);
     806
     807}
     808sub execute_detrends {
     809    my $date = shift;
     810    my $pretend = shift;
     811    my $exposures = 0;
     812    foreach my $target (sort (keys %detrend_config)) {
     813        my ($Nexposures) = pre_detrend_queue($date,$target);
     814        if ($Nexposures == 0) {
     815            print STDERR "execute_detrends: Target $target on $date had no exposures.\n";
     816            next;
     817        }
     818        $exposures++;
     819        unless(defined($pretend)) {
     820            detrend_queue($date,$target);
     821        }
     822        if (defined($pretend)) {
     823            add_to_macro_list('check_detrends',1,$date,$target,"dettool");
     824        }
     825        else {
     826            add_to_macro_list('queue_detrends',1,$date,$target,"dettool");
     827        }
     828    }
     829    if ($exposures == 0) {
     830        $metadata_out{nsDetState} = 'DETREND_DROP';
     831    }
     832    if (($metadata_out{nsDetState} eq 'CHECKDETRENDS') && ($exposures > 0)) {
     833        $metadata_out{nsDetState} eq 'QUEUEDETRENDS';
     834    }
     835}
    830836
    831837# #
     
    938944#     if ($Nstacks == 0) {
    939945#       print STDERR "execute_sweetspot: No new stacks to make ($Nstacks)\n";
    940 #       $metadata_out{nsState} = 'SS_EMPTY';
     946#       $metadata_out{nsSSState} = 'SS_EMPTY';
    941947#       return();
    942948#     }
    943949#     if ($Nstacks < 10) {
    944950#       print STDERR "execute_sweetspot: Too few new stacks to make ($Nstacks)\n";
    945 #       $metadata_out{nsState} = 'SS_FEW';
     951#       $metadata_out{nsSSState} = 'SS_FEW';
    946952#       return();
    947953#     }
    948954#     if ($is_lunation_date == 0) {
    949955#       print STDERR "execute_sweetspot: Invalid lunation date. ($date $is_lunation_date)\n";
    950 #       $metadata_out{nsState} = 'SS_ERROR';
     956#       $metadata_out{nsSSState} = 'SS_ERROR';
    951957#       return();
    952958#     }
    953 #     $metadata_out{nsState} = 'SS_QUEUE';
     959#     $metadata_out{nsSSState} = 'SS_QUEUE';
    954960#     $metadata_out{nsSweetSpot} = $Nstacks;
    955961#     unless(defined($pretend)) {
    956 #       $metadata_out{nsState} = 'SS_DONE';
     962#       $metadata_out{nsSSState} = 'SS_DONE';
    957963#       sweetspot_queue($date);
    958964#     }
     
    965971# }
    966972
    967 # #
    968 # # Stacking
    969 # ################################################################################
    970 
    971 # sub construct_stacktool_cmd {
    972 #     my $date = shift;
    973 #     my $target = shift;
    974 #     my $filter = shift;
    975 
    976 #     my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
    977 
    978 #     my $select =  "-select_dateobs_begin ${date}T00:00:00 -select_dateobs_end ${date}T23:59:59 ";
    979 #     $date =~ s/-//g;
    980 
    981 #     my $cmd = "$stacktool";
    982 # #    $cmd .= ' -pretend -simple -dbname gpc1 -definebyquery -min_new 4 ';  # Probably silly, but I want to be safe and not overwrite
    983 #     $cmd .= " -simple -dbname $dbname -definebyquery ";
    984 #     $cmd .= " -set_label $label -select_label $label ";
    985 #     $cmd .= " -set_workdir $workdir -set_dist_group $dist_group ";
    986 #     $cmd .= " -select_filter $filter -set_data_group $data_group ";
    987 #     $cmd .= " -min_num 4";
    988 #     $cmd .= " -select_good_frac_min 0.05";
    989 #     $cmd .= " $select ";
    990 #     if ($debug == 1) {
    991 #         $cmd .= ' -pretend ';
    992 #     }
    993 #     print STDERR "$cmd\n";
    994 #     return($cmd);
    995 # }
    996 
    997 # sub verify_uniqueness_stack {
    998 #     my $skycell = shift;
    999 #     my $date = shift;
    1000 #     my $target = shift;
    1001 #     my $filter = shift;
    1002 
    1003 #     my $db = init_gpc_db();
    1004 #     $date =~ s/-//g;
    1005 #     my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
    1006 
    1007 #     my $sth = "SELECT skycell_id from stackRun where data_group = '$data_group' AND skycell_id = '$skycell' AND filter = '$filter' AND tess_id = '$tess_id'";
    1008 #     my $data_ref = $db->selectall_arrayref( $sth );
    1009 
    1010 #     return($#{ $data_ref } + 1);
    1011 # }
    1012 
    1013 # sub pre_stack_queue {
    1014 #     my $date = shift;
    1015 #     my $target = shift;
    1016 #     my $filter = shift;
    1017 
    1018 #     my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
    1019 #     # check warp stage == chip stage
    1020 #     my $db = init_gpc_db();
    1021 
    1022 #     my $trunc_date = $date; $trunc_date =~ s/-//g;
    1023 
    1024 #     my $where = " label = '$label' AND data_group = '$data_group' ";
    1025 
    1026 #     my $where_possibly_faulted = $where . " AND ( " ;
    1027 #     foreach my $acceptable_quality (@unrecoverable_quality) {
    1028 #       $where_possibly_faulted .= " quality = $acceptable_quality OR ";
    1029 #     }
    1030 #     $where_possibly_faulted .= " 0 )";
    1031 
    1032 #     my $chip_sth = "SELECT * from chipRun WHERE (state = 'full' OR state = 'new') AND $where ";
    1033 #     my $cam_sth  = "SELECT * from camRun JOIN camProcessedExp USING(cam_id) WHERE state = 'full' AND $where_possibly_faulted ";
    1034 #     my $warp_sth = "SELECT * from warpRun WHERE state = 'full' AND $where ";
    1035 
    1036 #     my $chip_ref = $db->selectall_arrayref( $chip_sth );
    1037 #     my $cam_ref  = $db->selectall_arrayref( $cam_sth );
    1038 #     my $warp_ref = $db->selectall_arrayref( $warp_sth );
    1039 
    1040 #     # check that we will be able to queue them up
    1041 #     my $command = construct_stacktool_cmd($date,$target,$filter) . ' -pretend ';
    1042 #     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1043 #         run ( command => $command, verbose => $verbose );
    1044 #     unless ($success) {
    1045 #         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1046 #         &my_die("Unable to perform stacktool: $error_code", $#{ $chip_ref },$#{ $warp_ref },$date, $PS_EXIT_SYS_ERROR);
    1047 #     }
    1048 
    1049 #     my $Nalready = 0;
    1050 
    1051 #     my @input_exposures = split /\n/, (join '', @$stdout_buf);
    1052 #     foreach my $entry (@input_exposures) {
    1053 #         my ($warp_tess_id,$skycell, @trash) = split /\s+/, $entry;
    1054 #         @trash = verify_uniqueness_stack($skycell,$date,$target,$filter);
    1055 #         $Nalready += $trash[0];
    1056 #     }
    1057 
    1058 #     return($#input_exposures  + 1, $#{ $chip_ref } + 1, $#{ $cam_ref } + $#{ $warp_ref } + 2, $Nalready);
    1059 # }
    1060 
    1061 # sub stack_queue {
    1062 #     my $date = shift;
    1063 #     my $target = shift;
    1064 #     my $filter = shift;
    1065 
    1066 #     my $command = construct_stacktool_cmd($date,$target,$filter);
    1067 #     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1068 #         run ( command => $command, verbose => $verbose );
    1069 #     unless ($success) {
    1070 #         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1071 #         &my_die("Unable to perform stacktool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
    1072 #     }
    1073    
    1074    
    1075 #     return(0);
    1076 # }
    1077 
    1078 # sub execute_stacks {
    1079 #     my $date = shift;
    1080 #     my $pretend = shift;
    1081 #     foreach my $target (@target_list) {
    1082 #       foreach my $filter (@filter_list) {
    1083 #           if (exists($extra_processing{$target})) {
    1084 #                 my ($Nexposures,$NprocChips,$NprocWarps,$Nalready) = pre_stack_queue($date,$target,$filter);
    1085 #                 if ((!defined($force_stack_count))&&($NprocChips != $NprocWarps)) { # This makes me sad. :(
    1086 #                   next;
    1087 #               }
    1088 #               if ($Nexposures == 0) {
    1089 #                   if ($debug == 1) {
    1090 #                       print STDERR "execute_stacks: Target $target in filter $filter on $date has no exposures.\n";
    1091 #                   }
    1092 #                     next;
    1093 #               }                   
    1094 #               else {
    1095 #                   do_extra_processing($date,$target,$filter,$pretend);
    1096 #               }
    1097 
    1098 #           }
    1099 #           if ($science_config{$target}{STACKABLE} == 1) {
    1100 #                 my ($Nexposures,$NprocChips,$NprocWarps,$Nalready) = pre_stack_queue($date,$target,$filter);
    1101 #                 if ((!defined($force_stack_count))&&($NprocChips != $NprocWarps)) { # This makes me sad. :(
    1102 #                   if ($debug == 1) {
    1103 #                       print STDERR "execute_stacks: Target $target on $date is not fully processed. ($NprocChips $NprocWarps)\n";
    1104 #                   }
    1105 #                     $metadata_out{nsState} = 'FORCETOWARP';
    1106 #                     next;
    1107 #                 }
    1108 #                 if ($Nexposures == 0) {
    1109 #                   if ($debug == 1) {
    1110 #                       print STDERR "execute_stacks: Target $target in filter $filter on $date has no exposures.\n";
    1111 #                   }
    1112 #                     next;
    1113 #                 }
    1114 #                 if ($Nalready != 0) {
    1115 #                   if ($debug == 1) {
    1116 #                       print STDERR "execute_stacks: Not queueing $target in filter $filter on $date due to already existing stacks.\n";
    1117 #                   }
    1118 #                   unless ($metadata_out{nsState} eq 'FORCETOWARP') {
    1119 #                       $metadata_out{nsState} = 'STACKING';
    1120 #                   }
    1121 #                     next;
    1122 #                 }
    1123 #                 unless (($metadata_out{nsState} eq 'FORCETOWARP')||($metadata_out{nsState} eq 'STACKING')) {
    1124 #                     $metadata_out{nsState} = 'QUEUESTACKS';
    1125 #                 }
    1126 #                 unless(defined($pretend)) {
    1127 #                   if ($debug == 1) {
    1128 #                       print STDERR "execute_stacks: Target $target in filter $filter on $date has exposures and will be queued.\n";
    1129 #                   }
    1130 #                     stack_queue($date,$target,$filter);
    1131 #                 }
    1132 #               if (defined($pretend)) {
    1133 #                   add_to_macro_list('check_stacks',$stackable_list{$target},$date,$target,$filter);
    1134 #               }
    1135 #               else {
    1136 #                   add_to_macro_list('queue_stacks',$stackable_list{$target},$date,$target,$filter);
    1137 #               }
    1138 #             }
    1139 #           else {
    1140 #               # print STDERR "execute_stacks: Target $target is not auto-stackable.\n";
    1141 #           }
    1142 #         }
    1143 #     }
    1144 # }
    1145 
    1146 # sub post_stack_queue {
    1147 #     my $date = shift;
    1148 #     my $target = shift;
    1149 
    1150 #     my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
    1151 #     # check warp stage == chip stage
    1152 #     my $db = init_gpc_db();
    1153 
    1154 #     my $trunc_date = $date; $trunc_date =~ s/-//g;
    1155 
    1156 #     my $where = " label = '$label' AND data_group = '$data_group' ";
    1157 
    1158 #     my $stack_full_sth = "SELECT * from stackRun where $where ";
    1159 #     my $stack_done_sth = "SELECT * from stackRun LEFT JOIN stackSumSkyfile USING(stack_id) WHERE $where ";
    1160 #     $stack_done_sth .= " AND ((state = 'full') OR (state = 'new' && fault != 0)) ";
    1161 #     if ($debug == 1) {
    1162 #       print STDERR "post_stack_queue: database queries:\n";
    1163 #       print STDERR "$stack_full_sth\n";
    1164 #       print STDERR "$stack_done_sth\n";
    1165 #     }
    1166 #     my $stack_full_ref = $db->selectall_arrayref( $stack_full_sth );
    1167 #     my $stack_done_ref = $db->selectall_arrayref( $stack_done_sth );
    1168 
    1169 #     return($#{ $stack_full_ref } + 1, $#{ $stack_done_ref } + 1);
    1170 # }
    1171 
    1172 # sub confirm_stacks {
    1173 #     my $date = shift;
    1174 #     my $pretend = shift;
    1175    
    1176 #     foreach my $target (@target_list) {
    1177 #         if ($stackable_list{$target} == 1) {
    1178 #           my ($Nstacks, $Nattempted) = post_stack_queue($date,$target);
    1179 #           if ($debug == 1) {
    1180 #               print STDERR "confirm_stacks: Target $target on $date has $Nattempted attempts on $Nstacks.\n";
    1181 #           }
    1182 #           if ($Nstacks != $Nattempted) {
    1183 #               if ($debug == 1) {
    1184 #                   print STDERR "confirm_stacks: Target $target on $date is not done stacking. $Nstacks $Nattempted\n";
    1185 #               }
    1186 #               if ($metadata_out{nsState} eq 'CONFIRM_STACKING') {
    1187 #                   $metadata_out{nsState} = 'STACKING';
    1188 #               }
    1189 #               next;
    1190 #           }
    1191 #           if ($Nstacks == 0) {
    1192 #               if ($debug == 1) {
    1193 #                   print STDERR "confirm_stacks: Target $target on $date has no stacks. Skipping.\n";
    1194 #               }
    1195 #               next;
    1196 #           }
    1197 #           if ($metadata_out{nsState} eq 'CONFIRM_STACKING') {
    1198 #               if (defined($pretend)) {
    1199 #                   add_to_macro_list('check_confirm_stacks',$stackable_list{$target},$date,$target);
    1200 #               }
    1201 #               else {
    1202 #                   add_to_macro_list('confirm_stacks',$stackable_list{$target},$date,$target);
    1203 #               }
    1204 #           }
    1205 #       }
    1206 #     }
    1207 # }         
    1208 
    1209 # #
    1210 # # Extra processing
    1211 # ################################################################################
    1212 
    1213 # sub verify_uniqueness_diff {
    1214 #     my $warp_id_1 = shift;
    1215 #     my $warp_id_2 = shift;
    1216 #     my $date = shift;
    1217 #     my $target = shift;
    1218 
    1219 #     my $db = init_gpc_db();
    1220 #     $date =~ s/-//g;
    1221 #     my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
    1222 
    1223 #     my $sth = "SELECT diff_id from diffRun JOIN diffInputSkyfile USING(diff_id) where data_group = '$data_group' AND warp1 = $warp_id_1 AND warp2 = $warp_id_2";
    1224 #     my $data_ref = $db->selectall_arrayref( $sth );
    1225 
    1226 #     return($#{ $data_ref } + 1);
    1227 # }
    1228 
    1229 # sub do_extra_processing {
    1230 #     my $date = shift;
    1231 #     my $target = shift;
    1232 #     my $filter = shift;
    1233 #     my $pretend = shift;
    1234 #     my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
    1235 
    1236 #     if ($target eq 'OSS') {
    1237 #       my $db = init_gpc_db();
    1238 
    1239 #       my $obj_sth = "select DISTINCT rawExp.object from warpRun JOIN fakeRun USING(fake_id) JOIN camRun USING(cam_id) JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id) ";
    1240 #       $obj_sth .= " WHERE warpRun.state = 'full' AND warpRun.label = '$label' AND warpRun.data_group = '$data_group' AND rawExp.filter = '$filter' ORDER BY rawExp.object";
    1241 #       print STDERR "$obj_sth\n";
    1242 #       my $object_ref = $db->selectall_arrayref( $obj_sth );
    1243 
    1244 #       foreach my $object_row (@{ $object_ref }) {
    1245 #           my $this_object = shift @{ $object_row };
    1246 #           my $input_sth = "select exp_id,warp_id,dateobs from warpRun JOIN fakeRun USING(fake_id) JOIN camRun USING(cam_id) JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id) ";
    1247 #           $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' ";
    1248 #           $input_sth .= " ORDER BY dateobs ";
    1249 
    1250 #           my $warps = $db->selectall_arrayref( $input_sth );
     973#
     974# Stacking
     975################################################################################
     976
     977
     978sub construct_stacktool_cmd {
     979    my $date = shift;
     980    my $target = shift;
     981    my $filter = shift;
     982
     983    my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
     984
     985    my $select =  "-select_dateobs_begin ${date}T00:00:00 -select_dateobs_end ${date}T23:59:59 ";
     986    $date =~ s/-//g;
     987
     988    my $cmd = "$stacktool";
     989#    $cmd .= ' -pretend -simple -dbname gpc1 -definebyquery -min_new 4 ';  # Probably silly, but I want to be safe and not overwrite
     990    $cmd .= " -simple -dbname $dbname -definebyquery ";
     991    $cmd .= " -set_label $label -select_label $label ";
     992    $cmd .= " -set_workdir $workdir -set_dist_group $dist_group ";
     993    $cmd .= " -select_filter $filter -set_data_group $data_group ";
     994    $cmd .= " -min_num 4";
     995    $cmd .= " -select_good_frac_min 0.05";
     996    $cmd .= " $select ";
     997    if ($debug == 1) {
     998        $cmd .= ' -pretend ';
     999    }
     1000    print STDERR "$cmd\n";
     1001    return($cmd);
     1002}
     1003
     1004sub verify_uniqueness_stack {
     1005    my $skycell = shift;
     1006    my $date = shift;
     1007    my $target = shift;
     1008    my $filter = shift;
     1009
     1010    my $db = init_gpc_db();
     1011    $date =~ s/-//g;
     1012    my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
     1013
     1014    my $sth = "SELECT skycell_id from stackRun where data_group = '$data_group' AND skycell_id = '$skycell' AND filter = '$filter' AND tess_id = '$tess_id'";
     1015    my $data_ref = $db->selectall_arrayref( $sth );
     1016
     1017    return($#{ $data_ref } + 1);
     1018}
     1019
     1020sub pre_stack_queue {
     1021    my $date = shift;
     1022    my $observing_state = shift;
     1023    my $target = shift;
     1024    my $filter = shift;
     1025
     1026    my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
     1027
     1028    my $db = init_gpc_db();
     1029
     1030    my $trunc_date = $date; $trunc_date =~ s/-//g;
     1031
     1032    my $where = " label = '$label' AND data_group = '$data_group' ";
     1033
     1034    my $where_possibly_faulted = $where . " AND ( " ;
     1035    foreach my $acceptable_quality (@unrecoverable_quality) {
     1036        $where_possibly_faulted .= " quality = $acceptable_quality OR ";
     1037    }
     1038    $where_possibly_faulted .= " 0 )";
     1039
     1040    my $chip_sth = "SELECT * from chipRun WHERE (state = 'full' OR state = 'new') AND $where ";
     1041    my $cam_sth  = "SELECT * from camRun JOIN camProcessedExp USING(cam_id) WHERE state = 'full' AND $where_possibly_faulted ";
     1042    my $warp_sth = "SELECT * from warpRun WHERE state = 'full' AND $where ";
     1043
     1044    my $chip_ref = $db->selectall_arrayref( $chip_sth );
     1045    my $cam_ref  = $db->selectall_arrayref( $cam_sth );
     1046    my $warp_ref = $db->selectall_arrayref( $warp_sth );
     1047
     1048    # check that we will be able to queue them up
     1049    my $command = construct_stacktool_cmd($date,$target,$filter) . ' -pretend ';
     1050    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1051        run ( command => $command, verbose => $verbose );
     1052    unless ($success) {
     1053        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1054        &my_die("Unable to perform stacktool: $error_code", $#{ $chip_ref },$#{ $warp_ref },$date, $PS_EXIT_SYS_ERROR);
     1055    }
     1056
     1057    my $Nalready = 0;
     1058
     1059    my @input_exposures = split /\n/, (join '', @$stdout_buf);
     1060    foreach my $entry (@input_exposures) {
     1061        my ($warp_tess_id,$skycell, @trash) = split /\s+/, $entry;
     1062        @trash = verify_uniqueness_stack($skycell,$date,$target,$filter);
     1063        $Nalready += $trash[0];
     1064    }
     1065   
     1066    my $minimum_required_warp;
     1067    if ($observing_state eq 'OBSERVING') {
     1068        $minimum_required_warp = $science_config{$target}{MIN_STACK};
     1069    }
     1070    unless (defined($minimum_required_warp)) {
     1071        $minimum_required_warp = 0;
     1072    }
     1073
     1074    return($#input_exposures  + 1, $#{ $chip_ref } + 1, $#{ $cam_ref } + $#{ $warp_ref } + 2, $minimum_required_warp, $Nalready);
     1075}
     1076
     1077sub stack_queue {
     1078    my $date = shift;
     1079    my $target = shift;
     1080    my $filter = shift;
     1081
     1082    my $command = construct_stacktool_cmd($date,$target,$filter);
     1083    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1084        run ( command => $command, verbose => $verbose );
     1085    unless ($success) {
     1086        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1087        &my_die("Unable to perform stacktool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
     1088    }
     1089   
     1090   
     1091    return(0);
     1092}
     1093
     1094sub execute_stacks {
     1095    my $date = shift;
     1096    my $observing_state = shift;
     1097    my $pretend = shift;
     1098
     1099    foreach my $target (sort (keys %science_config)) {
     1100        if ($science_config{$target}{STACKABLE} == 1) {
     1101            foreach my $filter (@filter_list) {
     1102                my ($Nexposures,$NprocChips,$NprocWarps,$NrequiredWarps,$Nalready) = pre_stack_queue($date,$observing_state,$target,$filter);
     1103                if ((!defined($force_stack_count))&&($NprocChips != $NprocWarps)) { # This makes me sad. :(
     1104                    if ($debug == 1) {
     1105                        print STDERR "execute_stacks: Target $target on $date is not fully processed. ($NprocChips $NprocWarps)\n";
     1106                    }
     1107                    $metadata_out{nsStackState} = 'FORCETOWARP';
     1108                    next;
     1109                }
     1110                if ($Nexposures == 0) {
     1111                    if ($debug == 1) {
     1112                        print STDERR "execute_stacks: Target $target in filter $filter on $date has no exposures.\n";
     1113                    }
     1114                    next;
     1115                }
     1116                if ($NprocWarps < $NrequiredWarps) {
     1117                    if ($debug == 1) {
     1118                        print STDERR "execute_stacks: Target $target in filter $filter on $date has too few warps to begin stacking. ($NprocWarps $NrequiredWarps)\n";
     1119                    }
     1120                    next;
     1121                }
     1122                if ($Nalready != 0) {
     1123                    if ($debug == 1) {
     1124                        print STDERR "execute_stacks: Not queueing $target in filter $filter on $date due to already existing stacks.\n";
     1125                    }
     1126                    unless ($metadata_out{nsStackState} eq 'FORCETOWARP') {
     1127                        $metadata_out{nsStackState} = 'STACKING';
     1128                    }
     1129                    next;
     1130                }
     1131                unless (($metadata_out{nsStackState} eq 'FORCETOWARP')||($metadata_out{nsStackState} eq 'STACKING')) {
     1132                    $metadata_out{nsStackState} = 'QUEUESTACKS';
     1133                }
     1134                unless(defined($pretend)) {
     1135                    if ($debug == 1) {
     1136                        print STDERR "execute_stacks: Target $target in filter $filter on $date has exposures and will be queued.\n";
     1137                    }
     1138                    stack_queue($date,$target,$filter);
     1139                }
     1140                if (defined($pretend)) {
     1141                    add_to_macro_list('check_stacks',$science_config{$target}{STACKABLE},$date,$target,$filter);
     1142                }
     1143                else {
     1144                    add_to_macro_list('queue_stacks',$science_config{$target}{STACKABLE},$date,$target,$filter);
     1145                }
     1146            }
     1147        }
     1148        else {
     1149            if ($debug == 1) {
     1150                print STDERR "execute_stacks: Target $target is not auto-stackable.\n";
     1151            }
     1152        }
     1153    }
     1154}
     1155
     1156sub post_stack_queue {
     1157    my $date = shift;
     1158    my $target = shift;
     1159
     1160    my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
     1161    # check warp stage == chip stage
     1162    my $db = init_gpc_db();
     1163
     1164    my $trunc_date = $date; $trunc_date =~ s/-//g;
     1165
     1166    my $where = " label = '$label' AND data_group = '$data_group' ";
     1167
     1168    my $stack_full_sth = "SELECT * from stackRun where $where ";
     1169    my $stack_done_sth = "SELECT * from stackRun LEFT JOIN stackSumSkyfile USING(stack_id) WHERE $where ";
     1170    $stack_done_sth .= " AND ((state = 'full') OR (state = 'new' && fault != 0)) ";
     1171    if ($debug == 1) {
     1172        print STDERR "post_stack_queue: database queries:\n";
     1173        print STDERR "$stack_full_sth\n";
     1174        print STDERR "$stack_done_sth\n";
     1175    }
     1176    my $stack_full_ref = $db->selectall_arrayref( $stack_full_sth );
     1177    my $stack_done_ref = $db->selectall_arrayref( $stack_done_sth );
     1178
     1179    return($#{ $stack_full_ref } + 1, $#{ $stack_done_ref } + 1);
     1180}
     1181
     1182sub confirm_stacks {
     1183    my $date = shift;
     1184    my $pretend = shift;
     1185   
     1186    foreach my $target (sort (keys %science_config)) {
     1187        if ($science_config{$target}{STACKABLE} == 1) {
     1188            my ($Nstacks, $Nattempted) = post_stack_queue($date,$target);
     1189            if ($debug == 1) {
     1190                print STDERR "confirm_stacks: Target $target on $date has $Nattempted attempts on $Nstacks.\n";
     1191            }
     1192            if ($Nstacks != $Nattempted) {
     1193                if ($debug == 1) {
     1194                    print STDERR "confirm_stacks: Target $target on $date is not done stacking. $Nstacks $Nattempted\n";
     1195                }
     1196                if ($metadata_out{nsStackState} eq 'CONFIRM_STACKING') {
     1197                    $metadata_out{nsStackState} = 'STACKING';
     1198                }
     1199                next;
     1200            }
     1201            if ($Nstacks == 0) {
     1202                if ($debug == 1) {
     1203                    print STDERR "confirm_stacks: Target $target on $date has no stacks. Skipping.\n";
     1204                }
     1205                next;
     1206            }
     1207            if ($metadata_out{nsStackState} eq 'CONFIRM_STACKING') {
     1208                if (defined($pretend)) {
     1209                    add_to_macro_list('check_confirm_stacks',$science_config{$target}{STACKABLE},$date,$target);
     1210                }
     1211                else {
     1212                    add_to_macro_list('confirm_stacks',$science_config{$target}{STACKABLE},$date,$target);
     1213                }
     1214            }
     1215        }
     1216    }
     1217}           
     1218
     1219#
     1220# Extra processing
     1221################################################################################
     1222
     1223sub verify_uniqueness_diff {
     1224    my $warp_id_1 = shift;
     1225    my $warp_id_2 = shift;
     1226    my $date = shift;
     1227    my $target = shift;
     1228
     1229    my $db = init_gpc_db();
     1230    $date =~ s/-//g;
     1231    my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
     1232
     1233    my $sth = "SELECT diff_id from diffRun JOIN diffInputSkyfile USING(diff_id) where data_group = '$data_group' AND warp1 = $warp_id_1 AND warp2 = $warp_id_2";
     1234    my $data_ref = $db->selectall_arrayref( $sth );
     1235
     1236    return($#{ $data_ref } + 1);
     1237}
     1238
     1239sub pre_diff_queue {
     1240    my $date = shift;
     1241    my $observing_state = shift;
     1242    my $target = shift;
     1243    my $filter = shift;
     1244    my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
     1245   
     1246    my $db = init_gpc_db();
     1247
     1248    my $trunc_date = $date; $trunc_date =~ s/-//g;
     1249
     1250    my $where = " label = '$label' AND data_group = '$data_group' ";
     1251
     1252    my $where_possibly_faulted = $where . " AND ( " ;
     1253    foreach my $acceptable_quality (@unrecoverable_quality) {
     1254        $where_possibly_faulted .= " quality = $acceptable_quality OR ";
     1255    }
     1256    $where_possibly_faulted .= " 0 )";
     1257
     1258    my $chip_sth = "SELECT * from chipRun WHERE (state = 'full' OR state = 'new') AND $where ";
     1259    my $cam_sth  = "SELECT * from camRun JOIN camProcessedExp USING(cam_id) WHERE state = 'full' AND $where_possibly_faulted ";
     1260    my $warp_sth = "SELECT * from warpRun WHERE state = 'full' AND $where ";
     1261
     1262    my $chip_ref = $db->selectall_arrayref( $chip_sth );
     1263    my $cam_ref  = $db->selectall_arrayref( $cam_sth );
     1264    my $warp_ref = $db->selectall_arrayref( $warp_sth );
     1265
     1266    return($#{ $chip_ref } + 1, $#{ $cam_ref } + $#{ $warp_ref } + 2);
     1267}
     1268
     1269sub execute_diffs {
     1270    my $date = shift;
     1271    my $observing_state = shift;
     1272    my $pretend = shift;
     1273
     1274    foreach my $target (sort (keys %science_config)) {
     1275        if ($science_config{$target}{DIFFABLE} == 1) {
     1276            foreach my $filter (@filter_list) {
     1277                my ($NprocChips,$NprocWarps) = pre_diff_queue($date,$observing_state,$target,$filter);
     1278                if ((!defined($force_diff_count))&&($NprocChips != $NprocWarps)) {
     1279                    if ($debug == 1) {
     1280                        print STDERR "execute_diffs: Target $target on $date is not fully processed. ($NprocChips $NprocWarps)\n";
     1281                    }
     1282                    $metadata_out{nsDiffState} = 'FORCETOWARP';
     1283                    next;
     1284                }
     1285                if ($NprocChips == 0) {
     1286                    if ($debug == 1) {
     1287                        print STDERR "execute_diffs: Target $target in filter $filter on $date has no exposures.\n";
     1288                    }
     1289                    next;
     1290                }
     1291                unless (($metadata_out{nsDiffState} eq 'FORCETOWARP')||($metadata_out{nsDiffState} eq 'DIFFING')) {
     1292                    $metadata_out{nsDiffState} = 'QUEUEDIFFS';
     1293                }
     1294                if ($debug == 1) {
     1295                    print STDERR "execute_diffs: Target $target in filter $filter on $date has exposures and will be queued.\n";
     1296                }
     1297                diff_queue($date,$target,$filter,$pretend);
     1298
     1299                if (defined($pretend)) {
     1300                    add_to_macro_list('check_diffs',$science_config{$target}{DIFFABLE},$date,$target,$filter);
     1301                }
     1302                else {
     1303                    add_to_macro_list('queue_diffs',$science_config{$target}{DIFFABLE},$date,$target,$filter);
     1304                }
     1305            }
     1306        }
     1307        else {
     1308            if ($debug == 1) {
     1309                print STDERR "execute_diffs: Target $target is not auto-diffable.\n";
     1310            }
     1311        }
     1312    }
     1313}
     1314
     1315sub diff_queue {
     1316    my $date = shift;
     1317    my $target = shift;
     1318    my $filter = shift;
     1319    my $pretend = shift;
     1320    my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
     1321
     1322    my $db = init_gpc_db();
     1323
     1324    my $obj_sth = "select DISTINCT rawExp.object from warpRun JOIN fakeRun USING(fake_id) JOIN camRun USING(cam_id) JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id) ";
     1325    $obj_sth .= " WHERE warpRun.state = 'full' AND warpRun.label = '$label' AND warpRun.data_group = '$data_group' AND rawExp.filter = '$filter' ORDER BY rawExp.object";
     1326
     1327    my $object_ref = $db->selectall_arrayref( $obj_sth );
     1328   
     1329    foreach my $object_row (@{ $object_ref }) {
     1330        my $this_object = shift @{ $object_row };
     1331        my $input_sth = "select exp_id,warp_id,dateobs from warpRun JOIN fakeRun USING(fake_id) JOIN camRun USING(cam_id) JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id) ";
     1332        $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' ";
     1333        $input_sth .= " ORDER BY dateobs ";
     1334       
     1335        my $warps = $db->selectall_arrayref( $input_sth );
     1336       
     1337        if (($#{ $warps } + 1) % 2 != 0) {
     1338            print STDERR "diff_queue: Number of input warps to make diffs is not even for target $target and object $this_object! $#{ $warps }\n";
     1339            next;
     1340        }
     1341       
     1342        while ($#{ $warps } > -1) {
     1343            my $input_warp = shift @{ $warps };
     1344            my $template_warp = shift @{ $warps };
     1345            my $input_exp_id = ${ $input_warp }[0];
     1346            my $template_exp_id = ${ $template_warp }[0];
    12511347           
    1252 #           if (($#{ $warps } + 1) % 2 != 0) {
    1253 #               print STDERR "Number of input warps to make OSS diffs is not even! $#{ $warps }\n";
    1254 #               last;
    1255 #           }
     1348            my $input_warp_id = ${ $input_warp }[1];
     1349            my $template_warp_id = ${ $template_warp }[1];
    12561350           
    1257 #           while ($#{ $warps } > -1) {
    1258 #               my $input_warp = shift @{ $warps };
    1259 #               my $template_warp = shift @{ $warps };
    1260 #               my $input_exp_id = ${ $input_warp }[0];
    1261 #               my $template_exp_id = ${ $template_warp }[0];
    1262                
    1263 #               my $input_warp_id = ${ $input_warp }[1];
    1264 #               my $template_warp_id = ${ $template_warp }[1];
    1265 
    1266 #               if (verify_uniqueness_diff($input_warp_id,$template_warp_id,$date,$target) != 0) {
    1267 #                   print STDERR "Diffs already queued for this $date $target $input_exp_id $template_exp_id\n";
    1268 #                   next;
    1269 #               }
    1270 
    1271 #               my $cmd = "$difftool -dbname $dbname  -definewarpwarp ";
    1272 #               $cmd .= "-input_label $label  -template_label $label ";
    1273 #               $cmd .= "-backwards "; # Needed because difftool assumes a different date sorting.
    1274 #               $cmd .= "-set_workdir $workdir  -set_dist_group $dist_group  -set_data_group $data_group ";
    1275 #               $cmd .= " -simple  -set_label $label -exp_id $input_exp_id -template_exp_id $template_exp_id ";
    1276 # #             $cmd .= " -pretend ";
    1277 #               if (defined($pretend)) {
    1278 #                   $cmd .= ' -pretend ';
    1279 #               }
    1280 #               if ($debug == 1) {
    1281 #                   $cmd .= ' -pretend ';
    1282 #               }
    1283 #               print STDERR "EXTRA_PROCESSING: $cmd\n";
    1284 #               if (($debug == 0)&&(!defined($pretend))) {
    1285 #                   my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1286 #                       run ( command => $cmd, verbose => $verbose );
    1287 #                   unless ($success) {
    1288 #                       $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1289 #                       &my_die("Unable to perform difftool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
    1290 #                   }
    1291 #               }
    1292 #           }
    1293 #       }
    1294 #     }
    1295 # }
    1296 
     1351            if (verify_uniqueness_diff($input_warp_id,$template_warp_id,$date,$target) != 0) {
     1352                print STDERR "Diffs already queued for this $date $target $input_exp_id $template_exp_id\n";
     1353                next;
     1354            }
     1355           
     1356            my $cmd = "$difftool -dbname $dbname  -definewarpwarp ";
     1357            $cmd .= "-input_label $label  -template_label $label ";
     1358            $cmd .= "-backwards "; # Needed because difftool assumes a different date sorting.
     1359            $cmd .= "-set_workdir $workdir  -set_dist_group $dist_group  -set_data_group $data_group ";
     1360            $cmd .= " -simple  -set_label $label -exp_id $input_exp_id -template_exp_id $template_exp_id ";
     1361#               $cmd .= " -pretend ";
     1362            if (defined($pretend)) {
     1363                $cmd .= ' -pretend ';
     1364            }
     1365            if ($debug == 1) {
     1366                $cmd .= ' -pretend ';
     1367                print STDERR "diff_queue: $cmd\n";
     1368            }
     1369           
     1370            if (($debug == 0)&&(!defined($pretend))) {
     1371                my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1372                    run ( command => $cmd, verbose => $verbose );
     1373                unless ($success) {
     1374                    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1375                    &my_die("Unable to perform difftool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
     1376                }
     1377            }
     1378        }
     1379    }
     1380}
    12971381           
    12981382
     
    13021386################################################################################
    13031387
    1304 # sub construct_cleantool_args {
    1305 #     my $date = shift;
    1306 #     my $target = shift;
    1307 #     my $mode = shift;
    1308 
    1309 #     my $command = $clean_commands{$mode};
    1310 #     my $retention_time;
    1311 #     if (exists($cleanmods_list{$target}{$mode})) {
    1312 #       $retention_time = $cleanmods_list{$target}{$mode};
    1313 #     }
    1314 #     else {
    1315 #       $retention_time = $clean_retention{$mode};
    1316 #     }
    1317 #     if ($retention_time <= 0) {
    1318 #       return("no clean","true");
    1319 #     }
    1320 
    1321 #     my ($year,$month,$day) = split /-/,$date;
    1322 #     my $dt = DateTime->new(year => $year, month => $month, day => $day,
    1323 #                                hour => 0, minute => 0, second => 0, nanosecond => 0,
    1324 #                                time_zone => 'Pacific/Honolulu');
     1388sub construct_cleantool_args {
     1389    my $date = shift;
     1390    my $target = shift;
     1391    my $mode = shift;
     1392
     1393    my $command = $clean_config{$mode}{COMMAND};
     1394    my $retention_time;
     1395    if (exists($science_config{$target}{$mode})) {
     1396        $retention_time = $science_config{$target}{$mode};
     1397    }
     1398    else {
     1399        $retention_time = $clean_config{$mode}{RETENTION_TIME};
     1400    }
     1401    if ($retention_time <= 0) {
     1402        return("no clean","true");
     1403    }
     1404
     1405    my ($year,$month,$day) = split /-/,$date;
     1406    my $dt = DateTime->new(year => $year, month => $month, day => $day,
     1407                           hour => 0, minute => 0, second => 0, nanosecond => 0,
     1408                           time_zone => 'Pacific/Honolulu');
    13251409       
    1326 #     $dt->subtract(days => $retention_time);
    1327 #     my $cleaning_date = $dt->ymd;
    1328 
    1329 #     my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($cleaning_date,$target);
    1330    
    1331 #     $command =~ s/\@DBNAME\@/$dbname/;
    1332 #     $command =~ s/\@LABEL\@/$label/;
    1333 #     $command =~ s/\@WORKDIR\@/$workdir/;
    1334 #     $command =~ s/\@OBS_MODE\@/$obs_mode/;
    1335 #     $command =~ s/\@OBJECT\@/$object/;
    1336 #     $command =~ s/\@TESS_ID\@/$tess_id/;
    1337 #     $command =~ s/\@DIST_GROUP\@/$dist_group/;
    1338 #     $command =~ s/\@DATA_GROUP\@/$data_group/;
    1339 #     $command =~ s/\@REDUCTION\@/$reduction/;
    1340 #     $command =~ s/\@CURRENT_DATE\@/$cleaning_date/;
    1341    
    1342 #     if ($debug == 1) {
    1343 #         $command .= ' -pretend ';
    1344 #     }
    1345 #     return($cleaning_date,$command);
    1346 # }
    1347 
    1348 # sub execute_cleans {
    1349 #     my $date = shift;
    1350 #     my $pretend = shift;
    1351 
    1352 #     foreach my $mode (sort (keys %clean_config)) {
    1353 #       foreach my $target (sort (keys %science_config)) {
    1354 #           if (exists($noclean_list{$target})) {
    1355 #               next;
    1356 #           }
    1357 #           my ($cleaning_date,$command) = construct_cleantool_args($date,$target,$mode);
    1358 #           if ($cleaning_date eq 'no clean') {
    1359 #               next;
    1360 #           }           
    1361 #           print STDERR "$command\n";
    1362 #           if (!(defined($pretend) || $debug == 1)) {
    1363 # #           print STDERR "BEAR IS DRIVING!?\n";
    1364 #               my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1365 #                   run ( command => $command, verbose => $verbose );
    1366 #               unless ($success) {
    1367 #                   $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1368 #                   &my_die("Unable to perform cleantool ($command): $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
    1369 #               }
    1370 #               add_to_macro_list('clean_old',1,$date,$target,$mode);
    1371 #           }
    1372 #       }
    1373 #     }
    1374 #     return(0);
    1375 # }
     1410    $dt->subtract(days => $retention_time);
     1411    my $cleaning_date = $dt->ymd;
     1412
     1413    my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($cleaning_date,$target);
     1414   
     1415    $command =~ s/\@DBNAME\@/$dbname/;
     1416    $command =~ s/\@LABEL\@/$label/;
     1417    $command =~ s/\@WORKDIR\@/$workdir/;
     1418    $command =~ s/\@OBSMODE\@/$obs_mode/;
     1419    $command =~ s/\@OBJECT\@/$object/;
     1420    $command =~ s/\@TESS_ID\@/$tess_id/;
     1421    $command =~ s/\@DIST_GROUP\@/$dist_group/;
     1422    $command =~ s/\@DATA_GROUP\@/$data_group/;
     1423    $command =~ s/\@REDUCTION\@/$reduction/;
     1424    $command =~ s/\@CURRENT_DATE\@/$cleaning_date/;
     1425   
     1426    if ($debug == 1) {
     1427        $command .= ' -pretend ';
     1428    }
     1429    return($cleaning_date,$command);
     1430}
     1431
     1432sub execute_cleans {
     1433    my $date = shift;
     1434    my $pretend = shift;
     1435
     1436    foreach my $mode (sort (keys %clean_config)) {
     1437        foreach my $target (sort (keys %science_config)) {
     1438            if (exists($science_config{$target}{NOCLEAN})) {
     1439                next;
     1440            }
     1441            my ($cleaning_date,$command) = construct_cleantool_args($date,$target,$mode);
     1442            if ($cleaning_date eq 'no clean') {
     1443                next;
     1444            }           
     1445            print STDERR "$command\n";
     1446            if (!(defined($pretend) || $debug == 1)) {
     1447#           print STDERR "BEAR IS DRIVING!?\n";
     1448                my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1449                    run ( command => $command, verbose => $verbose );
     1450                unless ($success) {
     1451                    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1452                    &my_die("Unable to perform cleantool ($command): $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
     1453                }
     1454                add_to_macro_list('clean_old',1,$date,$target,$mode);
     1455            }
     1456        }
     1457    }
     1458    return(0);
     1459}
    13761460
    13771461#
     
    14261510    my $label      = "${target}.nightlyscience";
    14271511    my $workdir    = "neb://\@HOST\@.0/${dbname}/${target}.nt/${workdir_date}";
    1428     my $obs_mode   = $science_config{$target}{OBS_MODE};
     1512    my $obs_mode   = $science_config{$target}{OBSMODE};
    14291513    my $object     = $science_config{$target}{OBJECT};
    14301514    my $comment    = $science_config{$target}{COMMENT};
     
    14421526    my $trunc_date = $date; $trunc_date =~ s/-//g;
    14431527
    1444     my $exp_type   = $detrend_config{$target}{EXP_TYPE};
    1445     my $det_type   = $detrend_config{$target}{DET_TYPE};
    1446     my $ref_det_id = $detrend_config{$target}{REF_DET_ID};
     1528    my $exp_type   = $detrend_config{$target}{EXPTYPE};
     1529    my $det_type   = $detrend_config{$target}{DETTYPE};
     1530    my $ref_det_id = $detrend_config{$target}{REF_ID};
    14471531    my $ref_iter   = $detrend_config{$target}{REF_ITER};
    14481532    my $maxN       = $detrend_config{$target}{MAX_EXP};
     
    14551539    else {
    14561540        $internal_filter = '';
    1457     }   
     1541    }
     1542    unless (defined($maxN)) {
     1543        $maxN = 0;
     1544    }
    14581545    my $lc_type = lc($exp_type);
    14591546    my $label = "${lc_type}${internal_filter}.$trunc_date";
     
    14841571            $metadata_out{"ns${N}Macro"} =~ s/\@LABEL\@/$label/;
    14851572            $metadata_out{"ns${N}Macro"} =~ s/\@WORKDIR\@/$workdir/;
    1486             $metadata_out{"ns${N}Macro"} =~ s/\@OBS_MODE\@/$obs_mode/;
     1573            $metadata_out{"ns${N}Macro"} =~ s/\@OBSMODE\@/$obs_mode/;
    14871574            $metadata_out{"ns${N}Macro"} =~ s/\@OBJECT\@/$object/;
    14881575            $metadata_out{"ns${N}Macro"} =~ s/\@COMMENT\@/$comment/;
Note: See TracChangeset for help on using the changeset viewer.