Skip to content

Commit

Permalink
applied suggesteds changhes
Browse files Browse the repository at this point in the history
  • Loading branch information
kmilo9999 committed Mar 3, 2024
1 parent a2e8daa commit feb3d38
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 74 deletions.
13 changes: 3 additions & 10 deletions src/icesat2_tracks/local_modules/JONSWAP_gamma.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
73 changes: 33 additions & 40 deletions src/icesat2_tracks/local_modules/m_general_ph3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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

Expand Down Expand Up @@ -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]
Expand All @@ -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)
Expand Down Expand Up @@ -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]
Expand All @@ -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

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/icesat2_tracks/local_modules/m_spectrum_ph3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)])

Expand Down
28 changes: 5 additions & 23 deletions src/icesat2_tracks/local_modules/m_tools_ph3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import os
import matplotlib.pyplot as plt
import numpy as np
Expand All @@ -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:
Expand Down Expand Up @@ -54,18 +53,15 @@ def num_to_str(flt):


def mkdirs_r(path):

if not os.path.exists(path):
os.makedirs(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):
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand All @@ -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:
Expand All @@ -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")
Expand All @@ -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")

Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit feb3d38

Please sign in to comment.