From feb3d38a485f8ea660dc2d5b60768e7c958d7b32 Mon Sep 17 00:00:00 2001 From: Camilo Diaz Date: Sun, 3 Mar 2024 03:10:56 -0500 Subject: [PATCH] applied suggesteds changhes --- .../local_modules/JONSWAP_gamma.py | 13 +--- .../local_modules/m_general_ph3.py | 73 +++++++++---------- .../local_modules/m_spectrum_ph3.py | 1 - .../local_modules/m_tools_ph3.py | 28 ++----- 4 files changed, 41 insertions(+), 74 deletions(-) diff --git a/src/icesat2_tracks/local_modules/JONSWAP_gamma.py b/src/icesat2_tracks/local_modules/JONSWAP_gamma.py index 861c544a..c45b0821 100644 --- a/src/icesat2_tracks/local_modules/JONSWAP_gamma.py +++ b/src/icesat2_tracks/local_modules/JONSWAP_gamma.py @@ -1,15 +1,8 @@ -import os -from scipy.constants import g - -if __name__ == "__main__": - exec(open(os.environ["PYTHONSTARTUP"]).read()) - exec(open(STARTUP_2019_DP).read()) - import sys, imp - - from lmfit import minimize, Parameters - import copy +from scipy.constants import g +from lmfit import minimize, Parameters +import copy import matplotlib.pyplot as plt import numpy as np diff --git a/src/icesat2_tracks/local_modules/m_general_ph3.py b/src/icesat2_tracks/local_modules/m_general_ph3.py index 60f3661b..afa9e26f 100644 --- a/src/icesat2_tracks/local_modules/m_general_ph3.py +++ b/src/icesat2_tracks/local_modules/m_general_ph3.py @@ -124,7 +124,7 @@ def label(self, x="x", y="y", t=None): def save(self, name=None, path=None, verbose=True): savepath = ( path - if path is not None + if path else os.path.join(os.path.dirname(os.path.realpath("__file__")), "plot/") ) if not os.path.exists(savepath): @@ -140,7 +140,7 @@ def save_pup(self, name=None, path=None, verbose=True): name = re.sub("\.", "_", name) savepath = ( path - if path is not None + if path else os.path.join(os.path.dirname(os.path.realpath("__file__")), "plot/") ) if not os.path.exists(savepath): @@ -215,12 +215,7 @@ def power_linear(self, Color="b", fax="f"): plt.ylabel(("Power db(" + self.data_unit + "^2/" + self.sample_unit + ")")) plt.xlabel(xlabelstr) - # plt.set_xlabel(fax) - # plt.xlim(np.log(fax[1]) ,np.log(fax[-1])) plt.xlim(xax[1], xax[-1]) - # self.F.ax.set_xscale('log') - # self.F.ax.semilogx() - # ax1.set_xticks([20, 300, 500]) self.F.make_clear() plt.grid() @@ -267,22 +262,22 @@ def __init__( fs, data, clevs=None, - sample_unit=None, - data_unit=None, + sample_unit="df", + data_unit="X", ylim=None, - time_unit=None, - cmap=None, + time_unit="dt", + cmap=plt.cm.ocean_r, ): self.fs = fs[1:] self.time = time self.data = data[:, 1:] self.clevs = clevs - self.sample_unit = sample_unit if sample_unit is not None else "df" + self.sample_unit = sample_unit - self.data_unit = data_unit if data_unit is not None else "X" - self.time_unit = time_unit if time_unit is not None else "dt" + self.data_unit = data_unit + self.time_unit = time_unit - self.cmap = cmap if cmap is not None else plt.cm.ocean_r + self.cmap = cmap self.ylim = ylim if ylim is not None else [fs[0], fs[-1]] def loglog(self): @@ -340,7 +335,7 @@ def power(self, anomalie=False): self.F = FigureAxisXY(10, 4, fig_scale=2) dd = 10 * np.log10(self.data[:-1, :]) - if anomalie is True: + if anomalie: dd_tmp = dd.mean(axis=0).repeat(self.time.size - 1) dd = dd - dd_tmp.reshape(self.fs.size, self.time.size - 1).T dd = dd @@ -423,8 +418,8 @@ def power_imshow( cbar=True, ): - shading = "gouraud" if shading is True else "flat" - fig_size = [10, 4] if fig_size is None else fig_size + shading = "gouraud" if shading else "flat" + fig_size = [10, 4] if fig_size else fig_size if ax: assert type(ax) is tuple, "put ax as tuple ax=(ax,F)" self.F = ax[1] @@ -433,12 +428,12 @@ def power_imshow( self.F = FigureAxisXY(fig_size[0], fig_size[1], fig_scale=2) ax_local = self.F.ax - if nopower is True: + if nopower: dd = self.data else: dd = 10 * np.log10(self.data[:-1, :]) - if anomalie is True: + if anomalie: dd_tmp = dd.mean(axis=0).repeat(self.time.size - 1) dd = dd - dd_tmp.reshape(self.fs.size, self.time.size - 1).T @@ -474,13 +469,13 @@ def power_imshow( dd2 = ddn fn = self.fs[fsn_p] - if nopower is True: + if nopower: tt = tt else: tt = tt[:-1] else: - if nopower is True: + if nopower: tt = tt else: tt = tt[:-1] @@ -503,7 +498,7 @@ def power_imshow( ttt, fn, dd2, cmap=self.cmap, norm=norm, shading=shading ) plt.ylabel(("f (" + self.sample_unit + ")")) - if cbar is True: + if cbar: self.cbar = plt.colorbar(self.cs, pad=0.01) self.cbar.ax.aspect = 100 self.cbar.outline.set_linewidth(0) @@ -570,8 +565,8 @@ def linear_imshow( ax=None, ): - shading = "gouraud" if shading is True else "flat" - fig_size = [10, 4] if fig_size is None else fig_size + shading = "gouraud" if shading else "flat" + fig_size = [10, 4] if fig_size else fig_size if ax: assert type(ax) is tuple, "put ax as tuple ax=(ax,F)" self.F = ax[1] @@ -580,12 +575,12 @@ def linear_imshow( self.F = FigureAxisXY(fig_size[0], fig_size[1], fig_scale=2) ax_local = self.F.ax - if nopower is True: + if nopower: dd = self.data else: dd = 10 * np.log10(self.data[:-1, :]) - if anomalie is True: + if anomalie: dd_tmp = dd.mean(axis=0).repeat(self.time.size - 1) dd = dd - dd_tmp.reshape(self.fs.size, self.time.size - 1).T @@ -621,13 +616,13 @@ def linear_imshow( dd2 = ddn fn = self.fs[fsn_p] - if nopower is True: + if nopower: tt = tt else: tt = tt[:-1] else: - if nopower is True: + if nopower: tt = tt else: tt = tt[:-1] @@ -697,14 +692,14 @@ def set_xaxis_to_days(self, **kwargs): class PlotPolarspectra: def __init__( - self, f, thetas, data, unit=None, data_type="fraction", lims=None, verbose=False + self, f, thetas, data, unit="X", data_type="fraction", lims=None, verbose=False ): self.f = f self.data = data self.thetas = thetas - self.unit = unit if unit is not None else "X" + self.unit = unit # decided on freq limit lims = [self.f.min(), self.f.max()] if lims is None else lims @@ -815,7 +810,7 @@ def plot_scatter_2d(intersect_chain, slope_chain, xname="intersect", yname="slop plt.xlabel(xname) -def set_timeaxis_days(ax, int1=1, int2=2, bymonthday=None): +def set_timeaxis_days(ax, int1=1, int2=2, bymonthday=range(1, 32)): # int1 interval of the major (labeld) days # int2 intercal of the minor (only ticks) days @@ -868,9 +863,7 @@ def easy_dtstr(a): return str(a.astype("timedelta64[Y]")) -def clevels(data, dstep=None, symmetric=False): - dstep = dstep if dstep is not None else 21 - +def clevels(data, dstep=21, symmetric=False): mmax = np.ceil(np.nanmax(data)) mmin = np.floor(np.nanmin(data)) @@ -909,14 +902,14 @@ def read_cdo(file): def build_timestamp(time, unit, start, verbose=True): timestamp = np.datetime64(start) + time[:].astype("m8[" + unit + "]") - if verbose is True: + if verbose: print(timestamp) return timestamp def build_timestamp_v2(time, unit, start, verbose=True): timestamp = np.datetime64(start) + time[:].astype("datetime64[s]") - if verbose is True: + if verbose: print(timestamp) return timestamp @@ -1146,7 +1139,7 @@ def find_max_ts( data = np.copy(data_org) spreed = 2 if spreed is None else spreed - if smooth is True: + if smooth: data = runningmean(data, spreed) if threshold is not None and threshold > np.nanmin(data): @@ -1285,7 +1278,7 @@ def __init__(self, span, dt=None, unit=None): self.length = -self.span[0] + self.span[1] self.loop_iter = np.arange(0, self.length, 1) self.index_iter = np.arange(self.span[0], self.span[1], 1) - if dt is not None: + if dt: self.dt = dt self.time_iter = self.index_iter * dt time_str = [] @@ -1751,7 +1744,7 @@ def RAMSAC_regression_bootstrap(time, freq, time_lin_arg=None, plot=False, **kwa """ # NOTE: This function is not called in production. sklearn.bootstrap was depecrated and - # the implementation changed. + # the implementation needs to be changed. import sklearn.bootstrap as boot # might not work in python 3 RAMS_slope, RAMS_intercept = simple_RAMSAC_regression_estimator(time, freq) diff --git a/src/icesat2_tracks/local_modules/m_spectrum_ph3.py b/src/icesat2_tracks/local_modules/m_spectrum_ph3.py index e8390472..49c5eb44 100644 --- a/src/icesat2_tracks/local_modules/m_spectrum_ph3.py +++ b/src/icesat2_tracks/local_modules/m_spectrum_ph3.py @@ -914,7 +914,6 @@ def GetMem(a1,b1,a2,b2, theta): # calculate MEM using 1 deg resolution a = theta.T - #a = np.arange(1,361).T a = a*np.pi/180. a = np.reshape(a,[1,len(a)]) diff --git a/src/icesat2_tracks/local_modules/m_tools_ph3.py b/src/icesat2_tracks/local_modules/m_tools_ph3.py index b726cfec..4482424e 100644 --- a/src/icesat2_tracks/local_modules/m_tools_ph3.py +++ b/src/icesat2_tracks/local_modules/m_tools_ph3.py @@ -1,4 +1,3 @@ - import os import matplotlib.pyplot as plt import numpy as np @@ -25,7 +24,7 @@ def tick_formatter(a, interval=2, rounder=2, expt_flag=True, shift=0): fact = 10 ** (O - 1) b = np.round(a / fact, rounder + 1) * fact ticklabels = [" " for i in range(len(b))] - + tt = np.arange(shift, len(b), interval) for t in tt: @@ -54,7 +53,7 @@ def num_to_str(flt): def mkdirs_r(path): - + if not os.path.exists(path): os.makedirs(path) @@ -62,10 +61,7 @@ def mkdirs_r(path): def check_year(inputstr, yearstring): a = np.datetime64(inputstr).astype(object).year ref = np.datetime64(yearstring).astype(object).year - if a == ref: - return True - else: - return False + return a == ref def datetime64_to_sec(d): @@ -89,15 +85,12 @@ def sec_to_dt64(pp): def sec_to_float_plot(pp): - return dates.date2num(pp.astype("M8[s]").astype(datetime)) def sec_to_float_plot_single(pp): - return dates.date2num( - np.datetime64(int(pp), "s").astype("M8[s]").astype(datetime) - ) + return dates.date2num(np.datetime64(int(pp), "s").astype("M8[s]").astype(datetime)) def fake_2d_data(verbose=True, timeaxis=False): @@ -150,7 +143,6 @@ def pickle_load(name, path, verbose=True): def json_save(name, path, data, verbose=False, return_name=False): - if not os.path.exists(path): os.makedirs(path) @@ -198,14 +190,12 @@ def json_load(name, path, verbose=False): def h5_load(name, path, verbose=False): - full_name = os.path.join(path, name + ".h5") data = pd.read_hdf(full_name) return data def h5_load_v2(name, path, verbose=False): - h5f = h5py.File(path + name + ".h5", "r") if verbose: @@ -256,7 +246,6 @@ def h5_save(name, path, data_dict, verbose=False, mode="w"): def load_pandas_table_dict(name, save_path): - warnings.filterwarnings("ignore", category=PerformanceWarning) return_dict = dict() @@ -272,8 +261,6 @@ def save_pandas_table(table_dict, ID, save_path): if not os.path.exists(save_path): os.makedirs(save_path) - - warnings.filterwarnings("ignore", category=PerformanceWarning) with HDFStore(save_path + "/" + ID + ".h5") as store: @@ -282,7 +269,6 @@ def save_pandas_table(table_dict, ID, save_path): def write_log(hist, string, verbose=False, short=True, date=True): - if short: now = datetime.now().strftime("%Y%m%d") @@ -305,7 +291,6 @@ def add_line_var(ss, name, var): def write_variables_log(hist, var_list, locals, verbose=False, date=False): - now = datetime.now().strftime("%Y%m%d") @@ -336,10 +321,7 @@ def save_log_txt(name, path, hist, verbose=False): print("saved at: ", full_name) -def load_log_txt(name, path): - - - hist_file = name +def load_log_txt(hist_file, path): f = [] for h in glob.glob(os.path.join(path, hist_file)): f.append(open(h, "r").read())