epygram.util — Utilities

Some useful utilities…

Functions

epygram.util.find_re_in_list(regexp, a_list)[source]

Finds all elements from a list that match a regular expression. The regexp and the different elements of the list must be of the same type:

  • strings

  • tuples with the same length

  • dictionnaries: all regexp keys must be keys of the list

epygram.util.degrees_nearest_mod(d, ref)[source]

Returns the angle(s) d in the modulo nearest to ref.

epygram.util.positive_longitudes(lons)[source]

Returns numpy array lons forced (modulo) in [0;360[.

epygram.util.longitudes_between_minus180_180(lons)[source]

Returns numpy array lons forced (modulo) in ]-180;180].

epygram.util.positive_longitude(lon, unit='degrees')[source]

Returns lon shifted in [0;360[ or [0;2pi[ (depending on unit).

epygram.util.load_cmap(cmap)[source]

Reads and registers the epygram-or-user-colormap called cmap, which must be either in config.epygram_colormaps or config.usercolormaps.

Works with both old-way (.cmap) and new way (.json).

epygram.util.write_formatted(dest, label, value, align='<', firstcolumn_width=50, secondcolumn_width=20)[source]
epygram.util.write_formatted_fields(dest, label, value=None, compression=None, firstcolumn_width=50, secondcolumn_width=20)[source]
epygram.util.write_formatted_dict(dest, fid, sort_function=<built-in function sorted>)[source]
epygram.util.write_formatted_table(dest, table, alignments=['<', '^'], precision=6, float_type='E')[source]

A table is meant to be : <str> <str> <str> … <str> <num> <num> …

… … … …

epygram.util.nearlyEqual(a, b, epsilon=2.220446049250313e-16)[source]

Function to compare floats http://floating-point-gui.de/errors/comparison/ Float.MIN_NORMAL was replaced by sys.float_info.min Float.MAX_VALUE was replaced by sys.float_info.max

epygram.util.nearlyEqualArray(*args, **kwargs)

Vector version of nearlyEqual().

epygram.util.restrain_to_index_i_of_dim_d(a, i, d, n=None)[source]

Of an array a[d1, d2, d3, … dn], returns the array restricted to index i of the dimension d.

Parameters
  • a – the input array

  • i – index in dimension d

  • d – the dimension to restrain

  • n – specify a priori the number of dimensions of a

A more elegant solution would have been the following, but it does not work when accessing netCDF variable (for which it was necessary):

indexes = [range(len(self._dimensions[d])) for d in variable.dimensions] # equivalent to [:, :, :, ...]
for k in only.keys():
    indexes[variable.dimensions.index(k)] = [only[k]] # restrain to the "only" give
return array[numpy.ix_(*indexes)]
epygram.util.datetimes2fieldvaliditylist(datetimes, basis=None)[source]

Return a FieldValidityList from a list of datetime.datetime instances (or a single datetime.datetime).

Parameters

basis – can be either - None (default): basis = validity - a single datetime.datetime - a list of the same length as datetimes

epygram.util.ifNone_emptydict(arg)[source]

Transforms a None into a {}. To be used as workaround for empty dicts in default values of methods.

epygram.util.fmtfid(fmt, fid)[source]

Given a resource format name fmt and a (full) fid, returns the key corresponding the actual format of the field in resource. (Useful for distinguishing GRIB1/2)


Classes

class epygram.util.RecursiveObject[source]

Bases: epygram.util.CheckAttribute

Generic abstract class implementing useful recursive properties:

  • display of object: the __str__ method returns str(attr) for each of the object’s attributes, with automatical indentation for readability.

  • test of (in)equality: the a == b test will be true if a and b have the same attributes and a.attr == b.attr for each attribute.

copy()[source]

Returns a copy of the object.

deepcopy()[source]

Returns a deepcopy of the object.

recursive_diff(other)[source]

Recursively list what differs from other.

tolerant_equal(other, tolerance=2.220446049250313e-16)[source]

Test of equality by recursion on the object’s attributes, with a tolerance.

class epygram.util.Angle(value, unit)[source]

Bases: epygram.util.RecursiveObject

This class handles an angle. It enables conversions of units, while saving the original unit and value of the angle at its construction.

Available units: ‘degrees’, ‘radians’, ‘cos_sin’ (cos, sin),

‘DMS’ (Degree, Minutes, Seconds).

Constructor. ‘unit’ argument must be among: - ‘degrees’, - ‘DMS’ - in which case, value is a tuple (degrees, minutes, seconds), - ‘radians’, - ‘cos_sin’ - in which case, value is a tuple (cos, sin).

get(unit=None)[source]

Returns the angle in the requested unit. If no unit is supplied, the origin unit is used.

recursive_diff(other)[source]

Recursively list what differs from other.

tolerant_equal(other, tolerance=2.220446049250313e-16)[source]

Redefinition because of dynamism of buffering new computed values…