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
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: success, runtime = wrf_sim.run_wrf(disable_timeout=True) print(f'WRF ran successfully? {success}')