Prado EFO PVA
qin.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 """
3 Created on Tue Apr 28 13:09:53 2020
4 
5 @author: cd
6 """
7 
8 
9 import abc
10 import numpy as np
11 import h5py
12 from efo.time import TimeBase
13 from efo.lookup import LkupTblAnn
14 
15 class QinBase(metaclass=abc.ABCMeta):
16  def __init__(self, name):
17  # TODO: Some subclass properties could be super class properties
18  self.namename = name
19  super().__init__()
20  @abc.abstractmethod
21  def get_qin(self):
22  pass
23  @classmethod
24  def __subclasshook__(cls,C):
25  if cls is QinBase:
26  attrs = set(dir(C))
27  if set(cls.__abstractmethods__) <= attrs:
28  return True
29  return NotImplemented
30 
31 
32 # TODO: This class should be modified to check dates to make sure that the begin
33 # and end dates all match and if not then send an error (maybe this should just
34 # happen in the script for now)
35 class Qin(QinBase):
36  def __init__(self, name, time, qIn):
37  # Call super class constructor
38  super().__init__(name)
39  # Set class properties
40  # TODO: Error handling to make sure these things can be assigned
41  # TODO: Change this from qIn to just q
42  self.qInqIn = qIn
43  self.TT = time
44  # TODO: Add a time offset option that can add or subtract time to the current step
45 
46  def get_qin(self, tsOffset=0):
47  return self.qInqIn[min(self.TT.end,self.TT.step + tsOffset)].item()
48 
49 
51  def __init__(self, name, time, reachUs):
52  # Call super class constructor
53  super().__init__(name)
54  # Set class properties
55  # TODO: Error handling
56  self.reachUsreachUs = reachUs
57  # TODO: Should use error handling instead
58  if issubclass(type(time), TimeBase):
59  self.TT = time
60 
61  def get_qin(self, tsOffset=0):
62  # TODO: Need to apply and offset option
63  return self.reachUsreachUs.calc_qout(tsOffset=tsOffset)
64 
65 
67  # TODO: Add a hydrologic conditions option for this
68  def __init__(self, name, time, monthDayHr, qTblVals, *,
69  typ='interp', timeUnit=None):
70  # Call super class constructor
71  super().__init__(name)
72  # Set class properties
73  self.qLkupTblqLkupTbl = LkupTblAnn(name+'Tbl', time, monthDayHr, qTblVals,
74  typ=typ, timeUnit=timeUnit)
75  # TODO: Should use error handling instead
76  if issubclass(type(time), TimeBase):
77  self.TT = time
78  self.qInqIn = np.empty(time.nSteps)
79 
80  def get_qin(self, tsOffset=0):
81  # TODO: Need to apply and offset option
82  qIn = self.qLkupTblqLkupTbl.get_val(self.TT.get_datetime_offset(tsOffset))
83  self.qInqIn[min(self.TT.end, self.TT.step + tsOffset)] = qIn
84  return qIn
85 
86 
87 class Qloss(QinBase):
88  def __init__(self,name,qIn):
89  # Call super class constructor
90  super().__init__(name)
91  # TODO: Should use error handling instead
92  if issubclass(type(qIn), QinBase):
93  self.qInqIn = qIn
94 
95  def get_qin(self, tsOffset=0):
96  return -self.qInqIn.get_qin(tsOffset)
97 
98 
100  def __init__(self, name, time):
101  # Call super class constructor
102  super().__init__(name, time, [])
103  # Set class properties
104  self.TTT = time
105  self.qInqInqIn = np.full(self.TTT.nSteps, 0.)
106 
107  def set_qin(self, tsOffset=0, *, qSpecified=np.nan):
108  self.qInqInqIn[min(self.TTT.end, self.TTT.step + tsOffset)] = qSpecified
109 
110 
112  def __init__(self, name, timeFcst, qInFcst):
113  # Call super class constructor
114  super().__init__(name)
115  # Set class properties
116  # TODO: Error handling to make sure these things can be assigned
117  self.qInFcstqInFcst = qInFcst
118  self.tFcsttFcst = timeFcst
119  self.tFcsttFcst.bind_to(self.update_current_qFcstupdate_current_qFcst)
120  self.qInqIn = qInFcst[0, :].flatten() if qInFcst is not None else None
121 
122  def update_current_qFcst(self, rowFcst, dateTime):
123  self.qInqIn = self.qInFcstqInFcst[rowFcst, :].flatten().copy()
124 
125  def get_qin(self, tsOffset=0):
126  return self.qInqIn[min(self.tFcsttFcst.end, self.tFcsttFcst.step + tsOffset)]
127 
128 
130  def __init__(self, name, timeFcst, h5FilePath, h5FileVar, fcstMbr=None):
131  # Call super class constructor
132  super().__init__(name, timeFcst, None)
133  # Set class properties
134  self.qInFcstFileqInFcstFile = h5py.File(h5FilePath, 'r')[h5FileVar]
135  self.fcstMbrfcstMbr = fcstMbr
136  self.nDimnDim = self.qInFcstFileqInFcstFile.ndim
137  if self.nDimnDim == 2:
138  self.qInqInqIn = self.qInFcstFileqInFcstFile[0, :].flatten()
139  else:
140  self.qInqInqIn = self.qInFcstFileqInFcstFile[0, :, self.fcstMbrfcstMbr].flatten()
141 
142  def update_current_qFcst(self, rowFcst, dateTime):
143  if self.nDimnDim == 2:
144  self.qInqInqIn = self.qInFcstFileqInFcstFile[rowFcst, :].flatten()
145  else:
146  self.qInqInqIn = self.qInFcstFileqInFcstFile[rowFcst, :, self.fcstMbrfcstMbr].flatten()
147 
def get_qin(self)
Definition: qin.py:21
def __subclasshook__(cls, C)
Definition: qin.py:24
def __init__(self, name)
Definition: qin.py:16
def __init__(self, name, timeFcst, h5FilePath, h5FileVar, fcstMbr=None)
Definition: qin.py:130
def update_current_qFcst(self, rowFcst, dateTime)
Definition: qin.py:142
def __init__(self, name, timeFcst, qInFcst)
Definition: qin.py:112
def get_qin(self, tsOffset=0)
Definition: qin.py:125
def update_current_qFcst(self, rowFcst, dateTime)
Definition: qin.py:122
def __init__(self, name, time, monthDayHr, qTblVals, *typ='interp', timeUnit=None)
Definition: qin.py:69
def get_qin(self, tsOffset=0)
Definition: qin.py:80
qLkupTbl
Definition: qin.py:73
def __init__(self, name, time, reachUs)
Definition: qin.py:51
def get_qin(self, tsOffset=0)
Definition: qin.py:61
def __init__(self, name, time)
Definition: qin.py:100
def set_qin(self, tsOffset=0, *qSpecified=np.nan)
Definition: qin.py:107
Definition: qin.py:35
T
Definition: qin.py:43
def __init__(self, name, time, qIn)
Definition: qin.py:36
def get_qin(self, tsOffset=0)
Definition: qin.py:46
qIn
Definition: qin.py:42
Definition: qin.py:87
qIn
Definition: qin.py:93
def get_qin(self, tsOffset=0)
Definition: qin.py:95
def __init__(self, name, qIn)
Definition: qin.py:88