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 run(self, model, targets = ['reach_outvolume', 'groundwater', ], start = None, end = None, warmup = 30, verbose = False, ): """Creates a copy of the base model, adjusts a parameter value, runs the simulation, calculates and returns the perturbation.""" # if dates provided use, otherwise use default if start is None: start = self.start if end is None: end = self.end # build the input files and run model.build_wdminfile() model.warmup(start, days = warmup, hydrology = True) model.build_uci(targets, start, end, hydrology = True) model.run(verbose = verbose) # get the regression information using the postprocessor p = Postprocessor(model, (start, end)) p.get_calibration() p.calculate_errors(output = None, verbose = False) dr2, logdr2, dNS, logdNS, mr2, logmr2, mN2, logMS = p.regression p.close() model = None p = None perturbation = None if self.optimization == 'Nash-Sutcliffe Product': return dNS * logdNS if self.optimization == 'Nash-Sutcliffe Efficiency': return dNS
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)
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)
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)