def runDeterministicVIC(dbname, options): """Driver function for performing a deterministic VIC nowcast simulation.""" res = config.getResolution(options['nowcast']) vicexe = "{0}/vicNl".format(rpath.bins) basin = config.getBasinFile(options['nowcast']) saveto, savevars = config.getVICvariables(options) startyear, startmonth, startday = map( int, options['nowcast']['startdate'].split('-')) endyear, endmonth, endday = map(int, options['nowcast']['enddate'].split('-')) name = options['nowcast']['name'].lower() path = tempfile.mkdtemp(dir=".") model = vic.VIC(path, dbname, res, startyear, startmonth, startday, endyear, endmonth, endday, name) savestate, dbsavestate = _saveState(options['vic']) init, statefile = _initialize(options['vic']) model.writeParamFile(save_state=savestate, init_state=init, save_state_to_db=dbsavestate, state_file=statefile) model.writeSoilFile(basin) prec, tmax, tmin, wind = model.getForcings(options['vic']) model.writeForcings(prec, tmax, tmin, wind) model.run(vicexe) model.save(saveto, savevars) shutil.rmtree(path)
def __init__(self, nens, dbname, resolution, startyear, startmonth, startday, endyear, endmonth, endday, name=""): """Create an ensemble of models with size *nens*.""" self.nens = nens self.models = [] self.name = name self.statefiles = [] self.res = resolution self.startyear, self.startmonth, self.startday = startyear, startmonth, startday self.endyear, self.endmonth, self.endday = endyear, endmonth, endday self.dbname = dbname for e in range(nens): modelpath = tempfile.mkdtemp(dir=".") model = vic.VIC(modelpath, dbname, resolution, startyear, startmonth, startday, endyear, endmonth, endday, name=name) self.models.append(model)
def _initializePerturb(self, basin, forcings, vicexe, initdays=90, saveindb=False, saveto="db", saveargs=[], overwrite=True, skipsave=0): """Initialize ensemble of VIC models by perturbing the meterological forcings and running them *initmonths* prior to simulation start date.""" statefiles = [] t = date(self.startyear, self.startmonth, self.startday) - \ relativedelta(days=initdays) modelpath = tempfile.mkdtemp() model = vic.VIC(modelpath, self.dbname, self.res, t.year, t.month, t.day, self.startyear, self.startmonth, self.startday, self.name) prec, tmax, tmin, wind = model.getForcings(forcings) eprec, etmax, etmin, ewind = self.perturb(prec, tmax, tmin, wind) pmodels = [] for e in range(self.nens): modelpath = tempfile.mkdtemp() model = vic.VIC(modelpath, self.dbname, self.res, t.year, t.month, t.day, self.startyear, self.startmonth, self.startday, self.name) model.writeParamFile(save_state=modelpath, init_state=False) model.writeSoilFile(basin) model.writeForcings(eprec[e], etmax[e], etmin[e], ewind[e]) pmodels.append(model) procs = [Process(target=pmodels[e].run, args=(vicexe,)) for e in range(self.nens)] for p in procs: p.start() for p in procs: p.join() if saveindb: if skipsave < 0: skipdays = (date(self.startyear, self.startmonth, self.startday) - t).days + 1 + skipsave else: skipdays = skipsave init = overwrite for e in range(len(pmodels)): model = pmodels[e] if getattr(model.writeToDB, 'func_name') is not 'write_wrapper': model.writeToDB = self._ensembleTable( model.writeToDB, e + 1) if e > 0: init = False model.save(saveto, saveargs, init, skipsave=skipdays) for e in range(self.nens): statefile = pmodels[e].model_path + "/vic.state_{0:04d}{1:02d}{2:02d}".format( self.startyear, self.startmonth, self.startday) statefiles.append(statefile) return statefiles
def _initializeDeterm(self, basin, forcings, vicexe): """Initialize ensemble of VIC models deterministically.""" db = dbio.connect(self.dbname) cur = db.cursor() dt = "{0}-{1}-{2}".format(self.startyear, self.startmonth, self.startday) cur.execute( "select * from information_schema.tables where table_schema='{0}' and table_name='state'" .format(self.name)) if bool(cur.rowcount): sql = "select filename, fdate from {0}.state order by abs(date '{1}' - fdate)".format( self.name, dt) cur.execute(sql) if bool(cur.rowcount): statefile, t = cur.fetchone() else: statefile = "" else: statefile = "" if statefile == "": t = date(self.startyear - 1, self.startmonth, self.startday) # checks if statefile corresponds to requested forecast start date if (t - date(self.startyear, self.startmonth, self.startday)).days < 0: if (t - date(self.startyear - 1, self.startmonth, self.startday)).days < 0: # if statefile is older than a year, start the model # uninitialized for 1 year t = date(self.startyear - 1, self.startmonth, self.startday) modelpath = tempfile.mkdtemp(dir=".") model = vic.VIC(modelpath, self.dbname, self.res, t.year, t.month, t.day, self.startyear, self.startmonth, self.startday, self.name) model.writeParamFile(save_state=modelpath, init_state=bool(statefile)) model.writeSoilFile(basin) prec, tmax, tmin, wind = model.getForcings(forcings) model.writeForcings(prec, tmax, tmin, wind) model.run(vicexe) statefile = model.model_path + \ "/vic.state_{0:04d}{1:02d}{2:02d}".format( self.startyear, self.startmonth, self.startday) for emodel in self.models: shutil.copy(statefile, emodel.model_path) shutil.rmtree(model.model_path) statefiles = [statefile] * self.nens self.setStateFiles(statefiles) cur.close() db.close() return statefiles
def _initializeRandom(self, basin, forcings, vicexe, initdays=90, saveindb=False, saveto="db", saveargs=[], overwrite=True, skipsave=0): """Initialize ensemble of VIC models by sampling the meterological forcings and running them *initmonths* prior to simulation start date.""" db = dbio.connect(self.dbname) cur = db.cursor() sql = "select distinct (date_part('year', fdate)) as year from precip.{0}".format( forcings['precip']) cur.execute(sql) years = map(lambda y: int(y[0]), cur.fetchall()) years.remove(max(years)) cur.close() db.close() statefiles = [] t = date(self.startyear, self.startmonth, self.startday) - \ relativedelta(days=initdays) ndays = (date(self.startyear, self.startmonth, self.startday) - t).days modelpath = tempfile.mkdtemp() # (dir=".") model = vic.VIC(modelpath, self.dbname, self.res, t.year, t.month, t.day, self.startyear, self.startmonth, self.startday, self.name) years = np.random.choice(years, self.nens) pmodels = [] for e in range(self.nens): modelpath = tempfile.mkdtemp() # (dir=".") model = vic.VIC(modelpath, self.dbname, self.res, t.year, t.month, t.day, self.startyear, self.startmonth, self.startday, self.name) model.writeParamFile(save_state=modelpath, init_state=False) model.writeSoilFile(basin) model.startyear = years[e] model.endyear = years[e] + (model.endyear - t.year) ddays = (date(model.endyear, model.endmonth, model.endday) - date(model.startyear, model.startmonth, model.startday)).days - ndays t1 = date(model.endyear, model.endmonth, model.endday) - \ relativedelta(days=ddays) model.endyear, model.endmonth, model.endday = t1.year, t1.month, t1.day prec, tmax, tmin, wind = model.getForcings(forcings) model.writeForcings(prec, tmax, tmin, wind) model.startyear, model.startmonth, model.startday = t.year, t.month, t.day model.endyear, model.endmonth, model.endday = self.startyear, self.startmonth, self.startday pmodels.append(model) procs = [Process(target=pmodels[e].run, args=(vicexe,)) for e in range(self.nens)] for p in procs: p.start() for p in procs: p.join() if saveindb: if skipsave < 0: skipdays = (date(self.startyear, self.startmonth, self.startday) - t).days + 1 + skipsave else: skipdays = skipsave init = overwrite for e in range(len(pmodels)): model = pmodels[e] if getattr(model.writeToDB, 'func_name') is not 'write_wrapper': model.writeToDB = self._ensembleTable( model.writeToDB, e + 1) if e > 0: init = False model.save(saveto, saveargs, init, skipsave=skipdays) for e in range(self.nens): statefile = pmodels[e].model_path + "/vic.state_{0:04d}{1:02d}{2:02d}".format( self.startyear, self.startmonth, self.startday) statefiles.append(statefile) return statefiles