Ejemplo n.º 1
0
def test_wrfdir_setup():
    """If on AWS, Cheyenne, or Magma, tests that the WRF directory can be setup correclty."""
    if [on_aws, on_cheyenne, on_magma].count(True) is 0:
        print(
            '\n!!!Not running test_wrfdir_setup -- switch to Magma, Cheyenne, or AWS.'
        )
        return
    wrf_sim = WRFModel(param_ids, start_date, end_date)
    vtable_sfx = wrf_sim.get_bc_data()
    wrf_sim.wrfdir_setup(vtable_sfx)
    assert os.path.exists(wrf_sim.DIR_RUNWRF + 'wrf2era_error.ncl') == 1
Ejemplo n.º 2
0
def test_prepare_namelists():
    """If on AWS, Cheyenne, or Magma, tests that the template namelists can be edited
    to reflect the desired configuration for the current WRF model run."""
    if [on_aws, on_cheyenne, on_magma].count(True) is 0:
        print(
            '\n!!!Not running test_prepare_namelists -- switch to Magma, Cheyenne, or AWS.'
        )
        return
    wrf_sim = WRFModel(param_ids, start_date, end_date)
    vtable_sfx = wrf_sim.get_bc_data()
    wrf_sim.wrfdir_setup(vtable_sfx)
    wrf_sim.prepare_namelists()
    assert 0 == 0
Ejemplo n.º 3
0
def test_run_wps():
    """If on AWS, Cheyenne, or Magma, tests that WPS runs successfully."""
    if [on_aws, on_cheyenne, on_magma].count(True) is 0:
        print(
            '\n!!!Not running test_run_wps -- switch to Magma, Cheyenne, or AWS.'
        )
        return
    wrf_sim = WRFModel(param_ids, start_date, end_date)
    vtable_sfx = wrf_sim.get_bc_data()
    wrf_sim.wrfdir_setup(vtable_sfx)
    wrf_sim.prepare_namelists()
    success = wrf_sim.run_wps()
    lastmetfile = f'met_em.d{str(wrf_sim.n_domains).zfill(2)}.{wrf_sim.forecast_end.strftime("%Y-%m-%d")}_00:00:00.nc'
    assert success is True
    assert os.path.exists(wrf_sim.DIR_WRFOUT + lastmetfile)
Ejemplo n.º 4
0
def get_wrf_fitness(param_ids, start_date='Jan 15 2011', end_date='Jan 16 2011',
                    bc_data='ERA', n_domains=1, correction_factor=0.0004218304553577255,
                    setup_yaml='dirpath.yml', disable_timeout=False, verbose=False):
    """
    Using the input physics parameters, date, boundary condition, and domain data,
    this function runs the WRF model and computes the error between WRF and ERA5.

    :param param_ids: list of integers
        corresponding to each WRF physics parameterization.
    :param start_date: string
        specifying a desired start date.
    :param end_date: string
        specifying a desired end date.
    :param bc_data: string
        specifying the boundary condition data to be used for the WRF forecast.
        Currently, only ERA data (ds627.0) is supported.
    :param n_domains: integer
        specifing the number of WRF model domains. 1 - 3 domains are currently supported.
    :param correction_factor: float
        capuring the relationship between GHI and wind power density (WPD) errors averaged
        across an entrie year. Calculated using opwrf/examples/Fitness_correction_factor.py.
    :param setup_yaml: string
        defining the path to the yaml file where input directory paths are specified.
    :param disable_timeout: boolean (default = False)
        telling runwrf if subprogram timeouts are allowed or not.
    :param verbose: boolean (default = False)
        instructing the program to print everything or just key information to the screen.
    :return fitness: float
        value denoting how well the WRF model run performed.
        Fitness is a measure of accumlated error, so a lower value is better.

    """
    if verbose:
        print('- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -')
        print('\nCalculating fitness for: {}'.format(param_ids))

    # Create a WRFModel instance
    wrf_sim = WRFModel(param_ids, start_date, end_date,
                       bc_data=bc_data, n_domains=n_domains, setup_yaml=setup_yaml, verbose=verbose)

    # Check to see if WRFModel instance exists; if not, run the WRF model.
    wrfout_file_path = wrf_sim.DIR_WRFOUT + 'wrfout_d01.nc'
    orig_wrfout_file_path = wrf_sim.DIR_WRFOUT + 'wrfout_d01_' \
                       + wrf_sim.forecast_start.strftime('%Y') + '-' \
                       + wrf_sim.forecast_start.strftime('%m') + '-' \
                       + wrf_sim.forecast_start.strftime('%d') + '_00:00:00'
    if [os.path.exists(file) for file in [wrfout_file_path, orig_wrfout_file_path]].count(True) is 0:
        # Next, get boundary condition data for the simulation
        # ERA is the only supported data type right now.
        vtable_sfx = wrf_sim.get_bc_data()

        # Setup the working directory to run the simulation
        success = wrf_sim.wrfdir_setup(vtable_sfx)

        # Prepare the namelists
        if success:
            success = wrf_sim.prepare_namelists()

        # Run WPS
        if success:
            success = wrf_sim.run_wps(disable_timeout)
            if verbose:
                print(f'WPS ran successfully? {success}')

        # Run REAL
        if success:
            success = wrf_sim.run_real(disable_timeout)
            if verbose:
                print(f'Real ran successfully? {success}')

        # RUN WRF
        if success:
            success, runtime = wrf_sim.run_wrf(disable_timeout)
            if verbose:
                print(f'WRF ran successfully? {success}')
        else:
            runtime = '00h 00m 00s'
    else:
        success = True
        runtime = '00h 00m 00s'

    # Postprocess wrfout file and ERA5 data
    if success:
        proc_wrfout_file_path = wrf_sim.DIR_WRFOUT + 'wrfout_processed_d01.nc'
        if not os.path.exists(proc_wrfout_file_path):
            if verbose:
                print(f'Postprocessing wrfout file...')
            success = wrf_sim.process_wrfout_data()
        wrf_sim.process_era5_data()

    # Compute the error between WRF run and ERA5 dataset and return fitness
    if success:
        mae = wrf_sim.wrf_era5_diff()
        ghi_total_error = mae[1]
        wpd_total_error = mae[2]
        daylight_factor = hf.daylight_frac(start_date)  # daylight fraction
        fitness = daylight_factor * ghi_total_error + correction_factor * wpd_total_error
        if verbose:
            print(f'!!! Physics options set {param_ids} has fitness {fitness}')

    else:
        ghi_total_error = 6.022 * 10 ** 23
        wpd_total_error = 6.022 * 10 ** 23
        fitness = 6.022 * 10 ** 23

    return fitness, ghi_total_error, wpd_total_error, runtime
Ejemplo n.º 5
0
param_ids = flexible_generate(generate_params, mp, lw, sw, lsm, pbl, cu,
                              in_yaml)

wrf_sim = WRFModel(param_ids,
                   start_date,
                   end_date,
                   bc_data=bc_data,
                   n_domains=n_domains,
                   setup_yaml=setup_yaml)

# Next, get boundary condition data for the simulation
# ERA is the only supported data type right now.
vtable_sfx = wrf_sim.get_bc_data()

# Setup the working directory to run the simulation
wrf_sim.wrfdir_setup(vtable_sfx)

# Prepare the namelist
wrf_sim.prepare_namelists()

# Run WPS
success = wrf_sim.run_wps(disable_timeout=True)
print(f'WPS ran successfully? {success}')

# RUN REAL
if success:
    success = wrf_sim.run_real(disable_timeout=True)
    print(f'Real ran successfully? {success}')

# RUN WRF
if success: