def test_generate_random_dates():
    """Check if start and end dates can be randomly generated."""
    random_start_date, random_end_date = sga.generate_random_dates()
    print(f'Starting forecast on {random_start_date}')
    print(f'Ending forecast on {random_end_date}')
    r_start_date, r_end_date = sga.generate_random_dates(
        input_start_date=start_date)
    print(f'Starting forecast on {r_start_date}')
    print(f'Ending forecast on {r_end_date}')
    assert random_start_date is not None
    assert random_end_date is not None
    assert r_start_date is not None
    assert r_end_date is not None
def test_update_sim():
    """Checks to make sure that entries in the database can be updated."""
    # Generate a random set of parameters, a random start date, and a Chromosome
    r_start_date, r_end_date = sga.generate_random_dates()
    r_param_ids = wp.flexible_generate()
    individual = sga.Chromosome(r_param_ids,
                                fitness=100,
                                start_date=r_start_date,
                                end_date=r_end_date)
    # Put individual in the database
    db_conn = conn_to_db('optwrf_repeat.db')
    owp.insert_sim(individual, db_conn)
    print_database(db_conn)
    # Generate a new random start date and a Chromosome
    r_start_date, r_end_date = sga.generate_random_dates()
    individual = sga.Chromosome(r_param_ids,
                                fitness=50,
                                start_date=r_start_date,
                                end_date=r_end_date)
    # Update the individual in the database database
    owp.update_sim(individual, db_conn)
    print_database(db_conn)
def seed_initial_population(input_csv):
    """
    Reads the input csv file, which contains the dates and/or parameter combinations
    that you would like to see in the initial population. The CSV file should be
    formatted as follows (i.e., this function looks for these column names):

    Line 1: start_date, mp_physics, ra_lw_physics, ra_sw_physics, sf_surface_physics,
            bl_pbl_physics, cu_physics

    Line 2: Feb 28 2011,  1, 24, 99,  4, 11,  2

    Line 3: Aug 31 2011, 28,  4,  5,  2, 10, 93

    Line 4: Dec 10 2011,  7, 24,  3,  4,  9,  4

    Line 5: Jul 22 2011, 11, 24,  2,  1,  4, 10

    Line 6: May 23 2011,  1,  4,  5,  2, 10, 93

    Line 7: Mar 31 2011, 28, 24, 99,  4, 11,  2

    .

    .

    .

    :param input_csv: string
        defines the file name from which dates/parameters will be read.
    :return intial population: list of Chromosome instances
        made from the information specified in the input file.

    """
    initial_population = []
    try:
        with open(input_csv, newline='') as csvfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                start_date = row['start_date']
                param_ids = wrfparams.flexible_generate(generate_params=False,
                                                        mp=int(row['mp_physics']), lw=int(row['ra_lw_physics']),
                                                        sw=int(row['ra_sw_physics']), lsm=int(row['sf_surface_physics']),
                                                        pbl=int(row['bl_pbl_physics']), cu=int(row['cu_physics']))
                start_date, end_date = simplega.generate_random_dates(input_start_date=start_date)
                initial_population.append(Chromosome(param_ids, start_date, end_date))
    except IOError:
        print(f'IOEror in seed_initial_population: {input_csv} does not exist.')
        return initial_population

    return initial_population
def get_individual_by_genes(individual, db_conn):
    """
    Looks for an indivual set of genes in an SQLite database.

    ***NOTE: I'm not sure if I want to remove the first where clause contianing the start date.

    :param individual: simplega.Chromosome instance
        describing the simulation that you would to extract from the SQL simulation database
        if it exists there.
    :param db_conn: database connection object
        created using the conn_to_db() function.
    :return past_sim: simplega.Chromosome instance
        describing the simulation that was run previously and stored in the SQL simulation database,
        and corresponding to the input individual function parameter.

    """
    c = db_conn.cursor()
    c.execute("""SELECT * FROM simulations 
                WHERE start_date = :start_date
                AND mp_physics = :mp_physics
                AND ra_lw_physics = :ra_lw_physics
                AND ra_sw_physics = :ra_sw_physics
                AND sf_surface_physics = :sf_surface_physics
                AND bl_pbl_physics = :bl_pbl_physics
                AND cu_physics = :cu_physics
                AND sf_sfclay_physics = :sf_sfclay_physics""",
              {'start_date': individual.Start_date, 'mp_physics': individual.Genes[0],
               'ra_lw_physics': individual.Genes[1],
               'ra_sw_physics': individual.Genes[2], 'sf_surface_physics': individual.Genes[3],
               'bl_pbl_physics': individual.Genes[4], 'cu_physics': individual.Genes[5],
               'sf_sfclay_physics': individual.Genes[6]})
    sim_data = c.fetchone()
    if sim_data is not None:
        param_ids = list(sim_data[1:-2])
        s_date = sim_data[0]
        s_date, e_date = simplega.generate_random_dates(input_start_date=s_date)
        fitness = sim_data[-2]
        runtime = sim_data[-1]
        past_sim = Chromosome(param_ids, s_date, e_date, fitness, runtime)
    else:
        return None
    return past_sim