-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
122 lines (104 loc) · 4.61 KB
/
util.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from catalog import data_catalog
def validate_parameters_against_catalog(parameters, catalog=data_catalog):
"""
Validates the parameters against the metadata catalog to ensure at least some data exists for the given parameters.
This function should be a simple database search to see if any data exists for combinations of the parameters.
Returns a subset of the catalog, which could be empty.
"""
# do something to subset the catalog using parameters
catalog_subset = catalog
return catalog_subset
def fetch_data_using_catalog(parameters, catalog_subset):
"""
Fetches data using coverage info from the metadata catalog and given parameters.
This function will probably call other subfunctions to create WCPS queries to fetch the data.
"""
# perform data fetching operations using parameters and catalog_subset
data = {"data": {}}
return data
def package_data(service_category, data, format):
"""
Packages the data into a format specified by the user.
Allows different packaging options for different service categories.
"""
# perform data packaging operations using service_category, data, and format parameter
packaged_data = {"packaged_data": {}}
return packaged_data
def check_for_data_and_package_it(service_category, parameters):
catalog_subset = validate_parameters_against_catalog(parameters)
data = fetch_data_using_catalog(parameters, catalog_subset)
packaged_data = package_data(parameters, service_category, data)
return packaged_data
def get_metadata(service_category, variable_list, data_catalog=data_catalog):
all_variable_sources = []
all_variable_start_years = []
all_variable_end_years = []
all_bboxes = []
for variable in variable_list:
all_variable_sources.extend(
data_catalog["service_category"][service_category]["variable"][variable][
"source"
].keys()
)
for source in data_catalog["service_category"][service_category]["variable"][
variable
]["source"].keys():
all_variable_start_years.append(
data_catalog["service_category"][service_category]["variable"][
variable
]["source"][source]["start_year"]
)
all_variable_end_years.append(
data_catalog["service_category"][service_category]["variable"][
variable
]["source"][source]["end_year"]
)
all_bboxes.append(
data_catalog["service_category"][service_category]["variable"][
variable
]["source"][source]["bbox"]
)
# TODO: get combined extent of all bboxes (this would be more sophisticated in a real app)
# all_bboxes = list(map(list, zip(*all_bboxes)))
# all_bboxes = [
# min(all_bboxes[0]),
# min(all_bboxes[1]),
# max(all_bboxes[2]),
# max(all_bboxes[3]),
# ]
return {
# "sources": all_variable_sources,
"first_year": min(all_variable_start_years),
"last_year": max(all_variable_end_years),
# "bbox": all_bboxes,
}
def mockup_message(parameters, route):
"""
Helps to create a message for the API. This is just a mockup! The actual app will return actual data :)
"""
params = list(parameters.model_fields.keys())
if route == "about":
message = f"You have requested a general description of the API. "
if parameters.service_category is not None:
message += f"You have requested more information about a service category ({parameters.service_category}), and the service category you requested is valid."
return {"message": message}
else:
message = f"You have requested {route} data using the following parameters: "
if "variable" in params:
message += f"Variable(s): {parameters.variable} | "
if "lat" in params:
if parameters.lat is not None:
message += f"Latitude: {parameters.lat} | "
if "lon" in params:
if parameters.lon is not None:
message += f"Longitude: {parameters.lon} | "
if "location" in params:
if parameters.location is not None:
message += f"Location: {parameters.location} | "
if "start_year" in params:
message += f"Start Year: {parameters.start_year} | "
if "end_year" in params:
message += f"End Year: {parameters.end_year}"
if "format" in params:
message += f" | Format: {parameters.format}"
return {"message": message}