Skip to content

Commit ee83f00

Browse files
committed
Add method to extract period from network
1 parent a792e21 commit ee83f00

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

src/py_eddy_tracker/appli/network.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ def subset_network():
6161
parser = EddyParser("Subset network")
6262
parser.add_argument("input", help="input network file")
6363
parser.add_argument("out", help="output file")
64-
parser.add_argument(
65-
"--inverse_selection",
66-
action="store_true",
67-
help="Extract the inverse of selection",
68-
)
6964
parser.add_argument(
7065
"-l",
7166
"--length",
@@ -80,10 +75,25 @@ def subset_network():
8075
type=int,
8176
help="Remove short dead end, first is for minimal obs number and second for minimal segment time to keep",
8277
)
78+
parser.add_argument(
79+
"--remove_trash",
80+
action="store_true",
81+
help="Remove trash (network id == 0)",
82+
)
83+
parser.add_argument(
84+
"-p",
85+
"--period",
86+
nargs=2,
87+
type=int,
88+
help="Start day and end day, if it's negative value we will add to day min and add to day max,"
89+
"if 0 it s not use",
90+
)
8391
args = parser.parse_args()
84-
n = NetworkObservations.load_file(args.input)
92+
n = NetworkObservations.load_file(args.input, raw_data=True)
8593
if args.length is not None:
8694
n = n.longer_than(*args.length)
8795
if args.remove_dead_end is not None:
8896
n = n.remove_dead_end(*args.remove_dead_end)
97+
if args.period is not None:
98+
n = n.extract_with_period(args.period)
8999
n.write_file(filename=args.out)

src/py_eddy_tracker/generic.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ def build_index(groups):
7070
:param array groups: array which contain group to be separated
7171
:return: (first_index of each group, last_index of each group, value to shift group)
7272
:rtype: (array, array, int)
73+
74+
Examples
75+
--------
76+
>>> build_index(array((1, 1, 3, 4, 4)))
77+
(array([0, 2, 2, 3]), array([2, 2, 3, 5]), 1)
7378
"""
7479
i0, i1 = groups.min(), groups.max()
7580
amplitude = i1 - i0 + 1
@@ -78,7 +83,7 @@ def build_index(groups):
7883
for i, group in enumerate(groups[:-1]):
7984
# Get next value to compare
8085
next_group = groups[i + 1]
81-
# if different we need to set index
86+
# if different we need to set index for all group between the 2 values
8287
if group != next_group:
8388
first_index[group - i0 + 1 : next_group - i0 + 1] = i + 1
8489
last_index = zeros(amplitude, dtype=numba_types.int_)

src/py_eddy_tracker/observations/network.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
arange,
1111
array,
1212
bincount,
13+
bool_,
1314
concatenate,
1415
empty,
1516
in1d,
@@ -1237,6 +1238,30 @@ def extract_segment(self, segments, absolute=False):
12371238
mask[i] = False
12381239
return self.extract_with_mask(mask)
12391240

1241+
def extract_with_period(self, period):
1242+
"""
1243+
Extract within a time period
1244+
1245+
:param (int,int) period: two dates to define the period, must be specify from 1/1/1950
1246+
:return: Return all eddy tracks which are in bounds
1247+
:rtype: NetworkObservations
1248+
1249+
.. minigallery:: py_eddy_tracker.NetworkObservations.extract_with_period
1250+
"""
1251+
dataset_period = self.period
1252+
p_min, p_max = period
1253+
if p_min > 0:
1254+
mask = self.time >= p_min
1255+
elif p_min < 0:
1256+
mask = self.time >= (dataset_period[0] - p_min)
1257+
else:
1258+
mask = ones(self.time.shape, dtype=bool_)
1259+
if p_max > 0:
1260+
mask *= self.time <= p_max
1261+
elif p_max < 0:
1262+
mask *= self.time <= (dataset_period[1] + p_max)
1263+
return self.extract_with_mask(mask)
1264+
12401265
def extract_with_mask(self, mask):
12411266
"""
12421267
Extract a subset of observations.

src/scripts/EddySubSetter

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def id_parser():
2222
"--period",
2323
nargs=2,
2424
type=int,
25-
help="Start day and end day, if it s negative value we will add to day min and add to day max, if 0 it s not use",
25+
help="Start day and end day, if it's negative value we will add to day min and add to day max, if 0 it s not use",
2626
)
2727
group.add_argument(
2828
"-l",

0 commit comments

Comments
 (0)