#!/usr/bin/python3 # Link each defined gauge name to one of the allowed color SVG files, based # on its definition and current flow and/or stage import json, os, sys sys.path.append( "/usr/dd/common/python_lib" ) from sqlite import SQLite ############################################################################### def gauge_color( stage, flow, bf_type, bankfull, f_type, flood ): if stage is None and flow is None: return 'grey' if( ( bf_type == 'stage' and stage is None or bf_type == 'flow' and flow is None ) and ( f_type == 'stage' and stage is None or f_type == 'flow' and flow is None ) ): return 'grey' color = 'blue' if bankfull is not None or flood is not None: color = 'green' if bankfull is not None: if( bf_type == 'stage' and stage is not None and stage >= bankfull or bf_type == 'flow' and flow is not None and flow >= bankfull ): color = 'yellow' if flood is not None: if( f_type == 'stage' and stage is not None and stage >= flood or f_type == 'flow' and flow is not None and flow >= flood ): color = 'red' return color ############################################################################### ############################################################################### 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/gauges' ) for id in conf: stn = conf[id] if stn['classification'].lower() != 'gauge': continue ( bankfull_type, bankfull, flood_type, flood, flow, stage ) = ( None, None, None, None, None, None ) if 'bankfull' in stn: bankfull_type = stn['bankfull']['type'] bankfull = stn['bankfull']['value'] if 'flood' in stn: flood_type = stn['flood']['type'] flood = stn['flood']['value'] if 'flow' in stn: flow = db.ts_last( stn['flow'], 'kcfs' )[1] if 'stage' in stn: stage = db.ts_last( stn['stage'], 'ft' )[1] color = gauge_color( stage, flow, bankfull_type, bankfull, flood_type, flood ) filename = '%s.svg' % ( id ) if os.path.exists( filename ): os.unlink( filename ) os.symlink( '%s.svg' % ( color ), filename )