pyiem.util module#
Utility functions for pyIEM package
This module contains utility functions used by various parts of the codebase.
- class pyiem.util.CustomFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source]#
Bases:
FormatterA custom log formatter class.
- pyiem.util.web2ldm(url, ldm_product_name, md5_from_name=False, pqinsert='pqinsert')[source]#
Download a URL and insert into LDM.
Implements a common IEM workflow whereby a web resource is downloaded, saved to a temporary file, and then inserted into LDM.
- Parameters:
- Returns:
bool - success of this workflow.
- pyiem.util.convert_value(val, units_in, units_out)[source]#
DRY Helper to return magnitude of a metpy unit conversion.
- pyiem.util.c2f(val)[source]#
Helper to return magnitude of Celcius to Fahrenheit conversion.
- Parameters:
val (mixed) – something with values in C
- Returns:
something with values in F
- Return type:
val
- pyiem.util.mm2inch(val)[source]#
Helper to return magnitude of milimeters to inch conversion.
- Parameters:
val (mixed) – something with values in mm
- Returns:
something with values in inch
- Return type:
val
- pyiem.util.logger(name='pyiem', level=None)[source]#
Get pyiem’s logger with a stream handler attached.
- Parameters:
name (str) – The name of the logger to get, default pyiem
level (logging.LEVEL) – The log level for this pyiem logget, default is WARNING for non interactive sessions, INFO otherwise
- Returns:
logger instance
- pyiem.util.ncopen(ncfn, mode='r', timeout=60, _sleep=5)[source]#
Safely open netcdf files
The issue here is that we can only have the following situation for a given NetCDF file. 1. Only 1 or more readers 2. Only 1 appender
The netcdf is being accessed over NFS and perhaps local disk, so writing lock files is problematic.
- pyiem.util.utc(year=None, month=1, day=1, hour=0, minute=0, second=0, microsecond=0)[source]#
Create a datetime instance with tzinfo=timezone.utc
When no arguments are provided, returns datetime.now(timezone.utc).
- Returns:
datetime with tzinfo set
- pyiem.util.noaaport_text(text)[source]#
Make whatever text look like it is NOAAPort Pristine
- Parameters:
text (string) – the inbound text
- Returns:
text that looks noaaportish
- pyiem.util.handle_date_err(exp, value, fmt)[source]#
Attempt to fix up a date string, when possible.
- pyiem.util.get_autoplot_context(fdict, cfg, enforce_optional=False, **kwargs)[source]#
Get the variables out of a dict of strings
This helper for IEM autoplot gets values out of a dictionary of strings, as provided by CGI. It does some magic to get types right, defaults right and so on. The typical way this is called
ctx = iemutils.get_context(fdict, get_description())
- Parameters:
- Returns:
dictionary of variable names and values, with proper types!
- pyiem.util.exponential_backoff(func, *args, **kwargs)[source]#
Exponentially backoff some function until it stops erroring
- Parameters:
_ebfactor (int,optional) – Optional scale factor, allowing for faster test
- pyiem.util.delete_property(name, cursor=None)[source]#
Delete a property from the database.
- Parameters:
name (str) – The name of the property to delete
cursor (psycopg2.cursor) – Optional database cursor to use
- pyiem.util.get_properties(cursor=None)[source]#
Fetch the properties set
- Returns:
a dictionary of property names and values (both str)
- Return type:
- pyiem.util.grid_bounds(lons, lats, bounds)[source]#
Figure out indices that we can truncate big grid
- Parameters:
lons (np.array) – grid lons
lats (np.array) – grid lats
bounds (list) – [x0, y0, x1, y1]
- Returns:
[x0, y0, x1, y1]
- pyiem.util.archive_fetch(partialpath, localdir='/mesonet/ARCHIVE/data', method='get')[source]#
Helper to fetch a file from the archive, by first looking at the filesystem and then going to the website. This returns a filename. If a temporary file is created, it is deleted after the context manager exits.
- Parameters:
- Returns:
filename of the file found and available for use
- Return type: