IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 14009


Ignore:
Timestamp:
Jul 4, 2007, 1:53:22 PM (19 years ago)
Author:
eugene
Message:

extensive changes to use new stats calculation tools; general cleanups

Location:
trunk/ippScripts/scripts
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippScripts/scripts/chip_imfile.pl

    r13989 r14009  
    11#!/usr/bin/env perl
    22
     3use Carp;
    34use warnings;
    45use strict;
    5 use Carp;
    66
    77## report the program and machine
     
    1111print "Starting script $0 on $host\n\n";
    1212
     13use vars qw( $VERSION );
     14$VERSION = '0.01';
     15
    1316use IPC::Cmd 0.36 qw( can_run run );
    1417use PS::IPP::Metadata::Config;
    1518use PS::IPP::Metadata::Stats;
    16 use Data::Dumper;
     19
    1720use PS::IPP::Config qw($PS_EXIT_SUCCESS
    1821                       $PS_EXIT_UNKNOWN_ERROR
     
    3033
    3134# Parse the command-line arguments
    32 my ($exp_id,                    # Exposure identifier
    33     $chip_id,                   # Chiptool identifier
    34     $class_id,                  # Class identifier
    35     $input,                     # Input FITS file
    36     $camera,                    # Camera
    37     $dbname,                    # Database name
    38     $workdir,                   # Working directory, for output files
    39     $reduction,                 # Reduction class
    40     $no_update,                 # Don't update the database?
    41     $no_op,                     # Don't do any operations?
    42     );
     35my ($exp_id, $chip_id, $class_id, $input_uri, $camera, $dbname, $workdir, $reduction, $no_update, $no_op);
    4336GetOptions(
    44            'exp_id=s'      => \$exp_id,
    45            'chip_id=s'     => \$chip_id,
    46            'class_id=s'    => \$class_id,
    47            'uri|u=s'       => \$input,
    48            'camera|c=s'    => \$camera,
    49            'dbname|d=s'    => \$dbname, # Database name
    50            'workdir|w=s'   => \$workdir,
    51            'reduction=s'   => \$reduction,
    52            'no-update'     => \$no_update,
    53            'no-op'         => \$no_op,
     37           'exp_id=s'      => \$exp_id,    # Exposure identifier                       
     38           'chip_id=s'     => \$chip_id,   # Chiptool identifier                       
     39           'class_id=s'    => \$class_id,  # Class identifier                 
     40           'input_uri|u=s' => \$input_uri, # Input FITS file                   
     41           'camera|c=s'    => \$camera,    # Camera                           
     42           'dbname|d=s'    => \$dbname,    # Database name
     43           'workdir|w=s'   => \$workdir,   # Working directory, for output files 
     44           'reduction=s'   => \$reduction, # Reduction class                   
     45           'no-update'     => \$no_update, # Don't update the database?       
     46           'no-op'         => \$no_op,     # Don't do any operations?             
    5447           ) or pod2usage( 2 );
    5548
    5649pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
    57 pod2usage(
    58     -msg => "Required options: --exp_id --chip_id --class_id --uri --camera",
    59     -exitval => 3,
    60 ) unless defined $exp_id
     50pod2usage( -msg => "Required options: --exp_id --chip_id --class_id --uri --camera",
     51           -exitval => 3)
     52    unless defined $exp_id
    6153    and defined $chip_id
    6254    and defined $class_id
    63     and defined $input
     55    and defined $input_uri
    6456    and defined $camera;
    6557
    6658$ipprc->define_camera($camera);
     59
     60# Recipes to use based on reduction class
    6761$reduction = 'DEFAULT' unless defined $reduction;
    6862my $recipe = $ipprc->reduction($reduction, 'CHIP'); # Recipe to use
     63
     64# values to extract from output metadata and the stats to calculate
     65my $STATS =
     66   [   
     67       #          PPSTATS KEYWORD         STATISTIC          CHIPTOOL FLAG
     68       { name => "ROBUST_MEDIAN",  type => "mean",  flag => "-bg" },
     69       { name => "ROBUST_MEDIAN",  type => "stdev", flag => "-bg_mean_stdev" },
     70       { name => "ROBUST_STDEV",   type => "rms",   flag => "-bg_stdev" },
     71       { name => "FRINGE_0",       type => "rms",   flag => "-fringe_0" },
     72       { name => "FRINGE_RESID_0", type => "rms",   flag => "-fringe_1" },
     73       { name => "FRINGE_ERR_0",   type => "rms",   flag => "-fringe_2" },
     74       { name => "OVER_VAL",       type => "mean",  flag => "-bias" },
     75       { name => "FWHM",           type => "mean",  flag => "-fwhm" },
     76       { name => "APMIFIT",        type => "mean",  flag => "-ap_resid" },
     77       { name => "DAPMIFIT",       type => "rms",   flag => "-ap_resid_stdev" },
     78       { name => "CERROR",         type => "rms",   flag => "-sigma_ra" },
     79       { name => "CERROR",         type => "rms",   flag => "-sigma_dec" },
     80       { name => "NSTARS",         type => "sum",   flag => "-n_stars" },
     81       { name => "NASTRO",         type => "sum",   flag => "-n_astrom" },
     82   ];
    6983
    7084# Look for programs we need
     
    7690    exit($PS_EXIT_CONFIG_ERROR);
    7791}
    78 $ppImage .= " -dbname $dbname" if defined $dbname;
    79 
    80 &my_die("Couldn't find input file: $input\n", $chip_id, $class_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($input);
     92
     93&my_die("Couldn't find input file: $input_uri\n", $chip_id, $class_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($input_uri);
    8194
    8295$workdir = caturi( $workdir, $exp_id ) if defined $workdir;
    8396
    84 my $outputRoot = $ipprc->file_prepare( "$exp_id.chp$chip_id", $workdir, $input );
    85 my $outputImage = $ipprc->filename("PPIMAGE.CHIP", $outputRoot, $class_id);
    86 my $outputMask = $ipprc->filename("PPIMAGE.CHIP.MASK", $outputRoot, $class_id);
    87 my $outputBin1 = $ipprc->filename("PPIMAGE.BIN1", $outputRoot, $class_id);
    88 my $outputBin2 = $ipprc->filename("PPIMAGE.BIN2", $outputRoot, $class_id);
    89 my $outputStats = $outputRoot  . '.' . $class_id . '.stats';
    90 
    91 my $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
     97my $outputRoot  = $ipprc->file_prepare( "$exp_id.chp$chip_id", $workdir, $input_uri );
     98
     99my $outputImage = $ipprc->filename("PPIMAGE.CHIP",      $outputRoot, $class_id);
     100my $outputMask  = $ipprc->filename("PPIMAGE.CHIP.MASK", $outputRoot, $class_id);
     101my $outputBin1  = $ipprc->filename("PPIMAGE.BIN1",      $outputRoot, $class_id);
     102my $outputBin2  = $ipprc->filename("PPIMAGE.BIN2",      $outputRoot, $class_id);
     103my $outputStats = $ipprc->filename("PPIMAGE.STATS",     $outputRoot, $class_id);
     104
     105# Run ppImage
    92106unless ($no_op) {
    93     # Run ppImage
    94     print "outputImage: $outputImage\n";
    95     print "outputBin1: $outputBin1\n";
    96     print "outputStats: $outputStats\n";
    97 
    98     my $command = "$ppImage -file $input $outputRoot";
     107    my $command = "$ppImage -file $input_uri $outputRoot";
    99108    $command .= " -recipe PPIMAGE $recipe";
    100109    $command .= " -recipe PPSTATS CHIPSTATS";
    101     $command .= " -stats $outputStats"; # Command to run ppImage
     110    $command .= " -stats $outputStats";
     111    $command .= " -dbname $dbname" if defined $dbname;
    102112
    103113    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     
    119129    my @contents = <$statsFile>; # Contents of file
    120130    close $statsFile;
    121     my $mdcParser = PS::IPP::Metadata::Config->new;     # Parser for metadata config files
    122     my $metadata = $mdcParser->parse(join "", @contents) or
     131
     132    # parse the statistics MDC file
     133    my $mdcParser = PS::IPP::Metadata::Config->new();   # Parser for metadata config files
     134    my $metadata = $mdcParser->parse(join "", @contents);
     135    unless ($metadata) {
    123136        &my_die("Unable to parse metadata config doc", $chip_id, $class_id, $PS_EXIT_PROG_ERROR);
    124     $stats->parse($metadata) or
     137    }
     138
     139    # extract the stats from the metadata
     140    my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser
     141    unless ($stats->parse($metadata)) {
    125142        &my_die("Unable to find all values in statistics output.\n", $chip_id, $class_id, $PS_EXIT_PROG_ERROR);
     143    }
     144}
     145
     146# command to update database
     147my $command = "$chiptool -addprocessedimfile";
     148$command .= " -chip_id $chip_id";
     149$command .= " -class_id $class_id";
     150$command .= " -uri $outputImage";
     151$command .= " -path_base $outputRoot";
     152$command .= " -dbname $dbname" if defined $dbname;
     153
     154# add in the elements from the selected stats above
     155foreach my $entry (@$STATS) {
     156    my $value = $entry->{value};
     157    my $flag = $entry->{flag};
     158    $command .= " $flag $value";
    126159}
    127160
    128161# Add the processed file to the database
    129 my $bg            = $stats->bg_mean();
    130 my $bg_stdev      = $stats->bg_stdev();
    131 my $bg_mean_stdev = $stats->bg_mean_stdev();
    132 my $fringe_0      = ${$stats->fringe_mean()}[0];
    133 my $fringe_1      = ${$stats->fringe_err()}[0];
    134 my $fringe_2      = ${$stats->fringe_mean_stdev()}[0];
    135 my $dfringe_0     = ${$stats->dfringe_mean()}[0];
    136 my $dfringe_1     = ${$stats->dfringe_err()}[0];
    137 my $dfringe_2     = ${$stats->dfringe_mean_stdev()}[0];
    138 
    139162unless ($no_update) {
    140     # Command to run chiptool
    141     my $command = "$chiptool -addprocessedimfile";
    142     $command .= " -chip_id $chip_id";
    143     $command .= " -class_id $class_id";
    144     $command .= " -uri $outputImage";
    145     $command .= " -path_base $outputRoot";
    146     $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
    147     $command .= " -fringe_0 $fringe_0 -fringe_1 $fringe_1 -fringe_2 $fringe_2";
    148     $command .= " -user_1 $dfringe_0 -user_2 $dfringe_1 -user_3 $dfringe_2";
    149     # XXX add sigma_ra, etc here
    150     $command .= " -dbname $dbname" if defined $dbname;
    151 
    152163    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    153164        run(command => $command, verbose => 1);
     
    157168        exit($error_code);
    158169    }
    159 }
    160 
     170} else {
     171    print "skipping command: $command\n";
     172}
    161173
    162174sub my_die
  • trunk/ippScripts/scripts/detrend_norm_apply.pl

    r13989 r14009  
    1111print "Starting script $0 on $host\n\n";
    1212
     13use vars qw( $VERSION );
     14$VERSION = '0.01';
     15
     16use IPC::Cmd 0.36 qw( can_run run );
     17use PS::IPP::Metadata::Config;
    1318use PS::IPP::Metadata::Stats;
    14 use IPC::Cmd 0.36 qw( can_run run );
    15 use Data::Dumper;
    1619
    1720use PS::IPP::Config qw($PS_EXIT_SUCCESS
     
    3033
    3134# Parse the command-line
    32 my ($det_id,                     # Detrend ID
    33     $iter,                      # Iteration
    34     $class_id,                  # Class ID
    35     $value,                     # Value to multiple (for normalisation)
    36     $input,                     # Input file
    37     $camera,                    # Camera
    38     $det_type,                  # Detrend type
    39     $dbname,                    # Database name
    40     $workdir,                   # Working directory, for output files
    41     $no_update,                 # Don't update the database
    42     $no_op,                     # Don't do any operations
    43     );
     35my ($det_id, $iter, $class_id, $value, $input_uri, $camera, $det_type, $dbname, $workdir, $no_update, $no_op);
    4436GetOptions(
    45     'det_id|d=s'        => \$det_id,
    46     'iteration|n=s'     => \$iter,
    47     'class_id|i=s'      => \$class_id,
    48     'value|v=s'         => \$value,
    49     'input_uri|u=s'     => \$input,
    50     'camera|c=s'        => \$camera,
    51     'det_type|t=s'      => \$det_type,
    52     'dbname|d=s'        => \$dbname,
    53     'workdir|w=s'       => \$workdir, # Working directory, for output files
    54     'no-update'         => \$no_update,
    55     'no-op'             => \$no_op,
     37    'det_id|d=s'        => \$det_id,     # Detrend ID                           
     38    'iteration|n=s'     => \$iter,       # Iteration                           
     39    'class_id|i=s'      => \$class_id,   # Class ID                             
     40    'value|v=s'         => \$value,      # Value to multiple (for normalisation)       
     41    'input_uri|u=s'     => \$input_uri,  # Input file                           
     42    'camera|c=s'        => \$camera,     # Camera                               
     43    'det_type|t=s'      => \$det_type,   # Detrend type                         
     44    'dbname|d=s'        => \$dbname,     # Database name                               
     45    'workdir|w=s'       => \$workdir,    # Working directory, for output files 
     46    'no-update'         => \$no_update,  # Don't update the database           
     47    'no-op'             => \$no_op,      # Don't do any operations               
    5648    ) or pod2usage( 2 );
    5749   
    5850pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
    5951pod2usage( -msg => "Required options: --det_id --iteration --class_id --value --input_uri --camera --det_type",
    60     -exitval => 3,
    61     ) unless defined $det_id
     52           -exitval => 3)
     53    unless defined $det_id
    6254    and defined $iter
    6355    and defined $class_id
    6456    and defined $value
    65     and defined $input
     57    and defined $input_uri
    6658    and defined $camera
    6759    and defined $det_type;
     
    6961$ipprc->define_camera($camera);
    7062
    71 
    7263my $RECIPE_PPIMAGE = 'PPIMAGE_N'; # Recipe to use with ppImage
    7364my $RECIPE_PPSTATS = 'CHIPSTATS'; # Recipe to use with ppStats
     65
     66# values to extract from output metadata and the stats to calculate
     67my $STATS =
     68   [   
     69       #          PPSTATS KEYWORD         STATISTIC          CHIPTOOL FLAG
     70       { name => "ROBUST_MEDIAN",  type => "mean",  flag => "-bg" },
     71       { name => "ROBUST_MEDIAN",  type => "stdev", flag => "-bg_mean_stdev" },
     72       { name => "ROBUST_STDEV",   type => "rms",   flag => "-bg_stdev" },
     73   ];
    7474
    7575# Look for programs we need
     
    7777my $dettool = can_run('dettool') or (warn "Can't find dettool" and $missing_tools = 1);
    7878my $ppImage = can_run('ppImage') or (warn "Can't find ppImage" and $missing_tools = 1);
    79 
    8079if ($missing_tools) {
    8180    warn("Can't find required tools.");
     
    8382}
    8483
     84&my_die("Couldn't find input file: $input_uri\n", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($input_uri);
     85
    8586$workdir = caturi( $workdir, "$camera.$det_type.$det_id" ) if defined $workdir;
    8687
    87 my $outputRoot = $ipprc->file_prepare( "$camera.$det_type.norm.$det_id.$iter", $workdir, $input );
    88 my $output = $ipprc->filename("PPIMAGE.OUTPUT", $outputRoot, $class_id);
    89 my $b1name = $ipprc->filename("PPIMAGE.BIN1", $outputRoot, $class_id);
    90 my $b2name = $ipprc->filename("PPIMAGE.BIN2", $outputRoot, $class_id);
    91 my $statsName = $outputRoot . '.' . $class_id . '.stats'; # Statistics file
     88my $outputRoot = $ipprc->file_prepare( "$camera.$det_type.norm.$det_id.$iter", $workdir, $input_uri );
     89
     90my $output    = $ipprc->filename("PPIMAGE.OUTPUT", $outputRoot, $class_id);
     91my $b1name    = $ipprc->filename("PPIMAGE.BIN1",   $outputRoot, $class_id);
     92my $b2name    = $ipprc->filename("PPIMAGE.BIN2",   $outputRoot, $class_id);
     93my $statsName = $ipprc->filename("PPIMAGE.STATS",  $outputRoot, $class_id);
    9294
    9395# Run normalisation
    94 my $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
    9596unless ($no_op) {
    96     my $command = "$ppImage -file $input $outputRoot";
     97    my $command = "$ppImage -file $input_uri $outputRoot";
    9798    $command .= " -norm $value -stats $statsName";
    9899    $command .= " -recipe PPIMAGE $RECIPE_PPIMAGE";
     
    117118    my @contents = <$statsFile>; # Contents of file
    118119    close $statsFile;
     120
     121    # parse the statistics MDC file
    119122    my $mdcParser = PS::IPP::Metadata::Config->new;     # Parser for metadata config files
    120     my $metadata = $mdcParser->parse(join "", @contents)
    121         or &my_die("Unable to parse metadata config", $det_id, $iter, $class_id, $PS_EXIT_PROG_ERROR);
    122     $stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $det_id, $iter, $class_id, $PS_EXIT_PROG_ERROR);
     123    my $metadata = $mdcParser->parse(join "", @contents);
     124    unless ($metadata) {
     125        &my_die("Unable to parse metadata config", $det_id, $iter, $class_id, $PS_EXIT_PROG_ERROR);
     126    }
     127
     128    # extract the stats from the metadata
     129    my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser
     130    unless ($stats->parse($metadata)) {
     131        &my_die("Unable to find all values in statistics output.", $det_id, $iter, $class_id, $PS_EXIT_PROG_ERROR);
     132    }
    123133}
    124134
    125 # Update the database
    126 my $bg            = $stats->bg_mean();
    127 my $bg_stdev      = $stats->bg_stdev();
    128 my $bg_mean_stdev = $stats->bg_mean_stdev();
     135# Command to update the database
     136my $command = "$dettool -addnormalizedimfile";
     137$command .= " -det_id $det_id";
     138$command .= " -iteration $iter";
     139$command .= " -class_id $class_id";
     140$command .= " -uri $output";
     141$command .= " -path_base $outputRoot";
     142$command .= " -dbname $dbname" if defined $dbname;
    129143
     144# add in the elements from the selected stats above
     145foreach my $entry (@$STATS) {
     146    my $value = $entry->{value};
     147    my $flag = $entry->{flag};
     148    $command .= " $flag $value";
     149}
     150
     151# Add the processed file to the database
    130152unless ($no_update) {
    131     my $command = "$dettool -addnormalizedimfile -det_id $det_id -iteration $iter -class_id $class_id ".
    132         "-uri $output -path_base $outputRoot"; # Command to run
    133     $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
    134     $command .= " -dbname $dbname" if defined $dbname;
    135 
    136153    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    137154        run(command => $command, verbose => 1);
     
    141158        exit($error_code);
    142159    }
     160} else {
     161    print "skipping command: $command\n";
    143162}
    144163
     
    153172    carp($msg);
    154173    if ($det_id and $iter and $class_id and not $no_update) {
    155         my $command = "$dettool -addnormalizedimfile -det_id $det_id -iteration $iter -class_id $class_id -code $exit_code";
     174        my $command = "$dettool -addnormalizedimfile";
     175        $command .= " -det_id $det_id";
     176        $command .= " -iteration $iter";
     177        $command .= " -class_id $class_id";
     178        $command .= " -code $exit_code";
    156179        $command .= " -dbname $dbname" if defined $dbname;
    157180###        system ($command);
  • trunk/ippScripts/scripts/detrend_norm_calc.pl

    r13732 r14009  
    1111print "Starting script $0 on $host\n\n";
    1212
     13use vars qw( $VERSION );
     14$VERSION = '0.01';
     15
    1316use IPC::Cmd 0.36 qw( can_run );
    14 use IPC::Run qw ( run );
     17use IPC::Run 0.36 qw( run );
    1518use PS::IPP::Metadata::Config;
    1619use PS::IPP::Metadata::List qw( parse_md_list );
    17 use Data::Dumper;
    18 
    19 use PS::IPP::Config qw(
    20     $PS_EXIT_SUCCESS
    21     $PS_EXIT_UNKNOWN_ERROR
    22     $PS_EXIT_SYS_ERROR
    23     $PS_EXIT_CONFIG_ERROR
    24     $PS_EXIT_PROG_ERROR
    25     $PS_EXIT_DATA_ERROR
    26     $PS_EXIT_TIMEOUT_ERROR
    27     );
     20
     21use PS::IPP::Config qw($PS_EXIT_SUCCESS
     22                       $PS_EXIT_UNKNOWN_ERROR
     23                       $PS_EXIT_SYS_ERROR
     24                       $PS_EXIT_CONFIG_ERROR
     25                       $PS_EXIT_PROG_ERROR
     26                       $PS_EXIT_DATA_ERROR
     27                       $PS_EXIT_TIMEOUT_ERROR
     28                       );
    2829
    2930use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
    3031use Pod::Usage qw( pod2usage );
    3132
    32 
    3333# Parse command-line arguments
    34 my ($det_id,                    # Detrend id
    35     $iter,                      # Iteration
    36     $detType,                   # Detrend type
    37     $workdir,                   # Working directory for output files
    38     $dbname,                    # Database name
    39     $no_update,                 # Don't update the database?
    40     $no_op,                     # Don't do operations
    41     );
     34my ($det_id, $iter, $detType, $workdir, $dbname, $no_update, $no_op);
    4235GetOptions(
    43         'det_id|d=s'    => \$det_id,
    44         'iteration|i=s' => \$iter,
    45         'det_type|t=s'  => \$detType,
    46         'workdir|w=s'   => \$workdir,
    47         'dbname|d=s'    => \$dbname,# Database name
    48         'no-update'     => \$no_update,
    49         'no-op'         => \$no_op,
     36        'det_id|d=s'    => \$det_id,    # Detrend id                         
     37        'iteration|i=s' => \$iter,      # Iteration                         
     38        'det_type|t=s'  => \$detType,   # Detrend type                       
     39        'workdir|w=s'   => \$workdir,   # Working directory for output files
     40        'dbname|d=s'    => \$dbname,    # Database name                     
     41        'no-update'     => \$no_update, # Don't update the database?         
     42        'no-op'         => \$no_op,     # Don't do operations               
    5043        ) or pod2usage( 2 );
    5144
     
    5346pod2usage( -msg => "Required options --det_id --iteration --det_type",
    5447           -exitval => 3,
    55            ) unless defined $det_id
     48           )
     49    unless defined $det_id
    5650    and defined $iter
    5751    and defined $detType;
    5852
    59 
    6053use constant STATISTIC => 'bg'; # Background statistic to use from the database
     54
    6155# Define which detrend types we normalise
    6256use constant NORMALIZE => {
     
    7064    };
    7165
    72 
    7366# Look for programs we need
    7467my $missing_tools;
     
    8073}
    8174
    82 &my_die("Unrecognised detrend type: $detType", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless
    83     exists NORMALIZE()->{lc($detType)};
     75&my_die("Unrecognised detrend type: $detType", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless exists NORMALIZE()->{lc($detType)};
    8476
    8577my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
     
    172164}
    173165
     166my $commandBase = "$dettool -addnormalizedstat";
     167$commandBase .= " -det_id $det_id";
     168$commandBase .= " -iteration $iter";
     169$commandBase .= " -dbname $dbname" if defined $dbname;
    174170
    175171# Process output normalisations
    176172unless ($no_update) {
    177173    foreach my $normItem (@$norms) {
     174
    178175        my $className = $normItem->{name}; # Name of component
    179176        my $normalisation = $normItem->{value}; # Normalisation for component
    180177
    181         my $command = "$dettool -addnormalizedstat -det_id $det_id -iteration $iter -class_id $className ".
    182             "-norm $normalisation"; # Command to run
    183         $command .= " -dbname $dbname" if defined $dbname;
     178        my $command = $commandBase;
     179        $command .= " -class_id $className";
     180        $command .= " -norm $normalisation";
     181
    184182        my @command = split /\s+/, $command;
    185183
     
    192190        print $stdout . "\n";
    193191    }
     192} else {
     193    print "skipping command: $commandBase\n";
    194194}
    195195
     
    203203    carp($msg);
    204204    if ($det_id and $iter and not $no_update) {
    205         my $command = "$dettool -addnormalizedstat -det_id $det_id -iteration $iter -code $exit_code";
     205        my $command = "$dettool -addnormalizedstat";
     206        $command .= " -det_id $det_id";
     207        $command .= " -iteration $iter";
     208        $command .= " -code $exit_code";
    206209        $command .= " -dbname $dbname" if defined $dbname;
    207210###        system ($command);
  • trunk/ippScripts/scripts/detrend_norm_exp.pl

    r13760 r14009  
    1717use PS::IPP::Metadata::Config;
    1818use PS::IPP::Metadata::List qw( parse_md_list );
    19 use Statistics::Descriptive;
    2019use File::Temp qw( tempfile );
    2120
     
    4746
    4847pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
    49 pod2usage(
    50     -msg => "Required options: --det_id --iteration --camera --det_type",
    51     -exitval => 3,
    52 ) unless defined $det_id
    53      and defined $iter
    54      and defined $camera
    55      and defined $det_type;
     48pod2usage( -msg => "Required options: --det_id --iteration --camera --det_type",
     49           -exitval => 3)
     50    unless defined $det_id
     51    and defined $iter
     52    and defined $camera
     53    and defined $det_type;
    5654
    5755$ipprc->define_camera($camera);
    5856
    59 # Recipes to use, as a function of the detrend type
    60 use constant RECIPES => {
    61     'bin1' => {         # We're creating a master --- already processed the input
    62         'bias'     => 'PPIMAGE_J1_IMAGE_B',     # Bias only
    63         'dark'     => 'PPIMAGE_J1_IMAGE_B',     # Dark only
    64         'shutter'  => 'PPIMAGE_J1_IMAGE_F',     # Shutter only
    65         'flat'     => 'PPIMAGE_J1_IMAGE_F',     # Flat-field only
    66         'domeflat' => 'PPIMAGE_J1_IMAGE_F',     # Flat-field only
    67         'skyflat'  => 'PPIMAGE_J1_IMAGE_F',     # Flat-field only
    68         'fringe'   => 'PPIMAGE_J1_IMAGE_R',     # Fringe only
    69     },
    70     'bin2' => {         # We're checking the master --- input is not already processed
    71         'bias'     => 'PPIMAGE_J2_IMAGE_B',     # Bias only
    72         'dark'     => 'PPIMAGE_J2_IMAGE_B',     # Dark only
    73         'shutter'  => 'PPIMAGE_J2_IMAGE_F',     # Shutter only
    74         'flat'     => 'PPIMAGE_J2_IMAGE_F',     # Flat-field only
    75         'domeflat' => 'PPIMAGE_J2_IMAGE_F',     # Flat-field only
    76         'skyflat'  => 'PPIMAGE_J2_IMAGE_F',     # Flat-field only
    77         'fringe'   => 'PPIMAGE_J2_IMAGE_R',     # Fringe only
    78     },
    79 };
     57# Recipes to use based on reduction class
     58$reduction = 'DETREND' unless defined $reduction;
     59
     60my $recipe1 = $ipprc->reduction($reduction, 'JPEG_BIN1_IMAGE' . uc($det_type); # Recipe to use
     61&my_die("Unrecognised detrend type: $det_type", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless defined $recipe1;
     62
     63my $recipe2 = $ipprc->reduction($reduction, 'JPEG_BIN2_IMAGE' . uc($det_type); # Recipe to use
     64&my_die("Unrecognised detrend type: $det_type", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless defined $recipe2;
     65
     66# values to extract from output metadata and the stats to calculate
     67# XXX -bg_mean_stdev should take rms of bg_mean_stdev if bg_mean_stdev != 0 (A)
     68# XXX -bg_mean_stdev should take stdev of bg_mean if bg_mean_stdev == 0     (B)
     69# XXX  (A) if imfile.Ncomp > 1, (B) if imfile.Ncomp == 1
     70my $STATS =
     71   [   
     72       #          KEYWORD                 STATISTIC          CHIPTOOL FLAG
     73       { name => "bg",             type => "mean",  flag => "-bg" },
     74       { name => "bg",             type => "stdev", flag => "-bg_mean_stdev" },
     75       { name => "bg_stdev",       type => "rms",   flag => "-bg_stdev" },
     76       # { name => "bg_mean_stdev",  type => "rms",   flag => "-bg_mean_stdev" },
     77   ];
    8078
    8179# Look for programs we need
     
    8785    exit($PS_EXIT_CONFIG_ERROR);
    8886}
    89 
    90 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
    9187
    9288# Get list of component files
     
    10197        &my_die("Unable to perform ppImage: $error_code", $det_id, $iter, $error_code);
    10298    }
     99
     100    # convert stdout to a metadata
     101    my $mdcParser = PS::IPP::Metadata::Config->new;     # Parser for metadata config files
    103102    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
    104103        &my_die("Unable to parse metadata config doc", $det_id, $iter, $PS_EXIT_PROG_ERROR);
     104
     105    # parse the file info in the metadata
    105106    $files = parse_md_list($metadata) or
    106107        &my_die("Unable to parse metadata list", $det_id, $iter, $PS_EXIT_PROG_ERROR);
    107 }
    108 
    109 # Gather the statistics
    110 my ($bg, $bg_stdev, $bg_mean_stdev); # The statistics triplet
    111 {
    112     my @backgrounds;            # Array of backgrounds in each component
    113     my @variances;    # Array of variances for each component
    114     foreach my $file (@$files) {
    115         &my_die("Unable to find class id", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $file->{class_id};
    116         my $class_id = $file->{class_id};
    117         &my_die("Unable to find bg for class_id=$class_id", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $file->{bg};
    118         &my_die("Unable to find bg_mean_stdev for class_id=$class_id", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $file->{bg_mean_stdev};
    119         push @backgrounds, $file->{bg};
    120         push @variances, $file->{bg_stdev}**2;
    121     }
    122 
    123     {
    124         my $stats = Statistics::Descriptive::Sparse->new; # Statistics calculator
    125         $stats->add_data(@backgrounds);
    126         $bg = ($stats->mean() or 'NAN');
    127         $bg_mean_stdev = ($stats->standard_deviation() or 'NAN');
    128     }
    129     {
    130         my $stats = Statistics::Descriptive::Sparse->new; # Statistics calculator
    131         $stats->add_data(@variances);
    132         $bg_stdev = (sqrt( $stats->mean() ) or 'NAN');
     108
     109    # parse the stats in the metadata
     110    my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser
     111    unless ($stats->parse($metadata)) {
     112        &my_die("Unable to find all values in statistics output.\n", $det_id, $iter, $PS_EXIT_PROG_ERROR);
    133113    }
    134114}
     
    136116my ($list1File, $list1Name) = tempfile( "$camera.$det_type.norm.$det_id.$iter.b1.list.XXXX", UNLINK => 1 );
    137117my ($list2File, $list2Name) = tempfile( "$camera.$det_type.norm.$det_id.$iter.b2.list.XXXX", UNLINK => 1 );
    138 my @means;                      # Array of means
    139 my @stdevs;                     # Array of stdevs
    140118foreach my $file (@$files) {
    141119    print $list1File ( $ipprc->filename( "PPIMAGE.BIN1", $file->{path_base}, $file->{class_id} ) . "\n");
    142120    print $list2File ( $ipprc->filename( "PPIMAGE.BIN2", $file->{path_base}, $file->{class_id} ) . "\n");
    143     push @means, $file->{bg};
    144     push @stdevs, $file->{bg_stdev};
    145121}
    146122close $list1File;
     
    152128my $jpeg1Name = $ipprc->filename("PPIMAGE.JPEG1", $outputRoot); # Binned JPEG #1
    153129my $jpeg2Name = $ipprc->filename("PPIMAGE.JPEG2", $outputRoot); # Binned JPEG #2
    154 
    155 # Recipes to use in processing
    156 my $recipe1 = RECIPES->{"bin1"}->{lc($det_type)};
    157 &my_die("Unrecognised detrend type: $det_type", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless defined $recipe1;
    158 
    159 my $recipe2 = RECIPES->{"bin2"}->{lc($det_type)};
    160 &my_die("Unrecognised detrend type: $det_type", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless defined $recipe2;
    161130
    162131unless ($no_op) {
     
    178147}
    179148
     149# command to update the database
     150my $command = "$dettool -addnormalizedexp";
     151$command .= " -det_id $det_id";
     152$command .= " -iteration $iter";
     153$command .= " -recip $recipe1,$recipe2";
     154$command .= " -path_base $outputRoot ";
     155$command .= " -dbname $dbname" if defined $dbname;
     156
     157# add in the elements from the selected stats above
     158foreach my $entry (@$STATS) {
     159    my $value = $entry->{value};
     160    my $flag = $entry->{flag};
     161    $command .= " $flag $value";
     162}
     163
     164# Add the processed file to the database
    180165unless ($no_update) {
    181     my $command = "$dettool -addnormalizedexp -det_id $det_id -iteration $iter";
    182     $command .= " -recip $recipe1,$recipe2 -path_base $outputRoot ";
    183     $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
    184     $command .= " -dbname $dbname" if defined $dbname;
    185166    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    186167        run(command => $command, verbose => 1);
     
    189170        &my_die("Unable to perform dettool -addnormalizedexp: $error_code", $det_id, $iter, $error_code);
    190171    }
    191 }
    192 
     172} else {
     173    print "skipping command: $command\n";
     174}
    193175
    194176sub my_die
     
    201183    carp($msg);
    202184    if ($det_id and $iter and not $no_update) {
    203         my $command = "$dettool -addprocessedimfile -det_id $det_id -iter $iter -code $exit_code";
     185        my $command = "$dettool -addprocessedimfile";
     186        $command .= " -det_id $det_id";
     187        $command .= " -iter $iter";
     188        $command .= " -code $exit_code";
    204189        $command .= " -dbname $dbname" if defined $dbname;
    205190###        system ($command);
  • trunk/ippScripts/scripts/detrend_process_exp.pl

    r13760 r14009  
    1717use PS::IPP::Metadata::Config;
    1818use PS::IPP::Metadata::List qw( parse_md_list );
    19 use Statistics::Descriptive;
     19use File::Temp qw( tempfile );
    2020
    2121use PS::IPP::Config qw($PS_EXIT_SUCCESS
     
    2929                       );
    3030my $ipprc = PS::IPP::Config->new(); # IPP configuration
    31 use File::Temp qw( tempfile );
    3231
    3332use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
     
    4746
    4847pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
    49 pod2usage(
    50     -msg => "Required options: --det_id --det_type --exp_tag --camera",
    51     -exitval => 3,
    52 ) unless defined $det_id
     48pod2usage( -msg => "Required options: --det_id --det_type --exp_tag --camera",
     49           -exitval => 3)
     50    unless defined $det_id
    5351    and defined $det_type
    5452    and defined $exp_tag
     
    5755$ipprc->define_camera($camera);
    5856
    59 # Recipes to use, as a function of the detrend type
    60 use constant RECIPES => {
    61     'bin1' => {         # We're creating a master --- already processed the input
    62         'bias'     => 'PPIMAGE_J1_IMAGE_B',     # Bias only
    63         'dark'     => 'PPIMAGE_J1_IMAGE_B',     # Dark only
    64         'shutter'  => 'PPIMAGE_J1_IMAGE_F',     # Shutter only
    65         'flat'     => 'PPIMAGE_J1_IMAGE_F',     # Flat-field only
    66         'domeflat' => 'PPIMAGE_J1_IMAGE_F',     # Flat-field only
    67         'skyflat'  => 'PPIMAGE_J1_IMAGE_F',     # Flat-field only
    68         'fringe'   => 'PPIMAGE_J1_IMAGE_R',     # Fringe only
    69     },
    70     'bin2' => {         # We're checking the master --- input is not already processed
    71         'bias'     => 'PPIMAGE_J2_IMAGE_B',     # Bias only
    72         'dark'     => 'PPIMAGE_J2_IMAGE_B',     # Dark only
    73         'shutter'  => 'PPIMAGE_J2_IMAGE_F',     # Shutter only
    74         'flat'     => 'PPIMAGE_J2_IMAGE_F',     # Flat-field only
    75         'domeflat' => 'PPIMAGE_J2_IMAGE_F',     # Flat-field only
    76         'skyflat'  => 'PPIMAGE_J2_IMAGE_F',     # Flat-field only
    77         'fringe'   => 'PPIMAGE_J2_IMAGE_R',     # Fringe only
    78     },
    79 };
     57# Recipes to use based on reduction class
     58$reduction = 'DETREND' unless defined $reduction;
     59
     60my $recipe1 = $ipprc->reduction($reduction, 'JPEG_BIN1_IMAGE' . uc($det_type); # Recipe to use
     61&my_die("Unrecognised detrend type: $det_type", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless defined $recipe1;
     62
     63my $recipe2 = $ipprc->reduction($reduction, 'JPEG_BIN2_IMAGE' . uc($det_type); # Recipe to use
     64&my_die("Unrecognised detrend type: $det_type", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless defined $recipe2;
     65
     66# values to extract from output metadata and the stats to calculate
     67# XXX -bg_mean_stdev should take rms of bg_mean_stdev if bg_mean_stdev != 0 (A)
     68# XXX -bg_mean_stdev should take stdev of bg_mean if bg_mean_stdev == 0     (B)
     69# XXX  (A) if imfile.Ncomp > 1, (B) if imfile.Ncomp == 1
     70my $STATS =
     71   [   
     72       #          KEYWORD                 STATISTIC          CHIPTOOL FLAG
     73       { name => "bg",             type => "mean",  flag => "-bg" },
     74       { name => "bg",             type => "stdev", flag => "-bg_mean_stdev" },
     75       { name => "bg_stdev",       type => "rms",   flag => "-bg_stdev" },
     76       # { name => "bg_mean_stdev",  type => "rms",   flag => "-bg_mean_stdev" },
     77   ];
    8078
    8179# Look for programs we need
     
    8785    exit($PS_EXIT_CONFIG_ERROR);
    8886}
    89 
    90 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
    9187
    9288# Get list of component files
     
    10197        &my_die("Unable to perform dettool -processedimfile: $error_code", $det_id, $exp_tag, $error_code);
    10298    }
     99
     100    # convert stdout to a metadata
     101    my $mdcParser = PS::IPP::Metadata::Config->new;     # Parser for metadata config files
    103102    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
    104103        &my_die("Unable to parse metadata config doc", $det_id, $exp_tag, $PS_EXIT_PROG_ERROR);
     104
     105    # parse the file info in the metadata
    105106    $files = parse_md_list($metadata) or
    106107        &my_die("Unable to parse metadata list", $det_id, $exp_tag, $PS_EXIT_PROG_ERROR);
    107 }
    108 
    109 # Gather the statistics
    110 my ($bg, $bg_stdev, $bg_mean_stdev); # The statistics triplet
    111 {
    112     my @backgrounds;            # Array of backgrounds in each component
    113     my @variances;    # Array of variances for each component
    114     foreach my $file (@$files) {
    115         &my_die("Unable to find class id", $det_id, $exp_tag, $PS_EXIT_SYS_ERROR) unless defined $file->{class_id};
    116         my $class_id = $file->{class_id};
    117         &my_die("Unable to find bg for class_id=$class_id", $det_id, $exp_tag, $PS_EXIT_SYS_ERROR) unless defined $file->{bg};
    118         &my_die("Unable to find bg_mean_stdev for class_id=$class_id", $det_id, $exp_tag, $PS_EXIT_SYS_ERROR) unless defined $file->{bg_mean_stdev};
    119         push @backgrounds, $file->{bg};
    120         push @variances, $file->{bg_stdev}**2;
    121     }
    122 
    123     {
    124         my $stats = Statistics::Descriptive::Sparse->new; # Statistics calculator
    125         $stats->add_data(@backgrounds);
    126         $bg = ($stats->mean() or 'NAN');
    127         $bg_mean_stdev = ($stats->standard_deviation() or 'NAN');
    128     }
    129     {
    130         my $stats = Statistics::Descriptive::Sparse->new; # Statistics calculator
    131         $stats->add_data(@variances);
    132         $bg_stdev = (sqrt( $stats->mean() ) or 'NAN');
     108
     109    # parse the stats in the metadata
     110    my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser
     111    unless ($stats->parse($metadata)) {
     112        &my_die("Unable to find all values in statistics output.\n", $chip_id, $class_id, $PS_EXIT_PROG_ERROR);
    133113    }
    134114}
     
    137117my ($list1File, $list1Name) = tempfile( "$exp_tag.detproc.$det_id.b1.list.XXXX", UNLINK => 1 );
    138118my ($list2File, $list2Name) = tempfile( "$exp_tag.detproc.$det_id.b2.list.XXXX", UNLINK => 1 );
    139 my @means;                      # Array of means
    140 my @stdevs;                     # Array of stdevs
    141119foreach my $file (@$files) {
    142120    print $list1File ($ipprc->filename( "PPIMAGE.BIN1", $file->{path_base}, $file->{class_id} ) . "\n");
    143121    print $list2File ($ipprc->filename( "PPIMAGE.BIN2", $file->{path_base}, $file->{class_id} ) . "\n");
    144     push @means, $file->{bg};
    145     push @stdevs, $file->{bg_stdev};
    146122}
    147123close $list1File;
     
    153129my $jpeg1 = $ipprc->filename("PPIMAGE.JPEG1", $outputRoot); # Binned JPEG #1
    154130my $jpeg2 = $ipprc->filename("PPIMAGE.JPEG2", $outputRoot); # Binned JPEG #2
    155 
    156 # Recipes to use in processing
    157 my $recipe1 = RECIPES->{"bin1"}->{lc($det_type)};
    158 &my_die("Unrecognised detrend type: $det_type", $det_id, $exp_tag, $PS_EXIT_PROG_ERROR) unless defined $recipe1;
    159 
    160 my $recipe2 = RECIPES->{"bin2"}->{lc($det_type)};
    161 &my_die("Unrecognised detrend type: $det_type", $det_id, $exp_tag, $PS_EXIT_PROG_ERROR) unless defined $recipe2;
    162131
    163132unless ($no_op) {
     
    187156}
    188157
     158# Command to update the database
     159my $command = "$dettool -addprocessedexp";
     160$command .= " -det_id $det_id";
     161$command .= " -exp_tag $exp_tag";
     162$command .= " -recip $recipe1,$recipe2 -path_base $outputRoot";
     163$command .= " -dbname $dbname" if defined $dbname;
     164
     165# add in the elements from the selected stats above
     166foreach my $entry (@$STATS) {
     167    my $value = $entry->{value};
     168    my $flag = $entry->{flag};
     169    $command .= " $flag $value";
     170}
     171
     172# Add the processed file to the database
    189173unless ($no_update) {
    190     my $command = "$dettool -addprocessedexp -det_id $det_id -exp_tag $exp_tag";
    191     $command .= " -recip $recipe1,$recipe2 -path_base $outputRoot";
    192     $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
    193     $command .= " -dbname $dbname" if defined $dbname;
    194174    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    195175        run(command => $command, verbose => 1);
     
    199179        exit($error_code);
    200180    }
    201 }
    202 
     181} else {
     182    print "skipping command: $command\n";
     183}
    203184
    204185sub my_die
     
    211192    carp($msg);
    212193    if ($det_id and $exp_tag and not $no_update) {
    213         my $command = "$dettool -addprocessedexp -det_id $det_id -exp_tag $exp_tag -code $exit_code";
     194        my $command = "$dettool -addprocessedexp";
     195        $command .= " -det_id $det_id";
     196        $command .= " -exp_tag $exp_tag";
     197        $command .= " -code $exit_code";
    214198        $command .= " -dbname $dbname" if defined $dbname;
    215199###        system ($command);
  • trunk/ippScripts/scripts/detrend_process_imfile.pl

    r13989 r14009  
    4848
    4949pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
    50 pod2usage(
    51     -msg => "Required options: --det_id --exp_tag --class_id --det_type --input_uri --camera",
    52     -exitval => 3,
    53 ) unless defined $det_id
     50pod2usage( -msg => "Required options: --det_id --exp_tag --class_id --det_type --input_uri --camera",
     51           -exitval => 3)
     52    unless defined $det_id
    5453    and defined $exp_tag
    5554    and defined $class_id
     
    6160$ipprc->define_camera($camera);
    6261
     62# Recipes to use as a function of detrend type
    6363$reduction = "DETREND" unless defined $reduction;
    6464my $recipe = $ipprc->reduction($reduction, uc($det_type) . '_PROCESS'); # Recipe name to use
     65
     66# values to extract from output metadata and the stats to calculate
     67my $STATS =
     68   [   
     69       #          PPSTATS KEYWORD         STATISTIC          CHIPTOOL FLAG
     70       { name => "ROBUST_MEDIAN",  type => "mean",  flag => "-bg" },
     71       { name => "ROBUST_MEDIAN",  type => "stdev", flag => "-bg_mean_stdev" },
     72       { name => "ROBUST_STDEV",   type => "rms",   flag => "-bg_stdev" },
     73   ];
    6574
    6675# Look for programs we need
     
    7483$ppImage .= " -dbname $dbname" if defined $dbname;
    7584
     85&my_die("Couldn't find input file: $input_uri\n", $chip_id, $class_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($input_uri);
     86
    7687$workdir = caturi( $workdir, "$camera.$det_type.$det_id" ) if defined $workdir;
    7788
    78 my $outputRoot = $ipprc->file_prepare( "$exp_tag/$exp_tag.detproc.$det_id", $workdir, $input_uri );
     89my $outputRoot  = $ipprc->file_prepare( "$exp_tag/$exp_tag.detproc.$det_id", $workdir, $input_uri );
     90
    7991my $outputImage = $ipprc->filename("PPIMAGE.OUTPUT", $outputRoot, $class_id);
    80 my $outputBin1 = $ipprc->filename("PPIMAGE.BIN1", $outputRoot, $class_id);
    81 my $outputBin2 = $ipprc->filename("PPIMAGE.BIN2", $outputRoot, $class_id);
    82 my $outputStats = $outputRoot . '.' . $class_id . '.stats';
     92my $outputBin1  = $ipprc->filename("PPIMAGE.BIN1",  $outputRoot, $class_id);
     93my $outputBin2  = $ipprc->filename("PPIMAGE.BIN2",  $outputRoot, $class_id);
     94my $outputStats = $ipprc->filename("PPIMAGE.STATS",  $outputRoot, $class_id);
    8395
    8496# Run ppImage
    85 my $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
    8697unless ($no_op) {
    8798    my $command = "$ppImage -file $input_uri $outputRoot";
     
    96107        &my_die("Unable to perform ppImage: $error_code", $det_id, $exp_tag, $class_id, $error_code);
    97108    }
     109
    98110    &my_die("Couldn't find expected output file: $outputImage", $det_id, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($outputImage);
    99111    &my_die("Couldn't find expected output file: $outputStats", $det_id, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($outputStats);
     
    106118    my @contents = <$statsFile>; # Contents of file
    107119    close $statsFile;
     120
     121    # parse the statistics MDC file
    108122    my $mdcParser = PS::IPP::Metadata::Config->new;     # Parser for metadata config files
    109123    my $metadata = $mdcParser->parse(join "", @contents)
    110124        or &my_die("Unable to parse metadata config", $det_id, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
     125
     126    # extract the stats from the metadata
     127    my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser
    111128    $stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $det_id, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
    112129}
    113130
    114 my $bg            = $stats->bg_mean();
    115 my $bg_stdev      = $stats->bg_stdev();
    116 my $bg_mean_stdev = $stats->bg_mean_stdev();
     131# command to update database
     132my $command = "$dettool -addprocessedimfile";
     133$command .= " -det_id $det_id";
     134$command .= " -exp_tag $exp_tag";
     135$command .= " -class_id $class_id";
     136$command .= " -recip $reduction";
     137$command .= " -uri $outputImage -path_base $outputRoot";
     138$command .= " -dbname $dbname" if defined $dbname;
     139
     140# add in the elements from the selected stats above
     141foreach my $entry (@$STATS) {
     142    my $value = $entry->{value};
     143    my $flag = $entry->{flag};
     144    $command .= " $flag $value";
     145}
    117146
    118147# Add the processed file to the database
    119148unless ($no_update) {
    120     my $command = "$dettool -addprocessedimfile";
    121     $command .= " -det_id $det_id";
    122     $command .= " -exp_tag $exp_tag";
    123     $command .= " -class_id $class_id";
    124     $command .= " -recip $recipe";
    125     $command .= " -uri $outputImage -path_base $outputRoot";
    126     $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
    127     $command .= " -dbname $dbname" if defined $dbname;
    128 
    129149    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    130150        run(command => $command, verbose => 1);
     
    134154        exit($error_code);
    135155    }
     156} else {
     157    print "skipping command: $command\n";
    136158}
    137159
     
    146168    carp($msg);
    147169    if ($det_id and $exp_tag and $class_id and not $no_update) {
    148         my $command = "$dettool -addprocessedimfile -det_id $det_id -exp_tag $exp_tag -class_id $class_id -code $exit_code";
     170        my $command = "$dettool -addprocessedimfile";
     171        $command .= " -det_id $det_id";
     172        $command .= " -exp_tag $exp_tag";
     173        $command .= " -class_id $class_id";
     174        $command .= " -code $exit_code";
    149175        $command .= " -dbname $dbname" if defined $dbname;
    150176###        system ($command);
  • trunk/ippScripts/scripts/detrend_reject_exp.pl

    r13962 r14009  
    4747
    4848pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
    49 pod2usage(
    50     -msg => "Required options: --det_id --iteration --det_type --camera",
    51     -exitval => 3,
    52 ) unless defined $det_id
     49pod2usage( -msg => "Required options: --det_id --iteration --det_type --camera",
     50           -exitval => 3)
     51    unless defined $det_id
    5352    and defined $iter
    5453    and defined $det_type
    5554    and defined $camera;
    5655
     56# values to extract from output metadata and the stats to calculate
     57# XXX -bg_mean_stdev should take rms of bg_mean_stdev if bg_mean_stdev != 0 (A)
     58# XXX -bg_mean_stdev should take stdev of bg_mean if bg_mean_stdev == 0     (B)
     59# XXX  (A) if imfile.Ncomp > 1, (B) if imfile.Ncomp == 1
     60my $STATS =
     61   [   
     62       #          KEYWORD                 STATISTIC          CHIPTOOL FLAG
     63       { name => "bg",             type => "mean",  flag => "-bg" },
     64       { name => "bg_mean_stdev",  type => "stdev", flag => "-bg_mean_stdev" },
     65       { name => "bg_stdev",       type => "rms",   flag => "-bg_stdev" },
     66   ];
     67
     68my $REJSTATS =
     69   [   
     70       #          KEYWORD                 STATISTIC          CHIPTOOL FLAG
     71       { name => "bg",             type => "mean",   flag => "ensMeanMean" },
     72       { name => "bg",             type => "stdev",  flag => "ensMeanStdev" },
     73       { name => "bg_mean_stdev",  type => "mean",   flag => "ensMeanStdevMean" },
     74       { name => "bg_mean_stdev",  type => "stdev",  flag => "ensMeanStdevStdev" },
     75       { name => "bg_stdev",       type => "mean",   flag => "ensStdevMean" },
     76       { name => "bg_stdev",       type => "stdev",  flag => "ensStdevStdev" },
     77   ];
    5778
    5879# Look for programs we need
     
    6485}
    6586
    66 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
    67 
    6887# Get list of component files
    6988my $exposures;                  # Array of exposures
    7089{
    71     my $command = "$dettool -residexp -det_id $det_id -iteration $iter"; # Command to run
     90    # dettool command to select exp data for this det_run
     91    my $command = "$dettool -residexp";
     92    $command .= " -det_id $det_id";
     93    $command .= " -iteration $iter";
    7294    $command .= " -dbname $dbname" if defined $dbname;
    7395    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     
    78100    }
    79101
     102    # Parse the stdout buffer into a metadata
     103    my $mdcParser = PS::IPP::Metadata::Config->new;     # Parser for metadata config files
    80104    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
    81105        &my_die("Unable to parse metadata config doc", $det_id, $iter, $PS_EXIT_PROG_ERROR);
    82     # XXX need to test that the result list is 0 length and return with a different error
     106
     107    # parse the file info in the metadata
    83108    $exposures = parse_md_list($metadata) or
    84109        &my_die("Unable to parse metadata list", $det_id, $iter, $PS_EXIT_PROG_ERROR);
     110
     111    # Parse the statistics on the residual image
     112    my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser
     113    $stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $det_id, $iter, $PS_EXIT_PROG_ERROR);
     114
     115    # Parse the statistics for rejections
     116    my $rejstats = PS::IPP::Metadata::Stats->new($REJSTATS); # Stats parser
     117    $rejstats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $det_id, $iter, $PS_EXIT_PROG_ERROR);
    85118}
    86119
    87120my @expTags;                    # Array of exposure IDs
    88 my @means;                      # Array of means
    89 my @stdevs;                     # Array of standard deviations
    90 my @meanStdevs;                 # Array of standard deviations of the mean (normalised by the mean)
    91 my @variances;                  # Array of variances
    92121my @accept;                     # Array of accept flags
    93122my @include;                    # Array of include flags
    94123foreach my $exposure (@$exposures) {
    95124    &my_die("Unable to find exposure id.\n", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $exposure->{exp_tag};
    96     &my_die("Unable to find mean.\n", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $exposure->{bg};
    97     &my_die("Unable to find stdev.\n", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $exposure->{bg_stdev};
    98     &my_die("Unable to find mean stdev.\n", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $exposure->{bg_mean_stdev};
    99125    &my_die("Unable to find accept.\n", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $exposure->{accept};
    100126    &my_die("Unable to find include.\n", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $exposure->{include};
    101     push @expTags, $exposure->{exp_tag};
    102     push @means, $exposure->{bg};
    103     push @stdevs, $exposure->{bg_stdev};
    104     # XXX why are we using mean_stdev / bd?
    105     # push @meanStdevs, $exposure->{bg_mean_stdev} / $exposure->{bg};
    106     push @meanStdevs, $exposure->{bg_mean_stdev};
    107     push @variances, $exposure->{bg_stdev}**2;
    108     push @accept, $exposure->{accept};
    109     push @include, $exposure->{include};
    110 }
    111 my $meanStats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
    112 $meanStats->add_data(@means);
    113 my $stdevStats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
    114 $stdevStats->add_data(@stdevs);
    115 my $meanStdevStats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
    116 $meanStdevStats->add_data(@meanStdevs);
     127}
     128
     129my $ensMeanMean       = &STATS_value_for_flag ($REJSTATS, "ensMeanMean");     
     130my $ensMeanStdev      = &STATS_value_for_flag ($REJSTATS, "ensMeanStdev");     
     131my $ensMeanStdevMean  = &STATS_value_for_flag ($REJSTATS, "ensMeanStdevMean");
     132my $ensMeanStdevStdev = &STATS_value_for_flag ($REJSTATS, "ensMeanStdevStdev");
     133my $ensStdevMean      = &STATS_value_for_flag ($REJSTATS, "ensStdevMean");     
     134my $ensStdevStdev     = &STATS_value_for_flag ($REJSTATS, "ensStdevStdev");   
    117135
    118136$ipprc->define_camera($camera);
    119137# Rejection thresholds
    120 my $reject_mean = rejection_limit( 'ENSEMBLE.MEAN', $det_type, $filter );
    121 my $reject_stdev = rejection_limit( 'ENSEMBLE.STDEV', $det_type, $filter );
     138my $reject_mean      = rejection_limit( 'ENSEMBLE.MEAN',      $det_type, $filter );
     139my $reject_stdev     = rejection_limit( 'ENSEMBLE.STDEV',    $det_type, $filter );
    122140my $reject_meanstdev = rejection_limit( 'ENSEMBLE.MEANSTDEV', $det_type, $filter );
    123141
     
    129147unless ($no_op) {
    130148    $logFile = $ipprc->file_create_append( $logName );
    131     print $logFile "Ensemble mean " . $meanStats->mean() . " +/- " . $meanStats->standard_deviation .
    132         ", stdev " . $stdevStats->mean() . " +/- " . $stdevStats->standard_deviation() . "\n\n";
     149    print $logFile "Ensemble mean $meanEnsemble +/- $stdevEnsemble, stdev $meanStdevEnsemble\n\n";
    133150}
    134151
     
    136153my $numChanges = 0;             # Number of exposures with changed status
    137154my $numReject = 0;              # Number of exposures rejected
    138 my $command;
    139 my $reject;
    140 my $expTag;
    141 for (my $i = 0; $i < scalar @means; $i++) {
    142     $expTag = $expTags[$i];     # Exposure ID
    143     $command = "$dettool -updateresidexp -det_id $det_id -iteration $iter -exp_tag $expTag"; # Command to run
     155
     156for (my $i = 0; $i < scalar @$exposures; $i++) {
     157    my $file      = $files[$i];
     158    my $mean      = $file->{bg};        # Mean for this exposure
     159    my $stdev     = $file->{bg_stdev}; # Stdev for this exposure
     160    my $meanStdev = $file->{bg_mean_stdev}; # Stdev of Means for this exposure
     161
     162    my $expTag  = $exposure->{exp_tag};
     163    my $accept  = $exposure->{accept};
     164    my $include = $exposure->{include};
     165
     166    my $reject = 0;             # Reject this exposure?
     167
     168    my $command = "$dettool -updateresidexp";
     169    $command .= " -det_id $det_id";
     170    $command .= " -iteration $iter";
     171    $command .= " -exp_tag $expTag";
    144172    $command .= " -dbname $dbname" if defined $dbname;
    145173   
    146     $reject = 0;                # Reject this exposure?
    147 
    148     if (not $accept[$i]) {
     174    if (not $accept) {
    149175        # Rejected this at an earlier stage
    150176        unless ($no_op) {
     
    165191    }
    166192   
    167     if ($reject_mean > 0 and defined $meanStats->standard_deviation() ) {
    168         my $dMean = abs($means[$i] - $meanStats->mean()) ;
    169         if ($dMean > ($reject_mean * $meanStats->standard_deviation())) {
     193    if ($reject_mean > 0 and $ensMeanStdev > 0) {
     194        my $delta = abs($mean - $ensMeanMean);
     195        if ($delta > ($reject_mean * $ensMeanStdev)) {
    170196            print $logFile "Rejecting $expTag based on ensemble mean value: ";
    171197            $reject = 1;
     
    174200            print $logFile "$expTag OK against ensemble mean: ";
    175201        }
    176         print $logFile "$means[$i] --> $dMean vs " . $reject_mean * $meanStats->standard_deviation() . "\n";
     202        print $logFile "$mean --> $delta vs " . $reject_mean * $ensMeanStdev . "\n";
    177203    } else {
    178204        print $logFile "No rejection of $expTag for ensemble mean\n";
    179205    }
    180206
    181     if ($reject_stdev > 0 and defined $stdevStats->standard_deviation() > 0) {
    182         my $dMean = abs($stdevs[$i] - $stdevStats->mean());
    183         if ($dMean > ($reject_stdev * $stdevStats->standard_deviation())) {
     207    if ($reject_stdev > 0 and $ensStdevStdev > 0) {
     208        my $delta = abs($stdev - $ensStdevMean);
     209        if ($delta > ($reject_stdev * $ensStdevStdev)) {
    184210            print $logFile "Rejecting $expTag based on ensemble stdev: ";
    185211            $reject = 1;
     
    188214            print $logFile "$expTag OK against ensemble stdev: ";
    189215        }
    190         print $logFile "$stdevs[$i] --> $dMean sigma vs " . $reject_stdev  * $stdevStats->standard_deviation() . "\n";
     216        print $logFile "$stdev --> $delta sigma vs " . $reject_stdev * $ensStdevStdev . "\n";
    191217    } else {
    192218        print $logFile "No rejection of $expTag for ensemble stdev\n";
    193219    }
    194220   
    195     if ($reject_meanstdev > 0 and defined $meanStdevStats->standard_deviation() > 0) {
    196         my $dMean = abs($meanStdevs[$i] - $meanStdevStats->mean());
    197         if ($dMean > ($reject_meanstdev * $meanStdevStats->standard_deviation())) {
     221    if ($reject_meanstdev > 0 and $ensMeanStdevStdev > 0) {
     222        my $delta = abs($meanStdev - $ensMeanStdevMean);
     223        if ($delta > ($reject_meanstdev * $ensMeanStdevStdev)) {
    198224            print $logFile "Rejecting $expTag based on ensemble mean stdev: ";
    199225            $reject = 1;
     
    202228            print $logFile "$expTag OK against ensemble mean stdev: ";
    203229        }
    204         print $logFile "$meanStdevs[$i] --> $dMean sigma vs " . $reject_meanstdev . "\n";
     230        print $logFile "$meanStdev --> $delta sigma vs " . $reject_meanstdev * $ensMeanStdevStdev. "\n";
    205231    } else {
    206232        print $logFile "No rejection of $expTag for ensemble mean stdev\n";
     
    214240   
    215241    # Check for status changes
    216     if ((not $include[$i] and not $reject) or ($include[$i] and $reject)) {
     242    if ((not $include and not $reject) or ($include and $reject)) {
    217243        unless ($no_op) {
    218244            print $logFile "Status of $expTag has changed.\n";
     
    253279}
    254280
     281my $command = "$dettool -adddetrunsummary";
     282$command .= " -det_id $det_id";
     283$command .= " -iteration $iter";
     284$command .= " -accept" if $master;
     285$command .= " -dbname $dbname" if defined $dbname;
     286
     287# add in the elements from the selected stats above
     288foreach my $entry (@$STATS) {
     289    my $value = $entry->{value};
     290    my $flag = $entry->{flag};
     291    $command .= " $flag $value";
     292}
     293
    255294# Put results into the database
    256295unless ($no_update) {
    257 
    258     {
    259         # Add the summary
    260         my $varianceStats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
    261         $varianceStats->add_data(@variances);
    262        
    263         my $bg = ($meanStats->mean() or 'NAN');
    264         my $bg_stdev = (sqrt( $varianceStats->mean() ) or 'NAN');
    265         my $bg_mean_stdev = ($meanStats->standard_deviation() or 'NAN');
    266        
    267         my $command = "$dettool -adddetrunsummary -det_id $det_id -iteration $iter";
    268         $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
    269         $command .= " -accept" if $master;
    270         $command .= " -dbname $dbname" if defined $dbname;
    271        
    272         my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    273             run(command => $command, verbose => 1);
    274         unless ($success) {
    275             $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    276             warn("Unable to perform dettool -adddetrunsummary: $error_code");
    277             exit($error_code);
    278         }
    279     }
    280 
    281     # Re-run processing if required
    282     {
    283         my $command = "$dettool -updatedetrun -det_id $det_id";
    284         if ($stop) {
    285             $command .= ' -state stop';
    286         } else {
    287             $command .= ' -again';
    288         }
    289        
    290         $command .= " -dbname $dbname" if defined $dbname;
    291        
    292         my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    293             run(command => $command, verbose => 1);
    294         unless ($success) {
    295             $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    296             warn("Unable to perform dettool -updatedetrun: $error_code");
    297             exit($error_code);
    298         }
    299     }
     296    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     297        run(command => $command, verbose => 1);
     298    unless ($success) {
     299        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     300        warn("Unable to perform dettool -adddetrunsummary: $error_code");
     301        exit($error_code);
     302    }
     303
     304    $command = "$dettool -updatedetrun -det_id $det_id";
     305    if ($stop) {
     306        $command .= ' -state stop';
     307    } else {
     308        $command .= ' -again';
     309    }
     310    $command .= " -dbname $dbname" if defined $dbname;
     311
     312    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     313        run(command => $command, verbose => 1);
     314    unless ($success) {
     315        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     316        warn("Unable to perform dettool -updatedetrun: $error_code");
     317        exit($error_code);
     318    }
     319} else {
     320    print "skipping command: $command\n";
    300321}
    301322
     
    317338    carp($msg);
    318339    if ($det_id and $iter and not $no_update) {
    319         my $command = "$dettool -adddetrunsummary -det_id $det_id -iteration $iter -code $exit_code";
     340        my $command = "$dettool -adddetrunsummary";
     341        $command .= " -det_id $det_id";
     342        $command .= " -iteration $iter";
     343        $command .= " -code $exit_code";
    320344        $command .= " -dbname $dbname" if defined $dbname;
    321345###        system ($command);
     
    323347    exit $exit_code;
    324348}
    325 
    326 
    327349
    328350# Retrieve the requested rejection limit, dying unless extant
     
    342364}
    343365
     366sub STATS_value_for_flag
     367{
     368    my $STATS = shift;
     369    my $flag  = shift;
     370
     371    foreach my $entry (@$STATS) {
     372        if ($flag eq $entry->{flag}) {
     373            return $entry->{value};
     374        }
     375    }
     376    return 'NAN';
     377}
    344378
    345379__END__
  • trunk/ippScripts/scripts/detrend_reject_imfile.pl

    r13989 r14009  
    2222use IPC::Cmd 0.36 qw( can_run run );             # tools to run UNIX programs with control over I/O
    2323use PS::IPP::Metadata::Config;                   # tools to parse the psMetadataConfig files
    24 use PS::IPP::Metadata::List qw( parse_md_list ); # ?
     24use PS::IPP::Metadata::List qw( parse_md_list ); # tools to parse a metadata into a hash list
    2525use Statistics::Descriptive;                     # tools for calculating basic statistical quantities
    2626use File::Temp qw( tempfile );                   # tools to construct temp files
     27
    2728use PS::IPP::Config qw($PS_EXIT_SUCCESS         
    2829                       $PS_EXIT_UNKNOWN_ERROR
     
    5657
    5758pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
    58 pod2usage(
    59           -msg => "Required options: --det_id --iteration --exp_tag --det_type --camera",
    60           -exitval => 3,
    61           ) unless defined $det_id
     59pod2usage( -msg => "Required options: --det_id --iteration --exp_tag --det_type --camera",
     60           -exitval => 3)
     61    unless defined $det_id
    6262    and defined $iter
    6363    and defined $exp_tag
     
    6868$ipprc->define_camera($camera);
    6969
    70 # Recipes to use, as a function of the detrend type
    71 use constant RECIPES => {
    72     'bin1' => {         # We're creating a master --- already processed the input
    73         'bias'     => 'PPIMAGE_J1_RESID_B',     # Bias only
    74         'dark'     => 'PPIMAGE_J1_RESID_B',     # Dark only
    75         'shutter'  => 'PPIMAGE_J1_RESID_F',     # Shutter only
    76         'flat'     => 'PPIMAGE_J1_RESID_F',     # Flat-field only
    77         'domeflat' => 'PPIMAGE_J1_RESID_F',     # Flat-field only
    78         'skyflat'  => 'PPIMAGE_J1_RESID_F',     # Flat-field only
    79         'fringe'   => 'PPIMAGE_J1_RESID_R',     # Fringe only
    80     },
    81     'bin2' => {         # We're checking the master --- input is not already processed
    82         'bias'     => 'PPIMAGE_J2_RESID_B',     # Bias only
    83         'dark'     => 'PPIMAGE_J2_RESID_B',     # Dark only
    84         'shutter'  => 'PPIMAGE_J2_RESID_F',     # Shutter only
    85         'flat'     => 'PPIMAGE_J2_RESID_F',     # Flat-field only
    86         'domeflat' => 'PPIMAGE_J2_RESID_F',     # Flat-field only
    87         'skyflat'  => 'PPIMAGE_J2_RESID_F',     # Flat-field only
    88         'fringe'   => 'PPIMAGE_J2_RESID_R',     # Fringe only
    89     },
    90 };
     70# Recipes to use based on reduction class
     71$reduction = 'DETREND' unless defined $reduction;
     72
     73my $recipe1 = $ipprc->reduction($reduction, 'JPEG_BIN1_RESID' . uc($det_type); # Recipe to use
     74&my_die("Unrecognised detrend type: $det_type", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless defined $recipe1;
     75
     76my $recipe2 = $ipprc->reduction($reduction, 'JPEG_BIN2_RESID' . uc($det_type); # Recipe to use
     77&my_die("Unrecognised detrend type: $det_type", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless defined $recipe2;
     78
     79# values to extract from output metadata and the stats to calculate
     80# XXX -bg_mean_stdev should take rms of bg_mean_stdev if bg_mean_stdev != 0 (A)
     81# XXX -bg_mean_stdev should take stdev of bg_mean if bg_mean_stdev == 0     (B)
     82# XXX  (A) if imfile.Ncomp > 1, (B) if imfile.Ncomp == 1
     83my $STATS =
     84   [   
     85       #          KEYWORD                 STATISTIC          CHIPTOOL FLAG
     86       { name => "bg",             type => "mean",  flag => "-bg" },
     87       { name => "bg_mean_stdev",  type => "stdev", flag => "-bg_mean_stdev" },
     88       { name => "bg_stdev",       type => "rms",   flag => "-bg_stdev" },
     89       { name => "bin_stdev",      type => "rms",   flag => "-bin_stdev" },
     90       { name => "fringe_0",       type => "mean",  flag => "-fringe_0" },
     91       { name => "fringe_1",       type => "rms",   flag => "-fringe_1" },
     92       { name => "fringe_0",       type => "stdev", flag => "-fringe_2" },
     93       { name => "user_1",         type => "mean",  flag => "-user_1" }, # fringe residual
     94       { name => "user_2",         type => "rms",   flag => "-user_2" }, # fringe residual
     95       { name => "user_3",         type => "stdev", flag => "-user_1" }, # fringe residual
     96   ];
    9197
    9298# Look for programs we need
     
    98104    exit($PS_EXIT_CONFIG_ERROR);
    99105}
    100 
    101 # Parser for psMetadataConfig files (used for output from dettool)
    102 my $mdcParser = PS::IPP::Metadata::Config->new;
    103106
    104107# Get list of imfile files
     
    120123    # XXX report an error message if stdout_buf is empty
    121124
     125    # Parse the stdout buffer into a metadata
     126    my $mdcParser = PS::IPP::Metadata::Config->new;     
    122127    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
    123128        &my_die("Unable to parse metadata config doc", $det_id, $iter, $exp_tag, $PS_EXIT_PROG_ERROR);
    124129
     130    # parse the file info in the metadata
    125131    $files = parse_md_list($metadata) or
    126132        &my_die("Unable to parse metadata list", $det_id, $iter, $exp_tag, $PS_EXIT_PROG_ERROR);
     133
     134    # Parse the statistics on the residual image
     135    my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser
     136    $stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
    127137}
    128138
     
    136146my $logFile;
    137147unless ($no_op) {
    138     # XXX this will fail if the file exists (because of Nebulous rules)
    139148    $logFile = $ipprc->file_create_append( $logName );
    140149}
    141 
    142 #### build the JPEG images ####
    143150
    144151# XXX in debug mode, unlink = 0
     
    152159close $list2File;
    153160
    154 # Recipes to use in processing
    155 my $recipe1 = RECIPES->{"bin1"}->{lc($det_type)};
    156 &my_die("Unrecognised detrend type: $det_type", $det_id, $iter, $exp_tag, $PS_EXIT_PROG_ERROR) unless defined $recipe1;
    157 
    158 my $recipe2 = RECIPES->{"bin2"}->{lc($det_type)};
    159 &my_die("Unrecognised detrend type: $det_type", $det_id, $iter, $exp_tag, $PS_EXIT_PROG_ERROR) unless defined $recipe2;
    160 
     161
     162# build the JPEG images
    161163unless ($no_op) {
    162164
     
    206208
    207209# storage variables
    208 my @means;                      # Array of means
    209 my @fluxes;                     # Array of means / exptimes
    210 my @variances;                  # Array of variances
    211 my @binVariances;               # Array of binned variances
    212 my @meanStdevs;                 # Array of mean stdevs
    213 my @names;                      # Array of names (class_id)
    214 my @fringe_means;
    215 my @fringe_vars;
    216 my @dfringe_means;
    217 my @dfringe_vars;
    218210
    219211# load the arrays from the imfile output table
    220212foreach my $file (@$files) {
    221     push @means,        $file->{bg};                           # mean background counts
    222213    if ($file->{exp_time} > 0.0) {
    223214        push @fluxes,           $file->{bg} / $file->{exp_time};       # mean background counts / sec
     
    225216        push @fluxes,           $file->{bg};
    226217    }
    227     push @meanStdevs,        $file->{bg_mean_stdev};                # stdev of the mean counts (for imfile components)
    228     push @variances,         $file->{bg_stdev}*$file->{bg_stdev};   # total variance from all imfile components
    229     push @binVariances,      $file->{bin_stdev}*$file->{bin_stdev}; # total variance of binned images over all imfile components
    230     push @names,             $file->{class_id};                     # name of the component
    231     push @fringe_means,      $file->{fringe_0};                     # fringe amplitude mean for imfile
    232     push @fringe_vars,       $file->{fringe_1}*$file->{fringe_1};   # fringe variance
    233     push @dfringe_means,     $file->{user_1};                       # fringe residual mean for imfile
    234     push @dfringe_vars,      $file->{user_2}*$file->{user_2};       # fringe residual variance
    235     # push @fringe_mean_stdev, $file->{fringe_2};                     # fringe amplitude stdev for imfile
    236218}
    237219
     
    239221# it is VALID to reject on more than one criterion
    240222&my_die("Number of means and number of variances differ!", $det_id, $iter, $exp_tag, $PS_EXIT_PROG_ERROR) unless scalar @means == scalar @variances;
    241 for (my $i = 0; $i < scalar @means; $i++) {
    242     my $flux = $fluxes[$i];     # Flux for this imfile
    243     my $mean = $means[$i];      # Mean for this imfile
     223
     224# storage array
     225my @fluxes;
     226
     227for (my $i = 0; $i < scalar @$files; $i++) {
     228    my $file      = $files[$i];
     229    my $name      = $file->{class_id};
     230    my $mean      = $file->{bg};        # Mean for this imfile
     231    my $stdev     = $file->{bg_stdev}; # Stdev for this imfile
     232    my $meanStdev = $file->{bg_mean_stdev}; # Stdev of Means for this imfile
     233    my $binStdev  = $file->{bin_stdev}; # Binned Stdev for this imfile
     234
     235    # calculate and save the fluxes
     236    my $flux      = $mean / $file->{exp_time};
     237    if ($file->{exp_time} == 0.0) {
     238        push @fluxes, $mean;
     239    }
     240    push @fluxes, $flux;
     241
    244242    $mean -= $expected;
    245     my $stdev = sqrt($variances[$i]);   # Stdev for this imfile
    246     my $binStdev = sqrt($binVariances[$i]);     # Stdev for this imfile
    247     my $name = $names[$i];
    248243
    249244    last if $no_op;
     
    301296    # component means is larger than the limit
    302297    if ($reject_imfile_meanstdev > 0) {
    303         if ($meanStdevs[$i] > $reject_imfile_meanstdev) {
     298        if ($meanStdevs > $reject_imfile_meanstdev) {
    304299            print $logFile "Rejecting exposure based on bad imfile mean stdev for $name: ";
    305300            $reject = 1;
     
    307302            print $logFile "Imfile mean stdev for $name meets requirements: ";
    308303        }
    309         print $logFile "$meanStdevs[$i] vs $reject_imfile_meanstdev\n";
     304        print $logFile "$meanStdevs vs $reject_imfile_meanstdev\n";
    310305    } else {
    311306        print $logFile "No rejection on imfile mean stdev for $name\n";
     
    356351}
    357352
    358 # calculate the exposure ensemble statistics
    359 my $meanStats = Statistics::Descriptive::Sparse->new(); # Statistics calculator for means
    360 $meanStats->add_data(@means);
     353# basic ensemble stats
     354my $mean               = &STATS_value_for_flag ($STATS, "-bg");
     355my $meanStdev          = &STATS_value_for_flag ($STATS, "-bg_mean_stdev");
     356my $stdev              = &STATS_value_for_flag ($STATS, "-bg_stdev");
     357my $binStdev           = &STATS_value_for_flag ($STATS, "-bin_stdev");
     358my $fringe_mean        = &STATS_value_for_flag ($STATS, "-fringe_0");
     359my $fringe_err         = &STATS_value_for_flag ($STATS, "-fringe_1");
     360my $fringe_mean_stdev  = &STATS_value_for_flag ($STATS, "-fringe_2");
     361my $dfringe_mean       = &STATS_value_for_flag ($STATS, "-user_1");
     362my $dfringe_err        = &STATS_value_for_flag ($STATS, "-user_2");
     363my $dfringe_mean_stdev = &STATS_value_for_flag ($STATS, "-user_3");
     364
     365# other stats (flux depends on bg and exp_time)
    361366my $fluxStats = Statistics::Descriptive::Sparse->new(); # Statistics calculator for means
    362367$fluxStats->add_data(@fluxes);
    363 my $varianceStats = Statistics::Descriptive::Sparse->new(); # Statistics calculator for variances
    364 $varianceStats->add_data(@variances);
    365 my $binVarianceStats = Statistics::Descriptive::Sparse->new(); # Statistics calculator for variances
    366 $binVarianceStats->add_data(@binVariances);
    367 
    368 # background stats
    369 my $mean = $meanStats->mean();  # Mean of the imfile means
    370368my $flux = $fluxStats->mean();  # Mean of the imfile means
    371 my $meanStdev = $meanStats->standard_deviation(); # Stdev of the imfile means
    372 if (not defined $meanStdev) {
    373     # this is the case for Nimfile == 1
    374     $meanStdev = 0;
    375 }
    376 my $stdev = sqrt($varianceStats->mean()); # Root-Mean-Square of the imfile stdevs (root mean of variances)
    377 my $binStdev = sqrt($binVarianceStats->mean()); # Root-Mean-Square of the imfile stdevs (root mean of variances)
     369
     370# other stats
    378371my $exp_sn = 0.0;
    379372if ($stdev > 0) { $exp_sn = $mean / $stdev; }
    380 
    381 # prepare fringe amplitude stats
    382 my $fringeMeanStats = Statistics::Descriptive::Sparse->new();   # Statistics calculator for means
    383 $fringeMeanStats->add_data(@fringe_means);
    384 my $fringeVarStats = Statistics::Descriptive::Sparse->new();    # Statistics calculator for means
    385 $fringeVarStats->add_data(@fringe_vars);
    386 
    387 # fringe amplitude stats
    388 my $fringe_mean = $fringeMeanStats->mean();
    389 my $fringe_err = sqrt($fringeVarStats->mean());
    390 my $fringe_mean_stdev = $fringeMeanStats->standard_deviation();
    391 if (not defined $fringe_mean_stdev) {
    392     $fringe_mean_stdev = 0;
    393 }
    394 
    395 # prepare fringe residual stast
    396 my $dfringeMeanStats = Statistics::Descriptive::Sparse->new();  # Statistics calculator for means
    397 $dfringeMeanStats->add_data(@dfringe_means);
    398 my $dfringeVarStats = Statistics::Descriptive::Sparse->new();   # Statistics calculator for means
    399 $dfringeVarStats->add_data(@dfringe_vars);
    400 
    401 # fringe amplitude stats
    402 my $dfringe_mean = $dfringeMeanStats->mean();
    403 my $dfringe_err = sqrt($dfringeVarStats->mean());
    404 my $dfringe_mean_stdev = $dfringeMeanStats->standard_deviation();
    405 if (not defined $dfringe_mean_stdev) {
    406     $dfringe_mean_stdev = 0;
    407 }
    408373
    409374## Reject based on the exposure ensemble stats
     
    514479}
    515480
    516 # Add the result into the database
    517 my $bg            = $mean;
    518 my $bg_stdev      = $stdev;
    519 my $bg_mean_stdev = $meanStdev;
    520 my $bin_stdev     = $binStdev;
     481my $command = "$dettool -addresidexp -det_id $det_id -iteration $iter -exp_tag $exp_tag";
     482$command .= " -recip $recipe1,$recipe2 -path_base $outputRoot ";
     483$command .= ' -reject' if $reject;
     484$command .= " -dbname $dbname" if defined $dbname;
     485
     486# add in the elements from the selected stats above
     487foreach my $entry (@$STATS) {
     488    my $value = $entry->{value};
     489    my $flag = $entry->{flag};
     490    $command .= " $flag $value";
     491}
    521492
    522493unless ($no_update) {
    523     my $command = "$dettool -addresidexp -det_id $det_id -iteration $iter -exp_tag $exp_tag";
    524     $command .= " -recip $recipe1,$recipe2 -path_base $outputRoot ";
    525     $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev -bin_stdev $bin_stdev";
    526     $command .= " -fringe_0 $fringe_mean -fringe_1 $fringe_err -fringe_2 $fringe_mean_stdev";
    527     $command .= " -user_1 $dfringe_mean -user_2 $dfringe_err -user_3 $dfringe_mean_stdev";
    528     $command .= ' -reject' if $reject;
    529     $command .= " -dbname $dbname" if defined $dbname;
    530494    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    531495        run(command => $command, verbose => 1);
     
    535499        exit($error_code);
    536500    }
     501} else {
     502    print "skipping command: $command\n";
    537503}
    538504
     
    555521    carp($msg);
    556522    if ($det_id and $iter and $exp_tag and not $no_update) {
    557         my $command = "$dettool -addresidexp -det_id $det_id -iteration $iter -exp_tag $exp_tag -code $exit_code";
     523        my $command = "$dettool -addresidexp";
     524        $command .= " -det_id $det_id";
     525        $command .= " -iteration $iter";
     526        $command .= " -exp_tag $exp_tag";
     527        $command .= " -code $exit_code";
    558528        $command .= " -dbname $dbname" if defined $dbname;
    559529###        system ($command);
     
    561531    exit $exit_code;
    562532}
    563 
    564533
    565534# Retrieve the requested rejection limit, dying if not extant
     
    579548}
    580549
    581 
    582 
     550sub STATS_value_for_flag
     551{
     552    my $STATS = shift;
     553    my $flag  = shift;
     554
     555    foreach my $entry (@$STATS) {
     556        if ($flag eq $entry->{flag}) {
     557            return $entry->{value};
     558        }
     559    }
     560    return 'NAN';
     561}
    583562
    584563__END__
  • trunk/ippScripts/scripts/detrend_resid.pl

    r13973 r14009  
    1717use PS::IPP::Metadata::Config;
    1818use PS::IPP::Metadata::Stats;
    19 use Data::Dumper;
    2019
    2120use PS::IPP::Config qw($PS_EXIT_SUCCESS
     
    3332use Pod::Usage qw( pod2usage );
    3433
    35 my ($det_id, $iter, $exp_tag, $class_id, $det_type, $detrend,
    36         $input_uri, $camera, $mode, $dbname, $workdir, $reduction, $no_update, $no_op);
     34my ($det_id, $iter, $exp_tag, $class_id, $det_type, $detrend, $input_uri, $camera, $mode, $dbname, $workdir, $reduction, $no_update, $no_op);
    3735GetOptions(
    3836    'det_id|d=s'        => \$det_id,
     
    5351
    5452pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
    55 pod2usage(
    56     -msg => "Required options: --det_id --iteration --exp_tag --class_id --det_type --camera --input_uri --mode --detrend (not for 'verify' mode)",
    57     -exitval => 3,
    58 ) unless defined $det_id
     53pod2usage( -msg => "Required options: --det_id --iteration --exp_tag --class_id --det_type --camera --input_uri --mode --detrend (not for 'verify' mode)",
     54           -exitval => 3)
     55    unless defined $det_id
    5956    and defined $iter
    6057    and defined $exp_tag
     
    6865$ipprc->define_camera($camera);
    6966
     67# Recipes to use as a function of detrend type and mode
    7068$reduction = 'DETREND' unless defined $reduction;
    7169my $recipe;                     # Name of recipe to use
     
    7876}
    7977$recipe = $ipprc->reduction($reduction, $recipe);
     78
     79# values to extract from output metadata and the stats to calculate
     80my $STATS =
     81   [   
     82       #          PPSTATS KEYWORD         STATISTIC          CHIPTOOL FLAG
     83       { name => "ROBUST_MEDIAN",      type => "mean",  flag => "-bg" },
     84       { name => "ROBUST_MEDIAN",      type => "stdev", flag => "-bg_mean_stdev" },
     85       { name => "ROBUST_STDEV",       type => "rms",   flag => "-bg_stdev" },
     86       { name => "FRINGE_0",           type => "mean",  flag => "-fringe_0" },
     87       { name => "FRINGE_ERR_0",       type => "rms",   flag => "-fringe_1" },
     88       { name => "FRINGE_0",           type => "stdev", flag => "-fringe_2" },
     89       { name => "FRINGE_RESID_0",     type => "mean",  flag => "-user_1" },
     90       { name => "FRINGE_RESID_ERR_0", type => "rms",   flag => "-user_2" },
     91       { name => "FRINGE_RESID_0",     type => "stdev", flag => "-user_3" },
     92   ];
     93my $BINNED_STATS =
     94   [   
     95       { name => "ROBUST_STDEV",   type => "rms",   flag => "-bin_stdev" },
     96   ];
    8097
    8198# Flags to specify the particular detrend to use
     
    106123
    107124my $outputRoot = $ipprc->file_prepare( "$exp_tag/$exp_tag.detresid.$det_id.$iter", $workdir, $input_uri );
    108 my $outputName = $ipprc->filename("PPIMAGE.OUTPUT", $outputRoot, $class_id);
    109 my $bin1Name =  $ipprc->filename("PPIMAGE.BIN1", $outputRoot, $class_id);
    110 my $bin2Name =  $ipprc->filename("PPIMAGE.BIN2", $outputRoot, $class_id);
    111 my $outputStats = $outputRoot . '.' . $class_id . '.stats';
     125
     126my $outputName  = $ipprc->filename("PPIMAGE.OUTPUT", $outputRoot, $class_id);
     127my $bin1Name    = $ipprc->filename("PPIMAGE.BIN1", $outputRoot, $class_id);
     128my $bin2Name    = $ipprc->filename("PPIMAGE.BIN2", $outputRoot, $class_id);
     129my $outputStats = $ipprc->filename("PPIMAGE.STATS", $outputRoot, $class_id);
    112130
    113131# Run ppImage & ppStats
    114 my $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
    115 my $binnedStats = PS::IPP::Metadata::Stats->new(); # Stats parser
    116132unless ($no_op) {
    117133    my $command = "$ppImage -file $input_uri $outputRoot";
     
    144160    close $statsFile;
    145161
    146     # Parse the statistics on the residual image
     162    # Parse the stats file contents into a metadata
    147163    my $mdcParser = PS::IPP::Metadata::Config->new;     # Parser for metadata config files
    148164    my $metadata = $mdcParser->parse(join "", @contents) or &my_die("Unable to parse metadata config doc", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
     165
     166    # Parse the statistics on the residual image
     167    my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser
    149168    $stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
    150169
     
    160179    # parse the binned image statistics
    161180    my $binnedMetadata = $mdcParser->parse(join "", @$stdout_buf) or &my_die("Unable to parse metadata output", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
     181
     182    my $binnedStats = PS::IPP::Metadata::Stats->new($BINNED_STATS); # Stats parser
    162183    $binnedStats->parse($binnedMetadata) or &my_die("Unable to find all values in statistics output.", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
    163184}
    164185
     186# Command to update the database
     187my $command = "$dettool -addresidimfile";
     188$command .= " -det_id $det_id";
     189$command .= " -iteration $iter";
     190$command .= " -exp_tag $exp_tag";
     191$command .= " -class_id $class_id";
     192$command .= " -recip $recipe";
     193$command .= " -uri $outputName";
     194$command .= " -path_base $outputRoot";
     195$command .= " -dbname $dbname" if defined $dbname;
     196
     197# add in the elements from the selected stats above
     198foreach my $entry (@$STATS @$BINNED_STATS) {
     199    my $value = $entry->{value};
     200    my $flag = $entry->{flag};
     201    $command .= " $flag $value";
     202}
     203
    165204# Add the processed file to the database
    166 # these value must be defined (NAN if not valid), or we raise a programming error
    167 my $bg            = $stats->bg_mean();
    168 my $bg_stdev      = $stats->bg_stdev();
    169 my $bg_mean_stdev = $stats->bg_mean_stdev();
    170 my $bin_stdev     = $binnedStats->bg_stdev();
    171 my $fringe_0      = ${$stats->fringe_mean()}[0];
    172 my $fringe_1      = ${$stats->fringe_err()}[0];
    173 my $fringe_2      = ${$stats->fringe_mean_stdev()}[0];
    174 my $dfringe_0     = ${$stats->dfringe_mean()}[0];
    175 my $dfringe_1     = ${$stats->dfringe_err()}[0];
    176 my $dfringe_2     = ${$stats->dfringe_mean_stdev()}[0];
    177 
    178 print "bg_mean_stdev: " . $bg_mean_stdev . "\n";
    179 print "stats : bg_mean_stdev: " . $stats->bg_mean_stdev() . "\n";
    180 
    181205unless ($no_update) {
    182     my $command = "$dettool -addresidimfile -det_id $det_id -iteration $iter";
    183     $command .= " -exp_tag $exp_tag -class_id $class_id";
    184     $command .= " -recip $recipe -uri $outputName -path_base $outputRoot";
    185     $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
    186     $command .= " -bin_stdev $bin_stdev";
    187     $command .= " -fringe_0 $fringe_0 -fringe_1 $fringe_1 -fringe_2 $fringe_2";
    188     $command .= " -user_1 $dfringe_0 -user_2 $dfringe_1 -user_3 $dfringe_2";
    189     $command .= " -dbname $dbname" if defined $dbname;
    190 
    191206    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    192207        run(command => $command, verbose => 1);
     
    196211        exit($error_code);
    197212    }
     213} else {
     214    print "skipping command: $command\n";
    198215}
    199216
     
    209226    carp($msg);
    210227    if ($det_id and $iter and $exp_tag and not $no_update) {
    211         my $command = "$dettool -addresidimfile -det_id $det_id -iteration $iter -exp_tag $exp_tag -class_id $class_id -code $exit_code";
     228        my $command = "$dettool -addresidimfile";
     229        $command .= " -det_id $det_id";
     230        $command .= " -iteration $iter";
     231        $command .= " -exp_tag $exp_tag";
     232        $command .= " -class_id $class_id";
     233        $command .= " -code $exit_code";
    212234        $command .= " -dbname $dbname" if defined $dbname;
    213235###        system ($command);
  • trunk/ippScripts/scripts/detrend_stack.pl

    r13989 r14009  
    1616use IPC::Cmd 0.36 qw( can_run run );
    1717use PS::IPP::Metadata::Config;
     18use PS::IPP::Metadata::Stats;
    1819use PS::IPP::Metadata::List qw( parse_md_list );
    19 use PS::IPP::Metadata::Stats;
    2020
    2121use PS::IPP::Config qw($PS_EXIT_SUCCESS
     
    4141    'camera|c=s'        => \$camera,
    4242    'dbname|d=s'        => \$dbname, # Database name
    43     'workdir|w=s'       => \$workdir,   # Working directory, for output files
     43    'workdir|w=s'       => \$workdir, # Working directory, for output files
    4444    'reduction=s'       => \$reduction, # Reduction class for processing
    4545    'no-update'         => \$no_update,
     
    4949
    5050pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
    51 pod2usage( -msg => "Required options: --det_id --iteration --class_id --det_type --camera", -exitval => 3 )
     51pod2usage( -msg => "Required options: --det_id --iteration --class_id --det_type --camera",
     52           -exitval => 3)
    5253    unless defined $det_id
    5354    and defined $iter
     
    6465$reduction = "DETREND" unless defined $reduction;
    6566my $recipe = $ipprc->reduction($reduction, uc($det_type) . '_STACK'); # Recipe name to use
     67
     68# values to extract from output metadata and the stats to calculate
     69# XXX -bg_mean_stdev should take rms of bg_mean_stdev if bg_mean_stdev != 0 (A)
     70# XXX -bg_mean_stdev should take stdev of bg_mean if bg_mean_stdev == 0     (B)
     71# XXX  (A) if imfile.Ncomp > 1, (B) if imfile.Ncomp == 1
     72my $STATS =
     73   [   
     74       #          KEYWORD                 STATISTIC          CHIPTOOL FLAG
     75       { name => "bg",             type => "mean",  flag => "-bg" },
     76       { name => "bg",             type => "stdev", flag => "-bg_mean_stdev" },
     77       { name => "bg_stdev",       type => "rms",   flag => "-bg_stdev" },
     78       # { name => "bg_mean_stdev",  type => "rms",   flag => "-bg_mean_stdev" },
     79   ];
    6680
    6781# Look for programs we need
     
    7488}
    7589
    76 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
     90$workdir = caturi( $workdir, "$camera.$det_type.$det_id" ) if defined $workdir;
     91
     92my $outputRoot  = $ipprc->file_prepare( "$camera.$det_type.$det_id.$iter.$class_id", $workdir, ${$files}[0]->{uri} );
     93my $outputStack = $outputRoot . '.fits'; # Output name
     94my $outputStats = $outputRoot . '.stats'; # Statistics name
    7795
    7896# Get list of files to stack
    7997my $files;                      # Array of files to be stacked
    8098{
    81     my $command = "$dettool -processedimfile -det_id $det_id -class_id $class_id -included"; # Command to run
     99    my $command = "$dettool -processedimfile -included";
     100    $command .= " -det_id $det_id";
     101    $command .= " -class_id $class_id";
    82102    $command .= " -dbname $dbname" if defined $dbname;
     103
    83104    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    84105        run(command => $command, verbose => $verbose);
     
    87108        &my_die("Unable to perform dettool -processedimfile: $error_code", $det_id, $iter, $class_id, $error_code);
    88109    }
     110
     111    my $mdcParser = PS::IPP::Metadata::Config->new;     # Parser for metadata config files
    89112    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
    90113        &my_die("Unable to parse metadata config doc", $det_id, $iter, $class_id, $PS_EXIT_PROG_ERROR);
     
    93116}
    94117
    95 $workdir = caturi( $workdir, "$camera.$det_type.$det_id" ) if defined $workdir;
    96 
    97 my $outputRoot = $ipprc->file_prepare( "$camera.$det_type.$det_id.$iter.$class_id", $workdir, ${$files}[0]->{uri} );
    98 my $outputStack = $outputRoot . '.fits'; # Output name
    99 my $outputStats = $outputRoot . '.stats'; # Statistics name
     118my $command = "$ppMerge $outputStack"; # Command to run
     119foreach my $file (@$files) {
     120    $command .= ' ' . $file->{uri};
     121}
     122$command .= " -recipe PPMERGE $recipe";
     123$command .= ' -type ' . uc($det_type); # Type of stacking to perform
     124$command .= " -stats $outputStats";     # Statistics output filename
     125$command .= " -recipe PPSTATS CHIPSTATS";
    100126
    101127# Stack the files
    102 my $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
    103128unless ($no_op) {
    104     my $command = "$ppMerge $outputStack"; # Command to run
    105     foreach my $file (@$files) {
    106         $command .= ' ' . $file->{uri};
    107     }
    108     $command .= " -recipe PPMERGE $recipe";
    109     $command .= ' -type ' . uc($det_type); # Type of stacking to perform
    110     $command .= " -stats $outputStats"; # Statistics output filename
    111     $command .= " -recipe PPSTATS CHIPSTATS";
    112129   
    113130    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     
    129146    my $metadata = $mdcParser->parse($contents) or
    130147        &my_die("Unable to parse metadata config doc", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR);
    131     $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
     148
     149    my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser
    132150    $stats->parse($metadata)  or
    133151        &my_die("Unable to find all values in statistics output.", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR);
    134152}
    135153
    136 my $bg            = $stats->bg_mean();
    137 my $bg_stdev      = $stats->bg_stdev();
    138 my $bg_mean_stdev = $stats->bg_mean_stdev();
     154# Command to update the database
     155$command  = "$dettool -addstacked";
     156$command .= " -det_id $det_id -iteration $iter";
     157$command .= " -class_id $class_id";
     158$command .= " -uri $outputStack";
     159$command .= " -recip $recipe";
     160$command .= " -dbname $dbname" if defined $dbname;
     161
     162# add in the elements from the selected stats above
     163foreach my $entry (@$STATS) {
     164    my $value = $entry->{value};
     165    my $flag = $entry->{flag};
     166    $command .= " $flag $value";
     167}
    139168
    140169# Add the resultant into the database
    141170unless ($no_update) {
    142     my $command = "$dettool -addstacked";
    143     $command .= " -det_id $det_id -iteration $iter";
    144     $command .= " -class_id $class_id";
    145     $command .= " -uri $outputStack";
    146     $command .= " -recip $recipe";
    147     $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
    148     $command .= " -dbname $dbname" if defined $dbname;
    149 
    150171    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    151172        run(command => $command, verbose => $verbose);
     
    155176        exit($error_code);
    156177    }
    157 }
    158 
     178} else {
     179    print "skipping command: $command\n";
     180}
    159181
    160182sub my_die
     
    168190    carp($msg);
    169191    if ($det_id and $iter and $class_id and not $no_update) {
    170         my $command = "$dettool -addstacked -det_id $det_id -iteration $iter -class_id $class_id -code $exit_code";
     192        my $command = "$dettool -addstacked";
     193        $command .= " -det_id $det_id";
     194        $command .= " -iteration $iter";
     195        $command .= " -class_id $class_id";
     196        $command .= " -code $exit_code";
    171197        $command .= " -dbname $dbname" if defined $dbname;
    172198###        system ($command);
  • trunk/ippScripts/scripts/register_imfile.pl

    r13989 r14009  
    9898
    9999# setup cache interface
    100 
     100# XXX why is this being cached?
    101101my $c = Cache::File->new(
    102102    cache_root => File::Spec->catdir($ENV{'HOME'}, '.pxtools', basename($0)),
     
    125125    my @constants = keys %{CONSTANTS()}; # List of constants to parse out
    126126    my @variables = keys %{VARIABLES()}; # List of variables to parse out
     127
    127128    $stats = PS::IPP::Metadata::Stats->new(\@constants, \@variables); # Stats parser
    128129    unless ($stats->parse($metadata)) {
    129130        warn ("Unable to find all values");
    130131        &my_die ($exp_tag, $class_id, $PS_EXIT_CONFIG_ERROR);
    131     }
    132 
    133     if (0) {
    134         # XXX for a test, randomly declare a failure and return to pantasks
    135         my $rnd = rand(1);
    136         if ($rnd > 0.5) {
    137             warn ("random failure");
    138             &my_die ($exp_tag, $class_id, $PS_EXIT_DATA_ERROR);
    139         }
    140132    }
    141133}
Note: See TracChangeset for help on using the changeset viewer.