Changeset 8309
- Timestamp:
- Aug 11, 2006, 6:24:04 PM (20 years ago)
- Location:
- trunk/ippScripts/scripts
- Files:
-
- 2 edited
-
phase0exp.pl (modified) (4 diffs)
-
phase0imfile.pl (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ippScripts/scripts/phase0exp.pl
r8239 r8309 6 6 use IPC::Cmd qw( can_run run ); 7 7 use PS::IPP::Metadata::Config; 8 use PS::IPP::Metadata::List qw( parse_md_list ); 9 use Statistics::Descriptive; 8 10 use Data::Dumper; 9 10 use constant RECIPE => "PPSTATS_PHASE0_EXP"; # Recipe to use for ppStats11 11 12 12 use constant TYPE => "OBSTYPE"; # Observation type keyword … … 14 14 use constant DETREND_FLAG => "-detrend"; # Flag to use to mark detrend exposure 15 15 16 use constant IMFILE_URI => 'uri'; # Key for the URI out of p0search -pendingimfile 17 use constant IMFILE_BG => 'background'; # Key for the background out of p0search -pendingimfile 18 use constant IMFILE_BGSD => 'background_stdev'; # Key for the background standard deviation 16 use constant PHASE0_URI => 'uri'; # Key for the URI 17 use constant PHASE0_CLASSID => 'class_id'; # Key for the class id 18 use constant PHASE0_BG => 'bg'; # Key for the background 19 use constant PHASE0_BG_STDEV => 'bg_stdev'; # Key for the background standard deviation 20 use constant PHASE0_BG_MEAN_STDEV => 'bg_mean_stdev'; # Key for the mean of the background standard deviation 19 21 20 22 use constant EXP_BG => '-background'; # Switch to add background for exposure … … 23 25 24 26 # These values should be constant for all components 25 # The key is the name in the ppStats output; the value is the switch for "phase0 -updateexp" 26 use constant CONSTANTS => { 27 "FPA.FILTER" => "-filter", # Filter used 28 "FPA.AIRMASS" => "-airmass", # Airmass 29 "FPA.RA" => "-ra", # Right ascension 30 "FPA.DEC" => "-decl", # Declination 31 "FPA.POSANGLE" => "-posang", # Position angle 32 "TELALT" => "-alt", # Altitude 33 "TELAZ" => "-az", # Azimuth 34 "DETTEM" => "-ccd_temp" # CCD temperature 35 }; 27 use constant CONSTANTS => [ 28 "filter", # Filter used 29 "airmass", # Airmass 30 "ra", # Right ascension 31 "decl", # Declination 32 "posang", # Position angle 33 "alt", # Altitude 34 "az", # Azimuth 35 "ccd_temp" # CCD temperature 36 ]; 36 37 37 38 # These values may vary across components; we will take the average 38 # The key is the name in the ppStats output; the value is the switch for "phase0 -updateexp" 39 use constant VARIABLES => { 40 "CELL.EXPOSURE" => "-exp_time", # Exposure time 41 ### "CELL.TIME" # Time of exposure --- not yet implemented 42 }; 39 use constant VARIABLES => [ 40 "exp_time", # Exposure time 41 ### "time" # Time of exposure --- not yet implemented 42 ]; 43 43 44 44 if (scalar @ARGV == 0 || scalar @ARGV >= 2) { … … 46 46 "Usage: $0 EXP_ID\n\n"; 47 47 } 48 my $expid = shift @ARGV; # Exposure id 48 49 49 50 50 51 # Look for commands we need 51 52 my $missing_tools; 52 my $p0 search = can_run('p0search')53 or (warn "can't find p0 search" and $missing_tools = 1);53 my $p0tool = can_run('p0tool') 54 or (warn "can't find p0tool" and $missing_tools = 1); 54 55 my $ppStats = can_run('ppStats') 55 56 or (warn "can't find ppStats" and $missing_tools = 1); 56 57 die "Can't find required tools.\n" if $missing_tools; 57 58 58 my $expid = shift @ARGV; # Exposure id59 60 59 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files 61 60 62 # Get the list of filenames63 my $ filenames; # Hash of input files, with their background and stdev61 # Get the list of imfiles 62 my $imfiles; 64 63 { 65 my $command = "$p0 search -pendingimfile -exp_id $expid";64 my $command = "$p0tool -rawimfile -exp_id $expid"; 66 65 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 67 run(command => $command, verbose => 0);68 die "Unable to perform p0 searchon exposure id $expid: $error_code\n" if not $success;69 my $ parsed= $mdcParser->parse(join "", @$stdout_buf); # Parsed metadata70 $ filenames = mdFileInfo($parsed); # Get filenames from the output66 run(command => $command, verbose => 1); 67 die "Unable to perform p0tool on exposure id $expid: $error_code\n" if not $success; 68 my $metadata = $mdcParser->parse(join "", @$stdout_buf); # Parsed metadata 69 $imfiles = parse_md_list($metadata); # Data for imfiles 71 70 } 72 71 73 my %values; # Values to return 74 foreach my $fileName (keys %$filenames) { 75 my $command = "$ppStats $fileName -recipe PPSTATS " . RECIPE; # Command to run 76 print "Executing: $command\n"; 77 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 78 run(command => $command, verbose => 0); 79 die "Unable to perform ppStats on $fileName: $error_code\n" if not $success; 80 my $parsed = $mdcParser->parse(join "", @$stdout_buf); # Parsed metadata 81 82 mdParse($parsed, \%values); 72 # Process the values 73 my %values; # Values to update 74 foreach my $variable (@{VARIABLES()}) { 75 $values{$variable} = []; 76 } 77 my @backgrounds; # Array of backgrounds 78 my @stdevs; # Array of background standard deviations\ 79 my $obsType; # Observation type 80 81 foreach my $imfile (@$imfiles) { 82 foreach my $constant (@{CONSTANTS()}) { 83 my $value = get_value($imfile, $constant); # Value for imfile 84 if (not defined $values{$constant}) { 85 $values{$constant} = $value; 86 } elsif ($values{$constant} != $value) { 87 die "Value of $constant for " . $imfile->{PHASE0_CLASSID} . 88 " doesn't match previous value.\n"; 89 } 90 } 91 92 foreach my $variable (@{VARIABLES()}) { 93 my $value = get_value($imfile, $variable); # Value for imfile 94 my $array = $values{$variable}; # Array of data 95 push @$array, $value; 96 } 97 98 my $bg = get_value($imfile, PHASE0_BG()); 99 push @backgrounds, $bg; 100 101 my $stdev = get_value($imfile, PHASE0_BG_MEAN_STDEV()); 102 push @stdevs, $stdev; 103 104 my $type = get_value($imfile, TYPE()); 105 if (not defined $obsType) { 106 $obsType = $type; 107 } elsif ($obsType ne $type) { 108 die "Observation types differ.\n"; 109 } 83 110 } 84 111 85 my $command = "$p0search -updateexp"; # Command to execute to update exposure parameters 112 # Output results to the database 113 { 114 my $command = "$p0tool -updateexp"; # Command to execute to update exposure parameters 115 116 # Add the values of interest 117 foreach my $constant (@{CONSTANTS()}) { 118 $command .= " -" . $constant . " " . $values{$constant}; 119 } 120 foreach my $variable (@{VARIABLES()}) { 121 my $array = $values{$variable}; # Array of values 122 my $stats = Statistics::Descriptive->new; # Statistics calculator 123 $stats->add_data(@$array); 124 $command .= " -" . $variable . " " . $stats->mean(); 125 } 86 126 87 foreach my $constant (keys %{CONSTANTS()}) { 88 if (not defined $values{$constant}) { 89 die "Couldn't find value for $constant.\n"; 127 # Add the statistics 128 { 129 my $stats = Statistics::Descriptive->new; # Statistics calculator 130 $stats->add_data(@backgrounds); 131 $command .= " -" . PHASE0_BG() . " " . $stats->mean(); 132 $command .= " -" . PHASE0_BG_STDEV() . " " . $stats->standard_deviation(); 90 133 } 91 my $switch = CONSTANTS->{$constant}; # The switch to use on the value 92 my $value = $values{$constant}; # The value of interest 93 $command .= " $switch $value"; 134 { 135 my $stats = Statistics::Descriptive->new; # Statistics calculator 136 $stats->add_data(@stdevs); 137 $command .= " -" . PHASE0_BG_MEAN_STDEV() . " " . $stats->mean(); 138 } 139 140 # Add the detrend flag 141 foreach my $detrendType (@{DETRENDS()}) { 142 if (lc($obsType) eq lc($detrendType)) { 143 $command .= " " . DETREND_FLAG; 144 last; 145 } 146 } 147 148 print "$command\n"; 149 150 # my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 151 # run(command => $command, verbose => 1); 152 # die "Unable to run phase0 update for $expid: $error_code\n" if not $success; 94 153 } 95 96 foreach my $variable (keys %{VARIABLES()}) {97 if (not defined $values{$variable}) {98 die "Couldn't find value for $variable.\n";99 }100 my $switch = VARIABLES->{$variable}; # The switch to use on the value101 # Take the mean102 my $value = $values{$variable}->{value} / $values{$variable}->{num}; # Value of interest103 $command .= " $switch $value";104 }105 106 # Check if it's a detrend107 if ($values{TYPE}) {108 $command .= " " . DETREND_FLAG;109 }110 111 ### Now to do the background: mean background, SD of backgrounds, mean of background SDs112 my $bgMean = 0; # Mean background113 my $bgsdMean = 0; # Mean of background SDs114 foreach my $filename (keys %$filenames) {115 my $stats = $filenames->{$filename}; # Statistics for that file116 $bgMean += $stats->{bg};117 $bgsdMean += $stats->{bgsd};118 }119 $bgMean /= scalar(keys %$filenames);120 $bgsdMean /= scalar(keys %$filenames);121 my $bgSD = 0; # SD of backgrounds122 foreach my $filename (keys %$filenames) {123 my $stats = $filenames->{$filename}; # Statistics for that file124 $bgSD += ($stats->{bg} - $bgMean)**2;125 }126 $bgSD = sqrt($bgSD / (scalar(keys %$filenames) - 1));127 128 $command .= " " . EXP_BGMEAN() . " $bgMean " . EXP_BGSD() . " $bgSD " . EXP_BGSDMEAN() . " $bgsdMean";129 130 print "$command";131 132 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =133 run(command => $command, verbose => 0);134 die "Unable to run phase0 update for $expid: $error_code\n" if not $success;135 136 154 137 155 ### Pau. 138 156 139 157 140 # Parse a metadata containing a list of files; return a hash keyed by the URI, containing bg and sd141 sub mdFileInfo142 { 143 my $ mdItemArray = shift; # Array of metadata items from "p0search -pendingimfile"158 # Get the value for a particular imfile, along with a strong check for its existence 159 sub get_value { 160 my $imfile = shift; # The hash of values 161 my $name = shift; # The name of the value to check 144 162 145 my %uriHash; # Hash of URIs to return 146 IMFILE: foreach my $mdItem (@$mdItemArray) { 147 die "Unexpected format for filenames: does not consist solely of METADATAs.\n" 148 if $mdItem->{class} ne "metadata"; 149 my %imfileInfo = values2hash($mdItem->{value}); # File information 150 die "Unexpected format for filenames: METADATAs do not contain " . IMFILE_URI . " element.\n" 151 if not defined $imfileInfo{IMFILE_URI}; 152 die "Unexpected format for filenames: METADATAs do not contain " . IMFILE_BG . " element.\n" 153 if not defined $imfileInfo{IMFILE_BG}; 154 die "Unexpected format for filenames: METADATAs do not contain " . IMFILE_BGSD . " element.\n" 155 if not defined $imfileInfo{IMFILE_BGSD}; 156 $uriHash{$imfileInfo{IMFILE_URI}} = { 'bg' => $imfileInfo{IMFILE_BG}, 157 'bgsd' => $imfileInfo{IMFILE_BGSD} 158 }; 159 } 160 161 return \%uriHash; 163 my $source = $imfile->{PHASE0_CLASSID()}; # Where it comes from 164 die "Couldn't find value of $name for class_id=$source\n" if not defined $imfile->{$name}; 165 return $imfile->{$name}; 162 166 } 163 164 165 # Parse a metadata, looking for the appropriate values166 sub mdParse167 {168 my $mdItemArray = shift; # Array of metadata items169 my $values = shift; # The values170 171 MDITEM: foreach my $mdItem (@$mdItemArray) {172 # Recurse if required173 if ($mdItem->{class} eq "metadata") {174 mdParse($mdItem->{value}, $values);175 return;176 }177 178 return if not defined $mdItem->{name};179 180 if ($mdItem->{name} eq TYPE) {181 foreach my $type (DETRENDS) {182 if (lc($mdItem->{value}) eq lc($type)) {183 $values->{TYPE} = 1;184 next MDITEM;185 }186 }187 # If we got here, it's not a detrend188 $values->{TYPE} = 0;189 next MDITEM;190 }191 192 foreach my $constant (keys %{CONSTANTS()}) {193 if ($mdItem->{name} eq $constant) {194 if (defined $values->{$constant} and $mdItem->{value} ne $values->{$constant}) {195 die "Differing values found for $constant\n";196 }197 $values->{$constant} = $mdItem->{value};198 next MDITEM;199 }200 }201 202 foreach my $variable (keys %{VARIABLES()}) {203 if ($mdItem->{name} eq $variable) {204 $values->{$variable}->{value} += $mdItem->{value};205 $values->{$variable}->{num}++;206 next MDITEM;207 }208 }209 }210 }211 212 213 # Convert values to a hash214 sub values2hash215 {216 my $values = shift;217 218 my %hash;219 foreach my $data (@$values) {220 $hash{$data->{name}} = $data->{value};221 }222 223 return \%hash;224 } -
trunk/ippScripts/scripts/phase0imfile.pl
r8256 r8309 6 6 use IPC::Cmd qw( can_run run ); 7 7 use PS::IPP::Metadata::Config; 8 use PS::IPP::Metadata::Stats; 8 9 use Data::Dumper; 9 10 10 use constant RECIPE => "PPSTATS_PHASE0 _IMFILE"; # Recipe to use for ppStats11 use constant RECIPE => "PPSTATS_PHASE0"; # Recipe to use for ppStats 11 12 12 # Switches for p0search 13 use constant P0SEARCH_MODE => '-updateimfile'; # Mode for p0search 14 use constant P0SEARCH_EXPID => '-exp_id'; # Switch to specify the exposure id 15 use constant P0SEARCH_CLASSID => '-class_id'; # Switch to specify the class id 16 use constant P0SEARCH_BG => '-bg'; # Switch to add the background 17 use constant P0SEARCH_BGSD => '-background_stdev'; # Switch to add the bg stdev 18 use constant P0SEARCH_EXPTIME => '-exp_time'; # Switch to add the exposure time 13 # These values should be constant for all components 14 # The key is the name in the ppStats output; the value is the switch for "phase0 -updateexp" 15 use constant CONSTANTS => { 16 "FPA.FILTER" => "-filter", # Filter used 17 "FPA.AIRMASS" => "-airmass", # Airmass 18 "FPA.RA" => "-ra", # Right ascension 19 "FPA.DEC" => "-decl", # Declination 20 "TELALT" => "-alt", # Altitude 21 "TELAZ" => "-az", # Azimuth 22 "DETTEM" => "-ccd_temp", # CCD temperature 23 "FPA.POSANGLE" => "-posang", # Position angle 24 }; 19 25 26 # These values may vary across components; we will take the average 27 # The key is the name in the ppStats output; the value is the switch for "phase0 -updateexp" 28 use constant VARIABLES => { 29 "CELL.EXPOSURE" => "-exp_time" # Exposure time 30 }; 31 32 33 # Switches for p0tool 34 use constant P0TOOL_MODE => '-updateimfile'; # Mode for p0tool 35 use constant P0TOOL_EXPID => '-exp_id'; # Switch to specify the exposure id 36 use constant P0TOOL_CLASSID => '-class_id'; # Switch to specify the class id 37 use constant P0TOOL_BG_MEAN => '-bg'; # Switch to add the background 38 use constant P0TOOL_BG_STDEV => '-bg_stdev'; # Switch to add the bg stdev 39 use constant P0TOOL_BG_MEAN_STDEV => '-bg_mean_stdev'; # Switch to add the bg mean stdev 40 41 # Parse command-line arguments 20 42 if (scalar @ARGV == 0 || scalar @ARGV > 3) { 21 43 die "Perform phase 0 processing at the imfile level.\n\n" . … … 27 49 my $file = shift @ARGV; # Input filename 28 50 29 # Look for commands we need 51 52 # Look for programs we need 30 53 my $missing_tools; 31 my $p0search = can_run('p0search') 32 or (warn "can't find p0search" and $missing_tools = 1); 33 my $ppStats = can_run('ppStats') 34 or (warn "can't find ppStats" and $missing_tools = 1); 54 my $p0tool = can_run('p0tool') or (warn "Can't find p0tool" and $missing_tools = 1); 55 my $ppStats = can_run('ppStats') or (warn "Can't find ppStats" and $missing_tools = 1); 35 56 die "Can't find required tools.\n" if $missing_tools; 36 57 37 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files38 39 58 # Run ppStats on the input file 40 my $ parsed; # Parsed metadata59 my $stats; 41 60 { 42 61 my $command = "$ppStats $file -recipe PPSTATS " . RECIPE; # Command to run ppStats … … 44 63 run(command => $command, verbose => 1); 45 64 die "Unable to perform ppStats on exposure id $expid: $error_code\n" if not $success; 46 $parsed = $mdcParser->parse(join "", @$stdout_buf); 65 66 # Parse the output 67 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files 68 my $metadata = $mdcParser->parse(join "", @$stdout_buf); 69 my @constants = keys %{CONSTANTS()}; # List of constants to parse out 70 my @variables = keys %{VARIABLES()}; # List of variables to parse out 71 $stats = PS::IPP::Metadata::Stats->new(\@constants, \@variables); # Stats parser 72 $stats->parse($metadata) or die "Unable to find all values.\n"; 47 73 } 48 49 my $bg = 0; # Mean background level50 my $sd = 0; # Mean standard deviation51 my $expTime = 0; # Mean exposure time52 my $bgNum = 0; # Number of background entries53 my $sdNum = 0; # Number of standard deviation entries54 my $expTimeNum = 0; # Number of exposure time entries55 foreach my $fpaItem (@$parsed) {56 if ($fpaItem->{class} eq "metadata") {57 my $chipName = $fpaItem->{name}; # Name of chip58 my $chipData = $fpaItem->{value}; # Chip-level data59 foreach my $chipItem (@$chipData) {60 if ($chipItem->{class} eq "metadata") {61 my $cellName = $chipItem->{name}; # Name of cell62 my $cellData = $chipItem->{value}; # Cell-level data63 foreach my $cellItem (@$cellData) {64 if ($cellItem->{name} =~ /^(SAMPLE|ROBUST|FITTED|CLIPPED)/) {65 # It's a statistic of some sort66 if ($cellItem->{name} =~ /STDEV$/) {67 $sd += $cellItem->{value};68 $sdNum++;69 } else {70 $bg += $cellItem->{value};71 $bgNum++;72 }73 } elsif ($cellItem->{name} =~ /CELL\.EXPOSURE/) {74 $expTime += $cellItem->{value};75 $expTimeNum++;76 }77 }78 }79 }80 }81 }82 die "Unable to find statistics.\n" if ($bgNum == 0 or $sdNum == 0);83 $bg /= $bgNum;84 $sd /= $sdNum;85 die "Unable to find exposure times.\n" if ($expTimeNum == 0);86 $expTime /= $expTimeNum;87 74 88 75 # Push the results into the database 89 76 { 90 my $command = "$p0search " . P0SEARCH_MODE . " " . P0SEARCH_EXPID() . " $expid " . 91 P0SEARCH_CLASSID() . " $classid " . P0SEARCH_BG() ." $bg " . P0SEARCH_BGSD() ." $sd " . 92 P0SEARCH_EXPTIME() . " $expTime"; # Command to run p0search 77 my $command = $p0tool . " " . P0TOOL_MODE() . " " . P0TOOL_EXPID() . " " . $expid . " " . 78 P0TOOL_CLASSID() . " " . $classid; # Command to run p0tool 79 80 foreach my $constant (keys %{CONSTANTS()}) { 81 $command .= " " . CONSTANTS->{$constant} . " " . ($stats->data($constant))->{value}; 82 } 83 foreach my $variable (keys %{VARIABLES()}) { 84 # Just use the mean value 85 $command .= " " . VARIABLES->{$variable} . " " . ($stats->data($variable))->{mean}; 86 } 87 88 $command .= " " . P0TOOL_BG_MEAN() . " " . $stats->bg_mean(); 89 $command .= " " . P0TOOL_BG_STDEV() . " " . $stats->bg_stdev(); 90 $command .= " " . P0TOOL_BG_MEAN_STDEV() . " " . $stats->bg_mean_stdev(); 91 93 92 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 94 93 run(command => $command, verbose => 1); 95 die "Unable to perform p0 search-updateimfile: $error_code\n" if not $success;94 die "Unable to perform p0tool -updateimfile: $error_code\n" if not $success; 96 95 } 96 97 # Pau. 98 99 __END__
Note:
See TracChangeset
for help on using the changeset viewer.
