<commit_before>""" RHEAS module for main functionality.

.. module:: rheas
   :synopsis: Module for executing the RHEAS system

.. moduleauthor:: <NAME> <<EMAIL>>

"""

import config
import nowcast
import forecast
import argparse
import dbio
import datasets
from datetime import datetime


def parseArgs():
    """Parses command line arguments and prints help messages."""
    parser = argparse.ArgumentParser(description='Runs RHEAS simulation.')
    parser.add_argument('config', help='configuration file')
    parser.add_argument('-d', metavar='DB', help='name of database to connect')
    parser.add_argument('-u', help='update database', action='store_true')
    args = parser.parse_args()
    return args.config, args.d, args.u


def update(dbname, configfile):
    """Fetch datasets and update database."""
    conf = datasets.readDatasetList(configfile)
    try:
        bbox = map(lambda s: conf.getfloat('domain', s), [
                   'minlon', 'minlat', 'maxlon', 'maxlat'])
    except:
        bbox = None
    for name in conf.sections():
        if name != 'domain':
            try:
                mod = __import__("datasets.{0}".format(name), fromlist=[name])
            except:
                mod = None
            if mod is None:
                # download generic datasets
                datasets.download(name, dbname, bbox)
            else:
                if conf.has_option(name, 'startdate'):
                    t0 = datetime.strptime(conf.get(name, 'startdate'), "%Y-%m-%d")
                else:
                    t0 = None
                if conf.has_option(name, 'enddate'):
                    t1 = datetime.strptime(conf.get(name, 'enddate'), "%Y-%m-%d")
                else:
                    t1 = datetime.today()
                dt = mod.dates(dbname)
                if t0 is None:
                    if dt is None:
                        print("WARNING! Date information for {0} not found in the database or data.conf. Please add a startdate in the data.conf file.")
                    else:
                        dt = (dt[0], t1)
                else:
                    dt = (t0, t1)
                if dt is not None:
                    mod.download(dbname, dt, bbox)


def run():
    """Main RHEAS routine."""
    config_filename, dbname, db_update = parseArgs()
    if dbname is None:
        dbname = "rheas"
    dbio.connect(dbname)
    # check if database update is requested
    if db_update:
        print "Updating database!"
        update(dbname, config_filename)
    else:
        options = config.loadFromFile(config_filename)
        # check what simulations have been requested
        if "nowcast" in options:
            nowcast.execute(dbname, options)
        if "forecast" in options:
            forecast.execute(dbname, options)


if __name__ == '__main__':
    run()
<commit_msg>Fixed generic download function caller.<commit_after>""" RHEAS module for main functionality.

.. module:: rheas
   :synopsis: Module for executing the RHEAS system

.. moduleauthor:: <NAME> <<EMAIL>>

"""

import config
import nowcast
import forecast
import argparse
import dbio
import datasets
from datetime import datetime


def parseArgs():
    """Parses command line arguments and prints help messages."""
    parser = argparse.ArgumentParser(description='Runs RHEAS simulation.')
    parser.add_argument('config', help='configuration file')
    parser.add_argument('-d', metavar='DB', help='name of database to connect')
    parser.add_argument('-u', help='update database', action='store_true')
    args = parser.parse_args()
    return args.config, args.d, args.u


def update(dbname, configfile):
    """Fetch datasets and update database."""
    conf = datasets.readDatasetList(configfile)
    try:
        bbox = map(lambda s: conf.getfloat('domain', s), [
                   'minlon', 'minlat', 'maxlon', 'maxlat'])
    except:
        bbox = None
    for name in conf.sections():
        if name != 'domain':
            try:
                mod = __import__("datasets.{0}".format(name), fromlist=[name])
            except:
                mod = None
            if mod is None:
                # download generic datasets
                datasets.download(dbname, conf)
            else:
                if conf.has_option(name, 'startdate'):
                    t0 = datetime.strptime(conf.get(name, 'startdate'), "%Y-%m-%d")
                else:
                    t0 = None
                if conf.has_option(name, 'enddate'):
                    t1 = datetime.strptime(conf.get(name, 'enddate'), "%Y-%m-%d")
                else:
                    t1 = datetime.today()
                dt = mod.dates(dbname)
                if t0 is None:
                    if dt is None:
                        print("WARNING! Date information for {0} not found in the database or data.conf. Please add a startdate in the data.conf file.")
                    else:
                        dt = (dt[0], t1)
                else:
                    dt = (t0, t1)
                if dt is not None:
                    mod.download(dbname, dt, bbox)


def run():
    """Main RHEAS routine."""
    config_filename, dbname, db_update = parseArgs()
    if dbname is None:
        dbname = "rheas"
    dbio.connect(dbname)
    # check if database update is requested
    if db_update:
        print "Updating database!"
        update(dbname, config_filename)
    else:
        options = config.loadFromFile(config_filename)
        # check what simulations have been requested
        if "nowcast" in options:
            nowcast.execute(dbname, options)
        if "forecast" in options:
            forecast.execute(dbname, options)


if __name__ == '__main__':
    run()

{'fraction_non_alphanumeric': 0.11006617903169627, 'fraction_numerical': 0.0038314176245210726, 'mean_word_length': 1.9406041986687148, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '449710', 'n_tokens_mistral': 1618, 'n_tokens_neox': 1563, 'n_words': 475}