Skip to content

Commit

Permalink
remove all the print
Browse files Browse the repository at this point in the history
  • Loading branch information
MaceKuailv committed May 23, 2023
1 parent d3739f6 commit b3b0ef7
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 61 deletions.
13 changes: 5 additions & 8 deletions seaduck/get_masks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
import logging
import warnings

import numpy as np
Expand Down Expand Up @@ -129,7 +130,7 @@ def get_masks(od, tp):
keys = od._ds.keys()
if "maskC" not in keys:
warnings.warn("no maskC in the dataset, assuming nothing is masked.")
print("no maskC in the dataset, assuming nothing is masked.")
logging.warning("no maskC in the dataset, assuming nothing is masked.")
# od._ds.C_GRID_VARIABLE.to_masked_array().mask
maskC = np.ones_like(od._ds.XC + od._ds.Z)
# it is inappropriate to fill in the dataset,
Expand All @@ -143,22 +144,22 @@ def get_masks(od, tp):
"or you could set knw.ignore_mask = True"
)
if "maskU" not in keys:
print("creating maskU,this is going to be very slow!")
logging.info("creating maskU,this is going to be very slow!")
maskU = mask_u_node(maskC, tp)
od._ds["maskU"] = od._ds["Z"] + od._ds["XG"]
od._ds["maskU"].values = maskU
else:
maskU = np.array(od._ds["maskU"])
if "maskV" not in keys:
print("creating maskV,this is going to be very slow!")
logging.info("creating maskV,this is going to be very slow!")
maskV = mask_v_node(maskC, tp)
od._ds["maskV"] = od._ds["Z"] + od._ds["YG"]
od._ds["maskV"].values = maskV
else:
maskV = np.array(od._ds["maskV"])
if "maskWvel" not in keys:
# there is a maskW with W meaning West in ECCO
print("creating maskW,this is going to be somewhat slow")
logging.info("creating maskW,this is going to be somewhat slow")
maskW = mask_w_node(maskC)
od._ds["maskWvel"] = od._ds["Z"] + od._ds["YC"]
od._ds["maskWvel"].values = maskW
Expand Down Expand Up @@ -187,8 +188,6 @@ def get_masked(od, ind, gridtype="C"):
keys = od._ds.keys()
if "maskC" not in keys:
warnings.warn("no maskC in the dataset, assuming nothing is masked.")
# print('no maskC in the dataset, assuming nothing is masked.')
# od._ds.C_GRID_VARIABLE.to_masked_array().mask
return np.ones_like(ind[0])
elif gridtype == "C":
return smart_read(od._ds.maskC, ind)
Expand All @@ -206,12 +205,10 @@ def get_masked(od, ind, gridtype="C"):
small_mask = func_dic[gridtype](maskC, tp)
dims = tuple(map(rename_dic[gridtype], od._ds.maskC.dims))
sizes = tuple([len(od._ds[dim]) for dim in dims])
# print(sizes)
mask = np.zeros(sizes)
# indexing sensitive
old_size = small_mask.shape
slices = tuple([slice(0, i) for i in old_size])
# print(ind_str)
mask[slices] = small_mask
od._ds[name] = xr.DataArray(mask, dims=dims)
return mask[ind]
Expand Down
16 changes: 6 additions & 10 deletions seaduck/kernel_weight.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import copy
import logging

import numpy as np

from seaduck.runtime_conf import compileable, rcParam
from seaduck.runtime_conf import compileable
from seaduck.utils import get_combination

# default kernel for interpolation.
Expand Down Expand Up @@ -612,8 +613,7 @@ def get_func(kernel, hkernel="interp", h_order=0):

layer_3 = layer_2.get(h_order)
if layer_3 is None:
if rcParam["debug_level"] == "very_high":
print("Creating new weight function," " the first time is going to be slow")
logging.info("Creating new weight function, the first time is going to be slow")
layer_2[h_order] = kernel_weight(kernel, ktype=hkernel, order=h_order)
layer_3 = layer_2[h_order]

