IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 14021


Ignore:
Timestamp:
Jul 5, 2007, 1:32:33 PM (19 years ago)
Author:
eugene
Message:

remove all header examination from inject; use new Stats.pm APIs in register; rework of exp_tag, exp_id, exp_name; newImfile:class_id,telescope,inst are now temporary info; register supplies real telescope,inst,class_id

Location:
trunk/ippScripts/scripts
Files:
3 edited

Legend:

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

    r13751 r14021  
    11#!/usr/bin/env perl
    22
    3 # this program injects a list of single-file exposures into the db, taking the
    4 # filename (without .fits) as the exp_tag.
     3# this program injects a list of single-file exposures into the db,
     4# taking the filename (without .fits) as the exp_tag.  the user
     5# supplies a temporary telescope and instrument name.  these are used
     6# for informational purposes only until the registration step can
     7# determine the true telescope and instrument name from the image
     8# headers.
     9
     10# this program should not fail because of the data format or the
     11# configuration, except for the very basic database setup.
    512
    613use warnings;
     
    815
    916use IPC::Cmd 0.36 qw( can_run run );
    10 use PS::IPP::Metadata::Config;
    11 use PS::IPP::Metadata::List qw( parse_md_list );
    1217use File::Spec;
    13 # use File::Find::Rule;
    1418use PS::IPP::Config;
    15 my $ipprc = PS::IPP::Config->new();
    16 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
     19
     20my $ipprc = PS::IPP::Config->new(); # this is used for PATH, NEB filename conversions
    1721
    1822use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
     
    2024
    2125# Parse the command-line arguments
    22 my ($workdir, $dbname);
     26my ($workdir, $dbname, $telescope, $instrument, $help);
    2327GetOptions(
    24     'workdir|w=s'   => \$workdir,  # working directory for output files
    25     'dbname|d=s'    => \$dbname    # Database name
     28    'workdir|w=s'    => \$workdir, # working directory for output files
     29    'dbname|d=s'     => \$dbname, # Database name
     30    'telescope|t=s'  => \$telescope, # user-supplied telescope name
     31    'instrument|i=s' => \$instrument, # user-supplied instrument name
     32    'help'           => \$help # give help listing
    2633) or pod2usage( 2 );
    2734
    28 pod2usage( -msg => "Usage: $0 [--workdir path] [--dbname dbname] (files)", -exitval => 2 ) if scalar @ARGV == 0;
     35pod2usage( -msg => "inject one or many files into the IPP pipeline database", -exitval => 2
     36
     37pod2usage( -msg => "Usage: $0 --telescope (name) --instrument (name) [--workdir path] [--dbname dbname] (files)",
     38           -exitval => 2 ) if
     39           scalar @ARGV == 0;
     40
     41pod2usage( -msg => "Required options: --telescope (name) --instrument (name)",
     42           -exitval => 3) unless
     43           defined $telescope and
     44           defined $instrument;
    2945
    3046my $pxinject = can_run('pxinject') or die "Can't find pxinject\n";
    31 my $ppStats = can_run('ppStats') or die "Can't find ppStats\n";
    3247
    3348# if workdir is not defined, assign the current path
     
    4055foreach my $file ( @ARGV ) {
    4156    my $absfile = File::Spec->rel2abs( $file );
    42     inject($absfile, $workdir, $dbname);
     57    inject($absfile, $workdir, $dbname, $telescope, $instrument);
    4358    $num ++;
    4459}
     
    4863sub inject
    4964{
    50     my $absfile = shift;                # absolute path for this file
    51     my $workdir  = shift;               # absolute path for output directory
    52     my $dbname = shift;
     65    my $absfile = shift;        # absolute path for this file
     66    my $workdir  = shift;       # absolute path for output directory
     67    my $dbname = shift;         # IPP database to use
     68    my $telescope = shift;      # user-specified telescope
     69    my $instrument = shift;     # user-specified instrument
    5370
    5471    my ( $vol, $path, $name ) = File::Spec->splitpath( $absfile );
    55     my ( $exp ) = $name =~ /(.*)\.fits/;
     72    my ( $exp_name ) = $name =~ /(.*)\.fits/;
    5673
    5774    my $relfile = $ipprc->convert_filename_relative( $absfile );
    5875
    59     my $command_type = "$ppStats -recipe PPSTATS INJECT $absfile";
    60 
    61     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command_type, verbose => 1);
    62     die "Unable to perform ppStats: $error_code\n" if not $success;
    63     my $metadata = $mdcParser->parse(join "", @$stdout_buf)
    64         or die "unable to parse metadata config doc";
    65 
    66     my $type;
    67     my $telescope;
    68     my $instrument;
    69     foreach my $row (@$metadata) {
    70         if ($row->{name} eq "FPA.TELESCOPE")  { $telescope = $row->{value}; }
    71         if ($row->{name} eq "FPA.INSTRUMENT") { $instrument = $row->{value}; } # this entry must return the camera in the ipp/config system which we load
    72     }
    73    
    74     my $command_exp = "$pxinject -newExp -exp_id $exp -inst $instrument -telescope $telescope -imfiles 1 -workdir $workdir" ; # Command to run
     76    my $command_exp = "$pxinject -newExp -exp_name $exp_name -inst $instrument -telescope $telescope -imfiles 1 -workdir $workdir" ; # Command to run
    7577    if ($dbname) {
    7678        $command_exp = "$command_exp -dbname $dbname";
     
    7981    my ( $success_exp, $error_code_exp, $full_buf_exp, $stdout_buf_exp, $stderr_buf_exp ) =
    8082        run( command => $command_exp, verbose => 1 );
    81     die "Unable to inject $exp: $error_code_exp\n" if not $success_exp;
     83    die "Unable to inject $exp_name: $error_code_exp\n" if not $success_exp;
    8284   
    8385    my @line = split(/\s+/, $$stdout_buf_exp[0]); # The output line, containing the exposure tag
    84     my $exp_tag = $line[2];     # The exposure tag
     86    my $exp_id = $line[2];      # The exposure tag
    8587   
    86     my $command_imfile = "$pxinject -newImfile -exp_tag $exp_tag -class fpa -class_id fpa -uri $relfile"; # Command to run
     88    # XXX the class_id used here should be temporary : register should replace it with the true class_id
     89    my $command_imfile = "$pxinject -newImfile -exp_id $exp_id -exp_name $exp_name -class fpa -class_id fpa -uri $relfile"; # Command to run
    8790    if ($dbname) {
    8891        $command_imfile = "$command_imfile -dbname $dbname";
     
    9093   
    9194    my ( $success_imfile, $error_code_imfile, $full_buf_imfile, $stdout_buf_imfile, $stderr_buf_imfile ) = run( command => $command_imfile, verbose => 1 );
    92     die "Unable to inject $exp imfile: $error_code_imfile\n" if not $success_imfile;
     95    die "Unable to inject $exp_name imfile: $error_code_imfile\n" if not $success_imfile;
    9396
    9497    return 1;
  • trunk/ippScripts/scripts/register_exp.pl

    r13785 r14021  
    11#!/usr/bin/env perl
    22
     3use Carp;
    34use warnings;
    45use strict;
     
    910print "\n\n";
    1011print "Starting script $0 on $host\n\n";
     12
     13use vars qw( $VERSION );
     14$VERSION = '0.01';
    1115
    1216use Cache::File;
     
    3135    );
    3236
    33 my ($cache, $exp_tag, $workdir, $dbname, $no_update, $no_op);
    34 
     37my ($cache, $exp_id, $exp_tag, $workdir, $dbname, $no_update, $no_op);
    3538GetOptions(
    3639    'caches'        => \$cache,
    37     'exp_tag|e=s'   => \$exp_tag,
     40    'exp_id|e=s'    => \$exp_id,
     41    'exp_tag|t=s'   => \$exp_tag,
    3842    'workdir|w=s'   => \$workdir, # Working directory for output files
    3943    'dbname|d=s'    => \$dbname, # Database name   
     
    4347
    4448pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
    45 pod2usage(
    46     -msg => "Required options: --exp_tag",
    47     -exitval => 3,
    48 ) unless defined $exp_tag;
    49 
    50 # Define setup
    51 use constant TYPE => "exp_type"; # Observation type keyword
    52 
    53 # should we reverse this?  add -detrend UNLESS type is OBJECT? (we can always queue to chip by hand...)
    54 # use constant DETRENDS => [ "bias", "zero", "dark", "shutter", "flat", "fringe", "domeflat", "skyflat" ]; # Observation types to mark as detrend
    55 # use constant DETREND_FLAG => "-detrend"; # Flag to use to mark detrend exposure
    56 
    57 use constant SCIENCE => [ "object" ]; # Observation types to NOT mark as detrend
    58 use constant DETREND_FLAG => "-detrend"; # Flag to use to mark detrend exposure
    59 
    60 use constant REGISTER_URI => 'uri'; # Key for the URI
    61 use constant REGISTER_CLASSID => 'class_id'; # Key for the class id
    62 use constant REGISTER_BG => 'bg';        # Key for the background
    63 use constant REGISTER_BG_STDEV => 'bg_stdev'; # Key for the background standard deviation
    64 use constant REGISTER_BG_MEAN_STDEV => 'bg_mean_stdev'; # Key for the mean of the background standard deviation
    65 
    66 # These values should be constant for all components
    67 use constant CONSTANTS => [
    68                            "filelevel", # File level
    69                            "object", # Object
    70                            "exp_type", # Exposure type
    71                            "filter", # Filter used
    72                            "dateobs", # Time of exposure
    73                        ];
    74 
    75 # These values may vary across components; we will take the average
    76 use constant VARIABLES => [
    77                            "ccd_temp", # CCD temperature
    78                            "exp_time", # Exposure time
    79                            "sat_pixel_frac", # Fraction of saturated pixels
    80                            "airmass", # Airmass
    81                            "ra", # Right ascension
    82                            "decl", # Declination
    83                            "posang", # Position angle
    84                            "alt", # Altitude
    85                            "az", # Azimuth
    86                            ];
    87 
     49pod2usage( -msg => "Required options: --exp_id --exp_tag",
     50           -exitval => 3) unless
     51    defined $exp_id and
     52    defined $exp_tag;
     53
     54# add -detrend UNLESS type is one of SCIENCE listed below (eg, OBJECT)
     55my @SCIENCE = ( "object" ); # Observation types to NOT mark as detrend
     56my $DETREND_FLAG = "-detrend"; # Flag to use to mark detrend exposure
     57
     58# values to extract from output metadata and the stats to calculate
     59my $STATS =
     60   [   
     61       #          PPSTATS KEYWORD         STATISTIC          CHIPTOOL FLAG
     62       { name => "exp_name",       type => "constant",   flag => "-exp_name" }, # File level
     63       { name => "telescope",      type => "constant",   flag => "-telescope" }, # File level
     64       { name => "inst",           type => "constant",   flag => "-inst" }, # File level
     65       { name => "filelevel",      type => "constant",   flag => "-filelevel" }, # File level
     66       { name => "object",         type => "constant",   flag => "-object" },
     67       { name => "exp_type",       type => "constant",   flag => "-exp_type" }, # File level
     68       { name => "filter",         type => "constant",   flag => "-filter" }, # File level
     69       { name => "dateobs",        type => "constant",   flag => "-dateobs" }, # File level
     70       { name => "ccd_temp",       type => "mean",       flag => "-ccd_temp" }, # CCD temperature
     71       { name => "exp_time",       type => "mean",       flag => "-exp_time" }, # Exposure time
     72       { name => "sat_pixel_frac", type => "mean",       flag => "-sat_pixel_frac" }, # Fraction of saturated pixels
     73       { name => "airmass",        type => "mean",       flag => "-airmass" }, # Airmass
     74       { name => "ra",             type => "mean",       flag => "-ra" }, # Right ascension
     75       { name => "decl",           type => "mean",       flag => "-decl" }, # Declination
     76       { name => "posang",         type => "mean",       flag => "-posang" }, # Position angle
     77       { name => "alt",            type => "mean",       flag => "-alt" }, # Altitude
     78       { name => "az",             type => "mean",       flag => "-az" }, # Azimuth
     79       { name => "bg",             type => "mean",       flag => "-bg" }, # Azimuth
     80       { name => "bg",             type => "stdev",      flag => "-bg_mean_stdev" }, # Azimuth
     81       { name => "bg_stdev",       type => "rms",        flag => "-bg_stdev" }, # Azimuth
     82       ];
    8883
    8984# Look for commands we need
     
    9893
    9994# setup cache interface
    100 
    10195my $c = Cache::File->new(
    10296    cache_root => File::Spec->catdir($ENV{'HOME'}, '.pxtools', basename($0)),
     
    10498);
    10599
    106 my $mdcParser = PS::IPP::Metadata::Config->new;        # Parser for metadata config files
    107 
    108 # Get the list of imfiles
    109 my $imfiles;
    110 {
    111     my $command = "$regtool -processedimfile -exp_tag $exp_tag";
     100# Get the list of imfiles & their stats
     101{
     102    my $command = "$regtool -processedimfile -exp_id $exp_id";
    112103    $command .= " -dbname $dbname" if defined $dbname;
    113104    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     
    115106    unless ($success) {
    116107        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    117         warn ("Unable to perform regtool -processedimfile on exposure id $exp_tag: $error_code");
     108        warn ("Unable to perform regtool -processedimfile on exposure id $exp_id: $error_code");
    118109        exit ($error_code);
    119110    }
    120111
     112    # Parse the output
     113    my $mdcParser = PS::IPP::Metadata::Config->new;        # Parser for metadata config files
    121114    my $metadata = $mdcParser->parse(join "", @$stdout_buf);
    122115    unless ($metadata) {
    123         warn ("Unable to parse metadata config doc");
    124         &my_die ($exp_tag, $PS_EXIT_PROG_ERROR);
    125     }
    126     $imfiles = parse_md_list($metadata); # Data for imfiles
    127 }
    128 
    129 # Process the values
    130 my %values;                        # Values to update
    131 foreach my $variable (@{VARIABLES()}) {
    132     $values{$variable} = [];
    133 }
    134 my @backgrounds;                # Array of backgrounds
    135 my @variances;                        # Array of background standard deviations\
    136 my $obsType;                        # Observation type
    137  
    138 foreach my $imfile (@$imfiles) {
    139     foreach my $constant (@{CONSTANTS()}) {
    140         my $value = get_value($imfile, $constant); # Value for imfile
    141         if (not defined $values{$constant}) {
    142             $values{$constant} = $value;
    143         }
    144         if ($values{$constant} ne $value) {
    145             warn ("Value of $constant for " . $imfile->{REGISTER_CLASSID()} .
    146                   " doesn't match previous value");
    147             &my_die($exp_tag, $PS_EXIT_PROG_ERROR);
    148         }
    149     }
    150    
    151     foreach my $variable (@{VARIABLES()}) {
    152         my $value = get_value($imfile, $variable); # Value for imfile
    153         my $array = $values{$variable};        # Array of data
    154         push @$array, $value;
    155     }
    156    
    157     my $bg = get_value($imfile, REGISTER_BG());
    158     push @backgrounds, $bg;
    159    
    160     my $stdev = get_value($imfile, REGISTER_BG_MEAN_STDEV());
    161     push @variances, $stdev**2;
    162 }
    163 
    164 if (0) {
    165 # XXX for a test, randomly declare a failure and return to pantasks
    166     my $rnd = rand(1);
    167     if ($rnd > 0.5) {
    168         warn ("random failure");
    169         &my_die ($exp_tag, $PS_EXIT_DATA_ERROR);
    170     }
     116        &my_die ("Unable to parse metadata config doc", $exp_id, $PS_EXIT_PROG_ERROR);
     117    }
     118
     119    # extract the stats from the metadata
     120    my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser
     121    unless ($stats->parse($metadata)) {
     122        &my_die ("Unable to find all values", $exp_id, $class_id, $PS_EXIT_CONFIG_ERROR);
     123    }
     124}
     125
     126my $command = "$regtool -addprocessedexp";
     127$command .= " -exp_id $exp_id";
     128$command .= " -exp_tag $exp_tag";
     129$command .= " -dbname $dbname" if defined $dbname;
     130
     131# add in the elements from the selected stats above
     132foreach my $entry (@$STATS) {
     133    my $value = $entry->{value};
     134    my $flag = $entry->{flag};
     135    $command .= " $flag $value";
     136}
     137
     138my $exp_type = &STATS_value_for_flag ("-exp_type");
     139
     140# Add the detrend flag, if needed
     141foreach my $scienceType (@SCIENCE) {
     142    if (lc($exp_type) =~ /$scienceType/) {
     143        last;
     144    }
     145    $command .= " $DETREND_FLAG";
    171146}
    172147
    173148# Output results to the database
    174149unless ($no_update) {
    175     my $command = "$regtool -addprocessedexp -exp_tag $exp_tag"; # Command to execute
    176    
    177     # Add the values of interest
    178     foreach my $constant (@{CONSTANTS()}) {
    179         my $value = $values{$constant};
    180         if ($value =~ /\s/) {
    181             # Quote spaces
    182             $value = "\'$value\'";
    183         }
    184         $command .= " -$constant $value";
    185     }
    186     foreach my $variable (@{VARIABLES()}) {
    187         my $array = $values{$variable};        # Array of values
    188         my $stats = Statistics::Descriptive::Sparse->new; # Statistics calculator
    189         $stats->add_data(@$array);
    190 
    191         $command .= " -$variable " . $stats->mean();
    192     }
    193 
    194     # Add the statistics
    195     my $meanStats = Statistics::Descriptive::Sparse->new; # Statistics calculator
    196     $meanStats->add_data(@backgrounds);
    197     my $stdevStats = Statistics::Descriptive::Sparse->new; # Statistics calculator
    198     $stdevStats->add_data(@variances);
    199     my $bg = ($meanStats->mean() or 'NAN');
    200     my $bg_stdev = (sqrt($stdevStats->mean()) or 'NAN');
    201     my $bg_mean_stdev = ($meanStats->standard_deviation() or 'NAN');
    202     $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
    203    
    204     # Add the detrend flag
    205     foreach my $scienceType (@{SCIENCE()}) {
    206         if (lc($values{TYPE()}) =~ /$scienceType/) {
    207             last;
    208         }
    209         $command .= ' ' . DETREND_FLAG;
    210     }
    211 
    212     $command .= " -dbname $dbname" if defined $dbname;
    213 
    214150    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    215151        cache_run(command => $command, verbose => 1);
    216152    unless ($success) {
    217153        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    218         warn ("Unable to run regtool -addprocessedexp for $exp_tag: $error_code");
     154        warn ("Unable to run regtool -addprocessedexp for $exp_id: $error_code");
    219155        exit($error_code);
    220156    }
     157} else {
     158    print "skipping command: $command\n";
    221159}
    222160
    223161### Pau.
    224 
    225 # Get the value for a particular imfile, along with a strong check for its existence
    226 sub get_value {
    227     my $imfile = shift;                # The hash of values
    228     my $name = shift;                # The name of the value to check
    229 
    230     my $source = $imfile->{REGISTER_CLASSID()}; # Where it comes from
    231 
    232     unless (defined $imfile->{$name}) {
    233         warn ("Couldn't find value of $name for class_id=$source");
    234         &my_die ($exp_tag, $PS_EXIT_PROG_ERROR);
    235     }
    236     return $imfile->{$name};
    237 }
    238162
    239163sub cache_run
     
    253177sub my_die
    254178{
    255     my $exp_tag = $_[0];
    256     my $exit_code = $_[1];
    257     if ($exp_tag and not $no_update) {
    258         my $command = "$regtool -addprocessedexp -exp_tag $exp_tag -code $exit_code";
     179    my $msg = shift; # Warning message on die
     180    my $exp_id = shift;
     181    my $exit_code = shift;
     182
     183    carp($msg);
     184    if ($exp_id and not $no_update) {
     185        my $command = "$regtool -addprocessedexp -exp_id $exp_id -code $exit_code";
    259186        $command .= " -dbname $dbname" if defined $dbname;
    260187###        system($command);
    261188    }
    262189    exit $exit_code;
     190}
     191
     192sub STATS_value_for_flag
     193{
     194    my $STATS = shift;
     195    my $flag  = shift;
     196
     197    foreach my $entry (@$STATS) {
     198        if ($flag eq $entry->{flag}) {
     199            return $entry->{value};
     200        }
     201    }
     202    return 'NAN';
    263203}
    264204
  • trunk/ippScripts/scripts/register_imfile.pl

    r14009 r14021  
    11#!/usr/bin/env perl
    22
     3use Carp;
    34use warnings;
    45use strict;
     
    3738use Pod::Usage qw( pod2usage );
    3839
    39 my ($cache, $exp_tag, $class_id, $uri, $workdir, $dbname, $no_update, $no_op);
    40 
     40my ($cache, $exp_id, $class_id, $exp_name, $uri, $workdir, $dbname, $no_update, $no_op);
    4141GetOptions(
    4242    'caches'        => \$cache,
    43     'exp_tag|e=s'   => \$exp_tag,
     43    'exp_id|e=s'    => \$exp_id,
    4444    'class_id|i=s'  => \$class_id,
     45    'exp_name|n=s'  => \$exp_name,
    4546    'uri|u=s'       => \$uri,
    4647    'workdir|w=s'   => \$workdir, # Working directory for output files
     
    5152
    5253pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
    53 pod2usage(
    54     -msg => "Required options: --exp_tag --class_id --uri",
    55     -exitval => 3,
    56 ) unless defined $exp_tag
    57     and defined $class_id
    58     and defined $uri;
     54pod2usage( -msg => "Required options: --exp_id --class_id --exp_name --uri",
     55           -exitval => 3) unless
     56    defined $exp_id and
     57    defined $class_id and
     58    defined $exp_name and
     59    defined $uri;
    5960
    6061my $RECIPE = "REGISTER"; # Recipe to use for ppStats
    6162
    62 # These values should be constant for all components
    63 # The key is the name in the ppStats output; the value is the switch for "phase0 -updateexp"
    64 use constant CONSTANTS => {
    65     "FILE.LEVEL"   => "-filelevel", # File level
    66     "FPA.OBJECT"   => "-object", # Object
    67     "FPA.OBSTYPE"  => "-exp_type", # Exposure type
    68     "FPA.FILTER"   => "-filter", # Filter used
    69     "FPA.AIRMASS"  => "-airmass", # Airmass
    70     "FPA.RA"       => "-ra",        # Right ascension
    71     "FPA.DEC"      => "-decl",        # Declination
    72     "FPA.ALT"      => "-alt",        # Altitude
    73     "FPA.AZ"       => "-az",        # Azimuth
    74     "FPA.POSANGLE" => "-posang", # Position angle
    75     "FPA.TIME"     => "-dateobs", # Date of observation (UTC)
    76     };
    77 
    78 # These values may vary across components; we will take the average
    79 # The key is the name in the ppStats output; the value is the switch for "phase0 -updateexp"
    80 use constant VARIABLES => {
    81     "CHIP.TEMP"      => "-ccd_temp", # CCD temperature
    82     "CELL.EXPOSURE"  => "-exp_time", # Exposure time
    83     "SAT_PIXEL_FRAC" => "-sat_pixel_frac" # fraction of saturated pixels
    84     };
    85 
     63# values to extract from output metadata and the stats to calculate
     64my $STATS =
     65   [   
     66       #          PPSTATS KEYWORD         STATISTIC          CHIPTOOL FLAG
     67       { name => "FILE.LEVEL",     type => "constant", flag => "-filelevel" }, # File level
     68       { name => "CLASS.ID",       type => "constant", flag => "-class_id" },  # Real Class ID
     69       { name => "FPA.OBJECT",     type => "constant", flag => "-object" },    # Object
     70       { name => "FPA.OBSTYPE",    type => "constant", flag => "-exp_type" },  # Exposure type
     71       { name => "FPA.FILTER",     type => "constant", flag => "-filter" },    # Filter used
     72       { name => "FPA.AIRMASS",    type => "constant", flag => "-airmass" },   # Airmass
     73       { name => "FPA.RA",         type => "constant", flag => "-ra" },        # Right ascension
     74       { name => "FPA.DEC",        type => "constant", flag => "-decl" },      # Declination
     75       { name => "FPA.ALT",        type => "constant", flag => "-alt" },       # Altitude
     76       { name => "FPA.AZ",         type => "constant", flag => "-az" },        # Azimuth
     77       { name => "FPA.POSANGLE",   type => "constant", flag => "-posang" },    # Position angle
     78       { name => "FPA.TIME",       type => "constant", flag => "-dateobs" },   # Date of observation (UTC)
     79       { name => "FPA.TELESCOPE",  type => "constant", flag => "-telescope" }, # Telescope
     80       { name => "FPA.INSTRUMENT", type => "constant", flag => "-inst" },      # Instrument
     81       { name => "CHIP.TEMP"       type => "mean",     flag => "-ccd_temp" }, # CCD temperature
     82       { name => "CELL.EXPOSURE"   type => "mean",     flag => "-exp_time" }, # Exposure time
     83       { name => "SAT_PIXEL_FRAC"  type => "mean",     flag => "-sat_pixel_frac" } # fraction of saturated pixels
     84       { name => "ROBUST_MEDIAN",  type => "mean",     flag => "-bg" },
     85       { name => "ROBUST_MEDIAN",  type => "stdev",    flag => "-bg_mean_stdev" },
     86       { name => "ROBUST_STDEV",   type => "rms",      flag => "-bg_stdev" },
     87   ];
    8688
    8789# Look for programs we need
     
    105107
    106108# Run ppStats on the input file
    107 my $stats;
    108109{
     110    # extract the data from the image header; -level is used to get FILE.LEVEL and CLASS.ID
    109111    my $command = "$ppStats $uri -recipe PPSTATS $RECIPE -level"; # Command to run ppStats
    110112    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     
    112114    unless ($success) {
    113115        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    114         warn ("Unable to perform ppStats on exposure id $exp_tag: $error_code");
    115         &my_die ($exp_tag, $class_id, $error_code);
     116        &my_die ("Unable to perform ppStats on exposure id $exp_id: $error_code", $exp_id, $class_id, $error_code);
    116117    }
    117118   
     
    120121    my $metadata = $mdcParser->parse(join "", @$stdout_buf); # XXX is this join necessary?
    121122    unless ($metadata) {
    122         warn ("Unable to parse metadata config doc");
    123         &my_die ($exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
     123        &my_die ("Unable to parse metadata config doc", $exp_id, $class_id, $PS_EXIT_PROG_ERROR);
    124124    }
    125     my @constants = keys %{CONSTANTS()}; # List of constants to parse out
    126     my @variables = keys %{VARIABLES()}; # List of variables to parse out
    127125
    128     $stats = PS::IPP::Metadata::Stats->new(\@constants, \@variables); # Stats parser
     126    # extract the stats from the metadata
     127    my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser
    129128    unless ($stats->parse($metadata)) {
    130         warn ("Unable to find all values");
    131         &my_die ($exp_tag, $class_id, $PS_EXIT_CONFIG_ERROR);
     129        &my_die ("Unable to find all values", $exp_id, $class_id, $PS_EXIT_CONFIG_ERROR);
    132130    }
     131}
     132
     133my $command = "$regtool -addprocessedimfile";
     134$command .= " -exp_id $exp_id";
     135$command .= " -temp_class_id $class_id"; # the original class_id supplied by the user, replace by ppStats CLASS.ID
     136$command .= " -exp_name $exp_name"; # keep the supplied exp_name (could be derived from the file)
     137$command .= " -dbname $dbname" if defined $dbname;
     138
     139# add in the elements from the selected stats above
     140foreach my $entry (@$STATS) {
     141    my $value = $entry->{value};
     142    my $flag = $entry->{flag};
     143    $command .= " $flag $value";
    133144}
    134145
    135146# Push the results into the database
    136147unless ($no_update) {
    137     my $command = "$regtool -addprocessedimfile -exp_tag $exp_tag -class_id $class_id"; # Command to run regtool
    138 
    139     foreach my $constant (keys %{CONSTANTS()}) {
    140         my $value = ($stats->data($constant))->{value};
    141         if ($value =~ /\s/) {
    142             # Quote arguments with whitespace
    143             $value = "\'$value\'";
    144         }
    145         $command .=  ' ' . CONSTANTS->{$constant} . ' ' . $value;
    146     }
    147 
    148     foreach my $variable (keys %{VARIABLES()}) {
    149         # Just use the mean value
    150         $command .= ' ' . VARIABLES->{$variable} . ' ' . ($stats->data($variable))->{mean};
    151     }
    152 
    153     my $bg            = $stats->bg_mean();
    154     my $bg_stdev      = $stats->bg_stdev();
    155     my $bg_mean_stdev = $stats->bg_mean_stdev();
    156 
    157     $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
    158     $command .= " -dbname $dbname" if defined $dbname;
    159 
    160148    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    161149        run(command => $command, verbose => 1);
    162 
    163150    unless ($success) {
    164151        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    165         # If we can't run -addprocessedimfile, we can't actually set the error code. If this is not
    166         # a database error, it is probably a programming error (in regtool or the passed args)
    167152        warn ("Unable to perform regtool -addprocessedimfile: $error_code");
    168153        exit($error_code);
     
    186171sub my_die
    187172{
    188     my $exp_tag = $_[0];
    189     my $class_id = $_[1];
    190     my $exit_code = $_[2];
    191     if ($exp_tag && $class_id and not $no_update) {
    192         my $command = "$regtool -addprocessedimfile -exp_tag $exp_tag -class_id $class_id -code $exit_code";
     173    my $msg = shift; # Warning message on die
     174    my $exp_id = shift;
     175    my $class_id = shift;
     176    my $exit_code = shift;
     177
     178    carp($msg);
     179    if ($exp_id && $class_id and not $no_update) {
     180        my $command = "$regtool -addprocessedimfile";
     181        $command .= " -exp_id $exp_id";
     182        $command .= " -class_id $class_id";
     183        $command .= " -code $exit_code";
    193184        $command .= " -dbname $dbname" if defined $dbname;
    194185###        system ($command);
Note: See TracChangeset for help on using the changeset viewer.