#!/usr/bin/python3 # Link each defined teacup name to one of the allowed color SVG files, based # on its definition and current storage import json, os, sys sys.path.append( "/usr/dd/common/python_lib" ) from sqlite import SQLite db = SQLite() db.connect( '../../web_service/data/hydro.db' ) fh = open( '../config/stations.json', 'r' ) conf = json.loads( fh.read() ) fh.close() os.chdir( '../www/img/teacups' ) for id in conf: stn = conf[id] if 'Tea Cup' not in stn['layers']: continue units = stn['storage_units'] storage = db.ts_last( stn['storage_ts'], units )[1] storage_max = float( stn['storage_max'] ) storage_min = float( stn['storage_min'] ) if units == 'ac-ft': storage /= 1000 storage_min /= 1000 storage_max /= 1000 capacity = storage_max - storage_min pct = None rc_pct = None if storage is not None: pct = ( storage - storage_min ) / capacity * 100 color = 'none' if 'teacup_color' in stn: color = stn['teacup_color'] filename = '%s.svg' % ( id ) fh = open( filename + '.tmp', 'w' ) fh.write( '\n' ) msg = '' if pct is not None: # Compute the percent height based on the percentage full. This turns # out to be a quadratic equation, so the solution is a little odd. if pct < 0: pct = 0 pct /= 100 pct_high = ( 3 * pct + 1 ) ** 0.5 - 1 fill_y = 30 - ( pct_high * 30 ) x_off = 10 * ( 1 + pct_high ) fh.write( ( '\n' ) % ( 20 - x_off, fill_y, 20 + x_off, fill_y, 20 - x_off, fill_y ) ) msg = '%0.0f' % ( pct * 100 ) if rc_pct is not None: msg += '/%0.0f' % ( rc_pct ) else: # We don't know how full the reservoir is, so draw it askew. fh.write( '\n' ) msg = '??' fh.write( '''''' % ( color ) ) if len( msg ) == 1: x_offset = 5 else: x_offset = 10 fh.write( ( '%s\n' ) % ( 20 - x_offset , msg ) ) fh.write( '\n' ) fh.close() if os.path.exists( filename ): os.unlink( filename ) os.rename( filename + '.tmp', filename )