Example #1
0
def calibrate():

    with open(calibrated, "rb") as f:
        hunting = pickle.load(f)

    # find the comid of the gage

    flowgages = {v: k for k, v in hunting.subbasin_timeseries["flowgage"].items()}

    comid = flowgages["Hunting"]

    # create an instance of the autocalibrator and provide it with the
    # HSPFModel, the start and end dates, the working directory for the
    # calibration testing, the comid for the gage, and turn on the
    # hydrology (and other) modules

    calibrator = AutoCalibrator(hunting, start, end, hspf, comid=comid, hydrology=True)

    # provide the output location for the calibrated model, the variables
    # to use in the calibration, the optimization parameter, the perturbation
    # levels, and the parallel flag

    calibrator.autocalibrate(
        calibrated, variables=variables, optimization=optimization, perturbations=perturbations, parallel=parallel
    )

    for variable, value in zip(calibrator.variables, calibrator.values):

        print("{:6s} {:5.3f}".format(variable, value))

    print("\nsaving the calibration results\n")
Example #2
0
def calibrate():

    with open(calibrated, 'rb') as f: hunting = pickle.load(f)

    # find the comid of the gage

    flowgages = {v:k 
                 for k, v in list(hunting.subbasin_timeseries['flowgage'].items())}

    comid = flowgages['Hunting']

    # create an instance of the autocalibrator and provide it with the 
    # HSPFModel, the start and end dates, the working directory for the 
    # calibration testing, the comid for the gage, and turn on the 
    # hydrology (and other) modules

    calibrator = AutoCalibrator(hunting, start, end, hspf, comid = comid,
                                hydrology = True)

    # provide the output location for the calibrated model, the variables
    # to use in the calibration, the optimization parameter, the perturbation
    # levels, and the parallel flag

    calibrator.autocalibrate(calibrated,
                             variables = variables, 
                             optimization = optimization,
                             perturbations = perturbations,
                             parallel = parallel,
                             )

    for variable, value in zip(calibrator.variables, calibrator.values):

        print(('{:6s} {:5.3f}'.format(variable, value)))

    print('\nsaving the calibration results\n')
Example #3
0
def calibrate():

    with open(calibrated, 'rb') as f: hunting = pickle.load(f)

    calibrator = AutoCalibrator(hunting, start, end, hspf)

    calibrator.autocalibrate(calibrated,
                             variables = variables, 
                             optimization = optimization,
                             perturbations = perturbations,
                             parallel = parallel
                             )

    for variable, value in zip(calibrator.variables, calibrator.values):

        print('{:6s} {:5.3f}'.format(variable, value))

    print('\nsaving the calibration results\n')
Example #4
0
    calibrated = '{}/{}'.format(calibration, gageid)

    # make the directory for the calibration simulations

    if not os.path.isdir(calibration): os.mkdir(calibration)

    # make an instance of the autocalibrator and give it the path to the model,
    # the start and end dates for the calibration period, the working directory
    # location to use for simulation input and output files, the NWIS id
    # (or comid) of the gage, and the HSPF modules to use (SNOW, PWATER, etc.)

    calibrator = AutoCalibrator(filename,
                                start,
                                end,
                                directory,
                                warmup=warmup,
                                gageid=gageid,
                                atemp=True,
                                snow=True,
                                hydrology=True)

    # calibrate the model and save it to the "calibrated" location

    calibrator.autocalibrate(
        calibrated,
        variables=variables,
        optimization=optimization,
        perturbations=perturbations,
        parallel=parallel,
        nprocessors=nprocessors,
    )
