Skip to content

Commit

Permalink
feat: levels filtering with combination
Browse files Browse the repository at this point in the history
  • Loading branch information
fjugipe committed Nov 19, 2024
1 parent 3f3a241 commit 3f05f0d
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions api/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import datetime
from datetime import timedelta
from typing import Tuple
from itertools import chain

from isodate import ISO8601Error
import datastore_pb2 as dstore
Expand Down Expand Up @@ -181,25 +182,46 @@ def get_periods_or_range(periods: str) -> list[str]:

def get_z_levels_or_range(z: str) -> list[str]:
"""
Function for getting the levels as a list or a range
Function for getting the levels filters as a list of levels, ranges
or range intervals
"""
# it can be z=value1,value2,value3: z=2,10,80
# or z=minimum value/maximum value: z=10/100
# or z=Rn/min height/height interval: z=R20/100/50
# or a combination of the above: z=10,30/100,200,300,R20/100/50

values = [level_or_range.split("/") for level_or_range in split_and_strip(z)]

try:
split_on_slash = z.split("/")
if len(split_on_slash) == 2:
z_min = convert_m_to_cm(split_on_slash[0]) if split_on_slash[0] != ".." else -sys.maxsize - 1
z_max = convert_m_to_cm(split_on_slash[1]) if split_on_slash[1] != ".." else sys.maxsize
return [f"{z_min}/{z_max}"]
elif len(split_on_slash) > 2:
return get_z_values_from_interval(split_on_slash)
else:
return [convert_m_to_cm(level) for level in z.split(",")]
nested_filters = [
(
get_z_values_from_interval(level_or_range)
if len(level_or_range) > 2
else (
get_z_values_from_range(level_or_range)
if len(level_or_range) == 2
else [convert_m_to_cm(level_or_range[0])]
)
)
for level_or_range in values
]
# Return the flattened list of level filters
return list(chain(*nested_filters))
except ValueError:
raise HTTPException(status_code=400, detail=f"Invalid levels value: {z}")


def get_z_values_from_range(range: list[str]) -> list[str]:
"""
Function for getting the z values from a range.
"""
start, end = range[0], range[1]
z_min = convert_m_to_cm(start) if start != ".." else start
z_max = convert_m_to_cm(end) if end != ".." else end

return [f"{z_min}/{z_max}"]


def get_z_values_from_interval(interval: list[str]) -> list[str]:
"""
Function for getting the z values from a repeating-interval pattern.
Expand Down

0 comments on commit 3f05f0d

Please sign in to comment.