-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExternalFlow.py
More file actions
98 lines (87 loc) · 3.38 KB
/
Copy pathExternalFlow.py
File metadata and controls
98 lines (87 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from __future__ import division
import numpy as np
import pandas as pd
import sys
def ReadWPDown(FName, ID):
'''
Function to read the ID and flow fraction of water provinces downstream of
the given WP
Input
-----
FName: string
Path to the required .csv file.
ID: integer
Number of the waterprovince
Output
-----
DOWNnr: list of integers
List of water provinces located downstream of given WP
DOWNfrac: list of floats
List of flow fractions towards the downstream WPs, list is in the same
order as the order of DOWNnr
'''
try:
WP_info = pd.DataFrame.from_csv(FName,header=0)
except Exception, e:
print >> sys.stderr, 'Exception: %s' % str(e)
sys.exit(1)
#-Adjust column headers
WP_info.columns = map(str.upper, WP_info.columns)
#-Locate data corresponding to the ID
WP_info = WP_info.loc[ID,]
#-Empty lists to store downstream IDs and fractions in
DOWNnr = []
DOWNfrac = []
#-Loop through all downstream WPs, and store the data in the lists
for i in range(1, WP_info['NR_DOWN']+1):
DOWNnr += [int(WP_info['TO_ID_'+str(i)])]
DOWNfrac += [float(WP_info['TO_FRAC_'+str(i)])]
return DOWNnr, DOWNfrac
def Add_Headflow(FName, ID, Flow, Fraction, StartYY, EndYY):
'''
Function to add the outflow of the previously calculated WP to the external
flow of a WP located downstream
Input
-----
FName: string
Path to the 'ExtFlow.tss' file. NOTE: this must be without the .npy
extension
ID: int
ID of the downstream water province
Flow: numpy.array
Array of the outflow of the upstream water province
Fraction: float
Fracton the Flow that is going to the specified water province
StartYY: int
Start year of the calculations
EndYY: int
End year of the calculations
Output
-----
None -- this function edits the .tss file (given in FName)
'''
try:
tss = np.load (FName+'.npy')
except Exception, e:
print >> sys.stderr, FName + ' does not exist'
print >> sys.stderr, 'Exception: %s' % str(e)
sys.exit(1)
#-Extract the time series data for 'ID' from the tss array AND for the years
#-Retrieve top row from file, except first 4 cols, these are placeholders
IDs = tss[0,4:]
# Find right column, add 4 for the first 4 placeholder columns (YYYY, MM, DD, DD)
colIdx = 4 + np.searchsorted(IDs, int(ID))
#-Retrive the column containing the year values, except the first 3 rows, these are placeholders
YY = tss[3:, 0]
#-Find the right starting row, add 3 for the placeholder rows
YYstartID = 3 + np.searchsorted(YY, StartYY)
#-Calculate the number of months inbetween
YYendID = YYstartID + ((EndYY - StartYY + 1) * 12)
#-Slice the correct column and rows
PrevFlow = tss[YYstartID:YYendID, colIdx]
#-Adjust the new flow, based on the flow fraction and add this to the current value
NewFlow = PrevFlow + Flow * Fraction # NewFlow = PrevFlow + Flow (original)
#-Replace these values at the right positions
np.copyto(tss[YYstartID:YYendID, colIdx], NewFlow)
#-Save the file for later use
np.save(FName, tss)