Example #5
0
    # file path to place the calibrated model and results

    calibration = '{}/{}/calibration'.format(destination, HUC8)

    # path where the calibrated model will be saved/located

    calibrated = '{}/{}'.format(calibration, gageid)

    # make the directory for the calibration simulations

    if not os.path.isdir(calibration): os.mkdir(calibration)

    # make an instance of the autocalibrator and give it the path to the model,
    # the start and end dates for the calibration period, the working directory 
    # location to use for simulation input and output files, the NWIS id 
    # (or comid) of the gage, and the HSPF modules to use (SNOW, PWATER, etc.)

    calibrator = AutoCalibrator(filename, start, end, directory, 
                                warmup = warmup, gageid = gageid, atemp = True,
                                snow = True, hydrology = True)

    # calibrate the model and save it to the "calibrated" location

    calibrator.autocalibrate(calibrated,
                             variables = variables, 
                             optimization = optimization,
                             perturbations = perturbations,
                             parallel = parallel,
                             nprocessors = nprocessors,
                             )
Example #6
0
def main():

    # create an instance of the NWIS extractor

    nwisextractor = NWISExtractor(NWIS)

    # download and decompress the source metadata files

    nwisextractor.download_metadata()

    # extract all the gage stations and metadata into a shapefile for the HUC8

    nwisextractor.extract_HUC8(HUC8, output)

    # tell the extractor to use the metadata file above to find gage data

    nwisextractor.set_metadata(gagefile)

    # create an instance of the NHDPlus extractor

    nhdplusextractor = NHDPlusExtractor(drainid, VPU, NHDPlus)

    # download and decompress the source data for the Mid Atlantic Region

    nhdplusextractor.download_data()

    # extract the HUC8 data for the Patuxent watershed

    nhdplusextractor.extract_HUC8(HUC8, output)

    # create an instance of the NHDPlusDelineator to use to build the Watershed

    delineator = NHDPlusDelineator(VAAfile, flowfile, catchfile, elevfile,
                                   gagefile = gagefile)

    # delineate the watershed (extract the flowlines, catchments and other data)

    delineator.delineate_gage_watershed(gageid, output = gagepath)

    # add land use data from 1988 to the delineator

    delineator.add_basin_landuse(1988, landuse)

    # build the watershed

    delineator.build_gage_watershed(gageid, watershed, masslinkplot = masslink)

    # make the working directory for HSPF simulation files

    if not os.path.isdir(hspf): os.mkdir(hspf)

    # import old data for Hunting Creek

    wdm = WDMUtil()

    # path to hspexp2.4 data files (modify as needed)

    directory = os.path.abspath(os.path.dirname(__file__)) + '/data'

    # the data from the export file (*.exp) provided with hspexp need to be 
    # imported into a wdm file. WDMUtil has a method for this.

    hunthour = '{}/hunthour/huntobs.exp'.format(directory)

    f = 'temp.wdm'

    # import from exp to wdm

    wdm.import_exp(hunthour, f)

    # close the file and re-open the wdm for read access

    wdm.close(f)
    wdm.open(f, 'r')

    # the dsns are known from the exp file so just use those this time

    precip = wdm.get_data(f, 106)
    evap   = wdm.get_data(f, 111)
    flow   = wdm.get_data(f, 281)

    s, e = wdm.get_dates(f, 106)

    # add the time series to deal with HSPF looking backward stepping

    precip = [0] + [p * 25.4 for p in precip]
    evap   = [e * 25.4 / 24 for e in evap for i in range(24)]

    wdm.close(f)

    # create an HSPF model instance

    hunting = HSPFModel()

    # open the watershed built above

    with open(watershed, 'rb') as f: w = pickle.load(f)

    # use the data to build an HSPFModel

    hunting.build_from_watershed(w, model, ifraction = 1., verbose = True)

    # turn on the hydrology modules to the HSPF model

    hunting.add_hydrology()

    # add precip timeseries with label BWI and provided start date to the model

    hunting.add_timeseries('precipitation', 'BWI', s, precip)

    # add evap timeseries with label Beltsville and provided start date 

    hunting.add_timeseries('evaporation', 'Beltsville', s, evap)

    # add flow timeseries with label Hunting, start date, tstep (days)

    hunting.add_timeseries('flowgage', 'Hunting', s, flow, tstep = 60)

    # assign the evaporation and precipiation timeseries to the whole watershed

    hunting.assign_watershed_timeseries('precipitation', 'BWI')
    hunting.assign_watershed_timeseries('evaporation', 'Beltsville')

    # find the subbasin indentfier for the watershed outlet

    subbasin = [up for up, down in w.updown.items() if down == 0][0]

    # assign the flowgage to the outlet subbasin

    hunting.assign_subbasin_timeseries('flowgage', subbasin, 'Hunting')

    # using pan evaporation data, so need a pan coefficient < 1

    hunting.evap_multiplier = 0.75

    calibrator = AutoCalibrator(hunting, start, end, hspf)

    calibrator.autocalibrate(calibrated,
                             variables = variables, 
                             optimization = optimization,
                             perturbations = perturbations,
                             parallel = parallel
                             )

    for variable, value in zip(calibrator.variables, calibrator.values):

        print('{:6s} {:5.3f}'.format(variable, value))

    print('\nsaving the calibration results\n')
