def test_get_bc_data_w_csdapi(setup_yaml='mac_dirpath.yml'): """ Checks if WRF boundary condition data can be downloaded using the CDS api. This definitely works best if done on Magma using setup_yaml='dirpath.yml', but if you do want to run it on your local machine make sure that you change setup_yaml='local_dirpath.yml', where local_dirpath.yml is a yaml file where you specify root directory paths. Note to self: this test only works on the linux pc. """ if on_magma: wrf_sim = WRFModel(param_ids, start_date, end_date, bc_data='ERA5') else: print( f'WARNING: this test requires you to manually provide setup_yaml!\n' f'You have specified {setup_yaml}.') wrf_sim = WRFModel(param_ids, start_date, end_date, bc_data='ERA5', setup_yaml=setup_yaml) vtable_sfx = wrf_sim.get_bc_data() print(f'The following data files are in {wrf_sim.DIR_DATA_TMP}:\n') [print(name) for name in os.listdir(wrf_sim.DIR_DATA_TMP)] assert vtable_sfx == 'ERA-interim.pl' assert len([name for name in os.listdir(wrf_sim.DIR_DATA_TMP)]) > 0
def test_process_wrfout_data(): wrf_sim = WRFModel(param_ids, start_date, end_date) wrfout_file = wrf_sim.DIR_WRFOUT + 'wrfout_d01.nc' if not os.path.exists(wrfout_file): print(f'\nYour have an incorrect wrfout file path:\n{wrfout_file}.') raise FileNotFoundError wrf_sim.process_wrfout_data() processed_wrfout_file = wrf_sim.DIR_WRFOUT + 'wrfout_processed_d01.nc' assert os.path.exists(processed_wrfout_file) == 1
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
def test_process_era5_data(): if [on_aws, on_cheyenne, on_magma].count(True) is 0: print( '\n!!!Not running test_process_era5_data -- switch to Magma, Cheyenne, or AWS.' ) return wrf_sim = WRFModel(param_ids, start_date, end_date) wrf_sim.process_era5_data() ERA5_ROOT = '/share/mzhang/jas983/wrf_data/data/ERA5/' processed_era_file = ERA5_ROOT + 'ERA5_EastUS_WPD-GHI_' \ + wrf_sim.forecast_start.strftime('%Y') + '-' \ + wrf_sim.forecast_start.strftime('%m') + '.nc' assert os.path.exists(processed_era_file) == 1
def test_WRFModel(): """Checks that the WRFModel method works correctly. The assert statements assume that you are using dirpth.yml not one made for a local machine. Should probably change this at some point...""" wrf_sim = WRFModel(param_ids, start_date, end_date) assert wrf_sim.DIR_WRF == '/home/jas983/models/wrf/WRF/' assert wrf_sim.DIR_WPS_GEOG == '/share/mzhang/jas983/wrf_data/WPS_GEOG/' assert wrf_sim.DIR_RUNWRF == '/share/mzhang/jas983/wrf_data/met4ene/optwrf/optwrf/' assert wrf_sim.start_date == start_date
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)
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
def test_wrf_era5_diff_pyresample(): if [on_aws, on_cheyenne, on_magma].count(True) is 0: print( '\n!!!Not running test_wrf_era5_diff -- switch to Magma, Cheyenne, or AWS.' ) return wrf_sim = WRFModel(param_ids, start_date, end_date) processed_wrfout_file = wrf_sim.DIR_WRFOUT + 'wrfout_processed_d01.nc' ERA5_ROOT = '/share/mzhang/jas983/wrf_data/data/ERA5/' processed_era_file = ERA5_ROOT + 'ERA5_EastUS_WPD-GHI_' \ + wrf_sim.forecast_start.strftime('%Y') + '-' \ + wrf_sim.forecast_start.strftime('%m') + '.nc' if not os.path.exists(processed_wrfout_file): wrf_sim.process_wrfout_data() if not os.path.exists(processed_era_file): wrf_sim.process_era5_data() mae = wrf_sim.wrf_era5_diff(method='pyresample') total_error = sum(mae) assert total_error >= 0
""" Process ERA5 Data ================= This example lets you manually process the ERA5 data. I created this because restarting OptWRF is difficult (high-memory) unless all files have been created previously. """ from optwrf.runwrf import WRFModel # Specify desired physics options set below param_ids = [8, 7, 3, 1, 1, 10, 1] # Specify the desired start date below start_date = 'Aug 11 2011' end_date = 'Aug 12 2011' # Run get wrf fitness function wrf_sim = WRFModel(param_ids, start_date, end_date, verbose=True) wrf_sim.process_era5_data()
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
import os import sys # Define the parameters and start/end dates for the 'default' simulation param_ids = [10, 1, 1, 2, 2, 3, 2] start_dates = ['Jan 1 2011', 'Feb 1 2011', 'Mar 1 2011', 'Apr 1 2011', 'May 1 2011', 'Jun 1 2011', 'Jul 1 2011', 'Aug 1 2011', 'Sep 1 2011', 'Oct 1 2011', 'Nov 1 2011', 'Dec 1 2011'] end_dates = ['Feb 1 2011', 'Mar 1 2011', 'Apr 1 2011', 'May 1 2011', 'Jun 1 2011', 'Jul 1 2011', 'Aug 1 2011', 'Sep 1 2011', 'Oct 1 2011', 'Nov 1 2011', 'Dec 1 2011', 'Jan 1 2012'] # Postprocess all the wrfout files if necessary print('Starting wrfout file processing...') for start_date, end_date in zip(start_dates, end_dates): wrf_sim = WRFModel(param_ids, start_date, end_date) processed_wrfout_file = wrf_sim.DIR_WRFOUT + 'wrfout_processed_d01.nc' if not os.path.exists(processed_wrfout_file): wrfout_file = wrf_sim.DIR_WRFOUT + 'wrfout_d01.nc' if not os.path.exists(wrfout_file): print(f'\nYour have an incorrect wrfout file path:\n{wrfout_file}.') raise FileNotFoundError wrf_sim.process_wrfout_data() sys.stdout.flush() print('!Done!') # Process all the ERA5 data if necessary print('Starting ERA5 file processing...') for start_date, end_date in zip(start_dates, end_dates): wrf_sim = WRFModel(param_ids, start_date, end_date) ERA5_ROOT = '/share/mzhang/jas983/wrf_data/data/ERA5/'
n_domains = int(args.d) else: n_domains = 3 # Format the forecast start/end and determine the total time. start_date = args.s end_date = args.e # Generate a parameter combination of the 6 core parameters if the user has specified this option. # Otherwise, use specified input parameters and use defaults for the remaining paramters. 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)
import os import sys # Define the parameters and start/end dates for the 'default' simulation param_ids = [10, 1, 1, 2, 2, 3, 2] start_dates = ['Jan 1 2011', 'Feb 1 2011', 'Mar 1 2011', 'Apr 1 2011', 'May 1 2011', 'Jun 1 2011', 'Jul 1 2011', 'Aug 1 2011', 'Sep 1 2011', 'Oct 1 2011', 'Nov 1 2011', 'Dec 1 2011'] end_dates = ['Feb 1 2011', 'Mar 1 2011', 'Apr 1 2011', 'May 1 2011', 'Jun 1 2011', 'Jul 1 2011', 'Aug 1 2011', 'Sep 1 2011', 'Oct 1 2011', 'Nov 1 2011', 'Dec 1 2011', 'Jan 1 2012'] # Postprocess all the wrfout files if necessary print('Starting wrfout file processing...') for start_date, end_date in zip(start_dates, end_dates): wrf_sim = WRFModel(param_ids, start_date, end_date) wrf_sim.DIR_WRFOUT = wrf_sim.DIR_MET4ENE + 'wrfout/ARW/default_all_2011/%s_' % \ (wrf_sim.forecast_start.strftime('%Y-%m-%d')) + wrf_sim.paramstr + '/' processed_wrfout_file = wrf_sim.DIR_WRFOUT + 'wrfout_processed_d01.nc' if not os.path.exists(processed_wrfout_file): wrfout_file = wrf_sim.DIR_WRFOUT + 'wrfout_d01.nc' if not os.path.exists(wrfout_file): print(f'\nYour have an incorrect wrfout file path:\n{wrfout_file}.') raise FileNotFoundError wrf_sim.process_wrfout_data() sys.stdout.flush() print('!Done!') # Process all the ERA5 data if necessary print('Starting ERA5 file processing...') for start_date, end_date in zip(start_dates, end_dates):