def test(): plt.figure() b = Basemap() u = np.array([1, ]) v = np.array([1, ]) lon, lat = np.array([-90, ]), np.array([45, ]) xx, yy = b(lon, lat) print(xx.shape) b.quiver(xx, yy, u, v, color="r") urot, vrot = b.rotate_vector(u, v, lon, lat) b.quiver(xx, yy, urot, vrot, color="g") b.drawcoastlines() # Plot the same in rotpole projection HL_LABEL = "CRCM5_HL" NEMO_LABEL = "CRCM5_NEMO" sim_label_to_path = OrderedDict( [(HL_LABEL, "/RESCUE/skynet3_rech1/huziy/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl_oneway/Samples"), (NEMO_LABEL, "/HOME/huziy/skynet3_rech1/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl/Samples")] ) # get a coord file ... coord_file = "" found_coord_file = False for mdir in os.listdir(sim_label_to_path[HL_LABEL]): mdir_path = os.path.join(sim_label_to_path[HL_LABEL], mdir) if not os.path.isdir(mdir_path): continue for fn in os.listdir(mdir_path): print(fn) if fn[:2] not in ["pm", "dm", "pp", "dp"]: continue coord_file = os.path.join(mdir_path, fn) found_coord_file = True if found_coord_file: break bmp, lons, lats = nemo_hl_util.get_basemap_obj_and_coords_from_rpn_file(path=coord_file) plt.figure() urot, vrot = bmp.rotate_vector(u, v, lon, lat) xx, yy = bmp(lon, lat) bmp.quiver(xx, yy, urot, vrot, color="b") bmp.drawcoastlines() plt.show()
def main(): start_year = 1979 end_year = 1981 HL_LABEL = "CRCM5_HL" NEMO_LABEL = "CRCM5_NEMO" dx = 0.1 dy = 0.1 file_prefix = "pm" PR_level = -1 PR_level_type = level_kinds.ARBITRARY tprecip_vname = "PR" sprecip_vname = "SN" TT_level = 1 TT_level_type = level_kinds.HYBRID # The average profile will be calculated over the selection selection = IndexRectangle(ill=50, jll=60, ni=10, nj=10) sim_label_to_path = OrderedDict( [(HL_LABEL, "/RESCUE/skynet3_rech1/huziy/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl_oneway/Samples"), (NEMO_LABEL, "/HOME/huziy/skynet3_rech1/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl/Samples")] ) # get a coord file ... (use pm* files, since they contain NEM1 variable) # Should be NEMO_LABEL, since the hostetler case does not calculate NEM? vars coord_file = "" found_coord_file = False for mdir in os.listdir(sim_label_to_path[NEMO_LABEL]): mdir_path = os.path.join(sim_label_to_path[NEMO_LABEL], mdir) if not os.path.isdir(mdir_path): continue for fn in os.listdir(mdir_path): if fn[:2] not in ["pm", ]: continue if fn[-9:-1] == "0" * 8: continue coord_file = os.path.join(mdir_path, fn) found_coord_file = True if found_coord_file: break bmp, lons, lats = nemo_hl_util.get_basemap_obj_and_coords_from_rpn_file(path=coord_file) xx, yy = bmp(lons, lats) lake_mask = np.greater(commons.get_nemo_lake_mask_from_rpn(coord_file, vname="NEM1"), 0) fig = plt.figure() gs = GridSpec(1, 1) ax = fig.add_subplot(gs[0, 0]) bmp.drawcoastlines(linewidth=0.3, ax=ax) xll, yll = xx[selection.ill, selection.jll], yy[selection.ill, selection.jll] xur, yur = xx[selection.get_ur_corner()], yy[selection.get_ur_corner()] # ax.add_patch(Polygon(np.asarray([(xll, yll), (xll, yur), (xur, yur), (xur, yll)]))) # selection_mask = np.zeros_like(lake_mask, dtype=bool) # selection_mask[selection.get_2d_slice()] = True # bmp.pcolormesh(xx, yy, selection_mask, ax=ax) hl_profile = get_temperature_profile_HL(start_year=start_year, end_year=end_year, samples_dir=sim_label_to_path[HL_LABEL]) nemo_profile = get_temperature_profile_HL(start_year=start_year, end_year=end_year, samples_dir=sim_label_to_path[HL_LABEL]) plt.show()
def main(): start_year = 1979 end_year = 1981 HL_LABEL = "CRCM5_HL" NEMO_LABEL = "CRCM5_NEMO" dx = 0.1 dy = 0.1 file_prefix = "pm" PR_level = -1 PR_level_type = level_kinds.ARBITRARY tprecip_vname = "PR" sprecip_vname = "SN" TT_level = 1 TT_level_type = level_kinds.HYBRID sim_label_to_path = OrderedDict( [(HL_LABEL, "/RESCUE/skynet3_rech1/huziy/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl_oneway/Samples"), (NEMO_LABEL, "/HOME/huziy/skynet3_rech1/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl/Samples")] ) # get a coord file ... (use pm* files, since they contain NEM1 variable) # Should be NEMO_LABEL, since the hostetler case does not calculate NEM? vars coord_file = "" found_coord_file = False for mdir in os.listdir(sim_label_to_path[NEMO_LABEL]): mdir_path = os.path.join(sim_label_to_path[NEMO_LABEL], mdir) if not os.path.isdir(mdir_path): continue for fn in os.listdir(mdir_path): if fn[:2] not in ["pm", ]: continue if fn[-9:-1] == "0" * 8: continue coord_file = os.path.join(mdir_path, fn) found_coord_file = True if found_coord_file: break bmp, lons, lats = nemo_hl_util.get_basemap_obj_and_coords_from_rpn_file(path=coord_file) xx, yy = bmp(lons, lats) stride = 3 # rot_path = "/HOME/huziy/skynet3_rech1/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl/rotated_wind_CRCM5_NEMO.nc" fig = plt.figure() with Dataset(rot_path) as ds: ncvars = ds.variables uu_rot, vv_rot = ncvars["UU"][10, ...], ncvars["VV"][10, ...] plt.title("rotated in the file") bmp.quiver(xx[::stride, ::stride], yy[::stride, ::stride], uu_rot[::stride, ::stride], vv_rot[::stride, ::stride], scale=1000, color="r") lons[lons > 180] -= 360 not_rot_path = "/HOME/huziy/skynet3_rech1/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl/not_rotated_wind_CRCM5_NEMO.nc" with Dataset(not_rot_path) as ds: ncvars = ds.variables uu, vv = ncvars["UU"][10, ...], ncvars["VV"][10, ...] uu_rot1, vv_rot1 = rotate_vecs_from_geo_to_rotpole(uu, vv, lons, lats, bmp=bmp) plt.title("not rotated in the file") bmp.quiver(xx[::stride, ::stride], yy[::stride, ::stride], uu_rot1[::stride, ::stride], vv_rot1[::stride, ::stride], scale=1000) bmp.drawcoastlines() plt.figure() b = Basemap(lon_0=0) xx1, yy1 = b(lons, lats) uu_rot2, vv_rot2 = b.rotate_vector(uu, vv, lons, lats) b.quiver(xx1[::stride, ::stride], yy1[::stride, ::stride], uu_rot2[::stride, ::stride], vv_rot2[::stride, ::stride], scale=1000) b.drawcoastlines() plt.show()
def main(): start_year = 1979 end_year = 1981 HL_LABEL = "CRCM5_HL" NEMO_LABEL = "CRCM5_NEMO" file_prefix = "pm" PR_level = -1 PR_level_type = level_kinds.ARBITRARY tprecip_vname = "PR" sprecip_vname = "SN" TT_level = 1 TT_level_type = level_kinds.HYBRID sim_label_to_path = OrderedDict([ (HL_LABEL, "/RESCUE/skynet3_rech1/huziy/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl_oneway/Samples" ), (NEMO_LABEL, "/HOME/huziy/skynet3_rech1/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl/Samples" ) ]) # get a coord file ... coord_file = "" found_coord_file = False for mdir in os.listdir(sim_label_to_path[HL_LABEL]): mdir_path = os.path.join(sim_label_to_path[HL_LABEL], mdir) if not os.path.isdir(mdir_path): continue for fn in os.listdir(mdir_path): print(fn) if fn[:2] not in ["pm", "dm", "pp", "dp"]: continue coord_file = os.path.join(mdir_path, fn) found_coord_file = True if found_coord_file: break bmp, lons, lats = nemo_hl_util.get_basemap_obj_and_coords_from_rpn_file( path=coord_file) xx, yy = bmp(lons, lats) # read necessary input and calculate snowfall and save to the file for sim_label, samples_dir in sim_label_to_path.items(): samples_dir_path = Path(samples_dir) TT_monthdate_to_paths = get_monthyeardate_to_paths_map( file_prefix="dm", start_year=start_year, end_year=end_year, samples_dir_path=samples_dir_path) PR_monthdate_to_paths = get_monthyeardate_to_paths_map( file_prefix="pm", start_year=start_year, end_year=end_year, samples_dir_path=samples_dir_path) with Dataset( str(samples_dir_path.parent / "{}_snow_fall_{}-{}.nc".format( sim_label, start_year, end_year)), "w") as ds: assert isinstance(ds, Dataset) ds.createDimension("time", None) ds.createDimension("lon", lons.shape[0]) ds.createDimension("lat", lons.shape[1]) # create the schema of the output file snow_fall = ds.createVariable(sprecip_vname, "f4", dimensions=("time", "lon", "lat")) lons_var = ds.createVariable("lon", "f4", dimensions=("lon", "lat")) lats_var = ds.createVariable("lat", "f4", dimensions=("lon", "lat")) time_var = ds.createVariable("time", "i8", dimensions=("time", )) time_var.units = "hours since {:%Y-%m-%d %H:%M:%S}".format( datetime(start_year, 1, 1)) lons_var[:] = lons lats_var[:] = lats # use sorted dates record_count = 0 for month_date in sorted(TT_monthdate_to_paths): tt = MultiRPN(path=TT_monthdate_to_paths[month_date] ).get_all_time_records_for_name_and_level( varname="TT", level=TT_level, level_kind=TT_level_type) pr = MultiRPN(path=PR_monthdate_to_paths[month_date] ).get_all_time_records_for_name_and_level( varname="PR", level=PR_level, level_kind=PR_level_type) print("Processing {}".format(month_date)) for d in sorted(tt): t_field = tt[d] pr_field = pr[d] sn = get_snow_fall_m_per_s(precip_m_per_s=pr_field, tair_deg_c=t_field) snow_fall[record_count, :, :] = sn time_var[record_count] = date2num(d, time_var.units) record_count += 1
def main(): start_year = 1979 end_year = 1981 HL_LABEL = "CRCM5_HL" NEMO_LABEL = "CRCM5_NEMO" dx = 0.1 dy = 0.1 file_prefix = "pm" PR_level = -1 PR_level_type = level_kinds.ARBITRARY tprecip_vname = "PR" sprecip_vname = "SN" TT_level = 1 TT_level_type = level_kinds.HYBRID sim_label_to_path = OrderedDict( [(HL_LABEL, "/RESCUE/skynet3_rech1/huziy/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl_oneway/Samples"), (NEMO_LABEL, "/HOME/huziy/skynet3_rech1/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl/Samples")] ) # get a coord file ... (use pm* files, since they contain NEM1 variable) # Should be NEMO_LABEL, since the hostetler case does not calculate NEM? vars coord_file = "" found_coord_file = False for mdir in os.listdir(sim_label_to_path[NEMO_LABEL]): mdir_path = os.path.join(sim_label_to_path[NEMO_LABEL], mdir) if not os.path.isdir(mdir_path): continue for fn in os.listdir(mdir_path): if fn[:2] not in ["pm", ]: continue if fn[-9:-1] == "0" * 8: continue coord_file = os.path.join(mdir_path, fn) found_coord_file = True if found_coord_file: break bmp, lons, lats = nemo_hl_util.get_basemap_obj_and_coords_from_rpn_file(path=coord_file) xx, yy = bmp(lons, lats) r = RPN(coord_file) lats_rot = r.get_first_record_for_name("^^") lons_rot = r.get_first_record_for_name(">>") lake_mask = np.greater(commons.get_nemo_lake_mask_from_rpn(coord_file, vname="NEM1"), 0) # Get the 100km region around the lakes lake_effect_regions = get_mask_of_points_near_lakes(lake_mask, npoints_radius=10) local_amplification_limit = 4 * 1e-2 / (24.0 * 3600.0) # the radius is 500 km, i.e. 50 gridpoints ij_to_non_local_mask = get_map_ij_to_nonlocal_mask(lake_effect_regions, lake_mask, npoints_radius=50) # Snowfall amount criteria (>= 10 cm) lower_snow_fall_limit = 10 * 1e-2 / (24.0 * 3600.0) # convert to M/s # wind blows from lake: time limit wind_blows_from_lake_time_limit_hours = 6.0 months_of_interest = [10, 11, 12, 1, 2, 3, 4, 5] sim_label_to_duration_mean = {} sim_label_to_lake_effect_sprecip_mean = {} sim_label_to_year_to_lake_effect_snow_fall_duration = OrderedDict([(sim_label, OrderedDict()) for sim_label in sim_label_to_path]) for sim_label, samples_dir_path in sim_label_to_path.items(): # calculate the composites for the (Oct - March) period lake_effect_snowfall_mean_duration = None # the duration is in time steps lake_effect_mean_snowrate_m_per_s = None snowfall_current_event = None duration_current_event = None # the duration is in time steps n_events = None sn_previous = None time_wind_blows_from_lake = None samples_dir = Path(samples_dir_path) snowfall_file = samples_dir.parent / "{}_snow_fall_{}-{}.nc".format(sim_label, start_year, end_year) wind_components_file = samples_dir.parent / "rotated_wind_{}.nc".format(sim_label) ds_wind = Dataset(str(wind_components_file)) print("Working on {} ...".format(sim_label)) lkice_manager = RPNLakeIceManager(samples_dir=samples_dir) with Dataset(str(snowfall_file)) as ds: time_var = ds.variables["time"] nt = time_var.shape[0] snowfall_var_m_per_s = ds.variables["SN"] u_var = ds_wind.variables["UU"] v_var = ds_wind.variables["VV"] time_var_wind = ds_wind.variables["time"] assert time_var_wind.shape == time_var.shape assert time_var_wind[0] == time_var[0] assert time_var_wind[-1] == time_var_wind[-1] assert (u_var.shape == snowfall_var_m_per_s.shape) and (v_var.shape == snowfall_var_m_per_s.shape) times = num2date(time_var[:], time_var.units) dt_seconds = (times[1] - times[0]).total_seconds() year_to_lake_effect_snow_fall_duration = sim_label_to_year_to_lake_effect_snow_fall_duration[sim_label] for ti, t in enumerate(times): if t.month not in months_of_interest: continue if t.year > end_year or t.year < start_year: continue sn_current = snowfall_var_m_per_s[ti, :, :] if t.year not in year_to_lake_effect_snow_fall_duration: year_to_lake_effect_snow_fall_duration[t.year] = np.zeros_like(sn_current) # initialize aggragtion fields if lake_effect_snowfall_mean_duration is None: lake_effect_snowfall_mean_duration = np.zeros_like(sn_current) lake_effect_mean_snowrate_m_per_s = np.zeros_like(sn_current) n_events = np.zeros_like(sn_current) snowfall_current_event = np.zeros_like(sn_current) duration_current_event = np.zeros_like(sn_current) sn_previous = np.zeros_like(sn_current) time_wind_blows_from_lake = np.zeros_like(sn_current) where_lake_effect_snow = (sn_current > lower_snow_fall_limit) & lake_effect_regions & (~lake_mask) # add a condition on the local amplification i_arr, j_arr = np.where(where_lake_effect_snow) for i, j in zip(i_arr, j_arr): the_mask = ij_to_non_local_mask[(i, j)] where_lake_effect_snow[i, j] = sn_current[the_mask].mean() < sn_current[i, j] - local_amplification_limit # add a condition on the wind fetch from lakes and ice fraction. wind_blows_from_lake = get_wind_blows_from_lake_mask(lake_mask, lake_effect_regions, u_var[ti, :, :], v_var[ti, :, :], dx=dx, dy=dy, lake_ice_frac=lkice_manager.get_lake_fraction_for_date(the_date=t), lats_rot=lats_rot) time_wind_blows_from_lake[wind_blows_from_lake] += dt_seconds / 3600.0 where_lake_effect_snow = where_lake_effect_snow & (time_wind_blows_from_lake >= wind_blows_from_lake_time_limit_hours) time_wind_blows_from_lake[~wind_blows_from_lake] = 0 # update accumulators for current lake effect snowfall events snowfall_current_event[where_lake_effect_snow] += sn_current[where_lake_effect_snow] duration_current_event[where_lake_effect_snow] += 1.0 where_lake_effect_snow_finished = (~where_lake_effect_snow) & (sn_previous > lower_snow_fall_limit) # recalculate mean lake effect snowfall duration and rate lake_effect_snowfall_mean_duration[where_lake_effect_snow_finished] = (lake_effect_snowfall_mean_duration[where_lake_effect_snow_finished] * n_events[where_lake_effect_snow_finished] + duration_current_event[where_lake_effect_snow_finished]) / (n_events[where_lake_effect_snow_finished] + 1) lake_effect_mean_snowrate_m_per_s[where_lake_effect_snow_finished] = (lake_effect_mean_snowrate_m_per_s[where_lake_effect_snow_finished] * n_events[where_lake_effect_snow_finished] + snowfall_current_event[where_lake_effect_snow_finished]) / (n_events[where_lake_effect_snow_finished] + 1) year_to_lake_effect_snow_fall_duration[t.year][where_lake_effect_snow_finished] += duration_current_event[where_lake_effect_snow_finished] * dt_seconds # reset the current accumulators snowfall_current_event[where_lake_effect_snow_finished] = 0 duration_current_event[where_lake_effect_snow_finished] = 0 n_events[where_lake_effect_snow_finished] += 1 sn_previous = sn_current if ti % 1000 == 0: print("Done {} of {}".format(ti + 1, nt)) # normalization lake_effect_snowfall_mean_duration *= dt_seconds / (24 * 60 * 60.0) # convert to days lake_effect_mean_snowrate_m_per_s = np.ma.masked_where(~lake_effect_regions, lake_effect_mean_snowrate_m_per_s) lake_effect_snowfall_mean_duration = np.ma.masked_where(~lake_effect_regions, lake_effect_snowfall_mean_duration) for y, yearly_durations in sim_label_to_year_to_lake_effect_snow_fall_duration[sim_label].items(): sim_label_to_year_to_lake_effect_snow_fall_duration[sim_label][y] = np.ma.masked_where(~lake_effect_regions, yearly_durations) / (24 * 3600.0) sim_label_to_duration_mean[sim_label] = lake_effect_snowfall_mean_duration sim_label_to_lake_effect_sprecip_mean[sim_label] = lake_effect_mean_snowrate_m_per_s * 100 * 24 * 3600.0 # close the file with rotated wind components ds_wind.close() plot_utils.apply_plot_params(font_size=6, width_cm=18, height_cm=10) fig = plt.figure() gs = GridSpec(3, 3) duration_clevs = 20 # np.arange(0, 1.1, 0.1) snowrate_clevs = 20 # np.arange(0, 36, 4) duration_clevs_diff = 20 # np.arange(-1, 1.1, 0.1) snowrate_clevs_diff = 20 # np.arange(-10, 12, 2) vmax_duration = None vmax_snowrate = None vmax_days_per_year = None for row, sim_label in enumerate(sim_label_to_path): if vmax_duration is None: vmax_duration = sim_label_to_duration_mean[sim_label].max() vmax_snowrate = sim_label_to_lake_effect_sprecip_mean[sim_label].max() vmax_days_per_year = sim_label_to_year_to_lake_effect_snow_fall_duration[sim_label][1980].max() else: vmax_duration = max(vmax_duration, sim_label_to_duration_mean[sim_label].max()) vmax_snowrate = max(vmax_snowrate, sim_label_to_lake_effect_sprecip_mean[sim_label].max()) vmax_days_per_year = max(vmax_days_per_year, sim_label_to_year_to_lake_effect_snow_fall_duration[sim_label][1980].max()) for col, sim_label in enumerate(sim_label_to_path): # plot the duration of lake-effect snow events ax = fig.add_subplot(gs[0, col]) cs = bmp.pcolormesh(xx, yy, sim_label_to_duration_mean[sim_label], ax=ax, vmin=0, vmax=vmax_duration, cmap="rainbow_r") bmp.drawcoastlines(linewidth=0.3, ax=ax) plt.colorbar(cs, ax=ax) ax.set_title("Duration (days)") ax.set_xlabel("{}".format(sim_label)) # plot the mean intensity of the lake-effect snow events ax = fig.add_subplot(gs[1, col]) cs = bmp.pcolormesh(xx, yy, sim_label_to_lake_effect_sprecip_mean[sim_label], ax=ax, vmax=vmax_snowrate, vmin=lower_snow_fall_limit, cmap="rainbow_r") bmp.drawcoastlines(linewidth=0.3, ax=ax) plt.colorbar(cs, ax=ax) ax.set_title("Snowfall rate, (cm/day)") ax.set_xlabel("{}".format(sim_label)) # plot the mean duration of the lake effect snowfall events per year ax = fig.add_subplot(gs[2, col]) to_plot = sim_label_to_year_to_lake_effect_snow_fall_duration[sim_label][1980] clevs = [0, 0.1, ] + list(np.arange(0.4, 3.2, 0.4)) bn = BoundaryNorm(clevs, len(clevs)) cmap = cm.get_cmap("spectral_r", len(clevs)) cs = bmp.pcolormesh(xx, yy, to_plot, ax=ax, norm=bn, cmap=cmap) bmp.drawcoastlines(linewidth=0.3, ax=ax) plt.colorbar(cs, ax=ax, extend="max") ax.set_title("# Days per year") ax.set_xlabel("{}".format(sim_label)) # plot the difference # plot the duration of lake-effect snow events col = 2 cmap = cm.get_cmap("seismic", 40) vmin = -np.max(sim_label_to_duration_mean[NEMO_LABEL] - sim_label_to_duration_mean[HL_LABEL]) ax = fig.add_subplot(gs[0, col]) cs = bmp.pcolormesh(xx, yy, sim_label_to_duration_mean[NEMO_LABEL] - sim_label_to_duration_mean[HL_LABEL], vmin=vmin, ax=ax, cmap=cmap) plt.colorbar(cs, ax=ax) bmp.drawcoastlines(linewidth=0.3, ax=ax) ax.set_title("Duration (days)") ax.set_xlabel("{} - {}".format(NEMO_LABEL, HL_LABEL)) # plot the mean intensity of the lake-effect snow events ax = fig.add_subplot(gs[1, col]) vmin = -np.max(sim_label_to_lake_effect_sprecip_mean[NEMO_LABEL] - sim_label_to_lake_effect_sprecip_mean[HL_LABEL]) cs = bmp.pcolormesh(xx, yy, sim_label_to_lake_effect_sprecip_mean[NEMO_LABEL] - sim_label_to_lake_effect_sprecip_mean[HL_LABEL], ax=ax, vmin=vmin, cmap=cmap) # convert to cm/day bmp.drawcoastlines(linewidth=0.3, ax=ax) plt.colorbar(cs, ax=ax) ax.set_title("Snowfall rate, (cm/day)") ax.set_xlabel("{} - {}".format(NEMO_LABEL, HL_LABEL)) # plot the mean duration of the lake effect snowfall events per year ax = fig.add_subplot(gs[2, col]) to_plot = (sim_label_to_year_to_lake_effect_snow_fall_duration[NEMO_LABEL][1980] - sim_label_to_year_to_lake_effect_snow_fall_duration[HL_LABEL][1980]) cs = bmp.pcolormesh(xx, yy, to_plot, ax=ax, vmin=-to_plot.max(), cmap="seismic") bmp.drawcoastlines(linewidth=0.3, ax=ax) plt.colorbar(cs, ax=ax) ax.set_title("# Days per year") ax.set_xlabel("{} - {}".format(NEMO_LABEL, HL_LABEL)) fig.tight_layout() fig.savefig(os.path.join(img_folder, "lake_effect_snow_10cm_limit_and_loc_ampl_{}-{}.png".format(start_year, end_year)), dpi=commons.dpi, transparent=True, bbox_inches="tight")
def main(): start_year = 1979 end_year = 1979 HL_LABEL = "CRCM5_HL" NEMO_LABEL = "CRCM5_NEMO" # sim_label_to_path = OrderedDict( # [(HL_LABEL, "/RESCUE/skynet3_rech1/huziy/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl_oneway/Samples"), # (NEMO_LABEL, "/HOME/huziy/skynet3_rech1/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl/Samples")] # ) base_label = "Control" modif_label = "Control+perftweaks" sim_label_to_path = OrderedDict( [(base_label, "/RESCUE/skynet3_rech1/huziy/CNRCWP/C5/2016/test_ouptuts/control/Samples"), (modif_label, "/RESCUE/skynet3_rech1/huziy/CNRCWP/C5/2016/test_ouptuts/coupled-GL-perftest/Samples")] ) # var_name_list = ["TT", "PR", "LC", "HR", "HU", "AV", "I5", "AL", "TJ"] var_name_list = ["TT", "PR", "LC", "HU", "I5"] # season_to_months = commons.season_to_months season_to_months = OrderedDict([("January", [1, ]),]) vname_to_level = { "TT": 1, "PR": -1, "SN": -1, "LC": -1, "HR": 1, "HU": 1, "AV": -1, "I5": -1, "AL": -1, "TJ": -1 } vname_to_level_kind = { "TT": level_kinds.HYBRID, "PR": level_kinds.ARBITRARY, "SN": level_kinds.ARBITRARY, "LC": level_kinds.ARBITRARY, "HR": level_kinds.HYBRID, "HU": level_kinds.HYBRID, "AV": level_kinds.ARBITRARY, "I5": level_kinds.ARBITRARY, "AL": level_kinds.ARBITRARY, "TJ": level_kinds.ARBITRARY } vname_to_file_prefix = { "TT": "dm", "PR": "pm", "SN": "pm", "LC": "pm", "HR": "dm", "HU": "dm", "AV": "pm", "I5": "pm", "AL": "pm", "TJ": "pm" } vname_to_clevs = { "TT": np.arange(-5.1, 5.3, 0.2), "PR": np.arange(-5.1, 5.3, 0.2), "SN": np.arange(-5.1, 5.3, 0.2), "LC": [v for v in np.arange(-0.52, 0.54, 0.08)], "HR": [v for v in np.arange(-0.52, 0.54, 0.03)], "HU": np.arange(-0.5, 0.54, 0.04), "AV": np.arange(-150, 170, 20), "I5": np.arange(-30, 34, 4), "AL": [v for v in np.arange(-0.52, 0.54, 0.08)], "TJ": np.arange(-5.1, 5.3, 0.2) } vname_to_label = { "TT": "Air temperature, 2m", "PR": "Total precipitation" } vname_to_coeff = { "PR": 24 * 3600 * 1000, "HU": 1000 } vname_to_units = { "TT": r"$^\circ$C", "PR": "mm/day", "HU": "g/kg", "AV": r"W/m$^2$", "I5": "mm" } # get a coord file ... coord_file = "" found_coord_file = False samples_with_coords_path = Path(next(item for item in sim_label_to_path.items())[1]) for mdir in samples_with_coords_path.iterdir(): if not mdir.is_dir(): continue for fn in mdir.iterdir(): if fn.name[:2] not in ["pm", "dm", "pp", "dp"]: continue coord_file = str(fn) found_coord_file = True if found_coord_file: break bmp, lons, lats = nemo_hl_util.get_basemap_obj_and_coords_from_rpn_file(path=coord_file) xx, yy = bmp(lons, lats) # Do the calculations hl_data = OrderedDict() nemo_data = OrderedDict() for vname in var_name_list: field_props = dict(season_to_months=season_to_months, start_year=start_year, end_year=end_year, filename_prefix=vname_to_file_prefix[vname], varname=vname, level=vname_to_level[vname], level_kind=vname_to_level_kind[vname]) hl_data[vname] = get_seasonal_means_from_rpn_monthly_folders(samples_dir=sim_label_to_path[base_label], **field_props) nemo_data[vname] = get_seasonal_means_from_rpn_monthly_folders(samples_dir=sim_label_to_path[modif_label], **field_props) # Plotting plot_utils.apply_plot_params(font_size=6, width_cm=20, height_cm=20) fig = plt.figure() fig.suptitle("{} minus {}".format(modif_label, base_label)) nrows = len(var_name_list) gs = GridSpec(nrows, len(season_to_months) + 1, width_ratios=[1., ] * len(season_to_months) + [0.05, ]) for row, vname in enumerate(hl_data): hl_seas = hl_data[vname] nemo_seas = nemo_data[vname] cs = None for col, season in enumerate(season_to_months): ax = fig.add_subplot(gs[row, col]) norm = None if vname_to_clevs[vname] is not None: norm = BoundaryNorm(vname_to_clevs[vname], len(vname_to_clevs[vname]) - 1) cmap = cm.get_cmap("seismic", len(vname_to_clevs[vname]) - 1) else: cmap = cm.get_cmap("seismic", 11) to_plot = (nemo_seas[season] - hl_seas[season]) * vname_to_coeff.get(vname, 1) cs = bmp.contourf(xx, yy, to_plot, levels=vname_to_clevs[vname], ax=ax, extend="both", cmap=cmap, norm=norm) bmp.drawcoastlines(linewidth=0.3) if col == 0: ax.set_ylabel(vname_to_label.get(vname, vname)) ax.set_title("{}: min={:.3f}; max={:.3f}".format(season, np.min(to_plot), np.max(to_plot))) cax = fig.add_subplot(gs[row, -1]) plt.colorbar(cs, cax=cax) cax.set_title(vname_to_units.get(vname, "-")) if not os.path.isdir(img_folder): os.mkdir(img_folder) img_file = os.path.join(img_folder, "seas_2d_diff_{}vs{}_{}-{}.png".format(modif_label, base_label, start_year, end_year)) fig.savefig(img_file, dpi=commons.dpi, transparent=False, bbox_inches="tight")
def main(): start_year = 1979 end_year = 1981 HL_LABEL = "CRCM5_HL" NEMO_LABEL = "CRCM5_NEMO" file_prefix = "pm" PR_level = -1 PR_level_type = level_kinds.ARBITRARY tprecip_vname = "PR" sprecip_vname = "SN" TT_level = 1 TT_level_type = level_kinds.HYBRID sim_label_to_path = OrderedDict( [(HL_LABEL, "/RESCUE/skynet3_rech1/huziy/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl_oneway/Samples"), (NEMO_LABEL, "/HOME/huziy/skynet3_rech1/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl/Samples")] ) # get a coord file ... coord_file = "" found_coord_file = False for mdir in os.listdir(sim_label_to_path[HL_LABEL]): mdir_path = os.path.join(sim_label_to_path[HL_LABEL], mdir) if not os.path.isdir(mdir_path): continue for fn in os.listdir(mdir_path): print(fn) if fn[:2] not in ["pm", "dm", "pp", "dp"]: continue coord_file = os.path.join(mdir_path, fn) found_coord_file = True if found_coord_file: break bmp, lons, lats = nemo_hl_util.get_basemap_obj_and_coords_from_rpn_file(path=coord_file) xx, yy = bmp(lons, lats) # read necessary input and calculate snowfall and save to the file for sim_label, samples_dir in sim_label_to_path.items(): samples_dir_path = Path(samples_dir) TT_monthdate_to_paths = get_monthyeardate_to_paths_map(file_prefix="dm", start_year=start_year, end_year=end_year, samples_dir_path=samples_dir_path) PR_monthdate_to_paths = get_monthyeardate_to_paths_map(file_prefix="pm", start_year=start_year, end_year=end_year, samples_dir_path=samples_dir_path) with Dataset(str(samples_dir_path.parent / "{}_snow_fall_{}-{}.nc".format(sim_label, start_year, end_year)), "w") as ds: assert isinstance(ds, Dataset) ds.createDimension("time", None) ds.createDimension("lon", lons.shape[0]) ds.createDimension("lat", lons.shape[1]) # create the schema of the output file snow_fall = ds.createVariable(sprecip_vname, "f4", dimensions=("time", "lon", "lat")) lons_var = ds.createVariable("lon", "f4", dimensions=("lon", "lat")) lats_var = ds.createVariable("lat", "f4", dimensions=("lon", "lat")) time_var = ds.createVariable("time", "i8", dimensions=("time", )) time_var.units = "hours since {:%Y-%m-%d %H:%M:%S}".format(datetime(start_year, 1, 1)) lons_var[:] = lons lats_var[:] = lats # use sorted dates record_count = 0 for month_date in sorted(TT_monthdate_to_paths): tt = MultiRPN(path=TT_monthdate_to_paths[month_date]).get_all_time_records_for_name_and_level(varname="TT", level=TT_level, level_kind=TT_level_type) pr = MultiRPN(path=PR_monthdate_to_paths[month_date]).get_all_time_records_for_name_and_level(varname="PR", level=PR_level, level_kind=PR_level_type) print("Processing {}".format(month_date)) for d in sorted(tt): t_field = tt[d] pr_field = pr[d] sn = get_snow_fall_m_per_s(precip_m_per_s=pr_field, tair_deg_c=t_field) snow_fall[record_count, :, :] = sn time_var[record_count] = date2num(d, time_var.units) record_count += 1
def main(): start_year = 1979 end_year = 1981 HL_LABEL = "CRCM5_HL" NEMO_LABEL = "CRCM5_NEMO" file_prefix = "dm" level = 1 level_type = level_kinds.HYBRID wind_comp_names = ["UU", "VV"] sim_label_to_path = OrderedDict([ (HL_LABEL, "/RESCUE/skynet3_rech1/huziy/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl_oneway/Samples" ), (NEMO_LABEL, "/HOME/huziy/skynet3_rech1/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl/Samples" ) ]) # get a coord file ... coord_file = "" found_coord_file = False for mdir in os.listdir(sim_label_to_path[HL_LABEL]): mdir_path = os.path.join(sim_label_to_path[HL_LABEL], mdir) if not os.path.isdir(mdir_path): continue for fn in os.listdir(mdir_path): print(fn) if fn[:2] not in ["pm", "dm", "pp", "dp"]: continue coord_file = os.path.join(mdir_path, fn) found_coord_file = True if found_coord_file: break bmp, lons, lats = nemo_hl_util.get_basemap_obj_and_coords_from_rpn_file( path=coord_file) xx, yy = bmp(lons, lats) lons[lons > 180] -= 360 # loop through all files rotate vaectors and save to netcdf for sim_label, samples_dir in sim_label_to_path.items(): samples = Path(samples_dir) po = samples.parent monthdate_to_path_list = nemo_hl_util.get_monthyeardate_to_paths_map( file_prefix=file_prefix, start_year=start_year, end_year=end_year, samples_dir_path=samples) # Netcdf output file to put rotated winds po /= "rotated_wind_{}.nc".format(sim_label) with Dataset(str(po), "w") as ds: ds.createDimension("time", None) ds.createDimension("lon", lons.shape[0]) ds.createDimension("lat", lons.shape[1]) # create the schema of the output file vname_to_ncvar = {} for vname in wind_comp_names: vname_to_ncvar[vname] = ds.createVariable(vname, "f4", dimensions=("time", "lon", "lat")) vname_to_ncvar[vname].units = "knots" lons_var = ds.createVariable("lon", "f4", dimensions=("lon", "lat")) lats_var = ds.createVariable("lat", "f4", dimensions=("lon", "lat")) time_var = ds.createVariable("time", "i8", dimensions=("time", )) time_var.units = "hours since {:%Y-%m-%d %H:%M:%S}".format( datetime(start_year, 1, 1)) lons_var[:] = lons lats_var[:] = lats # use sorted dates record_count = 0 for month_date in sorted(monthdate_to_path_list): # select only dm files mr = MultiRPN(path=monthdate_to_path_list[month_date]) vname_to_fields = {} for vname in wind_comp_names: vname_to_fields[ vname] = mr.get_all_time_records_for_name_and_level( varname=vname, level=level, level_kind=level_type) for ti, t in enumerate( sorted(vname_to_fields[wind_comp_names[0]])): time_var[record_count] = date2num(t, time_var.units) uu = vname_to_fields[wind_comp_names[0]][t] vv = vname_to_fields[wind_comp_names[1]][t] uu_rot, vv_rot = rotate_vecs_from_geo_to_rotpole(uu, vv, lons, lats, bmp=bmp) # in knots not in m/s vname_to_ncvar[wind_comp_names[0]][ record_count, :, :] = uu_rot vname_to_ncvar[wind_comp_names[1]][ record_count, :, :] = vv_rot record_count += 1
def test(): plt.figure() b = Basemap() u = np.array([ 1, ]) v = np.array([ 1, ]) lon, lat = np.array([ -90, ]), np.array([ 45, ]) xx, yy = b(lon, lat) print(xx.shape) b.quiver(xx, yy, u, v, color="r") urot, vrot = b.rotate_vector(u, v, lon, lat) b.quiver(xx, yy, urot, vrot, color="g") b.drawcoastlines() # Plot the same in rotpole projection HL_LABEL = "CRCM5_HL" NEMO_LABEL = "CRCM5_NEMO" sim_label_to_path = OrderedDict([ (HL_LABEL, "/RESCUE/skynet3_rech1/huziy/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl_oneway/Samples" ), (NEMO_LABEL, "/HOME/huziy/skynet3_rech1/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl/Samples" ) ]) # get a coord file ... coord_file = "" found_coord_file = False for mdir in os.listdir(sim_label_to_path[HL_LABEL]): mdir_path = os.path.join(sim_label_to_path[HL_LABEL], mdir) if not os.path.isdir(mdir_path): continue for fn in os.listdir(mdir_path): print(fn) if fn[:2] not in ["pm", "dm", "pp", "dp"]: continue coord_file = os.path.join(mdir_path, fn) found_coord_file = True if found_coord_file: break bmp, lons, lats = nemo_hl_util.get_basemap_obj_and_coords_from_rpn_file( path=coord_file) plt.figure() urot, vrot = bmp.rotate_vector(u, v, lon, lat) xx, yy = bmp(lon, lat) bmp.quiver(xx, yy, urot, vrot, color="b") bmp.drawcoastlines() plt.show()
def main(): start_year = 1979 end_year = 1981 HL_LABEL = "CRCM5_HL" NEMO_LABEL = "CRCM5_NEMO" dx = 0.1 dy = 0.1 file_prefix = "pm" PR_level = -1 PR_level_type = level_kinds.ARBITRARY tprecip_vname = "PR" sprecip_vname = "SN" TT_level = 1 TT_level_type = level_kinds.HYBRID sim_label_to_path = OrderedDict([ (HL_LABEL, "/RESCUE/skynet3_rech1/huziy/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl_oneway/Samples" ), (NEMO_LABEL, "/HOME/huziy/skynet3_rech1/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl/Samples" ) ]) # get a coord file ... (use pm* files, since they contain NEM1 variable) # Should be NEMO_LABEL, since the hostetler case does not calculate NEM? vars coord_file = "" found_coord_file = False for mdir in os.listdir(sim_label_to_path[NEMO_LABEL]): mdir_path = os.path.join(sim_label_to_path[NEMO_LABEL], mdir) if not os.path.isdir(mdir_path): continue for fn in os.listdir(mdir_path): if fn[:2] not in [ "pm", ]: continue if fn[-9:-1] == "0" * 8: continue coord_file = os.path.join(mdir_path, fn) found_coord_file = True if found_coord_file: break bmp, lons, lats = nemo_hl_util.get_basemap_obj_and_coords_from_rpn_file( path=coord_file) xx, yy = bmp(lons, lats) r = RPN(coord_file) lats_rot = r.get_first_record_for_name("^^") lons_rot = r.get_first_record_for_name(">>") lake_mask = np.greater( commons.get_nemo_lake_mask_from_rpn(coord_file, vname="NEM1"), 0) # Get the 100km region around the lakes lake_effect_regions = get_mask_of_points_near_lakes(lake_mask, npoints_radius=10) local_amplification_limit = 4 * 1e-2 / (24.0 * 3600.0) # the radius is 500 km, i.e. 50 gridpoints ij_to_non_local_mask = get_map_ij_to_nonlocal_mask(lake_effect_regions, lake_mask, npoints_radius=50) # Snowfall amount criteria (>= 10 cm) lower_snow_fall_limit = 10 * 1e-2 / (24.0 * 3600.0) # convert to M/s # wind blows from lake: time limit wind_blows_from_lake_time_limit_hours = 6.0 months_of_interest = [10, 11, 12, 1, 2, 3, 4, 5] sim_label_to_duration_mean = {} sim_label_to_lake_effect_sprecip_mean = {} sim_label_to_year_to_lake_effect_snow_fall_duration = OrderedDict([ (sim_label, OrderedDict()) for sim_label in sim_label_to_path ]) for sim_label, samples_dir_path in sim_label_to_path.items(): # calculate the composites for the (Oct - March) period lake_effect_snowfall_mean_duration = None # the duration is in time steps lake_effect_mean_snowrate_m_per_s = None snowfall_current_event = None duration_current_event = None # the duration is in time steps n_events = None sn_previous = None time_wind_blows_from_lake = None samples_dir = Path(samples_dir_path) snowfall_file = samples_dir.parent / "{}_snow_fall_{}-{}.nc".format( sim_label, start_year, end_year) wind_components_file = samples_dir.parent / "rotated_wind_{}.nc".format( sim_label) ds_wind = Dataset(str(wind_components_file)) print("Working on {} ...".format(sim_label)) lkice_manager = RPNLakeIceManager(samples_dir=samples_dir) with Dataset(str(snowfall_file)) as ds: time_var = ds.variables["time"] nt = time_var.shape[0] snowfall_var_m_per_s = ds.variables["SN"] u_var = ds_wind.variables["UU"] v_var = ds_wind.variables["VV"] time_var_wind = ds_wind.variables["time"] assert time_var_wind.shape == time_var.shape assert time_var_wind[0] == time_var[0] assert time_var_wind[-1] == time_var_wind[-1] assert (u_var.shape == snowfall_var_m_per_s.shape) and ( v_var.shape == snowfall_var_m_per_s.shape) times = num2date(time_var[:], time_var.units) dt_seconds = (times[1] - times[0]).total_seconds() year_to_lake_effect_snow_fall_duration = sim_label_to_year_to_lake_effect_snow_fall_duration[ sim_label] for ti, t in enumerate(times): if t.month not in months_of_interest: continue if t.year > end_year or t.year < start_year: continue sn_current = snowfall_var_m_per_s[ti, :, :] if t.year not in year_to_lake_effect_snow_fall_duration: year_to_lake_effect_snow_fall_duration[ t.year] = np.zeros_like(sn_current) # initialize aggragtion fields if lake_effect_snowfall_mean_duration is None: lake_effect_snowfall_mean_duration = np.zeros_like( sn_current) lake_effect_mean_snowrate_m_per_s = np.zeros_like( sn_current) n_events = np.zeros_like(sn_current) snowfall_current_event = np.zeros_like(sn_current) duration_current_event = np.zeros_like(sn_current) sn_previous = np.zeros_like(sn_current) time_wind_blows_from_lake = np.zeros_like(sn_current) where_lake_effect_snow = ( sn_current > lower_snow_fall_limit) & lake_effect_regions & (~lake_mask) # add a condition on the local amplification i_arr, j_arr = np.where(where_lake_effect_snow) for i, j in zip(i_arr, j_arr): the_mask = ij_to_non_local_mask[(i, j)] where_lake_effect_snow[i, j] = sn_current[the_mask].mean( ) < sn_current[i, j] - local_amplification_limit # add a condition on the wind fetch from lakes and ice fraction. wind_blows_from_lake = get_wind_blows_from_lake_mask( lake_mask, lake_effect_regions, u_var[ti, :, :], v_var[ti, :, :], dx=dx, dy=dy, lake_ice_frac=lkice_manager.get_lake_fraction_for_date( the_date=t), lats_rot=lats_rot) time_wind_blows_from_lake[ wind_blows_from_lake] += dt_seconds / 3600.0 where_lake_effect_snow = where_lake_effect_snow & ( time_wind_blows_from_lake >= wind_blows_from_lake_time_limit_hours) time_wind_blows_from_lake[~wind_blows_from_lake] = 0 # update accumulators for current lake effect snowfall events snowfall_current_event[where_lake_effect_snow] += sn_current[ where_lake_effect_snow] duration_current_event[where_lake_effect_snow] += 1.0 where_lake_effect_snow_finished = (~where_lake_effect_snow) & ( sn_previous > lower_snow_fall_limit) # recalculate mean lake effect snowfall duration and rate lake_effect_snowfall_mean_duration[ where_lake_effect_snow_finished] = ( lake_effect_snowfall_mean_duration[ where_lake_effect_snow_finished] * n_events[where_lake_effect_snow_finished] + duration_current_event[where_lake_effect_snow_finished] ) / (n_events[where_lake_effect_snow_finished] + 1) lake_effect_mean_snowrate_m_per_s[ where_lake_effect_snow_finished] = ( lake_effect_mean_snowrate_m_per_s[ where_lake_effect_snow_finished] * n_events[where_lake_effect_snow_finished] + snowfall_current_event[where_lake_effect_snow_finished] ) / (n_events[where_lake_effect_snow_finished] + 1) year_to_lake_effect_snow_fall_duration[t.year][ where_lake_effect_snow_finished] += duration_current_event[ where_lake_effect_snow_finished] * dt_seconds # reset the current accumulators snowfall_current_event[where_lake_effect_snow_finished] = 0 duration_current_event[where_lake_effect_snow_finished] = 0 n_events[where_lake_effect_snow_finished] += 1 sn_previous = sn_current if ti % 1000 == 0: print("Done {} of {}".format(ti + 1, nt)) # normalization lake_effect_snowfall_mean_duration *= dt_seconds / ( 24 * 60 * 60.0) # convert to days lake_effect_mean_snowrate_m_per_s = np.ma.masked_where( ~lake_effect_regions, lake_effect_mean_snowrate_m_per_s) lake_effect_snowfall_mean_duration = np.ma.masked_where( ~lake_effect_regions, lake_effect_snowfall_mean_duration) for y, yearly_durations in sim_label_to_year_to_lake_effect_snow_fall_duration[ sim_label].items(): sim_label_to_year_to_lake_effect_snow_fall_duration[sim_label][ y] = np.ma.masked_where(~lake_effect_regions, yearly_durations) / (24 * 3600.0) sim_label_to_duration_mean[ sim_label] = lake_effect_snowfall_mean_duration sim_label_to_lake_effect_sprecip_mean[ sim_label] = lake_effect_mean_snowrate_m_per_s * 100 * 24 * 3600.0 # close the file with rotated wind components ds_wind.close() plot_utils.apply_plot_params(font_size=6, width_cm=18, height_cm=10) fig = plt.figure() gs = GridSpec(3, 3) duration_clevs = 20 # np.arange(0, 1.1, 0.1) snowrate_clevs = 20 # np.arange(0, 36, 4) duration_clevs_diff = 20 # np.arange(-1, 1.1, 0.1) snowrate_clevs_diff = 20 # np.arange(-10, 12, 2) vmax_duration = None vmax_snowrate = None vmax_days_per_year = None for row, sim_label in enumerate(sim_label_to_path): if vmax_duration is None: vmax_duration = sim_label_to_duration_mean[sim_label].max() vmax_snowrate = sim_label_to_lake_effect_sprecip_mean[ sim_label].max() vmax_days_per_year = sim_label_to_year_to_lake_effect_snow_fall_duration[ sim_label][1980].max() else: vmax_duration = max(vmax_duration, sim_label_to_duration_mean[sim_label].max()) vmax_snowrate = max( vmax_snowrate, sim_label_to_lake_effect_sprecip_mean[sim_label].max()) vmax_days_per_year = max( vmax_days_per_year, sim_label_to_year_to_lake_effect_snow_fall_duration[sim_label] [1980].max()) for col, sim_label in enumerate(sim_label_to_path): # plot the duration of lake-effect snow events ax = fig.add_subplot(gs[0, col]) cs = bmp.pcolormesh(xx, yy, sim_label_to_duration_mean[sim_label], ax=ax, vmin=0, vmax=vmax_duration, cmap="rainbow_r") bmp.drawcoastlines(linewidth=0.3, ax=ax) plt.colorbar(cs, ax=ax) ax.set_title("Duration (days)") ax.set_xlabel("{}".format(sim_label)) # plot the mean intensity of the lake-effect snow events ax = fig.add_subplot(gs[1, col]) cs = bmp.pcolormesh(xx, yy, sim_label_to_lake_effect_sprecip_mean[sim_label], ax=ax, vmax=vmax_snowrate, vmin=lower_snow_fall_limit, cmap="rainbow_r") bmp.drawcoastlines(linewidth=0.3, ax=ax) plt.colorbar(cs, ax=ax) ax.set_title("Snowfall rate, (cm/day)") ax.set_xlabel("{}".format(sim_label)) # plot the mean duration of the lake effect snowfall events per year ax = fig.add_subplot(gs[2, col]) to_plot = sim_label_to_year_to_lake_effect_snow_fall_duration[ sim_label][1980] clevs = [ 0, 0.1, ] + list(np.arange(0.4, 3.2, 0.4)) bn = BoundaryNorm(clevs, len(clevs)) cmap = cm.get_cmap("spectral_r", len(clevs)) cs = bmp.pcolormesh(xx, yy, to_plot, ax=ax, norm=bn, cmap=cmap) bmp.drawcoastlines(linewidth=0.3, ax=ax) plt.colorbar(cs, ax=ax, extend="max") ax.set_title("# Days per year") ax.set_xlabel("{}".format(sim_label)) # plot the difference # plot the duration of lake-effect snow events col = 2 cmap = cm.get_cmap("seismic", 40) vmin = -np.max(sim_label_to_duration_mean[NEMO_LABEL] - sim_label_to_duration_mean[HL_LABEL]) ax = fig.add_subplot(gs[0, col]) cs = bmp.pcolormesh(xx, yy, sim_label_to_duration_mean[NEMO_LABEL] - sim_label_to_duration_mean[HL_LABEL], vmin=vmin, ax=ax, cmap=cmap) plt.colorbar(cs, ax=ax) bmp.drawcoastlines(linewidth=0.3, ax=ax) ax.set_title("Duration (days)") ax.set_xlabel("{} - {}".format(NEMO_LABEL, HL_LABEL)) # plot the mean intensity of the lake-effect snow events ax = fig.add_subplot(gs[1, col]) vmin = -np.max(sim_label_to_lake_effect_sprecip_mean[NEMO_LABEL] - sim_label_to_lake_effect_sprecip_mean[HL_LABEL]) cs = bmp.pcolormesh(xx, yy, sim_label_to_lake_effect_sprecip_mean[NEMO_LABEL] - sim_label_to_lake_effect_sprecip_mean[HL_LABEL], ax=ax, vmin=vmin, cmap=cmap) # convert to cm/day bmp.drawcoastlines(linewidth=0.3, ax=ax) plt.colorbar(cs, ax=ax) ax.set_title("Snowfall rate, (cm/day)") ax.set_xlabel("{} - {}".format(NEMO_LABEL, HL_LABEL)) # plot the mean duration of the lake effect snowfall events per year ax = fig.add_subplot(gs[2, col]) to_plot = ( sim_label_to_year_to_lake_effect_snow_fall_duration[NEMO_LABEL][1980] - sim_label_to_year_to_lake_effect_snow_fall_duration[HL_LABEL][1980]) cs = bmp.pcolormesh(xx, yy, to_plot, ax=ax, vmin=-to_plot.max(), cmap="seismic") bmp.drawcoastlines(linewidth=0.3, ax=ax) plt.colorbar(cs, ax=ax) ax.set_title("# Days per year") ax.set_xlabel("{} - {}".format(NEMO_LABEL, HL_LABEL)) fig.tight_layout() fig.savefig(os.path.join( img_folder, "lake_effect_snow_10cm_limit_and_loc_ampl_{}-{}.png".format( start_year, end_year)), dpi=commons.dpi, transparent=True, bbox_inches="tight")
def main(): start_year = 1979 end_year = 1981 HL_LABEL = "CRCM5_HL" NEMO_LABEL = "CRCM5_NEMO" file_prefix = "dm" level = 1 level_type = level_kinds.HYBRID wind_comp_names = ["UU", "VV"] sim_label_to_path = OrderedDict( [(HL_LABEL, "/RESCUE/skynet3_rech1/huziy/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl_oneway/Samples"), (NEMO_LABEL, "/HOME/huziy/skynet3_rech1/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl/Samples")] ) # get a coord file ... coord_file = "" found_coord_file = False for mdir in os.listdir(sim_label_to_path[HL_LABEL]): mdir_path = os.path.join(sim_label_to_path[HL_LABEL], mdir) if not os.path.isdir(mdir_path): continue for fn in os.listdir(mdir_path): print(fn) if fn[:2] not in ["pm", "dm", "pp", "dp"]: continue coord_file = os.path.join(mdir_path, fn) found_coord_file = True if found_coord_file: break bmp, lons, lats = nemo_hl_util.get_basemap_obj_and_coords_from_rpn_file(path=coord_file) xx, yy = bmp(lons, lats) lons[lons > 180] -= 360 # loop through all files rotate vaectors and save to netcdf for sim_label, samples_dir in sim_label_to_path.items(): samples = Path(samples_dir) po = samples.parent monthdate_to_path_list = nemo_hl_util.get_monthyeardate_to_paths_map(file_prefix=file_prefix, start_year=start_year, end_year=end_year, samples_dir_path=samples) # Netcdf output file to put rotated winds po /= "rotated_wind_{}.nc".format(sim_label) with Dataset(str(po), "w") as ds: ds.createDimension("time", None) ds.createDimension("lon", lons.shape[0]) ds.createDimension("lat", lons.shape[1]) # create the schema of the output file vname_to_ncvar = {} for vname in wind_comp_names: vname_to_ncvar[vname] = ds.createVariable(vname, "f4", dimensions=("time", "lon", "lat")) vname_to_ncvar[vname].units = "knots" lons_var = ds.createVariable("lon", "f4", dimensions=("lon", "lat")) lats_var = ds.createVariable("lat", "f4", dimensions=("lon", "lat")) time_var = ds.createVariable("time", "i8", dimensions=("time",)) time_var.units = "hours since {:%Y-%m-%d %H:%M:%S}".format(datetime(start_year, 1, 1)) lons_var[:] = lons lats_var[:] = lats # use sorted dates record_count = 0 for month_date in sorted(monthdate_to_path_list): # select only dm files mr = MultiRPN(path=monthdate_to_path_list[month_date]) vname_to_fields = {} for vname in wind_comp_names: vname_to_fields[vname] = mr.get_all_time_records_for_name_and_level(varname=vname, level=level, level_kind=level_type) for ti, t in enumerate(sorted(vname_to_fields[wind_comp_names[0]])): time_var[record_count] = date2num(t, time_var.units) uu = vname_to_fields[wind_comp_names[0]][t] vv = vname_to_fields[wind_comp_names[1]][t] uu_rot, vv_rot = rotate_vecs_from_geo_to_rotpole(uu, vv, lons, lats, bmp=bmp) # in knots not in m/s vname_to_ncvar[wind_comp_names[0]][record_count, :, :] = uu_rot vname_to_ncvar[wind_comp_names[1]][record_count, :, :] = vv_rot record_count += 1
def main(): start_year = 1979 end_year = 1981 HL_LABEL = "CRCM5_HL" NEMO_LABEL = "CRCM5_NEMO" dx = 0.1 dy = 0.1 file_prefix = "pm" PR_level = -1 PR_level_type = level_kinds.ARBITRARY tprecip_vname = "PR" sprecip_vname = "SN" TT_level = 1 TT_level_type = level_kinds.HYBRID # The average profile will be calculated over the selection selection = IndexRectangle(ill=50, jll=60, ni=10, nj=10) sim_label_to_path = OrderedDict([ (HL_LABEL, "/RESCUE/skynet3_rech1/huziy/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl_oneway/Samples" ), (NEMO_LABEL, "/HOME/huziy/skynet3_rech1/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl/Samples" ) ]) # get a coord file ... (use pm* files, since they contain NEM1 variable) # Should be NEMO_LABEL, since the hostetler case does not calculate NEM? vars coord_file = "" found_coord_file = False for mdir in os.listdir(sim_label_to_path[NEMO_LABEL]): mdir_path = os.path.join(sim_label_to_path[NEMO_LABEL], mdir) if not os.path.isdir(mdir_path): continue for fn in os.listdir(mdir_path): if fn[:2] not in [ "pm", ]: continue if fn[-9:-1] == "0" * 8: continue coord_file = os.path.join(mdir_path, fn) found_coord_file = True if found_coord_file: break bmp, lons, lats = nemo_hl_util.get_basemap_obj_and_coords_from_rpn_file( path=coord_file) xx, yy = bmp(lons, lats) lake_mask = np.greater( commons.get_nemo_lake_mask_from_rpn(coord_file, vname="NEM1"), 0) fig = plt.figure() gs = GridSpec(1, 1) ax = fig.add_subplot(gs[0, 0]) bmp.drawcoastlines(linewidth=0.3, ax=ax) xll, yll = xx[selection.ill, selection.jll], yy[selection.ill, selection.jll] xur, yur = xx[selection.get_ur_corner()], yy[selection.get_ur_corner()] # ax.add_patch(Polygon(np.asarray([(xll, yll), (xll, yur), (xur, yur), (xur, yll)]))) # selection_mask = np.zeros_like(lake_mask, dtype=bool) # selection_mask[selection.get_2d_slice()] = True # bmp.pcolormesh(xx, yy, selection_mask, ax=ax) hl_profile = get_temperature_profile_HL( start_year=start_year, end_year=end_year, samples_dir=sim_label_to_path[HL_LABEL]) nemo_profile = get_temperature_profile_HL( start_year=start_year, end_year=end_year, samples_dir=sim_label_to_path[HL_LABEL]) plt.show()