Source code for epygram.V1DField

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from epygram.base import Field, FieldValidity
from epygram import config, V1DGeometry, epygramError, profiles
import numpy



[docs]class V1DField(Field): """ Vertical 1-Dimension (column) field class. A field is defined by its identifier 'fid', its data, its geometry, and its validity. """ _collector = ('field',) _footprint = dict( attr = dict( geometry = dict(type = V1DGeometry), validity = dict( type = FieldValidity, optional = True, default = FieldValidity()), processtype = dict( optional = True, info = "Generating process.") ) )
[docs] def setdata(self, data): """ Sets data, checking it to be 1D. """ if len(numpy.shape(data)) != 1: raise epygramError("data must be 1D array.") super(V1DField, self).setdata(data) ################### # PRE-APPLICATIVE # ################### # (but useful and rather standard) ! # [so that, subject to continuation through updated versions, # including suggestions/developments by users...]
[docs] def plotfield(self): """ Makes a simple (profile) plot of the field. """ return profiles.plotprofiles(self)
[docs] def stats(self): """ Computes some basic statistics on the field, as a dict containing: {'min', 'max', 'mean', 'std', 'quadmean', 'nonzero'}. See each of these methods for details. """ return {'min':self.min(), 'max':self.max(), 'mean':self.mean(), 'std':self.std(), 'quadmean':self.quadmean(), 'nonzero':self.nonzero()}
[docs] def min(self): """ Returns the minimum value of data. """ data = self.data return numpy.ma.masked_outside(data, -config.mask_outside, config.mask_outside).min()
[docs] def max(self): """ Returns the maximum value of data. """ data = self.data return numpy.ma.masked_outside(data, -config.mask_outside, config.mask_outside).max()
[docs] def mean(self): """ Returns the mean value of data. """ data = self.data return numpy.ma.masked_outside(data, -config.mask_outside, config.mask_outside).mean()
[docs] def std(self): """ Returns the standard deviation of data. """ data = self.data return numpy.ma.masked_outside(data, -config.mask_outside, config.mask_outside).std()
[docs] def quadmean(self): """ Returns the quadratic mean of data. """ data = self.data return numpy.sqrt((numpy.ma.masked_outside(data, -config.mask_outside, config.mask_outside)**2).mean())
[docs] def nonzero(self): """ Returns the number of non-zero values (whose absolute value > config.epsilon). """ data = self.data return numpy.count_nonzero(abs(numpy.ma.masked_outside(data, -config.mask_outside, config.mask_outside)) > config.epsilon) ############# # OPERATORS # ############# #def _check_operands(self, other): # """ # Internal method to check compatibility of terms in operations on fields. # """ # # if isinstance(other, self.__class__): # if self.geometry != other.geometry: # raise epygramError("operations on fields cannot be done if fields do not share their vertical geometry.") # else: # super(V1DField, self)._check_operands(other)
def __add__(self, other): """ Definition of addition, 'other' being: - a scalar (integer/float) - another Field of the same subclass. Returns a new Field whose data is the resulting operation, with 'fid' = {'op':'+'} and null validity. """ newfield = super(V1DField, self)._add(other, geometry=self.geometry) return newfield def __mul__(self, other): """ Definition of multiplication, 'other' being: - a scalar (integer/float) - another Field of the same subclass. Returns a new Field whose data is the resulting operation, with 'fid' = {'op':'*'} and null validity. """ newfield = super(V1DField, self)._mul(other, geometry=self.geometry) return newfield def __sub__(self, other): """ Definition of substraction, 'other' being: - a scalar (integer/float) - another Field of the same subclass. Returns a new Field whose data is the resulting operation, with 'fid' = {'op':'-'} and null validity. """ newfield = super(V1DField, self)._sub(other, geometry=self.geometry) return newfield def __div__(self, other): """ Definition of division, 'other' being: - a scalar (integer/float) - another Field of the same subclass. Returns a new Field whose data is the resulting operation, with 'fid' = {'op':'/'} and null validity. """ newfield = super(V1DField, self)._div(other, geometry=self.geometry) return newfield