| | |
- grads.gacore.GaCore(__builtin__.object)
-
- GaNum
class GaNum(grads.gacore.GaCore) |
| |
This class extends the GrADS client class by providing methods
for data exchange and numerical computations:
_Methods provided:
exp - exports a GrADS expression into a NumPy array, with metada
imp - imports NumPy array (+metadata) into GrADS
eof - compute Empirical Orthogonal Functions (EOFS) from expressions
lsq - least square parameter estimation from expressions |
| |
- Method resolution order:
- GaNum
- grads.gacore.GaCore
- __builtin__.object
Methods defined here:
- eof(self, expr, transf='anomaly', metric='area', keep=None)
- Given a GrADS generalized expression *expr*, calculates Empirical
Orthogonal Functions (EOFS) using Singular Value Decomposition (SVD).
V, d, c = eof(expr)
where
V --- A specialized GrADS Field holding eigenvectors
with *add* offset and *scale* factors to aid
subsequent decomposition in terms of V
d --- NumPy array with eigenvalues
c --- NumPy array with principal components
The other optional parameters are:
transf
Type of pre-processing transform to be applied:
None --- time series as is
anomaly --- remove time mean
z-score --- remove time mean and divide
by standard deviation
metric
Determines whether to scale the timeseries prior
to calculation of the EOFs; this is equivalent to
choosing a particular norm. Acceptable values are:
None --- do not scale
'area' --- multiply by cos(lat), the default
keep
How many eigenvectors to keep:
None --- in this case keep as many vectors as
there are timesteps (nt)
n --- keep "n" eigenvectors
Notice that *expr* on input is a *generalized expression* in the
sense that it can contain a string with a GrADS expression to be
evaluated or a valid GrADS field. See method *exp* for additional
information.
- exp(self, expr)
- Exports GrADS expression *expr*, returning a GrADS Field.
F = exp(expr)
where
F --- GrADS field
Generalized Expressions
=======================
For convenience, *expr* can also be a GrADS Field. In such
cases, the input Field is just returned back. This
*overloading* is useful for writing high level functions that
work equaly on a GrADS expression to be exported or on GrADS
fields already known to python.
- expr(self, expr)
- Evaluates a GrADS expression returning a GrADS Field. This is similar
to the exp() method except that the resulting GaField cannot be
imported back into GrADS. It relies on *gacore* methods eval()
and coords() to retrieve the data and coordinate information.
- imp(self, name, Field)
- Sends a GrADS Field containing a NumPy array and associated
grid information to GrADS, defining it in GrADS as *name*.
Notice that *Field* can be an instance of the GaField
class or a tuple with the (Array,Grid) components.
- lsq(self, y_expr, x_exprs, Bias=False, Mask=None)
- Given a target GrADS expression *y_expr* and a tuple of predictor
GrADS expressions *x_exprs*, returns a NumPy array with linear
regression coefficients
c, info = lsq(y_expr, x_exprs)
where *info* contains information about the minimization:
info.residuals --- sum square of residuals
info.rank --- rank of the predictor matrix
info.s --- singular values of predictor matrix
When Bias=False (default) the residual
y - c[0] * x[:,0] + c[1] * x[:,1] + ... + c[n] * x[:,N-1]
is minimized in the last square sense, where *x* and *y* are
NumPy arrays associated with the following GrADS Fields:
Y = exp(y_expr)
X[:,n] = exp(x_exprs[n]), n = 0, ..., N-1
When Bias=True, the additional predictor array
x[N] = ones(y)
is included in the calculation, resulting in an output array of
size N+1. This is equivalent to allowing for an *intercept* in
the regression equation.
The optional Mask is a 1D logical array with the location of the
data to be included (see compress() method).
On input, all expressions are *generalized expressions* in the
sense that they can contain a string with a GrADS expression to be
evaluated or a valid GrADS field. See method *expr* for additional
information.
Methods inherited from grads.gacore.GaCore:
- __call__ = cmd(self, gacmd, Quiet=False, Block=True)
- Sends a command to GrADS. When Block=True, the output is captured
and can be retrieved by methods rline() and rword().
- __del__(self)
- Sends GrADS the 'quit' command and close the pipes.
- __init__(self, Bin='gradshdf', Echo=True, Opts='', Port=False, Strict=False, Verb=0, Window=None)
- Starts the GrADS process using popen2() function. Optional input
parameters are:
Bin the name of the GrADS binary; you can enter a full path
here, say, "/opt/mygrads/bin/gradshdf", or simply the
name of other executables, e.g., "gradsdods" if this is
on your PATH.
Echo if True output from GrADS will not be echoed on the screen
Port If set to True GrADS will start in portrait mode; default is
to start GrADS in landscape mode.
Opts Use that to send additional options to GrADS, such as -c
Strict By default, it behaves a bit like perl: not raising
exceptions for index errors, etc. If Strict=1, then
exceptions will be raised.
Verb provides information about each GrADS command executed. If
set to zero, it will be real quiet. When Verb=1 the rc code
of failed GrADS commands will be printed. If verb=2 then
the rc for all GrADS commands will be printed.
Window If True GrADS will start with a graphics window; the
default is to start GrADS in batch mode.
- cmd(self, gacmd, Quiet=False, Block=True)
- Sends a command to GrADS. When Block=True, the output is captured
and can be retrieved by methods rline() and rword().
- coords(self)
- Returns a GaHandle object with attributes defining the
following coordinate variables associated with the current
dimension environmnet:
name --- set to "coords"
undef --- based on default file
dims --- list with names of varying dimensions
denv --- dimension environment: result of query("dims")
time --- list with time, e.g., ['00Z01JAN1987', '12Z01JAN1987' ]
lev --- float array with vertical levels
lat --- float array with latitudes
lon --- float array with longitudes
Notice that this method defines many of the attributes of
a GaGrid object.
- eval(self, expr)
- Exports GrADS expression *expr*, returning a flat Python array.
a = eval(expr)
where
a --- flat Python array (or flat jarray under Jython)
Generalized Expressions
=======================
For convenience, *expr* can also be an array. In such
cases, the input Field is just returned back. This
*overloading* is useful for writing high level functions that
work equaly on a GrADS expression to be exported or on arrays.
- flush(self)
- Flushes the GrADS pipes. This is primarily an internal
method that sometimes can be useful to the end-user. If for
some reason the echo you see on the screen seems out of sync
with the command you just issued, a call to *flush()* may be your
ticket. (This situation is usually a symptom of an internal
error.)
- jcoords(self)
- Returns a GaHandle object with attributes defining the
coordinate variables associated with the current dimension
environment. This is a Java wrapper; see coords() for
additional information.
- jopen(self, fname, ftype='default', Quiet=False)
- Similar to open() but returns a Java HashMap instead.
This method only makes sense under Jython.
- jquery(self, what, Quiet=False)
- Queries GrADS internal state and returns a Java Hashtable with
the results. This method only makes sense under Jython.
- open(self, fname, ftype='default', Quiet=False)
- Opens a GrADS file, returning the relevant metadata. On input,
ftype can be used to specify which GrADS open command to open
the file:
ctl the classic GrADS control file; uses the "open" command
sdf NetCDF, HDF or a DODS URL; uses the "sdfopen" command
xdf NetCDF, HDF or a DODS URL; uses the "xdfopen" command
default Open command is Guessed from the file name; the heuristic
algorithm works pretty well but is not perfect, therefore
the options above for when it fails.
- query(self, what, Quiet=False)
- Queries GrADS internal state and returns a GaHandle object
with the results of the query:
qh = query(what)
where *what* is the name of the properpty being queried.
When Quiet=True echo of GrADS commands is temporarily
suppressed. The following properties are implemented:
qh = self.query ( "file #" )
Returns information about the file number "#". If the file number
is ommitted the default file is used. The following *qh* attributes
are defined:
fid - the file Id number
title - the title of the dataset
desc - the description of the dataset
bin - the binary file name
type - file type
nx - number of longitudinal points
ny - number of latitudinal points
nz - number of vertical levels
nt - number of times on file
nvars - number of variables on file
vars - list of variable names in the file
var_levs - list of number of levels for each variable
var_titles - list of long names for each variable
var_info - list of tuplet triplets (var,levs,title) for each
variable to allow easy iterating:
qh = ga.query('file')
for varname, nlevels, vartitle in qh.var_info:
# ...
qh = self.query ( "dims" )
Returns information about the dimension environment. The following
*qh* attributes are define:
dfile - default file number
x_state - x-coordinate state: "fixed" or "varying"
lon - longitudinal range
x - x-index range
y_state - y-coordinate state: "fixed" or "varying"
lat - latitudinal range
y - y-index range
z_state - z-coordinate state: "fixed" or "varying"
lev - level range
z - z-index range
t_state - time-coordinate state: "fixed" or "varying"
time - time range
t - t-index range
- rline(self, i=None)
- Returns the ith line of the most recent GrADS output.
If *i* is not specified, returns the number of lines available
- rword(self, i, j)
- Returns the jth word of the ith line of the most recent
GrADS output
- setdim(self, dh)
- Sets the dimension environment. On input, 8dh* is usually obtained
as the output of:
dh = ga.query('dims')
Data descriptors inherited from grads.gacore.GaCore:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
| |