-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseutils.jl
More file actions
49 lines (34 loc) · 1.63 KB
/
parseutils.jl
File metadata and controls
49 lines (34 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using JSON
"""
@is_date_in_range(date_str, start_year, end_year)
Determines if the year extracted from a date string falls within a specified range of years.
# Arguments
- `date_str`: A string representing a date, expected to be in the format "YYYY-MM-DD".
- `start_year`: An integer specifying the start of the year range.
- `end_year`: An integer specifying the end of the year range.
# Returns
- `Bool`: `true` if the year is within the range, `false` otherwise.
"""
@inline function is_date_in_range(date_str, start_year::Int, end_year::Int)
year = parse(Int, split(date_str, "-")[1])
return year in start_year:end_year
end
"""
@generic_parse(response_json)
Parses a JSON object containing near-Earth object data, filtering for Earth-orbiting objects within a specified date range, and transforms the data into a JSON string.
# Arguments
- `response_json`: A JSON object containing an array of close approach data.
# Returns
- `String`: A JSON string representing the transformed data.
"""
function generic_parse(response_json)
earth_approach_data = filter(close_approach -> close_approach["orbiting_body"] == "Earth" && is_date_in_range(close_approach["close_approach_date_full"], 2000, 2024), response_json["close_approach_data"])
transformed_data = map(close_approach -> Dict(
"Date" => close_approach["close_approach_date_full"],
"Lunar Distance" => close_approach["miss_distance"]["lunar"],
"Velocity" => close_approach["relative_velocity"]["miles_per_hour"]),
earth_approach_data
)
earth_approach_data_json = JSON.json(transformed_data)
return earth_approach_data_json
end