11.07.2015 Views

PyClimate 1.1. A set of C and Python routines for the ... - Starship

PyClimate 1.1. A set of C and Python routines for the ... - Starship

PyClimate 1.1. A set of C and Python routines for the ... - Starship

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3 IO functions3.1 ASCII files: asciidat.py3.1.1 readdat.pyThe main goal <strong>of</strong> file readdat.py is to provide a <strong>set</strong> <strong>of</strong> functions to h<strong>and</strong>le IO fromASCII files. The ASCII files must be written in <strong>the</strong> way gnuplot files are, but notall <strong>of</strong> <strong>the</strong> gnuplot–compliant files are readable. Each row in <strong>the</strong> file is a row <strong>of</strong> <strong>the</strong>output data. Lines whose first token is a # are comments <strong>for</strong> <strong>the</strong>se functions. No commentsare allowed at <strong>the</strong> tail <strong>of</strong> a line. Empty lines are allowed, but <strong>the</strong>y are discarded<strong>and</strong> not used to separate data<strong>set</strong>s in <strong>the</strong> file. Better support <strong>for</strong> <strong>the</strong>se features existsin o<strong>the</strong>r packages <strong>for</strong> <strong>Python</strong>, like M. Haggerty’s Gnuplot or <strong>the</strong> IO functions in K.Hinsen’s Scientific <strong>Python</strong>. The interest <strong>of</strong> this file is that it allows to read files withcomplex numbers <strong>and</strong> o<strong>the</strong>r data types, a feature that is not supported by <strong>the</strong> mentionedfiles. Complex numbers are written as: real,imag, ,imag or real, in <strong>the</strong> inputfile, with no blank spaces (nor tabs) separating <strong>the</strong> members <strong>of</strong> a complex number. Examples<strong>of</strong> use <strong>of</strong> <strong>the</strong>se <strong>routines</strong> can be found in file exi<strong>of</strong>uncs.pyFunction: def readdat(fname,typecode=None)Reads all <strong>the</strong> data in <strong>the</strong> file fname <strong>and</strong> returns a bidimensional NumPy array <strong>of</strong> <strong>the</strong>same shape as <strong>the</strong> data in <strong>the</strong> original array <strong>and</strong> with a typecode given as Float64 bydefault (Complex64 in case <strong>the</strong>re were complex elements in <strong>the</strong> file). If <strong>the</strong> parametertypecode is explicitly assigned, <strong>the</strong> output array is coerced to that type. If <strong>the</strong> file to beread contains only one row or column <strong>of</strong> data <strong>the</strong> returned array is one–dimensional.Function: def readcol(fname,col=1,typecode=None)In this case, <strong>the</strong> col–eth column is read <strong>and</strong> returned as a one dimensional NumPy array.The numbering <strong>of</strong> columns is not according to <strong>Python</strong>’s scheme, but to gnuplot’sone instead, that is, from 1 to N. The type coercion works as in <strong>the</strong> previous function.Function: def readcols(fname,cols=[1],typecode=None)Similar to readcol, but reads from <strong>the</strong> file fname <strong>and</strong> returns a –dimensionalNumPy array, where is <strong>the</strong> length <strong>of</strong> <strong>the</strong> sequence object cols. The type coercionworks as in <strong>the</strong> previous two functions. The columns are returned in <strong>the</strong> same order as<strong>the</strong>y appear in cols.3.1.2 writedat.pyAs a readdat.py related tool is provided writedat.py. A NumPy array can bedirectly written in a way that can be recovered with readdat.py. This provides aneasy way to pass arrays from one program to ano<strong>the</strong>r with human readable intermediatefiles.Function: def writedat(fname, matrix, header=””, <strong>for</strong>matstring=”%g ”)The name <strong>of</strong> <strong>the</strong> file to be created is passed as <strong>the</strong> first argument fname. If <strong>the</strong> filealready exists it will be overwritten without prompting. One <strong>and</strong> two–dimensional arrayscan be written <strong>and</strong> <strong>the</strong>y are passed as <strong>the</strong> second argument matrix to <strong>the</strong> function.All o<strong>the</strong>r arguments are optional. A header can be specified, but <strong>the</strong> user must take care<strong>of</strong> writing a # at <strong>the</strong> beginning <strong>of</strong> each line. Finally, <strong>the</strong> output <strong>for</strong>mat can be controlledby passing a <strong>for</strong>mat string. The default <strong>for</strong>mat string is ”%g ”.5

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!