def funcP(iP, axP): siteNo = siteNoLst[iP] df = dbBasin.readSiteTS(siteNo, varLst=codeP, freq='D') area = dfCrd.loc[siteNo]['DRAIN_SQKM'] for k, code in enumerate(codeP): axplot.plotTS(axP[k], df.index, df[code].values, cLst='k') axP[k].set_title('{} of {} {}'.format(code, siteNo, area))
def funcP(iP, axP): siteNo = siteNoSel[iP] df = dbBasin.readSiteTS(siteNo, varLst=codeSel, freq='D') area = dfCrd.loc[siteNo]['DRAIN_SQKM'] axplot.multiTS(axP, df.index.values, df[codeSel].values, labelLst=codeSel) figP.subplots_adjust(hspace=0) figP.suptitle('{} {}'.format(siteNo, area))
import torch import os import json import numpy as np import pandas as pd import time import matplotlib.pyplot as plt from hydroDL.data import dbBasin siteNo = '06800500' codeLst = ['00915', '00925'] df = GLASS.readBasin(siteNo) # plot data df = dbBasin.readSiteTS(siteNo, varLst=codeLst, freq='D') nc = len(codeLst) fig, axes = plt.subplots(nc, 1, figsize=(12, 3)) for k, code in enumerate(codeLst): axplot.plotTS(axes[k], df.index, df[code].values, cLst='k') fig.show() # load data sd = '1982-01-01' ed = '2018-12-31' siteNoLst = [siteNo] dm = dbBasin.DataModelFull.new('test', siteNoLst, sdStr=sd, edStr=ed) # load data varF = gridMET.varLst + ntn.varLst varC = usgs.varC
wT = s1 / s2 wS = s2 / np.sum(s2) iS = np.random.choice(ns, nbatch, p=wS) iT = np.zeros(nbatch) for k in range(nbatch): iT[k] = np.random.choice(nt - rho, p=wT[:, iS[k]]) + rho # plot weights fig, axes = plt.subplots(ns, 1, figsize=(12, 3)) for k in range(ns): axes[k].plot(np.arange(nt), wT[:, k]) ax2 = axes[k].twinx() ax2.plot(np.arange(nt), y[:, k, 1], 'r*') fig.show() df = dbBasin.readSiteTS(siteNoLst[0], varLst=varY + varX, freq='D', sd=np.datetime64(sd), ed=np.datetime64(ed)) fig, ax = plt.subplots(1, 1, figsize=(12, 3)) k = 1 j = 15 yTemp = yTensor.detach().cpu().numpy() xTemp = xTensor.detach().cpu().numpy() datenum = transform.transOutAll(xTemp[:, :, -3:-2], ['norm'], [statTup[0][-3]]) ax.plot(dataTupRaw[0][:, 0, -3], dataTup[2][:, 0, k], 'k*') # ax.plot(df['datenum'], np.log(df[varTup[2][k]]+1), 'k*') ax.plot(datenum[:, j, 0], yTemp[:, j, k], 'r.') fig.show()
from hydroDL.data import ntn, dbBasin, usgs from hydroDL.post import axplot, figplot import matplotlib.pyplot as plt import numpy as np import pandas as pd # read time series siteNo = '01184000' df = dbBasin.readSiteTS(siteNo, dbBasin.io.varTLst, ed=np.datetime64('2010-01-01')) fig, axes = plt.subplots(3, 1, figsize=(12, 6)) axplot.plotTS(axes[0], df.index, df['datenum'], styLst='-', cLst='k') axplot.plotTS(axes[1], df.index, df['sinT'], styLst='-', cLst='k') axplot.plotTS(axes[2], df.index, df['cosT'], styLst='-', cLst='k') fig.show() df = dbBasin.readSiteTS(siteNo, usgs.varC + usgs.varQ) fig, axes = plt.subplots(3, 1, figsize=(12, 6)) axplot.plotTS(axes[0], df.index, df['runoff'], styLst='-', cLst='k') axplot.plotTS(axes[1], df.index, df['00915'], styLst='*', cLst='k') axplot.plotTS(axes[2], df.index, df['00945'], styLst='*', cLst='k') fig.show()
from hydroDL.data import ntn, dbBasin from hydroDL.post import axplot, figplot import matplotlib.pyplot as plt import numpy as np import pandas as pd dfSite = ntn.loadSite() lat = dfSite['latitude'] lon = dfSite['longitude'] yr = pd.to_datetime( dfSite['startdate']).values.astype('M8[Y]').astype(str).astype(int) fig, ax = plt.subplots(1, 1, figsize=(6, 8)) axplot.mapPoint(ax, lat, lon, yr, s=16) fig.show() # read time series siteNo = '01184000' df = dbBasin.readSiteTS(siteNo, ntn.varLst) fig, axes = plt.subplots(2, 1, figsize=(6, 8)) axplot.plotTS(axes[0], df.index, df['Ca'], styLst='-', cLst='k') axplot.plotTS(axes[1], df.index, df['distNTN'], styLst='-', cLst='k') fig.show()
def calWRTDS(siteNo, freq='D', trainSet='B10', the=[150, 50], fitAll=True, codeLst=usgs.varC, reCal=False): dirRoot = os.path.join(kPath.dirWQ, 'modelStat', 'WRTDS-{}'.format(freq)) dirOut = os.path.join(dirRoot, trainSet) saveName = os.path.join(dirOut, siteNo) if os.path.exists(saveName): print('calculated {}'.format(siteNo)) if reCal is False: return t0 = time.time() varQ = '00060' varLst = codeLst + [varQ] df = dbBasin.readSiteTS(siteNo, varLst=varLst, freq=freq) dfYP = pd.DataFrame(index=df.index, columns=codeLst) dfX = pd.DataFrame({'date': df.index}).set_index('date') dfX = dfX.join(np.log(df[varQ] + sn)).rename(columns={varQ: 'logQ'}) yr = dfX.index.year.values t = yr + dfX.index.dayofyear.values / 365 dfX['sinT'] = np.sin(2 * np.pi * t) dfX['cosT'] = np.cos(2 * np.pi * t) dfX['yr'] = yr dfX['t'] = t xVarLst = ['yr', 'logQ', 'sinT', 'cosT'] ind1, ind2 = defineTrainSet(df.index, trainSet) # train / test fitCodeLst = list() for code in codeLst: b1 = df.iloc[ind1][code].dropna().shape[0] > the[0] b2 = df.iloc[ind2][code].dropna().shape[0] > the[1] if b1 and b2: fitCodeLst.append(code) for code in fitCodeLst: dfXY = dfX.join(np.log(df[code] + sn)) df1 = dfXY.iloc[ind1].dropna() if fitAll: df2 = dfXY[xVarLst + ['t']].dropna() else: df2 = dfXY.iloc[ind2].dropna() # only fit for observations now n = len(df1) if n == 0: break # calculate weight h = np.array([7, 2, 0.5]) # window [Y Q S] from EGRET tLst = df2.index.tolist() for t in tLst: dY = np.abs((df2.loc[t]['t'] - df1['t']).values) dQ = np.abs((df2.loc[t]['logQ'] - df1['logQ']).values) dS = np.min(np.stack( [abs(np.ceil(dY) - dY), abs(dY - np.floor(dY))]), axis=0) d = np.stack([dY, dQ, dS]) ww, ind = calWeight(d) # fit WLS Y = df1.iloc[ind][code].values X = df1.iloc[ind][xVarLst].values model = sm.WLS(Y, X, weights=ww).fit() xp = df2.loc[t][xVarLst].values yp = model.predict(xp)[0] dfYP.loc[t][code] = np.exp(yp) - sn t1 = time.time() print(siteNo, code, t1 - t0) saveName = os.path.join(dirOut, siteNo) dfYP.to_csv(saveName) return dfYP