def postprocess(): """Postprocess the results and save.""" print('saving the calibration results\n') # open the calibrated model with open(calibrated, 'rb') as f: hunting = pickle.load(f) # external targets for calibration targets = [ 'reach_outvolume', 'evaporation', 'groundwater', 'runoff', 'water_state' ] # build the HSPF input files hunting.build_wdminfile() hunting.build_uci(targets, start, end, hydrology=True) # run it hunting.run(verbose=True) # create a postprocessor to analyze the results postprocessor = Postprocessor(hunting, (start, end)) # make a report of the calibration report = '{}/calibration_report.csv'.format(hspf) postprocessor.get_hspexp_parameters(verbose=False) postprocessor.get_calibration() postprocessor.calculate_errors(output=report, verbose=False) # plot the daily hydrograph hydrograph = '{}/dailyhydrograph'.format(hspf) postprocessor.plot_hydrograph(tstep='daily', output=hydrograph, show=False) # plot the monthly hydrograph hydrograph = '{}/monthlyhydrograph'.format(hspf) postprocessor.plot_hydrograph(tstep='monthly', output=hydrograph, show=False) # plot the calibration calibration_graph = '{}/calibration'.format(hspf) postprocessor.plot_calibration(output=calibration_graph, show=False)
def get_postprocessor(self, hspfmodel, dates, snowdata=None, verbose=True): """Postprocesses the data.""" if verbose: print('postprocessing simulation results\n') start, tstep, data = hspfmodel.flowgages[self.gageid] end = start + datetime.timedelta(minutes=tstep) * len(data) if self.process_dates[0] < start or end < self.process_dates[1]: print(('warning: missing data; processing only period of ' + 'available gage data\n')) process_dates = start, end else: process_dates = self.process_dates postprocessor = Postprocessor(hspfmodel, process_dates, comid=self.gagecomid, upcomids=self.upcomids) return postprocessor
def validate(directory, HUC8, NWISgage, start, end, temp = True, snow = True, hydrology = True, warmup = 1, upcomids = []): run_dates = (datetime.datetime(start, 1, 1), datetime.datetime(end, 1, 1)) process_dates = (datetime.datetime(start + warmup, 1, 1), datetime.datetime(end, 1, 1)) f = '{}/{}/calibrations/{}/hspfmodel'.format(directory, HUC8, NWISgage) folder = '{}/{}/validations/{}'.format(directory, HUC8, NWISgage) with open(f, 'rb') as m: hspfmodel = pickle.load(m) # figure out the external targets needed targets = ['reach_outvolume', 'groundwater', 'water_state', 'evaporation', 'runoff', 'snow_state', 'snowpack', 'snowfall'] # build the input wdmfile hspfmodel.build_wdminfile() # create the UCI file and the output WDM file hspfmodel.build_uci(targets, run_dates[0], run_dates[1], temp = temp, snow = snow, hydrology = hydrology) hspfmodel.messagepath = None # run it hspfmodel.run(verbose = True) # add the simulation parameters to the calibrator postprocessor = Postprocessor(hspfmodel, process_dates) # calculate the errors and make a calibration report postprocessor.get_hspexp_parameters(verbose = False) postprocessor.get_calibration(verbose = False, vverbose = False) # plot everything and save to file if not os.path.isdir('{}/{}/validations'.format(directory, HUC8)): os.mkdir('{}/{}/validations'.format(directory, HUC8)) if not os.path.isdir(folder): os.mkdir(folder) output = folder + '/calibration_report.csv' postprocessor.calculate_errors(verbose = False, output = output) output = folder + '/snow' postprocessor.plot_snow(output = output, show = False) output = folder + '/snowcalibration' postprocessor.plot_snowcalibration(output = output, show = False) output = folder + '/monthlyhydrograph' postprocessor.plot_hydrograph(output = output, show = False) output = folder + '/dayofyear' postprocessor.plot_dayofyear(output = output, show = False) output = folder + '/averagewaterbudget' postprocessor.plot_waterbudget(output = output, show = False) output = folder + '/waterbudgets' postprocessor.plot_allwaterbudgets(output = output, show = False) output = folder + '/runoff' postprocessor.plot_runoff(output = output, show = False) output = folder + '/storms' postprocessor.plot_storms(output = output, show = False) output = folder + '/calibration' postprocessor.plot_calibration(output = output, show = False) postprocessor.close()
def postprocess(): # open the calibrated model with open(calibrated, 'rb') as f: hunting = pickle.load(f) # external targets for calibration targets = ['reach_outvolume', 'evaporation', 'groundwater', 'runoff', 'water_state' ] # build the HSPF input files hunting.build_wdminfile() hunting.build_uci(targets, start, end, hydrology = True) # run it hunting.run(verbose = True) # create a postprocessor to analyze the results postprocessor = Postprocessor(hunting, (start, end)) # make a report of the calibration report = '{}/calibration_report.csv'.format(hspf) postprocessor.get_hspexp_parameters(verbose = False) postprocessor.get_calibration() postprocessor.calculate_errors(output = report, verbose = False) # plot the daily hydrograph hydrograph = '{}/dailyhydrograph'.format(hspf) postprocessor.plot_hydrograph(tstep = 'daily', output = hydrograph, show = False) # plot the monthly hydrograph hydrograph = '{}/monthlyhydrograph'.format(hspf) postprocessor.plot_hydrograph(tstep = 'monthly', output = hydrograph, show = False) # plot the calibration calibration_graph = '{}/calibration'.format(hspf) postprocessor.plot_calibration(output = calibration_graph, show = False) # storm events from HSPExp file (for plotting) storms = [[datetime.datetime(1989, 2, 20), datetime.datetime(1989, 2, 26)], [datetime.datetime(1989, 5, 1), datetime.datetime(1989, 5, 5)], [datetime.datetime(1989, 6, 6), datetime.datetime(1989, 6, 11)], [datetime.datetime(1989, 9, 25), datetime.datetime(1989, 9, 29)], [datetime.datetime(1989,10, 1), datetime.datetime(1989,10, 5)], [datetime.datetime(1989,12, 31), datetime.datetime(1990, 1, 5)], [datetime.datetime(1990, 1, 25), datetime.datetime(1990, 1, 30)], [datetime.datetime(1990, 5, 4), datetime.datetime(1990, 5, 8)], [datetime.datetime(1990, 5, 28), datetime.datetime(1990, 6, 1)], [datetime.datetime(1990, 6, 15), datetime.datetime(1990, 6, 18)], ] # plot the storm events storm_plots = '{}/storms'.format(hspf) postprocessor.plot_storms(stormdates = storms, output = storm_plots, tstep = 'hourly', show = False)