zapata.computation module

zapata.computation.zonal_var(dataset, var, season, option='LonTime', verbose=False)[source]

A routine to average xarray

This routine will accept xarray up to four dimensions (lat,lon,pressure, time) and return the averaged arrays with compatible dimensions.

Parameters:
  • dataset -- Name of the dataset, ERA5, GPCP

  • var -- Variable

  • season -- Month or Season. Resolved from dat_param

  • option --

    Control Averaging
    • None No Averaging

    • 'LonTime' Longitude and Time

    • 'Lon' Longitude

    • 'Time' Time averaging

  • verbose -- Tons of Output

Returns:

Average. The dimension is depending on the averaging option chosen

Return type:

average

Examples

>>> zonal_var('ERA5','Z','JAN',option='LonTime')   #Longitude and Time Average for Z from ERA5
>>> zonal_var('GPCP','TPREP','DJF',option='Time',verbose=True)   # Time average
zapata.computation.smooth_xarray(X, sigma=5, order=0, mode='wrap')[source]

Smooth xarray X with a gaussian filter .

It uses a routine from scipy ndimage ( ndimage.gaussian_filter). The filter is applied to all dimensions. See the doc page of ( ndimage.gaussian_filter) for a full documentation. The filter can be used for periodic fields, then the correct setting of mode is 'wrap'

Parameters:
  • X -- Input Xarray

  • sigma -- Standard deviation for the Gaussian kernel

  • order -- Order of the smoothing, 0 is a simple convolution

  • mode -- The mode parameter determines how the input array is extended when the filter overlaps a border. By passing a sequence of modes with length equal to the number of dimensions of the input array, different modes can be specified along each axis. Default value is ‘reflect’.

    The valid values and their behaviors are as follows:

    • ‘reflect’ (d c b a | a b c d | d c b a) The input is extended by reflecting about the edge of the last pixel.

    • ‘constant’ (k k k k | a b c d | k k k k) The input is extended by filling all values beyond the edge with the same constant value, defined by the cval parameter.

    • ‘nearest’ (a a a a | a b c d | d d d d) The input is extended by replicating the last pixel.

    • ‘mirror’ (d c b | a b c d | c b a) The input is extended by reflecting about the center of the last pixel.

    • ‘wrap’ (a b c d | a b c d | a b c d) The input is extended by wrapping around to the opposite edge.

Returns:

numpy array

Return type:

smooth_array

Examples

Smooth a X[lat,lon] array with nearest repetition in lat and periodicity in lon

>>> smooth_array(X,sigma=5,order=0,mode=['nearest','wrap'])
zapata.computation.anomaly(var, option='anom', freq='month')[source]

Compute Anomalies according to option

Long description here.

Parameters:
  • var (xarray) -- array to compute anomalies

  • option --

    Option controlling the type of anomaly calculation

    deviation

    Subtract the time mean of the time series

    deviation_std

    Subtract the time mean and normalize by standard deviation

    anom

    Compute anomalies from monthly climatology

    anomstd

    Compute standardized anomalies from monthly climatology

  • freq -- Frequency of data

Returns:

anom

Return type:

xarray

class zapata.computation.Xmat(X, dims: Hashable | Sequence[Hashable] | None = None, option=None)[source]

Bases: object

This class creates xarrays in vector mathematical form.

The xarray is stacked along dims dimensions with the spatial values as column vectors and time as the number of columns.

Specifying the parameter option as DropNaN will drop all the NaN values and the matrix can then be recontructed using the expand method.

Parameters:
  • X (xarray) -- xarray of at leasts two dimensions

  • dims -- Dimensions to be stacked, Default ('lat','lon')

  • options -- Options for Xmat creation None : Keep NaN (Default) DropNaN : Drop NaN values

Variables:
  • A (xarray) -- Stacked matrix of type xarray

  • _ntime -- Length of time points

  • _npoints -- Length of spatial points

  • _F (xarray) -- Original matrix of type xarray with only NaN values

Examples

Create a stacked data matrix along the 'lon' 'lat' dimension

>>> Z = Xmat(X, dims=('lat','lon'))
A
expand()[source]

Unroll Xmat matrix to xarray

Examples

Unroll a stacked and NaN-dropped matrix X

>>> Xlatlon = X.expand()
svd(N=10)[source]

Compute SVD of Data Matrix A.

The calculation is done in a way that the modes are equivalent to EOF

Parameters:

N -- Number of modes desired. If it is larger than the number of time levels then it is set to the maximum

Returns:

out --

Dictionary including

Pattern

EOF patterns

Singular_Values

Singular Values

Coefficient

Time Coefficients

Varex

Variance Explained

Return type:

dictionary

Examples

>>> out = Z.svd(N=10)
corr(y, Dim='time', option=None)[source]

Compute correlation of data matrix A with index y.

This method compute the correlation of the data matrix with an index of the same length of the time dimension of A

The p-value returned by corr is a two-sided p-value. For a given sample with correlation coefficient r, the p-value is the probability that the absolute value of the correlation of a random sample x' and y' drawn from the population with zero correlation would be greater than or equal to the computed correlation. The algorithms is taken from scipy.stats.pearsonsr' that can be consulted for full reference

Parameters:
  • y (xarray) -- Index, should have the same dimension length time

  • option (str) --

    • 'probability' _Returns the probability (p-value) that the correlation is smaller than a random sample

    • 'signicance' _Returns the significance level ( 1 - p-value)

Returns:

  • According to option

  • * None -- corr : Correlation array

  • * 'Probability' -- corr : Correlation array prob : p-value array

  • * 'Significance' -- corr : Correlation array prob : Significance array

Examples

Correlation of data matrix Z with index

>>> corr = Z.corr(index)
>>> corr,p = Z.corr(index,'Probability')
>>> corr,s = Z.corr(index,'Significance')
cov(y, Dim='time')[source]

Compute covariance of data matrix A with index.

This method compute the correlation of the data matrix with an index of the same length of the time dimension of A

Examples

Covariance of data matrix Z with index

>>> cov = Z.cov(index)
anom(**kw)[source]

Creates anomalies.

This is using the function anomaly from zapata.computation

detrend(**kw)[source]

Detrend data using the function scipy.signal.detrend

zapata.computation.feature_to_input(k, num, PsiX, Proj, icstart=0.15)[source]

Transform from Feature space to input space.

It computes an approximate back-image for the Gaussian kernels and and exact backimage for kernels based on scalar product whose nonlinear map can be inverted.

Still working on.

Parameters:
  • k (Kernel) -- Kernel to be used

  • num -- Number of RKHS vectors to transform

  • PsiX (array (npoints,ntime)) -- Original data defininf the kernel

  • Proj -- Projction coefficients on Feature Space

  • icstart -- Starting Value for iteration

Returns:

back_image

Return type:

array(npoints, num)

zapata.computation.make_random_index(dskm, inda, X, arealat, arealon)[source]

Generate an index from random sampling of modes

It computes an index defined over an area using a random sampling of the modes

Still working on.

Parameters:
  • dskm (Data Set) -- Data set containing the Koopman decomposition

  • inda -- Number of modes to be considered in the random reconstruction

  • X -- Data array with geographiucal information

  • arealat -- Latitudinal boundaries of index calculation

  • arealon -- Longitudinal boundaries of index calculation

Returns:

Correlation coefficient

Return type:

c