Closed rburghol closed 8 months ago
Add a group with hdf5 module in python
items()
method as a type of collections
object class ItemsView
https://docs.python.org/3/library/collections.abc.html?highlight=itemsview#collections.abc.ItemsView
import h5py
import numpy as np
import pandas as pd
from collections import defaultdict
from pandas import DataFrame, read_hdf, HDFStore
from numba.typed import Dict
fpath = 'C:/Workspace/modeling/cbp/hsp2/river/OR1_7700_7980.h5'
f.close() f = h5py.File(fpath,'a') # use mode 'a' which allows read, write, modify objs = f.create_dataset("OBJECTS", (50,), dtype='f')
del f["OBJECTS"] grp = f.create_group("OBJECTS") grp.create_group("RCHRES_0001") rch = grp["RCHRES_0001"] rch.create_group("Qin") Qin = rch["Qin"]
Qin.attrs['equation'] = 'Qup * 1.23' Qin.attrs['default_value'] = 0.0 Qin.attrs['state_path'] = "/STATE/RCHRES_0001/Qin" Qin.attrs['attribute_path'] = "/OBJECTS/RCHRES_0001/Qin" Qin.attrs['fn_step'] = "om_equation_step" # this can be used to specify any function whatsoever that takes the correct args, but might be difficult to use in njit? Qin.attrs['data_type'] = "f"
ts = f['TIMESERIES'].items() for a in ts: print(a[0], a[1])
dset = f['TIMESERIES/TS011/table'] dset[1]
dset[2]
dset[3]
dset[4]
Now add a timeseries indexed by integers - ts_ix
ts_ix = Dict.empty(key_type=types.int64, value_type=types.float32[:])
tsq = f['TIMESERIES/']
ts_ts = np.asarray(tsq['TS011']['table']['index']) # index of timestamps for each timestep. use known TS for this demo
# this code replicates a piece of the function of get_timeseries, and loads the full timeseries into a Dict
# the dict will be keyed by a simple integer, and have values at the time step only. The actual time
# at that step will be contained in ts_ts
ts_tables = list(tsq.keys())
for i in ts_tables:
if i == 'SUMMARY': continue # skip this non-numeric table
var_path = '/TIMESERIES/' + i
ix = set_state(state_ix, state_paths, var_path, 0.0)
ts_ix[ix] = np.asarray(tsq[i]['table']['values'], dtype="float32")
- now, the Qin variable from the WDM can be found in the ts_ix array keyed as integer
- we can get that integer by: `Qin_ix = get_state_ix(state_ix, state_paths, '/TIMESERIES/TS011')`
Iterate through the values for a simulation
Qin_ix = get_state_ix(state_ix, state_paths, '/TIMESERIES/TS011') for idx, x in np.ndenumerate(ts_ts): state_ix[Qin_ix] = ts_ix[Qin_ix][idx] # the timeseries inputs state get set here. must a special type of object to do this
[print(key,':',value) for key, value in state_ix.items()]
op_tokens, state_paths, state_ix, dict_ix = init_sim_dicts()
eqn_path = "/OBJECTS/RCHRES_0001/Qin"
eqn = "Qin * 1.21"
eq_ix = set_state(state_ix, state_paths, eqn_path, 0.0) # initialize this equation operator in the state_ix Dict, returns unique key
# NR uses weird global shit so we have to do this:
exprStack = []
exprStack[:] = []
ps = deconstruct_equation(eqn)
Overview
Tasks
eval()
innumba
- but maybenumexpr.evaluate()
See below Solver: eval optionsObject Model
Explore method of using
@jitclass
objects or a more functional approach.Handler Class
Execution Rendering
Parser
Data Model in hdf5
/TIMESERIES/INTAKE_001/
available_mgd = Qintake - flowby
/TIMESERIES/RCHRES_001/IVOL
- could also be an equation scaling to intake drainage area, i.e.Qreach = ivol * local_area / area
/TIMESERIES/INTAKE_001/flowby/value
(note: flowby would also be an equation)ts['/TIMESERIES/INTAKE_001/available_mgd/value'] = ts['/TIMESERIES/RCHRES_001/IVOL/value'] - ts['/TIMESERIES/INTAKE_001/flowby/value']
Solver
Decomposition/Optimization:
Eval
eval()
there are examples of usingnumexpr.evaluate()
(a numba analog ofeval()
) with@njit
however,ts['/TIMESERIES/RCHRES_001/IVOL/value'] - ts['/TIMESERIES/INTAKE_001/flowby/value']
cannot be evaluated.om
) BUT, the numba string replace methods do not allow replacing with a numeric value, only a string, and does not have the ability to convert a number to a string inside an@njit
compiled function.Pre-rendered Functions
state['/STATE/INTAKE_001/available_mgd/value'] = state['/STATE/RCHRES_001/IVOL/value'] - state['/STATE/INTAKE_001/flowby/value']
def fn_specl_rchres_0001_Qin(state, ui, ts, step)