# pull up the flow at the outlet and plot it along with the precipitation # and evapotranspiration dsns = wdm.get_datasets(wdmoutfile) idconss = [wdm.get_attribute(wdmoutfile, n, 'IDCONS') for n in dsns] staids = [wdm.get_attribute(wdmoutfile, n, 'STAID ') for n in dsns] # find the dsn n = [ dsn for dsn, idcons, staid in zip(dsns, idconss, staids) if idcons == 'ROVOL' and staid == '101' ][0] rovol = wdm.get_data(wdmoutfile, n) # close up the fortran files. wdm.close(wdmoutfile) flows = [r * 10**6 / 3600 / 4 for r in rovol] # plot it from matplotlib import pyplot # need a list of the dates/times for the plot times = [ start + i * datetime.timedelta(hours=4)
# uncomment this line to see what's here in the output file # print(dsns, idconss, staids) # one HSPF parameter we saved is ROVOL (PyHSPF has a Postprocessor that can # be used to simplify this, but WDMUtil can also be used more directly). # The following statement finds the dataset number for the ROVOL timeseries # for the reach for subbasin 101. n = [dsn for dsn, idcons, staid in zip(dsns, idconss, staids) if idcons == 'ROVOL' and staid == '101'][0] # get the data for the reach volume flux dataset rovol = wdm.get_data(wdmoutfile, n) # need to close up the files opened by Fortran wdm.close(wdmoutfile) # rovol is the total volume (in Mm3) at each time step. so we need to convert # it m3/s. we could have had HSPF do this, but it's nice to keep track of all # the fluxes for looking at mass balance checks. flows = [r * 10**6 / 3600 / 4 for r in rovol] # plot it up right quick with matplotlib using the plotdate method. from matplotlib import pyplot
wdm.close('test.wdm') # Run example1.uci--this simulation just inputs data from TEST01DT.91 to the # test.wdm file we just made hspf.hsppy('test01.uci', messagepath) # let's go back into the test.wdm and pull out the data and graph it (you may # get a warning about the file being open--i've worked around it but needs # a better fix) wdm.open('test.wdm', 'r') # get the datasets wtemps = wdm.get_data('test.wdm', 134) evaps = wdm.get_data('test.wdm', 41) winds = wdm.get_data('test.wdm', 42) flow1 = wdm.get_data('test.wdm', 113) flow2 = wdm.get_data('test.wdm', 119) dewp1 = wdm.get_data('test.wdm', 124) dewp2 = wdm.get_data('test.wdm', 125) dewp3 = wdm.get_data('test.wdm', 126) sedm = wdm.get_data('test.wdm', 127) flow3 = wdm.get_data('test.wdm', 136) # start and end dates (these are all the same so I will only do this once) start, end = wdm.get_dates('test.wdm', 134) times = [start + i * (end - start) / len(wtemps) for i in range(len(wtemps))]
# See if you can read the data from the WDM file wdm = WDMUtil(verbose=True, messagepath=mssgpath) # ADD BASIN TIMESERIES FROM THE WDM TO HSPFMODEL # open the wdm for read access wdm.open(wdmFile, 'r') start, end = wdm.get_dates(wdmFile, 101) x = 1 # Add specific basin met data for basin in range(0, len(basinRecords)): # The DSNs are known from the exp file so just use those this time prcp = wdm.get_data(wdmFile, 100 + x) evap = wdm.get_data(wdmFile, 200 + x) # Add and assign timeseries data hspfmodel.add_timeseries('precipitation', ('prcp_' + str(x)), start, prcp, tstep=tstep) hspfmodel.add_timeseries('evaporation', ('evap_' + str(x)), start, evap, tstep=tstep) # Assign to specific basin
wdm.open(f2, 'r') # make a list of the datasets and numbers dsns = wdm.get_datasets(f2) tstypes = [wdm.get_attribute(f2, n, 'TSTYPE') for n in dsns] # start date for the BASINS data (after the warmup period) bstart = start + datetime.timedelta(days=warmup) # get the precipitation data i = tstypes.index('PREC') prec = wdm.get_data(f2, dsns[i], start=bstart, end=end) # get the potential evapotranspiration and other climate data dsns = wdm.get_datasets(f1) tstypes = [wdm.get_attribute(f1, n, 'TSTYPE') for n in dsns] dates = [wdm.get_dates(f1, n) for n in dsns] tssteps = [wdm.get_attribute(f1, n, 'TSSTEP') for n in dsns] tcodes = [wdm.get_attribute(f1, n, 'TCODE ') for n in dsns] print(('Time series available in WDM file {}:\n'.format(f1))) for n, t, d, tstep, tcode in zip(dsns, tstypes, dates, tssteps, tcodes): s, e = d print(('{:02d}'.format(n), t, s)) # get the PyHSPF evapotranspiration data
dttm = [ start + t * datetime.timedelta(hours=1) for t in range(int((end - start).total_seconds() / 3600)) ] datOut = [dttm] datNms = ['Date'] for i in dsnBas1: tmpNme = idcons[i - 1] + '_' + staids[i - 1] datNms.append(tmpNme) tmpDat = wdm.get_data(wdmFile, i) datOut.append(tmpDat) volMod = wdm.get_data(wdmFile, 11) wdm.close(wdmFile) datDFOut = pd.DataFrame.from_items(zip(datNms, datOut)) datDFOut.to_csv('basin_1_output.csv', index=False) qMod = [q * 10**4 * 35.314666721 / (60 * 60) for q in volMod] # Read flow data flwData = pd.read_csv(
hspf.hsppy(ucifile, messagepath) wdm = WDMUtil() wdm.open(wdmfile, 'r') dsns = wdm.get_datasets(wdmfile) # see the datasets in the WDM file #for n in dsns: print(n, wdm.get_attribute(wdmfile, n, 'TSTYPE')) # look at the UCI file to get more info on the datasets precip = wdm.get_data(wdmfile, 106) evap = wdm.get_data(wdmfile, 426) pet = wdm.get_data(wdmfile, 425) rovol = wdm.get_data(wdmfile, 420) # acre-ft oflow = wdm.get_data(wdmfile, 281) # cfs start, end = wdm.get_dates(wdmfile, 420) # calculate the watershed area from the SCHEMATIC block in the UCI file area = (32 + 6 + 1318 + 193 + 231 + 84 + 3078 + 449 + 540 + 35) #print('watershed area: {} acres'.format(area)) # convert ROVOL and observed flows to m3/s
# first pass: subbasin if staid == o.subbasin: # second: land use category if descrp == o.landtype: # third: constituent id if idcons == v: # get the time series data data = wdmutil.get_data(output, n, start=start, end=end) # add the total to the database results[c][o.landtype][v] = data.sum() # go through the pervious land segments and get the area and runoff for o in hspfmodel.perlnds: c = o.subbasin # add the area results[c][o.landtype] = {'area': o.area}
f = 'hunting.wdm' # import from exp to wdm wdm.import_exp(hunthour, f) # copy the data to the hspfmodel using WDMUtil # open the wdm for read access 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) oflow = wdm.get_data(f, 281) start, end = wdm.get_dates(f, 106) # close up the wdm file (forgetting this WILL cause trouble) wdm.close('hunting.wdm') # the evaporation data is daily so it needs to be disaggregated to hourly for # an hourly simulation (see how easy this is with Python) # the time series in the WDM file starts at 1 am so had to add one extra # value to the beginning of the time series for consistency evap = [0] + [e / 24 for e in evap for i in range(24)]
dsns = wdm.get_datasets(f) # find all the time series types # (this is how they are identified in the exp file) tstypes = [wdm.get_attribute(f, n, 'TSTYPE') for n in dsns] # find the precip and evap timeseries (we could also just look at the exp files # to figure this out, but this illustrates some of the flexibility of PyHSPF) precip_dsn = dsns[tstypes.index('HPCP')] evap_dsn = dsns[tstypes.index('EVAP')] # get the time series and start and end dates precip = wdm.get_data(f, precip_dsn) start, end = wdm.get_dates(f, precip_dsn) evap = wdm.get_data(f, evap_dsn, start=start, end=end) # the observed flow is dsn 281 oflow = wdm.get_data(f, 281, start=start, end=end) # close up the wdm file (forgetting this WILL cause trouble) wdm.close('hunting.wdm') # make a list of the times in the daily time series using datetime "timedelta"
hspf.hsppy(ucifile, messagepath) wdm = WDMUtil() wdm.open(wdmfile, 'r') dsns = wdm.get_datasets(wdmfile) # see the datasets in the WDM file #for n in dsns: print(n, wdm.get_attribute(wdmfile, n, 'TSTYPE')) # look at the UCI file to get more info on the datasets precip = wdm.get_data(wdmfile, 106) evap = wdm.get_data(wdmfile, 426) pet = wdm.get_data(wdmfile, 425) rovol = wdm.get_data(wdmfile, 420) # acre-ft oflow = wdm.get_data(wdmfile, 281) # cfs start, end = wdm.get_dates(wdmfile, 420) # calculate the watershed area from the SCHEMATIC block in the UCI file area = (32 + 6 + 1318 + 193 + 231 + 84 +
wdm.open(f2, 'r') # make a list of the datasets and numbers dsns = wdm.get_datasets(f2) tstypes = [wdm.get_attribute(f2, n, 'TSTYPE') for n in dsns] # start date for the BASINS data (after the warmup period) bstart = start + datetime.timedelta(days = warmup) # get the precipitation data i = tstypes.index('PREC') prec = wdm.get_data(f2, dsns[i], start = bstart, end = end) # get the potential evapotranspiration and other climate data dsns = wdm.get_datasets(f1) tstypes = [wdm.get_attribute(f1, n, 'TSTYPE') for n in dsns] dates = [wdm.get_dates(f1, n) for n in dsns] tssteps = [wdm.get_attribute(f1, n, 'TSSTEP') for n in dsns] tcodes = [wdm.get_attribute(f1, n, 'TCODE ') for n in dsns] print('Time series available in WDM file {}:\n'.format(f1)) for n, t, d, tstep, tcode in zip(dsns, tstypes, dates, tssteps, tcodes): s, e = d print('{:02d}'.format(n), t, s) # get the PyHSPF evapotranspiration data
wdm.open(wdmoutfile, 'r') # pull up the flow at the outlet and plot it along with the precipitation # and evapotranspiration dsns = wdm.get_datasets(wdmoutfile) idconss = [wdm.get_attribute(wdmoutfile, n, 'IDCONS') for n in dsns] staids = [wdm.get_attribute(wdmoutfile, n, 'STAID ') for n in dsns] # find the dsn n = [dsn for dsn, idcons, staid in zip(dsns, idconss, staids) if idcons == 'ROVOL' and staid == '101'][0] rovol = wdm.get_data(wdmoutfile, n) # close up the fortran files. wdm.close(wdmoutfile) flows = [r * 10**6 / 3600 / 4 for r in rovol] # plot it from matplotlib import pyplot # need a list of the dates/times for the plot times = [start + i * datetime.timedelta(hours = 4) for i in range(int((end - start).total_seconds() / 3600 / 4))]
dsns = wdm.get_datasets(f) # find all the time series types # (this is how they are identified in the exp file) tstypes = [wdm.get_attribute(f, n, 'TSTYPE') for n in dsns] # find the precip and evap timeseries (we could also just look at the exp files # to figure this out, but this illustrates some of the flexibility of PyHSPF) precip_dsn = dsns[tstypes.index('HPCP')] evap_dsn = dsns[tstypes.index('EVAP')] # get the time series and start and end dates precip = wdm.get_data(f, precip_dsn) start, end = wdm.get_dates(f, precip_dsn) evap = wdm.get_data(f, evap_dsn, start = start, end = end) # the observed flow is dsn 281 oflow = wdm.get_data(f, 281, start = start, end = end) # close up the wdm file (forgetting this WILL cause trouble) wdm.close('hunting.wdm') # make a list of the times in the daily time series using datetime "timedelta"
# print(dsns, idconss, staids) # one HSPF parameter we saved is ROVOL (PyHSPF has a Postprocessor that can # be used to simplify this, but WDMUtil can also be used more directly). # The following statement finds the dataset number for the ROVOL timeseries # for the reach for subbasin 101. n = [ dsn for dsn, idcons, staid in zip(dsns, idconss, staids) if idcons == 'ROVOL' and staid == '101' ][0] # get the data for the reach volume flux dataset rovol = wdm.get_data(wdmoutfile, n) # need to close up the files opened by Fortran wdm.close(wdmoutfile) # rovol is the total volume (in Mm3) at each time step. so we need to convert # it m3/s. we could have had HSPF do this, but it's nice to keep track of all # the fluxes for looking at mass balance checks. flows = [r * 10**6 / 3600 / 4 for r in rovol] # plot it up right quick with matplotlib using the plotdate method. from matplotlib import pyplot