Skip to content

Commit

Permalink
Extend black coverage
Browse files Browse the repository at this point in the history
Related to #1620
  • Loading branch information
VeckoTheGecko committed Aug 5, 2024
1 parent 4a786c0 commit 83d9a55
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 61 deletions.
9 changes: 8 additions & 1 deletion parcels/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
import subprocess

try:
version = subprocess.check_output(['git', '-C', os.path.dirname(__file__), 'describe', '--tags'], stderr=subprocess.PIPE).decode('ascii').strip()
version = (
subprocess.check_output(
["git", "-C", os.path.dirname(__file__), "describe", "--tags"],
stderr=subprocess.PIPE,
)
.decode("ascii")
.strip()
)
except:
from parcels._version_setup import version as version # noqa
10 changes: 7 additions & 3 deletions parcels/interaction/neighborsearch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
KDTreeFlatNeighborSearch,
)

__all__ = ["HashFlatNeighborSearch", "HashSphericalNeighborSearch",
"BruteFlatNeighborSearch",
"BruteSphericalNeighborSearch", "KDTreeFlatNeighborSearch"]
__all__ = [
"HashFlatNeighborSearch",
"HashSphericalNeighborSearch",
"BruteFlatNeighborSearch",
"BruteSphericalNeighborSearch",
"KDTreeFlatNeighborSearch",
]
17 changes: 8 additions & 9 deletions parcels/interaction/neighborsearch/distanceutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ def fast_distance(lat1, lon1, lat2, lon2):
This is not the only possible implementation. It was taken from:
https://www.mkompf.com/gps/distcalc.html
"""
g = np.sin(lat1)*np.sin(lat2)+np.cos(lat1)*np.cos(lat2)*np.cos(lon1-lon2)
g = np.sin(lat1) * np.sin(lat2) + np.cos(lat1) * np.cos(lat2) * np.cos(lon1 - lon2)
return np.arccos(np.minimum(1, g))


def spherical_distance(depth1_m, lat1_deg, lon1_deg, depth2_m, lat2_deg,
lon2_deg):
def spherical_distance(depth1_m, lat1_deg, lon1_deg, depth2_m, lat2_deg, lon2_deg):
"""Compute the arc distance, uses degrees as input."""
R_earth = 6371000
lat1 = np.pi*lat1_deg/180
lon1 = np.pi*lon1_deg/180
lat2 = np.pi*lat2_deg/180
lon2 = np.pi*lon2_deg/180
lat1 = np.pi * lat1_deg / 180
lon1 = np.pi * lon1_deg / 180
lat2 = np.pi * lat2_deg / 180
lon2 = np.pi * lon2_deg / 180

horiz_dist = R_earth*fast_distance(lat1, lon1, lat2, lon2)
horiz_dist = R_earth * fast_distance(lat1, lon1, lat2, lon2)

vert_dist = np.abs(depth1_m-depth2_m)
vert_dist = np.abs(depth1_m - depth2_m)
return (vert_dist, horiz_dist)
4 changes: 2 additions & 2 deletions parcels/interaction/neighborsearch/kdtreeflat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
class KDTreeFlatNeighborSearch(BaseFlatNeighborSearch):
def find_neighbors_by_coor(self, coor):
coor = coor.reshape(3, 1)
corrected_coor = (coor/self.inter_dist).reshape(-1)
corrected_coor = (coor / self.inter_dist).reshape(-1)
rel_idx = np.array(self._kdtree.query_ball_point(corrected_coor, r=1))
neighbor_idx = self.active_idx[rel_idx]
return neighbor_idx, np.vstack(self._distance(coor, neighbor_idx))

def rebuild(self, values=None, active_mask=-1):
super().rebuild(values, active_mask)
self._corrected_values = values[:, self._active_idx]/self.inter_dist
self._corrected_values = values[:, self._active_idx] / self.inter_dist
self._kdtree = KDTree(self._corrected_values.T)
5 changes: 3 additions & 2 deletions parcels/tools/loggers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Script to create a `logger` for Parcels."""

import logging
import sys

__all__ = ['logger', 'XarrayDecodedFilter']
__all__ = ["logger", "XarrayDecodedFilter"]

warning_once_level = 25
info_once_level = 26
Expand Down Expand Up @@ -53,4 +54,4 @@ class XarrayDecodedFilter(logging.Filter):
"""Filters the warning_once from fieldfilebuffer when cf_decoding fails."""

