def run(self,pars): """ Run the model for the number of timesteps. """ # Run the initial part of the model (reads parameters and sets initial values) self.dynModelFw._runInitial() # Runs initial part #self.dynModelFw.wf_multParameterValues('M',pars[0]) self.multVarWithPar(pars) self.dynModelFw._runResume() # gets the state variables for ts in range(self.startTime,self.stopTime): self.dynModelFw._runDynamic(ts,ts) # runs for all timesteps # save output state self.dynModelFw._runSuspend() self.dynModelFw._wf_shutdown() tssfile = os.path.join(self.caseName,self.runId,"run.tss") results, head = pcrut.readtss(tssfile) return results[self.WarmUpSteps:,self.ColSim].astype(np.float64)
def run(self,pars): """ Run the model for the number of timesteps. """ # Run the initial part of the model (reads parameters and sets initial values) self.dynModelFw._runInitial() # Runs initial part #self.dynModelFw.wf_multParameterValues('M',pars[0]) self.multVarWithPar(pars) self.dynModelFw._runResume() # gets the state variables for ts in range(self.startTime,self.stopTime): self.dynModelFw._runDynamic(ts,ts) # runs for all timesteps # save output state self.dynModelFw._runSuspend() self.dynModelFw._wf_shutdown() tssfile = os.path.join(self.caseName,self.runId,self.qmodname) results, head = pcrut.readtss(tssfile) return results[self.WarmUpSteps:,self.ColSim].astype(np.float64)
def tss_topixml(tssfile,xmlfile,locationname,parametername,Sdate,timestep): """ Converts a .tss file to a PI-xml file """ try: tss,header = pcrut.readtss(tssfile) except: logger.error("Tss file not found: ", tssfile) return trange = timedelta(seconds=timestep * (tss.shape[0])) # The tss have one timeste less as the mapstacks extraday = timedelta(seconds=timestep) #extraday = timedelta(seconds=0) Sdate = Sdate + extraday Edate = Sdate + trange - extraday Sdatestr = Sdate.strftime('%Y-%m-%d') Stimestr = Sdate.strftime('%H:%M:%S') Edatestr = Edate.strftime('%Y-%m-%d') Etimestr = Edate.strftime('%H:%M:%S') ofile = open(xmlfile,'w') ofile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") ofile.write("<TimeSeries xmlns=\"http://www.wldelft.nl/fews/PI\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.wldelft.nl/fews/PI http://fews.wldelft.nl/schemas/version1.0/pi-schemas/pi_timeseries.xsd\" version=\"1.2\">\n") ofile.write("<timeZone>0.0</timeZone>\n") count = 0 for col in tss.transpose(): count = count + 1 ofile.write("<series>\n") ofile.write("<header>\n") ofile.write("<type>instantaneous</type>\n") ofile.write("<locationId>" + header[count-1] + "</locationId>\n") ofile.write("<parameterId>" + parametername + "</parameterId>\n") ofile.write("<timeStep unit=\"second\" multiplier=\"" + str(timestep) + "\"/>\n") ofile.write("<startDate date=\"" + Sdatestr +"\" time=\""+ Stimestr + "\"/>\n") ofile.write("<endDate date=\"" + Edatestr + "\" time=\"" + Etimestr + "\"/>\n") ofile.write("<missVal>-999.0</missVal>\n") ofile.write("<stationName>" + header[count-1] + "</stationName>\n") ofile.write("</header>\n") # add data here xdate = Sdate for pt in col: Sdatestr = xdate.strftime('%Y-%m-%d') Stimestr = xdate.strftime('%H:%M:%S') ofile.write("<event date=\"" + Sdatestr + "\" time=\"" + Stimestr + "\" value=\"" + str(pt) + "\" />\n") xdate = xdate + timedelta(seconds=timestep) ofile.write("</series>\n") ofile.write("</TimeSeries>\n") ofile.close() return tss
def tss_topixml(tssfile, xmlfile, locationname, parametername, Sdate, timestep): """ Converts a .tss file to a PI-xml file """ missval = "-999.0" #try: tss, header = pcrut.readtss(tssfile) #except: # logger.error("Tss file not found or corrupt: ", tssfile) # return # Add dummpy first timesteps if len(tss.shape) > 1: dumm = tss[0, :].copy() dumm[:] = -999.0 tss = pcrut.numpy.vstack((dumm, tss)) else: dumm = tss.copy() dumm[:] = -999.0 tss = pcrut.numpy.vstack((dumm, tss)) # replace NaN with missing values tss[pcrut.numpy.isnan(tss)] = missval trange = timedelta(seconds=timestep * (tss.shape[0])) extraday = timedelta(seconds=timestep) #extraday = timedelta(seconds=0) #Sdate = Sdate + extraday #Edate = Sdate + trange - extraday Sdate = Sdate + extraday Edate = Sdate + trange - extraday - extraday Sdatestr = Sdate.strftime('%Y-%m-%d') Stimestr = Sdate.strftime('%H:%M:%S') Edatestr = Edate.strftime('%Y-%m-%d') Etimestr = Edate.strftime('%H:%M:%S') ofile = open(xmlfile, 'w') ofile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") ofile.write( "<TimeSeries xmlns=\"http://www.wldelft.nl/fews/PI\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.wldelft.nl/fews/PI http://fews.wldelft.nl/schemas/version1.0/pi-schemas/pi_timeseries.xsd\" version=\"1.2\">\n" ) ofile.write("<timeZone>0.0</timeZone>\n") count = 0 for col in tss.transpose(): count = count + 1 ofile.write("<series>\n") ofile.write("<header>\n") ofile.write("<type>instantaneous</type>\n") ofile.write("<locationId>" + header[count - 1] + "</locationId>\n") ofile.write("<parameterId>" + parametername + "</parameterId>\n") ofile.write("<timeStep unit=\"second\" multiplier=\"" + str(timestep) + "\"/>\n") ofile.write("<startDate date=\"" + Sdatestr + "\" time=\"" + Stimestr + "\"/>\n") ofile.write("<endDate date=\"" + Edatestr + "\" time=\"" + Etimestr + "\"/>\n") ofile.write("<missVal>" + str(missval) + "</missVal>\n") ofile.write("<stationName>" + header[count - 1] + "</stationName>\n") ofile.write("</header>\n") # add data here xdate = Sdate xcount = 1 for pt in col: if xcount > 1: Ndatestr = xdate.strftime('%Y-%m-%d') Ntimestr = xdate.strftime('%H:%M:%S') ofile.write("<event date=\"" + Ndatestr + "\" time=\"" + Ntimestr + "\" value=\"" + str(pt) + "\" />\n") xdate = xdate + timedelta(seconds=timestep) xcount = xcount + 1 ofile.write("</series>\n") ofile.write("</TimeSeries>\n") ofile.close() return tss
def main(argv=None): caseName = "not_set" _lastTimeStep = 10 _firstTimeStep = 1 fitname = "wflow_fit.res" runId = "_fitrun" # theModel = 'wflow_cqf' theModel = "wflow_sbm" configfile = None saveResults = False fitmethod = "fmin" if argv is None: argv = sys.argv[1:] if len(argv) == 0: usage() return opts, args = getopt.getopt(argv, "C:S:T:c:s:R:hM:U") for o, a in opts: if o == "-C": caseName = a if o == "-c": configfile = a if o == "-s": timestepsecs = int(a) if o == "-T": _lastTimeStep = int(a) if o == "-S": _firstTimeStep = int(a) if o == "-R": runId = a if o == "-M": theModel = a if o == "-U": saveResults = True if o == "-h": usage() if configfile == None: configfile = theModel + ".ini" mimo = wfmodel_fit_API( _firstTimeStep, _lastTimeStep, caseName, runId, modeltofit=theModel, config=configfile, ) pars = mimo.pars diag = mimo.pars catchment = 0 mimo.saveinitpars() for catch in mimo.ColSimS: fitname = str(catch) + "_wflow_fit.res" mimo.NS = [] mimo.BIAS = [] mimo.CORR = [] mimo.MABSE = [] # set the catchment # print "........> Catchment: " _ str(catchment) mimo.ColSim = mimo.ColSimS[catchment] mimo.ColMeas = mimo.ColMeasS[catchment] mimo.AreaCode = mimo.AreaCodeS[catchment] # print mimo.AreaCode # print mimo.ColMeas # print mimo.ColSim qmeas, header = pcrut.readtss(os.path.join(caseName, mimo.qmeasname)) qmeas = qmeas.astype(np.float64) qmeas = qmeas[ _firstTimeStep - 1 + mimo.WarmUpSteps : _lastTimeStep - 1, mimo.ColMeas ] lstr = ( "Currently fitting... Sim: " + str(mimo.ColSim) + " Meas: " + str(mimo.ColMeas) + " Area: " + str(mimo.AreaCode) ) mimo.log.log(45, lstr) pylab.plot(qmeas, "+") pylab.title( "Sim: " + str(mimo.ColSim) + " Meas: " + str(mimo.ColMeas) + " Area: " + str(mimo.AreaCode) ) # bruteforce(mimo) res = bruteforce(mimo, qmeas, caseName, runId) # print pp # pylab.plot(mimo.run(pp),"r",linewidth=2.0) # printresults(pp,a,b,c,d,mimo.calibpars,os.path.join(caseName,runId,fitname),mimo) catchment = catchment + 1 pylab.clf() # mimo.results.append([catchment,pp,a,b,c,d,mimo.NS,mimo.BIAS,mimo.CORR,mimo.MABSE]) # mimo.savemaps(pp,saveResults) mimo.shutdown() f = open(os.path.join(caseName, runId, "wflow_fit.csv"), "wb") writer = csv.writer(f) writer.writerows(res) f.close()
def tss_topixml(tssfile, xmlfile, locationname, parametername, Sdate, timestep): """ Converts a .tss file to a PI-xml file """ missval = "-999.0" # try: tss, header = pcrut.readtss(tssfile) # except: # logger.error("Tss file not found or corrupt: ", tssfile) # return # Add dummpy first timesteps if len(tss.shape) > 1: dumm = tss[0, :].copy() dumm[:] = -999.0 tss = pcrut.numpy.vstack((dumm, tss)) else: dumm = tss.copy() dumm[:] = -999.0 tss = pcrut.numpy.vstack((dumm, tss)) # replace NaN with missing values tss[pcrut.numpy.isnan(tss)] = missval trange = timedelta(seconds=timestep * (tss.shape[0])) extraday = timedelta(seconds=timestep) # extraday = timedelta(seconds=0) # Sdate = Sdate + extraday # Edate = Sdate + trange - extraday Sdate = Sdate + extraday Edate = Sdate + trange - extraday - extraday Sdatestr = Sdate.strftime("%Y-%m-%d") Stimestr = Sdate.strftime("%H:%M:%S") Edatestr = Edate.strftime("%Y-%m-%d") Etimestr = Edate.strftime("%H:%M:%S") ofile = open(xmlfile, "w") ofile.write('<?xml version="1.0" encoding="UTF-8"?>\n') ofile.write( '<TimeSeries xmlns="http://www.wldelft.nl/fews/PI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.wldelft.nl/fews/PI http://fews.wldelft.nl/schemas/version1.0/pi-schemas/pi_timeseries.xsd" version="1.2">\n' ) ofile.write("<timeZone>0.0</timeZone>\n") count = 0 for col in tss.transpose(): count = count + 1 ofile.write("<series>\n") ofile.write("<header>\n") ofile.write("<type>instantaneous</type>\n") ofile.write("<locationId>" + header[count - 1] + "</locationId>\n") ofile.write("<parameterId>" + parametername + "</parameterId>\n") ofile.write('<timeStep unit="second" multiplier="' + str(timestep) + '"/>\n') ofile.write('<startDate date="' + Sdatestr + '" time="' + Stimestr + '"/>\n') ofile.write('<endDate date="' + Edatestr + '" time="' + Etimestr + '"/>\n') ofile.write("<missVal>" + str(missval) + "</missVal>\n") ofile.write("<stationName>" + header[count - 1] + "</stationName>\n") ofile.write("</header>\n") # add data here xdate = Sdate xcount = 1 for pt in col: if xcount > 1: Ndatestr = xdate.strftime("%Y-%m-%d") Ntimestr = xdate.strftime("%H:%M:%S") ofile.write('<event date="' + Ndatestr + '" time="' + Ntimestr + '" value="' + str(pt) + '" />\n') xdate = xdate + timedelta(seconds=timestep) xcount = xcount + 1 ofile.write("</series>\n") ofile.write("</TimeSeries>\n") ofile.close() return tss
def main(argv=None): caseName = "not_set" _lastTimeStep = 10 _firstTimeStep = 1 fitname = "wflow_fit.res" runId = "_fitrun" # theModel = 'wflow_cqf' theModel = "wflow_sbm" configfile = None saveResults = False fitmethod = "fmin" if argv is None: argv = sys.argv[1:] if len(argv) == 0: usage() return opts, args = getopt.getopt(argv, "C:S:T:c:s:R:hM:U") for o, a in opts: if o == "-C": caseName = a if o == "-c": configfile = a if o == "-s": timestepsecs = int(a) if o == "-T": _lastTimeStep = int(a) if o == "-S": _firstTimeStep = int(a) if o == "-R": runId = a if o == "-M": theModel = a if o == "-U": saveResults = True if o == "-h": usage() if configfile == None: configfile = theModel + ".ini" mimo = wfmodel_fit_API( _firstTimeStep, _lastTimeStep, caseName, runId, modeltofit=theModel, config=configfile, ) pars = mimo.pars diag = mimo.pars catchment = 0 mimo.saveinitpars() for catch in mimo.ColSimS: fitname = str(catch) + "_wflow_fit.res" mimo.NS = [] mimo.BIAS = [] mimo.CORR = [] mimo.MABSE = [] # set the catchment # print "........> Catchment: " _ str(catchment) mimo.ColSim = mimo.ColSimS[catchment] mimo.ColMeas = mimo.ColMeasS[catchment] mimo.AreaCode = mimo.AreaCodeS[catchment] # print mimo.AreaCode # print mimo.ColMeas # print mimo.ColSim qmeas, header = pcrut.readtss(os.path.join(caseName, mimo.qmeasname)) qmeas = qmeas.astype(np.float64) qmeas = qmeas[_firstTimeStep - 1 + mimo.WarmUpSteps:_lastTimeStep - 1, mimo.ColMeas] lstr = ("Currently fitting... Sim: " + str(mimo.ColSim) + " Meas: " + str(mimo.ColMeas) + " Area: " + str(mimo.AreaCode)) mimo.log.log(45, lstr) pylab.plot(qmeas, "+") pylab.title("Sim: " + str(mimo.ColSim) + " Meas: " + str(mimo.ColMeas) + " Area: " + str(mimo.AreaCode)) # bruteforce(mimo) res = bruteforce(mimo, qmeas, caseName, runId) # print pp # pylab.plot(mimo.run(pp),"r",linewidth=2.0) # printresults(pp,a,b,c,d,mimo.calibpars,os.path.join(caseName,runId,fitname),mimo) catchment = catchment + 1 pylab.clf() # mimo.results.append([catchment,pp,a,b,c,d,mimo.NS,mimo.BIAS,mimo.CORR,mimo.MABSE]) # mimo.savemaps(pp,saveResults) mimo.shutdown() f = open(os.path.join(caseName, runId, "wflow_fit.csv"), "wb") writer = csv.writer(f) writer.writerows(res) f.close()
def main (argv=None): caseName ="not_set" _lastTimeStep = 10 _firstTimeStep = 1 fitname = "wflow_fit.res" runId = "_fitrun" #theModel = 'wflow_cqf' theModel = 'wflow_sbm' configfile=None saveResults = False fitmethod="fmin" if argv is None: argv = sys.argv[1:] if len(argv) == 0: usage() return opts, args = getopt.getopt(argv, 'C:S:T:c:s:R:hM:U') for o, a in opts: if o == '-C': caseName = a if o == '-c': configfile = a if o == '-s': timestepsecs = int(a) if o == '-T': _lastTimeStep=int(a) if o == '-S': _firstTimeStep=int(a) if o == '-R': runId=a if o == '-M': theModel=a if o == '-U': saveResults=True if o == '-h': usage() if configfile == None: configfile = theModel+".ini" mimo = wfmodel_fit_API(_firstTimeStep,_lastTimeStep,caseName,runId,modeltofit=theModel,config=configfile) pars = mimo.pars diag=mimo.pars catchment = 0 mimo.saveinitpars() for catch in mimo.ColSimS: fitname = str(catch) + "_wflow_fit.res" mimo.NS =[] mimo.BIAS =[] mimo.CORR=[] mimo.MABSE=[] # set the catchment #print "........> Catchment: " _ str(catchment) mimo.ColSim = mimo.ColSimS[catchment] mimo.ColMeas = mimo.ColMeasS[catchment] mimo.AreaCode = mimo.AreaCodeS[catchment] #print mimo.AreaCode #print mimo.ColMeas #print mimo.ColSim qmeas, header = pcrut.readtss(os.path.join(caseName,mimo.qmeasname)) qmeas= qmeas.astype(np.float64) qmeas = qmeas[_firstTimeStep-1 + mimo.WarmUpSteps:_lastTimeStep-1,mimo.ColMeas] lstr = "Currently fitting... Sim: " + str(mimo.ColSim) + " Meas: " + str(mimo.ColMeas) + " Area: " + str(mimo.AreaCode) mimo.log.log(45,lstr) pylab.plot(qmeas,"+") pylab.title("Sim: " + str(mimo.ColSim) + " Meas: " + str(mimo.ColMeas) + " Area: " + str(mimo.AreaCode)) pp,a,b,c,d =scipy.optimize.leastsq(errfuncFIT,mimo.pars,args=(qmeas,mimo,caseName,runId),ftol=mimo.ftol,xtol=mimo.xtol,gtol=mimo.gtol,epsfcn=mimo.epsfcn,full_output=True,maxfev=200,factor=mimo.factor,diag=diag) #print pp #pylab.plot(mimo.run(pp),"r",linewidth=2.0) printresults(pp,a,b,c,d,mimo.calibpars,os.path.join(caseName,runId,fitname),mimo) catchment = catchment + 1 pylab.clf() mimo.results.append([catchment,pp,a,b,c,d,mimo.NS,mimo.BIAS,mimo.CORR,mimo.MABSE]) mimo.savemaps(pp,saveResults) mimo.shutdown(pp) f = open(os.path.join(caseName,runId,'wflow_fit.csv'),'wb') writer=csv.writer(f) writer.writerows(mimo.results) f.close()