IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 8236


Ignore:
Timestamp:
Aug 8, 2006, 2:54:43 PM (20 years ago)
Author:
Paul Price
Message:

Fixed to do phase 0 processing on the exposure level, including
getting the backgrounds (measured at the imfile level) from the
database.

File:
1 edited

Legend:

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

    r8197 r8236  
    1010use constant RECIPE => "PPSTATS_PHASE0"; # Recipe to use for ppStats
    1111
     12use constant TYPE => "OBSTYPE"; # Observation type keyword
     13use constant DETRENDS => ( "bias", "dark", "flat", "fringe" ); # Observation types to mark as detrend
     14use constant DETREND_FLAG => "-detrend"; # Flag to use to mark detrend exposure
     15
     16use constant IMFILE_URI => 'uri'; # Key for the URI out of p0search -pendingimfile
     17use constant IMFILE_BG => 'background'; # Key for the background out of p0search -pendingimfile
     18use constant IMFILE_BGSD => 'background_stdev'; # Key for the background standard deviation
     19
     20use constant EXP_BG => '-background'; # Switch to add background for exposure
     21use constant EXP_BGSD => '-background_stdev'; # Switch to add background standard deviation
     22use constant EXP_BGMEANSD => '-background_mean_stdev'; # Switch to add mean of background standard deviation
     23
    1224# These values should be constant for all components
    13 use constant CONSTANTS => (
    14                            "OBSTYPE", # Observation type
    15                            "DETTEM", # Detector temperature
    16                            "FPA.FILTER", # Filter used
    17                            "FPA.RA", # Right ascension
    18                            "FPA.DEC", # Declination
    19                            "FPA.AIRMASS", # Airmass
    20                            "TELALT", # Altitude
    21                            "TELAZ", # Azimuth
    22                            "FPA.POSANGLE" # Position angle
    23     );
     25use constant CONSTANTS => {
     26                           "FPA.FILTER"   => "-filter", # Filter used
     27                           "FPA.AIRMASS"  => "-airmass", # Airmass
     28                           "FPA.RA"       => "-ra", # Right ascension
     29                           "FPA.DEC"      => "-decl", # Declination
     30                           "FPA.POSANGLE" => "-posang", # Position angle
     31                           "TELALT"       => "-alt", # Altitude
     32                           "TELAZ"        => "-az", # Azimuth
     33                           "DETTEM"       => "-ccd_temp" # CCD temperature
     34    };
    2435
    2536# These values may vary across components; we will take the average
    26 use constant VARIABLES => (
    27                            "CELL.EXPOSURE", # Exposure time
     37use constant VARIABLES => {
     38                           "CELL.EXPOSURE" => "-exp_time", # Exposure time
    2839###                        "CELL.TIME" # Time of exposure --- not yet implemented
    29                            );
    30 if (scalar @ARGV == 0) {
    31     die "Perform phase 0 processing on the component files of an FPA.\n\n" .
    32         "Usage: $0 FPA_FILE_1.fits FPA_FILE_2.fits FPA_FILE_3.fits ...\n\n";
     40                           };
     41
     42if (scalar @ARGV == 0 || scalar @ARGV >= 2) {
     43    die "Perform phase 0 processing at the exposure level.\n\n" .
     44        "Usage: $0 EXP_ID\n\n";
    3345}
    3446
     
    3648# Look for commands we need
    3749my $missing_tools;
     50my $p0search = can_run('p0search')
     51    or (warn "can't find p0search" and $missing_tools = 1);
    3852my $ppStats = can_run('ppStats')
    3953    or (warn "can't find ppStats" and $missing_tools = 1);
    4054die "Can't find required tools.\n" if $missing_tools;
    4155
    42 my @filenames = @ARGV;          # Input files, all for one FPA
     56my $expid = shift @ARGV;        # Exposure id
     57
     58my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
     59
     60# Get the list of filenames
     61my $filenames;                  # Hash of input files, with their background and stdev
     62{
     63    my $command = "$p0search -pendingimfile -exp_id $expid";
     64    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     65        run(command => $command, verbose => 0);
     66    die "Unable to perform ppStats on exposure id $expid: $error_code\n" if not $success;
     67    my $parsed = $mdcParser->parse(join "", @$stdout_buf); # Parsed metadata
     68    $filenames = mdFileInfo($parsed); # Get filenames from the output
     69}
     70
    4371my %values;                     # Values to return
    44 
    45 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
    46 
    47 foreach my $fileName (@filenames) {
     72foreach my $fileName (keys %$filenames) {
    4873    my $command = "$ppStats $fileName -recipe PPSTATS " . RECIPE; # Command to run
    4974    print "Executing: $command\n";
     
    5681}
    5782
    58 foreach my $constant (CONSTANTS) {
     83my $command = "$p0search -updateexp"; # Command to execute to update exposure parameters
     84
     85foreach my $constant (keys %{CONSTANTS()}) {
    5986    if (not defined $values{$constant}) {
    6087        die "Couldn't find value for $constant.\n";
    6188    }
     89    my $switch = CONSTANTS->{$constant}; # The switch to use on the value
    6290    my $value = $values{$constant}; # The value of interest
    63     print "$constant: $value\n";
    64 }
    65 
    66 foreach my $variable (VARIABLES) {
     91    $command .= " $switch $value";
     92}
     93
     94foreach my $variable (keys %{VARIABLES()}) {
    6795    if (not defined $values{$variable}) {
    6896        die "Couldn't find value for $variable.\n";
    6997    }
     98    my $switch = VARIABLES->{$variable}; # The switch to use on the value
    7099    # Take the mean
    71100    my $value = $values{$variable}->{value} / $values{$variable}->{num}; # Value of interest
    72     print "$variable: $value\n";
    73 }
     101    $command .= " $switch $value";
     102}
     103
     104### Now to do the background: mean background, SD of backgrounds, mean of background SDs
     105my $bgMean = 0;                 # Mean background
     106my $bgsdMean = 0;               # Mean of background SDs
     107foreach my $filename (keys %$filenames) {
     108    my $stats = $filenames->{$filename}; # Statistics for that file
     109    $bgMean += $stats->{bg};
     110    $bgsdMean += $stats->{bgsd};
     111}
     112$bgMean /= scalar(keys %$filenames);
     113$bgsdMean /= scalar(keys %$filenames);
     114my $bgSD = 0;                   # SD of backgrounds
     115foreach my $filename (keys %$filenames) {
     116    my $stats = $filenames->{$filename}; # Statistics for that file
     117    $bgSD += ($stats->{bg} - $bgMean)**2;
     118}
     119$bgSD = sqrt($bgSD / (scalar(keys %$filenames) - 1));
     120
     121$command .= " " . EXP_BGMEAN() . " $bgMean " . EXP_BGSD() . " $bgSD " . EXP_BGSDMEAN() . " $bgsdMean";
     122
     123print "$command";
     124
     125my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     126    run(command => $command, verbose => 0);
     127die "Unable to run phase0 update for $expid: $error_code\n" if not $success;
     128
    74129
    75130### Pau.
     131
     132
     133# Parse a metadata containing a list of files; return a hash keyed by the URI, containing bg and sd
     134sub mdFileInfo
     135{
     136    my $mdItemArray = shift;    # Array of metadata items from "p0search -pendingimfile"
     137
     138    my %uriHash;                # Hash of URIs to return
     139  IMFILE: foreach my $mdItem (@$mdItemArray) {
     140      die "Unexpected format for filenames: does not consist solely of METADATAs.\n"
     141          if $mdItem->{class} ne "metadata";
     142      my %imfileInfo = values2hash($mdItem->{value});           # File information
     143      die "Unexpected format for filenames: METADATAs do not contain " . IMFILE_URI . " element.\n"
     144          if not defined $imfileInfo{IMFILE_URI};
     145      die "Unexpected format for filenames: METADATAs do not contain " . IMFILE_BG . " element.\n"
     146          if not defined $imfileInfo{IMFILE_BG};
     147      die "Unexpected format for filenames: METADATAs do not contain " . IMFILE_BGSD . " element.\n"
     148          if not defined $imfileInfo{IMFILE_BGSD};
     149      $uriHash{$imfileInfo{IMFILE_URI}} = { 'bg' => $imfileInfo{IMFILE_BG},
     150                                            'bgsd' => $imfileInfo{IMFILE_BGSD}
     151                                        };
     152  }
     153   
     154    return \%uriHash;
     155}
     156
    76157
    77158# Parse a metadata, looking for the appropriate values
     
    89170     
    90171      return if not defined $mdItem->{name};
     172
     173      if ($mdItem->{name} eq TYPE) {
     174          foreach my $type (DETRENDS) {
     175              if (lc($mdItem->{value}) eq lc($type)) {
     176                  $values->{TYPE} = 1;
     177                  next MDITEM;
     178              }
     179          }
     180          # If we got here, it's not a detrend
     181          $values->{TYPE} = 0;
     182          next MDITEM;
     183      }
    91184     
    92       foreach my $constant (CONSTANTS) {
     185      foreach my $constant (keys %{CONSTANTS()}) {
    93186          if ($mdItem->{name} eq $constant) {
    94187              if (defined $values->{$constant} and $mdItem->{value} ne $values->{$constant}) {
     
    100193      }
    101194       
    102       foreach my $variable (VARIABLES) {
     195      foreach my $variable (keys %{VARIABLES()}) {
    103196          if ($mdItem->{name} eq $variable) {
    104197              $values->{$variable}->{value} += $mdItem->{value};
     
    109202  }
    110203}
     204
     205
     206# Convert values to a hash
     207sub values2hash
     208{
     209    my $values = shift;
     210 
     211    my %hash;
     212    foreach my $data (@$values) {
     213        $hash{$data->{name}} = $data->{value};
     214    }
     215 
     216    return \%hash;
     217}
Note: See TracChangeset for help on using the changeset viewer.