Example #7
0
def calibrate():
    """Builds and calibrates the model."""

    # make the working directory for HSPF

    if not os.path.isdir(hspf): os.mkdir(hspf)

    # import old data for Hunting Creek

    wdm = WDMUtil()

    # path to hspexp2.4 data files (modify as needed)

    directory = os.path.abspath(os.path.dirname(__file__)) + '/data'

    # the data from the export file (*.exp) provided with hspexp need to be
    # imported into a wdm file. WDMUtil has a method for this.

    hunthour = '{}/hunthour/huntobs.exp'.format(directory)

    f = 'temp.wdm'

    # import from exp to wdm

    wdm.import_exp(hunthour, f)

    # close the file and re-open the wdm for read access

    wdm.close(f)
    wdm.open(f, 'r')

    # the dsns are known from the exp file so just use those this time

    precip = wdm.get_data(f, 106)
    evap = wdm.get_data(f, 111)
    flow = wdm.get_data(f, 281)

    s, e = wdm.get_dates(f, 106)

    # add the time series to deal with HSPF looking backward stepping

    precip = [0] + [p * 25.4 for p in precip]
    evap = [e * 25.4 / 24 for e in evap for i in range(24)]

    wdm.close(f)

    # create an HSPF model instance

    hunting = HSPFModel()

    # open the watershed built above

    with open(watershed, 'rb') as f:
        w = pickle.load(f)

    # use the data to build an HSPFModel

    hunting.build_from_watershed(w, model, verbose=True)

    # turn on the hydrology modules to the HSPF model

    hunting.add_hydrology()

    # add precip timeseries with label BWI and provided start date to the model

    hunting.add_timeseries('precipitation', 'BWI', s, precip)

    # add evap timeseries with label Beltsville and provided start date

    hunting.add_timeseries('evaporation', 'Beltsville', s, evap)

    # add flow timeseries with label Hunting, start date, tstep (days)

    #hunting.add_timeseries('flowgage', 'Hunting', start, flow, tstep = 1440)
    hunting.add_timeseries('flowgage', 'Hunting', s, flow, tstep=60)

    # assign the evaporation and precipiation timeseries to the whole watershed

    hunting.assign_watershed_timeseries('precipitation', 'BWI')
    hunting.assign_watershed_timeseries('evaporation', 'Beltsville')

    # find the subbasin indentfier for the watershed outlet

    subbasin = [up for up, down in w.updown.items() if down == 0][0]

    # assign the flowgage to the outlet subbasin

    hunting.assign_subbasin_timeseries('flowgage', subbasin, 'Hunting')

    # using pan evaporation data, so need a pan coefficient < 1

    hunting.evap_multiplier = 0.75

    calibrator = AutoCalibrator(hunting, start, end, hspf)

    calibrator.autocalibrate(calibrated,
                             variables=variables,
                             optimization=optimization,
                             perturbations=perturbations,
                             parallel=parallel)

    for variable, value in zip(calibrator.variables, calibrator.values):

        print('{:6s} {:5.3f}'.format(variable, value))

    print('')