Misc(3pm)
β¨ Misc(3pm) β¨
okay bestie, this is the Misc(3pm) page from User Contributed Perl Documentation β and it's serving PDL::IO::Misc, the misc IO routines for PDL. Let's get into it π
πΈ NAME πΈ
PDL::IO::Misc β misc IO routines for PDL
π¦ DESCRIPTION π¦
Some basic I/O functionality: FITS, tables, byte-swapping
β¨ SYNOPSIS β¨
use PDL::IO::Misc;
π₯ FUNCTIONS π₯
π« bswap2
Signature: (x(); )
Swaps pairs of bytes in argument x()
bswap2 does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
π« bswap4
Signature: (x(); )
Swaps quads of bytes in argument x()
bswap4 does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
π« bswap8
Signature: (x(); )
Swaps octets of bytes in argument x()
bswap8 does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
π rcols
Read specified ASCII cols from a file into ndarrays and perl arrays (also see rgrep).
Usage:
($x,$y,...) = rcols( *HANDLE|"filename", { EXCLUDE => '/^!/' }, $col1, $col2, ... )
$x = rcols( *HANDLE|"filename", { EXCLUDE => '/^!/' }, [] )
($x,$y,...) = rcols( *HANDLE|"filename", $col1, $col2, ..., { EXCLUDE => '/^!/' } )
($x,$y,...) = rcols( *HANDLE|"filename", "/foo/", $col1, $col2, ... )
For each column number specified, a 1D output PDL will be generated. Anonymous arrays of column numbers generate 2D output ndarrays with dim0 for the column data and dim1 equal to the number of columns in the anonymous array(s).
An empty anonymous array as column specification will produce a single output data ndarray with dim(1) equal to the number of columns available.
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 a hash reference. This reference can be given as either the second or last argument.
The default behaviour is to ignore lines beginning with a # character and lines that only consist of whitespace. Options exist to only read from lines that match, or do not match, supplied patterns, and to set the types of the created ndarrays.
Can take file name or *HANDLE, and if no explicit column numbers are specified, all are assumed. For the allowed types, see Datatype_conversions in PDL::Core.
Options (case insensitive):
EXCLUDEorIGNOREβ ignore lines matching this pattern (default'/^#/').INCLUDEorKEEPβ only use lines which match this pattern (default'').LINESβ a string pattern specifying which line numbers to use. Line numbers start at 0 and the syntax is'a:b:c'to use every c'th matching line between a and b (default'').DEFTYPEβ default data type for stored data (if not specified, use the type stored in$PDL::IO::Misc::deftype, which starts off asdouble).TYPESβ reference to an array of data types, one element for each column to be read in. Any missing columns use theDEFTYPEvalue (default[]).COLSEPβ splits on this string/pattern/qr{} between columns of data. Defaults to$PDL::IO::Misc::defcolsep.PERLCOLSβ an array of column numbers which are to be read into perl arrays rather than ndarrays. Any columns not specified in the explicit list of columns to read will be returned after the explicit columns. (defaultundef).COLIDSβ if defined to an array reference, it will be assigned the column ID values obtained by splitting the first line of the file in the identical fashion to the column data.CHUNKSIZEβ the number of input data elements to batch together before appending to each output data ndarray (Default value is100). IfCHUNKSIZEis greater than the number of lines of data to read, the entire file is slurped in, lines split, and perl lists of column data are generated. At the end, effectivelypdl(@column_data)produces any result ndarrays.VERBOSEβ be verbose about IO processing (default$PDL::vebose)
For example:
$x = PDL->rcols 'file1'; # file1 has only one column of data
$x = PDL->rcols 'file2', []; # file2 can have multiple columns, still 1 ndarray output
# (empty array ref spec means all possible data fields)
($x,$y) = rcols 'table.csv', { COLSEP => ',' }; # read CSV data file
($x,$y) = rcols *STDOUT; # default separator for lines like '32 24'
# read in lines containing the string foo, where the first
# example also ignores lines that begin with a # character.
($x,$y,$z) = rcols 'file2', 0,4,5, { INCLUDE => '/foo/' };
($x,$y,$z) = rcols 'file2', 0,4,5, { INCLUDE => '/foo/', EXCLUDE => '' };
# ignore the first 27 lines of the file, reading in as ushort's
($x,$y) = rcols 'file3', { LINES => '27:-1', DEFTYPE => ushort };
($x,$y) = rcols 'file3', { LINES => '27:', TYPES => [ ushort, ushort ] };
# read in the first column as a perl array and the next two as ndarrays
# with the perl column returned after the ndarray outputs
($x,$y,$name) = rcols 'file4', 1, 2 , { PERLCOLS => [ 0 ] };
printf "Number of names read in = %d\n", 1 + $#$name;
# read in the first column as a perl array and the next two as ndarrays
# with PERLCOLS changing the type of the first returned value to perl list ref
($name,$x,$y) = rcols 'file4', 0, 1, 2, { PERLCOLS => [ 0 ] };
# read in the first column as a perl array returned first followed by the
# the next two data columns in the file as a single Nx2 ndarray
($name,$xy) = rcols 'file4', 0, [1, 2], { PERLCOLS => [ 0 ] };
NOTES:
- Quotes are required on patterns or use the
qr{}quote regexp syntax. - Columns are separated by whitespace by default, use the
COLSEPoption separator to specify an alternate split pattern or string or specify an alternate default separator by setting$PDL::IO::Misc::defcolsep. - Legacy support is present to use
$PDL::IO::Misc::colsepto set the column separator but$PDL::IO::Misc::colsepis not defined by default. If you set the variable to a defined value it will get picked up. LINES => '-1:0:3'may not work as you expect, since lines are skipped when read in, then the whole array reversed.- For consistency with
wcolsandrcols1D usage, column data is loaded into the rows of the pdls (i.e.,dim(0)is the elements read per column in the file anddim(1)is the number of columns of data read.
π wcols
Write ASCII columns into file from 1D or 2D ndarrays and/or 1D listrefs efficiently.
Can take file name or *HANDLE, and if no file/filehandle is given defaults to STDOUT.
Options (case insensitive):
HEADERβ prints this string before the data. If the string is not terminated by a newline, one is added. (default'').COLSEPβ prints this string between columns of data. Defaults to$PDL::IO::Misc::defcolsep.FORMATβ A printf-style format string that is cycled through column output for user controlled formatting.
Usage:
Usage: wcols $data1, $data2, $data3,..., *HANDLE|"outfile", [\%options]; # or
wcols $format_string, $data1, $data2, $data3,..., *HANDLE|"outfile", [\%options];
where the $dataN args are either 1D ndarrays, 1D perl array refs, or 2D ndarrays (as might be returned from rcols() with the [] column syntax and/or using the PERLCOLS option). dim(0) of all ndarrays written must be the same size. The printf-style $format_string, if given, overrides any FORMAT key settings in the option hash.
e.g.,
$x = random(4); $y = ones(4);
wcols $x, $y+2, 'foo.dat';
wcols $x, $y+2, *STDERR;
wcols $x, $y+2, '|wc';
$x = sequence(3); $y = zeros(3); $c = random(3);
wcols $x,$y,$c; # Orthogonal version of 'print $x,$y,$c' :-)
wcols "%10.3f", $x,$y; # Formatted
wcols "%10.3f %10.5g", $x,$y; # Individual column formatting
$x = sequence(3); $y = zeros(3); $units = [ 'm/sec', 'kg', 'MPH' ];
wcols $x,$y, { HEADER => "# x y" };
wcols $x,$y, { Header => "# x y", Colsep => ', ' }; # case insensitive option names!
wcols " %4.1f %4.1f %s",$x,$y,$units, { header => "# Day Time Units" };
$a52 = sequence(5,2); $y = ones(5); $c = [ 1, 2, 4 ];
wcols $a52; # now can write out 2D pdls (2 columns data in output)
wcols $y, $a52, $c # ...and mix and match with 1D listrefs as well
NOTES:
- Columns are separated by whitespace by default, use
$PDL::IO::Misc::defcolsepto modify the default value or theCOLSEPoption - Support for the
$PDL::IO::Misc::colsepglobal value of PDL-2.4.6 and earlier is maintained but the initial value of the global is undef until you set it. The value will be then be picked up and used as if defcolsep were specified. - Dim 0 corresponds to the column data dimension for both
rcolsandwcols. This makeswcolsthe reverse operation ofrcols.
π§ swcols
generate string list from sprintf format specifier and a list of ndarrays
swcols takes an (optional) format specifier of the printf sort and a list of 1D ndarrays as input. It returns a perl array (or array reference if called in scalar context) where each element of the array is the string generated by printing the corresponding element of the ndarray(s) using the format specified. If no format is specified it uses the default print format.
Usage:
Usage: @str = swcols format, pdl1,pdl2,pdl3,...;
or $str = swcols format, pdl1,pdl2,pdl3,...;
π rgrep
Read columns into ndarrays using full regexp pattern matching.
Options:
UNDEFINEDβ This option determines what will be done for undefined values. For instance when reading a comma-separated file of the type1,2,,4where the,,indicates a missing value. The default value is to assign$PDL::undefvalto undefined values, but ifUNDEFINEDis set this is used instead. This would normally be set to a number, but if it is set toBadand PDL is compiled with Badvalue support (seePDL::Bad) then undefined values are set to the appropriate badvalue and the column is marked as bad.DEFTYPEβ Sets the default type of the columns - see the documentation forrcols()TYPESβ A reference to a Perl array with types for each column - see the documentation forrcols()BUFFERSIZEβ The number of lines to extend the ndarray by. It might speed up the reading a little bit by setting this to the number of lines in the file, but in generalrasc()is a better choice
Usage:
($x,$y,...) = rgrep(sub, *HANDLE|"filename")
e.g.
($x,$y) = rgrep {/Foo (.*) Bar (.*) Mumble/} $file;
okay bestie, that's part 1 β part 2 is still loading, so don't go anywhere ππ
i.e. the vectors $x and $y get the progressive values of $1, $2 etc. cute, right? β¨
isbigendian π§
Okay so this one is literally a vibe check for your machine's byte order β it figures out the endianness of the machine and returns 0 or 1 accordingly. That's it, that's the whole function. No cap. π
rasc π₯
A simple little function to slurp in ASCII numbers, and it does it quite quickly bestie, lowkey. Fair warning tho: the error handling is marginal (to nonexistent) π so don't come crying to me if it eats garbage.
$pdl->rasc("filename"|FILEHANDLE [,$noElements]);
Where, in detail, because we don't gatekeep:
filenameβ the name of the ASCII file to read, or an open file handle.$noElementsβ the optional number of elements in the file to read. If it's not present, all of the file will be read to fill up$pdl.$pdlcan be of typefloatordouble(for more precision). π«Ά
Here's the example, and yes it's giving exactly what you think it's giving:
# (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');
rcube π§β¨
Read a whole list of files directly into one big data cube, because efficiency is the moment. π₯
$cube = rcube \&reader_function, @files;
$cube = rcube \&rfits, glob("*.fits");
So this IO function allows direct reading of files into a large data cube. Obviously one could just use cat() β but bestie, this is more memory efficient. π
The reading function (e.g. rfits, readfraw) is passed as a reference, and the files are the arguments. That's the whole recipe. π
The cube gets created with the same X,Y dims and datatype as the first image specified. The Z dim is simply the number of images. Iconic behavior honestly. π
πΈ AUTHOR πΈ
Copyright (C) Karl Glazebrook 1997, Craig DeForest 2001, 2003, and Chris Marshall 2010. 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.
And that's a wrap on Misc(3pm) from the perl v5.38.2 era, dated 2024-04-10 π go forth and cube your data, bestie, you got this π¦βπ
π¦ where this page comes from
- ubuntu noble: pdl 1:2.085-1ubuntu1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms