Esempio n. 1
0
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)
Esempio n. 2
0
def runVIC(dbname, options):
    """Driver function for performing a VIC forecast simulation"""
    startyear, startmonth, startday = map(
        int, options['forecast']['startdate'].split('-'))
    endyear, endmonth, endday = map(int,
                                    options['forecast']['enddate'].split('-'))
    # if date(endyear, endmonth, endday) > (date(startyear, startmonth, startday) + relativedelta(months=3)):
    #     print("WARNING! Forecast with lead time longer than 3 months requested. Exiting...")
    #     sys.exit()
    res = config.getResolution(options['forecast'])
    vicexe = "{0}/vicNl".format(rpath.bins)
    basin = config.getBasinFile(options['forecast'])
    saveto, savevars = config.getVICvariables(options)
    name = options['forecast']['name'].lower()
    nens = int(options['forecast']['ensemble size'])
    method = options['forecast']['method']
    name = options['forecast']['name'].lower()
    models = ensemble.Ensemble(nens, dbname, res, startyear, startmonth,
                               startday, endyear, endmonth, endday, name)
    if 'initialize' in options['vic'] and options['vic']['initialize'] in [
            'perturb', 'random'
    ]:
        init_method = options['vic']['initialize']
    else:
        init_method = "determ"  # default option to initialize the ensemble from the same state
    # override initializaton method if assimilation was requested
    if 'observations' in options['vic']:
        init_method = "random"
        models.initialize(options,
                          basin,
                          init_method,
                          vicexe,
                          saveindb=True,
                          saveto=saveto,
                          saveargs=savevars,
                          skipsave=-1)
        data, alat, alon, agid = assimilate(
            options, date(models.startyear, models.startmonth,
                          models.startday), models)
        models.updateStateFiles(data, alat, alon, agid)
    else:
        models.initialize(options,
                          basin,
                          init_method,
                          vicexe,
                          saveindb=True,
                          saveto=saveto,
                          saveargs=savevars)
    models.writeParamFiles()
    models.writeForcings(method, options)
    models.run(vicexe)
    models.setDates(startyear, startmonth, startday, endyear, endmonth, endday)
    models.save(saveto, savevars)
    for varname in savevars:
        raster.stddev(models.dbname, "{0}.{1}".format(models.name, varname))
        raster.mean(models.dbname, "{0}.{1}".format(models.name, varname))
    for e in range(nens):
        shutil.rmtree(models[e].model_path)
Esempio n. 3
0
def runVIC(dbname, options):
    """Driver function for performing a VIC forecast simulation"""
    startyear, startmonth, startday = map(
        int, options['forecast']['startdate'].split('-'))
    endyear, endmonth, endday = map(
        int, options['forecast']['enddate'].split('-'))
    # if date(endyear, endmonth, endday) > (date(startyear, startmonth, startday) + relativedelta(months=3)):
    #     print("WARNING! Forecast with lead time longer than 3 months requested. Exiting...")
    #     sys.exit()
    res = config.getResolution(options['forecast'])
    vicexe = "{0}/vicNl".format(rpath.bins)
    basin = config.getBasinFile(options['forecast'])
    saveto, savevars = config.getVICvariables(options)
    name = options['forecast']['name'].lower()
    nens = int(options['forecast']['ensemble size'])
    method = options['forecast']['method']
    name = options['forecast']['name'].lower()
    models = ensemble.Ensemble(nens, dbname, res, startyear,
                               startmonth, startday, endyear, endmonth, endday, name)
    if 'initialize' in options['vic'] and options['vic']['initialize'] in ['perturb', 'random']:
        init_method = options['vic']['initialize']
    else:
        init_method = "determ"  # default option to initialize the ensemble from the same state
    # override initializaton method if assimilation was requested
    if 'observations' in options['vic']:
        init_method = "random"
        models.initialize(options, basin, init_method, vicexe,
                          saveindb=True, saveto=saveto, saveargs=savevars, skipsave=-1)
        data, alat, alon, agid = assimilate(options, date(
            models.startyear, models.startmonth, models.startday), models)
        models.updateStateFiles(data, alat, alon, agid)
    else:
        models.initialize(options, basin, init_method, vicexe,
                          saveindb=True, saveto=saveto, saveargs=savevars)
    models.writeParamFiles()
    models.writeForcings(method, options)
    models.run(vicexe)
    models.setDates(startyear, startmonth, startday, endyear, endmonth, endday)
    models.save(saveto, savevars)
    for varname in savevars:
        raster.stddev(models.dbname, "{0}.{1}".format(
            models.name, varname))
        raster.mean(models.dbname, "{0}.{1}".format(
            models.name, varname))
    for e in range(nens):
        shutil.rmtree(models[e].model_path)
Esempio n. 4
0
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)
Esempio n. 5
0
def runEnsembleVIC(dbname, options):
    """Driver function for performing a VIC nowcast simulation."""
    res = config.getResolution(options['nowcast'])
    name = options['nowcast']['name'].lower()
    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('-'))
    precipdatasets = options['vic']['precip'].split(",")
    savestate, _ = _saveState(options['vic'])
    if 'ensemble size' in options['vic']:
        nens = int(options['vic']['ensemble size'])
    elif 'observations' in options['vic']:
        nens = 20
    else:
        nens = len(precipdatasets)
    models = ensemble.Ensemble(nens, dbname, res, startyear,
                               startmonth, startday, endyear, endmonth, endday, name)
    if 'initialize' in options['vic'] and options['vic']['initialize']:
        init_method = options['vic']['initialize']
        if isinstance(init_method, bool):
            init_method = "determ"
        models.initialize(options, basin, init_method, vicexe)
    else:
        models.writeSoilFiles(basin)
    if 'observations' in options['vic']:
        method = "random"
        obsnames = options['vic']['observations'].split(",")
        if 'update' in options['vic']:
            update = options['vic']['update']
        else:
            update = None
        updateDates = observationDates(
            obsnames, dbname, startyear, startmonth, startday, endyear, endmonth, endday, update)
        t0 = date(startyear, startmonth, startday)
        updateDates += [date(endyear, endmonth, endday)]
        for t in updateDates:
            if t0 == date(startyear, startmonth, startday):
                overwrite = True
            else:
                overwrite = False
            ndays = (date(t.year, t.month, t.day) - t0).days
            t1 = t + timedelta(1)
            models.setDates(t.year, t.month, t.day, t1.year, t1.month, t1.day)
            models.initialize(options, basin, method, vicexe, saveindb=True,
                              saveto=saveto, saveargs=savevars, initdays=ndays, overwrite=overwrite)
            data, alat, alon, agid = assimilate(options, date(
                models.startyear, models.startmonth, models.startday), models)
            db = dbio.connect(models.dbname)
            cur = db.cursor()
            sql = "select tablename from pg_tables where schemaname='{0}'".format(
                models.name)
            cur.execute(sql)
            tables = [tbl[0] for tbl in cur.fetchall() if tbl[0] != "dssat"]
            for tbl in tables:
                sql = "delete from {0}.{1} where fdate=date '{2}-{3}-{4}'".format(
                    models.name, tbl, t.year, t.month, t.day)
            cur.close()
            db.close()
            if bool(data):
                models.updateStateFiles(data, alat, alon, agid)
            t0 = date(t.year, t.month, t.day)
    else:
        method = "random"
        t = date(endyear, endmonth, endday)
        t1 = t + timedelta(1)
        models.setDates(t.year, t.month, t.day, t1.year, t1.month, t1.day)
        ndays = (t - date(startyear, startmonth, startday)).days
        models.initialize(options, basin, method, vicexe, saveindb=True,
                          saveto=saveto, saveargs=savevars, initdays=ndays)
    for varname in savevars:
        raster.stddev(models.dbname, "{0}.{1}".format(
            models.name, varname))
    for model in models:
        shutil.rmtree(model.model_path)
Esempio n. 6
0
def runEnsembleVIC(dbname, options):
    """Driver function for performing a VIC nowcast simulation."""
    res = config.getResolution(options['nowcast'])
    name = options['nowcast']['name'].lower()
    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('-'))
    precipdatasets = options['vic']['precip'].split(",")
    savestate, _ = _saveState(options['vic'])
    if 'ensemble size' in options['vic']:
        nens = int(options['vic']['ensemble size'])
    elif 'observations' in options['vic']:
        nens = 20
    else:
        nens = len(precipdatasets)
    models = ensemble.Ensemble(nens, dbname, res, startyear,
                               startmonth, startday, endyear, endmonth, endday, name)
    if 'initialize' in options['vic'] and options['vic']['initialize']:
        init_method = options['vic']['initialize']
        if isinstance(init_method, bool):
            init_method = "determ"
        models.initialize(options, basin, init_method, vicexe)
    else:
        models.writeSoilFiles(basin)
    if 'observations' in options['vic']:
        method = "random"
        obsnames = options['vic']['observations'].split(",")
        if 'update' in options['vic']:
            update = options['vic']['update']
        else:
            update = None
        updateDates = observationDates(
            obsnames, dbname, startyear, startmonth, startday, endyear, endmonth, endday, update)
        t0 = date(startyear, startmonth, startday)
        updateDates += [date(endyear, endmonth, endday)]
        for t in updateDates:
            if t0 == date(startyear, startmonth, startday):
                overwrite = True
            else:
                overwrite = False
            ndays = (date(t.year, t.month, t.day) - t0).days
            t1 = t + timedelta(1)
            models.setDates(t.year, t.month, t.day, t1.year, t1.month, t1.day)
            models.initialize(options, basin, method, vicexe, saveindb=True,
                              saveto=saveto, saveargs=savevars, initdays=ndays, overwrite=overwrite)
            data, alat, alon, agid = assimilate(options, date(
                models.startyear, models.startmonth, models.startday), models)
            db = dbio.connect(models.dbname)
            cur = db.cursor()
            sql = "select tablename from pg_tables where schemaname='{0}'".format(
                models.name)
            cur.execute(sql)
            tables = [tbl[0] for tbl in cur.fetchall() if tbl[0] != "dssat"]
            for tbl in tables:
                sql = "delete from {0}.{1} where fdate=date '{2}-{3}-{4}'".format(
                    models.name, tbl, t.year, t.month, t.day)
            cur.close()
            db.close()
            if bool(data):
                models.updateStateFiles(data, alat, alon, agid)
            t0 = date(t.year, t.month, t.day)
    else:
        method = "random"
        t = date(endyear, endmonth, endday)
        t1 = t + timedelta(1)
        models.setDates(t.year, t.month, t.day, t1.year, t1.month, t1.day)
        ndays = (t - date(startyear, startmonth, startday)).days
        models.initialize(options, basin, method, vicexe, saveindb=True,
                          saveto=saveto, saveargs=savevars, initdays=ndays)
    for varname in savevars:
        raster.stddev(models.dbname, "{0}.{1}".format(
            models.name, varname))
    for model in models:
        shutil.rmtree(model.model_path)