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_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.add_meridians_and_parallels_to(bm, meridians='auto', parallels='auto', ax=None, drawparallels_kwargs=None, drawmeridians_kwargs=None, drawequator_kwargs=None, drawgreenwich_kwargs=None)[source]

Adds meridians and parallels to a basemap instance bm.

meridians and parallels enable to fine-tune the choice of lines to plot, with either:

  • ‘auto’: automatic scaling to the basemap extents
  • ‘default’: range(0,360,10) and range(-90,90,10)
  • a list of values
  • a grid step, e.g. 5 to plot each 5 degree.
  • None: no one is plot
  • meridian == ‘greenwich’ // ‘datechange’ // ‘greenwich+datechange’ parallel == ‘equator’ // ‘polarcircles’ // ‘tropics’ or any combination (,) will plot only these.
Parameters:
  • ax – the ax to be plotted on
  • drawparallels_kwargs – kwargs to be passed to basemap.drawparallels()
  • drawmeridians_kwargs – kwargs to be passed to basemap.drawgreenwich()
  • drawequator_kwargs – draw kwargs to emphasize equator parallel
  • drawgreenwich_kwargs – draw kwargs to emphasize greenwich meridian
epygram.util.nearlyEqual(a, b, epsilon=1e-11)[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.scale_colormap(cmap, max_val=None)[source]

Deprecated since version 1.3.9.

Creates a matplotlib.colors.BoundaryNorm object tuned for scaled colormaps, i.e. discrete, irregular colorshades.

Parameters:
  • cmap – name of the colormap, as found in config.colormaps_scaling
  • max_val – if given, replaces the upper bound.
Returns:

a tuple (norm, scaling), scaling being eventually modified according to max_val

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.set_map_up(bm, ax, drawrivers=False, drawcoastlines=True, drawcountries=True, meridians='auto', parallels='auto', departments=False, bluemarble=0.0, background=False, drawmapboundary_kwargs=None, fillcontinents_kwargs=None, drawcoastlines_kwargs=None, drawcountries_kwargs=None, drawparallels_kwargs=None, drawmeridians_kwargs=None, drawequator_kwargs=None, drawgreenwich_kwargs=None)[source]

Cf. H2DField.plotfield() documentation.

epygram.util.datetimerange(*_, **__)[source]

Deprecated since version 1.2.11.

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: object

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=1e-11)[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.