-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_client.py
More file actions
1451 lines (1243 loc) · 56.7 KB
/
Copy pathmodel_client.py
File metadata and controls
1451 lines (1243 loc) · 56.7 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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Created by Sara Geleskie Damiano
"""
#%%
import time
import copy
import re
from pathlib import Path
from typing import Dict, List, TypedDict, Union, Any
from typing_extensions import NotRequired
import collections
from collections import OrderedDict
import requests
from requests import Request, Response, Session
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
import pandas as pd
import json
import logging
module_logger = logging.getLogger(__name__)
#%%
class ModelMyWatershedJob(TypedDict):
job_label: str
request_host: str
request_endpoint: str
payload: NotRequired[Union[str, Dict]]
start_job_status: str
start_job_response: NotRequired[Any]
job_result_status: str
result_response: NotRequired[Any]
error_response: NotRequired[Dict]
class ModemMyWatershedLayerOverride(TypedDict):
__LAND__: NotRequired[str]
__STREAMS__: NotRequired[str]
class ModelMyWatershedAPI:
api_logger = module_logger.getChild(__qualname__)
# the ModelMyWatershed page
staging_mmw_host: str = "https://staging.modelmywatershed.org"
production_mmw_host: str = "https://modelmywatershed.org"
# low level endpoints
api_endpoint: str = "api/"
analyze_endpoint: str = api_endpoint + "analyze/"
modeling_endpoint: str = api_endpoint + "modeling/"
old_modeling_endpoint: str = "mmw/modeling/"
# simple analysis endpoints
protected_lands_endpoint: str = analyze_endpoint + "protected-lands/"
soil_endpoint: str = analyze_endpoint + "soil/"
terrain_endpoint: str = analyze_endpoint + "terrain/"
climate_endpoint: str = analyze_endpoint + "climate/"
point_source_endpoint: str = analyze_endpoint + "pointsource/"
animal_endpoint: str = analyze_endpoint + "animals/"
srat_endpoint: str = analyze_endpoint + "catchment-water-quality/"
catchment_water_quality_endpoint: str = srat_endpoint
# more detailed analysis endpoints
land_endpoint: str = analyze_endpoint + "land/{}/"
forcast_endpoint: str = analyze_endpoint + "drb-2100-land/{}/"
streams_endpoint: str = analyze_endpoint + "streams/{}/"
# GWLF-E endpoints
gwlfe_prepare_endpoint: str = modeling_endpoint + "gwlf-e/prepare/"
mapshed_endpoint: str = gwlfe_prepare_endpoint
gwlfe_run_endpoint: str = modeling_endpoint + "gwlf-e/run/"
gwlfe_endpoint: str = gwlfe_run_endpoint
# Subbasin (GWLF-E) endpoints
subbasin_prepare_endpoint: str = modeling_endpoint + "subbasin/prepare/"
subbasin_run_endpoint: str = modeling_endpoint + "subbasin/run/"
# TR-55 (Site Storm Model) endpoint
tr55_endpoint: str = old_modeling_endpoint + "tr55/"
# project endpoint
project_endpoint: str = old_modeling_endpoint + "projects/"
# NOTE: These are NLCD layers ONLY! The Shippensburg 2100 predictions are called
# from the Drexel-provided API, and are not available as a geoprocessing layer
# from https://github.com/WikiWatershed/model-my-watershed/blob/develop/src/mmw/js/src/modeling/utils.js
land_use_layers: Dict[str, str] = {
"2019_2019": "nlcd-2019-30m-epsg5070-512-byte",
"2019_2016": "nlcd-2016-30m-epsg5070-512-byte",
"2019_2011": "nlcd-2011-30m-epsg5070-512-byte",
"2019_2006": "nlcd-2006-30m-epsg5070-512-byte",
"2019_2001": "nlcd-2001-30m-epsg5070-512-byte",
"2011_2011": "nlcd-2011-30m-epsg5070-512-int8",
}
drb_2011_keys = [
"centers",
"centers_np",
"centers_osi",
"corridors",
"corridors_np",
"corridors_osi",
]
streams_datasources = ["nhd", "nhdhr", "drb"]
weather_layers = ["NASA_NLDAS_2000_2019", "RCP45_2080_2099", "RCP85_2080_2099"]
# conversion dictionaries
# dictionary for converting NLCD types to those used by MapShed
# we need the land use modifications for the Shippensburg 2100 predictions
nlcd_to_mapshed: Dict[str, str] = {
"Pasture/Hay": "01-Hay/Past",
"Cultivated Crops": "02-Cropland",
"Deciduous Forest": "03-Forest",
"Evergreen Forest": "03-Forest",
"Mixed Forest": "03-Forest",
"Shrub/Scrub": "03-Forest",
"Woody Wetlands": "04-Wetland",
"Emergent Herbaceous Wetlands": "04-Wetland",
"Grassland/Herbaceous": "05-Open_Land",
"Perennial Ice/Snow": "06-Bare_Rock",
"Barren Land (Rock/Sand/Clay)": "06-Bare_Rock",
"Developed, Low Intensity": "11-Ld_Mixed",
"Developed, Medium Intensity": "12-Md_Mixed",
"Developed, High Intensity": "13-Hd_Mixed",
"Developed, Open Space": "14-Ld_Open_Space",
"Open Water": None,
}
# the actual modification area value is a little different
mapshed_to_area_id: Dict[str, str] = {
"01-Hay/Past": "Area__0",
"02-Cropland": "Area__1",
"03-Forest": "Area__2",
"04-Wetland": "Area__3",
"05-Open_Land": "Area__6",
"06-Bare_Rock": "Area__7",
"11-Ld_Mixed": "Area__10",
"12-Md_Mixed": "Area__11",
"13-Hd_Mixed": "Area__12",
"14-Ld_Open_Space": "Area__13",
}
# The hash for no-modifications
inputmod_hash: str = (
"d751713988987e9331980363e24189ced751713988987e9331980363e24189ce"
)
def __init__(
self,
api_key: str,
save_path: str = None,
use_staging: bool = False,
):
"""Create a new class for accessing ModelMyWatershed's API's
Args:
save_path (str): The path you want any json objects to be saved to
api_key (str): Your API key (needed for analysis requests)
use_staging (bool, optional): Use the staging version of ModelMyWatershed rather than the
production website. Defaults to False.
"""
# set up instance variables
self.mmw_host = (
self.staging_mmw_host if use_staging else self.production_mmw_host
)
self.api_key = api_key
self.save_path = save_path
# TODO(SRGDamia1): Find out the max response time from Terence
DEFAULT_TIMEOUT = 30 # seconds
# create a TimeoutHTTPAdapter to enforce a default timeout on the session
# from https://findwork.dev/blog/advanced-usage-python-requests-timeouts-retries-hooks/
class TimeoutHTTPAdapter(HTTPAdapter):
def __init__(self, *args, **kwargs):
self.timeout = DEFAULT_TIMEOUT
if "timeout" in kwargs:
self.timeout = kwargs["timeout"]
del kwargs["timeout"]
super().__init__(*args, **kwargs)
def send(self, request, **kwargs):
timeout = kwargs.get("timeout")
if timeout is None:
kwargs["timeout"] = self.timeout
return super().send(request, **kwargs)
retry_strategy = Retry(
total=5,
backoff_factor=1,
status_forcelist=[413, 429, 500, 502, 503, 504],
method_whitelist=["HEAD", "GET", "PUT", "DELETE", "OPTIONS", "TRACE"],
)
adapter = TimeoutHTTPAdapter(max_retries=retry_strategy)
# create a request session
self.mmw_session = Session()
self.mmw_session.verify = True
# mount the session for all requests, attaching the timeout/retry adapter
self.mmw_session.mount("https://", adapter)
self.mmw_session.mount("http://", adapter)
self.mmw_session.headers.update(
{
# "Host": "staging.modelmywatershed.org",
"Authorization": "Token " + self.api_key,
# "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:85.0) Gecko/20100101 Firefox/85.0",
# "Accept": "*/*",
# "Connection": "keep-alive",
# "Accept-Encoding": "gzip, deflate, br",
# "Accept-Language": "en-US,en;q=0.5",
# "DNT": "1",
# "Upgrade-Insecure-Requests": "1",
# "Origin": "https://staging.modelmywatershed.org",
# "Referer": "{}/".format(self.mmw_host),
}
)
def _print_headers(self, headers: Dict) -> str:
"""Helper function for tracing errors in requests - prints out the header dictionary
Args:
headers (Dict): The headers to print
Returns:
str: A string version of the header dictionary
"""
out_str = ""
for header in headers:
out_str += "\t{}: {}\n".format(header, headers[header])
return out_str
def _print_req(
self,
the_request: requests.Response,
logging_level: int = logging.DEBUG,
) -> None:
"""Helper function for tracing errors in requests - Prints out the request input
Args:
the_request (requests.Response): The response object from the request
logging_level (logging._Level): The logging level to use for the request
"""
print_format = "\nRequest:\nmethod: {}\nurl: {}\nheaders:\n{}\nbody: {}\n\nResponse:\nstatus code: {}\nurl: {}\nheaders: {}\ncookies: {}"
self.api_logger.log(
logging_level,
print_format.format(
the_request.request.method,
the_request.request.url,
self._print_headers(the_request.request.headers),
the_request.request.body
if the_request.request.body is None
or (
the_request.request.body is not None
and len(the_request.request.body) < 1000
)
else "{} ...".format(the_request.request.body[0:250]),
the_request.status_code,
the_request.url,
self._print_headers(the_request.headers),
the_request.cookies,
),
)
def _print_req_trace(
self,
the_request: requests.Response,
logging_level: int = logging.DEBUG,
) -> None:
"""Helper function for tracing errors in requests - Prints out the request input and response
Args:
the_request (requests.Response): The response object from the request
logging_level (logging._Level): The logging level to use for the request
"""
if the_request.history:
self.api_logger.debug("\nRequest was redirected")
for resp in the_request.history:
self._print_req(resp, logging.DEBUG)
self.api_logger.debug("\n\nFinal destination:")
self._print_req(the_request, logging_level)
else:
self._print_req(the_request, logging_level)
def login(self, mmw_user: str, mmw_pass: str) -> bool:
"""Log in to the ModelMyWatershed API
Args:
mmw_user (str): Your username
mmw_pass (str): Your password
Returns:
bool: True if the login is successful
"""
# construct the auth payload
auth_payload = {
"username": mmw_user,
"password": mmw_pass,
}
# The log-in page
login_page = "{}/user/login".format(self.mmw_host)
try:
# log in
self.mmw_session.post(
login_page,
data=auth_payload,
headers={
"Referer": self.mmw_host,
"Pragma": "no-cache",
"Cache-Control": "no-cache",
},
)
# self.mmw_session.headers.update(
# {"X-CSRFToken": self.mmw_session.cookies["csrftoken"]}
# )
except Exception as ex:
self.api_logger.warn("Failed to log in: {}".format(ex))
return False
# self.api_logger.debug("\nSession cookies: {}".format(self.mmw_session.cookies))
return True
def _set_request_headers(self, request_endpoint: str) -> None:
"""Adds the right referer and datatype headers to a request
Args:
request_endpoint (str): The endpoint for the request
"""
pass
# if self.project_endpoint in request_endpoint:
# headers = {
# "Content-Type": "application/json",
# "Referer": "https://staging.modelmywatershed.org/project/",
# "X-Requested-With": "XMLHttpRequest",
# }
# elif self.old_modeling_endpoint in request_endpoint:
# headers = {
# "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
# "Referer": "https://staging.modelmywatershed.org/project/",
# "X-Requested-With": "XMLHttpRequest",
# }
# else:
# headers = {
# "Content-Type": "application/json",
# "Referer": "https://staging.modelmywatershed.org/analyze",
# "X-Requested-With": "XMLHttpRequest",
# }
# self.mmw_session.headers.update(headers)
def _pprint_endpoint(self, request_endpoint: str) -> None:
"""Prints out the request endpoint in a format usable for a Windows endpoint
Args:
request_endpoint (string): the request endpoint
"""
return (
request_endpoint.replace(self.analyze_endpoint, "")
.replace(self.modeling_endpoint, "")
.replace("/", "_")
.strip(" _")
)
def _make_mmw_request(
self, req: Request, required_json_fields: Union[List[str], None] = None
) -> Dict:
"""Make a request to ModelMW with retries including handeling for throttling.
Args:
req (Request): A requests "Request" object
required_json_fields (List[str]): A list of fields, at least one of which
must be present in the response json. If none of these fields are
present, the request will be retried.
Returns:
Dict: The response json and details about the response
"""
# "prepare" the request, in the session
prepped = self.mmw_session.prepare_request(req)
throttle_time = 30.0
attempts = 0
req_resp = None
req_resp_json = None
while attempts < 5:
# use the session to send the request
# NOTE: The http method is already part of the prepared request, so here we just "send"
try:
req_resp = self.mmw_session.send(prepped)
self._print_req_trace(req_resp, logging.DEBUG)
except requests.exceptions.Timeout:
self.api_logger.warn("\t***Request timed out!***")
attempts += 1
continue
except requests.exceptions.RetryError:
self.api_logger.warn("\tMaximum retries exceeded")
attempts = 5
break
# make sure we got valid json - all responses from ModelMW - except for DELETE's - should be json, even errors
try:
if prepped.method != "DELETE":
req_resp_json = req_resp.json(object_pairs_hook=OrderedDict)
except (requests.exceptions.JSONDecodeError, json.JSONDecodeError):
self.api_logger.warn(
"\t***Proper JSON not returned for ModelMW request!***"
)
self.api_logger.debug(
"\t***Got {} with text {}!***".format(req_resp, req_resp.text)
)
# if we got a positive response code, we have proper json, and it has the required fields, return it
if (
req_resp.status_code in [200, 201]
and (
(
req_resp_json is not None
and required_json_fields is not None
and required_json_fields != []
and (
(type(req_resp_json) in [dict, OrderedDict])
and any(
(
(req_key in req_resp_json.keys())
and (req_resp_json[req_key] is not None)
for req_key in required_json_fields
)
)
)
)
or (
req_resp_json is not None
and (required_json_fields is None or required_json_fields == [])
)
)
or (req_resp.status_code in [204, 404] and prepped.method == "DELETE")
):
return {
"succeeded": True,
"json_response": copy.deepcopy(req_resp_json),
"error_response": None,
}
# If we didn't get a positive response code, or we didn't get proper json,
# or the expected fields aren't in it
self.api_logger.warn(
"\tModelMW {} request to {} FAILED on attempt {} with status code {}".format(
req_resp.request.method,
req_resp.request.url,
attempts,
req_resp.status_code,
)
)
# status codes not to retry
if req_resp.status_code in [400, 404]:
self.api_logger.warn(
"\tGot status code {}; will not retry".format(req_resp.status_code)
)
attempts = 5
break
# try to read the error details to see if we've been throttled
req_resp_details = ""
if (
req_resp_json is not None
and (
type(req_resp_details) is dict
or type(req_resp_details) is OrderedDict
)
and "detail" in req_resp_details.keys()
):
req_resp_details = req_resp_details["detail"]
if "throttled" in req_resp_details:
search_pat = "Expected available in (?P<throttle_time>[\d\.]+) seconds."
throttle_match = re.search(search_pat, req_resp_details)
if throttle_match is not None:
throttle_time = float(throttle_match.group("throttle_time"))
if throttle_time > 60.0 * 30.0:
self.api_logger.warn(
"\twait time of {}s is too long, will not retry".format(
throttle_time
)
)
attempts = 5
break
if attempts < 4:
self.api_logger.debug("\tretrying in {}s...".format(throttle_time))
time.sleep(throttle_time)
attempts += 1
# if we get all the way here, just return whatever we got
self.api_logger.error("\t***ERROR IN ModelMW REQUEST***")
if req_resp is not None:
self._print_req_trace(req_resp, logging.ERROR)
return {
"succeeded": True,
"json_response": None,
"error_response": req_resp_json if req_resp_json is not None else req_resp,
}
def start_job(
self,
request_endpoint: str,
job_label: str,
payload: Dict = None,
) -> ModelMyWatershedJob:
"""Starts an analysis or modeling job
Args:
request_endpoint (str): The endpoint for the request
payload (Dict): The payload going to the request.
Either a JSON serializable dictionary or pre-formatted form data.
job_label (str): A label to use to save the output files
Returns:
ModelMyWatershedJob: A typed dictionary with the job inputs and output
"""
job_dict: ModelMyWatershedJob = {
"job_label": job_label,
"request_host": self.mmw_host,
"request_endpoint": request_endpoint,
"payload": payload,
"start_job_status": "Not Started",
"job_result_status": "Not Started",
}
self._set_request_headers(request_endpoint)
if self.api_endpoint in request_endpoint:
# the api endpoint expects json, expected to be dumped from a dictionary
json_data = payload
payload = None
elif self.old_modeling_endpoint in request_endpoint:
# the older modeling endpoint expected form data, that should be pre-prepared by the user
json_data = None
else:
payload = payload
json_data = None
outgoing_request: Request = Request(
"POST",
"{}/{}".format(self.mmw_host, request_endpoint),
data=payload,
json=json_data,
)
start_job_req: Dict = self._make_mmw_request(
outgoing_request, ["job", "job_uuid"]
)
if start_job_req["succeeded"] == True:
job_dict["start_job_status"] = "succeeded"
job_dict["start_job_response"] = start_job_req["json_response"]
else:
job_dict["start_job_status"] = "failed"
job_dict["start_job_response"] = start_job_req["error_response"]
return job_dict
def get_job_result(
self, start_job_dict: ModelMyWatershedJob
) -> ModelMyWatershedJob:
"""Given a job input, waits for and retrievs the job results
Args:
start_job_dict (ModelMyWatershedJob): The dictionary with the job input information
Returns:
ModelMyWatershedJob: A copy of the input dictionary with the job output appended.
"""
if start_job_dict["start_job_status"] != "succeeded":
self.api_logger.warn(
"Job was not successfully started, cannot get results."
)
return start_job_dict
job_id = None
if (
(
type(start_job_dict["start_job_response"]) is dict
or type(start_job_dict["start_job_response"]) is collections.OrderedDict
)
and "job_uuid" in start_job_dict["start_job_response"].keys()
and start_job_dict["start_job_response"]["job_uuid"] is not None
):
job_id = start_job_dict["start_job_response"]["job_uuid"]
elif (
(
type(start_job_dict["start_job_response"]) is dict
or type(start_job_dict["start_job_response"]) is collections.OrderedDict
)
and "job" in start_job_dict["start_job_response"].keys()
and start_job_dict["start_job_response"]["job"] is not None
):
job_id = start_job_dict["start_job_response"]["job"]
if job_id is None:
self.api_logger.warn(
"Not enough information about start of job to retreive results."
)
return start_job_dict
self.api_logger.debug("Job ID to retreive: {}".format(job_id))
finished_job_dict = copy.deepcopy(start_job_dict)
self._set_request_headers(start_job_dict["request_endpoint"])
if self.api_endpoint in start_job_dict["request_endpoint"]:
job_endpoint = "{}/{}jobs/{}/".format(
self.mmw_host, self.api_endpoint, job_id
)
elif self.old_modeling_endpoint in start_job_dict["request_endpoint"]:
job_endpoint = "{}/{}jobs/{}/".format(
self.mmw_host, self.old_modeling_endpoint, job_id
)
job_results_req = Request("GET", job_endpoint)
job_results_json = {}
is_finished = False
while is_finished == False:
job_results_resp = self._make_mmw_request(job_results_req, ["status"])
job_results_json = job_results_resp["json_response"]
if job_results_resp["succeeded"] == False:
finished_job_dict["error_response"] = job_results_resp["error_response"]
finished_job_dict["job_result_status"] = "failed"
return finished_job_dict
elif (
job_results_json is not None
and "error" in job_results_resp["json_response"].keys()
and job_results_resp["json_response"]["error"] != ""
):
self.api_logger.error(
"\t***ERROR GETTING JOB RESULTS***\n\t{}".format(
job_results_json["error"]
)
)
finished_job_dict["error_response"] = job_results_json
finished_job_dict["job_result_status"] = "failed"
return finished_job_dict
elif job_results_json is None:
self.api_logger.error(
"\t***ERROR GETTING JOB RESULTS***\n\t{}".format(job_results_resp)
)
finished_job_dict["error_response"] = job_results_resp.text
finished_job_dict["job_result_status"] = "failed"
return finished_job_dict
is_finished = job_results_json["status"] == "complete"
if not is_finished:
self.api_logger.debug("ModelMW job has not yet finished.")
time.sleep(0.5)
if "result" in job_results_json.keys() and job_results_json["result"] != "":
finished_job_dict["result_response"] = job_results_json
finished_job_dict["job_result_status"] = "succeeded"
self.api_logger.info(
"\tGot {} results for {}".format(
self._pprint_endpoint(start_job_dict["request_endpoint"]),
start_job_dict["job_label"],
)
)
else:
self.api_logger.warn("\t***Did not get a results key in the job results***")
finished_job_dict["error_response"] = job_results_json
finished_job_dict["job_result_status"] = "failed"
# dump out the whole job for posterity
self.dump_job_json(finished_job_dict)
return finished_job_dict
def run_mmw_job(
self,
request_endpoint: str,
job_label: str,
payload: Union[Dict, None] = None,
) -> ModelMyWatershedJob:
"""Starts a ModelMyWatershed job and waits for and returns the results
Args:
request_endpoint (str): The endpoint for the request
payload (Dict): The payload going to the request.
Either a JSON serializable dictionary or pre-formatted form data.
job_label (str): A label to use to save the output files
Returns:
ModelMyWatershedJob: The job request and result
"""
start_job_dict = self.start_job(
request_endpoint=request_endpoint,
payload=payload,
job_label=job_label,
)
if start_job_dict["start_job_status"] != "succeeded":
self.api_logger.warn(
"\t{} job FAILED for {}".format(
self._pprint_endpoint(start_job_dict["request_endpoint"]),
job_label,
)
)
return copy.deepcopy(start_job_dict)
time.sleep(3.5) # max of 20 requests per minute!
finished_job_dict = copy.deepcopy(self.get_job_result(start_job_dict))
return finished_job_dict
def create_project(
self,
model_package: str,
name: str = "Untitled Project",
area_of_interest: Union[Dict, None] = None,
wkaoi: Union[str, None] = None,
huc: Union[str, None] = None,
mapshed_job_uuid: Union[str, None] = None,
subbasin_mapshed_job_uuid: Union[str, None] = None,
layer_overrides: Union[ModemMyWatershedLayerOverride, None] = None,
) -> Dict:
"""Creates a new project on ModelMyWatershed. At this time, a project
is needed to access some data, including weather projections for a site.
NOTE: Projects created with this function **will not be usable** in the
ModelMyWatershed web app, but they will appear under the projects for your user.
In order not to have many unusable project, I _strongly_ recommend following
any create_project actions with a delete_project action later in your script.
YOU MUST BE LOGGED IN TO USE THIS FEATURE!
Args:
model_package (str): The model package to use.
Must be either "gwlfe" or "tr-55"
name (str): A name for the project, can be any text
area_of_interest (Union[Dict, None]): A geojson dictionary with the
shape of the area of interest for the project. One of the
area_of_interest, wkaoi, or huc must be given.
wkaoi (Union[str, None]): A well known area of interest identifier
for the project area. One of the area_of_interest, wkaoi, or huc
must be given.
huc (Union[str, None]): A HUC identifier code (HUC8, HUC10, or HUC12) for
the project area. One of the area_of_interest, wkaoi, or huc must
be given.
mapshed_job_uuid (str): The UUID for the GWLF-E prepare (MapShed) job tied
to this project, if applicable.
subbasin_mapshed_job_uuid (str): The UUID for the subbasin GWLF-E prepare
(MapShed) job tied to this project, if applicable.
layer_overrides (ModemMyWatershedLayerOverride): Any layer overrides to
use in the project
Returns:
Dict: A dictionary with information about the new project
"""
request_endpoint = self.project_endpoint
self._set_request_headers(request_endpoint)
if huc is None and wkaoi is None and area_of_interest is None:
self.api_logger.error(
"\t***Either a HUC code, an WKAoI, or a geojson is required to create a project!***"
)
return {}
payload = {
"name": name,
"model_package": model_package,
}
if area_of_interest is not None:
payload["area_of_interest"] = area_of_interest
elif huc is not None and huc != "":
payload["huc"] = huc
elif wkaoi is not None and wkaoi != "":
payload["wkaoi"] = wkaoi
if mapshed_job_uuid is not None and mapshed_job_uuid != "":
payload["mapshed_job_uuid"] = mapshed_job_uuid
if subbasin_mapshed_job_uuid is not None and subbasin_mapshed_job_uuid != "":
payload["subbasin_mapshed_job_uuid"] = subbasin_mapshed_job_uuid
if layer_overrides is not None:
payload["layer_overrides"] = layer_overrides
create_project_req: Request = Request(
"POST", "{}/{}".format(self.mmw_host, request_endpoint), json=payload
)
create_project_resp = self._make_mmw_request(create_project_req, ["id"])
if create_project_resp["succeeded"] == True:
return create_project_resp["json_response"]
return {}
def delete_project(
self,
project_id: Union[str, int],
) -> None:
"""Deletes a ModelMW project. If you're using the API to get information based
on API-created skeleton projects, you probably want to be able to delete the
project so as not to clutter up your user data with zillions of project
YOU MUST BE LOGGED IN TO USE THIS FEATURE!
Args:
project_id (Union[str,int]): The project id.
Returns:
None
"""
request_endpoint = self.project_endpoint + "{}".format(project_id)
self._set_request_headers(request_endpoint)
delete_project_req: Request = Request(
"DELETE", "{}/{}".format(self.mmw_host, request_endpoint)
)
self._make_mmw_request(delete_project_req)
def get_project_weather(
self, project_id: Union[str, int], weather_layer: str
) -> Dict:
"""Get weather data for project given a category, if available.
Current categories are NASA_NLDAS_2000_2019, RCP45_2080_2099 and
RCP85_2080_2099, and only support shapes within the DRB. Given a project
within the DRB, we find the N=2 nearest weather stations and
average their values.
Args:
project_id (Union[str,int]): The project id.
weather_layer (str) The weather layer to use, must be one of
"NASA_NLDAS_2000_2019", "RCP45_2080_2099" or "RCP85_2080_2099"
Returns:
Dict: Weather output, ready to be fed into a project GWLF-E modification run
"""
request_endpoint = self.project_endpoint + "{}/weather/{}".format(
project_id, weather_layer
)
self._set_request_headers(request_endpoint)
weather_data_req = Request(
"GET", "{}/{}".format(self.mmw_host, request_endpoint)
)
weather_data_resp = self._make_mmw_request(
weather_data_req, ["output"] # "WxYrBeg"
)
if weather_data_resp["succeeded"] == True:
# # dump out the weather data
# if self.save_path is not None:
# with open(
# self.save_path
# + "{}_weather_{}.json".format(project_id, weather_layer),
# "w",
# ) as fp:
# json.dump(weather_data_resp["json_response"], fp, indent=2)
return weather_data_resp["json_response"]
self.api_logger.error("\t***ERROR GETTING WEATHER DATA***")
return {}
def get_subbasin_details(
self,
mapshed_job_uuid: str,
) -> list:
"""Gets information (geojson, metadata) about the HUC-12 subbasins within the results of a sub-basin preparation (MapShed) request.
Args:
mapshed_job_uuid (str): The UUID for the SUBBASIN GWLF-E prepare (MapShed) job tied to this project, if applicable.
Returns:
list: A list of geojsons for the HUC-12's in the subbasins contained in the MapShed job
"""
request_endpoint = self.old_modeling_endpoint + "subbasins"
self._set_request_headers(request_endpoint)
params = {"mapshed_job_uuid": mapshed_job_uuid}
subbasin_detail_req = Request(
"POST", "{}/{}".format(self.mmw_host, request_endpoint), params=params
)
subbasin_detail_resp = self._make_mmw_request(subbasin_detail_req)
subbasin_detail_resp_json = subbasin_detail_resp["json_response"]
if (
subbasin_detail_resp_json is not None
and type(subbasin_detail_resp_json) is list
and len(subbasin_detail_resp_json) > 0
and (
type(subbasin_detail_resp_json[0]) is dict
or type(subbasin_detail_resp_json[0]) is OrderedDict
)
and "shape" in subbasin_detail_resp_json[0].keys()
and subbasin_detail_resp_json[0]["shape"] is not None
):
self.api_logger.info(
"\tGot information about {} HUC-12 subbasins".format(
len(subbasin_detail_resp_json)
)
)
# dump out the subbasins
# if self.save_path is not None:
# with open(
# self.save_path + mapshed_job_uuid + "_subbasin_geojsons.json",
# "w",
# ) as fp:
# json.dump(subbasin_detail_resp_json, fp, indent=2)
return subbasin_detail_resp_json
self.api_logger.error("\t***ERROR GETTING SUB-BASIN DETAILS***")
return []
def run_batch_analysis(
self, list_of_aois: List, analysis_endpoint: str
) -> pd.DataFrame:
"""Given a list of areas of interest (AOIs), runs all of them for the same analysis endpoint. Depending on the number of site in the list, this may take a very long time to return.
Args:
list_of_aois (List): A list of AOI's. They can be strings or geojsons.
analysis_endpoint (str): The analysis endpoint to use.
Returns:
pd.DataFrame: A pandas data frame with the results from all of the runs.
"""
run_frames = []
run_number: int = 1
for aoi in list_of_aois:
# TODO(SRGDamia1): validate strings
# if it's a string with underscores, we're assuming it's a WKAoI from the hidden well-known area of interest table
# this is not expected, but we'll support it
if isinstance(aoi, str) and "__" in aoi:
job_label = aoi
payload = {"wkaoi": aoi}
# if it doesn't have underscores, we're assuiming it's a HUC
elif isinstance(aoi, str) and (
len(aoi) == 8 or len(aoi) == 10 or len(aoi) == 12
):
job_label = aoi
payload = {"huc": aoi}
# if it's not a string, hopefully it's a valid geojson
# TODO(SRGDamia1): validate geojson! Must be a valid single-ringed Multipolygon GeoJSON representation of the shape to analyze
# NOTE: In order to validate geojson, we'd need to add some sort of geo dependency. I'm not sure if we want to add that.
else:
if (
isinstance(aoi, Dict)
and "properties" in aoi.keys()
and "name" in aoi["properties"].keys
):
job_label = aoi["properties"]["name"]
else:
job_label = "shape_{}".format(run_number)
payload = aoi
try:
req_dump = self.run_mmw_job(
request_endpoint=analysis_endpoint,
job_label=job_label,
payload=payload,
)
res_frame = pd.DataFrame(
copy.deepcopy(
req_dump["result_response"]["result"]["survey"]["categories"]
)
)
except Exception as ex:
self.api_logger.warn("\tUnexpected exception:\n\t{}".format(ex))
continue
res_frame["job_label"] = job_label
res_frame["request_endpoint"] = analysis_endpoint
run_frames.append(res_frame)
run_number += 1
# join all of the frames together into one frame with the batch results
if len(run_frames) > 0:
lu_results = pd.concat(run_frames, ignore_index=True)
return lu_results
return None
def run_batch_gwlfe(
self, list_of_aois: List, layer_overrides: ModemMyWatershedLayerOverride = None