def createRunConfiguration(self, regionName, modelName, runType, Tstart, deltaT, nSteps, dataOffset): r=enki.RunConfiguration() r.regionName=regionName with open('../../../Data/SentralReg/'+regionName+'.rgx', 'rb') as regionFile: r.regionXml= regionFile.read() r.modelName=modelName with open('../../../Data/SentralReg/'+modelName+'.mdx', 'rb') as modelFile: r.modelXml= modelFile.read() dt= self.cal.calendarUnits(Tstart); stateFileName= "%s.state.%04d-%02d-%02d_%02d.stx"%(modelName, dt.year, dt.month, dt.day, dt.hour) with open('../../../Data/SentralReg/'+stateFileName) as stateFile: r.stateXml= stateFile.read() r.Tstart=Tstart r.deltaT=deltaT r.nSteps=nSteps r.runType=runType Tw = self.cal.time(enki.YMDhms(2012, 3, 1, 0, 0, 0)) Td= r.Tstart + dataOffset inputTsType= enki.InputTs timeAxis=enki.FixedIntervalTimeAxis(Td, r.deltaT, r.nSteps) # now we have to put at least one timeseries in there..: # temperature: dayly sinus curve with average +3.0, +-8 deg C, phase +9 hours, (mid day at 1500) # the node average= 3.0 + i/numberofNodes. (4.0 at highest node) nTStatsNodes=14 TnodeId= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 18] #tsc= enki.TsContainer() for i in range(nTStatsNodes): fx = enki.FxSinus(3.0 + i/(1.0*nTStatsNodes), -10.0, +20.0, 8.0, Tw+enki.deltahours(9), enki.deltahours(24)) tstats= enki.FxSinusTs(fx, "TStats", TnodeId[i], inputTsType, timeAxis) r.ts.push_back(tstats); # precipitation: # sine curve , with period equal to forecast length, clipped to zero nPstatsNodes=10 forecastLength=r.deltaT*r.nSteps; for i in range(nPstatsNodes): fx= enki.FxSinus(10.0 + i/(1.0*nPstatsNodes), 0.0, +20.0, 8.0, Tw, forecastLength) pstats= enki.FxSinusTs(fx, "PStats", 1 + i, inputTsType, timeAxis) r.ts.push_back(pstats) # radiation # max at high day, 1200, aprox 400, zero during dark. # uses a sin wave amplitude 700,clipped at 400 and 0. starts at 0700 nRstatNodes = 1 #// 1,2,4,5 RnodeId=[1, 2, 4, 5] for i in range(nRstatNodes): fx= enki.FxSinus(0.0, 0.0, +400.0, 500.0, Tw+ enki.deltahours(7), enki.deltahours(24)) rstats= enki.FxSinusTs(fx, "RStats", RnodeId[i], inputTsType, timeAxis) r.ts.push_back(rstats) simplSimDischarge= enki.MetaInfo(0, "SimplSimDischarge", "SimplSimDischarge", 0, enki.OutputTs) instantRunoff= enki.MetaInfo(0, "InstantRunoff", "InstantRunoff", 0, enki.OutputTs) r.resultVariables.push_back(simplSimDischarge) r.resultVariables.push_back(instantRunoff) return r
def __init__(s, f_0=0.0, f_min=0.0, f_max=1.0, amp=1.0, delay=enki.deltahours(9), period_length=enki.deltahours(24)): s.f_0 = f_0 s.f_min = f_min s.f_max = f_max s.amp = amp s.delay = delay s.period_length = period_length
def extract_point_source_variables_from_region(self, region, t0=0): region_var = region.GetVariables() delta_t = enki.deltahours(1) time_axis = enki.FixedIntervalTimeAxis(t0, delta_t, 10*24 - 1) # hourly data for 100 days class MockSourceMap(dict): def __init__(s, f_0=0.0, f_min=0.0, f_max=1.0, amp=1.0, delay=enki.deltahours(9), period_length=enki.deltahours(24)): s.f_0 = f_0 s.f_min = f_min s.f_max = f_max s.amp = amp s.delay = delay s.period_length = period_length def __getitem__(s, idx): t_w = t0 ts_name = "".join([random.choice("abcdefghijklmnop") for _ in xrange(10)]) f = enki.FxSinus(s.f_0, s.f_min, s.f_max, s.amp, t_w + s.delay, s.period_length) ts = enki.FxSinusTs(f, ts_name, idx, enki.InputTs, time_axis) return ts temperature_sources = self.extract_point_source(region_var, source_info('Tstats_elev', enki.TemperatureSource, enki.TemperatureSourceVector, MockSourceMap(4.0, -2.0, 10.0, 6.0, enki.deltahours(9), enki.deltahours(24)))) precipitation_sources = self.extract_point_source(region_var, source_info('Pstats_elev', enki.PrecipitationSource, enki.PrecipitationSourceVector, MockSourceMap(-10.0, 0.0, 4.00, 100.0, 0, enki.deltahours(30)))) radiation_sources = self.extract_point_source(region_var, source_info('Rstats', enki.RadiationSource, enki.RadiationSourceVector, MockSourceMap(0.0, 0.0, 400.0, 700.0, enki.deltahours(7), enki.deltahours(24)))) return {'temperature': temperature_sources, 'precipitation': precipitation_sources, 'radiation': radiation_sources}, time_axis
def run(self, runType, Tstart, nSteps, dataOffset): runCfg = self.createRunConfiguration('SentralReg', 'enki', runType, Tstart, enki.deltahours(1), nSteps, dataOffset) runCfg.modelName = 'enki' enkiService = enki.EnkiApiService(False) enkiService.setRootPath(enkiModelPath) print "Running enki .." result=enkiService.run(runCfg) print "Done,", result.success return result
def TestUtcTimeVector(): """ Just to verify that 64bits int array are convertible to utctime in enki. """ llv=np.ones(10, np.int64) v=Enp.UtcTimeVector.FromNdArray(llv) print "Size of long long alias UtcTime vector is ", v.size() utc = enki.Calendar() Tnow= utc.trim(enki.utctimeNow(), enki.deltahours(1)); v.push_back(Tnow) print "Size after adding Tnow to the list is ", v.size() Xnow=v[10] print "Original timestamp is ", utc.toString(Tnow), " and roundtrip tstamp is ", utc.toString(Xnow) return
def demo(self): Tstart= self.cal.time(enki.YMDhms(2012, 3, 8, 0, 0, 0)) nSteps=12 runCfg=self.createRunConfiguration('SentralReg', 'Enki', enki.UpdateRun, Tstart, enki.deltahours(1), nSteps, enki.deltahours(0)) runCfg.calculationModelName='Enki' print runCfg.regionName print runCfg.calculationModelName print self.cal.toString(runCfg.Tstart), runCfg.deltaT, runCfg.nSteps result = self.run(enki.UpdateRun, Tstart, nSteps, enki.deltahours(0)) print result.messageCount(), result.tsCount() print 'The stateXml size=', len(result.stateXml) for i in range(result.messageCount()): try: if(len(result.message(i))) : print result.message(i) except: pass for i in range(result.tsCount()): ts_i= result.ts(i) try: print ts_i.getName(), ts_i.getEnkiName(), ts_i.id() except: pass for j in range (ts_i.nPoints()): try: print i, self.cal.toString(ts_i.getTime(j)), ' ', ts_i.getValue(j) except: pass for i in range(result.rtsCount()): rts_i = result.rts(i) for j in range (rts_i.nPoints()): rts=rts_i.getValue(j) asVector=rts.asRowMajorVector() try: print rts_i.getName(), '/', rts_i.getEnkiName(), rts_i.id(), i, self.cal.toString(rts_i.getTime(j)), 'rxc=', rts.nRows(), ' x ', rts.nCols() except: pass print 'Done demo run'
def IncrementalTesting(): utc = enki.Calendar() Tnow = utc.trim(enki.utctimeNow(), enki.deltahours(1)); timeAxis = enki.FixedIntervalTimeAxis(Tnow, enki.deltahours(1), 24) FxSinus = enki.FxSinus(10.0, -5.0, 15.0, 10.0, Tnow, enki.deltahours(24)) ts1 = enki.FxSinusTs(FxSinus, 'ts1', 1, enki.InputTs, timeAxis); print ts1.getValue(timeAxis.getTime(2)) #print("Size of container is %s"%tscontainer.size()) enkiService=enki.EnkiApiService() enkiService.setRootPath(enkiModelPath) runCfg=enki.RunConfiguration() runCfg.calculationModelName='enki' # Testing vector tv= enki.UtcTimeVector() tv.push_back(Tnow) qv= enki.IntVector() qv.push_back(0);qv.push_back(0);qv.push_back(0); pvv= np.array([1.0, 2.0, 3.0, 4.0], np.float32) #print "Now it works..:", Ex.rms(pvv) #xvv=enki.MakeFloatVectorFromNp(pvv) #print xvv.size(), " xvv " , xvv[0] vv= enki.FloatVector() vv.push_back(1.0) vv.push_back(2.0) vv.push_back(3.0) vv.push_back(4.0) npvv=np.fromiter(vv, np.float32) print "Ok, got np array:", npvv #xvv=Ex.MakeFloatVector(npvv) xxv=Enp.FloatVector.FromNdArray(pvv) print "Ok, now a float vector,", xxv.size() #-- now check float64 FloatVector dv= np.ones(3, dtype=np.float64); fdv= Enp.FloatVector.FromNdArrayDouble(dv); print "Ok, now a float vector from np.float64", fdv.size() for i in range(fdv.size()): print i, "=", fdv[i] #-- xx rst1=enki.RasterFloat(2, 2, xxv); print "Raster from flat float array ", rst1.nRows(), " x ", rst1.nCols(), "=", rst1.nCells() #xnpvv=enki.MakeFloatVectorFromNp(ct.c_void_p(npvv.ctypes.data),ct.c_int(3)) #print "Round trip ",xnpvv.size() qv= enki.IntVector() qv.push_back(0);qv.push_back(0);qv.push_back(0); factory= enki.TsFactory(); metaInfo=enki.MetaInfo(1, "InstantRunoff", "InstantRunoff", 0, enki.InputTs) pts= factory.CreatePointTs(metaInfo, 3, Tnow, enki.deltahours(1), vv, qv); print "tv size=", tv.size(), " vv.size", vv.size(), " pts.Count()=", pts.nPoints() for i in range(pts.nPoints()): ti=pts.getTime(i) print i, utc.toString(ti), pts.getValue(i) runCfg.regionName='SentralReg' runCfg.Tstart = utc.time(enki.YMDhms(2012, 01, 01, 00, 00, 00)) runCfg.deltaT=enki.deltahours(1) runCfg.nSteps=24 runCfg.runType=enki.UpdateRun