Perl as a tool in Astronomy
Making easy jobs easy
Ulrike Heiter
UAO
Click or press Enter or use controls (lower right) to view next slide
Outline
- Introduction
- Basic concepts
- Examples
- Module archive
- Perl Data Language
- Resources for further reading
Introduction
- PERL = Practical Extraction and Report Language
- History of Perl
- 1987: Larry Wall takes features of several Unix tools and merges them into new scripting language: Perl 1.000
- Four more major releases over the next few years, introducing many new features
- Since 1993: current standard Perl 5 - both simple and rich programming language
- Several minor releases of Perl 5 by team of programmers
- Purpose
-
"Perl is designed to make the easy jobs easy, without making the hard jobs impossible."
Quote from "Programming Perl" book
Introduction
- Perl can be used to perform tasks for which
- Shell scripts are not powerful enough
- Shell scripts are not portable enough
- Higher level languages (C, etc.) are not worth the trouble
- Properties
- Readily available (included in most Unix distributions)
- Portable
- Concise
- Programs are easy to read and to maintain, when sticking to some simple guidelines
Basic concepts
- Programs are parsed and compiled before execution
- Free format - amount and kind of whitespace between elements is arbitrary
- Statements listed after one another, separated by ";"
- Comments start with "#" and end at new line
- Variables can be "scalars", "lists" (arrays), or "hashes"
$number = 10; $number = 1.234; $string = 'Uppsala';
@somelist = ('Sirius', 'HD 10000', 'Vega');
$somelist[0] = 'Sirius';
%somehash = ('Sirius', 10, 'HD 10000', 20, 'Vega', 30);
$somehash{'Sirius'} = 10;
Similar to shell scripts, no "main program", no beginning or end statements
Basic concepts
- Calculations are always done in double precision
- Type conversions are done automatically, when necessary
- $_ contains default input and pattern-searching space
- @_ contains parameters passed to subroutine
- Control statements similar to those of C
- Regular expressions (pattern to be matched against string) similar to those in grep, sed, awk, vi, emacs
- Input/Output is done via FILEHANDLES
Example
# Read a list of star names and altitudes from a file,
# print names of selected stars
# Inititalize
@starlist = ();
%altitude = ();
# Read file
read_file(); # subroutine call
# Print selected stars
foreach $star (@starlist) {
if ( select_star($star,$altitude{$star}) ) {
print $star;
}
} # ----- end foreach -----
Example
sub read_file {
open FILE, "< starfile";
while (<FILE>) {
push @starlist, $_;
$altitude{$_} = <FILE>;
}
close FILE;
}
Sirius
20
HD 10000
30
HD 20000
5
Vega
-5
HD 30000
50
Example
sub select_star {
my ($name, $altitude) = @_;
if ( ($name =~ /^HD/) and ($altitude > 10) ) {
return 1; # anything else but "0" or "" is TRUE
}
else {
return 0; # "0" is FALSE
}
}
unix> perl example.pl
HD 10000
HD 30000
Link to starfile
More examples
- Writing into a command's standard input:
open PRINTER, "| lpr");
print PRINTER "some stuff to print\n";
close PRINTER;
- Reading a command's standard output:
open DIR, "ls some_directory |");
while (<DIR>) { } # do something with file
close DIR;
More examples
- Collection of various subroutines:
libUH.pm
e.g. sub common compares two sets of numbers and returns the set of numbers common to both sets within a certain tolerance
- Collection of Perl scripts:
libUH.html
Ulrike's Homepage → Toolbox → Various Perl scripts
- histogram.pl calculates frequency distribution for a dataset
- print_csvlist.pl prints selected fields from comma separated list
Stellar Model Grid Tool (SMGT)
- Object-oriented set of Perl packages, which
calculates grids of iterative stellar models
- Written by W. Schmidt at Univ. Vienna in 1999
- Currently only ATLAS9 atmosphere models (R. Kurucz) implemented
- Manages input/output of ATLAS9 processes
- Generates initialization models and estimates number of iterations required to satisfy given convergence criteria
- Used for study of convection treatment in ATLAS9 models (Heiter et al. 2002, A&A 392, 619)
Perl Modules
- Packages of user contributed subroutines, extending basic features
- Collected at CPAN (Comprehensive Perl Archive Network)
- Included in scripts with
use Modulename;
- Examples:
- Statistics::Descriptive - basic functions used in descriptive statistics
- Astro::FITS::CFITSIO - Perl interface to William Pence's cfitsio subroutine library
- Astro::STSDAS::Table - Access STSDAS format table files
- Astro::Cosmology - Set of routines to calculate a number of cosmological quantities based on distance and time
Perl Data Language (PDL)s
- Perl extension for efficient storage and manipulation of large N-dimensional data sets
- Example: variable $a holding a 1024x1024 floating point image
→ stored in 4MB of memory
→ expressions like $a=sqrt($a)+2 manipulate whole image in a few milliseconds
- Includes matrix multiplication, slicing and sectioning operators
- Input/Output functions for various data formats
- Visualisation using existing graphics libraries, e.g. PGPLOT
- Maintainers are astronomers and software engineers
Resources for further reading