#!/usr/bin/python3 """ Replace any Location Levels found in a station configuration file with their current values and emit the result. This is useful for exporting to places where CWMS location levels are not available, specifically the public web site. """ import cx_Oracle, json, os, sys sys.path.append( '/home/usace/g0cwpa26/rwcds/lib' ) from password import Password from get_node import get_node def lookup( level, units ): value = cur.var( cx_Oracle.NUMBER ) cur.execute( """ BEGIN cwms_level.retrieve_location_level_value( :value, :level, :units, sysdate ); END;""", [ value, level, units ] ) return round( value.getvalue(), 2 ) ############################################################################### if __name__ == '__main__': conf = json.loads( open( sys.argv[1], 'r' ).read() ) user = 'g0cwpa26' dsn = get_node()[0].upper() db = cx_Oracle.Connection( user=user, password=Password().get( user ), dsn=dsn ) cur = db.cursor() for station in conf: for level in [ 'storage_max', 'storage_min' ]: if level in conf[station] and 'storage_units' in conf[station]: if type( conf[station][level] ) == type( u'' ): conf[station][level] = lookup( conf[station][level], conf[station]['storage_units'] ) print( json.dumps( conf, sort_keys = True, indent = 2 ) )