From cfae2b7a887958b31e5e61fb670e95475d84e329 Mon Sep 17 00:00:00 2001 From: John Gerrard Holland Date: Thu, 14 Mar 2024 12:47:27 -0400 Subject: [PATCH 1/5] chore: reformat all files using black Co-Authored-By: Carlos Paniagua <68481491+cpaniaguam@users.noreply.github.com> Co-Authored-By: Camilo Diaz <16551165+kmilo9999@users.noreply.github.com> --- src/icesat2waves/__init__.py | 2 +- .../analysis_db/A02c_IOWAGA_thredds_prior.py | 28 +- .../analysis_db/B02_make_spectra_gFT.py | 9 +- .../analysis_db/B03_plot_spectra_ov.py | 40 +- src/icesat2waves/analysis_db/B04_angle.py | 42 +- .../analysis_db/B05_define_angle.py | 20 +- .../analysis_db/B06_correct_separate_var.py | 17 +- src/icesat2waves/analysis_db/__init__.py | 2 +- src/icesat2waves/app.py | 1 - src/icesat2waves/config/__init__.py | 2 +- src/icesat2waves/config/generate_colors.py | 80 +- src/icesat2waves/config/startup.py | 110 ++- src/icesat2waves/local_modules/__init__.py | 2 +- .../local_modules/jonswap_gamma.py | 8 +- .../local_modules/m_colormanager_ph3.py | 781 ++++++++++++------ .../local_modules/m_general_ph3.py | 72 +- .../local_modules/m_spectrum_ph3.py | 34 +- src/icesat2waves/local_modules/m_tools_ph3.py | 61 +- src/icesat2waves/tools/__init__.py | 2 +- src/icesat2waves/tools/angle_optimizer.py | 2 +- src/icesat2waves/tools/generalized_FT.py | 16 +- src/icesat2waves/tools/iotools.py | 6 +- src/icesat2waves/tools/lanczos.py | 2 +- 23 files changed, 818 insertions(+), 521 deletions(-) diff --git a/src/icesat2waves/__init__.py b/src/icesat2waves/__init__.py index f574b610..3c453ede 100644 --- a/src/icesat2waves/__init__.py +++ b/src/icesat2waves/__init__.py @@ -1 +1 @@ -"""Tools to analyse data from ICESat-2.""" \ No newline at end of file +"""Tools to analyse data from ICESat-2.""" diff --git a/src/icesat2waves/analysis_db/A02c_IOWAGA_thredds_prior.py b/src/icesat2waves/analysis_db/A02c_IOWAGA_thredds_prior.py index 5d406259..d0d2501f 100644 --- a/src/icesat2waves/analysis_db/A02c_IOWAGA_thredds_prior.py +++ b/src/icesat2waves/analysis_db/A02c_IOWAGA_thredds_prior.py @@ -412,21 +412,13 @@ def draw_range(lon_range, lat_range, *args, **kwargs): draw_range(lon_range, lat_range, c="blue", linewidth=0.7, zorder=10) if fv != "ice": - cm = plt.pcolor( - lon, lat, G_beam[fv], vmin=cl[0], vmax=cl[-1], cmap=fc - ) + cm = plt.pcolor(lon, lat, G_beam[fv], vmin=cl[0], vmax=cl[-1], cmap=fc) if G_beam.ice.shape[0] > 1: - plt.contour( - lon, lat, G_beam.ice, colors="black", linewidths=0.6 - ) + plt.contour(lon, lat, G_beam.ice, colors="black", linewidths=0.6) else: - cm = plt.pcolor( - lon, lat, G_beam[fv], vmin=cl[0], vmax=cl[-1], cmap=fc - ) + cm = plt.pcolor(lon, lat, G_beam[fv], vmin=cl[0], vmax=cl[-1], cmap=fc) - plt.title( - G_beam[fv].long_name.replace(" ", "\n") + "\n" + fv, loc="left" - ) + plt.title(G_beam[fv].long_name.replace(" ", "\n") + "\n" + fv, loc="left") ax1.axis("equal") ax2 = F.fig.add_subplot(gs[-1, fp]) @@ -471,9 +463,7 @@ def test_nan_frac(imask): } # flatten key_list_pairs.values to a list - key_list_pairs2 = [ - item for pair in key_list_pairs.values() for item in pair - ] + key_list_pairs2 = [item for pair in key_list_pairs.values() for item in pair] key_list_scaler = set(key_list) - set(key_list_pairs2) @@ -501,12 +491,8 @@ def test_nan_frac(imask): "lontigude", # TODO: fix typo? ] Tend["lat"] = [ - ice_mask_prior.latitude[ice_mask_prior.sum("longitude") == 0] - .mean() - .data, - ice_mask_prior.latitude[ice_mask_prior.sum("longitude") == 0] - .std() - .data, + ice_mask_prior.latitude[ice_mask_prior.sum("longitude") == 0].mean().data, + ice_mask_prior.latitude[ice_mask_prior.sum("longitude") == 0].std().data, "latitude", ] Tend = Tend.T diff --git a/src/icesat2waves/analysis_db/B02_make_spectra_gFT.py b/src/icesat2waves/analysis_db/B02_make_spectra_gFT.py index dca7e76f..9920f241 100644 --- a/src/icesat2waves/analysis_db/B02_make_spectra_gFT.py +++ b/src/icesat2waves/analysis_db/B02_make_spectra_gFT.py @@ -88,7 +88,6 @@ def run_B02_make_spectra_gFT( } report_input_parameters(**kargs) - workdir, _ = update_paths_mconfig(output_dir, mconfig) load_path = Path(workdir, batch_key, "B01_regrid") @@ -111,9 +110,7 @@ def run_B02_make_spectra_gFT( nan_fraction = list() for k in all_beams: heights_c_std = io.get_beam_var_hdf_store(Gd[k], "x") - nan_fraction.append( - np.sum(np.isnan(heights_c_std)) / heights_c_std.shape[0] - ) + nan_fraction.append(np.sum(np.isnan(heights_c_std)) / heights_c_std.shape[0]) del heights_c_std @@ -185,7 +182,9 @@ def run_B02_make_spectra_gFT( for k in all_beams: dist_i = io.get_beam_var_hdf_store(Gd[k], "x") x_mask = (dist_i > xlims[0]) & (dist_i < xlims[1]) - _logger.debug("k: %s, sum/range: %s", k, sum(x_mask["x"]) / (xlims[1] - xlims[0])) + _logger.debug( + "k: %s, sum/range: %s", k, sum(x_mask["x"]) / (xlims[1] - xlims[0]) + ) _logger.debug("-reduced frequency resolution") kk = kk[::2] diff --git a/src/icesat2waves/analysis_db/B03_plot_spectra_ov.py b/src/icesat2waves/analysis_db/B03_plot_spectra_ov.py index e517b753..f17efd59 100644 --- a/src/icesat2waves/analysis_db/B03_plot_spectra_ov.py +++ b/src/icesat2waves/analysis_db/B03_plot_spectra_ov.py @@ -139,9 +139,7 @@ def run_B03_plot_spectra_ov( for k in all_beams: I = Gk.sel(beam=k) I2 = Gx.sel(beam=k) - plt.plot( - I["lon"], I["lat"], ".", c=col_dict[k], markersize=0.7, linewidth=0.3 - ) + plt.plot(I["lon"], I["lat"], ".", c=col_dict[k], markersize=0.7, linewidth=0.3) plt.plot(I2["lon"], I2["lat"], "|", c=col_dict[k], markersize=0.7) plt.xlabel("lon") @@ -185,8 +183,7 @@ def run_B03_plot_spectra_ov( # TODO: refactor to make more readable. CP G_gFT_wmean = ( - Gk["gFT_PSD_data"].where(~np.isnan(Gk["gFT_PSD_data"]), 0) - * Gk["N_per_stancil"] + Gk["gFT_PSD_data"].where(~np.isnan(Gk["gFT_PSD_data"]), 0) * Gk["N_per_stancil"] ).sum("beam") / Gk["N_per_stancil"].sum("beam") G_gFT_wmean["N_per_stancil"] = Gk["N_per_stancil"].sum("beam") @@ -219,9 +216,7 @@ def run_B03_plot_spectra_ov( ) dd = 10 * np.log10(Gplot) dd = dd.where(~np.isinf(dd), np.nan) - clev_log = ( - M.clevels([dd.quantile(0.01).data, dd.quantile(0.98).data * 1.2], 31) * 1 - ) + clev_log = M.clevels([dd.quantile(0.01).data, dd.quantile(0.98).data * 1.2], 31) * 1 xlims = Gmean.x[0] / 1e3, Gmean.x[-1] / 1e3 @@ -313,9 +308,7 @@ def run_B03_plot_spectra_ov( I = Gfft.sel(beam=k) plt.scatter( (x0 + I.x.data) / 1e3, - I.power_spec.sel(k=slice(k_max_range[0], k_max_range[2])).integrate( - "k" - ), + I.power_spec.sel(k=slice(k_max_range[0], k_max_range[2])).integrate("k"), s=0.5, marker=".", c="blue", @@ -376,19 +369,14 @@ def run_B03_plot_spectra_ov( .argmax() .data ) - xpp = x_pos_sel[ - [int(i) for i in np.round(np.linspace(0, x_pos_sel.size - 1, 4))] - ] + xpp = x_pos_sel[[int(i) for i in np.round(np.linspace(0, x_pos_sel.size - 1, 4))]] xpp = np.insert(xpp, 0, x_pos_max) for i in xpp: F = M.FigureAxisXY(6, 8, container=True, view_scale=0.8) plt.suptitle( - "gFT Model and Spectrograms | x=" - + str(Gk.x[i].data) - + " \n" - + track_name, + "gFT Model and Spectrograms | x=" + str(Gk.x[i].data) + " \n" + track_name, y=0.95, ) gs = GridSpec(5, 6, wspace=0.2, hspace=0.7) @@ -457,9 +445,7 @@ def run_B03_plot_spectra_ov( plt.ylabel("relative slope (m/m)") # TODO: compute xlabel as fstring. CP plt.xlabel( - "segment distance $\eta$ (km) @ x=" - + fltostr(Gx_1.x.data / 1e3, 2) - + "km" + "segment distance $\eta$ (km) @ x=" + fltostr(Gx_1.x.data / 1e3, 2) + "km" ) # spectra @@ -485,9 +471,7 @@ def run_B03_plot_spectra_ov( dd = Gk_1.gFT_PSD_data plt.plot(Gk_1.k, dd, color="gray", linewidth=0.5, alpha=0.5) - dd = Gk_1.gFT_PSD_data.rolling( - k=10, min_periods=1, center=True - ).mean() + dd = Gk_1.gFT_PSD_data.rolling(k=10, min_periods=1, center=True).mean() plt.plot(Gk_1.k, dd, color=col_d[k], linewidth=0.8) # handle the 'All-NaN slice encountered' warning if np.all(np.isnan(dd.data)): @@ -567,14 +551,10 @@ def run_B03_plot_spectra_ov( plt.ylabel("relative slope (m/m)") # TODO: compute xlabel as fstring. CP plt.xlabel( - "segment distance $\eta$ (km) @ x=" - + fltostr(Gx_1.x.data / 1e3, 2) - + "km" + "segment distance $\eta$ (km) @ x=" + fltostr(Gx_1.x.data / 1e3, 2) + "km" ) - F.save_pup( - path=str(plot_path / "B03_spectra"), name=f"B03_freq_reconst_x{i}" - ) + F.save_pup(path=str(plot_path / "B03_spectra"), name=f"B03_freq_reconst_x{i}") MT.json_save( "B03_success", diff --git a/src/icesat2waves/analysis_db/B04_angle.py b/src/icesat2waves/analysis_db/B04_angle.py index 2f0a9e72..b3acf6cb 100644 --- a/src/icesat2waves/analysis_db/B04_angle.py +++ b/src/icesat2waves/analysis_db/B04_angle.py @@ -142,12 +142,10 @@ def run_B04_angle( #### Define Prior Pperiod = Prior.loc[["ptp0", "ptp1", "ptp2", "ptp3", "ptp4", "ptp5"]]["mean"] - Pdir = Prior.loc[["pdp0", "pdp1", "pdp2", "pdp3", "pdp4", "pdp5"]][ - "mean" - ].astype("float") - Pspread = Prior.loc[["pspr0", "pspr1", "pspr2", "pspr3", "pspr4", "pspr5"]][ - "mean" - ] + Pdir = Prior.loc[["pdp0", "pdp1", "pdp2", "pdp3", "pdp4", "pdp5"]]["mean"].astype( + "float" + ) + Pspread = Prior.loc[["pspr0", "pspr1", "pspr2", "pspr3", "pspr4", "pspr5"]]["mean"] Pperiod = Pperiod[~np.isnan(list(Pspread))] Pdir = Pdir[~np.isnan(list(Pspread))] @@ -266,7 +264,9 @@ def run_B04_angle( prior_angle = Prior_smth.Prior_direction * 180 / np.pi if (abs(prior_angle) > 80).all(): _logger.critical( - "Prior angle is %s %s. Quit.", prior_angle.min().data, prior_angle.max().data, + "Prior angle is %s %s. Quit.", + prior_angle.min().data, + prior_angle.max().data, ) dd_save = { "time": time.asctime(time.localtime(time.time())), @@ -452,9 +452,7 @@ def plot_instance( "-", c=next(col_list), ) - plt.xlim( - x_concat[y_concat == y_pos][0], x_concat[y_concat == y_pos][-1] - ) + plt.xlim(x_concat[y_concat == y_pos][0], x_concat[y_concat == y_pos][-1]) plt.xlabel("meter") F.ax3 = F.fig.add_subplot(gs[2:, 0:-1]) @@ -465,9 +463,7 @@ def plot_instance( ) if optimze is True: - SM.plot_optimze( - color="r", markersize=10, zorder=12, label="Dual Annealing" - ) + SM.plot_optimze(color="r", markersize=10, zorder=12, label="Dual Annealing") if sample is True: SM.plot_sample( @@ -624,9 +620,7 @@ def plot_instance( amp_Z = 1 prior_sel = { "alpha": ( - Prior_smth.sel( - k=k_prime_max, method="nearest" - ).Prior_direction.data, + Prior_smth.sel(k=k_prime_max, method="nearest").Prior_direction.data, Prior_smth.sel(k=k_prime_max, method="nearest").Prior_spread.data, ) } @@ -657,9 +651,7 @@ def get_instance(k_pair): Prior_smth.sel( k=k_prime_max, method="nearest" ).Prior_direction.data, - Prior_smth.sel( - k=k_prime_max, method="nearest" - ).Prior_spread.data, + Prior_smth.sel(k=k_prime_max, method="nearest").Prior_spread.data, ) } @@ -676,9 +668,7 @@ def get_instance(k_pair): min=k_prime_max * 0.5, max=k_prime_max * 1.5, ) - SM.params.add( - "K_amp", amp_Z, vary=False, min=amp_Z * 0.0, max=amp_Z * 5 - ) + SM.params.add("K_amp", amp_Z, vary=False, min=amp_Z * 0.0, max=amp_Z * 5) L_sample_i = None L_optimize_i = None @@ -714,9 +704,7 @@ def get_instance(k_pair): "alpha", alpha_dx, burn=N_sample_chain_burn, plot_flag=False ) fitter = SM.fitter # MCMC results - z_model = SM.objective_func( - fitter.params, *fitting_args, test_flag=True - ) + z_model = SM.objective_func(fitter.params, *fitting_args, test_flag=True) cost = (fitter.residual**2).sum() / (z_concat**2).sum() if plot_flag: @@ -822,9 +810,7 @@ def get_instance(k_pair): cost_stack = dict() marginal_stack = dict() L_sample = pd.DataFrame(index=["alpha", "group_phase", "K_prime", "K_amp"]) - L_optimize = pd.DataFrame( - index=["alpha", "group_phase", "K_prime", "K_amp"] - ) + L_optimize = pd.DataFrame(index=["alpha", "group_phase", "K_prime", "K_amp"]) L_brute = pd.DataFrame(index=["alpha", "group_phase", "K_prime", "K_amp"]) for kk, I in A.items(): diff --git a/src/icesat2waves/analysis_db/B05_define_angle.py b/src/icesat2waves/analysis_db/B05_define_angle.py index 30434890..ad3daf71 100644 --- a/src/icesat2waves/analysis_db/B05_define_angle.py +++ b/src/icesat2waves/analysis_db/B05_define_angle.py @@ -325,9 +325,7 @@ def define_angle( ax_list[group] = ax0 data = corrected_marginals.isel(x=xi).sel(beam_group=group) - weights = derive_weights( - Marginals.weight.isel(x=xi).sel(beam_group=group) - ) + weights = derive_weights(Marginals.weight.isel(x=xi).sel(beam_group=group)) weights = weights**2 # derive angle axis @@ -349,9 +347,9 @@ def define_angle( data_collect[group] = data_wmean data_collect = xr.concat(data_collect.values(), dim="beam_group") - final_data = (group_weight * data_collect).sum( + final_data = (group_weight * data_collect).sum("beam_group") / group_weight.sum( "beam_group" - ) / group_weight.sum("beam_group").data + ).data plt.sca(ax_sum) plt.stairs(final_data, x_angle, color="k", alpha=1, linewidth=0.8) @@ -378,9 +376,7 @@ def define_angle( priors_k = Marginals.Prior_direction[~np.isnan(k_mask.isel(x=xi))] for pk in priors_k: - ax_final.axvline( - pk, color=color_schemes.cascade2, linewidth=1, alpha=0.7 - ) + ax_final.axvline(pk, color=color_schemes.cascade2, linewidth=1, alpha=0.7) plt.stairs(final_data, x_angle, color="k", alpha=0.5, linewidth=0.8) @@ -466,9 +462,7 @@ def define_angle( _title = f"{track_name}\nPower Spectra (m/m)$^2$ k$^{{-1}}$" plt.title(_title, loc="left") - cbar = plt.colorbar( - fraction=0.018, pad=0.01, orientation="vertical", label="Power" - ) + cbar = plt.colorbar(fraction=0.018, pad=0.01, orientation="vertical", label="Power") cbar.outline.set_visible(False) clev_ticks = np.round(clev_spec[::3], 0) cbar.set_ticks(clev_ticks) @@ -529,9 +523,9 @@ def define_angle( i_spec = weighted_spec.sel(x=slice(x_range[0], x_range[-1])) i_dir = corrected_marginals.sel(x=slice(x_range[0], x_range[-1])) - dir_data = (i_dir * i_dir.N_data).sum( + dir_data = (i_dir * i_dir.N_data).sum(["beam_group", "x"]) / i_dir.N_data.sum( ["beam_group", "x"] - ) / i_dir.N_data.sum(["beam_group", "x"]) + ) lims = get_first_and_last_nonzero_data(dir_data) plot_data = build_plot_data(dir_data, i_spec, lims) diff --git a/src/icesat2waves/analysis_db/B06_correct_separate_var.py b/src/icesat2waves/analysis_db/B06_correct_separate_var.py index 7ae72628..274aaff6 100644 --- a/src/icesat2waves/analysis_db/B06_correct_separate_var.py +++ b/src/icesat2waves/analysis_db/B06_correct_separate_var.py @@ -2,6 +2,7 @@ This file open a ICEsat2 track applied filters and corections and returns smoothed photon heights on a regular grid in an .nc file. This is python 3 """ + import logging import os @@ -317,9 +318,7 @@ def run_B06_correct_separate_var( file_suffixes = ["_gFT_k.nc", "_gFT_x.nc"] Gk, Gx = [ - xr.open_dataset( - load_path_work / "B02_spectra" / f"B02_{track_name}{suffix}" - ) + xr.open_dataset(load_path_work / "B02_spectra" / f"B02_{track_name}{suffix}") for suffix in file_suffixes ] @@ -522,9 +521,7 @@ def run_B06_correct_separate_var( + " to " + str(np.round(Gx.isel(x=-1).lat.mean().data, 2)) ) - plt.title( - next(fn) + "Mean Displacement Spectra\n(lat=" + lat_str + ")", loc="left" - ) + plt.title(next(fn) + "Mean Displacement Spectra\n(lat=" + lat_str + ")", loc="left") dd = 10 * np.log((G_gFT_smth / G_gFT_smth.k).isel(x=slice(0, -1))) dd = dd.where(~np.isinf(dd), np.nan) @@ -690,17 +687,13 @@ def reconstruct_displacement(Gx_1, Gk_1, T3, k_thresh): T3["heights_c_model_err"] = np.interp( T3["dist"], continous_x_grid, concented_err ) - T3["heights_c_residual"] = ( - T3["heights_c_weighted_mean"] - T3["heights_c_model"] - ) + T3["heights_c_residual"] = T3["heights_c_weighted_mean"] - T3["heights_c_model"] B3_v2[bb] = T3 Gx_v2[bb] = Gx_k try: - G_angle = xr.open_dataset( - load_path_angle / (f"B05_{track_name}_angle_pdf.nc") - ) + G_angle = xr.open_dataset(load_path_angle / (f"B05_{track_name}_angle_pdf.nc")) except ValueError as e: _logger.warning("%s no angle data found, skip angle corretion", e) diff --git a/src/icesat2waves/analysis_db/__init__.py b/src/icesat2waves/analysis_db/__init__.py index 468e6aa7..8a4cd3c6 100644 --- a/src/icesat2waves/analysis_db/__init__.py +++ b/src/icesat2waves/analysis_db/__init__.py @@ -1 +1 @@ -"Logic to download and analyse WaveWatch3 files, plot spectra results" \ No newline at end of file +"Logic to download and analyse WaveWatch3 files, plot spectra results" diff --git a/src/icesat2waves/app.py b/src/icesat2waves/app.py index 150cd767..dde9ce35 100644 --- a/src/icesat2waves/app.py +++ b/src/icesat2waves/app.py @@ -205,6 +205,5 @@ def correct_separate( # TODO: rename with a verb or something ) - if __name__ == "__main__": app() diff --git a/src/icesat2waves/config/__init__.py b/src/icesat2waves/config/__init__.py index c318732e..64d3f348 100644 --- a/src/icesat2waves/config/__init__.py +++ b/src/icesat2waves/config/__init__.py @@ -1 +1 @@ -"""Configuration file containing project paths""" \ No newline at end of file +"""Configuration file containing project paths""" diff --git a/src/icesat2waves/config/generate_colors.py b/src/icesat2waves/config/generate_colors.py index b8b5f451..b382eff9 100644 --- a/src/icesat2waves/config/generate_colors.py +++ b/src/icesat2waves/config/generate_colors.py @@ -1,63 +1,63 @@ -#%matplotlib inline +# %matplotlib inline import logging from icesat2waves.local_modules import m_colormanager_ph3 as M_color _logger = logging.getLogger(__name__) -mconfig['paths'] -path=mconfig['paths']['config'] -A = M_color.ase_to_json(path+'color_def.ase') +mconfig["paths"] +path = mconfig["paths"]["config"] +A = M_color.ase_to_json(path + "color_def.ase") -B=dict() -for i in A[0]['swatches']: - B[i['name']] = i['data']['values'] - _logger.debug("swatch name: %s, values: %s", i['name'], i['data']['values']) +B = dict() +for i in A[0]["swatches"]: + B[i["name"]] = i["data"]["values"] + _logger.debug("swatch name: %s, values: %s", i["name"], i["data"]["values"]) -rels=dict() +rels = dict() -rels['plus']=B['red'] -rels['minus']=B['blue'] +rels["plus"] = B["red"] +rels["minus"] = B["blue"] -rels['blue']=B['blue'] -rels['lightblue']=B['cascade3'] -rels['darkblue']=B['cascade1'] +rels["blue"] = B["blue"] +rels["lightblue"] = B["cascade3"] +rels["darkblue"] = B["cascade1"] -rels['white']=B['white'] -rels['gridcolor']=B['gridcolor'] -rels['grey']=B['gray'] +rels["white"] = B["white"] +rels["gridcolor"] = B["gridcolor"] +rels["grey"] = B["gray"] -rels['orange']=B['orange'] -rels['red']=B['red'] -rels['green']=B['green'] +rels["orange"] = B["orange"] +rels["red"] = B["red"] +rels["green"] = B["green"] -rels['cascade1']=B['cascade1'] -rels['cascade2']=B['cascade2'] -rels['cascade3']=B['cascade3'] -rels['cascade4']=B['gridcolor'] +rels["cascade1"] = B["cascade1"] +rels["cascade2"] = B["cascade2"] +rels["cascade3"] = B["cascade3"] +rels["cascade4"] = B["gridcolor"] -rels['rascade1']=B['rascade2'] -rels['rascade2']=B['rascade1'] -rels['rascade3']=B['rascade3'] +rels["rascade1"] = B["rascade2"] +rels["rascade2"] = B["rascade1"] +rels["rascade3"] = B["rascade3"] -rels['aug1']=B['orange'] -rels['aug2']=B['green'] +rels["aug1"] = B["orange"] +rels["aug2"] = B["green"] -rels['gt1l']=B['rascade1'] -rels['gt1r']=B['rascade3'] +rels["gt1l"] = B["rascade1"] +rels["gt1r"] = B["rascade3"] -rels['gt2l']=B['green1'] -rels['gt2r']=B['green2'] +rels["gt2l"] = B["green1"] +rels["gt2r"] = B["green2"] -rels['gt3l']=B['cascade1'] -rels['gt3r']=B['blue'] +rels["gt3l"] = B["cascade1"] +rels["gt3r"] = B["blue"] -rels['group1']=B['rascade1'] -rels['group2']=B['green1'] -rels['group3']=B['cascade1'] +rels["group1"] = B["rascade1"] +rels["group2"] = B["green1"] +rels["group3"] = B["cascade1"] -B['rels']=rels +B["rels"] = rels -M_color.json_save('color_def', path, B) +M_color.json_save("color_def", path, B) diff --git a/src/icesat2waves/config/startup.py b/src/icesat2waves/config/startup.py index dd7415eb..51160aac 100644 --- a/src/icesat2waves/config/startup.py +++ b/src/icesat2waves/config/startup.py @@ -12,7 +12,7 @@ ## Read folders and configuration paths config_dir_path = os.path.dirname(__file__) -mconfig=MT.json_load('config',config_dir_path) +mconfig = MT.json_load("config", config_dir_path) ## check folders exist. Create if dont. for folder_name, folder_path in mconfig["paths"].items(): @@ -22,54 +22,80 @@ # add config path mconfig["paths"].update({"config": config_dir_path}) -#load colorscheme -color_schemes=M_color.color(path=mconfig['paths']['config'], name='color_def') +# load colorscheme +color_schemes = M_color.color(path=mconfig["paths"]["config"], name="color_def") -lstrings =iter([i+') ' for i in list(string.ascii_lowercase)]) +lstrings = iter([i + ") " for i in list(string.ascii_lowercase)]) # define journal fig sizes -fig_sizes = mconfig['fig_sizes']['AMS'] +fig_sizes = mconfig["fig_sizes"]["AMS"] SMALL_SIZE = 8 MEDIUM_SIZE = 10 BIGGER_SIZE = 12 -legend_properties = {'weight':'bold'} +legend_properties = {"weight": "bold"} -plt.rc('font', size=SMALL_SIZE, serif='Helvetica Neue', weight='normal') # controls default text sizes -plt.rc('text', usetex='false') -plt.rc('axes', titlesize=MEDIUM_SIZE, labelweight='normal') # fontsize of the axes title -plt.rc('axes', labelsize=SMALL_SIZE, labelweight='normal') # fontsize of the x and y labels -plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels -plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels -plt.rc('legend', fontsize=SMALL_SIZE, frameon=False) # legend fontsize -plt.rc('figure', titlesize=MEDIUM_SIZE, titleweight='bold', autolayout=True) # fontsize of the figure title -plt.rc('path', simplify=True) -plt.rcParams['figure.figsize'] = (10, 8) -plt.rcParams['pcolor.shading'] = 'auto' -plt.rc('pcolor', shading = 'auto') -plt.rc('xtick.major', size= 4, width=1 ) -plt.rc('ytick.major', size= 3.8, width=1 ) -plt.rc('axes', labelsize= MEDIUM_SIZE, labelweight='normal') -plt.rc('axes.spines', top= False, right=False ) +plt.rc( + "font", size=SMALL_SIZE, serif="Helvetica Neue", weight="normal" +) # controls default text sizes +plt.rc("text", usetex="false") +plt.rc( + "axes", titlesize=MEDIUM_SIZE, labelweight="normal" +) # fontsize of the axes title +plt.rc( + "axes", labelsize=SMALL_SIZE, labelweight="normal" +) # fontsize of the x and y labels +plt.rc("xtick", labelsize=SMALL_SIZE) # fontsize of the tick labels +plt.rc("ytick", labelsize=SMALL_SIZE) # fontsize of the tick labels +plt.rc("legend", fontsize=SMALL_SIZE, frameon=False) # legend fontsize +plt.rc( + "figure", titlesize=MEDIUM_SIZE, titleweight="bold", autolayout=True +) # fontsize of the figure title +plt.rc("path", simplify=True) +plt.rcParams["figure.figsize"] = (10, 8) +plt.rcParams["pcolor.shading"] = "auto" +plt.rc("pcolor", shading="auto") +plt.rc("xtick.major", size=4, width=1) +plt.rc("ytick.major", size=3.8, width=1) +plt.rc("axes", labelsize=MEDIUM_SIZE, labelweight="normal") +plt.rc("axes.spines", top=False, right=False) -def font_for_print(SMALL_SIZE = 6, MEDIUM_SIZE = 8): - plt.rc('font', size=SMALL_SIZE, serif='Helvetica Neue', weight='normal') # controls default text sizes - plt.rc('text', usetex='false') - plt.rc('axes', titlesize=MEDIUM_SIZE, labelweight='normal') # fontsize of the axes title - plt.rc('axes', labelsize=SMALL_SIZE, labelweight='normal') #, family='bold') # fontsize of the x and y labels - plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels - plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels - plt.rc('legend', fontsize=SMALL_SIZE, frameon=False) # legend fontsize - plt.rc('figure', titlesize=MEDIUM_SIZE, titleweight='bold', autolayout=True) #, family='bold') # fontsize of the figure title - plt.rc('axes', labelsize= SMALL_SIZE, labelweight='normal') -def font_for_pres(SMALL_SIZE = 10, MEDIUM_SIZE = 12): - plt.rc('font', size=SMALL_SIZE, serif='Helvetica Neue', weight='normal') # controls default text sizes - plt.rc('text', usetex='false') - plt.rc('axes', titlesize=MEDIUM_SIZE, labelweight='normal') # fontsize of the axes title - plt.rc('axes', labelsize=SMALL_SIZE, labelweight='normal') #, family='bold') # fontsize of the x and y labels - plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels - plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels - plt.rc('legend', fontsize=SMALL_SIZE, frameon=False) # legend fontsize - plt.rc('figure', titlesize=MEDIUM_SIZE, titleweight='bold', autolayout=True) # fontsize of the figure title - plt.rc('axes', labelsize= SMALL_SIZE, labelweight='normal') +def font_for_print(SMALL_SIZE=6, MEDIUM_SIZE=8): + plt.rc( + "font", size=SMALL_SIZE, serif="Helvetica Neue", weight="normal" + ) # controls default text sizes + plt.rc("text", usetex="false") + plt.rc( + "axes", titlesize=MEDIUM_SIZE, labelweight="normal" + ) # fontsize of the axes title + plt.rc( + "axes", labelsize=SMALL_SIZE, labelweight="normal" + ) # , family='bold') # fontsize of the x and y labels + plt.rc("xtick", labelsize=SMALL_SIZE) # fontsize of the tick labels + plt.rc("ytick", labelsize=SMALL_SIZE) # fontsize of the tick labels + plt.rc("legend", fontsize=SMALL_SIZE, frameon=False) # legend fontsize + plt.rc( + "figure", titlesize=MEDIUM_SIZE, titleweight="bold", autolayout=True + ) # , family='bold') # fontsize of the figure title + plt.rc("axes", labelsize=SMALL_SIZE, labelweight="normal") + + +def font_for_pres(SMALL_SIZE=10, MEDIUM_SIZE=12): + plt.rc( + "font", size=SMALL_SIZE, serif="Helvetica Neue", weight="normal" + ) # controls default text sizes + plt.rc("text", usetex="false") + plt.rc( + "axes", titlesize=MEDIUM_SIZE, labelweight="normal" + ) # fontsize of the axes title + plt.rc( + "axes", labelsize=SMALL_SIZE, labelweight="normal" + ) # , family='bold') # fontsize of the x and y labels + plt.rc("xtick", labelsize=SMALL_SIZE) # fontsize of the tick labels + plt.rc("ytick", labelsize=SMALL_SIZE) # fontsize of the tick labels + plt.rc("legend", fontsize=SMALL_SIZE, frameon=False) # legend fontsize + plt.rc( + "figure", titlesize=MEDIUM_SIZE, titleweight="bold", autolayout=True + ) # fontsize of the figure title + plt.rc("axes", labelsize=SMALL_SIZE, labelweight="normal") diff --git a/src/icesat2waves/local_modules/__init__.py b/src/icesat2waves/local_modules/__init__.py index 360a572a..0f73c056 100644 --- a/src/icesat2waves/local_modules/__init__.py +++ b/src/icesat2waves/local_modules/__init__.py @@ -1 +1 @@ -"""Utility functions to manage parameters to plot results""" \ No newline at end of file +"""Utility functions to manage parameters to plot results""" diff --git a/src/icesat2waves/local_modules/jonswap_gamma.py b/src/icesat2waves/local_modules/jonswap_gamma.py index 540ac361..2665650d 100644 --- a/src/icesat2waves/local_modules/jonswap_gamma.py +++ b/src/icesat2waves/local_modules/jonswap_gamma.py @@ -8,6 +8,7 @@ _logger = logging.getLogger(__name__) + # def normalize_time(time): """ @@ -122,7 +123,7 @@ def pierson_moskowitz_fetch_limit(f, X, U): """ - w = 2.0 * np.pi * f # rad/sec + w = 2.0 * np.pi * f # rad/sec # dimensionless alpha = 0.076 * (g * X / U**2) ** (-0.22) @@ -156,7 +157,7 @@ def JONSWAP_default(f, X, U, gamma=3.3): varying gamma to less then its JONSWAP value will mimic the attenuation of the peak with travel time to the limit of gamma=1, which is the PM spectrums """ - w = 2.0 * np.pi * f # rad/sec + w = 2.0 * np.pi * f # rad/sec # dimensionless alpha = 0.076 * (g * X / U**2) ** (-0.22) @@ -171,7 +172,7 @@ def JONSWAP_default(f, X, U, gamma=3.3): return ( alpha * g**2.0 * w ** (-5.0) * np.exp(-5.0 / 4.0 * (w / wp) ** -4) * peak_factor - ) # Hz**-5 m**2 /s**4 = m**2 sec + ) # Hz**-5 m**2 /s**4 = m**2 sec """ add function for X_tilde(X, U10), alpha(f_max, U10) and f_max(U10, X_tilde) or f_max(U, X)""" @@ -734,7 +735,6 @@ def reg_func(p0, pi, p_err_unit): _logger.debug(Jm) - def cost(value_dict, time, f, data=None, weight=None, prior=False, eps=None): """ Wrapper around residual and regulizer. diff --git a/src/icesat2waves/local_modules/m_colormanager_ph3.py b/src/icesat2waves/local_modules/m_colormanager_ph3.py index 6e35e456..2aa723f8 100644 --- a/src/icesat2waves/local_modules/m_colormanager_ph3.py +++ b/src/icesat2waves/local_modules/m_colormanager_ph3.py @@ -14,23 +14,27 @@ def ase_to_json(path): os.getcwd() import swatch - A=swatch.parse(path) - for i in A[0]['swatches']: - _logger.debug("swatch name: %s, swatch values: %s", i['name'], i['data']['values']) + A = swatch.parse(path) + + for i in A[0]["swatches"]: + _logger.debug( + "swatch name: %s, swatch values: %s", i["name"], i["data"]["values"] + ) return A def json_save(name, path, data, return_name=False): import json + if not os.path.exists(path): os.makedirs(path) - full_name_root=os.path.join(path,name) - full_name= (os.path.join(full_name_root+ '.json')) - with open(full_name, 'w') as outfile: + full_name_root = os.path.join(path, name) + full_name = os.path.join(full_name_root + ".json") + with open(full_name, "w") as outfile: json.dump(data, outfile) - _logger.debug('save at %s:', full_name) + _logger.debug("save at %s:", full_name) if return_name: return full_name_root else: @@ -39,245 +43,538 @@ def json_save(name, path, data, return_name=False): def json_load(name, path): import json - full_name= (os.path.join(path,name+ '.json')) - - with open(full_name, 'r') as ifile: - data=json.load(ifile) - _logger.debug('loaded from: %s', full_name) - return data + full_name = os.path.join(path, name + ".json") + with open(full_name, "r") as ifile: + data = json.load(ifile) + _logger.debug("loaded from: %s", full_name) + return data class color: - def __init__(self, path=None, name=None): - self.white=(1,1,1) - if (path is not None) & (name is not None): - _logger.debug('color theme: %s', name) - try: - theme=json_load(name, path) - for k, v in theme.items(): - setattr(self, k, v) - except: - _logger.debug('fail load theme, fall back to default theme') - _logger.debug("path: %s%s", path, 'mhell_colortheme17') - self.default_colors() - - else: + def __init__(self, path=None, name=None): + self.white = (1, 1, 1) + if (path is not None) & (name is not None): + _logger.debug("color theme: %s", name) + try: + theme = json_load(name, path) + for k, v in theme.items(): + setattr(self, k, v) + except: + _logger.debug("fail load theme, fall back to default theme") + _logger.debug("path: %s%s", path, "mhell_colortheme17") self.default_colors() - def alpha(self, color, a): - return ( color[0], color[1], color[2], a) - - - def colormaps2(self,n, gamma=None): - gamma=1 if gamma is None else gamma - - rels = self.rels - self.divergingwhite=LinearSegmentedColormap.from_list("my_colormap", (rels['plus'],rels['white'], rels['minus']), N=n, gamma=gamma) - self.divergingwhitelarge=LinearSegmentedColormap.from_list("my_colormap", - (rels['darkblue'], rels['blue'],rels['white'], rels['orange'], rels['red']), N=n, gamma=gamma) - - self.whiteblue=LinearSegmentedColormap.from_list("my_colormap", - (rels['white'],rels['lightblue'], rels['blue'],rels['darkblue']), N=n, gamma=gamma) - self.greyblue=LinearSegmentedColormap.from_list("my_colormap", - (rels['gridcolor'],rels['lightblue'], rels['blue'],rels['darkblue']), N=n, gamma=gamma) - self.greyblue_r=LinearSegmentedColormap.from_list("my_colormap", - (rels['darkblue'] , rels['blue'], rels['lightblue'], rels['gridcolor'] ), N=n, gamma=gamma) - - self.greyred=LinearSegmentedColormap.from_list("my_colormap", - (rels['gridcolor'], rels['rascade1'],rels['rascade2']), N=n, gamma=gamma) - self.greyred_r=LinearSegmentedColormap.from_list("my_colormap", - (rels['rascade3'] , rels['rascade2'], rels['rascade1'], rels['gridcolor'] ), N=n, gamma=gamma) - - self.greyredorange=LinearSegmentedColormap.from_list("my_colormap", - (rels['gridcolor'],rels['cascade3'], rels['red'],rels['orange']), N=n, gamma=gamma) - - self.white_base_blue=LinearSegmentedColormap.from_list("my_colormap", - (rels['white'], rels['darkblue'], rels['blue'],rels['lightblue']), N=n, gamma=gamma) - self.white_base_blue_r=LinearSegmentedColormap.from_list("my_colormap", - (rels['white'], rels['lightblue'], rels['blue'],rels['darkblue']), N=n, gamma=gamma) - - self.white_base_bluegreen=LinearSegmentedColormap.from_list("my_colormap", - (rels['white'], rels['darkblue'], rels['blue'],rels['green']), N=n, gamma=gamma) - self.white_base_bluegreen_r=LinearSegmentedColormap.from_list("my_colormap", - (rels['white'], rels['lightblue'], rels['blue'],rels['darkblue'],rels['group2']), N=n, gamma=gamma) - self.white_base_blgror=LinearSegmentedColormap.from_list("my_colormap", #rels['blue'], - (rels['white'], rels['gridcolor'] ,rels['cascade2'], rels['cascade1'],rels['group1'] ), N=n, gamma=gamma) - - - self.rainbow=LinearSegmentedColormap.from_list("my_colormap", - (rels['darkblue'], rels['blue'], rels['lightblue'], rels['green'],rels['orange'], rels['red'] ), N=n, gamma=gamma) - self.cascade=LinearSegmentedColormap.from_list("my_colormap", - (rels['cascade1'], rels['cascade2'], rels['cascade3'], rels['cascade4'] ), N=n, gamma=gamma) - self.cascade_r=LinearSegmentedColormap.from_list("my_colormap", - (rels['white'], rels['cascade4'], rels['cascade3'], rels['cascade2'], rels['cascade1'], rels['green'] ), N=n, gamma=gamma) - self.cascade_r_nowhite=LinearSegmentedColormap.from_list("my_colormap", - (rels['cascade4'], rels['cascade3'], rels['cascade2'], rels['cascade1'], rels['green'] ), N=n, gamma=gamma) - - self.cascade_highcontrast=LinearSegmentedColormap.from_list("my_colormap", - (rels['cascade1'], rels['cascade3'], rels['aug1'], rels['grey'], rels['aug2'] ), N=n, gamma=gamma) - - - self.circle_small=LinearSegmentedColormap.from_list("my_colormap", - (rels['minus'], rels['plus'],rels['plus'], rels['minus']), N=n, gamma=gamma) - self.circle_small_r=LinearSegmentedColormap.from_list("my_colormap", - (rels['plus'],rels['minus'], rels['minus'], rels['plus']), N=n, gamma=gamma) - - self.circle_medium=LinearSegmentedColormap.from_list("my_colormap", - (rels['red'],rels['green'], rels['darkblue'],rels['green'], rels['red']), N=n, gamma=gamma) - self.circle_medium_r=LinearSegmentedColormap.from_list("my_colormap", - (rels['darkblue'],rels['green'], rels['red'], rels['green'], rels['darkblue']), N=n, gamma=gamma) - - - self.circle_big=LinearSegmentedColormap.from_list("my_colormap", - (rels['darkblue'], rels['blue'], rels['orange'], rels['red'], rels['orange'], rels['blue']), N=n, gamma=gamma) - self.circle_big_r=LinearSegmentedColormap.from_list("my_colormap", - (rels['red'], rels['orange'], rels['blue'], rels['darkblue'], rels['blue'], rels['orange']), N=n, gamma=gamma) - - self.circle_medium_triple=LinearSegmentedColormap.from_list("my_colormap", - (rels['darkblue'], rels['orange'], rels['orange'] , rels['red'], rels['red'], rels['darkblue']), N=n, gamma=gamma) - self.circle_medium_triple_r=LinearSegmentedColormap.from_list("my_colormap", - (rels['red'], rels['darkblue'], rels['darkblue'] , rels['orange'], rels['orange'],rels['red']), N=n, gamma=gamma) - - def colormaps(self,n, gamma=None): - gamma=1 if gamma is None else gamma - self.divergingwhite=LinearSegmentedColormap.from_list("my_colormap", (self.plus,self.white, self.minus), N=n, gamma=gamma) - self.divergingwhitelarge=LinearSegmentedColormap.from_list("my_colormap", - (self.colors['darkblue'], self.colors['blue'],self.white, self.colors['orange'], self.colors['red']), N=n, gamma=gamma) - - self.whiteblue=LinearSegmentedColormap.from_list("my_colormap", - (self.white,self.colors['lightblue'], self.colors['blue'],self.colors['darkblue']), N=n, gamma=gamma) - self.greyblue=LinearSegmentedColormap.from_list("my_colormap", - (self.gridcolor,self.colors['lightblue'], self.colors['blue'],self.colors['darkblue']), N=n, gamma=gamma) - self.greyblue_r=LinearSegmentedColormap.from_list("my_colormap", - (self.colors['darkblue'] , self.colors['blue'], self.colors['lightblue'], self.gridcolor ), N=n, gamma=gamma) - self.greyredorange=LinearSegmentedColormap.from_list("my_colormap", - (self.gridcolor,self.cascade3, self.colors['red'],self.colors['orange']), N=n, gamma=gamma) - - self.white_base_blue=LinearSegmentedColormap.from_list("my_colormap", - (self.white, self.colors['darkblue'], self.colors['blue'],self.colors['lightblue']), N=n, gamma=gamma) - self.white_base_bluegreen=LinearSegmentedColormap.from_list("my_colormap", - (self.white, self.colors['darkblue'], self.colors['blue'],self.colors['green']), N=n, gamma=gamma) - self.rainbow=LinearSegmentedColormap.from_list("my_colormap", - (self.colors['darkblue'], self.colors['blue'], self.colors['lightblue'], self.colors['green'],self.colors['orange'], self.colors['red'] ), N=n, gamma=gamma) - self.cascade=LinearSegmentedColormap.from_list("my_colormap", - (self.cascade1, self.cascade2, self.cascade3, self.cascade4 ), N=n, gamma=gamma) - self.cascade_r=LinearSegmentedColormap.from_list("my_colormap", - (self.white, self.cascade4, self.cascade3, self.cascade2, self.cascade1, self.colors['green'] ), N=n, gamma=gamma) - self.cascade_r_nowhite=LinearSegmentedColormap.from_list("my_colormap", - (self.cascade4, self.cascade3, self.cascade2, self.cascade1, self.colors['green'] ), N=n, gamma=gamma) - - self.cascade_highcontrast=LinearSegmentedColormap.from_list("my_colormap", - (self.cascade1, self.cascade3, self.aug1, self.grey, self.aug2 ), N=n, gamma=gamma) - - def show(self): - for key in self.__dict__.keys(): - _logger.debug("%s",key) - - _logger.debug(' rels dict keys:') - for key in self.rels.keys(): - _logger.debug(" %s", key) - - def plot(self): - dd=self.__dict__.copy() - dd_colors=dd['rels'] - del dd['rels'] - - Y=2.0 - coldd=dd - ncolor=len(coldd) - fig = plt.figure(figsize=[2, Y]) - ax = fig.add_axes([0, 0, .5, Y]) - dy=1/ncolor - y=np.arange(0, 1, dy) - - j=0 - for k,I in coldd.items(): - try: - r1 = mpatch.Rectangle((0, y[j]), 1, dy, color=I) - txt = ax.text(1.1, y[j]+dy*.5, ' '+k, va='center', fontsize=10, - weight='regular') - - ax.add_patch(r1) - j+=1 - except: - pass - - coldd=dd_colors - ncolor=len(coldd) - ax1 = fig.add_axes([1.5, 0, .5, Y]) - dy=1/ncolor - y=np.arange(0, 1, dy) - - j=0 - for k,I in coldd.items(): + + else: + self.default_colors() + + def alpha(self, color, a): + return (color[0], color[1], color[2], a) + + def colormaps2(self, n, gamma=None): + gamma = 1 if gamma is None else gamma + + rels = self.rels + self.divergingwhite = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["plus"], rels["white"], rels["minus"]), + N=n, + gamma=gamma, + ) + self.divergingwhitelarge = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["darkblue"], + rels["blue"], + rels["white"], + rels["orange"], + rels["red"], + ), + N=n, + gamma=gamma, + ) + + self.whiteblue = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["white"], rels["lightblue"], rels["blue"], rels["darkblue"]), + N=n, + gamma=gamma, + ) + self.greyblue = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["gridcolor"], rels["lightblue"], rels["blue"], rels["darkblue"]), + N=n, + gamma=gamma, + ) + self.greyblue_r = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["darkblue"], rels["blue"], rels["lightblue"], rels["gridcolor"]), + N=n, + gamma=gamma, + ) + + self.greyred = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["gridcolor"], rels["rascade1"], rels["rascade2"]), + N=n, + gamma=gamma, + ) + self.greyred_r = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["rascade3"], rels["rascade2"], rels["rascade1"], rels["gridcolor"]), + N=n, + gamma=gamma, + ) + + self.greyredorange = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["gridcolor"], rels["cascade3"], rels["red"], rels["orange"]), + N=n, + gamma=gamma, + ) + + self.white_base_blue = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["white"], rels["darkblue"], rels["blue"], rels["lightblue"]), + N=n, + gamma=gamma, + ) + self.white_base_blue_r = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["white"], rels["lightblue"], rels["blue"], rels["darkblue"]), + N=n, + gamma=gamma, + ) + + self.white_base_bluegreen = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["white"], rels["darkblue"], rels["blue"], rels["green"]), + N=n, + gamma=gamma, + ) + self.white_base_bluegreen_r = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["white"], + rels["lightblue"], + rels["blue"], + rels["darkblue"], + rels["group2"], + ), + N=n, + gamma=gamma, + ) + self.white_base_blgror = LinearSegmentedColormap.from_list( + "my_colormap", # rels['blue'], + ( + rels["white"], + rels["gridcolor"], + rels["cascade2"], + rels["cascade1"], + rels["group1"], + ), + N=n, + gamma=gamma, + ) + + self.rainbow = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["darkblue"], + rels["blue"], + rels["lightblue"], + rels["green"], + rels["orange"], + rels["red"], + ), + N=n, + gamma=gamma, + ) + self.cascade = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["cascade1"], rels["cascade2"], rels["cascade3"], rels["cascade4"]), + N=n, + gamma=gamma, + ) + self.cascade_r = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["white"], + rels["cascade4"], + rels["cascade3"], + rels["cascade2"], + rels["cascade1"], + rels["green"], + ), + N=n, + gamma=gamma, + ) + self.cascade_r_nowhite = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["cascade4"], + rels["cascade3"], + rels["cascade2"], + rels["cascade1"], + rels["green"], + ), + N=n, + gamma=gamma, + ) + + self.cascade_highcontrast = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["cascade1"], + rels["cascade3"], + rels["aug1"], + rels["grey"], + rels["aug2"], + ), + N=n, + gamma=gamma, + ) + + self.circle_small = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["minus"], rels["plus"], rels["plus"], rels["minus"]), + N=n, + gamma=gamma, + ) + self.circle_small_r = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["plus"], rels["minus"], rels["minus"], rels["plus"]), + N=n, + gamma=gamma, + ) + + self.circle_medium = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["red"], rels["green"], rels["darkblue"], rels["green"], rels["red"]), + N=n, + gamma=gamma, + ) + self.circle_medium_r = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["darkblue"], + rels["green"], + rels["red"], + rels["green"], + rels["darkblue"], + ), + N=n, + gamma=gamma, + ) + + self.circle_big = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["darkblue"], + rels["blue"], + rels["orange"], + rels["red"], + rels["orange"], + rels["blue"], + ), + N=n, + gamma=gamma, + ) + self.circle_big_r = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["red"], + rels["orange"], + rels["blue"], + rels["darkblue"], + rels["blue"], + rels["orange"], + ), + N=n, + gamma=gamma, + ) + + self.circle_medium_triple = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["darkblue"], + rels["orange"], + rels["orange"], + rels["red"], + rels["red"], + rels["darkblue"], + ), + N=n, + gamma=gamma, + ) + self.circle_medium_triple_r = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["red"], + rels["darkblue"], + rels["darkblue"], + rels["orange"], + rels["orange"], + rels["red"], + ), + N=n, + gamma=gamma, + ) + + def colormaps(self, n, gamma=None): + gamma = 1 if gamma is None else gamma + self.divergingwhite = LinearSegmentedColormap.from_list( + "my_colormap", (self.plus, self.white, self.minus), N=n, gamma=gamma + ) + self.divergingwhitelarge = LinearSegmentedColormap.from_list( + "my_colormap", + ( + self.colors["darkblue"], + self.colors["blue"], + self.white, + self.colors["orange"], + self.colors["red"], + ), + N=n, + gamma=gamma, + ) + + self.whiteblue = LinearSegmentedColormap.from_list( + "my_colormap", + ( + self.white, + self.colors["lightblue"], + self.colors["blue"], + self.colors["darkblue"], + ), + N=n, + gamma=gamma, + ) + self.greyblue = LinearSegmentedColormap.from_list( + "my_colormap", + ( + self.gridcolor, + self.colors["lightblue"], + self.colors["blue"], + self.colors["darkblue"], + ), + N=n, + gamma=gamma, + ) + self.greyblue_r = LinearSegmentedColormap.from_list( + "my_colormap", + ( + self.colors["darkblue"], + self.colors["blue"], + self.colors["lightblue"], + self.gridcolor, + ), + N=n, + gamma=gamma, + ) + self.greyredorange = LinearSegmentedColormap.from_list( + "my_colormap", + (self.gridcolor, self.cascade3, self.colors["red"], self.colors["orange"]), + N=n, + gamma=gamma, + ) + + self.white_base_blue = LinearSegmentedColormap.from_list( + "my_colormap", + ( + self.white, + self.colors["darkblue"], + self.colors["blue"], + self.colors["lightblue"], + ), + N=n, + gamma=gamma, + ) + self.white_base_bluegreen = LinearSegmentedColormap.from_list( + "my_colormap", + ( + self.white, + self.colors["darkblue"], + self.colors["blue"], + self.colors["green"], + ), + N=n, + gamma=gamma, + ) + self.rainbow = LinearSegmentedColormap.from_list( + "my_colormap", + ( + self.colors["darkblue"], + self.colors["blue"], + self.colors["lightblue"], + self.colors["green"], + self.colors["orange"], + self.colors["red"], + ), + N=n, + gamma=gamma, + ) + self.cascade = LinearSegmentedColormap.from_list( + "my_colormap", + (self.cascade1, self.cascade2, self.cascade3, self.cascade4), + N=n, + gamma=gamma, + ) + self.cascade_r = LinearSegmentedColormap.from_list( + "my_colormap", + ( + self.white, + self.cascade4, + self.cascade3, + self.cascade2, + self.cascade1, + self.colors["green"], + ), + N=n, + gamma=gamma, + ) + self.cascade_r_nowhite = LinearSegmentedColormap.from_list( + "my_colormap", + ( + self.cascade4, + self.cascade3, + self.cascade2, + self.cascade1, + self.colors["green"], + ), + N=n, + gamma=gamma, + ) + + self.cascade_highcontrast = LinearSegmentedColormap.from_list( + "my_colormap", + (self.cascade1, self.cascade3, self.aug1, self.grey, self.aug2), + N=n, + gamma=gamma, + ) + + def show(self): + for key in self.__dict__.keys(): + _logger.debug("%s", key) + + _logger.debug(" rels dict keys:") + for key in self.rels.keys(): + _logger.debug(" %s", key) + + def plot(self): + dd = self.__dict__.copy() + dd_colors = dd["rels"] + del dd["rels"] + + Y = 2.0 + coldd = dd + ncolor = len(coldd) + fig = plt.figure(figsize=[2, Y]) + ax = fig.add_axes([0, 0, 0.5, Y]) + dy = 1 / ncolor + y = np.arange(0, 1, dy) + + j = 0 + for k, I in coldd.items(): + try: r1 = mpatch.Rectangle((0, y[j]), 1, dy, color=I) - txt = ax1.text(1.1, y[j]+dy*.5, ' '+k, va='center', fontsize=10, - weight='regular') - - ax1.add_patch(r1) - j+=1 - plt.title('rels') - - def add_standard_colors(self): - self.colors=dict() - self.gridcolor=(0,0,0) - - self.colors['red']=(203/255, 32/255, 39/255) - self.colors['green']=(15/255, 150/255, 72/255) - self.colors['orange']=(247/255, 191/255, 88/255) - - self.colors['grey1']=(167/255, 180/255, 183/255) - self.colors['grey']=self.colors['grey1'] - self.colors['lightgrey']=self.colors['grey1'] - self.colors['grey2']=(123/255, 121/255, 125/255) - self.colors['grey3']=(72/255, 70/255, 77/255) - - self.colors['darkblue']=(18/255, 78/255, 153/255) - self.colors['blue']=(85/255, 133/255, 196/255) - self.colors['lightblue']=(129/255, 140/255, 192/255) - - self.colors['blue4']=(7/255, 137/255, 198/255) - self.colors['black']=(0 , 0 ,0 ) - self.colors['white']=(1,1,1) - - - def default_colors(self): - self.colors=dict() - self.gridcolor=(0,0,0) - - self.colors['red']=(203/255, 32/255, 39/255) - self.colors['green']=(15/255, 150/255, 72/255) - self.colors['orange']=(247/255, 191/255, 88/255) - - self.colors['grey1']=(167/255, 180/255, 183/255) - self.colors['grey']=self.colors['grey1'] - self.colors['lightgrey']=self.colors['grey1'] - self.colors['grey2']=(123/255, 121/255, 125/255) - self.colors['grey3']=(72/255, 70/255, 77/255) - - self.colors['darkblue']=(18/255, 78/255, 153/255) - self.colors['blue']=(85/255, 133/255, 196/255) - self.colors['lightblue']=(129/255, 140/255, 192/255) - self.colors['blue4']=(7/255, 137/255, 198/255) - self.colors['black']=(0 , 0 ,0 ) - self.colors['white']=(1,1,1) - - converter={'lead1':'blue', - 'lead2': 'red', - 'lead3': 'green', - 'plus':'red', - 'minus': 'blue', - 'pair2_a':'darkblue', - 'pair2_b':'green', - 'aug1':'green', - 'aug2':'orange', - 'axiscolor':'black', - 'gridcolor':'grey2', - 'cascade1':'darkblue', - 'cascade2':'blue', - 'cascade3':'blue4', - 'cascade4':'lightblue'} - - for k,I in converter.items(): - setattr(self, k, self.colors[I]) + txt = ax.text( + 1.1, + y[j] + dy * 0.5, + " " + k, + va="center", + fontsize=10, + weight="regular", + ) + + ax.add_patch(r1) + j += 1 + except: + pass + + coldd = dd_colors + ncolor = len(coldd) + ax1 = fig.add_axes([1.5, 0, 0.5, Y]) + dy = 1 / ncolor + y = np.arange(0, 1, dy) + + j = 0 + for k, I in coldd.items(): + r1 = mpatch.Rectangle((0, y[j]), 1, dy, color=I) + txt = ax1.text( + 1.1, + y[j] + dy * 0.5, + " " + k, + va="center", + fontsize=10, + weight="regular", + ) + + ax1.add_patch(r1) + j += 1 + plt.title("rels") + + def add_standard_colors(self): + self.colors = dict() + self.gridcolor = (0, 0, 0) + + self.colors["red"] = (203 / 255, 32 / 255, 39 / 255) + self.colors["green"] = (15 / 255, 150 / 255, 72 / 255) + self.colors["orange"] = (247 / 255, 191 / 255, 88 / 255) + + self.colors["grey1"] = (167 / 255, 180 / 255, 183 / 255) + self.colors["grey"] = self.colors["grey1"] + self.colors["lightgrey"] = self.colors["grey1"] + self.colors["grey2"] = (123 / 255, 121 / 255, 125 / 255) + self.colors["grey3"] = (72 / 255, 70 / 255, 77 / 255) + + self.colors["darkblue"] = (18 / 255, 78 / 255, 153 / 255) + self.colors["blue"] = (85 / 255, 133 / 255, 196 / 255) + self.colors["lightblue"] = (129 / 255, 140 / 255, 192 / 255) + + self.colors["blue4"] = (7 / 255, 137 / 255, 198 / 255) + self.colors["black"] = (0, 0, 0) + self.colors["white"] = (1, 1, 1) + + def default_colors(self): + self.colors = dict() + self.gridcolor = (0, 0, 0) + + self.colors["red"] = (203 / 255, 32 / 255, 39 / 255) + self.colors["green"] = (15 / 255, 150 / 255, 72 / 255) + self.colors["orange"] = (247 / 255, 191 / 255, 88 / 255) + + self.colors["grey1"] = (167 / 255, 180 / 255, 183 / 255) + self.colors["grey"] = self.colors["grey1"] + self.colors["lightgrey"] = self.colors["grey1"] + self.colors["grey2"] = (123 / 255, 121 / 255, 125 / 255) + self.colors["grey3"] = (72 / 255, 70 / 255, 77 / 255) + + self.colors["darkblue"] = (18 / 255, 78 / 255, 153 / 255) + self.colors["blue"] = (85 / 255, 133 / 255, 196 / 255) + self.colors["lightblue"] = (129 / 255, 140 / 255, 192 / 255) + self.colors["blue4"] = (7 / 255, 137 / 255, 198 / 255) + self.colors["black"] = (0, 0, 0) + self.colors["white"] = (1, 1, 1) + + converter = { + "lead1": "blue", + "lead2": "red", + "lead3": "green", + "plus": "red", + "minus": "blue", + "pair2_a": "darkblue", + "pair2_b": "green", + "aug1": "green", + "aug2": "orange", + "axiscolor": "black", + "gridcolor": "grey2", + "cascade1": "darkblue", + "cascade2": "blue", + "cascade3": "blue4", + "cascade4": "lightblue", + } + + for k, I in converter.items(): + setattr(self, k, self.colors[I]) diff --git a/src/icesat2waves/local_modules/m_general_ph3.py b/src/icesat2waves/local_modules/m_general_ph3.py index a6f30335..e95343e1 100644 --- a/src/icesat2waves/local_modules/m_general_ph3.py +++ b/src/icesat2waves/local_modules/m_general_ph3.py @@ -18,6 +18,7 @@ _logger = logging.getLogger(__name__) + class Color: def __init__(self): @@ -77,7 +78,6 @@ def __init__( xsize = x_size if x_size is not None else 8 ysize = y_size if y_size is not None else 5 - self.label_letters = iter(f"{i}) " for i in string.ascii_lowercase) if container: @@ -173,7 +173,7 @@ def __init__(self, ax): class PlotSprecta: - def __init__(self, fs, Xdata, sample_unit="df", data_unit= "X"): + def __init__(self, fs, Xdata, sample_unit="df", data_unit="X"): self.fs = fs self.Xdata = Xdata @@ -341,8 +341,12 @@ def power(self, anomalie=False): self.F.ax.set_yscale("log", nonposy="clip") tt = self.time.astype(DT.datetime) - _logger.debug("Shape of tt: %s, Shape of fs: %s, Shape of dd.T: %s", - tt[:-1].shape, self.fs[:].shape, dd.T.shape) + _logger.debug( + "Shape of tt: %s, Shape of fs: %s, Shape of dd.T: %s", + tt[:-1].shape, + self.fs[:].shape, + dd.T.shape, + ) self.cs = plt.contourf(tt[:-1], self.fs[:], dd.T, self.clevs, cmap=self.cmap) self.x = np.arange(0, tt[:-1].size) _logger.debug("clevels: %s", self.clevs) @@ -478,14 +482,18 @@ def power_imshow( fn = self.fs if isinstance(tt[0], np.datetime64): - _logger.debug('time axis is numpy.datetime64, converted to number for plotting') + _logger.debug( + "time axis is numpy.datetime64, converted to number for plotting" + ) ttt = dates.date2num(tt.astype(DT.datetime)) elif isinstance(tt[0], np.timedelta64): - _logger.debug('time axis is numpy.timedelta64, converted to number for plotting') + _logger.debug( + "time axis is numpy.timedelta64, converted to number for plotting" + ) ttt = tt else: - _logger.debug('time axis is not converted') + _logger.debug("time axis is not converted") ttt = tt MT.stats_format(dd2) @@ -623,14 +631,14 @@ def linear_imshow( fn = self.fs if isinstance(tt[0], np.datetime64): - _logger.debug('numpy.datetime64') + _logger.debug("numpy.datetime64") ttt = dates.date2num(tt.astype(DT.datetime)) elif isinstance(tt[0], np.timedelta64): - _logger.debug('numpy.timedelta64') + _logger.debug("numpy.timedelta64") ttt = tt else: - _logger.debug('something else') + _logger.debug("something else") ttt = tt self.cs = plt.pcolormesh( @@ -721,7 +729,6 @@ def linear(self, radial_axis="period", circles=None, ax=None, fontsize=10): " Polar Spectrum", y=0.95, x=0.5, horizontalalignment="center" ) - # left turned postive ax.set_theta_direction(-1) ax.set_theta_zero_location("N") @@ -881,7 +888,7 @@ def save_anyfig(fig, name=None, path=None): extension = ".png" full_name = (os.path.join(savepath, name)) + extension fig.savefig(full_name, bbox_inches="tight", format="png", dpi=180) - _logger.debug('save at: %s',full_name) + _logger.debug("save at: %s", full_name) def read_cdo(file): @@ -912,7 +919,7 @@ def cut_nparray(var, low, high, verbose=False): _logger.debug("out of lower limit!") if high > var[-1]: if verbose: - _logger.debug('out of upper limit!') + _logger.debug("out of upper limit!") _logger.debug("high: %s, last var: %s", high, var[-1]) return (var >= low) & (var <= high) @@ -965,8 +972,7 @@ def boxmean(data, lon, lat, xlim, ylim): case (2, 1): datan = data[:, :, xbool][:, ybool, :] case _: - _logger.debug('arrays have not the same shape') - + _logger.debug("arrays have not the same shape") _logger.debug("new shape %s", datan.shape) @@ -1265,7 +1271,7 @@ def __init__(self, span, dt=None, unit="h"): for s in self.span: assert type(s) is int, "span is not an integrer!" - self.length = self.span[1] -self.span[0] + self.length = self.span[1] - self.span[0] self.loop_iter = np.arange(0, self.length, 1) self.index_iter = np.arange(self.span[0], self.span[1], 1) if dt: @@ -1326,8 +1332,8 @@ def corse_iter(self, dt, unit=None): .astype("float") ) span_new = np.array(self.span) * dt_format / dt - _logger.debug('old span= %s',self.span) - _logger.debug('new span= %s',span_new ) + _logger.debug("old span= %s", self.span) + _logger.debug("new span= %s", span_new) for s in span_new: span.append(int(np.floor(s))) @@ -1379,23 +1385,23 @@ def compose_ts(self, ts, name, time=None): for i in iindex: if i + span[0] < 0: - _logger.debug('i: %s span: %s %s', i, span[0], span[1]) - _logger.debug('left postion: %s', i+span[0]) + _logger.debug("i: %s span: %s %s", i, span[0], span[1]) + _logger.debug("left postion: %s", i + span[0]) raise ValueError("composite span exceeds ts limits") return -1 elif i + span[1] > ts.size: return -1 _logger.debug("%s %s %s", i, span[0], span[1]) - _logger.debug('i: %s span: %s %s', i, span[0], span[1]) - _logger.debug('right postion: %s',i+span[1]) + _logger.debug("i: %s span: %s %s", i, span[0], span[1]) + _logger.debug("right postion: %s", i + span[1]) raise ValueError("composite span exceeds ts limits") return -1 - _logger.debug('comp %s', comp.shape) - _logger.debug('ts %s', ts[i + span[0]:i + span[1]].shape) + _logger.debug("comp %s", comp.shape) + _logger.debug("ts %s", ts[i + span[0] : i + span[1]].shape) _logger.debug("%s %s %s", i, span[0], span[1]) - comp = np.vstack((comp, ts[i + span[0]:i + span[1]])) + comp = np.vstack((comp, ts[i + span[0] : i + span[1]])) comp = np.delete(comp, 0, 0) comp1 = CompositeData(comp, self.weigthing) @@ -1416,7 +1422,7 @@ def compose_2d(self, field, name, time=None): span = self.iter_operate.span if span != [0, 0]: - _logger.debug("%s %s", -span[0]+span[1],field.shape[1]) + _logger.debug("%s %s", -span[0] + span[1], field.shape[1]) comp = np.empty((-span[0] + span[1], field.shape[1])) * np.NaN self.length = -span[0] + span[1] for i in iindex: @@ -1438,7 +1444,7 @@ def compose_2d(self, field, name, time=None): comp1 = CompositeData(comp, self.weigthing) self.comp[name] = comp1 else: - _logger.debug('no span defined') + _logger.debug("no span defined") comp = field[iindex, :] comp1 = CompositeData(comp, self.weigthing) @@ -1526,12 +1532,14 @@ def gen_log_space(limit, n): def linefit2Points(time_lin, f, data, f1, f2, f_delta=None, plot=False): if isinstance(time_lin[0], np.datetime64): - _logger.debug('type is numpy.datetime64, shape %s', time_lin.shape) + _logger.debug("type is numpy.datetime64, shape %s", time_lin.shape) time_lin = time_lin.astype("m8[s]").astype(int) if f.shape[0] != data.shape[0]: _logger.error("ERROR: shapes are not correct") - _logger.error("f: %s; time_lin: %s; data: %s", f.shape, time_lin.shape, data.shape) + _logger.error( + "f: %s; time_lin: %s; data: %s", f.shape, time_lin.shape, data.shape + ) return # find neerest discrete frequency @@ -1616,12 +1624,14 @@ def find_max_along_line( if mode == "free_limits" or mode == "lower_limit": if line_right[-1] > time_lin[-1]: _logger.debug(" right line > time window") - _logger.debug("line_right[-1]: %s, time_lin[-1]: %s", line_right[-1], time_lin[-1]) + _logger.debug( + "line_right[-1]: %s, time_lin[-1]: %s", line_right[-1], time_lin[-1] + ) a = line_right - time_lin[-1] f_end = np.unravel_index(np.abs(a).argmin(), np.transpose(a.shape))[0] - 1 else: _logger.debug(" right line < time window") - _logger.debug("%s %s",line_right[-1], time_lin[-1]) + _logger.debug("%s %s", line_right[-1], time_lin[-1]) f_end = time_lin.size - 2 else: a = f - f2 diff --git a/src/icesat2waves/local_modules/m_spectrum_ph3.py b/src/icesat2waves/local_modules/m_spectrum_ph3.py index 36099aaf..dd41f2c8 100644 --- a/src/icesat2waves/local_modules/m_spectrum_ph3.py +++ b/src/icesat2waves/local_modules/m_spectrum_ph3.py @@ -27,7 +27,6 @@ def calc_freq(self): """calculate array of spectral variable (frequency or wavenumber) in cycles per unit of L""" - self.df = 1.0 / ((self.n - 1) * self.dt) self.f = ( @@ -69,7 +68,7 @@ def create_timeaxis_collection(time_as_datetime64): "day": day, "datetime": datetime, "float_plot": float_plot, - "dt64": dt64 + "dt64": dt64, } return T @@ -99,11 +98,15 @@ def spicke_remover(data, nstd=20.0, spreed=500.0, max_loops=10.0, verbose=False) data2 = M.spickes_to_mean(data2, nloop=0, spreed=spreed, gaussian=False) looper_count += 1 else: - peak_remove=False + peak_remove = False - _logger.debug("%s: %s %s %s", - f"{'True' if act_flag else 'False'}", data_std, - f" {'<' if act_flag else '>'}", max_abs_data2) + _logger.debug( + "%s: %s %s %s", + f"{'True' if act_flag else 'False'}", + data_std, + f" {'<' if act_flag else '>'}", + max_abs_data2, + ) if looper_count > max_loops: peak_remove = False @@ -166,8 +169,9 @@ def parceval(self): _logger.debug("variance of unweighted timeseries: %s", self.data.var()) _logger.debug( "variance of weighted timeseries: %s", - self.phi.var() if self.win_flag is 1 else "data not windowed") - _logger.debug("variance of weighted timeseries: %s", self.phi.var() ) + self.phi.var() if self.win_flag is 1 else "data not windowed", + ) + _logger.debug("variance of weighted timeseries: %s", self.phi.var()) _logger.debug("variance of the Spectrum: %s", self.var) @@ -499,14 +503,15 @@ def error(self, ci=0.95): def parceval(self): _logger.debug("Parcevals Theorem:") _logger.debug("variance of unweighted timeseries: %s", self.data.var()) - _logger.debug("mean variance of timeseries chunks: %s", + _logger.debug( + "mean variance of timeseries chunks: %s", ( self.chunks.var(axis=1).mean() if self.save_chunks is True else "data not saved" ), ) - _logger.debug("variance of the pwelch Spectrum: %s",self.var) + _logger.debug("variance of the pwelch Spectrum: %s", self.var) def calc_var(self): """Compute total variance from spectrum""" @@ -584,8 +589,8 @@ def __init__( n_specs = [] k = 0 - _logger.debug('subL %s', subL) - _logger.debug('L %s', L) + _logger.debug("subL %s", subL) + _logger.debug("L %s", L) _logger.debug("data_size_adjust: %s", data_size_adjust) for i in np.arange(0, data_size_adjust - int(L - ov) + 1, int(L - ov)): @@ -704,6 +709,7 @@ def write_log(self, s, verbose=False): def log(self): _logger.debug(".hist variable: %s", self.hist) + def power_anomalie(self, clim=None): dd = 10 * np.log10(self.data[:, :]) @@ -830,7 +836,6 @@ def MEM_cal(moments_est, freq, theta=None, flim=None): # Lygre and Krongstad 1986 have here sqrt(N_sel['P11'] *(N_sel['P22'] + N_sel['P33']). I guess its a typo. d2 = N_sel["Q13"] / np.sqrt(N_sel["P11"] * (N_sel["P22"] + N_sel["P33"])) - d3 = (N_sel["P22"] - N_sel["P33"]) / (N_sel["P22"] + N_sel["P33"]) d4 = 2 * N_sel["P23"] / (N_sel["P22"] + N_sel["P33"]) # define power spectrum @@ -939,8 +944,7 @@ def save_file(data, path): outfile = path with open(outfile, "wb") as f: pickle.dump(data, f, pickle.HIGHEST_PROTOCOL) - _logger.debug('saved to: %s',outfile) - + _logger.debug("saved to: %s", outfile) ## ceasars funcitons diff --git a/src/icesat2waves/local_modules/m_tools_ph3.py b/src/icesat2waves/local_modules/m_tools_ph3.py index 11b570a7..751466c8 100644 --- a/src/icesat2waves/local_modules/m_tools_ph3.py +++ b/src/icesat2waves/local_modules/m_tools_ph3.py @@ -18,12 +18,12 @@ def dt_form_timestamp(timestamp, unit="h"): - return (timestamp[1]-timestamp[0]).astype(f"m8[{unit}]") + return (timestamp[1] - timestamp[0]).astype(f"m8[{unit}]") def tick_formatter(a, interval=2, rounder=2, expt_flag=True, shift=0): - fact = 10**(int(np.log10(a.max())) - 1) + fact = 10 ** (int(np.log10(a.max())) - 1) b = np.round(a / fact, rounder + 1) * fact ticklabels = [" "] * len(b) @@ -107,9 +107,9 @@ def fake_2d_data(verbose=True, timeaxis=False): z3 = 1 / (sigma * np.sqrt(2 * np.pi)) * np.exp(-((YY - mu) ** 2) / (2 * sigma**2)) z3 = z3 / z3.max() if verbose: - _logger.debug("x %s" , x.shape) - _logger.debug("y %s" , y.shape) - _logger.debug("z %s" , z3.shape) + _logger.debug("x %s", x.shape) + _logger.debug("y %s", y.shape) + _logger.debug("z %s", z3.shape) plt.contourf(x, y, z2 / 2 + z3 / 2) plt.colorbar() @@ -129,6 +129,7 @@ def pickle_save(name, path, data, verbose=True): if verbose: _logger.debug("save at: %s", full_name) + def pickle_load(name, path, verbose=True): full_name = os.path.join(path, name + ".npy") @@ -191,7 +192,7 @@ def h5_load(name, path, verbose=False): def h5_load_v2(name, path, verbose=False): - with h5py.File(path + name + '.h5','r') as h5f: + with h5py.File(path + name + ".h5", "r") as h5f: if verbose: _logger.debug("%s h5f keys: %s", name, h5f.keys()) @@ -240,13 +241,15 @@ def save_pandas_table(table_dict, ID, save_path): def write_log(hist, string, verbose=False, short=True, date=True): _now = datetime.datetime.now() - _short = "%Y%m%d" + _short = "%Y%m%d" _long = "%Y-%m-%d %H:%M" now = _now.strftime(_short) if short else _now.strftime(_long) message = f"\n{now} {string}" if date else f"\n {string}" - if verbose in [True, 'all']: - _logger.debug("hist message: %s", hist + message if verbose == 'all' else message) + if verbose in [True, "all"]: + _logger.debug( + "hist message: %s", hist + message if verbose == "all" else message + ) return hist + message @@ -264,8 +267,8 @@ def write_variables_log(hist, var_list, locals, verbose=False, date=False): message = f"\n{now} {stringg}" if date else f"\n{' '.ljust(5)} {stringg}" - if verbose in [True, 'all']: - _logger.debug("%s", hist + message if verbose == 'all' else message) + if verbose in [True, "all"]: + _logger.debug("%s", hist + message if verbose == "all" else message) def save_log_txt(name, path, hist, verbose=False): @@ -275,7 +278,7 @@ def save_log_txt(name, path, hist, verbose=False): with open(full_name, "w") as ifile: ifile.write(str(hist)) if verbose: - _logger.debug("saved at: %s",full_name) + _logger.debug("saved at: %s", full_name) def load_log_txt(hist_file, path): @@ -314,19 +317,27 @@ def find_O(a, case="round"): def stats(a): _logger.debug( - f"shape: %s\n" - f"Nans: %s\n" - f"max: %s\n" - f"min: %s\n" - f"mean: %s" - , a.shape, np.sum(np.isnan(a)), np.nanmax(a), np.nanmin(a), np.nanmean(a)) + f"shape: %s\n" f"Nans: %s\n" f"max: %s\n" f"min: %s\n" f"mean: %s", + a.shape, + np.sum(np.isnan(a)), + np.nanmax(a), + np.nanmin(a), + np.nanmean(a), + ) def stats_format(a, name=None): - _logger.debug("Name: %s\n" - " Shape: %s\n" - " NaNs: %s\n" - " max: %s\n" - " min: %s\n" - " mean: %s", - name, a.shape, np.sum(np.isnan(a)), np.nanmax(a), np.nanmin(a), np.nanmean(a)) + _logger.debug( + "Name: %s\n" + " Shape: %s\n" + " NaNs: %s\n" + " max: %s\n" + " min: %s\n" + " mean: %s", + name, + a.shape, + np.sum(np.isnan(a)), + np.nanmax(a), + np.nanmin(a), + np.nanmean(a), + ) diff --git a/src/icesat2waves/tools/__init__.py b/src/icesat2waves/tools/__init__.py index a391f31e..8c3cde44 100644 --- a/src/icesat2waves/tools/__init__.py +++ b/src/icesat2waves/tools/__init__.py @@ -1 +1 @@ -"Utility functions for loading files/data and tools for ploting tables and figures " \ No newline at end of file +"Utility functions for loading files/data and tools for ploting tables and figures " diff --git a/src/icesat2waves/tools/angle_optimizer.py b/src/icesat2waves/tools/angle_optimizer.py index 497e835a..628345e9 100644 --- a/src/icesat2waves/tools/angle_optimizer.py +++ b/src/icesat2waves/tools/angle_optimizer.py @@ -125,7 +125,7 @@ def likelyhood_func( tot_var = z_error**2 + z_model**2 def simple_log_panelty(x, x0, sigma): - return -((x - x0) / sigma) ** 2.0 / 2.0 + return -(((x - x0) / sigma) ** 2.0) / 2.0 # try ot get prior if prior is not None: diff --git a/src/icesat2waves/tools/generalized_FT.py b/src/icesat2waves/tools/generalized_FT.py index 7ff8c2b3..d01f90c6 100644 --- a/src/icesat2waves/tools/generalized_FT.py +++ b/src/icesat2waves/tools/generalized_FT.py @@ -408,8 +408,16 @@ def calc_gFT_apply(stancil, prior): if prior[0] is False: _logger.debug("1st GFD failed (priors[0]=false), skip 2nd step") else: - _logger.debug("2nd step: use set priors of types: %s %s", type(prior[0]), type(prior[1])) - _logger.debug("first three elements of priors: %s %s", prior[0][0:3], prior[1][0:3]) + _logger.debug( + "2nd step: use set priors of types: %s %s", + type(prior[0]), + type(prior[1]), + ) + _logger.debug( + "first three elements of priors: %s %s", + prior[0][0:3], + prior[1][0:3], + ) I_return = calc_gFT_apply(ss, prior=prior) prior = I_return["PSD"], I_return["weight"] @@ -661,7 +669,9 @@ def get_stancil_var_apply(stancil): _logger.debug("Parcevals Theorem:") _logger.debug("variance of timeseries: %s", DATA.var()) _logger.debug("mean variance of stancils: %s", stancil_weighted_variance) - _logger.debug("variance of the optimzed windowed LS Spectrum: %s", self.calc_var()) + _logger.debug( + "variance of the optimzed windowed LS Spectrum: %s", self.calc_var() + ) if add_attrs: self.G.attrs["variance_unweighted_data"] = DATA.var() diff --git a/src/icesat2waves/tools/iotools.py b/src/icesat2waves/tools/iotools.py index 51146c47..8e342930 100644 --- a/src/icesat2waves/tools/iotools.py +++ b/src/icesat2waves/tools/iotools.py @@ -69,7 +69,9 @@ def init_data(ID_name, batch_key, ID_flag, ID_root, prefix="A01b_ID"): """ - _logger.debug("id name: %s, batch key: %s, id flag: %s", ID_name, batch_key, ID_flag) + _logger.debug( + "id name: %s, batch key: %s, id flag: %s", ID_name, batch_key, ID_flag + ) hemis, batch = batch_key.split("_") if ID_flag: @@ -187,7 +189,7 @@ def set_ATL03_trackname(self): def set_ATL10_trackname(self): block1 = (self.YY, self.MM, self.DD) block1b = (self.HH, self.MN, self.SS) - block2 = (self.TRK, self.CYC, "01") # granule is always '01' for ATL10 + block2 = (self.TRK, self.CYC, "01") # granule is always '01' for ATL10 if self.RL is "": raise ValueError("RL not set") if self.VRS is "": diff --git a/src/icesat2waves/tools/lanczos.py b/src/icesat2waves/tools/lanczos.py index 530b4e32..35209e9c 100644 --- a/src/icesat2waves/tools/lanczos.py +++ b/src/icesat2waves/tools/lanczos.py @@ -1,6 +1,6 @@ import numpy as np import scipy.signal as signal -from scipy.ndimage import convolve +from scipy.ndimage import convolve import scipy.ndimage.filters as filters From b1b6119c5b23eccaa7ab8b07d6e0827d8c0e6af0 Mon Sep 17 00:00:00 2001 From: John Gerrard Holland Date: Fri, 15 Mar 2024 10:49:35 -0400 Subject: [PATCH 2/5] Apply suggestions from code review Co-authored-by: Carlos Paniagua --- src/icesat2waves/tools/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/icesat2waves/tools/__init__.py b/src/icesat2waves/tools/__init__.py index 8c3cde44..1a4fd3d4 100644 --- a/src/icesat2waves/tools/__init__.py +++ b/src/icesat2waves/tools/__init__.py @@ -1 +1 @@ -"Utility functions for loading files/data and tools for ploting tables and figures " +"""Utility functions for loading files/data and tools for plotting tables and figures. """ From ef9e9f153511f5fc4f2a06f61bf62d4452b5bea4 Mon Sep 17 00:00:00 2001 From: John Gerrard Holland Date: Fri, 15 Mar 2024 11:28:59 -0400 Subject: [PATCH 3/5] chore: run black again --- src/icesat2waves/local_modules/m_general_ph3.py | 4 +++- src/icesat2waves/local_modules/m_tools_ph3.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/icesat2waves/local_modules/m_general_ph3.py b/src/icesat2waves/local_modules/m_general_ph3.py index d5414aa8..8150a7e4 100644 --- a/src/icesat2waves/local_modules/m_general_ph3.py +++ b/src/icesat2waves/local_modules/m_general_ph3.py @@ -1624,7 +1624,9 @@ def find_max_along_line( f_start = np.unravel_index(np.abs(a).argmin(), np.transpose(a.shape))[0] if mode == "free_limits" or mode == "lower_limit": - _logger.debug("line_right[-1]: %s, time_lin[-1]: %s", line_right[-1], time_lin[-1]) + _logger.debug( + "line_right[-1]: %s, time_lin[-1]: %s", line_right[-1], time_lin[-1] + ) if line_right[-1] > time_lin[-1]: _logger.debug(" right line > time window") a = line_right - time_lin[-1] diff --git a/src/icesat2waves/local_modules/m_tools_ph3.py b/src/icesat2waves/local_modules/m_tools_ph3.py index 2aed85a5..8b57739b 100644 --- a/src/icesat2waves/local_modules/m_tools_ph3.py +++ b/src/icesat2waves/local_modules/m_tools_ph3.py @@ -268,7 +268,9 @@ def write_variables_log(hist, var_list, locals, verbose=False, date=False): message = f"\n{now} {stringg}" if date else f"\n{' '.ljust(5)} {stringg}" if verbose in [True, "all"]: - _logger.debug("write_variables_log: %s", hist + message if verbose == "all" else message) + _logger.debug( + "write_variables_log: %s", hist + message if verbose == "all" else message + ) def save_log_txt(name, path, hist, verbose=False): From f97a97ba12843f70e2348bd34d9afee76fdb3f63 Mon Sep 17 00:00:00 2001 From: John Gerrard Holland Date: Fri, 15 Mar 2024 12:47:20 -0400 Subject: [PATCH 4/5] chore: run black again --- .../local_modules/m_colormanager_ph3.py | 781 ++++++++++++------ 1 file changed, 539 insertions(+), 242 deletions(-) diff --git a/src/icesat2waves/local_modules/m_colormanager_ph3.py b/src/icesat2waves/local_modules/m_colormanager_ph3.py index 9bb03267..3d1c24fc 100644 --- a/src/icesat2waves/local_modules/m_colormanager_ph3.py +++ b/src/icesat2waves/local_modules/m_colormanager_ph3.py @@ -14,23 +14,27 @@ def ase_to_json(path): os.getcwd() import swatch - A=swatch.parse(path) - for i in A[0]['swatches']: - _logger.debug("swatch name: %s, swatch values: %s", i['name'], i['data']['values']) + A = swatch.parse(path) + + for i in A[0]["swatches"]: + _logger.debug( + "swatch name: %s, swatch values: %s", i["name"], i["data"]["values"] + ) return A def json_save(name, path, data, return_name=False): import json + if not os.path.exists(path): os.makedirs(path) - full_name_root=os.path.join(path,name) - full_name= (os.path.join(full_name_root+ '.json')) - with open(full_name, 'w') as outfile: + full_name_root = os.path.join(path, name) + full_name = os.path.join(full_name_root + ".json") + with open(full_name, "w") as outfile: json.dump(data, outfile) - _logger.debug('save at %s:', full_name) + _logger.debug("save at %s:", full_name) if return_name: return full_name_root else: @@ -39,245 +43,538 @@ def json_save(name, path, data, return_name=False): def json_load(name, path): import json - full_name= (os.path.join(path,name+ '.json')) - - with open(full_name, 'r') as ifile: - data=json.load(ifile) - _logger.debug('loaded from: %s', full_name) - return data + full_name = os.path.join(path, name + ".json") + with open(full_name, "r") as ifile: + data = json.load(ifile) + _logger.debug("loaded from: %s", full_name) + return data class color: - def __init__(self, path=None, name=None): - self.white=(1,1,1) - if (path is not None) & (name is not None): - _logger.debug('color theme: %s', name) - try: - theme=json_load(name, path) - for k, v in theme.items(): - setattr(self, k, v) - except: - _logger.debug('fail load theme, fall back to default theme') - _logger.debug("path: %s%s", path, 'mhell_colortheme17') - self.default_colors() - - else: + def __init__(self, path=None, name=None): + self.white = (1, 1, 1) + if (path is not None) & (name is not None): + _logger.debug("color theme: %s", name) + try: + theme = json_load(name, path) + for k, v in theme.items(): + setattr(self, k, v) + except: + _logger.debug("fail load theme, fall back to default theme") + _logger.debug("path: %s%s", path, "mhell_colortheme17") self.default_colors() - def alpha(self, color, a): - return ( color[0], color[1], color[2], a) - - - def colormaps2(self,n, gamma=None): - gamma=1 if gamma is None else gamma - - rels = self.rels - self.divergingwhite=LinearSegmentedColormap.from_list("my_colormap", (rels['plus'],rels['white'], rels['minus']), N=n, gamma=gamma) - self.divergingwhitelarge=LinearSegmentedColormap.from_list("my_colormap", - (rels['darkblue'], rels['blue'],rels['white'], rels['orange'], rels['red']), N=n, gamma=gamma) - - self.whiteblue=LinearSegmentedColormap.from_list("my_colormap", - (rels['white'],rels['lightblue'], rels['blue'],rels['darkblue']), N=n, gamma=gamma) - self.greyblue=LinearSegmentedColormap.from_list("my_colormap", - (rels['gridcolor'],rels['lightblue'], rels['blue'],rels['darkblue']), N=n, gamma=gamma) - self.greyblue_r=LinearSegmentedColormap.from_list("my_colormap", - (rels['darkblue'] , rels['blue'], rels['lightblue'], rels['gridcolor'] ), N=n, gamma=gamma) - - self.greyred=LinearSegmentedColormap.from_list("my_colormap", - (rels['gridcolor'], rels['rascade1'],rels['rascade2']), N=n, gamma=gamma) - self.greyred_r=LinearSegmentedColormap.from_list("my_colormap", - (rels['rascade3'] , rels['rascade2'], rels['rascade1'], rels['gridcolor'] ), N=n, gamma=gamma) - - self.greyredorange=LinearSegmentedColormap.from_list("my_colormap", - (rels['gridcolor'],rels['cascade3'], rels['red'],rels['orange']), N=n, gamma=gamma) - - self.white_base_blue=LinearSegmentedColormap.from_list("my_colormap", - (rels['white'], rels['darkblue'], rels['blue'],rels['lightblue']), N=n, gamma=gamma) - self.white_base_blue_r=LinearSegmentedColormap.from_list("my_colormap", - (rels['white'], rels['lightblue'], rels['blue'],rels['darkblue']), N=n, gamma=gamma) - - self.white_base_bluegreen=LinearSegmentedColormap.from_list("my_colormap", - (rels['white'], rels['darkblue'], rels['blue'],rels['green']), N=n, gamma=gamma) - self.white_base_bluegreen_r=LinearSegmentedColormap.from_list("my_colormap", - (rels['white'], rels['lightblue'], rels['blue'],rels['darkblue'],rels['group2']), N=n, gamma=gamma) - self.white_base_blgror=LinearSegmentedColormap.from_list("my_colormap", #rels['blue'], - (rels['white'], rels['gridcolor'] ,rels['cascade2'], rels['cascade1'],rels['group1'] ), N=n, gamma=gamma) - - - self.rainbow=LinearSegmentedColormap.from_list("my_colormap", - (rels['darkblue'], rels['blue'], rels['lightblue'], rels['green'],rels['orange'], rels['red'] ), N=n, gamma=gamma) - self.cascade=LinearSegmentedColormap.from_list("my_colormap", - (rels['cascade1'], rels['cascade2'], rels['cascade3'], rels['cascade4'] ), N=n, gamma=gamma) - self.cascade_r=LinearSegmentedColormap.from_list("my_colormap", - (rels['white'], rels['cascade4'], rels['cascade3'], rels['cascade2'], rels['cascade1'], rels['green'] ), N=n, gamma=gamma) - self.cascade_r_nowhite=LinearSegmentedColormap.from_list("my_colormap", - (rels['cascade4'], rels['cascade3'], rels['cascade2'], rels['cascade1'], rels['green'] ), N=n, gamma=gamma) - - self.cascade_highcontrast=LinearSegmentedColormap.from_list("my_colormap", - (rels['cascade1'], rels['cascade3'], rels['aug1'], rels['grey'], rels['aug2'] ), N=n, gamma=gamma) - - - self.circle_small=LinearSegmentedColormap.from_list("my_colormap", - (rels['minus'], rels['plus'],rels['plus'], rels['minus']), N=n, gamma=gamma) - self.circle_small_r=LinearSegmentedColormap.from_list("my_colormap", - (rels['plus'],rels['minus'], rels['minus'], rels['plus']), N=n, gamma=gamma) - - self.circle_medium=LinearSegmentedColormap.from_list("my_colormap", - (rels['red'],rels['green'], rels['darkblue'],rels['green'], rels['red']), N=n, gamma=gamma) - self.circle_medium_r=LinearSegmentedColormap.from_list("my_colormap", - (rels['darkblue'],rels['green'], rels['red'], rels['green'], rels['darkblue']), N=n, gamma=gamma) - - - self.circle_big=LinearSegmentedColormap.from_list("my_colormap", - (rels['darkblue'], rels['blue'], rels['orange'], rels['red'], rels['orange'], rels['blue']), N=n, gamma=gamma) - self.circle_big_r=LinearSegmentedColormap.from_list("my_colormap", - (rels['red'], rels['orange'], rels['blue'], rels['darkblue'], rels['blue'], rels['orange']), N=n, gamma=gamma) - - self.circle_medium_triple=LinearSegmentedColormap.from_list("my_colormap", - (rels['darkblue'], rels['orange'], rels['orange'] , rels['red'], rels['red'], rels['darkblue']), N=n, gamma=gamma) - self.circle_medium_triple_r=LinearSegmentedColormap.from_list("my_colormap", - (rels['red'], rels['darkblue'], rels['darkblue'] , rels['orange'], rels['orange'],rels['red']), N=n, gamma=gamma) - - def colormaps(self,n, gamma=None): - gamma=1 if gamma is None else gamma - self.divergingwhite=LinearSegmentedColormap.from_list("my_colormap", (self.plus,self.white, self.minus), N=n, gamma=gamma) - self.divergingwhitelarge=LinearSegmentedColormap.from_list("my_colormap", - (self.colors['darkblue'], self.colors['blue'],self.white, self.colors['orange'], self.colors['red']), N=n, gamma=gamma) - - self.whiteblue=LinearSegmentedColormap.from_list("my_colormap", - (self.white,self.colors['lightblue'], self.colors['blue'],self.colors['darkblue']), N=n, gamma=gamma) - self.greyblue=LinearSegmentedColormap.from_list("my_colormap", - (self.gridcolor,self.colors['lightblue'], self.colors['blue'],self.colors['darkblue']), N=n, gamma=gamma) - self.greyblue_r=LinearSegmentedColormap.from_list("my_colormap", - (self.colors['darkblue'] , self.colors['blue'], self.colors['lightblue'], self.gridcolor ), N=n, gamma=gamma) - self.greyredorange=LinearSegmentedColormap.from_list("my_colormap", - (self.gridcolor,self.cascade3, self.colors['red'],self.colors['orange']), N=n, gamma=gamma) - - self.white_base_blue=LinearSegmentedColormap.from_list("my_colormap", - (self.white, self.colors['darkblue'], self.colors['blue'],self.colors['lightblue']), N=n, gamma=gamma) - self.white_base_bluegreen=LinearSegmentedColormap.from_list("my_colormap", - (self.white, self.colors['darkblue'], self.colors['blue'],self.colors['green']), N=n, gamma=gamma) - self.rainbow=LinearSegmentedColormap.from_list("my_colormap", - (self.colors['darkblue'], self.colors['blue'], self.colors['lightblue'], self.colors['green'],self.colors['orange'], self.colors['red'] ), N=n, gamma=gamma) - self.cascade=LinearSegmentedColormap.from_list("my_colormap", - (self.cascade1, self.cascade2, self.cascade3, self.cascade4 ), N=n, gamma=gamma) - self.cascade_r=LinearSegmentedColormap.from_list("my_colormap", - (self.white, self.cascade4, self.cascade3, self.cascade2, self.cascade1, self.colors['green'] ), N=n, gamma=gamma) - self.cascade_r_nowhite=LinearSegmentedColormap.from_list("my_colormap", - (self.cascade4, self.cascade3, self.cascade2, self.cascade1, self.colors['green'] ), N=n, gamma=gamma) - - self.cascade_highcontrast=LinearSegmentedColormap.from_list("my_colormap", - (self.cascade1, self.cascade3, self.aug1, self.grey, self.aug2 ), N=n, gamma=gamma) - - def show(self): - for key in self.__dict__.keys(): - _logger.debug("key: %s", key) - - _logger.debug(' rels dict keys:') - for key in self.rels.keys(): - _logger.debug(" %s", key) - - def plot(self): - dd=self.__dict__.copy() - dd_colors=dd['rels'] - del dd['rels'] - - Y=2.0 - coldd=dd - ncolor=len(coldd) - fig = plt.figure(figsize=[2, Y]) - ax = fig.add_axes([0, 0, .5, Y]) - dy=1/ncolor - y=np.arange(0, 1, dy) - - j=0 - for k,I in coldd.items(): - try: - r1 = mpatch.Rectangle((0, y[j]), 1, dy, color=I) - txt = ax.text(1.1, y[j]+dy*.5, ' '+k, va='center', fontsize=10, - weight='regular') - - ax.add_patch(r1) - j+=1 - except: - pass - - coldd=dd_colors - ncolor=len(coldd) - ax1 = fig.add_axes([1.5, 0, .5, Y]) - dy=1/ncolor - y=np.arange(0, 1, dy) - - j=0 - for k,I in coldd.items(): + + else: + self.default_colors() + + def alpha(self, color, a): + return (color[0], color[1], color[2], a) + + def colormaps2(self, n, gamma=None): + gamma = 1 if gamma is None else gamma + + rels = self.rels + self.divergingwhite = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["plus"], rels["white"], rels["minus"]), + N=n, + gamma=gamma, + ) + self.divergingwhitelarge = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["darkblue"], + rels["blue"], + rels["white"], + rels["orange"], + rels["red"], + ), + N=n, + gamma=gamma, + ) + + self.whiteblue = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["white"], rels["lightblue"], rels["blue"], rels["darkblue"]), + N=n, + gamma=gamma, + ) + self.greyblue = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["gridcolor"], rels["lightblue"], rels["blue"], rels["darkblue"]), + N=n, + gamma=gamma, + ) + self.greyblue_r = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["darkblue"], rels["blue"], rels["lightblue"], rels["gridcolor"]), + N=n, + gamma=gamma, + ) + + self.greyred = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["gridcolor"], rels["rascade1"], rels["rascade2"]), + N=n, + gamma=gamma, + ) + self.greyred_r = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["rascade3"], rels["rascade2"], rels["rascade1"], rels["gridcolor"]), + N=n, + gamma=gamma, + ) + + self.greyredorange = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["gridcolor"], rels["cascade3"], rels["red"], rels["orange"]), + N=n, + gamma=gamma, + ) + + self.white_base_blue = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["white"], rels["darkblue"], rels["blue"], rels["lightblue"]), + N=n, + gamma=gamma, + ) + self.white_base_blue_r = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["white"], rels["lightblue"], rels["blue"], rels["darkblue"]), + N=n, + gamma=gamma, + ) + + self.white_base_bluegreen = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["white"], rels["darkblue"], rels["blue"], rels["green"]), + N=n, + gamma=gamma, + ) + self.white_base_bluegreen_r = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["white"], + rels["lightblue"], + rels["blue"], + rels["darkblue"], + rels["group2"], + ), + N=n, + gamma=gamma, + ) + self.white_base_blgror = LinearSegmentedColormap.from_list( + "my_colormap", # rels['blue'], + ( + rels["white"], + rels["gridcolor"], + rels["cascade2"], + rels["cascade1"], + rels["group1"], + ), + N=n, + gamma=gamma, + ) + + self.rainbow = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["darkblue"], + rels["blue"], + rels["lightblue"], + rels["green"], + rels["orange"], + rels["red"], + ), + N=n, + gamma=gamma, + ) + self.cascade = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["cascade1"], rels["cascade2"], rels["cascade3"], rels["cascade4"]), + N=n, + gamma=gamma, + ) + self.cascade_r = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["white"], + rels["cascade4"], + rels["cascade3"], + rels["cascade2"], + rels["cascade1"], + rels["green"], + ), + N=n, + gamma=gamma, + ) + self.cascade_r_nowhite = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["cascade4"], + rels["cascade3"], + rels["cascade2"], + rels["cascade1"], + rels["green"], + ), + N=n, + gamma=gamma, + ) + + self.cascade_highcontrast = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["cascade1"], + rels["cascade3"], + rels["aug1"], + rels["grey"], + rels["aug2"], + ), + N=n, + gamma=gamma, + ) + + self.circle_small = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["minus"], rels["plus"], rels["plus"], rels["minus"]), + N=n, + gamma=gamma, + ) + self.circle_small_r = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["plus"], rels["minus"], rels["minus"], rels["plus"]), + N=n, + gamma=gamma, + ) + + self.circle_medium = LinearSegmentedColormap.from_list( + "my_colormap", + (rels["red"], rels["green"], rels["darkblue"], rels["green"], rels["red"]), + N=n, + gamma=gamma, + ) + self.circle_medium_r = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["darkblue"], + rels["green"], + rels["red"], + rels["green"], + rels["darkblue"], + ), + N=n, + gamma=gamma, + ) + + self.circle_big = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["darkblue"], + rels["blue"], + rels["orange"], + rels["red"], + rels["orange"], + rels["blue"], + ), + N=n, + gamma=gamma, + ) + self.circle_big_r = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["red"], + rels["orange"], + rels["blue"], + rels["darkblue"], + rels["blue"], + rels["orange"], + ), + N=n, + gamma=gamma, + ) + + self.circle_medium_triple = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["darkblue"], + rels["orange"], + rels["orange"], + rels["red"], + rels["red"], + rels["darkblue"], + ), + N=n, + gamma=gamma, + ) + self.circle_medium_triple_r = LinearSegmentedColormap.from_list( + "my_colormap", + ( + rels["red"], + rels["darkblue"], + rels["darkblue"], + rels["orange"], + rels["orange"], + rels["red"], + ), + N=n, + gamma=gamma, + ) + + def colormaps(self, n, gamma=None): + gamma = 1 if gamma is None else gamma + self.divergingwhite = LinearSegmentedColormap.from_list( + "my_colormap", (self.plus, self.white, self.minus), N=n, gamma=gamma + ) + self.divergingwhitelarge = LinearSegmentedColormap.from_list( + "my_colormap", + ( + self.colors["darkblue"], + self.colors["blue"], + self.white, + self.colors["orange"], + self.colors["red"], + ), + N=n, + gamma=gamma, + ) + + self.whiteblue = LinearSegmentedColormap.from_list( + "my_colormap", + ( + self.white, + self.colors["lightblue"], + self.colors["blue"], + self.colors["darkblue"], + ), + N=n, + gamma=gamma, + ) + self.greyblue = LinearSegmentedColormap.from_list( + "my_colormap", + ( + self.gridcolor, + self.colors["lightblue"], + self.colors["blue"], + self.colors["darkblue"], + ), + N=n, + gamma=gamma, + ) + self.greyblue_r = LinearSegmentedColormap.from_list( + "my_colormap", + ( + self.colors["darkblue"], + self.colors["blue"], + self.colors["lightblue"], + self.gridcolor, + ), + N=n, + gamma=gamma, + ) + self.greyredorange = LinearSegmentedColormap.from_list( + "my_colormap", + (self.gridcolor, self.cascade3, self.colors["red"], self.colors["orange"]), + N=n, + gamma=gamma, + ) + + self.white_base_blue = LinearSegmentedColormap.from_list( + "my_colormap", + ( + self.white, + self.colors["darkblue"], + self.colors["blue"], + self.colors["lightblue"], + ), + N=n, + gamma=gamma, + ) + self.white_base_bluegreen = LinearSegmentedColormap.from_list( + "my_colormap", + ( + self.white, + self.colors["darkblue"], + self.colors["blue"], + self.colors["green"], + ), + N=n, + gamma=gamma, + ) + self.rainbow = LinearSegmentedColormap.from_list( + "my_colormap", + ( + self.colors["darkblue"], + self.colors["blue"], + self.colors["lightblue"], + self.colors["green"], + self.colors["orange"], + self.colors["red"], + ), + N=n, + gamma=gamma, + ) + self.cascade = LinearSegmentedColormap.from_list( + "my_colormap", + (self.cascade1, self.cascade2, self.cascade3, self.cascade4), + N=n, + gamma=gamma, + ) + self.cascade_r = LinearSegmentedColormap.from_list( + "my_colormap", + ( + self.white, + self.cascade4, + self.cascade3, + self.cascade2, + self.cascade1, + self.colors["green"], + ), + N=n, + gamma=gamma, + ) + self.cascade_r_nowhite = LinearSegmentedColormap.from_list( + "my_colormap", + ( + self.cascade4, + self.cascade3, + self.cascade2, + self.cascade1, + self.colors["green"], + ), + N=n, + gamma=gamma, + ) + + self.cascade_highcontrast = LinearSegmentedColormap.from_list( + "my_colormap", + (self.cascade1, self.cascade3, self.aug1, self.grey, self.aug2), + N=n, + gamma=gamma, + ) + + def show(self): + for key in self.__dict__.keys(): + _logger.debug("key: %s", key) + + _logger.debug(" rels dict keys:") + for key in self.rels.keys(): + _logger.debug(" %s", key) + + def plot(self): + dd = self.__dict__.copy() + dd_colors = dd["rels"] + del dd["rels"] + + Y = 2.0 + coldd = dd + ncolor = len(coldd) + fig = plt.figure(figsize=[2, Y]) + ax = fig.add_axes([0, 0, 0.5, Y]) + dy = 1 / ncolor + y = np.arange(0, 1, dy) + + j = 0 + for k, I in coldd.items(): + try: r1 = mpatch.Rectangle((0, y[j]), 1, dy, color=I) - txt = ax1.text(1.1, y[j]+dy*.5, ' '+k, va='center', fontsize=10, - weight='regular') - - ax1.add_patch(r1) - j+=1 - plt.title('rels') - - def add_standard_colors(self): - self.colors=dict() - self.gridcolor=(0,0,0) - - self.colors['red']=(203/255, 32/255, 39/255) - self.colors['green']=(15/255, 150/255, 72/255) - self.colors['orange']=(247/255, 191/255, 88/255) - - self.colors['grey1']=(167/255, 180/255, 183/255) - self.colors['grey']=self.colors['grey1'] - self.colors['lightgrey']=self.colors['grey1'] - self.colors['grey2']=(123/255, 121/255, 125/255) - self.colors['grey3']=(72/255, 70/255, 77/255) - - self.colors['darkblue']=(18/255, 78/255, 153/255) - self.colors['blue']=(85/255, 133/255, 196/255) - self.colors['lightblue']=(129/255, 140/255, 192/255) - - self.colors['blue4']=(7/255, 137/255, 198/255) - self.colors['black']=(0 , 0 ,0 ) - self.colors['white']=(1,1,1) - - - def default_colors(self): - self.colors=dict() - self.gridcolor=(0,0,0) - - self.colors['red']=(203/255, 32/255, 39/255) - self.colors['green']=(15/255, 150/255, 72/255) - self.colors['orange']=(247/255, 191/255, 88/255) - - self.colors['grey1']=(167/255, 180/255, 183/255) - self.colors['grey']=self.colors['grey1'] - self.colors['lightgrey']=self.colors['grey1'] - self.colors['grey2']=(123/255, 121/255, 125/255) - self.colors['grey3']=(72/255, 70/255, 77/255) - - self.colors['darkblue']=(18/255, 78/255, 153/255) - self.colors['blue']=(85/255, 133/255, 196/255) - self.colors['lightblue']=(129/255, 140/255, 192/255) - self.colors['blue4']=(7/255, 137/255, 198/255) - self.colors['black']=(0 , 0 ,0 ) - self.colors['white']=(1,1,1) - - converter={'lead1':'blue', - 'lead2': 'red', - 'lead3': 'green', - 'plus':'red', - 'minus': 'blue', - 'pair2_a':'darkblue', - 'pair2_b':'green', - 'aug1':'green', - 'aug2':'orange', - 'axiscolor':'black', - 'gridcolor':'grey2', - 'cascade1':'darkblue', - 'cascade2':'blue', - 'cascade3':'blue4', - 'cascade4':'lightblue'} - - for k,I in converter.items(): - setattr(self, k, self.colors[I]) + txt = ax.text( + 1.1, + y[j] + dy * 0.5, + " " + k, + va="center", + fontsize=10, + weight="regular", + ) + + ax.add_patch(r1) + j += 1 + except: + pass + + coldd = dd_colors + ncolor = len(coldd) + ax1 = fig.add_axes([1.5, 0, 0.5, Y]) + dy = 1 / ncolor + y = np.arange(0, 1, dy) + + j = 0 + for k, I in coldd.items(): + r1 = mpatch.Rectangle((0, y[j]), 1, dy, color=I) + txt = ax1.text( + 1.1, + y[j] + dy * 0.5, + " " + k, + va="center", + fontsize=10, + weight="regular", + ) + + ax1.add_patch(r1) + j += 1 + plt.title("rels") + + def add_standard_colors(self): + self.colors = dict() + self.gridcolor = (0, 0, 0) + + self.colors["red"] = (203 / 255, 32 / 255, 39 / 255) + self.colors["green"] = (15 / 255, 150 / 255, 72 / 255) + self.colors["orange"] = (247 / 255, 191 / 255, 88 / 255) + + self.colors["grey1"] = (167 / 255, 180 / 255, 183 / 255) + self.colors["grey"] = self.colors["grey1"] + self.colors["lightgrey"] = self.colors["grey1"] + self.colors["grey2"] = (123 / 255, 121 / 255, 125 / 255) + self.colors["grey3"] = (72 / 255, 70 / 255, 77 / 255) + + self.colors["darkblue"] = (18 / 255, 78 / 255, 153 / 255) + self.colors["blue"] = (85 / 255, 133 / 255, 196 / 255) + self.colors["lightblue"] = (129 / 255, 140 / 255, 192 / 255) + + self.colors["blue4"] = (7 / 255, 137 / 255, 198 / 255) + self.colors["black"] = (0, 0, 0) + self.colors["white"] = (1, 1, 1) + + def default_colors(self): + self.colors = dict() + self.gridcolor = (0, 0, 0) + + self.colors["red"] = (203 / 255, 32 / 255, 39 / 255) + self.colors["green"] = (15 / 255, 150 / 255, 72 / 255) + self.colors["orange"] = (247 / 255, 191 / 255, 88 / 255) + + self.colors["grey1"] = (167 / 255, 180 / 255, 183 / 255) + self.colors["grey"] = self.colors["grey1"] + self.colors["lightgrey"] = self.colors["grey1"] + self.colors["grey2"] = (123 / 255, 121 / 255, 125 / 255) + self.colors["grey3"] = (72 / 255, 70 / 255, 77 / 255) + + self.colors["darkblue"] = (18 / 255, 78 / 255, 153 / 255) + self.colors["blue"] = (85 / 255, 133 / 255, 196 / 255) + self.colors["lightblue"] = (129 / 255, 140 / 255, 192 / 255) + self.colors["blue4"] = (7 / 255, 137 / 255, 198 / 255) + self.colors["black"] = (0, 0, 0) + self.colors["white"] = (1, 1, 1) + + converter = { + "lead1": "blue", + "lead2": "red", + "lead3": "green", + "plus": "red", + "minus": "blue", + "pair2_a": "darkblue", + "pair2_b": "green", + "aug1": "green", + "aug2": "orange", + "axiscolor": "black", + "gridcolor": "grey2", + "cascade1": "darkblue", + "cascade2": "blue", + "cascade3": "blue4", + "cascade4": "lightblue", + } + + for k, I in converter.items(): + setattr(self, k, self.colors[I]) From 58eaef3a0322b2003928bb9371baa6df48f7d75f Mon Sep 17 00:00:00 2001 From: John Gerrard Holland Date: Tue, 19 Mar 2024 15:32:19 +0100 Subject: [PATCH 5/5] update formatting --- .../local_modules/m_general_ph3.py | 74 +++++++++++-------- src/icesat2waves/local_modules/m_tools_ph3.py | 39 ++++++---- 2 files changed, 67 insertions(+), 46 deletions(-) diff --git a/src/icesat2waves/local_modules/m_general_ph3.py b/src/icesat2waves/local_modules/m_general_ph3.py index ce6381fa..05542371 100644 --- a/src/icesat2waves/local_modules/m_general_ph3.py +++ b/src/icesat2waves/local_modules/m_general_ph3.py @@ -18,6 +18,7 @@ _logger = logging.getLogger(__name__) + class Color: def __init__(self): @@ -77,7 +78,6 @@ def __init__( xsize = x_size if x_size is not None else 8 ysize = y_size if y_size is not None else 5 - self.label_letters = iter(f"{i}) " for i in string.ascii_lowercase) if container: @@ -173,7 +173,7 @@ def __init__(self, ax): class PlotSprecta: - def __init__(self, fs, Xdata, sample_unit="df", data_unit= "X"): + def __init__(self, fs, Xdata, sample_unit="df", data_unit="X"): self.fs = fs self.Xdata = Xdata @@ -341,8 +341,12 @@ def power(self, anomalie=False): self.F.ax.set_yscale("log", nonposy="clip") tt = self.time.astype(DT.datetime) - _logger.debug("Shape of tt: %s, Shape of fs: %s, Shape of dd.T: %s", - tt[:-1].shape, self.fs[:].shape, dd.T.shape) + _logger.debug( + "Shape of tt: %s, Shape of fs: %s, Shape of dd.T: %s", + tt[:-1].shape, + self.fs[:].shape, + dd.T.shape, + ) self.cs = plt.contourf(tt[:-1], self.fs[:], dd.T, self.clevs, cmap=self.cmap) self.x = np.arange(0, tt[:-1].size) _logger.debug("clevels: %s", self.clevs) @@ -478,14 +482,18 @@ def power_imshow( fn = self.fs if isinstance(tt[0], np.datetime64): - _logger.debug('time axis is numpy.datetime64, converted to number for plotting') + _logger.debug( + "time axis is numpy.datetime64, converted to number for plotting" + ) ttt = dates.date2num(tt.astype(DT.datetime)) elif isinstance(tt[0], np.timedelta64): - _logger.debug('time axis is numpy.timedelta64, converted to number for plotting') + _logger.debug( + "time axis is numpy.timedelta64, converted to number for plotting" + ) ttt = tt else: - _logger.debug('time axis is not converted') + _logger.debug("time axis is not converted") ttt = tt MT.stats_format(dd2) @@ -623,14 +631,14 @@ def linear_imshow( fn = self.fs if isinstance(tt[0], np.datetime64): - _logger.debug('numpy.datetime64') + _logger.debug("numpy.datetime64") ttt = dates.date2num(tt.astype(DT.datetime)) elif isinstance(tt[0], np.timedelta64): - _logger.debug('numpy.timedelta64') + _logger.debug("numpy.timedelta64") ttt = tt else: - _logger.debug('something else') + _logger.debug("something else") ttt = tt self.cs = plt.pcolormesh( @@ -721,7 +729,6 @@ def linear(self, radial_axis="period", circles=None, ax=None, fontsize=10): " Polar Spectrum", y=0.95, x=0.5, horizontalalignment="center" ) - # left turned postive ax.set_theta_direction(-1) ax.set_theta_zero_location("N") @@ -881,7 +888,7 @@ def save_anyfig(fig, name=None, path=None): extension = ".png" full_name = (os.path.join(savepath, name)) + extension fig.savefig(full_name, bbox_inches="tight", format="png", dpi=180) - _logger.debug('save at: %s',full_name) + _logger.debug("save at: %s", full_name) def read_cdo(file): @@ -912,7 +919,7 @@ def cut_nparray(var, low, high, verbose=False): _logger.debug("out of lower limit!") if high > var[-1]: if verbose: - _logger.debug('out of upper limit!') + _logger.debug("out of upper limit!") _logger.debug("high: %s, last var: %s", high, var[-1]) return (var >= low) & (var <= high) @@ -965,8 +972,7 @@ def boxmean(data, lon, lat, xlim, ylim): case (2, 1): datan = data[:, :, xbool][:, ybool, :] case _: - _logger.debug('arrays do not have the same shape') - + _logger.debug("arrays do not have the same shape") _logger.debug("new shape %s", datan.shape) @@ -1265,7 +1271,7 @@ def __init__(self, span, dt=None, unit="h"): for s in self.span: assert type(s) is int, "span is not an integrer!" - self.length = self.span[1] -self.span[0] + self.length = self.span[1] - self.span[0] self.loop_iter = np.arange(0, self.length, 1) self.index_iter = np.arange(self.span[0], self.span[1], 1) if dt: @@ -1326,8 +1332,8 @@ def corse_iter(self, dt, unit=None): .astype("float") ) span_new = np.array(self.span) * dt_format / dt - _logger.debug('old span= %s',self.span) - _logger.debug('new span= %s',span_new ) + _logger.debug("old span= %s", self.span) + _logger.debug("new span= %s", span_new) for s in span_new: span.append(int(np.floor(s))) @@ -1379,22 +1385,22 @@ def compose_ts(self, ts, name, time=None): for i in iindex: if i + span[0] < 0: - _logger.debug('i: %s span: %s %s', i, span[0], span[1]) - _logger.debug('left postion: %s', i+span[0]) + _logger.debug("i: %s span: %s %s", i, span[0], span[1]) + _logger.debug("left postion: %s", i + span[0]) raise ValueError("composite span exceeds ts limits") return -1 elif i + span[1] > ts.size: return -1 - _logger.debug('i: %s span: %s %s', i, span[0], span[1]) - _logger.debug('right postion: %s',i+span[1]) + _logger.debug("i: %s span: %s %s", i, span[0], span[1]) + _logger.debug("right postion: %s", i + span[1]) raise ValueError("composite span exceeds ts limits") return -1 - _logger.debug('comp %s', comp.shape) - _logger.debug('ts %s', ts[i + span[0]:i + span[1]].shape) - _logger.debug('i: %s span: %s %s', i, span[0], span[1]) - comp = np.vstack((comp, ts[i + span[0]:i + span[1]])) + _logger.debug("comp %s", comp.shape) + _logger.debug("ts %s", ts[i + span[0] : i + span[1]].shape) + _logger.debug("i: %s span: %s %s", i, span[0], span[1]) + comp = np.vstack((comp, ts[i + span[0] : i + span[1]])) comp = np.delete(comp, 0, 0) comp1 = CompositeData(comp, self.weigthing) @@ -1415,7 +1421,9 @@ def compose_2d(self, field, name, time=None): span = self.iter_operate.span if span != [0, 0]: - _logger.debug("span range: %s, field shape: %s", -span[0]+span[1],field.shape[1]) + _logger.debug( + "span range: %s, field shape: %s", -span[0] + span[1], field.shape[1] + ) comp = np.empty((-span[0] + span[1], field.shape[1])) * np.NaN self.length = -span[0] + span[1] for i in iindex: @@ -1437,7 +1445,7 @@ def compose_2d(self, field, name, time=None): comp1 = CompositeData(comp, self.weigthing) self.comp[name] = comp1 else: - _logger.debug('no span defined') + _logger.debug("no span defined") comp = field[iindex, :] comp1 = CompositeData(comp, self.weigthing) @@ -1525,12 +1533,14 @@ def gen_log_space(limit, n): def linefit2Points(time_lin, f, data, f1, f2, f_delta=None, plot=False): if isinstance(time_lin[0], np.datetime64): - _logger.debug('type is numpy.datetime64, shape %s', time_lin.shape) + _logger.debug("type is numpy.datetime64, shape %s", time_lin.shape) time_lin = time_lin.astype("m8[s]").astype(int) if f.shape[0] != data.shape[0]: _logger.error("ERROR: shapes are not correct") - _logger.error("f: %s; time_lin: %s; data: %s", f.shape, time_lin.shape, data.shape) + _logger.error( + "f: %s; time_lin: %s; data: %s", f.shape, time_lin.shape, data.shape + ) return # find neerest discrete frequency @@ -1615,7 +1625,9 @@ def find_max_along_line( f_start = np.unravel_index(np.abs(a).argmin(), np.transpose(a.shape))[0] if mode == "free_limits" or mode == "lower_limit": - _logger.debug("line_right[-1]: %s, time_lin[-1]: %s", line_right[-1], time_lin[-1]) + _logger.debug( + "line_right[-1]: %s, time_lin[-1]: %s", line_right[-1], time_lin[-1] + ) if line_right[-1] > time_lin[-1]: _logger.debug(" right line > time window") a = line_right - time_lin[-1] diff --git a/src/icesat2waves/local_modules/m_tools_ph3.py b/src/icesat2waves/local_modules/m_tools_ph3.py index 23b911fc..3fdb0ac6 100644 --- a/src/icesat2waves/local_modules/m_tools_ph3.py +++ b/src/icesat2waves/local_modules/m_tools_ph3.py @@ -18,12 +18,12 @@ def dt_form_timestamp(timestamp, unit="h"): - return (timestamp[1]-timestamp[0]).astype(f"m8[{unit}]") + return (timestamp[1] - timestamp[0]).astype(f"m8[{unit}]") def tick_formatter(a, interval=2, rounder=2, expt_flag=True, shift=0): - fact = 10**(int(np.log10(a.max())) - 1) + fact = 10 ** (int(np.log10(a.max())) - 1) b = np.round(a / fact, rounder + 1) * fact ticklabels = [" "] * len(b) @@ -107,9 +107,9 @@ def fake_2d_data(verbose=True, timeaxis=False): z3 = 1 / (sigma * np.sqrt(2 * np.pi)) * np.exp(-((YY - mu) ** 2) / (2 * sigma**2)) z3 = z3 / z3.max() if verbose: - _logger.debug("x %s" , x.shape) - _logger.debug("y %s" , y.shape) - _logger.debug("z %s" , z3.shape) + _logger.debug("x %s", x.shape) + _logger.debug("y %s", y.shape) + _logger.debug("z %s", z3.shape) plt.contourf(x, y, z2 / 2 + z3 / 2) plt.colorbar() @@ -129,6 +129,7 @@ def pickle_save(name, path, data, verbose=True): if verbose: _logger.debug("save at: %s", full_name) + def pickle_load(name, path, verbose=True): full_name = os.path.join(path, name + ".npy") @@ -191,7 +192,7 @@ def h5_load(name, path, verbose=False): def h5_load_v2(name, path, verbose=False): - with h5py.File(path + name + '.h5','r') as h5f: + with h5py.File(path + name + ".h5", "r") as h5f: if verbose: _logger.debug("%s h5f keys: %s", name, h5f.keys()) @@ -240,13 +241,15 @@ def save_pandas_table(table_dict, ID, save_path): def write_log(hist, string, verbose=False, short=True, date=True): _now = datetime.datetime.now() - _short = "%Y%m%d" + _short = "%Y%m%d" _long = "%Y-%m-%d %H:%M" now = _now.strftime(_short) if short else _now.strftime(_long) message = f"\n{now} {string}" if date else f"\n {string}" - if verbose in [True, 'all']: - _logger.debug("hist message: %s", hist + message if verbose == 'all' else message) + if verbose in [True, "all"]: + _logger.debug( + "hist message: %s", hist + message if verbose == "all" else message + ) return hist + message @@ -264,8 +267,10 @@ def write_variables_log(hist, var_list, locals, verbose=False, date=False): message = f"\n{now} {stringg}" if date else f"\n{' '.ljust(5)} {stringg}" - if verbose in [True, 'all']: - _logger.debug("write_variables_log: %s", hist + message if verbose == 'all' else message) + if verbose in [True, "all"]: + _logger.debug( + "write_variables_log: %s", hist + message if verbose == "all" else message + ) def save_log_txt(name, path, hist, verbose=False): @@ -275,7 +280,7 @@ def save_log_txt(name, path, hist, verbose=False): with open(full_name, "w") as ifile: ifile.write(str(hist)) if verbose: - _logger.debug("saved at: %s",full_name) + _logger.debug("saved at: %s", full_name) def load_log_txt(hist_file, path): @@ -319,11 +324,10 @@ def stats(a): np.sum(np.isnan(a)), np.nanmax(a), np.nanmin(a), - np.nanmean(a) + np.nanmean(a), ) - def stats_format(a, name=None): _logger.debug( "Name: %s\n" @@ -332,5 +336,10 @@ def stats_format(a, name=None): " max: %s\n" " min: %s\n" " mean: %s", - name, a.shape, np.sum(np.isnan(a)), np.nanmax(a), np.nanmin(a), np.nanmean(a) + name, + a.shape, + np.sum(np.isnan(a)), + np.nanmax(a), + np.nanmin(a), + np.nanmean(a), )