-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_data.py
324 lines (277 loc) · 11 KB
/
upload_data.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import httpx
import pandas as pd
import argparse
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# upload stations function
def upload_things(username, password, hydroserver_url, file_path):
auth = (username, password)
logger.info(
f"Uploading stations to HydroServer: {hydroserver_url}, using file: {file_path}"
)
things_endpoint = f"{hydroserver_url}/api/data/things"
df = pd.read_csv(file_path)
df["station_name"] = df["name"]
results = df.apply(
make_thing_post_request,
args=(
things_endpoint,
auth,
),
axis=1,
)
pass
# helper function to make the http post request
def make_thing_post_request(row, hydroserver_url, auth):
request_body = {
"latitude": row.latitude,
"longitude": row.longitude,
"elevation_m": row.elevation_m,
"elevationDatum": row.elevationDatum,
"state": row.state,
"county": row.county,
"country": row.country,
"name": row.station_name,
"description": row.description,
"samplingFeatureType": row.samplingFeatureType,
"samplingFeatureCode": row.samplingFeatureCode,
"siteType": row.siteType,
"dataDisclaimer": row.dataDisclaimer,
}
# print(request_body)
headers = {
"accept": "application/json",
"Content-Type": "application/json",
}
# breakpoint()
response = httpx.post(
hydroserver_url, headers=headers, json=request_body, auth=auth
)
logger.info(
f"Uploading station {row.name} to HydroServer: {hydroserver_url}, Status Code {response.status_code}"
)
def delete_all_things(username, password, hydroserver_url):
auth = (username, password)
logger.info(f"Deleting stations from HydroServer: {hydroserver_url}")
headers = {
"accept": "application/json",
"Content-Type": "application/json",
}
things_endpoint = f"{hydroserver_url}/api/data/things"
things_list_json = httpx.get(things_endpoint, headers=headers, auth=auth)
for thing in things_list_json.json():
# data = {'thing_id': thing['id']}
thing_id = thing["id"]
# breakpoint()
deleted_thing_response = httpx.delete(
f"{things_endpoint}/{thing_id}", auth=auth
)
logger.info(
f"Deleting station {thing['name']} from HydroServer: {hydroserver_url}, Status Code {deleted_thing_response.status_code}"
)
pass
def delete_all_datastreams(username, password, hydroserver_url):
auth = (username, password)
logger.info(f"Deleting data streams from HydroServer: {hydroserver_url}")
headers = {
"accept": "application/json",
"Content-Type": "application/json",
}
datastream_list_json = httpx.get(hydroserver_url, headers=headers, auth=auth)
for datastream in datastream_list_json.json():
datastream_id = datastream["id"]
# breakpoint()
deleted_thing_response = httpx.delete(
f"{hydroserver_url}/{datastream_id}", auth=auth
)
logger.info(
f"Deleting station {datastream['name']} from HydroServer: {hydroserver_url}, Status Code {deleted_thing_response.status_code}"
)
pass
def upload_observations_sync(
username,
password,
hydroserver_url,
file_path,
date_column,
value_column,
datastream_id,
):
auth = (username, password)
logger.info(
f"Uploading observations to HydroServer: {hydroserver_url}, using file: {file_path}"
)
df = pd.read_csv(file_path)
list_observations = df[[date_column, value_column]].values.tolist()
chunk_size = 10000
list_chunks = chunk_list(list_observations, chunk_size)
api_endpoint = f"{hydroserver_url}/api/sensorthings/v1.1/CreateObservations"
for chunk in list_chunks:
make_observations_post_request(datastream_id, chunk, api_endpoint, auth)
logger.info(f"uploading,chunk of {len(chunk)}")
# helper function to make the http post request
def make_observations_post_request(
datastream_id, list_observations, api_endpoint, auth
):
post_body = [
{
"Datastream": {"@iot.id": datastream_id},
"components": ["phenomenonTime", "result"],
"dataArray": list_observations,
}
]
headers = {
"accept": "application/json",
"Content-Type": "application/json",
}
response = httpx.post(
api_endpoint, headers=headers, json=post_body, auth=auth, timeout=None
)
logger.info(
f"Uploading observations for data stream {datastream_id} to HydroServer: {api_endpoint}, Status Code {response.status_code}"
)
def upload_datastreams(username, password, hydroserver_url, file_path):
auth = (username, password)
logger.info(
f"Uploading datastreams to HydroServer: {hydroserver_url}, using file: {file_path}"
)
df = pd.read_csv(file_path)
df["name_datastream"] = df["name"]
# breakpoint()
results = df.apply(
make_datastream_post_request,
args=(
hydroserver_url,
auth,
),
axis=1,
)
pass
# helper function to make the http post request
def make_datastream_post_request(row, hydroserver_url, auth):
request_body = {
"name": row.name_datastream,
"description": row.description,
"observationType": row.observationType,
"sampledMedium": row.sampledMedium,
"noDataValue": row.noDataValue,
"aggregationStatistic": row.aggregationStatistic,
"timeAggregationInterval": row.timeAggregationInterval,
"status": None,
"resultType": row.resultType,
"valueCount": None,
"intendedTimeSpacing": row.intendedTimeSpacing,
"phenomenonBeginTime": None,
"phenomenonEndTime": None,
"resultBeginTime": None,
"resultEndTime": None,
"dataSourceId": None,
"dataSourceColumn": None,
"isVisible": row.isVisible,
"thingId": row.thingId,
"sensorId": row.sensorId,
"observedPropertyId": row.observedPropertyId,
"processingLevelId": row.processingLevelId,
"unitId": row.unitId,
"timeAggregationIntervalUnitsId": row.timeAggregationIntervalUnitsId,
"intendedTimeSpacingUnitsId": None,
}
headers = {
"accept": "application/json",
"Content-Type": "application/json",
}
# breakpoint()
response = httpx.post(
hydroserver_url, headers=headers, json=request_body, auth=auth
)
if response.status_code == 403:
breakpoint()
logger.info(
f"Uploading datastream {row.name} to HydroServer: {hydroserver_url}, Status Code {response.status_code}"
)
def chunk_list(input_list, chunk_size):
# Initialize an empty list to store the smaller chunks
chunks = []
# Iterate through the input_list in steps of chunk_size
for i in range(0, len(input_list), chunk_size):
# Append a chunk of the list to the result list
chunk = input_list[i : i + chunk_size]
chunks.append(chunk)
return chunks
def main():
parser = argparse.ArgumentParser(
description="Command-line tool for uploading stations, time series, and datastreams"
)
# Add subparsers for each function
subparsers = parser.add_subparsers(title="Subcommands", dest="subcommand")
# Subparser for uploading stations
parser_upload_stations = subparsers.add_parser(
"upload-things", help="Upload stations"
)
parser_upload_stations.add_argument("username", help="HydroServer Username")
parser_upload_stations.add_argument("password", help="HydroServer Password")
parser_upload_stations.add_argument("hydroserver_url", help="HydroServer URL")
parser_upload_stations.add_argument("file_path", help="File path")
parser_upload_stations.set_defaults(func=upload_things)
# Subparser for uploading observations
parser_upload_observations = subparsers.add_parser(
"upload-observations", help="Upload observations"
)
parser_upload_observations.add_argument("username", help="HydroServer Username")
parser_upload_observations.add_argument("password", help="HydroServer Password")
parser_upload_observations.add_argument("hydroserver_url", help="HydroServer URL")
parser_upload_observations.add_argument("file_path", help="File path")
parser_upload_observations.add_argument("date_column", help="Date Column")
parser_upload_observations.add_argument("value_column", help="Value column")
parser_upload_observations.add_argument("datastream_id", help="DataStreamer ID")
parser_upload_observations.set_defaults(func=upload_observations_sync)
# Subparser for uploading datastreams
parser_upload_datastreams = subparsers.add_parser(
"upload-datastreams", help="Upload datastreams"
)
parser_upload_datastreams.add_argument("username", help="HydroServer Username")
parser_upload_datastreams.add_argument("password", help="HydroServer Password")
parser_upload_datastreams.add_argument("hydroserver_url", help="HydroServer URL")
parser_upload_datastreams.add_argument("file_path", help="File path")
parser_upload_datastreams.set_defaults(func=upload_datastreams)
# Subparser for deleting all things
parser_delete_stations = subparsers.add_parser(
"delete-all-things", help="Delete stations"
)
parser_delete_stations.add_argument("username", help="HydroServer Username")
parser_delete_stations.add_argument("password", help="HydroServer Password")
parser_delete_stations.add_argument("hydroserver_url", help="HydroServer URL")
parser_delete_stations.set_defaults(func=delete_all_things)
# Subparser for deleting all streamers
parser_delete_stations = subparsers.add_parser(
"delete-all-datastreams", help="Delete datastreams"
)
parser_delete_stations.add_argument("username", help="HydroServer Username")
parser_delete_stations.add_argument("password", help="HydroServer Password")
parser_delete_stations.add_argument("hydroserver_url", help="HydroServer URL")
parser_delete_stations.set_defaults(func=delete_all_datastreams)
args = parser.parse_args()
# breakpoint()
if hasattr(args, "func"):
if hasattr(args, "date_column") and hasattr(args, "value_column"):
# asyncio.run(args.func(args.username, args.password, args.hydroserver_url, args.file_path,args.date_column,args.value_column))
args.func(
args.username,
args.password,
args.hydroserver_url,
args.file_path,
args.date_column,
args.value_column,
args.datastream_id,
)
elif not hasattr(args, "file_path"):
args.func(args.username, args.password, args.hydroserver_url)
else:
args.func(
args.username, args.password, args.hydroserver_url, args.file_path
)
else:
parser.print_help()
if __name__ == "__main__":
main()