def test_thread_set(self): omp_set_num_threads(4) max_threads = omp_get_max_threads() self.assertEqual(max_threads, 4) num_threads = omp_get_num_threads() self.assertEqual(num_threads, 1) # Always 1 outside of parallel region thread_num = omp_get_thread_num() self.assertEqual(thread_num, 0) # Always 0 outside of parallel region num_procs = omp_get_num_procs() in_parallel = omp_in_parallel() self.assertFalse(in_parallel) # Always False outside of parallel region limit = omp_get_thread_limit()
def main(): # Get files from command line or take hard-coded folder. # Arguements are optional, but if one is specified, they all should be # vertical_profile_plots.py [input_pattern] [output_directory] [num_threads] # you can use a wildcard pattern: # i.e., python vertical_profile_plots.py ../output_files/wrfout* ../plots/ 8 # or you can list the input files: # i.e., python vertical_profile_plots.py ../output_files/wrfout_d01 ../output_files/wrfout_d02 ../plots/ 1 if len(sys.argv) > 1: filenames = sys.argv[1:-2] #glob.glob(sys.argv[1]) print(filenames) output_dir = sys.argv[-2] wrf.omp_set_num_threads(int(sys.argv[-1])) else: filenames = glob.glob( "/project/ssmith_uksr/WRF_ARW/cases/eclipse_2017/eclipse_model_on_5_dom_out/wrfout_d01*" ) output_dir = '' # Get data from published sensor data ws_workbook = xlrd.open_workbook( '/project/ssmith_uksr/WRF_ARW/DATA/2017_eclipse_observed/Weather_Station_data.xlsx' ) ws_first_sheet = ws_workbook.sheet_by_index(0) tower_workbook = xlrd.open_workbook( '/project/ssmith_uksr/WRF_ARW/DATA/2017_eclipse_observed/Tower_data.xlsx' ) tower_first_sheet = tower_workbook.sheet_by_index(0) soil_workbook = xlrd.open_workbook( '/project/ssmith_uksr/WRF_ARW/DATA/2017_eclipse_observed/Soil_data.xlsx' ) soil_first_sheet = soil_workbook.sheet_by_index(0) time_ws, temp_ws, rad_ws, wspd_ws, wdir_ws = get_observed_series( ws_first_sheet) time_tower, temp_tower, _, wspd_tower, wdir_tower = get_observed_series( tower_first_sheet) time_soil, temp_soil, _, _, _ = get_observed_series(soil_first_sheet) # Coordinates to take sample from center_lat = 36.797326 center_lon = -86.812341 for filename in sorted(filenames): print(filename) #Structure the WRF output file ncfile = Dataset(filename) #Extract data from WRF output files tc = wrf.getvar(ncfile, "tc", wrf.ALL_TIMES) # Atmospheric temperature in celsius t2 = wrf.getvar(ncfile, "T2", wrf.ALL_TIMES) # Temperature at 2 m, in Kelvin # Convert T2 to degrees C t2 = t2 - 273.15 t2.attrs["units"] = "degC" theta = wrf.getvar(ncfile, "theta", wrf.ALL_TIMES, units="degC") rh = wrf.getvar(ncfile, "rh", wrf.ALL_TIMES) wspd_wdir = wrf.getvar(ncfile, "uvmet_wspd_wdir", wrf.ALL_TIMES) # Split wind speed and direction wspd = wspd_wdir[0, :, :, :, :] wdir = wspd_wdir[1, :, :, :, :] # These variables aren't included in getvar, so have to be extracted manually swdown = wrf.extract_vars(ncfile, wrf.ALL_TIMES, "SWDOWN").get('SWDOWN') gnd_flx = wrf.extract_vars(ncfile, wrf.ALL_TIMES, "GRDFLX").get('GRDFLX') #Create Dictionary to associate quanitity names with the corresponding data two_dim_vars = {'swdown': swdown, 'gnd_flx': gnd_flx, 'T2': t2} three_dim_vars = {'tc': tc, 'theta': theta, 'rh': rh, 'wspd': wspd} #Get the grid coordinates from our earth lat/long coordinates center_x, center_y = wrf.ll_to_xy(ncfile, center_lat, center_lon) # Plot all 3D variables over time for var_name, var_data in three_dim_vars.items(): # Convert to Local Time var_data = to_local_time(var_data) # Get data frequency freq = pd.Timedelta(var_data["Time"][1].values - var_data["Time"][0].values) # Interpolate to height above ground level try: var_data_agl = wrf.vinterp(ncfile, var_data, 'ght_agl', np.linspace(0, 0.1, 100), field_type=var_name, timeidx=wrf.ALL_TIMES) except ValueError: var_data_agl = wrf.vinterp(ncfile, var_data, 'ght_agl', np.linspace(0, 0.1, 100), field_type='none', timeidx=wrf.ALL_TIMES) # Convert height to meters var_data_agl["interp_level"] = var_data_agl["interp_level"] * 1000 # Time ranges plot_time_ranges = [] plot_time_range1 = pd.date_range(start="2017-08-21T10:24:00", end="2017-08-21T14:33:00", freq=freq) plot_time_range1 = plot_time_range1.floor(freq) plot_time_range2 = pd.date_range(start="2017-08-21T13:09:00", end="2017-08-21T14:33:00", freq=freq) plot_time_range2 = plot_time_range2.floor(freq) plot_time_range3 = pd.date_range(start="2017-08-21T09:45:00", end="2017-08-21T15:00:00", freq=freq) plot_time_range3 = plot_time_range3.floor(freq) plot_time_ranges = [ plot_time_range1, plot_time_range2, plot_time_range3 ] # Vertical Profile Plots for plot_time_range in [ rng for rng in plot_time_ranges if len(rng) > 1 ]: fig, ax = plt.subplots() var_data_agl.isel( south_north=center_y, west_east=center_x).sel(Time=plot_time_range, method="nearest").plot(ax=ax, x="Time") save_plot(ax=ax, title='', y_label="z (m)", x_label="Local Time (CDT)", var_name=var_name, plot_type_name="vertical_profile", plot_time_range=plot_time_range, filename_in=filename, output_dir=output_dir) plt.close(fig) # Line plots fig, ax = plt.subplots() if (var_name == 'tc'): ax.plot(time_ws, temp_ws, '^k-', label='2.5m', markevery=500) ax.plot(time_soil, temp_soil, 'vb-', label='-0.02m', markevery=500) if (var_name == 'wspd'): y_label = "wind speed" + " (" + var_data.attrs[ "units"] + ")" wspd_ws_rolling = pd.DataFrame(wspd_ws).rolling( 120).mean().values wspd_tower_rolling = pd.DataFrame(wspd_tower).rolling( 120).mean().values ax.plot(time_ws, wspd_ws_rolling, 'c-', label='3 m, 2 min avg', linewidth=0.5) ax.plot(time_tower, wspd_tower_rolling, 'k-', label='7 m, 2 min avg', linewidth=0.5, zorder=0) var_data.isel(bottom_top=0, south_north=center_y, west_east=center_x).sel(Time=plot_time_range, method="nearest").plot( ax=ax, x="Time", label="WRF-Eclipse", color="orange") y_label = var_data.name + " (" + var_data.attrs["units"] + ")" save_plot(ax=ax, title='', y_label=y_label, x_label="Local Time (CDT)", var_name=var_name, plot_type_name="line_plot", plot_time_range=plot_time_ranges[2], filename_in=filename, output_dir=output_dir) plt.close(fig) # Plot 2D values for var_name, var_data in two_dim_vars.items(): # Convert to Local Time var_data = to_local_time(var_data) # Line plots fig, ax = plt.subplots() y_label = var_data.name + " (" + var_data.attrs["units"] + ")" if (var_name == 'swdown'): ax.plot(time_ws, rad_ws, 'or-', label='measured', linewidth=0.5, markevery=500) y_label = "solar radiation" + " (" + var_data.attrs[ "units"] + ")" if (var_name == 'T2'): ax.plot(time_ws, temp_ws, '^k-', label='2.5m', markevery=500) ax.plot(time_soil, temp_soil, 'vb-', label='-0.02m', markevery=500) y_label = "temperature" + " (" + var_data.attrs["units"] + ")" var_data.isel(south_north=center_y, west_east=center_x).sel(Time=plot_time_range, method="nearest").plot( ax=ax, x="Time", label="WRF-Eclipse", color="orange") save_plot(ax=ax, title='', y_label=y_label, x_label="Local Time (CDT)", var_name=var_name, plot_type_name="line_plot", plot_time_range=plot_time_range, filename_in=filename, output_dir=output_dir)
class WRFVarsTest(ut.TestCase): longMessage = True class WRFInterpTest(ut.TestCase): longMessage = True class WRFLatLonTest(ut.TestCase): longMessage = True if __name__ == "__main__": from wrf import (omp_set_num_threads, omp_set_schedule, omp_get_schedule, omp_set_dynamic, omp_get_num_procs, OMP_SCHED_STATIC) omp_set_num_threads(omp_get_num_procs() // 2) omp_set_schedule(OMP_SCHED_STATIC, 0) omp_set_dynamic(False) ignore_vars = [] # Not testable yet wrf_vars = [ "avo", "eth", "cape_2d", "cape_3d", "ctt", "dbz", "mdbz", "geopt", "helicity", "lat", "lon", "omg", "p", "pressure", "pvo", "pw", "rh2", "rh", "slp", "ter", "td2", "td", "tc", "theta", "tk", "tv", "twb", "updraft_helicity", "ua", "va", "wa", "uvmet10", "uvmet", "z", "cfrac", "zstag", "geopt_stag", "height_agl", "wspd_wdir", "wspd_wdir10", "uvmet_wspd_wdir", "uvmet10_wspd_wdir" ] interp_methods = ["interplevel", "vertcross", "interpline", "vinterp"] latlon_tests = ["xy", "ll"]
class WRFVarsTest(ut.TestCase): longMessage = True class WRFInterpTest(ut.TestCase): longMessage = True class WRFLatLonTest(ut.TestCase): longMessage = True if __name__ == "__main__": from wrf import (omp_set_num_threads, omp_set_schedule, omp_get_schedule, omp_set_dynamic, omp_get_num_procs, OMP_SCHED_STATIC) omp_set_num_threads(omp_get_num_procs() - 1) omp_set_schedule(OMP_SCHED_STATIC, 0) omp_set_dynamic(False) ignore_vars = [] # Not testable yet wrf_vars = [ "avo", "eth", "cape_2d", "cape_3d", "ctt", "dbz", "mdbz", "geopt", "helicity", "lat", "lon", "omg", "p", "pressure", "pvo", "pw", "rh2", "rh", "slp", "ter", "td2", "td", "tc", "theta", "tk", "tv", "twb", "updraft_helicity", "ua", "va", "wa", "uvmet10", "uvmet", "z", "cfrac", "zstag", "geopt_stag" ] interp_methods = ["interplevel", "vertcross", "interpline", "vinterp"] latlon_tests = ["xy_out", "ll_out"] for nc_lib in ("netcdf4", "pynio", "scipy"):