Expand Down Expand Up @@ -686,13 +686,9 @@ def __init__(
# Avoid points having same distance
ksort_inv = ksort.argsort()

if (
(inheritance is not None)
and (ignore_mask)
and (rcParam["debug_level"] == "very_high")
):
print(
"Warning:overwriting the inheritance object to None,"
if (inheritance is not None) and (ignore_mask):
logging.info(
"Overwriting the inheritance object to None,"
" because we ignore masking"
)

Expand Down
48 changes: 22 additions & 26 deletions seaduck/lagrangian.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import copy
import logging
import warnings

import numpy as np

Expand Down Expand Up @@ -528,73 +530,69 @@ def _out_of_bound(self): # pragma: no cover
z_out = False
return np.logical_or(np.logical_or(x_out, y_out), z_out)

def trim(self, verbose=False, tol=1e-6):
def trim(self, tol=1e-6):
"""Move the particles from outside the cell into the cell.
At the same time change the velocity accordingly.
In the mean time, creating some negiligible error in time.
**Parameters:**
+ verbose: Boolean
Whether to dump the maximum value trimmed.
Only set True if something is not going correctly
+ tol: float
The relative tolerance when particles is significantly
close to the cell.
"""
# tol = 1e-6 # about 10 m horizontal for 1 degree
if verbose: # pragma: no cover
if logging.DEBUG >= logging.root.level: # pragma: no cover
xmax = np.nanmax(self.rx)
xmin = np.nanmin(self.rx)
ymax = np.nanmax(self.ry)
ymin = np.nanmin(self.ry)

logging.debug(f"converting {xmax} to 0.5")
logging.debug(f"converting {xmin} to -0.5")
logging.debug(f"converting {ymax} to 0.5")
logging.debug(f"converting {ymin} to -0.5")

if self.rzl_lin is not None:
zmax = np.nanmax(self.rzl_lin)
zmin = np.nanmin(self.rzl_lin)
logging.debug(f"converting {zmax} to 1")
logging.debug(f"converting {zmin} to 0")

# if xmax>=0.5-tol:
where = self.rx >= 0.5 - tol
cdx = (0.5 - tol) - self.rx[where]
self.rx[where] += cdx
self.u[where] += self.du[where] * cdx
if verbose: # pragma: no cover
print(f"converting {xmax} to 0.5")
# if xmin<=-0.5+tol:
where = self.rx <= -0.5 + tol
cdx = (-0.5 + tol) - self.rx[where]
self.rx[where] += cdx
self.u[where] += self.du[where] * cdx
if verbose: # pragma: no cover
print(f"converting {xmin} to -0.5")
# if ymax>=0.5-tol:
where = self.ry >= 0.5 - tol
cdx = (0.5 - tol) - self.ry[where]
self.ry[where] += cdx
self.v[where] += self.dv[where] * cdx
if verbose: # pragma: no cover
print(f"converting {ymax} to 0.5")
# if ymin<=-0.5+tol:
where = self.ry <= -0.5 + tol
cdx = (-0.5 + tol) - self.ry[where]
self.ry[where] += cdx
self.v[where] += self.dv[where] * cdx
if verbose: # pragma: no cover
print(f"converting {ymin} to -0.5")
if self.rzl_lin is not None:
zmax = np.nanmax(self.rzl_lin)
zmin = np.nanmin(self.rzl_lin)
np.nanmax(self.rzl_lin)
np.nanmin(self.rzl_lin)
# if zmax>=1.-tol:
where = self.rzl_lin >= 1.0 - tol
cdx = (1.0 - tol) - self.rzl_lin[where]
self.rzl_lin[where] += cdx
self.w[where] += self.dw[where] * cdx
if verbose: # pragma: no cover
print(f"converting {zmax} to 1")
# if zmin<=-0.+tol:
where = self.rzl_lin <= -0.0 + tol
cdx = (-0.0 + tol) - self.rzl_lin[where]
self.rzl_lin[where] += cdx
self.w[where] += self.dw[where] * cdx
if verbose: # pragma: no cover
print(f"converting {zmin} to 0")

def _contract(self): # pragma: no cover
"""Warp time to move particle into cell.
Expand Down Expand Up @@ -931,7 +929,7 @@ def to_next_stop(self, t1):
elif i > 10:
trim_tol = 1e-10
self.trim(tol=trim_tol)
print(sum(todo), "left", " ", end="\r")
logging.debug(sum(todo), "left")
self.analytical_step(tf, todo)
self.update_after_cell_change()
if self.transport:
Expand All @@ -946,10 +944,9 @@ def to_next_stop(self, t1):
if self.save_raw:
# record those who cross the wall
self.note_taking(todo)
# self._contract()
print(sum(todo), "left", " ", end=" ")
if i == self.max_iteration - 1:
print("maximum iteration count reached")

if i == self.max_iteration - 1: # pragma: no cover
warnings.warn("maximum iteration count reached")
self.t = np.ones(self.N) * t1
if self.ocedata.readiness["time"]:
self.it, self.rt, self.dt, self.bt = self.ocedata.find_rel_t(self.t)
Expand Down Expand Up @@ -1023,7 +1020,7 @@ def to_list_of_time(
self.get_u_du()
R = []
for i, tl in enumerate(stops):
print(np.datetime64(round(tl), "s"))
logging.info(np.datetime64(round(tl), "s"))
if self.save_raw:
# save the very start of everything.
self.note_taking()
Expand All @@ -1042,5 +1039,4 @@ def to_list_of_time(
R.append(self.deepcopy())
if self.save_raw:
self.empty_lists()
print()
return stops, R
18 changes: 11 additions & 7 deletions seaduck/ocedata.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

import numpy as np

try: # pragma: no cover
Expand Down Expand Up @@ -182,7 +184,9 @@ def hgrid2array(self):
"""
way = self.readiness["h"]
if self.too_large: # pragma: no cover
print("Loading grid into memory, it's a large dataset please be patient")
logging.warning(
"Loading grid into memory, it's a large dataset please be patient"
)

if way == "oceanparcel":
for var in ["XC", "YC", "XG", "YG"]:
Expand All @@ -191,7 +195,7 @@ def hgrid2array(self):
try:
self[var] = np.array(self[var]).astype("float32")
except KeyError:
print(f"no {var} in dataset, skip")
logging.info(f"no {var} in dataset, skip")
self[var] = None
try:
self.dX = np.array(self["dXG"]).astype("float32")
Expand All @@ -200,10 +204,10 @@ def hgrid2array(self):
self.dX = None
self.dY = None
if self.too_large: # pragma: no cover
print("numpy arrays of grid loaded into memory")
logging.info("numpy arrays of grid loaded into memory")
self.tree = create_tree(self.XC, self.YC)
if self.too_large: # pragma: no cover
print("cKD created")
logging.info("cKD created")

if way == "local_cartesian":
for var in ["XC", "YC", "CS", "SN"]:
Expand All @@ -216,13 +220,13 @@ def hgrid2array(self):
try:
self[var] = np.array(self[var]).astype("float32")
except KeyError:
print(f"no {var} in dataset, skip")
logging.info(f"no {var} in dataset, skip")
self[var] = None
if self.too_large: # pragma: no cover
print("numpy arrays of grid loaded into memory")
logging.info("numpy arrays of grid loaded into memory")
self.tree = create_tree(self.XC, self.YC)
if self.too_large: # pragma: no cover
print("cKD created")
logging.info("cKD created")

if way == "rectilinear":
self.lon = np.array(self["lon"]).astype("float32")
Expand Down
7 changes: 3 additions & 4 deletions seaduck/runtime_conf.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
rcParam = {
"compilable": True,
"debug_level": "not that high",
"dump_masks_to_local": False,
}

try: # pragma: no cover
from numba import njit

useJIT = True
rcParam["compilable"] = True
except ImportError: # pragma: no cover
useJIT = False
rcParam["compilable"] = False


def compileable(func): # pragma: no cover
if useJIT:
if rcParam["compilable"]:
return njit(func)
else:
return func
5 changes: 2 additions & 3 deletions seaduck/smart_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def smart_read(da, ind, memory_chunk=3, xarray_more_efficient=100):
+ values: numpy.ndarray
The values of the points of interest. Has the same shape as the elements in ind.
"""
# print('read called')
the_shape = ind[0].shape
ind = tuple([i.ravel() for i in ind])
if len(da.dims) != len(ind):
Expand Down Expand Up @@ -58,7 +57,7 @@ def smart_read(da, ind, memory_chunk=3, xarray_more_efficient=100):
ckus, inverse = np.unique(ckbl, axis=0, return_inverse=True)
# ckus is the individual chunks used
if len(ckus) <= xarray_more_efficient:
# print('use smart')
# logging.debug('use smart')
for i, k in enumerate(ckus):
ind_str = []
pre = []
Expand All @@ -75,6 +74,6 @@ def smart_read(da, ind, memory_chunk=3, xarray_more_efficient=100):
result[which] = npck[subind]
return result.reshape(the_shape)
else:
# print('use xarray')
# logging.debug('use xarray')
xrind = tuple([xr.DataArray(dim, dims=["x"]) for dim in ind])
return np.array(da[xrind]).reshape(the_shape)
7 changes: 4 additions & 3 deletions seaduck/topology.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import copy
import logging

import numpy as np

from seaduck.runtime_conf import compileable, rcParam
from seaduck.runtime_conf import compileable

# If you have encountered a NotImplementedError and come to this file,
# I suggest you read the ***class topology*** near the bottom of this file.
Expand Down Expand Up @@ -521,8 +522,8 @@ def ind_tend_vec(self, inds, tend, **kwarg):
particle_on_edge = True
n_ind = ind
inds[:, j] = np.array(n_ind).ravel()
if particle_on_edge and rcParam["debug_level"] == "very_high":
print("Warning:Some points are on the edge")
if particle_on_edge:
logging.warning("Some points are on the edge")
for i in range(len(inds)):
inds[i] = inds[i].astype(int)
return inds
Expand Down

0 comments on commit b3b0ef7

Please sign in to comment.