55import logging
66from log import LOG_NAME
77from write import write_param_file
8- from share import NcGlobals , SECSPERDAY
8+ from share import NcGlobals , SECSPERDAY , MAX_NC_CHARS
99import os
1010from datetime import date
1111import plots
@@ -48,26 +48,28 @@ def finish_params(outlets, dom_data, config_dict, directories):
4848 outlets , full_time_length , \
4949 before , after = subset (outlets , subset_length = subset_length )
5050
51+ slc = slice (min (len (before ), 1000 ))
52+
5153 log .debug ('plotting unit hydrograph timeseries now for before'
5254 ' / after subseting' )
5355
5456 title = 'UHS before subset'
55- pfname = plots .uhs (before , title , options ['CASEID' ],
57+ pfname = plots .uhs (before [ slc ] , title , options ['CASEID' ],
5658 directories ['plots' ])
5759 log .info ('%s Plot: %s' , title , pfname )
5860
5961 title = 'UHS after subset'
60- pfname = plots .uhs (after , title , options ['CASEID' ],
62+ pfname = plots .uhs (after [ slc ] , title , options ['CASEID' ],
6163 directories ['plots' ])
6264 log .info ('%s Plot: %s' , title , pfname )
6365 else :
64- subset_length = routing ['BASIN_FLOWDAYS' ]* SECSPERDAY / routing ['OUTPUT_INTERVAL' ]
6566 log .info ('Not subsetting because either SUBSET_DAYS is null or '
6667 'SUBSET_DAYS<BASIN_FLOWDAYS' )
67- for i , ( cell_id , outlet ) in enumerate ( outlets .iteritems () ):
68- outlets [ cell_id ] .offset = np .zeros (outlet .unit_hydrograph .shape [1 ],
69- dtype = np .int16 )
68+ for outlet in outlets .itervalues ( ):
69+ outlet .offset = np .zeros (outlet .unit_hydrograph .shape [1 ],
70+ dtype = np .int16 )
7071 full_time_length = outlet .unit_hydrograph .shape [0 ]
72+ subset_length = full_time_length
7173 # ---------------------------------------------------------------- #
7274
7375 # ---------------------------------------------------------------- #
@@ -94,7 +96,7 @@ def finish_params(outlets, dom_data, config_dict, directories):
9496
9597 # ---------------------------------------------------------------- #
9698 # Group
97- grouped_data = group (outlets )
99+ grouped_data = group (outlets , subset_length )
98100
99101 # unpack grouped data
100102 unit_hydrograph = grouped_data ['unit_hydrograph' ]
@@ -290,12 +292,12 @@ def subset(outlets, subset_length=None):
290292 """ Shorten the Unit Hydrograph"""
291293
292294 log .info ('subsetting unit-hydrographs now...' )
293- log .debug ('Subset Length: %s' % subset_length )
295+ log .debug ('Subset Length: %s' , subset_length )
294296
295297 for i , (cell_id , outlet ) in enumerate (outlets .iteritems ()):
296298 if i == 0 :
297299 full_time_length = outlet .unit_hydrograph .shape [0 ]
298- log .debug ('Subset Length: %s' % subset_length )
300+ log .debug ('Subset Length: %s' , subset_length )
299301 if not subset_length :
300302 subset_length = full_time_length
301303 log .debug ('No subset_length provided, using full_time_length' )
@@ -362,93 +364,86 @@ def subset(outlets, subset_length=None):
362364
363365
364366# -------------------------------------------------------------------- #
365- def group (outlets ):
367+ def group (outlets , subset_length ):
366368 """
367369 group the outlets into one set of arrays
368370 """
369371
370- for i , (cell_id , outlet ) in enumerate (outlets .iteritems ()):
372+ n_outlets = len (outlets )
373+ n_sources = 0
374+ for outlet in outlets .itervalues ():
375+ n_sources += len (outlet .y_source )
371376
372- y = outlet .y_source
373- x = outlet .x_source
377+ gd = {}
374378
375- if i == 0 :
376- # -------------------------------------------------------- #
377- # Source specific values
378- unit_hydrograph = outlet .unit_hydrograph
379- frac_sources = outlet .fractions
380- source_lon = outlet .lon_source
381- source_lat = outlet .lat_source
382- source_x_ind = x
383- source_y_ind = y
384- source_decomp_ind = outlet .cell_id_source
385- source_time_offset = outlet .offset
386- source2outlet_ind = np .zeros (len (outlet .fractions ))
387- # -------------------------------------------------------- #
388-
389- # -------------------------------------------------------- #
390- # outlet specific inputs
391- outlet_lon = np .array (outlet .lon , dtype = np .float64 )
392- outlet_lat = np .array (outlet .lat , dtype = np .float64 )
393- outlet_x_ind = np .array (outlet .domx , dtype = np .int16 )
394- outlet_y_ind = np .array (outlet .domy , dtype = np .int16 )
395- outlet_decomp_ind = np .array (cell_id , dtype = np .int16 )
396- outlet_number = np .array (i , dtype = np .int16 )
397- outlet_name = np .array (outlet .name )
398- outlet_upstream_gridcells = np .array (outlet .upstream_gridcells ,
399- dtype = np .int16 )
400- outlet_upstream_area = np .array (outlet .upstream_area ,
401- dtype = np .float64 )
402- else :
403- # -------------------------------------------------------- #
404- # Point specific values
405- unit_hydrograph = np .append (unit_hydrograph ,
406- outlet .unit_hydrograph , axis = 1 )
407- frac_sources = np .append (frac_sources , outlet .fractions )
408- source_lon = np .append (source_lon , outlet .lon_source )
409- source_lat = np .append (source_lat , outlet .lat_source )
410- source_x_ind = np .append (source_x_ind , x )
411- source_y_ind = np .append (source_y_ind , y )
412- source_decomp_ind = np .append (source_decomp_ind ,
413- outlet .cell_id_source )
414- source_time_offset = np .append (source_time_offset , outlet .offset )
415- source2outlet_ind = np .append (source2outlet_ind ,
416- np .zeros_like (outlet .offset ) + i )
417- # -------------------------------------------------------- #
418-
419- # -------------------------------------------------------- #
420- # outlet specific inputs
421- outlet_lon = np .append (outlet_lon , outlet .lon )
422- outlet_lat = np .append (outlet_lat , outlet .lat )
423- outlet_x_ind = np .append (outlet_x_ind , outlet .domx )
424- outlet_y_ind = np .append (outlet_y_ind , outlet .domy )
425- outlet_decomp_ind = np .append (outlet_decomp_ind , cell_id )
426- outlet_number = np .append (outlet_number , i )
427- outlet_name = np .append (outlet_name , outlet .name )
428- outlet_upstream_gridcells = np .append (outlet_upstream_gridcells ,
429- outlet .upstream_gridcells )
430- outlet_upstream_area = np .append (outlet_upstream_area ,
431- outlet .upstream_area )
432- # -------------------------------------------------------- #
433-
434- grouped_data = {'unit_hydrograph' : unit_hydrograph ,
435- 'frac_sources' : frac_sources ,
436- 'source_lon' : source_lon ,
437- 'source_lat' : source_lat ,
438- 'source_x_ind' : source_x_ind ,
439- 'source_y_ind' : source_y_ind ,
440- 'source_decomp_ind' : source_decomp_ind ,
441- 'source_time_offset' : source_time_offset ,
442- 'source2outlet_ind' : source2outlet_ind ,
443- 'outlet_lon' : outlet_lon ,
444- 'outlet_lat' : outlet_lat ,
445- 'outlet_x_ind' : outlet_x_ind ,
446- 'outlet_y_ind' : outlet_y_ind ,
447- 'outlet_decomp_ind' : outlet_decomp_ind ,
448- 'outlet_number' : outlet_number ,
449- 'outlet_name' : outlet_name ,
450- 'outlet_upstream_gridcells' : outlet_upstream_gridcells ,
451- 'outlet_upstream_area' : outlet_upstream_area }
452-
453- return grouped_data
379+ log .debug ('n_outlets: %s' , n_outlets )
380+ log .debug ('n_sources: %s' , n_sources )
381+ log .debug ('subset_length: %s' , subset_length )
382+
383+ # ---------------------------------------------------------------- #
384+ # Source specific values
385+ gd ['unit_hydrograph' ] = np .empty ((subset_length , n_sources ),
386+ dtype = np .float64 )
387+ gd ['frac_sources' ] = np .empty (n_sources , dtype = np .float64 )
388+ gd ['source_lon' ] = np .empty (n_sources , dtype = np .float64 )
389+ gd ['source_lat' ] = np .empty (n_sources , dtype = np .float64 )
390+ gd ['source_x_ind' ] = np .empty (n_sources , dtype = np .int16 )
391+ gd ['source_y_ind' ] = np .empty (n_sources , dtype = np .int16 )
392+ gd ['source_decomp_ind' ] = np .empty (n_sources , dtype = np .int16 )
393+ gd ['source_time_offset' ] = np .empty (n_sources , dtype = np .int16 )
394+ gd ['source2outlet_ind' ] = np .empty (n_sources , dtype = np .int16 )
395+ # ---------------------------------------------------------------- #
396+
397+ # ---------------------------------------------------------------- #
398+ # outlet specific inputs
399+ gd ['outlet_lon' ] = np .empty (n_outlets , dtype = np .float64 )
400+ gd ['outlet_lat' ] = np .empty (n_outlets , dtype = np .float64 )
401+ gd ['outlet_x_ind' ] = np .empty (n_outlets , dtype = np .int16 )
402+ gd ['outlet_y_ind' ] = np .empty (n_outlets , dtype = np .int16 )
403+ gd ['outlet_decomp_ind' ] = np .empty (n_outlets , dtype = np .int16 )
404+ gd ['outlet_number' ] = np .empty (n_outlets , dtype = np .int16 )
405+ gd ['outlet_name' ] = np .empty (n_outlets , dtype = "S{0}" .format (MAX_NC_CHARS ))
406+ gd ['outlet_upstream_gridcells' ] = np .empty (n_outlets , dtype = np .int16 )
407+ gd ['outlet_upstream_area' ] = np .empty (n_outlets , dtype = np .float64 )
408+ # ---------------------------------------------------------------- #
409+
410+ # ---------------------------------------------------------------- #
411+ # place outlet and source vars into gd dictionary
412+ a = 0
413+ for i , (cell_id , outlet ) in enumerate (outlets .iteritems ()):
414+ b = a + len (outlet .y_source )
415+ log .debug ('unit_hydrograph.shape %s' , outlet .unit_hydrograph .shape )
416+ # -------------------------------------------------------- #
417+ # Point specific values
418+ gd ['unit_hydrograph' ][:, a :b ] = outlet .unit_hydrograph
419+ gd ['frac_sources' ][a :b ] = outlet .fractions
420+ gd ['source_lon' ][a :b ] = outlet .lon_source
421+ gd ['source_lat' ][a :b ] = outlet .lat_source
422+ gd ['source_x_ind' ][a :b ] = outlet .x_source
423+ gd ['source_y_ind' ][a :b ] = outlet .y_source
424+ gd ['source_decomp_ind' ][a :b ] = outlet .cell_id_source
425+ gd ['source_time_offset' ][a :b ] = outlet .offset
426+ gd ['source2outlet_ind' ][a :b ] = i
427+ # -------------------------------------------------------- #
428+
429+ # -------------------------------------------------------- #
430+ # outlet specific inputs
431+ gd ['outlet_lon' ][i ] = outlet .lon
432+ gd ['outlet_lat' ][i ] = outlet .lat
433+ gd ['outlet_x_ind' ][i ] = outlet .domx
434+ gd ['outlet_y_ind' ][i ] = outlet .domy
435+ gd ['outlet_decomp_ind' ][i ] = cell_id
436+ gd ['outlet_number' ][i ] = i
437+ gd ['outlet_name' ][i ] = outlet .name
438+ gd ['outlet_upstream_gridcells' ][i ] = outlet .upstream_gridcells
439+ gd ['outlet_upstream_area' ][i ] = outlet .upstream_area
440+ # -------------------------------------------------------- #
441+
442+ # -------------------------------------------------------- #
443+ # update src counter
444+ a = b
445+ # -------------------------------------------------------- #
446+ # ---------------------------------------------------------------- #
447+
448+ return gd
454449# -------------------------------------------------------------------- #
0 commit comments