PDL::IO::Misc - misc IO routines for PDL
A mixture of basic I/O functionality
use PDL::IO::Misc;
Simple piddle FITS reader.
$pdl = rfits('file.fits');
Suffix magic:
# Automatically uncompress via gunzip pipe $pdl = rfits('file.fits.gz'); # Automatically uncompress via uncompress pipe $pdl = rfits('file.fits.Z');
FITS Headers stored in piddle and can be retrived with $a->gethdr.
This header is a reference to a hash where the hash keys are the keywords in the FITS header. It is important to note that for strings, the surrounding quotes are kept to ensure that strings that look like numbers are kept as strings. This is also of importance if you create your own header information and you want the value to be printed out as a string.
Comments in headers are stored as $$h{COMMENT}{<Keyword>} where
$h
is the header retrieved with $a->gethdr. History entries
in the header are stored as $$h{HISTORY}, which is an anonymous array for
each HISTORY entry in the header.
Simple piddle FITS writer
wfits $pdl, 'filename.fits', [$BITPIX]; $pdl->wfits('foo.fits',-32);
Suffix magic:
# Automatically compress through pipe to gzip wfits $pdl, 'filename.fits.gz'; # Automatically compress through pipe to compress wfits $pdl, 'filename.fits.Z';
$BITPIX
is optional and coerces the output format.
Read ASCII whitespaced cols from file into piddles efficiently.
There are two calling conventions - the old version, where a pattern can be specified after the filename/handle, and the new version where options are given as as hash reference. This reference can be given as either the second or last argument.
The default behaviour is to read in from all lines unless they begin with a # character, or if a pattern is given, read in data from lines matching the pattern.
Options:
EXCLUDE or IGNORE - ignore lines matching this pattern (default '/^#/'). INCLUDE or KEEP - only use lines which match this pattern (default ''). LINES - which line numbers to use. Line numbers start at 0 and the syntax is 'a:b:c' to use every c'th line between a and b (default '').
Can take file name or *HANDLE, and if no columns are specified, all are assumed.
Usage: ($x,$y,...) = rcols( *HANDLE|"filename", { EXCLUDE => '/^!/' }, $col1, $col2, ... ) ($x,$y,...) = rcols( *HANDLE|"filename", $col1, $col2, ..., { EXCLUDE => '/^!/' } ) ($x,$y,...) = rcols( *HANDLE|"filename", "/foo/", $col1, $col2, ... ) e.g.,
$x = PDL->rcols 'file1'; ($x,$y) = rcols 'file1'; ($x,$y) = rcols *STDOUT;
# to only read lines containing the string foo that do not # begin with # (since EXCLUDE defaults to '^#') ($x,$y,$z) = rcols 'file2', 0,4,5, { INCLUDE => '/foo/' };
# to only read in lines containing the string foo ($x,$y,$z) = rcols 'file2', 0,4,5, { INCLUDE => '/foo/', EXCLUDE => '' }; # or, using the old-style ($x,$y,$z) = rcols 'file2', "/foo/",0,4,5;
# ignore the first 27 lines of the file ($x,$y) = rcols 'file3', { LINES => '27:-1' } ($x,$y) = rcols 'file3', { LINES => '27:' }
Notes:
1. Quotes are required on patterns.
2. Columns are separated by whitespace by default, use $PDL::IO::Misc::colsep to specify an alternate separator.
3. To remove a particular check set its value to ''.
4. Although the LINES option looks remarkably similar to the format of
slice(),
it works on line numbers - not the resulting array -
so both '0:-1' and '0:-2' give the same result IF the last line starts with
a '#'.
Write ASCII whitespaced cols into file from piddles efficiently.
If no columns are specified all are assumed. Will optionally only process lines matching a pattern. Can take file name or *HANDLE, e.g.
Usage: wcols $piddle1, $piddle2,..., *HANDLE|"outfile";
e.g.,
wcols $x, $y+2, 'foo.dat'; wcols $x, $y+2, *STDERR; wcols $x, $y+2, '|wc'; wcols $a,$b,$c; # Orthogonal version of 'print $a,$b,$c' :-)
wcols "%10.3f", $a,$b; # Formatted wcols "%10.3f %10.5g", $a,$b; # Individual column formatting
Note: columns are separated by whitespace by default, use $PDL::IO::Misc::colsep to specify an alternate separator.
If no file/filehandle is given defaults to STDOUT
Read columns into piddles using full regexp pattern matching.
Usage
($x,$y,...) = rgrep(sub, *HANDLE|"filename")
e.g.
($a,$b) = rgrep {/Foo (.*) Bar (.*) Mumble/} $file;
i.e. the vectors $a and $b
get the progressive values of $1
, $2
etc.
Read a FIGARO/NDF format file.
Requires non-PDL DSA module. Contact Frossie (frossie@jach.hawaii.edu) Usage:
([$xaxis],$data) = rdsa($file)
$a = rdsa 'file.sdf'
Not yet tested with PDL-1.9X versions
Determine endianness of machine - returns 0 or 1 accordingly
Simple function to slurp in ASCII numbers quite quickly, although error handling is marginal (to nonexistent).
$pdl->rasc("filename" [,$noElements]); Where: filename is the name of the ASCII file to read $noElements is the optional number of elements in the file to read. (If not present, all of the file will be read to fill up $pdl)
# (test.num is an ascii file with 20 numbers. One number per line.) $in = PDL->null; $num = 20; $in->rasc('test.num',20); $imm = zeroes(float,20,2); $imm->rasc('test.num');
Read list of files directly into a large data cube (for efficiency)
$cube = rcube \&reader_function, @files;
$cube = rcube \&rfits, glob("*.fits");
This IO function allows direct reading of files into a large data cube,
Obviously one could use cat()
but this is more memory
efficient.
The reading function (e.g. rfits, readfraw) (passed as a reference) and files are the arguments.
The cube is created as the same X,Y dims and datatype as the first image specified. The Z dim is simply the number of images.
Signature: ([o] nums(n); int num => n; char* fd)
Internal Function used by rasc.
Signature: ([o]x(); )
Swaps pairs of bytes in argument x()
Signature: ([o]x(); )
Swaps quads of bytes in argument x()
Signature: ([o]x(); )
Swaps octets of bytes in argument x()
Copyright (C) Karl Glazebrook 1997. All rights reserved. There is no warranty. You are allowed to redistribute this software / documentation under certain conditions. For details, see the file COPYING in the PDL distribution. If this file is separated from the PDL distribution, the copyright notice should be included in the file.