-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.py
140 lines (127 loc) · 5.36 KB
/
script.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
import requests
import logging
import time
from os.path import join
from collections import OrderedDict
import mw_settings as s
import muirweb as mw
logfile = join(s.ROOT_DIR, 'muirweb.log')
logging.basicConfig(filename=logfile,
filemode='w',
format='%(asctime)s %(levelname)s: %(message)s',
level=logging.DEBUG,
datefmt='%Y-%m-%d %H:%M:%S')
report_csv_grid_elements = join(s.ROOT_DIR, 'grid_elements.csv')
report_csv_nogrid_elements = join(s.ROOT_DIR, 'nogrid_elements.csv')
report_csv_element_inputs = join(s.ROOT_DIR, 'element_inputs.csv')
client = requests.session()
headers = mw.api_headers(client)
if headers:
definition_types = client.get('%smw_definition_types/' % s.API, **headers).json()
mw.frequency_types = client.get('%smw_frequency_types/' % s.API, **headers).json()
# states = client.get('%smw_states/' % s.API, **headers).json()
# groups = client.get('%smw_groups/' % s.API, **headers).json()
interaction_types = client.get('%smw_interaction_types/' % s.API, **headers).json()
mw.strength_types = client.get('%smw_strength_types/' % s.API, **headers).json()
# testing data:
test_elements = [
{
"id": 30,
"elementid": "30.00",
"name": "Elevation",
"spatially_explicit": True,
"mapped_manually": True,
"native_units": True,
"subset_rule": "",
"adjacency_rule": None,
"description": "elevation is a function of topography",
"last_modified": "2018-07-08T23:17:56.597733-04:00",
"species": None,
"mw_definition": 1,
"mw_taxontype": 1,
"mw_class": 1,
"aggregationtype": 0,
"frequencytype": 30,
"references": []
},
{
"id": 108,
"elementid": "70.30",
"name": "Mean tide to mean high water",
"spatially_explicit": True,
"mapped_manually": False,
"native_units": False,
"subset_rule": "logical_and([30.00] > -1, [30.00] < 5)",
"adjacency_rule": None,
"description": "Mean tide to mean high water",
"last_modified": None,
"species": None,
"mw_definition": 2,
"mw_taxontype": 1,
"mw_class": 1,
"aggregationtype": 2,
"frequencytype": 31,
"references": []
}
]
test_relationships = [
{
"id": 9139,
"id_subject": "70.30",
"id_object": "30.00",
"notes": "",
"state": 14,
"relationshiptype": 22,
"strengthtype": 3,
"interactiontype": 1
}
]
# mw.relationships = test_relationships
# mw.elements = {e['elementid']: mw.Element(e) for e in test_elements}
raw_elements = client.get('%smw_elements/' % s.API, **headers).json()
sorted_elements = sorted(raw_elements, key=lambda k: float(k['elementid']))
mw.elements = OrderedDict([(e['elementid'], mw.Element(e)) for e in sorted_elements])
mw.relationships = client.get('%smw_relationships/' % s.API, **headers).json()
def map_muirweb(run, initally_mapped):
logging.info('Starting run %s through elements' % run)
mapped = list(initally_mapped)
for elementid in mw.elements:
if (mw.elements[elementid].spatially_explicit is True and
mw.elements[elementid].mapped_manually is False and
mw.elements[elementid].status is False):
if mw.calc_grid(elementid):
logging.info('mapped %s' % elementid)
mapped.append(elementid)
if len(mapped) != len(initally_mapped):
num_mapped = list(set(mapped) - set(initally_mapped))
logging.info('Mapped %s elements: %s' % (len(num_mapped), num_mapped))
map_muirweb(run + 1, mapped)
else:
logging.warning('No additional elements mapped.')
elapsed = time.strftime('%H:%M:%S', time.gmtime(time.time() - start_time))
logging.info('Execution time: {}'.format(elapsed))
start_time = time.time()
# Uncomment to clear out everything not mapped by hand before beginning runs
# mw.clear_automapped()
print('Starting first run: monitor %s for progress' % logfile)
map_muirweb(1, [])
grid_elements = []
nogrid_elements = []
element_inputs_rows = []
for elementid in mw.elements:
mw.elements[elementid].set_relationships()
if mw.elements[elementid].status:
grid_elements.append([elementid])
else:
if mw.elements[elementid].spatially_explicit is True:
nogrid_elements.append([elementid])
grid_inputs = [o.elementid for o in mw.elements[elementid].object_list if o.status is True]
nogrid_inputs = [o.elementid for o in mw.elements[elementid].object_list if o.status is False]
element_inputs_rows.append([elementid, ','.join(grid_inputs), ','.join(nogrid_inputs)])
mw.write_csv(report_csv_grid_elements, ['elementid'], grid_elements)
mw.write_csv(report_csv_nogrid_elements, ['elementid'], nogrid_elements)
mw.write_csv(
report_csv_element_inputs,
['elementid', 'inputs with grids', 'inputs missing grids'],
element_inputs_rows
)