@@ -1855,8 +1855,9 @@ def apply_sfr_obs():
1855
1855
1856
1856
1857
1857
def load_sfr_out (sfr_out_file , selection = None ):
1858
- """load an ASCII SFR output file into a dictionary of kper: dataframes. aggregates
1859
- flow to aquifer for segments and returns and flow out at downstream end of segment.
1858
+ """load an ASCII SFR output file into a dictionary of kper: dataframes.
1859
+ aggregates flow to aquifer for segments and returns and flow out at
1860
+ downstream end of segment.
1860
1861
1861
1862
Parameters
1862
1863
----------
@@ -1877,10 +1878,13 @@ def load_sfr_out(sfr_out_file, selection=None):
1877
1878
if selection is None :
1878
1879
pass
1879
1880
elif isinstance (selection , str ):
1880
- assert selection == 'all' , "If string passed as selection only 'all' allowed: {}" .format (selection )
1881
+ assert selection == 'all' , \
1882
+ "If string passed as selection only 'all' allowed: " \
1883
+ "{}" .format (selection )
1881
1884
else :
1882
- assert isinstance (
1883
- selection , pd .DataFrame ), "'selection needs to be pandas Dataframe. Type {} passed." .format (type (selection ))
1885
+ assert isinstance (selection , pd .DataFrame ), \
1886
+ "'selection needs to be pandas Dataframe. " \
1887
+ "Type {} passed." .format (type (selection ))
1884
1888
assert np .all ([sr in selection .columns for sr in ['segment' , 'reach' ]]
1885
1889
), "Either 'segment' or 'reach' not in selection columns"
1886
1890
with open (sfr_out_file ) as f :
@@ -1905,34 +1909,47 @@ def load_sfr_out(sfr_out_file, selection=None):
1905
1909
dlines .append (draw )
1906
1910
df = pd .DataFrame (data = np .array (dlines )).iloc [:, [3 , 4 , 6 , 7 ]]
1907
1911
df .columns = ["segment" , "reach" , "flaqx" , "flout" ]
1908
- df .loc [:, "segment" ] = df .segment .apply (np .int )
1909
- df .loc [:, "reach" ] = df .reach .apply (np .int )
1910
- df .loc [:, "flaqx" ] = df .flaqx .apply (np .float )
1911
- df .loc [:, "flout" ] = df .flout .apply (np .float )
1912
- df .index = df .apply (lambda x : "{0:03d}_{1:03d}" .format (int (x .segment ), int (x .reach )), axis = 1 )
1912
+ df ["segment" ] = df .segment .astype (np .int )
1913
+ df ["reach" ] = df .reach .astype (np .int )
1914
+ df ["flaqx" ] = df .flaqx .astype (np .float )
1915
+ df ["flout" ] = df .flout .astype (np .float )
1916
+ df .index = ["{0:03d}_{1:03d}" .format (s , r ) for s , r in
1917
+ np .array ([df .segment .values , df .reach .values ]).T ]
1918
+ # df.index = df.apply(
1919
+ # lambda x: "{0:03d}_{1:03d}".format(
1920
+ # int(x.segment), int(x.reach)), axis=1)
1913
1921
if selection is None : # setup for all segs, aggregate
1914
1922
gp = df .groupby (df .segment )
1915
1923
bot_reaches = gp [['reach' ]].max ().apply (
1916
- lambda x : "{0:03d}_{1:03d}" .format (int (x .name ), int (x .reach )), axis = 1 )
1917
- df2 = pd .DataFrame (index = gp .groups .keys (), columns = ['flaqx' , 'flout' ])
1918
- df2 ['flaqx' ] = gp .flaqx .sum () # only sum distributed output
1919
- df2 ['flout' ] = df .loc [bot_reaches , 'flout' ].values # take flow out of seg
1924
+ lambda x : "{0:03d}_{1:03d}" .format (
1925
+ int (x .name ), int (x .reach )), axis = 1 )
1926
+ # only sum distributed output # take flow out of seg
1927
+ df2 = pd .DataFrame (
1928
+ {'flaqx' :gp .flaqx .sum (),
1929
+ 'flout' : df .loc [bot_reaches , 'flout' ].values },
1930
+ index = gp .groups .keys ())
1920
1931
# df = df.groupby(df.segment).sum()
1921
- df2 . loc [:, "segment" ] = df2 .index
1932
+ df2 [ "segment" ] = df2 .index
1922
1933
elif isinstance (selection , str ) and selection == 'all' :
1923
1934
df2 = df
1924
1935
else :
1925
- seg_reach_id = selection .apply (lambda x : "{0:03d}_{1:03d}" .
1926
- format (int (x .segment ), int (x .reach )), axis = 1 ).values
1936
+ seg_reach_id = selection .apply (
1937
+ lambda x : "{0:03d}_{1:03d}" .format (
1938
+ int (x .segment ), int (x .reach )), axis = 1 ).values
1927
1939
for sr in seg_reach_id :
1928
1940
if sr not in df .index :
1929
1941
s , r = [x .lstrip ('0' ) for x in sr .split ('_' )]
1930
- warnings .warn ("Requested segment reach pair ({0},{1}) is not in sfr output. Dropping..." .
1931
- format (int (r ), int (s )), PyemuWarning )
1932
- seg_reach_id = np .delete (seg_reach_id , np .where (seg_reach_id == sr ), axis = 0 )
1942
+ warnings .warn (
1943
+ "Requested segment reach pair ({0},{1}) "
1944
+ "is not in sfr output. Dropping..." .format (
1945
+ int (r ), int (s )), PyemuWarning )
1946
+ seg_reach_id = np .delete (
1947
+ seg_reach_id ,
1948
+ np .where (seg_reach_id == sr ), axis = 0 )
1933
1949
df2 = df .loc [seg_reach_id ].copy ()
1934
1950
if kper in sfr_dict .keys ():
1935
- print ("multiple entries found for kper {0}, replacing..." .format (kper ))
1951
+ print ("multiple entries found for kper {0}, "
1952
+ "replacing..." .format (kper ))
1936
1953
sfr_dict [kper ] = df2
1937
1954
return sfr_dict
1938
1955
0 commit comments