def filter(self, record):
return 'Filling values might be wrongly parsed' not in record.getMessage()
return "Filling values might be wrongly parsed" not in record.getMessage()
28 changes: 1 addition & 27 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,38 +104,17 @@ convention = "numpy"
[tool.ruff.format]
# List of files to ignore formatting (ordered by ascending line count)
exclude = [
"parcels/interaction/__init__.py",
"parcels/compilation/__init__.py",
"parcels/application_kernels/__init__.py",
"parcels/tools/__init__.py",
"parcels/_version.py",
"tests/test_converters.py",
"parcels/__init__.py",
"parcels/interaction/neighborsearch/bruteforce.py",
"parcels/interaction/neighborsearch/kdtreeflat.py",
"tests/test_data/fieldset_nemo.py",
"tests/test_examples.py",
"parcels/interaction/neighborsearch/__init__.py",
"tests/test_data/fieldset_nemo_error.py",
"parcels/interaction/neighborsearch/distanceutils.py",
"parcels/tools/global_statics.py",
"tests/test_mpirun.py",
"parcels/tools/loggers.py",
"parcels/tools/global_statics.py",
"tests/test_tools.py",
"docs/examples/example_brownian.py",
"docs/examples/example_mitgcm.py",
"parcels/gridset.py",
"parcels/tools/timer.py",
"docs/examples/example_ofam.py",
"parcels/application_kernels/TEOSseawaterdensity.py",
"tests/test_particles.py",
"docs/examples/example_radial_rotation.py",
"parcels/application_kernels/interaction.py",
"parcels/application_kernels/advectiondiffusion.py",
"docs/examples/example_decaying_moving_eddy.py",
"parcels/tools/statuscodes.py",
"tests/test_data/create_testfields.py",
"docs/examples/example_nemo_curvilinear.py",
"tests/test_diffusion.py",
"parcels/tools/exampledata_utils.py",
"parcels/tools/interpolation_utils.py",
Expand All @@ -148,15 +127,11 @@ exclude = [
"parcels/tools/converters.py",
"parcels/application_kernels/advection.py",
"parcels/particlefile.py",
"docs/examples/example_stommel.py",
"parcels/compilation/codecompiler.py",
"parcels/particle.py",
"tests/test_interaction.py",
"docs/examples/example_peninsula.py",
"tests/test_particlefile.py",
"docs/examples/example_globcurrent.py",
"parcels/application_kernels/EOSseawaterproperties.py",
"docs/examples/example_moving_eddies.py",
"tests/test_kernel_execution.py",
"tests/test_particlesets.py",
"parcels/particledata.py",
Expand All @@ -166,7 +141,6 @@ exclude = [
"tests/test_advection.py",
"parcels/kernel.py",
"parcels/fieldfilebuffer.py",
"docs/examples/example_dask_chunk_OCMs.py",
"tests/test_fieldset_sampling.py",
"parcels/compilation/codegenerator.py",
"tests/test_grids.py",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ def test_TimeConverter():
date = getattr(cftime, cf_datetime)(1990, 1, 1)
assert TimeConverter(date).calendar == date.calendar
assert TimeConverter(None).calendar is None
date_datetime64 = np.datetime64('2001-01-01T12:00')
date_datetime64 = np.datetime64("2001-01-01T12:00")
assert TimeConverter(date_datetime64).calendar == "np_datetime64"
22 changes: 14 additions & 8 deletions tests/test_data/fieldset_nemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@
def create_fieldset(indices=None):
data_path = os.path.join(os.path.dirname(__file__))

filenames = {'U': {'lon': os.path.join(data_path, 'mask_nemo_cross_180lon.nc'),
'lat': os.path.join(data_path, 'mask_nemo_cross_180lon.nc'),
'data': os.path.join(data_path, 'Uu_eastward_nemo_cross_180lon.nc')},
'V': {'lon': os.path.join(data_path, 'mask_nemo_cross_180lon.nc'),
'lat': os.path.join(data_path, 'mask_nemo_cross_180lon.nc'),
'data': os.path.join(data_path, 'Vv_eastward_nemo_cross_180lon.nc')}}
variables = {'U': 'U', 'V': 'V'}
dimensions = {'lon': 'glamf', 'lat': 'gphif', 'time': 'time_counter'}
filenames = {
"U": {
"lon": os.path.join(data_path, "mask_nemo_cross_180lon.nc"),
"lat": os.path.join(data_path, "mask_nemo_cross_180lon.nc"),
"data": os.path.join(data_path, "Uu_eastward_nemo_cross_180lon.nc"),
},
"V": {
"lon": os.path.join(data_path, "mask_nemo_cross_180lon.nc"),
"lat": os.path.join(data_path, "mask_nemo_cross_180lon.nc"),
"data": os.path.join(data_path, "Vv_eastward_nemo_cross_180lon.nc"),
},
}
variables = {"U": "U", "V": "V"}
dimensions = {"lon": "glamf", "lat": "gphif", "time": "time_counter"}
indices = indices or {}
return parcels.FieldSet.from_nemo(filenames, variables, dimensions, indices=indices)
22 changes: 14 additions & 8 deletions tests/test_data/fieldset_nemo_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@
def random_function_name():
data_path = os.path.join(os.path.dirname(__file__))

filenames = {'U': {'lon': os.path.join(data_path, 'mask_nemo_cross_180lon.nc'),
'lat': os.path.join(data_path, 'mask_nemo_cross_180lon.nc'),
'data': os.path.join(data_path, 'Uu_eastward_nemo_cross_180lon.nc')},
'V': {'lon': os.path.join(data_path, 'mask_nemo_cross_180lon.nc'),
'lat': os.path.join(data_path, 'mask_nemo_cross_180lon.nc'),
'data': os.path.join(data_path, 'Vv_eastward_nemo_cross_180lon.nc')}}
variables = {'U': 'U', 'V': 'V'}
dimensions = {'lon': 'glamf', 'lat': 'gphif', 'time': 'time_counter'}
filenames = {
"U": {
"lon": os.path.join(data_path, "mask_nemo_cross_180lon.nc"),
"lat": os.path.join(data_path, "mask_nemo_cross_180lon.nc"),
"data": os.path.join(data_path, "Uu_eastward_nemo_cross_180lon.nc"),
},
"V": {
"lon": os.path.join(data_path, "mask_nemo_cross_180lon.nc"),
"lat": os.path.join(data_path, "mask_nemo_cross_180lon.nc"),
"data": os.path.join(data_path, "Vv_eastward_nemo_cross_180lon.nc"),
},
}
variables = {"U": "U", "V": "V"}
dimensions = {"lon": "glamf", "lat": "gphif", "time": "time_counter"}
return parcels.FieldSet.from_nemo(filenames, variables, dimensions)


Expand Down

0 comments on commit 83d9a55

Please sign in to comment.